9711586aa7c9d7dc416f8f57ebc75f525c899d1b
[safe/jmp/linux-2.6] / 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 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/proc_fs.h>
15 #include <linux/kernel.h>
16 #include <linux/syscalls.h>
17 #include <linux/string.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20 #include <linux/utsname.h>
21 #include <linux/ioport.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/initrd.h>
25 #include <linux/bootmem.h>
26 #include <linux/tty.h>
27 #include <linux/gfp.h>
28 #include <linux/percpu.h>
29 #include <linux/kmod.h>
30 #include <linux/vmalloc.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/start_kernel.h>
33 #include <linux/security.h>
34 #include <linux/smp.h>
35 #include <linux/workqueue.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/unwind.h>
54 #include <linux/buffer_head.h>
55 #include <linux/page_cgroup.h>
56 #include <linux/debug_locks.h>
57 #include <linux/debugobjects.h>
58 #include <linux/lockdep.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/ftrace.h>
66 #include <trace/boot.h>
67
68 #include <asm/io.h>
69 #include <asm/bugs.h>
70 #include <asm/setup.h>
71 #include <asm/sections.h>
72 #include <asm/cacheflush.h>
73 #include <linux/kmemtrace.h>
74
75 #ifdef CONFIG_X86_LOCAL_APIC
76 #include <asm/smp.h>
77 #endif
78
79 /*
80  * This is one of the first .c files built. Error out early if we have compiler
81  * trouble.
82  */
83
84 #if __GNUC__ == 4 && __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ == 0
85 #warning gcc-4.1.0 is known to miscompile the kernel.  A different compiler version is recommended.
86 #endif
87
88 static int kernel_init(void *);
89
90 extern void init_IRQ(void);
91 extern void fork_init(unsigned long);
92 extern void mca_init(void);
93 extern void sbus_init(void);
94 extern void prio_tree_init(void);
95 extern void radix_tree_init(void);
96 extern void free_initmem(void);
97 #ifdef  CONFIG_ACPI
98 extern void acpi_early_init(void);
99 #else
100 static inline void acpi_early_init(void) { }
101 #endif
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 enum system_states system_state;
111 EXPORT_SYMBOL(system_state);
112
113 /*
114  * Boot command-line arguments
115  */
116 #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
117 #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
118
119 extern void time_init(void);
120 /* Default late time init is NULL. archs can override this later. */
121 void (*late_time_init)(void);
122 extern void softirq_init(void);
123
124 /* Untouched command line saved by arch-specific code. */
125 char __initdata boot_command_line[COMMAND_LINE_SIZE];
126 /* Untouched saved command line (eg. for /proc) */
127 char *saved_command_line;
128 /* Command line for parameter parsing */
129 static char *static_command_line;
130
131 static char *execute_command;
132 static char *ramdisk_execute_command;
133
134 #ifdef CONFIG_SMP
135 /* Setup configured maximum number of CPUs to activate */
136 unsigned int __initdata setup_max_cpus = NR_CPUS;
137
138 /*
139  * Setup routine for controlling SMP activation
140  *
141  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
142  * activation entirely (the MPS table probe still happens, though).
143  *
144  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
145  * greater than 0, limits the maximum number of CPUs activated in
146  * SMP mode to <NUM>.
147  */
148 #ifndef CONFIG_X86_IO_APIC
149 static inline void disable_ioapic_setup(void) {};
150 #endif
151
152 static int __init nosmp(char *str)
153 {
154         setup_max_cpus = 0;
155         disable_ioapic_setup();
156         return 0;
157 }
158
159 early_param("nosmp", nosmp);
160
161 static int __init maxcpus(char *str)
162 {
163         get_option(&str, &setup_max_cpus);
164         if (setup_max_cpus == 0)
165                 disable_ioapic_setup();
166
167         return 0;
168 }
169
170 early_param("maxcpus", maxcpus);
171 #else
172 #define setup_max_cpus NR_CPUS
173 #endif
174
175 /*
176  * If set, this is an indication to the drivers that reset the underlying
177  * device before going ahead with the initialization otherwise driver might
178  * rely on the BIOS and skip the reset operation.
179  *
180  * This is useful if kernel is booting in an unreliable environment.
181  * For ex. kdump situaiton where previous kernel has crashed, BIOS has been
182  * skipped and devices will be in unknown state.
183  */
184 unsigned int reset_devices;
185 EXPORT_SYMBOL(reset_devices);
186
187 static int __init set_reset_devices(char *str)
188 {
189         reset_devices = 1;
190         return 1;
191 }
192
193 __setup("reset_devices", set_reset_devices);
194
195 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
196 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
197 static const char *panic_later, *panic_param;
198
199 extern struct obs_kernel_param __setup_start[], __setup_end[];
200
201 static int __init obsolete_checksetup(char *line)
202 {
203         struct obs_kernel_param *p;
204         int had_early_param = 0;
205
206         p = __setup_start;
207         do {
208                 int n = strlen(p->str);
209                 if (!strncmp(line, p->str, n)) {
210                         if (p->early) {
211                                 /* Already done in parse_early_param?
212                                  * (Needs exact match on param part).
213                                  * Keep iterating, as we can have early
214                                  * params and __setups of same names 8( */
215                                 if (line[n] == '\0' || line[n] == '=')
216                                         had_early_param = 1;
217                         } else if (!p->setup_func) {
218                                 printk(KERN_WARNING "Parameter %s is obsolete,"
219                                        " ignored\n", p->str);
220                                 return 1;
221                         } else if (p->setup_func(line + n))
222                                 return 1;
223                 }
224                 p++;
225         } while (p < __setup_end);
226
227         return had_early_param;
228 }
229
230 /*
231  * This should be approx 2 Bo*oMips to start (note initial shift), and will
232  * still work even if initially too large, it will just take slightly longer
233  */
234 unsigned long loops_per_jiffy = (1<<12);
235
236 EXPORT_SYMBOL(loops_per_jiffy);
237
238 static int __init debug_kernel(char *str)
239 {
240         console_loglevel = 10;
241         return 0;
242 }
243
244 static int __init quiet_kernel(char *str)
245 {
246         console_loglevel = 4;
247         return 0;
248 }
249
250 early_param("debug", debug_kernel);
251 early_param("quiet", quiet_kernel);
252
253 static int __init loglevel(char *str)
254 {
255         get_option(&str, &console_loglevel);
256         return 0;
257 }
258
259 early_param("loglevel", loglevel);
260
261 /*
262  * Unknown boot options get handed to init, unless they look like
263  * failed parameters
264  */
265 static int __init unknown_bootoption(char *param, char *val)
266 {
267         /* Change NUL term back to "=", to make "param" the whole string. */
268         if (val) {
269                 /* param=val or param="val"? */
270                 if (val == param+strlen(param)+1)
271                         val[-1] = '=';
272                 else if (val == param+strlen(param)+2) {
273                         val[-2] = '=';
274                         memmove(val-1, val, strlen(val)+1);
275                         val--;
276                 } else
277                         BUG();
278         }
279
280         /* Handle obsolete-style parameters */
281         if (obsolete_checksetup(param))
282                 return 0;
283
284         /*
285          * Preemptive maintenance for "why didn't my misspelled command
286          * line work?"
287          */
288         if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
289                 printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
290                 return 0;
291         }
292
293         if (panic_later)
294                 return 0;
295
296         if (val) {
297                 /* Environment option */
298                 unsigned int i;
299                 for (i = 0; envp_init[i]; i++) {
300                         if (i == MAX_INIT_ENVS) {
301                                 panic_later = "Too many boot env vars at `%s'";
302                                 panic_param = param;
303                         }
304                         if (!strncmp(param, envp_init[i], val - param))
305                                 break;
306                 }
307                 envp_init[i] = param;
308         } else {
309                 /* Command line option */
310                 unsigned int i;
311                 for (i = 0; argv_init[i]; i++) {
312                         if (i == MAX_INIT_ARGS) {
313                                 panic_later = "Too many boot init vars at `%s'";
314                                 panic_param = param;
315                         }
316                 }
317                 argv_init[i] = param;
318         }
319         return 0;
320 }
321
322 #ifdef CONFIG_DEBUG_PAGEALLOC
323 int __read_mostly debug_pagealloc_enabled = 0;
324 #endif
325
326 static int __init init_setup(char *str)
327 {
328         unsigned int i;
329
330         execute_command = str;
331         /*
332          * In case LILO is going to boot us with default command line,
333          * it prepends "auto" before the whole cmdline which makes
334          * the shell think it should execute a script with such name.
335          * So we ignore all arguments entered _before_ init=... [MJ]
336          */
337         for (i = 1; i < MAX_INIT_ARGS; i++)
338                 argv_init[i] = NULL;
339         return 1;
340 }
341 __setup("init=", init_setup);
342
343 static int __init rdinit_setup(char *str)
344 {
345         unsigned int i;
346
347         ramdisk_execute_command = str;
348         /* See "auto" comment in init_setup */
349         for (i = 1; i < MAX_INIT_ARGS; i++)
350                 argv_init[i] = NULL;
351         return 1;
352 }
353 __setup("rdinit=", rdinit_setup);
354
355 #ifndef CONFIG_SMP
356
357 #ifdef CONFIG_X86_LOCAL_APIC
358 static void __init smp_init(void)
359 {
360         APIC_init_uniprocessor();
361 }
362 #else
363 #define smp_init()      do { } while (0)
364 #endif
365
366 static inline void setup_per_cpu_areas(void) { }
367 static inline void setup_nr_cpu_ids(void) { }
368 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
369
370 #else
371
372 #if NR_CPUS > BITS_PER_LONG
373 cpumask_t cpu_mask_all __read_mostly = CPU_MASK_ALL;
374 EXPORT_SYMBOL(cpu_mask_all);
375 #endif
376
377 /* Setup number of possible processor ids */
378 int nr_cpu_ids __read_mostly = NR_CPUS;
379 EXPORT_SYMBOL(nr_cpu_ids);
380
381 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
382 static void __init setup_nr_cpu_ids(void)
383 {
384         int cpu, highest_cpu = 0;
385
386         for_each_possible_cpu(cpu)
387                 highest_cpu = cpu;
388
389         nr_cpu_ids = highest_cpu + 1;
390 }
391
392 #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
393 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
394
395 EXPORT_SYMBOL(__per_cpu_offset);
396
397 static void __init setup_per_cpu_areas(void)
398 {
399         unsigned long size, i;
400         char *ptr;
401         unsigned long nr_possible_cpus = num_possible_cpus();
402
403         /* Copy section for each CPU (we discard the original) */
404         size = ALIGN(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
405         ptr = alloc_bootmem_pages(size * nr_possible_cpus);
406
407         for_each_possible_cpu(i) {
408                 __per_cpu_offset[i] = ptr - __per_cpu_start;
409                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
410                 ptr += size;
411         }
412 }
413 #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
414
415 /* Called by boot processor to activate the rest. */
416 static void __init smp_init(void)
417 {
418         unsigned int cpu;
419
420         /*
421          * Set up the current CPU as possible to migrate to.
422          * The other ones will be done by cpu_up/cpu_down()
423          */
424         cpu = smp_processor_id();
425         cpu_set(cpu, cpu_active_map);
426
427         /* FIXME: This should be done in userspace --RR */
428         for_each_present_cpu(cpu) {
429                 if (num_online_cpus() >= setup_max_cpus)
430                         break;
431                 if (!cpu_online(cpu))
432                         cpu_up(cpu);
433         }
434
435         /* Any cleanup work */
436         printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
437         smp_cpus_done(setup_max_cpus);
438 }
439
440 #endif
441
442 /*
443  * We need to store the untouched command line for future reference.
444  * We also need to store the touched command line since the parameter
445  * parsing is performed in place, and we should allow a component to
446  * store reference of name/value for future reference.
447  */
448 static void __init setup_command_line(char *command_line)
449 {
450         saved_command_line = alloc_bootmem(strlen (boot_command_line)+1);
451         static_command_line = alloc_bootmem(strlen (command_line)+1);
452         strcpy (saved_command_line, boot_command_line);
453         strcpy (static_command_line, command_line);
454 }
455
456 /*
457  * We need to finalize in a non-__init function or else race conditions
458  * between the root thread and the init thread may cause start_kernel to
459  * be reaped by free_initmem before the root thread has proceeded to
460  * cpu_idle.
461  *
462  * gcc-3.4 accidentally inlines this function, so use noinline.
463  */
464
465 static void noinline __init_refok rest_init(void)
466         __releases(kernel_lock)
467 {
468         int pid;
469
470         kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
471         numa_default_policy();
472         pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
473         kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
474         unlock_kernel();
475
476         /*
477          * The boot idle thread must execute schedule()
478          * at least once to get things moving:
479          */
480         init_idle_bootup_task(current);
481         preempt_enable_no_resched();
482         schedule();
483         preempt_disable();
484
485         /* Call into cpu_idle with preempt disabled */
486         cpu_idle();
487 }
488
489 /* Check for early params. */
490 static int __init do_early_param(char *param, char *val)
491 {
492         struct obs_kernel_param *p;
493
494         for (p = __setup_start; p < __setup_end; p++) {
495                 if ((p->early && strcmp(param, p->str) == 0) ||
496                     (strcmp(param, "console") == 0 &&
497                      strcmp(p->str, "earlycon") == 0)
498                 ) {
499                         if (p->setup_func(val) != 0)
500                                 printk(KERN_WARNING
501                                        "Malformed early option '%s'\n", param);
502                 }
503         }
504         /* We accept everything at this stage. */
505         return 0;
506 }
507
508 /* Arch code calls this early on, or if not, just before other parsing. */
509 void __init parse_early_param(void)
510 {
511         static __initdata int done = 0;
512         static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
513
514         if (done)
515                 return;
516
517         /* All fall through to do_early_param. */
518         strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
519         parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
520         done = 1;
521 }
522
523 /*
524  *      Activate the first processor.
525  */
526
527 static void __init boot_cpu_init(void)
528 {
529         int cpu = smp_processor_id();
530         /* Mark the boot cpu "present", "online" etc for SMP and UP case */
531         cpu_set(cpu, cpu_online_map);
532         cpu_set(cpu, cpu_present_map);
533         cpu_set(cpu, cpu_possible_map);
534 }
535
536 void __init __weak smp_setup_processor_id(void)
537 {
538 }
539
540 void __init __weak thread_info_cache_init(void)
541 {
542 }
543
544 asmlinkage void __init start_kernel(void)
545 {
546         char * command_line;
547         extern struct kernel_param __start___param[], __stop___param[];
548
549         smp_setup_processor_id();
550
551         /*
552          * Need to run as early as possible, to initialize the
553          * lockdep hash:
554          */
555         unwind_init();
556         lockdep_init();
557         debug_objects_early_init();
558         cgroup_init_early();
559
560         local_irq_disable();
561         early_boot_irqs_off();
562         early_init_irq_lock_class();
563
564 /*
565  * Interrupts are still disabled. Do necessary setups, then
566  * enable them
567  */
568         lock_kernel();
569         tick_init();
570         boot_cpu_init();
571         page_address_init();
572         printk(KERN_NOTICE);
573         printk(linux_banner);
574         setup_arch(&command_line);
575         mm_init_owner(&init_mm, &init_task);
576         setup_command_line(command_line);
577         unwind_setup();
578         setup_per_cpu_areas();
579         setup_nr_cpu_ids();
580         smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
581
582         /*
583          * Set up the scheduler prior starting any interrupts (such as the
584          * timer interrupt). Full topology setup happens at smp_init()
585          * time - but meanwhile we still have a functioning scheduler.
586          */
587         sched_init();
588         /*
589          * Disable preemption - early bootup scheduling is extremely
590          * fragile until we cpu_idle() for the first time.
591          */
592         preempt_disable();
593         build_all_zonelists();
594         page_alloc_init();
595         printk(KERN_NOTICE "Kernel command line: %s\n", boot_command_line);
596         parse_early_param();
597         parse_args("Booting kernel", static_command_line, __start___param,
598                    __stop___param - __start___param,
599                    &unknown_bootoption);
600         if (!irqs_disabled()) {
601                 printk(KERN_WARNING "start_kernel(): bug: interrupts were "
602                                 "enabled *very* early, fixing it\n");
603                 local_irq_disable();
604         }
605         sort_main_extable();
606         trap_init();
607         rcu_init();
608         init_IRQ();
609         pidhash_init();
610         init_timers();
611         hrtimers_init();
612         softirq_init();
613         timekeeping_init();
614         time_init();
615         sched_clock_init();
616         profile_init();
617         if (!irqs_disabled())
618                 printk("start_kernel(): bug: interrupts were enabled early\n");
619         early_boot_irqs_on();
620         local_irq_enable();
621
622         /*
623          * HACK ALERT! This is early. We're enabling the console before
624          * we've done PCI setups etc, and console_init() must be aware of
625          * this. But we do want output early, in case something goes wrong.
626          */
627         console_init();
628         if (panic_later)
629                 panic(panic_later, panic_param);
630
631         lockdep_info();
632
633         /*
634          * Need to run this when irqs are enabled, because it wants
635          * to self-test [hard/soft]-irqs on/off lock inversion bugs
636          * too:
637          */
638         locking_selftest();
639
640 #ifdef CONFIG_BLK_DEV_INITRD
641         if (initrd_start && !initrd_below_start_ok &&
642             page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
643                 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
644                     "disabling it.\n",
645                     page_to_pfn(virt_to_page((void *)initrd_start)),
646                     min_low_pfn);
647                 initrd_start = 0;
648         }
649 #endif
650         vmalloc_init();
651         vfs_caches_init_early();
652         cpuset_init_early();
653         page_cgroup_init();
654         mem_init();
655         enable_debug_pagealloc();
656         cpu_hotplug_init();
657         kmem_cache_init();
658         kmemtrace_init();
659         debug_objects_mem_init();
660         idr_init_cache();
661         setup_per_cpu_pageset();
662         numa_policy_init();
663         if (late_time_init)
664                 late_time_init();
665         calibrate_delay();
666         pidmap_init();
667         pgtable_cache_init();
668         prio_tree_init();
669         anon_vma_init();
670 #ifdef CONFIG_X86
671         if (efi_enabled)
672                 efi_enter_virtual_mode();
673 #endif
674         thread_info_cache_init();
675         cred_init();
676         fork_init(num_physpages);
677         proc_caches_init();
678         buffer_init();
679         key_init();
680         security_init();
681         vfs_caches_init(num_physpages);
682         radix_tree_init();
683         signals_init();
684         /* rootfs populating might need page-writeback */
685         page_writeback_init();
686 #ifdef CONFIG_PROC_FS
687         proc_root_init();
688 #endif
689         cgroup_init();
690         cpuset_init();
691         taskstats_init_early();
692         delayacct_init();
693
694         check_bugs();
695
696         acpi_early_init(); /* before LAPIC and SMP init */
697
698         ftrace_init();
699
700         /* Do the rest non-__init'ed, we're now alive */
701         rest_init();
702 }
703
704 static int initcall_debug;
705 core_param(initcall_debug, initcall_debug, bool, 0644);
706
707 int do_one_initcall(initcall_t fn)
708 {
709         int count = preempt_count();
710         ktime_t calltime, delta, rettime;
711         char msgbuf[64];
712         struct boot_trace_call call;
713         struct boot_trace_ret ret;
714
715         if (initcall_debug) {
716                 call.caller = task_pid_nr(current);
717                 printk("calling  %pF @ %i\n", fn, call.caller);
718                 calltime = ktime_get();
719                 trace_boot_call(&call, fn);
720                 enable_boot_trace();
721         }
722
723         ret.result = fn();
724
725         if (initcall_debug) {
726                 disable_boot_trace();
727                 rettime = ktime_get();
728                 delta = ktime_sub(rettime, calltime);
729                 ret.duration = (unsigned long long) ktime_to_ns(delta) >> 10;
730                 trace_boot_ret(&ret, fn);
731                 printk("initcall %pF returned %d after %Ld usecs\n", fn,
732                         ret.result, ret.duration);
733         }
734
735         msgbuf[0] = 0;
736
737         if (ret.result && ret.result != -ENODEV && initcall_debug)
738                 sprintf(msgbuf, "error code %d ", ret.result);
739
740         if (preempt_count() != count) {
741                 strlcat(msgbuf, "preemption imbalance ", sizeof(msgbuf));
742                 preempt_count() = count;
743         }
744         if (irqs_disabled()) {
745                 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
746                 local_irq_enable();
747         }
748         if (msgbuf[0]) {
749                 printk("initcall %pF returned with %s\n", fn, msgbuf);
750         }
751
752         return ret.result;
753 }
754
755
756 extern initcall_t __initcall_start[], __initcall_end[], __early_initcall_end[];
757
758 static void __init do_initcalls(void)
759 {
760         initcall_t *call;
761
762         for (call = __early_initcall_end; call < __initcall_end; call++)
763                 do_one_initcall(*call);
764
765         /* Make sure there is no pending stuff from the initcall sequence */
766         flush_scheduled_work();
767 }
768
769 /*
770  * Ok, the machine is now initialized. None of the devices
771  * have been touched yet, but the CPU subsystem is up and
772  * running, and memory and process management works.
773  *
774  * Now we can finally start doing some real work..
775  */
776 static void __init do_basic_setup(void)
777 {
778         rcu_init_sched(); /* needed by module_init stage. */
779         init_workqueues();
780         usermodehelper_init();
781         driver_init();
782         init_irq_proc();
783         do_initcalls();
784 }
785
786 static void __init do_pre_smp_initcalls(void)
787 {
788         initcall_t *call;
789
790         for (call = __initcall_start; call < __early_initcall_end; call++)
791                 do_one_initcall(*call);
792 }
793
794 static void run_init_process(char *init_filename)
795 {
796         argv_init[0] = init_filename;
797         kernel_execve(init_filename, argv_init, envp_init);
798 }
799
800 /* This is a non __init function. Force it to be noinline otherwise gcc
801  * makes it inline to init() and it becomes part of init.text section
802  */
803 static int noinline init_post(void)
804 {
805         free_initmem();
806         unlock_kernel();
807         mark_rodata_ro();
808         system_state = SYSTEM_RUNNING;
809         numa_default_policy();
810
811         if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
812                 printk(KERN_WARNING "Warning: unable to open an initial console.\n");
813
814         (void) sys_dup(0);
815         (void) sys_dup(0);
816
817         current->signal->flags |= SIGNAL_UNKILLABLE;
818
819         if (ramdisk_execute_command) {
820                 run_init_process(ramdisk_execute_command);
821                 printk(KERN_WARNING "Failed to execute %s\n",
822                                 ramdisk_execute_command);
823         }
824
825         /*
826          * We try each of these until one succeeds.
827          *
828          * The Bourne shell can be used instead of init if we are
829          * trying to recover a really broken machine.
830          */
831         if (execute_command) {
832                 run_init_process(execute_command);
833                 printk(KERN_WARNING "Failed to execute %s.  Attempting "
834                                         "defaults...\n", execute_command);
835         }
836         run_init_process("/sbin/init");
837         run_init_process("/etc/init");
838         run_init_process("/bin/init");
839         run_init_process("/bin/sh");
840
841         panic("No init found.  Try passing init= option to kernel.");
842 }
843
844 static int __init kernel_init(void * unused)
845 {
846         lock_kernel();
847         /*
848          * init can run on any cpu.
849          */
850         set_cpus_allowed_ptr(current, CPU_MASK_ALL_PTR);
851         /*
852          * Tell the world that we're going to be the grim
853          * reaper of innocent orphaned children.
854          *
855          * We don't want people to have to make incorrect
856          * assumptions about where in the task array this
857          * can be found.
858          */
859         init_pid_ns.child_reaper = current;
860
861         cad_pid = task_pid(current);
862
863         smp_prepare_cpus(setup_max_cpus);
864
865         do_pre_smp_initcalls();
866         start_boot_trace();
867
868         smp_init();
869         sched_init_smp();
870
871         cpuset_init_smp();
872
873         do_basic_setup();
874
875         /*
876          * check if there is an early userspace init.  If yes, let it do all
877          * the work
878          */
879
880         if (!ramdisk_execute_command)
881                 ramdisk_execute_command = "/init";
882
883         if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
884                 ramdisk_execute_command = NULL;
885                 prepare_namespace();
886         }
887
888         /*
889          * Ok, we have completed the initial bootup, and
890          * we're essentially up and running. Get rid of the
891          * initmem segments and start the user-mode stuff..
892          */
893
894         init_post();
895         return 0;
896 }