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/tty.h>
34 #if defined(CONFIG_ARCH_S390)
35 #include <asm/s390mach.h>
36 #include <asm/ccwcache.h>
40 #include <linux/pci.h>
44 #include <linux/dio.h>
48 #include <linux/zorro.h>
52 # include <asm/mtrr.h>
56 #include <linux/nubus.h>
60 #include <linux/isapnp.h>
64 extern int irda_proto_init(void);
65 extern int irda_device_init(void);
68 #ifdef CONFIG_X86_LOCAL_APIC
73 * Versions of gcc older than that listed below may actually compile
74 * and link okay, but the end product can have subtle run time bugs.
75 * To avoid associated bogus bug reports, we flatly refuse to compile
76 * with a gcc that is known to be too old from the very beginning.
78 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
79 #error Sorry, your GCC is too old. It builds incorrect kernels.
82 extern char _stext, _etext;
83 extern char *linux_banner;
85 static int init(void *);
87 extern void init_IRQ(void);
88 extern void init_modules(void);
89 extern void sock_init(void);
90 extern void fork_init(unsigned long);
91 extern void mca_init(void);
92 extern void sbus_init(void);
93 extern void ppc_init(void);
94 extern void sysctl_init(void);
95 extern void signals_init(void);
96 extern int init_pcmcia_ds(void);
98 extern void free_initmem(void);
101 extern void tc_init(void);
104 extern void ecard_init(void);
106 #if defined(CONFIG_SYSVIPC)
107 extern void ipc_init(void);
111 * Boot command-line arguments
113 #define MAX_INIT_ARGS 8
114 #define MAX_INIT_ENVS 8
116 extern void time_init(void);
117 extern void softirq_init(void);
121 #ifdef CONFIG_BLK_DEV_INITRD
122 kdev_t real_root_dev;
125 int root_mountflags = MS_RDONLY;
126 char *execute_command;
127 char root_device_name[64];
130 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
131 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
133 static int __init profile_setup(char *str)
136 if (get_option(&str,&par)) prof_shift = par;
140 __setup("profile=", profile_setup);
143 static struct dev_name_struct {
146 } root_dev_names[] __initdata = {
197 { "cdu535", 0x1800 },
198 { "sonycd", 0x1800 },
200 { "cm206cd", 0x2000 },
211 { "apblock", APBLOCK_MAJOR << 8},
212 { "ddv", DDV_MAJOR << 8},
213 { "jsfd", JSFD_MAJOR << 8},
214 #if defined(CONFIG_ARCH_S390)
215 { "dasda", (DASD_MAJOR << MINORBITS) },
216 { "dasdb", (DASD_MAJOR << MINORBITS) + (1 << 2) },
217 { "dasdc", (DASD_MAJOR << MINORBITS) + (2 << 2) },
218 { "dasdd", (DASD_MAJOR << MINORBITS) + (3 << 2) },
219 { "dasde", (DASD_MAJOR << MINORBITS) + (4 << 2) },
220 { "dasdf", (DASD_MAJOR << MINORBITS) + (5 << 2) },
221 { "dasdg", (DASD_MAJOR << MINORBITS) + (6 << 2) },
222 { "dasdh", (DASD_MAJOR << MINORBITS) + (7 << 2) },
224 #if defined(CONFIG_BLK_CPQ_DA) || defined(CONFIG_BLK_CPQ_DA_MODULE)
225 { "ida/c0d0p",0x4800 },
226 { "ida/c0d1p",0x4810 },
227 { "ida/c0d2p",0x4820 },
228 { "ida/c0d3p",0x4830 },
229 { "ida/c0d4p",0x4840 },
230 { "ida/c0d5p",0x4850 },
231 { "ida/c0d6p",0x4860 },
232 { "ida/c0d7p",0x4870 },
233 { "ida/c0d8p",0x4880 },
234 { "ida/c0d9p",0x4890 },
235 { "ida/c0d10p",0x48A0 },
236 { "ida/c0d11p",0x48B0 },
237 { "ida/c0d12p",0x48C0 },
238 { "ida/c0d13p",0x48D0 },
239 { "ida/c0d14p",0x48E0 },
240 { "ida/c0d15p",0x48F0 },
242 #if defined(CONFIG_BLK_CPQ_CISS_DA) || defined(CONFIG_BLK_CPQ_CISS_DA_MODULE)
243 { "cciss/c0d0p",0x6800 },
244 { "cciss/c0d1p",0x6810 },
245 { "cciss/c0d2p",0x6820 },
246 { "cciss/c0d3p",0x6830 },
247 { "cciss/c0d4p",0x6840 },
248 { "cciss/c0d5p",0x6850 },
249 { "cciss/c0d6p",0x6860 },
250 { "cciss/c0d7p",0x6870 },
251 { "cciss/c0d8p",0x6880 },
252 { "cciss/c0d9p",0x6890 },
253 { "cciss/c0d10p",0x68A0 },
254 { "cciss/c0d11p",0x68B0 },
255 { "cciss/c0d12p",0x68C0 },
256 { "cciss/c0d13p",0x68D0 },
257 { "cciss/c0d14p",0x68E0 },
258 { "cciss/c0d15p",0x68F0 },
268 { "mtdblock", 0x1f00 },
272 kdev_t __init name_to_kdev_t(char *line)
276 if (strncmp(line,"/dev/",5) == 0) {
277 struct dev_name_struct *dev = root_dev_names;
280 int len = strlen(dev->name);
281 if (strncmp(line,dev->name,len) == 0) {
289 return to_kdev_t(base + simple_strtoul(line,NULL,base?10:16));
292 static int __init root_dev_setup(char *line)
297 ROOT_DEV = name_to_kdev_t(line);
298 memset (root_device_name, 0, sizeof root_device_name);
299 if (strncmp (line, "/dev/", 5) == 0) line += 5;
300 for (i = 0; i < sizeof root_device_name - 1; ++i)
303 if ( isspace (ch) || (ch == ',') || (ch == '\0') ) break;
304 root_device_name[i] = ch;
309 __setup("root=", root_dev_setup);
311 static int __init checksetup(char *line)
313 struct kernel_param *p;
317 int n = strlen(p->str);
318 if (!strncmp(line,p->str,n)) {
319 if (p->setup_func(line+n))
323 } while (p < &__setup_end);
327 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
328 still work even if initially too large, it will just take slightly longer */
329 unsigned long loops_per_jiffy = (1<<12);
331 /* This is the number of bits of precision for the loops_per_jiffy. Each
332 bit takes on average 1.5/HZ seconds. This (like the original) is a little
336 void __init calibrate_delay(void)
338 unsigned long ticks, loopbit;
339 int lps_precision = LPS_PREC;
341 loops_per_jiffy = (1<<12);
343 printk("Calibrating delay loop... ");
344 while (loops_per_jiffy <<= 1) {
345 /* wait for "start of" clock tick */
347 while (ticks == jiffies)
351 __delay(loops_per_jiffy);
352 ticks = jiffies - ticks;
357 /* Do a binary approximation to get loops_per_jiffy set to equal one clock
358 (up to lps_precision bits) */
359 loops_per_jiffy >>= 1;
360 loopbit = loops_per_jiffy;
361 while ( lps_precision-- && (loopbit >>= 1) ) {
362 loops_per_jiffy |= loopbit;
364 while (ticks == jiffies);
366 __delay(loops_per_jiffy);
367 if (jiffies != ticks) /* longer than 1 tick */
368 loops_per_jiffy &= ~loopbit;
371 /* Round the value and print it */
372 printk("%lu.%02lu BogoMIPS\n",
373 loops_per_jiffy/(500000/HZ),
374 (loops_per_jiffy/(5000/HZ)) % 100);
377 static int __init readonly(char *str)
381 root_mountflags |= MS_RDONLY;
385 static int __init readwrite(char *str)
389 root_mountflags &= ~MS_RDONLY;
393 static int __init debug_kernel(char *str)
397 console_loglevel = 10;
401 static int __init quiet_kernel(char *str)
405 console_loglevel = 4;
409 __setup("ro", readonly);
410 __setup("rw", readwrite);
411 __setup("debug", debug_kernel);
412 __setup("quiet", quiet_kernel);
415 * This is a simple kernel command line parsing function: it parses
416 * the command line, and fills in the arguments/environment to init
417 * as appropriate. Any cmd-line option is taken to be an environment
418 * variable if it contains the character '='.
420 * This routine also checks for options meant for the kernel.
421 * These options are not given to init - they are for internal kernel use only.
423 static void __init parse_options(char *line)
431 envs = 1; /* TERM is set to 'linux' by default */
433 while ((line = next) != NULL) {
434 quote = strchr(line,'"');
435 next = strchr(line, ' ');
436 while (next != NULL && quote != NULL && quote < next) {
437 /* we found a left quote before the next blank
438 * now we have to find the matching right quote
440 next = strchr(quote+1, '"');
442 quote = strchr(next+1, '"');
443 next = strchr(next+1, ' ');
448 if (!strncmp(line,"init=",5)) {
450 execute_command = line;
451 /* In case LILO is going to boot us with default command line,
452 * it prepends "auto" before the whole cmdline which makes
453 * the shell think it should execute a script with such name.
454 * So we ignore all arguments entered _before_ init=... [MJ]
459 if (checksetup(line))
463 * Then check if it's an environment variable or
466 if (strchr(line,'=')) {
467 if (envs >= MAX_INIT_ENVS)
469 envp_init[++envs] = line;
471 if (args >= MAX_INIT_ARGS)
474 argv_init[++args] = line;
477 argv_init[args+1] = NULL;
478 envp_init[envs+1] = NULL;
482 extern void setup_arch(char **);
483 extern void cpu_idle(void);
485 volatile unsigned long wait_init_idle = 0UL;
489 #ifdef CONFIG_X86_LOCAL_APIC
490 static void __init smp_init(void)
492 APIC_init_uniprocessor();
495 #define smp_init() do { } while (0)
501 /* Called by boot processor to activate the rest. */
502 static void __init smp_init(void)
504 /* Get other processors into their bootup holding patterns. */
506 wait_init_idle = cpu_online_map;
507 clear_bit(current->processor, &wait_init_idle); /* Don't wait on me! */
508 printk("Waiting on wait_init_idle (map = 0x%lx)\n", wait_init_idle);
512 /* Wait for the other cpus to set up their idle processes */
518 printk("All processors have done init_idle\n");
524 * We need to finalize in a non-__init function or else race conditions
525 * between the root thread and the init thread may cause start_kernel to
526 * be reaped by free_initmem before the root thread has proceeded to
530 static void rest_init(void)
532 kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);
534 current->need_resched = 1;
539 * Activate the first processor.
542 asmlinkage void __init start_kernel(void)
545 unsigned long mempages;
546 extern char saved_command_line[];
548 * Interrupts are still disabled. Do necessary setups, then
552 printk(linux_banner);
553 setup_arch(&command_line);
554 printk("Kernel command line: %s\n", saved_command_line);
555 parse_options(command_line);
563 * HACK ALERT! This is early. We're enabling the console before
564 * we've done PCI setups etc, and console_init() must be aware of
565 * this. But we do want output early, in case something goes wrong.
568 #ifdef CONFIG_MODULES
573 /* only text is profiled */
574 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
575 prof_len >>= prof_shift;
577 size = prof_len * sizeof(unsigned int) + PAGE_SIZE-1;
578 prof_buffer = (unsigned int *) alloc_bootmem(size);
584 #ifdef CONFIG_BLK_DEV_INITRD
585 if (initrd_start && !initrd_below_start_ok &&
586 initrd_start < min_low_pfn << PAGE_SHIFT) {
587 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
588 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
593 kmem_cache_sizes_init();
594 mempages = num_physpages;
598 vfs_caches_init(mempages);
599 buffer_init(mempages);
600 page_cache_init(mempages);
601 #if defined(CONFIG_ARCH_S390)
605 #ifdef CONFIG_PROC_FS
608 #if defined(CONFIG_SYSVIPC)
612 printk("POSIX conformance testing by UNIFIX\n");
615 * We count on the initial thread going ok
616 * Like idlers init is an unlocked kernel thread, which will
617 * make syscalls (and thus be locked).
623 #ifdef CONFIG_BLK_DEV_INITRD
624 static int do_linuxrc(void * shell)
626 static char *argv[] = { "linuxrc", NULL, };
628 close(0);close(1);close(2);
630 (void) open("/dev/console",O_RDWR,0);
633 return execve(shell, argv, envp_init);
638 struct task_struct *child_reaper = &init_task;
640 static void __init do_initcalls(void)
644 call = &__initcall_start;
648 } while (call < &__initcall_end);
650 /* Make sure there is no pending stuff from the initcall sequence */
651 flush_scheduled_tasks();
655 * Ok, the machine is now initialized. None of the devices
656 * have been touched yet, but the CPU subsystem is up and
657 * running, and memory and process management works.
659 * Now we can finally start doing some real work..
661 static void __init do_basic_setup(void)
665 * Tell the world that we're going to be the grim
666 * reaper of innocent orphaned children.
668 * We don't want people to have to make incorrect
669 * assumptions about where in the task array this
672 child_reaper = current;
674 #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
676 * We should probably create some architecture-dependent "fixup after
677 * everything is up" style function where this would belong better
678 * than in init/main.c..
688 * Ok, at this point all CPU's should be initialized, so
689 * we can start looking into devices..
691 #if defined(CONFIG_ARCH_S390)
692 s390_init_machine_check();
701 #if defined(CONFIG_PPC)
707 #ifdef CONFIG_ARCH_ACORN
726 /* Networking initialization needs a process context */
729 start_context_thread();
734 irda_device_init(); /* Must be done after protocol initialization */
737 init_pcmcia_ds(); /* Do this last */
741 extern void rd_load(void);
742 extern void initrd_load(void);
745 * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
747 static void prepare_namespace(void)
749 #ifdef CONFIG_BLK_DEV_INITRD
750 int real_root_mountflags = root_mountflags;
754 root_mountflags &= ~MS_RDONLY;
755 real_root_dev = ROOT_DEV;
758 #ifdef CONFIG_BLK_DEV_RAM
759 #ifdef CONFIG_BLK_DEV_INITRD
767 /* Mount the root filesystem.. */
772 #ifdef CONFIG_BLK_DEV_INITRD
773 root_mountflags = real_root_mountflags;
774 if (mount_initrd && ROOT_DEV != real_root_dev
775 && MAJOR(ROOT_DEV) == RAMDISK_MAJOR && MINOR(ROOT_DEV) == 0) {
779 pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
781 while (pid != wait(&i));
782 if (MAJOR(real_root_dev) != RAMDISK_MAJOR
783 || MINOR(real_root_dev) != 0) {
784 error = change_root(real_root_dev,"/initrd");
786 printk(KERN_ERR "Change root to /initrd: "
793 static int init(void * unused)
801 * Ok, we have completed the initial bootup, and
802 * we're essentially up and running. Get rid of the
803 * initmem segments and start the user-mode stuff..
808 if (open("/dev/console", O_RDWR, 0) < 0)
809 printk("Warning: unable to open an initial console.\n");
815 * We try each of these until one succeeds.
817 * The Bourne shell can be used instead of init if we are
818 * trying to recover a really broken machine.
822 execve(execute_command,argv_init,envp_init);
823 execve("/sbin/init",argv_init,envp_init);
824 execve("/etc/init",argv_init,envp_init);
825 execve("/bin/init",argv_init,envp_init);
826 execve("/bin/sh",argv_init,envp_init);
827 panic("No init found. Try passing init= option to kernel.");