xref: /linux-6.15/init/main.c (revision 7e2762e1)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/init/main.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  GK 2/5/95  -  Changed to support mounting root fs via NFS
81da177e4SLinus Torvalds  *  Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
91da177e4SLinus Torvalds  *  Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
101da177e4SLinus Torvalds  *  Simplified starting of init:  Michael A. Griffith <[email protected]>
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13ea676e84SAndrew Morton #define DEBUG		/* Enable initcall_debug */
14ea676e84SAndrew Morton 
151da177e4SLinus Torvalds #include <linux/types.h>
168a293be0SPaul Gortmaker #include <linux/extable.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/proc_fs.h>
195c2c5c55SIngo Molnar #include <linux/binfmts.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/syscalls.h>
229b5609fdSIngo Molnar #include <linux/stackprotector.h>
231da177e4SLinus Torvalds #include <linux/string.h>
241da177e4SLinus Torvalds #include <linux/ctype.h>
251da177e4SLinus Torvalds #include <linux/delay.h>
261da177e4SLinus Torvalds #include <linux/ioport.h>
271da177e4SLinus Torvalds #include <linux/init.h>
281da177e4SLinus Torvalds #include <linux/initrd.h>
2957c8a661SMike Rapoport #include <linux/memblock.h>
304a7a16dcSLen Brown #include <linux/acpi.h>
310c688614SNicolas Pitre #include <linux/console.h>
3238b8d208SIngo Molnar #include <linux/nmi.h>
331da177e4SLinus Torvalds #include <linux/percpu.h>
341da177e4SLinus Torvalds #include <linux/kmod.h>
35db64fe02SNick Piggin #include <linux/vmalloc.h>
361da177e4SLinus Torvalds #include <linux/kernel_stat.h>
37d7cd5611SRusty Russell #include <linux/start_kernel.h>
381da177e4SLinus Torvalds #include <linux/security.h>
393d442233SJens Axboe #include <linux/smp.h>
401da177e4SLinus Torvalds #include <linux/profile.h>
411da177e4SLinus Torvalds #include <linux/rcupdate.h>
421da177e4SLinus Torvalds #include <linux/moduleparam.h>
431da177e4SLinus Torvalds #include <linux/kallsyms.h>
441da177e4SLinus Torvalds #include <linux/writeback.h>
451da177e4SLinus Torvalds #include <linux/cpu.h>
461da177e4SLinus Torvalds #include <linux/cpuset.h>
47ddbcc7e8SPaul Menage #include <linux/cgroup.h>
481da177e4SLinus Torvalds #include <linux/efi.h>
49906568c9SThomas Gleixner #include <linux/tick.h>
5078634061SFrederic Weisbecker #include <linux/sched/isolation.h>
516168a702SAndrew Morton #include <linux/interrupt.h>
52c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
53ca74e92bSShailabh Nagar #include <linux/delayacct.h>
541da177e4SLinus Torvalds #include <linux/unistd.h>
553ea056c5SAlexey Dobriyan #include <linux/utsname.h>
561da177e4SLinus Torvalds #include <linux/rmap.h>
571da177e4SLinus Torvalds #include <linux/mempolicy.h>
581da177e4SLinus Torvalds #include <linux/key.h>
59b6cd0b77SAdrian Bunk #include <linux/buffer_head.h>
60eefa864bSJoonsoo Kim #include <linux/page_ext.h>
619a11b49aSIngo Molnar #include <linux/debug_locks.h>
623ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
63fbb9ce95SIngo Molnar #include <linux/lockdep.h>
643c7b4e6bSCatalin Marinas #include <linux/kmemleak.h>
6584d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
664c002c97SGreg Kroah-Hartman #include <linux/device/driver.h>
6773c27992SEric W. Biederman #include <linux/kthread.h>
68e6fe6649SAdrian Bunk #include <linux/sched.h>
691777e463SIngo Molnar #include <linux/sched/init.h>
70a1c9eea9SAdrian Bunk #include <linux/signal.h>
71199f0ca5SAkinobu Mita #include <linux/idr.h>
720b4b3827SJason Wessel #include <linux/kgdb.h>
7368bf21aaSSteven Rostedt #include <linux/ftrace.h>
7422a9d645SArjan van de Ven #include <linux/async.h>
756ae6996aSFeng Tang #include <linux/sfi.h>
762b2af54aSKay Sievers #include <linux/shmem_fs.h>
775a0e3ad6STejun Heo #include <linux/slab.h>
7824a24bb6SPeter Zijlstra #include <linux/perf_event.h>
79a74fb73cSAl Viro #include <linux/ptrace.h>
80aa8c6248SThomas Gleixner #include <linux/pti.h>
81bb813f4cSTejun Heo #include <linux/blkdev.h>
82bb813f4cSTejun Heo #include <linux/elevator.h>
835d2a4e91SPavel Tatashin #include <linux/sched/clock.h>
8429930025SIngo Molnar #include <linux/sched/task.h>
8568db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
8665f382fdSFrederic Weisbecker #include <linux/context_tracking.h>
8747d06e53STheodore Ts'o #include <linux/random.h>
887b0b73d7SPrarit Bhargava #include <linux/list.h>
89c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h>
90e149ed2bSAl Viro #include <linux/proc_ns.h>
910ddab1d2SToshi Kani #include <linux/io.h>
9239290b38SAKASHI Takahiro #include <linux/cache.h>
932959a5f7SJinbum Park #include <linux/rodata_test.h>
9433352244SJosh Poimboeuf #include <linux/jump_label.h>
95ae67d58dSMathieu Malaterre #include <linux/mem_encrypt.h>
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #include <asm/io.h>
981da177e4SLinus Torvalds #include <asm/bugs.h>
991da177e4SLinus Torvalds #include <asm/setup.h>
100a940199fSAndi Kleen #include <asm/sections.h>
10137b73c82SArjan van de Ven #include <asm/cacheflush.h>
1021da177e4SLinus Torvalds 
1034ee7c60dSSteven Rostedt (VMware) #define CREATE_TRACE_POINTS
1044ee7c60dSSteven Rostedt (VMware) #include <trace/events/initcall.h>
1054ee7c60dSSteven Rostedt (VMware) 
106aae5f662SSam Ravnborg static int kernel_init(void *);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds extern void init_IRQ(void);
1091da177e4SLinus Torvalds extern void radix_tree_init(void);
1101da177e4SLinus Torvalds 
1112ce802f6STejun Heo /*
1122ce802f6STejun Heo  * Debug helper: via this flag we know that we are in 'early bootup code'
1132ce802f6STejun Heo  * where only the boot processor is running with IRQ disabled.  This means
1142ce802f6STejun Heo  * two things - IRQ must not be enabled before the flag is cleared and some
1152ce802f6STejun Heo  * operations which are not allowed with IRQ disabled are allowed while the
1162ce802f6STejun Heo  * flag is set.
1172ce802f6STejun Heo  */
1182ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly;
1192ce802f6STejun Heo 
120a6826048SPaul E. McKenney enum system_states system_state __read_mostly;
1211da177e4SLinus Torvalds EXPORT_SYMBOL(system_state);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * Boot command-line arguments
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
1271da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds extern void time_init(void);
1301da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */
131d2e3192bSJan Beulich void (*__initdata late_time_init)(void);
1321da177e4SLinus Torvalds 
13330d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */
13430d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE];
13530d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */
13630d7e0d4SAlon Bar-Lev char *saved_command_line;
13730d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */
13830d7e0d4SAlon Bar-Lev static char *static_command_line;
13908746a65SKrzysztof Mazur /* Command line for per-initcall parameter parsing */
14008746a65SKrzysztof Mazur static char *initcall_command_line;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static char *execute_command;
143ffdfc409SOlof Johansson static char *ramdisk_execute_command;
1441da177e4SLinus Torvalds 
1458b3b2955SJan Beulich /*
146c4b2c0c5SHannes Frederic Sowa  * Used to generate warnings if static_key manipulation functions are used
147c4b2c0c5SHannes Frederic Sowa  * before jump_label_init is called.
148c4b2c0c5SHannes Frederic Sowa  */
149dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly;
150c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized);
151c4b2c0c5SHannes Frederic Sowa 
152c4b2c0c5SHannes Frederic Sowa /*
1538b3b2955SJan Beulich  * If set, this is an indication to the drivers that reset the underlying
1548b3b2955SJan Beulich  * device before going ahead with the initialization otherwise driver might
1558b3b2955SJan Beulich  * rely on the BIOS and skip the reset operation.
1568b3b2955SJan Beulich  *
1578b3b2955SJan Beulich  * This is useful if kernel is booting in an unreliable environment.
158fc454fdcSFrans Klaver  * For ex. kdump situation where previous kernel has crashed, BIOS has been
1598b3b2955SJan Beulich  * skipped and devices will be in unknown state.
1608b3b2955SJan Beulich  */
1618b3b2955SJan Beulich unsigned int reset_devices;
1628b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices);
1631da177e4SLinus Torvalds 
1647e96287dSVivek Goyal static int __init set_reset_devices(char *str)
1657e96287dSVivek Goyal {
1667e96287dSVivek Goyal 	reset_devices = 1;
1677e96287dSVivek Goyal 	return 1;
1687e96287dSVivek Goyal }
1697e96287dSVivek Goyal 
1707e96287dSVivek Goyal __setup("reset_devices", set_reset_devices);
1717e96287dSVivek Goyal 
172d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
173d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1741da177e4SLinus Torvalds static const char *panic_later, *panic_param;
1751da177e4SLinus Torvalds 
176914dcaa8SRusty Russell extern const struct obs_kernel_param __setup_start[], __setup_end[];
1771da177e4SLinus Torvalds 
17831c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line)
1791da177e4SLinus Torvalds {
180914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
18131c025b5SYaowei Bai 	bool had_early_param = false;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	p = __setup_start;
1841da177e4SLinus Torvalds 	do {
1851da177e4SLinus Torvalds 		int n = strlen(p->str);
186b1e4d20cSMichal Schmidt 		if (parameqn(line, p->str, n)) {
1871da177e4SLinus Torvalds 			if (p->early) {
18833df0d19SRusty Russell 				/* Already done in parse_early_param?
18933df0d19SRusty Russell 				 * (Needs exact match on param part).
19033df0d19SRusty Russell 				 * Keep iterating, as we can have early
19133df0d19SRusty Russell 				 * params and __setups of same names 8( */
1921da177e4SLinus Torvalds 				if (line[n] == '\0' || line[n] == '=')
19331c025b5SYaowei Bai 					had_early_param = true;
1941da177e4SLinus Torvalds 			} else if (!p->setup_func) {
195ea676e84SAndrew Morton 				pr_warn("Parameter %s is obsolete, ignored\n",
196ea676e84SAndrew Morton 					p->str);
19731c025b5SYaowei Bai 				return true;
1981da177e4SLinus Torvalds 			} else if (p->setup_func(line + n))
19931c025b5SYaowei Bai 				return true;
2001da177e4SLinus Torvalds 		}
2011da177e4SLinus Torvalds 		p++;
2021da177e4SLinus Torvalds 	} while (p < __setup_end);
20333df0d19SRusty Russell 
20433df0d19SRusty Russell 	return had_early_param;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  * This should be approx 2 Bo*oMips to start (note initial shift), and will
2091da177e4SLinus Torvalds  * still work even if initially too large, it will just take slightly longer
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12);
2121da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static int __init debug_kernel(char *str)
2151da177e4SLinus Torvalds {
216a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
217f6f21c81SYinghai Lu 	return 0;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static int __init quiet_kernel(char *str)
2211da177e4SLinus Torvalds {
222a8fe19ebSBorislav Petkov 	console_loglevel = CONSOLE_LOGLEVEL_QUIET;
223f6f21c81SYinghai Lu 	return 0;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
226f6f21c81SYinghai Lu early_param("debug", debug_kernel);
227f6f21c81SYinghai Lu early_param("quiet", quiet_kernel);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds static int __init loglevel(char *str)
2301da177e4SLinus Torvalds {
231808bf29bSAlexander Sverdlin 	int newlevel;
232808bf29bSAlexander Sverdlin 
233808bf29bSAlexander Sverdlin 	/*
234808bf29bSAlexander Sverdlin 	 * Only update loglevel value when a correct setting was passed,
235808bf29bSAlexander Sverdlin 	 * to prevent blind crashes (when loglevel being set to 0) that
236808bf29bSAlexander Sverdlin 	 * are quite hard to debug
237808bf29bSAlexander Sverdlin 	 */
238808bf29bSAlexander Sverdlin 	if (get_option(&str, &newlevel)) {
239808bf29bSAlexander Sverdlin 		console_loglevel = newlevel;
240d9d4fcfeSAlex Riesen 		return 0;
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds 
243808bf29bSAlexander Sverdlin 	return -EINVAL;
244808bf29bSAlexander Sverdlin }
245808bf29bSAlexander Sverdlin 
246f6f21c81SYinghai Lu early_param("loglevel", loglevel);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */
249*7e2762e1SArvind Sankar static void __init repair_env_string(char *param, char *val)
250a99cd112SChris Metcalf {
2511da177e4SLinus Torvalds 	if (val) {
2521da177e4SLinus Torvalds 		/* param=val or param="val"? */
2531da177e4SLinus Torvalds 		if (val == param+strlen(param)+1)
2541da177e4SLinus Torvalds 			val[-1] = '=';
2551da177e4SLinus Torvalds 		else if (val == param+strlen(param)+2) {
2561da177e4SLinus Torvalds 			val[-2] = '=';
2571da177e4SLinus Torvalds 			memmove(val-1, val, strlen(val)+1);
2581da177e4SLinus Torvalds 			val--;
2591da177e4SLinus Torvalds 		} else
2601da177e4SLinus Torvalds 			BUG();
2611da177e4SLinus Torvalds 	}
262a99cd112SChris Metcalf }
263a99cd112SChris Metcalf 
26451e158c1SRusty Russell /* Anything after -- gets handed straight to init. */
265ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val,
266ecc86170SLuis R. Rodriguez 			       const char *unused, void *arg)
26751e158c1SRusty Russell {
26851e158c1SRusty Russell 	unsigned int i;
26951e158c1SRusty Russell 
27051e158c1SRusty Russell 	if (panic_later)
27151e158c1SRusty Russell 		return 0;
27251e158c1SRusty Russell 
273*7e2762e1SArvind Sankar 	repair_env_string(param, val);
27451e158c1SRusty Russell 
27551e158c1SRusty Russell 	for (i = 0; argv_init[i]; i++) {
27651e158c1SRusty Russell 		if (i == MAX_INIT_ARGS) {
27751e158c1SRusty Russell 			panic_later = "init";
27851e158c1SRusty Russell 			panic_param = param;
27951e158c1SRusty Russell 			return 0;
28051e158c1SRusty Russell 		}
28151e158c1SRusty Russell 	}
28251e158c1SRusty Russell 	argv_init[i] = param;
28351e158c1SRusty Russell 	return 0;
28451e158c1SRusty Russell }
28551e158c1SRusty Russell 
286a99cd112SChris Metcalf /*
287a99cd112SChris Metcalf  * Unknown boot options get handed to init, unless they look like
288a99cd112SChris Metcalf  * unused parameters (modprobe will find them in /proc/cmdline).
289a99cd112SChris Metcalf  */
290ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val,
291ecc86170SLuis R. Rodriguez 				     const char *unused, void *arg)
292a99cd112SChris Metcalf {
293*7e2762e1SArvind Sankar 	repair_env_string(param, val);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/* Handle obsolete-style parameters */
2961da177e4SLinus Torvalds 	if (obsolete_checksetup(param))
2971da177e4SLinus Torvalds 		return 0;
2981da177e4SLinus Torvalds 
299f066a4f6SRusty Russell 	/* Unused module parameter. */
300f066a4f6SRusty Russell 	if (strchr(param, '.') && (!val || strchr(param, '.') < val))
3011da177e4SLinus Torvalds 		return 0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	if (panic_later)
3041da177e4SLinus Torvalds 		return 0;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if (val) {
3071da177e4SLinus Torvalds 		/* Environment option */
3081da177e4SLinus Torvalds 		unsigned int i;
3091da177e4SLinus Torvalds 		for (i = 0; envp_init[i]; i++) {
3101da177e4SLinus Torvalds 			if (i == MAX_INIT_ENVS) {
311499a4584STetsuo Handa 				panic_later = "env";
3121da177e4SLinus Torvalds 				panic_param = param;
3131da177e4SLinus Torvalds 			}
3141da177e4SLinus Torvalds 			if (!strncmp(param, envp_init[i], val - param))
3151da177e4SLinus Torvalds 				break;
3161da177e4SLinus Torvalds 		}
3171da177e4SLinus Torvalds 		envp_init[i] = param;
3181da177e4SLinus Torvalds 	} else {
3191da177e4SLinus Torvalds 		/* Command line option */
3201da177e4SLinus Torvalds 		unsigned int i;
3211da177e4SLinus Torvalds 		for (i = 0; argv_init[i]; i++) {
3221da177e4SLinus Torvalds 			if (i == MAX_INIT_ARGS) {
323499a4584STetsuo Handa 				panic_later = "init";
3241da177e4SLinus Torvalds 				panic_param = param;
3251da177e4SLinus Torvalds 			}
3261da177e4SLinus Torvalds 		}
3271da177e4SLinus Torvalds 		argv_init[i] = param;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	return 0;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static int __init init_setup(char *str)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	unsigned int i;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	execute_command = str;
3371da177e4SLinus Torvalds 	/*
3381da177e4SLinus Torvalds 	 * In case LILO is going to boot us with default command line,
3391da177e4SLinus Torvalds 	 * it prepends "auto" before the whole cmdline which makes
3401da177e4SLinus Torvalds 	 * the shell think it should execute a script with such name.
3411da177e4SLinus Torvalds 	 * So we ignore all arguments entered _before_ init=... [MJ]
3421da177e4SLinus Torvalds 	 */
3431da177e4SLinus Torvalds 	for (i = 1; i < MAX_INIT_ARGS; i++)
3441da177e4SLinus Torvalds 		argv_init[i] = NULL;
3451da177e4SLinus Torvalds 	return 1;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds __setup("init=", init_setup);
3481da177e4SLinus Torvalds 
349ffdfc409SOlof Johansson static int __init rdinit_setup(char *str)
350ffdfc409SOlof Johansson {
351ffdfc409SOlof Johansson 	unsigned int i;
352ffdfc409SOlof Johansson 
353ffdfc409SOlof Johansson 	ramdisk_execute_command = str;
354ffdfc409SOlof Johansson 	/* See "auto" comment in init_setup */
355ffdfc409SOlof Johansson 	for (i = 1; i < MAX_INIT_ARGS; i++)
356ffdfc409SOlof Johansson 		argv_init[i] = NULL;
357ffdfc409SOlof Johansson 	return 1;
358ffdfc409SOlof Johansson }
359ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup);
360ffdfc409SOlof Johansson 
3611da177e4SLinus Torvalds #ifndef CONFIG_SMP
36234db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS;
363e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { }
3641da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { }
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds /*
36830d7e0d4SAlon Bar-Lev  * We need to store the untouched command line for future reference.
36930d7e0d4SAlon Bar-Lev  * We also need to store the touched command line since the parameter
37030d7e0d4SAlon Bar-Lev  * parsing is performed in place, and we should allow a component to
37130d7e0d4SAlon Bar-Lev  * store reference of name/value for future reference.
37230d7e0d4SAlon Bar-Lev  */
37330d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line)
37430d7e0d4SAlon Bar-Lev {
375f5c7310aSMike Rapoport 	size_t len = strlen(boot_command_line) + 1;
376f5c7310aSMike Rapoport 
377f5c7310aSMike Rapoport 	saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
378f5c7310aSMike Rapoport 	if (!saved_command_line)
379f5c7310aSMike Rapoport 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
380f5c7310aSMike Rapoport 
381f5c7310aSMike Rapoport 	initcall_command_line =	memblock_alloc(len, SMP_CACHE_BYTES);
382f5c7310aSMike Rapoport 	if (!initcall_command_line)
383f5c7310aSMike Rapoport 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
384f5c7310aSMike Rapoport 
385f5c7310aSMike Rapoport 	static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
386f5c7310aSMike Rapoport 	if (!static_command_line)
387f5c7310aSMike Rapoport 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
388f5c7310aSMike Rapoport 
38930d7e0d4SAlon Bar-Lev 	strcpy(saved_command_line, boot_command_line);
39030d7e0d4SAlon Bar-Lev 	strcpy(static_command_line, command_line);
39130d7e0d4SAlon Bar-Lev }
39230d7e0d4SAlon Bar-Lev 
39330d7e0d4SAlon Bar-Lev /*
3941da177e4SLinus Torvalds  * We need to finalize in a non-__init function or else race conditions
3951da177e4SLinus Torvalds  * between the root thread and the init thread may cause start_kernel to
3961da177e4SLinus Torvalds  * be reaped by free_initmem before the root thread has proceeded to
3971da177e4SLinus Torvalds  * cpu_idle.
3981da177e4SLinus Torvalds  *
3991da177e4SLinus Torvalds  * gcc-3.4 accidentally inlines this function, so use noinline.
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds 
402b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done);
403b433c3d4SPeter Zijlstra 
40453c99bd6SMartin Schwidefsky noinline void __ref rest_init(void)
4051da177e4SLinus Torvalds {
4068fb12156SThomas Gleixner 	struct task_struct *tsk;
40773c27992SEric W. Biederman 	int pid;
40873c27992SEric W. Biederman 
4097db905e6SPaul E. McKenney 	rcu_scheduler_starting();
410b433c3d4SPeter Zijlstra 	/*
41197158569SPeter Zijlstra 	 * We need to spawn init first so that it obtains pid 1, however
412b433c3d4SPeter Zijlstra 	 * the init task will end up wanting to create kthreads, which, if
413b433c3d4SPeter Zijlstra 	 * we schedule it before we create kthreadd, will OOPS.
414b433c3d4SPeter Zijlstra 	 */
4158fb12156SThomas Gleixner 	pid = kernel_thread(kernel_init, NULL, CLONE_FS);
4168fb12156SThomas Gleixner 	/*
4178fb12156SThomas Gleixner 	 * Pin init on the boot CPU. Task migration is not properly working
4188fb12156SThomas Gleixner 	 * until sched_init_smp() has been run. It will set the allowed
4198fb12156SThomas Gleixner 	 * CPUs for init to the non isolated CPUs.
4208fb12156SThomas Gleixner 	 */
4218fb12156SThomas Gleixner 	rcu_read_lock();
4228fb12156SThomas Gleixner 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
4238fb12156SThomas Gleixner 	set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
4248fb12156SThomas Gleixner 	rcu_read_unlock();
4258fb12156SThomas Gleixner 
4261da177e4SLinus Torvalds 	numa_default_policy();
42773c27992SEric W. Biederman 	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
428d11c563dSPaul E. McKenney 	rcu_read_lock();
4295cd20455SPavel Emelyanov 	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
430d11c563dSPaul E. McKenney 	rcu_read_unlock();
4311c3c5eabSThomas Gleixner 
4321c3c5eabSThomas Gleixner 	/*
4331c3c5eabSThomas Gleixner 	 * Enable might_sleep() and smp_processor_id() checks.
434c1a280b6SThomas Gleixner 	 * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
4351c3c5eabSThomas Gleixner 	 * kernel_thread() would trigger might_sleep() splats. With
4361c3c5eabSThomas Gleixner 	 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
4371c3c5eabSThomas Gleixner 	 * already, but it's stuck on the kthreadd_done completion.
4381c3c5eabSThomas Gleixner 	 */
4391c3c5eabSThomas Gleixner 	system_state = SYSTEM_SCHEDULING;
4401c3c5eabSThomas Gleixner 
441b433c3d4SPeter Zijlstra 	complete(&kthreadd_done);
442f340c0d1SIngo Molnar 
443f340c0d1SIngo Molnar 	/*
444f340c0d1SIngo Molnar 	 * The boot idle thread must execute schedule()
4451df21055SIngo Molnar 	 * at least once to get things moving:
446f340c0d1SIngo Molnar 	 */
447bd2f5536SThomas Gleixner 	schedule_preempt_disabled();
4485bfb5d69SNick Piggin 	/* Call into cpu_idle with preempt disabled */
449a1a04ec3SThomas Gleixner 	cpu_startup_entry(CPUHP_ONLINE);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /* Check for early params. */
453ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val,
454ecc86170SLuis R. Rodriguez 				 const char *unused, void *arg)
4551da177e4SLinus Torvalds {
456914dcaa8SRusty Russell 	const struct obs_kernel_param *p;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	for (p = __setup_start; p < __setup_end; p++) {
459b1e4d20cSMichal Schmidt 		if ((p->early && parameq(param, p->str)) ||
46018a8bd94SYinghai Lu 		    (strcmp(param, "console") == 0 &&
46118a8bd94SYinghai Lu 		     strcmp(p->str, "earlycon") == 0)
46218a8bd94SYinghai Lu 		) {
4631da177e4SLinus Torvalds 			if (p->setup_func(val) != 0)
464ea676e84SAndrew Morton 				pr_warn("Malformed early option '%s'\n", param);
4651da177e4SLinus Torvalds 		}
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	/* We accept everything at this stage. */
4681da177e4SLinus Torvalds 	return 0;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
47113977091SMagnus Damm void __init parse_early_options(char *cmdline)
47213977091SMagnus Damm {
473ecc86170SLuis R. Rodriguez 	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
474ecc86170SLuis R. Rodriguez 		   do_early_param);
47513977091SMagnus Damm }
47613977091SMagnus Damm 
4771da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */
4781da177e4SLinus Torvalds void __init parse_early_param(void)
4791da177e4SLinus Torvalds {
480dd4d9fecSFabian Frederick 	static int done __initdata;
481dd4d9fecSFabian Frederick 	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (done)
4841da177e4SLinus Torvalds 		return;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	/* All fall through to do_early_param. */
48730d7e0d4SAlon Bar-Lev 	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
48813977091SMagnus Damm 	parse_early_options(tmp_cmdline);
4891da177e4SLinus Torvalds 	done = 1;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
492e7ff3a47SThomas Gleixner void __init __weak arch_post_acpi_subsys_init(void) { }
493e7ff3a47SThomas Gleixner 
494839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void)
495033ab7f8SAndrew Morton {
496033ab7f8SAndrew Morton }
497033ab7f8SAndrew Morton 
498eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE
499b235beeaSLinus Torvalds void __init __weak thread_stack_cache_init(void)
5008c9843e5SBenjamin Herrenschmidt {
5018c9843e5SBenjamin Herrenschmidt }
502eded09ccSDavid Howells #endif
5038c9843e5SBenjamin Herrenschmidt 
504c7753208STom Lendacky void __init __weak mem_encrypt_init(void) { }
505c7753208STom Lendacky 
5064fc19708SNadav Amit void __init __weak poking_init(void) { }
5074fc19708SNadav Amit 
508782de70cSMike Rapoport void __init __weak pgtable_cache_init(void) { }
509caa84136SNadav Amit 
5104e37958dSSteven Rostedt (VMware) bool initcall_debug;
5114e37958dSSteven Rostedt (VMware) core_param(initcall_debug, initcall_debug, bool, 0644);
512b0dc52f1SSteven Rostedt (VMware) 
513b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED
5144e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void);
515b0dc52f1SSteven Rostedt (VMware) #else
516b0dc52f1SSteven Rostedt (VMware) static inline void initcall_debug_enable(void)
517b0dc52f1SSteven Rostedt (VMware) {
518b0dc52f1SSteven Rostedt (VMware) }
519b0dc52f1SSteven Rostedt (VMware) #endif
5204e37958dSSteven Rostedt (VMware) 
52123a5c8cbSAlexander Potapenko /* Report memory auto-initialization states for this boot. */
52223a5c8cbSAlexander Potapenko static void __init report_meminit(void)
52323a5c8cbSAlexander Potapenko {
52423a5c8cbSAlexander Potapenko 	const char *stack;
52523a5c8cbSAlexander Potapenko 
52623a5c8cbSAlexander Potapenko 	if (IS_ENABLED(CONFIG_INIT_STACK_ALL))
52723a5c8cbSAlexander Potapenko 		stack = "all";
52823a5c8cbSAlexander Potapenko 	else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL))
52923a5c8cbSAlexander Potapenko 		stack = "byref_all";
53023a5c8cbSAlexander Potapenko 	else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF))
53123a5c8cbSAlexander Potapenko 		stack = "byref";
53223a5c8cbSAlexander Potapenko 	else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER))
53323a5c8cbSAlexander Potapenko 		stack = "__user";
53423a5c8cbSAlexander Potapenko 	else
53523a5c8cbSAlexander Potapenko 		stack = "off";
53623a5c8cbSAlexander Potapenko 
53723a5c8cbSAlexander Potapenko 	pr_info("mem auto-init: stack:%s, heap alloc:%s, heap free:%s\n",
53823a5c8cbSAlexander Potapenko 		stack, want_init_on_alloc(GFP_KERNEL) ? "on" : "off",
53923a5c8cbSAlexander Potapenko 		want_init_on_free() ? "on" : "off");
54023a5c8cbSAlexander Potapenko 	if (want_init_on_free())
54123a5c8cbSAlexander Potapenko 		pr_info("mem auto-init: clearing system memory may take some time...\n");
54223a5c8cbSAlexander Potapenko }
54323a5c8cbSAlexander Potapenko 
544444f478fSPekka Enberg /*
545444f478fSPekka Enberg  * Set up kernel memory allocators
546444f478fSPekka Enberg  */
547444f478fSPekka Enberg static void __init mm_init(void)
548444f478fSPekka Enberg {
549eefa864bSJoonsoo Kim 	/*
550eefa864bSJoonsoo Kim 	 * page_ext requires contiguous pages,
551eefa864bSJoonsoo Kim 	 * bigger than MAX_ORDER unless SPARSEMEM.
552eefa864bSJoonsoo Kim 	 */
553eefa864bSJoonsoo Kim 	page_ext_init_flatmem();
5548e57f8acSVlastimil Babka 	init_debug_pagealloc();
55523a5c8cbSAlexander Potapenko 	report_meminit();
556444f478fSPekka Enberg 	mem_init();
557444f478fSPekka Enberg 	kmem_cache_init();
558c5665868SCatalin Marinas 	kmemleak_init();
559b35f1819SKirill A. Shutemov 	pgtable_init();
560a9ee3a63SQian Cai 	debug_objects_mem_init();
561444f478fSPekka Enberg 	vmalloc_init();
5620ddab1d2SToshi Kani 	ioremap_huge_init();
563613e396bSThomas Gleixner 	/* Should be run before the first non-init thread is created */
564613e396bSThomas Gleixner 	init_espfix_bsp();
565aa8c6248SThomas Gleixner 	/* Should be run after espfix64 is set up. */
566aa8c6248SThomas Gleixner 	pti_init();
567444f478fSPekka Enberg }
568444f478fSPekka Enberg 
56953c99bd6SMartin Schwidefsky void __init __weak arch_call_rest_init(void)
57053c99bd6SMartin Schwidefsky {
57153c99bd6SMartin Schwidefsky 	rest_init();
57253c99bd6SMartin Schwidefsky }
57353c99bd6SMartin Schwidefsky 
574722a9f92SAndi Kleen asmlinkage __visible void __init start_kernel(void)
5751da177e4SLinus Torvalds {
576dd4d9fecSFabian Frederick 	char *command_line;
577dd4d9fecSFabian Frederick 	char *after_dashes;
578033ab7f8SAndrew Morton 
579d4311ff1SAaron Tomlin 	set_task_stack_end_magic(&init_task);
58073839c5bSMing Lei 	smp_setup_processor_id();
5813ac7fe5aSThomas Gleixner 	debug_objects_early_init();
58242059429SIngo Molnar 
583ddbcc7e8SPaul Menage 	cgroup_init_early();
584fbb9ce95SIngo Molnar 
585fbb9ce95SIngo Molnar 	local_irq_disable();
5862ce802f6STejun Heo 	early_boot_irqs_disabled = true;
587fbb9ce95SIngo Molnar 
588fbb9ce95SIngo Molnar 	/*
5891da177e4SLinus Torvalds 	 * Interrupts are still disabled. Do necessary setups, then
5901b3b3b49SViresh Kumar 	 * enable them.
5911da177e4SLinus Torvalds 	 */
59244fd2299SStas Sergeev 	boot_cpu_init();
5931da177e4SLinus Torvalds 	page_address_init();
594ea676e84SAndrew Morton 	pr_notice("%s", linux_banner);
595e6b1db98SMatthew Garrett 	early_security_init();
5961da177e4SLinus Torvalds 	setup_arch(&command_line);
59730d7e0d4SAlon Bar-Lev 	setup_command_line(command_line);
598e0982e90SMike Travis 	setup_nr_cpu_ids();
599d6647bdfSTejun Heo 	setup_per_cpu_areas();
60044fd2299SStas Sergeev 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
601b5b1404dSLinus Torvalds 	boot_cpu_hotplug_init();
6021da177e4SLinus Torvalds 
60372675e13SMichal Hocko 	build_all_zonelists(NULL);
60483b519e8SPekka Enberg 	page_alloc_init();
60583b519e8SPekka Enberg 
606ea676e84SAndrew Morton 	pr_notice("Kernel command line: %s\n", boot_command_line);
6076041186aSDan Williams 	/* parameters may set static keys */
6086041186aSDan Williams 	jump_label_init();
60983b519e8SPekka Enberg 	parse_early_param();
61051e158c1SRusty Russell 	after_dashes = parse_args("Booting kernel",
61151e158c1SRusty Russell 				  static_command_line, __start___param,
61283b519e8SPekka Enberg 				  __stop___param - __start___param,
613ecc86170SLuis R. Rodriguez 				  -1, -1, NULL, &unknown_bootoption);
6143438cf54SDaniel Thompson 	if (!IS_ERR_OR_NULL(after_dashes))
61551e158c1SRusty Russell 		parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
616ecc86170SLuis R. Rodriguez 			   NULL, set_init_arg);
61797ce2c88SJeremy Fitzhardinge 
61883b519e8SPekka Enberg 	/*
61983b519e8SPekka Enberg 	 * These use large bootmem allocations and must precede
62083b519e8SPekka Enberg 	 * kmem_cache_init()
62183b519e8SPekka Enberg 	 */
622162a7e75SMike Travis 	setup_log_buf(0);
62383b519e8SPekka Enberg 	vfs_caches_init_early();
62483b519e8SPekka Enberg 	sort_main_extable();
62583b519e8SPekka Enberg 	trap_init();
626444f478fSPekka Enberg 	mm_init();
627de03c72cSKOSAKI Motohiro 
628f631718dSSteven Rostedt (VMware) 	ftrace_init();
629f631718dSSteven Rostedt (VMware) 
630e725c731SSteven Rostedt (VMware) 	/* trace_printk can be enabled here */
631e725c731SSteven Rostedt (VMware) 	early_trace_init();
632e725c731SSteven Rostedt (VMware) 
6331da177e4SLinus Torvalds 	/*
6341da177e4SLinus Torvalds 	 * Set up the scheduler prior starting any interrupts (such as the
6351da177e4SLinus Torvalds 	 * timer interrupt). Full topology setup happens at smp_init()
6361da177e4SLinus Torvalds 	 * time - but meanwhile we still have a functioning scheduler.
6371da177e4SLinus Torvalds 	 */
6381da177e4SLinus Torvalds 	sched_init();
6391da177e4SLinus Torvalds 	/*
6401da177e4SLinus Torvalds 	 * Disable preemption - early bootup scheduling is extremely
6411da177e4SLinus Torvalds 	 * fragile until we cpu_idle() for the first time.
6421da177e4SLinus Torvalds 	 */
6431da177e4SLinus Torvalds 	preempt_disable();
644dd4d9fecSFabian Frederick 	if (WARN(!irqs_disabled(),
645dd4d9fecSFabian Frederick 		 "Interrupts were enabled *very* early, fixing it\n"))
646c4a68306SArd van Breemen 		local_irq_disable();
6470a835c4fSMatthew Wilcox 	radix_tree_init();
6483347fa09STejun Heo 
6493347fa09STejun Heo 	/*
6507d229c66STal Shorer 	 * Set up housekeeping before setting up workqueues to allow the unbound
6517d229c66STal Shorer 	 * workqueue to take non-housekeeping into account.
6527d229c66STal Shorer 	 */
6537d229c66STal Shorer 	housekeeping_init();
6547d229c66STal Shorer 
6557d229c66STal Shorer 	/*
6563347fa09STejun Heo 	 * Allow workqueue creation and work item queueing/cancelling
6573347fa09STejun Heo 	 * early.  Work item execution depends on kthreads and starts after
6583347fa09STejun Heo 	 * workqueue_init().
6593347fa09STejun Heo 	 */
6603347fa09STejun Heo 	workqueue_init_early();
6613347fa09STejun Heo 
6621da177e4SLinus Torvalds 	rcu_init();
6635f893b26SSteven Rostedt (Red Hat) 
664e725c731SSteven Rostedt (VMware) 	/* Trace events are available after this */
6655f893b26SSteven Rostedt (Red Hat) 	trace_init();
6665f893b26SSteven Rostedt (Red Hat) 
6674e37958dSSteven Rostedt (VMware) 	if (initcall_debug)
6684e37958dSSteven Rostedt (VMware) 		initcall_debug_enable();
6694e37958dSSteven Rostedt (VMware) 
67065f382fdSFrederic Weisbecker 	context_tracking_init();
6710b8f1efaSYinghai Lu 	/* init some links before init_ISA_irqs() */
6720b8f1efaSYinghai Lu 	early_irq_init();
6731da177e4SLinus Torvalds 	init_IRQ();
674ad2b1353SThomas Gleixner 	tick_init();
675d6dd50e0SLinus Torvalds 	rcu_init_nohz();
6761da177e4SLinus Torvalds 	init_timers();
677c0a31329SThomas Gleixner 	hrtimers_init();
6781da177e4SLinus Torvalds 	softirq_init();
679ad596171Sjohn stultz 	timekeeping_init();
680d5553523SKees Cook 
681d5553523SKees Cook 	/*
682d5553523SKees Cook 	 * For best initial stack canary entropy, prepare it after:
683d5553523SKees Cook 	 * - setup_arch() for any UEFI RNG entropy and boot cmdline access
684d5553523SKees Cook 	 * - timekeeping_init() for ktime entropy used in rand_initialize()
685d5553523SKees Cook 	 * - rand_initialize() to get any arch-specific entropy like RDRAND
686d5553523SKees Cook 	 * - add_latent_entropy() to get any latent entropy
687d5553523SKees Cook 	 * - adding command line entropy
688d5553523SKees Cook 	 */
689d5553523SKees Cook 	rand_initialize();
690d5553523SKees Cook 	add_latent_entropy();
691d5553523SKees Cook 	add_device_randomness(command_line, strlen(command_line));
692d5553523SKees Cook 	boot_init_stack_canary();
693d5553523SKees Cook 
69488fecaa2Sjohn stultz 	time_init();
695f92bac3bSSergey Senozhatsky 	printk_safe_init();
6969e630205SStephane Eranian 	perf_event_init();
69793e02814SHeiko Carstens 	profile_init();
698d8ad7d11STakao Indoh 	call_function_init();
699f91eb62fSSteven Rostedt 	WARN(!irqs_disabled(), "Interrupts were enabled early\n");
700c3bc8fd6SJoel Fernandes (Google) 
7012ce802f6STejun Heo 	early_boot_irqs_disabled = false;
70293e02814SHeiko Carstens 	local_irq_enable();
703dcce284aSBenjamin Herrenschmidt 
7047e85ee0cSPekka Enberg 	kmem_cache_init_late();
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	/*
7071da177e4SLinus Torvalds 	 * HACK ALERT! This is early. We're enabling the console before
7081da177e4SLinus Torvalds 	 * we've done PCI setups etc, and console_init() must be aware of
7091da177e4SLinus Torvalds 	 * this. But we do want output early, in case something goes wrong.
7101da177e4SLinus Torvalds 	 */
7111da177e4SLinus Torvalds 	console_init();
7121da177e4SLinus Torvalds 	if (panic_later)
713499a4584STetsuo Handa 		panic("Too many boot %s vars at `%s'", panic_later,
714499a4584STetsuo Handa 		      panic_param);
715fbb9ce95SIngo Molnar 
716c3bc8fd6SJoel Fernandes (Google) 	lockdep_init();
717fbb9ce95SIngo Molnar 
7189a11b49aSIngo Molnar 	/*
7199a11b49aSIngo Molnar 	 * Need to run this when irqs are enabled, because it wants
7209a11b49aSIngo Molnar 	 * to self-test [hard/soft]-irqs on/off lock inversion bugs
7219a11b49aSIngo Molnar 	 * too:
7229a11b49aSIngo Molnar 	 */
7239a11b49aSIngo Molnar 	locking_selftest();
7249a11b49aSIngo Molnar 
725c7753208STom Lendacky 	/*
726c7753208STom Lendacky 	 * This needs to be called before any devices perform DMA
727c7753208STom Lendacky 	 * operations that might use the SWIOTLB bounce buffers. It will
728c7753208STom Lendacky 	 * mark the bounce buffers as decrypted so that their usage will
729c7753208STom Lendacky 	 * not cause "plain-text" data to be decrypted when accessed.
730c7753208STom Lendacky 	 */
731c7753208STom Lendacky 	mem_encrypt_init();
732c7753208STom Lendacky 
7331da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
7341da177e4SLinus Torvalds 	if (initrd_start && !initrd_below_start_ok &&
735bd673c7cSGeert Uytterhoeven 	    page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
736ea676e84SAndrew Morton 		pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
737bd673c7cSGeert Uytterhoeven 		    page_to_pfn(virt_to_page((void *)initrd_start)),
738bd673c7cSGeert Uytterhoeven 		    min_low_pfn);
7391da177e4SLinus Torvalds 		initrd_start = 0;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds #endif
742e7c8d5c9SChristoph Lameter 	setup_per_cpu_pageset();
7431da177e4SLinus Torvalds 	numa_policy_init();
7449c71206dSDou Liyang 	acpi_early_init();
7451da177e4SLinus Torvalds 	if (late_time_init)
7461da177e4SLinus Torvalds 		late_time_init();
747857baa87SPavel Tatashin 	sched_clock_init();
7481da177e4SLinus Torvalds 	calibrate_delay();
74995846ecfSGargi Sharma 	pid_idr_init();
7501da177e4SLinus Torvalds 	anon_vma_init();
75111520e5eSLinus Torvalds #ifdef CONFIG_X86
75283e68189SMatt Fleming 	if (efi_enabled(EFI_RUNTIME_SERVICES))
75311520e5eSLinus Torvalds 		efi_enter_virtual_mode();
75411520e5eSLinus Torvalds #endif
755b235beeaSLinus Torvalds 	thread_stack_cache_init();
756d84f4f99SDavid Howells 	cred_init();
757ff691f6eSHeinrich Schuchardt 	fork_init();
7581da177e4SLinus Torvalds 	proc_caches_init();
7593ea056c5SAlexey Dobriyan 	uts_ns_init();
7601da177e4SLinus Torvalds 	buffer_init();
7611da177e4SLinus Torvalds 	key_init();
7621da177e4SLinus Torvalds 	security_init();
7630b4b3827SJason Wessel 	dbg_late_init();
7644248b0daSMel Gorman 	vfs_caches_init();
76562906027SNicholas Piggin 	pagecache_init();
7661da177e4SLinus Torvalds 	signals_init();
76709652320SAlexey Dobriyan 	seq_file_init();
7681da177e4SLinus Torvalds 	proc_root_init();
769e149ed2bSAl Viro 	nsfs_init();
7701da177e4SLinus Torvalds 	cpuset_init();
771695df213SZefan Li 	cgroup_init();
772c757249aSShailabh Nagar 	taskstats_init_early();
773ca74e92bSShailabh Nagar 	delayacct_init();
7741da177e4SLinus Torvalds 
7754fc19708SNadav Amit 	poking_init();
7761da177e4SLinus Torvalds 	check_bugs();
7771da177e4SLinus Torvalds 
778b064a8faSRafael J. Wysocki 	acpi_subsystem_init();
779e7ff3a47SThomas Gleixner 	arch_post_acpi_subsys_init();
7806ae6996aSFeng Tang 	sfi_init_late();
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/* Do the rest non-__init'ed, we're now alive */
78353c99bd6SMartin Schwidefsky 	arch_call_rest_init();
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
786b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */
787b99b87f7SPeter Oberparleiter static void __init do_ctors(void)
788b99b87f7SPeter Oberparleiter {
789b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
790196a15b4SH Hartley Sweeten 	ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
791b99b87f7SPeter Oberparleiter 
792196a15b4SH Hartley Sweeten 	for (; fn < (ctor_fn_t *) __ctors_end; fn++)
793196a15b4SH Hartley Sweeten 		(*fn)();
794b99b87f7SPeter Oberparleiter #endif
795b99b87f7SPeter Oberparleiter }
796b99b87f7SPeter Oberparleiter 
7977b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS
7987b0b73d7SPrarit Bhargava struct blacklist_entry {
7997b0b73d7SPrarit Bhargava 	struct list_head next;
8007b0b73d7SPrarit Bhargava 	char *buf;
8017b0b73d7SPrarit Bhargava };
8027b0b73d7SPrarit Bhargava 
8037b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
8047b0b73d7SPrarit Bhargava 
8057b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
8067b0b73d7SPrarit Bhargava {
8077b0b73d7SPrarit Bhargava 	char *str_entry;
8087b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
8097b0b73d7SPrarit Bhargava 
8107b0b73d7SPrarit Bhargava 	/* str argument is a comma-separated list of functions */
8117b0b73d7SPrarit Bhargava 	do {
8127b0b73d7SPrarit Bhargava 		str_entry = strsep(&str, ",");
8137b0b73d7SPrarit Bhargava 		if (str_entry) {
8147b0b73d7SPrarit Bhargava 			pr_debug("blacklisting initcall %s\n", str_entry);
8157e1c4e27SMike Rapoport 			entry = memblock_alloc(sizeof(*entry),
8167e1c4e27SMike Rapoport 					       SMP_CACHE_BYTES);
817f5c7310aSMike Rapoport 			if (!entry)
818f5c7310aSMike Rapoport 				panic("%s: Failed to allocate %zu bytes\n",
819f5c7310aSMike Rapoport 				      __func__, sizeof(*entry));
8207e1c4e27SMike Rapoport 			entry->buf = memblock_alloc(strlen(str_entry) + 1,
8217e1c4e27SMike Rapoport 						    SMP_CACHE_BYTES);
822f5c7310aSMike Rapoport 			if (!entry->buf)
823f5c7310aSMike Rapoport 				panic("%s: Failed to allocate %zu bytes\n",
824f5c7310aSMike Rapoport 				      __func__, strlen(str_entry) + 1);
8257b0b73d7SPrarit Bhargava 			strcpy(entry->buf, str_entry);
8267b0b73d7SPrarit Bhargava 			list_add(&entry->next, &blacklisted_initcalls);
8277b0b73d7SPrarit Bhargava 		}
8287b0b73d7SPrarit Bhargava 	} while (str_entry);
8297b0b73d7SPrarit Bhargava 
8307b0b73d7SPrarit Bhargava 	return 0;
8317b0b73d7SPrarit Bhargava }
8327b0b73d7SPrarit Bhargava 
8337b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
8347b0b73d7SPrarit Bhargava {
8357b0b73d7SPrarit Bhargava 	struct blacklist_entry *entry;
836c8cdd2beSRasmus Villemoes 	char fn_name[KSYM_SYMBOL_LEN];
8370fd5ed8dSRasmus Villemoes 	unsigned long addr;
8387b0b73d7SPrarit Bhargava 
839c8cdd2beSRasmus Villemoes 	if (list_empty(&blacklisted_initcalls))
8407b0b73d7SPrarit Bhargava 		return false;
8417b0b73d7SPrarit Bhargava 
8420fd5ed8dSRasmus Villemoes 	addr = (unsigned long) dereference_function_descriptor(fn);
8430fd5ed8dSRasmus Villemoes 	sprint_symbol_no_offset(fn_name, addr);
844c8cdd2beSRasmus Villemoes 
845841c06d7SPrarit Bhargava 	/*
846841c06d7SPrarit Bhargava 	 * fn will be "function_name [module_name]" where [module_name] is not
847841c06d7SPrarit Bhargava 	 * displayed for built-in init functions.  Strip off the [module_name].
848841c06d7SPrarit Bhargava 	 */
849841c06d7SPrarit Bhargava 	strreplace(fn_name, ' ', '\0');
850841c06d7SPrarit Bhargava 
851e6fd1fb3SGeliang Tang 	list_for_each_entry(entry, &blacklisted_initcalls, next) {
8527b0b73d7SPrarit Bhargava 		if (!strcmp(fn_name, entry->buf)) {
8537b0b73d7SPrarit Bhargava 			pr_debug("initcall %s blacklisted\n", fn_name);
8547b0b73d7SPrarit Bhargava 			return true;
8557b0b73d7SPrarit Bhargava 		}
8567b0b73d7SPrarit Bhargava 	}
8577b0b73d7SPrarit Bhargava 
8587b0b73d7SPrarit Bhargava 	return false;
8597b0b73d7SPrarit Bhargava }
8607b0b73d7SPrarit Bhargava #else
8617b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str)
8627b0b73d7SPrarit Bhargava {
8637b0b73d7SPrarit Bhargava 	pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
8647b0b73d7SPrarit Bhargava 	return 0;
8657b0b73d7SPrarit Bhargava }
8667b0b73d7SPrarit Bhargava 
8677b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn)
8687b0b73d7SPrarit Bhargava {
8697b0b73d7SPrarit Bhargava 	return false;
8707b0b73d7SPrarit Bhargava }
8717b0b73d7SPrarit Bhargava #endif
8727b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist);
8737b0b73d7SPrarit Bhargava 
8744e37958dSSteven Rostedt (VMware) static __init_or_module void
8754e37958dSSteven Rostedt (VMware) trace_initcall_start_cb(void *data, initcall_t fn)
8761da177e4SLinus Torvalds {
8774e37958dSSteven Rostedt (VMware) 	ktime_t *calltime = (ktime_t *)data;
8781da177e4SLinus Torvalds 
879d75f773cSSakari Ailus 	printk(KERN_DEBUG "calling  %pS @ %i\n", fn, task_pid_nr(current));
8804e37958dSSteven Rostedt (VMware) 	*calltime = ktime_get();
8814e37958dSSteven Rostedt (VMware) }
8824e37958dSSteven Rostedt (VMware) 
8834e37958dSSteven Rostedt (VMware) static __init_or_module void
8844e37958dSSteven Rostedt (VMware) trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
8854e37958dSSteven Rostedt (VMware) {
8864e37958dSSteven Rostedt (VMware) 	ktime_t *calltime = (ktime_t *)data;
8874e37958dSSteven Rostedt (VMware) 	ktime_t delta, rettime;
8884e37958dSSteven Rostedt (VMware) 	unsigned long long duration;
8894e37958dSSteven Rostedt (VMware) 
89074239072SFrederic Weisbecker 	rettime = ktime_get();
8914e37958dSSteven Rostedt (VMware) 	delta = ktime_sub(rettime, *calltime);
89230dbb20eSAmérico Wang 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
893d75f773cSSakari Ailus 	printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
894ea676e84SAndrew Morton 		 fn, ret, duration);
8958f0c45cdSIngo Molnar }
8968f0c45cdSIngo Molnar 
8974e37958dSSteven Rostedt (VMware) static ktime_t initcall_calltime;
8984e37958dSSteven Rostedt (VMware) 
899b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED
9004e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void)
9014e37958dSSteven Rostedt (VMware) {
9024e37958dSSteven Rostedt (VMware) 	int ret;
9034e37958dSSteven Rostedt (VMware) 
9044e37958dSSteven Rostedt (VMware) 	ret = register_trace_initcall_start(trace_initcall_start_cb,
9054e37958dSSteven Rostedt (VMware) 					    &initcall_calltime);
9064e37958dSSteven Rostedt (VMware) 	ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
9074e37958dSSteven Rostedt (VMware) 					      &initcall_calltime);
9084e37958dSSteven Rostedt (VMware) 	WARN(ret, "Failed to register initcall tracepoints\n");
909c1cda48aSAndrew Morton }
910b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_start	trace_initcall_start
911b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_finish	trace_initcall_finish
912b0dc52f1SSteven Rostedt (VMware) #else
913b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_start(initcall_t fn)
914b0dc52f1SSteven Rostedt (VMware) {
915b0dc52f1SSteven Rostedt (VMware) 	if (!initcall_debug)
916b0dc52f1SSteven Rostedt (VMware) 		return;
917b0dc52f1SSteven Rostedt (VMware) 	trace_initcall_start_cb(&initcall_calltime, fn);
918b0dc52f1SSteven Rostedt (VMware) }
919b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_finish(initcall_t fn, int ret)
920b0dc52f1SSteven Rostedt (VMware) {
921b0dc52f1SSteven Rostedt (VMware) 	if (!initcall_debug)
922b0dc52f1SSteven Rostedt (VMware) 		return;
923b0dc52f1SSteven Rostedt (VMware) 	trace_initcall_finish_cb(&initcall_calltime, fn, ret);
924b0dc52f1SSteven Rostedt (VMware) }
925b0dc52f1SSteven Rostedt (VMware) #endif /* !TRACEPOINTS_ENABLED */
926c1cda48aSAndrew Morton 
927e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn)
92822c5c03bSKevin Winchester {
92922c5c03bSKevin Winchester 	int count = preempt_count();
930ff1c8facSSteven Rostedt 	char msgbuf[64];
9314e37958dSSteven Rostedt (VMware) 	int ret;
93222c5c03bSKevin Winchester 
9337b0b73d7SPrarit Bhargava 	if (initcall_blacklisted(fn))
9347b0b73d7SPrarit Bhargava 		return -EPERM;
9357b0b73d7SPrarit Bhargava 
936b0dc52f1SSteven Rostedt (VMware) 	do_trace_initcall_start(fn);
93722c5c03bSKevin Winchester 	ret = fn();
938b0dc52f1SSteven Rostedt (VMware) 	do_trace_initcall_finish(fn, ret);
93922c5c03bSKevin Winchester 
940e662e1cfSCyrill Gorcunov 	msgbuf[0] = 0;
941e662e1cfSCyrill Gorcunov 
9421da177e4SLinus Torvalds 	if (preempt_count() != count) {
943bf5d770bSSteven Rostedt 		sprintf(msgbuf, "preemption imbalance ");
9444a2b4b22SPeter Zijlstra 		preempt_count_set(count);
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 	if (irqs_disabled()) {
947a76bfd0dSCyrill Gorcunov 		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
9481da177e4SLinus Torvalds 		local_irq_enable();
9491da177e4SLinus Torvalds 	}
950d75f773cSSakari Ailus 	WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
95159f9415fSArjan van de Ven 
95238addce8SEmese Revfy 	add_latent_entropy();
95330dbb20eSAmérico Wang 	return ret;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
956e0df154fSLinus Torvalds 
9571b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall_start[];
9581b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall0_start[];
9591b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall1_start[];
9601b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall2_start[];
9611b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall3_start[];
9621b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall4_start[];
9631b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall5_start[];
9641b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall6_start[];
9651b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall7_start[];
9661b1eeca7SArd Biesheuvel extern initcall_entry_t __initcall_end[];
967026cee00SPawel Moll 
9681b1eeca7SArd Biesheuvel static initcall_entry_t *initcall_levels[] __initdata = {
969026cee00SPawel Moll 	__initcall0_start,
970026cee00SPawel Moll 	__initcall1_start,
971026cee00SPawel Moll 	__initcall2_start,
972026cee00SPawel Moll 	__initcall3_start,
973026cee00SPawel Moll 	__initcall4_start,
974026cee00SPawel Moll 	__initcall5_start,
975026cee00SPawel Moll 	__initcall6_start,
976026cee00SPawel Moll 	__initcall7_start,
977026cee00SPawel Moll 	__initcall_end,
978026cee00SPawel Moll };
979026cee00SPawel Moll 
98096263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */
9817c8f7193SAlexey Dobriyan static const char *initcall_level_names[] __initdata = {
982a6fb6012SSteven Rostedt (VMware) 	"pure",
9839fb48c74SJim Cromie 	"core",
9849fb48c74SJim Cromie 	"postcore",
9859fb48c74SJim Cromie 	"arch",
9869fb48c74SJim Cromie 	"subsys",
9879fb48c74SJim Cromie 	"fs",
9889fb48c74SJim Cromie 	"device",
9899fb48c74SJim Cromie 	"late",
990026cee00SPawel Moll };
991026cee00SPawel Moll 
992*7e2762e1SArvind Sankar static int __init ignore_unknown_bootoption(char *param, char *val,
993*7e2762e1SArvind Sankar 			       const char *unused, void *arg)
994*7e2762e1SArvind Sankar {
995*7e2762e1SArvind Sankar 	return 0;
996*7e2762e1SArvind Sankar }
997*7e2762e1SArvind Sankar 
998026cee00SPawel Moll static void __init do_initcall_level(int level)
999026cee00SPawel Moll {
10001b1eeca7SArd Biesheuvel 	initcall_entry_t *fn;
1001026cee00SPawel Moll 
100208746a65SKrzysztof Mazur 	strcpy(initcall_command_line, saved_command_line);
1003026cee00SPawel Moll 	parse_args(initcall_level_names[level],
100408746a65SKrzysztof Mazur 		   initcall_command_line, __start___param,
1005026cee00SPawel Moll 		   __stop___param - __start___param,
1006026cee00SPawel Moll 		   level, level,
1007*7e2762e1SArvind Sankar 		   NULL, ignore_unknown_bootoption);
1008026cee00SPawel Moll 
10094ee7c60dSSteven Rostedt (VMware) 	trace_initcall_level(initcall_level_names[level]);
1010026cee00SPawel Moll 	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
10111b1eeca7SArd Biesheuvel 		do_one_initcall(initcall_from_entry(fn));
1012026cee00SPawel Moll }
1013e0df154fSLinus Torvalds 
1014e0df154fSLinus Torvalds static void __init do_initcalls(void)
1015e0df154fSLinus Torvalds {
1016026cee00SPawel Moll 	int level;
1017e0df154fSLinus Torvalds 
101819efb72fSBorislav Petkov 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
1019026cee00SPawel Moll 		do_initcall_level(level);
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /*
10231da177e4SLinus Torvalds  * Ok, the machine is now initialized. None of the devices
10241da177e4SLinus Torvalds  * have been touched yet, but the CPU subsystem is up and
10251da177e4SLinus Torvalds  * running, and memory and process management works.
10261da177e4SLinus Torvalds  *
10271da177e4SLinus Torvalds  * Now we can finally start doing some real work..
10281da177e4SLinus Torvalds  */
10291da177e4SLinus Torvalds static void __init do_basic_setup(void)
10301da177e4SLinus Torvalds {
1031759ee091SLai Jiangshan 	cpuset_init_smp();
10321da177e4SLinus Torvalds 	driver_init();
1033b04c3afbSEric W. Biederman 	init_irq_proc();
1034b99b87f7SPeter Oberparleiter 	do_ctors();
1035d5767c53SLinus Torvalds 	usermodehelper_enable();
1036b0f84374Swangyanqing 	do_initcalls();
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10397babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void)
1040c2147a50SEduard - Gabriel Munteanu {
10411b1eeca7SArd Biesheuvel 	initcall_entry_t *fn;
1042c2147a50SEduard - Gabriel Munteanu 
10434ee7c60dSSteven Rostedt (VMware) 	trace_initcall_level("early");
1044026cee00SPawel Moll 	for (fn = __initcall_start; fn < __initcall0_start; fn++)
10451b1eeca7SArd Biesheuvel 		do_one_initcall(initcall_from_entry(fn));
1046c2147a50SEduard - Gabriel Munteanu }
1047c2147a50SEduard - Gabriel Munteanu 
1048a74fb73cSAl Viro static int run_init_process(const char *init_filename)
10491da177e4SLinus Torvalds {
1050b88c50acSArvind Sankar 	const char *const *p;
1051b88c50acSArvind Sankar 
10521da177e4SLinus Torvalds 	argv_init[0] = init_filename;
10533f5c15d8SPaul Menzel 	pr_info("Run %s as init process\n", init_filename);
1054b88c50acSArvind Sankar 	pr_debug("  with arguments:\n");
1055b88c50acSArvind Sankar 	for (p = argv_init; *p; p++)
1056b88c50acSArvind Sankar 		pr_debug("    %s\n", *p);
1057b88c50acSArvind Sankar 	pr_debug("  with environment:\n");
1058b88c50acSArvind Sankar 	for (p = envp_init; *p; p++)
1059b88c50acSArvind Sankar 		pr_debug("    %s\n", *p);
1060c4ad8f98SLinus Torvalds 	return do_execve(getname_kernel(init_filename),
1061ae903caaSAl Viro 		(const char __user *const __user *)argv_init,
1062ae903caaSAl Viro 		(const char __user *const __user *)envp_init);
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
1065ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename)
1066ba24762bSMichael Opdenacker {
1067ba24762bSMichael Opdenacker 	int ret;
1068ba24762bSMichael Opdenacker 
1069ba24762bSMichael Opdenacker 	ret = run_init_process(init_filename);
1070ba24762bSMichael Opdenacker 
1071ba24762bSMichael Opdenacker 	if (ret && ret != -ENOENT) {
1072ba24762bSMichael Opdenacker 		pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
1073ba24762bSMichael Opdenacker 		       init_filename, ret);
1074ba24762bSMichael Opdenacker 	}
1075ba24762bSMichael Opdenacker 
1076ba24762bSMichael Opdenacker 	return ret;
1077ba24762bSMichael Opdenacker }
1078ba24762bSMichael Opdenacker 
1079f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void);
1080d6b21238SAl Viro 
10810f5bf6d0SLaura Abbott #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
108239290b38SAKASHI Takahiro bool rodata_enabled __ro_after_init = true;
1083d2aa1acaSKees Cook static int __init set_debug_rodata(char *str)
1084d2aa1acaSKees Cook {
1085d2aa1acaSKees Cook 	return strtobool(str, &rodata_enabled);
1086d2aa1acaSKees Cook }
1087d2aa1acaSKees Cook __setup("rodata=", set_debug_rodata);
108839290b38SAKASHI Takahiro #endif
1089d2aa1acaSKees Cook 
10900f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
1091d2aa1acaSKees Cook static void mark_readonly(void)
1092d2aa1acaSKees Cook {
10932959a5f7SJinbum Park 	if (rodata_enabled) {
1094ae646f0bSJeffrey Hugo 		/*
1095ba180314SPaul E. McKenney 		 * load_module() results in W+X mappings, which are cleaned
1096ba180314SPaul E. McKenney 		 * up with call_rcu().  Let's make sure that queued work is
1097ae646f0bSJeffrey Hugo 		 * flushed so that we don't hit false positives looking for
1098ae646f0bSJeffrey Hugo 		 * insecure pages which are W+X.
1099ae646f0bSJeffrey Hugo 		 */
1100ba180314SPaul E. McKenney 		rcu_barrier();
1101d2aa1acaSKees Cook 		mark_rodata_ro();
11022959a5f7SJinbum Park 		rodata_test();
11032959a5f7SJinbum Park 	} else
1104d2aa1acaSKees Cook 		pr_info("Kernel memory protection disabled.\n");
1105d2aa1acaSKees Cook }
1106d2aa1acaSKees Cook #else
1107d2aa1acaSKees Cook static inline void mark_readonly(void)
1108d2aa1acaSKees Cook {
1109d2aa1acaSKees Cook 	pr_warn("This architecture does not have kernel memory protection.\n");
1110d2aa1acaSKees Cook }
1111d2aa1acaSKees Cook #endif
1112d2aa1acaSKees Cook 
1113997aef68SMike Rapoport void __weak free_initmem(void)
1114997aef68SMike Rapoport {
1115f4039999SMike Rapoport 	free_initmem_default(POISON_FREE_INITMEM);
1116997aef68SMike Rapoport }
1117997aef68SMike Rapoport 
1118d6b21238SAl Viro static int __ref kernel_init(void *unused)
1119ee5bfa64SVivek Goyal {
1120ba24762bSMichael Opdenacker 	int ret;
1121ba24762bSMichael Opdenacker 
1122d6b21238SAl Viro 	kernel_init_freeable();
112322a9d645SArjan van de Ven 	/* need to finish all async __init code before freeing the memory */
112422a9d645SArjan van de Ven 	async_synchronize_full();
1125b80f0f6cSSteven Rostedt (VMware) 	ftrace_free_init_mem();
1126ee5bfa64SVivek Goyal 	free_initmem();
1127d2aa1acaSKees Cook 	mark_readonly();
1128b976690fSJoerg Roedel 
1129b976690fSJoerg Roedel 	/*
1130b976690fSJoerg Roedel 	 * Kernel mappings are now finalized - update the userspace page-table
1131b976690fSJoerg Roedel 	 * to finalize PTI.
1132b976690fSJoerg Roedel 	 */
1133b976690fSJoerg Roedel 	pti_finalize();
1134b976690fSJoerg Roedel 
1135ee5bfa64SVivek Goyal 	system_state = SYSTEM_RUNNING;
1136ee5bfa64SVivek Goyal 	numa_default_policy();
1137ee5bfa64SVivek Goyal 
1138967dcb8fSPaul E. McKenney 	rcu_end_inkernel_boot();
1139967dcb8fSPaul E. McKenney 
1140ee5bfa64SVivek Goyal 	if (ramdisk_execute_command) {
1141ba24762bSMichael Opdenacker 		ret = run_init_process(ramdisk_execute_command);
1142ba24762bSMichael Opdenacker 		if (!ret)
1143a74fb73cSAl Viro 			return 0;
1144ba24762bSMichael Opdenacker 		pr_err("Failed to execute %s (error %d)\n",
1145ba24762bSMichael Opdenacker 		       ramdisk_execute_command, ret);
1146ee5bfa64SVivek Goyal 	}
1147ee5bfa64SVivek Goyal 
1148ee5bfa64SVivek Goyal 	/*
1149ee5bfa64SVivek Goyal 	 * We try each of these until one succeeds.
1150ee5bfa64SVivek Goyal 	 *
1151ee5bfa64SVivek Goyal 	 * The Bourne shell can be used instead of init if we are
1152ee5bfa64SVivek Goyal 	 * trying to recover a really broken machine.
1153ee5bfa64SVivek Goyal 	 */
1154ee5bfa64SVivek Goyal 	if (execute_command) {
1155ba24762bSMichael Opdenacker 		ret = run_init_process(execute_command);
1156ba24762bSMichael Opdenacker 		if (!ret)
1157a74fb73cSAl Viro 			return 0;
11586ef4536eSAndy Lutomirski 		panic("Requested init %s failed (error %d).",
11596ef4536eSAndy Lutomirski 		      execute_command, ret);
1160ee5bfa64SVivek Goyal 	}
1161ba24762bSMichael Opdenacker 	if (!try_to_run_init_process("/sbin/init") ||
1162ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/etc/init") ||
1163ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/init") ||
1164ba24762bSMichael Opdenacker 	    !try_to_run_init_process("/bin/sh"))
1165a74fb73cSAl Viro 		return 0;
1166ee5bfa64SVivek Goyal 
1167ba24762bSMichael Opdenacker 	panic("No working init found.  Try passing init= option to kernel. "
11688c27ceffSMauro Carvalho Chehab 	      "See Linux Documentation/admin-guide/init.rst for guidance.");
1169ee5bfa64SVivek Goyal }
1170ee5bfa64SVivek Goyal 
1171b49a733dSDominik Brodowski void console_on_rootfs(void)
1172b49a733dSDominik Brodowski {
117374f1a299SDominik Brodowski 	/* Open the /dev/console as stdin, this should never fail */
117474f1a299SDominik Brodowski 	if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
11758243186fSDominik Brodowski 		pr_err("Warning: unable to open an initial console.\n");
117674f1a299SDominik Brodowski 
117774f1a299SDominik Brodowski 	/* create stdout/stderr */
117874f1a299SDominik Brodowski 	(void) ksys_dup(0);
117974f1a299SDominik Brodowski 	(void) ksys_dup(0);
1180b49a733dSDominik Brodowski }
1181b49a733dSDominik Brodowski 
1182f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void)
11831da177e4SLinus Torvalds {
1184b433c3d4SPeter Zijlstra 	/*
1185b433c3d4SPeter Zijlstra 	 * Wait until kthreadd is all set-up.
1186b433c3d4SPeter Zijlstra 	 */
1187b433c3d4SPeter Zijlstra 	wait_for_completion(&kthreadd_done);
118831a67102SLinus Torvalds 
118931a67102SLinus Torvalds 	/* Now the scheduler is fully set up and can do blocking allocations */
119031a67102SLinus Torvalds 	gfp_allowed_mask = __GFP_BITS_MASK;
119131a67102SLinus Torvalds 
119258568d2aSMiao Xie 	/*
119358568d2aSMiao Xie 	 * init can allocate pages on any node
119458568d2aSMiao Xie 	 */
11953c466d46SLai Jiangshan 	set_mems_allowed(node_states[N_MEMORY]);
11961da177e4SLinus Torvalds 
11979ec52099SCedric Le Goater 	cad_pid = task_pid(current);
11989ec52099SCedric Le Goater 
1199ca74a6f8SAndi Kleen 	smp_prepare_cpus(setup_max_cpus);
12001da177e4SLinus Torvalds 
12013347fa09STejun Heo 	workqueue_init();
12023347fa09STejun Heo 
1203597b7305SMichal Hocko 	init_mm_internals();
1204597b7305SMichal Hocko 
12051da177e4SLinus Torvalds 	do_pre_smp_initcalls();
1206004417a6SPeter Zijlstra 	lockup_detector_init();
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	smp_init();
12091da177e4SLinus Torvalds 	sched_init_smp();
12101da177e4SLinus Torvalds 
12110e1cc95bSMel Gorman 	page_alloc_init_late();
12122f1ee091SQian Cai 	/* Initialize page ext after all struct pages are initialized. */
12132f1ee091SQian Cai 	page_ext_init();
12140e1cc95bSMel Gorman 
12151da177e4SLinus Torvalds 	do_basic_setup();
12161da177e4SLinus Torvalds 
1217b49a733dSDominik Brodowski 	console_on_rootfs();
12182bd3a997SEric W. Biederman 
12191da177e4SLinus Torvalds 	/*
12201da177e4SLinus Torvalds 	 * check if there is an early userspace init.  If yes, let it do all
12211da177e4SLinus Torvalds 	 * the work
12221da177e4SLinus Torvalds 	 */
1223ffdfc409SOlof Johansson 
1224ffdfc409SOlof Johansson 	if (!ramdisk_execute_command)
1225ffdfc409SOlof Johansson 		ramdisk_execute_command = "/init";
1226ffdfc409SOlof Johansson 
1227cbfe20f5SDominik Brodowski 	if (ksys_access((const char __user *)
1228cbfe20f5SDominik Brodowski 			ramdisk_execute_command, 0) != 0) {
1229ffdfc409SOlof Johansson 		ramdisk_execute_command = NULL;
12301da177e4SLinus Torvalds 		prepare_namespace();
1231ffdfc409SOlof Johansson 	}
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	/*
12341da177e4SLinus Torvalds 	 * Ok, we have completed the initial bootup, and
12351da177e4SLinus Torvalds 	 * we're essentially up and running. Get rid of the
12361da177e4SLinus Torvalds 	 * initmem segments and start the user-mode stuff..
1237c9cd2ce2SDmitry Kasatkin 	 *
1238c9cd2ce2SDmitry Kasatkin 	 * rootfs is available now, try loading the public keys
1239c9cd2ce2SDmitry Kasatkin 	 * and default modules
12401da177e4SLinus Torvalds 	 */
1241bb813f4cSTejun Heo 
1242c9cd2ce2SDmitry Kasatkin 	integrity_load_keys();
12431da177e4SLinus Torvalds }
1244