1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/init/main.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * GK 2/5/95 - Changed to support mounting root fs via NFS 81da177e4SLinus Torvalds * Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96 91da177e4SLinus Torvalds * Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96 101da177e4SLinus Torvalds * Simplified starting of init: Michael A. Griffith <[email protected]> 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13ea676e84SAndrew Morton #define DEBUG /* Enable initcall_debug */ 14ea676e84SAndrew Morton 151da177e4SLinus Torvalds #include <linux/types.h> 168a293be0SPaul Gortmaker #include <linux/extable.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/proc_fs.h> 195c2c5c55SIngo Molnar #include <linux/binfmts.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/syscalls.h> 229b5609fdSIngo Molnar #include <linux/stackprotector.h> 231da177e4SLinus Torvalds #include <linux/string.h> 241da177e4SLinus Torvalds #include <linux/ctype.h> 251da177e4SLinus Torvalds #include <linux/delay.h> 261da177e4SLinus Torvalds #include <linux/ioport.h> 271da177e4SLinus Torvalds #include <linux/init.h> 281da177e4SLinus Torvalds #include <linux/initrd.h> 2957c8a661SMike Rapoport #include <linux/memblock.h> 304a7a16dcSLen Brown #include <linux/acpi.h> 317684b858SMasami Hiramatsu #include <linux/bootconfig.h> 320c688614SNicolas Pitre #include <linux/console.h> 3338b8d208SIngo Molnar #include <linux/nmi.h> 341da177e4SLinus Torvalds #include <linux/percpu.h> 351da177e4SLinus Torvalds #include <linux/kmod.h> 3682d083abSMasami Hiramatsu #include <linux/kprobes.h> 373c206509SAlexander Potapenko #include <linux/kmsan.h> 38db64fe02SNick Piggin #include <linux/vmalloc.h> 391da177e4SLinus Torvalds #include <linux/kernel_stat.h> 40d7cd5611SRusty Russell #include <linux/start_kernel.h> 411da177e4SLinus Torvalds #include <linux/security.h> 423d442233SJens Axboe #include <linux/smp.h> 431da177e4SLinus Torvalds #include <linux/profile.h> 440ce20dd8SAlexander Potapenko #include <linux/kfence.h> 451da177e4SLinus Torvalds #include <linux/rcupdate.h> 468e9c01c7SFrederic Weisbecker #include <linux/srcu.h> 471da177e4SLinus Torvalds #include <linux/moduleparam.h> 481da177e4SLinus Torvalds #include <linux/kallsyms.h> 4983cc6fa0SStephen Boyd #include <linux/buildid.h> 501da177e4SLinus Torvalds #include <linux/writeback.h> 511da177e4SLinus Torvalds #include <linux/cpu.h> 521da177e4SLinus Torvalds #include <linux/cpuset.h> 53ddbcc7e8SPaul Menage #include <linux/cgroup.h> 541da177e4SLinus Torvalds #include <linux/efi.h> 55906568c9SThomas Gleixner #include <linux/tick.h> 5678634061SFrederic Weisbecker #include <linux/sched/isolation.h> 576168a702SAndrew Morton #include <linux/interrupt.h> 58c757249aSShailabh Nagar #include <linux/taskstats_kern.h> 59ca74e92bSShailabh Nagar #include <linux/delayacct.h> 601da177e4SLinus Torvalds #include <linux/unistd.h> 613ea056c5SAlexey Dobriyan #include <linux/utsname.h> 621da177e4SLinus Torvalds #include <linux/rmap.h> 631da177e4SLinus Torvalds #include <linux/mempolicy.h> 641da177e4SLinus Torvalds #include <linux/key.h> 659a11b49aSIngo Molnar #include <linux/debug_locks.h> 663ac7fe5aSThomas Gleixner #include <linux/debugobjects.h> 67fbb9ce95SIngo Molnar #include <linux/lockdep.h> 683c7b4e6bSCatalin Marinas #include <linux/kmemleak.h> 69f1b192b1SDaniel Jordan #include <linux/padata.h> 7084d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h> 714c002c97SGreg Kroah-Hartman #include <linux/device/driver.h> 7273c27992SEric W. Biederman #include <linux/kthread.h> 73e6fe6649SAdrian Bunk #include <linux/sched.h> 741777e463SIngo Molnar #include <linux/sched/init.h> 75a1c9eea9SAdrian Bunk #include <linux/signal.h> 76199f0ca5SAkinobu Mita #include <linux/idr.h> 770b4b3827SJason Wessel #include <linux/kgdb.h> 7868bf21aaSSteven Rostedt #include <linux/ftrace.h> 7922a9d645SArjan van de Ven #include <linux/async.h> 802b2af54aSKay Sievers #include <linux/shmem_fs.h> 815a0e3ad6STejun Heo #include <linux/slab.h> 8224a24bb6SPeter Zijlstra #include <linux/perf_event.h> 83a74fb73cSAl Viro #include <linux/ptrace.h> 84aa8c6248SThomas Gleixner #include <linux/pti.h> 85bb813f4cSTejun Heo #include <linux/blkdev.h> 865d2a4e91SPavel Tatashin #include <linux/sched/clock.h> 8729930025SIngo Molnar #include <linux/sched/task.h> 8868db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 8965f382fdSFrederic Weisbecker #include <linux/context_tracking.h> 9047d06e53STheodore Ts'o #include <linux/random.h> 918f8cd6c0SChangbin Du #include <linux/moduleloader.h> 927b0b73d7SPrarit Bhargava #include <linux/list.h> 93c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h> 94e149ed2bSAl Viro #include <linux/proc_ns.h> 950ddab1d2SToshi Kani #include <linux/io.h> 9639290b38SAKASHI Takahiro #include <linux/cache.h> 972959a5f7SJinbum Park #include <linux/rodata_test.h> 9833352244SJosh Poimboeuf #include <linux/jump_label.h> 99dfd402a4SMarco Elver #include <linux/kcsan.h> 100eb9d7d39SChristoph Hellwig #include <linux/init_syscalls.h> 101e1fdc403SVijayanand Jitta #include <linux/stackdepot.h> 102375561bdSGONG, Ruiqi #include <linux/randomize_kstack.h> 103cb12fd8eSChristian Brauner #include <linux/pidfs.h> 104a5e8131aSChristophe Leroy #include <linux/ptdump.h> 1059c1be193SEric Dumazet #include <net/net_namespace.h> 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds #include <asm/io.h> 1081da177e4SLinus Torvalds #include <asm/setup.h> 109a940199fSAndi Kleen #include <asm/sections.h> 11037b73c82SArjan van de Ven #include <asm/cacheflush.h> 1111da177e4SLinus Torvalds 1124ee7c60dSSteven Rostedt (VMware) #define CREATE_TRACE_POINTS 1134ee7c60dSSteven Rostedt (VMware) #include <trace/events/initcall.h> 1144ee7c60dSSteven Rostedt (VMware) 1158c0d8849SBrendan Higgins #include <kunit/test.h> 1168c0d8849SBrendan Higgins 117aae5f662SSam Ravnborg static int kernel_init(void *); 1181da177e4SLinus Torvalds 1192ce802f6STejun Heo /* 1202ce802f6STejun Heo * Debug helper: via this flag we know that we are in 'early bootup code' 1212ce802f6STejun Heo * where only the boot processor is running with IRQ disabled. This means 1222ce802f6STejun Heo * two things - IRQ must not be enabled before the flag is cleared and some 1232ce802f6STejun Heo * operations which are not allowed with IRQ disabled are allowed while the 1242ce802f6STejun Heo * flag is set. 1252ce802f6STejun Heo */ 1262ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly; 1272ce802f6STejun Heo 128a6826048SPaul E. McKenney enum system_states system_state __read_mostly; 1291da177e4SLinus Torvalds EXPORT_SYMBOL(system_state); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * Boot command-line arguments 1331da177e4SLinus Torvalds */ 1341da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT 1351da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */ 138d2e3192bSJan Beulich void (*__initdata late_time_init)(void); 1391da177e4SLinus Torvalds 14030d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */ 14130d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE]; 14230d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */ 143941baf6fSAlexey Dobriyan char *saved_command_line __ro_after_init; 144941baf6fSAlexey Dobriyan unsigned int saved_command_line_len __ro_after_init; 14530d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */ 14630d7e0d4SAlon Bar-Lev static char *static_command_line; 14751887d03SMasami Hiramatsu /* Untouched extra command line */ 14851887d03SMasami Hiramatsu static char *extra_command_line; 14913199162SMasami Hiramatsu /* Extra init arguments */ 15013199162SMasami Hiramatsu static char *extra_init_args; 1511da177e4SLinus Torvalds 152f61872bbSSteven Rostedt (VMware) #ifdef CONFIG_BOOT_CONFIG 153f61872bbSSteven Rostedt (VMware) /* Is bootconfig on command line? */ 154caa0708aSMasami Hiramatsu (Google) static bool bootconfig_found; 155b66fbbe8SMasami Hiramatsu static size_t initargs_offs; 156f61872bbSSteven Rostedt (VMware) #else 157f61872bbSSteven Rostedt (VMware) # define bootconfig_found false 158b66fbbe8SMasami Hiramatsu # define initargs_offs 0 159f61872bbSSteven Rostedt (VMware) #endif 160f61872bbSSteven Rostedt (VMware) 1611da177e4SLinus Torvalds static char *execute_command; 162916db733SChristoph Hellwig static char *ramdisk_execute_command = "/init"; 1631da177e4SLinus Torvalds 1648b3b2955SJan Beulich /* 165c4b2c0c5SHannes Frederic Sowa * Used to generate warnings if static_key manipulation functions are used 166c4b2c0c5SHannes Frederic Sowa * before jump_label_init is called. 167c4b2c0c5SHannes Frederic Sowa */ 168dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly; 169c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized); 170c4b2c0c5SHannes Frederic Sowa 171c4b2c0c5SHannes Frederic Sowa /* 1728b3b2955SJan Beulich * If set, this is an indication to the drivers that reset the underlying 1738b3b2955SJan Beulich * device before going ahead with the initialization otherwise driver might 1748b3b2955SJan Beulich * rely on the BIOS and skip the reset operation. 1758b3b2955SJan Beulich * 1768b3b2955SJan Beulich * This is useful if kernel is booting in an unreliable environment. 177fc454fdcSFrans Klaver * For ex. kdump situation where previous kernel has crashed, BIOS has been 1788b3b2955SJan Beulich * skipped and devices will be in unknown state. 1798b3b2955SJan Beulich */ 1808b3b2955SJan Beulich unsigned int reset_devices; 1818b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices); 1821da177e4SLinus Torvalds 1837e96287dSVivek Goyal static int __init set_reset_devices(char *str) 1847e96287dSVivek Goyal { 1857e96287dSVivek Goyal reset_devices = 1; 1867e96287dSVivek Goyal return 1; 1877e96287dSVivek Goyal } 1887e96287dSVivek Goyal 1897e96287dSVivek Goyal __setup("reset_devices", set_reset_devices); 1907e96287dSVivek Goyal 191d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, }; 192d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, }; 1931da177e4SLinus Torvalds static const char *panic_later, *panic_param; 1941da177e4SLinus Torvalds 19531c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line) 1961da177e4SLinus Torvalds { 197914dcaa8SRusty Russell const struct obs_kernel_param *p; 19831c025b5SYaowei Bai bool had_early_param = false; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds p = __setup_start; 2011da177e4SLinus Torvalds do { 2021da177e4SLinus Torvalds int n = strlen(p->str); 203b1e4d20cSMichal Schmidt if (parameqn(line, p->str, n)) { 2041da177e4SLinus Torvalds if (p->early) { 20533df0d19SRusty Russell /* Already done in parse_early_param? 20633df0d19SRusty Russell * (Needs exact match on param part). 20733df0d19SRusty Russell * Keep iterating, as we can have early 20833df0d19SRusty Russell * params and __setups of same names 8( */ 2091da177e4SLinus Torvalds if (line[n] == '\0' || line[n] == '=') 21031c025b5SYaowei Bai had_early_param = true; 2111da177e4SLinus Torvalds } else if (!p->setup_func) { 212ea676e84SAndrew Morton pr_warn("Parameter %s is obsolete, ignored\n", 213ea676e84SAndrew Morton p->str); 21431c025b5SYaowei Bai return true; 2151da177e4SLinus Torvalds } else if (p->setup_func(line + n)) 21631c025b5SYaowei Bai return true; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds p++; 2191da177e4SLinus Torvalds } while (p < __setup_end); 22033df0d19SRusty Russell 22133df0d19SRusty Russell return had_early_param; 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds * This should be approx 2 Bo*oMips to start (note initial shift), and will 2261da177e4SLinus Torvalds * still work even if initially too large, it will just take slightly longer 2271da177e4SLinus Torvalds */ 2281da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12); 2291da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy); 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static int __init debug_kernel(char *str) 2321da177e4SLinus Torvalds { 233a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEBUG; 234f6f21c81SYinghai Lu return 0; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static int __init quiet_kernel(char *str) 2381da177e4SLinus Torvalds { 239a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_QUIET; 240f6f21c81SYinghai Lu return 0; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 243f6f21c81SYinghai Lu early_param("debug", debug_kernel); 244f6f21c81SYinghai Lu early_param("quiet", quiet_kernel); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static int __init loglevel(char *str) 2471da177e4SLinus Torvalds { 248808bf29bSAlexander Sverdlin int newlevel; 249808bf29bSAlexander Sverdlin 250808bf29bSAlexander Sverdlin /* 251808bf29bSAlexander Sverdlin * Only update loglevel value when a correct setting was passed, 252808bf29bSAlexander Sverdlin * to prevent blind crashes (when loglevel being set to 0) that 253808bf29bSAlexander Sverdlin * are quite hard to debug 254808bf29bSAlexander Sverdlin */ 255808bf29bSAlexander Sverdlin if (get_option(&str, &newlevel)) { 256808bf29bSAlexander Sverdlin console_loglevel = newlevel; 257d9d4fcfeSAlex Riesen return 0; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 260808bf29bSAlexander Sverdlin return -EINVAL; 261808bf29bSAlexander Sverdlin } 262808bf29bSAlexander Sverdlin 263f6f21c81SYinghai Lu early_param("loglevel", loglevel); 2641da177e4SLinus Torvalds 265de462e5fSMasami Hiramatsu #ifdef CONFIG_BLK_DEV_INITRD 266a2a9d67aSMasami Hiramatsu static void * __init get_boot_config_from_initrd(size_t *_size) 267de462e5fSMasami Hiramatsu { 268de462e5fSMasami Hiramatsu u32 size, csum; 269de462e5fSMasami Hiramatsu char *data; 270de462e5fSMasami Hiramatsu u32 *hdr; 27150b8a742SMasami Hiramatsu int i; 272de462e5fSMasami Hiramatsu 273de462e5fSMasami Hiramatsu if (!initrd_end) 274de462e5fSMasami Hiramatsu return NULL; 275de462e5fSMasami Hiramatsu 276de462e5fSMasami Hiramatsu data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN; 27750b8a742SMasami Hiramatsu /* 27850b8a742SMasami Hiramatsu * Since Grub may align the size of initrd to 4, we must 27950b8a742SMasami Hiramatsu * check the preceding 3 bytes as well. 28050b8a742SMasami Hiramatsu */ 28150b8a742SMasami Hiramatsu for (i = 0; i < 4; i++) { 28250b8a742SMasami Hiramatsu if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN)) 28350b8a742SMasami Hiramatsu goto found; 28450b8a742SMasami Hiramatsu data--; 28550b8a742SMasami Hiramatsu } 286de462e5fSMasami Hiramatsu return NULL; 287de462e5fSMasami Hiramatsu 28850b8a742SMasami Hiramatsu found: 289de462e5fSMasami Hiramatsu hdr = (u32 *)(data - 8); 29024aed094SMasami Hiramatsu size = le32_to_cpu(hdr[0]); 29124aed094SMasami Hiramatsu csum = le32_to_cpu(hdr[1]); 292de462e5fSMasami Hiramatsu 293de462e5fSMasami Hiramatsu data = ((void *)hdr) - size; 294de462e5fSMasami Hiramatsu if ((unsigned long)data < initrd_start) { 295de462e5fSMasami Hiramatsu pr_err("bootconfig size %d is greater than initrd size %ld\n", 296de462e5fSMasami Hiramatsu size, initrd_end - initrd_start); 297de462e5fSMasami Hiramatsu return NULL; 298de462e5fSMasami Hiramatsu } 299de462e5fSMasami Hiramatsu 300765b8552SMasami Hiramatsu if (xbc_calc_checksum(data, size) != csum) { 301765b8552SMasami Hiramatsu pr_err("bootconfig checksum failed\n"); 302765b8552SMasami Hiramatsu return NULL; 303765b8552SMasami Hiramatsu } 304765b8552SMasami Hiramatsu 305de462e5fSMasami Hiramatsu /* Remove bootconfig from initramfs/initrd */ 306de462e5fSMasami Hiramatsu initrd_end = (unsigned long)data; 307de462e5fSMasami Hiramatsu if (_size) 308de462e5fSMasami Hiramatsu *_size = size; 309de462e5fSMasami Hiramatsu 310de462e5fSMasami Hiramatsu return data; 311de462e5fSMasami Hiramatsu } 312de462e5fSMasami Hiramatsu #else 313a2a9d67aSMasami Hiramatsu static void * __init get_boot_config_from_initrd(size_t *_size) 314de462e5fSMasami Hiramatsu { 315de462e5fSMasami Hiramatsu return NULL; 316de462e5fSMasami Hiramatsu } 317de462e5fSMasami Hiramatsu #endif 318de462e5fSMasami Hiramatsu 3197684b858SMasami Hiramatsu #ifdef CONFIG_BOOT_CONFIG 32051887d03SMasami Hiramatsu 321a27026e9SJason Yan static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata; 32251887d03SMasami Hiramatsu 32351887d03SMasami Hiramatsu #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0) 32451887d03SMasami Hiramatsu 32551887d03SMasami Hiramatsu static int __init xbc_snprint_cmdline(char *buf, size_t size, 32651887d03SMasami Hiramatsu struct xbc_node *root) 32751887d03SMasami Hiramatsu { 32851887d03SMasami Hiramatsu struct xbc_node *knode, *vnode; 32951887d03SMasami Hiramatsu char *end = buf + size; 330efee03a5SRasmus Villemoes const char *val, *q; 33151887d03SMasami Hiramatsu int ret; 33251887d03SMasami Hiramatsu 33351887d03SMasami Hiramatsu xbc_node_for_each_key_value(root, knode, val) { 33451887d03SMasami Hiramatsu ret = xbc_node_compose_key_after(root, knode, 33551887d03SMasami Hiramatsu xbc_namebuf, XBC_KEYLEN_MAX); 33651887d03SMasami Hiramatsu if (ret < 0) 33751887d03SMasami Hiramatsu return ret; 33851887d03SMasami Hiramatsu 33951887d03SMasami Hiramatsu vnode = xbc_node_get_child(knode); 34088b91371SMasami Hiramatsu if (!vnode) { 34188b91371SMasami Hiramatsu ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf); 34251887d03SMasami Hiramatsu if (ret < 0) 34351887d03SMasami Hiramatsu return ret; 34451887d03SMasami Hiramatsu buf += ret; 34551887d03SMasami Hiramatsu continue; 34688b91371SMasami Hiramatsu } 34751887d03SMasami Hiramatsu xbc_array_for_each_value(vnode, val) { 348212f863fSRasmus Villemoes /* 349212f863fSRasmus Villemoes * For prettier and more readable /proc/cmdline, only 350212f863fSRasmus Villemoes * quote the value when necessary, i.e. when it contains 351212f863fSRasmus Villemoes * whitespace. 352212f863fSRasmus Villemoes */ 353efee03a5SRasmus Villemoes q = strpbrk(val, " \t\r\n") ? "\"" : ""; 354efee03a5SRasmus Villemoes ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ", 355efee03a5SRasmus Villemoes xbc_namebuf, q, val, q); 35651887d03SMasami Hiramatsu if (ret < 0) 35751887d03SMasami Hiramatsu return ret; 35851887d03SMasami Hiramatsu buf += ret; 35951887d03SMasami Hiramatsu } 36051887d03SMasami Hiramatsu } 36151887d03SMasami Hiramatsu 36251887d03SMasami Hiramatsu return buf - (end - size); 36351887d03SMasami Hiramatsu } 36451887d03SMasami Hiramatsu #undef rest 36551887d03SMasami Hiramatsu 36651887d03SMasami Hiramatsu /* Make an extra command line under given key word */ 36751887d03SMasami Hiramatsu static char * __init xbc_make_cmdline(const char *key) 36851887d03SMasami Hiramatsu { 36951887d03SMasami Hiramatsu struct xbc_node *root; 37051887d03SMasami Hiramatsu char *new_cmdline; 37151887d03SMasami Hiramatsu int ret, len = 0; 37251887d03SMasami Hiramatsu 37351887d03SMasami Hiramatsu root = xbc_find_node(key); 37451887d03SMasami Hiramatsu if (!root) 37551887d03SMasami Hiramatsu return NULL; 37651887d03SMasami Hiramatsu 37751887d03SMasami Hiramatsu /* Count required buffer size */ 37851887d03SMasami Hiramatsu len = xbc_snprint_cmdline(NULL, 0, root); 37951887d03SMasami Hiramatsu if (len <= 0) 38051887d03SMasami Hiramatsu return NULL; 38151887d03SMasami Hiramatsu 38251887d03SMasami Hiramatsu new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES); 38351887d03SMasami Hiramatsu if (!new_cmdline) { 38451887d03SMasami Hiramatsu pr_err("Failed to allocate memory for extra kernel cmdline.\n"); 38551887d03SMasami Hiramatsu return NULL; 38651887d03SMasami Hiramatsu } 38751887d03SMasami Hiramatsu 38851887d03SMasami Hiramatsu ret = xbc_snprint_cmdline(new_cmdline, len + 1, root); 38951887d03SMasami Hiramatsu if (ret < 0 || ret > len) { 39051887d03SMasami Hiramatsu pr_err("Failed to print extra kernel cmdline.\n"); 3914421cca0SMike Rapoport memblock_free(new_cmdline, len + 1); 39251887d03SMasami Hiramatsu return NULL; 39351887d03SMasami Hiramatsu } 39451887d03SMasami Hiramatsu 39551887d03SMasami Hiramatsu return new_cmdline; 39651887d03SMasami Hiramatsu } 39751887d03SMasami Hiramatsu 398f61872bbSSteven Rostedt (VMware) static int __init bootconfig_params(char *param, char *val, 399f61872bbSSteven Rostedt (VMware) const char *unused, void *arg) 400f61872bbSSteven Rostedt (VMware) { 401f61872bbSSteven Rostedt (VMware) if (strcmp(param, "bootconfig") == 0) { 402f61872bbSSteven Rostedt (VMware) bootconfig_found = true; 403f61872bbSSteven Rostedt (VMware) } 404f61872bbSSteven Rostedt (VMware) return 0; 405f61872bbSSteven Rostedt (VMware) } 406f61872bbSSteven Rostedt (VMware) 407d0ac5fbaSMasami Hiramatsu static int __init warn_bootconfig(char *str) 408d0ac5fbaSMasami Hiramatsu { 409d0ac5fbaSMasami Hiramatsu /* The 'bootconfig' has been handled by bootconfig_params(). */ 410d0ac5fbaSMasami Hiramatsu return 0; 411d0ac5fbaSMasami Hiramatsu } 412d0ac5fbaSMasami Hiramatsu 4132b7d2fe7SCao jin static void __init setup_boot_config(void) 4147684b858SMasami Hiramatsu { 415f61872bbSSteven Rostedt (VMware) static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata; 416a2a9d67aSMasami Hiramatsu const char *msg, *data; 417a2a9d67aSMasami Hiramatsu int pos, ret; 418a2a9d67aSMasami Hiramatsu size_t size; 419a2a9d67aSMasami Hiramatsu char *err; 4207684b858SMasami Hiramatsu 421611d0a95SMasami Hiramatsu /* Cut out the bootconfig data even if we have no bootconfig option */ 422765b8552SMasami Hiramatsu data = get_boot_config_from_initrd(&size); 423a2a9d67aSMasami Hiramatsu /* If there is no bootconfig in initrd, try embedded one. */ 424a2a9d67aSMasami Hiramatsu if (!data) 425a2a9d67aSMasami Hiramatsu data = xbc_get_embedded_bootconfig(&size); 426de462e5fSMasami Hiramatsu 427a1d3a6d9SWolfram Sang strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE); 428477d0847SMasami Hiramatsu err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL, 429f61872bbSSteven Rostedt (VMware) bootconfig_params); 430f61872bbSSteven Rostedt (VMware) 431caa0708aSMasami Hiramatsu (Google) if (IS_ERR(err) || !(bootconfig_found || IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE))) 4327684b858SMasami Hiramatsu return; 4337684b858SMasami Hiramatsu 434b66fbbe8SMasami Hiramatsu /* parse_args() stops at the next param of '--' and returns an address */ 435477d0847SMasami Hiramatsu if (err) 436b66fbbe8SMasami Hiramatsu initargs_offs = err - tmp_cmdline; 437477d0847SMasami Hiramatsu 438611d0a95SMasami Hiramatsu if (!data) { 439caa0708aSMasami Hiramatsu (Google) /* If user intended to use bootconfig, show an error level message */ 440caa0708aSMasami Hiramatsu (Google) if (bootconfig_found) 441611d0a95SMasami Hiramatsu pr_err("'bootconfig' found on command line, but no bootconfig found\n"); 442caa0708aSMasami Hiramatsu (Google) else 443caa0708aSMasami Hiramatsu (Google) pr_info("No bootconfig data provided, so skipping bootconfig"); 444611d0a95SMasami Hiramatsu return; 445611d0a95SMasami Hiramatsu } 446611d0a95SMasami Hiramatsu 4477495e092SSteven Rostedt (VMware) if (size >= XBC_DATA_MAX) { 448a2a9d67aSMasami Hiramatsu pr_err("bootconfig size %ld greater than max size %d\n", 449a2a9d67aSMasami Hiramatsu (long)size, XBC_DATA_MAX); 4507684b858SMasami Hiramatsu return; 4517495e092SSteven Rostedt (VMware) } 4527684b858SMasami Hiramatsu 453bdac5c2bSMasami Hiramatsu ret = xbc_init(data, size, &msg, &pos); 45489b74cacSMasami Hiramatsu if (ret < 0) { 45589b74cacSMasami Hiramatsu if (pos < 0) 45689b74cacSMasami Hiramatsu pr_err("Failed to init bootconfig: %s.\n", msg); 45789b74cacSMasami Hiramatsu else 45889b74cacSMasami Hiramatsu pr_err("Failed to parse bootconfig: %s at %d.\n", 45989b74cacSMasami Hiramatsu msg, pos); 46089b74cacSMasami Hiramatsu } else { 461e306220cSMasami Hiramatsu xbc_get_info(&ret, NULL); 462a2a9d67aSMasami Hiramatsu pr_info("Load bootconfig: %ld bytes %d nodes\n", (long)size, ret); 46351887d03SMasami Hiramatsu /* keys starting with "kernel." are passed via cmdline */ 46451887d03SMasami Hiramatsu extra_command_line = xbc_make_cmdline("kernel"); 46513199162SMasami Hiramatsu /* Also, "init." keys are init arguments */ 46613199162SMasami Hiramatsu extra_init_args = xbc_make_cmdline("init"); 46751887d03SMasami Hiramatsu } 4687495e092SSteven Rostedt (VMware) return; 4697684b858SMasami Hiramatsu } 470de462e5fSMasami Hiramatsu 47140caa127SMasami Hiramatsu static void __init exit_boot_config(void) 47240caa127SMasami Hiramatsu { 473115d4d08SMasami Hiramatsu xbc_exit(); 47440caa127SMasami Hiramatsu } 47540caa127SMasami Hiramatsu 47640caa127SMasami Hiramatsu #else /* !CONFIG_BOOT_CONFIG */ 477de462e5fSMasami Hiramatsu 4782b7d2fe7SCao jin static void __init setup_boot_config(void) 479de462e5fSMasami Hiramatsu { 480de462e5fSMasami Hiramatsu /* Remove bootconfig data from initrd */ 481765b8552SMasami Hiramatsu get_boot_config_from_initrd(NULL); 482de462e5fSMasami Hiramatsu } 483d8a953ddSMasami Hiramatsu 484d8a953ddSMasami Hiramatsu static int __init warn_bootconfig(char *str) 485d8a953ddSMasami Hiramatsu { 48636c6aa26SShaokun Zhang pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOT_CONFIG is not set.\n"); 487d8a953ddSMasami Hiramatsu return 0; 488d8a953ddSMasami Hiramatsu } 48940caa127SMasami Hiramatsu 49040caa127SMasami Hiramatsu #define exit_boot_config() do {} while (0) 49140caa127SMasami Hiramatsu 49240caa127SMasami Hiramatsu #endif /* CONFIG_BOOT_CONFIG */ 49340caa127SMasami Hiramatsu 494d0ac5fbaSMasami Hiramatsu early_param("bootconfig", warn_bootconfig); 4957684b858SMasami Hiramatsu 496c722cea2SMasami Hiramatsu bool __init cmdline_has_extra_options(void) 497c722cea2SMasami Hiramatsu { 498c722cea2SMasami Hiramatsu return extra_command_line || extra_init_args; 499c722cea2SMasami Hiramatsu } 500c722cea2SMasami Hiramatsu 5011da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */ 5027e2762e1SArvind Sankar static void __init repair_env_string(char *param, char *val) 503a99cd112SChris Metcalf { 5041da177e4SLinus Torvalds if (val) { 5051da177e4SLinus Torvalds /* param=val or param="val"? */ 5061da177e4SLinus Torvalds if (val == param+strlen(param)+1) 5071da177e4SLinus Torvalds val[-1] = '='; 5081da177e4SLinus Torvalds else if (val == param+strlen(param)+2) { 5091da177e4SLinus Torvalds val[-2] = '='; 5101da177e4SLinus Torvalds memmove(val-1, val, strlen(val)+1); 5111da177e4SLinus Torvalds } else 5121da177e4SLinus Torvalds BUG(); 5131da177e4SLinus Torvalds } 514a99cd112SChris Metcalf } 515a99cd112SChris Metcalf 51651e158c1SRusty Russell /* Anything after -- gets handed straight to init. */ 517ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val, 518ecc86170SLuis R. Rodriguez const char *unused, void *arg) 51951e158c1SRusty Russell { 52051e158c1SRusty Russell unsigned int i; 52151e158c1SRusty Russell 52251e158c1SRusty Russell if (panic_later) 52351e158c1SRusty Russell return 0; 52451e158c1SRusty Russell 5257e2762e1SArvind Sankar repair_env_string(param, val); 52651e158c1SRusty Russell 52751e158c1SRusty Russell for (i = 0; argv_init[i]; i++) { 52851e158c1SRusty Russell if (i == MAX_INIT_ARGS) { 52951e158c1SRusty Russell panic_later = "init"; 53051e158c1SRusty Russell panic_param = param; 53151e158c1SRusty Russell return 0; 53251e158c1SRusty Russell } 53351e158c1SRusty Russell } 53451e158c1SRusty Russell argv_init[i] = param; 53551e158c1SRusty Russell return 0; 53651e158c1SRusty Russell } 53751e158c1SRusty Russell 538a99cd112SChris Metcalf /* 539a99cd112SChris Metcalf * Unknown boot options get handed to init, unless they look like 540a99cd112SChris Metcalf * unused parameters (modprobe will find them in /proc/cmdline). 541a99cd112SChris Metcalf */ 542ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val, 543ecc86170SLuis R. Rodriguez const char *unused, void *arg) 544a99cd112SChris Metcalf { 545283900e8SArvind Sankar size_t len = strlen(param); 546283900e8SArvind Sankar 5478001f493SKrister Johansen /* Handle params aliased to sysctls */ 5488001f493SKrister Johansen if (sysctl_is_alias(param)) 5498001f493SKrister Johansen return 0; 5508001f493SKrister Johansen 5517e2762e1SArvind Sankar repair_env_string(param, val); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds /* Handle obsolete-style parameters */ 5541da177e4SLinus Torvalds if (obsolete_checksetup(param)) 5551da177e4SLinus Torvalds return 0; 5561da177e4SLinus Torvalds 557f066a4f6SRusty Russell /* Unused module parameter. */ 558283900e8SArvind Sankar if (strnchr(param, len, '.')) 5591da177e4SLinus Torvalds return 0; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if (panic_later) 5621da177e4SLinus Torvalds return 0; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds if (val) { 5651da177e4SLinus Torvalds /* Environment option */ 5661da177e4SLinus Torvalds unsigned int i; 5671da177e4SLinus Torvalds for (i = 0; envp_init[i]; i++) { 5681da177e4SLinus Torvalds if (i == MAX_INIT_ENVS) { 569499a4584STetsuo Handa panic_later = "env"; 5701da177e4SLinus Torvalds panic_param = param; 5711da177e4SLinus Torvalds } 572283900e8SArvind Sankar if (!strncmp(param, envp_init[i], len+1)) 5731da177e4SLinus Torvalds break; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds envp_init[i] = param; 5761da177e4SLinus Torvalds } else { 5771da177e4SLinus Torvalds /* Command line option */ 5781da177e4SLinus Torvalds unsigned int i; 5791da177e4SLinus Torvalds for (i = 0; argv_init[i]; i++) { 5801da177e4SLinus Torvalds if (i == MAX_INIT_ARGS) { 581499a4584STetsuo Handa panic_later = "init"; 5821da177e4SLinus Torvalds panic_param = param; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds argv_init[i] = param; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds return 0; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds static int __init init_setup(char *str) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds unsigned int i; 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds execute_command = str; 5951da177e4SLinus Torvalds /* 5961da177e4SLinus Torvalds * In case LILO is going to boot us with default command line, 5971da177e4SLinus Torvalds * it prepends "auto" before the whole cmdline which makes 5981da177e4SLinus Torvalds * the shell think it should execute a script with such name. 5991da177e4SLinus Torvalds * So we ignore all arguments entered _before_ init=... [MJ] 6001da177e4SLinus Torvalds */ 6011da177e4SLinus Torvalds for (i = 1; i < MAX_INIT_ARGS; i++) 6021da177e4SLinus Torvalds argv_init[i] = NULL; 6031da177e4SLinus Torvalds return 1; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds __setup("init=", init_setup); 6061da177e4SLinus Torvalds 607ffdfc409SOlof Johansson static int __init rdinit_setup(char *str) 608ffdfc409SOlof Johansson { 609ffdfc409SOlof Johansson unsigned int i; 610ffdfc409SOlof Johansson 611ffdfc409SOlof Johansson ramdisk_execute_command = str; 612ffdfc409SOlof Johansson /* See "auto" comment in init_setup */ 613ffdfc409SOlof Johansson for (i = 1; i < MAX_INIT_ARGS; i++) 614ffdfc409SOlof Johansson argv_init[i] = NULL; 615ffdfc409SOlof Johansson return 1; 616ffdfc409SOlof Johansson } 617ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup); 618ffdfc409SOlof Johansson 6191da177e4SLinus Torvalds #ifndef CONFIG_SMP 620e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { } 6211da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { } 6221da177e4SLinus Torvalds #endif 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* 62530d7e0d4SAlon Bar-Lev * We need to store the untouched command line for future reference. 62630d7e0d4SAlon Bar-Lev * We also need to store the touched command line since the parameter 62730d7e0d4SAlon Bar-Lev * parsing is performed in place, and we should allow a component to 62830d7e0d4SAlon Bar-Lev * store reference of name/value for future reference. 62930d7e0d4SAlon Bar-Lev */ 63030d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line) 63130d7e0d4SAlon Bar-Lev { 63213199162SMasami Hiramatsu size_t len, xlen = 0, ilen = 0; 633f5c7310aSMike Rapoport 63451887d03SMasami Hiramatsu if (extra_command_line) 63551887d03SMasami Hiramatsu xlen = strlen(extra_command_line); 636cd24bdb0SYuntao Wang if (extra_init_args) { 637cd24bdb0SYuntao Wang extra_init_args = strim(extra_init_args); /* remove trailing space */ 63813199162SMasami Hiramatsu ilen = strlen(extra_init_args) + 4; /* for " -- " */ 639cd24bdb0SYuntao Wang } 64051887d03SMasami Hiramatsu 641ddd53363SYuntao Wang len = xlen + strlen(boot_command_line) + ilen + 1; 642f5c7310aSMike Rapoport 643ddd53363SYuntao Wang saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES); 644f5c7310aSMike Rapoport if (!saved_command_line) 645ddd53363SYuntao Wang panic("%s: Failed to allocate %zu bytes\n", __func__, len); 646f5c7310aSMike Rapoport 64746dad3c1SYuntao Wang len = xlen + strlen(command_line) + 1; 64846dad3c1SYuntao Wang 649f5c7310aSMike Rapoport static_command_line = memblock_alloc(len, SMP_CACHE_BYTES); 650f5c7310aSMike Rapoport if (!static_command_line) 651f5c7310aSMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, len); 652f5c7310aSMike Rapoport 65351887d03SMasami Hiramatsu if (xlen) { 65451887d03SMasami Hiramatsu /* 65551887d03SMasami Hiramatsu * We have to put extra_command_line before boot command 65651887d03SMasami Hiramatsu * lines because there could be dashes (separator of init 65751887d03SMasami Hiramatsu * command line) in the command lines. 65851887d03SMasami Hiramatsu */ 65951887d03SMasami Hiramatsu strcpy(saved_command_line, extra_command_line); 66051887d03SMasami Hiramatsu strcpy(static_command_line, extra_command_line); 66151887d03SMasami Hiramatsu } 66251887d03SMasami Hiramatsu strcpy(saved_command_line + xlen, boot_command_line); 66351887d03SMasami Hiramatsu strcpy(static_command_line + xlen, command_line); 66413199162SMasami Hiramatsu 66513199162SMasami Hiramatsu if (ilen) { 66613199162SMasami Hiramatsu /* 66713199162SMasami Hiramatsu * Append supplemental init boot args to saved_command_line 66813199162SMasami Hiramatsu * so that user can check what command line options passed 66913199162SMasami Hiramatsu * to init. 670b66fbbe8SMasami Hiramatsu * The order should always be 671b66fbbe8SMasami Hiramatsu * " -- "[bootconfig init-param][cmdline init-param] 67213199162SMasami Hiramatsu */ 673b66fbbe8SMasami Hiramatsu if (initargs_offs) { 674b66fbbe8SMasami Hiramatsu len = xlen + initargs_offs; 675b66fbbe8SMasami Hiramatsu strcpy(saved_command_line + len, extra_init_args); 676b66fbbe8SMasami Hiramatsu len += ilen - 4; /* strlen(extra_init_args) */ 677b66fbbe8SMasami Hiramatsu strcpy(saved_command_line + len, 678b66fbbe8SMasami Hiramatsu boot_command_line + initargs_offs - 1); 679f61872bbSSteven Rostedt (VMware) } else { 680b66fbbe8SMasami Hiramatsu len = strlen(saved_command_line); 68113199162SMasami Hiramatsu strcpy(saved_command_line + len, " -- "); 68213199162SMasami Hiramatsu len += 4; 68313199162SMasami Hiramatsu strcpy(saved_command_line + len, extra_init_args); 68413199162SMasami Hiramatsu } 68530d7e0d4SAlon Bar-Lev } 686941baf6fSAlexey Dobriyan 687941baf6fSAlexey Dobriyan saved_command_line_len = strlen(saved_command_line); 688b66fbbe8SMasami Hiramatsu } 68930d7e0d4SAlon Bar-Lev 69030d7e0d4SAlon Bar-Lev /* 6911da177e4SLinus Torvalds * We need to finalize in a non-__init function or else race conditions 6921da177e4SLinus Torvalds * between the root thread and the init thread may cause start_kernel to 6931da177e4SLinus Torvalds * be reaped by free_initmem before the root thread has proceeded to 6941da177e4SLinus Torvalds * cpu_idle. 6951da177e4SLinus Torvalds * 6961da177e4SLinus Torvalds * gcc-3.4 accidentally inlines this function, so use noinline. 6971da177e4SLinus Torvalds */ 6981da177e4SLinus Torvalds 699b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done); 700b433c3d4SPeter Zijlstra 701ac4db926SGeert Uytterhoeven static noinline void __ref __noreturn rest_init(void) 7021da177e4SLinus Torvalds { 7038fb12156SThomas Gleixner struct task_struct *tsk; 70473c27992SEric W. Biederman int pid; 70573c27992SEric W. Biederman 7067db905e6SPaul E. McKenney rcu_scheduler_starting(); 707b433c3d4SPeter Zijlstra /* 70897158569SPeter Zijlstra * We need to spawn init first so that it obtains pid 1, however 709b433c3d4SPeter Zijlstra * the init task will end up wanting to create kthreads, which, if 710b433c3d4SPeter Zijlstra * we schedule it before we create kthreadd, will OOPS. 711b433c3d4SPeter Zijlstra */ 712343f4c49SEric W. Biederman pid = user_mode_thread(kernel_init, NULL, CLONE_FS); 7138fb12156SThomas Gleixner /* 7148fb12156SThomas Gleixner * Pin init on the boot CPU. Task migration is not properly working 7158fb12156SThomas Gleixner * until sched_init_smp() has been run. It will set the allowed 7168fb12156SThomas Gleixner * CPUs for init to the non isolated CPUs. 7178fb12156SThomas Gleixner */ 7188fb12156SThomas Gleixner rcu_read_lock(); 7198fb12156SThomas Gleixner tsk = find_task_by_pid_ns(pid, &init_pid_ns); 72015faafc6SPeter Zijlstra tsk->flags |= PF_NO_SETAFFINITY; 7218fb12156SThomas Gleixner set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id())); 7228fb12156SThomas Gleixner rcu_read_unlock(); 7238fb12156SThomas Gleixner 7241da177e4SLinus Torvalds numa_default_policy(); 725cf587db2SMike Christie pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES); 726d11c563dSPaul E. McKenney rcu_read_lock(); 7275cd20455SPavel Emelyanov kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); 728d11c563dSPaul E. McKenney rcu_read_unlock(); 7291c3c5eabSThomas Gleixner 7301c3c5eabSThomas Gleixner /* 7311c3c5eabSThomas Gleixner * Enable might_sleep() and smp_processor_id() checks. 732c1a280b6SThomas Gleixner * They cannot be enabled earlier because with CONFIG_PREEMPTION=y 7331c3c5eabSThomas Gleixner * kernel_thread() would trigger might_sleep() splats. With 7341c3c5eabSThomas Gleixner * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled 7351c3c5eabSThomas Gleixner * already, but it's stuck on the kthreadd_done completion. 7361c3c5eabSThomas Gleixner */ 7371c3c5eabSThomas Gleixner system_state = SYSTEM_SCHEDULING; 7381c3c5eabSThomas Gleixner 739b433c3d4SPeter Zijlstra complete(&kthreadd_done); 740f340c0d1SIngo Molnar 741f340c0d1SIngo Molnar /* 742f340c0d1SIngo Molnar * The boot idle thread must execute schedule() 7431df21055SIngo Molnar * at least once to get things moving: 744f340c0d1SIngo Molnar */ 745bd2f5536SThomas Gleixner schedule_preempt_disabled(); 7465bfb5d69SNick Piggin /* Call into cpu_idle with preempt disabled */ 747a1a04ec3SThomas Gleixner cpu_startup_entry(CPUHP_ONLINE); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds /* Check for early params. */ 751ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val, 752ecc86170SLuis R. Rodriguez const char *unused, void *arg) 7531da177e4SLinus Torvalds { 754914dcaa8SRusty Russell const struct obs_kernel_param *p; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds for (p = __setup_start; p < __setup_end; p++) { 757b1e4d20cSMichal Schmidt if ((p->early && parameq(param, p->str)) || 75818a8bd94SYinghai Lu (strcmp(param, "console") == 0 && 75918a8bd94SYinghai Lu strcmp(p->str, "earlycon") == 0) 76018a8bd94SYinghai Lu ) { 7611da177e4SLinus Torvalds if (p->setup_func(val) != 0) 762ea676e84SAndrew Morton pr_warn("Malformed early option '%s'\n", param); 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds /* We accept everything at this stage. */ 7661da177e4SLinus Torvalds return 0; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 76913977091SMagnus Damm void __init parse_early_options(char *cmdline) 77013977091SMagnus Damm { 771ecc86170SLuis R. Rodriguez parse_args("early options", cmdline, NULL, 0, 0, 0, NULL, 772ecc86170SLuis R. Rodriguez do_early_param); 77313977091SMagnus Damm } 77413977091SMagnus Damm 7751da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */ 7761da177e4SLinus Torvalds void __init parse_early_param(void) 7771da177e4SLinus Torvalds { 778dd4d9fecSFabian Frederick static int done __initdata; 779dd4d9fecSFabian Frederick static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds if (done) 7821da177e4SLinus Torvalds return; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* All fall through to do_early_param. */ 785a1d3a6d9SWolfram Sang strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE); 78613977091SMagnus Damm parse_early_options(tmp_cmdline); 7871da177e4SLinus Torvalds done = 1; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 790e7ff3a47SThomas Gleixner void __init __weak arch_post_acpi_subsys_init(void) { } 791e7ff3a47SThomas Gleixner 792839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void) 793033ab7f8SAndrew Morton { 794033ab7f8SAndrew Morton } 795033ab7f8SAndrew Morton 79671261072SThomas Gleixner void __init __weak smp_prepare_boot_cpu(void) 79771261072SThomas Gleixner { 79871261072SThomas Gleixner } 79971261072SThomas Gleixner 800eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE 801b235beeaSLinus Torvalds void __init __weak thread_stack_cache_init(void) 8028c9843e5SBenjamin Herrenschmidt { 8038c9843e5SBenjamin Herrenschmidt } 804eded09ccSDavid Howells #endif 8058c9843e5SBenjamin Herrenschmidt 8064fc19708SNadav Amit void __init __weak poking_init(void) { } 8074fc19708SNadav Amit 808782de70cSMike Rapoport void __init __weak pgtable_cache_init(void) { } 809caa84136SNadav Amit 8108b097881SKefeng Wang void __init __weak trap_init(void) { } 8118b097881SKefeng Wang 8124e37958dSSteven Rostedt (VMware) bool initcall_debug; 8134e37958dSSteven Rostedt (VMware) core_param(initcall_debug, initcall_debug, bool, 0644); 814b0dc52f1SSteven Rostedt (VMware) 815b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED 8164e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void); 817b0dc52f1SSteven Rostedt (VMware) #else 818b0dc52f1SSteven Rostedt (VMware) static inline void initcall_debug_enable(void) 819b0dc52f1SSteven Rostedt (VMware) { 820b0dc52f1SSteven Rostedt (VMware) } 821b0dc52f1SSteven Rostedt (VMware) #endif 8224e37958dSSteven Rostedt (VMware) 8238cb37a59SMarco Elver #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET 82439218ff4SKees Cook DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, 82539218ff4SKees Cook randomize_kstack_offset); 82639218ff4SKees Cook DEFINE_PER_CPU(u32, kstack_offset); 82739218ff4SKees Cook 82839218ff4SKees Cook static int __init early_randomize_kstack_offset(char *buf) 82939218ff4SKees Cook { 83039218ff4SKees Cook int ret; 83139218ff4SKees Cook bool bool_result; 83239218ff4SKees Cook 83339218ff4SKees Cook ret = kstrtobool(buf, &bool_result); 83439218ff4SKees Cook if (ret) 83539218ff4SKees Cook return ret; 83639218ff4SKees Cook 83739218ff4SKees Cook if (bool_result) 83839218ff4SKees Cook static_branch_enable(&randomize_kstack_offset); 83939218ff4SKees Cook else 84039218ff4SKees Cook static_branch_disable(&randomize_kstack_offset); 84139218ff4SKees Cook return 0; 84239218ff4SKees Cook } 84339218ff4SKees Cook early_param("randomize_kstack_offset", early_randomize_kstack_offset); 84439218ff4SKees Cook #endif 84539218ff4SKees Cook 84686d1919aSAndrew Halaney static void __init print_unknown_bootoptions(void) 84786d1919aSAndrew Halaney { 84886d1919aSAndrew Halaney char *unknown_options; 84986d1919aSAndrew Halaney char *end; 85086d1919aSAndrew Halaney const char *const *p; 85186d1919aSAndrew Halaney size_t len; 85286d1919aSAndrew Halaney 85386d1919aSAndrew Halaney if (panic_later || (!argv_init[1] && !envp_init[2])) 85486d1919aSAndrew Halaney return; 85586d1919aSAndrew Halaney 85686d1919aSAndrew Halaney /* 85786d1919aSAndrew Halaney * Determine how many options we have to print out, plus a space 85886d1919aSAndrew Halaney * before each 85986d1919aSAndrew Halaney */ 86086d1919aSAndrew Halaney len = 1; /* null terminator */ 86186d1919aSAndrew Halaney for (p = &argv_init[1]; *p; p++) { 86286d1919aSAndrew Halaney len++; 86386d1919aSAndrew Halaney len += strlen(*p); 86486d1919aSAndrew Halaney } 86586d1919aSAndrew Halaney for (p = &envp_init[2]; *p; p++) { 86686d1919aSAndrew Halaney len++; 86786d1919aSAndrew Halaney len += strlen(*p); 86886d1919aSAndrew Halaney } 86986d1919aSAndrew Halaney 87086d1919aSAndrew Halaney unknown_options = memblock_alloc(len, SMP_CACHE_BYTES); 87186d1919aSAndrew Halaney if (!unknown_options) { 87286d1919aSAndrew Halaney pr_err("%s: Failed to allocate %zu bytes\n", 87386d1919aSAndrew Halaney __func__, len); 87486d1919aSAndrew Halaney return; 87586d1919aSAndrew Halaney } 87686d1919aSAndrew Halaney end = unknown_options; 87786d1919aSAndrew Halaney 87886d1919aSAndrew Halaney for (p = &argv_init[1]; *p; p++) 87986d1919aSAndrew Halaney end += sprintf(end, " %s", *p); 88086d1919aSAndrew Halaney for (p = &envp_init[2]; *p; p++) 88186d1919aSAndrew Halaney end += sprintf(end, " %s", *p); 88286d1919aSAndrew Halaney 8838bc2b3dcSAndrew Halaney /* Start at unknown_options[1] to skip the initial space */ 8848bc2b3dcSAndrew Halaney pr_notice("Unknown kernel command line parameters \"%s\", will be passed to user space.\n", 8858bc2b3dcSAndrew Halaney &unknown_options[1]); 8864421cca0SMike Rapoport memblock_free(unknown_options, len); 88786d1919aSAndrew Halaney } 88886d1919aSAndrew Halaney 889f9899c02SHuang Shijie static void __init early_numa_node_init(void) 890f9899c02SHuang Shijie { 891f9899c02SHuang Shijie #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID 892f9899c02SHuang Shijie #ifndef cpu_to_node 893f9899c02SHuang Shijie int cpu; 894f9899c02SHuang Shijie 895f9899c02SHuang Shijie /* The early_cpu_to_node() should be ready here. */ 896f9899c02SHuang Shijie for_each_possible_cpu(cpu) 897f9899c02SHuang Shijie set_cpu_numa_node(cpu, early_cpu_to_node(cpu)); 898f9899c02SHuang Shijie #endif 899f9899c02SHuang Shijie #endif 900f9899c02SHuang Shijie } 901f9899c02SHuang Shijie 902514ca14eS[email protected] asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector 903514ca14eS[email protected] void start_kernel(void) 9041da177e4SLinus Torvalds { 905dd4d9fecSFabian Frederick char *command_line; 906dd4d9fecSFabian Frederick char *after_dashes; 907033ab7f8SAndrew Morton 908d4311ff1SAaron Tomlin set_task_stack_end_magic(&init_task); 90973839c5bSMing Lei smp_setup_processor_id(); 9103ac7fe5aSThomas Gleixner debug_objects_early_init(); 91183cc6fa0SStephen Boyd init_vmlinux_build_id(); 91242059429SIngo Molnar 913ddbcc7e8SPaul Menage cgroup_init_early(); 914fbb9ce95SIngo Molnar 915fbb9ce95SIngo Molnar local_irq_disable(); 9162ce802f6STejun Heo early_boot_irqs_disabled = true; 917fbb9ce95SIngo Molnar 918fbb9ce95SIngo Molnar /* 9191da177e4SLinus Torvalds * Interrupts are still disabled. Do necessary setups, then 9201b3b3b49SViresh Kumar * enable them. 9211da177e4SLinus Torvalds */ 92244fd2299SStas Sergeev boot_cpu_init(); 9231da177e4SLinus Torvalds page_address_init(); 924ea676e84SAndrew Morton pr_notice("%s", linux_banner); 9251da177e4SLinus Torvalds setup_arch(&command_line); 926*77b644c3SKP Singh /* Static keys and static calls are needed by LSMs */ 927*77b644c3SKP Singh jump_label_init(); 928*77b644c3SKP Singh static_call_init(); 929*77b644c3SKP Singh early_security_init(); 9302b7d2fe7SCao jin setup_boot_config(); 93130d7e0d4SAlon Bar-Lev setup_command_line(command_line); 932e0982e90SMike Travis setup_nr_cpu_ids(); 933d6647bdfSTejun Heo setup_per_cpu_areas(); 93444fd2299SStas Sergeev smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */ 935f9899c02SHuang Shijie early_numa_node_init(); 936b5b1404dSLinus Torvalds boot_cpu_hotplug_init(); 9371da177e4SLinus Torvalds 93851887d03SMasami Hiramatsu pr_notice("Kernel command line: %s\n", saved_command_line); 9396041186aSDan Williams /* parameters may set static keys */ 94083b519e8SPekka Enberg parse_early_param(); 94151e158c1SRusty Russell after_dashes = parse_args("Booting kernel", 94251e158c1SRusty Russell static_command_line, __start___param, 94383b519e8SPekka Enberg __stop___param - __start___param, 944ecc86170SLuis R. Rodriguez -1, -1, NULL, &unknown_bootoption); 94586d1919aSAndrew Halaney print_unknown_bootoptions(); 9463438cf54SDaniel Thompson if (!IS_ERR_OR_NULL(after_dashes)) 94751e158c1SRusty Russell parse_args("Setting init args", after_dashes, NULL, 0, -1, -1, 948ecc86170SLuis R. Rodriguez NULL, set_init_arg); 94913199162SMasami Hiramatsu if (extra_init_args) 95013199162SMasami Hiramatsu parse_args("Setting extra init args", extra_init_args, 95113199162SMasami Hiramatsu NULL, 0, -1, -1, NULL, set_init_arg); 95297ce2c88SJeremy Fitzhardinge 953f6238499SJason A. Donenfeld /* Architectural and non-timekeeping rng init, before allocator init */ 954f6238499SJason A. Donenfeld random_init_early(command_line); 955f6238499SJason A. Donenfeld 95683b519e8SPekka Enberg /* 95783b519e8SPekka Enberg * These use large bootmem allocations and must precede 958b7ec1bf3SMike Rapoport (IBM) * initalization of page allocator 95983b519e8SPekka Enberg */ 960162a7e75SMike Travis setup_log_buf(0); 96183b519e8SPekka Enberg vfs_caches_init_early(); 96283b519e8SPekka Enberg sort_main_extable(); 96383b519e8SPekka Enberg trap_init(); 964b7ec1bf3SMike Rapoport (IBM) mm_core_init(); 9655b93a836SPeter Zijlstra poking_init(); 966f631718dSSteven Rostedt (VMware) ftrace_init(); 967f631718dSSteven Rostedt (VMware) 968e725c731SSteven Rostedt (VMware) /* trace_printk can be enabled here */ 969e725c731SSteven Rostedt (VMware) early_trace_init(); 970e725c731SSteven Rostedt (VMware) 9711da177e4SLinus Torvalds /* 9721da177e4SLinus Torvalds * Set up the scheduler prior starting any interrupts (such as the 9731da177e4SLinus Torvalds * timer interrupt). Full topology setup happens at smp_init() 9741da177e4SLinus Torvalds * time - but meanwhile we still have a functioning scheduler. 9751da177e4SLinus Torvalds */ 9761da177e4SLinus Torvalds sched_init(); 977f1a0a376SValentin Schneider 978dd4d9fecSFabian Frederick if (WARN(!irqs_disabled(), 979dd4d9fecSFabian Frederick "Interrupts were enabled *very* early, fixing it\n")) 980c4a68306SArd van Breemen local_irq_disable(); 9810a835c4fSMatthew Wilcox radix_tree_init(); 98254a611b6SLiam R. Howlett maple_tree_init(); 9833347fa09STejun Heo 9843347fa09STejun Heo /* 9857d229c66STal Shorer * Set up housekeeping before setting up workqueues to allow the unbound 9867d229c66STal Shorer * workqueue to take non-housekeeping into account. 9877d229c66STal Shorer */ 9887d229c66STal Shorer housekeeping_init(); 9897d229c66STal Shorer 9907d229c66STal Shorer /* 9913347fa09STejun Heo * Allow workqueue creation and work item queueing/cancelling 9923347fa09STejun Heo * early. Work item execution depends on kthreads and starts after 9933347fa09STejun Heo * workqueue_init(). 9943347fa09STejun Heo */ 9953347fa09STejun Heo workqueue_init_early(); 9963347fa09STejun Heo 9971da177e4SLinus Torvalds rcu_init(); 9985f893b26SSteven Rostedt (Red Hat) 999e725c731SSteven Rostedt (VMware) /* Trace events are available after this */ 10005f893b26SSteven Rostedt (Red Hat) trace_init(); 10015f893b26SSteven Rostedt (Red Hat) 10024e37958dSSteven Rostedt (VMware) if (initcall_debug) 10034e37958dSSteven Rostedt (VMware) initcall_debug_enable(); 10044e37958dSSteven Rostedt (VMware) 100565f382fdSFrederic Weisbecker context_tracking_init(); 10060b8f1efaSYinghai Lu /* init some links before init_ISA_irqs() */ 10070b8f1efaSYinghai Lu early_irq_init(); 10081da177e4SLinus Torvalds init_IRQ(); 1009ad2b1353SThomas Gleixner tick_init(); 1010d6dd50e0SLinus Torvalds rcu_init_nohz(); 10111da177e4SLinus Torvalds init_timers(); 10128e9c01c7SFrederic Weisbecker srcu_init(); 1013c0a31329SThomas Gleixner hrtimers_init(); 10141da177e4SLinus Torvalds softirq_init(); 1015ad596171Sjohn stultz timekeeping_init(); 1016fe222a6cSJason A. Donenfeld time_init(); 1017d5553523SKees Cook 1018f6238499SJason A. Donenfeld /* This must be after timekeeping is initialized */ 1019f6238499SJason A. Donenfeld random_init(); 1020f6238499SJason A. Donenfeld 1021f6238499SJason A. Donenfeld /* These make use of the fully initialized rng */ 1022f6238499SJason A. Donenfeld kfence_init(); 1023d5553523SKees Cook boot_init_stack_canary(); 1024d5553523SKees Cook 10259e630205SStephane Eranian perf_event_init(); 102693e02814SHeiko Carstens profile_init(); 1027d8ad7d11STakao Indoh call_function_init(); 1028f91eb62fSSteven Rostedt WARN(!irqs_disabled(), "Interrupts were enabled early\n"); 1029c3bc8fd6SJoel Fernandes (Google) 10302ce802f6STejun Heo early_boot_irqs_disabled = false; 103193e02814SHeiko Carstens local_irq_enable(); 1032dcce284aSBenjamin Herrenschmidt 10337e85ee0cSPekka Enberg kmem_cache_init_late(); 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* 10361da177e4SLinus Torvalds * HACK ALERT! This is early. We're enabling the console before 10371da177e4SLinus Torvalds * we've done PCI setups etc, and console_init() must be aware of 10381da177e4SLinus Torvalds * this. But we do want output early, in case something goes wrong. 10391da177e4SLinus Torvalds */ 10401da177e4SLinus Torvalds console_init(); 10411da177e4SLinus Torvalds if (panic_later) 1042499a4584STetsuo Handa panic("Too many boot %s vars at `%s'", panic_later, 1043499a4584STetsuo Handa panic_param); 1044fbb9ce95SIngo Molnar 1045c3bc8fd6SJoel Fernandes (Google) lockdep_init(); 1046fbb9ce95SIngo Molnar 10479a11b49aSIngo Molnar /* 10489a11b49aSIngo Molnar * Need to run this when irqs are enabled, because it wants 10499a11b49aSIngo Molnar * to self-test [hard/soft]-irqs on/off lock inversion bugs 10509a11b49aSIngo Molnar * too: 10519a11b49aSIngo Molnar */ 10529a11b49aSIngo Molnar locking_selftest(); 10539a11b49aSIngo Molnar 10541da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 10551da177e4SLinus Torvalds if (initrd_start && !initrd_below_start_ok && 1056bd673c7cSGeert Uytterhoeven page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) { 1057ea676e84SAndrew Morton pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n", 1058bd673c7cSGeert Uytterhoeven page_to_pfn(virt_to_page((void *)initrd_start)), 1059bd673c7cSGeert Uytterhoeven min_low_pfn); 10601da177e4SLinus Torvalds initrd_start = 0; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds #endif 1063e7c8d5c9SChristoph Lameter setup_per_cpu_pageset(); 10641da177e4SLinus Torvalds numa_policy_init(); 10659c71206dSDou Liyang acpi_early_init(); 10661da177e4SLinus Torvalds if (late_time_init) 10671da177e4SLinus Torvalds late_time_init(); 1068857baa87SPavel Tatashin sched_clock_init(); 10691da177e4SLinus Torvalds calibrate_delay(); 1070e45e761bSMichael Kelley 10719df9d2f0SThomas Gleixner arch_cpu_finalize_init(); 10729df9d2f0SThomas Gleixner 107395846ecfSGargi Sharma pid_idr_init(); 10741da177e4SLinus Torvalds anon_vma_init(); 107511520e5eSLinus Torvalds #ifdef CONFIG_X86 107683e68189SMatt Fleming if (efi_enabled(EFI_RUNTIME_SERVICES)) 107711520e5eSLinus Torvalds efi_enter_virtual_mode(); 107811520e5eSLinus Torvalds #endif 1079b235beeaSLinus Torvalds thread_stack_cache_init(); 1080d84f4f99SDavid Howells cred_init(); 1081ff691f6eSHeinrich Schuchardt fork_init(); 10821da177e4SLinus Torvalds proc_caches_init(); 10833ea056c5SAlexey Dobriyan uts_ns_init(); 10841da177e4SLinus Torvalds key_init(); 10851da177e4SLinus Torvalds security_init(); 10860b4b3827SJason Wessel dbg_late_init(); 10879c1be193SEric Dumazet net_ns_init(); 10884248b0daSMel Gorman vfs_caches_init(); 108962906027SNicholas Piggin pagecache_init(); 10901da177e4SLinus Torvalds signals_init(); 109109652320SAlexey Dobriyan seq_file_init(); 10921da177e4SLinus Torvalds proc_root_init(); 1093e149ed2bSAl Viro nsfs_init(); 1094cb12fd8eSChristian Brauner pidfs_init(); 10951da177e4SLinus Torvalds cpuset_init(); 1096695df213SZefan Li cgroup_init(); 1097c757249aSShailabh Nagar taskstats_init_early(); 1098ca74e92bSShailabh Nagar delayacct_init(); 10991da177e4SLinus Torvalds 1100b064a8faSRafael J. Wysocki acpi_subsystem_init(); 1101e7ff3a47SThomas Gleixner arch_post_acpi_subsys_init(); 1102dfd402a4SMarco Elver kcsan_init(); 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds /* Do the rest non-__init'ed, we're now alive */ 1105ac4db926SGeert Uytterhoeven rest_init(); 1106a9a3ed1eSBorislav Petkov 1107dc1d0553S[email protected] /* 1108dc1d0553S[email protected] * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10 1109dc1d0553S[email protected] * and older. 1110dc1d0553S[email protected] */ 1111dc1d0553S[email protected] #if !__has_attribute(__no_stack_protector__) 1112a9a3ed1eSBorislav Petkov prevent_tail_call_optimization(); 1113dc1d0553S[email protected] #endif 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 1116b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */ 1117b99b87f7SPeter Oberparleiter static void __init do_ctors(void) 1118b99b87f7SPeter Oberparleiter { 111955b6f763SJohannes Berg /* 112055b6f763SJohannes Berg * For UML, the constructors have already been called by the 112155b6f763SJohannes Berg * normal setup code as it's just a normal ELF binary, so we 112255b6f763SJohannes Berg * cannot do it again - but we do need CONFIG_CONSTRUCTORS 112355b6f763SJohannes Berg * even on UML for modules. 112455b6f763SJohannes Berg */ 112555b6f763SJohannes Berg #if defined(CONFIG_CONSTRUCTORS) && !defined(CONFIG_UML) 1126196a15b4SH Hartley Sweeten ctor_fn_t *fn = (ctor_fn_t *) __ctors_start; 1127b99b87f7SPeter Oberparleiter 1128196a15b4SH Hartley Sweeten for (; fn < (ctor_fn_t *) __ctors_end; fn++) 1129196a15b4SH Hartley Sweeten (*fn)(); 1130b99b87f7SPeter Oberparleiter #endif 1131b99b87f7SPeter Oberparleiter } 1132b99b87f7SPeter Oberparleiter 11337b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS 11347b0b73d7SPrarit Bhargava struct blacklist_entry { 11357b0b73d7SPrarit Bhargava struct list_head next; 11367b0b73d7SPrarit Bhargava char *buf; 11377b0b73d7SPrarit Bhargava }; 11387b0b73d7SPrarit Bhargava 11397b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls); 11407b0b73d7SPrarit Bhargava 11417b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str) 11427b0b73d7SPrarit Bhargava { 11437b0b73d7SPrarit Bhargava char *str_entry; 11447b0b73d7SPrarit Bhargava struct blacklist_entry *entry; 11457b0b73d7SPrarit Bhargava 11467b0b73d7SPrarit Bhargava /* str argument is a comma-separated list of functions */ 11477b0b73d7SPrarit Bhargava do { 11487b0b73d7SPrarit Bhargava str_entry = strsep(&str, ","); 11497b0b73d7SPrarit Bhargava if (str_entry) { 11507b0b73d7SPrarit Bhargava pr_debug("blacklisting initcall %s\n", str_entry); 11517e1c4e27SMike Rapoport entry = memblock_alloc(sizeof(*entry), 11527e1c4e27SMike Rapoport SMP_CACHE_BYTES); 1153f5c7310aSMike Rapoport if (!entry) 1154f5c7310aSMike Rapoport panic("%s: Failed to allocate %zu bytes\n", 1155f5c7310aSMike Rapoport __func__, sizeof(*entry)); 11567e1c4e27SMike Rapoport entry->buf = memblock_alloc(strlen(str_entry) + 1, 11577e1c4e27SMike Rapoport SMP_CACHE_BYTES); 1158f5c7310aSMike Rapoport if (!entry->buf) 1159f5c7310aSMike Rapoport panic("%s: Failed to allocate %zu bytes\n", 1160f5c7310aSMike Rapoport __func__, strlen(str_entry) + 1); 11617b0b73d7SPrarit Bhargava strcpy(entry->buf, str_entry); 11627b0b73d7SPrarit Bhargava list_add(&entry->next, &blacklisted_initcalls); 11637b0b73d7SPrarit Bhargava } 11647b0b73d7SPrarit Bhargava } while (str_entry); 11657b0b73d7SPrarit Bhargava 1166f9a40b08SRandy Dunlap return 1; 11677b0b73d7SPrarit Bhargava } 11687b0b73d7SPrarit Bhargava 11697b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn) 11707b0b73d7SPrarit Bhargava { 11717b0b73d7SPrarit Bhargava struct blacklist_entry *entry; 1172c8cdd2beSRasmus Villemoes char fn_name[KSYM_SYMBOL_LEN]; 11730fd5ed8dSRasmus Villemoes unsigned long addr; 11747b0b73d7SPrarit Bhargava 1175c8cdd2beSRasmus Villemoes if (list_empty(&blacklisted_initcalls)) 11767b0b73d7SPrarit Bhargava return false; 11777b0b73d7SPrarit Bhargava 11780fd5ed8dSRasmus Villemoes addr = (unsigned long) dereference_function_descriptor(fn); 11790fd5ed8dSRasmus Villemoes sprint_symbol_no_offset(fn_name, addr); 1180c8cdd2beSRasmus Villemoes 1181841c06d7SPrarit Bhargava /* 1182841c06d7SPrarit Bhargava * fn will be "function_name [module_name]" where [module_name] is not 1183841c06d7SPrarit Bhargava * displayed for built-in init functions. Strip off the [module_name]. 1184841c06d7SPrarit Bhargava */ 1185841c06d7SPrarit Bhargava strreplace(fn_name, ' ', '\0'); 1186841c06d7SPrarit Bhargava 1187e6fd1fb3SGeliang Tang list_for_each_entry(entry, &blacklisted_initcalls, next) { 11887b0b73d7SPrarit Bhargava if (!strcmp(fn_name, entry->buf)) { 11897b0b73d7SPrarit Bhargava pr_debug("initcall %s blacklisted\n", fn_name); 11907b0b73d7SPrarit Bhargava return true; 11917b0b73d7SPrarit Bhargava } 11927b0b73d7SPrarit Bhargava } 11937b0b73d7SPrarit Bhargava 11947b0b73d7SPrarit Bhargava return false; 11957b0b73d7SPrarit Bhargava } 11967b0b73d7SPrarit Bhargava #else 11977b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str) 11987b0b73d7SPrarit Bhargava { 11997b0b73d7SPrarit Bhargava pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n"); 12007b0b73d7SPrarit Bhargava return 0; 12017b0b73d7SPrarit Bhargava } 12027b0b73d7SPrarit Bhargava 12037b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn) 12047b0b73d7SPrarit Bhargava { 12057b0b73d7SPrarit Bhargava return false; 12067b0b73d7SPrarit Bhargava } 12077b0b73d7SPrarit Bhargava #endif 12087b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist); 12097b0b73d7SPrarit Bhargava 12104e37958dSSteven Rostedt (VMware) static __init_or_module void 12114e37958dSSteven Rostedt (VMware) trace_initcall_start_cb(void *data, initcall_t fn) 12121da177e4SLinus Torvalds { 1213374d6cdaSZhou jie ktime_t *calltime = data; 12141da177e4SLinus Torvalds 121558e2cf5dSGeert Uytterhoeven printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current)); 12164e37958dSSteven Rostedt (VMware) *calltime = ktime_get(); 12174e37958dSSteven Rostedt (VMware) } 12184e37958dSSteven Rostedt (VMware) 12194e37958dSSteven Rostedt (VMware) static __init_or_module void 12204e37958dSSteven Rostedt (VMware) trace_initcall_finish_cb(void *data, initcall_t fn, int ret) 12214e37958dSSteven Rostedt (VMware) { 1222374d6cdaSZhou jie ktime_t rettime, *calltime = data; 12234e37958dSSteven Rostedt (VMware) 122474239072SFrederic Weisbecker rettime = ktime_get(); 122558e2cf5dSGeert Uytterhoeven printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n", 1226105e8c2eSMark-PK Tsai fn, ret, (unsigned long long)ktime_us_delta(rettime, *calltime)); 12278f0c45cdSIngo Molnar } 12288f0c45cdSIngo Molnar 12294e37958dSSteven Rostedt (VMware) static ktime_t initcall_calltime; 12304e37958dSSteven Rostedt (VMware) 1231b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED 12324e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void) 12334e37958dSSteven Rostedt (VMware) { 12344e37958dSSteven Rostedt (VMware) int ret; 12354e37958dSSteven Rostedt (VMware) 12364e37958dSSteven Rostedt (VMware) ret = register_trace_initcall_start(trace_initcall_start_cb, 12374e37958dSSteven Rostedt (VMware) &initcall_calltime); 12384e37958dSSteven Rostedt (VMware) ret |= register_trace_initcall_finish(trace_initcall_finish_cb, 12394e37958dSSteven Rostedt (VMware) &initcall_calltime); 12404e37958dSSteven Rostedt (VMware) WARN(ret, "Failed to register initcall tracepoints\n"); 1241c1cda48aSAndrew Morton } 1242b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_start trace_initcall_start 1243b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_finish trace_initcall_finish 1244b0dc52f1SSteven Rostedt (VMware) #else 1245b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_start(initcall_t fn) 1246b0dc52f1SSteven Rostedt (VMware) { 1247b0dc52f1SSteven Rostedt (VMware) if (!initcall_debug) 1248b0dc52f1SSteven Rostedt (VMware) return; 1249b0dc52f1SSteven Rostedt (VMware) trace_initcall_start_cb(&initcall_calltime, fn); 1250b0dc52f1SSteven Rostedt (VMware) } 1251b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_finish(initcall_t fn, int ret) 1252b0dc52f1SSteven Rostedt (VMware) { 1253b0dc52f1SSteven Rostedt (VMware) if (!initcall_debug) 1254b0dc52f1SSteven Rostedt (VMware) return; 1255b0dc52f1SSteven Rostedt (VMware) trace_initcall_finish_cb(&initcall_calltime, fn, ret); 1256b0dc52f1SSteven Rostedt (VMware) } 1257b0dc52f1SSteven Rostedt (VMware) #endif /* !TRACEPOINTS_ENABLED */ 1258c1cda48aSAndrew Morton 1259e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn) 126022c5c03bSKevin Winchester { 126122c5c03bSKevin Winchester int count = preempt_count(); 1262ff1c8facSSteven Rostedt char msgbuf[64]; 12634e37958dSSteven Rostedt (VMware) int ret; 126422c5c03bSKevin Winchester 12657b0b73d7SPrarit Bhargava if (initcall_blacklisted(fn)) 12667b0b73d7SPrarit Bhargava return -EPERM; 12677b0b73d7SPrarit Bhargava 1268b0dc52f1SSteven Rostedt (VMware) do_trace_initcall_start(fn); 126922c5c03bSKevin Winchester ret = fn(); 1270b0dc52f1SSteven Rostedt (VMware) do_trace_initcall_finish(fn, ret); 127122c5c03bSKevin Winchester 1272e662e1cfSCyrill Gorcunov msgbuf[0] = 0; 1273e662e1cfSCyrill Gorcunov 12741da177e4SLinus Torvalds if (preempt_count() != count) { 1275bf5d770bSSteven Rostedt sprintf(msgbuf, "preemption imbalance "); 12764a2b4b22SPeter Zijlstra preempt_count_set(count); 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds if (irqs_disabled()) { 1279a76bfd0dSCyrill Gorcunov strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf)); 12801da177e4SLinus Torvalds local_irq_enable(); 12811da177e4SLinus Torvalds } 1282d75f773cSSakari Ailus WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf); 128359f9415fSArjan van de Ven 128438addce8SEmese Revfy add_latent_entropy(); 128530dbb20eSAmérico Wang return ret; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 1288e0df154fSLinus Torvalds 12891b1eeca7SArd Biesheuvel static initcall_entry_t *initcall_levels[] __initdata = { 1290026cee00SPawel Moll __initcall0_start, 1291026cee00SPawel Moll __initcall1_start, 1292026cee00SPawel Moll __initcall2_start, 1293026cee00SPawel Moll __initcall3_start, 1294026cee00SPawel Moll __initcall4_start, 1295026cee00SPawel Moll __initcall5_start, 1296026cee00SPawel Moll __initcall6_start, 1297026cee00SPawel Moll __initcall7_start, 1298026cee00SPawel Moll __initcall_end, 1299026cee00SPawel Moll }; 1300026cee00SPawel Moll 130196263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */ 13027c8f7193SAlexey Dobriyan static const char *initcall_level_names[] __initdata = { 1303a6fb6012SSteven Rostedt (VMware) "pure", 13049fb48c74SJim Cromie "core", 13059fb48c74SJim Cromie "postcore", 13069fb48c74SJim Cromie "arch", 13079fb48c74SJim Cromie "subsys", 13089fb48c74SJim Cromie "fs", 13099fb48c74SJim Cromie "device", 13109fb48c74SJim Cromie "late", 1311026cee00SPawel Moll }; 1312026cee00SPawel Moll 13137e2762e1SArvind Sankar static int __init ignore_unknown_bootoption(char *param, char *val, 13147e2762e1SArvind Sankar const char *unused, void *arg) 13157e2762e1SArvind Sankar { 13167e2762e1SArvind Sankar return 0; 13177e2762e1SArvind Sankar } 13187e2762e1SArvind Sankar 13190068c92aSMasami Hiramatsu static void __init do_initcall_level(int level, char *command_line) 1320026cee00SPawel Moll { 13211b1eeca7SArd Biesheuvel initcall_entry_t *fn; 1322026cee00SPawel Moll 1323026cee00SPawel Moll parse_args(initcall_level_names[level], 13240068c92aSMasami Hiramatsu command_line, __start___param, 1325026cee00SPawel Moll __stop___param - __start___param, 1326026cee00SPawel Moll level, level, 13277e2762e1SArvind Sankar NULL, ignore_unknown_bootoption); 1328026cee00SPawel Moll 13294ee7c60dSSteven Rostedt (VMware) trace_initcall_level(initcall_level_names[level]); 1330026cee00SPawel Moll for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++) 13311b1eeca7SArd Biesheuvel do_one_initcall(initcall_from_entry(fn)); 1332026cee00SPawel Moll } 1333e0df154fSLinus Torvalds 1334e0df154fSLinus Torvalds static void __init do_initcalls(void) 1335e0df154fSLinus Torvalds { 1336026cee00SPawel Moll int level; 1337941baf6fSAlexey Dobriyan size_t len = saved_command_line_len + 1; 13380068c92aSMasami Hiramatsu char *command_line; 1339e0df154fSLinus Torvalds 13400068c92aSMasami Hiramatsu command_line = kzalloc(len, GFP_KERNEL); 13410068c92aSMasami Hiramatsu if (!command_line) 13420068c92aSMasami Hiramatsu panic("%s: Failed to allocate %zu bytes\n", __func__, len); 13430068c92aSMasami Hiramatsu 13440068c92aSMasami Hiramatsu for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) { 13450068c92aSMasami Hiramatsu /* Parser modifies command_line, restore it each time */ 13460068c92aSMasami Hiramatsu strcpy(command_line, saved_command_line); 13470068c92aSMasami Hiramatsu do_initcall_level(level, command_line); 13480068c92aSMasami Hiramatsu } 13490068c92aSMasami Hiramatsu 13500068c92aSMasami Hiramatsu kfree(command_line); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds /* 13541da177e4SLinus Torvalds * Ok, the machine is now initialized. None of the devices 13551da177e4SLinus Torvalds * have been touched yet, but the CPU subsystem is up and 13561da177e4SLinus Torvalds * running, and memory and process management works. 13571da177e4SLinus Torvalds * 13581da177e4SLinus Torvalds * Now we can finally start doing some real work.. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds static void __init do_basic_setup(void) 13611da177e4SLinus Torvalds { 1362759ee091SLai Jiangshan cpuset_init_smp(); 13631da177e4SLinus Torvalds driver_init(); 1364b04c3afbSEric W. Biederman init_irq_proc(); 1365b99b87f7SPeter Oberparleiter do_ctors(); 1366b0f84374Swangyanqing do_initcalls(); 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13697babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void) 1370c2147a50SEduard - Gabriel Munteanu { 13711b1eeca7SArd Biesheuvel initcall_entry_t *fn; 1372c2147a50SEduard - Gabriel Munteanu 13734ee7c60dSSteven Rostedt (VMware) trace_initcall_level("early"); 1374026cee00SPawel Moll for (fn = __initcall_start; fn < __initcall0_start; fn++) 13751b1eeca7SArd Biesheuvel do_one_initcall(initcall_from_entry(fn)); 1376c2147a50SEduard - Gabriel Munteanu } 1377c2147a50SEduard - Gabriel Munteanu 1378a74fb73cSAl Viro static int run_init_process(const char *init_filename) 13791da177e4SLinus Torvalds { 1380b88c50acSArvind Sankar const char *const *p; 1381b88c50acSArvind Sankar 13821da177e4SLinus Torvalds argv_init[0] = init_filename; 13833f5c15d8SPaul Menzel pr_info("Run %s as init process\n", init_filename); 1384b88c50acSArvind Sankar pr_debug(" with arguments:\n"); 1385b88c50acSArvind Sankar for (p = argv_init; *p; p++) 1386b88c50acSArvind Sankar pr_debug(" %s\n", *p); 1387b88c50acSArvind Sankar pr_debug(" with environment:\n"); 1388b88c50acSArvind Sankar for (p = envp_init; *p; p++) 1389b88c50acSArvind Sankar pr_debug(" %s\n", *p); 1390be619f7fSEric W. Biederman return kernel_execve(init_filename, argv_init, envp_init); 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 1393ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename) 1394ba24762bSMichael Opdenacker { 1395ba24762bSMichael Opdenacker int ret; 1396ba24762bSMichael Opdenacker 1397ba24762bSMichael Opdenacker ret = run_init_process(init_filename); 1398ba24762bSMichael Opdenacker 1399ba24762bSMichael Opdenacker if (ret && ret != -ENOENT) { 1400ba24762bSMichael Opdenacker pr_err("Starting init: %s exists but couldn't execute it (error %d)\n", 1401ba24762bSMichael Opdenacker init_filename, ret); 1402ba24762bSMichael Opdenacker } 1403ba24762bSMichael Opdenacker 1404ba24762bSMichael Opdenacker return ret; 1405ba24762bSMichael Opdenacker } 1406ba24762bSMichael Opdenacker 1407f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void); 1408d6b21238SAl Viro 14090f5bf6d0SLaura Abbott #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX) 141039290b38SAKASHI Takahiro bool rodata_enabled __ro_after_init = true; 14112e8cff0aSMark Rutland 14122e8cff0aSMark Rutland #ifndef arch_parse_debug_rodata 14132e8cff0aSMark Rutland static inline bool arch_parse_debug_rodata(char *str) { return false; } 14142e8cff0aSMark Rutland #endif 14152e8cff0aSMark Rutland 1416d2aa1acaSKees Cook static int __init set_debug_rodata(char *str) 1417d2aa1acaSKees Cook { 14182e8cff0aSMark Rutland if (arch_parse_debug_rodata(str)) 14192e8cff0aSMark Rutland return 0; 14202e8cff0aSMark Rutland 14212e8cff0aSMark Rutland if (str && !strcmp(str, "on")) 14222e8cff0aSMark Rutland rodata_enabled = true; 14232e8cff0aSMark Rutland else if (str && !strcmp(str, "off")) 14242e8cff0aSMark Rutland rodata_enabled = false; 14252e8cff0aSMark Rutland else 1426f9a40b08SRandy Dunlap pr_warn("Invalid option string for rodata: '%s'\n", str); 14272e8cff0aSMark Rutland return 0; 1428d2aa1acaSKees Cook } 14292e8cff0aSMark Rutland early_param("rodata", set_debug_rodata); 143039290b38SAKASHI Takahiro #endif 1431d2aa1acaSKees Cook 1432d2aa1acaSKees Cook static void mark_readonly(void) 1433d2aa1acaSKees Cook { 1434398ec3e9SChristophe Leroy if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) { 1435ae646f0bSJeffrey Hugo /* 1436ba180314SPaul E. McKenney * load_module() results in W+X mappings, which are cleaned 14378f8cd6c0SChangbin Du * up with init_free_wq. Let's make sure that queued work is 1438ae646f0bSJeffrey Hugo * flushed so that we don't hit false positives looking for 1439ae646f0bSJeffrey Hugo * insecure pages which are W+X. 1440ae646f0bSJeffrey Hugo */ 14418f8cd6c0SChangbin Du flush_module_init_free_work(); 144291a1d97eSPeter Zijlstra jump_label_init_ro(); 1443d2aa1acaSKees Cook mark_rodata_ro(); 1444a5e8131aSChristophe Leroy debug_checkwx(); 14452959a5f7SJinbum Park rodata_test(); 1446398ec3e9SChristophe Leroy } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) { 1447d2aa1acaSKees Cook pr_info("Kernel memory protection disabled.\n"); 1448398ec3e9SChristophe Leroy } else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) { 1449f596ded1SChristophe Leroy pr_warn("Kernel memory protection not selected by kernel config.\n"); 1450398ec3e9SChristophe Leroy } else { 1451d2aa1acaSKees Cook pr_warn("This architecture does not have kernel memory protection.\n"); 1452d2aa1acaSKees Cook } 1453398ec3e9SChristophe Leroy } 1454d2aa1acaSKees Cook 1455997aef68SMike Rapoport void __weak free_initmem(void) 1456997aef68SMike Rapoport { 1457f4039999SMike Rapoport free_initmem_default(POISON_FREE_INITMEM); 1458997aef68SMike Rapoport } 1459997aef68SMike Rapoport 1460d6b21238SAl Viro static int __ref kernel_init(void *unused) 1461ee5bfa64SVivek Goyal { 1462ba24762bSMichael Opdenacker int ret; 1463ba24762bSMichael Opdenacker 146415faafc6SPeter Zijlstra /* 146515faafc6SPeter Zijlstra * Wait until kthreadd is all set-up. 146615faafc6SPeter Zijlstra */ 146715faafc6SPeter Zijlstra wait_for_completion(&kthreadd_done); 146815faafc6SPeter Zijlstra 1469d6b21238SAl Viro kernel_init_freeable(); 147022a9d645SArjan van de Ven /* need to finish all async __init code before freeing the memory */ 147122a9d645SArjan van de Ven async_synchronize_full(); 1472d2635f20SChristophe Leroy 1473d2635f20SChristophe Leroy system_state = SYSTEM_FREEING_INITMEM; 147482d083abSMasami Hiramatsu kprobe_free_init_mem(); 1475b80f0f6cSSteven Rostedt (VMware) ftrace_free_init_mem(); 1476d54ce615SSumit Garg kgdb_free_init_mem(); 147740caa127SMasami Hiramatsu exit_boot_config(); 1478ee5bfa64SVivek Goyal free_initmem(); 1479d2aa1acaSKees Cook mark_readonly(); 1480b976690fSJoerg Roedel 1481b976690fSJoerg Roedel /* 1482b976690fSJoerg Roedel * Kernel mappings are now finalized - update the userspace page-table 1483b976690fSJoerg Roedel * to finalize PTI. 1484b976690fSJoerg Roedel */ 1485b976690fSJoerg Roedel pti_finalize(); 1486b976690fSJoerg Roedel 1487ee5bfa64SVivek Goyal system_state = SYSTEM_RUNNING; 1488ee5bfa64SVivek Goyal numa_default_policy(); 1489ee5bfa64SVivek Goyal 1490967dcb8fSPaul E. McKenney rcu_end_inkernel_boot(); 1491967dcb8fSPaul E. McKenney 14923db978d4SVlastimil Babka do_sysctl_args(); 14933db978d4SVlastimil Babka 1494ee5bfa64SVivek Goyal if (ramdisk_execute_command) { 1495ba24762bSMichael Opdenacker ret = run_init_process(ramdisk_execute_command); 1496ba24762bSMichael Opdenacker if (!ret) 1497a74fb73cSAl Viro return 0; 1498ba24762bSMichael Opdenacker pr_err("Failed to execute %s (error %d)\n", 1499ba24762bSMichael Opdenacker ramdisk_execute_command, ret); 1500ee5bfa64SVivek Goyal } 1501ee5bfa64SVivek Goyal 1502ee5bfa64SVivek Goyal /* 1503ee5bfa64SVivek Goyal * We try each of these until one succeeds. 1504ee5bfa64SVivek Goyal * 1505ee5bfa64SVivek Goyal * The Bourne shell can be used instead of init if we are 1506ee5bfa64SVivek Goyal * trying to recover a really broken machine. 1507ee5bfa64SVivek Goyal */ 1508ee5bfa64SVivek Goyal if (execute_command) { 1509ba24762bSMichael Opdenacker ret = run_init_process(execute_command); 1510ba24762bSMichael Opdenacker if (!ret) 1511a74fb73cSAl Viro return 0; 15126ef4536eSAndy Lutomirski panic("Requested init %s failed (error %d).", 15136ef4536eSAndy Lutomirski execute_command, ret); 1514ee5bfa64SVivek Goyal } 1515ada4ab7aSChris Down 1516ada4ab7aSChris Down if (CONFIG_DEFAULT_INIT[0] != '\0') { 1517ada4ab7aSChris Down ret = run_init_process(CONFIG_DEFAULT_INIT); 1518ada4ab7aSChris Down if (ret) 1519ada4ab7aSChris Down pr_err("Default init %s failed (error %d)\n", 1520ada4ab7aSChris Down CONFIG_DEFAULT_INIT, ret); 1521ada4ab7aSChris Down else 1522ada4ab7aSChris Down return 0; 1523ada4ab7aSChris Down } 1524ada4ab7aSChris Down 1525ba24762bSMichael Opdenacker if (!try_to_run_init_process("/sbin/init") || 1526ba24762bSMichael Opdenacker !try_to_run_init_process("/etc/init") || 1527ba24762bSMichael Opdenacker !try_to_run_init_process("/bin/init") || 1528ba24762bSMichael Opdenacker !try_to_run_init_process("/bin/sh")) 1529a74fb73cSAl Viro return 0; 1530ee5bfa64SVivek Goyal 1531ba24762bSMichael Opdenacker panic("No working init found. Try passing init= option to kernel. " 15328c27ceffSMauro Carvalho Chehab "See Linux Documentation/admin-guide/init.rst for guidance."); 1533ee5bfa64SVivek Goyal } 1534ee5bfa64SVivek Goyal 15358f740636SChristoph Hellwig /* Open /dev/console, for stdin/stdout/stderr, this should never fail */ 1536a94b5214SChristoph Hellwig void __init console_on_rootfs(void) 1537b49a733dSDominik Brodowski { 15388f740636SChristoph Hellwig struct file *file = filp_open("/dev/console", O_RDWR, 0); 153974f1a299SDominik Brodowski 15408f740636SChristoph Hellwig if (IS_ERR(file)) { 1541a91bd622SPetr Mladek pr_err("Warning: unable to open an initial console.\n"); 15428f740636SChristoph Hellwig return; 15438f740636SChristoph Hellwig } 1544f0735310SChristoph Hellwig init_dup(file); 1545f0735310SChristoph Hellwig init_dup(file); 1546f0735310SChristoph Hellwig init_dup(file); 1547f0735310SChristoph Hellwig fput(file); 1548b49a733dSDominik Brodowski } 1549b49a733dSDominik Brodowski 1550f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void) 15511da177e4SLinus Torvalds { 155231a67102SLinus Torvalds /* Now the scheduler is fully set up and can do blocking allocations */ 155331a67102SLinus Torvalds gfp_allowed_mask = __GFP_BITS_MASK; 155431a67102SLinus Torvalds 155558568d2aSMiao Xie /* 155658568d2aSMiao Xie * init can allocate pages on any node 155758568d2aSMiao Xie */ 15583c466d46SLai Jiangshan set_mems_allowed(node_states[N_MEMORY]); 15591da177e4SLinus Torvalds 15600711f0d7SMark Rutland cad_pid = get_pid(task_pid(current)); 15619ec52099SCedric Le Goater 1562ca74a6f8SAndi Kleen smp_prepare_cpus(setup_max_cpus); 15631da177e4SLinus Torvalds 15643347fa09STejun Heo workqueue_init(); 15653347fa09STejun Heo 1566597b7305SMichal Hocko init_mm_internals(); 1567597b7305SMichal Hocko 15681b04fa99SUladzislau Rezki (Sony) rcu_init_tasks_generic(); 15691da177e4SLinus Torvalds do_pre_smp_initcalls(); 1570004417a6SPeter Zijlstra lockup_detector_init(); 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds smp_init(); 15731da177e4SLinus Torvalds sched_init_smp(); 15741da177e4SLinus Torvalds 15752930155bSTejun Heo workqueue_init_topology(); 1576bf52b1acSTejun Heo async_init(); 1577f1b192b1SDaniel Jordan padata_init(); 15780e1cc95bSMel Gorman page_alloc_init_late(); 15790e1cc95bSMel Gorman 15801da177e4SLinus Torvalds do_basic_setup(); 15811da177e4SLinus Torvalds 15828c0d8849SBrendan Higgins kunit_run_all_tests(); 15838c0d8849SBrendan Higgins 1584e7cb072eSRasmus Villemoes wait_for_initramfs(); 1585b49a733dSDominik Brodowski console_on_rootfs(); 15862bd3a997SEric W. Biederman 15871da177e4SLinus Torvalds /* 15881da177e4SLinus Torvalds * check if there is an early userspace init. If yes, let it do all 15891da177e4SLinus Torvalds * the work 15901da177e4SLinus Torvalds */ 1591eb9d7d39SChristoph Hellwig if (init_eaccess(ramdisk_execute_command) != 0) { 1592ffdfc409SOlof Johansson ramdisk_execute_command = NULL; 15931da177e4SLinus Torvalds prepare_namespace(); 1594ffdfc409SOlof Johansson } 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds /* 15971da177e4SLinus Torvalds * Ok, we have completed the initial bootup, and 15981da177e4SLinus Torvalds * we're essentially up and running. Get rid of the 15991da177e4SLinus Torvalds * initmem segments and start the user-mode stuff.. 1600c9cd2ce2SDmitry Kasatkin * 1601c9cd2ce2SDmitry Kasatkin * rootfs is available now, try loading the public keys 1602c9cd2ce2SDmitry Kasatkin * and default modules 16031da177e4SLinus Torvalds */ 1604bb813f4cSTejun Heo 1605c9cd2ce2SDmitry Kasatkin integrity_load_keys(); 16061da177e4SLinus Torvalds } 1607