11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/init/main.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * GK 2/5/95 - Changed to support mounting root fs via NFS 71da177e4SLinus Torvalds * Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96 81da177e4SLinus Torvalds * Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96 91da177e4SLinus Torvalds * Simplified starting of init: Michael A. Griffith <grif@acm.org> 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 12ea676e84SAndrew Morton #define DEBUG /* Enable initcall_debug */ 13ea676e84SAndrew Morton 141da177e4SLinus Torvalds #include <linux/types.h> 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/proc_fs.h> 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/syscalls.h> 199b5609fdSIngo Molnar #include <linux/stackprotector.h> 201da177e4SLinus Torvalds #include <linux/string.h> 211da177e4SLinus Torvalds #include <linux/ctype.h> 221da177e4SLinus Torvalds #include <linux/delay.h> 231da177e4SLinus Torvalds #include <linux/ioport.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/initrd.h> 261da177e4SLinus Torvalds #include <linux/bootmem.h> 274a7a16dcSLen Brown #include <linux/acpi.h> 281da177e4SLinus Torvalds #include <linux/tty.h> 291da177e4SLinus Torvalds #include <linux/percpu.h> 301da177e4SLinus Torvalds #include <linux/kmod.h> 31db64fe02SNick Piggin #include <linux/vmalloc.h> 321da177e4SLinus Torvalds #include <linux/kernel_stat.h> 33d7cd5611SRusty Russell #include <linux/start_kernel.h> 341da177e4SLinus Torvalds #include <linux/security.h> 353d442233SJens Axboe #include <linux/smp.h> 361da177e4SLinus Torvalds #include <linux/profile.h> 371da177e4SLinus Torvalds #include <linux/rcupdate.h> 381da177e4SLinus Torvalds #include <linux/moduleparam.h> 391da177e4SLinus Torvalds #include <linux/kallsyms.h> 401da177e4SLinus Torvalds #include <linux/writeback.h> 411da177e4SLinus Torvalds #include <linux/cpu.h> 421da177e4SLinus Torvalds #include <linux/cpuset.h> 43ddbcc7e8SPaul Menage #include <linux/cgroup.h> 441da177e4SLinus Torvalds #include <linux/efi.h> 45906568c9SThomas Gleixner #include <linux/tick.h> 466168a702SAndrew Morton #include <linux/interrupt.h> 47c757249aSShailabh Nagar #include <linux/taskstats_kern.h> 48ca74e92bSShailabh Nagar #include <linux/delayacct.h> 491da177e4SLinus Torvalds #include <linux/unistd.h> 501da177e4SLinus Torvalds #include <linux/rmap.h> 511da177e4SLinus Torvalds #include <linux/mempolicy.h> 521da177e4SLinus Torvalds #include <linux/key.h> 53b6cd0b77SAdrian Bunk #include <linux/buffer_head.h> 54eefa864bSJoonsoo Kim #include <linux/page_ext.h> 559a11b49aSIngo Molnar #include <linux/debug_locks.h> 563ac7fe5aSThomas Gleixner #include <linux/debugobjects.h> 57fbb9ce95SIngo Molnar #include <linux/lockdep.h> 583c7b4e6bSCatalin Marinas #include <linux/kmemleak.h> 5984d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h> 601f21782eSAdrian Bunk #include <linux/device.h> 6173c27992SEric W. Biederman #include <linux/kthread.h> 62e6fe6649SAdrian Bunk #include <linux/sched.h> 63a1c9eea9SAdrian Bunk #include <linux/signal.h> 64199f0ca5SAkinobu Mita #include <linux/idr.h> 650b4b3827SJason Wessel #include <linux/kgdb.h> 6668bf21aaSSteven Rostedt #include <linux/ftrace.h> 6722a9d645SArjan van de Ven #include <linux/async.h> 68dfec072eSVegard Nossum #include <linux/kmemcheck.h> 696ae6996aSFeng Tang #include <linux/sfi.h> 702b2af54aSKay Sievers #include <linux/shmem_fs.h> 715a0e3ad6STejun Heo #include <linux/slab.h> 7224a24bb6SPeter Zijlstra #include <linux/perf_event.h> 734a9d4b02SAl Viro #include <linux/file.h> 74a74fb73cSAl Viro #include <linux/ptrace.h> 75bb813f4cSTejun Heo #include <linux/blkdev.h> 76bb813f4cSTejun Heo #include <linux/elevator.h> 7738ff87f7SStephen Boyd #include <linux/sched_clock.h> 7865f382fdSFrederic Weisbecker #include <linux/context_tracking.h> 7947d06e53STheodore Ts'o #include <linux/random.h> 807b0b73d7SPrarit Bhargava #include <linux/list.h> 81c9cd2ce2SDmitry Kasatkin #include <linux/integrity.h> 82e149ed2bSAl Viro #include <linux/proc_ns.h> 830ddab1d2SToshi Kani #include <linux/io.h> 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds #include <asm/io.h> 861da177e4SLinus Torvalds #include <asm/bugs.h> 871da177e4SLinus Torvalds #include <asm/setup.h> 88a940199fSAndi Kleen #include <asm/sections.h> 8937b73c82SArjan van de Ven #include <asm/cacheflush.h> 901da177e4SLinus Torvalds 91aae5f662SSam Ravnborg static int kernel_init(void *); 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds extern void init_IRQ(void); 94ff691f6eSHeinrich Schuchardt extern void fork_init(void); 951da177e4SLinus Torvalds extern void radix_tree_init(void); 9637b73c82SArjan van de Ven #ifndef CONFIG_DEBUG_RODATA 9737b73c82SArjan van de Ven static inline void mark_rodata_ro(void) { } 9837b73c82SArjan van de Ven #endif 991da177e4SLinus Torvalds 1002ce802f6STejun Heo /* 1012ce802f6STejun Heo * Debug helper: via this flag we know that we are in 'early bootup code' 1022ce802f6STejun Heo * where only the boot processor is running with IRQ disabled. This means 1032ce802f6STejun Heo * two things - IRQ must not be enabled before the flag is cleared and some 1042ce802f6STejun Heo * operations which are not allowed with IRQ disabled are allowed while the 1052ce802f6STejun Heo * flag is set. 1062ce802f6STejun Heo */ 1072ce802f6STejun Heo bool early_boot_irqs_disabled __read_mostly; 1082ce802f6STejun Heo 109a6826048SPaul E. McKenney enum system_states system_state __read_mostly; 1101da177e4SLinus Torvalds EXPORT_SYMBOL(system_state); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* 1131da177e4SLinus Torvalds * Boot command-line arguments 1141da177e4SLinus Torvalds */ 1151da177e4SLinus Torvalds #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT 1161da177e4SLinus Torvalds #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds extern void time_init(void); 1191da177e4SLinus Torvalds /* Default late time init is NULL. archs can override this later. */ 120d2e3192bSJan Beulich void (*__initdata late_time_init)(void); 1211da177e4SLinus Torvalds 12230d7e0d4SAlon Bar-Lev /* Untouched command line saved by arch-specific code. */ 12330d7e0d4SAlon Bar-Lev char __initdata boot_command_line[COMMAND_LINE_SIZE]; 12430d7e0d4SAlon Bar-Lev /* Untouched saved command line (eg. for /proc) */ 12530d7e0d4SAlon Bar-Lev char *saved_command_line; 12630d7e0d4SAlon Bar-Lev /* Command line for parameter parsing */ 12730d7e0d4SAlon Bar-Lev static char *static_command_line; 12808746a65SKrzysztof Mazur /* Command line for per-initcall parameter parsing */ 12908746a65SKrzysztof Mazur static char *initcall_command_line; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static char *execute_command; 132ffdfc409SOlof Johansson static char *ramdisk_execute_command; 1331da177e4SLinus Torvalds 1348b3b2955SJan Beulich /* 135c4b2c0c5SHannes Frederic Sowa * Used to generate warnings if static_key manipulation functions are used 136c4b2c0c5SHannes Frederic Sowa * before jump_label_init is called. 137c4b2c0c5SHannes Frederic Sowa */ 138dd4d9fecSFabian Frederick bool static_key_initialized __read_mostly; 139c4b2c0c5SHannes Frederic Sowa EXPORT_SYMBOL_GPL(static_key_initialized); 140c4b2c0c5SHannes Frederic Sowa 141c4b2c0c5SHannes Frederic Sowa /* 1428b3b2955SJan Beulich * If set, this is an indication to the drivers that reset the underlying 1438b3b2955SJan Beulich * device before going ahead with the initialization otherwise driver might 1448b3b2955SJan Beulich * rely on the BIOS and skip the reset operation. 1458b3b2955SJan Beulich * 1468b3b2955SJan Beulich * This is useful if kernel is booting in an unreliable environment. 147fc454fdcSFrans Klaver * For ex. kdump situation where previous kernel has crashed, BIOS has been 1488b3b2955SJan Beulich * skipped and devices will be in unknown state. 1498b3b2955SJan Beulich */ 1508b3b2955SJan Beulich unsigned int reset_devices; 1518b3b2955SJan Beulich EXPORT_SYMBOL(reset_devices); 1521da177e4SLinus Torvalds 1537e96287dSVivek Goyal static int __init set_reset_devices(char *str) 1547e96287dSVivek Goyal { 1557e96287dSVivek Goyal reset_devices = 1; 1567e96287dSVivek Goyal return 1; 1577e96287dSVivek Goyal } 1587e96287dSVivek Goyal 1597e96287dSVivek Goyal __setup("reset_devices", set_reset_devices); 1607e96287dSVivek Goyal 161d7627467SDavid Howells static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, }; 162d7627467SDavid Howells const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, }; 1631da177e4SLinus Torvalds static const char *panic_later, *panic_param; 1641da177e4SLinus Torvalds 165914dcaa8SRusty Russell extern const struct obs_kernel_param __setup_start[], __setup_end[]; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds static int __init obsolete_checksetup(char *line) 1681da177e4SLinus Torvalds { 169914dcaa8SRusty Russell const struct obs_kernel_param *p; 17033df0d19SRusty Russell int had_early_param = 0; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds p = __setup_start; 1731da177e4SLinus Torvalds do { 1741da177e4SLinus Torvalds int n = strlen(p->str); 175b1e4d20cSMichal Schmidt if (parameqn(line, p->str, n)) { 1761da177e4SLinus Torvalds if (p->early) { 17733df0d19SRusty Russell /* Already done in parse_early_param? 17833df0d19SRusty Russell * (Needs exact match on param part). 17933df0d19SRusty Russell * Keep iterating, as we can have early 18033df0d19SRusty Russell * params and __setups of same names 8( */ 1811da177e4SLinus Torvalds if (line[n] == '\0' || line[n] == '=') 18233df0d19SRusty Russell had_early_param = 1; 1831da177e4SLinus Torvalds } else if (!p->setup_func) { 184ea676e84SAndrew Morton pr_warn("Parameter %s is obsolete, ignored\n", 185ea676e84SAndrew Morton p->str); 1861da177e4SLinus Torvalds return 1; 1871da177e4SLinus Torvalds } else if (p->setup_func(line + n)) 1881da177e4SLinus Torvalds return 1; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds p++; 1911da177e4SLinus Torvalds } while (p < __setup_end); 19233df0d19SRusty Russell 19333df0d19SRusty Russell return had_early_param; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* 1971da177e4SLinus Torvalds * This should be approx 2 Bo*oMips to start (note initial shift), and will 1981da177e4SLinus Torvalds * still work even if initially too large, it will just take slightly longer 1991da177e4SLinus Torvalds */ 2001da177e4SLinus Torvalds unsigned long loops_per_jiffy = (1<<12); 2011da177e4SLinus Torvalds EXPORT_SYMBOL(loops_per_jiffy); 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds static int __init debug_kernel(char *str) 2041da177e4SLinus Torvalds { 205a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEBUG; 206f6f21c81SYinghai Lu return 0; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds static int __init quiet_kernel(char *str) 2101da177e4SLinus Torvalds { 211a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_QUIET; 212f6f21c81SYinghai Lu return 0; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 215f6f21c81SYinghai Lu early_param("debug", debug_kernel); 216f6f21c81SYinghai Lu early_param("quiet", quiet_kernel); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds static int __init loglevel(char *str) 2191da177e4SLinus Torvalds { 220808bf29bSAlexander Sverdlin int newlevel; 221808bf29bSAlexander Sverdlin 222808bf29bSAlexander Sverdlin /* 223808bf29bSAlexander Sverdlin * Only update loglevel value when a correct setting was passed, 224808bf29bSAlexander Sverdlin * to prevent blind crashes (when loglevel being set to 0) that 225808bf29bSAlexander Sverdlin * are quite hard to debug 226808bf29bSAlexander Sverdlin */ 227808bf29bSAlexander Sverdlin if (get_option(&str, &newlevel)) { 228808bf29bSAlexander Sverdlin console_loglevel = newlevel; 229d9d4fcfeSAlex Riesen return 0; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 232808bf29bSAlexander Sverdlin return -EINVAL; 233808bf29bSAlexander Sverdlin } 234808bf29bSAlexander Sverdlin 235f6f21c81SYinghai Lu early_param("loglevel", loglevel); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /* Change NUL term back to "=", to make "param" the whole string. */ 238ecc86170SLuis R. Rodriguez static int __init repair_env_string(char *param, char *val, 239ecc86170SLuis R. Rodriguez const char *unused, void *arg) 240a99cd112SChris Metcalf { 2411da177e4SLinus Torvalds if (val) { 2421da177e4SLinus Torvalds /* param=val or param="val"? */ 2431da177e4SLinus Torvalds if (val == param+strlen(param)+1) 2441da177e4SLinus Torvalds val[-1] = '='; 2451da177e4SLinus Torvalds else if (val == param+strlen(param)+2) { 2461da177e4SLinus Torvalds val[-2] = '='; 2471da177e4SLinus Torvalds memmove(val-1, val, strlen(val)+1); 2481da177e4SLinus Torvalds val--; 2491da177e4SLinus Torvalds } else 2501da177e4SLinus Torvalds BUG(); 2511da177e4SLinus Torvalds } 252a99cd112SChris Metcalf return 0; 253a99cd112SChris Metcalf } 254a99cd112SChris Metcalf 25551e158c1SRusty Russell /* Anything after -- gets handed straight to init. */ 256ecc86170SLuis R. Rodriguez static int __init set_init_arg(char *param, char *val, 257ecc86170SLuis R. Rodriguez const char *unused, void *arg) 25851e158c1SRusty Russell { 25951e158c1SRusty Russell unsigned int i; 26051e158c1SRusty Russell 26151e158c1SRusty Russell if (panic_later) 26251e158c1SRusty Russell return 0; 26351e158c1SRusty Russell 264ecc86170SLuis R. Rodriguez repair_env_string(param, val, unused, NULL); 26551e158c1SRusty Russell 26651e158c1SRusty Russell for (i = 0; argv_init[i]; i++) { 26751e158c1SRusty Russell if (i == MAX_INIT_ARGS) { 26851e158c1SRusty Russell panic_later = "init"; 26951e158c1SRusty Russell panic_param = param; 27051e158c1SRusty Russell return 0; 27151e158c1SRusty Russell } 27251e158c1SRusty Russell } 27351e158c1SRusty Russell argv_init[i] = param; 27451e158c1SRusty Russell return 0; 27551e158c1SRusty Russell } 27651e158c1SRusty Russell 277a99cd112SChris Metcalf /* 278a99cd112SChris Metcalf * Unknown boot options get handed to init, unless they look like 279a99cd112SChris Metcalf * unused parameters (modprobe will find them in /proc/cmdline). 280a99cd112SChris Metcalf */ 281ecc86170SLuis R. Rodriguez static int __init unknown_bootoption(char *param, char *val, 282ecc86170SLuis R. Rodriguez const char *unused, void *arg) 283a99cd112SChris Metcalf { 284ecc86170SLuis R. Rodriguez repair_env_string(param, val, unused, NULL); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* Handle obsolete-style parameters */ 2871da177e4SLinus Torvalds if (obsolete_checksetup(param)) 2881da177e4SLinus Torvalds return 0; 2891da177e4SLinus Torvalds 290f066a4f6SRusty Russell /* Unused module parameter. */ 291f066a4f6SRusty Russell if (strchr(param, '.') && (!val || strchr(param, '.') < val)) 2921da177e4SLinus Torvalds return 0; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds if (panic_later) 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds if (val) { 2981da177e4SLinus Torvalds /* Environment option */ 2991da177e4SLinus Torvalds unsigned int i; 3001da177e4SLinus Torvalds for (i = 0; envp_init[i]; i++) { 3011da177e4SLinus Torvalds if (i == MAX_INIT_ENVS) { 302499a4584STetsuo Handa panic_later = "env"; 3031da177e4SLinus Torvalds panic_param = param; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds if (!strncmp(param, envp_init[i], val - param)) 3061da177e4SLinus Torvalds break; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds envp_init[i] = param; 3091da177e4SLinus Torvalds } else { 3101da177e4SLinus Torvalds /* Command line option */ 3111da177e4SLinus Torvalds unsigned int i; 3121da177e4SLinus Torvalds for (i = 0; argv_init[i]; i++) { 3131da177e4SLinus Torvalds if (i == MAX_INIT_ARGS) { 314499a4584STetsuo Handa panic_later = "init"; 3151da177e4SLinus Torvalds panic_param = param; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds argv_init[i] = param; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds return 0; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds static int __init init_setup(char *str) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds unsigned int i; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds execute_command = str; 3281da177e4SLinus Torvalds /* 3291da177e4SLinus Torvalds * In case LILO is going to boot us with default command line, 3301da177e4SLinus Torvalds * it prepends "auto" before the whole cmdline which makes 3311da177e4SLinus Torvalds * the shell think it should execute a script with such name. 3321da177e4SLinus Torvalds * So we ignore all arguments entered _before_ init=... [MJ] 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds for (i = 1; i < MAX_INIT_ARGS; i++) 3351da177e4SLinus Torvalds argv_init[i] = NULL; 3361da177e4SLinus Torvalds return 1; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds __setup("init=", init_setup); 3391da177e4SLinus Torvalds 340ffdfc409SOlof Johansson static int __init rdinit_setup(char *str) 341ffdfc409SOlof Johansson { 342ffdfc409SOlof Johansson unsigned int i; 343ffdfc409SOlof Johansson 344ffdfc409SOlof Johansson ramdisk_execute_command = str; 345ffdfc409SOlof Johansson /* See "auto" comment in init_setup */ 346ffdfc409SOlof Johansson for (i = 1; i < MAX_INIT_ARGS; i++) 347ffdfc409SOlof Johansson argv_init[i] = NULL; 348ffdfc409SOlof Johansson return 1; 349ffdfc409SOlof Johansson } 350ffdfc409SOlof Johansson __setup("rdinit=", rdinit_setup); 351ffdfc409SOlof Johansson 3521da177e4SLinus Torvalds #ifndef CONFIG_SMP 35334db18a0SAmerigo Wang static const unsigned int setup_max_cpus = NR_CPUS; 354e0982e90SMike Travis static inline void setup_nr_cpu_ids(void) { } 3551da177e4SLinus Torvalds static inline void smp_prepare_cpus(unsigned int maxcpus) { } 3561da177e4SLinus Torvalds #endif 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds /* 35930d7e0d4SAlon Bar-Lev * We need to store the untouched command line for future reference. 36030d7e0d4SAlon Bar-Lev * We also need to store the touched command line since the parameter 36130d7e0d4SAlon Bar-Lev * parsing is performed in place, and we should allow a component to 36230d7e0d4SAlon Bar-Lev * store reference of name/value for future reference. 36330d7e0d4SAlon Bar-Lev */ 36430d7e0d4SAlon Bar-Lev static void __init setup_command_line(char *command_line) 36530d7e0d4SAlon Bar-Lev { 366098b081bSSantosh Shilimkar saved_command_line = 367098b081bSSantosh Shilimkar memblock_virt_alloc(strlen(boot_command_line) + 1, 0); 368098b081bSSantosh Shilimkar initcall_command_line = 369098b081bSSantosh Shilimkar memblock_virt_alloc(strlen(boot_command_line) + 1, 0); 370098b081bSSantosh Shilimkar static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0); 37130d7e0d4SAlon Bar-Lev strcpy(saved_command_line, boot_command_line); 37230d7e0d4SAlon Bar-Lev strcpy(static_command_line, command_line); 37330d7e0d4SAlon Bar-Lev } 37430d7e0d4SAlon Bar-Lev 37530d7e0d4SAlon Bar-Lev /* 3761da177e4SLinus Torvalds * We need to finalize in a non-__init function or else race conditions 3771da177e4SLinus Torvalds * between the root thread and the init thread may cause start_kernel to 3781da177e4SLinus Torvalds * be reaped by free_initmem before the root thread has proceeded to 3791da177e4SLinus Torvalds * cpu_idle. 3801da177e4SLinus Torvalds * 3811da177e4SLinus Torvalds * gcc-3.4 accidentally inlines this function, so use noinline. 3821da177e4SLinus Torvalds */ 3831da177e4SLinus Torvalds 384b433c3d4SPeter Zijlstra static __initdata DECLARE_COMPLETION(kthreadd_done); 385b433c3d4SPeter Zijlstra 386f99ebf0aSRakib Mullick static noinline void __init_refok rest_init(void) 3871da177e4SLinus Torvalds { 38873c27992SEric W. Biederman int pid; 38973c27992SEric W. Biederman 3907db905e6SPaul E. McKenney rcu_scheduler_starting(); 39100df35f9SPaul E. McKenney smpboot_thread_init(); 392b433c3d4SPeter Zijlstra /* 39397158569SPeter Zijlstra * We need to spawn init first so that it obtains pid 1, however 394b433c3d4SPeter Zijlstra * the init task will end up wanting to create kthreads, which, if 395b433c3d4SPeter Zijlstra * we schedule it before we create kthreadd, will OOPS. 396b433c3d4SPeter Zijlstra */ 39734a1b723SOleg Nesterov kernel_thread(kernel_init, NULL, CLONE_FS); 3981da177e4SLinus Torvalds numa_default_policy(); 39973c27992SEric W. Biederman pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES); 400d11c563dSPaul E. McKenney rcu_read_lock(); 4015cd20455SPavel Emelyanov kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns); 402d11c563dSPaul E. McKenney rcu_read_unlock(); 403b433c3d4SPeter Zijlstra complete(&kthreadd_done); 404f340c0d1SIngo Molnar 405f340c0d1SIngo Molnar /* 406f340c0d1SIngo Molnar * The boot idle thread must execute schedule() 4071df21055SIngo Molnar * at least once to get things moving: 408f340c0d1SIngo Molnar */ 4091df21055SIngo Molnar init_idle_bootup_task(current); 410bd2f5536SThomas Gleixner schedule_preempt_disabled(); 4115bfb5d69SNick Piggin /* Call into cpu_idle with preempt disabled */ 412a1a04ec3SThomas Gleixner cpu_startup_entry(CPUHP_ONLINE); 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* Check for early params. */ 416ecc86170SLuis R. Rodriguez static int __init do_early_param(char *param, char *val, 417ecc86170SLuis R. Rodriguez const char *unused, void *arg) 4181da177e4SLinus Torvalds { 419914dcaa8SRusty Russell const struct obs_kernel_param *p; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds for (p = __setup_start; p < __setup_end; p++) { 422b1e4d20cSMichal Schmidt if ((p->early && parameq(param, p->str)) || 42318a8bd94SYinghai Lu (strcmp(param, "console") == 0 && 42418a8bd94SYinghai Lu strcmp(p->str, "earlycon") == 0) 42518a8bd94SYinghai Lu ) { 4261da177e4SLinus Torvalds if (p->setup_func(val) != 0) 427ea676e84SAndrew Morton pr_warn("Malformed early option '%s'\n", param); 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds /* We accept everything at this stage. */ 4311da177e4SLinus Torvalds return 0; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 43413977091SMagnus Damm void __init parse_early_options(char *cmdline) 43513977091SMagnus Damm { 436ecc86170SLuis R. Rodriguez parse_args("early options", cmdline, NULL, 0, 0, 0, NULL, 437ecc86170SLuis R. Rodriguez do_early_param); 43813977091SMagnus Damm } 43913977091SMagnus Damm 4401da177e4SLinus Torvalds /* Arch code calls this early on, or if not, just before other parsing. */ 4411da177e4SLinus Torvalds void __init parse_early_param(void) 4421da177e4SLinus Torvalds { 443dd4d9fecSFabian Frederick static int done __initdata; 444dd4d9fecSFabian Frederick static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds if (done) 4471da177e4SLinus Torvalds return; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* All fall through to do_early_param. */ 45030d7e0d4SAlon Bar-Lev strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE); 45113977091SMagnus Damm parse_early_options(tmp_cmdline); 4521da177e4SLinus Torvalds done = 1; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * Activate the first processor. 4571da177e4SLinus Torvalds */ 4581da177e4SLinus Torvalds 45944fd2299SStas Sergeev static void __init boot_cpu_init(void) 46044fd2299SStas Sergeev { 46144fd2299SStas Sergeev int cpu = smp_processor_id(); 46244fd2299SStas Sergeev /* Mark the boot cpu "present", "online" etc for SMP and UP case */ 463915441b6SRusty Russell set_cpu_online(cpu, true); 464933b0618SPeter Zijlstra set_cpu_active(cpu, true); 465915441b6SRusty Russell set_cpu_present(cpu, true); 466915441b6SRusty Russell set_cpu_possible(cpu, true); 46744fd2299SStas Sergeev } 46844fd2299SStas Sergeev 469839ad62eSBenjamin Herrenschmidt void __init __weak smp_setup_processor_id(void) 470033ab7f8SAndrew Morton { 471033ab7f8SAndrew Morton } 472033ab7f8SAndrew Morton 473eded09ccSDavid Howells # if THREAD_SIZE >= PAGE_SIZE 4748c9843e5SBenjamin Herrenschmidt void __init __weak thread_info_cache_init(void) 4758c9843e5SBenjamin Herrenschmidt { 4768c9843e5SBenjamin Herrenschmidt } 477eded09ccSDavid Howells #endif 4788c9843e5SBenjamin Herrenschmidt 479444f478fSPekka Enberg /* 480444f478fSPekka Enberg * Set up kernel memory allocators 481444f478fSPekka Enberg */ 482444f478fSPekka Enberg static void __init mm_init(void) 483444f478fSPekka Enberg { 484eefa864bSJoonsoo Kim /* 485eefa864bSJoonsoo Kim * page_ext requires contiguous pages, 486eefa864bSJoonsoo Kim * bigger than MAX_ORDER unless SPARSEMEM. 487eefa864bSJoonsoo Kim */ 488eefa864bSJoonsoo Kim page_ext_init_flatmem(); 489444f478fSPekka Enberg mem_init(); 490444f478fSPekka Enberg kmem_cache_init(); 491099a19d9STejun Heo percpu_init_late(); 492b35f1819SKirill A. Shutemov pgtable_init(); 493444f478fSPekka Enberg vmalloc_init(); 4940ddab1d2SToshi Kani ioremap_huge_init(); 495444f478fSPekka Enberg } 496444f478fSPekka Enberg 497722a9f92SAndi Kleen asmlinkage __visible void __init start_kernel(void) 4981da177e4SLinus Torvalds { 499dd4d9fecSFabian Frederick char *command_line; 500dd4d9fecSFabian Frederick char *after_dashes; 501033ab7f8SAndrew Morton 5021da177e4SLinus Torvalds /* 503fbb9ce95SIngo Molnar * Need to run as early as possible, to initialize the 504fbb9ce95SIngo Molnar * lockdep hash: 505fbb9ce95SIngo Molnar */ 506fbb9ce95SIngo Molnar lockdep_init(); 507d4311ff1SAaron Tomlin set_task_stack_end_magic(&init_task); 50873839c5bSMing Lei smp_setup_processor_id(); 5093ac7fe5aSThomas Gleixner debug_objects_early_init(); 51042059429SIngo Molnar 51142059429SIngo Molnar /* 51242059429SIngo Molnar * Set up the the initial canary ASAP: 51342059429SIngo Molnar */ 51442059429SIngo Molnar boot_init_stack_canary(); 51542059429SIngo Molnar 516ddbcc7e8SPaul Menage cgroup_init_early(); 517fbb9ce95SIngo Molnar 518fbb9ce95SIngo Molnar local_irq_disable(); 5192ce802f6STejun Heo early_boot_irqs_disabled = true; 520fbb9ce95SIngo Molnar 521fbb9ce95SIngo Molnar /* 5221da177e4SLinus Torvalds * Interrupts are still disabled. Do necessary setups, then 5231da177e4SLinus Torvalds * enable them 5241da177e4SLinus Torvalds */ 52544fd2299SStas Sergeev boot_cpu_init(); 5261da177e4SLinus Torvalds page_address_init(); 527ea676e84SAndrew Morton pr_notice("%s", linux_banner); 5281da177e4SLinus Torvalds setup_arch(&command_line); 5296345d24dSLinus Torvalds mm_init_cpumask(&init_mm); 53030d7e0d4SAlon Bar-Lev setup_command_line(command_line); 531e0982e90SMike Travis setup_nr_cpu_ids(); 532d6647bdfSTejun Heo setup_per_cpu_areas(); 53344fd2299SStas Sergeev smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */ 5341da177e4SLinus Torvalds 5359adb62a5SJiang Liu build_all_zonelists(NULL, NULL); 53683b519e8SPekka Enberg page_alloc_init(); 53783b519e8SPekka Enberg 538ea676e84SAndrew Morton pr_notice("Kernel command line: %s\n", boot_command_line); 53983b519e8SPekka Enberg parse_early_param(); 54051e158c1SRusty Russell after_dashes = parse_args("Booting kernel", 54151e158c1SRusty Russell static_command_line, __start___param, 54283b519e8SPekka Enberg __stop___param - __start___param, 543ecc86170SLuis R. Rodriguez -1, -1, NULL, &unknown_bootoption); 5443438cf54SDaniel Thompson if (!IS_ERR_OR_NULL(after_dashes)) 54551e158c1SRusty Russell parse_args("Setting init args", after_dashes, NULL, 0, -1, -1, 546ecc86170SLuis R. Rodriguez NULL, set_init_arg); 54797ce2c88SJeremy Fitzhardinge 54897ce2c88SJeremy Fitzhardinge jump_label_init(); 54997ce2c88SJeremy Fitzhardinge 55083b519e8SPekka Enberg /* 55183b519e8SPekka Enberg * These use large bootmem allocations and must precede 55283b519e8SPekka Enberg * kmem_cache_init() 55383b519e8SPekka Enberg */ 554162a7e75SMike Travis setup_log_buf(0); 55583b519e8SPekka Enberg pidhash_init(); 55683b519e8SPekka Enberg vfs_caches_init_early(); 55783b519e8SPekka Enberg sort_main_extable(); 55883b519e8SPekka Enberg trap_init(); 559444f478fSPekka Enberg mm_init(); 560de03c72cSKOSAKI Motohiro 5611da177e4SLinus Torvalds /* 5621da177e4SLinus Torvalds * Set up the scheduler prior starting any interrupts (such as the 5631da177e4SLinus Torvalds * timer interrupt). Full topology setup happens at smp_init() 5641da177e4SLinus Torvalds * time - but meanwhile we still have a functioning scheduler. 5651da177e4SLinus Torvalds */ 5661da177e4SLinus Torvalds sched_init(); 5671da177e4SLinus Torvalds /* 5681da177e4SLinus Torvalds * Disable preemption - early bootup scheduling is extremely 5691da177e4SLinus Torvalds * fragile until we cpu_idle() for the first time. 5701da177e4SLinus Torvalds */ 5711da177e4SLinus Torvalds preempt_disable(); 572dd4d9fecSFabian Frederick if (WARN(!irqs_disabled(), 573dd4d9fecSFabian Frederick "Interrupts were enabled *very* early, fixing it\n")) 574c4a68306SArd van Breemen local_irq_disable(); 5759f58a205SPeter Zijlstra idr_init_cache(); 5761da177e4SLinus Torvalds rcu_init(); 5775f893b26SSteven Rostedt (Red Hat) 5785f893b26SSteven Rostedt (Red Hat) /* trace_printk() and trace points may be used after this */ 5795f893b26SSteven Rostedt (Red Hat) trace_init(); 5805f893b26SSteven Rostedt (Red Hat) 58165f382fdSFrederic Weisbecker context_tracking_init(); 582773e3eb7SYinghai Lu radix_tree_init(); 5830b8f1efaSYinghai Lu /* init some links before init_ISA_irqs() */ 5840b8f1efaSYinghai Lu early_irq_init(); 5851da177e4SLinus Torvalds init_IRQ(); 586ad2b1353SThomas Gleixner tick_init(); 587d6dd50e0SLinus Torvalds rcu_init_nohz(); 5881da177e4SLinus Torvalds init_timers(); 589c0a31329SThomas Gleixner hrtimers_init(); 5901da177e4SLinus Torvalds softirq_init(); 591ad596171Sjohn stultz timekeeping_init(); 59288fecaa2Sjohn stultz time_init(); 59338ff87f7SStephen Boyd sched_clock_postinit(); 5949e630205SStephane Eranian perf_event_init(); 59593e02814SHeiko Carstens profile_init(); 596d8ad7d11STakao Indoh call_function_init(); 597f91eb62fSSteven Rostedt WARN(!irqs_disabled(), "Interrupts were enabled early\n"); 5982ce802f6STejun Heo early_boot_irqs_disabled = false; 59993e02814SHeiko Carstens local_irq_enable(); 600dcce284aSBenjamin Herrenschmidt 6017e85ee0cSPekka Enberg kmem_cache_init_late(); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * HACK ALERT! This is early. We're enabling the console before 6051da177e4SLinus Torvalds * we've done PCI setups etc, and console_init() must be aware of 6061da177e4SLinus Torvalds * this. But we do want output early, in case something goes wrong. 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds console_init(); 6091da177e4SLinus Torvalds if (panic_later) 610499a4584STetsuo Handa panic("Too many boot %s vars at `%s'", panic_later, 611499a4584STetsuo Handa panic_param); 612fbb9ce95SIngo Molnar 613fbb9ce95SIngo Molnar lockdep_info(); 614fbb9ce95SIngo Molnar 6159a11b49aSIngo Molnar /* 6169a11b49aSIngo Molnar * Need to run this when irqs are enabled, because it wants 6179a11b49aSIngo Molnar * to self-test [hard/soft]-irqs on/off lock inversion bugs 6189a11b49aSIngo Molnar * too: 6199a11b49aSIngo Molnar */ 6209a11b49aSIngo Molnar locking_selftest(); 6219a11b49aSIngo Molnar 6221da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 6231da177e4SLinus Torvalds if (initrd_start && !initrd_below_start_ok && 624bd673c7cSGeert Uytterhoeven page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) { 625ea676e84SAndrew Morton pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n", 626bd673c7cSGeert Uytterhoeven page_to_pfn(virt_to_page((void *)initrd_start)), 627bd673c7cSGeert Uytterhoeven min_low_pfn); 6281da177e4SLinus Torvalds initrd_start = 0; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds #endif 631eefa864bSJoonsoo Kim page_ext_init(); 6323ac7fe5aSThomas Gleixner debug_objects_mem_init(); 6339b090f2dSCatalin Marinas kmemleak_init(); 634e7c8d5c9SChristoph Lameter setup_per_cpu_pageset(); 6351da177e4SLinus Torvalds numa_policy_init(); 6361da177e4SLinus Torvalds if (late_time_init) 6371da177e4SLinus Torvalds late_time_init(); 638fa84e9eeSThomas Gleixner sched_clock_init(); 6391da177e4SLinus Torvalds calibrate_delay(); 6401da177e4SLinus Torvalds pidmap_init(); 6411da177e4SLinus Torvalds anon_vma_init(); 642c4e1acbbSRafael J. Wysocki acpi_early_init(); 64311520e5eSLinus Torvalds #ifdef CONFIG_X86 64483e68189SMatt Fleming if (efi_enabled(EFI_RUNTIME_SERVICES)) 64511520e5eSLinus Torvalds efi_enter_virtual_mode(); 64611520e5eSLinus Torvalds #endif 647197725deSH. Peter Anvin #ifdef CONFIG_X86_ESPFIX64 6483891a04aSH. Peter Anvin /* Should be run before the first non-init thread is created */ 6493891a04aSH. Peter Anvin init_espfix_bsp(); 6503891a04aSH. Peter Anvin #endif 6518c9843e5SBenjamin Herrenschmidt thread_info_cache_init(); 652d84f4f99SDavid Howells cred_init(); 653ff691f6eSHeinrich Schuchardt fork_init(); 6541da177e4SLinus Torvalds proc_caches_init(); 6551da177e4SLinus Torvalds buffer_init(); 6561da177e4SLinus Torvalds key_init(); 6571da177e4SLinus Torvalds security_init(); 6580b4b3827SJason Wessel dbg_late_init(); 6594248b0daSMel Gorman vfs_caches_init(); 6601da177e4SLinus Torvalds signals_init(); 6611da177e4SLinus Torvalds /* rootfs populating might need page-writeback */ 6621da177e4SLinus Torvalds page_writeback_init(); 6631da177e4SLinus Torvalds proc_root_init(); 664e149ed2bSAl Viro nsfs_init(); 6651da177e4SLinus Torvalds cpuset_init(); 666695df213SZefan Li cgroup_init(); 667c757249aSShailabh Nagar taskstats_init_early(); 668ca74e92bSShailabh Nagar delayacct_init(); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds check_bugs(); 6711da177e4SLinus Torvalds 672b064a8faSRafael J. Wysocki acpi_subsystem_init(); 6736ae6996aSFeng Tang sfi_init_late(); 6741da177e4SLinus Torvalds 67583e68189SMatt Fleming if (efi_enabled(EFI_RUNTIME_SERVICES)) { 6762223af38SJosh Triplett efi_late_init(); 67778510792SJosh Triplett efi_free_boot_services(); 6782223af38SJosh Triplett } 67978510792SJosh Triplett 68068bf21aaSSteven Rostedt ftrace_init(); 68168bf21aaSSteven Rostedt 6821da177e4SLinus Torvalds /* Do the rest non-__init'ed, we're now alive */ 6831da177e4SLinus Torvalds rest_init(); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 686b99b87f7SPeter Oberparleiter /* Call all constructor functions linked into the kernel. */ 687b99b87f7SPeter Oberparleiter static void __init do_ctors(void) 688b99b87f7SPeter Oberparleiter { 689b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS 690196a15b4SH Hartley Sweeten ctor_fn_t *fn = (ctor_fn_t *) __ctors_start; 691b99b87f7SPeter Oberparleiter 692196a15b4SH Hartley Sweeten for (; fn < (ctor_fn_t *) __ctors_end; fn++) 693196a15b4SH Hartley Sweeten (*fn)(); 694b99b87f7SPeter Oberparleiter #endif 695b99b87f7SPeter Oberparleiter } 696b99b87f7SPeter Oberparleiter 6972329abfaSRusty Russell bool initcall_debug; 698d0ea3d7dSRusty Russell core_param(initcall_debug, initcall_debug, bool, 0644); 6991da177e4SLinus Torvalds 7007b0b73d7SPrarit Bhargava #ifdef CONFIG_KALLSYMS 7017b0b73d7SPrarit Bhargava struct blacklist_entry { 7027b0b73d7SPrarit Bhargava struct list_head next; 7037b0b73d7SPrarit Bhargava char *buf; 7047b0b73d7SPrarit Bhargava }; 7057b0b73d7SPrarit Bhargava 7067b0b73d7SPrarit Bhargava static __initdata_or_module LIST_HEAD(blacklisted_initcalls); 7077b0b73d7SPrarit Bhargava 7087b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str) 7097b0b73d7SPrarit Bhargava { 7107b0b73d7SPrarit Bhargava char *str_entry; 7117b0b73d7SPrarit Bhargava struct blacklist_entry *entry; 7127b0b73d7SPrarit Bhargava 7137b0b73d7SPrarit Bhargava /* str argument is a comma-separated list of functions */ 7147b0b73d7SPrarit Bhargava do { 7157b0b73d7SPrarit Bhargava str_entry = strsep(&str, ","); 7167b0b73d7SPrarit Bhargava if (str_entry) { 7177b0b73d7SPrarit Bhargava pr_debug("blacklisting initcall %s\n", str_entry); 7187b0b73d7SPrarit Bhargava entry = alloc_bootmem(sizeof(*entry)); 7197b0b73d7SPrarit Bhargava entry->buf = alloc_bootmem(strlen(str_entry) + 1); 7207b0b73d7SPrarit Bhargava strcpy(entry->buf, str_entry); 7217b0b73d7SPrarit Bhargava list_add(&entry->next, &blacklisted_initcalls); 7227b0b73d7SPrarit Bhargava } 7237b0b73d7SPrarit Bhargava } while (str_entry); 7247b0b73d7SPrarit Bhargava 7257b0b73d7SPrarit Bhargava return 0; 7267b0b73d7SPrarit Bhargava } 7277b0b73d7SPrarit Bhargava 7287b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn) 7297b0b73d7SPrarit Bhargava { 7307b0b73d7SPrarit Bhargava struct list_head *tmp; 7317b0b73d7SPrarit Bhargava struct blacklist_entry *entry; 7327b0b73d7SPrarit Bhargava char *fn_name; 7337b0b73d7SPrarit Bhargava 7347b0b73d7SPrarit Bhargava fn_name = kasprintf(GFP_KERNEL, "%pf", fn); 7357b0b73d7SPrarit Bhargava if (!fn_name) 7367b0b73d7SPrarit Bhargava return false; 7377b0b73d7SPrarit Bhargava 7387b0b73d7SPrarit Bhargava list_for_each(tmp, &blacklisted_initcalls) { 7397b0b73d7SPrarit Bhargava entry = list_entry(tmp, struct blacklist_entry, next); 7407b0b73d7SPrarit Bhargava if (!strcmp(fn_name, entry->buf)) { 7417b0b73d7SPrarit Bhargava pr_debug("initcall %s blacklisted\n", fn_name); 7427b0b73d7SPrarit Bhargava kfree(fn_name); 7437b0b73d7SPrarit Bhargava return true; 7447b0b73d7SPrarit Bhargava } 7457b0b73d7SPrarit Bhargava } 7467b0b73d7SPrarit Bhargava 7477b0b73d7SPrarit Bhargava kfree(fn_name); 7487b0b73d7SPrarit Bhargava return false; 7497b0b73d7SPrarit Bhargava } 7507b0b73d7SPrarit Bhargava #else 7517b0b73d7SPrarit Bhargava static int __init initcall_blacklist(char *str) 7527b0b73d7SPrarit Bhargava { 7537b0b73d7SPrarit Bhargava pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n"); 7547b0b73d7SPrarit Bhargava return 0; 7557b0b73d7SPrarit Bhargava } 7567b0b73d7SPrarit Bhargava 7577b0b73d7SPrarit Bhargava static bool __init_or_module initcall_blacklisted(initcall_t fn) 7587b0b73d7SPrarit Bhargava { 7597b0b73d7SPrarit Bhargava return false; 7607b0b73d7SPrarit Bhargava } 7617b0b73d7SPrarit Bhargava #endif 7627b0b73d7SPrarit Bhargava __setup("initcall_blacklist=", initcall_blacklist); 7637b0b73d7SPrarit Bhargava 764e4461271SKevin Winchester static int __init_or_module do_one_initcall_debug(initcall_t fn) 7651da177e4SLinus Torvalds { 76674239072SFrederic Weisbecker ktime_t calltime, delta, rettime; 76730dbb20eSAmérico Wang unsigned long long duration; 76830dbb20eSAmérico Wang int ret; 7691da177e4SLinus Torvalds 770d62cf815SAndrew Morton printk(KERN_DEBUG "calling %pF @ %i\n", fn, task_pid_nr(current)); 77174239072SFrederic Weisbecker calltime = ktime_get(); 77230dbb20eSAmérico Wang ret = fn(); 77374239072SFrederic Weisbecker rettime = ktime_get(); 77474239072SFrederic Weisbecker delta = ktime_sub(rettime, calltime); 77530dbb20eSAmérico Wang duration = (unsigned long long) ktime_to_ns(delta) >> 10; 776d62cf815SAndrew Morton printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n", 777ea676e84SAndrew Morton fn, ret, duration); 77822c5c03bSKevin Winchester 77922c5c03bSKevin Winchester return ret; 7808f0c45cdSIngo Molnar } 7818f0c45cdSIngo Molnar 782e4461271SKevin Winchester int __init_or_module do_one_initcall(initcall_t fn) 78322c5c03bSKevin Winchester { 78422c5c03bSKevin Winchester int count = preempt_count(); 78522c5c03bSKevin Winchester int ret; 786ff1c8facSSteven Rostedt char msgbuf[64]; 78722c5c03bSKevin Winchester 7887b0b73d7SPrarit Bhargava if (initcall_blacklisted(fn)) 7897b0b73d7SPrarit Bhargava return -EPERM; 7907b0b73d7SPrarit Bhargava 79122c5c03bSKevin Winchester if (initcall_debug) 79222c5c03bSKevin Winchester ret = do_one_initcall_debug(fn); 79322c5c03bSKevin Winchester else 79422c5c03bSKevin Winchester ret = fn(); 79522c5c03bSKevin Winchester 796e662e1cfSCyrill Gorcunov msgbuf[0] = 0; 797e662e1cfSCyrill Gorcunov 7981da177e4SLinus Torvalds if (preempt_count() != count) { 799bf5d770bSSteven Rostedt sprintf(msgbuf, "preemption imbalance "); 8004a2b4b22SPeter Zijlstra preempt_count_set(count); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds if (irqs_disabled()) { 803a76bfd0dSCyrill Gorcunov strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf)); 8041da177e4SLinus Torvalds local_irq_enable(); 8051da177e4SLinus Torvalds } 806f91eb62fSSteven Rostedt WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf); 80759f9415fSArjan van de Ven 80830dbb20eSAmérico Wang return ret; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 811e0df154fSLinus Torvalds 812026cee00SPawel Moll extern initcall_t __initcall_start[]; 813026cee00SPawel Moll extern initcall_t __initcall0_start[]; 814026cee00SPawel Moll extern initcall_t __initcall1_start[]; 815026cee00SPawel Moll extern initcall_t __initcall2_start[]; 816026cee00SPawel Moll extern initcall_t __initcall3_start[]; 817026cee00SPawel Moll extern initcall_t __initcall4_start[]; 818026cee00SPawel Moll extern initcall_t __initcall5_start[]; 819026cee00SPawel Moll extern initcall_t __initcall6_start[]; 820026cee00SPawel Moll extern initcall_t __initcall7_start[]; 821026cee00SPawel Moll extern initcall_t __initcall_end[]; 822026cee00SPawel Moll 823026cee00SPawel Moll static initcall_t *initcall_levels[] __initdata = { 824026cee00SPawel Moll __initcall0_start, 825026cee00SPawel Moll __initcall1_start, 826026cee00SPawel Moll __initcall2_start, 827026cee00SPawel Moll __initcall3_start, 828026cee00SPawel Moll __initcall4_start, 829026cee00SPawel Moll __initcall5_start, 830026cee00SPawel Moll __initcall6_start, 831026cee00SPawel Moll __initcall7_start, 832026cee00SPawel Moll __initcall_end, 833026cee00SPawel Moll }; 834026cee00SPawel Moll 83596263d28SJim Cromie /* Keep these in sync with initcalls in include/linux/init.h */ 836026cee00SPawel Moll static char *initcall_level_names[] __initdata = { 8379fb48c74SJim Cromie "early", 8389fb48c74SJim Cromie "core", 8399fb48c74SJim Cromie "postcore", 8409fb48c74SJim Cromie "arch", 8419fb48c74SJim Cromie "subsys", 8429fb48c74SJim Cromie "fs", 8439fb48c74SJim Cromie "device", 8449fb48c74SJim Cromie "late", 845026cee00SPawel Moll }; 846026cee00SPawel Moll 847026cee00SPawel Moll static void __init do_initcall_level(int level) 848026cee00SPawel Moll { 849026cee00SPawel Moll initcall_t *fn; 850026cee00SPawel Moll 85108746a65SKrzysztof Mazur strcpy(initcall_command_line, saved_command_line); 852026cee00SPawel Moll parse_args(initcall_level_names[level], 85308746a65SKrzysztof Mazur initcall_command_line, __start___param, 854026cee00SPawel Moll __stop___param - __start___param, 855026cee00SPawel Moll level, level, 856ecc86170SLuis R. Rodriguez NULL, &repair_env_string); 857026cee00SPawel Moll 858026cee00SPawel Moll for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++) 859026cee00SPawel Moll do_one_initcall(*fn); 860026cee00SPawel Moll } 861e0df154fSLinus Torvalds 862e0df154fSLinus Torvalds static void __init do_initcalls(void) 863e0df154fSLinus Torvalds { 864026cee00SPawel Moll int level; 865e0df154fSLinus Torvalds 86619efb72fSBorislav Petkov for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) 867026cee00SPawel Moll do_initcall_level(level); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* 8711da177e4SLinus Torvalds * Ok, the machine is now initialized. None of the devices 8721da177e4SLinus Torvalds * have been touched yet, but the CPU subsystem is up and 8731da177e4SLinus Torvalds * running, and memory and process management works. 8741da177e4SLinus Torvalds * 8751da177e4SLinus Torvalds * Now we can finally start doing some real work.. 8761da177e4SLinus Torvalds */ 8771da177e4SLinus Torvalds static void __init do_basic_setup(void) 8781da177e4SLinus Torvalds { 879759ee091SLai Jiangshan cpuset_init_smp(); 88041ffe5d5SHugh Dickins shmem_init(); 8811da177e4SLinus Torvalds driver_init(); 882b04c3afbSEric W. Biederman init_irq_proc(); 883b99b87f7SPeter Oberparleiter do_ctors(); 884d5767c53SLinus Torvalds usermodehelper_enable(); 885b0f84374Swangyanqing do_initcalls(); 88647d06e53STheodore Ts'o random_int_secret_init(); 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8897babe8dbSEduard - Gabriel Munteanu static void __init do_pre_smp_initcalls(void) 890c2147a50SEduard - Gabriel Munteanu { 891196a15b4SH Hartley Sweeten initcall_t *fn; 892c2147a50SEduard - Gabriel Munteanu 893026cee00SPawel Moll for (fn = __initcall_start; fn < __initcall0_start; fn++) 894196a15b4SH Hartley Sweeten do_one_initcall(*fn); 895c2147a50SEduard - Gabriel Munteanu } 896c2147a50SEduard - Gabriel Munteanu 897bb813f4cSTejun Heo /* 898bb813f4cSTejun Heo * This function requests modules which should be loaded by default and is 899bb813f4cSTejun Heo * called twice right after initrd is mounted and right before init is 900bb813f4cSTejun Heo * exec'd. If such modules are on either initrd or rootfs, they will be 901bb813f4cSTejun Heo * loaded before control is passed to userland. 902bb813f4cSTejun Heo */ 903bb813f4cSTejun Heo void __init load_default_modules(void) 904bb813f4cSTejun Heo { 905bb813f4cSTejun Heo load_default_elevator_module(); 906bb813f4cSTejun Heo } 907bb813f4cSTejun Heo 908a74fb73cSAl Viro static int run_init_process(const char *init_filename) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds argv_init[0] = init_filename; 911c4ad8f98SLinus Torvalds return do_execve(getname_kernel(init_filename), 912ae903caaSAl Viro (const char __user *const __user *)argv_init, 913ae903caaSAl Viro (const char __user *const __user *)envp_init); 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 916ba24762bSMichael Opdenacker static int try_to_run_init_process(const char *init_filename) 917ba24762bSMichael Opdenacker { 918ba24762bSMichael Opdenacker int ret; 919ba24762bSMichael Opdenacker 920ba24762bSMichael Opdenacker ret = run_init_process(init_filename); 921ba24762bSMichael Opdenacker 922ba24762bSMichael Opdenacker if (ret && ret != -ENOENT) { 923ba24762bSMichael Opdenacker pr_err("Starting init: %s exists but couldn't execute it (error %d)\n", 924ba24762bSMichael Opdenacker init_filename, ret); 925ba24762bSMichael Opdenacker } 926ba24762bSMichael Opdenacker 927ba24762bSMichael Opdenacker return ret; 928ba24762bSMichael Opdenacker } 929ba24762bSMichael Opdenacker 930f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void); 931d6b21238SAl Viro 932d6b21238SAl Viro static int __ref kernel_init(void *unused) 933ee5bfa64SVivek Goyal { 934ba24762bSMichael Opdenacker int ret; 935ba24762bSMichael Opdenacker 936d6b21238SAl Viro kernel_init_freeable(); 93722a9d645SArjan van de Ven /* need to finish all async __init code before freeing the memory */ 93822a9d645SArjan van de Ven async_synchronize_full(); 939ee5bfa64SVivek Goyal free_initmem(); 940ee5bfa64SVivek Goyal mark_rodata_ro(); 941ee5bfa64SVivek Goyal system_state = SYSTEM_RUNNING; 942ee5bfa64SVivek Goyal numa_default_policy(); 943ee5bfa64SVivek Goyal 9444a9d4b02SAl Viro flush_delayed_fput(); 945fae5fa44SOleg Nesterov 946ee5bfa64SVivek Goyal if (ramdisk_execute_command) { 947ba24762bSMichael Opdenacker ret = run_init_process(ramdisk_execute_command); 948ba24762bSMichael Opdenacker if (!ret) 949a74fb73cSAl Viro return 0; 950ba24762bSMichael Opdenacker pr_err("Failed to execute %s (error %d)\n", 951ba24762bSMichael Opdenacker ramdisk_execute_command, ret); 952ee5bfa64SVivek Goyal } 953ee5bfa64SVivek Goyal 954ee5bfa64SVivek Goyal /* 955ee5bfa64SVivek Goyal * We try each of these until one succeeds. 956ee5bfa64SVivek Goyal * 957ee5bfa64SVivek Goyal * The Bourne shell can be used instead of init if we are 958ee5bfa64SVivek Goyal * trying to recover a really broken machine. 959ee5bfa64SVivek Goyal */ 960ee5bfa64SVivek Goyal if (execute_command) { 961ba24762bSMichael Opdenacker ret = run_init_process(execute_command); 962ba24762bSMichael Opdenacker if (!ret) 963a74fb73cSAl Viro return 0; 9646ef4536eSAndy Lutomirski panic("Requested init %s failed (error %d).", 9656ef4536eSAndy Lutomirski execute_command, ret); 966ee5bfa64SVivek Goyal } 967ba24762bSMichael Opdenacker if (!try_to_run_init_process("/sbin/init") || 968ba24762bSMichael Opdenacker !try_to_run_init_process("/etc/init") || 969ba24762bSMichael Opdenacker !try_to_run_init_process("/bin/init") || 970ba24762bSMichael Opdenacker !try_to_run_init_process("/bin/sh")) 971a74fb73cSAl Viro return 0; 972ee5bfa64SVivek Goyal 973ba24762bSMichael Opdenacker panic("No working init found. Try passing init= option to kernel. " 9749a85b8d6SAndreas Mohr "See Linux Documentation/init.txt for guidance."); 975ee5bfa64SVivek Goyal } 976ee5bfa64SVivek Goyal 977f80b0c90SVineet Gupta static noinline void __init kernel_init_freeable(void) 9781da177e4SLinus Torvalds { 979b433c3d4SPeter Zijlstra /* 980b433c3d4SPeter Zijlstra * Wait until kthreadd is all set-up. 981b433c3d4SPeter Zijlstra */ 982b433c3d4SPeter Zijlstra wait_for_completion(&kthreadd_done); 98331a67102SLinus Torvalds 98431a67102SLinus Torvalds /* Now the scheduler is fully set up and can do blocking allocations */ 98531a67102SLinus Torvalds gfp_allowed_mask = __GFP_BITS_MASK; 98631a67102SLinus Torvalds 98758568d2aSMiao Xie /* 98858568d2aSMiao Xie * init can allocate pages on any node 98958568d2aSMiao Xie */ 9903c466d46SLai Jiangshan set_mems_allowed(node_states[N_MEMORY]); 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * init can run on any cpu. 9931da177e4SLinus Torvalds */ 9941a2142afSRusty Russell set_cpus_allowed_ptr(current, cpu_all_mask); 9951da177e4SLinus Torvalds 9969ec52099SCedric Le Goater cad_pid = task_pid(current); 9979ec52099SCedric Le Goater 998ca74a6f8SAndi Kleen smp_prepare_cpus(setup_max_cpus); 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds do_pre_smp_initcalls(); 1001004417a6SPeter Zijlstra lockup_detector_init(); 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds smp_init(); 10041da177e4SLinus Torvalds sched_init_smp(); 10051da177e4SLinus Torvalds 10060e1cc95bSMel Gorman page_alloc_init_late(); 10070e1cc95bSMel Gorman 10081da177e4SLinus Torvalds do_basic_setup(); 10091da177e4SLinus Torvalds 10102bd3a997SEric W. Biederman /* Open the /dev/console on the rootfs, this should never fail */ 10112bd3a997SEric W. Biederman if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0) 1012ea676e84SAndrew Morton pr_err("Warning: unable to open an initial console.\n"); 10132bd3a997SEric W. Biederman 10142bd3a997SEric W. Biederman (void) sys_dup(0); 10152bd3a997SEric W. Biederman (void) sys_dup(0); 10161da177e4SLinus Torvalds /* 10171da177e4SLinus Torvalds * check if there is an early userspace init. If yes, let it do all 10181da177e4SLinus Torvalds * the work 10191da177e4SLinus Torvalds */ 1020ffdfc409SOlof Johansson 1021ffdfc409SOlof Johansson if (!ramdisk_execute_command) 1022ffdfc409SOlof Johansson ramdisk_execute_command = "/init"; 1023ffdfc409SOlof Johansson 1024ffdfc409SOlof Johansson if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) { 1025ffdfc409SOlof Johansson ramdisk_execute_command = NULL; 10261da177e4SLinus Torvalds prepare_namespace(); 1027ffdfc409SOlof Johansson } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds /* 10301da177e4SLinus Torvalds * Ok, we have completed the initial bootup, and 10311da177e4SLinus Torvalds * we're essentially up and running. Get rid of the 10321da177e4SLinus Torvalds * initmem segments and start the user-mode stuff.. 1033c9cd2ce2SDmitry Kasatkin * 1034c9cd2ce2SDmitry Kasatkin * rootfs is available now, try loading the public keys 1035c9cd2ce2SDmitry Kasatkin * and default modules 10361da177e4SLinus Torvalds */ 1037bb813f4cSTejun Heo 1038c9cd2ce2SDmitry Kasatkin integrity_load_keys(); 1039bb813f4cSTejun Heo load_default_modules(); 10401da177e4SLinus Torvalds } 1041