xref: /linux-6.15/init/main.c (revision b80f0f6c)
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>
301da177e4SLinus Torvalds #include <linux/tty.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>
496168a702SAndrew Morton #include <linux/interrupt.h>
50c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
51ca74e92bSShailabh Nagar #include <linux/delayacct.h>
521da177e4SLinus Torvalds #include <linux/unistd.h>
531da177e4SLinus Torvalds #include <linux/rmap.h>
541da177e4SLinus Torvalds #include <linux/mempolicy.h>
551da177e4SLinus Torvalds #include <linux/key.h>
56b6cd0b77SAdrian Bunk #include <linux/buffer_head.h>
57eefa864bSJoonsoo Kim #include <linux/page_ext.h>
589a11b49aSIngo Molnar #include <linux/debug_locks.h>
593ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
60fbb9ce95SIngo Molnar #include <linux/lockdep.h>
613c7b4e6bSCatalin Marinas #include <linux/kmemleak.h>
6284d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
631f21782eSAdrian Bunk #include <linux/device.h>
6473c27992SEric W. Biederman #include <linux/kthread.h>
65e6fe6649SAdrian Bunk #include <linux/sched.h>
661777e463SIngo Molnar #include <linux/sched/init.h>
67a1c9eea9SAdrian Bunk #include <linux/signal.h>
68199f0ca5SAkinobu Mita #include <linux/idr.h>
690b4b3827SJason Wessel #include <linux/kgdb.h>
7068bf21aaSSteven Rostedt #include <linux/ftrace.h>
7122a9d645SArjan van de Ven #include <linux/async.h>
72dfec072eSVegard Nossum #include <linux/kmemcheck.h>
736ae6996aSFeng Tang #include <linux/sfi.h>
742b2af54aSKay Sievers #include <linux/shmem_fs.h>
755a0e3ad6STejun Heo #include <linux/slab.h>
7624a24bb6SPeter Zijlstra #include <linux/perf_event.h>
77a74fb73cSAl Viro #include <linux/ptrace.h>
78bb813f4cSTejun Heo #include <linux/blkdev.h>
79bb813f4cSTejun Heo #include <linux/elevator.h>
8038ff87f7SStephen Boyd #include <linux/sched_clock.h>
8129930025SIngo Molnar #include <linux/sched/task.h>
8268db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
8365f382fdSFrederic Weisbecker #include <linux/context_tracking.h>
8447d06e53STheodore Ts'o #include <linux/random.h>
857b0b73d7SPrarit Bhargava #include <linux/list.h>
86c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h>
87e149ed2bSAl Viro #include <linux/proc_ns.h>
880ddab1d2SToshi Kani #include <linux/io.h>
8939290b38SAKASHI Takahiro #include <linux/cache.h>
902959a5f7SJinbum Park #include <linux/rodata_test.h>
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #include <asm/io.h>
931da177e4SLinus Torvalds #include <asm/bugs.h>
941da177e4SLinus Torvalds #include <asm/setup.h>
95a940199fSAndi Kleen #include <asm/sections.h>
9637b73c82SArjan van de Ven #include <asm/cacheflush.h>
971da177e4SLinus Torvalds 
98aae5f662SSam Ravnborg static int kernel_init(void *);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds extern void init_IRQ(void);
101ff691f6eSHeinrich Schuchardt extern void fork_init(void);
1021da177e4SLinus Torvalds extern void radix_tree_init(void);
1031da177e4SLinus Torvalds 
1042ce802f6STejun Heo /*
1052ce802f6STejun Heo  * Debug helper: via this flag we know that we are in 'early bootup code'
1062ce802f6STejun Heo  * where only the boot processor is running with IRQ disabled.  This means
1072ce802f6STejun Heo  * two things - IRQ must not be enabled before the flag is cleared and some
1082ce802f6STejun Heo  * operations which are not allowed with IRQ disabled are allowed while the
1092ce802f6STejun Heo  * flag is set.
1102ce802f6STejun Heo  */
1112ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly;
1122ce802f6STejun Heo 
113a6826048SPaul E. McKenney enum system_states system_state __read_mostly;
1141da177e4SLinus Torvalds EXPORT_SYMBOL(system_state);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * Boot command-line arguments
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
1201da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds extern void time_init(void);
1231da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */
124d2e3192bSJan Beulich void (*__initdata late_time_init)(void);
1251da177e4SLinus Torvalds 
12630d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */
12730d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE];
12830d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */
12930d7e0d4SAlon Bar-Lev char *saved_command_line;
13030d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */
13130d7e0d4SAlon Bar-Lev static char *static_command_line;
13208746a65SKrzysztof Mazur /* Command line for per-initcall parameter parsing */
13308746a65SKrzysztof Mazur static char *initcall_command_line;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static char *execute_command;
136ffdfc409SOlof Johansson static char *ramdisk_execute_command;
1371da177e4SLinus Torvalds 
1388b3b2955SJan Beulich /*
139c4b2c0c5SHannes Frederic Sowa  * Used to generate warnings if static_key manipulation functions are used
140c4b2c0c5SHannes Frederic Sowa  * before jump_label_init is called.
141c4b2c0c5SHannes Frederic Sowa  */
142dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly;
143c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized);
144c4b2c0c5SHannes Frederic Sowa 
145c4b2c0c5SHannes Frederic Sowa /*
1468b3b2955SJan Beulich  * If set, this is an indication to the drivers that reset the underlying
1478b3b2955SJan Beulich  * device before going ahead with the initialization otherwise driver might
1488b3b2955SJan Beulich  * rely on the BIOS and skip the reset operation.
1498b3b2955SJan Beulich  *
1508b3b2955SJan Beulich  * This is useful if kernel is booting in an unreliable environment.
151fc454fdcSFrans Klaver  * For ex. kdump situation where previous kernel has crashed, BIOS has been
1528b3b2955SJan Beulich  * skipped and devices will be in unknown state.
1538b3b2955SJan Beulich  */
1548b3b2955SJan Beulich unsigned int reset_devices;
1558b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices);
1561da177e4SLinus Torvalds 
1577e96287dSVivek Goyal static int __init set_reset_devices(char *str)
1587e96287dSVivek Goyal {
1597e96287dSVivek Goyal 	reset_devices = 1;
1607e96287dSVivek Goyal 	return 1;
1617e96287dSVivek Goyal }
1627e96287dSVivek Goyal 
1637e96287dSVivek Goyal __setup("reset_devices", set_reset_devices);
1647e96287dSVivek Goyal 
165d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
166d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1671da177e4SLinus Torvalds static const char *panic_later, *panic_param;
1681da177e4SLinus Torvalds 
169914dcaa8SRusty Russell extern const struct obs_kernel_param __setup_start[], __setup_end[];
1701da177e4SLinus Torvalds 
17131c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line)
1721da177e4SLinus Torvalds {
173914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
17431c025b5SYaowei Bai 	bool had_early_param = false;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	p = __setup_start;
1771da177e4SLinus Torvalds 	do {
1781da177e4SLinus Torvalds 		int n = strlen(p->str);
179b1e4d20cSMichal Schmidt 		if (parameqn(line, p->str, n)) {
1801da177e4SLinus Torvalds 			if (p->early) {
18133df0d19SRusty Russell 				/* Already done in parse_early_param?
18233df0d19SRusty Russell 				 * (Needs exact match on param part).
18333df0d19SRusty Russell 				 * Keep iterating, as we can have early
18433df0d19SRusty Russell 				 * params and __setups of same names 8( */
1851da177e4SLinus Torvalds 				if (line[n] == '\0' || line[n] == '=')
18631c025b5SYaowei Bai 					had_early_param = true;
1871da177e4SLinus Torvalds 			} else if (!p->setup_func) {
188ea676e84SAndrew Morton 				pr_warn("Parameter %s is obsolete, ignored\n",
189ea676e84SAndrew Morton 					p->str);
19031c025b5SYaowei Bai 				return true;
1911da177e4SLinus Torvalds 			} else if (p->setup_func(line + n))
19231c025b5SYaowei Bai 				return true;
1931da177e4SLinus Torvalds 		}
1941da177e4SLinus Torvalds 		p++;
1951da177e4SLinus Torvalds 	} while (p < __setup_end);
19633df0d19SRusty Russell 
19733df0d19SRusty Russell 	return had_early_param;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /*
2011da177e4SLinus Torvalds  * This should be approx 2 Bo*oMips to start (note initial shift), and will
2021da177e4SLinus Torvalds  * still work even if initially too large, it will just take slightly longer
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12);
2051da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy);
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static int __init debug_kernel(char *str)
2081da177e4SLinus Torvalds {
209a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
210f6f21c81SYinghai Lu 	return 0;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static int __init quiet_kernel(char *str)
2141da177e4SLinus Torvalds {
215a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_QUIET;
216f6f21c81SYinghai Lu 	return 0;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
219f6f21c81SYinghai Lu early_param("debug", debug_kernel);
220f6f21c81SYinghai Lu early_param("quiet", quiet_kernel);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static int __init loglevel(char *str)
2231da177e4SLinus Torvalds {
224808bf29bSAlexander Sverdlin 	int newlevel;
225808bf29bSAlexander Sverdlin 
226808bf29bSAlexander Sverdlin 	/*
227808bf29bSAlexander Sverdlin 	 * Only update loglevel value when a correct setting was passed,
228808bf29bSAlexander Sverdlin 	 * to prevent blind crashes (when loglevel being set to 0) that
229808bf29bSAlexander Sverdlin 	 * are quite hard to debug
230808bf29bSAlexander Sverdlin 	 */
231808bf29bSAlexander Sverdlin 	if (get_option(&str, &newlevel)) {
232808bf29bSAlexander Sverdlin 		console_loglevel = newlevel;
233d9d4fcfeSAlex Riesen 		return 0;
2341da177e4SLinus Torvalds 	}
2351da177e4SLinus Torvalds 
236808bf29bSAlexander Sverdlin 	return -EINVAL;
237808bf29bSAlexander Sverdlin }
238808bf29bSAlexander Sverdlin 
239f6f21c81SYinghai Lu early_param("loglevel", loglevel);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */
242ecc86170SLuis R. Rodriguez static int __init repair_env_string(char *param, char *val,
243ecc86170SLuis R. Rodriguez 				    const char *unused, void *arg)
244a99cd112SChris Metcalf {
2451da177e4SLinus Torvalds 	if (val) {
2461da177e4SLinus Torvalds 		/* param=val or param="val"? */
2471da177e4SLinus Torvalds 		if (val == param+strlen(param)+1)
2481da177e4SLinus Torvalds 			val[-1] = '=';
2491da177e4SLinus Torvalds 		else if (val == param+strlen(param)+2) {
2501da177e4SLinus Torvalds 			val[-2] = '=';
2511da177e4SLinus Torvalds 			memmove(val-1, val, strlen(val)+1);
2521da177e4SLinus Torvalds 			val--;
2531da177e4SLinus Torvalds 		} else
2541da177e4SLinus Torvalds 			BUG();
2551da177e4SLinus Torvalds 	}
256a99cd112SChris Metcalf 	return 0;
257a99cd112SChris Metcalf }
258a99cd112SChris Metcalf 
25951e158c1SRusty Russell /* Anything after -- gets handed straight to init. */
260ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val,
261ecc86170SLuis R. Rodriguez 			       const char *unused, void *arg)
26251e158c1SRusty Russell {
26351e158c1SRusty Russell 	unsigned int i;
26451e158c1SRusty Russell 
26551e158c1SRusty Russell 	if (panic_later)
26651e158c1SRusty Russell 		return 0;
26751e158c1SRusty Russell 
268ecc86170SLuis R. Rodriguez 	repair_env_string(param, val, unused, NULL);
26951e158c1SRusty Russell 
27051e158c1SRusty Russell 	for (i = 0; argv_init[i]; i++) {
27151e158c1SRusty Russell 		if (i == MAX_INIT_ARGS) {
27251e158c1SRusty Russell 			panic_later = "init";
27351e158c1SRusty Russell 			panic_param = param;
27451e158c1SRusty Russell 			return 0;
27551e158c1SRusty Russell 		}
27651e158c1SRusty Russell 	}
27751e158c1SRusty Russell 	argv_init[i] = param;
27851e158c1SRusty Russell 	return 0;
27951e158c1SRusty Russell }
28051e158c1SRusty Russell 
281a99cd112SChris Metcalf /*
282a99cd112SChris Metcalf  * Unknown boot options get handed to init, unless they look like
283a99cd112SChris Metcalf  * unused parameters (modprobe will find them in /proc/cmdline).
284a99cd112SChris Metcalf  */
285ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val,
286ecc86170SLuis R. Rodriguez 				     const char *unused, void *arg)
287a99cd112SChris Metcalf {
288ecc86170SLuis R. Rodriguez 	repair_env_string(param, val, unused, NULL);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/* Handle obsolete-style parameters */
2911da177e4SLinus Torvalds 	if (obsolete_checksetup(param))
2921da177e4SLinus Torvalds 		return 0;
2931da177e4SLinus Torvalds 
294f066a4f6SRusty Russell 	/* Unused module parameter. */
295f066a4f6SRusty Russell 	if (strchr(param, '.') && (!val || strchr(param, '.') < val))
2961da177e4SLinus Torvalds 		return 0;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (panic_later)
2991da177e4SLinus Torvalds 		return 0;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (val) {
3021da177e4SLinus Torvalds 		/* Environment option */
3031da177e4SLinus Torvalds 		unsigned int i;
3041da177e4SLinus Torvalds 		for (i = 0; envp_init[i]; i++) {
3051da177e4SLinus Torvalds 			if (i == MAX_INIT_ENVS) {
306499a4584STetsuo Handa 				panic_later = "env";
3071da177e4SLinus Torvalds 				panic_param = param;
3081da177e4SLinus Torvalds 			}
3091da177e4SLinus Torvalds 			if (!strncmp(param, envp_init[i], val - param))
3101da177e4SLinus Torvalds 				break;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 		envp_init[i] = param;
3131da177e4SLinus Torvalds 	} else {
3141da177e4SLinus Torvalds 		/* Command line option */
3151da177e4SLinus Torvalds 		unsigned int i;
3161da177e4SLinus Torvalds 		for (i = 0; argv_init[i]; i++) {
3171da177e4SLinus Torvalds 			if (i == MAX_INIT_ARGS) {
318499a4584STetsuo Handa 				panic_later = "init";
3191da177e4SLinus Torvalds 				panic_param = param;
3201da177e4SLinus Torvalds 			}
3211da177e4SLinus Torvalds 		}
3221da177e4SLinus Torvalds 		argv_init[i] = param;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 	return 0;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds static int __init init_setup(char *str)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	unsigned int i;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	execute_command = str;
3321da177e4SLinus Torvalds 	/*
3331da177e4SLinus Torvalds 	 * In case LILO is going to boot us with default command line,
3341da177e4SLinus Torvalds 	 * it prepends "auto" before the whole cmdline which makes
3351da177e4SLinus Torvalds 	 * the shell think it should execute a script with such name.
3361da177e4SLinus Torvalds 	 * So we ignore all arguments entered _before_ init=... [MJ]
3371da177e4SLinus Torvalds 	 */
3381da177e4SLinus Torvalds 	for (i = 1; i < MAX_INIT_ARGS; i++)
3391da177e4SLinus Torvalds 		argv_init[i] = NULL;
3401da177e4SLinus Torvalds 	return 1;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds __setup("init=", init_setup);
3431da177e4SLinus Torvalds 
344ffdfc409SOlof Johansson static int __init rdinit_setup(char *str)
345ffdfc409SOlof Johansson {
346ffdfc409SOlof Johansson 	unsigned int i;
347ffdfc409SOlof Johansson 
348ffdfc409SOlof Johansson 	ramdisk_execute_command = str;
349ffdfc409SOlof Johansson 	/* See "auto" comment in init_setup */
350ffdfc409SOlof Johansson 	for (i = 1; i < MAX_INIT_ARGS; i++)
351ffdfc409SOlof Johansson 		argv_init[i] = NULL;
352ffdfc409SOlof Johansson 	return 1;
353ffdfc409SOlof Johansson }
354ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup);
355ffdfc409SOlof Johansson 
3561da177e4SLinus Torvalds #ifndef CONFIG_SMP
35734db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS;
358e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { }
3591da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { }
3601da177e4SLinus Torvalds #endif
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
36330d7e0d4SAlon Bar-Lev  * We need to store the untouched command line for future reference.
36430d7e0d4SAlon Bar-Lev  * We also need to store the touched command line since the parameter
36530d7e0d4SAlon Bar-Lev  * parsing is performed in place, and we should allow a component to
36630d7e0d4SAlon Bar-Lev  * store reference of name/value for future reference.
36730d7e0d4SAlon Bar-Lev  */
36830d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line)
36930d7e0d4SAlon Bar-Lev {
370098b081bSSantosh Shilimkar 	saved_command_line =
371098b081bSSantosh Shilimkar 		memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
372098b081bSSantosh Shilimkar 	initcall_command_line =
373098b081bSSantosh Shilimkar 		memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
374098b081bSSantosh Shilimkar 	static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
37530d7e0d4SAlon Bar-Lev 	strcpy(saved_command_line, boot_command_line);
37630d7e0d4SAlon Bar-Lev 	strcpy(static_command_line, command_line);
37730d7e0d4SAlon Bar-Lev }
37830d7e0d4SAlon Bar-Lev 
37930d7e0d4SAlon Bar-Lev /*
3801da177e4SLinus Torvalds  * We need to finalize in a non-__init function or else race conditions
3811da177e4SLinus Torvalds  * between the root thread and the init thread may cause start_kernel to
3821da177e4SLinus Torvalds  * be reaped by free_initmem before the root thread has proceeded to
3831da177e4SLinus Torvalds  * cpu_idle.
3841da177e4SLinus Torvalds  *
3851da177e4SLinus Torvalds  * gcc-3.4 accidentally inlines this function, so use noinline.
3861da177e4SLinus Torvalds  */
3871da177e4SLinus Torvalds 
388b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done);
389b433c3d4SPeter Zijlstra 
390bd721ea7SFabian Frederick static noinline void __ref rest_init(void)
3911da177e4SLinus Torvalds {
39273c27992SEric W. Biederman 	int pid;
39373c27992SEric W. Biederman 
3947db905e6SPaul E. McKenney 	rcu_scheduler_starting();
395b433c3d4SPeter Zijlstra 	/*
39697158569SPeter Zijlstra 	 * We need to spawn init first so that it obtains pid 1, however
397b433c3d4SPeter Zijlstra 	 * the init task will end up wanting to create kthreads, which, if
398b433c3d4SPeter Zijlstra 	 * we schedule it before we create kthreadd, will OOPS.
399b433c3d4SPeter Zijlstra 	 */
40034a1b723SOleg Nesterov 	kernel_thread(kernel_init, NULL, CLONE_FS);
4011da177e4SLinus Torvalds 	numa_default_policy();
40273c27992SEric W. Biederman 	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
403d11c563dSPaul E. McKenney 	rcu_read_lock();
4045cd20455SPavel Emelyanov 	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
405d11c563dSPaul E. McKenney 	rcu_read_unlock();
406b433c3d4SPeter Zijlstra 	complete(&kthreadd_done);
407f340c0d1SIngo Molnar 
408f340c0d1SIngo Molnar 	/*
409f340c0d1SIngo Molnar 	 * The boot idle thread must execute schedule()
4101df21055SIngo Molnar 	 * at least once to get things moving:
411f340c0d1SIngo Molnar 	 */
4121df21055SIngo Molnar 	init_idle_bootup_task(current);
413bd2f5536SThomas Gleixner 	schedule_preempt_disabled();
4145bfb5d69SNick Piggin 	/* Call into cpu_idle with preempt disabled */
415a1a04ec3SThomas Gleixner 	cpu_startup_entry(CPUHP_ONLINE);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /* Check for early params. */
419ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val,
420ecc86170SLuis R. Rodriguez 				 const char *unused, void *arg)
4211da177e4SLinus Torvalds {
422914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	for (p = __setup_start; p < __setup_end; p++) {
425b1e4d20cSMichal Schmidt 		if ((p->early && parameq(param, p->str)) ||
42618a8bd94SYinghai Lu 		    (strcmp(param, "console") == 0 &&
42718a8bd94SYinghai Lu 		     strcmp(p->str, "earlycon") == 0)
42818a8bd94SYinghai Lu 		) {
4291da177e4SLinus Torvalds 			if (p->setup_func(val) != 0)
430ea676e84SAndrew Morton 				pr_warn("Malformed early option '%s'\n", param);
4311da177e4SLinus Torvalds 		}
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds 	/* We accept everything at this stage. */
4341da177e4SLinus Torvalds 	return 0;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
43713977091SMagnus Damm void __init parse_early_options(char *cmdline)
43813977091SMagnus Damm {
439ecc86170SLuis R. Rodriguez 	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
440ecc86170SLuis R. Rodriguez 		   do_early_param);
44113977091SMagnus Damm }
44213977091SMagnus Damm 
4431da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */
4441da177e4SLinus Torvalds void __init parse_early_param(void)
4451da177e4SLinus Torvalds {
446dd4d9fecSFabian Frederick 	static int done __initdata;
447dd4d9fecSFabian Frederick 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (done)
4501da177e4SLinus Torvalds 		return;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	/* All fall through to do_early_param. */
45330d7e0d4SAlon Bar-Lev 	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
45413977091SMagnus Damm 	parse_early_options(tmp_cmdline);
4551da177e4SLinus Torvalds 	done = 1;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
458e7ff3a47SThomas Gleixner void __init __weak arch_post_acpi_subsys_init(void) { }
459e7ff3a47SThomas Gleixner 
460839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void)
461033ab7f8SAndrew Morton {
462033ab7f8SAndrew Morton }
463033ab7f8SAndrew Morton 
464eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE
465b235beeaSLinus Torvalds void __init __weak thread_stack_cache_init(void)
4668c9843e5SBenjamin Herrenschmidt {
4678c9843e5SBenjamin Herrenschmidt }
468eded09ccSDavid Howells #endif
4698c9843e5SBenjamin Herrenschmidt 
470444f478fSPekka Enberg /*
471444f478fSPekka Enberg  * Set up kernel memory allocators
472444f478fSPekka Enberg  */
473444f478fSPekka Enberg static void __init mm_init(void)
474444f478fSPekka Enberg {
475eefa864bSJoonsoo Kim 	/*
476eefa864bSJoonsoo Kim 	 * page_ext requires contiguous pages,
477eefa864bSJoonsoo Kim 	 * bigger than MAX_ORDER unless SPARSEMEM.
478eefa864bSJoonsoo Kim 	 */
479eefa864bSJoonsoo Kim 	page_ext_init_flatmem();
480444f478fSPekka Enberg 	mem_init();
481444f478fSPekka Enberg 	kmem_cache_init();
482099a19d9STejun Heo 	percpu_init_late();
483b35f1819SKirill A. Shutemov 	pgtable_init();
484444f478fSPekka Enberg 	vmalloc_init();
4850ddab1d2SToshi Kani 	ioremap_huge_init();
486444f478fSPekka Enberg }
487444f478fSPekka Enberg 
488722a9f92SAndi Kleen asmlinkage __visible void __init start_kernel(void)
4891da177e4SLinus Torvalds {
490dd4d9fecSFabian Frederick 	char *command_line;
491dd4d9fecSFabian Frederick 	char *after_dashes;
492033ab7f8SAndrew Morton 
493d4311ff1SAaron Tomlin 	set_task_stack_end_magic(&init_task);
49473839c5bSMing Lei 	smp_setup_processor_id();
4953ac7fe5aSThomas Gleixner 	debug_objects_early_init();
49642059429SIngo Molnar 
49742059429SIngo Molnar 	/*
49842059429SIngo Molnar 	 * Set up the the initial canary ASAP:
49942059429SIngo Molnar 	 */
50042059429SIngo Molnar 	boot_init_stack_canary();
50142059429SIngo Molnar 
502ddbcc7e8SPaul Menage 	cgroup_init_early();
503fbb9ce95SIngo Molnar 
504fbb9ce95SIngo Molnar 	local_irq_disable();
5052ce802f6STejun Heo 	early_boot_irqs_disabled = true;
506fbb9ce95SIngo Molnar 
507fbb9ce95SIngo Molnar /*
5081da177e4SLinus Torvalds  * Interrupts are still disabled. Do necessary setups, then
5091da177e4SLinus Torvalds  * enable them
5101da177e4SLinus Torvalds  */
51144fd2299SStas Sergeev 	boot_cpu_init();
5121da177e4SLinus Torvalds 	page_address_init();
513ea676e84SAndrew Morton 	pr_notice("%s", linux_banner);
5141da177e4SLinus Torvalds 	setup_arch(&command_line);
5156345d24dSLinus Torvalds 	mm_init_cpumask(&init_mm);
51630d7e0d4SAlon Bar-Lev 	setup_command_line(command_line);
517e0982e90SMike Travis 	setup_nr_cpu_ids();
518d6647bdfSTejun Heo 	setup_per_cpu_areas();
519cff7d378SThomas Gleixner 	boot_cpu_state_init();
52044fd2299SStas Sergeev 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
5211da177e4SLinus Torvalds 
5229adb62a5SJiang Liu 	build_all_zonelists(NULL, NULL);
52383b519e8SPekka Enberg 	page_alloc_init();
52483b519e8SPekka Enberg 
525ea676e84SAndrew Morton 	pr_notice("Kernel command line: %s\n", boot_command_line);
52683b519e8SPekka Enberg 	parse_early_param();
52751e158c1SRusty Russell 	after_dashes = parse_args("Booting kernel",
52851e158c1SRusty Russell 				  static_command_line, __start___param,
52983b519e8SPekka Enberg 				  __stop___param - __start___param,
530ecc86170SLuis R. Rodriguez 				  -1, -1, NULL, &unknown_bootoption);
5313438cf54SDaniel Thompson 	if (!IS_ERR_OR_NULL(after_dashes))
53251e158c1SRusty Russell 		parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
533ecc86170SLuis R. Rodriguez 			   NULL, set_init_arg);
53497ce2c88SJeremy Fitzhardinge 
53597ce2c88SJeremy Fitzhardinge 	jump_label_init();
53697ce2c88SJeremy Fitzhardinge 
53783b519e8SPekka Enberg 	/*
53883b519e8SPekka Enberg 	 * These use large bootmem allocations and must precede
53983b519e8SPekka Enberg 	 * kmem_cache_init()
54083b519e8SPekka Enberg 	 */
541162a7e75SMike Travis 	setup_log_buf(0);
54283b519e8SPekka Enberg 	pidhash_init();
54383b519e8SPekka Enberg 	vfs_caches_init_early();
54483b519e8SPekka Enberg 	sort_main_extable();
54583b519e8SPekka Enberg 	trap_init();
546444f478fSPekka Enberg 	mm_init();
547de03c72cSKOSAKI Motohiro 
548f631718dSSteven Rostedt (VMware) 	ftrace_init();
549f631718dSSteven Rostedt (VMware) 
550e725c731SSteven Rostedt (VMware) 	/* trace_printk can be enabled here */
551e725c731SSteven Rostedt (VMware) 	early_trace_init();
552e725c731SSteven Rostedt (VMware) 
5531da177e4SLinus Torvalds 	/*
5541da177e4SLinus Torvalds 	 * Set up the scheduler prior starting any interrupts (such as the
5551da177e4SLinus Torvalds 	 * timer interrupt). Full topology setup happens at smp_init()
5561da177e4SLinus Torvalds 	 * time - but meanwhile we still have a functioning scheduler.
5571da177e4SLinus Torvalds 	 */
5581da177e4SLinus Torvalds 	sched_init();
5591da177e4SLinus Torvalds 	/*
5601da177e4SLinus Torvalds 	 * Disable preemption - early bootup scheduling is extremely
5611da177e4SLinus Torvalds 	 * fragile until we cpu_idle() for the first time.
5621da177e4SLinus Torvalds 	 */
5631da177e4SLinus Torvalds 	preempt_disable();
564dd4d9fecSFabian Frederick 	if (WARN(!irqs_disabled(),
565dd4d9fecSFabian Frederick 		 "Interrupts were enabled *very* early, fixing it\n"))
566c4a68306SArd van Breemen 		local_irq_disable();
5670a835c4fSMatthew Wilcox 	radix_tree_init();
5683347fa09STejun Heo 
5693347fa09STejun Heo 	/*
5703347fa09STejun Heo 	 * Allow workqueue creation and work item queueing/cancelling
5713347fa09STejun Heo 	 * early.  Work item execution depends on kthreads and starts after
5723347fa09STejun Heo 	 * workqueue_init().
5733347fa09STejun Heo 	 */
5743347fa09STejun Heo 	workqueue_init_early();
5753347fa09STejun Heo 
5761da177e4SLinus Torvalds 	rcu_init();
5775f893b26SSteven Rostedt (Red Hat) 
578e725c731SSteven Rostedt (VMware) 	/* Trace events are available after this */
5795f893b26SSteven Rostedt (Red Hat) 	trace_init();
5805f893b26SSteven Rostedt (Red Hat) 
58165f382fdSFrederic Weisbecker 	context_tracking_init();
5820b8f1efaSYinghai Lu 	/* init some links before init_ISA_irqs() */
5830b8f1efaSYinghai Lu 	early_irq_init();
5841da177e4SLinus Torvalds 	init_IRQ();
585ad2b1353SThomas Gleixner 	tick_init();
586d6dd50e0SLinus Torvalds 	rcu_init_nohz();
5871da177e4SLinus Torvalds 	init_timers();
588c0a31329SThomas Gleixner 	hrtimers_init();
5891da177e4SLinus Torvalds 	softirq_init();
590ad596171Sjohn stultz 	timekeeping_init();
59188fecaa2Sjohn stultz 	time_init();
59238ff87f7SStephen Boyd 	sched_clock_postinit();
593f92bac3bSSergey Senozhatsky 	printk_safe_init();
5949e630205SStephane Eranian 	perf_event_init();
59593e02814SHeiko Carstens 	profile_init();
596d8ad7d11STakao Indoh 	call_function_init();
597f91eb62fSSteven Rostedt 	WARN(!irqs_disabled(), "Interrupts were enabled early\n");
5982ce802f6STejun Heo 	early_boot_irqs_disabled = false;
59993e02814SHeiko Carstens 	local_irq_enable();
600dcce284aSBenjamin Herrenschmidt 
6017e85ee0cSPekka Enberg 	kmem_cache_init_late();
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	/*
6041da177e4SLinus Torvalds 	 * HACK ALERT! This is early. We're enabling the console before
6051da177e4SLinus Torvalds 	 * we've done PCI setups etc, and console_init() must be aware of
6061da177e4SLinus Torvalds 	 * this. But we do want output early, in case something goes wrong.
6071da177e4SLinus Torvalds 	 */
6081da177e4SLinus Torvalds 	console_init();
6091da177e4SLinus Torvalds 	if (panic_later)
610499a4584STetsuo Handa 		panic("Too many boot %s vars at `%s'", panic_later,
611499a4584STetsuo Handa 		      panic_param);
612fbb9ce95SIngo Molnar 
613fbb9ce95SIngo Molnar 	lockdep_info();
614fbb9ce95SIngo Molnar 
6159a11b49aSIngo Molnar 	/*
6169a11b49aSIngo Molnar 	 * Need to run this when irqs are enabled, because it wants
6179a11b49aSIngo Molnar 	 * to self-test [hard/soft]-irqs on/off lock inversion bugs
6189a11b49aSIngo Molnar 	 * too:
6199a11b49aSIngo Molnar 	 */
6209a11b49aSIngo Molnar 	locking_selftest();
6219a11b49aSIngo Molnar 
6221da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
6231da177e4SLinus Torvalds 	if (initrd_start && !initrd_below_start_ok &&
624bd673c7cSGeert Uytterhoeven 	    page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
625ea676e84SAndrew Morton 		pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
626bd673c7cSGeert Uytterhoeven 		    page_to_pfn(virt_to_page((void *)initrd_start)),
627bd673c7cSGeert Uytterhoeven 		    min_low_pfn);
6281da177e4SLinus Torvalds 		initrd_start = 0;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds #endif
631fe53ca54SYang Shi 	page_ext_init();
6323ac7fe5aSThomas Gleixner 	debug_objects_mem_init();
6339b090f2dSCatalin Marinas 	kmemleak_init();
634e7c8d5c9SChristoph Lameter 	setup_per_cpu_pageset();
6351da177e4SLinus Torvalds 	numa_policy_init();
6361da177e4SLinus Torvalds 	if (late_time_init)
6371da177e4SLinus Torvalds 		late_time_init();
6381da177e4SLinus Torvalds 	calibrate_delay();
6391da177e4SLinus Torvalds 	pidmap_init();
6401da177e4SLinus Torvalds 	anon_vma_init();
641c4e1acbbSRafael J. Wysocki 	acpi_early_init();
64211520e5eSLinus Torvalds #ifdef CONFIG_X86
64383e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES))
64411520e5eSLinus Torvalds 		efi_enter_virtual_mode();
64511520e5eSLinus Torvalds #endif
646197725deSH. Peter Anvin #ifdef CONFIG_X86_ESPFIX64
6473891a04aSH. Peter Anvin 	/* Should be run before the first non-init thread is created */
6483891a04aSH. Peter Anvin 	init_espfix_bsp();
6493891a04aSH. Peter Anvin #endif
650b235beeaSLinus Torvalds 	thread_stack_cache_init();
651d84f4f99SDavid Howells 	cred_init();
652ff691f6eSHeinrich Schuchardt 	fork_init();
6531da177e4SLinus Torvalds 	proc_caches_init();
6541da177e4SLinus Torvalds 	buffer_init();
6551da177e4SLinus Torvalds 	key_init();
6561da177e4SLinus Torvalds 	security_init();
6570b4b3827SJason Wessel 	dbg_late_init();
6584248b0daSMel Gorman 	vfs_caches_init();
65962906027SNicholas Piggin 	pagecache_init();
6601da177e4SLinus Torvalds 	signals_init();
6611da177e4SLinus Torvalds 	proc_root_init();
662e149ed2bSAl Viro 	nsfs_init();
6631da177e4SLinus Torvalds 	cpuset_init();
664695df213SZefan Li 	cgroup_init();
665c757249aSShailabh Nagar 	taskstats_init_early();
666ca74e92bSShailabh Nagar 	delayacct_init();
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	check_bugs();
6691da177e4SLinus Torvalds 
670b064a8faSRafael J. Wysocki 	acpi_subsystem_init();
671e7ff3a47SThomas Gleixner 	arch_post_acpi_subsys_init();
6726ae6996aSFeng Tang 	sfi_init_late();
6731da177e4SLinus Torvalds 
67483e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES)) {
67578510792SJosh Triplett 		efi_free_boot_services();
6762223af38SJosh Triplett 	}
67778510792SJosh Triplett 
6781da177e4SLinus Torvalds 	/* Do the rest non-__init'ed, we're now alive */
6791da177e4SLinus Torvalds 	rest_init();
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
682b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */
683b99b87f7SPeter Oberparleiter static void __init do_ctors(void)
684b99b87f7SPeter Oberparleiter {
685b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
686196a15b4SH Hartley Sweeten 	ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
687b99b87f7SPeter Oberparleiter 
688196a15b4SH Hartley Sweeten 	for (; fn < (ctor_fn_t *) __ctors_end; fn++)
689196a15b4SH Hartley Sweeten 		(*fn)();
690b99b87f7SPeter Oberparleiter #endif
691b99b87f7SPeter Oberparleiter }
692b99b87f7SPeter Oberparleiter 
6932329abfaSRusty Russell bool initcall_debug;
694d0ea3d7dSRusty Russell core_param(initcall_debug, initcall_debug, bool, 0644);
6951da177e4SLinus Torvalds 
6967b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS
6977b0b73d7SPrarit Bhargava struct blacklist_entry {
6987b0b73d7SPrarit Bhargava 	struct list_head next;
6997b0b73d7SPrarit Bhargava 	char *buf;
7007b0b73d7SPrarit Bhargava };
7017b0b73d7SPrarit Bhargava 
7027b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
7037b0b73d7SPrarit Bhargava 
7047b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
7057b0b73d7SPrarit Bhargava {
7067b0b73d7SPrarit Bhargava 	char *str_entry;
7077b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
7087b0b73d7SPrarit Bhargava 
7097b0b73d7SPrarit Bhargava 	/* str argument is a comma-separated list of functions */
7107b0b73d7SPrarit Bhargava 	do {
7117b0b73d7SPrarit Bhargava 		str_entry = strsep(&str, ",");
7127b0b73d7SPrarit Bhargava 		if (str_entry) {
7137b0b73d7SPrarit Bhargava 			pr_debug("blacklisting initcall %s\n", str_entry);
7147b0b73d7SPrarit Bhargava 			entry = alloc_bootmem(sizeof(*entry));
7157b0b73d7SPrarit Bhargava 			entry->buf = alloc_bootmem(strlen(str_entry) + 1);
7167b0b73d7SPrarit Bhargava 			strcpy(entry->buf, str_entry);
7177b0b73d7SPrarit Bhargava 			list_add(&entry->next, &blacklisted_initcalls);
7187b0b73d7SPrarit Bhargava 		}
7197b0b73d7SPrarit Bhargava 	} while (str_entry);
7207b0b73d7SPrarit Bhargava 
7217b0b73d7SPrarit Bhargava 	return 0;
7227b0b73d7SPrarit Bhargava }
7237b0b73d7SPrarit Bhargava 
7247b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
7257b0b73d7SPrarit Bhargava {
7267b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
727c8cdd2beSRasmus Villemoes 	char fn_name[KSYM_SYMBOL_LEN];
7280fd5ed8dSRasmus Villemoes 	unsigned long addr;
7297b0b73d7SPrarit Bhargava 
730c8cdd2beSRasmus Villemoes 	if (list_empty(&blacklisted_initcalls))
7317b0b73d7SPrarit Bhargava 		return false;
7327b0b73d7SPrarit Bhargava 
7330fd5ed8dSRasmus Villemoes 	addr = (unsigned long) dereference_function_descriptor(fn);
7340fd5ed8dSRasmus Villemoes 	sprint_symbol_no_offset(fn_name, addr);
735c8cdd2beSRasmus Villemoes 
736841c06d7SPrarit Bhargava 	/*
737841c06d7SPrarit Bhargava 	 * fn will be "function_name [module_name]" where [module_name] is not
738841c06d7SPrarit Bhargava 	 * displayed for built-in init functions.  Strip off the [module_name].
739841c06d7SPrarit Bhargava 	 */
740841c06d7SPrarit Bhargava 	strreplace(fn_name, ' ', '\0');
741841c06d7SPrarit Bhargava 
742e6fd1fb3SGeliang Tang 	list_for_each_entry(entry, &blacklisted_initcalls, next) {
7437b0b73d7SPrarit Bhargava 		if (!strcmp(fn_name, entry->buf)) {
7447b0b73d7SPrarit Bhargava 			pr_debug("initcall %s blacklisted\n", fn_name);
7457b0b73d7SPrarit Bhargava 			return true;
7467b0b73d7SPrarit Bhargava 		}
7477b0b73d7SPrarit Bhargava 	}
7487b0b73d7SPrarit Bhargava 
7497b0b73d7SPrarit Bhargava 	return false;
7507b0b73d7SPrarit Bhargava }
7517b0b73d7SPrarit Bhargava #else
7527b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
7537b0b73d7SPrarit Bhargava {
7547b0b73d7SPrarit Bhargava 	pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
7557b0b73d7SPrarit Bhargava 	return 0;
7567b0b73d7SPrarit Bhargava }
7577b0b73d7SPrarit Bhargava 
7587b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
7597b0b73d7SPrarit Bhargava {
7607b0b73d7SPrarit Bhargava 	return false;
7617b0b73d7SPrarit Bhargava }
7627b0b73d7SPrarit Bhargava #endif
7637b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist);
7647b0b73d7SPrarit Bhargava 
765e4461271SKevin Winchester static int __init_or_module do_one_initcall_debug(initcall_t fn)
7661da177e4SLinus Torvalds {
76774239072SFrederic Weisbecker 	ktime_t calltime, delta, rettime;
76830dbb20eSAmérico Wang 	unsigned long long duration;
76930dbb20eSAmérico Wang 	int ret;
7701da177e4SLinus Torvalds 
771d62cf815SAndrew Morton 	printk(KERN_DEBUG "calling  %pF @ %i\n", fn, task_pid_nr(current));
77274239072SFrederic Weisbecker 	calltime = ktime_get();
77330dbb20eSAmérico Wang 	ret = fn();
77474239072SFrederic Weisbecker 	rettime = ktime_get();
77574239072SFrederic Weisbecker 	delta = ktime_sub(rettime, calltime);
77630dbb20eSAmérico Wang 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
777d62cf815SAndrew Morton 	printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
778ea676e84SAndrew Morton 		 fn, ret, duration);
77922c5c03bSKevin Winchester 
78022c5c03bSKevin Winchester 	return ret;
7818f0c45cdSIngo Molnar }
7828f0c45cdSIngo Molnar 
783e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn)
78422c5c03bSKevin Winchester {
78522c5c03bSKevin Winchester 	int count = preempt_count();
78622c5c03bSKevin Winchester 	int ret;
787ff1c8facSSteven Rostedt 	char msgbuf[64];
78822c5c03bSKevin Winchester 
7897b0b73d7SPrarit Bhargava 	if (initcall_blacklisted(fn))
7907b0b73d7SPrarit Bhargava 		return -EPERM;
7917b0b73d7SPrarit Bhargava 
79222c5c03bSKevin Winchester 	if (initcall_debug)
79322c5c03bSKevin Winchester 		ret = do_one_initcall_debug(fn);
79422c5c03bSKevin Winchester 	else
79522c5c03bSKevin Winchester 		ret = fn();
79622c5c03bSKevin Winchester 
797e662e1cfSCyrill Gorcunov 	msgbuf[0] = 0;
798e662e1cfSCyrill Gorcunov 
7991da177e4SLinus Torvalds 	if (preempt_count() != count) {
800bf5d770bSSteven Rostedt 		sprintf(msgbuf, "preemption imbalance ");
8014a2b4b22SPeter Zijlstra 		preempt_count_set(count);
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 	if (irqs_disabled()) {
804a76bfd0dSCyrill Gorcunov 		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
8051da177e4SLinus Torvalds 		local_irq_enable();
8061da177e4SLinus Torvalds 	}
807f91eb62fSSteven Rostedt 	WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
80859f9415fSArjan van de Ven 
80938addce8SEmese Revfy 	add_latent_entropy();
81030dbb20eSAmérico Wang 	return ret;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
813e0df154fSLinus Torvalds 
814026cee00SPawel Moll extern initcall_t __initcall_start[];
815026cee00SPawel Moll extern initcall_t __initcall0_start[];
816026cee00SPawel Moll extern initcall_t __initcall1_start[];
817026cee00SPawel Moll extern initcall_t __initcall2_start[];
818026cee00SPawel Moll extern initcall_t __initcall3_start[];
819026cee00SPawel Moll extern initcall_t __initcall4_start[];
820026cee00SPawel Moll extern initcall_t __initcall5_start[];
821026cee00SPawel Moll extern initcall_t __initcall6_start[];
822026cee00SPawel Moll extern initcall_t __initcall7_start[];
823026cee00SPawel Moll extern initcall_t __initcall_end[];
824026cee00SPawel Moll 
825026cee00SPawel Moll static initcall_t *initcall_levels[] __initdata = {
826026cee00SPawel Moll 	__initcall0_start,
827026cee00SPawel Moll 	__initcall1_start,
828026cee00SPawel Moll 	__initcall2_start,
829026cee00SPawel Moll 	__initcall3_start,
830026cee00SPawel Moll 	__initcall4_start,
831026cee00SPawel Moll 	__initcall5_start,
832026cee00SPawel Moll 	__initcall6_start,
833026cee00SPawel Moll 	__initcall7_start,
834026cee00SPawel Moll 	__initcall_end,
835026cee00SPawel Moll };
836026cee00SPawel Moll 
83796263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */
838026cee00SPawel Moll static char *initcall_level_names[] __initdata = {
8399fb48c74SJim Cromie 	"early",
8409fb48c74SJim Cromie 	"core",
8419fb48c74SJim Cromie 	"postcore",
8429fb48c74SJim Cromie 	"arch",
8439fb48c74SJim Cromie 	"subsys",
8449fb48c74SJim Cromie 	"fs",
8459fb48c74SJim Cromie 	"device",
8469fb48c74SJim Cromie 	"late",
847026cee00SPawel Moll };
848026cee00SPawel Moll 
849026cee00SPawel Moll static void __init do_initcall_level(int level)
850026cee00SPawel Moll {
851026cee00SPawel Moll 	initcall_t *fn;
852026cee00SPawel Moll 
85308746a65SKrzysztof Mazur 	strcpy(initcall_command_line, saved_command_line);
854026cee00SPawel Moll 	parse_args(initcall_level_names[level],
85508746a65SKrzysztof Mazur 		   initcall_command_line, __start___param,
856026cee00SPawel Moll 		   __stop___param - __start___param,
857026cee00SPawel Moll 		   level, level,
858ecc86170SLuis R. Rodriguez 		   NULL, &repair_env_string);
859026cee00SPawel Moll 
860026cee00SPawel Moll 	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
861026cee00SPawel Moll 		do_one_initcall(*fn);
862026cee00SPawel Moll }
863e0df154fSLinus Torvalds 
864e0df154fSLinus Torvalds static void __init do_initcalls(void)
865e0df154fSLinus Torvalds {
866026cee00SPawel Moll 	int level;
867e0df154fSLinus Torvalds 
86819efb72fSBorislav Petkov 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
869026cee00SPawel Moll 		do_initcall_level(level);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /*
8731da177e4SLinus Torvalds  * Ok, the machine is now initialized. None of the devices
8741da177e4SLinus Torvalds  * have been touched yet, but the CPU subsystem is up and
8751da177e4SLinus Torvalds  * running, and memory and process management works.
8761da177e4SLinus Torvalds  *
8771da177e4SLinus Torvalds  * Now we can finally start doing some real work..
8781da177e4SLinus Torvalds  */
8791da177e4SLinus Torvalds static void __init do_basic_setup(void)
8801da177e4SLinus Torvalds {
881759ee091SLai Jiangshan 	cpuset_init_smp();
88241ffe5d5SHugh Dickins 	shmem_init();
8831da177e4SLinus Torvalds 	driver_init();
884b04c3afbSEric W. Biederman 	init_irq_proc();
885b99b87f7SPeter Oberparleiter 	do_ctors();
886d5767c53SLinus Torvalds 	usermodehelper_enable();
887b0f84374Swangyanqing 	do_initcalls();
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8907babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void)
891c2147a50SEduard - Gabriel Munteanu {
892196a15b4SH Hartley Sweeten 	initcall_t *fn;
893c2147a50SEduard - Gabriel Munteanu 
894026cee00SPawel Moll 	for (fn = __initcall_start; fn < __initcall0_start; fn++)
895196a15b4SH Hartley Sweeten 		do_one_initcall(*fn);
896c2147a50SEduard - Gabriel Munteanu }
897c2147a50SEduard - Gabriel Munteanu 
898bb813f4cSTejun Heo /*
899bb813f4cSTejun Heo  * This function requests modules which should be loaded by default and is
900bb813f4cSTejun Heo  * called twice right after initrd is mounted and right before init is
901bb813f4cSTejun Heo  * exec'd.  If such modules are on either initrd or rootfs, they will be
902bb813f4cSTejun Heo  * loaded before control is passed to userland.
903bb813f4cSTejun Heo  */
904bb813f4cSTejun Heo void __init load_default_modules(void)
905bb813f4cSTejun Heo {
906bb813f4cSTejun Heo 	load_default_elevator_module();
907bb813f4cSTejun Heo }
908bb813f4cSTejun Heo 
909a74fb73cSAl Viro static int run_init_process(const char *init_filename)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	argv_init[0] = init_filename;
912c4ad8f98SLinus Torvalds 	return do_execve(getname_kernel(init_filename),
913ae903caaSAl Viro 		(const char __user *const __user *)argv_init,
914ae903caaSAl Viro 		(const char __user *const __user *)envp_init);
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
917ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename)
918ba24762bSMichael Opdenacker {
919ba24762bSMichael Opdenacker 	int ret;
920ba24762bSMichael Opdenacker 
921ba24762bSMichael Opdenacker 	ret = run_init_process(init_filename);
922ba24762bSMichael Opdenacker 
923ba24762bSMichael Opdenacker 	if (ret && ret != -ENOENT) {
924ba24762bSMichael Opdenacker 		pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
925ba24762bSMichael Opdenacker 		       init_filename, ret);
926ba24762bSMichael Opdenacker 	}
927ba24762bSMichael Opdenacker 
928ba24762bSMichael Opdenacker 	return ret;
929ba24762bSMichael Opdenacker }
930ba24762bSMichael Opdenacker 
931f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void);
932d6b21238SAl Viro 
9330f5bf6d0SLaura Abbott #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
93439290b38SAKASHI Takahiro bool rodata_enabled __ro_after_init = true;
935d2aa1acaSKees Cook static int __init set_debug_rodata(char *str)
936d2aa1acaSKees Cook {
937d2aa1acaSKees Cook 	return strtobool(str, &rodata_enabled);
938d2aa1acaSKees Cook }
939d2aa1acaSKees Cook __setup("rodata=", set_debug_rodata);
94039290b38SAKASHI Takahiro #endif
941d2aa1acaSKees Cook 
9420f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
943d2aa1acaSKees Cook static void mark_readonly(void)
944d2aa1acaSKees Cook {
9452959a5f7SJinbum Park 	if (rodata_enabled) {
946d2aa1acaSKees Cook 		mark_rodata_ro();
9472959a5f7SJinbum Park 		rodata_test();
9482959a5f7SJinbum Park 	} else
949d2aa1acaSKees Cook 		pr_info("Kernel memory protection disabled.\n");
950d2aa1acaSKees Cook }
951d2aa1acaSKees Cook #else
952d2aa1acaSKees Cook static inline void mark_readonly(void)
953d2aa1acaSKees Cook {
954d2aa1acaSKees Cook 	pr_warn("This architecture does not have kernel memory protection.\n");
955d2aa1acaSKees Cook }
956d2aa1acaSKees Cook #endif
957d2aa1acaSKees Cook 
958d6b21238SAl Viro static int __ref kernel_init(void *unused)
959ee5bfa64SVivek Goyal {
960ba24762bSMichael Opdenacker 	int ret;
961ba24762bSMichael Opdenacker 
962d6b21238SAl Viro 	kernel_init_freeable();
96322a9d645SArjan van de Ven 	/* need to finish all async __init code before freeing the memory */
96422a9d645SArjan van de Ven 	async_synchronize_full();
965*b80f0f6cSSteven Rostedt (VMware) 	ftrace_free_init_mem();
966ee5bfa64SVivek Goyal 	free_initmem();
967d2aa1acaSKees Cook 	mark_readonly();
968ee5bfa64SVivek Goyal 	system_state = SYSTEM_RUNNING;
969ee5bfa64SVivek Goyal 	numa_default_policy();
970ee5bfa64SVivek Goyal 
971967dcb8fSPaul E. McKenney 	rcu_end_inkernel_boot();
972967dcb8fSPaul E. McKenney 
973ee5bfa64SVivek Goyal 	if (ramdisk_execute_command) {
974ba24762bSMichael Opdenacker 		ret = run_init_process(ramdisk_execute_command);
975ba24762bSMichael Opdenacker 		if (!ret)
976a74fb73cSAl Viro 			return 0;
977ba24762bSMichael Opdenacker 		pr_err("Failed to execute %s (error %d)\n",
978ba24762bSMichael Opdenacker 		       ramdisk_execute_command, ret);
979ee5bfa64SVivek Goyal 	}
980ee5bfa64SVivek Goyal 
981ee5bfa64SVivek Goyal 	/*
982ee5bfa64SVivek Goyal 	 * We try each of these until one succeeds.
983ee5bfa64SVivek Goyal 	 *
984ee5bfa64SVivek Goyal 	 * The Bourne shell can be used instead of init if we are
985ee5bfa64SVivek Goyal 	 * trying to recover a really broken machine.
986ee5bfa64SVivek Goyal 	 */
987ee5bfa64SVivek Goyal 	if (execute_command) {
988ba24762bSMichael Opdenacker 		ret = run_init_process(execute_command);
989ba24762bSMichael Opdenacker 		if (!ret)
990a74fb73cSAl Viro 			return 0;
9916ef4536eSAndy Lutomirski 		panic("Requested init %s failed (error %d).",
9926ef4536eSAndy Lutomirski 		      execute_command, ret);
993ee5bfa64SVivek Goyal 	}
994ba24762bSMichael Opdenacker 	if (!try_to_run_init_process("/sbin/init") ||
995ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/etc/init") ||
996ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/init") ||
997ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/sh"))
998a74fb73cSAl Viro 		return 0;
999ee5bfa64SVivek Goyal 
1000ba24762bSMichael Opdenacker 	panic("No working init found.  Try passing init= option to kernel. "
10018c27ceffSMauro Carvalho Chehab 	      "See Linux Documentation/admin-guide/init.rst for guidance.");
1002ee5bfa64SVivek Goyal }
1003ee5bfa64SVivek Goyal 
1004f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void)
10051da177e4SLinus Torvalds {
1006b433c3d4SPeter Zijlstra 	/*
1007b433c3d4SPeter Zijlstra 	 * Wait until kthreadd is all set-up.
1008b433c3d4SPeter Zijlstra 	 */
1009b433c3d4SPeter Zijlstra 	wait_for_completion(&kthreadd_done);
101031a67102SLinus Torvalds 
101131a67102SLinus Torvalds 	/* Now the scheduler is fully set up and can do blocking allocations */
101231a67102SLinus Torvalds 	gfp_allowed_mask = __GFP_BITS_MASK;
101331a67102SLinus Torvalds 
101458568d2aSMiao Xie 	/*
101558568d2aSMiao Xie 	 * init can allocate pages on any node
101658568d2aSMiao Xie 	 */
10173c466d46SLai Jiangshan 	set_mems_allowed(node_states[N_MEMORY]);
10181da177e4SLinus Torvalds 	/*
10191da177e4SLinus Torvalds 	 * init can run on any cpu.
10201da177e4SLinus Torvalds 	 */
10211a2142afSRusty Russell 	set_cpus_allowed_ptr(current, cpu_all_mask);
10221da177e4SLinus Torvalds 
10239ec52099SCedric Le Goater 	cad_pid = task_pid(current);
10249ec52099SCedric Le Goater 
1025ca74a6f8SAndi Kleen 	smp_prepare_cpus(setup_max_cpus);
10261da177e4SLinus Torvalds 
10273347fa09STejun Heo 	workqueue_init();
10283347fa09STejun Heo 
10291da177e4SLinus Torvalds 	do_pre_smp_initcalls();
1030004417a6SPeter Zijlstra 	lockup_detector_init();
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	smp_init();
10331da177e4SLinus Torvalds 	sched_init_smp();
10341da177e4SLinus Torvalds 
10350e1cc95bSMel Gorman 	page_alloc_init_late();
10360e1cc95bSMel Gorman 
10371da177e4SLinus Torvalds 	do_basic_setup();
10381da177e4SLinus Torvalds 
10392bd3a997SEric W. Biederman 	/* Open the /dev/console on the rootfs, this should never fail */
10402bd3a997SEric W. Biederman 	if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1041ea676e84SAndrew Morton 		pr_err("Warning: unable to open an initial console.\n");
10422bd3a997SEric W. Biederman 
10432bd3a997SEric W. Biederman 	(void) sys_dup(0);
10442bd3a997SEric W. Biederman 	(void) sys_dup(0);
10451da177e4SLinus Torvalds 	/*
10461da177e4SLinus Torvalds 	 * check if there is an early userspace init.  If yes, let it do all
10471da177e4SLinus Torvalds 	 * the work
10481da177e4SLinus Torvalds 	 */
1049ffdfc409SOlof Johansson 
1050ffdfc409SOlof Johansson 	if (!ramdisk_execute_command)
1051ffdfc409SOlof Johansson 		ramdisk_execute_command = "/init";
1052ffdfc409SOlof Johansson 
1053ffdfc409SOlof Johansson 	if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
1054ffdfc409SOlof Johansson 		ramdisk_execute_command = NULL;
10551da177e4SLinus Torvalds 		prepare_namespace();
1056ffdfc409SOlof Johansson 	}
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	/*
10591da177e4SLinus Torvalds 	 * Ok, we have completed the initial bootup, and
10601da177e4SLinus Torvalds 	 * we're essentially up and running. Get rid of the
10611da177e4SLinus Torvalds 	 * initmem segments and start the user-mode stuff..
1062c9cd2ce2SDmitry Kasatkin 	 *
1063c9cd2ce2SDmitry Kasatkin 	 * rootfs is available now, try loading the public keys
1064c9cd2ce2SDmitry Kasatkin 	 * and default modules
10651da177e4SLinus Torvalds 	 */
1066bb813f4cSTejun Heo 
1067c9cd2ce2SDmitry Kasatkin 	integrity_load_keys();
1068bb813f4cSTejun Heo 	load_default_modules();
10691da177e4SLinus Torvalds }
1070