1 /*
2 * linux/init/main.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * GK 2/5/95 - Changed to support mounting root fs via NFS
7 * Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
8 * Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
9 * Simplified starting of init: Michael A. Griffith <grif@acm.org>
10 */
11
12 #define DEBUG /* Enable initcall_debug */
13
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/proc_fs.h>
17 #include <linux/kernel.h>
18 #include <linux/syscalls.h>
19 #include <linux/stackprotector.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
22 #include <linux/delay.h>
23 #include <linux/ioport.h>
24 #include <linux/init.h>
25 #include <linux/initrd.h>
26 #include <linux/bootmem.h>
27 #include <linux/acpi.h>
28 #include <linux/tty.h>
29 #include <linux/percpu.h>
30 #include <linux/kmod.h>
31 #include <linux/vmalloc.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/start_kernel.h>
34 #include <linux/security.h>
35 #include <linux/smp.h>
36 #include <linux/profile.h>
37 #include <linux/rcupdate.h>
38 #include <linux/moduleparam.h>
39 #include <linux/kallsyms.h>
40 #include <linux/writeback.h>
41 #include <linux/cpu.h>
42 #include <linux/cpuset.h>
43 #include <linux/cgroup.h>
44 #include <linux/efi.h>
45 #include <linux/tick.h>
46 #include <linux/interrupt.h>
47 #include <linux/taskstats_kern.h>
48 #include <linux/delayacct.h>
49 #include <linux/unistd.h>
50 #include <linux/rmap.h>
51 #include <linux/mempolicy.h>
52 #include <linux/key.h>
53 #include <linux/buffer_head.h>
54 #include <linux/page_cgroup.h>
55 #include <linux/debug_locks.h>
56 #include <linux/debugobjects.h>
57 #include <linux/lockdep.h>
58 #include <linux/kmemleak.h>
59 #include <linux/pid_namespace.h>
60 #include <linux/device.h>
61 #include <linux/kthread.h>
62 #include <linux/sched.h>
63 #include <linux/signal.h>
64 #include <linux/idr.h>
65 #include <linux/kgdb.h>
66 #include <linux/ftrace.h>
67 #include <linux/async.h>
68 #include <linux/kmemcheck.h>
69 #include <linux/sfi.h>
70 #include <linux/shmem_fs.h>
71 #include <linux/slab.h>
72 #include <linux/perf_event.h>
73 #include <linux/file.h>
74 #include <linux/ptrace.h>
75 #include <linux/blkdev.h>
76 #include <linux/elevator.h>
77 #include <linux/sched_clock.h>
78 #include <linux/context_tracking.h>
79 #include <linux/random.h>
80 #include <linux/list.h>
81
82 #include <asm/io.h>
83 #include <asm/bugs.h>
84 #include <asm/setup.h>
85 #include <asm/sections.h>
86 #include <asm/cacheflush.h>
87
88 #ifdef CONFIG_X86_LOCAL_APIC
89 #include <asm/smp.h>
90 #endif
91
92 static int kernel_init(void *);
93
94 extern void init_IRQ(void);
95 extern void fork_init(unsigned long);
96 extern void radix_tree_init(void);
97
98 /*
99 * Debug helper: via this flag we know that we are in 'early bootup code'
100 * where only the boot processor is running with IRQ disabled. This means
101 * two things - IRQ must not be enabled before the flag is cleared and some
102 * operations which are not allowed with IRQ disabled are allowed while the
103 * flag is set.
104 */
105 bool early_boot_irqs_disabled __read_mostly;
106
107 enum system_states system_state __read_mostly;
108 EXPORT_SYMBOL(system_state);
109
110 /*
111 * Boot command-line arguments
112 */
113 #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
114 #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
115
116 extern void time_init(void);
117 /* Default late time init is NULL. archs can override this later. */
118 void (*__initdata late_time_init)(void);
119
120 /* Untouched command line saved by arch-specific code. */
121 char __initdata boot_command_line[COMMAND_LINE_SIZE];
122 /* Untouched saved command line (eg. for /proc) */
123 char *saved_command_line;
124 /* Command line for parameter parsing */
125 static char *static_command_line;
126 /* Command line for per-initcall parameter parsing */
127 static char *initcall_command_line;
128
129 static char *execute_command;
130 static char *ramdisk_execute_command;
131
132 /*
133 * Used to generate warnings if static_key manipulation functions are used
134 * before jump_label_init is called.
135 */
136 bool static_key_initialized __read_mostly;
137 EXPORT_SYMBOL_GPL(static_key_initialized);
138
139 /*
140 * If set, this is an indication to the drivers that reset the underlying
141 * device before going ahead with the initialization otherwise driver might
142 * rely on the BIOS and skip the reset operation.
143 *
144 * This is useful if kernel is booting in an unreliable environment.
145 * For ex. kdump situaiton where previous kernel has crashed, BIOS has been
146 * skipped and devices will be in unknown state.
147 */
148 unsigned int reset_devices;
149 EXPORT_SYMBOL(reset_devices);
150
set_reset_devices(char * str)151 static int __init set_reset_devices(char *str)
152 {
153 reset_devices = 1;
154 return 1;
155 }
156
157 __setup("reset_devices", set_reset_devices);
158
159 static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
160 const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
161 static const char *panic_later, *panic_param;
162
163 extern const struct obs_kernel_param __setup_start[], __setup_end[];
164
obsolete_checksetup(char * line)165 static int __init obsolete_checksetup(char *line)
166 {
167 const struct obs_kernel_param *p;
168 int had_early_param = 0;
169
170 p = __setup_start;
171 do {
172 int n = strlen(p->str);
173 if (parameqn(line, p->str, n)) {
174 if (p->early) {
175 /* Already done in parse_early_param?
176 * (Needs exact match on param part).
177 * Keep iterating, as we can have early
178 * params and __setups of same names 8( */
179 if (line[n] == '\0' || line[n] == '=')
180 had_early_param = 1;
181 } else if (!p->setup_func) {
182 pr_warn("Parameter %s is obsolete, ignored\n",
183 p->str);
184 return 1;
185 } else if (p->setup_func(line + n))
186 return 1;
187 }
188 p++;
189 } while (p < __setup_end);
190
191 return had_early_param;
192 }
193
194 /*
195 * This should be approx 2 Bo*oMips to start (note initial shift), and will
196 * still work even if initially too large, it will just take slightly longer
197 */
198 unsigned long loops_per_jiffy = (1<<12);
199 EXPORT_SYMBOL(loops_per_jiffy);
200
debug_kernel(char * str)201 static int __init debug_kernel(char *str)
202 {
203 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
204 return 0;
205 }
206
quiet_kernel(char * str)207 static int __init quiet_kernel(char *str)
208 {
209 console_loglevel = CONSOLE_LOGLEVEL_QUIET;
210 return 0;
211 }
212
213 early_param("debug", debug_kernel);
214 early_param("quiet", quiet_kernel);
215
loglevel(char * str)216 static int __init loglevel(char *str)
217 {
218 int newlevel;
219
220 /*
221 * Only update loglevel value when a correct setting was passed,
222 * to prevent blind crashes (when loglevel being set to 0) that
223 * are quite hard to debug
224 */
225 if (get_option(&str, &newlevel)) {
226 console_loglevel = newlevel;
227 return 0;
228 }
229
230 return -EINVAL;
231 }
232
233 early_param("loglevel", loglevel);
234
235 /* Change NUL term back to "=", to make "param" the whole string. */
repair_env_string(char * param,char * val,const char * unused)236 static int __init repair_env_string(char *param, char *val, const char *unused)
237 {
238 if (val) {
239 /* param=val or param="val"? */
240 if (val == param+strlen(param)+1)
241 val[-1] = '=';
242 else if (val == param+strlen(param)+2) {
243 val[-2] = '=';
244 memmove(val-1, val, strlen(val)+1);
245 val--;
246 } else
247 BUG();
248 }
249 return 0;
250 }
251
252 /* Anything after -- gets handed straight to init. */
set_init_arg(char * param,char * val,const char * unused)253 static int __init set_init_arg(char *param, char *val, const char *unused)
254 {
255 unsigned int i;
256
257 if (panic_later)
258 return 0;
259
260 repair_env_string(param, val, unused);
261
262 for (i = 0; argv_init[i]; i++) {
263 if (i == MAX_INIT_ARGS) {
264 panic_later = "init";
265 panic_param = param;
266 return 0;
267 }
268 }
269 argv_init[i] = param;
270 return 0;
271 }
272
273 /*
274 * Unknown boot options get handed to init, unless they look like
275 * unused parameters (modprobe will find them in /proc/cmdline).
276 */
unknown_bootoption(char * param,char * val,const char * unused)277 static int __init unknown_bootoption(char *param, char *val, const char *unused)
278 {
279 repair_env_string(param, val, unused);
280
281 /* Handle obsolete-style parameters */
282 if (obsolete_checksetup(param))
283 return 0;
284
285 /* Unused module parameter. */
286 if (strchr(param, '.') && (!val || strchr(param, '.') < val))
287 return 0;
288
289 if (panic_later)
290 return 0;
291
292 if (val) {
293 /* Environment option */
294 unsigned int i;
295 for (i = 0; envp_init[i]; i++) {
296 if (i == MAX_INIT_ENVS) {
297 panic_later = "env";
298 panic_param = param;
299 }
300 if (!strncmp(param, envp_init[i], val - param))
301 break;
302 }
303 envp_init[i] = param;
304 } else {
305 /* Command line option */
306 unsigned int i;
307 for (i = 0; argv_init[i]; i++) {
308 if (i == MAX_INIT_ARGS) {
309 panic_later = "init";
310 panic_param = param;
311 }
312 }
313 argv_init[i] = param;
314 }
315 return 0;
316 }
317
init_setup(char * str)318 static int __init init_setup(char *str)
319 {
320 unsigned int i;
321
322 execute_command = str;
323 /*
324 * In case LILO is going to boot us with default command line,
325 * it prepends "auto" before the whole cmdline which makes
326 * the shell think it should execute a script with such name.
327 * So we ignore all arguments entered _before_ init=... [MJ]
328 */
329 for (i = 1; i < MAX_INIT_ARGS; i++)
330 argv_init[i] = NULL;
331 return 1;
332 }
333 __setup("init=", init_setup);
334
rdinit_setup(char * str)335 static int __init rdinit_setup(char *str)
336 {
337 unsigned int i;
338
339 ramdisk_execute_command = str;
340 /* See "auto" comment in init_setup */
341 for (i = 1; i < MAX_INIT_ARGS; i++)
342 argv_init[i] = NULL;
343 return 1;
344 }
345 __setup("rdinit=", rdinit_setup);
346
347 #ifndef CONFIG_SMP
348 static const unsigned int setup_max_cpus = NR_CPUS;
349 #ifdef CONFIG_X86_LOCAL_APIC
smp_init(void)350 static void __init smp_init(void)
351 {
352 APIC_init_uniprocessor();
353 }
354 #else
355 #define smp_init() do { } while (0)
356 #endif
357
setup_nr_cpu_ids(void)358 static inline void setup_nr_cpu_ids(void) { }
smp_prepare_cpus(unsigned int maxcpus)359 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
360 #endif
361
362 /*
363 * We need to store the untouched command line for future reference.
364 * We also need to store the touched command line since the parameter
365 * parsing is performed in place, and we should allow a component to
366 * store reference of name/value for future reference.
367 */
setup_command_line(char * command_line)368 static void __init setup_command_line(char *command_line)
369 {
370 saved_command_line =
371 memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
372 initcall_command_line =
373 memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
374 static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
375 strcpy(saved_command_line, boot_command_line);
376 strcpy(static_command_line, command_line);
377 }
378
379 /*
380 * We need to finalize in a non-__init function or else race conditions
381 * between the root thread and the init thread may cause start_kernel to
382 * be reaped by free_initmem before the root thread has proceeded to
383 * cpu_idle.
384 *
385 * gcc-3.4 accidentally inlines this function, so use noinline.
386 */
387
388 static __initdata DECLARE_COMPLETION(kthreadd_done);
389
rest_init(void)390 static noinline void __init_refok rest_init(void)
391 {
392 int pid;
393
394 rcu_scheduler_starting();
395 /*
396 * We need to spawn init first so that it obtains pid 1, however
397 * the init task will end up wanting to create kthreads, which, if
398 * we schedule it before we create kthreadd, will OOPS.
399 */
400 kernel_thread(kernel_init, NULL, CLONE_FS);
401 numa_default_policy();
402 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
403 rcu_read_lock();
404 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
405 rcu_read_unlock();
406 complete(&kthreadd_done);
407
408 /*
409 * The boot idle thread must execute schedule()
410 * at least once to get things moving:
411 */
412 init_idle_bootup_task(current);
413 schedule_preempt_disabled();
414 /* Call into cpu_idle with preempt disabled */
415 cpu_startup_entry(CPUHP_ONLINE);
416 }
417
418 /* Check for early params. */
do_early_param(char * param,char * val,const char * unused)419 static int __init do_early_param(char *param, char *val, const char *unused)
420 {
421 const struct obs_kernel_param *p;
422
423 for (p = __setup_start; p < __setup_end; p++) {
424 if ((p->early && parameq(param, p->str)) ||
425 (strcmp(param, "console") == 0 &&
426 strcmp(p->str, "earlycon") == 0)
427 ) {
428 if (p->setup_func(val) != 0)
429 pr_warn("Malformed early option '%s'\n", param);
430 }
431 }
432 /* We accept everything at this stage. */
433 return 0;
434 }
435
parse_early_options(char * cmdline)436 void __init parse_early_options(char *cmdline)
437 {
438 parse_args("early options", cmdline, NULL, 0, 0, 0, do_early_param);
439 }
440
441 /* Arch code calls this early on, or if not, just before other parsing. */
parse_early_param(void)442 void __init parse_early_param(void)
443 {
444 static int done __initdata;
445 static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
446
447 if (done)
448 return;
449
450 /* All fall through to do_early_param. */
451 strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
452 parse_early_options(tmp_cmdline);
453 done = 1;
454 }
455
456 /*
457 * Activate the first processor.
458 */
459
boot_cpu_init(void)460 static void __init boot_cpu_init(void)
461 {
462 int cpu = smp_processor_id();
463 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
464 set_cpu_online(cpu, true);
465 set_cpu_active(cpu, true);
466 set_cpu_present(cpu, true);
467 set_cpu_possible(cpu, true);
468 }
469
smp_setup_processor_id(void)470 void __init __weak smp_setup_processor_id(void)
471 {
472 }
473
474 # if THREAD_SIZE >= PAGE_SIZE
thread_info_cache_init(void)475 void __init __weak thread_info_cache_init(void)
476 {
477 }
478 #endif
479
480 /*
481 * Set up kernel memory allocators
482 */
mm_init(void)483 static void __init mm_init(void)
484 {
485 /*
486 * page_cgroup requires contiguous pages,
487 * bigger than MAX_ORDER unless SPARSEMEM.
488 */
489 page_cgroup_init_flatmem();
490 mem_init();
491 kmem_cache_init();
492 percpu_init_late();
493 pgtable_init();
494 vmalloc_init();
495 }
496
start_kernel(void)497 asmlinkage __visible void __init start_kernel(void)
498 {
499 char *command_line;
500 char *after_dashes;
501
502 /*
503 * Need to run as early as possible, to initialize the
504 * lockdep hash:
505 */
506 lockdep_init();
507 set_task_stack_end_magic(&init_task);
508 smp_setup_processor_id();
509 debug_objects_early_init();
510
511 /*
512 * Set up the the initial canary ASAP:
513 */
514 boot_init_stack_canary();
515
516 cgroup_init_early();
517
518 local_irq_disable();
519 early_boot_irqs_disabled = true;
520
521 /*
522 * Interrupts are still disabled. Do necessary setups, then
523 * enable them
524 */
525 boot_cpu_init();
526 page_address_init();
527 pr_notice("%s", linux_banner);
528 setup_arch(&command_line);
529 mm_init_cpumask(&init_mm);
530 setup_command_line(command_line);
531 setup_nr_cpu_ids();
532 setup_per_cpu_areas();
533 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
534
535 build_all_zonelists(NULL, NULL);
536 page_alloc_init();
537
538 pr_notice("Kernel command line: %s\n", boot_command_line);
539 parse_early_param();
540 after_dashes = parse_args("Booting kernel",
541 static_command_line, __start___param,
542 __stop___param - __start___param,
543 -1, -1, &unknown_bootoption);
544 if (!IS_ERR_OR_NULL(after_dashes))
545 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
546 set_init_arg);
547
548 jump_label_init();
549
550 /*
551 * These use large bootmem allocations and must precede
552 * kmem_cache_init()
553 */
554 setup_log_buf(0);
555 pidhash_init();
556 vfs_caches_init_early();
557 sort_main_extable();
558 trap_init();
559 mm_init();
560
561 /*
562 * Set up the scheduler prior starting any interrupts (such as the
563 * timer interrupt). Full topology setup happens at smp_init()
564 * time - but meanwhile we still have a functioning scheduler.
565 */
566 sched_init();
567 /*
568 * Disable preemption - early bootup scheduling is extremely
569 * fragile until we cpu_idle() for the first time.
570 */
571 preempt_disable();
572 if (WARN(!irqs_disabled(),
573 "Interrupts were enabled *very* early, fixing it\n"))
574 local_irq_disable();
575 idr_init_cache();
576 rcu_init();
577
578 /* trace_printk() and trace points may be used after this */
579 trace_init();
580
581 context_tracking_init();
582 radix_tree_init();
583 /* init some links before init_ISA_irqs() */
584 early_irq_init();
585 init_IRQ();
586 tick_init();
587 rcu_init_nohz();
588 init_timers();
589 hrtimers_init();
590 softirq_init();
591 timekeeping_init();
592 time_init();
593 sched_clock_postinit();
594 perf_event_init();
595 profile_init();
596 call_function_init();
597 WARN(!irqs_disabled(), "Interrupts were enabled early\n");
598 early_boot_irqs_disabled = false;
599 local_irq_enable();
600
601 kmem_cache_init_late();
602
603 /*
604 * HACK ALERT! This is early. We're enabling the console before
605 * we've done PCI setups etc, and console_init() must be aware of
606 * this. But we do want output early, in case something goes wrong.
607 */
608 console_init();
609 if (panic_later)
610 panic("Too many boot %s vars at `%s'", panic_later,
611 panic_param);
612
613 lockdep_info();
614
615 /*
616 * Need to run this when irqs are enabled, because it wants
617 * to self-test [hard/soft]-irqs on/off lock inversion bugs
618 * too:
619 */
620 locking_selftest();
621
622 #ifdef CONFIG_BLK_DEV_INITRD
623 if (initrd_start && !initrd_below_start_ok &&
624 page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
625 pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
626 page_to_pfn(virt_to_page((void *)initrd_start)),
627 min_low_pfn);
628 initrd_start = 0;
629 }
630 #endif
631 page_cgroup_init();
632 debug_objects_mem_init();
633 kmemleak_init();
634 setup_per_cpu_pageset();
635 numa_policy_init();
636 if (late_time_init)
637 late_time_init();
638 sched_clock_init();
639 calibrate_delay();
640 pidmap_init();
641 anon_vma_init();
642 acpi_early_init();
643 #ifdef CONFIG_X86
644 if (efi_enabled(EFI_RUNTIME_SERVICES))
645 efi_enter_virtual_mode();
646 #endif
647 #ifdef CONFIG_X86_ESPFIX64
648 /* Should be run before the first non-init thread is created */
649 init_espfix_bsp();
650 #endif
651 thread_info_cache_init();
652 cred_init();
653 fork_init(totalram_pages);
654 proc_caches_init();
655 buffer_init();
656 key_init();
657 security_init();
658 dbg_late_init();
659 vfs_caches_init(totalram_pages);
660 signals_init();
661 /* rootfs populating might need page-writeback */
662 page_writeback_init();
663 proc_root_init();
664 cgroup_init();
665 cpuset_init();
666 taskstats_init_early();
667 delayacct_init();
668
669 check_bugs();
670
671 acpi_subsystem_init();
672 sfi_init_late();
673
674 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
675 efi_late_init();
676 efi_free_boot_services();
677 }
678
679 ftrace_init();
680
681 /* Do the rest non-__init'ed, we're now alive */
682 rest_init();
683 }
684
685 /* Call all constructor functions linked into the kernel. */
do_ctors(void)686 static void __init do_ctors(void)
687 {
688 #ifdef CONFIG_CONSTRUCTORS
689 ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
690
691 for (; fn < (ctor_fn_t *) __ctors_end; fn++)
692 (*fn)();
693 #endif
694 }
695
696 bool initcall_debug;
697 core_param(initcall_debug, initcall_debug, bool, 0644);
698
699 #ifdef CONFIG_KALLSYMS
700 struct blacklist_entry {
701 struct list_head next;
702 char *buf;
703 };
704
705 static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
706
initcall_blacklist(char * str)707 static int __init initcall_blacklist(char *str)
708 {
709 char *str_entry;
710 struct blacklist_entry *entry;
711
712 /* str argument is a comma-separated list of functions */
713 do {
714 str_entry = strsep(&str, ",");
715 if (str_entry) {
716 pr_debug("blacklisting initcall %s\n", str_entry);
717 entry = alloc_bootmem(sizeof(*entry));
718 entry->buf = alloc_bootmem(strlen(str_entry) + 1);
719 strcpy(entry->buf, str_entry);
720 list_add(&entry->next, &blacklisted_initcalls);
721 }
722 } while (str_entry);
723
724 return 0;
725 }
726
initcall_blacklisted(initcall_t fn)727 static bool __init_or_module initcall_blacklisted(initcall_t fn)
728 {
729 struct list_head *tmp;
730 struct blacklist_entry *entry;
731 char *fn_name;
732
733 fn_name = kasprintf(GFP_KERNEL, "%pf", fn);
734 if (!fn_name)
735 return false;
736
737 list_for_each(tmp, &blacklisted_initcalls) {
738 entry = list_entry(tmp, struct blacklist_entry, next);
739 if (!strcmp(fn_name, entry->buf)) {
740 pr_debug("initcall %s blacklisted\n", fn_name);
741 kfree(fn_name);
742 return true;
743 }
744 }
745
746 kfree(fn_name);
747 return false;
748 }
749 #else
initcall_blacklist(char * str)750 static int __init initcall_blacklist(char *str)
751 {
752 pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
753 return 0;
754 }
755
initcall_blacklisted(initcall_t fn)756 static bool __init_or_module initcall_blacklisted(initcall_t fn)
757 {
758 return false;
759 }
760 #endif
761 __setup("initcall_blacklist=", initcall_blacklist);
762
do_one_initcall_debug(initcall_t fn)763 static int __init_or_module do_one_initcall_debug(initcall_t fn)
764 {
765 ktime_t calltime, delta, rettime;
766 unsigned long long duration;
767 int ret;
768
769 printk(KERN_DEBUG "calling %pF @ %i\n", fn, task_pid_nr(current));
770 calltime = ktime_get();
771 ret = fn();
772 rettime = ktime_get();
773 delta = ktime_sub(rettime, calltime);
774 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
775 printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
776 fn, ret, duration);
777
778 return ret;
779 }
780
do_one_initcall(initcall_t fn)781 int __init_or_module do_one_initcall(initcall_t fn)
782 {
783 int count = preempt_count();
784 int ret;
785 char msgbuf[64];
786
787 if (initcall_blacklisted(fn))
788 return -EPERM;
789
790 if (initcall_debug)
791 ret = do_one_initcall_debug(fn);
792 else
793 ret = fn();
794
795 msgbuf[0] = 0;
796
797 if (preempt_count() != count) {
798 sprintf(msgbuf, "preemption imbalance ");
799 preempt_count_set(count);
800 }
801 if (irqs_disabled()) {
802 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
803 local_irq_enable();
804 }
805 WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
806
807 return ret;
808 }
809
810
811 extern initcall_t __initcall_start[];
812 extern initcall_t __initcall0_start[];
813 extern initcall_t __initcall1_start[];
814 extern initcall_t __initcall2_start[];
815 extern initcall_t __initcall3_start[];
816 extern initcall_t __initcall4_start[];
817 extern initcall_t __initcall5_start[];
818 extern initcall_t __initcall6_start[];
819 extern initcall_t __initcall7_start[];
820 extern initcall_t __initcall_end[];
821
822 static initcall_t *initcall_levels[] __initdata = {
823 __initcall0_start,
824 __initcall1_start,
825 __initcall2_start,
826 __initcall3_start,
827 __initcall4_start,
828 __initcall5_start,
829 __initcall6_start,
830 __initcall7_start,
831 __initcall_end,
832 };
833
834 /* Keep these in sync with initcalls in include/linux/init.h */
835 static char *initcall_level_names[] __initdata = {
836 "early",
837 "core",
838 "postcore",
839 "arch",
840 "subsys",
841 "fs",
842 "device",
843 "late",
844 };
845
do_initcall_level(int level)846 static void __init do_initcall_level(int level)
847 {
848 initcall_t *fn;
849
850 strcpy(initcall_command_line, saved_command_line);
851 parse_args(initcall_level_names[level],
852 initcall_command_line, __start___param,
853 __stop___param - __start___param,
854 level, level,
855 &repair_env_string);
856
857 for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
858 do_one_initcall(*fn);
859 }
860
do_initcalls(void)861 static void __init do_initcalls(void)
862 {
863 int level;
864
865 for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
866 do_initcall_level(level);
867 }
868
869 /*
870 * Ok, the machine is now initialized. None of the devices
871 * have been touched yet, but the CPU subsystem is up and
872 * running, and memory and process management works.
873 *
874 * Now we can finally start doing some real work..
875 */
do_basic_setup(void)876 static void __init do_basic_setup(void)
877 {
878 cpuset_init_smp();
879 usermodehelper_init();
880 shmem_init();
881 driver_init();
882 init_irq_proc();
883 do_ctors();
884 usermodehelper_enable();
885 do_initcalls();
886 random_int_secret_init();
887 }
888
do_pre_smp_initcalls(void)889 static void __init do_pre_smp_initcalls(void)
890 {
891 initcall_t *fn;
892
893 for (fn = __initcall_start; fn < __initcall0_start; fn++)
894 do_one_initcall(*fn);
895 }
896
897 /*
898 * This function requests modules which should be loaded by default and is
899 * called twice right after initrd is mounted and right before init is
900 * exec'd. If such modules are on either initrd or rootfs, they will be
901 * loaded before control is passed to userland.
902 */
load_default_modules(void)903 void __init load_default_modules(void)
904 {
905 load_default_elevator_module();
906 }
907
run_init_process(const char * init_filename)908 static int run_init_process(const char *init_filename)
909 {
910 argv_init[0] = init_filename;
911 return do_execve(getname_kernel(init_filename),
912 (const char __user *const __user *)argv_init,
913 (const char __user *const __user *)envp_init);
914 }
915
try_to_run_init_process(const char * init_filename)916 static int try_to_run_init_process(const char *init_filename)
917 {
918 int ret;
919
920 ret = run_init_process(init_filename);
921
922 if (ret && ret != -ENOENT) {
923 pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
924 init_filename, ret);
925 }
926
927 return ret;
928 }
929
930 static noinline void __init kernel_init_freeable(void);
931
932 #ifdef CONFIG_DEBUG_RODATA
933 static bool rodata_enabled = true;
set_debug_rodata(char * str)934 static int __init set_debug_rodata(char *str)
935 {
936 return strtobool(str, &rodata_enabled);
937 }
938 __setup("rodata=", set_debug_rodata);
939
mark_readonly(void)940 static void mark_readonly(void)
941 {
942 if (rodata_enabled)
943 mark_rodata_ro();
944 else
945 pr_info("Kernel memory protection disabled.\n");
946 }
947 #else
mark_readonly(void)948 static inline void mark_readonly(void)
949 {
950 pr_warn("This architecture does not have kernel memory protection.\n");
951 }
952 #endif
953
kernel_init(void * unused)954 static int __ref kernel_init(void *unused)
955 {
956 int ret;
957
958 kernel_init_freeable();
959 /* need to finish all async __init code before freeing the memory */
960 async_synchronize_full();
961 free_initmem();
962 mark_readonly();
963 system_state = SYSTEM_RUNNING;
964 numa_default_policy();
965
966 flush_delayed_fput();
967
968 if (ramdisk_execute_command) {
969 ret = run_init_process(ramdisk_execute_command);
970 if (!ret)
971 return 0;
972 pr_err("Failed to execute %s (error %d)\n",
973 ramdisk_execute_command, ret);
974 }
975
976 /*
977 * We try each of these until one succeeds.
978 *
979 * The Bourne shell can be used instead of init if we are
980 * trying to recover a really broken machine.
981 */
982 if (execute_command) {
983 ret = run_init_process(execute_command);
984 if (!ret)
985 return 0;
986 pr_err("Failed to execute %s (error %d). Attempting defaults...\n",
987 execute_command, ret);
988 }
989 if (!try_to_run_init_process("/sbin/init") ||
990 !try_to_run_init_process("/etc/init") ||
991 !try_to_run_init_process("/bin/init") ||
992 !try_to_run_init_process("/bin/sh"))
993 return 0;
994
995 panic("No working init found. Try passing init= option to kernel. "
996 "See Linux Documentation/init.txt for guidance.");
997 }
998
kernel_init_freeable(void)999 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 do_pre_smp_initcalls();
1023 lockup_detector_init();
1024
1025 smp_init();
1026 sched_init_smp();
1027
1028 do_basic_setup();
1029
1030 /* Open the /dev/console on the rootfs, this should never fail */
1031 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1032 pr_err("Warning: unable to open an initial console.\n");
1033
1034 (void) sys_dup(0);
1035 (void) sys_dup(0);
1036 /*
1037 * check if there is an early userspace init. If yes, let it do all
1038 * the work
1039 */
1040
1041 if (!ramdisk_execute_command)
1042 ramdisk_execute_command = "/init";
1043
1044 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
1045 ramdisk_execute_command = NULL;
1046 prepare_namespace();
1047 }
1048
1049 /*
1050 * Ok, we have completed the initial bootup, and
1051 * we're essentially up and running. Get rid of the
1052 * initmem segments and start the user-mode stuff..
1053 */
1054
1055 /* rootfs is available now, try loading default modules */
1056 load_default_modules();
1057 }
1058