xref: /linux-6.15/init/main.c (revision d2aa1aca)
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>
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/proc_fs.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/syscalls.h>
199b5609fdSIngo Molnar #include <linux/stackprotector.h>
201da177e4SLinus Torvalds #include <linux/string.h>
211da177e4SLinus Torvalds #include <linux/ctype.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/ioport.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/initrd.h>
261da177e4SLinus Torvalds #include <linux/bootmem.h>
274a7a16dcSLen Brown #include <linux/acpi.h>
281da177e4SLinus Torvalds #include <linux/tty.h>
291da177e4SLinus Torvalds #include <linux/percpu.h>
301da177e4SLinus Torvalds #include <linux/kmod.h>
31db64fe02SNick Piggin #include <linux/vmalloc.h>
321da177e4SLinus Torvalds #include <linux/kernel_stat.h>
33d7cd5611SRusty Russell #include <linux/start_kernel.h>
341da177e4SLinus Torvalds #include <linux/security.h>
353d442233SJens Axboe #include <linux/smp.h>
361da177e4SLinus Torvalds #include <linux/profile.h>
371da177e4SLinus Torvalds #include <linux/rcupdate.h>
381da177e4SLinus Torvalds #include <linux/moduleparam.h>
391da177e4SLinus Torvalds #include <linux/kallsyms.h>
401da177e4SLinus Torvalds #include <linux/writeback.h>
411da177e4SLinus Torvalds #include <linux/cpu.h>
421da177e4SLinus Torvalds #include <linux/cpuset.h>
43ddbcc7e8SPaul Menage #include <linux/cgroup.h>
441da177e4SLinus Torvalds #include <linux/efi.h>
45906568c9SThomas Gleixner #include <linux/tick.h>
466168a702SAndrew Morton #include <linux/interrupt.h>
47c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
48ca74e92bSShailabh Nagar #include <linux/delayacct.h>
491da177e4SLinus Torvalds #include <linux/unistd.h>
501da177e4SLinus Torvalds #include <linux/rmap.h>
511da177e4SLinus Torvalds #include <linux/mempolicy.h>
521da177e4SLinus Torvalds #include <linux/key.h>
53b6cd0b77SAdrian Bunk #include <linux/buffer_head.h>
54eefa864bSJoonsoo Kim #include <linux/page_ext.h>
559a11b49aSIngo Molnar #include <linux/debug_locks.h>
563ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
57fbb9ce95SIngo Molnar #include <linux/lockdep.h>
583c7b4e6bSCatalin Marinas #include <linux/kmemleak.h>
5984d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
601f21782eSAdrian Bunk #include <linux/device.h>
6173c27992SEric W. Biederman #include <linux/kthread.h>
62e6fe6649SAdrian Bunk #include <linux/sched.h>
63a1c9eea9SAdrian Bunk #include <linux/signal.h>
64199f0ca5SAkinobu Mita #include <linux/idr.h>
650b4b3827SJason Wessel #include <linux/kgdb.h>
6668bf21aaSSteven Rostedt #include <linux/ftrace.h>
6722a9d645SArjan van de Ven #include <linux/async.h>
68dfec072eSVegard Nossum #include <linux/kmemcheck.h>
696ae6996aSFeng Tang #include <linux/sfi.h>
702b2af54aSKay Sievers #include <linux/shmem_fs.h>
715a0e3ad6STejun Heo #include <linux/slab.h>
7224a24bb6SPeter Zijlstra #include <linux/perf_event.h>
734a9d4b02SAl Viro #include <linux/file.h>
74a74fb73cSAl Viro #include <linux/ptrace.h>
75bb813f4cSTejun Heo #include <linux/blkdev.h>
76bb813f4cSTejun Heo #include <linux/elevator.h>
7738ff87f7SStephen Boyd #include <linux/sched_clock.h>
7865f382fdSFrederic Weisbecker #include <linux/context_tracking.h>
7947d06e53STheodore Ts'o #include <linux/random.h>
807b0b73d7SPrarit Bhargava #include <linux/list.h>
81c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h>
82e149ed2bSAl Viro #include <linux/proc_ns.h>
830ddab1d2SToshi Kani #include <linux/io.h>
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #include <asm/io.h>
861da177e4SLinus Torvalds #include <asm/bugs.h>
871da177e4SLinus Torvalds #include <asm/setup.h>
88a940199fSAndi Kleen #include <asm/sections.h>
8937b73c82SArjan van de Ven #include <asm/cacheflush.h>
901da177e4SLinus Torvalds 
91aae5f662SSam Ravnborg static int kernel_init(void *);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds extern void init_IRQ(void);
94ff691f6eSHeinrich Schuchardt extern void fork_init(void);
951da177e4SLinus Torvalds extern void radix_tree_init(void);
961da177e4SLinus Torvalds 
972ce802f6STejun Heo /*
982ce802f6STejun Heo  * Debug helper: via this flag we know that we are in 'early bootup code'
992ce802f6STejun Heo  * where only the boot processor is running with IRQ disabled.  This means
1002ce802f6STejun Heo  * two things - IRQ must not be enabled before the flag is cleared and some
1012ce802f6STejun Heo  * operations which are not allowed with IRQ disabled are allowed while the
1022ce802f6STejun Heo  * flag is set.
1032ce802f6STejun Heo  */
1042ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly;
1052ce802f6STejun Heo 
106a6826048SPaul E. McKenney enum system_states system_state __read_mostly;
1071da177e4SLinus Torvalds EXPORT_SYMBOL(system_state);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * Boot command-line arguments
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
1131da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds extern void time_init(void);
1161da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */
117d2e3192bSJan Beulich void (*__initdata late_time_init)(void);
1181da177e4SLinus Torvalds 
11930d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */
12030d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE];
12130d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */
12230d7e0d4SAlon Bar-Lev char *saved_command_line;
12330d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */
12430d7e0d4SAlon Bar-Lev static char *static_command_line;
12508746a65SKrzysztof Mazur /* Command line for per-initcall parameter parsing */
12608746a65SKrzysztof Mazur static char *initcall_command_line;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static char *execute_command;
129ffdfc409SOlof Johansson static char *ramdisk_execute_command;
1301da177e4SLinus Torvalds 
1318b3b2955SJan Beulich /*
132c4b2c0c5SHannes Frederic Sowa  * Used to generate warnings if static_key manipulation functions are used
133c4b2c0c5SHannes Frederic Sowa  * before jump_label_init is called.
134c4b2c0c5SHannes Frederic Sowa  */
135dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly;
136c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized);
137c4b2c0c5SHannes Frederic Sowa 
138c4b2c0c5SHannes Frederic Sowa /*
1398b3b2955SJan Beulich  * If set, this is an indication to the drivers that reset the underlying
1408b3b2955SJan Beulich  * device before going ahead with the initialization otherwise driver might
1418b3b2955SJan Beulich  * rely on the BIOS and skip the reset operation.
1428b3b2955SJan Beulich  *
1438b3b2955SJan Beulich  * This is useful if kernel is booting in an unreliable environment.
144fc454fdcSFrans Klaver  * For ex. kdump situation where previous kernel has crashed, BIOS has been
1458b3b2955SJan Beulich  * skipped and devices will be in unknown state.
1468b3b2955SJan Beulich  */
1478b3b2955SJan Beulich unsigned int reset_devices;
1488b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices);
1491da177e4SLinus Torvalds 
1507e96287dSVivek Goyal static int __init set_reset_devices(char *str)
1517e96287dSVivek Goyal {
1527e96287dSVivek Goyal 	reset_devices = 1;
1537e96287dSVivek Goyal 	return 1;
1547e96287dSVivek Goyal }
1557e96287dSVivek Goyal 
1567e96287dSVivek Goyal __setup("reset_devices", set_reset_devices);
1577e96287dSVivek Goyal 
158d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
159d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1601da177e4SLinus Torvalds static const char *panic_later, *panic_param;
1611da177e4SLinus Torvalds 
162914dcaa8SRusty Russell extern const struct obs_kernel_param __setup_start[], __setup_end[];
1631da177e4SLinus Torvalds 
16431c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line)
1651da177e4SLinus Torvalds {
166914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
16731c025b5SYaowei Bai 	bool had_early_param = false;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	p = __setup_start;
1701da177e4SLinus Torvalds 	do {
1711da177e4SLinus Torvalds 		int n = strlen(p->str);
172b1e4d20cSMichal Schmidt 		if (parameqn(line, p->str, n)) {
1731da177e4SLinus Torvalds 			if (p->early) {
17433df0d19SRusty Russell 				/* Already done in parse_early_param?
17533df0d19SRusty Russell 				 * (Needs exact match on param part).
17633df0d19SRusty Russell 				 * Keep iterating, as we can have early
17733df0d19SRusty Russell 				 * params and __setups of same names 8( */
1781da177e4SLinus Torvalds 				if (line[n] == '\0' || line[n] == '=')
17931c025b5SYaowei Bai 					had_early_param = true;
1801da177e4SLinus Torvalds 			} else if (!p->setup_func) {
181ea676e84SAndrew Morton 				pr_warn("Parameter %s is obsolete, ignored\n",
182ea676e84SAndrew Morton 					p->str);
18331c025b5SYaowei Bai 				return true;
1841da177e4SLinus Torvalds 			} else if (p->setup_func(line + n))
18531c025b5SYaowei Bai 				return true;
1861da177e4SLinus Torvalds 		}
1871da177e4SLinus Torvalds 		p++;
1881da177e4SLinus Torvalds 	} while (p < __setup_end);
18933df0d19SRusty Russell 
19033df0d19SRusty Russell 	return had_early_param;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds  * This should be approx 2 Bo*oMips to start (note initial shift), and will
1951da177e4SLinus Torvalds  * still work even if initially too large, it will just take slightly longer
1961da177e4SLinus Torvalds  */
1971da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12);
1981da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static int __init debug_kernel(char *str)
2011da177e4SLinus Torvalds {
202a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
203f6f21c81SYinghai Lu 	return 0;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int __init quiet_kernel(char *str)
2071da177e4SLinus Torvalds {
208a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_QUIET;
209f6f21c81SYinghai Lu 	return 0;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
212f6f21c81SYinghai Lu early_param("debug", debug_kernel);
213f6f21c81SYinghai Lu early_param("quiet", quiet_kernel);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds static int __init loglevel(char *str)
2161da177e4SLinus Torvalds {
217808bf29bSAlexander Sverdlin 	int newlevel;
218808bf29bSAlexander Sverdlin 
219808bf29bSAlexander Sverdlin 	/*
220808bf29bSAlexander Sverdlin 	 * Only update loglevel value when a correct setting was passed,
221808bf29bSAlexander Sverdlin 	 * to prevent blind crashes (when loglevel being set to 0) that
222808bf29bSAlexander Sverdlin 	 * are quite hard to debug
223808bf29bSAlexander Sverdlin 	 */
224808bf29bSAlexander Sverdlin 	if (get_option(&str, &newlevel)) {
225808bf29bSAlexander Sverdlin 		console_loglevel = newlevel;
226d9d4fcfeSAlex Riesen 		return 0;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
229808bf29bSAlexander Sverdlin 	return -EINVAL;
230808bf29bSAlexander Sverdlin }
231808bf29bSAlexander Sverdlin 
232f6f21c81SYinghai Lu early_param("loglevel", loglevel);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */
235ecc86170SLuis R. Rodriguez static int __init repair_env_string(char *param, char *val,
236ecc86170SLuis R. Rodriguez 				    const char *unused, void *arg)
237a99cd112SChris Metcalf {
2381da177e4SLinus Torvalds 	if (val) {
2391da177e4SLinus Torvalds 		/* param=val or param="val"? */
2401da177e4SLinus Torvalds 		if (val == param+strlen(param)+1)
2411da177e4SLinus Torvalds 			val[-1] = '=';
2421da177e4SLinus Torvalds 		else if (val == param+strlen(param)+2) {
2431da177e4SLinus Torvalds 			val[-2] = '=';
2441da177e4SLinus Torvalds 			memmove(val-1, val, strlen(val)+1);
2451da177e4SLinus Torvalds 			val--;
2461da177e4SLinus Torvalds 		} else
2471da177e4SLinus Torvalds 			BUG();
2481da177e4SLinus Torvalds 	}
249a99cd112SChris Metcalf 	return 0;
250a99cd112SChris Metcalf }
251a99cd112SChris Metcalf 
25251e158c1SRusty Russell /* Anything after -- gets handed straight to init. */
253ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val,
254ecc86170SLuis R. Rodriguez 			       const char *unused, void *arg)
25551e158c1SRusty Russell {
25651e158c1SRusty Russell 	unsigned int i;
25751e158c1SRusty Russell 
25851e158c1SRusty Russell 	if (panic_later)
25951e158c1SRusty Russell 		return 0;
26051e158c1SRusty Russell 
261ecc86170SLuis R. Rodriguez 	repair_env_string(param, val, unused, NULL);
26251e158c1SRusty Russell 
26351e158c1SRusty Russell 	for (i = 0; argv_init[i]; i++) {
26451e158c1SRusty Russell 		if (i == MAX_INIT_ARGS) {
26551e158c1SRusty Russell 			panic_later = "init";
26651e158c1SRusty Russell 			panic_param = param;
26751e158c1SRusty Russell 			return 0;
26851e158c1SRusty Russell 		}
26951e158c1SRusty Russell 	}
27051e158c1SRusty Russell 	argv_init[i] = param;
27151e158c1SRusty Russell 	return 0;
27251e158c1SRusty Russell }
27351e158c1SRusty Russell 
274a99cd112SChris Metcalf /*
275a99cd112SChris Metcalf  * Unknown boot options get handed to init, unless they look like
276a99cd112SChris Metcalf  * unused parameters (modprobe will find them in /proc/cmdline).
277a99cd112SChris Metcalf  */
278ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val,
279ecc86170SLuis R. Rodriguez 				     const char *unused, void *arg)
280a99cd112SChris Metcalf {
281ecc86170SLuis R. Rodriguez 	repair_env_string(param, val, unused, NULL);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* Handle obsolete-style parameters */
2841da177e4SLinus Torvalds 	if (obsolete_checksetup(param))
2851da177e4SLinus Torvalds 		return 0;
2861da177e4SLinus Torvalds 
287f066a4f6SRusty Russell 	/* Unused module parameter. */
288f066a4f6SRusty Russell 	if (strchr(param, '.') && (!val || strchr(param, '.') < val))
2891da177e4SLinus Torvalds 		return 0;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (panic_later)
2921da177e4SLinus Torvalds 		return 0;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (val) {
2951da177e4SLinus Torvalds 		/* Environment option */
2961da177e4SLinus Torvalds 		unsigned int i;
2971da177e4SLinus Torvalds 		for (i = 0; envp_init[i]; i++) {
2981da177e4SLinus Torvalds 			if (i == MAX_INIT_ENVS) {
299499a4584STetsuo Handa 				panic_later = "env";
3001da177e4SLinus Torvalds 				panic_param = param;
3011da177e4SLinus Torvalds 			}
3021da177e4SLinus Torvalds 			if (!strncmp(param, envp_init[i], val - param))
3031da177e4SLinus Torvalds 				break;
3041da177e4SLinus Torvalds 		}
3051da177e4SLinus Torvalds 		envp_init[i] = param;
3061da177e4SLinus Torvalds 	} else {
3071da177e4SLinus Torvalds 		/* Command line option */
3081da177e4SLinus Torvalds 		unsigned int i;
3091da177e4SLinus Torvalds 		for (i = 0; argv_init[i]; i++) {
3101da177e4SLinus Torvalds 			if (i == MAX_INIT_ARGS) {
311499a4584STetsuo Handa 				panic_later = "init";
3121da177e4SLinus Torvalds 				panic_param = param;
3131da177e4SLinus Torvalds 			}
3141da177e4SLinus Torvalds 		}
3151da177e4SLinus Torvalds 		argv_init[i] = param;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static int __init init_setup(char *str)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	unsigned int i;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	execute_command = str;
3251da177e4SLinus Torvalds 	/*
3261da177e4SLinus Torvalds 	 * In case LILO is going to boot us with default command line,
3271da177e4SLinus Torvalds 	 * it prepends "auto" before the whole cmdline which makes
3281da177e4SLinus Torvalds 	 * the shell think it should execute a script with such name.
3291da177e4SLinus Torvalds 	 * So we ignore all arguments entered _before_ init=... [MJ]
3301da177e4SLinus Torvalds 	 */
3311da177e4SLinus Torvalds 	for (i = 1; i < MAX_INIT_ARGS; i++)
3321da177e4SLinus Torvalds 		argv_init[i] = NULL;
3331da177e4SLinus Torvalds 	return 1;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds __setup("init=", init_setup);
3361da177e4SLinus Torvalds 
337ffdfc409SOlof Johansson static int __init rdinit_setup(char *str)
338ffdfc409SOlof Johansson {
339ffdfc409SOlof Johansson 	unsigned int i;
340ffdfc409SOlof Johansson 
341ffdfc409SOlof Johansson 	ramdisk_execute_command = str;
342ffdfc409SOlof Johansson 	/* See "auto" comment in init_setup */
343ffdfc409SOlof Johansson 	for (i = 1; i < MAX_INIT_ARGS; i++)
344ffdfc409SOlof Johansson 		argv_init[i] = NULL;
345ffdfc409SOlof Johansson 	return 1;
346ffdfc409SOlof Johansson }
347ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup);
348ffdfc409SOlof Johansson 
3491da177e4SLinus Torvalds #ifndef CONFIG_SMP
35034db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS;
351e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { }
3521da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { }
3531da177e4SLinus Torvalds #endif
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds /*
35630d7e0d4SAlon Bar-Lev  * We need to store the untouched command line for future reference.
35730d7e0d4SAlon Bar-Lev  * We also need to store the touched command line since the parameter
35830d7e0d4SAlon Bar-Lev  * parsing is performed in place, and we should allow a component to
35930d7e0d4SAlon Bar-Lev  * store reference of name/value for future reference.
36030d7e0d4SAlon Bar-Lev  */
36130d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line)
36230d7e0d4SAlon Bar-Lev {
363098b081bSSantosh Shilimkar 	saved_command_line =
364098b081bSSantosh Shilimkar 		memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
365098b081bSSantosh Shilimkar 	initcall_command_line =
366098b081bSSantosh Shilimkar 		memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
367098b081bSSantosh Shilimkar 	static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
36830d7e0d4SAlon Bar-Lev 	strcpy(saved_command_line, boot_command_line);
36930d7e0d4SAlon Bar-Lev 	strcpy(static_command_line, command_line);
37030d7e0d4SAlon Bar-Lev }
37130d7e0d4SAlon Bar-Lev 
37230d7e0d4SAlon Bar-Lev /*
3731da177e4SLinus Torvalds  * We need to finalize in a non-__init function or else race conditions
3741da177e4SLinus Torvalds  * between the root thread and the init thread may cause start_kernel to
3751da177e4SLinus Torvalds  * be reaped by free_initmem before the root thread has proceeded to
3761da177e4SLinus Torvalds  * cpu_idle.
3771da177e4SLinus Torvalds  *
3781da177e4SLinus Torvalds  * gcc-3.4 accidentally inlines this function, so use noinline.
3791da177e4SLinus Torvalds  */
3801da177e4SLinus Torvalds 
381b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done);
382b433c3d4SPeter Zijlstra 
383f99ebf0aSRakib Mullick static noinline void __init_refok rest_init(void)
3841da177e4SLinus Torvalds {
38573c27992SEric W. Biederman 	int pid;
38673c27992SEric W. Biederman 
3877db905e6SPaul E. McKenney 	rcu_scheduler_starting();
38800df35f9SPaul E. McKenney 	smpboot_thread_init();
389b433c3d4SPeter Zijlstra 	/*
39097158569SPeter Zijlstra 	 * We need to spawn init first so that it obtains pid 1, however
391b433c3d4SPeter Zijlstra 	 * the init task will end up wanting to create kthreads, which, if
392b433c3d4SPeter Zijlstra 	 * we schedule it before we create kthreadd, will OOPS.
393b433c3d4SPeter Zijlstra 	 */
39434a1b723SOleg Nesterov 	kernel_thread(kernel_init, NULL, CLONE_FS);
3951da177e4SLinus Torvalds 	numa_default_policy();
39673c27992SEric W. Biederman 	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
397d11c563dSPaul E. McKenney 	rcu_read_lock();
3985cd20455SPavel Emelyanov 	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
399d11c563dSPaul E. McKenney 	rcu_read_unlock();
400b433c3d4SPeter Zijlstra 	complete(&kthreadd_done);
401f340c0d1SIngo Molnar 
402f340c0d1SIngo Molnar 	/*
403f340c0d1SIngo Molnar 	 * The boot idle thread must execute schedule()
4041df21055SIngo Molnar 	 * at least once to get things moving:
405f340c0d1SIngo Molnar 	 */
4061df21055SIngo Molnar 	init_idle_bootup_task(current);
407bd2f5536SThomas Gleixner 	schedule_preempt_disabled();
4085bfb5d69SNick Piggin 	/* Call into cpu_idle with preempt disabled */
409a1a04ec3SThomas Gleixner 	cpu_startup_entry(CPUHP_ONLINE);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /* Check for early params. */
413ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val,
414ecc86170SLuis R. Rodriguez 				 const char *unused, void *arg)
4151da177e4SLinus Torvalds {
416914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	for (p = __setup_start; p < __setup_end; p++) {
419b1e4d20cSMichal Schmidt 		if ((p->early && parameq(param, p->str)) ||
42018a8bd94SYinghai Lu 		    (strcmp(param, "console") == 0 &&
42118a8bd94SYinghai Lu 		     strcmp(p->str, "earlycon") == 0)
42218a8bd94SYinghai Lu 		) {
4231da177e4SLinus Torvalds 			if (p->setup_func(val) != 0)
424ea676e84SAndrew Morton 				pr_warn("Malformed early option '%s'\n", param);
4251da177e4SLinus Torvalds 		}
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 	/* We accept everything at this stage. */
4281da177e4SLinus Torvalds 	return 0;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
43113977091SMagnus Damm void __init parse_early_options(char *cmdline)
43213977091SMagnus Damm {
433ecc86170SLuis R. Rodriguez 	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
434ecc86170SLuis R. Rodriguez 		   do_early_param);
43513977091SMagnus Damm }
43613977091SMagnus Damm 
4371da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */
4381da177e4SLinus Torvalds void __init parse_early_param(void)
4391da177e4SLinus Torvalds {
440dd4d9fecSFabian Frederick 	static int done __initdata;
441dd4d9fecSFabian Frederick 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if (done)
4441da177e4SLinus Torvalds 		return;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/* All fall through to do_early_param. */
44730d7e0d4SAlon Bar-Lev 	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
44813977091SMagnus Damm 	parse_early_options(tmp_cmdline);
4491da177e4SLinus Torvalds 	done = 1;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  *	Activate the first processor.
4541da177e4SLinus Torvalds  */
4551da177e4SLinus Torvalds 
45644fd2299SStas Sergeev static void __init boot_cpu_init(void)
45744fd2299SStas Sergeev {
45844fd2299SStas Sergeev 	int cpu = smp_processor_id();
45944fd2299SStas Sergeev 	/* Mark the boot cpu "present", "online" etc for SMP and UP case */
460915441b6SRusty Russell 	set_cpu_online(cpu, true);
461933b0618SPeter Zijlstra 	set_cpu_active(cpu, true);
462915441b6SRusty Russell 	set_cpu_present(cpu, true);
463915441b6SRusty Russell 	set_cpu_possible(cpu, true);
46444fd2299SStas Sergeev }
46544fd2299SStas Sergeev 
466839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void)
467033ab7f8SAndrew Morton {
468033ab7f8SAndrew Morton }
469033ab7f8SAndrew Morton 
470eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE
4718c9843e5SBenjamin Herrenschmidt void __init __weak thread_info_cache_init(void)
4728c9843e5SBenjamin Herrenschmidt {
4738c9843e5SBenjamin Herrenschmidt }
474eded09ccSDavid Howells #endif
4758c9843e5SBenjamin Herrenschmidt 
476444f478fSPekka Enberg /*
477444f478fSPekka Enberg  * Set up kernel memory allocators
478444f478fSPekka Enberg  */
479444f478fSPekka Enberg static void __init mm_init(void)
480444f478fSPekka Enberg {
481eefa864bSJoonsoo Kim 	/*
482eefa864bSJoonsoo Kim 	 * page_ext requires contiguous pages,
483eefa864bSJoonsoo Kim 	 * bigger than MAX_ORDER unless SPARSEMEM.
484eefa864bSJoonsoo Kim 	 */
485eefa864bSJoonsoo Kim 	page_ext_init_flatmem();
486444f478fSPekka Enberg 	mem_init();
487444f478fSPekka Enberg 	kmem_cache_init();
488099a19d9STejun Heo 	percpu_init_late();
489b35f1819SKirill A. Shutemov 	pgtable_init();
490444f478fSPekka Enberg 	vmalloc_init();
4910ddab1d2SToshi Kani 	ioremap_huge_init();
492444f478fSPekka Enberg }
493444f478fSPekka Enberg 
494722a9f92SAndi Kleen asmlinkage __visible void __init start_kernel(void)
4951da177e4SLinus Torvalds {
496dd4d9fecSFabian Frederick 	char *command_line;
497dd4d9fecSFabian Frederick 	char *after_dashes;
498033ab7f8SAndrew Morton 
4991da177e4SLinus Torvalds 	/*
500fbb9ce95SIngo Molnar 	 * Need to run as early as possible, to initialize the
501fbb9ce95SIngo Molnar 	 * lockdep hash:
502fbb9ce95SIngo Molnar 	 */
503fbb9ce95SIngo Molnar 	lockdep_init();
504d4311ff1SAaron Tomlin 	set_task_stack_end_magic(&init_task);
50573839c5bSMing Lei 	smp_setup_processor_id();
5063ac7fe5aSThomas Gleixner 	debug_objects_early_init();
50742059429SIngo Molnar 
50842059429SIngo Molnar 	/*
50942059429SIngo Molnar 	 * Set up the the initial canary ASAP:
51042059429SIngo Molnar 	 */
51142059429SIngo Molnar 	boot_init_stack_canary();
51242059429SIngo Molnar 
513ddbcc7e8SPaul Menage 	cgroup_init_early();
514fbb9ce95SIngo Molnar 
515fbb9ce95SIngo Molnar 	local_irq_disable();
5162ce802f6STejun Heo 	early_boot_irqs_disabled = true;
517fbb9ce95SIngo Molnar 
518fbb9ce95SIngo Molnar /*
5191da177e4SLinus Torvalds  * Interrupts are still disabled. Do necessary setups, then
5201da177e4SLinus Torvalds  * enable them
5211da177e4SLinus Torvalds  */
52244fd2299SStas Sergeev 	boot_cpu_init();
5231da177e4SLinus Torvalds 	page_address_init();
524ea676e84SAndrew Morton 	pr_notice("%s", linux_banner);
5251da177e4SLinus Torvalds 	setup_arch(&command_line);
5266345d24dSLinus Torvalds 	mm_init_cpumask(&init_mm);
52730d7e0d4SAlon Bar-Lev 	setup_command_line(command_line);
528e0982e90SMike Travis 	setup_nr_cpu_ids();
529d6647bdfSTejun Heo 	setup_per_cpu_areas();
53044fd2299SStas Sergeev 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
5311da177e4SLinus Torvalds 
5329adb62a5SJiang Liu 	build_all_zonelists(NULL, NULL);
53383b519e8SPekka Enberg 	page_alloc_init();
53483b519e8SPekka Enberg 
535ea676e84SAndrew Morton 	pr_notice("Kernel command line: %s\n", boot_command_line);
53683b519e8SPekka Enberg 	parse_early_param();
53751e158c1SRusty Russell 	after_dashes = parse_args("Booting kernel",
53851e158c1SRusty Russell 				  static_command_line, __start___param,
53983b519e8SPekka Enberg 				  __stop___param - __start___param,
540ecc86170SLuis R. Rodriguez 				  -1, -1, NULL, &unknown_bootoption);
5413438cf54SDaniel Thompson 	if (!IS_ERR_OR_NULL(after_dashes))
54251e158c1SRusty Russell 		parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
543ecc86170SLuis R. Rodriguez 			   NULL, set_init_arg);
54497ce2c88SJeremy Fitzhardinge 
54597ce2c88SJeremy Fitzhardinge 	jump_label_init();
54697ce2c88SJeremy Fitzhardinge 
54783b519e8SPekka Enberg 	/*
54883b519e8SPekka Enberg 	 * These use large bootmem allocations and must precede
54983b519e8SPekka Enberg 	 * kmem_cache_init()
55083b519e8SPekka Enberg 	 */
551162a7e75SMike Travis 	setup_log_buf(0);
55283b519e8SPekka Enberg 	pidhash_init();
55383b519e8SPekka Enberg 	vfs_caches_init_early();
55483b519e8SPekka Enberg 	sort_main_extable();
55583b519e8SPekka Enberg 	trap_init();
556444f478fSPekka Enberg 	mm_init();
557de03c72cSKOSAKI Motohiro 
5581da177e4SLinus Torvalds 	/*
5591da177e4SLinus Torvalds 	 * Set up the scheduler prior starting any interrupts (such as the
5601da177e4SLinus Torvalds 	 * timer interrupt). Full topology setup happens at smp_init()
5611da177e4SLinus Torvalds 	 * time - but meanwhile we still have a functioning scheduler.
5621da177e4SLinus Torvalds 	 */
5631da177e4SLinus Torvalds 	sched_init();
5641da177e4SLinus Torvalds 	/*
5651da177e4SLinus Torvalds 	 * Disable preemption - early bootup scheduling is extremely
5661da177e4SLinus Torvalds 	 * fragile until we cpu_idle() for the first time.
5671da177e4SLinus Torvalds 	 */
5681da177e4SLinus Torvalds 	preempt_disable();
569dd4d9fecSFabian Frederick 	if (WARN(!irqs_disabled(),
570dd4d9fecSFabian Frederick 		 "Interrupts were enabled *very* early, fixing it\n"))
571c4a68306SArd van Breemen 		local_irq_disable();
5729f58a205SPeter Zijlstra 	idr_init_cache();
5731da177e4SLinus Torvalds 	rcu_init();
5745f893b26SSteven Rostedt (Red Hat) 
5755f893b26SSteven Rostedt (Red Hat) 	/* trace_printk() and trace points may be used after this */
5765f893b26SSteven Rostedt (Red Hat) 	trace_init();
5775f893b26SSteven Rostedt (Red Hat) 
57865f382fdSFrederic Weisbecker 	context_tracking_init();
579773e3eb7SYinghai Lu 	radix_tree_init();
5800b8f1efaSYinghai Lu 	/* init some links before init_ISA_irqs() */
5810b8f1efaSYinghai Lu 	early_irq_init();
5821da177e4SLinus Torvalds 	init_IRQ();
583ad2b1353SThomas Gleixner 	tick_init();
584d6dd50e0SLinus Torvalds 	rcu_init_nohz();
5851da177e4SLinus Torvalds 	init_timers();
586c0a31329SThomas Gleixner 	hrtimers_init();
5871da177e4SLinus Torvalds 	softirq_init();
588ad596171Sjohn stultz 	timekeeping_init();
58988fecaa2Sjohn stultz 	time_init();
59038ff87f7SStephen Boyd 	sched_clock_postinit();
5919e630205SStephane Eranian 	perf_event_init();
59293e02814SHeiko Carstens 	profile_init();
593d8ad7d11STakao Indoh 	call_function_init();
594f91eb62fSSteven Rostedt 	WARN(!irqs_disabled(), "Interrupts were enabled early\n");
5952ce802f6STejun Heo 	early_boot_irqs_disabled = false;
59693e02814SHeiko Carstens 	local_irq_enable();
597dcce284aSBenjamin Herrenschmidt 
5987e85ee0cSPekka Enberg 	kmem_cache_init_late();
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/*
6011da177e4SLinus Torvalds 	 * HACK ALERT! This is early. We're enabling the console before
6021da177e4SLinus Torvalds 	 * we've done PCI setups etc, and console_init() must be aware of
6031da177e4SLinus Torvalds 	 * this. But we do want output early, in case something goes wrong.
6041da177e4SLinus Torvalds 	 */
6051da177e4SLinus Torvalds 	console_init();
6061da177e4SLinus Torvalds 	if (panic_later)
607499a4584STetsuo Handa 		panic("Too many boot %s vars at `%s'", panic_later,
608499a4584STetsuo Handa 		      panic_param);
609fbb9ce95SIngo Molnar 
610fbb9ce95SIngo Molnar 	lockdep_info();
611fbb9ce95SIngo Molnar 
6129a11b49aSIngo Molnar 	/*
6139a11b49aSIngo Molnar 	 * Need to run this when irqs are enabled, because it wants
6149a11b49aSIngo Molnar 	 * to self-test [hard/soft]-irqs on/off lock inversion bugs
6159a11b49aSIngo Molnar 	 * too:
6169a11b49aSIngo Molnar 	 */
6179a11b49aSIngo Molnar 	locking_selftest();
6189a11b49aSIngo Molnar 
6191da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
6201da177e4SLinus Torvalds 	if (initrd_start && !initrd_below_start_ok &&
621bd673c7cSGeert Uytterhoeven 	    page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
622ea676e84SAndrew Morton 		pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
623bd673c7cSGeert Uytterhoeven 		    page_to_pfn(virt_to_page((void *)initrd_start)),
624bd673c7cSGeert Uytterhoeven 		    min_low_pfn);
6251da177e4SLinus Torvalds 		initrd_start = 0;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds #endif
628eefa864bSJoonsoo Kim 	page_ext_init();
6293ac7fe5aSThomas Gleixner 	debug_objects_mem_init();
6309b090f2dSCatalin Marinas 	kmemleak_init();
631e7c8d5c9SChristoph Lameter 	setup_per_cpu_pageset();
6321da177e4SLinus Torvalds 	numa_policy_init();
6331da177e4SLinus Torvalds 	if (late_time_init)
6341da177e4SLinus Torvalds 		late_time_init();
635fa84e9eeSThomas Gleixner 	sched_clock_init();
6361da177e4SLinus Torvalds 	calibrate_delay();
6371da177e4SLinus Torvalds 	pidmap_init();
6381da177e4SLinus Torvalds 	anon_vma_init();
639c4e1acbbSRafael J. Wysocki 	acpi_early_init();
64011520e5eSLinus Torvalds #ifdef CONFIG_X86
64183e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES))
64211520e5eSLinus Torvalds 		efi_enter_virtual_mode();
64311520e5eSLinus Torvalds #endif
644197725deSH. Peter Anvin #ifdef CONFIG_X86_ESPFIX64
6453891a04aSH. Peter Anvin 	/* Should be run before the first non-init thread is created */
6463891a04aSH. Peter Anvin 	init_espfix_bsp();
6473891a04aSH. Peter Anvin #endif
6488c9843e5SBenjamin Herrenschmidt 	thread_info_cache_init();
649d84f4f99SDavid Howells 	cred_init();
650ff691f6eSHeinrich Schuchardt 	fork_init();
6511da177e4SLinus Torvalds 	proc_caches_init();
6521da177e4SLinus Torvalds 	buffer_init();
6531da177e4SLinus Torvalds 	key_init();
6541da177e4SLinus Torvalds 	security_init();
6550b4b3827SJason Wessel 	dbg_late_init();
6564248b0daSMel Gorman 	vfs_caches_init();
6571da177e4SLinus Torvalds 	signals_init();
6581da177e4SLinus Torvalds 	/* rootfs populating might need page-writeback */
6591da177e4SLinus Torvalds 	page_writeback_init();
6601da177e4SLinus Torvalds 	proc_root_init();
661e149ed2bSAl Viro 	nsfs_init();
6621da177e4SLinus Torvalds 	cpuset_init();
663695df213SZefan Li 	cgroup_init();
664c757249aSShailabh Nagar 	taskstats_init_early();
665ca74e92bSShailabh Nagar 	delayacct_init();
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	check_bugs();
6681da177e4SLinus Torvalds 
669b064a8faSRafael J. Wysocki 	acpi_subsystem_init();
6706ae6996aSFeng Tang 	sfi_init_late();
6711da177e4SLinus Torvalds 
67283e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES)) {
6732223af38SJosh Triplett 		efi_late_init();
67478510792SJosh Triplett 		efi_free_boot_services();
6752223af38SJosh Triplett 	}
67678510792SJosh Triplett 
67768bf21aaSSteven Rostedt 	ftrace_init();
67868bf21aaSSteven Rostedt 
6791da177e4SLinus Torvalds 	/* Do the rest non-__init'ed, we're now alive */
6801da177e4SLinus Torvalds 	rest_init();
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
683b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */
684b99b87f7SPeter Oberparleiter static void __init do_ctors(void)
685b99b87f7SPeter Oberparleiter {
686b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
687196a15b4SH Hartley Sweeten 	ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
688b99b87f7SPeter Oberparleiter 
689196a15b4SH Hartley Sweeten 	for (; fn < (ctor_fn_t *) __ctors_end; fn++)
690196a15b4SH Hartley Sweeten 		(*fn)();
691b99b87f7SPeter Oberparleiter #endif
692b99b87f7SPeter Oberparleiter }
693b99b87f7SPeter Oberparleiter 
6942329abfaSRusty Russell bool initcall_debug;
695d0ea3d7dSRusty Russell core_param(initcall_debug, initcall_debug, bool, 0644);
6961da177e4SLinus Torvalds 
6977b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS
6987b0b73d7SPrarit Bhargava struct blacklist_entry {
6997b0b73d7SPrarit Bhargava 	struct list_head next;
7007b0b73d7SPrarit Bhargava 	char *buf;
7017b0b73d7SPrarit Bhargava };
7027b0b73d7SPrarit Bhargava 
7037b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
7047b0b73d7SPrarit Bhargava 
7057b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
7067b0b73d7SPrarit Bhargava {
7077b0b73d7SPrarit Bhargava 	char *str_entry;
7087b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
7097b0b73d7SPrarit Bhargava 
7107b0b73d7SPrarit Bhargava 	/* str argument is a comma-separated list of functions */
7117b0b73d7SPrarit Bhargava 	do {
7127b0b73d7SPrarit Bhargava 		str_entry = strsep(&str, ",");
7137b0b73d7SPrarit Bhargava 		if (str_entry) {
7147b0b73d7SPrarit Bhargava 			pr_debug("blacklisting initcall %s\n", str_entry);
7157b0b73d7SPrarit Bhargava 			entry = alloc_bootmem(sizeof(*entry));
7167b0b73d7SPrarit Bhargava 			entry->buf = alloc_bootmem(strlen(str_entry) + 1);
7177b0b73d7SPrarit Bhargava 			strcpy(entry->buf, str_entry);
7187b0b73d7SPrarit Bhargava 			list_add(&entry->next, &blacklisted_initcalls);
7197b0b73d7SPrarit Bhargava 		}
7207b0b73d7SPrarit Bhargava 	} while (str_entry);
7217b0b73d7SPrarit Bhargava 
7227b0b73d7SPrarit Bhargava 	return 0;
7237b0b73d7SPrarit Bhargava }
7247b0b73d7SPrarit Bhargava 
7257b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
7267b0b73d7SPrarit Bhargava {
7277b0b73d7SPrarit Bhargava 	struct list_head *tmp;
7287b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
7297b0b73d7SPrarit Bhargava 	char *fn_name;
7307b0b73d7SPrarit Bhargava 
7317b0b73d7SPrarit Bhargava 	fn_name = kasprintf(GFP_KERNEL, "%pf", fn);
7327b0b73d7SPrarit Bhargava 	if (!fn_name)
7337b0b73d7SPrarit Bhargava 		return false;
7347b0b73d7SPrarit Bhargava 
7357b0b73d7SPrarit Bhargava 	list_for_each(tmp, &blacklisted_initcalls) {
7367b0b73d7SPrarit Bhargava 		entry = list_entry(tmp, struct blacklist_entry, next);
7377b0b73d7SPrarit Bhargava 		if (!strcmp(fn_name, entry->buf)) {
7387b0b73d7SPrarit Bhargava 			pr_debug("initcall %s blacklisted\n", fn_name);
7397b0b73d7SPrarit Bhargava 			kfree(fn_name);
7407b0b73d7SPrarit Bhargava 			return true;
7417b0b73d7SPrarit Bhargava 		}
7427b0b73d7SPrarit Bhargava 	}
7437b0b73d7SPrarit Bhargava 
7447b0b73d7SPrarit Bhargava 	kfree(fn_name);
7457b0b73d7SPrarit Bhargava 	return false;
7467b0b73d7SPrarit Bhargava }
7477b0b73d7SPrarit Bhargava #else
7487b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
7497b0b73d7SPrarit Bhargava {
7507b0b73d7SPrarit Bhargava 	pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
7517b0b73d7SPrarit Bhargava 	return 0;
7527b0b73d7SPrarit Bhargava }
7537b0b73d7SPrarit Bhargava 
7547b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
7557b0b73d7SPrarit Bhargava {
7567b0b73d7SPrarit Bhargava 	return false;
7577b0b73d7SPrarit Bhargava }
7587b0b73d7SPrarit Bhargava #endif
7597b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist);
7607b0b73d7SPrarit Bhargava 
761e4461271SKevin Winchester static int __init_or_module do_one_initcall_debug(initcall_t fn)
7621da177e4SLinus Torvalds {
76374239072SFrederic Weisbecker 	ktime_t calltime, delta, rettime;
76430dbb20eSAmérico Wang 	unsigned long long duration;
76530dbb20eSAmérico Wang 	int ret;
7661da177e4SLinus Torvalds 
767d62cf815SAndrew Morton 	printk(KERN_DEBUG "calling  %pF @ %i\n", fn, task_pid_nr(current));
76874239072SFrederic Weisbecker 	calltime = ktime_get();
76930dbb20eSAmérico Wang 	ret = fn();
77074239072SFrederic Weisbecker 	rettime = ktime_get();
77174239072SFrederic Weisbecker 	delta = ktime_sub(rettime, calltime);
77230dbb20eSAmérico Wang 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
773d62cf815SAndrew Morton 	printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
774ea676e84SAndrew Morton 		 fn, ret, duration);
77522c5c03bSKevin Winchester 
77622c5c03bSKevin Winchester 	return ret;
7778f0c45cdSIngo Molnar }
7788f0c45cdSIngo Molnar 
779e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn)
78022c5c03bSKevin Winchester {
78122c5c03bSKevin Winchester 	int count = preempt_count();
78222c5c03bSKevin Winchester 	int ret;
783ff1c8facSSteven Rostedt 	char msgbuf[64];
78422c5c03bSKevin Winchester 
7857b0b73d7SPrarit Bhargava 	if (initcall_blacklisted(fn))
7867b0b73d7SPrarit Bhargava 		return -EPERM;
7877b0b73d7SPrarit Bhargava 
78822c5c03bSKevin Winchester 	if (initcall_debug)
78922c5c03bSKevin Winchester 		ret = do_one_initcall_debug(fn);
79022c5c03bSKevin Winchester 	else
79122c5c03bSKevin Winchester 		ret = fn();
79222c5c03bSKevin Winchester 
793e662e1cfSCyrill Gorcunov 	msgbuf[0] = 0;
794e662e1cfSCyrill Gorcunov 
7951da177e4SLinus Torvalds 	if (preempt_count() != count) {
796bf5d770bSSteven Rostedt 		sprintf(msgbuf, "preemption imbalance ");
7974a2b4b22SPeter Zijlstra 		preempt_count_set(count);
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 	if (irqs_disabled()) {
800a76bfd0dSCyrill Gorcunov 		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
8011da177e4SLinus Torvalds 		local_irq_enable();
8021da177e4SLinus Torvalds 	}
803f91eb62fSSteven Rostedt 	WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
80459f9415fSArjan van de Ven 
80530dbb20eSAmérico Wang 	return ret;
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
808e0df154fSLinus Torvalds 
809026cee00SPawel Moll extern initcall_t __initcall_start[];
810026cee00SPawel Moll extern initcall_t __initcall0_start[];
811026cee00SPawel Moll extern initcall_t __initcall1_start[];
812026cee00SPawel Moll extern initcall_t __initcall2_start[];
813026cee00SPawel Moll extern initcall_t __initcall3_start[];
814026cee00SPawel Moll extern initcall_t __initcall4_start[];
815026cee00SPawel Moll extern initcall_t __initcall5_start[];
816026cee00SPawel Moll extern initcall_t __initcall6_start[];
817026cee00SPawel Moll extern initcall_t __initcall7_start[];
818026cee00SPawel Moll extern initcall_t __initcall_end[];
819026cee00SPawel Moll 
820026cee00SPawel Moll static initcall_t *initcall_levels[] __initdata = {
821026cee00SPawel Moll 	__initcall0_start,
822026cee00SPawel Moll 	__initcall1_start,
823026cee00SPawel Moll 	__initcall2_start,
824026cee00SPawel Moll 	__initcall3_start,
825026cee00SPawel Moll 	__initcall4_start,
826026cee00SPawel Moll 	__initcall5_start,
827026cee00SPawel Moll 	__initcall6_start,
828026cee00SPawel Moll 	__initcall7_start,
829026cee00SPawel Moll 	__initcall_end,
830026cee00SPawel Moll };
831026cee00SPawel Moll 
83296263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */
833026cee00SPawel Moll static char *initcall_level_names[] __initdata = {
8349fb48c74SJim Cromie 	"early",
8359fb48c74SJim Cromie 	"core",
8369fb48c74SJim Cromie 	"postcore",
8379fb48c74SJim Cromie 	"arch",
8389fb48c74SJim Cromie 	"subsys",
8399fb48c74SJim Cromie 	"fs",
8409fb48c74SJim Cromie 	"device",
8419fb48c74SJim Cromie 	"late",
842026cee00SPawel Moll };
843026cee00SPawel Moll 
844026cee00SPawel Moll static void __init do_initcall_level(int level)
845026cee00SPawel Moll {
846026cee00SPawel Moll 	initcall_t *fn;
847026cee00SPawel Moll 
84808746a65SKrzysztof Mazur 	strcpy(initcall_command_line, saved_command_line);
849026cee00SPawel Moll 	parse_args(initcall_level_names[level],
85008746a65SKrzysztof Mazur 		   initcall_command_line, __start___param,
851026cee00SPawel Moll 		   __stop___param - __start___param,
852026cee00SPawel Moll 		   level, level,
853ecc86170SLuis R. Rodriguez 		   NULL, &repair_env_string);
854026cee00SPawel Moll 
855026cee00SPawel Moll 	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
856026cee00SPawel Moll 		do_one_initcall(*fn);
857026cee00SPawel Moll }
858e0df154fSLinus Torvalds 
859e0df154fSLinus Torvalds static void __init do_initcalls(void)
860e0df154fSLinus Torvalds {
861026cee00SPawel Moll 	int level;
862e0df154fSLinus Torvalds 
86319efb72fSBorislav Petkov 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
864026cee00SPawel Moll 		do_initcall_level(level);
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds /*
8681da177e4SLinus Torvalds  * Ok, the machine is now initialized. None of the devices
8691da177e4SLinus Torvalds  * have been touched yet, but the CPU subsystem is up and
8701da177e4SLinus Torvalds  * running, and memory and process management works.
8711da177e4SLinus Torvalds  *
8721da177e4SLinus Torvalds  * Now we can finally start doing some real work..
8731da177e4SLinus Torvalds  */
8741da177e4SLinus Torvalds static void __init do_basic_setup(void)
8751da177e4SLinus Torvalds {
876759ee091SLai Jiangshan 	cpuset_init_smp();
87741ffe5d5SHugh Dickins 	shmem_init();
8781da177e4SLinus Torvalds 	driver_init();
879b04c3afbSEric W. Biederman 	init_irq_proc();
880b99b87f7SPeter Oberparleiter 	do_ctors();
881d5767c53SLinus Torvalds 	usermodehelper_enable();
882b0f84374Swangyanqing 	do_initcalls();
88347d06e53STheodore Ts'o 	random_int_secret_init();
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8867babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void)
887c2147a50SEduard - Gabriel Munteanu {
888196a15b4SH Hartley Sweeten 	initcall_t *fn;
889c2147a50SEduard - Gabriel Munteanu 
890026cee00SPawel Moll 	for (fn = __initcall_start; fn < __initcall0_start; fn++)
891196a15b4SH Hartley Sweeten 		do_one_initcall(*fn);
892c2147a50SEduard - Gabriel Munteanu }
893c2147a50SEduard - Gabriel Munteanu 
894bb813f4cSTejun Heo /*
895bb813f4cSTejun Heo  * This function requests modules which should be loaded by default and is
896bb813f4cSTejun Heo  * called twice right after initrd is mounted and right before init is
897bb813f4cSTejun Heo  * exec'd.  If such modules are on either initrd or rootfs, they will be
898bb813f4cSTejun Heo  * loaded before control is passed to userland.
899bb813f4cSTejun Heo  */
900bb813f4cSTejun Heo void __init load_default_modules(void)
901bb813f4cSTejun Heo {
902bb813f4cSTejun Heo 	load_default_elevator_module();
903bb813f4cSTejun Heo }
904bb813f4cSTejun Heo 
905a74fb73cSAl Viro static int run_init_process(const char *init_filename)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	argv_init[0] = init_filename;
908c4ad8f98SLinus Torvalds 	return do_execve(getname_kernel(init_filename),
909ae903caaSAl Viro 		(const char __user *const __user *)argv_init,
910ae903caaSAl Viro 		(const char __user *const __user *)envp_init);
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
913ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename)
914ba24762bSMichael Opdenacker {
915ba24762bSMichael Opdenacker 	int ret;
916ba24762bSMichael Opdenacker 
917ba24762bSMichael Opdenacker 	ret = run_init_process(init_filename);
918ba24762bSMichael Opdenacker 
919ba24762bSMichael Opdenacker 	if (ret && ret != -ENOENT) {
920ba24762bSMichael Opdenacker 		pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
921ba24762bSMichael Opdenacker 		       init_filename, ret);
922ba24762bSMichael Opdenacker 	}
923ba24762bSMichael Opdenacker 
924ba24762bSMichael Opdenacker 	return ret;
925ba24762bSMichael Opdenacker }
926ba24762bSMichael Opdenacker 
927f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void);
928d6b21238SAl Viro 
929*d2aa1acaSKees Cook #ifdef CONFIG_DEBUG_RODATA
930*d2aa1acaSKees Cook static bool rodata_enabled = true;
931*d2aa1acaSKees Cook static int __init set_debug_rodata(char *str)
932*d2aa1acaSKees Cook {
933*d2aa1acaSKees Cook 	return strtobool(str, &rodata_enabled);
934*d2aa1acaSKees Cook }
935*d2aa1acaSKees Cook __setup("rodata=", set_debug_rodata);
936*d2aa1acaSKees Cook 
937*d2aa1acaSKees Cook static void mark_readonly(void)
938*d2aa1acaSKees Cook {
939*d2aa1acaSKees Cook 	if (rodata_enabled)
940*d2aa1acaSKees Cook 		mark_rodata_ro();
941*d2aa1acaSKees Cook 	else
942*d2aa1acaSKees Cook 		pr_info("Kernel memory protection disabled.\n");
943*d2aa1acaSKees Cook }
944*d2aa1acaSKees Cook #else
945*d2aa1acaSKees Cook static inline void mark_readonly(void)
946*d2aa1acaSKees Cook {
947*d2aa1acaSKees Cook 	pr_warn("This architecture does not have kernel memory protection.\n");
948*d2aa1acaSKees Cook }
949*d2aa1acaSKees Cook #endif
950*d2aa1acaSKees Cook 
951d6b21238SAl Viro static int __ref kernel_init(void *unused)
952ee5bfa64SVivek Goyal {
953ba24762bSMichael Opdenacker 	int ret;
954ba24762bSMichael Opdenacker 
955d6b21238SAl Viro 	kernel_init_freeable();
95622a9d645SArjan van de Ven 	/* need to finish all async __init code before freeing the memory */
95722a9d645SArjan van de Ven 	async_synchronize_full();
958ee5bfa64SVivek Goyal 	free_initmem();
959*d2aa1acaSKees Cook 	mark_readonly();
960ee5bfa64SVivek Goyal 	system_state = SYSTEM_RUNNING;
961ee5bfa64SVivek Goyal 	numa_default_policy();
962ee5bfa64SVivek Goyal 
9634a9d4b02SAl Viro 	flush_delayed_fput();
964fae5fa44SOleg Nesterov 
965967dcb8fSPaul E. McKenney 	rcu_end_inkernel_boot();
966967dcb8fSPaul E. McKenney 
967ee5bfa64SVivek Goyal 	if (ramdisk_execute_command) {
968ba24762bSMichael Opdenacker 		ret = run_init_process(ramdisk_execute_command);
969ba24762bSMichael Opdenacker 		if (!ret)
970a74fb73cSAl Viro 			return 0;
971ba24762bSMichael Opdenacker 		pr_err("Failed to execute %s (error %d)\n",
972ba24762bSMichael Opdenacker 		       ramdisk_execute_command, ret);
973ee5bfa64SVivek Goyal 	}
974ee5bfa64SVivek Goyal 
975ee5bfa64SVivek Goyal 	/*
976ee5bfa64SVivek Goyal 	 * We try each of these until one succeeds.
977ee5bfa64SVivek Goyal 	 *
978ee5bfa64SVivek Goyal 	 * The Bourne shell can be used instead of init if we are
979ee5bfa64SVivek Goyal 	 * trying to recover a really broken machine.
980ee5bfa64SVivek Goyal 	 */
981ee5bfa64SVivek Goyal 	if (execute_command) {
982ba24762bSMichael Opdenacker 		ret = run_init_process(execute_command);
983ba24762bSMichael Opdenacker 		if (!ret)
984a74fb73cSAl Viro 			return 0;
9856ef4536eSAndy Lutomirski 		panic("Requested init %s failed (error %d).",
9866ef4536eSAndy Lutomirski 		      execute_command, ret);
987ee5bfa64SVivek Goyal 	}
988ba24762bSMichael Opdenacker 	if (!try_to_run_init_process("/sbin/init") ||
989ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/etc/init") ||
990ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/init") ||
991ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/sh"))
992a74fb73cSAl Viro 		return 0;
993ee5bfa64SVivek Goyal 
994ba24762bSMichael Opdenacker 	panic("No working init found.  Try passing init= option to kernel. "
9959a85b8d6SAndreas Mohr 	      "See Linux Documentation/init.txt for guidance.");
996ee5bfa64SVivek Goyal }
997ee5bfa64SVivek Goyal 
998f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void)
9991da177e4SLinus Torvalds {
1000b433c3d4SPeter Zijlstra 	/*
1001b433c3d4SPeter Zijlstra 	 * Wait until kthreadd is all set-up.
1002b433c3d4SPeter Zijlstra 	 */
1003b433c3d4SPeter Zijlstra 	wait_for_completion(&kthreadd_done);
100431a67102SLinus Torvalds 
100531a67102SLinus Torvalds 	/* Now the scheduler is fully set up and can do blocking allocations */
100631a67102SLinus Torvalds 	gfp_allowed_mask = __GFP_BITS_MASK;
100731a67102SLinus Torvalds 
100858568d2aSMiao Xie 	/*
100958568d2aSMiao Xie 	 * init can allocate pages on any node
101058568d2aSMiao Xie 	 */
10113c466d46SLai Jiangshan 	set_mems_allowed(node_states[N_MEMORY]);
10121da177e4SLinus Torvalds 	/*
10131da177e4SLinus Torvalds 	 * init can run on any cpu.
10141da177e4SLinus Torvalds 	 */
10151a2142afSRusty Russell 	set_cpus_allowed_ptr(current, cpu_all_mask);
10161da177e4SLinus Torvalds 
10179ec52099SCedric Le Goater 	cad_pid = task_pid(current);
10189ec52099SCedric Le Goater 
1019ca74a6f8SAndi Kleen 	smp_prepare_cpus(setup_max_cpus);
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	do_pre_smp_initcalls();
1022004417a6SPeter Zijlstra 	lockup_detector_init();
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	smp_init();
10251da177e4SLinus Torvalds 	sched_init_smp();
10261da177e4SLinus Torvalds 
10270e1cc95bSMel Gorman 	page_alloc_init_late();
10280e1cc95bSMel Gorman 
10291da177e4SLinus Torvalds 	do_basic_setup();
10301da177e4SLinus Torvalds 
10312bd3a997SEric W. Biederman 	/* Open the /dev/console on the rootfs, this should never fail */
10322bd3a997SEric W. Biederman 	if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1033ea676e84SAndrew Morton 		pr_err("Warning: unable to open an initial console.\n");
10342bd3a997SEric W. Biederman 
10352bd3a997SEric W. Biederman 	(void) sys_dup(0);
10362bd3a997SEric W. Biederman 	(void) sys_dup(0);
10371da177e4SLinus Torvalds 	/*
10381da177e4SLinus Torvalds 	 * check if there is an early userspace init.  If yes, let it do all
10391da177e4SLinus Torvalds 	 * the work
10401da177e4SLinus Torvalds 	 */
1041ffdfc409SOlof Johansson 
1042ffdfc409SOlof Johansson 	if (!ramdisk_execute_command)
1043ffdfc409SOlof Johansson 		ramdisk_execute_command = "/init";
1044ffdfc409SOlof Johansson 
1045ffdfc409SOlof Johansson 	if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
1046ffdfc409SOlof Johansson 		ramdisk_execute_command = NULL;
10471da177e4SLinus Torvalds 		prepare_namespace();
1048ffdfc409SOlof Johansson 	}
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	/*
10511da177e4SLinus Torvalds 	 * Ok, we have completed the initial bootup, and
10521da177e4SLinus Torvalds 	 * we're essentially up and running. Get rid of the
10531da177e4SLinus Torvalds 	 * initmem segments and start the user-mode stuff..
1054c9cd2ce2SDmitry Kasatkin 	 *
1055c9cd2ce2SDmitry Kasatkin 	 * rootfs is available now, try loading the public keys
1056c9cd2ce2SDmitry Kasatkin 	 * and default modules
10571da177e4SLinus Torvalds 	 */
1058bb813f4cSTejun Heo 
1059c9cd2ce2SDmitry Kasatkin 	integrity_load_keys();
1060bb813f4cSTejun Heo 	load_default_modules();
10611da177e4SLinus Torvalds }
1062