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> 917b0b73d7SPrarit Bhargava #include <linux/list.h> 92c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h> 93e149ed2bSAl Viro #include <linux/proc_ns.h> 940ddab1d2SToshi Kani #include <linux/io.h> 9539290b38SAKASHI Takahiro #include <linux/cache.h> 962959a5f7SJinbum Park #include <linux/rodata_test.h> 9733352244SJosh Poimboeuf #include <linux/jump_label.h> 98dfd402a4SMarco Elver #include <linux/kcsan.h> 99eb9d7d39SChristoph Hellwig #include <linux/init_syscalls.h> 100e1fdc403SVijayanand Jitta #include <linux/stackdepot.h> 101375561bdSGONG, Ruiqi #include <linux/randomize_kstack.h> 102*cb12fd8eSChristian Brauner #include <linux/pidfs.h> 1039c1be193SEric Dumazet #include <net/net_namespace.h> 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #include <asm/io.h> 1061da177e4SLinus Torvalds #include <asm/setup.h> 107a940199fSAndi Kleen #include <asm/sections.h> 10837b73c82SArjan van de Ven #include <asm/cacheflush.h> 1091da177e4SLinus Torvalds 1104ee7c60dSSteven Rostedt (VMware) #define CREATE_TRACE_POINTS 1114ee7c60dSSteven Rostedt (VMware) #include <trace/events/initcall.h> 1124ee7c60dSSteven Rostedt (VMware) 1138c0d8849SBrendan Higgins #include <kunit/test.h> 1148c0d8849SBrendan Higgins 115aae5f662SSam Ravnborg static int kernel_init(void *); 1161da177e4SLinus Torvalds 1172ce802f6STejun Heo /* 1182ce802f6STejun Heo * Debug helper: via this flag we know that we are in 'early bootup code' 1192ce802f6STejun Heo * where only the boot processor is running with IRQ disabled. This means 1202ce802f6STejun Heo * two things - IRQ must not be enabled before the flag is cleared and some 1212ce802f6STejun Heo * operations which are not allowed with IRQ disabled are allowed while the 1222ce802f6STejun Heo * flag is set. 1232ce802f6STejun Heo */ 1242ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly; 1252ce802f6STejun Heo 126a6826048SPaul E. McKenney enum system_states system_state __read_mostly; 1271da177e4SLinus Torvalds EXPORT_SYMBOL(system_state); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * Boot command-line arguments 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT 1331da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */ 136d2e3192bSJan Beulich void (*__initdata late_time_init)(void); 1371da177e4SLinus Torvalds 13830d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */ 13930d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE]; 14030d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */ 141941baf6fSAlexey Dobriyan char *saved_command_line __ro_after_init; 142941baf6fSAlexey Dobriyan unsigned int saved_command_line_len __ro_after_init; 14330d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */ 14430d7e0d4SAlon Bar-Lev static char *static_command_line; 14551887d03SMasami Hiramatsu /* Untouched extra command line */ 14651887d03SMasami Hiramatsu static char *extra_command_line; 14713199162SMasami Hiramatsu /* Extra init arguments */ 14813199162SMasami Hiramatsu static char *extra_init_args; 1491da177e4SLinus Torvalds 150f61872bbSSteven Rostedt (VMware) #ifdef CONFIG_BOOT_CONFIG 151f61872bbSSteven Rostedt (VMware) /* Is bootconfig on command line? */ 152caa0708aSMasami Hiramatsu (Google) static bool bootconfig_found; 153b66fbbe8SMasami Hiramatsu static size_t initargs_offs; 154f61872bbSSteven Rostedt (VMware) #else 155f61872bbSSteven Rostedt (VMware) # define bootconfig_found false 156b66fbbe8SMasami Hiramatsu # define initargs_offs 0 157f61872bbSSteven Rostedt (VMware) #endif 158f61872bbSSteven Rostedt (VMware) 1591da177e4SLinus Torvalds static char *execute_command; 160916db733SChristoph Hellwig static char *ramdisk_execute_command = "/init"; 1611da177e4SLinus Torvalds 1628b3b2955SJan Beulich /* 163c4b2c0c5SHannes Frederic Sowa * Used to generate warnings if static_key manipulation functions are used 164c4b2c0c5SHannes Frederic Sowa * before jump_label_init is called. 165c4b2c0c5SHannes Frederic Sowa */ 166dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly; 167c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized); 168c4b2c0c5SHannes Frederic Sowa 169c4b2c0c5SHannes Frederic Sowa /* 1708b3b2955SJan Beulich * If set, this is an indication to the drivers that reset the underlying 1718b3b2955SJan Beulich * device before going ahead with the initialization otherwise driver might 1728b3b2955SJan Beulich * rely on the BIOS and skip the reset operation. 1738b3b2955SJan Beulich * 1748b3b2955SJan Beulich * This is useful if kernel is booting in an unreliable environment. 175fc454fdcSFrans Klaver * For ex. kdump situation where previous kernel has crashed, BIOS has been 1768b3b2955SJan Beulich * skipped and devices will be in unknown state. 1778b3b2955SJan Beulich */ 1788b3b2955SJan Beulich unsigned int reset_devices; 1798b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices); 1801da177e4SLinus Torvalds 1817e96287dSVivek Goyal static int __init set_reset_devices(char *str) 1827e96287dSVivek Goyal { 1837e96287dSVivek Goyal reset_devices = 1; 1847e96287dSVivek Goyal return 1; 1857e96287dSVivek Goyal } 1867e96287dSVivek Goyal 1877e96287dSVivek Goyal __setup("reset_devices", set_reset_devices); 1887e96287dSVivek Goyal 189d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, }; 190d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, }; 1911da177e4SLinus Torvalds static const char *panic_later, *panic_param; 1921da177e4SLinus Torvalds 19331c025b5SYaowei Bai static bool __init obsolete_checksetup(char *line) 1941da177e4SLinus Torvalds { 195914dcaa8SRusty Russell const struct obs_kernel_param *p; 19631c025b5SYaowei Bai bool had_early_param = false; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds p = __setup_start; 1991da177e4SLinus Torvalds do { 2001da177e4SLinus Torvalds int n = strlen(p->str); 201b1e4d20cSMichal Schmidt if (parameqn(line, p->str, n)) { 2021da177e4SLinus Torvalds if (p->early) { 20333df0d19SRusty Russell /* Already done in parse_early_param? 20433df0d19SRusty Russell * (Needs exact match on param part). 20533df0d19SRusty Russell * Keep iterating, as we can have early 20633df0d19SRusty Russell * params and __setups of same names 8( */ 2071da177e4SLinus Torvalds if (line[n] == '\0' || line[n] == '=') 20831c025b5SYaowei Bai had_early_param = true; 2091da177e4SLinus Torvalds } else if (!p->setup_func) { 210ea676e84SAndrew Morton pr_warn("Parameter %s is obsolete, ignored\n", 211ea676e84SAndrew Morton p->str); 21231c025b5SYaowei Bai return true; 2131da177e4SLinus Torvalds } else if (p->setup_func(line + n)) 21431c025b5SYaowei Bai return true; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds p++; 2171da177e4SLinus Torvalds } while (p < __setup_end); 21833df0d19SRusty Russell 21933df0d19SRusty Russell return had_early_param; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 2231da177e4SLinus Torvalds * This should be approx 2 Bo*oMips to start (note initial shift), and will 2241da177e4SLinus Torvalds * still work even if initially too large, it will just take slightly longer 2251da177e4SLinus Torvalds */ 2261da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12); 2271da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static int __init debug_kernel(char *str) 2301da177e4SLinus Torvalds { 231a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEBUG; 232f6f21c81SYinghai Lu return 0; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds static int __init quiet_kernel(char *str) 2361da177e4SLinus Torvalds { 237a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_QUIET; 238f6f21c81SYinghai Lu return 0; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 241f6f21c81SYinghai Lu early_param("debug", debug_kernel); 242f6f21c81SYinghai Lu early_param("quiet", quiet_kernel); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static int __init loglevel(char *str) 2451da177e4SLinus Torvalds { 246808bf29bSAlexander Sverdlin int newlevel; 247808bf29bSAlexander Sverdlin 248808bf29bSAlexander Sverdlin /* 249808bf29bSAlexander Sverdlin * Only update loglevel value when a correct setting was passed, 250808bf29bSAlexander Sverdlin * to prevent blind crashes (when loglevel being set to 0) that 251808bf29bSAlexander Sverdlin * are quite hard to debug 252808bf29bSAlexander Sverdlin */ 253808bf29bSAlexander Sverdlin if (get_option(&str, &newlevel)) { 254808bf29bSAlexander Sverdlin console_loglevel = newlevel; 255d9d4fcfeSAlex Riesen return 0; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 258808bf29bSAlexander Sverdlin return -EINVAL; 259808bf29bSAlexander Sverdlin } 260808bf29bSAlexander Sverdlin 261f6f21c81SYinghai Lu early_param("loglevel", loglevel); 2621da177e4SLinus Torvalds 263de462e5fSMasami Hiramatsu #ifdef CONFIG_BLK_DEV_INITRD 264a2a9d67aSMasami Hiramatsu static void * __init get_boot_config_from_initrd(size_t *_size) 265de462e5fSMasami Hiramatsu { 266de462e5fSMasami Hiramatsu u32 size, csum; 267de462e5fSMasami Hiramatsu char *data; 268de462e5fSMasami Hiramatsu u32 *hdr; 26950b8a742SMasami Hiramatsu int i; 270de462e5fSMasami Hiramatsu 271de462e5fSMasami Hiramatsu if (!initrd_end) 272de462e5fSMasami Hiramatsu return NULL; 273de462e5fSMasami Hiramatsu 274de462e5fSMasami Hiramatsu data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN; 27550b8a742SMasami Hiramatsu /* 27650b8a742SMasami Hiramatsu * Since Grub may align the size of initrd to 4, we must 27750b8a742SMasami Hiramatsu * check the preceding 3 bytes as well. 27850b8a742SMasami Hiramatsu */ 27950b8a742SMasami Hiramatsu for (i = 0; i < 4; i++) { 28050b8a742SMasami Hiramatsu if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN)) 28150b8a742SMasami Hiramatsu goto found; 28250b8a742SMasami Hiramatsu data--; 28350b8a742SMasami Hiramatsu } 284de462e5fSMasami Hiramatsu return NULL; 285de462e5fSMasami Hiramatsu 28650b8a742SMasami Hiramatsu found: 287de462e5fSMasami Hiramatsu hdr = (u32 *)(data - 8); 28824aed094SMasami Hiramatsu size = le32_to_cpu(hdr[0]); 28924aed094SMasami Hiramatsu csum = le32_to_cpu(hdr[1]); 290de462e5fSMasami Hiramatsu 291de462e5fSMasami Hiramatsu data = ((void *)hdr) - size; 292de462e5fSMasami Hiramatsu if ((unsigned long)data < initrd_start) { 293de462e5fSMasami Hiramatsu pr_err("bootconfig size %d is greater than initrd size %ld\n", 294de462e5fSMasami Hiramatsu size, initrd_end - initrd_start); 295de462e5fSMasami Hiramatsu return NULL; 296de462e5fSMasami Hiramatsu } 297de462e5fSMasami Hiramatsu 298765b8552SMasami Hiramatsu if (xbc_calc_checksum(data, size) != csum) { 299765b8552SMasami Hiramatsu pr_err("bootconfig checksum failed\n"); 300765b8552SMasami Hiramatsu return NULL; 301765b8552SMasami Hiramatsu } 302765b8552SMasami Hiramatsu 303de462e5fSMasami Hiramatsu /* Remove bootconfig from initramfs/initrd */ 304de462e5fSMasami Hiramatsu initrd_end = (unsigned long)data; 305de462e5fSMasami Hiramatsu if (_size) 306de462e5fSMasami Hiramatsu *_size = size; 307de462e5fSMasami Hiramatsu 308de462e5fSMasami Hiramatsu return data; 309de462e5fSMasami Hiramatsu } 310de462e5fSMasami Hiramatsu #else 311a2a9d67aSMasami Hiramatsu static void * __init get_boot_config_from_initrd(size_t *_size) 312de462e5fSMasami Hiramatsu { 313de462e5fSMasami Hiramatsu return NULL; 314de462e5fSMasami Hiramatsu } 315de462e5fSMasami Hiramatsu #endif 316de462e5fSMasami Hiramatsu 3177684b858SMasami Hiramatsu #ifdef CONFIG_BOOT_CONFIG 31851887d03SMasami Hiramatsu 319a27026e9SJason Yan static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata; 32051887d03SMasami Hiramatsu 32151887d03SMasami Hiramatsu #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0) 32251887d03SMasami Hiramatsu 32351887d03SMasami Hiramatsu static int __init xbc_snprint_cmdline(char *buf, size_t size, 32451887d03SMasami Hiramatsu struct xbc_node *root) 32551887d03SMasami Hiramatsu { 32651887d03SMasami Hiramatsu struct xbc_node *knode, *vnode; 32751887d03SMasami Hiramatsu char *end = buf + size; 32851887d03SMasami Hiramatsu const char *val; 32951887d03SMasami Hiramatsu int ret; 33051887d03SMasami Hiramatsu 33151887d03SMasami Hiramatsu xbc_node_for_each_key_value(root, knode, val) { 33251887d03SMasami Hiramatsu ret = xbc_node_compose_key_after(root, knode, 33351887d03SMasami Hiramatsu xbc_namebuf, XBC_KEYLEN_MAX); 33451887d03SMasami Hiramatsu if (ret < 0) 33551887d03SMasami Hiramatsu return ret; 33651887d03SMasami Hiramatsu 33751887d03SMasami Hiramatsu vnode = xbc_node_get_child(knode); 33888b91371SMasami Hiramatsu if (!vnode) { 33988b91371SMasami Hiramatsu ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf); 34051887d03SMasami Hiramatsu if (ret < 0) 34151887d03SMasami Hiramatsu return ret; 34251887d03SMasami Hiramatsu buf += ret; 34351887d03SMasami Hiramatsu continue; 34488b91371SMasami Hiramatsu } 34551887d03SMasami Hiramatsu xbc_array_for_each_value(vnode, val) { 34688b91371SMasami Hiramatsu ret = snprintf(buf, rest(buf, end), "%s=\"%s\" ", 34788b91371SMasami Hiramatsu xbc_namebuf, val); 34851887d03SMasami Hiramatsu if (ret < 0) 34951887d03SMasami Hiramatsu return ret; 35051887d03SMasami Hiramatsu buf += ret; 35151887d03SMasami Hiramatsu } 35251887d03SMasami Hiramatsu } 35351887d03SMasami Hiramatsu 35451887d03SMasami Hiramatsu return buf - (end - size); 35551887d03SMasami Hiramatsu } 35651887d03SMasami Hiramatsu #undef rest 35751887d03SMasami Hiramatsu 35851887d03SMasami Hiramatsu /* Make an extra command line under given key word */ 35951887d03SMasami Hiramatsu static char * __init xbc_make_cmdline(const char *key) 36051887d03SMasami Hiramatsu { 36151887d03SMasami Hiramatsu struct xbc_node *root; 36251887d03SMasami Hiramatsu char *new_cmdline; 36351887d03SMasami Hiramatsu int ret, len = 0; 36451887d03SMasami Hiramatsu 36551887d03SMasami Hiramatsu root = xbc_find_node(key); 36651887d03SMasami Hiramatsu if (!root) 36751887d03SMasami Hiramatsu return NULL; 36851887d03SMasami Hiramatsu 36951887d03SMasami Hiramatsu /* Count required buffer size */ 37051887d03SMasami Hiramatsu len = xbc_snprint_cmdline(NULL, 0, root); 37151887d03SMasami Hiramatsu if (len <= 0) 37251887d03SMasami Hiramatsu return NULL; 37351887d03SMasami Hiramatsu 37451887d03SMasami Hiramatsu new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES); 37551887d03SMasami Hiramatsu if (!new_cmdline) { 37651887d03SMasami Hiramatsu pr_err("Failed to allocate memory for extra kernel cmdline.\n"); 37751887d03SMasami Hiramatsu return NULL; 37851887d03SMasami Hiramatsu } 37951887d03SMasami Hiramatsu 38051887d03SMasami Hiramatsu ret = xbc_snprint_cmdline(new_cmdline, len + 1, root); 38151887d03SMasami Hiramatsu if (ret < 0 || ret > len) { 38251887d03SMasami Hiramatsu pr_err("Failed to print extra kernel cmdline.\n"); 3834421cca0SMike Rapoport memblock_free(new_cmdline, len + 1); 38451887d03SMasami Hiramatsu return NULL; 38551887d03SMasami Hiramatsu } 38651887d03SMasami Hiramatsu 38751887d03SMasami Hiramatsu return new_cmdline; 38851887d03SMasami Hiramatsu } 38951887d03SMasami Hiramatsu 390f61872bbSSteven Rostedt (VMware) static int __init bootconfig_params(char *param, char *val, 391f61872bbSSteven Rostedt (VMware) const char *unused, void *arg) 392f61872bbSSteven Rostedt (VMware) { 393f61872bbSSteven Rostedt (VMware) if (strcmp(param, "bootconfig") == 0) { 394f61872bbSSteven Rostedt (VMware) bootconfig_found = true; 395f61872bbSSteven Rostedt (VMware) } 396f61872bbSSteven Rostedt (VMware) return 0; 397f61872bbSSteven Rostedt (VMware) } 398f61872bbSSteven Rostedt (VMware) 399d0ac5fbaSMasami Hiramatsu static int __init warn_bootconfig(char *str) 400d0ac5fbaSMasami Hiramatsu { 401d0ac5fbaSMasami Hiramatsu /* The 'bootconfig' has been handled by bootconfig_params(). */ 402d0ac5fbaSMasami Hiramatsu return 0; 403d0ac5fbaSMasami Hiramatsu } 404d0ac5fbaSMasami Hiramatsu 4052b7d2fe7SCao jin static void __init setup_boot_config(void) 4067684b858SMasami Hiramatsu { 407f61872bbSSteven Rostedt (VMware) static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata; 408a2a9d67aSMasami Hiramatsu const char *msg, *data; 409a2a9d67aSMasami Hiramatsu int pos, ret; 410a2a9d67aSMasami Hiramatsu size_t size; 411a2a9d67aSMasami Hiramatsu char *err; 4127684b858SMasami Hiramatsu 413611d0a95SMasami Hiramatsu /* Cut out the bootconfig data even if we have no bootconfig option */ 414765b8552SMasami Hiramatsu data = get_boot_config_from_initrd(&size); 415a2a9d67aSMasami Hiramatsu /* If there is no bootconfig in initrd, try embedded one. */ 416a2a9d67aSMasami Hiramatsu if (!data) 417a2a9d67aSMasami Hiramatsu data = xbc_get_embedded_bootconfig(&size); 418de462e5fSMasami Hiramatsu 419a1d3a6d9SWolfram Sang strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE); 420477d0847SMasami Hiramatsu err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL, 421f61872bbSSteven Rostedt (VMware) bootconfig_params); 422f61872bbSSteven Rostedt (VMware) 423caa0708aSMasami Hiramatsu (Google) if (IS_ERR(err) || !(bootconfig_found || IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE))) 4247684b858SMasami Hiramatsu return; 4257684b858SMasami Hiramatsu 426b66fbbe8SMasami Hiramatsu /* parse_args() stops at the next param of '--' and returns an address */ 427477d0847SMasami Hiramatsu if (err) 428b66fbbe8SMasami Hiramatsu initargs_offs = err - tmp_cmdline; 429477d0847SMasami Hiramatsu 430611d0a95SMasami Hiramatsu if (!data) { 431caa0708aSMasami Hiramatsu (Google) /* If user intended to use bootconfig, show an error level message */ 432caa0708aSMasami Hiramatsu (Google) if (bootconfig_found) 433611d0a95SMasami Hiramatsu pr_err("'bootconfig' found on command line, but no bootconfig found\n"); 434caa0708aSMasami Hiramatsu (Google) else 435caa0708aSMasami Hiramatsu (Google) pr_info("No bootconfig data provided, so skipping bootconfig"); 436611d0a95SMasami Hiramatsu return; 437611d0a95SMasami Hiramatsu } 438611d0a95SMasami Hiramatsu 4397495e092SSteven Rostedt (VMware) if (size >= XBC_DATA_MAX) { 440a2a9d67aSMasami Hiramatsu pr_err("bootconfig size %ld greater than max size %d\n", 441a2a9d67aSMasami Hiramatsu (long)size, XBC_DATA_MAX); 4427684b858SMasami Hiramatsu return; 4437495e092SSteven Rostedt (VMware) } 4447684b858SMasami Hiramatsu 445bdac5c2bSMasami Hiramatsu ret = xbc_init(data, size, &msg, &pos); 44689b74cacSMasami Hiramatsu if (ret < 0) { 44789b74cacSMasami Hiramatsu if (pos < 0) 44889b74cacSMasami Hiramatsu pr_err("Failed to init bootconfig: %s.\n", msg); 44989b74cacSMasami Hiramatsu else 45089b74cacSMasami Hiramatsu pr_err("Failed to parse bootconfig: %s at %d.\n", 45189b74cacSMasami Hiramatsu msg, pos); 45289b74cacSMasami Hiramatsu } else { 453e306220cSMasami Hiramatsu xbc_get_info(&ret, NULL); 454a2a9d67aSMasami Hiramatsu pr_info("Load bootconfig: %ld bytes %d nodes\n", (long)size, ret); 45551887d03SMasami Hiramatsu /* keys starting with "kernel." are passed via cmdline */ 45651887d03SMasami Hiramatsu extra_command_line = xbc_make_cmdline("kernel"); 45713199162SMasami Hiramatsu /* Also, "init." keys are init arguments */ 45813199162SMasami Hiramatsu extra_init_args = xbc_make_cmdline("init"); 45951887d03SMasami Hiramatsu } 4607495e092SSteven Rostedt (VMware) return; 4617684b858SMasami Hiramatsu } 462de462e5fSMasami Hiramatsu 46340caa127SMasami Hiramatsu static void __init exit_boot_config(void) 46440caa127SMasami Hiramatsu { 465115d4d08SMasami Hiramatsu xbc_exit(); 46640caa127SMasami Hiramatsu } 46740caa127SMasami Hiramatsu 46840caa127SMasami Hiramatsu #else /* !CONFIG_BOOT_CONFIG */ 469de462e5fSMasami Hiramatsu 4702b7d2fe7SCao jin static void __init setup_boot_config(void) 471de462e5fSMasami Hiramatsu { 472de462e5fSMasami Hiramatsu /* Remove bootconfig data from initrd */ 473765b8552SMasami Hiramatsu get_boot_config_from_initrd(NULL); 474de462e5fSMasami Hiramatsu } 475d8a953ddSMasami Hiramatsu 476d8a953ddSMasami Hiramatsu static int __init warn_bootconfig(char *str) 477d8a953ddSMasami Hiramatsu { 47836c6aa26SShaokun Zhang pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOT_CONFIG is not set.\n"); 479d8a953ddSMasami Hiramatsu return 0; 480d8a953ddSMasami Hiramatsu } 48140caa127SMasami Hiramatsu 48240caa127SMasami Hiramatsu #define exit_boot_config() do {} while (0) 48340caa127SMasami Hiramatsu 48440caa127SMasami Hiramatsu #endif /* CONFIG_BOOT_CONFIG */ 48540caa127SMasami Hiramatsu 486d0ac5fbaSMasami Hiramatsu early_param("bootconfig", warn_bootconfig); 4877684b858SMasami Hiramatsu 4881da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */ 4897e2762e1SArvind Sankar static void __init repair_env_string(char *param, char *val) 490a99cd112SChris Metcalf { 4911da177e4SLinus Torvalds if (val) { 4921da177e4SLinus Torvalds /* param=val or param="val"? */ 4931da177e4SLinus Torvalds if (val == param+strlen(param)+1) 4941da177e4SLinus Torvalds val[-1] = '='; 4951da177e4SLinus Torvalds else if (val == param+strlen(param)+2) { 4961da177e4SLinus Torvalds val[-2] = '='; 4971da177e4SLinus Torvalds memmove(val-1, val, strlen(val)+1); 4981da177e4SLinus Torvalds } else 4991da177e4SLinus Torvalds BUG(); 5001da177e4SLinus Torvalds } 501a99cd112SChris Metcalf } 502a99cd112SChris Metcalf 50351e158c1SRusty Russell /* Anything after -- gets handed straight to init. */ 504ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val, 505ecc86170SLuis R. Rodriguez const char *unused, void *arg) 50651e158c1SRusty Russell { 50751e158c1SRusty Russell unsigned int i; 50851e158c1SRusty Russell 50951e158c1SRusty Russell if (panic_later) 51051e158c1SRusty Russell return 0; 51151e158c1SRusty Russell 5127e2762e1SArvind Sankar repair_env_string(param, val); 51351e158c1SRusty Russell 51451e158c1SRusty Russell for (i = 0; argv_init[i]; i++) { 51551e158c1SRusty Russell if (i == MAX_INIT_ARGS) { 51651e158c1SRusty Russell panic_later = "init"; 51751e158c1SRusty Russell panic_param = param; 51851e158c1SRusty Russell return 0; 51951e158c1SRusty Russell } 52051e158c1SRusty Russell } 52151e158c1SRusty Russell argv_init[i] = param; 52251e158c1SRusty Russell return 0; 52351e158c1SRusty Russell } 52451e158c1SRusty Russell 525a99cd112SChris Metcalf /* 526a99cd112SChris Metcalf * Unknown boot options get handed to init, unless they look like 527a99cd112SChris Metcalf * unused parameters (modprobe will find them in /proc/cmdline). 528a99cd112SChris Metcalf */ 529ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val, 530ecc86170SLuis R. Rodriguez const char *unused, void *arg) 531a99cd112SChris Metcalf { 532283900e8SArvind Sankar size_t len = strlen(param); 533283900e8SArvind Sankar 5348001f493SKrister Johansen /* Handle params aliased to sysctls */ 5358001f493SKrister Johansen if (sysctl_is_alias(param)) 5368001f493SKrister Johansen return 0; 5378001f493SKrister Johansen 5387e2762e1SArvind Sankar repair_env_string(param, val); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* Handle obsolete-style parameters */ 5411da177e4SLinus Torvalds if (obsolete_checksetup(param)) 5421da177e4SLinus Torvalds return 0; 5431da177e4SLinus Torvalds 544f066a4f6SRusty Russell /* Unused module parameter. */ 545283900e8SArvind Sankar if (strnchr(param, len, '.')) 5461da177e4SLinus Torvalds return 0; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds if (panic_later) 5491da177e4SLinus Torvalds return 0; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds if (val) { 5521da177e4SLinus Torvalds /* Environment option */ 5531da177e4SLinus Torvalds unsigned int i; 5541da177e4SLinus Torvalds for (i = 0; envp_init[i]; i++) { 5551da177e4SLinus Torvalds if (i == MAX_INIT_ENVS) { 556499a4584STetsuo Handa panic_later = "env"; 5571da177e4SLinus Torvalds panic_param = param; 5581da177e4SLinus Torvalds } 559283900e8SArvind Sankar if (!strncmp(param, envp_init[i], len+1)) 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds envp_init[i] = param; 5631da177e4SLinus Torvalds } else { 5641da177e4SLinus Torvalds /* Command line option */ 5651da177e4SLinus Torvalds unsigned int i; 5661da177e4SLinus Torvalds for (i = 0; argv_init[i]; i++) { 5671da177e4SLinus Torvalds if (i == MAX_INIT_ARGS) { 568499a4584STetsuo Handa panic_later = "init"; 5691da177e4SLinus Torvalds panic_param = param; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds argv_init[i] = param; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds return 0; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds static int __init init_setup(char *str) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds unsigned int i; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds execute_command = str; 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * In case LILO is going to boot us with default command line, 5841da177e4SLinus Torvalds * it prepends "auto" before the whole cmdline which makes 5851da177e4SLinus Torvalds * the shell think it should execute a script with such name. 5861da177e4SLinus Torvalds * So we ignore all arguments entered _before_ init=... [MJ] 5871da177e4SLinus Torvalds */ 5881da177e4SLinus Torvalds for (i = 1; i < MAX_INIT_ARGS; i++) 5891da177e4SLinus Torvalds argv_init[i] = NULL; 5901da177e4SLinus Torvalds return 1; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds __setup("init=", init_setup); 5931da177e4SLinus Torvalds 594ffdfc409SOlof Johansson static int __init rdinit_setup(char *str) 595ffdfc409SOlof Johansson { 596ffdfc409SOlof Johansson unsigned int i; 597ffdfc409SOlof Johansson 598ffdfc409SOlof Johansson ramdisk_execute_command = str; 599ffdfc409SOlof Johansson /* See "auto" comment in init_setup */ 600ffdfc409SOlof Johansson for (i = 1; i < MAX_INIT_ARGS; i++) 601ffdfc409SOlof Johansson argv_init[i] = NULL; 602ffdfc409SOlof Johansson return 1; 603ffdfc409SOlof Johansson } 604ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup); 605ffdfc409SOlof Johansson 6061da177e4SLinus Torvalds #ifndef CONFIG_SMP 60734db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS; 608e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { } 6091da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { } 6101da177e4SLinus Torvalds #endif 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* 61330d7e0d4SAlon Bar-Lev * We need to store the untouched command line for future reference. 61430d7e0d4SAlon Bar-Lev * We also need to store the touched command line since the parameter 61530d7e0d4SAlon Bar-Lev * parsing is performed in place, and we should allow a component to 61630d7e0d4SAlon Bar-Lev * store reference of name/value for future reference. 61730d7e0d4SAlon Bar-Lev */ 61830d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line) 61930d7e0d4SAlon Bar-Lev { 62013199162SMasami Hiramatsu size_t len, xlen = 0, ilen = 0; 621f5c7310aSMike Rapoport 62251887d03SMasami Hiramatsu if (extra_command_line) 62351887d03SMasami Hiramatsu xlen = strlen(extra_command_line); 62413199162SMasami Hiramatsu if (extra_init_args) 62513199162SMasami Hiramatsu ilen = strlen(extra_init_args) + 4; /* for " -- " */ 62651887d03SMasami Hiramatsu 62751887d03SMasami Hiramatsu len = xlen + strlen(boot_command_line) + 1; 628f5c7310aSMike Rapoport 62913199162SMasami Hiramatsu saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES); 630f5c7310aSMike Rapoport if (!saved_command_line) 63113199162SMasami Hiramatsu panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen); 632f5c7310aSMike Rapoport 633f5c7310aSMike Rapoport static_command_line = memblock_alloc(len, SMP_CACHE_BYTES); 634f5c7310aSMike Rapoport if (!static_command_line) 635f5c7310aSMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, len); 636f5c7310aSMike Rapoport 63751887d03SMasami Hiramatsu if (xlen) { 63851887d03SMasami Hiramatsu /* 63951887d03SMasami Hiramatsu * We have to put extra_command_line before boot command 64051887d03SMasami Hiramatsu * lines because there could be dashes (separator of init 64151887d03SMasami Hiramatsu * command line) in the command lines. 64251887d03SMasami Hiramatsu */ 64351887d03SMasami Hiramatsu strcpy(saved_command_line, extra_command_line); 64451887d03SMasami Hiramatsu strcpy(static_command_line, extra_command_line); 64551887d03SMasami Hiramatsu } 64651887d03SMasami Hiramatsu strcpy(saved_command_line + xlen, boot_command_line); 64751887d03SMasami Hiramatsu strcpy(static_command_line + xlen, command_line); 64813199162SMasami Hiramatsu 64913199162SMasami Hiramatsu if (ilen) { 65013199162SMasami Hiramatsu /* 65113199162SMasami Hiramatsu * Append supplemental init boot args to saved_command_line 65213199162SMasami Hiramatsu * so that user can check what command line options passed 65313199162SMasami Hiramatsu * to init. 654b66fbbe8SMasami Hiramatsu * The order should always be 655b66fbbe8SMasami Hiramatsu * " -- "[bootconfig init-param][cmdline init-param] 65613199162SMasami Hiramatsu */ 657b66fbbe8SMasami Hiramatsu if (initargs_offs) { 658b66fbbe8SMasami Hiramatsu len = xlen + initargs_offs; 659b66fbbe8SMasami Hiramatsu strcpy(saved_command_line + len, extra_init_args); 660b66fbbe8SMasami Hiramatsu len += ilen - 4; /* strlen(extra_init_args) */ 661b66fbbe8SMasami Hiramatsu strcpy(saved_command_line + len, 662b66fbbe8SMasami Hiramatsu boot_command_line + initargs_offs - 1); 663f61872bbSSteven Rostedt (VMware) } else { 664b66fbbe8SMasami Hiramatsu len = strlen(saved_command_line); 66513199162SMasami Hiramatsu strcpy(saved_command_line + len, " -- "); 66613199162SMasami Hiramatsu len += 4; 66713199162SMasami Hiramatsu strcpy(saved_command_line + len, extra_init_args); 66813199162SMasami Hiramatsu } 66930d7e0d4SAlon Bar-Lev } 670941baf6fSAlexey Dobriyan 671941baf6fSAlexey Dobriyan saved_command_line_len = strlen(saved_command_line); 672b66fbbe8SMasami Hiramatsu } 67330d7e0d4SAlon Bar-Lev 67430d7e0d4SAlon Bar-Lev /* 6751da177e4SLinus Torvalds * We need to finalize in a non-__init function or else race conditions 6761da177e4SLinus Torvalds * between the root thread and the init thread may cause start_kernel to 6771da177e4SLinus Torvalds * be reaped by free_initmem before the root thread has proceeded to 6781da177e4SLinus Torvalds * cpu_idle. 6791da177e4SLinus Torvalds * 6801da177e4SLinus Torvalds * gcc-3.4 accidentally inlines this function, so use noinline. 6811da177e4SLinus Torvalds */ 6821da177e4SLinus Torvalds 683b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done); 684b433c3d4SPeter Zijlstra 6859ea7e6b6SJosh Poimboeuf noinline void __ref __noreturn rest_init(void) 6861da177e4SLinus Torvalds { 6878fb12156SThomas Gleixner struct task_struct *tsk; 68873c27992SEric W. Biederman int pid; 68973c27992SEric W. Biederman 6907db905e6SPaul E. McKenney rcu_scheduler_starting(); 691b433c3d4SPeter Zijlstra /* 69297158569SPeter Zijlstra * We need to spawn init first so that it obtains pid 1, however 693b433c3d4SPeter Zijlstra * the init task will end up wanting to create kthreads, which, if 694b433c3d4SPeter Zijlstra * we schedule it before we create kthreadd, will OOPS. 695b433c3d4SPeter Zijlstra */ 696343f4c49SEric W. Biederman pid = user_mode_thread(kernel_init, NULL, CLONE_FS); 6978fb12156SThomas Gleixner /* 6988fb12156SThomas Gleixner * Pin init on the boot CPU. Task migration is not properly working 6998fb12156SThomas Gleixner * until sched_init_smp() has been run. It will set the allowed 7008fb12156SThomas Gleixner * CPUs for init to the non isolated CPUs. 7018fb12156SThomas Gleixner */ 7028fb12156SThomas Gleixner rcu_read_lock(); 7038fb12156SThomas Gleixner tsk = find_task_by_pid_ns(pid, &init_pid_ns); 70415faafc6SPeter Zijlstra tsk->flags |= PF_NO_SETAFFINITY; 7058fb12156SThomas Gleixner set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id())); 7068fb12156SThomas Gleixner rcu_read_unlock(); 7078fb12156SThomas Gleixner 7081da177e4SLinus Torvalds numa_default_policy(); 709cf587db2SMike Christie pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES); 710d11c563dSPaul E. McKenney rcu_read_lock(); 7115cd20455SPavel Emelyanov kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); 712d11c563dSPaul E. McKenney rcu_read_unlock(); 7131c3c5eabSThomas Gleixner 7141c3c5eabSThomas Gleixner /* 7151c3c5eabSThomas Gleixner * Enable might_sleep() and smp_processor_id() checks. 716c1a280b6SThomas Gleixner * They cannot be enabled earlier because with CONFIG_PREEMPTION=y 7171c3c5eabSThomas Gleixner * kernel_thread() would trigger might_sleep() splats. With 7181c3c5eabSThomas Gleixner * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled 7191c3c5eabSThomas Gleixner * already, but it's stuck on the kthreadd_done completion. 7201c3c5eabSThomas Gleixner */ 7211c3c5eabSThomas Gleixner system_state = SYSTEM_SCHEDULING; 7221c3c5eabSThomas Gleixner 723b433c3d4SPeter Zijlstra complete(&kthreadd_done); 724f340c0d1SIngo Molnar 725f340c0d1SIngo Molnar /* 726f340c0d1SIngo Molnar * The boot idle thread must execute schedule() 7271df21055SIngo Molnar * at least once to get things moving: 728f340c0d1SIngo Molnar */ 729bd2f5536SThomas Gleixner schedule_preempt_disabled(); 7305bfb5d69SNick Piggin /* Call into cpu_idle with preempt disabled */ 731a1a04ec3SThomas Gleixner cpu_startup_entry(CPUHP_ONLINE); 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* Check for early params. */ 735ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val, 736ecc86170SLuis R. Rodriguez const char *unused, void *arg) 7371da177e4SLinus Torvalds { 738914dcaa8SRusty Russell const struct obs_kernel_param *p; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds for (p = __setup_start; p < __setup_end; p++) { 741b1e4d20cSMichal Schmidt if ((p->early && parameq(param, p->str)) || 74218a8bd94SYinghai Lu (strcmp(param, "console") == 0 && 74318a8bd94SYinghai Lu strcmp(p->str, "earlycon") == 0) 74418a8bd94SYinghai Lu ) { 7451da177e4SLinus Torvalds if (p->setup_func(val) != 0) 746ea676e84SAndrew Morton pr_warn("Malformed early option '%s'\n", param); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds /* We accept everything at this stage. */ 7501da177e4SLinus Torvalds return 0; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 75313977091SMagnus Damm void __init parse_early_options(char *cmdline) 75413977091SMagnus Damm { 755ecc86170SLuis R. Rodriguez parse_args("early options", cmdline, NULL, 0, 0, 0, NULL, 756ecc86170SLuis R. Rodriguez do_early_param); 75713977091SMagnus Damm } 75813977091SMagnus Damm 7591da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */ 7601da177e4SLinus Torvalds void __init parse_early_param(void) 7611da177e4SLinus Torvalds { 762dd4d9fecSFabian Frederick static int done __initdata; 763dd4d9fecSFabian Frederick static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata; 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds if (done) 7661da177e4SLinus Torvalds return; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds /* All fall through to do_early_param. */ 769a1d3a6d9SWolfram Sang strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE); 77013977091SMagnus Damm parse_early_options(tmp_cmdline); 7711da177e4SLinus Torvalds done = 1; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 774e7ff3a47SThomas Gleixner void __init __weak arch_post_acpi_subsys_init(void) { } 775e7ff3a47SThomas Gleixner 776839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void) 777033ab7f8SAndrew Morton { 778033ab7f8SAndrew Morton } 779033ab7f8SAndrew Morton 780eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE 781b235beeaSLinus Torvalds void __init __weak thread_stack_cache_init(void) 7828c9843e5SBenjamin Herrenschmidt { 7838c9843e5SBenjamin Herrenschmidt } 784eded09ccSDavid Howells #endif 7858c9843e5SBenjamin Herrenschmidt 7864fc19708SNadav Amit void __init __weak poking_init(void) { } 7874fc19708SNadav Amit 788782de70cSMike Rapoport void __init __weak pgtable_cache_init(void) { } 789caa84136SNadav Amit 7908b097881SKefeng Wang void __init __weak trap_init(void) { } 7918b097881SKefeng Wang 7924e37958dSSteven Rostedt (VMware) bool initcall_debug; 7934e37958dSSteven Rostedt (VMware) core_param(initcall_debug, initcall_debug, bool, 0644); 794b0dc52f1SSteven Rostedt (VMware) 795b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED 7964e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void); 797b0dc52f1SSteven Rostedt (VMware) #else 798b0dc52f1SSteven Rostedt (VMware) static inline void initcall_debug_enable(void) 799b0dc52f1SSteven Rostedt (VMware) { 800b0dc52f1SSteven Rostedt (VMware) } 801b0dc52f1SSteven Rostedt (VMware) #endif 8024e37958dSSteven Rostedt (VMware) 8038cb37a59SMarco Elver #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET 80439218ff4SKees Cook DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, 80539218ff4SKees Cook randomize_kstack_offset); 80639218ff4SKees Cook DEFINE_PER_CPU(u32, kstack_offset); 80739218ff4SKees Cook 80839218ff4SKees Cook static int __init early_randomize_kstack_offset(char *buf) 80939218ff4SKees Cook { 81039218ff4SKees Cook int ret; 81139218ff4SKees Cook bool bool_result; 81239218ff4SKees Cook 81339218ff4SKees Cook ret = kstrtobool(buf, &bool_result); 81439218ff4SKees Cook if (ret) 81539218ff4SKees Cook return ret; 81639218ff4SKees Cook 81739218ff4SKees Cook if (bool_result) 81839218ff4SKees Cook static_branch_enable(&randomize_kstack_offset); 81939218ff4SKees Cook else 82039218ff4SKees Cook static_branch_disable(&randomize_kstack_offset); 82139218ff4SKees Cook return 0; 82239218ff4SKees Cook } 82339218ff4SKees Cook early_param("randomize_kstack_offset", early_randomize_kstack_offset); 82439218ff4SKees Cook #endif 82539218ff4SKees Cook 8269ea7e6b6SJosh Poimboeuf void __init __weak __noreturn arch_call_rest_init(void) 82753c99bd6SMartin Schwidefsky { 82853c99bd6SMartin Schwidefsky rest_init(); 82953c99bd6SMartin Schwidefsky } 83053c99bd6SMartin Schwidefsky 83186d1919aSAndrew Halaney static void __init print_unknown_bootoptions(void) 83286d1919aSAndrew Halaney { 83386d1919aSAndrew Halaney char *unknown_options; 83486d1919aSAndrew Halaney char *end; 83586d1919aSAndrew Halaney const char *const *p; 83686d1919aSAndrew Halaney size_t len; 83786d1919aSAndrew Halaney 83886d1919aSAndrew Halaney if (panic_later || (!argv_init[1] && !envp_init[2])) 83986d1919aSAndrew Halaney return; 84086d1919aSAndrew Halaney 84186d1919aSAndrew Halaney /* 84286d1919aSAndrew Halaney * Determine how many options we have to print out, plus a space 84386d1919aSAndrew Halaney * before each 84486d1919aSAndrew Halaney */ 84586d1919aSAndrew Halaney len = 1; /* null terminator */ 84686d1919aSAndrew Halaney for (p = &argv_init[1]; *p; p++) { 84786d1919aSAndrew Halaney len++; 84886d1919aSAndrew Halaney len += strlen(*p); 84986d1919aSAndrew Halaney } 85086d1919aSAndrew Halaney for (p = &envp_init[2]; *p; p++) { 85186d1919aSAndrew Halaney len++; 85286d1919aSAndrew Halaney len += strlen(*p); 85386d1919aSAndrew Halaney } 85486d1919aSAndrew Halaney 85586d1919aSAndrew Halaney unknown_options = memblock_alloc(len, SMP_CACHE_BYTES); 85686d1919aSAndrew Halaney if (!unknown_options) { 85786d1919aSAndrew Halaney pr_err("%s: Failed to allocate %zu bytes\n", 85886d1919aSAndrew Halaney __func__, len); 85986d1919aSAndrew Halaney return; 86086d1919aSAndrew Halaney } 86186d1919aSAndrew Halaney end = unknown_options; 86286d1919aSAndrew Halaney 86386d1919aSAndrew Halaney for (p = &argv_init[1]; *p; p++) 86486d1919aSAndrew Halaney end += sprintf(end, " %s", *p); 86586d1919aSAndrew Halaney for (p = &envp_init[2]; *p; p++) 86686d1919aSAndrew Halaney end += sprintf(end, " %s", *p); 86786d1919aSAndrew Halaney 8688bc2b3dcSAndrew Halaney /* Start at unknown_options[1] to skip the initial space */ 8698bc2b3dcSAndrew Halaney pr_notice("Unknown kernel command line parameters \"%s\", will be passed to user space.\n", 8708bc2b3dcSAndrew Halaney &unknown_options[1]); 8714421cca0SMike Rapoport memblock_free(unknown_options, len); 87286d1919aSAndrew Halaney } 87386d1919aSAndrew Halaney 874514ca14eS[email protected] asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector 875514ca14eS[email protected] void start_kernel(void) 8761da177e4SLinus Torvalds { 877dd4d9fecSFabian Frederick char *command_line; 878dd4d9fecSFabian Frederick char *after_dashes; 879033ab7f8SAndrew Morton 880d4311ff1SAaron Tomlin set_task_stack_end_magic(&init_task); 88173839c5bSMing Lei smp_setup_processor_id(); 8823ac7fe5aSThomas Gleixner debug_objects_early_init(); 88383cc6fa0SStephen Boyd init_vmlinux_build_id(); 88442059429SIngo Molnar 885ddbcc7e8SPaul Menage cgroup_init_early(); 886fbb9ce95SIngo Molnar 887fbb9ce95SIngo Molnar local_irq_disable(); 8882ce802f6STejun Heo early_boot_irqs_disabled = true; 889fbb9ce95SIngo Molnar 890fbb9ce95SIngo Molnar /* 8911da177e4SLinus Torvalds * Interrupts are still disabled. Do necessary setups, then 8921b3b3b49SViresh Kumar * enable them. 8931da177e4SLinus Torvalds */ 89444fd2299SStas Sergeev boot_cpu_init(); 8951da177e4SLinus Torvalds page_address_init(); 896ea676e84SAndrew Morton pr_notice("%s", linux_banner); 897e6b1db98SMatthew Garrett early_security_init(); 8981da177e4SLinus Torvalds setup_arch(&command_line); 8992b7d2fe7SCao jin setup_boot_config(); 90030d7e0d4SAlon Bar-Lev setup_command_line(command_line); 901e0982e90SMike Travis setup_nr_cpu_ids(); 902d6647bdfSTejun Heo setup_per_cpu_areas(); 90344fd2299SStas Sergeev smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */ 904b5b1404dSLinus Torvalds boot_cpu_hotplug_init(); 9051da177e4SLinus Torvalds 90651887d03SMasami Hiramatsu pr_notice("Kernel command line: %s\n", saved_command_line); 9076041186aSDan Williams /* parameters may set static keys */ 9086041186aSDan Williams jump_label_init(); 90983b519e8SPekka Enberg parse_early_param(); 91051e158c1SRusty Russell after_dashes = parse_args("Booting kernel", 91151e158c1SRusty Russell static_command_line, __start___param, 91283b519e8SPekka Enberg __stop___param - __start___param, 913ecc86170SLuis R. Rodriguez -1, -1, NULL, &unknown_bootoption); 91486d1919aSAndrew Halaney print_unknown_bootoptions(); 9153438cf54SDaniel Thompson if (!IS_ERR_OR_NULL(after_dashes)) 91651e158c1SRusty Russell parse_args("Setting init args", after_dashes, NULL, 0, -1, -1, 917ecc86170SLuis R. Rodriguez NULL, set_init_arg); 91813199162SMasami Hiramatsu if (extra_init_args) 91913199162SMasami Hiramatsu parse_args("Setting extra init args", extra_init_args, 92013199162SMasami Hiramatsu NULL, 0, -1, -1, NULL, set_init_arg); 92197ce2c88SJeremy Fitzhardinge 922f6238499SJason A. Donenfeld /* Architectural and non-timekeeping rng init, before allocator init */ 923f6238499SJason A. Donenfeld random_init_early(command_line); 924f6238499SJason A. Donenfeld 92583b519e8SPekka Enberg /* 92683b519e8SPekka Enberg * These use large bootmem allocations and must precede 927b7ec1bf3SMike Rapoport (IBM) * initalization of page allocator 92883b519e8SPekka Enberg */ 929162a7e75SMike Travis setup_log_buf(0); 93083b519e8SPekka Enberg vfs_caches_init_early(); 93183b519e8SPekka Enberg sort_main_extable(); 93283b519e8SPekka Enberg trap_init(); 933b7ec1bf3SMike Rapoport (IBM) mm_core_init(); 9345b93a836SPeter Zijlstra poking_init(); 935f631718dSSteven Rostedt (VMware) ftrace_init(); 936f631718dSSteven Rostedt (VMware) 937e725c731SSteven Rostedt (VMware) /* trace_printk can be enabled here */ 938e725c731SSteven Rostedt (VMware) early_trace_init(); 939e725c731SSteven Rostedt (VMware) 9401da177e4SLinus Torvalds /* 9411da177e4SLinus Torvalds * Set up the scheduler prior starting any interrupts (such as the 9421da177e4SLinus Torvalds * timer interrupt). Full topology setup happens at smp_init() 9431da177e4SLinus Torvalds * time - but meanwhile we still have a functioning scheduler. 9441da177e4SLinus Torvalds */ 9451da177e4SLinus Torvalds sched_init(); 946f1a0a376SValentin Schneider 947dd4d9fecSFabian Frederick if (WARN(!irqs_disabled(), 948dd4d9fecSFabian Frederick "Interrupts were enabled *very* early, fixing it\n")) 949c4a68306SArd van Breemen local_irq_disable(); 9500a835c4fSMatthew Wilcox radix_tree_init(); 95154a611b6SLiam R. Howlett maple_tree_init(); 9523347fa09STejun Heo 9533347fa09STejun Heo /* 9547d229c66STal Shorer * Set up housekeeping before setting up workqueues to allow the unbound 9557d229c66STal Shorer * workqueue to take non-housekeeping into account. 9567d229c66STal Shorer */ 9577d229c66STal Shorer housekeeping_init(); 9587d229c66STal Shorer 9597d229c66STal Shorer /* 9603347fa09STejun Heo * Allow workqueue creation and work item queueing/cancelling 9613347fa09STejun Heo * early. Work item execution depends on kthreads and starts after 9623347fa09STejun Heo * workqueue_init(). 9633347fa09STejun Heo */ 9643347fa09STejun Heo workqueue_init_early(); 9653347fa09STejun Heo 9661da177e4SLinus Torvalds rcu_init(); 9675f893b26SSteven Rostedt (Red Hat) 968e725c731SSteven Rostedt (VMware) /* Trace events are available after this */ 9695f893b26SSteven Rostedt (Red Hat) trace_init(); 9705f893b26SSteven Rostedt (Red Hat) 9714e37958dSSteven Rostedt (VMware) if (initcall_debug) 9724e37958dSSteven Rostedt (VMware) initcall_debug_enable(); 9734e37958dSSteven Rostedt (VMware) 97465f382fdSFrederic Weisbecker context_tracking_init(); 9750b8f1efaSYinghai Lu /* init some links before init_ISA_irqs() */ 9760b8f1efaSYinghai Lu early_irq_init(); 9771da177e4SLinus Torvalds init_IRQ(); 978ad2b1353SThomas Gleixner tick_init(); 979d6dd50e0SLinus Torvalds rcu_init_nohz(); 9801da177e4SLinus Torvalds init_timers(); 9818e9c01c7SFrederic Weisbecker srcu_init(); 982c0a31329SThomas Gleixner hrtimers_init(); 9831da177e4SLinus Torvalds softirq_init(); 984ad596171Sjohn stultz timekeeping_init(); 985fe222a6cSJason A. Donenfeld time_init(); 986d5553523SKees Cook 987f6238499SJason A. Donenfeld /* This must be after timekeeping is initialized */ 988f6238499SJason A. Donenfeld random_init(); 989f6238499SJason A. Donenfeld 990f6238499SJason A. Donenfeld /* These make use of the fully initialized rng */ 991f6238499SJason A. Donenfeld kfence_init(); 992d5553523SKees Cook boot_init_stack_canary(); 993d5553523SKees Cook 9949e630205SStephane Eranian perf_event_init(); 99593e02814SHeiko Carstens profile_init(); 996d8ad7d11STakao Indoh call_function_init(); 997f91eb62fSSteven Rostedt WARN(!irqs_disabled(), "Interrupts were enabled early\n"); 998c3bc8fd6SJoel Fernandes (Google) 9992ce802f6STejun Heo early_boot_irqs_disabled = false; 100093e02814SHeiko Carstens local_irq_enable(); 1001dcce284aSBenjamin Herrenschmidt 10027e85ee0cSPekka Enberg kmem_cache_init_late(); 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds /* 10051da177e4SLinus Torvalds * HACK ALERT! This is early. We're enabling the console before 10061da177e4SLinus Torvalds * we've done PCI setups etc, and console_init() must be aware of 10071da177e4SLinus Torvalds * this. But we do want output early, in case something goes wrong. 10081da177e4SLinus Torvalds */ 10091da177e4SLinus Torvalds console_init(); 10101da177e4SLinus Torvalds if (panic_later) 1011499a4584STetsuo Handa panic("Too many boot %s vars at `%s'", panic_later, 1012499a4584STetsuo Handa panic_param); 1013fbb9ce95SIngo Molnar 1014c3bc8fd6SJoel Fernandes (Google) lockdep_init(); 1015fbb9ce95SIngo Molnar 10169a11b49aSIngo Molnar /* 10179a11b49aSIngo Molnar * Need to run this when irqs are enabled, because it wants 10189a11b49aSIngo Molnar * to self-test [hard/soft]-irqs on/off lock inversion bugs 10199a11b49aSIngo Molnar * too: 10209a11b49aSIngo Molnar */ 10219a11b49aSIngo Molnar locking_selftest(); 10229a11b49aSIngo Molnar 10231da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 10241da177e4SLinus Torvalds if (initrd_start && !initrd_below_start_ok && 1025bd673c7cSGeert Uytterhoeven page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) { 1026ea676e84SAndrew Morton pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n", 1027bd673c7cSGeert Uytterhoeven page_to_pfn(virt_to_page((void *)initrd_start)), 1028bd673c7cSGeert Uytterhoeven min_low_pfn); 10291da177e4SLinus Torvalds initrd_start = 0; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds #endif 1032e7c8d5c9SChristoph Lameter setup_per_cpu_pageset(); 10331da177e4SLinus Torvalds numa_policy_init(); 10349c71206dSDou Liyang acpi_early_init(); 10351da177e4SLinus Torvalds if (late_time_init) 10361da177e4SLinus Torvalds late_time_init(); 1037857baa87SPavel Tatashin sched_clock_init(); 10381da177e4SLinus Torvalds calibrate_delay(); 1039e45e761bSMichael Kelley 10409df9d2f0SThomas Gleixner arch_cpu_finalize_init(); 10419df9d2f0SThomas Gleixner 104295846ecfSGargi Sharma pid_idr_init(); 10431da177e4SLinus Torvalds anon_vma_init(); 104411520e5eSLinus Torvalds #ifdef CONFIG_X86 104583e68189SMatt Fleming if (efi_enabled(EFI_RUNTIME_SERVICES)) 104611520e5eSLinus Torvalds efi_enter_virtual_mode(); 104711520e5eSLinus Torvalds #endif 1048b235beeaSLinus Torvalds thread_stack_cache_init(); 1049d84f4f99SDavid Howells cred_init(); 1050ff691f6eSHeinrich Schuchardt fork_init(); 10511da177e4SLinus Torvalds proc_caches_init(); 10523ea056c5SAlexey Dobriyan uts_ns_init(); 10531da177e4SLinus Torvalds key_init(); 10541da177e4SLinus Torvalds security_init(); 10550b4b3827SJason Wessel dbg_late_init(); 10569c1be193SEric Dumazet net_ns_init(); 10574248b0daSMel Gorman vfs_caches_init(); 105862906027SNicholas Piggin pagecache_init(); 10591da177e4SLinus Torvalds signals_init(); 106009652320SAlexey Dobriyan seq_file_init(); 10611da177e4SLinus Torvalds proc_root_init(); 1062e149ed2bSAl Viro nsfs_init(); 1063*cb12fd8eSChristian Brauner pidfs_init(); 10641da177e4SLinus Torvalds cpuset_init(); 1065695df213SZefan Li cgroup_init(); 1066c757249aSShailabh Nagar taskstats_init_early(); 1067ca74e92bSShailabh Nagar delayacct_init(); 10681da177e4SLinus Torvalds 1069b064a8faSRafael J. Wysocki acpi_subsystem_init(); 1070e7ff3a47SThomas Gleixner arch_post_acpi_subsys_init(); 1071dfd402a4SMarco Elver kcsan_init(); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /* Do the rest non-__init'ed, we're now alive */ 107453c99bd6SMartin Schwidefsky arch_call_rest_init(); 1075a9a3ed1eSBorislav Petkov 1076dc1d0553S[email protected] /* 1077dc1d0553S[email protected] * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10 1078dc1d0553S[email protected] * and older. 1079dc1d0553S[email protected] */ 1080dc1d0553S[email protected] #if !__has_attribute(__no_stack_protector__) 1081a9a3ed1eSBorislav Petkov prevent_tail_call_optimization(); 1082dc1d0553S[email protected] #endif 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 1085b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */ 1086b99b87f7SPeter Oberparleiter static void __init do_ctors(void) 1087b99b87f7SPeter Oberparleiter { 108855b6f763SJohannes Berg /* 108955b6f763SJohannes Berg * For UML, the constructors have already been called by the 109055b6f763SJohannes Berg * normal setup code as it's just a normal ELF binary, so we 109155b6f763SJohannes Berg * cannot do it again - but we do need CONFIG_CONSTRUCTORS 109255b6f763SJohannes Berg * even on UML for modules. 109355b6f763SJohannes Berg */ 109455b6f763SJohannes Berg #if defined(CONFIG_CONSTRUCTORS) && !defined(CONFIG_UML) 1095196a15b4SH Hartley Sweeten ctor_fn_t *fn = (ctor_fn_t *) __ctors_start; 1096b99b87f7SPeter Oberparleiter 1097196a15b4SH Hartley Sweeten for (; fn < (ctor_fn_t *) __ctors_end; fn++) 1098196a15b4SH Hartley Sweeten (*fn)(); 1099b99b87f7SPeter Oberparleiter #endif 1100b99b87f7SPeter Oberparleiter } 1101b99b87f7SPeter Oberparleiter 11027b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS 11037b0b73d7SPrarit Bhargava struct blacklist_entry { 11047b0b73d7SPrarit Bhargava struct list_head next; 11057b0b73d7SPrarit Bhargava char *buf; 11067b0b73d7SPrarit Bhargava }; 11077b0b73d7SPrarit Bhargava 11087b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls); 11097b0b73d7SPrarit Bhargava 11107b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str) 11117b0b73d7SPrarit Bhargava { 11127b0b73d7SPrarit Bhargava char *str_entry; 11137b0b73d7SPrarit Bhargava struct blacklist_entry *entry; 11147b0b73d7SPrarit Bhargava 11157b0b73d7SPrarit Bhargava /* str argument is a comma-separated list of functions */ 11167b0b73d7SPrarit Bhargava do { 11177b0b73d7SPrarit Bhargava str_entry = strsep(&str, ","); 11187b0b73d7SPrarit Bhargava if (str_entry) { 11197b0b73d7SPrarit Bhargava pr_debug("blacklisting initcall %s\n", str_entry); 11207e1c4e27SMike Rapoport entry = memblock_alloc(sizeof(*entry), 11217e1c4e27SMike Rapoport SMP_CACHE_BYTES); 1122f5c7310aSMike Rapoport if (!entry) 1123f5c7310aSMike Rapoport panic("%s: Failed to allocate %zu bytes\n", 1124f5c7310aSMike Rapoport __func__, sizeof(*entry)); 11257e1c4e27SMike Rapoport entry->buf = memblock_alloc(strlen(str_entry) + 1, 11267e1c4e27SMike Rapoport SMP_CACHE_BYTES); 1127f5c7310aSMike Rapoport if (!entry->buf) 1128f5c7310aSMike Rapoport panic("%s: Failed to allocate %zu bytes\n", 1129f5c7310aSMike Rapoport __func__, strlen(str_entry) + 1); 11307b0b73d7SPrarit Bhargava strcpy(entry->buf, str_entry); 11317b0b73d7SPrarit Bhargava list_add(&entry->next, &blacklisted_initcalls); 11327b0b73d7SPrarit Bhargava } 11337b0b73d7SPrarit Bhargava } while (str_entry); 11347b0b73d7SPrarit Bhargava 1135f9a40b08SRandy Dunlap return 1; 11367b0b73d7SPrarit Bhargava } 11377b0b73d7SPrarit Bhargava 11387b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn) 11397b0b73d7SPrarit Bhargava { 11407b0b73d7SPrarit Bhargava struct blacklist_entry *entry; 1141c8cdd2beSRasmus Villemoes char fn_name[KSYM_SYMBOL_LEN]; 11420fd5ed8dSRasmus Villemoes unsigned long addr; 11437b0b73d7SPrarit Bhargava 1144c8cdd2beSRasmus Villemoes if (list_empty(&blacklisted_initcalls)) 11457b0b73d7SPrarit Bhargava return false; 11467b0b73d7SPrarit Bhargava 11470fd5ed8dSRasmus Villemoes addr = (unsigned long) dereference_function_descriptor(fn); 11480fd5ed8dSRasmus Villemoes sprint_symbol_no_offset(fn_name, addr); 1149c8cdd2beSRasmus Villemoes 1150841c06d7SPrarit Bhargava /* 1151841c06d7SPrarit Bhargava * fn will be "function_name [module_name]" where [module_name] is not 1152841c06d7SPrarit Bhargava * displayed for built-in init functions. Strip off the [module_name]. 1153841c06d7SPrarit Bhargava */ 1154841c06d7SPrarit Bhargava strreplace(fn_name, ' ', '\0'); 1155841c06d7SPrarit Bhargava 1156e6fd1fb3SGeliang Tang list_for_each_entry(entry, &blacklisted_initcalls, next) { 11577b0b73d7SPrarit Bhargava if (!strcmp(fn_name, entry->buf)) { 11587b0b73d7SPrarit Bhargava pr_debug("initcall %s blacklisted\n", fn_name); 11597b0b73d7SPrarit Bhargava return true; 11607b0b73d7SPrarit Bhargava } 11617b0b73d7SPrarit Bhargava } 11627b0b73d7SPrarit Bhargava 11637b0b73d7SPrarit Bhargava return false; 11647b0b73d7SPrarit Bhargava } 11657b0b73d7SPrarit Bhargava #else 11667b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str) 11677b0b73d7SPrarit Bhargava { 11687b0b73d7SPrarit Bhargava pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n"); 11697b0b73d7SPrarit Bhargava return 0; 11707b0b73d7SPrarit Bhargava } 11717b0b73d7SPrarit Bhargava 11727b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn) 11737b0b73d7SPrarit Bhargava { 11747b0b73d7SPrarit Bhargava return false; 11757b0b73d7SPrarit Bhargava } 11767b0b73d7SPrarit Bhargava #endif 11777b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist); 11787b0b73d7SPrarit Bhargava 11794e37958dSSteven Rostedt (VMware) static __init_or_module void 11804e37958dSSteven Rostedt (VMware) trace_initcall_start_cb(void *data, initcall_t fn) 11811da177e4SLinus Torvalds { 1182374d6cdaSZhou jie ktime_t *calltime = data; 11831da177e4SLinus Torvalds 118458e2cf5dSGeert Uytterhoeven printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current)); 11854e37958dSSteven Rostedt (VMware) *calltime = ktime_get(); 11864e37958dSSteven Rostedt (VMware) } 11874e37958dSSteven Rostedt (VMware) 11884e37958dSSteven Rostedt (VMware) static __init_or_module void 11894e37958dSSteven Rostedt (VMware) trace_initcall_finish_cb(void *data, initcall_t fn, int ret) 11904e37958dSSteven Rostedt (VMware) { 1191374d6cdaSZhou jie ktime_t rettime, *calltime = data; 11924e37958dSSteven Rostedt (VMware) 119374239072SFrederic Weisbecker rettime = ktime_get(); 119458e2cf5dSGeert Uytterhoeven printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n", 1195105e8c2eSMark-PK Tsai fn, ret, (unsigned long long)ktime_us_delta(rettime, *calltime)); 11968f0c45cdSIngo Molnar } 11978f0c45cdSIngo Molnar 11984e37958dSSteven Rostedt (VMware) static ktime_t initcall_calltime; 11994e37958dSSteven Rostedt (VMware) 1200b0dc52f1SSteven Rostedt (VMware) #ifdef TRACEPOINTS_ENABLED 12014e37958dSSteven Rostedt (VMware) static void __init initcall_debug_enable(void) 12024e37958dSSteven Rostedt (VMware) { 12034e37958dSSteven Rostedt (VMware) int ret; 12044e37958dSSteven Rostedt (VMware) 12054e37958dSSteven Rostedt (VMware) ret = register_trace_initcall_start(trace_initcall_start_cb, 12064e37958dSSteven Rostedt (VMware) &initcall_calltime); 12074e37958dSSteven Rostedt (VMware) ret |= register_trace_initcall_finish(trace_initcall_finish_cb, 12084e37958dSSteven Rostedt (VMware) &initcall_calltime); 12094e37958dSSteven Rostedt (VMware) WARN(ret, "Failed to register initcall tracepoints\n"); 1210c1cda48aSAndrew Morton } 1211b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_start trace_initcall_start 1212b0dc52f1SSteven Rostedt (VMware) # define do_trace_initcall_finish trace_initcall_finish 1213b0dc52f1SSteven Rostedt (VMware) #else 1214b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_start(initcall_t fn) 1215b0dc52f1SSteven Rostedt (VMware) { 1216b0dc52f1SSteven Rostedt (VMware) if (!initcall_debug) 1217b0dc52f1SSteven Rostedt (VMware) return; 1218b0dc52f1SSteven Rostedt (VMware) trace_initcall_start_cb(&initcall_calltime, fn); 1219b0dc52f1SSteven Rostedt (VMware) } 1220b0dc52f1SSteven Rostedt (VMware) static inline void do_trace_initcall_finish(initcall_t fn, int ret) 1221b0dc52f1SSteven Rostedt (VMware) { 1222b0dc52f1SSteven Rostedt (VMware) if (!initcall_debug) 1223b0dc52f1SSteven Rostedt (VMware) return; 1224b0dc52f1SSteven Rostedt (VMware) trace_initcall_finish_cb(&initcall_calltime, fn, ret); 1225b0dc52f1SSteven Rostedt (VMware) } 1226b0dc52f1SSteven Rostedt (VMware) #endif /* !TRACEPOINTS_ENABLED */ 1227c1cda48aSAndrew Morton 1228e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn) 122922c5c03bSKevin Winchester { 123022c5c03bSKevin Winchester int count = preempt_count(); 1231ff1c8facSSteven Rostedt char msgbuf[64]; 12324e37958dSSteven Rostedt (VMware) int ret; 123322c5c03bSKevin Winchester 12347b0b73d7SPrarit Bhargava if (initcall_blacklisted(fn)) 12357b0b73d7SPrarit Bhargava return -EPERM; 12367b0b73d7SPrarit Bhargava 1237b0dc52f1SSteven Rostedt (VMware) do_trace_initcall_start(fn); 123822c5c03bSKevin Winchester ret = fn(); 1239b0dc52f1SSteven Rostedt (VMware) do_trace_initcall_finish(fn, ret); 124022c5c03bSKevin Winchester 1241e662e1cfSCyrill Gorcunov msgbuf[0] = 0; 1242e662e1cfSCyrill Gorcunov 12431da177e4SLinus Torvalds if (preempt_count() != count) { 1244bf5d770bSSteven Rostedt sprintf(msgbuf, "preemption imbalance "); 12454a2b4b22SPeter Zijlstra preempt_count_set(count); 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds if (irqs_disabled()) { 1248a76bfd0dSCyrill Gorcunov strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf)); 12491da177e4SLinus Torvalds local_irq_enable(); 12501da177e4SLinus Torvalds } 1251d75f773cSSakari Ailus WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf); 125259f9415fSArjan van de Ven 125338addce8SEmese Revfy add_latent_entropy(); 125430dbb20eSAmérico Wang return ret; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 1257e0df154fSLinus Torvalds 12581b1eeca7SArd Biesheuvel static initcall_entry_t *initcall_levels[] __initdata = { 1259026cee00SPawel Moll __initcall0_start, 1260026cee00SPawel Moll __initcall1_start, 1261026cee00SPawel Moll __initcall2_start, 1262026cee00SPawel Moll __initcall3_start, 1263026cee00SPawel Moll __initcall4_start, 1264026cee00SPawel Moll __initcall5_start, 1265026cee00SPawel Moll __initcall6_start, 1266026cee00SPawel Moll __initcall7_start, 1267026cee00SPawel Moll __initcall_end, 1268026cee00SPawel Moll }; 1269026cee00SPawel Moll 127096263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */ 12717c8f7193SAlexey Dobriyan static const char *initcall_level_names[] __initdata = { 1272a6fb6012SSteven Rostedt (VMware) "pure", 12739fb48c74SJim Cromie "core", 12749fb48c74SJim Cromie "postcore", 12759fb48c74SJim Cromie "arch", 12769fb48c74SJim Cromie "subsys", 12779fb48c74SJim Cromie "fs", 12789fb48c74SJim Cromie "device", 12799fb48c74SJim Cromie "late", 1280026cee00SPawel Moll }; 1281026cee00SPawel Moll 12827e2762e1SArvind Sankar static int __init ignore_unknown_bootoption(char *param, char *val, 12837e2762e1SArvind Sankar const char *unused, void *arg) 12847e2762e1SArvind Sankar { 12857e2762e1SArvind Sankar return 0; 12867e2762e1SArvind Sankar } 12877e2762e1SArvind Sankar 12880068c92aSMasami Hiramatsu static void __init do_initcall_level(int level, char *command_line) 1289026cee00SPawel Moll { 12901b1eeca7SArd Biesheuvel initcall_entry_t *fn; 1291026cee00SPawel Moll 1292026cee00SPawel Moll parse_args(initcall_level_names[level], 12930068c92aSMasami Hiramatsu command_line, __start___param, 1294026cee00SPawel Moll __stop___param - __start___param, 1295026cee00SPawel Moll level, level, 12967e2762e1SArvind Sankar NULL, ignore_unknown_bootoption); 1297026cee00SPawel Moll 12984ee7c60dSSteven Rostedt (VMware) trace_initcall_level(initcall_level_names[level]); 1299026cee00SPawel Moll for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++) 13001b1eeca7SArd Biesheuvel do_one_initcall(initcall_from_entry(fn)); 1301026cee00SPawel Moll } 1302e0df154fSLinus Torvalds 1303e0df154fSLinus Torvalds static void __init do_initcalls(void) 1304e0df154fSLinus Torvalds { 1305026cee00SPawel Moll int level; 1306941baf6fSAlexey Dobriyan size_t len = saved_command_line_len + 1; 13070068c92aSMasami Hiramatsu char *command_line; 1308e0df154fSLinus Torvalds 13090068c92aSMasami Hiramatsu command_line = kzalloc(len, GFP_KERNEL); 13100068c92aSMasami Hiramatsu if (!command_line) 13110068c92aSMasami Hiramatsu panic("%s: Failed to allocate %zu bytes\n", __func__, len); 13120068c92aSMasami Hiramatsu 13130068c92aSMasami Hiramatsu for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) { 13140068c92aSMasami Hiramatsu /* Parser modifies command_line, restore it each time */ 13150068c92aSMasami Hiramatsu strcpy(command_line, saved_command_line); 13160068c92aSMasami Hiramatsu do_initcall_level(level, command_line); 13170068c92aSMasami Hiramatsu } 13180068c92aSMasami Hiramatsu 13190068c92aSMasami Hiramatsu kfree(command_line); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Ok, the machine is now initialized. None of the devices 13241da177e4SLinus Torvalds * have been touched yet, but the CPU subsystem is up and 13251da177e4SLinus Torvalds * running, and memory and process management works. 13261da177e4SLinus Torvalds * 13271da177e4SLinus Torvalds * Now we can finally start doing some real work.. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds static void __init do_basic_setup(void) 13301da177e4SLinus Torvalds { 1331759ee091SLai Jiangshan cpuset_init_smp(); 13321da177e4SLinus Torvalds driver_init(); 1333b04c3afbSEric W. Biederman init_irq_proc(); 1334b99b87f7SPeter Oberparleiter do_ctors(); 1335b0f84374Swangyanqing do_initcalls(); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13387babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void) 1339c2147a50SEduard - Gabriel Munteanu { 13401b1eeca7SArd Biesheuvel initcall_entry_t *fn; 1341c2147a50SEduard - Gabriel Munteanu 13424ee7c60dSSteven Rostedt (VMware) trace_initcall_level("early"); 1343026cee00SPawel Moll for (fn = __initcall_start; fn < __initcall0_start; fn++) 13441b1eeca7SArd Biesheuvel do_one_initcall(initcall_from_entry(fn)); 1345c2147a50SEduard - Gabriel Munteanu } 1346c2147a50SEduard - Gabriel Munteanu 1347a74fb73cSAl Viro static int run_init_process(const char *init_filename) 13481da177e4SLinus Torvalds { 1349b88c50acSArvind Sankar const char *const *p; 1350b88c50acSArvind Sankar 13511da177e4SLinus Torvalds argv_init[0] = init_filename; 13523f5c15d8SPaul Menzel pr_info("Run %s as init process\n", init_filename); 1353b88c50acSArvind Sankar pr_debug(" with arguments:\n"); 1354b88c50acSArvind Sankar for (p = argv_init; *p; p++) 1355b88c50acSArvind Sankar pr_debug(" %s\n", *p); 1356b88c50acSArvind Sankar pr_debug(" with environment:\n"); 1357b88c50acSArvind Sankar for (p = envp_init; *p; p++) 1358b88c50acSArvind Sankar pr_debug(" %s\n", *p); 1359be619f7fSEric W. Biederman return kernel_execve(init_filename, argv_init, envp_init); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 1362ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename) 1363ba24762bSMichael Opdenacker { 1364ba24762bSMichael Opdenacker int ret; 1365ba24762bSMichael Opdenacker 1366ba24762bSMichael Opdenacker ret = run_init_process(init_filename); 1367ba24762bSMichael Opdenacker 1368ba24762bSMichael Opdenacker if (ret && ret != -ENOENT) { 1369ba24762bSMichael Opdenacker pr_err("Starting init: %s exists but couldn't execute it (error %d)\n", 1370ba24762bSMichael Opdenacker init_filename, ret); 1371ba24762bSMichael Opdenacker } 1372ba24762bSMichael Opdenacker 1373ba24762bSMichael Opdenacker return ret; 1374ba24762bSMichael Opdenacker } 1375ba24762bSMichael Opdenacker 1376f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void); 1377d6b21238SAl Viro 13780f5bf6d0SLaura Abbott #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX) 137939290b38SAKASHI Takahiro bool rodata_enabled __ro_after_init = true; 13802e8cff0aSMark Rutland 13812e8cff0aSMark Rutland #ifndef arch_parse_debug_rodata 13822e8cff0aSMark Rutland static inline bool arch_parse_debug_rodata(char *str) { return false; } 13832e8cff0aSMark Rutland #endif 13842e8cff0aSMark Rutland 1385d2aa1acaSKees Cook static int __init set_debug_rodata(char *str) 1386d2aa1acaSKees Cook { 13872e8cff0aSMark Rutland if (arch_parse_debug_rodata(str)) 13882e8cff0aSMark Rutland return 0; 13892e8cff0aSMark Rutland 13902e8cff0aSMark Rutland if (str && !strcmp(str, "on")) 13912e8cff0aSMark Rutland rodata_enabled = true; 13922e8cff0aSMark Rutland else if (str && !strcmp(str, "off")) 13932e8cff0aSMark Rutland rodata_enabled = false; 13942e8cff0aSMark Rutland else 1395f9a40b08SRandy Dunlap pr_warn("Invalid option string for rodata: '%s'\n", str); 13962e8cff0aSMark Rutland return 0; 1397d2aa1acaSKees Cook } 13982e8cff0aSMark Rutland early_param("rodata", set_debug_rodata); 139939290b38SAKASHI Takahiro #endif 1400d2aa1acaSKees Cook 14010f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX 1402d2aa1acaSKees Cook static void mark_readonly(void) 1403d2aa1acaSKees Cook { 14042959a5f7SJinbum Park if (rodata_enabled) { 1405ae646f0bSJeffrey Hugo /* 1406ba180314SPaul E. McKenney * load_module() results in W+X mappings, which are cleaned 1407ba180314SPaul E. McKenney * up with call_rcu(). Let's make sure that queued work is 1408ae646f0bSJeffrey Hugo * flushed so that we don't hit false positives looking for 1409ae646f0bSJeffrey Hugo * insecure pages which are W+X. 1410ae646f0bSJeffrey Hugo */ 1411ba180314SPaul E. McKenney rcu_barrier(); 1412d2aa1acaSKees Cook mark_rodata_ro(); 14132959a5f7SJinbum Park rodata_test(); 14142959a5f7SJinbum Park } else 1415d2aa1acaSKees Cook pr_info("Kernel memory protection disabled.\n"); 1416d2aa1acaSKees Cook } 1417f596ded1SChristophe Leroy #elif defined(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX) 1418f596ded1SChristophe Leroy static inline void mark_readonly(void) 1419f596ded1SChristophe Leroy { 1420f596ded1SChristophe Leroy pr_warn("Kernel memory protection not selected by kernel config.\n"); 1421f596ded1SChristophe Leroy } 1422d2aa1acaSKees Cook #else 1423d2aa1acaSKees Cook static inline void mark_readonly(void) 1424d2aa1acaSKees Cook { 1425d2aa1acaSKees Cook pr_warn("This architecture does not have kernel memory protection.\n"); 1426d2aa1acaSKees Cook } 1427d2aa1acaSKees Cook #endif 1428d2aa1acaSKees Cook 1429997aef68SMike Rapoport void __weak free_initmem(void) 1430997aef68SMike Rapoport { 1431f4039999SMike Rapoport free_initmem_default(POISON_FREE_INITMEM); 1432997aef68SMike Rapoport } 1433997aef68SMike Rapoport 1434d6b21238SAl Viro static int __ref kernel_init(void *unused) 1435ee5bfa64SVivek Goyal { 1436ba24762bSMichael Opdenacker int ret; 1437ba24762bSMichael Opdenacker 143815faafc6SPeter Zijlstra /* 143915faafc6SPeter Zijlstra * Wait until kthreadd is all set-up. 144015faafc6SPeter Zijlstra */ 144115faafc6SPeter Zijlstra wait_for_completion(&kthreadd_done); 144215faafc6SPeter Zijlstra 1443d6b21238SAl Viro kernel_init_freeable(); 144422a9d645SArjan van de Ven /* need to finish all async __init code before freeing the memory */ 144522a9d645SArjan van de Ven async_synchronize_full(); 1446d2635f20SChristophe Leroy 1447d2635f20SChristophe Leroy system_state = SYSTEM_FREEING_INITMEM; 144882d083abSMasami Hiramatsu kprobe_free_init_mem(); 1449b80f0f6cSSteven Rostedt (VMware) ftrace_free_init_mem(); 1450d54ce615SSumit Garg kgdb_free_init_mem(); 145140caa127SMasami Hiramatsu exit_boot_config(); 1452ee5bfa64SVivek Goyal free_initmem(); 1453d2aa1acaSKees Cook mark_readonly(); 1454b976690fSJoerg Roedel 1455b976690fSJoerg Roedel /* 1456b976690fSJoerg Roedel * Kernel mappings are now finalized - update the userspace page-table 1457b976690fSJoerg Roedel * to finalize PTI. 1458b976690fSJoerg Roedel */ 1459b976690fSJoerg Roedel pti_finalize(); 1460b976690fSJoerg Roedel 1461ee5bfa64SVivek Goyal system_state = SYSTEM_RUNNING; 1462ee5bfa64SVivek Goyal numa_default_policy(); 1463ee5bfa64SVivek Goyal 1464967dcb8fSPaul E. McKenney rcu_end_inkernel_boot(); 1465967dcb8fSPaul E. McKenney 14663db978d4SVlastimil Babka do_sysctl_args(); 14673db978d4SVlastimil Babka 1468ee5bfa64SVivek Goyal if (ramdisk_execute_command) { 1469ba24762bSMichael Opdenacker ret = run_init_process(ramdisk_execute_command); 1470ba24762bSMichael Opdenacker if (!ret) 1471a74fb73cSAl Viro return 0; 1472ba24762bSMichael Opdenacker pr_err("Failed to execute %s (error %d)\n", 1473ba24762bSMichael Opdenacker ramdisk_execute_command, ret); 1474ee5bfa64SVivek Goyal } 1475ee5bfa64SVivek Goyal 1476ee5bfa64SVivek Goyal /* 1477ee5bfa64SVivek Goyal * We try each of these until one succeeds. 1478ee5bfa64SVivek Goyal * 1479ee5bfa64SVivek Goyal * The Bourne shell can be used instead of init if we are 1480ee5bfa64SVivek Goyal * trying to recover a really broken machine. 1481ee5bfa64SVivek Goyal */ 1482ee5bfa64SVivek Goyal if (execute_command) { 1483ba24762bSMichael Opdenacker ret = run_init_process(execute_command); 1484ba24762bSMichael Opdenacker if (!ret) 1485a74fb73cSAl Viro return 0; 14866ef4536eSAndy Lutomirski panic("Requested init %s failed (error %d).", 14876ef4536eSAndy Lutomirski execute_command, ret); 1488ee5bfa64SVivek Goyal } 1489ada4ab7aSChris Down 1490ada4ab7aSChris Down if (CONFIG_DEFAULT_INIT[0] != '\0') { 1491ada4ab7aSChris Down ret = run_init_process(CONFIG_DEFAULT_INIT); 1492ada4ab7aSChris Down if (ret) 1493ada4ab7aSChris Down pr_err("Default init %s failed (error %d)\n", 1494ada4ab7aSChris Down CONFIG_DEFAULT_INIT, ret); 1495ada4ab7aSChris Down else 1496ada4ab7aSChris Down return 0; 1497ada4ab7aSChris Down } 1498ada4ab7aSChris Down 1499ba24762bSMichael Opdenacker if (!try_to_run_init_process("/sbin/init") || 1500ba24762bSMichael Opdenacker !try_to_run_init_process("/etc/init") || 1501ba24762bSMichael Opdenacker !try_to_run_init_process("/bin/init") || 1502ba24762bSMichael Opdenacker !try_to_run_init_process("/bin/sh")) 1503a74fb73cSAl Viro return 0; 1504ee5bfa64SVivek Goyal 1505ba24762bSMichael Opdenacker panic("No working init found. Try passing init= option to kernel. " 15068c27ceffSMauro Carvalho Chehab "See Linux Documentation/admin-guide/init.rst for guidance."); 1507ee5bfa64SVivek Goyal } 1508ee5bfa64SVivek Goyal 15098f740636SChristoph Hellwig /* Open /dev/console, for stdin/stdout/stderr, this should never fail */ 1510a94b5214SChristoph Hellwig void __init console_on_rootfs(void) 1511b49a733dSDominik Brodowski { 15128f740636SChristoph Hellwig struct file *file = filp_open("/dev/console", O_RDWR, 0); 151374f1a299SDominik Brodowski 15148f740636SChristoph Hellwig if (IS_ERR(file)) { 1515a91bd622SPetr Mladek pr_err("Warning: unable to open an initial console.\n"); 15168f740636SChristoph Hellwig return; 15178f740636SChristoph Hellwig } 1518f0735310SChristoph Hellwig init_dup(file); 1519f0735310SChristoph Hellwig init_dup(file); 1520f0735310SChristoph Hellwig init_dup(file); 1521f0735310SChristoph Hellwig fput(file); 1522b49a733dSDominik Brodowski } 1523b49a733dSDominik Brodowski 1524f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void) 15251da177e4SLinus Torvalds { 152631a67102SLinus Torvalds /* Now the scheduler is fully set up and can do blocking allocations */ 152731a67102SLinus Torvalds gfp_allowed_mask = __GFP_BITS_MASK; 152831a67102SLinus Torvalds 152958568d2aSMiao Xie /* 153058568d2aSMiao Xie * init can allocate pages on any node 153158568d2aSMiao Xie */ 15323c466d46SLai Jiangshan set_mems_allowed(node_states[N_MEMORY]); 15331da177e4SLinus Torvalds 15340711f0d7SMark Rutland cad_pid = get_pid(task_pid(current)); 15359ec52099SCedric Le Goater 1536ca74a6f8SAndi Kleen smp_prepare_cpus(setup_max_cpus); 15371da177e4SLinus Torvalds 15383347fa09STejun Heo workqueue_init(); 15393347fa09STejun Heo 1540597b7305SMichal Hocko init_mm_internals(); 1541597b7305SMichal Hocko 15421b04fa99SUladzislau Rezki (Sony) rcu_init_tasks_generic(); 15431da177e4SLinus Torvalds do_pre_smp_initcalls(); 1544004417a6SPeter Zijlstra lockup_detector_init(); 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds smp_init(); 15471da177e4SLinus Torvalds sched_init_smp(); 15481da177e4SLinus Torvalds 15492930155bSTejun Heo workqueue_init_topology(); 1550f1b192b1SDaniel Jordan padata_init(); 15510e1cc95bSMel Gorman page_alloc_init_late(); 15520e1cc95bSMel Gorman 15531da177e4SLinus Torvalds do_basic_setup(); 15541da177e4SLinus Torvalds 15558c0d8849SBrendan Higgins kunit_run_all_tests(); 15568c0d8849SBrendan Higgins 1557e7cb072eSRasmus Villemoes wait_for_initramfs(); 1558b49a733dSDominik Brodowski console_on_rootfs(); 15592bd3a997SEric W. Biederman 15601da177e4SLinus Torvalds /* 15611da177e4SLinus Torvalds * check if there is an early userspace init. If yes, let it do all 15621da177e4SLinus Torvalds * the work 15631da177e4SLinus Torvalds */ 1564eb9d7d39SChristoph Hellwig if (init_eaccess(ramdisk_execute_command) != 0) { 1565ffdfc409SOlof Johansson ramdisk_execute_command = NULL; 15661da177e4SLinus Torvalds prepare_namespace(); 1567ffdfc409SOlof Johansson } 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds /* 15701da177e4SLinus Torvalds * Ok, we have completed the initial bootup, and 15711da177e4SLinus Torvalds * we're essentially up and running. Get rid of the 15721da177e4SLinus Torvalds * initmem segments and start the user-mode stuff.. 1573c9cd2ce2SDmitry Kasatkin * 1574c9cd2ce2SDmitry Kasatkin * rootfs is available now, try loading the public keys 1575c9cd2ce2SDmitry Kasatkin * and default modules 15761da177e4SLinus Torvalds */ 1577bb813f4cSTejun Heo 1578c9cd2ce2SDmitry Kasatkin integrity_load_keys(); 15791da177e4SLinus Torvalds } 1580