]> bbs.cooldavid.org Git - net-next-2.6.git/blame - init/main.c
relayfs: replace BUG() with WARN_ON() in relay_late_setup_files()
[net-next-2.6.git] / init / main.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/proc_fs.h>
1da177e4
LT
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>
1da177e4
LT
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>
db64fe02 30#include <linux/vmalloc.h>
1da177e4 31#include <linux/kernel_stat.h>
d7cd5611 32#include <linux/start_kernel.h>
1da177e4 33#include <linux/security.h>
3d442233 34#include <linux/smp.h>
1da177e4
LT
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>
ddbcc7e8 43#include <linux/cgroup.h>
1da177e4 44#include <linux/efi.h>
906568c9 45#include <linux/tick.h>
6168a702 46#include <linux/interrupt.h>
c757249a 47#include <linux/taskstats_kern.h>
ca74e92b 48#include <linux/delayacct.h>
1da177e4
LT
49#include <linux/unistd.h>
50#include <linux/rmap.h>
51#include <linux/mempolicy.h>
52#include <linux/key.h>
4552d5dc 53#include <linux/unwind.h>
b6cd0b77 54#include <linux/buffer_head.h>
94b6da5a 55#include <linux/page_cgroup.h>
9a11b49a 56#include <linux/debug_locks.h>
3ac7fe5a 57#include <linux/debugobjects.h>
fbb9ce95 58#include <linux/lockdep.h>
84d73786 59#include <linux/pid_namespace.h>
1f21782e 60#include <linux/device.h>
73c27992 61#include <linux/kthread.h>
e6fe6649 62#include <linux/sched.h>
a1c9eea9 63#include <linux/signal.h>
199f0ca5 64#include <linux/idr.h>
68bf21aa 65#include <linux/ftrace.h>
3f5ec136 66#include <trace/boot.h>
1da177e4
LT
67
68#include <asm/io.h>
69#include <asm/bugs.h>
70#include <asm/setup.h>
a940199f 71#include <asm/sections.h>
37b73c82 72#include <asm/cacheflush.h>
b9ce08c0 73#include <linux/kmemtrace.h>
1da177e4 74
1da177e4
LT
75#ifdef CONFIG_X86_LOCAL_APIC
76#include <asm/smp.h>
77#endif
78
79/*
69c99ac1
CQH
80 * This is one of the first .c files built. Error out early if we have compiler
81 * trouble.
1da177e4 82 */
1da177e4 83
f1a60dbf
AM
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
aae5f662 88static int kernel_init(void *);
1da177e4
LT
89
90extern void init_IRQ(void);
1da177e4
LT
91extern void fork_init(unsigned long);
92extern void mca_init(void);
93extern void sbus_init(void);
1da177e4
LT
94extern void prio_tree_init(void);
95extern void radix_tree_init(void);
96extern void free_initmem(void);
1da177e4
LT
97#ifdef CONFIG_ACPI
98extern void acpi_early_init(void);
99#else
100static inline void acpi_early_init(void) { }
101#endif
37b73c82
AV
102#ifndef CONFIG_DEBUG_RODATA
103static inline void mark_rodata_ro(void) { }
104#endif
1da177e4
LT
105
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
30d7e0d4
ABL
124/* Untouched command line saved by arch-specific code. */
125char __initdata boot_command_line[COMMAND_LINE_SIZE];
126/* Untouched saved command line (eg. for /proc) */
127char *saved_command_line;
128/* Command line for parameter parsing */
129static char *static_command_line;
1da177e4
LT
130
131static char *execute_command;
ffdfc409 132static char *ramdisk_execute_command;
1da177e4 133
8b3b2955 134#ifdef CONFIG_SMP
1da177e4 135/* Setup configured maximum number of CPUs to activate */
ca74a6f8 136unsigned int __initdata setup_max_cpus = NR_CPUS;
7e96287d 137
1da177e4
LT
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 */
61ec7567
LB
148#ifndef CONFIG_X86_IO_APIC
149static inline void disable_ioapic_setup(void) {};
150#endif
151
1da177e4
LT
152static int __init nosmp(char *str)
153{
ca74a6f8 154 setup_max_cpus = 0;
61ec7567 155 disable_ioapic_setup();
8b3b2955 156 return 0;
1da177e4
LT
157}
158
8b3b2955 159early_param("nosmp", nosmp);
1da177e4
LT
160
161static int __init maxcpus(char *str)
162{
ca74a6f8
AK
163 get_option(&str, &setup_max_cpus);
164 if (setup_max_cpus == 0)
61ec7567
LB
165 disable_ioapic_setup();
166
167 return 0;
1da177e4
LT
168}
169
81340977 170early_param("maxcpus", maxcpus);
8b3b2955 171#else
ca74a6f8 172#define setup_max_cpus NR_CPUS
8b3b2955
JB
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 */
184unsigned int reset_devices;
185EXPORT_SYMBOL(reset_devices);
1da177e4 186
7e96287d
VG
187static 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
1da177e4
LT
195static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
196char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
197static const char *panic_later, *panic_param;
198
199extern struct obs_kernel_param __setup_start[], __setup_end[];
200
201static int __init obsolete_checksetup(char *line)
202{
203 struct obs_kernel_param *p;
33df0d19 204 int had_early_param = 0;
1da177e4
LT
205
206 p = __setup_start;
207 do {
208 int n = strlen(p->str);
209 if (!strncmp(line, p->str, n)) {
210 if (p->early) {
33df0d19
RR
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( */
1da177e4 215 if (line[n] == '\0' || line[n] == '=')
33df0d19 216 had_early_param = 1;
1da177e4
LT
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);
33df0d19
RR
226
227 return had_early_param;
1da177e4
LT
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 */
234unsigned long loops_per_jiffy = (1<<12);
235
236EXPORT_SYMBOL(loops_per_jiffy);
237
238static int __init debug_kernel(char *str)
239{
1da177e4 240 console_loglevel = 10;
f6f21c81 241 return 0;
1da177e4
LT
242}
243
244static int __init quiet_kernel(char *str)
245{
1da177e4 246 console_loglevel = 4;
f6f21c81 247 return 0;
1da177e4
LT
248}
249
f6f21c81
YL
250early_param("debug", debug_kernel);
251early_param("quiet", quiet_kernel);
1da177e4
LT
252
253static int __init loglevel(char *str)
254{
255 get_option(&str, &console_loglevel);
d9d4fcfe 256 return 0;
1da177e4
LT
257}
258
f6f21c81 259early_param("loglevel", loglevel);
1da177e4
LT
260
261/*
262 * Unknown boot options get handed to init, unless they look like
263 * failed parameters
264 */
265static 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 /*
211fee8a 285 * Preemptive maintenance for "why didn't my misspelled command
1da177e4
LT
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
12d6f21e
IM
322#ifdef CONFIG_DEBUG_PAGEALLOC
323int __read_mostly debug_pagealloc_enabled = 0;
324#endif
325
1da177e4
LT
326static 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
ffdfc409
OJ
343static 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
1da177e4
LT
355#ifndef CONFIG_SMP
356
357#ifdef CONFIG_X86_LOCAL_APIC
358static void __init smp_init(void)
359{
360 APIC_init_uniprocessor();
361}
362#else
363#define smp_init() do { } while (0)
364#endif
365
366static inline void setup_per_cpu_areas(void) { }
e0982e90 367static inline void setup_nr_cpu_ids(void) { }
1da177e4
LT
368static inline void smp_prepare_cpus(unsigned int maxcpus) { }
369
370#else
371
321a8e9d
MT
372#if NR_CPUS > BITS_PER_LONG
373cpumask_t cpu_mask_all __read_mostly = CPU_MASK_ALL;
374EXPORT_SYMBOL(cpu_mask_all);
375#endif
376
e0982e90
MT
377/* Setup number of possible processor ids */
378int nr_cpu_ids __read_mostly = NR_CPUS;
379EXPORT_SYMBOL(nr_cpu_ids);
380
381/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
382static 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
dd5af90a 392#ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
b73b459f 393unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
1da177e4
LT
394
395EXPORT_SYMBOL(__per_cpu_offset);
396
397static void __init setup_per_cpu_areas(void)
398{
399 unsigned long size, i;
400 char *ptr;
63872f87 401 unsigned long nr_possible_cpus = num_possible_cpus();
1da177e4
LT
402
403 /* Copy section for each CPU (we discard the original) */
b6e3590f
JF
404 size = ALIGN(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
405 ptr = alloc_bootmem_pages(size * nr_possible_cpus);
1da177e4 406
0a945022 407 for_each_possible_cpu(i) {
1da177e4
LT
408 __per_cpu_offset[i] = ptr - __per_cpu_start;
409 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
63872f87 410 ptr += size;
1da177e4
LT
411 }
412}
dd5af90a 413#endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */
1da177e4
LT
414
415/* Called by boot processor to activate the rest. */
416static void __init smp_init(void)
417{
53b8a315 418 unsigned int cpu;
1da177e4 419
e761b772
MK
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
1da177e4 427 /* FIXME: This should be done in userspace --RR */
53b8a315 428 for_each_present_cpu(cpu) {
ca74a6f8 429 if (num_online_cpus() >= setup_max_cpus)
1da177e4 430 break;
53b8a315
CL
431 if (!cpu_online(cpu))
432 cpu_up(cpu);
1da177e4
LT
433 }
434
435 /* Any cleanup work */
436 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
ca74a6f8 437 smp_cpus_done(setup_max_cpus);
1da177e4
LT
438}
439
440#endif
441
30d7e0d4
ABL
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 */
448static 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
1da177e4
LT
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
92080309 465static void noinline __init_refok rest_init(void)
1da177e4
LT
466 __releases(kernel_lock)
467{
73c27992
EB
468 int pid;
469
aae5f662 470 kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
1da177e4 471 numa_default_policy();
73c27992 472 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
5cd20455 473 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
1da177e4 474 unlock_kernel();
f340c0d1
IM
475
476 /*
477 * The boot idle thread must execute schedule()
1df21055 478 * at least once to get things moving:
f340c0d1 479 */
1df21055 480 init_idle_bootup_task(current);
5bfb5d69 481 preempt_enable_no_resched();
f340c0d1 482 schedule();
5bfb5d69 483 preempt_disable();
f340c0d1 484
5bfb5d69 485 /* Call into cpu_idle with preempt disabled */
1da177e4 486 cpu_idle();
1df21055 487}
1da177e4
LT
488
489/* Check for early params. */
490static 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++) {
18a8bd94
YL
495 if ((p->early && strcmp(param, p->str) == 0) ||
496 (strcmp(param, "console") == 0 &&
497 strcmp(p->str, "earlycon") == 0)
498 ) {
1da177e4
LT
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. */
509void __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. */
30d7e0d4 518 strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
1da177e4
LT
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
44fd2299
SS
527static 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
839ad62e 536void __init __weak smp_setup_processor_id(void)
033ab7f8
AM
537{
538}
539
8c9843e5
BH
540void __init __weak thread_info_cache_init(void)
541{
542}
543
1da177e4
LT
544asmlinkage void __init start_kernel(void)
545{
546 char * command_line;
547 extern struct kernel_param __start___param[], __stop___param[];
033ab7f8
AM
548
549 smp_setup_processor_id();
550
fbb9ce95
IM
551 /*
552 * Need to run as early as possible, to initialize the
553 * lockdep hash:
554 */
c9538ed4 555 unwind_init();
fbb9ce95 556 lockdep_init();
3ac7fe5a 557 debug_objects_early_init();
ddbcc7e8 558 cgroup_init_early();
fbb9ce95
IM
559
560 local_irq_disable();
561 early_boot_irqs_off();
243c7621 562 early_init_irq_lock_class();
fbb9ce95 563
1da177e4
LT
564/*
565 * Interrupts are still disabled. Do necessary setups, then
566 * enable them
567 */
568 lock_kernel();
906568c9 569 tick_init();
44fd2299 570 boot_cpu_init();
1da177e4
LT
571 page_address_init();
572 printk(KERN_NOTICE);
8993780a 573 printk(linux_banner);
1da177e4 574 setup_arch(&command_line);
cf475ad2 575 mm_init_owner(&init_mm, &init_task);
30d7e0d4 576 setup_command_line(command_line);
690a973f 577 unwind_setup();
1da177e4 578 setup_per_cpu_areas();
e0982e90 579 setup_nr_cpu_ids();
44fd2299 580 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
1da177e4
LT
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();
30d7e0d4 595 printk(KERN_NOTICE "Kernel command line: %s\n", boot_command_line);
1da177e4 596 parse_early_param();
30d7e0d4 597 parse_args("Booting kernel", static_command_line, __start___param,
1da177e4
LT
598 __stop___param - __start___param,
599 &unknown_bootoption);
c4a68306
AB
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 }
1da177e4
LT
605 sort_main_extable();
606 trap_init();
607 rcu_init();
608 init_IRQ();
609 pidhash_init();
610 init_timers();
c0a31329 611 hrtimers_init();
1da177e4 612 softirq_init();
ad596171 613 timekeeping_init();
88fecaa2 614 time_init();
3e51f33f 615 sched_clock_init();
93e02814
HC
616 profile_init();
617 if (!irqs_disabled())
618 printk("start_kernel(): bug: interrupts were enabled early\n");
fbb9ce95 619 early_boot_irqs_on();
93e02814 620 local_irq_enable();
1da177e4
LT
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);
fbb9ce95
IM
630
631 lockdep_info();
632
9a11b49a
IM
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
1da177e4
LT
640#ifdef CONFIG_BLK_DEV_INITRD
641 if (initrd_start && !initrd_below_start_ok &&
bd673c7c 642 page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
1da177e4 643 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
fb6624eb 644 "disabling it.\n",
bd673c7c
GU
645 page_to_pfn(virt_to_page((void *)initrd_start)),
646 min_low_pfn);
1da177e4
LT
647 initrd_start = 0;
648 }
649#endif
db64fe02 650 vmalloc_init();
1da177e4 651 vfs_caches_init_early();
c417f024 652 cpuset_init_early();
94b6da5a 653 page_cgroup_init();
1da177e4 654 mem_init();
a03c2a48 655 enable_debug_pagealloc();
d221938c 656 cpu_hotplug_init();
1da177e4 657 kmem_cache_init();
b9ce08c0 658 kmemtrace_init();
3ac7fe5a 659 debug_objects_mem_init();
199f0ca5 660 idr_init_cache();
e7c8d5c9 661 setup_per_cpu_pageset();
1da177e4
LT
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
8c9843e5 674 thread_info_cache_init();
d84f4f99 675 cred_init();
1da177e4
LT
676 fork_init(num_physpages);
677 proc_caches_init();
678 buffer_init();
1da177e4
LT
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
ddbcc7e8 689 cgroup_init();
1da177e4 690 cpuset_init();
c757249a 691 taskstats_init_early();
ca74e92b 692 delayacct_init();
1da177e4
LT
693
694 check_bugs();
695
696 acpi_early_init(); /* before LAPIC and SMP init */
697
68bf21aa
SR
698 ftrace_init();
699
1da177e4
LT
700 /* Do the rest non-__init'ed, we're now alive */
701 rest_init();
702}
703
59f9415f 704static int initcall_debug;
d0ea3d7d 705core_param(initcall_debug, initcall_debug, bool, 0644);
1da177e4 706
59f9415f 707int do_one_initcall(initcall_t fn)
1da177e4 708{
1da177e4 709 int count = preempt_count();
74239072 710 ktime_t calltime, delta, rettime;
a76bfd0d 711 char msgbuf[64];
74239072
FW
712 struct boot_trace_call call;
713 struct boot_trace_ret ret;
1da177e4 714
e0df154f 715 if (initcall_debug) {
74239072
FW
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);
71566a0d 720 enable_boot_trace();
e0df154f 721 }
1da177e4 722
74239072 723 ret.result = fn();
1da177e4 724
e0df154f 725 if (initcall_debug) {
71566a0d 726 disable_boot_trace();
74239072
FW
727 rettime = ktime_get();
728 delta = ktime_sub(rettime, calltime);
1d926f27 729 ret.duration = (unsigned long long) ktime_to_ns(delta) >> 10;
74239072 730 trace_boot_ret(&ret, fn);
ca538f6b 731 printk("initcall %pF returned %d after %Ld usecs\n", fn,
74239072 732 ret.result, ret.duration);
e0df154f 733 }
8f0c45cd 734
e0df154f 735 msgbuf[0] = 0;
e662e1cf 736
74239072
FW
737 if (ret.result && ret.result != -ENODEV && initcall_debug)
738 sprintf(msgbuf, "error code %d ", ret.result);
e662e1cf 739
e0df154f 740 if (preempt_count() != count) {
a76bfd0d 741 strlcat(msgbuf, "preemption imbalance ", sizeof(msgbuf));
e0df154f 742 preempt_count() = count;
1da177e4 743 }
e0df154f 744 if (irqs_disabled()) {
a76bfd0d 745 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
e0df154f
LT
746 local_irq_enable();
747 }
748 if (msgbuf[0]) {
96d746c6 749 printk("initcall %pF returned with %s\n", fn, msgbuf);
e0df154f 750 }
59f9415f 751
74239072 752 return ret.result;
e0df154f
LT
753}
754
755
c2147a50 756extern initcall_t __initcall_start[], __initcall_end[], __early_initcall_end[];
e0df154f
LT
757
758static void __init do_initcalls(void)
759{
760 initcall_t *call;
761
c2147a50 762 for (call = __early_initcall_end; call < __initcall_end; call++)
e0df154f 763 do_one_initcall(*call);
1da177e4
LT
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 */
776static void __init do_basic_setup(void)
777{
4446a36f 778 rcu_init_sched(); /* needed by module_init stage. */
4403b406 779 init_workqueues();
1da177e4
LT
780 usermodehelper_init();
781 driver_init();
b04c3afb 782 init_irq_proc();
1da177e4
LT
783 do_initcalls();
784}
785
7babe8db 786static void __init do_pre_smp_initcalls(void)
c2147a50
EGM
787{
788 initcall_t *call;
789
790 for (call = __initcall_start; call < __early_initcall_end; call++)
791 do_one_initcall(*call);
792}
793
1da177e4
LT
794static void run_init_process(char *init_filename)
795{
796 argv_init[0] = init_filename;
67608567 797 kernel_execve(init_filename, argv_init, envp_init);
1da177e4
LT
798}
799
ee5bfa64
VG
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 */
803static 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
fae5fa44
ON
817 current->signal->flags |= SIGNAL_UNKILLABLE;
818
ee5bfa64
VG
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
aae5f662 844static int __init kernel_init(void * unused)
1da177e4
LT
845{
846 lock_kernel();
847 /*
848 * init can run on any cpu.
849 */
321a8e9d 850 set_cpus_allowed_ptr(current, CPU_MASK_ALL_PTR);
1da177e4
LT
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 */
84d73786 859 init_pid_ns.child_reaper = current;
1da177e4 860
9ec52099
CLG
861 cad_pid = task_pid(current);
862
ca74a6f8 863 smp_prepare_cpus(setup_max_cpus);
1da177e4
LT
864
865 do_pre_smp_initcalls();
3bf77af6 866 start_boot_trace();
1da177e4 867
1da177e4
LT
868 smp_init();
869 sched_init_smp();
870
871 cpuset_init_smp();
872
1da177e4
LT
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 */
ffdfc409
OJ
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;
1da177e4 885 prepare_namespace();
ffdfc409 886 }
1da177e4
LT
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 */
71566a0d 893
ee5bfa64
VG
894 init_post();
895 return 0;
1da177e4 896}