4 * Copyright (C) 1991, 1992 Linus Torvalds
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>
12 #define __KERNEL_SYSCALLS__
14 #include <linux/config.h>
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/proc_fs.h>
18 #include <linux/devfs_fs_kernel.h>
19 #include <linux/kernel.h>
20 #include <linux/syscalls.h>
21 #include <linux/string.h>
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/utsname.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/smp_lock.h>
28 #include <linux/initrd.h>
29 #include <linux/hdreg.h>
30 #include <linux/bootmem.h>
31 #include <linux/tty.h>
32 #include <linux/gfp.h>
33 #include <linux/percpu.h>
34 #include <linux/kmod.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/security.h>
37 #include <linux/workqueue.h>
38 #include <linux/profile.h>
39 #include <linux/rcupdate.h>
40 #include <linux/moduleparam.h>
41 #include <linux/kallsyms.h>
42 #include <linux/writeback.h>
43 #include <linux/cpu.h>
44 #include <linux/efi.h>
45 #include <linux/unistd.h>
46 #include <linux/rmap.h>
47 #include <linux/mempolicy.h>
48 #include <linux/key.h>
52 #include <asm/setup.h>
55 * This is one of the first .c files built. Error out early
56 * if we have compiler trouble..
58 #if __GNUC__ == 2 && __GNUC_MINOR__ == 96
59 #ifdef CONFIG_FRAME_POINTER
60 #error This compiler cannot compile correctly with frame pointers enabled
64 #ifdef CONFIG_X86_LOCAL_APIC
69 * Versions of gcc older than that listed below may actually compile
70 * and link okay, but the end product can have subtle run time bugs.
71 * To avoid associated bogus bug reports, we flatly refuse to compile
72 * with a gcc that is known to be too old from the very beginning.
74 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 95)
75 #error Sorry, your GCC is too old. It builds incorrect kernels.
78 static int init(void *);
80 extern void init_IRQ(void);
81 extern void sock_init(void);
82 extern void fork_init(unsigned long);
83 extern void mca_init(void);
84 extern void sbus_init(void);
85 extern void sysctl_init(void);
86 extern void signals_init(void);
87 extern void buffer_init(void);
88 extern void pidhash_init(void);
89 extern void pidmap_init(void);
90 extern void prio_tree_init(void);
91 extern void radix_tree_init(void);
92 extern void free_initmem(void);
93 extern void populate_rootfs(void);
94 extern void driver_init(void);
95 extern void prepare_namespace(void);
97 extern void acpi_early_init(void);
99 static inline void acpi_early_init(void) { }
103 extern void tc_init(void);
106 enum system_states system_state;
107 EXPORT_SYMBOL(system_state);
110 * Boot command-line arguments
112 #define MAX_INIT_ARGS 32
113 #define MAX_INIT_ENVS 32
115 extern void time_init(void);
116 /* Default late time init is NULL. archs can override this later. */
117 void (*late_time_init)(void);
118 extern void softirq_init(void);
120 /* Untouched command line (eg. for /proc) saved by arch-specific code. */
121 char saved_command_line[COMMAND_LINE_SIZE];
123 static char *execute_command;
125 /* Setup configured maximum number of CPUs to activate */
126 static unsigned int max_cpus = NR_CPUS;
129 * Setup routine for controlling SMP activation
131 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
132 * activation entirely (the MPS table probe still happens, though).
134 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
135 * greater than 0, limits the maximum number of CPUs activated in
138 static int __init nosmp(char *str)
144 __setup("nosmp", nosmp);
146 static int __init maxcpus(char *str)
148 get_option(&str, &max_cpus);
152 __setup("maxcpus=", maxcpus);
154 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
155 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
156 static const char *panic_later, *panic_param;
158 extern struct obs_kernel_param __setup_start[], __setup_end[];
160 static int __init obsolete_checksetup(char *line)
162 struct obs_kernel_param *p;
166 int n = strlen(p->str);
167 if (!strncmp(line, p->str, n)) {
169 /* Already done in parse_early_param? (Needs
170 * exact match on param part) */
171 if (line[n] == '\0' || line[n] == '=')
173 } else if (!p->setup_func) {
174 printk(KERN_WARNING "Parameter %s is obsolete,"
175 " ignored\n", p->str);
177 } else if (p->setup_func(line + n))
181 } while (p < __setup_end);
186 * This should be approx 2 Bo*oMips to start (note initial shift), and will
187 * still work even if initially too large, it will just take slightly longer
189 unsigned long loops_per_jiffy = (1<<12);
191 EXPORT_SYMBOL(loops_per_jiffy);
193 static int __init debug_kernel(char *str)
197 console_loglevel = 10;
201 static int __init quiet_kernel(char *str)
205 console_loglevel = 4;
209 __setup("debug", debug_kernel);
210 __setup("quiet", quiet_kernel);
213 * Unknown boot options get handed to init, unless they look like
216 static int __init unknown_bootoption(char *param, char *val)
218 /* Change NUL term back to "=", to make "param" the whole string. */
220 /* param=val or param="val"? */
221 if (val == param+strlen(param)+1)
223 else if (val == param+strlen(param)+2) {
225 memmove(val-1, val, strlen(val)+1);
231 /* Handle obsolete-style parameters */
232 if (obsolete_checksetup(param))
236 * Preemptive maintenance for "why didn't my mispelled command
239 if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
240 printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
248 /* Environment option */
250 for (i = 0; envp_init[i]; i++) {
251 if (i == MAX_INIT_ENVS) {
252 panic_later = "Too many boot env vars at `%s'";
255 if (!strncmp(param, envp_init[i], val - param))
258 envp_init[i] = param;
260 /* Command line option */
262 for (i = 0; argv_init[i]; i++) {
263 if (i == MAX_INIT_ARGS) {
264 panic_later = "Too many boot init vars at `%s'";
268 argv_init[i] = param;
273 static int __init init_setup(char *str)
277 execute_command = str;
279 * In case LILO is going to boot us with default command line,
280 * it prepends "auto" before the whole cmdline which makes
281 * the shell think it should execute a script with such name.
282 * So we ignore all arguments entered _before_ init=... [MJ]
284 for (i = 1; i < MAX_INIT_ARGS; i++)
288 __setup("init=", init_setup);
290 extern void setup_arch(char **);
294 #ifdef CONFIG_X86_LOCAL_APIC
295 static void __init smp_init(void)
297 APIC_init_uniprocessor();
300 #define smp_init() do { } while (0)
303 static inline void setup_per_cpu_areas(void) { }
304 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
308 #ifdef __GENERIC_PER_CPU
309 unsigned long __per_cpu_offset[NR_CPUS];
311 EXPORT_SYMBOL(__per_cpu_offset);
313 static void __init setup_per_cpu_areas(void)
315 unsigned long size, i;
317 /* Created by linker magic */
318 extern char __per_cpu_start[], __per_cpu_end[];
320 /* Copy section for each CPU (we discard the original) */
321 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
322 #ifdef CONFIG_MODULES
323 if (size < PERCPU_ENOUGH_ROOM)
324 size = PERCPU_ENOUGH_ROOM;
327 ptr = alloc_bootmem(size * NR_CPUS);
329 for (i = 0; i < NR_CPUS; i++, ptr += size) {
330 __per_cpu_offset[i] = ptr - __per_cpu_start;
331 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
334 #endif /* !__GENERIC_PER_CPU */
336 /* Called by boot processor to activate the rest. */
337 static void __init smp_init(void)
341 /* FIXME: This should be done in userspace --RR */
342 for_each_present_cpu(i) {
343 if (num_online_cpus() >= max_cpus)
349 /* Any cleanup work */
350 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
351 smp_cpus_done(max_cpus);
353 /* Get other processors into their bootup holding patterns. */
363 * We need to finalize in a non-__init function or else race conditions
364 * between the root thread and the init thread may cause start_kernel to
365 * be reaped by free_initmem before the root thread has proceeded to
368 * gcc-3.4 accidentally inlines this function, so use noinline.
371 static void noinline rest_init(void)
372 __releases(kernel_lock)
374 kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
375 numa_default_policy();
377 preempt_enable_no_resched();
381 /* Check for early params. */
382 static int __init do_early_param(char *param, char *val)
384 struct obs_kernel_param *p;
386 for (p = __setup_start; p < __setup_end; p++) {
387 if (p->early && strcmp(param, p->str) == 0) {
388 if (p->setup_func(val) != 0)
390 "Malformed early option '%s'\n", param);
393 /* We accept everything at this stage. */
397 /* Arch code calls this early on, or if not, just before other parsing. */
398 void __init parse_early_param(void)
400 static __initdata int done = 0;
401 static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
406 /* All fall through to do_early_param. */
407 strlcpy(tmp_cmdline, saved_command_line, COMMAND_LINE_SIZE);
408 parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
413 * Activate the first processor.
416 asmlinkage void __init start_kernel(void)
419 extern struct kernel_param __start___param[], __stop___param[];
421 * Interrupts are still disabled. Do necessary setups, then
426 printk(linux_banner);
427 setup_arch(&command_line);
428 setup_per_cpu_areas();
431 * Mark the boot cpu "online" so that it can call console drivers in
432 * printk() and can access its per-cpu storage.
434 smp_prepare_boot_cpu();
437 * Set up the scheduler prior starting any interrupts (such as the
438 * timer interrupt). Full topology setup happens at smp_init()
439 * time - but meanwhile we still have a functioning scheduler.
443 * Disable preemption - early bootup scheduling is extremely
444 * fragile until we cpu_idle() for the first time.
447 build_all_zonelists();
449 printk("Kernel command line: %s\n", saved_command_line);
451 parse_args("Booting kernel", command_line, __start___param,
452 __stop___param - __start___param,
453 &unknown_bootoption);
464 * HACK ALERT! This is early. We're enabling the console before
465 * we've done PCI setups etc, and console_init() must be aware of
466 * this. But we do want output early, in case something goes wrong.
470 panic(panic_later, panic_param);
473 #ifdef CONFIG_BLK_DEV_INITRD
474 if (initrd_start && !initrd_below_start_ok &&
475 initrd_start < min_low_pfn << PAGE_SHIFT) {
476 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
477 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
481 vfs_caches_init_early();
489 pgtable_cache_init();
494 efi_enter_virtual_mode();
496 fork_init(num_physpages);
501 vfs_caches_init(num_physpages);
504 /* rootfs populating might need page-writeback */
505 page_writeback_init();
506 #ifdef CONFIG_PROC_FS
511 acpi_early_init(); /* before LAPIC and SMP init */
513 /* Do the rest non-__init'ed, we're now alive */
517 static int __initdata initcall_debug;
519 static int __init initcall_debug_setup(char *str)
524 __setup("initcall_debug", initcall_debug_setup);
526 struct task_struct *child_reaper = &init_task;
528 extern initcall_t __initcall_start[], __initcall_end[];
530 static void __init do_initcalls(void)
533 int count = preempt_count();
535 for (call = __initcall_start; call < __initcall_end; call++) {
538 if (initcall_debug) {
539 printk(KERN_DEBUG "Calling initcall 0x%p", *call);
540 print_fn_descriptor_symbol(": %s()", (unsigned long) *call);
547 if (preempt_count() != count) {
548 msg = "preemption imbalance";
549 preempt_count() = count;
551 if (irqs_disabled()) {
552 msg = "disabled interrupts";
556 printk("error in initcall at 0x%p: "
557 "returned with %s\n", *call, msg);
561 /* Make sure there is no pending stuff from the initcall sequence */
562 flush_scheduled_work();
566 * Ok, the machine is now initialized. None of the devices
567 * have been touched yet, but the CPU subsystem is up and
568 * running, and memory and process management works.
570 * Now we can finally start doing some real work..
572 static void __init do_basic_setup(void)
574 /* drivers will send hotplug events */
576 usermodehelper_init();
584 /* Networking initialization needs a process context */
590 static void do_pre_smp_initcalls(void)
592 extern int spawn_ksoftirqd(void);
594 extern int migration_init(void);
601 static void run_init_process(char *init_filename)
603 argv_init[0] = init_filename;
604 execve(init_filename, argv_init, envp_init);
607 static inline void fixup_cpu_present_map(void)
613 * If arch is not hotplug ready and did not populate
614 * cpu_present_map, just make cpu_present_map same as cpu_possible_map
615 * for other cpu bringup code to function as normal. e.g smp_init() etc.
617 if (cpus_empty(cpu_present_map)) {
619 cpu_set(i, cpu_present_map);
625 static int init(void * unused)
629 * Tell the world that we're going to be the grim
630 * reaper of innocent orphaned children.
632 * We don't want people to have to make incorrect
633 * assumptions about where in the task array this
636 child_reaper = current;
638 /* Sets up cpus_possible() */
639 smp_prepare_cpus(max_cpus);
641 do_pre_smp_initcalls();
643 fixup_cpu_present_map();
648 * Do this before initcalls, because some drivers want to access
656 * check if there is an early userspace init. If yes, let it do all
659 if (sys_access((const char __user *) "/init", 0) == 0)
660 execute_command = "/init";
665 * Ok, we have completed the initial bootup, and
666 * we're essentially up and running. Get rid of the
667 * initmem segments and start the user-mode stuff..
671 system_state = SYSTEM_RUNNING;
672 numa_default_policy();
674 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
675 printk("Warning: unable to open an initial console.\n");
681 * We try each of these until one succeeds.
683 * The Bourne shell can be used instead of init if we are
684 * trying to recover a really broken machine.
688 run_init_process(execute_command);
690 run_init_process("/sbin/init");
691 run_init_process("/etc/init");
692 run_init_process("/bin/init");
693 run_init_process("/bin/sh");
695 panic("No init found. Try passing init= option to kernel.");