xref: /linux-6.15/init/main.c (revision de462e5f)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/init/main.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  GK 2/5/95  -  Changed to support mounting root fs via NFS
81da177e4SLinus Torvalds  *  Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
91da177e4SLinus Torvalds  *  Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
101da177e4SLinus Torvalds  *  Simplified starting of init:  Michael A. Griffith <[email protected]>
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13ea676e84SAndrew Morton #define DEBUG		/* Enable initcall_debug */
14ea676e84SAndrew Morton 
151da177e4SLinus Torvalds #include <linux/types.h>
168a293be0SPaul Gortmaker #include <linux/extable.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/proc_fs.h>
195c2c5c55SIngo Molnar #include <linux/binfmts.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/syscalls.h>
229b5609fdSIngo Molnar #include <linux/stackprotector.h>
231da177e4SLinus Torvalds #include <linux/string.h>
241da177e4SLinus Torvalds #include <linux/ctype.h>
251da177e4SLinus Torvalds #include <linux/delay.h>
261da177e4SLinus Torvalds #include <linux/ioport.h>
271da177e4SLinus Torvalds #include <linux/init.h>
281da177e4SLinus Torvalds #include <linux/initrd.h>
2957c8a661SMike Rapoport #include <linux/memblock.h>
304a7a16dcSLen Brown #include <linux/acpi.h>
317684b858SMasami Hiramatsu #include <linux/bootconfig.h>
320c688614SNicolas Pitre #include <linux/console.h>
3338b8d208SIngo Molnar #include <linux/nmi.h>
341da177e4SLinus Torvalds #include <linux/percpu.h>
351da177e4SLinus Torvalds #include <linux/kmod.h>
36db64fe02SNick Piggin #include <linux/vmalloc.h>
371da177e4SLinus Torvalds #include <linux/kernel_stat.h>
38d7cd5611SRusty Russell #include <linux/start_kernel.h>
391da177e4SLinus Torvalds #include <linux/security.h>
403d442233SJens Axboe #include <linux/smp.h>
411da177e4SLinus Torvalds #include <linux/profile.h>
421da177e4SLinus Torvalds #include <linux/rcupdate.h>
431da177e4SLinus Torvalds #include <linux/moduleparam.h>
441da177e4SLinus Torvalds #include <linux/kallsyms.h>
451da177e4SLinus Torvalds #include <linux/writeback.h>
461da177e4SLinus Torvalds #include <linux/cpu.h>
471da177e4SLinus Torvalds #include <linux/cpuset.h>
48ddbcc7e8SPaul Menage #include <linux/cgroup.h>
491da177e4SLinus Torvalds #include <linux/efi.h>
50906568c9SThomas Gleixner #include <linux/tick.h>
5178634061SFrederic Weisbecker #include <linux/sched/isolation.h>
526168a702SAndrew Morton #include <linux/interrupt.h>
53c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
54ca74e92bSShailabh Nagar #include <linux/delayacct.h>
551da177e4SLinus Torvalds #include <linux/unistd.h>
563ea056c5SAlexey Dobriyan #include <linux/utsname.h>
571da177e4SLinus Torvalds #include <linux/rmap.h>
581da177e4SLinus Torvalds #include <linux/mempolicy.h>
591da177e4SLinus Torvalds #include <linux/key.h>
60b6cd0b77SAdrian Bunk #include <linux/buffer_head.h>
61eefa864bSJoonsoo Kim #include <linux/page_ext.h>
629a11b49aSIngo Molnar #include <linux/debug_locks.h>
633ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
64fbb9ce95SIngo Molnar #include <linux/lockdep.h>
653c7b4e6bSCatalin Marinas #include <linux/kmemleak.h>
6684d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
674c002c97SGreg Kroah-Hartman #include <linux/device/driver.h>
6873c27992SEric W. Biederman #include <linux/kthread.h>
69e6fe6649SAdrian Bunk #include <linux/sched.h>
701777e463SIngo Molnar #include <linux/sched/init.h>
71a1c9eea9SAdrian Bunk #include <linux/signal.h>
72199f0ca5SAkinobu Mita #include <linux/idr.h>
730b4b3827SJason Wessel #include <linux/kgdb.h>
7468bf21aaSSteven Rostedt #include <linux/ftrace.h>
7522a9d645SArjan van de Ven #include <linux/async.h>
766ae6996aSFeng Tang #include <linux/sfi.h>
772b2af54aSKay Sievers #include <linux/shmem_fs.h>
785a0e3ad6STejun Heo #include <linux/slab.h>
7924a24bb6SPeter Zijlstra #include <linux/perf_event.h>
80a74fb73cSAl Viro #include <linux/ptrace.h>
81aa8c6248SThomas Gleixner #include <linux/pti.h>
82bb813f4cSTejun Heo #include <linux/blkdev.h>
83bb813f4cSTejun Heo #include <linux/elevator.h>
845d2a4e91SPavel Tatashin #include <linux/sched/clock.h>
8529930025SIngo Molnar #include <linux/sched/task.h>
8668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
8765f382fdSFrederic Weisbecker #include <linux/context_tracking.h>
8847d06e53STheodore Ts'o #include <linux/random.h>
897b0b73d7SPrarit Bhargava #include <linux/list.h>
90c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h>
91e149ed2bSAl Viro #include <linux/proc_ns.h>
920ddab1d2SToshi Kani #include <linux/io.h>
9339290b38SAKASHI Takahiro #include <linux/cache.h>
942959a5f7SJinbum Park #include <linux/rodata_test.h>
9533352244SJosh Poimboeuf #include <linux/jump_label.h>
96ae67d58dSMathieu Malaterre #include <linux/mem_encrypt.h>
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #include <asm/io.h>
991da177e4SLinus Torvalds #include <asm/bugs.h>
1001da177e4SLinus Torvalds #include <asm/setup.h>
101a940199fSAndi Kleen #include <asm/sections.h>
10237b73c82SArjan van de Ven #include <asm/cacheflush.h>
1031da177e4SLinus Torvalds 
1044ee7c60dSSteven Rostedt (VMware) #define CREATE_TRACE_POINTS
1054ee7c60dSSteven Rostedt (VMware) #include <trace/events/initcall.h>
1064ee7c60dSSteven Rostedt (VMware) 
107aae5f662SSam Ravnborg static int kernel_init(void *);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds extern void init_IRQ(void);
1101da177e4SLinus Torvalds extern void radix_tree_init(void);
1111da177e4SLinus Torvalds 
1122ce802f6STejun Heo /*
1132ce802f6STejun Heo  * Debug helper: via this flag we know that we are in 'early bootup code'
1142ce802f6STejun Heo  * where only the boot processor is running with IRQ disabled.  This means
1152ce802f6STejun Heo  * two things - IRQ must not be enabled before the flag is cleared and some
1162ce802f6STejun Heo  * operations which are not allowed with IRQ disabled are allowed while the
1172ce802f6STejun Heo  * flag is set.
1182ce802f6STejun Heo  */
1192ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly;
1202ce802f6STejun Heo 
121a6826048SPaul E. McKenney enum system_states system_state __read_mostly;
1221da177e4SLinus Torvalds EXPORT_SYMBOL(system_state);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /*
1251da177e4SLinus Torvalds  * Boot command-line arguments
1261da177e4SLinus Torvalds  */
1271da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
1281da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds extern void time_init(void);
1311da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */
132d2e3192bSJan Beulich void (*__initdata late_time_init)(void);
1331da177e4SLinus Torvalds 
13430d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */
13530d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE];
13630d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */
13730d7e0d4SAlon Bar-Lev char *saved_command_line;
13830d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */
13930d7e0d4SAlon Bar-Lev static char *static_command_line;
14051887d03SMasami Hiramatsu /* Untouched extra command line */
14151887d03SMasami Hiramatsu static char *extra_command_line;
14213199162SMasami Hiramatsu /* Extra init arguments */
14313199162SMasami Hiramatsu static char *extra_init_args;
1441da177e4SLinus Torvalds 
145f61872bbSSteven Rostedt (VMware) #ifdef CONFIG_BOOT_CONFIG
146f61872bbSSteven Rostedt (VMware) /* Is bootconfig on command line? */
147f61872bbSSteven Rostedt (VMware) static bool bootconfig_found;
148f61872bbSSteven Rostedt (VMware) static bool initargs_found;
149f61872bbSSteven Rostedt (VMware) #else
150f61872bbSSteven Rostedt (VMware) # define bootconfig_found false
151f61872bbSSteven Rostedt (VMware) # define initargs_found false
152f61872bbSSteven Rostedt (VMware) #endif
153f61872bbSSteven Rostedt (VMware) 
1541da177e4SLinus Torvalds static char *execute_command;
155ffdfc409SOlof Johansson static char *ramdisk_execute_command;
1561da177e4SLinus Torvalds 
1578b3b2955SJan Beulich /*
158c4b2c0c5SHannes Frederic Sowa  * Used to generate warnings if static_key manipulation functions are used
159c4b2c0c5SHannes Frederic Sowa  * before jump_label_init is called.
160c4b2c0c5SHannes Frederic Sowa  */
161dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly;
162c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized);
163c4b2c0c5SHannes Frederic Sowa 
164c4b2c0c5SHannes Frederic Sowa /*
1658b3b2955SJan Beulich  * If set, this is an indication to the drivers that reset the underlying
1668b3b2955SJan Beulich  * device before going ahead with the initialization otherwise driver might
1678b3b2955SJan Beulich  * rely on the BIOS and skip the reset operation.
1688b3b2955SJan Beulich  *
1698b3b2955SJan Beulich  * This is useful if kernel is booting in an unreliable environment.
170fc454fdcSFrans Klaver  * For ex. kdump situation where previous kernel has crashed, BIOS has been
1718b3b2955SJan Beulich  * skipped and devices will be in unknown state.
1728b3b2955SJan Beulich  */
1738b3b2955SJan Beulich unsigned int reset_devices;
1748b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices);
1751da177e4SLinus Torvalds 
1767e96287dSVivek Goyal static int __init set_reset_devices(char *str)
1777e96287dSVivek Goyal {
1787e96287dSVivek Goyal 	reset_devices = 1;
1797e96287dSVivek Goyal 	return 1;
1807e96287dSVivek Goyal }
1817e96287dSVivek Goyal 
1827e96287dSVivek Goyal __setup("reset_devices", set_reset_devices);
1837e96287dSVivek Goyal 
184d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
185d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1861da177e4SLinus Torvalds static const char *panic_later, *panic_param;
1871da177e4SLinus Torvalds 
188914dcaa8SRusty Russell extern const struct obs_kernel_param __setup_start[], __setup_end[];
1891da177e4SLinus Torvalds 
19031c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line)
1911da177e4SLinus Torvalds {
192914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
19331c025b5SYaowei Bai 	bool had_early_param = false;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	p = __setup_start;
1961da177e4SLinus Torvalds 	do {
1971da177e4SLinus Torvalds 		int n = strlen(p->str);
198b1e4d20cSMichal Schmidt 		if (parameqn(line, p->str, n)) {
1991da177e4SLinus Torvalds 			if (p->early) {
20033df0d19SRusty Russell 				/* Already done in parse_early_param?
20133df0d19SRusty Russell 				 * (Needs exact match on param part).
20233df0d19SRusty Russell 				 * Keep iterating, as we can have early
20333df0d19SRusty Russell 				 * params and __setups of same names 8( */
2041da177e4SLinus Torvalds 				if (line[n] == '\0' || line[n] == '=')
20531c025b5SYaowei Bai 					had_early_param = true;
2061da177e4SLinus Torvalds 			} else if (!p->setup_func) {
207ea676e84SAndrew Morton 				pr_warn("Parameter %s is obsolete, ignored\n",
208ea676e84SAndrew Morton 					p->str);
20931c025b5SYaowei Bai 				return true;
2101da177e4SLinus Torvalds 			} else if (p->setup_func(line + n))
21131c025b5SYaowei Bai 				return true;
2121da177e4SLinus Torvalds 		}
2131da177e4SLinus Torvalds 		p++;
2141da177e4SLinus Torvalds 	} while (p < __setup_end);
21533df0d19SRusty Russell 
21633df0d19SRusty Russell 	return had_early_param;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds /*
2201da177e4SLinus Torvalds  * This should be approx 2 Bo*oMips to start (note initial shift), and will
2211da177e4SLinus Torvalds  * still work even if initially too large, it will just take slightly longer
2221da177e4SLinus Torvalds  */
2231da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12);
2241da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static int __init debug_kernel(char *str)
2271da177e4SLinus Torvalds {
228a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
229f6f21c81SYinghai Lu 	return 0;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static int __init quiet_kernel(char *str)
2331da177e4SLinus Torvalds {
234a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_QUIET;
235f6f21c81SYinghai Lu 	return 0;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238f6f21c81SYinghai Lu early_param("debug", debug_kernel);
239f6f21c81SYinghai Lu early_param("quiet", quiet_kernel);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static int __init loglevel(char *str)
2421da177e4SLinus Torvalds {
243808bf29bSAlexander Sverdlin 	int newlevel;
244808bf29bSAlexander Sverdlin 
245808bf29bSAlexander Sverdlin 	/*
246808bf29bSAlexander Sverdlin 	 * Only update loglevel value when a correct setting was passed,
247808bf29bSAlexander Sverdlin 	 * to prevent blind crashes (when loglevel being set to 0) that
248808bf29bSAlexander Sverdlin 	 * are quite hard to debug
249808bf29bSAlexander Sverdlin 	 */
250808bf29bSAlexander Sverdlin 	if (get_option(&str, &newlevel)) {
251808bf29bSAlexander Sverdlin 		console_loglevel = newlevel;
252d9d4fcfeSAlex Riesen 		return 0;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
255808bf29bSAlexander Sverdlin 	return -EINVAL;
256808bf29bSAlexander Sverdlin }
257808bf29bSAlexander Sverdlin 
258f6f21c81SYinghai Lu early_param("loglevel", loglevel);
2591da177e4SLinus Torvalds 
260*de462e5fSMasami Hiramatsu #ifdef CONFIG_BLK_DEV_INITRD
261*de462e5fSMasami Hiramatsu static void * __init get_boot_config_from_initrd(u32 *_size, u32 *_csum)
262*de462e5fSMasami Hiramatsu {
263*de462e5fSMasami Hiramatsu 	u32 size, csum;
264*de462e5fSMasami Hiramatsu 	char *data;
265*de462e5fSMasami Hiramatsu 	u32 *hdr;
266*de462e5fSMasami Hiramatsu 
267*de462e5fSMasami Hiramatsu 	if (!initrd_end)
268*de462e5fSMasami Hiramatsu 		return NULL;
269*de462e5fSMasami Hiramatsu 
270*de462e5fSMasami Hiramatsu 	data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
271*de462e5fSMasami Hiramatsu 	if (memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
272*de462e5fSMasami Hiramatsu 		return NULL;
273*de462e5fSMasami Hiramatsu 
274*de462e5fSMasami Hiramatsu 	hdr = (u32 *)(data - 8);
275*de462e5fSMasami Hiramatsu 	size = hdr[0];
276*de462e5fSMasami Hiramatsu 	csum = hdr[1];
277*de462e5fSMasami Hiramatsu 
278*de462e5fSMasami Hiramatsu 	data = ((void *)hdr) - size;
279*de462e5fSMasami Hiramatsu 	if ((unsigned long)data < initrd_start) {
280*de462e5fSMasami Hiramatsu 		pr_err("bootconfig size %d is greater than initrd size %ld\n",
281*de462e5fSMasami Hiramatsu 			size, initrd_end - initrd_start);
282*de462e5fSMasami Hiramatsu 		return NULL;
283*de462e5fSMasami Hiramatsu 	}
284*de462e5fSMasami Hiramatsu 
285*de462e5fSMasami Hiramatsu 	/* Remove bootconfig from initramfs/initrd */
286*de462e5fSMasami Hiramatsu 	initrd_end = (unsigned long)data;
287*de462e5fSMasami Hiramatsu 	if (_size)
288*de462e5fSMasami Hiramatsu 		*_size = size;
289*de462e5fSMasami Hiramatsu 	if (_csum)
290*de462e5fSMasami Hiramatsu 		*_csum = csum;
291*de462e5fSMasami Hiramatsu 
292*de462e5fSMasami Hiramatsu 	return data;
293*de462e5fSMasami Hiramatsu }
294*de462e5fSMasami Hiramatsu #else
295*de462e5fSMasami Hiramatsu static void * __init get_boot_config_from_initrd(u32 *_size, u32 *_csum)
296*de462e5fSMasami Hiramatsu {
297*de462e5fSMasami Hiramatsu 	return NULL;
298*de462e5fSMasami Hiramatsu }
299*de462e5fSMasami Hiramatsu #endif
300*de462e5fSMasami Hiramatsu 
3017684b858SMasami Hiramatsu #ifdef CONFIG_BOOT_CONFIG
30251887d03SMasami Hiramatsu 
30351887d03SMasami Hiramatsu char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
30451887d03SMasami Hiramatsu 
30551887d03SMasami Hiramatsu #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
30651887d03SMasami Hiramatsu 
30751887d03SMasami Hiramatsu static int __init xbc_snprint_cmdline(char *buf, size_t size,
30851887d03SMasami Hiramatsu 				      struct xbc_node *root)
30951887d03SMasami Hiramatsu {
31051887d03SMasami Hiramatsu 	struct xbc_node *knode, *vnode;
31151887d03SMasami Hiramatsu 	char *end = buf + size;
31251887d03SMasami Hiramatsu 	const char *val;
31351887d03SMasami Hiramatsu 	int ret;
31451887d03SMasami Hiramatsu 
31551887d03SMasami Hiramatsu 	xbc_node_for_each_key_value(root, knode, val) {
31651887d03SMasami Hiramatsu 		ret = xbc_node_compose_key_after(root, knode,
31751887d03SMasami Hiramatsu 					xbc_namebuf, XBC_KEYLEN_MAX);
31851887d03SMasami Hiramatsu 		if (ret < 0)
31951887d03SMasami Hiramatsu 			return ret;
32051887d03SMasami Hiramatsu 
32151887d03SMasami Hiramatsu 		vnode = xbc_node_get_child(knode);
32288b91371SMasami Hiramatsu 		if (!vnode) {
32388b91371SMasami Hiramatsu 			ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
32451887d03SMasami Hiramatsu 			if (ret < 0)
32551887d03SMasami Hiramatsu 				return ret;
32651887d03SMasami Hiramatsu 			buf += ret;
32751887d03SMasami Hiramatsu 			continue;
32888b91371SMasami Hiramatsu 		}
32951887d03SMasami Hiramatsu 		xbc_array_for_each_value(vnode, val) {
33088b91371SMasami Hiramatsu 			ret = snprintf(buf, rest(buf, end), "%s=\"%s\" ",
33188b91371SMasami Hiramatsu 				       xbc_namebuf, val);
33251887d03SMasami Hiramatsu 			if (ret < 0)
33351887d03SMasami Hiramatsu 				return ret;
33451887d03SMasami Hiramatsu 			buf += ret;
33551887d03SMasami Hiramatsu 		}
33651887d03SMasami Hiramatsu 	}
33751887d03SMasami Hiramatsu 
33851887d03SMasami Hiramatsu 	return buf - (end - size);
33951887d03SMasami Hiramatsu }
34051887d03SMasami Hiramatsu #undef rest
34151887d03SMasami Hiramatsu 
34251887d03SMasami Hiramatsu /* Make an extra command line under given key word */
34351887d03SMasami Hiramatsu static char * __init xbc_make_cmdline(const char *key)
34451887d03SMasami Hiramatsu {
34551887d03SMasami Hiramatsu 	struct xbc_node *root;
34651887d03SMasami Hiramatsu 	char *new_cmdline;
34751887d03SMasami Hiramatsu 	int ret, len = 0;
34851887d03SMasami Hiramatsu 
34951887d03SMasami Hiramatsu 	root = xbc_find_node(key);
35051887d03SMasami Hiramatsu 	if (!root)
35151887d03SMasami Hiramatsu 		return NULL;
35251887d03SMasami Hiramatsu 
35351887d03SMasami Hiramatsu 	/* Count required buffer size */
35451887d03SMasami Hiramatsu 	len = xbc_snprint_cmdline(NULL, 0, root);
35551887d03SMasami Hiramatsu 	if (len <= 0)
35651887d03SMasami Hiramatsu 		return NULL;
35751887d03SMasami Hiramatsu 
35851887d03SMasami Hiramatsu 	new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES);
35951887d03SMasami Hiramatsu 	if (!new_cmdline) {
36051887d03SMasami Hiramatsu 		pr_err("Failed to allocate memory for extra kernel cmdline.\n");
36151887d03SMasami Hiramatsu 		return NULL;
36251887d03SMasami Hiramatsu 	}
36351887d03SMasami Hiramatsu 
36451887d03SMasami Hiramatsu 	ret = xbc_snprint_cmdline(new_cmdline, len + 1, root);
36551887d03SMasami Hiramatsu 	if (ret < 0 || ret > len) {
36651887d03SMasami Hiramatsu 		pr_err("Failed to print extra kernel cmdline.\n");
36751887d03SMasami Hiramatsu 		return NULL;
36851887d03SMasami Hiramatsu 	}
36951887d03SMasami Hiramatsu 
37051887d03SMasami Hiramatsu 	return new_cmdline;
37151887d03SMasami Hiramatsu }
37251887d03SMasami Hiramatsu 
37308d9e686SQiujun Huang static u32 boot_config_checksum(unsigned char *p, u32 size)
3747684b858SMasami Hiramatsu {
3757684b858SMasami Hiramatsu 	u32 ret = 0;
3767684b858SMasami Hiramatsu 
3777684b858SMasami Hiramatsu 	while (size--)
3787684b858SMasami Hiramatsu 		ret += *p++;
3797684b858SMasami Hiramatsu 
3807684b858SMasami Hiramatsu 	return ret;
3817684b858SMasami Hiramatsu }
3827684b858SMasami Hiramatsu 
383f61872bbSSteven Rostedt (VMware) static int __init bootconfig_params(char *param, char *val,
384f61872bbSSteven Rostedt (VMware) 				    const char *unused, void *arg)
385f61872bbSSteven Rostedt (VMware) {
386f61872bbSSteven Rostedt (VMware) 	if (strcmp(param, "bootconfig") == 0) {
387f61872bbSSteven Rostedt (VMware) 		bootconfig_found = true;
388f61872bbSSteven Rostedt (VMware) 	} else if (strcmp(param, "--") == 0) {
389f61872bbSSteven Rostedt (VMware) 		initargs_found = true;
390f61872bbSSteven Rostedt (VMware) 	}
391f61872bbSSteven Rostedt (VMware) 	return 0;
392f61872bbSSteven Rostedt (VMware) }
393f61872bbSSteven Rostedt (VMware) 
3947495e092SSteven Rostedt (VMware) static void __init setup_boot_config(const char *cmdline)
3957684b858SMasami Hiramatsu {
396f61872bbSSteven Rostedt (VMware) 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
39789b74cacSMasami Hiramatsu 	const char *msg;
39889b74cacSMasami Hiramatsu 	int pos;
3997684b858SMasami Hiramatsu 	u32 size, csum;
4007684b858SMasami Hiramatsu 	char *data, *copy;
401a0057403SMasami Hiramatsu 	int ret;
4027684b858SMasami Hiramatsu 
403*de462e5fSMasami Hiramatsu 	data = get_boot_config_from_initrd(&size, &csum);
404*de462e5fSMasami Hiramatsu 	if (!data)
405*de462e5fSMasami Hiramatsu 		goto not_found;
406*de462e5fSMasami Hiramatsu 
407f61872bbSSteven Rostedt (VMware) 	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
408f61872bbSSteven Rostedt (VMware) 	parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL,
409f61872bbSSteven Rostedt (VMware) 		   bootconfig_params);
410f61872bbSSteven Rostedt (VMware) 
411f61872bbSSteven Rostedt (VMware) 	if (!bootconfig_found)
4127684b858SMasami Hiramatsu 		return;
4137684b858SMasami Hiramatsu 
4147495e092SSteven Rostedt (VMware) 	if (size >= XBC_DATA_MAX) {
4157495e092SSteven Rostedt (VMware) 		pr_err("bootconfig size %d greater than max size %d\n",
4167495e092SSteven Rostedt (VMware) 			size, XBC_DATA_MAX);
4177684b858SMasami Hiramatsu 		return;
4187495e092SSteven Rostedt (VMware) 	}
4197684b858SMasami Hiramatsu 
4207495e092SSteven Rostedt (VMware) 	if (boot_config_checksum((unsigned char *)data, size) != csum) {
4217495e092SSteven Rostedt (VMware) 		pr_err("bootconfig checksum failed\n");
4227684b858SMasami Hiramatsu 		return;
4237495e092SSteven Rostedt (VMware) 	}
4247684b858SMasami Hiramatsu 
4257684b858SMasami Hiramatsu 	copy = memblock_alloc(size + 1, SMP_CACHE_BYTES);
4267684b858SMasami Hiramatsu 	if (!copy) {
4277684b858SMasami Hiramatsu 		pr_err("Failed to allocate memory for bootconfig\n");
4287684b858SMasami Hiramatsu 		return;
4297684b858SMasami Hiramatsu 	}
4307684b858SMasami Hiramatsu 
4317684b858SMasami Hiramatsu 	memcpy(copy, data, size);
4327684b858SMasami Hiramatsu 	copy[size] = '\0';
4337684b858SMasami Hiramatsu 
43489b74cacSMasami Hiramatsu 	ret = xbc_init(copy, &msg, &pos);
43589b74cacSMasami Hiramatsu 	if (ret < 0) {
43689b74cacSMasami Hiramatsu 		if (pos < 0)
43789b74cacSMasami Hiramatsu 			pr_err("Failed to init bootconfig: %s.\n", msg);
43889b74cacSMasami Hiramatsu 		else
43989b74cacSMasami Hiramatsu 			pr_err("Failed to parse bootconfig: %s at %d.\n",
44089b74cacSMasami Hiramatsu 				msg, pos);
44189b74cacSMasami Hiramatsu 	} else {
442a0057403SMasami Hiramatsu 		pr_info("Load bootconfig: %d bytes %d nodes\n", size, ret);
44351887d03SMasami Hiramatsu 		/* keys starting with "kernel." are passed via cmdline */
44451887d03SMasami Hiramatsu 		extra_command_line = xbc_make_cmdline("kernel");
44513199162SMasami Hiramatsu 		/* Also, "init." keys are init arguments */
44613199162SMasami Hiramatsu 		extra_init_args = xbc_make_cmdline("init");
44751887d03SMasami Hiramatsu 	}
4487495e092SSteven Rostedt (VMware) 	return;
4497495e092SSteven Rostedt (VMware) not_found:
4507495e092SSteven Rostedt (VMware) 	pr_err("'bootconfig' found on command line, but no bootconfig found\n");
4517684b858SMasami Hiramatsu }
452*de462e5fSMasami Hiramatsu 
4537684b858SMasami Hiramatsu #else
454*de462e5fSMasami Hiramatsu 
455*de462e5fSMasami Hiramatsu static void __init setup_boot_config(const char *cmdline)
456*de462e5fSMasami Hiramatsu {
457*de462e5fSMasami Hiramatsu 	/* Remove bootconfig data from initrd */
458*de462e5fSMasami Hiramatsu 	get_boot_config_from_initrd(NULL, NULL);
459*de462e5fSMasami Hiramatsu }
460d8a953ddSMasami Hiramatsu 
461d8a953ddSMasami Hiramatsu static int __init warn_bootconfig(char *str)
462d8a953ddSMasami Hiramatsu {
463d8a953ddSMasami Hiramatsu 	pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOTCONFIG is not set.\n");
464d8a953ddSMasami Hiramatsu 	return 0;
465d8a953ddSMasami Hiramatsu }
466d8a953ddSMasami Hiramatsu early_param("bootconfig", warn_bootconfig);
467d8a953ddSMasami Hiramatsu 
4687684b858SMasami Hiramatsu #endif
4697684b858SMasami Hiramatsu 
4701da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */
4717e2762e1SArvind Sankar static void __init repair_env_string(char *param, char *val)
472a99cd112SChris Metcalf {
4731da177e4SLinus Torvalds 	if (val) {
4741da177e4SLinus Torvalds 		/* param=val or param="val"? */
4751da177e4SLinus Torvalds 		if (val == param+strlen(param)+1)
4761da177e4SLinus Torvalds 			val[-1] = '=';
4771da177e4SLinus Torvalds 		else if (val == param+strlen(param)+2) {
4781da177e4SLinus Torvalds 			val[-2] = '=';
4791da177e4SLinus Torvalds 			memmove(val-1, val, strlen(val)+1);
4801da177e4SLinus Torvalds 		} else
4811da177e4SLinus Torvalds 			BUG();
4821da177e4SLinus Torvalds 	}
483a99cd112SChris Metcalf }
484a99cd112SChris Metcalf 
48551e158c1SRusty Russell /* Anything after -- gets handed straight to init. */
486ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val,
487ecc86170SLuis R. Rodriguez 			       const char *unused, void *arg)
48851e158c1SRusty Russell {
48951e158c1SRusty Russell 	unsigned int i;
49051e158c1SRusty Russell 
49151e158c1SRusty Russell 	if (panic_later)
49251e158c1SRusty Russell 		return 0;
49351e158c1SRusty Russell 
4947e2762e1SArvind Sankar 	repair_env_string(param, val);
49551e158c1SRusty Russell 
49651e158c1SRusty Russell 	for (i = 0; argv_init[i]; i++) {
49751e158c1SRusty Russell 		if (i == MAX_INIT_ARGS) {
49851e158c1SRusty Russell 			panic_later = "init";
49951e158c1SRusty Russell 			panic_param = param;
50051e158c1SRusty Russell 			return 0;
50151e158c1SRusty Russell 		}
50251e158c1SRusty Russell 	}
50351e158c1SRusty Russell 	argv_init[i] = param;
50451e158c1SRusty Russell 	return 0;
50551e158c1SRusty Russell }
50651e158c1SRusty Russell 
507a99cd112SChris Metcalf /*
508a99cd112SChris Metcalf  * Unknown boot options get handed to init, unless they look like
509a99cd112SChris Metcalf  * unused parameters (modprobe will find them in /proc/cmdline).
510a99cd112SChris Metcalf  */
511ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val,
512ecc86170SLuis R. Rodriguez 				     const char *unused, void *arg)
513a99cd112SChris Metcalf {
514283900e8SArvind Sankar 	size_t len = strlen(param);
515283900e8SArvind Sankar 
5167e2762e1SArvind Sankar 	repair_env_string(param, val);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	/* Handle obsolete-style parameters */
5191da177e4SLinus Torvalds 	if (obsolete_checksetup(param))
5201da177e4SLinus Torvalds 		return 0;
5211da177e4SLinus Torvalds 
522f066a4f6SRusty Russell 	/* Unused module parameter. */
523283900e8SArvind Sankar 	if (strnchr(param, len, '.'))
5241da177e4SLinus Torvalds 		return 0;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (panic_later)
5271da177e4SLinus Torvalds 		return 0;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (val) {
5301da177e4SLinus Torvalds 		/* Environment option */
5311da177e4SLinus Torvalds 		unsigned int i;
5321da177e4SLinus Torvalds 		for (i = 0; envp_init[i]; i++) {
5331da177e4SLinus Torvalds 			if (i == MAX_INIT_ENVS) {
534499a4584STetsuo Handa 				panic_later = "env";
5351da177e4SLinus Torvalds 				panic_param = param;
5361da177e4SLinus Torvalds 			}
537283900e8SArvind Sankar 			if (!strncmp(param, envp_init[i], len+1))
5381da177e4SLinus Torvalds 				break;
5391da177e4SLinus Torvalds 		}
5401da177e4SLinus Torvalds 		envp_init[i] = param;
5411da177e4SLinus Torvalds 	} else {
5421da177e4SLinus Torvalds 		/* Command line option */
5431da177e4SLinus Torvalds 		unsigned int i;
5441da177e4SLinus Torvalds 		for (i = 0; argv_init[i]; i++) {
5451da177e4SLinus Torvalds 			if (i == MAX_INIT_ARGS) {
546499a4584STetsuo Handa 				panic_later = "init";
5471da177e4SLinus Torvalds 				panic_param = param;
5481da177e4SLinus Torvalds 			}
5491da177e4SLinus Torvalds 		}
5501da177e4SLinus Torvalds 		argv_init[i] = param;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 	return 0;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds static int __init init_setup(char *str)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	unsigned int i;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	execute_command = str;
5601da177e4SLinus Torvalds 	/*
5611da177e4SLinus Torvalds 	 * In case LILO is going to boot us with default command line,
5621da177e4SLinus Torvalds 	 * it prepends "auto" before the whole cmdline which makes
5631da177e4SLinus Torvalds 	 * the shell think it should execute a script with such name.
5641da177e4SLinus Torvalds 	 * So we ignore all arguments entered _before_ init=... [MJ]
5651da177e4SLinus Torvalds 	 */
5661da177e4SLinus Torvalds 	for (i = 1; i < MAX_INIT_ARGS; i++)
5671da177e4SLinus Torvalds 		argv_init[i] = NULL;
5681da177e4SLinus Torvalds 	return 1;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds __setup("init=", init_setup);
5711da177e4SLinus Torvalds 
572ffdfc409SOlof Johansson static int __init rdinit_setup(char *str)
573ffdfc409SOlof Johansson {
574ffdfc409SOlof Johansson 	unsigned int i;
575ffdfc409SOlof Johansson 
576ffdfc409SOlof Johansson 	ramdisk_execute_command = str;
577ffdfc409SOlof Johansson 	/* See "auto" comment in init_setup */
578ffdfc409SOlof Johansson 	for (i = 1; i < MAX_INIT_ARGS; i++)
579ffdfc409SOlof Johansson 		argv_init[i] = NULL;
580ffdfc409SOlof Johansson 	return 1;
581ffdfc409SOlof Johansson }
582ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup);
583ffdfc409SOlof Johansson 
5841da177e4SLinus Torvalds #ifndef CONFIG_SMP
58534db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS;
586e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { }
5871da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { }
5881da177e4SLinus Torvalds #endif
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /*
59130d7e0d4SAlon Bar-Lev  * We need to store the untouched command line for future reference.
59230d7e0d4SAlon Bar-Lev  * We also need to store the touched command line since the parameter
59330d7e0d4SAlon Bar-Lev  * parsing is performed in place, and we should allow a component to
59430d7e0d4SAlon Bar-Lev  * store reference of name/value for future reference.
59530d7e0d4SAlon Bar-Lev  */
59630d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line)
59730d7e0d4SAlon Bar-Lev {
59813199162SMasami Hiramatsu 	size_t len, xlen = 0, ilen = 0;
599f5c7310aSMike Rapoport 
60051887d03SMasami Hiramatsu 	if (extra_command_line)
60151887d03SMasami Hiramatsu 		xlen = strlen(extra_command_line);
60213199162SMasami Hiramatsu 	if (extra_init_args)
60313199162SMasami Hiramatsu 		ilen = strlen(extra_init_args) + 4; /* for " -- " */
60451887d03SMasami Hiramatsu 
60551887d03SMasami Hiramatsu 	len = xlen + strlen(boot_command_line) + 1;
606f5c7310aSMike Rapoport 
60713199162SMasami Hiramatsu 	saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
608f5c7310aSMike Rapoport 	if (!saved_command_line)
60913199162SMasami Hiramatsu 		panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
610f5c7310aSMike Rapoport 
611f5c7310aSMike Rapoport 	static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
612f5c7310aSMike Rapoport 	if (!static_command_line)
613f5c7310aSMike Rapoport 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
614f5c7310aSMike Rapoport 
61551887d03SMasami Hiramatsu 	if (xlen) {
61651887d03SMasami Hiramatsu 		/*
61751887d03SMasami Hiramatsu 		 * We have to put extra_command_line before boot command
61851887d03SMasami Hiramatsu 		 * lines because there could be dashes (separator of init
61951887d03SMasami Hiramatsu 		 * command line) in the command lines.
62051887d03SMasami Hiramatsu 		 */
62151887d03SMasami Hiramatsu 		strcpy(saved_command_line, extra_command_line);
62251887d03SMasami Hiramatsu 		strcpy(static_command_line, extra_command_line);
62351887d03SMasami Hiramatsu 	}
62451887d03SMasami Hiramatsu 	strcpy(saved_command_line + xlen, boot_command_line);
62551887d03SMasami Hiramatsu 	strcpy(static_command_line + xlen, command_line);
62613199162SMasami Hiramatsu 
62713199162SMasami Hiramatsu 	if (ilen) {
62813199162SMasami Hiramatsu 		/*
62913199162SMasami Hiramatsu 		 * Append supplemental init boot args to saved_command_line
63013199162SMasami Hiramatsu 		 * so that user can check what command line options passed
63113199162SMasami Hiramatsu 		 * to init.
63213199162SMasami Hiramatsu 		 */
63313199162SMasami Hiramatsu 		len = strlen(saved_command_line);
634f61872bbSSteven Rostedt (VMware) 		if (initargs_found) {
635f61872bbSSteven Rostedt (VMware) 			saved_command_line[len++] = ' ';
636f61872bbSSteven Rostedt (VMware) 		} else {
63713199162SMasami Hiramatsu 			strcpy(saved_command_line + len, " -- ");
63813199162SMasami Hiramatsu 			len += 4;
639f61872bbSSteven Rostedt (VMware) 		}
64013199162SMasami Hiramatsu 
64113199162SMasami Hiramatsu 		strcpy(saved_command_line + len, extra_init_args);
64213199162SMasami Hiramatsu 	}
64330d7e0d4SAlon Bar-Lev }
64430d7e0d4SAlon Bar-Lev 
64530d7e0d4SAlon Bar-Lev /*
6461da177e4SLinus Torvalds  * We need to finalize in a non-__init function or else race conditions
6471da177e4SLinus Torvalds  * between the root thread and the init thread may cause start_kernel to
6481da177e4SLinus Torvalds  * be reaped by free_initmem before the root thread has proceeded to
6491da177e4SLinus Torvalds  * cpu_idle.
6501da177e4SLinus Torvalds  *
6511da177e4SLinus Torvalds  * gcc-3.4 accidentally inlines this function, so use noinline.
6521da177e4SLinus Torvalds  */
6531da177e4SLinus Torvalds 
654b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done);
655b433c3d4SPeter Zijlstra 
65653c99bd6SMartin Schwidefsky noinline void __ref rest_init(void)
6571da177e4SLinus Torvalds {
6588fb12156SThomas Gleixner 	struct task_struct *tsk;
65973c27992SEric W. Biederman 	int pid;
66073c27992SEric W. Biederman 
6617db905e6SPaul E. McKenney 	rcu_scheduler_starting();
662b433c3d4SPeter Zijlstra 	/*
66397158569SPeter Zijlstra 	 * We need to spawn init first so that it obtains pid 1, however
664b433c3d4SPeter Zijlstra 	 * the init task will end up wanting to create kthreads, which, if
665b433c3d4SPeter Zijlstra 	 * we schedule it before we create kthreadd, will OOPS.
666b433c3d4SPeter Zijlstra 	 */
6678fb12156SThomas Gleixner 	pid = kernel_thread(kernel_init, NULL, CLONE_FS);
6688fb12156SThomas Gleixner 	/*
6698fb12156SThomas Gleixner 	 * Pin init on the boot CPU. Task migration is not properly working
6708fb12156SThomas Gleixner 	 * until sched_init_smp() has been run. It will set the allowed
6718fb12156SThomas Gleixner 	 * CPUs for init to the non isolated CPUs.
6728fb12156SThomas Gleixner 	 */
6738fb12156SThomas Gleixner 	rcu_read_lock();
6748fb12156SThomas Gleixner 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
6758fb12156SThomas Gleixner 	set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
6768fb12156SThomas Gleixner 	rcu_read_unlock();
6778fb12156SThomas Gleixner 
6781da177e4SLinus Torvalds 	numa_default_policy();
67973c27992SEric W. Biederman 	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
680d11c563dSPaul E. McKenney 	rcu_read_lock();
6815cd20455SPavel Emelyanov 	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
682d11c563dSPaul E. McKenney 	rcu_read_unlock();
6831c3c5eabSThomas Gleixner 
6841c3c5eabSThomas Gleixner 	/*
6851c3c5eabSThomas Gleixner 	 * Enable might_sleep() and smp_processor_id() checks.
686c1a280b6SThomas Gleixner 	 * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
6871c3c5eabSThomas Gleixner 	 * kernel_thread() would trigger might_sleep() splats. With
6881c3c5eabSThomas Gleixner 	 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
6891c3c5eabSThomas Gleixner 	 * already, but it's stuck on the kthreadd_done completion.
6901c3c5eabSThomas Gleixner 	 */
6911c3c5eabSThomas Gleixner 	system_state = SYSTEM_SCHEDULING;
6921c3c5eabSThomas Gleixner 
693b433c3d4SPeter Zijlstra 	complete(&kthreadd_done);
694f340c0d1SIngo Molnar 
695f340c0d1SIngo Molnar 	/*
696f340c0d1SIngo Molnar 	 * The boot idle thread must execute schedule()
6971df21055SIngo Molnar 	 * at least once to get things moving:
698f340c0d1SIngo Molnar 	 */
699bd2f5536SThomas Gleixner 	schedule_preempt_disabled();
7005bfb5d69SNick Piggin 	/* Call into cpu_idle with preempt disabled */
701a1a04ec3SThomas Gleixner 	cpu_startup_entry(CPUHP_ONLINE);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /* Check for early params. */
705ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val,
706ecc86170SLuis R. Rodriguez 				 const char *unused, void *arg)
7071da177e4SLinus Torvalds {
708914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	for (p = __setup_start; p < __setup_end; p++) {
711b1e4d20cSMichal Schmidt 		if ((p->early && parameq(param, p->str)) ||
71218a8bd94SYinghai Lu 		    (strcmp(param, "console") == 0 &&
71318a8bd94SYinghai Lu 		     strcmp(p->str, "earlycon") == 0)
71418a8bd94SYinghai Lu 		) {
7151da177e4SLinus Torvalds 			if (p->setup_func(val) != 0)
716ea676e84SAndrew Morton 				pr_warn("Malformed early option '%s'\n", param);
7171da177e4SLinus Torvalds 		}
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 	/* We accept everything at this stage. */
7201da177e4SLinus Torvalds 	return 0;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
72313977091SMagnus Damm void __init parse_early_options(char *cmdline)
72413977091SMagnus Damm {
725ecc86170SLuis R. Rodriguez 	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
726ecc86170SLuis R. Rodriguez 		   do_early_param);
72713977091SMagnus Damm }
72813977091SMagnus Damm 
7291da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */
7301da177e4SLinus Torvalds void __init parse_early_param(void)
7311da177e4SLinus Torvalds {
732dd4d9fecSFabian Frederick 	static int done __initdata;
733dd4d9fecSFabian Frederick 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (done)
7361da177e4SLinus Torvalds 		return;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	/* All fall through to do_early_param. */
73930d7e0d4SAlon Bar-Lev 	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
74013977091SMagnus Damm 	parse_early_options(tmp_cmdline);
7411da177e4SLinus Torvalds 	done = 1;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744e7ff3a47SThomas Gleixner void __init __weak arch_post_acpi_subsys_init(void) { }
745e7ff3a47SThomas Gleixner 
746839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void)
747033ab7f8SAndrew Morton {
748033ab7f8SAndrew Morton }
749033ab7f8SAndrew Morton 
750eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE
751b235beeaSLinus Torvalds void __init __weak thread_stack_cache_init(void)
7528c9843e5SBenjamin Herrenschmidt {
7538c9843e5SBenjamin Herrenschmidt }
754eded09ccSDavid Howells #endif
7558c9843e5SBenjamin Herrenschmidt 
756c7753208STom Lendacky void __init __weak mem_encrypt_init(void) { }
757c7753208STom Lendacky 
7584fc19708SNadav Amit void __init __weak poking_init(void) { }
7594fc19708SNadav Amit 
760782de70cSMike Rapoport void __init __weak pgtable_cache_init(void) { }
761caa84136SNadav Amit 
7624e37958dSSteven Rostedt (VMware) bool initcall_debug;
7634e37958dSSteven Rostedt (VMware) core_param(initcall_debug, initcall_debug, bool, 0644);
764b0dc52f1SSteven Rostedt (VMware) 
765b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED
7664e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void);
767b0dc52f1SSteven Rostedt (VMware) #else
768b0dc52f1SSteven Rostedt (VMware) static inline void initcall_debug_enable(void)
769b0dc52f1SSteven Rostedt (VMware) {
770b0dc52f1SSteven Rostedt (VMware) }
771b0dc52f1SSteven Rostedt (VMware) #endif
7724e37958dSSteven Rostedt (VMware) 
77323a5c8cbSAlexander Potapenko /* Report memory auto-initialization states for this boot. */
77423a5c8cbSAlexander Potapenko static void __init report_meminit(void)
77523a5c8cbSAlexander Potapenko {
77623a5c8cbSAlexander Potapenko 	const char *stack;
77723a5c8cbSAlexander Potapenko 
77823a5c8cbSAlexander Potapenko 	if (IS_ENABLED(CONFIG_INIT_STACK_ALL))
77923a5c8cbSAlexander Potapenko 		stack = "all";
78023a5c8cbSAlexander Potapenko 	else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL))
78123a5c8cbSAlexander Potapenko 		stack = "byref_all";
78223a5c8cbSAlexander Potapenko 	else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF))
78323a5c8cbSAlexander Potapenko 		stack = "byref";
78423a5c8cbSAlexander Potapenko 	else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER))
78523a5c8cbSAlexander Potapenko 		stack = "__user";
78623a5c8cbSAlexander Potapenko 	else
78723a5c8cbSAlexander Potapenko 		stack = "off";
78823a5c8cbSAlexander Potapenko 
78923a5c8cbSAlexander Potapenko 	pr_info("mem auto-init: stack:%s, heap alloc:%s, heap free:%s\n",
79023a5c8cbSAlexander Potapenko 		stack, want_init_on_alloc(GFP_KERNEL) ? "on" : "off",
79123a5c8cbSAlexander Potapenko 		want_init_on_free() ? "on" : "off");
79223a5c8cbSAlexander Potapenko 	if (want_init_on_free())
79323a5c8cbSAlexander Potapenko 		pr_info("mem auto-init: clearing system memory may take some time...\n");
79423a5c8cbSAlexander Potapenko }
79523a5c8cbSAlexander Potapenko 
796444f478fSPekka Enberg /*
797444f478fSPekka Enberg  * Set up kernel memory allocators
798444f478fSPekka Enberg  */
799444f478fSPekka Enberg static void __init mm_init(void)
800444f478fSPekka Enberg {
801eefa864bSJoonsoo Kim 	/*
802eefa864bSJoonsoo Kim 	 * page_ext requires contiguous pages,
803eefa864bSJoonsoo Kim 	 * bigger than MAX_ORDER unless SPARSEMEM.
804eefa864bSJoonsoo Kim 	 */
805eefa864bSJoonsoo Kim 	page_ext_init_flatmem();
8068e57f8acSVlastimil Babka 	init_debug_pagealloc();
80723a5c8cbSAlexander Potapenko 	report_meminit();
808444f478fSPekka Enberg 	mem_init();
809444f478fSPekka Enberg 	kmem_cache_init();
810c5665868SCatalin Marinas 	kmemleak_init();
811b35f1819SKirill A. Shutemov 	pgtable_init();
812a9ee3a63SQian Cai 	debug_objects_mem_init();
813444f478fSPekka Enberg 	vmalloc_init();
8140ddab1d2SToshi Kani 	ioremap_huge_init();
815613e396bSThomas Gleixner 	/* Should be run before the first non-init thread is created */
816613e396bSThomas Gleixner 	init_espfix_bsp();
817aa8c6248SThomas Gleixner 	/* Should be run after espfix64 is set up. */
818aa8c6248SThomas Gleixner 	pti_init();
819444f478fSPekka Enberg }
820444f478fSPekka Enberg 
82153c99bd6SMartin Schwidefsky void __init __weak arch_call_rest_init(void)
82253c99bd6SMartin Schwidefsky {
82353c99bd6SMartin Schwidefsky 	rest_init();
82453c99bd6SMartin Schwidefsky }
82553c99bd6SMartin Schwidefsky 
826722a9f92SAndi Kleen asmlinkage __visible void __init start_kernel(void)
8271da177e4SLinus Torvalds {
828dd4d9fecSFabian Frederick 	char *command_line;
829dd4d9fecSFabian Frederick 	char *after_dashes;
830033ab7f8SAndrew Morton 
831d4311ff1SAaron Tomlin 	set_task_stack_end_magic(&init_task);
83273839c5bSMing Lei 	smp_setup_processor_id();
8333ac7fe5aSThomas Gleixner 	debug_objects_early_init();
83442059429SIngo Molnar 
835ddbcc7e8SPaul Menage 	cgroup_init_early();
836fbb9ce95SIngo Molnar 
837fbb9ce95SIngo Molnar 	local_irq_disable();
8382ce802f6STejun Heo 	early_boot_irqs_disabled = true;
839fbb9ce95SIngo Molnar 
840fbb9ce95SIngo Molnar 	/*
8411da177e4SLinus Torvalds 	 * Interrupts are still disabled. Do necessary setups, then
8421b3b3b49SViresh Kumar 	 * enable them.
8431da177e4SLinus Torvalds 	 */
84444fd2299SStas Sergeev 	boot_cpu_init();
8451da177e4SLinus Torvalds 	page_address_init();
846ea676e84SAndrew Morton 	pr_notice("%s", linux_banner);
847e6b1db98SMatthew Garrett 	early_security_init();
8481da177e4SLinus Torvalds 	setup_arch(&command_line);
8497495e092SSteven Rostedt (VMware) 	setup_boot_config(command_line);
85030d7e0d4SAlon Bar-Lev 	setup_command_line(command_line);
851e0982e90SMike Travis 	setup_nr_cpu_ids();
852d6647bdfSTejun Heo 	setup_per_cpu_areas();
85344fd2299SStas Sergeev 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
854b5b1404dSLinus Torvalds 	boot_cpu_hotplug_init();
8551da177e4SLinus Torvalds 
85672675e13SMichal Hocko 	build_all_zonelists(NULL);
85783b519e8SPekka Enberg 	page_alloc_init();
85883b519e8SPekka Enberg 
85951887d03SMasami Hiramatsu 	pr_notice("Kernel command line: %s\n", saved_command_line);
8606041186aSDan Williams 	/* parameters may set static keys */
8616041186aSDan Williams 	jump_label_init();
86283b519e8SPekka Enberg 	parse_early_param();
86351e158c1SRusty Russell 	after_dashes = parse_args("Booting kernel",
86451e158c1SRusty Russell 				  static_command_line, __start___param,
86583b519e8SPekka Enberg 				  __stop___param - __start___param,
866ecc86170SLuis R. Rodriguez 				  -1, -1, NULL, &unknown_bootoption);
8673438cf54SDaniel Thompson 	if (!IS_ERR_OR_NULL(after_dashes))
86851e158c1SRusty Russell 		parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
869ecc86170SLuis R. Rodriguez 			   NULL, set_init_arg);
87013199162SMasami Hiramatsu 	if (extra_init_args)
87113199162SMasami Hiramatsu 		parse_args("Setting extra init args", extra_init_args,
87213199162SMasami Hiramatsu 			   NULL, 0, -1, -1, NULL, set_init_arg);
87397ce2c88SJeremy Fitzhardinge 
87483b519e8SPekka Enberg 	/*
87583b519e8SPekka Enberg 	 * These use large bootmem allocations and must precede
87683b519e8SPekka Enberg 	 * kmem_cache_init()
87783b519e8SPekka Enberg 	 */
878162a7e75SMike Travis 	setup_log_buf(0);
87983b519e8SPekka Enberg 	vfs_caches_init_early();
88083b519e8SPekka Enberg 	sort_main_extable();
88183b519e8SPekka Enberg 	trap_init();
882444f478fSPekka Enberg 	mm_init();
883de03c72cSKOSAKI Motohiro 
884f631718dSSteven Rostedt (VMware) 	ftrace_init();
885f631718dSSteven Rostedt (VMware) 
886e725c731SSteven Rostedt (VMware) 	/* trace_printk can be enabled here */
887e725c731SSteven Rostedt (VMware) 	early_trace_init();
888e725c731SSteven Rostedt (VMware) 
8891da177e4SLinus Torvalds 	/*
8901da177e4SLinus Torvalds 	 * Set up the scheduler prior starting any interrupts (such as the
8911da177e4SLinus Torvalds 	 * timer interrupt). Full topology setup happens at smp_init()
8921da177e4SLinus Torvalds 	 * time - but meanwhile we still have a functioning scheduler.
8931da177e4SLinus Torvalds 	 */
8941da177e4SLinus Torvalds 	sched_init();
8951da177e4SLinus Torvalds 	/*
8961da177e4SLinus Torvalds 	 * Disable preemption - early bootup scheduling is extremely
8971da177e4SLinus Torvalds 	 * fragile until we cpu_idle() for the first time.
8981da177e4SLinus Torvalds 	 */
8991da177e4SLinus Torvalds 	preempt_disable();
900dd4d9fecSFabian Frederick 	if (WARN(!irqs_disabled(),
901dd4d9fecSFabian Frederick 		 "Interrupts were enabled *very* early, fixing it\n"))
902c4a68306SArd van Breemen 		local_irq_disable();
9030a835c4fSMatthew Wilcox 	radix_tree_init();
9043347fa09STejun Heo 
9053347fa09STejun Heo 	/*
9067d229c66STal Shorer 	 * Set up housekeeping before setting up workqueues to allow the unbound
9077d229c66STal Shorer 	 * workqueue to take non-housekeeping into account.
9087d229c66STal Shorer 	 */
9097d229c66STal Shorer 	housekeeping_init();
9107d229c66STal Shorer 
9117d229c66STal Shorer 	/*
9123347fa09STejun Heo 	 * Allow workqueue creation and work item queueing/cancelling
9133347fa09STejun Heo 	 * early.  Work item execution depends on kthreads and starts after
9143347fa09STejun Heo 	 * workqueue_init().
9153347fa09STejun Heo 	 */
9163347fa09STejun Heo 	workqueue_init_early();
9173347fa09STejun Heo 
9181da177e4SLinus Torvalds 	rcu_init();
9195f893b26SSteven Rostedt (Red Hat) 
920e725c731SSteven Rostedt (VMware) 	/* Trace events are available after this */
9215f893b26SSteven Rostedt (Red Hat) 	trace_init();
9225f893b26SSteven Rostedt (Red Hat) 
9234e37958dSSteven Rostedt (VMware) 	if (initcall_debug)
9244e37958dSSteven Rostedt (VMware) 		initcall_debug_enable();
9254e37958dSSteven Rostedt (VMware) 
92665f382fdSFrederic Weisbecker 	context_tracking_init();
9270b8f1efaSYinghai Lu 	/* init some links before init_ISA_irqs() */
9280b8f1efaSYinghai Lu 	early_irq_init();
9291da177e4SLinus Torvalds 	init_IRQ();
930ad2b1353SThomas Gleixner 	tick_init();
931d6dd50e0SLinus Torvalds 	rcu_init_nohz();
9321da177e4SLinus Torvalds 	init_timers();
933c0a31329SThomas Gleixner 	hrtimers_init();
9341da177e4SLinus Torvalds 	softirq_init();
935ad596171Sjohn stultz 	timekeeping_init();
936d5553523SKees Cook 
937d5553523SKees Cook 	/*
938d5553523SKees Cook 	 * For best initial stack canary entropy, prepare it after:
939d5553523SKees Cook 	 * - setup_arch() for any UEFI RNG entropy and boot cmdline access
940d5553523SKees Cook 	 * - timekeeping_init() for ktime entropy used in rand_initialize()
941d5553523SKees Cook 	 * - rand_initialize() to get any arch-specific entropy like RDRAND
942d5553523SKees Cook 	 * - add_latent_entropy() to get any latent entropy
943d5553523SKees Cook 	 * - adding command line entropy
944d5553523SKees Cook 	 */
945d5553523SKees Cook 	rand_initialize();
946d5553523SKees Cook 	add_latent_entropy();
947d5553523SKees Cook 	add_device_randomness(command_line, strlen(command_line));
948d5553523SKees Cook 	boot_init_stack_canary();
949d5553523SKees Cook 
95088fecaa2Sjohn stultz 	time_init();
9519e630205SStephane Eranian 	perf_event_init();
95293e02814SHeiko Carstens 	profile_init();
953d8ad7d11STakao Indoh 	call_function_init();
954f91eb62fSSteven Rostedt 	WARN(!irqs_disabled(), "Interrupts were enabled early\n");
955c3bc8fd6SJoel Fernandes (Google) 
9562ce802f6STejun Heo 	early_boot_irqs_disabled = false;
95793e02814SHeiko Carstens 	local_irq_enable();
958dcce284aSBenjamin Herrenschmidt 
9597e85ee0cSPekka Enberg 	kmem_cache_init_late();
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	/*
9621da177e4SLinus Torvalds 	 * HACK ALERT! This is early. We're enabling the console before
9631da177e4SLinus Torvalds 	 * we've done PCI setups etc, and console_init() must be aware of
9641da177e4SLinus Torvalds 	 * this. But we do want output early, in case something goes wrong.
9651da177e4SLinus Torvalds 	 */
9661da177e4SLinus Torvalds 	console_init();
9671da177e4SLinus Torvalds 	if (panic_later)
968499a4584STetsuo Handa 		panic("Too many boot %s vars at `%s'", panic_later,
969499a4584STetsuo Handa 		      panic_param);
970fbb9ce95SIngo Molnar 
971c3bc8fd6SJoel Fernandes (Google) 	lockdep_init();
972fbb9ce95SIngo Molnar 
9739a11b49aSIngo Molnar 	/*
9749a11b49aSIngo Molnar 	 * Need to run this when irqs are enabled, because it wants
9759a11b49aSIngo Molnar 	 * to self-test [hard/soft]-irqs on/off lock inversion bugs
9769a11b49aSIngo Molnar 	 * too:
9779a11b49aSIngo Molnar 	 */
9789a11b49aSIngo Molnar 	locking_selftest();
9799a11b49aSIngo Molnar 
980c7753208STom Lendacky 	/*
981c7753208STom Lendacky 	 * This needs to be called before any devices perform DMA
982c7753208STom Lendacky 	 * operations that might use the SWIOTLB bounce buffers. It will
983c7753208STom Lendacky 	 * mark the bounce buffers as decrypted so that their usage will
984c7753208STom Lendacky 	 * not cause "plain-text" data to be decrypted when accessed.
985c7753208STom Lendacky 	 */
986c7753208STom Lendacky 	mem_encrypt_init();
987c7753208STom Lendacky 
9881da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
9891da177e4SLinus Torvalds 	if (initrd_start && !initrd_below_start_ok &&
990bd673c7cSGeert Uytterhoeven 	    page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
991ea676e84SAndrew Morton 		pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
992bd673c7cSGeert Uytterhoeven 		    page_to_pfn(virt_to_page((void *)initrd_start)),
993bd673c7cSGeert Uytterhoeven 		    min_low_pfn);
9941da177e4SLinus Torvalds 		initrd_start = 0;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds #endif
997e7c8d5c9SChristoph Lameter 	setup_per_cpu_pageset();
9981da177e4SLinus Torvalds 	numa_policy_init();
9999c71206dSDou Liyang 	acpi_early_init();
10001da177e4SLinus Torvalds 	if (late_time_init)
10011da177e4SLinus Torvalds 		late_time_init();
1002857baa87SPavel Tatashin 	sched_clock_init();
10031da177e4SLinus Torvalds 	calibrate_delay();
100495846ecfSGargi Sharma 	pid_idr_init();
10051da177e4SLinus Torvalds 	anon_vma_init();
100611520e5eSLinus Torvalds #ifdef CONFIG_X86
100783e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES))
100811520e5eSLinus Torvalds 		efi_enter_virtual_mode();
100911520e5eSLinus Torvalds #endif
1010b235beeaSLinus Torvalds 	thread_stack_cache_init();
1011d84f4f99SDavid Howells 	cred_init();
1012ff691f6eSHeinrich Schuchardt 	fork_init();
10131da177e4SLinus Torvalds 	proc_caches_init();
10143ea056c5SAlexey Dobriyan 	uts_ns_init();
10151da177e4SLinus Torvalds 	buffer_init();
10161da177e4SLinus Torvalds 	key_init();
10171da177e4SLinus Torvalds 	security_init();
10180b4b3827SJason Wessel 	dbg_late_init();
10194248b0daSMel Gorman 	vfs_caches_init();
102062906027SNicholas Piggin 	pagecache_init();
10211da177e4SLinus Torvalds 	signals_init();
102209652320SAlexey Dobriyan 	seq_file_init();
10231da177e4SLinus Torvalds 	proc_root_init();
1024e149ed2bSAl Viro 	nsfs_init();
10251da177e4SLinus Torvalds 	cpuset_init();
1026695df213SZefan Li 	cgroup_init();
1027c757249aSShailabh Nagar 	taskstats_init_early();
1028ca74e92bSShailabh Nagar 	delayacct_init();
10291da177e4SLinus Torvalds 
10304fc19708SNadav Amit 	poking_init();
10311da177e4SLinus Torvalds 	check_bugs();
10321da177e4SLinus Torvalds 
1033b064a8faSRafael J. Wysocki 	acpi_subsystem_init();
1034e7ff3a47SThomas Gleixner 	arch_post_acpi_subsys_init();
10356ae6996aSFeng Tang 	sfi_init_late();
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	/* Do the rest non-__init'ed, we're now alive */
103853c99bd6SMartin Schwidefsky 	arch_call_rest_init();
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
1041b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */
1042b99b87f7SPeter Oberparleiter static void __init do_ctors(void)
1043b99b87f7SPeter Oberparleiter {
1044b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
1045196a15b4SH Hartley Sweeten 	ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
1046b99b87f7SPeter Oberparleiter 
1047196a15b4SH Hartley Sweeten 	for (; fn < (ctor_fn_t *) __ctors_end; fn++)
1048196a15b4SH Hartley Sweeten 		(*fn)();
1049b99b87f7SPeter Oberparleiter #endif
1050b99b87f7SPeter Oberparleiter }
1051b99b87f7SPeter Oberparleiter 
10527b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS
10537b0b73d7SPrarit Bhargava struct blacklist_entry {
10547b0b73d7SPrarit Bhargava 	struct list_head next;
10557b0b73d7SPrarit Bhargava 	char *buf;
10567b0b73d7SPrarit Bhargava };
10577b0b73d7SPrarit Bhargava 
10587b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
10597b0b73d7SPrarit Bhargava 
10607b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
10617b0b73d7SPrarit Bhargava {
10627b0b73d7SPrarit Bhargava 	char *str_entry;
10637b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
10647b0b73d7SPrarit Bhargava 
10657b0b73d7SPrarit Bhargava 	/* str argument is a comma-separated list of functions */
10667b0b73d7SPrarit Bhargava 	do {
10677b0b73d7SPrarit Bhargava 		str_entry = strsep(&str, ",");
10687b0b73d7SPrarit Bhargava 		if (str_entry) {
10697b0b73d7SPrarit Bhargava 			pr_debug("blacklisting initcall %s\n", str_entry);
10707e1c4e27SMike Rapoport 			entry = memblock_alloc(sizeof(*entry),
10717e1c4e27SMike Rapoport 					       SMP_CACHE_BYTES);
1072f5c7310aSMike Rapoport 			if (!entry)
1073f5c7310aSMike Rapoport 				panic("%s: Failed to allocate %zu bytes\n",
1074f5c7310aSMike Rapoport 				      __func__, sizeof(*entry));
10757e1c4e27SMike Rapoport 			entry->buf = memblock_alloc(strlen(str_entry) + 1,
10767e1c4e27SMike Rapoport 						    SMP_CACHE_BYTES);
1077f5c7310aSMike Rapoport 			if (!entry->buf)
1078f5c7310aSMike Rapoport 				panic("%s: Failed to allocate %zu bytes\n",
1079f5c7310aSMike Rapoport 				      __func__, strlen(str_entry) + 1);
10807b0b73d7SPrarit Bhargava 			strcpy(entry->buf, str_entry);
10817b0b73d7SPrarit Bhargava 			list_add(&entry->next, &blacklisted_initcalls);
10827b0b73d7SPrarit Bhargava 		}
10837b0b73d7SPrarit Bhargava 	} while (str_entry);
10847b0b73d7SPrarit Bhargava 
10857b0b73d7SPrarit Bhargava 	return 0;
10867b0b73d7SPrarit Bhargava }
10877b0b73d7SPrarit Bhargava 
10887b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
10897b0b73d7SPrarit Bhargava {
10907b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
1091c8cdd2beSRasmus Villemoes 	char fn_name[KSYM_SYMBOL_LEN];
10920fd5ed8dSRasmus Villemoes 	unsigned long addr;
10937b0b73d7SPrarit Bhargava 
1094c8cdd2beSRasmus Villemoes 	if (list_empty(&blacklisted_initcalls))
10957b0b73d7SPrarit Bhargava 		return false;
10967b0b73d7SPrarit Bhargava 
10970fd5ed8dSRasmus Villemoes 	addr = (unsigned long) dereference_function_descriptor(fn);
10980fd5ed8dSRasmus Villemoes 	sprint_symbol_no_offset(fn_name, addr);
1099c8cdd2beSRasmus Villemoes 
1100841c06d7SPrarit Bhargava 	/*
1101841c06d7SPrarit Bhargava 	 * fn will be "function_name [module_name]" where [module_name] is not
1102841c06d7SPrarit Bhargava 	 * displayed for built-in init functions.  Strip off the [module_name].
1103841c06d7SPrarit Bhargava 	 */
1104841c06d7SPrarit Bhargava 	strreplace(fn_name, ' ', '\0');
1105841c06d7SPrarit Bhargava 
1106e6fd1fb3SGeliang Tang 	list_for_each_entry(entry, &blacklisted_initcalls, next) {
11077b0b73d7SPrarit Bhargava 		if (!strcmp(fn_name, entry->buf)) {
11087b0b73d7SPrarit Bhargava 			pr_debug("initcall %s blacklisted\n", fn_name);
11097b0b73d7SPrarit Bhargava 			return true;
11107b0b73d7SPrarit Bhargava 		}
11117b0b73d7SPrarit Bhargava 	}
11127b0b73d7SPrarit Bhargava 
11137b0b73d7SPrarit Bhargava 	return false;
11147b0b73d7SPrarit Bhargava }
11157b0b73d7SPrarit Bhargava #else
11167b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
11177b0b73d7SPrarit Bhargava {
11187b0b73d7SPrarit Bhargava 	pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
11197b0b73d7SPrarit Bhargava 	return 0;
11207b0b73d7SPrarit Bhargava }
11217b0b73d7SPrarit Bhargava 
11227b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
11237b0b73d7SPrarit Bhargava {
11247b0b73d7SPrarit Bhargava 	return false;
11257b0b73d7SPrarit Bhargava }
11267b0b73d7SPrarit Bhargava #endif
11277b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist);
11287b0b73d7SPrarit Bhargava 
11294e37958dSSteven Rostedt (VMware) static __init_or_module void
11304e37958dSSteven Rostedt (VMware) trace_initcall_start_cb(void *data, initcall_t fn)
11311da177e4SLinus Torvalds {
11324e37958dSSteven Rostedt (VMware) 	ktime_t *calltime = (ktime_t *)data;
11331da177e4SLinus Torvalds 
1134d75f773cSSakari Ailus 	printk(KERN_DEBUG "calling  %pS @ %i\n", fn, task_pid_nr(current));
11354e37958dSSteven Rostedt (VMware) 	*calltime = ktime_get();
11364e37958dSSteven Rostedt (VMware) }
11374e37958dSSteven Rostedt (VMware) 
11384e37958dSSteven Rostedt (VMware) static __init_or_module void
11394e37958dSSteven Rostedt (VMware) trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
11404e37958dSSteven Rostedt (VMware) {
11414e37958dSSteven Rostedt (VMware) 	ktime_t *calltime = (ktime_t *)data;
11424e37958dSSteven Rostedt (VMware) 	ktime_t delta, rettime;
11434e37958dSSteven Rostedt (VMware) 	unsigned long long duration;
11444e37958dSSteven Rostedt (VMware) 
114574239072SFrederic Weisbecker 	rettime = ktime_get();
11464e37958dSSteven Rostedt (VMware) 	delta = ktime_sub(rettime, *calltime);
114730dbb20eSAmérico Wang 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1148d75f773cSSakari Ailus 	printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
1149ea676e84SAndrew Morton 		 fn, ret, duration);
11508f0c45cdSIngo Molnar }
11518f0c45cdSIngo Molnar 
11524e37958dSSteven Rostedt (VMware) static ktime_t initcall_calltime;
11534e37958dSSteven Rostedt (VMware) 
1154b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED
11554e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void)
11564e37958dSSteven Rostedt (VMware) {
11574e37958dSSteven Rostedt (VMware) 	int ret;
11584e37958dSSteven Rostedt (VMware) 
11594e37958dSSteven Rostedt (VMware) 	ret = register_trace_initcall_start(trace_initcall_start_cb,
11604e37958dSSteven Rostedt (VMware) 					    &initcall_calltime);
11614e37958dSSteven Rostedt (VMware) 	ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
11624e37958dSSteven Rostedt (VMware) 					      &initcall_calltime);
11634e37958dSSteven Rostedt (VMware) 	WARN(ret, "Failed to register initcall tracepoints\n");
1164c1cda48aSAndrew Morton }
1165b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_start	trace_initcall_start
1166b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_finish	trace_initcall_finish
1167b0dc52f1SSteven Rostedt (VMware) #else
1168b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_start(initcall_t fn)
1169b0dc52f1SSteven Rostedt (VMware) {
1170b0dc52f1SSteven Rostedt (VMware) 	if (!initcall_debug)
1171b0dc52f1SSteven Rostedt (VMware) 		return;
1172b0dc52f1SSteven Rostedt (VMware) 	trace_initcall_start_cb(&initcall_calltime, fn);
1173b0dc52f1SSteven Rostedt (VMware) }
1174b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_finish(initcall_t fn, int ret)
1175b0dc52f1SSteven Rostedt (VMware) {
1176b0dc52f1SSteven Rostedt (VMware) 	if (!initcall_debug)
1177b0dc52f1SSteven Rostedt (VMware) 		return;
1178b0dc52f1SSteven Rostedt (VMware) 	trace_initcall_finish_cb(&initcall_calltime, fn, ret);
1179b0dc52f1SSteven Rostedt (VMware) }
1180b0dc52f1SSteven Rostedt (VMware) #endif /* !TRACEPOINTS_ENABLED */
1181c1cda48aSAndrew Morton 
1182e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn)
118322c5c03bSKevin Winchester {
118422c5c03bSKevin Winchester 	int count = preempt_count();
1185ff1c8facSSteven Rostedt 	char msgbuf[64];
11864e37958dSSteven Rostedt (VMware) 	int ret;
118722c5c03bSKevin Winchester 
11887b0b73d7SPrarit Bhargava 	if (initcall_blacklisted(fn))
11897b0b73d7SPrarit Bhargava 		return -EPERM;
11907b0b73d7SPrarit Bhargava 
1191b0dc52f1SSteven Rostedt (VMware) 	do_trace_initcall_start(fn);
119222c5c03bSKevin Winchester 	ret = fn();
1193b0dc52f1SSteven Rostedt (VMware) 	do_trace_initcall_finish(fn, ret);
119422c5c03bSKevin Winchester 
1195e662e1cfSCyrill Gorcunov 	msgbuf[0] = 0;
1196e662e1cfSCyrill Gorcunov 
11971da177e4SLinus Torvalds 	if (preempt_count() != count) {
1198bf5d770bSSteven Rostedt 		sprintf(msgbuf, "preemption imbalance ");
11994a2b4b22SPeter Zijlstra 		preempt_count_set(count);
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 	if (irqs_disabled()) {
1202a76bfd0dSCyrill Gorcunov 		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
12031da177e4SLinus Torvalds 		local_irq_enable();
12041da177e4SLinus Torvalds 	}
1205d75f773cSSakari Ailus 	WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
120659f9415fSArjan van de Ven 
120738addce8SEmese Revfy 	add_latent_entropy();
120830dbb20eSAmérico Wang 	return ret;
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
1211e0df154fSLinus Torvalds 
12121b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall_start[];
12131b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall0_start[];
12141b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall1_start[];
12151b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall2_start[];
12161b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall3_start[];
12171b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall4_start[];
12181b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall5_start[];
12191b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall6_start[];
12201b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall7_start[];
12211b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall_end[];
1222026cee00SPawel Moll 
12231b1eeca7SArd Biesheuvel static initcall_entry_t *initcall_levels[] __initdata = {
1224026cee00SPawel Moll 	__initcall0_start,
1225026cee00SPawel Moll 	__initcall1_start,
1226026cee00SPawel Moll 	__initcall2_start,
1227026cee00SPawel Moll 	__initcall3_start,
1228026cee00SPawel Moll 	__initcall4_start,
1229026cee00SPawel Moll 	__initcall5_start,
1230026cee00SPawel Moll 	__initcall6_start,
1231026cee00SPawel Moll 	__initcall7_start,
1232026cee00SPawel Moll 	__initcall_end,
1233026cee00SPawel Moll };
1234026cee00SPawel Moll 
123596263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */
12367c8f7193SAlexey Dobriyan static const char *initcall_level_names[] __initdata = {
1237a6fb6012SSteven Rostedt (VMware) 	"pure",
12389fb48c74SJim Cromie 	"core",
12399fb48c74SJim Cromie 	"postcore",
12409fb48c74SJim Cromie 	"arch",
12419fb48c74SJim Cromie 	"subsys",
12429fb48c74SJim Cromie 	"fs",
12439fb48c74SJim Cromie 	"device",
12449fb48c74SJim Cromie 	"late",
1245026cee00SPawel Moll };
1246026cee00SPawel Moll 
12477e2762e1SArvind Sankar static int __init ignore_unknown_bootoption(char *param, char *val,
12487e2762e1SArvind Sankar 			       const char *unused, void *arg)
12497e2762e1SArvind Sankar {
12507e2762e1SArvind Sankar 	return 0;
12517e2762e1SArvind Sankar }
12527e2762e1SArvind Sankar 
12530068c92aSMasami Hiramatsu static void __init do_initcall_level(int level, char *command_line)
1254026cee00SPawel Moll {
12551b1eeca7SArd Biesheuvel 	initcall_entry_t *fn;
1256026cee00SPawel Moll 
1257026cee00SPawel Moll 	parse_args(initcall_level_names[level],
12580068c92aSMasami Hiramatsu 		   command_line, __start___param,
1259026cee00SPawel Moll 		   __stop___param - __start___param,
1260026cee00SPawel Moll 		   level, level,
12617e2762e1SArvind Sankar 		   NULL, ignore_unknown_bootoption);
1262026cee00SPawel Moll 
12634ee7c60dSSteven Rostedt (VMware) 	trace_initcall_level(initcall_level_names[level]);
1264026cee00SPawel Moll 	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
12651b1eeca7SArd Biesheuvel 		do_one_initcall(initcall_from_entry(fn));
1266026cee00SPawel Moll }
1267e0df154fSLinus Torvalds 
1268e0df154fSLinus Torvalds static void __init do_initcalls(void)
1269e0df154fSLinus Torvalds {
1270026cee00SPawel Moll 	int level;
12710068c92aSMasami Hiramatsu 	size_t len = strlen(saved_command_line) + 1;
12720068c92aSMasami Hiramatsu 	char *command_line;
1273e0df154fSLinus Torvalds 
12740068c92aSMasami Hiramatsu 	command_line = kzalloc(len, GFP_KERNEL);
12750068c92aSMasami Hiramatsu 	if (!command_line)
12760068c92aSMasami Hiramatsu 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
12770068c92aSMasami Hiramatsu 
12780068c92aSMasami Hiramatsu 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
12790068c92aSMasami Hiramatsu 		/* Parser modifies command_line, restore it each time */
12800068c92aSMasami Hiramatsu 		strcpy(command_line, saved_command_line);
12810068c92aSMasami Hiramatsu 		do_initcall_level(level, command_line);
12820068c92aSMasami Hiramatsu 	}
12830068c92aSMasami Hiramatsu 
12840068c92aSMasami Hiramatsu 	kfree(command_line);
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds /*
12881da177e4SLinus Torvalds  * Ok, the machine is now initialized. None of the devices
12891da177e4SLinus Torvalds  * have been touched yet, but the CPU subsystem is up and
12901da177e4SLinus Torvalds  * running, and memory and process management works.
12911da177e4SLinus Torvalds  *
12921da177e4SLinus Torvalds  * Now we can finally start doing some real work..
12931da177e4SLinus Torvalds  */
12941da177e4SLinus Torvalds static void __init do_basic_setup(void)
12951da177e4SLinus Torvalds {
1296759ee091SLai Jiangshan 	cpuset_init_smp();
12971da177e4SLinus Torvalds 	driver_init();
1298b04c3afbSEric W. Biederman 	init_irq_proc();
1299b99b87f7SPeter Oberparleiter 	do_ctors();
1300d5767c53SLinus Torvalds 	usermodehelper_enable();
1301b0f84374Swangyanqing 	do_initcalls();
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13047babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void)
1305c2147a50SEduard - Gabriel Munteanu {
13061b1eeca7SArd Biesheuvel 	initcall_entry_t *fn;
1307c2147a50SEduard - Gabriel Munteanu 
13084ee7c60dSSteven Rostedt (VMware) 	trace_initcall_level("early");
1309026cee00SPawel Moll 	for (fn = __initcall_start; fn < __initcall0_start; fn++)
13101b1eeca7SArd Biesheuvel 		do_one_initcall(initcall_from_entry(fn));
1311c2147a50SEduard - Gabriel Munteanu }
1312c2147a50SEduard - Gabriel Munteanu 
1313a74fb73cSAl Viro static int run_init_process(const char *init_filename)
13141da177e4SLinus Torvalds {
1315b88c50acSArvind Sankar 	const char *const *p;
1316b88c50acSArvind Sankar 
13171da177e4SLinus Torvalds 	argv_init[0] = init_filename;
13183f5c15d8SPaul Menzel 	pr_info("Run %s as init process\n", init_filename);
1319b88c50acSArvind Sankar 	pr_debug("  with arguments:\n");
1320b88c50acSArvind Sankar 	for (p = argv_init; *p; p++)
1321b88c50acSArvind Sankar 		pr_debug("    %s\n", *p);
1322b88c50acSArvind Sankar 	pr_debug("  with environment:\n");
1323b88c50acSArvind Sankar 	for (p = envp_init; *p; p++)
1324b88c50acSArvind Sankar 		pr_debug("    %s\n", *p);
1325c4ad8f98SLinus Torvalds 	return do_execve(getname_kernel(init_filename),
1326ae903caaSAl Viro 		(const char __user *const __user *)argv_init,
1327ae903caaSAl Viro 		(const char __user *const __user *)envp_init);
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
1330ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename)
1331ba24762bSMichael Opdenacker {
1332ba24762bSMichael Opdenacker 	int ret;
1333ba24762bSMichael Opdenacker 
1334ba24762bSMichael Opdenacker 	ret = run_init_process(init_filename);
1335ba24762bSMichael Opdenacker 
1336ba24762bSMichael Opdenacker 	if (ret && ret != -ENOENT) {
1337ba24762bSMichael Opdenacker 		pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
1338ba24762bSMichael Opdenacker 		       init_filename, ret);
1339ba24762bSMichael Opdenacker 	}
1340ba24762bSMichael Opdenacker 
1341ba24762bSMichael Opdenacker 	return ret;
1342ba24762bSMichael Opdenacker }
1343ba24762bSMichael Opdenacker 
1344f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void);
1345d6b21238SAl Viro 
13460f5bf6d0SLaura Abbott #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
134739290b38SAKASHI Takahiro bool rodata_enabled __ro_after_init = true;
1348d2aa1acaSKees Cook static int __init set_debug_rodata(char *str)
1349d2aa1acaSKees Cook {
1350d2aa1acaSKees Cook 	return strtobool(str, &rodata_enabled);
1351d2aa1acaSKees Cook }
1352d2aa1acaSKees Cook __setup("rodata=", set_debug_rodata);
135339290b38SAKASHI Takahiro #endif
1354d2aa1acaSKees Cook 
13550f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
1356d2aa1acaSKees Cook static void mark_readonly(void)
1357d2aa1acaSKees Cook {
13582959a5f7SJinbum Park 	if (rodata_enabled) {
1359ae646f0bSJeffrey Hugo 		/*
1360ba180314SPaul E. McKenney 		 * load_module() results in W+X mappings, which are cleaned
1361ba180314SPaul E. McKenney 		 * up with call_rcu().  Let's make sure that queued work is
1362ae646f0bSJeffrey Hugo 		 * flushed so that we don't hit false positives looking for
1363ae646f0bSJeffrey Hugo 		 * insecure pages which are W+X.
1364ae646f0bSJeffrey Hugo 		 */
1365ba180314SPaul E. McKenney 		rcu_barrier();
1366d2aa1acaSKees Cook 		mark_rodata_ro();
13672959a5f7SJinbum Park 		rodata_test();
13682959a5f7SJinbum Park 	} else
1369d2aa1acaSKees Cook 		pr_info("Kernel memory protection disabled.\n");
1370d2aa1acaSKees Cook }
1371f596ded1SChristophe Leroy #elif defined(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)
1372f596ded1SChristophe Leroy static inline void mark_readonly(void)
1373f596ded1SChristophe Leroy {
1374f596ded1SChristophe Leroy 	pr_warn("Kernel memory protection not selected by kernel config.\n");
1375f596ded1SChristophe Leroy }
1376d2aa1acaSKees Cook #else
1377d2aa1acaSKees Cook static inline void mark_readonly(void)
1378d2aa1acaSKees Cook {
1379d2aa1acaSKees Cook 	pr_warn("This architecture does not have kernel memory protection.\n");
1380d2aa1acaSKees Cook }
1381d2aa1acaSKees Cook #endif
1382d2aa1acaSKees Cook 
1383997aef68SMike Rapoport void __weak free_initmem(void)
1384997aef68SMike Rapoport {
1385f4039999SMike Rapoport 	free_initmem_default(POISON_FREE_INITMEM);
1386997aef68SMike Rapoport }
1387997aef68SMike Rapoport 
1388d6b21238SAl Viro static int __ref kernel_init(void *unused)
1389ee5bfa64SVivek Goyal {
1390ba24762bSMichael Opdenacker 	int ret;
1391ba24762bSMichael Opdenacker 
1392d6b21238SAl Viro 	kernel_init_freeable();
139322a9d645SArjan van de Ven 	/* need to finish all async __init code before freeing the memory */
139422a9d645SArjan van de Ven 	async_synchronize_full();
1395b80f0f6cSSteven Rostedt (VMware) 	ftrace_free_init_mem();
1396ee5bfa64SVivek Goyal 	free_initmem();
1397d2aa1acaSKees Cook 	mark_readonly();
1398b976690fSJoerg Roedel 
1399b976690fSJoerg Roedel 	/*
1400b976690fSJoerg Roedel 	 * Kernel mappings are now finalized - update the userspace page-table
1401b976690fSJoerg Roedel 	 * to finalize PTI.
1402b976690fSJoerg Roedel 	 */
1403b976690fSJoerg Roedel 	pti_finalize();
1404b976690fSJoerg Roedel 
1405ee5bfa64SVivek Goyal 	system_state = SYSTEM_RUNNING;
1406ee5bfa64SVivek Goyal 	numa_default_policy();
1407ee5bfa64SVivek Goyal 
1408967dcb8fSPaul E. McKenney 	rcu_end_inkernel_boot();
1409967dcb8fSPaul E. McKenney 
1410ee5bfa64SVivek Goyal 	if (ramdisk_execute_command) {
1411ba24762bSMichael Opdenacker 		ret = run_init_process(ramdisk_execute_command);
1412ba24762bSMichael Opdenacker 		if (!ret)
1413a74fb73cSAl Viro 			return 0;
1414ba24762bSMichael Opdenacker 		pr_err("Failed to execute %s (error %d)\n",
1415ba24762bSMichael Opdenacker 		       ramdisk_execute_command, ret);
1416ee5bfa64SVivek Goyal 	}
1417ee5bfa64SVivek Goyal 
1418ee5bfa64SVivek Goyal 	/*
1419ee5bfa64SVivek Goyal 	 * We try each of these until one succeeds.
1420ee5bfa64SVivek Goyal 	 *
1421ee5bfa64SVivek Goyal 	 * The Bourne shell can be used instead of init if we are
1422ee5bfa64SVivek Goyal 	 * trying to recover a really broken machine.
1423ee5bfa64SVivek Goyal 	 */
1424ee5bfa64SVivek Goyal 	if (execute_command) {
1425ba24762bSMichael Opdenacker 		ret = run_init_process(execute_command);
1426ba24762bSMichael Opdenacker 		if (!ret)
1427a74fb73cSAl Viro 			return 0;
14286ef4536eSAndy Lutomirski 		panic("Requested init %s failed (error %d).",
14296ef4536eSAndy Lutomirski 		      execute_command, ret);
1430ee5bfa64SVivek Goyal 	}
1431ba24762bSMichael Opdenacker 	if (!try_to_run_init_process("/sbin/init") ||
1432ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/etc/init") ||
1433ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/init") ||
1434ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/sh"))
1435a74fb73cSAl Viro 		return 0;
1436ee5bfa64SVivek Goyal 
1437ba24762bSMichael Opdenacker 	panic("No working init found.  Try passing init= option to kernel. "
14388c27ceffSMauro Carvalho Chehab 	      "See Linux Documentation/admin-guide/init.rst for guidance.");
1439ee5bfa64SVivek Goyal }
1440ee5bfa64SVivek Goyal 
1441b49a733dSDominik Brodowski void console_on_rootfs(void)
1442b49a733dSDominik Brodowski {
144374f1a299SDominik Brodowski 	/* Open the /dev/console as stdin, this should never fail */
144474f1a299SDominik Brodowski 	if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
14458243186fSDominik Brodowski 		pr_err("Warning: unable to open an initial console.\n");
144674f1a299SDominik Brodowski 
144774f1a299SDominik Brodowski 	/* create stdout/stderr */
144874f1a299SDominik Brodowski 	(void) ksys_dup(0);
144974f1a299SDominik Brodowski 	(void) ksys_dup(0);
1450b49a733dSDominik Brodowski }
1451b49a733dSDominik Brodowski 
1452f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void)
14531da177e4SLinus Torvalds {
1454b433c3d4SPeter Zijlstra 	/*
1455b433c3d4SPeter Zijlstra 	 * Wait until kthreadd is all set-up.
1456b433c3d4SPeter Zijlstra 	 */
1457b433c3d4SPeter Zijlstra 	wait_for_completion(&kthreadd_done);
145831a67102SLinus Torvalds 
145931a67102SLinus Torvalds 	/* Now the scheduler is fully set up and can do blocking allocations */
146031a67102SLinus Torvalds 	gfp_allowed_mask = __GFP_BITS_MASK;
146131a67102SLinus Torvalds 
146258568d2aSMiao Xie 	/*
146358568d2aSMiao Xie 	 * init can allocate pages on any node
146458568d2aSMiao Xie 	 */
14653c466d46SLai Jiangshan 	set_mems_allowed(node_states[N_MEMORY]);
14661da177e4SLinus Torvalds 
14679ec52099SCedric Le Goater 	cad_pid = task_pid(current);
14689ec52099SCedric Le Goater 
1469ca74a6f8SAndi Kleen 	smp_prepare_cpus(setup_max_cpus);
14701da177e4SLinus Torvalds 
14713347fa09STejun Heo 	workqueue_init();
14723347fa09STejun Heo 
1473597b7305SMichal Hocko 	init_mm_internals();
1474597b7305SMichal Hocko 
14751da177e4SLinus Torvalds 	do_pre_smp_initcalls();
1476004417a6SPeter Zijlstra 	lockup_detector_init();
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	smp_init();
14791da177e4SLinus Torvalds 	sched_init_smp();
14801da177e4SLinus Torvalds 
14810e1cc95bSMel Gorman 	page_alloc_init_late();
14822f1ee091SQian Cai 	/* Initialize page ext after all struct pages are initialized. */
14832f1ee091SQian Cai 	page_ext_init();
14840e1cc95bSMel Gorman 
14851da177e4SLinus Torvalds 	do_basic_setup();
14861da177e4SLinus Torvalds 
1487b49a733dSDominik Brodowski 	console_on_rootfs();
14882bd3a997SEric W. Biederman 
14891da177e4SLinus Torvalds 	/*
14901da177e4SLinus Torvalds 	 * check if there is an early userspace init.  If yes, let it do all
14911da177e4SLinus Torvalds 	 * the work
14921da177e4SLinus Torvalds 	 */
1493ffdfc409SOlof Johansson 
1494ffdfc409SOlof Johansson 	if (!ramdisk_execute_command)
1495ffdfc409SOlof Johansson 		ramdisk_execute_command = "/init";
1496ffdfc409SOlof Johansson 
1497cbfe20f5SDominik Brodowski 	if (ksys_access((const char __user *)
1498cbfe20f5SDominik Brodowski 			ramdisk_execute_command, 0) != 0) {
1499ffdfc409SOlof Johansson 		ramdisk_execute_command = NULL;
15001da177e4SLinus Torvalds 		prepare_namespace();
1501ffdfc409SOlof Johansson 	}
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/*
15041da177e4SLinus Torvalds 	 * Ok, we have completed the initial bootup, and
15051da177e4SLinus Torvalds 	 * we're essentially up and running. Get rid of the
15061da177e4SLinus Torvalds 	 * initmem segments and start the user-mode stuff..
1507c9cd2ce2SDmitry Kasatkin 	 *
1508c9cd2ce2SDmitry Kasatkin 	 * rootfs is available now, try loading the public keys
1509c9cd2ce2SDmitry Kasatkin 	 * and default modules
15101da177e4SLinus Torvalds 	 */
1511bb813f4cSTejun Heo 
1512c9cd2ce2SDmitry Kasatkin 	integrity_load_keys();
15131da177e4SLinus Torvalds }
1514