Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/init/main.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  *
0006  *  GK 2/5/95  -  Changed to support mounting root fs via NFS
0007  *  Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
0008  *  Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
0009  *  Simplified starting of init:  Michael A. Griffith <grif@acm.org>
0010  */
0011 
0012 #define DEBUG       /* Enable initcall_debug */
0013 
0014 #include <linux/types.h>
0015 #include <linux/module.h>
0016 #include <linux/proc_fs.h>
0017 #include <linux/kernel.h>
0018 #include <linux/syscalls.h>
0019 #include <linux/stackprotector.h>
0020 #include <linux/string.h>
0021 #include <linux/ctype.h>
0022 #include <linux/delay.h>
0023 #include <linux/ioport.h>
0024 #include <linux/init.h>
0025 #include <linux/initrd.h>
0026 #include <linux/bootmem.h>
0027 #include <linux/acpi.h>
0028 #include <linux/tty.h>
0029 #include <linux/percpu.h>
0030 #include <linux/kmod.h>
0031 #include <linux/vmalloc.h>
0032 #include <linux/kernel_stat.h>
0033 #include <linux/start_kernel.h>
0034 #include <linux/security.h>
0035 #include <linux/smp.h>
0036 #include <linux/profile.h>
0037 #include <linux/rcupdate.h>
0038 #include <linux/moduleparam.h>
0039 #include <linux/kallsyms.h>
0040 #include <linux/writeback.h>
0041 #include <linux/cpu.h>
0042 #include <linux/cpuset.h>
0043 #include <linux/cgroup.h>
0044 #include <linux/efi.h>
0045 #include <linux/tick.h>
0046 #include <linux/interrupt.h>
0047 #include <linux/taskstats_kern.h>
0048 #include <linux/delayacct.h>
0049 #include <linux/unistd.h>
0050 #include <linux/rmap.h>
0051 #include <linux/mempolicy.h>
0052 #include <linux/key.h>
0053 #include <linux/buffer_head.h>
0054 #include <linux/page_ext.h>
0055 #include <linux/debug_locks.h>
0056 #include <linux/debugobjects.h>
0057 #include <linux/lockdep.h>
0058 #include <linux/kmemleak.h>
0059 #include <linux/pid_namespace.h>
0060 #include <linux/device.h>
0061 #include <linux/kthread.h>
0062 #include <linux/sched.h>
0063 #include <linux/signal.h>
0064 #include <linux/idr.h>
0065 #include <linux/kgdb.h>
0066 #include <linux/ftrace.h>
0067 #include <linux/async.h>
0068 #include <linux/kmemcheck.h>
0069 #include <linux/sfi.h>
0070 #include <linux/shmem_fs.h>
0071 #include <linux/slab.h>
0072 #include <linux/perf_event.h>
0073 #include <linux/file.h>
0074 #include <linux/ptrace.h>
0075 #include <linux/blkdev.h>
0076 #include <linux/elevator.h>
0077 #include <linux/sched_clock.h>
0078 #include <linux/context_tracking.h>
0079 #include <linux/random.h>
0080 #include <linux/list.h>
0081 #include <linux/integrity.h>
0082 #include <linux/proc_ns.h>
0083 #include <linux/io.h>
0084 #include <linux/cache.h>
0085 
0086 #include <asm/io.h>
0087 #include <asm/bugs.h>
0088 #include <asm/setup.h>
0089 #include <asm/sections.h>
0090 #include <asm/cacheflush.h>
0091 
0092 static int kernel_init(void *);
0093 
0094 extern void init_IRQ(void);
0095 extern void fork_init(void);
0096 extern void radix_tree_init(void);
0097 
0098 /*
0099  * Debug helper: via this flag we know that we are in 'early bootup code'
0100  * where only the boot processor is running with IRQ disabled.  This means
0101  * two things - IRQ must not be enabled before the flag is cleared and some
0102  * operations which are not allowed with IRQ disabled are allowed while the
0103  * flag is set.
0104  */
0105 bool early_boot_irqs_disabled __read_mostly;
0106 
0107 enum system_states system_state __read_mostly;
0108 EXPORT_SYMBOL(system_state);
0109 
0110 /*
0111  * Boot command-line arguments
0112  */
0113 #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
0114 #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
0115 
0116 extern void time_init(void);
0117 /* Default late time init is NULL. archs can override this later. */
0118 void (*__initdata late_time_init)(void);
0119 
0120 /* Untouched command line saved by arch-specific code. */
0121 char __initdata boot_command_line[COMMAND_LINE_SIZE];
0122 /* Untouched saved command line (eg. for /proc) */
0123 char *saved_command_line;
0124 /* Command line for parameter parsing */
0125 static char *static_command_line;
0126 /* Command line for per-initcall parameter parsing */
0127 static char *initcall_command_line;
0128 
0129 static char *execute_command;
0130 static char *ramdisk_execute_command;
0131 
0132 /*
0133  * Used to generate warnings if static_key manipulation functions are used
0134  * before jump_label_init is called.
0135  */
0136 bool static_key_initialized __read_mostly;
0137 EXPORT_SYMBOL_GPL(static_key_initialized);
0138 
0139 /*
0140  * If set, this is an indication to the drivers that reset the underlying
0141  * device before going ahead with the initialization otherwise driver might
0142  * rely on the BIOS and skip the reset operation.
0143  *
0144  * This is useful if kernel is booting in an unreliable environment.
0145  * For ex. kdump situation where previous kernel has crashed, BIOS has been
0146  * skipped and devices will be in unknown state.
0147  */
0148 unsigned int reset_devices;
0149 EXPORT_SYMBOL(reset_devices);
0150 
0151 static int __init set_reset_devices(char *str)
0152 {
0153     reset_devices = 1;
0154     return 1;
0155 }
0156 
0157 __setup("reset_devices", set_reset_devices);
0158 
0159 static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
0160 const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
0161 static const char *panic_later, *panic_param;
0162 
0163 extern const struct obs_kernel_param __setup_start[], __setup_end[];
0164 
0165 static bool __init obsolete_checksetup(char *line)
0166 {
0167     const struct obs_kernel_param *p;
0168     bool had_early_param = false;
0169 
0170     p = __setup_start;
0171     do {
0172         int n = strlen(p->str);
0173         if (parameqn(line, p->str, n)) {
0174             if (p->early) {
0175                 /* Already done in parse_early_param?
0176                  * (Needs exact match on param part).
0177                  * Keep iterating, as we can have early
0178                  * params and __setups of same names 8( */
0179                 if (line[n] == '\0' || line[n] == '=')
0180                     had_early_param = true;
0181             } else if (!p->setup_func) {
0182                 pr_warn("Parameter %s is obsolete, ignored\n",
0183                     p->str);
0184                 return true;
0185             } else if (p->setup_func(line + n))
0186                 return true;
0187         }
0188         p++;
0189     } while (p < __setup_end);
0190 
0191     return had_early_param;
0192 }
0193 
0194 /*
0195  * This should be approx 2 Bo*oMips to start (note initial shift), and will
0196  * still work even if initially too large, it will just take slightly longer
0197  */
0198 unsigned long loops_per_jiffy = (1<<12);
0199 EXPORT_SYMBOL(loops_per_jiffy);
0200 
0201 static int __init debug_kernel(char *str)
0202 {
0203     console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
0204     return 0;
0205 }
0206 
0207 static int __init quiet_kernel(char *str)
0208 {
0209     console_loglevel = CONSOLE_LOGLEVEL_QUIET;
0210     return 0;
0211 }
0212 
0213 early_param("debug", debug_kernel);
0214 early_param("quiet", quiet_kernel);
0215 
0216 static int __init loglevel(char *str)
0217 {
0218     int newlevel;
0219 
0220     /*
0221      * Only update loglevel value when a correct setting was passed,
0222      * to prevent blind crashes (when loglevel being set to 0) that
0223      * are quite hard to debug
0224      */
0225     if (get_option(&str, &newlevel)) {
0226         console_loglevel = newlevel;
0227         return 0;
0228     }
0229 
0230     return -EINVAL;
0231 }
0232 
0233 early_param("loglevel", loglevel);
0234 
0235 /* Change NUL term back to "=", to make "param" the whole string. */
0236 static int __init repair_env_string(char *param, char *val,
0237                     const char *unused, void *arg)
0238 {
0239     if (val) {
0240         /* param=val or param="val"? */
0241         if (val == param+strlen(param)+1)
0242             val[-1] = '=';
0243         else if (val == param+strlen(param)+2) {
0244             val[-2] = '=';
0245             memmove(val-1, val, strlen(val)+1);
0246             val--;
0247         } else
0248             BUG();
0249     }
0250     return 0;
0251 }
0252 
0253 /* Anything after -- gets handed straight to init. */
0254 static int __init set_init_arg(char *param, char *val,
0255                    const char *unused, void *arg)
0256 {
0257     unsigned int i;
0258 
0259     if (panic_later)
0260         return 0;
0261 
0262     repair_env_string(param, val, unused, NULL);
0263 
0264     for (i = 0; argv_init[i]; i++) {
0265         if (i == MAX_INIT_ARGS) {
0266             panic_later = "init";
0267             panic_param = param;
0268             return 0;
0269         }
0270     }
0271     argv_init[i] = param;
0272     return 0;
0273 }
0274 
0275 /*
0276  * Unknown boot options get handed to init, unless they look like
0277  * unused parameters (modprobe will find them in /proc/cmdline).
0278  */
0279 static int __init unknown_bootoption(char *param, char *val,
0280                      const char *unused, void *arg)
0281 {
0282     repair_env_string(param, val, unused, NULL);
0283 
0284     /* Handle obsolete-style parameters */
0285     if (obsolete_checksetup(param))
0286         return 0;
0287 
0288     /* Unused module parameter. */
0289     if (strchr(param, '.') && (!val || strchr(param, '.') < val))
0290         return 0;
0291 
0292     if (panic_later)
0293         return 0;
0294 
0295     if (val) {
0296         /* Environment option */
0297         unsigned int i;
0298         for (i = 0; envp_init[i]; i++) {
0299             if (i == MAX_INIT_ENVS) {
0300                 panic_later = "env";
0301                 panic_param = param;
0302             }
0303             if (!strncmp(param, envp_init[i], val - param))
0304                 break;
0305         }
0306         envp_init[i] = param;
0307     } else {
0308         /* Command line option */
0309         unsigned int i;
0310         for (i = 0; argv_init[i]; i++) {
0311             if (i == MAX_INIT_ARGS) {
0312                 panic_later = "init";
0313                 panic_param = param;
0314             }
0315         }
0316         argv_init[i] = param;
0317     }
0318     return 0;
0319 }
0320 
0321 static int __init init_setup(char *str)
0322 {
0323     unsigned int i;
0324 
0325     execute_command = str;
0326     /*
0327      * In case LILO is going to boot us with default command line,
0328      * it prepends "auto" before the whole cmdline which makes
0329      * the shell think it should execute a script with such name.
0330      * So we ignore all arguments entered _before_ init=... [MJ]
0331      */
0332     for (i = 1; i < MAX_INIT_ARGS; i++)
0333         argv_init[i] = NULL;
0334     return 1;
0335 }
0336 __setup("init=", init_setup);
0337 
0338 static int __init rdinit_setup(char *str)
0339 {
0340     unsigned int i;
0341 
0342     ramdisk_execute_command = str;
0343     /* See "auto" comment in init_setup */
0344     for (i = 1; i < MAX_INIT_ARGS; i++)
0345         argv_init[i] = NULL;
0346     return 1;
0347 }
0348 __setup("rdinit=", rdinit_setup);
0349 
0350 #ifndef CONFIG_SMP
0351 static const unsigned int setup_max_cpus = NR_CPUS;
0352 static inline void setup_nr_cpu_ids(void) { }
0353 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
0354 #endif
0355 
0356 /*
0357  * We need to store the untouched command line for future reference.
0358  * We also need to store the touched command line since the parameter
0359  * parsing is performed in place, and we should allow a component to
0360  * store reference of name/value for future reference.
0361  */
0362 static void __init setup_command_line(char *command_line)
0363 {
0364     saved_command_line =
0365         memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
0366     initcall_command_line =
0367         memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
0368     static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
0369     strcpy(saved_command_line, boot_command_line);
0370     strcpy(static_command_line, command_line);
0371 }
0372 
0373 /*
0374  * We need to finalize in a non-__init function or else race conditions
0375  * between the root thread and the init thread may cause start_kernel to
0376  * be reaped by free_initmem before the root thread has proceeded to
0377  * cpu_idle.
0378  *
0379  * gcc-3.4 accidentally inlines this function, so use noinline.
0380  */
0381 
0382 static __initdata DECLARE_COMPLETION(kthreadd_done);
0383 
0384 static noinline void __ref rest_init(void)
0385 {
0386     int pid;
0387 
0388     rcu_scheduler_starting();
0389     /*
0390      * We need to spawn init first so that it obtains pid 1, however
0391      * the init task will end up wanting to create kthreads, which, if
0392      * we schedule it before we create kthreadd, will OOPS.
0393      */
0394     kernel_thread(kernel_init, NULL, CLONE_FS);
0395     numa_default_policy();
0396     pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
0397     rcu_read_lock();
0398     kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
0399     rcu_read_unlock();
0400     complete(&kthreadd_done);
0401 
0402     /*
0403      * The boot idle thread must execute schedule()
0404      * at least once to get things moving:
0405      */
0406     init_idle_bootup_task(current);
0407     schedule_preempt_disabled();
0408     /* Call into cpu_idle with preempt disabled */
0409     cpu_startup_entry(CPUHP_ONLINE);
0410 }
0411 
0412 /* Check for early params. */
0413 static int __init do_early_param(char *param, char *val,
0414                  const char *unused, void *arg)
0415 {
0416     const struct obs_kernel_param *p;
0417 
0418     for (p = __setup_start; p < __setup_end; p++) {
0419         if ((p->early && parameq(param, p->str)) ||
0420             (strcmp(param, "console") == 0 &&
0421              strcmp(p->str, "earlycon") == 0)
0422         ) {
0423             if (p->setup_func(val) != 0)
0424                 pr_warn("Malformed early option '%s'\n", param);
0425         }
0426     }
0427     /* We accept everything at this stage. */
0428     return 0;
0429 }
0430 
0431 void __init parse_early_options(char *cmdline)
0432 {
0433     parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
0434            do_early_param);
0435 }
0436 
0437 /* Arch code calls this early on, or if not, just before other parsing. */
0438 void __init parse_early_param(void)
0439 {
0440     static int done __initdata;
0441     static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
0442 
0443     if (done)
0444         return;
0445 
0446     /* All fall through to do_early_param. */
0447     strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
0448     parse_early_options(tmp_cmdline);
0449     done = 1;
0450 }
0451 
0452 void __init __weak arch_post_acpi_subsys_init(void) { }
0453 
0454 void __init __weak smp_setup_processor_id(void)
0455 {
0456 }
0457 
0458 # if THREAD_SIZE >= PAGE_SIZE
0459 void __init __weak thread_stack_cache_init(void)
0460 {
0461 }
0462 #endif
0463 
0464 /*
0465  * Set up kernel memory allocators
0466  */
0467 static void __init mm_init(void)
0468 {
0469     /*
0470      * page_ext requires contiguous pages,
0471      * bigger than MAX_ORDER unless SPARSEMEM.
0472      */
0473     page_ext_init_flatmem();
0474     mem_init();
0475     kmem_cache_init();
0476     percpu_init_late();
0477     pgtable_init();
0478     vmalloc_init();
0479     ioremap_huge_init();
0480 }
0481 
0482 asmlinkage __visible void __init start_kernel(void)
0483 {
0484     char *command_line;
0485     char *after_dashes;
0486 
0487     set_task_stack_end_magic(&init_task);
0488     smp_setup_processor_id();
0489     debug_objects_early_init();
0490 
0491     /*
0492      * Set up the the initial canary ASAP:
0493      */
0494     boot_init_stack_canary();
0495 
0496     cgroup_init_early();
0497 
0498     local_irq_disable();
0499     early_boot_irqs_disabled = true;
0500 
0501 /*
0502  * Interrupts are still disabled. Do necessary setups, then
0503  * enable them
0504  */
0505     boot_cpu_init();
0506     page_address_init();
0507     pr_notice("%s", linux_banner);
0508     setup_arch(&command_line);
0509     mm_init_cpumask(&init_mm);
0510     setup_command_line(command_line);
0511     setup_nr_cpu_ids();
0512     setup_per_cpu_areas();
0513     boot_cpu_state_init();
0514     smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
0515 
0516     build_all_zonelists(NULL, NULL);
0517     page_alloc_init();
0518 
0519     pr_notice("Kernel command line: %s\n", boot_command_line);
0520     parse_early_param();
0521     after_dashes = parse_args("Booting kernel",
0522                   static_command_line, __start___param,
0523                   __stop___param - __start___param,
0524                   -1, -1, NULL, &unknown_bootoption);
0525     if (!IS_ERR_OR_NULL(after_dashes))
0526         parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
0527                NULL, set_init_arg);
0528 
0529     jump_label_init();
0530 
0531     /*
0532      * These use large bootmem allocations and must precede
0533      * kmem_cache_init()
0534      */
0535     setup_log_buf(0);
0536     pidhash_init();
0537     vfs_caches_init_early();
0538     sort_main_extable();
0539     trap_init();
0540     mm_init();
0541 
0542     /*
0543      * Set up the scheduler prior starting any interrupts (such as the
0544      * timer interrupt). Full topology setup happens at smp_init()
0545      * time - but meanwhile we still have a functioning scheduler.
0546      */
0547     sched_init();
0548     /*
0549      * Disable preemption - early bootup scheduling is extremely
0550      * fragile until we cpu_idle() for the first time.
0551      */
0552     preempt_disable();
0553     if (WARN(!irqs_disabled(),
0554          "Interrupts were enabled *very* early, fixing it\n"))
0555         local_irq_disable();
0556     idr_init_cache();
0557 
0558     /*
0559      * Allow workqueue creation and work item queueing/cancelling
0560      * early.  Work item execution depends on kthreads and starts after
0561      * workqueue_init().
0562      */
0563     workqueue_init_early();
0564 
0565     rcu_init();
0566 
0567     /* trace_printk() and trace points may be used after this */
0568     trace_init();
0569 
0570     context_tracking_init();
0571     radix_tree_init();
0572     /* init some links before init_ISA_irqs() */
0573     early_irq_init();
0574     init_IRQ();
0575     tick_init();
0576     rcu_init_nohz();
0577     init_timers();
0578     hrtimers_init();
0579     softirq_init();
0580     timekeeping_init();
0581     time_init();
0582     sched_clock_postinit();
0583     printk_nmi_init();
0584     perf_event_init();
0585     profile_init();
0586     call_function_init();
0587     WARN(!irqs_disabled(), "Interrupts were enabled early\n");
0588     early_boot_irqs_disabled = false;
0589     local_irq_enable();
0590 
0591     kmem_cache_init_late();
0592 
0593     /*
0594      * HACK ALERT! This is early. We're enabling the console before
0595      * we've done PCI setups etc, and console_init() must be aware of
0596      * this. But we do want output early, in case something goes wrong.
0597      */
0598     console_init();
0599     if (panic_later)
0600         panic("Too many boot %s vars at `%s'", panic_later,
0601               panic_param);
0602 
0603     lockdep_info();
0604 
0605     /*
0606      * Need to run this when irqs are enabled, because it wants
0607      * to self-test [hard/soft]-irqs on/off lock inversion bugs
0608      * too:
0609      */
0610     locking_selftest();
0611 
0612 #ifdef CONFIG_BLK_DEV_INITRD
0613     if (initrd_start && !initrd_below_start_ok &&
0614         page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
0615         pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
0616             page_to_pfn(virt_to_page((void *)initrd_start)),
0617             min_low_pfn);
0618         initrd_start = 0;
0619     }
0620 #endif
0621     page_ext_init();
0622     debug_objects_mem_init();
0623     kmemleak_init();
0624     setup_per_cpu_pageset();
0625     numa_policy_init();
0626     if (late_time_init)
0627         late_time_init();
0628     sched_clock_init();
0629     calibrate_delay();
0630     pidmap_init();
0631     anon_vma_init();
0632     acpi_early_init();
0633 #ifdef CONFIG_X86
0634     if (efi_enabled(EFI_RUNTIME_SERVICES))
0635         efi_enter_virtual_mode();
0636 #endif
0637 #ifdef CONFIG_X86_ESPFIX64
0638     /* Should be run before the first non-init thread is created */
0639     init_espfix_bsp();
0640 #endif
0641     thread_stack_cache_init();
0642     cred_init();
0643     fork_init();
0644     proc_caches_init();
0645     buffer_init();
0646     key_init();
0647     security_init();
0648     dbg_late_init();
0649     vfs_caches_init();
0650     pagecache_init();
0651     signals_init();
0652     proc_root_init();
0653     nsfs_init();
0654     cpuset_init();
0655     cgroup_init();
0656     taskstats_init_early();
0657     delayacct_init();
0658 
0659     check_bugs();
0660 
0661     acpi_subsystem_init();
0662     arch_post_acpi_subsys_init();
0663     sfi_init_late();
0664 
0665     if (efi_enabled(EFI_RUNTIME_SERVICES)) {
0666         efi_late_init();
0667         efi_free_boot_services();
0668     }
0669 
0670     ftrace_init();
0671 
0672     /* Do the rest non-__init'ed, we're now alive */
0673     rest_init();
0674 }
0675 
0676 /* Call all constructor functions linked into the kernel. */
0677 static void __init do_ctors(void)
0678 {
0679 #ifdef CONFIG_CONSTRUCTORS
0680     ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
0681 
0682     for (; fn < (ctor_fn_t *) __ctors_end; fn++)
0683         (*fn)();
0684 #endif
0685 }
0686 
0687 bool initcall_debug;
0688 core_param(initcall_debug, initcall_debug, bool, 0644);
0689 
0690 #ifdef CONFIG_KALLSYMS
0691 struct blacklist_entry {
0692     struct list_head next;
0693     char *buf;
0694 };
0695 
0696 static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
0697 
0698 static int __init initcall_blacklist(char *str)
0699 {
0700     char *str_entry;
0701     struct blacklist_entry *entry;
0702 
0703     /* str argument is a comma-separated list of functions */
0704     do {
0705         str_entry = strsep(&str, ",");
0706         if (str_entry) {
0707             pr_debug("blacklisting initcall %s\n", str_entry);
0708             entry = alloc_bootmem(sizeof(*entry));
0709             entry->buf = alloc_bootmem(strlen(str_entry) + 1);
0710             strcpy(entry->buf, str_entry);
0711             list_add(&entry->next, &blacklisted_initcalls);
0712         }
0713     } while (str_entry);
0714 
0715     return 0;
0716 }
0717 
0718 static bool __init_or_module initcall_blacklisted(initcall_t fn)
0719 {
0720     struct blacklist_entry *entry;
0721     char fn_name[KSYM_SYMBOL_LEN];
0722     unsigned long addr;
0723 
0724     if (list_empty(&blacklisted_initcalls))
0725         return false;
0726 
0727     addr = (unsigned long) dereference_function_descriptor(fn);
0728     sprint_symbol_no_offset(fn_name, addr);
0729 
0730     /*
0731      * fn will be "function_name [module_name]" where [module_name] is not
0732      * displayed for built-in init functions.  Strip off the [module_name].
0733      */
0734     strreplace(fn_name, ' ', '\0');
0735 
0736     list_for_each_entry(entry, &blacklisted_initcalls, next) {
0737         if (!strcmp(fn_name, entry->buf)) {
0738             pr_debug("initcall %s blacklisted\n", fn_name);
0739             return true;
0740         }
0741     }
0742 
0743     return false;
0744 }
0745 #else
0746 static int __init initcall_blacklist(char *str)
0747 {
0748     pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
0749     return 0;
0750 }
0751 
0752 static bool __init_or_module initcall_blacklisted(initcall_t fn)
0753 {
0754     return false;
0755 }
0756 #endif
0757 __setup("initcall_blacklist=", initcall_blacklist);
0758 
0759 static int __init_or_module do_one_initcall_debug(initcall_t fn)
0760 {
0761     ktime_t calltime, delta, rettime;
0762     unsigned long long duration;
0763     int ret;
0764 
0765     printk(KERN_DEBUG "calling  %pF @ %i\n", fn, task_pid_nr(current));
0766     calltime = ktime_get();
0767     ret = fn();
0768     rettime = ktime_get();
0769     delta = ktime_sub(rettime, calltime);
0770     duration = (unsigned long long) ktime_to_ns(delta) >> 10;
0771     printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
0772          fn, ret, duration);
0773 
0774     return ret;
0775 }
0776 
0777 int __init_or_module do_one_initcall(initcall_t fn)
0778 {
0779     int count = preempt_count();
0780     int ret;
0781     char msgbuf[64];
0782 
0783     if (initcall_blacklisted(fn))
0784         return -EPERM;
0785 
0786     if (initcall_debug)
0787         ret = do_one_initcall_debug(fn);
0788     else
0789         ret = fn();
0790 
0791     msgbuf[0] = 0;
0792 
0793     if (preempt_count() != count) {
0794         sprintf(msgbuf, "preemption imbalance ");
0795         preempt_count_set(count);
0796     }
0797     if (irqs_disabled()) {
0798         strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
0799         local_irq_enable();
0800     }
0801     WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
0802 
0803     add_latent_entropy();
0804     return ret;
0805 }
0806 
0807 
0808 extern initcall_t __initcall_start[];
0809 extern initcall_t __initcall0_start[];
0810 extern initcall_t __initcall1_start[];
0811 extern initcall_t __initcall2_start[];
0812 extern initcall_t __initcall3_start[];
0813 extern initcall_t __initcall4_start[];
0814 extern initcall_t __initcall5_start[];
0815 extern initcall_t __initcall6_start[];
0816 extern initcall_t __initcall7_start[];
0817 extern initcall_t __initcall_end[];
0818 
0819 static initcall_t *initcall_levels[] __initdata = {
0820     __initcall0_start,
0821     __initcall1_start,
0822     __initcall2_start,
0823     __initcall3_start,
0824     __initcall4_start,
0825     __initcall5_start,
0826     __initcall6_start,
0827     __initcall7_start,
0828     __initcall_end,
0829 };
0830 
0831 /* Keep these in sync with initcalls in include/linux/init.h */
0832 static char *initcall_level_names[] __initdata = {
0833     "early",
0834     "core",
0835     "postcore",
0836     "arch",
0837     "subsys",
0838     "fs",
0839     "device",
0840     "late",
0841 };
0842 
0843 static void __init do_initcall_level(int level)
0844 {
0845     initcall_t *fn;
0846 
0847     strcpy(initcall_command_line, saved_command_line);
0848     parse_args(initcall_level_names[level],
0849            initcall_command_line, __start___param,
0850            __stop___param - __start___param,
0851            level, level,
0852            NULL, &repair_env_string);
0853 
0854     for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
0855         do_one_initcall(*fn);
0856 }
0857 
0858 static void __init do_initcalls(void)
0859 {
0860     int level;
0861 
0862     for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
0863         do_initcall_level(level);
0864 }
0865 
0866 /*
0867  * Ok, the machine is now initialized. None of the devices
0868  * have been touched yet, but the CPU subsystem is up and
0869  * running, and memory and process management works.
0870  *
0871  * Now we can finally start doing some real work..
0872  */
0873 static void __init do_basic_setup(void)
0874 {
0875     cpuset_init_smp();
0876     shmem_init();
0877     driver_init();
0878     init_irq_proc();
0879     do_ctors();
0880     usermodehelper_enable();
0881     do_initcalls();
0882     random_int_secret_init();
0883 }
0884 
0885 static void __init do_pre_smp_initcalls(void)
0886 {
0887     initcall_t *fn;
0888 
0889     for (fn = __initcall_start; fn < __initcall0_start; fn++)
0890         do_one_initcall(*fn);
0891 }
0892 
0893 /*
0894  * This function requests modules which should be loaded by default and is
0895  * called twice right after initrd is mounted and right before init is
0896  * exec'd.  If such modules are on either initrd or rootfs, they will be
0897  * loaded before control is passed to userland.
0898  */
0899 void __init load_default_modules(void)
0900 {
0901     load_default_elevator_module();
0902 }
0903 
0904 static int run_init_process(const char *init_filename)
0905 {
0906     argv_init[0] = init_filename;
0907     return do_execve(getname_kernel(init_filename),
0908         (const char __user *const __user *)argv_init,
0909         (const char __user *const __user *)envp_init);
0910 }
0911 
0912 static int try_to_run_init_process(const char *init_filename)
0913 {
0914     int ret;
0915 
0916     ret = run_init_process(init_filename);
0917 
0918     if (ret && ret != -ENOENT) {
0919         pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
0920                init_filename, ret);
0921     }
0922 
0923     return ret;
0924 }
0925 
0926 static noinline void __init kernel_init_freeable(void);
0927 
0928 #if defined(CONFIG_DEBUG_RODATA) || defined(CONFIG_DEBUG_SET_MODULE_RONX)
0929 bool rodata_enabled __ro_after_init = true;
0930 static int __init set_debug_rodata(char *str)
0931 {
0932     return strtobool(str, &rodata_enabled);
0933 }
0934 __setup("rodata=", set_debug_rodata);
0935 #endif
0936 
0937 #ifdef CONFIG_DEBUG_RODATA
0938 static void mark_readonly(void)
0939 {
0940     if (rodata_enabled)
0941         mark_rodata_ro();
0942     else
0943         pr_info("Kernel memory protection disabled.\n");
0944 }
0945 #else
0946 static inline void mark_readonly(void)
0947 {
0948     pr_warn("This architecture does not have kernel memory protection.\n");
0949 }
0950 #endif
0951 
0952 static int __ref kernel_init(void *unused)
0953 {
0954     int ret;
0955 
0956     kernel_init_freeable();
0957     /* need to finish all async __init code before freeing the memory */
0958     async_synchronize_full();
0959     free_initmem();
0960     mark_readonly();
0961     system_state = SYSTEM_RUNNING;
0962     numa_default_policy();
0963 
0964     flush_delayed_fput();
0965 
0966     rcu_end_inkernel_boot();
0967 
0968     if (ramdisk_execute_command) {
0969         ret = run_init_process(ramdisk_execute_command);
0970         if (!ret)
0971             return 0;
0972         pr_err("Failed to execute %s (error %d)\n",
0973                ramdisk_execute_command, ret);
0974     }
0975 
0976     /*
0977      * We try each of these until one succeeds.
0978      *
0979      * The Bourne shell can be used instead of init if we are
0980      * trying to recover a really broken machine.
0981      */
0982     if (execute_command) {
0983         ret = run_init_process(execute_command);
0984         if (!ret)
0985             return 0;
0986         panic("Requested init %s failed (error %d).",
0987               execute_command, ret);
0988     }
0989     if (!try_to_run_init_process("/sbin/init") ||
0990         !try_to_run_init_process("/etc/init") ||
0991         !try_to_run_init_process("/bin/init") ||
0992         !try_to_run_init_process("/bin/sh"))
0993         return 0;
0994 
0995     panic("No working init found.  Try passing init= option to kernel. "
0996           "See Linux Documentation/admin-guide/init.rst for guidance.");
0997 }
0998 
0999 static noinline void __init kernel_init_freeable(void)
1000 {
1001     /*
1002      * Wait until kthreadd is all set-up.
1003      */
1004     wait_for_completion(&kthreadd_done);
1005 
1006     /* Now the scheduler is fully set up and can do blocking allocations */
1007     gfp_allowed_mask = __GFP_BITS_MASK;
1008 
1009     /*
1010      * init can allocate pages on any node
1011      */
1012     set_mems_allowed(node_states[N_MEMORY]);
1013     /*
1014      * init can run on any cpu.
1015      */
1016     set_cpus_allowed_ptr(current, cpu_all_mask);
1017 
1018     cad_pid = task_pid(current);
1019 
1020     smp_prepare_cpus(setup_max_cpus);
1021 
1022     workqueue_init();
1023 
1024     do_pre_smp_initcalls();
1025     lockup_detector_init();
1026 
1027     smp_init();
1028     sched_init_smp();
1029 
1030     page_alloc_init_late();
1031 
1032     do_basic_setup();
1033 
1034     /* Open the /dev/console on the rootfs, this should never fail */
1035     if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1036         pr_err("Warning: unable to open an initial console.\n");
1037 
1038     (void) sys_dup(0);
1039     (void) sys_dup(0);
1040     /*
1041      * check if there is an early userspace init.  If yes, let it do all
1042      * the work
1043      */
1044 
1045     if (!ramdisk_execute_command)
1046         ramdisk_execute_command = "/init";
1047 
1048     if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
1049         ramdisk_execute_command = NULL;
1050         prepare_namespace();
1051     }
1052 
1053     /*
1054      * Ok, we have completed the initial bootup, and
1055      * we're essentially up and running. Get rid of the
1056      * initmem segments and start the user-mode stuff..
1057      *
1058      * rootfs is available now, try loading the public keys
1059      * and default modules
1060      */
1061 
1062     integrity_load_keys();
1063     load_default_modules();
1064 }