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