blob: 1174ae3aec8c0741f7ec3303c241816613ce3ff0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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/hdreg.h>
26#include <linux/bootmem.h>
27#include <linux/tty.h>
28#include <linux/gfp.h>
29#include <linux/percpu.h>
30#include <linux/kmod.h>
31#include <linux/kernel_stat.h>
Rusty Russelld7cd5612006-12-07 02:14:08 +010032#include <linux/start_kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/security.h>
34#include <linux/workqueue.h>
35#include <linux/profile.h>
36#include <linux/rcupdate.h>
37#include <linux/moduleparam.h>
38#include <linux/kallsyms.h>
39#include <linux/writeback.h>
40#include <linux/cpu.h>
41#include <linux/cpuset.h>
42#include <linux/efi.h>
Shailabh Nagarc7572492006-07-14 00:24:40 -070043#include <linux/taskstats_kern.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070044#include <linux/delayacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/unistd.h>
46#include <linux/rmap.h>
47#include <linux/mempolicy.h>
48#include <linux/key.h>
Jan Beulich4552d5d2006-06-26 13:57:28 +020049#include <linux/unwind.h>
Adrian Bunkb6cd0b72006-06-27 02:53:54 -070050#include <linux/buffer_head.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070051#include <linux/debug_locks.h>
Ingo Molnarfbb9ce92006-07-03 00:24:50 -070052#include <linux/lockdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <asm/io.h>
55#include <asm/bugs.h>
56#include <asm/setup.h>
Andi Kleena9401992005-07-28 21:15:30 -070057#include <asm/sections.h>
Arjan van de Ven37b73c82006-01-06 00:12:01 -080058#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_X86_LOCAL_APIC
61#include <asm/smp.h>
62#endif
63
64/*
Coywolf Qi Hunt69c99ac2006-01-14 13:21:11 -080065 * This is one of the first .c files built. Error out early if we have compiler
66 * trouble.
67 *
68 * Versions of gcc older than that listed below may actually compile and link
69 * okay, but the end product can have subtle run time bugs. To avoid associated
70 * bogus bug reports, we flatly refuse to compile with a gcc that is known to be
71 * too old from the very beginning.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Andrew Mortonfd285bb2006-01-08 01:04:07 -080073#if (__GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#error Sorry, your GCC is too old. It builds incorrect kernels.
75#endif
76
Andrew Mortonf1a60db2006-12-06 20:39:59 -080077#if __GNUC__ == 4 && __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ == 0
78#warning gcc-4.1.0 is known to miscompile the kernel. A different compiler version is recommended.
79#endif
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int init(void *);
82
83extern void init_IRQ(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084extern void fork_init(unsigned long);
85extern void mca_init(void);
86extern void sbus_init(void);
87extern void sysctl_init(void);
88extern void signals_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089extern void pidhash_init(void);
90extern void pidmap_init(void);
91extern void prio_tree_init(void);
92extern void radix_tree_init(void);
93extern void free_initmem(void);
94extern void populate_rootfs(void);
95extern void driver_init(void);
96extern void prepare_namespace(void);
97#ifdef CONFIG_ACPI
98extern void acpi_early_init(void);
99#else
100static inline void acpi_early_init(void) { }
101#endif
Arjan van de Ven37b73c82006-01-06 00:12:01 -0800102#ifndef CONFIG_DEBUG_RODATA
103static inline void mark_rodata_ro(void) { }
104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#ifdef CONFIG_TC
107extern void tc_init(void);
108#endif
109
110enum system_states system_state;
111EXPORT_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
119extern void time_init(void);
120/* Default late time init is NULL. archs can override this later. */
121void (*late_time_init)(void);
122extern void softirq_init(void);
123
124/* Untouched command line (eg. for /proc) saved by arch-specific code. */
125char saved_command_line[COMMAND_LINE_SIZE];
126
127static char *execute_command;
Olof Johanssonffdfc402005-09-06 15:17:19 -0700128static char *ramdisk_execute_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* Setup configured maximum number of CPUs to activate */
131static unsigned int max_cpus = NR_CPUS;
132
133/*
Vivek Goyal7e962872006-09-27 01:50:44 -0700134 * If set, this is an indication to the drivers that reset the underlying
135 * device before going ahead with the initialization otherwise driver might
136 * rely on the BIOS and skip the reset operation.
137 *
138 * This is useful if kernel is booting in an unreliable environment.
139 * For ex. kdump situaiton where previous kernel has crashed, BIOS has been
140 * skipped and devices will be in unknown state.
141 */
142unsigned int reset_devices;
143EXPORT_SYMBOL(reset_devices);
144
145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * Setup routine for controlling SMP activation
147 *
148 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
149 * activation entirely (the MPS table probe still happens, though).
150 *
151 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
152 * greater than 0, limits the maximum number of CPUs activated in
153 * SMP mode to <NUM>.
154 */
155static int __init nosmp(char *str)
156{
157 max_cpus = 0;
158 return 1;
159}
160
161__setup("nosmp", nosmp);
162
163static int __init maxcpus(char *str)
164{
165 get_option(&str, &max_cpus);
166 return 1;
167}
168
169__setup("maxcpus=", maxcpus);
170
Vivek Goyal7e962872006-09-27 01:50:44 -0700171static int __init set_reset_devices(char *str)
172{
173 reset_devices = 1;
174 return 1;
175}
176
177__setup("reset_devices", set_reset_devices);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
180char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
181static const char *panic_later, *panic_param;
182
183extern struct obs_kernel_param __setup_start[], __setup_end[];
184
185static int __init obsolete_checksetup(char *line)
186{
187 struct obs_kernel_param *p;
Rusty Russell33df0d12006-09-26 10:52:32 +0200188 int had_early_param = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 p = __setup_start;
191 do {
192 int n = strlen(p->str);
193 if (!strncmp(line, p->str, n)) {
194 if (p->early) {
Rusty Russell33df0d12006-09-26 10:52:32 +0200195 /* Already done in parse_early_param?
196 * (Needs exact match on param part).
197 * Keep iterating, as we can have early
198 * params and __setups of same names 8( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (line[n] == '\0' || line[n] == '=')
Rusty Russell33df0d12006-09-26 10:52:32 +0200200 had_early_param = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 } else if (!p->setup_func) {
202 printk(KERN_WARNING "Parameter %s is obsolete,"
203 " ignored\n", p->str);
204 return 1;
205 } else if (p->setup_func(line + n))
206 return 1;
207 }
208 p++;
209 } while (p < __setup_end);
Rusty Russell33df0d12006-09-26 10:52:32 +0200210
211 return had_early_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * This should be approx 2 Bo*oMips to start (note initial shift), and will
216 * still work even if initially too large, it will just take slightly longer
217 */
218unsigned long loops_per_jiffy = (1<<12);
219
220EXPORT_SYMBOL(loops_per_jiffy);
221
222static int __init debug_kernel(char *str)
223{
224 if (*str)
225 return 0;
226 console_loglevel = 10;
227 return 1;
228}
229
230static int __init quiet_kernel(char *str)
231{
232 if (*str)
233 return 0;
234 console_loglevel = 4;
235 return 1;
236}
237
238__setup("debug", debug_kernel);
239__setup("quiet", quiet_kernel);
240
241static int __init loglevel(char *str)
242{
243 get_option(&str, &console_loglevel);
244 return 1;
245}
246
247__setup("loglevel=", loglevel);
248
249/*
250 * Unknown boot options get handed to init, unless they look like
251 * failed parameters
252 */
253static int __init unknown_bootoption(char *param, char *val)
254{
255 /* Change NUL term back to "=", to make "param" the whole string. */
256 if (val) {
257 /* param=val or param="val"? */
258 if (val == param+strlen(param)+1)
259 val[-1] = '=';
260 else if (val == param+strlen(param)+2) {
261 val[-2] = '=';
262 memmove(val-1, val, strlen(val)+1);
263 val--;
264 } else
265 BUG();
266 }
267
268 /* Handle obsolete-style parameters */
269 if (obsolete_checksetup(param))
270 return 0;
271
272 /*
273 * Preemptive maintenance for "why didn't my mispelled command
274 * line work?"
275 */
276 if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
277 printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
278 return 0;
279 }
280
281 if (panic_later)
282 return 0;
283
284 if (val) {
285 /* Environment option */
286 unsigned int i;
287 for (i = 0; envp_init[i]; i++) {
288 if (i == MAX_INIT_ENVS) {
289 panic_later = "Too many boot env vars at `%s'";
290 panic_param = param;
291 }
292 if (!strncmp(param, envp_init[i], val - param))
293 break;
294 }
295 envp_init[i] = param;
296 } else {
297 /* Command line option */
298 unsigned int i;
299 for (i = 0; argv_init[i]; i++) {
300 if (i == MAX_INIT_ARGS) {
301 panic_later = "Too many boot init vars at `%s'";
302 panic_param = param;
303 }
304 }
305 argv_init[i] = param;
306 }
307 return 0;
308}
309
310static int __init init_setup(char *str)
311{
312 unsigned int i;
313
314 execute_command = str;
315 /*
316 * In case LILO is going to boot us with default command line,
317 * it prepends "auto" before the whole cmdline which makes
318 * the shell think it should execute a script with such name.
319 * So we ignore all arguments entered _before_ init=... [MJ]
320 */
321 for (i = 1; i < MAX_INIT_ARGS; i++)
322 argv_init[i] = NULL;
323 return 1;
324}
325__setup("init=", init_setup);
326
Olof Johanssonffdfc402005-09-06 15:17:19 -0700327static int __init rdinit_setup(char *str)
328{
329 unsigned int i;
330
331 ramdisk_execute_command = str;
332 /* See "auto" comment in init_setup */
333 for (i = 1; i < MAX_INIT_ARGS; i++)
334 argv_init[i] = NULL;
335 return 1;
336}
337__setup("rdinit=", rdinit_setup);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#ifndef CONFIG_SMP
340
341#ifdef CONFIG_X86_LOCAL_APIC
342static void __init smp_init(void)
343{
344 APIC_init_uniprocessor();
345}
346#else
347#define smp_init() do { } while (0)
348#endif
349
350static inline void setup_per_cpu_areas(void) { }
351static inline void smp_prepare_cpus(unsigned int maxcpus) { }
352
353#else
354
355#ifdef __GENERIC_PER_CPU
Eric Dumazetb73b4592006-03-23 03:01:07 -0800356unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358EXPORT_SYMBOL(__per_cpu_offset);
359
360static void __init setup_per_cpu_areas(void)
361{
362 unsigned long size, i;
363 char *ptr;
Eric Dumazet63872f82006-03-23 03:01:04 -0800364 unsigned long nr_possible_cpus = num_possible_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* Copy section for each CPU (we discard the original) */
367 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
368#ifdef CONFIG_MODULES
369 if (size < PERCPU_ENOUGH_ROOM)
370 size = PERCPU_ENOUGH_ROOM;
371#endif
Eric Dumazet63872f82006-03-23 03:01:04 -0800372 ptr = alloc_bootmem(size * nr_possible_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800374 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 __per_cpu_offset[i] = ptr - __per_cpu_start;
376 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
Eric Dumazet63872f82006-03-23 03:01:04 -0800377 ptr += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379}
380#endif /* !__GENERIC_PER_CPU */
381
382/* Called by boot processor to activate the rest. */
383static void __init smp_init(void)
384{
385 unsigned int i;
386
387 /* FIXME: This should be done in userspace --RR */
388 for_each_present_cpu(i) {
389 if (num_online_cpus() >= max_cpus)
390 break;
391 if (!cpu_online(i))
392 cpu_up(i);
393 }
394
395 /* Any cleanup work */
396 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
397 smp_cpus_done(max_cpus);
398#if 0
399 /* Get other processors into their bootup holding patterns. */
400
401 smp_commence();
402#endif
403}
404
405#endif
406
407/*
408 * We need to finalize in a non-__init function or else race conditions
409 * between the root thread and the init thread may cause start_kernel to
410 * be reaped by free_initmem before the root thread has proceeded to
411 * cpu_idle.
412 *
413 * gcc-3.4 accidentally inlines this function, so use noinline.
414 */
415
416static void noinline rest_init(void)
417 __releases(kernel_lock)
418{
419 kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
420 numa_default_policy();
421 unlock_kernel();
Ingo Molnarf340c0d2005-06-28 16:40:42 +0200422
423 /*
424 * The boot idle thread must execute schedule()
425 * at least one to get things moving:
426 */
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800427 preempt_enable_no_resched();
Ingo Molnarf340c0d2005-06-28 16:40:42 +0200428 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800429 preempt_disable();
Ingo Molnarf340c0d2005-06-28 16:40:42 +0200430
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800431 /* Call into cpu_idle with preempt disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 cpu_idle();
433}
434
435/* Check for early params. */
436static int __init do_early_param(char *param, char *val)
437{
438 struct obs_kernel_param *p;
439
440 for (p = __setup_start; p < __setup_end; p++) {
441 if (p->early && strcmp(param, p->str) == 0) {
442 if (p->setup_func(val) != 0)
443 printk(KERN_WARNING
444 "Malformed early option '%s'\n", param);
445 }
446 }
447 /* We accept everything at this stage. */
448 return 0;
449}
450
451/* Arch code calls this early on, or if not, just before other parsing. */
452void __init parse_early_param(void)
453{
454 static __initdata int done = 0;
455 static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
456
457 if (done)
458 return;
459
460 /* All fall through to do_early_param. */
461 strlcpy(tmp_cmdline, saved_command_line, COMMAND_LINE_SIZE);
462 parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
463 done = 1;
464}
465
466/*
467 * Activate the first processor.
468 */
469
Stas Sergeev44fd2292006-03-23 02:59:44 -0800470static void __init boot_cpu_init(void)
471{
472 int cpu = smp_processor_id();
473 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
474 cpu_set(cpu, cpu_online_map);
475 cpu_set(cpu, cpu_present_map);
476 cpu_set(cpu, cpu_possible_map);
477}
478
Andrew Morton033ab7f2006-06-30 01:55:50 -0700479void __init __attribute__((weak)) smp_setup_processor_id(void)
480{
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483asmlinkage void __init start_kernel(void)
484{
485 char * command_line;
486 extern struct kernel_param __start___param[], __stop___param[];
Andrew Morton033ab7f2006-06-30 01:55:50 -0700487
488 smp_setup_processor_id();
489
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700490 /*
491 * Need to run as early as possible, to initialize the
492 * lockdep hash:
493 */
Andi Kleenc9538ed2006-09-26 10:52:34 +0200494 unwind_init();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700495 lockdep_init();
496
497 local_irq_disable();
498 early_boot_irqs_off();
Ingo Molnar243c7622006-07-03 00:25:06 -0700499 early_init_irq_lock_class();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * Interrupts are still disabled. Do necessary setups, then
503 * enable them
504 */
505 lock_kernel();
Stas Sergeev44fd2292006-03-23 02:59:44 -0800506 boot_cpu_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 page_address_init();
508 printk(KERN_NOTICE);
509 printk(linux_banner);
510 setup_arch(&command_line);
Jan Beulich690a9732006-10-21 18:37:01 +0200511 unwind_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 setup_per_cpu_areas();
Stas Sergeev44fd2292006-03-23 02:59:44 -0800513 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * Set up the scheduler prior starting any interrupts (such as the
517 * timer interrupt). Full topology setup happens at smp_init()
518 * time - but meanwhile we still have a functioning scheduler.
519 */
520 sched_init();
521 /*
522 * Disable preemption - early bootup scheduling is extremely
523 * fragile until we cpu_idle() for the first time.
524 */
525 preempt_disable();
526 build_all_zonelists();
527 page_alloc_init();
528 printk(KERN_NOTICE "Kernel command line: %s\n", saved_command_line);
529 parse_early_param();
530 parse_args("Booting kernel", command_line, __start___param,
531 __stop___param - __start___param,
532 &unknown_bootoption);
533 sort_main_extable();
534 trap_init();
535 rcu_init();
536 init_IRQ();
537 pidhash_init();
538 init_timers();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800539 hrtimers_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 softirq_init();
john stultzad596172006-06-26 00:25:06 -0700541 timekeeping_init();
john stultz88fecaa2006-07-03 00:24:04 -0700542 time_init();
Heiko Carstens93e02812006-07-03 00:24:24 -0700543 profile_init();
544 if (!irqs_disabled())
545 printk("start_kernel(): bug: interrupts were enabled early\n");
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700546 early_boot_irqs_on();
Heiko Carstens93e02812006-07-03 00:24:24 -0700547 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /*
550 * HACK ALERT! This is early. We're enabling the console before
551 * we've done PCI setups etc, and console_init() must be aware of
552 * this. But we do want output early, in case something goes wrong.
553 */
554 console_init();
555 if (panic_later)
556 panic(panic_later, panic_param);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700557
558 lockdep_info();
559
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700560 /*
561 * Need to run this when irqs are enabled, because it wants
562 * to self-test [hard/soft]-irqs on/off lock inversion bugs
563 * too:
564 */
565 locking_selftest();
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#ifdef CONFIG_BLK_DEV_INITRD
568 if (initrd_start && !initrd_below_start_ok &&
569 initrd_start < min_low_pfn << PAGE_SHIFT) {
570 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
571 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
572 initrd_start = 0;
573 }
574#endif
575 vfs_caches_init_early();
Paul Jacksonc417f022006-01-08 01:02:01 -0800576 cpuset_init_early();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 mem_init();
578 kmem_cache_init();
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700579 setup_per_cpu_pageset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 numa_policy_init();
581 if (late_time_init)
582 late_time_init();
583 calibrate_delay();
584 pidmap_init();
585 pgtable_cache_init();
586 prio_tree_init();
587 anon_vma_init();
588#ifdef CONFIG_X86
589 if (efi_enabled)
590 efi_enter_virtual_mode();
591#endif
592 fork_init(num_physpages);
593 proc_caches_init();
594 buffer_init();
595 unnamed_dev_init();
596 key_init();
597 security_init();
598 vfs_caches_init(num_physpages);
599 radix_tree_init();
600 signals_init();
601 /* rootfs populating might need page-writeback */
602 page_writeback_init();
603#ifdef CONFIG_PROC_FS
604 proc_root_init();
605#endif
606 cpuset_init();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700607 taskstats_init_early();
Shailabh Nagarca74e922006-07-14 00:24:36 -0700608 delayacct_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 check_bugs();
611
612 acpi_early_init(); /* before LAPIC and SMP init */
613
614 /* Do the rest non-__init'ed, we're now alive */
615 rest_init();
616}
617
618static int __initdata initcall_debug;
619
620static int __init initcall_debug_setup(char *str)
621{
622 initcall_debug = 1;
623 return 1;
624}
625__setup("initcall_debug", initcall_debug_setup);
626
627struct task_struct *child_reaper = &init_task;
628
629extern initcall_t __initcall_start[], __initcall_end[];
630
631static void __init do_initcalls(void)
632{
633 initcall_t *call;
634 int count = preempt_count();
635
636 for (call = __initcall_start; call < __initcall_end; call++) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800637 char *msg = NULL;
638 char msgbuf[40];
639 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (initcall_debug) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800642 printk("Calling initcall 0x%p", *call);
643 print_fn_descriptor_symbol(": %s()",
644 (unsigned long) *call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 printk("\n");
646 }
647
Andrew Mortonc1cda482006-03-25 03:07:15 -0800648 result = (*call)();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Andrew Mortonf3a19cb2006-05-01 12:15:52 -0700650 if (result && result != -ENODEV && initcall_debug) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800651 sprintf(msgbuf, "error code %d", result);
652 msg = msgbuf;
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (preempt_count() != count) {
655 msg = "preemption imbalance";
656 preempt_count() = count;
657 }
658 if (irqs_disabled()) {
659 msg = "disabled interrupts";
660 local_irq_enable();
661 }
662 if (msg) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800663 printk(KERN_WARNING "initcall at 0x%p", *call);
664 print_fn_descriptor_symbol(": %s()",
665 (unsigned long) *call);
666 printk(": returned with %s\n", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 }
669
670 /* Make sure there is no pending stuff from the initcall sequence */
671 flush_scheduled_work();
672}
673
674/*
675 * Ok, the machine is now initialized. None of the devices
676 * have been touched yet, but the CPU subsystem is up and
677 * running, and memory and process management works.
678 *
679 * Now we can finally start doing some real work..
680 */
681static void __init do_basic_setup(void)
682{
683 /* drivers will send hotplug events */
684 init_workqueues();
685 usermodehelper_init();
686 driver_init();
687
688#ifdef CONFIG_SYSCTL
689 sysctl_init();
690#endif
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 do_initcalls();
693}
694
695static void do_pre_smp_initcalls(void)
696{
697 extern int spawn_ksoftirqd(void);
698#ifdef CONFIG_SMP
699 extern int migration_init(void);
700
701 migration_init();
702#endif
703 spawn_ksoftirqd();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700704 spawn_softlockup_task();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static void run_init_process(char *init_filename)
708{
709 argv_init[0] = init_filename;
Arnd Bergmann67608562006-10-02 02:18:26 -0700710 kernel_execve(init_filename, argv_init, envp_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static int init(void * unused)
714{
715 lock_kernel();
716 /*
717 * init can run on any cpu.
718 */
719 set_cpus_allowed(current, CPU_MASK_ALL);
720 /*
721 * Tell the world that we're going to be the grim
722 * reaper of innocent orphaned children.
723 *
724 * We don't want people to have to make incorrect
725 * assumptions about where in the task array this
726 * can be found.
727 */
728 child_reaper = current;
729
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700730 cad_pid = task_pid(current);
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 smp_prepare_cpus(max_cpus);
733
734 do_pre_smp_initcalls();
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 smp_init();
737 sched_init_smp();
738
739 cpuset_init_smp();
740
741 /*
742 * Do this before initcalls, because some drivers want to access
743 * firmware files.
744 */
745 populate_rootfs();
746
747 do_basic_setup();
748
749 /*
750 * check if there is an early userspace init. If yes, let it do all
751 * the work
752 */
Olof Johanssonffdfc402005-09-06 15:17:19 -0700753
754 if (!ramdisk_execute_command)
755 ramdisk_execute_command = "/init";
756
757 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
758 ramdisk_execute_command = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 prepare_namespace();
Olof Johanssonffdfc402005-09-06 15:17:19 -0700760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /*
763 * Ok, we have completed the initial bootup, and
764 * we're essentially up and running. Get rid of the
765 * initmem segments and start the user-mode stuff..
766 */
767 free_initmem();
768 unlock_kernel();
Arjan van de Ven37b73c82006-01-06 00:12:01 -0800769 mark_rodata_ro();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 system_state = SYSTEM_RUNNING;
771 numa_default_policy();
772
773 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
774 printk(KERN_WARNING "Warning: unable to open an initial console.\n");
775
776 (void) sys_dup(0);
777 (void) sys_dup(0);
Olof Johanssonffdfc402005-09-06 15:17:19 -0700778
779 if (ramdisk_execute_command) {
780 run_init_process(ramdisk_execute_command);
781 printk(KERN_WARNING "Failed to execute %s\n",
782 ramdisk_execute_command);
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
786 * We try each of these until one succeeds.
787 *
788 * The Bourne shell can be used instead of init if we are
789 * trying to recover a really broken machine.
790 */
Avery, Brianc1d7ef72005-09-06 15:16:56 -0700791 if (execute_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 run_init_process(execute_command);
Avery, Brianc1d7ef72005-09-06 15:16:56 -0700793 printk(KERN_WARNING "Failed to execute %s. Attempting "
794 "defaults...\n", execute_command);
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 run_init_process("/sbin/init");
797 run_init_process("/etc/init");
798 run_init_process("/bin/init");
799 run_init_process("/bin/sh");
800
801 panic("No init found. Try passing init= option to kernel.");
802}