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/proc_fs.h>
16 #include <linux/devfs_fs_kernel.h>
17 #include <linux/unistd.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/delay.h>
21 #include <linux/utsname.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/smp_lock.h>
25 #include <linux/blk.h>
26 #include <linux/hdreg.h>
27 #include <linux/iobuf.h>
28 #include <linux/bootmem.h>
29 #include <linux/file.h>
30 #include <linux/tty.h>
35 #if defined(CONFIG_ARCH_S390)
36 #include <asm/s390mach.h>
37 #include <asm/ccwcache.h>
41 #include <linux/acpi.h>
45 #include <linux/pci.h>
49 #include <linux/dio.h>
53 #include <linux/zorro.h>
57 # include <asm/mtrr.h>
61 #include <linux/nubus.h>
65 #include <linux/isapnp.h>
69 extern int irda_proto_init(void);
70 extern int irda_device_init(void);
73 #ifdef CONFIG_X86_LOCAL_APIC
78 * Versions of gcc older than that listed below may actually compile
79 * and link okay, but the end product can have subtle run time bugs.
80 * To avoid associated bogus bug reports, we flatly refuse to compile
81 * with a gcc that is known to be too old from the very beginning.
83 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
84 #error Sorry, your GCC is too old. It builds incorrect kernels.
87 extern char _stext, _etext;
88 extern char *linux_banner;
90 static int init(void *);
92 extern void init_IRQ(void);
93 extern void init_modules(void);
94 extern void sock_init(void);
95 extern void fork_init(unsigned long);
96 extern void mca_init(void);
97 extern void sbus_init(void);
98 extern void ppc_init(void);
99 extern void sysctl_init(void);
100 extern void signals_init(void);
101 extern int init_pcmcia_ds(void);
103 extern void free_initmem(void);
104 #ifdef CONFIG_ACPI_BUS
105 extern void acpi_early_init(void);
107 static inline void acpi_early_init(void) { }
111 extern void tc_init(void);
114 extern void ecard_init(void);
116 #if defined(CONFIG_SYSVIPC)
117 extern void ipc_init(void);
121 * Boot command-line arguments
123 #define MAX_INIT_ARGS 8
124 #define MAX_INIT_ENVS 8
126 extern void time_init(void);
127 extern void softirq_init(void);
131 char *execute_command;
133 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
134 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
136 static int __init profile_setup(char *str)
139 if (get_option(&str,&par)) prof_shift = par;
143 __setup("profile=", profile_setup);
145 static int __init checksetup(char *line)
147 struct kernel_param *p;
151 int n = strlen(p->str);
152 if (!strncmp(line,p->str,n)) {
153 if (p->setup_func(line+n))
157 } while (p < &__setup_end);
161 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
162 still work even if initially too large, it will just take slightly longer */
163 unsigned long loops_per_jiffy = (1<<12);
165 /* This is the number of bits of precision for the loops_per_jiffy. Each
166 bit takes on average 1.5/HZ seconds. This (like the original) is a little
170 void __init calibrate_delay(void)
172 unsigned long ticks, loopbit;
173 int lps_precision = LPS_PREC;
175 loops_per_jiffy = (1<<12);
177 printk("Calibrating delay loop... ");
178 while (loops_per_jiffy <<= 1) {
179 /* wait for "start of" clock tick */
181 while (ticks == jiffies)
185 __delay(loops_per_jiffy);
186 ticks = jiffies - ticks;
191 /* Do a binary approximation to get loops_per_jiffy set to equal one clock
192 (up to lps_precision bits) */
193 loops_per_jiffy >>= 1;
194 loopbit = loops_per_jiffy;
195 while ( lps_precision-- && (loopbit >>= 1) ) {
196 loops_per_jiffy |= loopbit;
198 while (ticks == jiffies);
200 __delay(loops_per_jiffy);
201 if (jiffies != ticks) /* longer than 1 tick */
202 loops_per_jiffy &= ~loopbit;
205 /* Round the value and print it */
206 printk("%lu.%02lu BogoMIPS\n",
207 loops_per_jiffy/(500000/HZ),
208 (loops_per_jiffy/(5000/HZ)) % 100);
211 static int __init debug_kernel(char *str)
215 console_loglevel = 10;
219 static int __init quiet_kernel(char *str)
223 console_loglevel = 4;
227 __setup("debug", debug_kernel);
228 __setup("quiet", quiet_kernel);
231 * This is a simple kernel command line parsing function: it parses
232 * the command line, and fills in the arguments/environment to init
233 * as appropriate. Any cmd-line option is taken to be an environment
234 * variable if it contains the character '='.
236 * This routine also checks for options meant for the kernel.
237 * These options are not given to init - they are for internal kernel use only.
239 static void __init parse_options(char *line)
247 envs = 1; /* TERM is set to 'linux' by default */
249 while ((line = next) != NULL) {
250 quote = strchr(line,'"');
251 next = strchr(line, ' ');
252 while (next != NULL && quote != NULL && quote < next) {
253 /* we found a left quote before the next blank
254 * now we have to find the matching right quote
256 next = strchr(quote+1, '"');
258 quote = strchr(next+1, '"');
259 next = strchr(next+1, ' ');
264 if (!strncmp(line,"init=",5)) {
266 execute_command = line;
267 /* In case LILO is going to boot us with default command line,
268 * it prepends "auto" before the whole cmdline which makes
269 * the shell think it should execute a script with such name.
270 * So we ignore all arguments entered _before_ init=... [MJ]
275 if (checksetup(line))
279 * Then check if it's an environment variable or
282 if (strchr(line,'=')) {
283 if (envs >= MAX_INIT_ENVS)
285 envp_init[++envs] = line;
287 if (args >= MAX_INIT_ARGS)
290 argv_init[++args] = line;
293 argv_init[args+1] = NULL;
294 envp_init[envs+1] = NULL;
298 extern void setup_arch(char **);
299 extern void cpu_idle(void);
301 unsigned long wait_init_idle;
305 #ifdef CONFIG_X86_LOCAL_APIC
306 static void __init smp_init(void)
308 APIC_init_uniprocessor();
311 #define smp_init() do { } while (0)
317 /* Called by boot processor to activate the rest. */
318 static void __init smp_init(void)
320 /* Get other processors into their bootup holding patterns. */
322 wait_init_idle = cpu_online_map;
323 clear_bit(current->processor, &wait_init_idle); /* Don't wait on me! */
328 /* Wait for the other cpus to set up their idle processes */
329 printk("Waiting on wait_init_idle (map = 0x%lx)\n", wait_init_idle);
330 while (wait_init_idle) {
334 printk("All processors have done init_idle\n");
340 * We need to finalize in a non-__init function or else race conditions
341 * between the root thread and the init thread may cause start_kernel to
342 * be reaped by free_initmem before the root thread has proceeded to
346 static void rest_init(void)
348 kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);
350 current->need_resched = 1;
355 * Activate the first processor.
358 asmlinkage void __init start_kernel(void)
361 extern char saved_command_line[];
363 * Interrupts are still disabled. Do necessary setups, then
367 printk(linux_banner);
368 setup_arch(&command_line);
369 printk("Kernel command line: %s\n", saved_command_line);
370 parse_options(command_line);
378 * HACK ALERT! This is early. We're enabling the console before
379 * we've done PCI setups etc, and console_init() must be aware of
380 * this. But we do want output early, in case something goes wrong.
383 #ifdef CONFIG_MODULES
388 /* only text is profiled */
389 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
390 prof_len >>= prof_shift;
392 size = prof_len * sizeof(unsigned int) + PAGE_SIZE-1;
393 prof_buffer = (unsigned int *) alloc_bootmem(size);
399 #ifdef CONFIG_BLK_DEV_INITRD
400 if (initrd_start && !initrd_below_start_ok &&
401 initrd_start < min_low_pfn << PAGE_SHIFT) {
402 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
403 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
408 kmem_cache_sizes_init();
409 pgtable_cache_init();
412 * For architectures that have highmem, num_mappedpages represents
413 * the amount of memory the kernel can use. For other architectures
414 * it's the same as the total pages. We need both numbers because
415 * some subsystems need to initialize based on how much memory the
418 if (num_mappedpages == 0)
419 num_mappedpages = num_physpages;
421 fork_init(num_mappedpages);
423 vfs_caches_init(num_physpages);
424 buffer_init(num_physpages);
425 page_cache_init(num_physpages);
426 #if defined(CONFIG_ARCH_S390)
430 #ifdef CONFIG_PROC_FS
434 acpi_early_init(); /* before LAPIC and SMP init */
435 printk("POSIX conformance testing by UNIFIX\n");
438 * We count on the initial thread going ok
439 * Like idlers init is an unlocked kernel thread, which will
440 * make syscalls (and thus be locked).
443 #if defined(CONFIG_SYSVIPC)
449 struct task_struct *child_reaper = &init_task;
451 static void __init do_initcalls(void)
455 call = &__initcall_start;
459 } while (call < &__initcall_end);
461 /* Make sure there is no pending stuff from the initcall sequence */
462 flush_scheduled_tasks();
466 * Ok, the machine is now initialized. None of the devices
467 * have been touched yet, but the CPU subsystem is up and
468 * running, and memory and process management works.
470 * Now we can finally start doing some real work..
472 static void __init do_basic_setup(void)
476 * Tell the world that we're going to be the grim
477 * reaper of innocent orphaned children.
479 * We don't want people to have to make incorrect
480 * assumptions about where in the task array this
483 child_reaper = current;
485 #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
487 * We should probably create some architecture-dependent "fixup after
488 * everything is up" style function where this would belong better
489 * than in init/main.c..
499 * Ok, at this point all CPU's should be initialized, so
500 * we can start looking into devices..
502 #if defined(CONFIG_ARCH_S390)
503 s390_init_machine_check();
505 #ifdef CONFIG_ACPI_INTERPRETER
514 #if defined(CONFIG_PPC)
520 #ifdef CONFIG_ARCH_ACORN
539 /* Networking initialization needs a process context */
542 start_context_thread();
547 irda_device_init(); /* Must be done after protocol initialization */
550 init_pcmcia_ds(); /* Do this last */
554 static void run_init_process(char *init_filename)
556 argv_init[0] = init_filename;
557 execve(init_filename, argv_init, envp_init);
560 extern void prepare_namespace(void);
562 static int init(void * unused)
564 struct files_struct *files;
571 * Ok, we have completed the initial bootup, and
572 * we're essentially up and running. Get rid of the
573 * initmem segments and start the user-mode stuff..
579 * Right now we are a thread sharing with a ton of kernel
580 * stuff. We don't want to end up in user space in that state
583 files = current->files;
586 put_files_struct(files);
588 if (open("/dev/console", O_RDWR, 0) < 0)
589 printk("Warning: unable to open an initial console.\n");
595 * We try each of these until one succeeds.
597 * The Bourne shell can be used instead of init if we are
598 * trying to recover a really broken machine.
602 run_init_process(execute_command);
604 run_init_process("/sbin/init");
605 run_init_process("/etc/init");
606 run_init_process("/bin/init");
607 run_init_process("/bin/sh");
609 panic("No init found. Try passing init= option to kernel.");