xref: /linux-6.15/init/main.c (revision ae646f0b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/init/main.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  GK 2/5/95  -  Changed to support mounting root fs via NFS
71da177e4SLinus Torvalds  *  Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
81da177e4SLinus Torvalds  *  Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
91da177e4SLinus Torvalds  *  Simplified starting of init:  Michael A. Griffith <[email protected]>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
12ea676e84SAndrew Morton #define DEBUG		/* Enable initcall_debug */
13ea676e84SAndrew Morton 
141da177e4SLinus Torvalds #include <linux/types.h>
158a293be0SPaul Gortmaker #include <linux/extable.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/proc_fs.h>
185c2c5c55SIngo Molnar #include <linux/binfmts.h>
191da177e4SLinus Torvalds #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/syscalls.h>
219b5609fdSIngo Molnar #include <linux/stackprotector.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/ctype.h>
241da177e4SLinus Torvalds #include <linux/delay.h>
251da177e4SLinus Torvalds #include <linux/ioport.h>
261da177e4SLinus Torvalds #include <linux/init.h>
271da177e4SLinus Torvalds #include <linux/initrd.h>
281da177e4SLinus Torvalds #include <linux/bootmem.h>
294a7a16dcSLen Brown #include <linux/acpi.h>
300c688614SNicolas Pitre #include <linux/console.h>
3138b8d208SIngo Molnar #include <linux/nmi.h>
321da177e4SLinus Torvalds #include <linux/percpu.h>
331da177e4SLinus Torvalds #include <linux/kmod.h>
34db64fe02SNick Piggin #include <linux/vmalloc.h>
351da177e4SLinus Torvalds #include <linux/kernel_stat.h>
36d7cd5611SRusty Russell #include <linux/start_kernel.h>
371da177e4SLinus Torvalds #include <linux/security.h>
383d442233SJens Axboe #include <linux/smp.h>
391da177e4SLinus Torvalds #include <linux/profile.h>
401da177e4SLinus Torvalds #include <linux/rcupdate.h>
411da177e4SLinus Torvalds #include <linux/moduleparam.h>
421da177e4SLinus Torvalds #include <linux/kallsyms.h>
431da177e4SLinus Torvalds #include <linux/writeback.h>
441da177e4SLinus Torvalds #include <linux/cpu.h>
451da177e4SLinus Torvalds #include <linux/cpuset.h>
46ddbcc7e8SPaul Menage #include <linux/cgroup.h>
471da177e4SLinus Torvalds #include <linux/efi.h>
48906568c9SThomas Gleixner #include <linux/tick.h>
4978634061SFrederic Weisbecker #include <linux/sched/isolation.h>
506168a702SAndrew Morton #include <linux/interrupt.h>
51c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
52ca74e92bSShailabh Nagar #include <linux/delayacct.h>
531da177e4SLinus Torvalds #include <linux/unistd.h>
543ea056c5SAlexey Dobriyan #include <linux/utsname.h>
551da177e4SLinus Torvalds #include <linux/rmap.h>
561da177e4SLinus Torvalds #include <linux/mempolicy.h>
571da177e4SLinus Torvalds #include <linux/key.h>
58b6cd0b77SAdrian Bunk #include <linux/buffer_head.h>
59eefa864bSJoonsoo Kim #include <linux/page_ext.h>
609a11b49aSIngo Molnar #include <linux/debug_locks.h>
613ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
62fbb9ce95SIngo Molnar #include <linux/lockdep.h>
633c7b4e6bSCatalin Marinas #include <linux/kmemleak.h>
6484d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
651f21782eSAdrian Bunk #include <linux/device.h>
6673c27992SEric W. Biederman #include <linux/kthread.h>
67e6fe6649SAdrian Bunk #include <linux/sched.h>
681777e463SIngo Molnar #include <linux/sched/init.h>
69a1c9eea9SAdrian Bunk #include <linux/signal.h>
70199f0ca5SAkinobu Mita #include <linux/idr.h>
710b4b3827SJason Wessel #include <linux/kgdb.h>
7268bf21aaSSteven Rostedt #include <linux/ftrace.h>
7322a9d645SArjan van de Ven #include <linux/async.h>
746ae6996aSFeng Tang #include <linux/sfi.h>
752b2af54aSKay Sievers #include <linux/shmem_fs.h>
765a0e3ad6STejun Heo #include <linux/slab.h>
7724a24bb6SPeter Zijlstra #include <linux/perf_event.h>
78a74fb73cSAl Viro #include <linux/ptrace.h>
79aa8c6248SThomas Gleixner #include <linux/pti.h>
80bb813f4cSTejun Heo #include <linux/blkdev.h>
81bb813f4cSTejun Heo #include <linux/elevator.h>
8238ff87f7SStephen Boyd #include <linux/sched_clock.h>
8329930025SIngo Molnar #include <linux/sched/task.h>
8468db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
8565f382fdSFrederic Weisbecker #include <linux/context_tracking.h>
8647d06e53STheodore Ts'o #include <linux/random.h>
877b0b73d7SPrarit Bhargava #include <linux/list.h>
88c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h>
89e149ed2bSAl Viro #include <linux/proc_ns.h>
900ddab1d2SToshi Kani #include <linux/io.h>
9139290b38SAKASHI Takahiro #include <linux/cache.h>
922959a5f7SJinbum Park #include <linux/rodata_test.h>
9333352244SJosh Poimboeuf #include <linux/jump_label.h>
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #include <asm/io.h>
961da177e4SLinus Torvalds #include <asm/bugs.h>
971da177e4SLinus Torvalds #include <asm/setup.h>
98a940199fSAndi Kleen #include <asm/sections.h>
9937b73c82SArjan van de Ven #include <asm/cacheflush.h>
1001da177e4SLinus Torvalds 
1014ee7c60dSSteven Rostedt (VMware) #define CREATE_TRACE_POINTS
1024ee7c60dSSteven Rostedt (VMware) #include <trace/events/initcall.h>
1034ee7c60dSSteven Rostedt (VMware) 
104aae5f662SSam Ravnborg static int kernel_init(void *);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds extern void init_IRQ(void);
107ff691f6eSHeinrich Schuchardt extern void fork_init(void);
1081da177e4SLinus Torvalds extern void radix_tree_init(void);
1091da177e4SLinus Torvalds 
1102ce802f6STejun Heo /*
1112ce802f6STejun Heo  * Debug helper: via this flag we know that we are in 'early bootup code'
1122ce802f6STejun Heo  * where only the boot processor is running with IRQ disabled.  This means
1132ce802f6STejun Heo  * two things - IRQ must not be enabled before the flag is cleared and some
1142ce802f6STejun Heo  * operations which are not allowed with IRQ disabled are allowed while the
1152ce802f6STejun Heo  * flag is set.
1162ce802f6STejun Heo  */
1172ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly;
1182ce802f6STejun Heo 
119a6826048SPaul E. McKenney enum system_states system_state __read_mostly;
1201da177e4SLinus Torvalds EXPORT_SYMBOL(system_state);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  * Boot command-line arguments
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
1261da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds extern void time_init(void);
1291da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */
130d2e3192bSJan Beulich void (*__initdata late_time_init)(void);
1311da177e4SLinus Torvalds 
13230d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */
13330d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE];
13430d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */
13530d7e0d4SAlon Bar-Lev char *saved_command_line;
13630d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */
13730d7e0d4SAlon Bar-Lev static char *static_command_line;
13808746a65SKrzysztof Mazur /* Command line for per-initcall parameter parsing */
13908746a65SKrzysztof Mazur static char *initcall_command_line;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static char *execute_command;
142ffdfc409SOlof Johansson static char *ramdisk_execute_command;
1431da177e4SLinus Torvalds 
1448b3b2955SJan Beulich /*
145c4b2c0c5SHannes Frederic Sowa  * Used to generate warnings if static_key manipulation functions are used
146c4b2c0c5SHannes Frederic Sowa  * before jump_label_init is called.
147c4b2c0c5SHannes Frederic Sowa  */
148dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly;
149c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized);
150c4b2c0c5SHannes Frederic Sowa 
151c4b2c0c5SHannes Frederic Sowa /*
1528b3b2955SJan Beulich  * If set, this is an indication to the drivers that reset the underlying
1538b3b2955SJan Beulich  * device before going ahead with the initialization otherwise driver might
1548b3b2955SJan Beulich  * rely on the BIOS and skip the reset operation.
1558b3b2955SJan Beulich  *
1568b3b2955SJan Beulich  * This is useful if kernel is booting in an unreliable environment.
157fc454fdcSFrans Klaver  * For ex. kdump situation where previous kernel has crashed, BIOS has been
1588b3b2955SJan Beulich  * skipped and devices will be in unknown state.
1598b3b2955SJan Beulich  */
1608b3b2955SJan Beulich unsigned int reset_devices;
1618b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices);
1621da177e4SLinus Torvalds 
1637e96287dSVivek Goyal static int __init set_reset_devices(char *str)
1647e96287dSVivek Goyal {
1657e96287dSVivek Goyal 	reset_devices = 1;
1667e96287dSVivek Goyal 	return 1;
1677e96287dSVivek Goyal }
1687e96287dSVivek Goyal 
1697e96287dSVivek Goyal __setup("reset_devices", set_reset_devices);
1707e96287dSVivek Goyal 
171d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
172d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1731da177e4SLinus Torvalds static const char *panic_later, *panic_param;
1741da177e4SLinus Torvalds 
175914dcaa8SRusty Russell extern const struct obs_kernel_param __setup_start[], __setup_end[];
1761da177e4SLinus Torvalds 
17731c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line)
1781da177e4SLinus Torvalds {
179914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
18031c025b5SYaowei Bai 	bool had_early_param = false;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	p = __setup_start;
1831da177e4SLinus Torvalds 	do {
1841da177e4SLinus Torvalds 		int n = strlen(p->str);
185b1e4d20cSMichal Schmidt 		if (parameqn(line, p->str, n)) {
1861da177e4SLinus Torvalds 			if (p->early) {
18733df0d19SRusty Russell 				/* Already done in parse_early_param?
18833df0d19SRusty Russell 				 * (Needs exact match on param part).
18933df0d19SRusty Russell 				 * Keep iterating, as we can have early
19033df0d19SRusty Russell 				 * params and __setups of same names 8( */
1911da177e4SLinus Torvalds 				if (line[n] == '\0' || line[n] == '=')
19231c025b5SYaowei Bai 					had_early_param = true;
1931da177e4SLinus Torvalds 			} else if (!p->setup_func) {
194ea676e84SAndrew Morton 				pr_warn("Parameter %s is obsolete, ignored\n",
195ea676e84SAndrew Morton 					p->str);
19631c025b5SYaowei Bai 				return true;
1971da177e4SLinus Torvalds 			} else if (p->setup_func(line + n))
19831c025b5SYaowei Bai 				return true;
1991da177e4SLinus Torvalds 		}
2001da177e4SLinus Torvalds 		p++;
2011da177e4SLinus Torvalds 	} while (p < __setup_end);
20233df0d19SRusty Russell 
20333df0d19SRusty Russell 	return had_early_param;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds /*
2071da177e4SLinus Torvalds  * This should be approx 2 Bo*oMips to start (note initial shift), and will
2081da177e4SLinus Torvalds  * still work even if initially too large, it will just take slightly longer
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12);
2111da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static int __init debug_kernel(char *str)
2141da177e4SLinus Torvalds {
215a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
216f6f21c81SYinghai Lu 	return 0;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static int __init quiet_kernel(char *str)
2201da177e4SLinus Torvalds {
221a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_QUIET;
222f6f21c81SYinghai Lu 	return 0;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225f6f21c81SYinghai Lu early_param("debug", debug_kernel);
226f6f21c81SYinghai Lu early_param("quiet", quiet_kernel);
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static int __init loglevel(char *str)
2291da177e4SLinus Torvalds {
230808bf29bSAlexander Sverdlin 	int newlevel;
231808bf29bSAlexander Sverdlin 
232808bf29bSAlexander Sverdlin 	/*
233808bf29bSAlexander Sverdlin 	 * Only update loglevel value when a correct setting was passed,
234808bf29bSAlexander Sverdlin 	 * to prevent blind crashes (when loglevel being set to 0) that
235808bf29bSAlexander Sverdlin 	 * are quite hard to debug
236808bf29bSAlexander Sverdlin 	 */
237808bf29bSAlexander Sverdlin 	if (get_option(&str, &newlevel)) {
238808bf29bSAlexander Sverdlin 		console_loglevel = newlevel;
239d9d4fcfeSAlex Riesen 		return 0;
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 
242808bf29bSAlexander Sverdlin 	return -EINVAL;
243808bf29bSAlexander Sverdlin }
244808bf29bSAlexander Sverdlin 
245f6f21c81SYinghai Lu early_param("loglevel", loglevel);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */
248ecc86170SLuis R. Rodriguez static int __init repair_env_string(char *param, char *val,
249ecc86170SLuis R. Rodriguez 				    const char *unused, void *arg)
250a99cd112SChris Metcalf {
2511da177e4SLinus Torvalds 	if (val) {
2521da177e4SLinus Torvalds 		/* param=val or param="val"? */
2531da177e4SLinus Torvalds 		if (val == param+strlen(param)+1)
2541da177e4SLinus Torvalds 			val[-1] = '=';
2551da177e4SLinus Torvalds 		else if (val == param+strlen(param)+2) {
2561da177e4SLinus Torvalds 			val[-2] = '=';
2571da177e4SLinus Torvalds 			memmove(val-1, val, strlen(val)+1);
2581da177e4SLinus Torvalds 			val--;
2591da177e4SLinus Torvalds 		} else
2601da177e4SLinus Torvalds 			BUG();
2611da177e4SLinus Torvalds 	}
262a99cd112SChris Metcalf 	return 0;
263a99cd112SChris Metcalf }
264a99cd112SChris Metcalf 
26551e158c1SRusty Russell /* Anything after -- gets handed straight to init. */
266ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val,
267ecc86170SLuis R. Rodriguez 			       const char *unused, void *arg)
26851e158c1SRusty Russell {
26951e158c1SRusty Russell 	unsigned int i;
27051e158c1SRusty Russell 
27151e158c1SRusty Russell 	if (panic_later)
27251e158c1SRusty Russell 		return 0;
27351e158c1SRusty Russell 
274ecc86170SLuis R. Rodriguez 	repair_env_string(param, val, unused, NULL);
27551e158c1SRusty Russell 
27651e158c1SRusty Russell 	for (i = 0; argv_init[i]; i++) {
27751e158c1SRusty Russell 		if (i == MAX_INIT_ARGS) {
27851e158c1SRusty Russell 			panic_later = "init";
27951e158c1SRusty Russell 			panic_param = param;
28051e158c1SRusty Russell 			return 0;
28151e158c1SRusty Russell 		}
28251e158c1SRusty Russell 	}
28351e158c1SRusty Russell 	argv_init[i] = param;
28451e158c1SRusty Russell 	return 0;
28551e158c1SRusty Russell }
28651e158c1SRusty Russell 
287a99cd112SChris Metcalf /*
288a99cd112SChris Metcalf  * Unknown boot options get handed to init, unless they look like
289a99cd112SChris Metcalf  * unused parameters (modprobe will find them in /proc/cmdline).
290a99cd112SChris Metcalf  */
291ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val,
292ecc86170SLuis R. Rodriguez 				     const char *unused, void *arg)
293a99cd112SChris Metcalf {
294ecc86170SLuis R. Rodriguez 	repair_env_string(param, val, unused, NULL);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* Handle obsolete-style parameters */
2971da177e4SLinus Torvalds 	if (obsolete_checksetup(param))
2981da177e4SLinus Torvalds 		return 0;
2991da177e4SLinus Torvalds 
300f066a4f6SRusty Russell 	/* Unused module parameter. */
301f066a4f6SRusty Russell 	if (strchr(param, '.') && (!val || strchr(param, '.') < val))
3021da177e4SLinus Torvalds 		return 0;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (panic_later)
3051da177e4SLinus Torvalds 		return 0;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if (val) {
3081da177e4SLinus Torvalds 		/* Environment option */
3091da177e4SLinus Torvalds 		unsigned int i;
3101da177e4SLinus Torvalds 		for (i = 0; envp_init[i]; i++) {
3111da177e4SLinus Torvalds 			if (i == MAX_INIT_ENVS) {
312499a4584STetsuo Handa 				panic_later = "env";
3131da177e4SLinus Torvalds 				panic_param = param;
3141da177e4SLinus Torvalds 			}
3151da177e4SLinus Torvalds 			if (!strncmp(param, envp_init[i], val - param))
3161da177e4SLinus Torvalds 				break;
3171da177e4SLinus Torvalds 		}
3181da177e4SLinus Torvalds 		envp_init[i] = param;
3191da177e4SLinus Torvalds 	} else {
3201da177e4SLinus Torvalds 		/* Command line option */
3211da177e4SLinus Torvalds 		unsigned int i;
3221da177e4SLinus Torvalds 		for (i = 0; argv_init[i]; i++) {
3231da177e4SLinus Torvalds 			if (i == MAX_INIT_ARGS) {
324499a4584STetsuo Handa 				panic_later = "init";
3251da177e4SLinus Torvalds 				panic_param = param;
3261da177e4SLinus Torvalds 			}
3271da177e4SLinus Torvalds 		}
3281da177e4SLinus Torvalds 		argv_init[i] = param;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 	return 0;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds static int __init init_setup(char *str)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	unsigned int i;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	execute_command = str;
3381da177e4SLinus Torvalds 	/*
3391da177e4SLinus Torvalds 	 * In case LILO is going to boot us with default command line,
3401da177e4SLinus Torvalds 	 * it prepends "auto" before the whole cmdline which makes
3411da177e4SLinus Torvalds 	 * the shell think it should execute a script with such name.
3421da177e4SLinus Torvalds 	 * So we ignore all arguments entered _before_ init=... [MJ]
3431da177e4SLinus Torvalds 	 */
3441da177e4SLinus Torvalds 	for (i = 1; i < MAX_INIT_ARGS; i++)
3451da177e4SLinus Torvalds 		argv_init[i] = NULL;
3461da177e4SLinus Torvalds 	return 1;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds __setup("init=", init_setup);
3491da177e4SLinus Torvalds 
350ffdfc409SOlof Johansson static int __init rdinit_setup(char *str)
351ffdfc409SOlof Johansson {
352ffdfc409SOlof Johansson 	unsigned int i;
353ffdfc409SOlof Johansson 
354ffdfc409SOlof Johansson 	ramdisk_execute_command = str;
355ffdfc409SOlof Johansson 	/* See "auto" comment in init_setup */
356ffdfc409SOlof Johansson 	for (i = 1; i < MAX_INIT_ARGS; i++)
357ffdfc409SOlof Johansson 		argv_init[i] = NULL;
358ffdfc409SOlof Johansson 	return 1;
359ffdfc409SOlof Johansson }
360ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup);
361ffdfc409SOlof Johansson 
3621da177e4SLinus Torvalds #ifndef CONFIG_SMP
36334db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS;
364e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { }
3651da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { }
3661da177e4SLinus Torvalds #endif
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /*
36930d7e0d4SAlon Bar-Lev  * We need to store the untouched command line for future reference.
37030d7e0d4SAlon Bar-Lev  * We also need to store the touched command line since the parameter
37130d7e0d4SAlon Bar-Lev  * parsing is performed in place, and we should allow a component to
37230d7e0d4SAlon Bar-Lev  * store reference of name/value for future reference.
37330d7e0d4SAlon Bar-Lev  */
37430d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line)
37530d7e0d4SAlon Bar-Lev {
376098b081bSSantosh Shilimkar 	saved_command_line =
377098b081bSSantosh Shilimkar 		memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
378098b081bSSantosh Shilimkar 	initcall_command_line =
379098b081bSSantosh Shilimkar 		memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
380098b081bSSantosh Shilimkar 	static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
38130d7e0d4SAlon Bar-Lev 	strcpy(saved_command_line, boot_command_line);
38230d7e0d4SAlon Bar-Lev 	strcpy(static_command_line, command_line);
38330d7e0d4SAlon Bar-Lev }
38430d7e0d4SAlon Bar-Lev 
38530d7e0d4SAlon Bar-Lev /*
3861da177e4SLinus Torvalds  * We need to finalize in a non-__init function or else race conditions
3871da177e4SLinus Torvalds  * between the root thread and the init thread may cause start_kernel to
3881da177e4SLinus Torvalds  * be reaped by free_initmem before the root thread has proceeded to
3891da177e4SLinus Torvalds  * cpu_idle.
3901da177e4SLinus Torvalds  *
3911da177e4SLinus Torvalds  * gcc-3.4 accidentally inlines this function, so use noinline.
3921da177e4SLinus Torvalds  */
3931da177e4SLinus Torvalds 
394b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done);
395b433c3d4SPeter Zijlstra 
396bd721ea7SFabian Frederick static noinline void __ref rest_init(void)
3971da177e4SLinus Torvalds {
3988fb12156SThomas Gleixner 	struct task_struct *tsk;
39973c27992SEric W. Biederman 	int pid;
40073c27992SEric W. Biederman 
4017db905e6SPaul E. McKenney 	rcu_scheduler_starting();
402b433c3d4SPeter Zijlstra 	/*
40397158569SPeter Zijlstra 	 * We need to spawn init first so that it obtains pid 1, however
404b433c3d4SPeter Zijlstra 	 * the init task will end up wanting to create kthreads, which, if
405b433c3d4SPeter Zijlstra 	 * we schedule it before we create kthreadd, will OOPS.
406b433c3d4SPeter Zijlstra 	 */
4078fb12156SThomas Gleixner 	pid = kernel_thread(kernel_init, NULL, CLONE_FS);
4088fb12156SThomas Gleixner 	/*
4098fb12156SThomas Gleixner 	 * Pin init on the boot CPU. Task migration is not properly working
4108fb12156SThomas Gleixner 	 * until sched_init_smp() has been run. It will set the allowed
4118fb12156SThomas Gleixner 	 * CPUs for init to the non isolated CPUs.
4128fb12156SThomas Gleixner 	 */
4138fb12156SThomas Gleixner 	rcu_read_lock();
4148fb12156SThomas Gleixner 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
4158fb12156SThomas Gleixner 	set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
4168fb12156SThomas Gleixner 	rcu_read_unlock();
4178fb12156SThomas Gleixner 
4181da177e4SLinus Torvalds 	numa_default_policy();
41973c27992SEric W. Biederman 	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
420d11c563dSPaul E. McKenney 	rcu_read_lock();
4215cd20455SPavel Emelyanov 	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
422d11c563dSPaul E. McKenney 	rcu_read_unlock();
4231c3c5eabSThomas Gleixner 
4241c3c5eabSThomas Gleixner 	/*
4251c3c5eabSThomas Gleixner 	 * Enable might_sleep() and smp_processor_id() checks.
426f142f08bSFlorian La Roche 	 * They cannot be enabled earlier because with CONFIG_PREEMPT=y
4271c3c5eabSThomas Gleixner 	 * kernel_thread() would trigger might_sleep() splats. With
4281c3c5eabSThomas Gleixner 	 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
4291c3c5eabSThomas Gleixner 	 * already, but it's stuck on the kthreadd_done completion.
4301c3c5eabSThomas Gleixner 	 */
4311c3c5eabSThomas Gleixner 	system_state = SYSTEM_SCHEDULING;
4321c3c5eabSThomas Gleixner 
433b433c3d4SPeter Zijlstra 	complete(&kthreadd_done);
434f340c0d1SIngo Molnar 
435f340c0d1SIngo Molnar 	/*
436f340c0d1SIngo Molnar 	 * The boot idle thread must execute schedule()
4371df21055SIngo Molnar 	 * at least once to get things moving:
438f340c0d1SIngo Molnar 	 */
439bd2f5536SThomas Gleixner 	schedule_preempt_disabled();
4405bfb5d69SNick Piggin 	/* Call into cpu_idle with preempt disabled */
441a1a04ec3SThomas Gleixner 	cpu_startup_entry(CPUHP_ONLINE);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /* Check for early params. */
445ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val,
446ecc86170SLuis R. Rodriguez 				 const char *unused, void *arg)
4471da177e4SLinus Torvalds {
448914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	for (p = __setup_start; p < __setup_end; p++) {
451b1e4d20cSMichal Schmidt 		if ((p->early && parameq(param, p->str)) ||
45218a8bd94SYinghai Lu 		    (strcmp(param, "console") == 0 &&
45318a8bd94SYinghai Lu 		     strcmp(p->str, "earlycon") == 0)
45418a8bd94SYinghai Lu 		) {
4551da177e4SLinus Torvalds 			if (p->setup_func(val) != 0)
456ea676e84SAndrew Morton 				pr_warn("Malformed early option '%s'\n", param);
4571da177e4SLinus Torvalds 		}
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 	/* We accept everything at this stage. */
4601da177e4SLinus Torvalds 	return 0;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
46313977091SMagnus Damm void __init parse_early_options(char *cmdline)
46413977091SMagnus Damm {
465ecc86170SLuis R. Rodriguez 	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
466ecc86170SLuis R. Rodriguez 		   do_early_param);
46713977091SMagnus Damm }
46813977091SMagnus Damm 
4691da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */
4701da177e4SLinus Torvalds void __init parse_early_param(void)
4711da177e4SLinus Torvalds {
472dd4d9fecSFabian Frederick 	static int done __initdata;
473dd4d9fecSFabian Frederick 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (done)
4761da177e4SLinus Torvalds 		return;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/* All fall through to do_early_param. */
47930d7e0d4SAlon Bar-Lev 	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
48013977091SMagnus Damm 	parse_early_options(tmp_cmdline);
4811da177e4SLinus Torvalds 	done = 1;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484e7ff3a47SThomas Gleixner void __init __weak arch_post_acpi_subsys_init(void) { }
485e7ff3a47SThomas Gleixner 
486839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void)
487033ab7f8SAndrew Morton {
488033ab7f8SAndrew Morton }
489033ab7f8SAndrew Morton 
490eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE
491b235beeaSLinus Torvalds void __init __weak thread_stack_cache_init(void)
4928c9843e5SBenjamin Herrenschmidt {
4938c9843e5SBenjamin Herrenschmidt }
494eded09ccSDavid Howells #endif
4958c9843e5SBenjamin Herrenschmidt 
496c7753208STom Lendacky void __init __weak mem_encrypt_init(void) { }
497c7753208STom Lendacky 
4984e37958dSSteven Rostedt (VMware) bool initcall_debug;
4994e37958dSSteven Rostedt (VMware) core_param(initcall_debug, initcall_debug, bool, 0644);
500b0dc52f1SSteven Rostedt (VMware) 
501b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED
5024e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void);
503b0dc52f1SSteven Rostedt (VMware) #else
504b0dc52f1SSteven Rostedt (VMware) static inline void initcall_debug_enable(void)
505b0dc52f1SSteven Rostedt (VMware) {
506b0dc52f1SSteven Rostedt (VMware) }
507b0dc52f1SSteven Rostedt (VMware) #endif
5084e37958dSSteven Rostedt (VMware) 
509444f478fSPekka Enberg /*
510444f478fSPekka Enberg  * Set up kernel memory allocators
511444f478fSPekka Enberg  */
512444f478fSPekka Enberg static void __init mm_init(void)
513444f478fSPekka Enberg {
514eefa864bSJoonsoo Kim 	/*
515eefa864bSJoonsoo Kim 	 * page_ext requires contiguous pages,
516eefa864bSJoonsoo Kim 	 * bigger than MAX_ORDER unless SPARSEMEM.
517eefa864bSJoonsoo Kim 	 */
518eefa864bSJoonsoo Kim 	page_ext_init_flatmem();
519444f478fSPekka Enberg 	mem_init();
520444f478fSPekka Enberg 	kmem_cache_init();
521b35f1819SKirill A. Shutemov 	pgtable_init();
522444f478fSPekka Enberg 	vmalloc_init();
5230ddab1d2SToshi Kani 	ioremap_huge_init();
524613e396bSThomas Gleixner 	/* Should be run before the first non-init thread is created */
525613e396bSThomas Gleixner 	init_espfix_bsp();
526aa8c6248SThomas Gleixner 	/* Should be run after espfix64 is set up. */
527aa8c6248SThomas Gleixner 	pti_init();
528444f478fSPekka Enberg }
529444f478fSPekka Enberg 
530722a9f92SAndi Kleen asmlinkage __visible void __init start_kernel(void)
5311da177e4SLinus Torvalds {
532dd4d9fecSFabian Frederick 	char *command_line;
533dd4d9fecSFabian Frederick 	char *after_dashes;
534033ab7f8SAndrew Morton 
535d4311ff1SAaron Tomlin 	set_task_stack_end_magic(&init_task);
53673839c5bSMing Lei 	smp_setup_processor_id();
5373ac7fe5aSThomas Gleixner 	debug_objects_early_init();
53842059429SIngo Molnar 
539ddbcc7e8SPaul Menage 	cgroup_init_early();
540fbb9ce95SIngo Molnar 
541fbb9ce95SIngo Molnar 	local_irq_disable();
5422ce802f6STejun Heo 	early_boot_irqs_disabled = true;
543fbb9ce95SIngo Molnar 
544fbb9ce95SIngo Molnar 	/*
5451da177e4SLinus Torvalds 	 * Interrupts are still disabled. Do necessary setups, then
5461b3b3b49SViresh Kumar 	 * enable them.
5471da177e4SLinus Torvalds 	 */
54844fd2299SStas Sergeev 	boot_cpu_init();
5491da177e4SLinus Torvalds 	page_address_init();
550ea676e84SAndrew Morton 	pr_notice("%s", linux_banner);
5511da177e4SLinus Torvalds 	setup_arch(&command_line);
552121388a3SLaura Abbott 	/*
553121388a3SLaura Abbott 	 * Set up the the initial canary and entropy after arch
55433d72f38SDaniel Micay 	 * and after adding latent and command line entropy.
555121388a3SLaura Abbott 	 */
556121388a3SLaura Abbott 	add_latent_entropy();
55733d72f38SDaniel Micay 	add_device_randomness(command_line, strlen(command_line));
558121388a3SLaura Abbott 	boot_init_stack_canary();
5596345d24dSLinus Torvalds 	mm_init_cpumask(&init_mm);
56030d7e0d4SAlon Bar-Lev 	setup_command_line(command_line);
561e0982e90SMike Travis 	setup_nr_cpu_ids();
562d6647bdfSTejun Heo 	setup_per_cpu_areas();
563cff7d378SThomas Gleixner 	boot_cpu_state_init();
56444fd2299SStas Sergeev 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
5651da177e4SLinus Torvalds 
56672675e13SMichal Hocko 	build_all_zonelists(NULL);
56783b519e8SPekka Enberg 	page_alloc_init();
56883b519e8SPekka Enberg 
569ea676e84SAndrew Morton 	pr_notice("Kernel command line: %s\n", boot_command_line);
57083b519e8SPekka Enberg 	parse_early_param();
57151e158c1SRusty Russell 	after_dashes = parse_args("Booting kernel",
57251e158c1SRusty Russell 				  static_command_line, __start___param,
57383b519e8SPekka Enberg 				  __stop___param - __start___param,
574ecc86170SLuis R. Rodriguez 				  -1, -1, NULL, &unknown_bootoption);
5753438cf54SDaniel Thompson 	if (!IS_ERR_OR_NULL(after_dashes))
57651e158c1SRusty Russell 		parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
577ecc86170SLuis R. Rodriguez 			   NULL, set_init_arg);
57897ce2c88SJeremy Fitzhardinge 
57997ce2c88SJeremy Fitzhardinge 	jump_label_init();
58097ce2c88SJeremy Fitzhardinge 
58183b519e8SPekka Enberg 	/*
58283b519e8SPekka Enberg 	 * These use large bootmem allocations and must precede
58383b519e8SPekka Enberg 	 * kmem_cache_init()
58483b519e8SPekka Enberg 	 */
585162a7e75SMike Travis 	setup_log_buf(0);
58683b519e8SPekka Enberg 	vfs_caches_init_early();
58783b519e8SPekka Enberg 	sort_main_extable();
58883b519e8SPekka Enberg 	trap_init();
589444f478fSPekka Enberg 	mm_init();
590de03c72cSKOSAKI Motohiro 
591f631718dSSteven Rostedt (VMware) 	ftrace_init();
592f631718dSSteven Rostedt (VMware) 
593e725c731SSteven Rostedt (VMware) 	/* trace_printk can be enabled here */
594e725c731SSteven Rostedt (VMware) 	early_trace_init();
595e725c731SSteven Rostedt (VMware) 
5961da177e4SLinus Torvalds 	/*
5971da177e4SLinus Torvalds 	 * Set up the scheduler prior starting any interrupts (such as the
5981da177e4SLinus Torvalds 	 * timer interrupt). Full topology setup happens at smp_init()
5991da177e4SLinus Torvalds 	 * time - but meanwhile we still have a functioning scheduler.
6001da177e4SLinus Torvalds 	 */
6011da177e4SLinus Torvalds 	sched_init();
6021da177e4SLinus Torvalds 	/*
6031da177e4SLinus Torvalds 	 * Disable preemption - early bootup scheduling is extremely
6041da177e4SLinus Torvalds 	 * fragile until we cpu_idle() for the first time.
6051da177e4SLinus Torvalds 	 */
6061da177e4SLinus Torvalds 	preempt_disable();
607dd4d9fecSFabian Frederick 	if (WARN(!irqs_disabled(),
608dd4d9fecSFabian Frederick 		 "Interrupts were enabled *very* early, fixing it\n"))
609c4a68306SArd van Breemen 		local_irq_disable();
6100a835c4fSMatthew Wilcox 	radix_tree_init();
6113347fa09STejun Heo 
6123347fa09STejun Heo 	/*
6137d229c66STal Shorer 	 * Set up housekeeping before setting up workqueues to allow the unbound
6147d229c66STal Shorer 	 * workqueue to take non-housekeeping into account.
6157d229c66STal Shorer 	 */
6167d229c66STal Shorer 	housekeeping_init();
6177d229c66STal Shorer 
6187d229c66STal Shorer 	/*
6193347fa09STejun Heo 	 * Allow workqueue creation and work item queueing/cancelling
6203347fa09STejun Heo 	 * early.  Work item execution depends on kthreads and starts after
6213347fa09STejun Heo 	 * workqueue_init().
6223347fa09STejun Heo 	 */
6233347fa09STejun Heo 	workqueue_init_early();
6243347fa09STejun Heo 
6251da177e4SLinus Torvalds 	rcu_init();
6265f893b26SSteven Rostedt (Red Hat) 
627e725c731SSteven Rostedt (VMware) 	/* Trace events are available after this */
6285f893b26SSteven Rostedt (Red Hat) 	trace_init();
6295f893b26SSteven Rostedt (Red Hat) 
6304e37958dSSteven Rostedt (VMware) 	if (initcall_debug)
6314e37958dSSteven Rostedt (VMware) 		initcall_debug_enable();
6324e37958dSSteven Rostedt (VMware) 
63365f382fdSFrederic Weisbecker 	context_tracking_init();
6340b8f1efaSYinghai Lu 	/* init some links before init_ISA_irqs() */
6350b8f1efaSYinghai Lu 	early_irq_init();
6361da177e4SLinus Torvalds 	init_IRQ();
637ad2b1353SThomas Gleixner 	tick_init();
638d6dd50e0SLinus Torvalds 	rcu_init_nohz();
6391da177e4SLinus Torvalds 	init_timers();
640c0a31329SThomas Gleixner 	hrtimers_init();
6411da177e4SLinus Torvalds 	softirq_init();
642ad596171Sjohn stultz 	timekeeping_init();
64388fecaa2Sjohn stultz 	time_init();
64438ff87f7SStephen Boyd 	sched_clock_postinit();
645f92bac3bSSergey Senozhatsky 	printk_safe_init();
6469e630205SStephane Eranian 	perf_event_init();
64793e02814SHeiko Carstens 	profile_init();
648d8ad7d11STakao Indoh 	call_function_init();
649f91eb62fSSteven Rostedt 	WARN(!irqs_disabled(), "Interrupts were enabled early\n");
6502ce802f6STejun Heo 	early_boot_irqs_disabled = false;
65193e02814SHeiko Carstens 	local_irq_enable();
652dcce284aSBenjamin Herrenschmidt 
6537e85ee0cSPekka Enberg 	kmem_cache_init_late();
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	/*
6561da177e4SLinus Torvalds 	 * HACK ALERT! This is early. We're enabling the console before
6571da177e4SLinus Torvalds 	 * we've done PCI setups etc, and console_init() must be aware of
6581da177e4SLinus Torvalds 	 * this. But we do want output early, in case something goes wrong.
6591da177e4SLinus Torvalds 	 */
6601da177e4SLinus Torvalds 	console_init();
6611da177e4SLinus Torvalds 	if (panic_later)
662499a4584STetsuo Handa 		panic("Too many boot %s vars at `%s'", panic_later,
663499a4584STetsuo Handa 		      panic_param);
664fbb9ce95SIngo Molnar 
665fbb9ce95SIngo Molnar 	lockdep_info();
666fbb9ce95SIngo Molnar 
6679a11b49aSIngo Molnar 	/*
6689a11b49aSIngo Molnar 	 * Need to run this when irqs are enabled, because it wants
6699a11b49aSIngo Molnar 	 * to self-test [hard/soft]-irqs on/off lock inversion bugs
6709a11b49aSIngo Molnar 	 * too:
6719a11b49aSIngo Molnar 	 */
6729a11b49aSIngo Molnar 	locking_selftest();
6739a11b49aSIngo Molnar 
674c7753208STom Lendacky 	/*
675c7753208STom Lendacky 	 * This needs to be called before any devices perform DMA
676c7753208STom Lendacky 	 * operations that might use the SWIOTLB bounce buffers. It will
677c7753208STom Lendacky 	 * mark the bounce buffers as decrypted so that their usage will
678c7753208STom Lendacky 	 * not cause "plain-text" data to be decrypted when accessed.
679c7753208STom Lendacky 	 */
680c7753208STom Lendacky 	mem_encrypt_init();
681c7753208STom Lendacky 
6821da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
6831da177e4SLinus Torvalds 	if (initrd_start && !initrd_below_start_ok &&
684bd673c7cSGeert Uytterhoeven 	    page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
685ea676e84SAndrew Morton 		pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
686bd673c7cSGeert Uytterhoeven 		    page_to_pfn(virt_to_page((void *)initrd_start)),
687bd673c7cSGeert Uytterhoeven 		    min_low_pfn);
6881da177e4SLinus Torvalds 		initrd_start = 0;
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds #endif
691fe53ca54SYang Shi 	page_ext_init();
6929b090f2dSCatalin Marinas 	kmemleak_init();
693caba4cbbSWaiman Long 	debug_objects_mem_init();
694e7c8d5c9SChristoph Lameter 	setup_per_cpu_pageset();
6951da177e4SLinus Torvalds 	numa_policy_init();
6969c71206dSDou Liyang 	acpi_early_init();
6971da177e4SLinus Torvalds 	if (late_time_init)
6981da177e4SLinus Torvalds 		late_time_init();
6991da177e4SLinus Torvalds 	calibrate_delay();
70095846ecfSGargi Sharma 	pid_idr_init();
7011da177e4SLinus Torvalds 	anon_vma_init();
70211520e5eSLinus Torvalds #ifdef CONFIG_X86
70383e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES))
70411520e5eSLinus Torvalds 		efi_enter_virtual_mode();
70511520e5eSLinus Torvalds #endif
706b235beeaSLinus Torvalds 	thread_stack_cache_init();
707d84f4f99SDavid Howells 	cred_init();
708ff691f6eSHeinrich Schuchardt 	fork_init();
7091da177e4SLinus Torvalds 	proc_caches_init();
7103ea056c5SAlexey Dobriyan 	uts_ns_init();
7111da177e4SLinus Torvalds 	buffer_init();
7121da177e4SLinus Torvalds 	key_init();
7131da177e4SLinus Torvalds 	security_init();
7140b4b3827SJason Wessel 	dbg_late_init();
7154248b0daSMel Gorman 	vfs_caches_init();
71662906027SNicholas Piggin 	pagecache_init();
7171da177e4SLinus Torvalds 	signals_init();
71809652320SAlexey Dobriyan 	seq_file_init();
7191da177e4SLinus Torvalds 	proc_root_init();
720e149ed2bSAl Viro 	nsfs_init();
7211da177e4SLinus Torvalds 	cpuset_init();
722695df213SZefan Li 	cgroup_init();
723c757249aSShailabh Nagar 	taskstats_init_early();
724ca74e92bSShailabh Nagar 	delayacct_init();
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	check_bugs();
7271da177e4SLinus Torvalds 
728b064a8faSRafael J. Wysocki 	acpi_subsystem_init();
729e7ff3a47SThomas Gleixner 	arch_post_acpi_subsys_init();
7306ae6996aSFeng Tang 	sfi_init_late();
7311da177e4SLinus Torvalds 
73283e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES)) {
73378510792SJosh Triplett 		efi_free_boot_services();
7342223af38SJosh Triplett 	}
73578510792SJosh Triplett 
7361da177e4SLinus Torvalds 	/* Do the rest non-__init'ed, we're now alive */
7371da177e4SLinus Torvalds 	rest_init();
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
740b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */
741b99b87f7SPeter Oberparleiter static void __init do_ctors(void)
742b99b87f7SPeter Oberparleiter {
743b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
744196a15b4SH Hartley Sweeten 	ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
745b99b87f7SPeter Oberparleiter 
746196a15b4SH Hartley Sweeten 	for (; fn < (ctor_fn_t *) __ctors_end; fn++)
747196a15b4SH Hartley Sweeten 		(*fn)();
748b99b87f7SPeter Oberparleiter #endif
749b99b87f7SPeter Oberparleiter }
750b99b87f7SPeter Oberparleiter 
7517b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS
7527b0b73d7SPrarit Bhargava struct blacklist_entry {
7537b0b73d7SPrarit Bhargava 	struct list_head next;
7547b0b73d7SPrarit Bhargava 	char *buf;
7557b0b73d7SPrarit Bhargava };
7567b0b73d7SPrarit Bhargava 
7577b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
7587b0b73d7SPrarit Bhargava 
7597b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
7607b0b73d7SPrarit Bhargava {
7617b0b73d7SPrarit Bhargava 	char *str_entry;
7627b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
7637b0b73d7SPrarit Bhargava 
7647b0b73d7SPrarit Bhargava 	/* str argument is a comma-separated list of functions */
7657b0b73d7SPrarit Bhargava 	do {
7667b0b73d7SPrarit Bhargava 		str_entry = strsep(&str, ",");
7677b0b73d7SPrarit Bhargava 		if (str_entry) {
7687b0b73d7SPrarit Bhargava 			pr_debug("blacklisting initcall %s\n", str_entry);
7697b0b73d7SPrarit Bhargava 			entry = alloc_bootmem(sizeof(*entry));
7707b0b73d7SPrarit Bhargava 			entry->buf = alloc_bootmem(strlen(str_entry) + 1);
7717b0b73d7SPrarit Bhargava 			strcpy(entry->buf, str_entry);
7727b0b73d7SPrarit Bhargava 			list_add(&entry->next, &blacklisted_initcalls);
7737b0b73d7SPrarit Bhargava 		}
7747b0b73d7SPrarit Bhargava 	} while (str_entry);
7757b0b73d7SPrarit Bhargava 
7767b0b73d7SPrarit Bhargava 	return 0;
7777b0b73d7SPrarit Bhargava }
7787b0b73d7SPrarit Bhargava 
7797b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
7807b0b73d7SPrarit Bhargava {
7817b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
782c8cdd2beSRasmus Villemoes 	char fn_name[KSYM_SYMBOL_LEN];
7830fd5ed8dSRasmus Villemoes 	unsigned long addr;
7847b0b73d7SPrarit Bhargava 
785c8cdd2beSRasmus Villemoes 	if (list_empty(&blacklisted_initcalls))
7867b0b73d7SPrarit Bhargava 		return false;
7877b0b73d7SPrarit Bhargava 
7880fd5ed8dSRasmus Villemoes 	addr = (unsigned long) dereference_function_descriptor(fn);
7890fd5ed8dSRasmus Villemoes 	sprint_symbol_no_offset(fn_name, addr);
790c8cdd2beSRasmus Villemoes 
791841c06d7SPrarit Bhargava 	/*
792841c06d7SPrarit Bhargava 	 * fn will be "function_name [module_name]" where [module_name] is not
793841c06d7SPrarit Bhargava 	 * displayed for built-in init functions.  Strip off the [module_name].
794841c06d7SPrarit Bhargava 	 */
795841c06d7SPrarit Bhargava 	strreplace(fn_name, ' ', '\0');
796841c06d7SPrarit Bhargava 
797e6fd1fb3SGeliang Tang 	list_for_each_entry(entry, &blacklisted_initcalls, next) {
7987b0b73d7SPrarit Bhargava 		if (!strcmp(fn_name, entry->buf)) {
7997b0b73d7SPrarit Bhargava 			pr_debug("initcall %s blacklisted\n", fn_name);
8007b0b73d7SPrarit Bhargava 			return true;
8017b0b73d7SPrarit Bhargava 		}
8027b0b73d7SPrarit Bhargava 	}
8037b0b73d7SPrarit Bhargava 
8047b0b73d7SPrarit Bhargava 	return false;
8057b0b73d7SPrarit Bhargava }
8067b0b73d7SPrarit Bhargava #else
8077b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
8087b0b73d7SPrarit Bhargava {
8097b0b73d7SPrarit Bhargava 	pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
8107b0b73d7SPrarit Bhargava 	return 0;
8117b0b73d7SPrarit Bhargava }
8127b0b73d7SPrarit Bhargava 
8137b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
8147b0b73d7SPrarit Bhargava {
8157b0b73d7SPrarit Bhargava 	return false;
8167b0b73d7SPrarit Bhargava }
8177b0b73d7SPrarit Bhargava #endif
8187b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist);
8197b0b73d7SPrarit Bhargava 
8204e37958dSSteven Rostedt (VMware) static __init_or_module void
8214e37958dSSteven Rostedt (VMware) trace_initcall_start_cb(void *data, initcall_t fn)
8221da177e4SLinus Torvalds {
8234e37958dSSteven Rostedt (VMware) 	ktime_t *calltime = (ktime_t *)data;
8241da177e4SLinus Torvalds 
825d62cf815SAndrew Morton 	printk(KERN_DEBUG "calling  %pF @ %i\n", fn, task_pid_nr(current));
8264e37958dSSteven Rostedt (VMware) 	*calltime = ktime_get();
8274e37958dSSteven Rostedt (VMware) }
8284e37958dSSteven Rostedt (VMware) 
8294e37958dSSteven Rostedt (VMware) static __init_or_module void
8304e37958dSSteven Rostedt (VMware) trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
8314e37958dSSteven Rostedt (VMware) {
8324e37958dSSteven Rostedt (VMware) 	ktime_t *calltime = (ktime_t *)data;
8334e37958dSSteven Rostedt (VMware) 	ktime_t delta, rettime;
8344e37958dSSteven Rostedt (VMware) 	unsigned long long duration;
8354e37958dSSteven Rostedt (VMware) 
83674239072SFrederic Weisbecker 	rettime = ktime_get();
8374e37958dSSteven Rostedt (VMware) 	delta = ktime_sub(rettime, *calltime);
83830dbb20eSAmérico Wang 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
839d62cf815SAndrew Morton 	printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
840ea676e84SAndrew Morton 		 fn, ret, duration);
8418f0c45cdSIngo Molnar }
8428f0c45cdSIngo Molnar 
8434e37958dSSteven Rostedt (VMware) static ktime_t initcall_calltime;
8444e37958dSSteven Rostedt (VMware) 
845b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED
8464e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void)
8474e37958dSSteven Rostedt (VMware) {
8484e37958dSSteven Rostedt (VMware) 	int ret;
8494e37958dSSteven Rostedt (VMware) 
8504e37958dSSteven Rostedt (VMware) 	ret = register_trace_initcall_start(trace_initcall_start_cb,
8514e37958dSSteven Rostedt (VMware) 					    &initcall_calltime);
8524e37958dSSteven Rostedt (VMware) 	ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
8534e37958dSSteven Rostedt (VMware) 					      &initcall_calltime);
8544e37958dSSteven Rostedt (VMware) 	WARN(ret, "Failed to register initcall tracepoints\n");
855c1cda48aSAndrew Morton }
856b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_start	trace_initcall_start
857b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_finish	trace_initcall_finish
858b0dc52f1SSteven Rostedt (VMware) #else
859b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_start(initcall_t fn)
860b0dc52f1SSteven Rostedt (VMware) {
861b0dc52f1SSteven Rostedt (VMware) 	if (!initcall_debug)
862b0dc52f1SSteven Rostedt (VMware) 		return;
863b0dc52f1SSteven Rostedt (VMware) 	trace_initcall_start_cb(&initcall_calltime, fn);
864b0dc52f1SSteven Rostedt (VMware) }
865b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_finish(initcall_t fn, int ret)
866b0dc52f1SSteven Rostedt (VMware) {
867b0dc52f1SSteven Rostedt (VMware) 	if (!initcall_debug)
868b0dc52f1SSteven Rostedt (VMware) 		return;
869b0dc52f1SSteven Rostedt (VMware) 	trace_initcall_finish_cb(&initcall_calltime, fn, ret);
870b0dc52f1SSteven Rostedt (VMware) }
871b0dc52f1SSteven Rostedt (VMware) #endif /* !TRACEPOINTS_ENABLED */
872c1cda48aSAndrew Morton 
873e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn)
87422c5c03bSKevin Winchester {
87522c5c03bSKevin Winchester 	int count = preempt_count();
876ff1c8facSSteven Rostedt 	char msgbuf[64];
8774e37958dSSteven Rostedt (VMware) 	int ret;
87822c5c03bSKevin Winchester 
8797b0b73d7SPrarit Bhargava 	if (initcall_blacklisted(fn))
8807b0b73d7SPrarit Bhargava 		return -EPERM;
8817b0b73d7SPrarit Bhargava 
882b0dc52f1SSteven Rostedt (VMware) 	do_trace_initcall_start(fn);
88322c5c03bSKevin Winchester 	ret = fn();
884b0dc52f1SSteven Rostedt (VMware) 	do_trace_initcall_finish(fn, ret);
88522c5c03bSKevin Winchester 
886e662e1cfSCyrill Gorcunov 	msgbuf[0] = 0;
887e662e1cfSCyrill Gorcunov 
8881da177e4SLinus Torvalds 	if (preempt_count() != count) {
889bf5d770bSSteven Rostedt 		sprintf(msgbuf, "preemption imbalance ");
8904a2b4b22SPeter Zijlstra 		preempt_count_set(count);
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 	if (irqs_disabled()) {
893a76bfd0dSCyrill Gorcunov 		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
8941da177e4SLinus Torvalds 		local_irq_enable();
8951da177e4SLinus Torvalds 	}
896f91eb62fSSteven Rostedt 	WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
89759f9415fSArjan van de Ven 
89838addce8SEmese Revfy 	add_latent_entropy();
89930dbb20eSAmérico Wang 	return ret;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
902e0df154fSLinus Torvalds 
903026cee00SPawel Moll extern initcall_t __initcall_start[];
904026cee00SPawel Moll extern initcall_t __initcall0_start[];
905026cee00SPawel Moll extern initcall_t __initcall1_start[];
906026cee00SPawel Moll extern initcall_t __initcall2_start[];
907026cee00SPawel Moll extern initcall_t __initcall3_start[];
908026cee00SPawel Moll extern initcall_t __initcall4_start[];
909026cee00SPawel Moll extern initcall_t __initcall5_start[];
910026cee00SPawel Moll extern initcall_t __initcall6_start[];
911026cee00SPawel Moll extern initcall_t __initcall7_start[];
912026cee00SPawel Moll extern initcall_t __initcall_end[];
913026cee00SPawel Moll 
914026cee00SPawel Moll static initcall_t *initcall_levels[] __initdata = {
915026cee00SPawel Moll 	__initcall0_start,
916026cee00SPawel Moll 	__initcall1_start,
917026cee00SPawel Moll 	__initcall2_start,
918026cee00SPawel Moll 	__initcall3_start,
919026cee00SPawel Moll 	__initcall4_start,
920026cee00SPawel Moll 	__initcall5_start,
921026cee00SPawel Moll 	__initcall6_start,
922026cee00SPawel Moll 	__initcall7_start,
923026cee00SPawel Moll 	__initcall_end,
924026cee00SPawel Moll };
925026cee00SPawel Moll 
92696263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */
927026cee00SPawel Moll static char *initcall_level_names[] __initdata = {
928a6fb6012SSteven Rostedt (VMware) 	"pure",
9299fb48c74SJim Cromie 	"core",
9309fb48c74SJim Cromie 	"postcore",
9319fb48c74SJim Cromie 	"arch",
9329fb48c74SJim Cromie 	"subsys",
9339fb48c74SJim Cromie 	"fs",
9349fb48c74SJim Cromie 	"device",
9359fb48c74SJim Cromie 	"late",
936026cee00SPawel Moll };
937026cee00SPawel Moll 
938026cee00SPawel Moll static void __init do_initcall_level(int level)
939026cee00SPawel Moll {
940026cee00SPawel Moll 	initcall_t *fn;
941026cee00SPawel Moll 
94208746a65SKrzysztof Mazur 	strcpy(initcall_command_line, saved_command_line);
943026cee00SPawel Moll 	parse_args(initcall_level_names[level],
94408746a65SKrzysztof Mazur 		   initcall_command_line, __start___param,
945026cee00SPawel Moll 		   __stop___param - __start___param,
946026cee00SPawel Moll 		   level, level,
947ecc86170SLuis R. Rodriguez 		   NULL, &repair_env_string);
948026cee00SPawel Moll 
9494ee7c60dSSteven Rostedt (VMware) 	trace_initcall_level(initcall_level_names[level]);
950026cee00SPawel Moll 	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
951026cee00SPawel Moll 		do_one_initcall(*fn);
952026cee00SPawel Moll }
953e0df154fSLinus Torvalds 
954e0df154fSLinus Torvalds static void __init do_initcalls(void)
955e0df154fSLinus Torvalds {
956026cee00SPawel Moll 	int level;
957e0df154fSLinus Torvalds 
95819efb72fSBorislav Petkov 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
959026cee00SPawel Moll 		do_initcall_level(level);
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds /*
9631da177e4SLinus Torvalds  * Ok, the machine is now initialized. None of the devices
9641da177e4SLinus Torvalds  * have been touched yet, but the CPU subsystem is up and
9651da177e4SLinus Torvalds  * running, and memory and process management works.
9661da177e4SLinus Torvalds  *
9671da177e4SLinus Torvalds  * Now we can finally start doing some real work..
9681da177e4SLinus Torvalds  */
9691da177e4SLinus Torvalds static void __init do_basic_setup(void)
9701da177e4SLinus Torvalds {
971759ee091SLai Jiangshan 	cpuset_init_smp();
97241ffe5d5SHugh Dickins 	shmem_init();
9731da177e4SLinus Torvalds 	driver_init();
974b04c3afbSEric W. Biederman 	init_irq_proc();
975b99b87f7SPeter Oberparleiter 	do_ctors();
976d5767c53SLinus Torvalds 	usermodehelper_enable();
977b0f84374Swangyanqing 	do_initcalls();
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9807babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void)
981c2147a50SEduard - Gabriel Munteanu {
982196a15b4SH Hartley Sweeten 	initcall_t *fn;
983c2147a50SEduard - Gabriel Munteanu 
9844ee7c60dSSteven Rostedt (VMware) 	trace_initcall_level("early");
985026cee00SPawel Moll 	for (fn = __initcall_start; fn < __initcall0_start; fn++)
986196a15b4SH Hartley Sweeten 		do_one_initcall(*fn);
987c2147a50SEduard - Gabriel Munteanu }
988c2147a50SEduard - Gabriel Munteanu 
989bb813f4cSTejun Heo /*
990bb813f4cSTejun Heo  * This function requests modules which should be loaded by default and is
991bb813f4cSTejun Heo  * called twice right after initrd is mounted and right before init is
992bb813f4cSTejun Heo  * exec'd.  If such modules are on either initrd or rootfs, they will be
993bb813f4cSTejun Heo  * loaded before control is passed to userland.
994bb813f4cSTejun Heo  */
995bb813f4cSTejun Heo void __init load_default_modules(void)
996bb813f4cSTejun Heo {
997bb813f4cSTejun Heo 	load_default_elevator_module();
998bb813f4cSTejun Heo }
999bb813f4cSTejun Heo 
1000a74fb73cSAl Viro static int run_init_process(const char *init_filename)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	argv_init[0] = init_filename;
1003c4ad8f98SLinus Torvalds 	return do_execve(getname_kernel(init_filename),
1004ae903caaSAl Viro 		(const char __user *const __user *)argv_init,
1005ae903caaSAl Viro 		(const char __user *const __user *)envp_init);
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
1008ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename)
1009ba24762bSMichael Opdenacker {
1010ba24762bSMichael Opdenacker 	int ret;
1011ba24762bSMichael Opdenacker 
1012ba24762bSMichael Opdenacker 	ret = run_init_process(init_filename);
1013ba24762bSMichael Opdenacker 
1014ba24762bSMichael Opdenacker 	if (ret && ret != -ENOENT) {
1015ba24762bSMichael Opdenacker 		pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
1016ba24762bSMichael Opdenacker 		       init_filename, ret);
1017ba24762bSMichael Opdenacker 	}
1018ba24762bSMichael Opdenacker 
1019ba24762bSMichael Opdenacker 	return ret;
1020ba24762bSMichael Opdenacker }
1021ba24762bSMichael Opdenacker 
1022f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void);
1023d6b21238SAl Viro 
10240f5bf6d0SLaura Abbott #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
102539290b38SAKASHI Takahiro bool rodata_enabled __ro_after_init = true;
1026d2aa1acaSKees Cook static int __init set_debug_rodata(char *str)
1027d2aa1acaSKees Cook {
1028d2aa1acaSKees Cook 	return strtobool(str, &rodata_enabled);
1029d2aa1acaSKees Cook }
1030d2aa1acaSKees Cook __setup("rodata=", set_debug_rodata);
103139290b38SAKASHI Takahiro #endif
1032d2aa1acaSKees Cook 
10330f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
1034d2aa1acaSKees Cook static void mark_readonly(void)
1035d2aa1acaSKees Cook {
10362959a5f7SJinbum Park 	if (rodata_enabled) {
1037*ae646f0bSJeffrey Hugo 		/*
1038*ae646f0bSJeffrey Hugo 		 * load_module() results in W+X mappings, which are cleaned up
1039*ae646f0bSJeffrey Hugo 		 * with call_rcu_sched().  Let's make sure that queued work is
1040*ae646f0bSJeffrey Hugo 		 * flushed so that we don't hit false positives looking for
1041*ae646f0bSJeffrey Hugo 		 * insecure pages which are W+X.
1042*ae646f0bSJeffrey Hugo 		 */
1043*ae646f0bSJeffrey Hugo 		rcu_barrier_sched();
1044d2aa1acaSKees Cook 		mark_rodata_ro();
10452959a5f7SJinbum Park 		rodata_test();
10462959a5f7SJinbum Park 	} else
1047d2aa1acaSKees Cook 		pr_info("Kernel memory protection disabled.\n");
1048d2aa1acaSKees Cook }
1049d2aa1acaSKees Cook #else
1050d2aa1acaSKees Cook static inline void mark_readonly(void)
1051d2aa1acaSKees Cook {
1052d2aa1acaSKees Cook 	pr_warn("This architecture does not have kernel memory protection.\n");
1053d2aa1acaSKees Cook }
1054d2aa1acaSKees Cook #endif
1055d2aa1acaSKees Cook 
1056d6b21238SAl Viro static int __ref kernel_init(void *unused)
1057ee5bfa64SVivek Goyal {
1058ba24762bSMichael Opdenacker 	int ret;
1059ba24762bSMichael Opdenacker 
1060d6b21238SAl Viro 	kernel_init_freeable();
106122a9d645SArjan van de Ven 	/* need to finish all async __init code before freeing the memory */
106222a9d645SArjan van de Ven 	async_synchronize_full();
1063b80f0f6cSSteven Rostedt (VMware) 	ftrace_free_init_mem();
1064578ae447SJosh Poimboeuf 	jump_label_invalidate_initmem();
1065ee5bfa64SVivek Goyal 	free_initmem();
1066d2aa1acaSKees Cook 	mark_readonly();
1067ee5bfa64SVivek Goyal 	system_state = SYSTEM_RUNNING;
1068ee5bfa64SVivek Goyal 	numa_default_policy();
1069ee5bfa64SVivek Goyal 
1070967dcb8fSPaul E. McKenney 	rcu_end_inkernel_boot();
1071967dcb8fSPaul E. McKenney 
1072ee5bfa64SVivek Goyal 	if (ramdisk_execute_command) {
1073ba24762bSMichael Opdenacker 		ret = run_init_process(ramdisk_execute_command);
1074ba24762bSMichael Opdenacker 		if (!ret)
1075a74fb73cSAl Viro 			return 0;
1076ba24762bSMichael Opdenacker 		pr_err("Failed to execute %s (error %d)\n",
1077ba24762bSMichael Opdenacker 		       ramdisk_execute_command, ret);
1078ee5bfa64SVivek Goyal 	}
1079ee5bfa64SVivek Goyal 
1080ee5bfa64SVivek Goyal 	/*
1081ee5bfa64SVivek Goyal 	 * We try each of these until one succeeds.
1082ee5bfa64SVivek Goyal 	 *
1083ee5bfa64SVivek Goyal 	 * The Bourne shell can be used instead of init if we are
1084ee5bfa64SVivek Goyal 	 * trying to recover a really broken machine.
1085ee5bfa64SVivek Goyal 	 */
1086ee5bfa64SVivek Goyal 	if (execute_command) {
1087ba24762bSMichael Opdenacker 		ret = run_init_process(execute_command);
1088ba24762bSMichael Opdenacker 		if (!ret)
1089a74fb73cSAl Viro 			return 0;
10906ef4536eSAndy Lutomirski 		panic("Requested init %s failed (error %d).",
10916ef4536eSAndy Lutomirski 		      execute_command, ret);
1092ee5bfa64SVivek Goyal 	}
1093ba24762bSMichael Opdenacker 	if (!try_to_run_init_process("/sbin/init") ||
1094ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/etc/init") ||
1095ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/init") ||
1096ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/sh"))
1097a74fb73cSAl Viro 		return 0;
1098ee5bfa64SVivek Goyal 
1099ba24762bSMichael Opdenacker 	panic("No working init found.  Try passing init= option to kernel. "
11008c27ceffSMauro Carvalho Chehab 	      "See Linux Documentation/admin-guide/init.rst for guidance.");
1101ee5bfa64SVivek Goyal }
1102ee5bfa64SVivek Goyal 
1103f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void)
11041da177e4SLinus Torvalds {
1105b433c3d4SPeter Zijlstra 	/*
1106b433c3d4SPeter Zijlstra 	 * Wait until kthreadd is all set-up.
1107b433c3d4SPeter Zijlstra 	 */
1108b433c3d4SPeter Zijlstra 	wait_for_completion(&kthreadd_done);
110931a67102SLinus Torvalds 
111031a67102SLinus Torvalds 	/* Now the scheduler is fully set up and can do blocking allocations */
111131a67102SLinus Torvalds 	gfp_allowed_mask = __GFP_BITS_MASK;
111231a67102SLinus Torvalds 
111358568d2aSMiao Xie 	/*
111458568d2aSMiao Xie 	 * init can allocate pages on any node
111558568d2aSMiao Xie 	 */
11163c466d46SLai Jiangshan 	set_mems_allowed(node_states[N_MEMORY]);
11171da177e4SLinus Torvalds 
11189ec52099SCedric Le Goater 	cad_pid = task_pid(current);
11199ec52099SCedric Le Goater 
1120ca74a6f8SAndi Kleen 	smp_prepare_cpus(setup_max_cpus);
11211da177e4SLinus Torvalds 
11223347fa09STejun Heo 	workqueue_init();
11233347fa09STejun Heo 
1124597b7305SMichal Hocko 	init_mm_internals();
1125597b7305SMichal Hocko 
11261da177e4SLinus Torvalds 	do_pre_smp_initcalls();
1127004417a6SPeter Zijlstra 	lockup_detector_init();
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	smp_init();
11301da177e4SLinus Torvalds 	sched_init_smp();
11311da177e4SLinus Torvalds 
11320e1cc95bSMel Gorman 	page_alloc_init_late();
11330e1cc95bSMel Gorman 
11341da177e4SLinus Torvalds 	do_basic_setup();
11351da177e4SLinus Torvalds 
11362bd3a997SEric W. Biederman 	/* Open the /dev/console on the rootfs, this should never fail */
1137bae217eaSDominik Brodowski 	if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1138ea676e84SAndrew Morton 		pr_err("Warning: unable to open an initial console.\n");
11392bd3a997SEric W. Biederman 
1140c7248321SDominik Brodowski 	(void) ksys_dup(0);
1141c7248321SDominik Brodowski 	(void) ksys_dup(0);
11421da177e4SLinus Torvalds 	/*
11431da177e4SLinus Torvalds 	 * check if there is an early userspace init.  If yes, let it do all
11441da177e4SLinus Torvalds 	 * the work
11451da177e4SLinus Torvalds 	 */
1146ffdfc409SOlof Johansson 
1147ffdfc409SOlof Johansson 	if (!ramdisk_execute_command)
1148ffdfc409SOlof Johansson 		ramdisk_execute_command = "/init";
1149ffdfc409SOlof Johansson 
1150cbfe20f5SDominik Brodowski 	if (ksys_access((const char __user *)
1151cbfe20f5SDominik Brodowski 			ramdisk_execute_command, 0) != 0) {
1152ffdfc409SOlof Johansson 		ramdisk_execute_command = NULL;
11531da177e4SLinus Torvalds 		prepare_namespace();
1154ffdfc409SOlof Johansson 	}
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	/*
11571da177e4SLinus Torvalds 	 * Ok, we have completed the initial bootup, and
11581da177e4SLinus Torvalds 	 * we're essentially up and running. Get rid of the
11591da177e4SLinus Torvalds 	 * initmem segments and start the user-mode stuff..
1160c9cd2ce2SDmitry Kasatkin 	 *
1161c9cd2ce2SDmitry Kasatkin 	 * rootfs is available now, try loading the public keys
1162c9cd2ce2SDmitry Kasatkin 	 * and default modules
11631da177e4SLinus Torvalds 	 */
1164bb813f4cSTejun Heo 
1165c9cd2ce2SDmitry Kasatkin 	integrity_load_keys();
1166bb813f4cSTejun Heo 	load_default_modules();
11671da177e4SLinus Torvalds }
1168