blob: 8b4a7d76916235df139b98bd41019a228615a2dd [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>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080053#include <linux/pid_namespace.h>
Adrian Bunk1f217822006-12-19 13:01:28 -080054#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/io.h>
57#include <asm/bugs.h>
58#include <asm/setup.h>
Andi Kleena9401992005-07-28 21:15:30 -070059#include <asm/sections.h>
Arjan van de Ven37b73c82006-01-06 00:12:01 -080060#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_X86_LOCAL_APIC
63#include <asm/smp.h>
64#endif
65
66/*
Coywolf Qi Hunt69c99ac2006-01-14 13:21:11 -080067 * This is one of the first .c files built. Error out early if we have compiler
68 * trouble.
69 *
70 * Versions of gcc older than that listed below may actually compile and link
71 * okay, but the end product can have subtle run time bugs. To avoid associated
72 * bogus bug reports, we flatly refuse to compile with a gcc that is known to be
73 * too old from the very beginning.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Andrew Mortonfd285bb2006-01-08 01:04:07 -080075#if (__GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#error Sorry, your GCC is too old. It builds incorrect kernels.
77#endif
78
Andrew Mortonf1a60db2006-12-06 20:39:59 -080079#if __GNUC__ == 4 && __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ == 0
80#warning gcc-4.1.0 is known to miscompile the kernel. A different compiler version is recommended.
81#endif
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int init(void *);
84
85extern void init_IRQ(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086extern void fork_init(unsigned long);
87extern void mca_init(void);
88extern void sbus_init(void);
89extern void sysctl_init(void);
90extern void signals_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091extern void pidhash_init(void);
92extern void pidmap_init(void);
93extern void prio_tree_init(void);
94extern void radix_tree_init(void);
95extern void free_initmem(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096extern 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);
Linus Torvalds89937802006-12-11 09:28:46 -0800509 printk(linux_banner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 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);
Ard van Breemenc4a68302007-01-05 16:36:19 -0800533 if (!irqs_disabled()) {
534 printk(KERN_WARNING "start_kernel(): bug: interrupts were "
535 "enabled *very* early, fixing it\n");
536 local_irq_disable();
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 sort_main_extable();
539 trap_init();
540 rcu_init();
541 init_IRQ();
542 pidhash_init();
543 init_timers();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800544 hrtimers_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 softirq_init();
john stultzad596172006-06-26 00:25:06 -0700546 timekeeping_init();
john stultz88fecaa2006-07-03 00:24:04 -0700547 time_init();
Heiko Carstens93e02812006-07-03 00:24:24 -0700548 profile_init();
549 if (!irqs_disabled())
550 printk("start_kernel(): bug: interrupts were enabled early\n");
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700551 early_boot_irqs_on();
Heiko Carstens93e02812006-07-03 00:24:24 -0700552 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /*
555 * HACK ALERT! This is early. We're enabling the console before
556 * we've done PCI setups etc, and console_init() must be aware of
557 * this. But we do want output early, in case something goes wrong.
558 */
559 console_init();
560 if (panic_later)
561 panic(panic_later, panic_param);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700562
563 lockdep_info();
564
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700565 /*
566 * Need to run this when irqs are enabled, because it wants
567 * to self-test [hard/soft]-irqs on/off lock inversion bugs
568 * too:
569 */
570 locking_selftest();
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#ifdef CONFIG_BLK_DEV_INITRD
573 if (initrd_start && !initrd_below_start_ok &&
574 initrd_start < min_low_pfn << PAGE_SHIFT) {
575 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
576 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
577 initrd_start = 0;
578 }
579#endif
580 vfs_caches_init_early();
Paul Jacksonc417f022006-01-08 01:02:01 -0800581 cpuset_init_early();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 mem_init();
583 kmem_cache_init();
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700584 setup_per_cpu_pageset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 numa_policy_init();
586 if (late_time_init)
587 late_time_init();
588 calibrate_delay();
589 pidmap_init();
590 pgtable_cache_init();
591 prio_tree_init();
592 anon_vma_init();
593#ifdef CONFIG_X86
594 if (efi_enabled)
595 efi_enter_virtual_mode();
596#endif
597 fork_init(num_physpages);
598 proc_caches_init();
599 buffer_init();
600 unnamed_dev_init();
601 key_init();
602 security_init();
603 vfs_caches_init(num_physpages);
604 radix_tree_init();
605 signals_init();
606 /* rootfs populating might need page-writeback */
607 page_writeback_init();
608#ifdef CONFIG_PROC_FS
609 proc_root_init();
610#endif
611 cpuset_init();
Shailabh Nagarc7572492006-07-14 00:24:40 -0700612 taskstats_init_early();
Shailabh Nagarca74e922006-07-14 00:24:36 -0700613 delayacct_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 check_bugs();
616
617 acpi_early_init(); /* before LAPIC and SMP init */
618
619 /* Do the rest non-__init'ed, we're now alive */
620 rest_init();
621}
622
623static int __initdata initcall_debug;
624
625static int __init initcall_debug_setup(char *str)
626{
627 initcall_debug = 1;
628 return 1;
629}
630__setup("initcall_debug", initcall_debug_setup);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632extern initcall_t __initcall_start[], __initcall_end[];
633
634static void __init do_initcalls(void)
635{
636 initcall_t *call;
637 int count = preempt_count();
638
639 for (call = __initcall_start; call < __initcall_end; call++) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800640 char *msg = NULL;
641 char msgbuf[40];
642 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (initcall_debug) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800645 printk("Calling initcall 0x%p", *call);
646 print_fn_descriptor_symbol(": %s()",
647 (unsigned long) *call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 printk("\n");
649 }
650
Andrew Mortonc1cda482006-03-25 03:07:15 -0800651 result = (*call)();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Andrew Mortonf3a19cb2006-05-01 12:15:52 -0700653 if (result && result != -ENODEV && initcall_debug) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800654 sprintf(msgbuf, "error code %d", result);
655 msg = msgbuf;
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (preempt_count() != count) {
658 msg = "preemption imbalance";
659 preempt_count() = count;
660 }
661 if (irqs_disabled()) {
662 msg = "disabled interrupts";
663 local_irq_enable();
664 }
665 if (msg) {
Andrew Mortonc1cda482006-03-25 03:07:15 -0800666 printk(KERN_WARNING "initcall at 0x%p", *call);
667 print_fn_descriptor_symbol(": %s()",
668 (unsigned long) *call);
669 printk(": returned with %s\n", msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 }
672
673 /* Make sure there is no pending stuff from the initcall sequence */
674 flush_scheduled_work();
675}
676
677/*
678 * Ok, the machine is now initialized. None of the devices
679 * have been touched yet, but the CPU subsystem is up and
680 * running, and memory and process management works.
681 *
682 * Now we can finally start doing some real work..
683 */
684static void __init do_basic_setup(void)
685{
686 /* drivers will send hotplug events */
687 init_workqueues();
688 usermodehelper_init();
689 driver_init();
690
691#ifdef CONFIG_SYSCTL
692 sysctl_init();
693#endif
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 do_initcalls();
696}
697
Vivek Goyal88d20322007-01-11 01:52:44 +0100698static void __init do_pre_smp_initcalls(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 extern int spawn_ksoftirqd(void);
701#ifdef CONFIG_SMP
702 extern int migration_init(void);
703
704 migration_init();
705#endif
706 spawn_ksoftirqd();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700707 spawn_softlockup_task();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void run_init_process(char *init_filename)
711{
712 argv_init[0] = init_filename;
Arnd Bergmann67608562006-10-02 02:18:26 -0700713 kernel_execve(init_filename, argv_init, envp_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int init(void * unused)
717{
718 lock_kernel();
719 /*
720 * init can run on any cpu.
721 */
722 set_cpus_allowed(current, CPU_MASK_ALL);
723 /*
724 * Tell the world that we're going to be the grim
725 * reaper of innocent orphaned children.
726 *
727 * We don't want people to have to make incorrect
728 * assumptions about where in the task array this
729 * can be found.
730 */
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -0800731 init_pid_ns.child_reaper = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700733 cad_pid = task_pid(current);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 smp_prepare_cpus(max_cpus);
736
737 do_pre_smp_initcalls();
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 smp_init();
740 sched_init_smp();
741
742 cpuset_init_smp();
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 do_basic_setup();
745
746 /*
747 * check if there is an early userspace init. If yes, let it do all
748 * the work
749 */
Olof Johanssonffdfc402005-09-06 15:17:19 -0700750
751 if (!ramdisk_execute_command)
752 ramdisk_execute_command = "/init";
753
754 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
755 ramdisk_execute_command = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 prepare_namespace();
Olof Johanssonffdfc402005-09-06 15:17:19 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * Ok, we have completed the initial bootup, and
761 * we're essentially up and running. Get rid of the
762 * initmem segments and start the user-mode stuff..
763 */
764 free_initmem();
765 unlock_kernel();
Arjan van de Ven37b73c82006-01-06 00:12:01 -0800766 mark_rodata_ro();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 system_state = SYSTEM_RUNNING;
768 numa_default_policy();
769
770 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
771 printk(KERN_WARNING "Warning: unable to open an initial console.\n");
772
773 (void) sys_dup(0);
774 (void) sys_dup(0);
Olof Johanssonffdfc402005-09-06 15:17:19 -0700775
776 if (ramdisk_execute_command) {
777 run_init_process(ramdisk_execute_command);
778 printk(KERN_WARNING "Failed to execute %s\n",
779 ramdisk_execute_command);
780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /*
783 * We try each of these until one succeeds.
784 *
785 * The Bourne shell can be used instead of init if we are
786 * trying to recover a really broken machine.
787 */
Avery, Brianc1d7ef72005-09-06 15:16:56 -0700788 if (execute_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 run_init_process(execute_command);
Avery, Brianc1d7ef72005-09-06 15:16:56 -0700790 printk(KERN_WARNING "Failed to execute %s. Attempting "
791 "defaults...\n", execute_command);
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 run_init_process("/sbin/init");
794 run_init_process("/etc/init");
795 run_init_process("/bin/init");
796 run_init_process("/bin/sh");
797
798 panic("No init found. Try passing init= option to kernel.");
799}