Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / init / main.c
blob32659ba5d8e06528f327e1b2f87380811ae03117
1 /*
2 * linux/init/main.c
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/raid/md.h>
25 #include <linux/smp_lock.h>
26 #include <linux/blk.h>
27 #include <linux/hdreg.h>
28 #include <linux/iobuf.h>
29 #include <linux/bootmem.h>
31 #include <asm/io.h>
32 #include <asm/bugs.h>
34 #ifdef CONFIG_PCI
35 #include <linux/pci.h>
36 #endif
38 #ifdef CONFIG_DIO
39 #include <linux/dio.h>
40 #endif
42 #ifdef CONFIG_ZORRO
43 #include <linux/zorro.h>
44 #endif
46 #ifdef CONFIG_MTRR
47 # include <asm/mtrr.h>
48 #endif
50 #ifdef CONFIG_3215_CONSOLE
51 extern int con3215_activate(void);
52 #endif
54 #ifdef CONFIG_MAC
55 extern void nubus_init(void);
56 #endif
58 #ifdef CONFIG_ISAPNP
59 #include <linux/isapnp.h>
60 #endif
62 #ifdef CONFIG_IRDA
63 #include <net/irda/irda_device.h>
64 #endif
66 #ifdef CONFIG_X86_IO_APIC
67 #include <asm/smp.h>
68 #endif
71 * Versions of gcc older than that listed below may actually compile
72 * and link okay, but the end product can have subtle run time bugs.
73 * To avoid associated bogus bug reports, we flatly refuse to compile
74 * with a gcc that is known to be too old from the very beginning.
76 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
77 #error sorry, your GCC is too old. It builds incorrect kernels.
78 #endif
80 extern char _stext, _etext;
81 extern char *linux_banner;
83 extern int console_loglevel;
85 static int init(void *);
87 extern void init_IRQ(void);
88 extern void init_modules(void);
89 extern void init_inventory(void);
90 extern void sock_init(void);
91 extern void fork_init(unsigned long);
92 extern void mca_init(void);
93 extern void sbus_init(void);
94 extern void ppc_init(void);
95 extern void sysctl_init(void);
96 extern void filescache_init(void);
97 extern void signals_init(void);
98 extern void bdev_init(void);
99 extern int init_pcmcia_ds(void);
100 extern int usb_init(void);
102 extern void free_initmem(void);
103 extern void filesystem_setup(void);
105 #ifdef CONFIG_TC
106 extern void tc_init(void);
107 #endif
109 extern void ecard_init(void);
111 #if defined(CONFIG_SYSVIPC)
112 extern void ipc_init(void);
113 #endif
114 #if defined(CONFIG_QUOTA)
115 extern void dquot_init_hash(void);
116 #endif
118 #ifdef CONFIG_REMOTE_DEBUG
119 #include <asm/gdb-stub.h>
120 #endif
123 * Boot command-line arguments
125 #define MAX_INIT_ARGS 8
126 #define MAX_INIT_ENVS 8
128 extern void time_init(void);
129 extern void softirq_init(void);
131 int rows, cols;
133 #ifdef CONFIG_BLK_DEV_INITRD
134 kdev_t real_root_dev;
135 #endif
137 int root_mountflags = MS_RDONLY;
138 char *execute_command;
139 char root_device_name[64];
142 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
143 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
146 * Read an int from an option string; if available accept a subsequent
147 * comma as well.
149 * Return values:
150 * 0 : no int in string
151 * 1 : int found, no subsequent comma
152 * 2 : int found including a subsequent comma
154 int get_option(char **str, int *pint)
156 char *cur = *str;
158 if (!cur || !(*cur)) return 0;
159 *pint = simple_strtol(cur,str,0);
160 if (cur==*str) return 0;
161 if (**str==',') {
162 (*str)++;
163 return 2;
166 return 1;
169 char *get_options(char *str, int nints, int *ints)
171 int res,i=1;
173 while (i<nints) {
174 res = get_option(&str, ints+i);
175 if (res==0) break;
176 i++;
177 if (res==1) break;
179 ints[0] = i-1;
180 return(str);
183 static int __init profile_setup(char *str)
185 int par;
186 if (get_option(&str,&par)) prof_shift = par;
187 return 1;
190 __setup("profile=", profile_setup);
193 static struct dev_name_struct {
194 const char *name;
195 const int num;
196 } root_dev_names[] __initdata = {
197 { "nfs", 0x00ff },
198 { "hda", 0x0300 },
199 { "hdb", 0x0340 },
200 { "hdc", 0x1600 },
201 { "hdd", 0x1640 },
202 { "hde", 0x2100 },
203 { "hdf", 0x2140 },
204 { "hdg", 0x2200 },
205 { "hdh", 0x2240 },
206 { "hdi", 0x3800 },
207 { "hdj", 0x3840 },
208 { "hdk", 0x3900 },
209 { "hdl", 0x3940 },
210 { "hdm", 0x5800 },
211 { "hdn", 0x5840 },
212 { "hdo", 0x5900 },
213 { "hdp", 0x5940 },
214 { "hdq", 0x5A00 },
215 { "hdr", 0x5A40 },
216 { "hds", 0x5B00 },
217 { "hdt", 0x5B40 },
218 { "sda", 0x0800 },
219 { "sdb", 0x0810 },
220 { "sdc", 0x0820 },
221 { "sdd", 0x0830 },
222 { "sde", 0x0840 },
223 { "sdf", 0x0850 },
224 { "sdg", 0x0860 },
225 { "sdh", 0x0870 },
226 { "sdi", 0x0880 },
227 { "sdj", 0x0890 },
228 { "sdk", 0x08a0 },
229 { "sdl", 0x08b0 },
230 { "sdm", 0x08c0 },
231 { "sdn", 0x08d0 },
232 { "sdo", 0x08e0 },
233 { "sdp", 0x08f0 },
234 { "ada", 0x1c00 },
235 { "adb", 0x1c10 },
236 { "adc", 0x1c20 },
237 { "add", 0x1c30 },
238 { "ade", 0x1c40 },
239 { "fd", 0x0200 },
240 { "md", 0x0900 },
241 { "xda", 0x0d00 },
242 { "xdb", 0x0d40 },
243 { "ram", 0x0100 },
244 { "scd", 0x0b00 },
245 { "mcd", 0x1700 },
246 { "cdu535", 0x1800 },
247 { "sonycd", 0x1800 },
248 { "aztcd", 0x1d00 },
249 { "cm206cd", 0x2000 },
250 { "gscd", 0x1000 },
251 { "sbpcd", 0x1900 },
252 { "eda", 0x2400 },
253 { "edb", 0x2440 },
254 { "pda", 0x2d00 },
255 { "pdb", 0x2d10 },
256 { "pdc", 0x2d20 },
257 { "pdd", 0x2d30 },
258 { "pcd", 0x2e00 },
259 { "pf", 0x2f00 },
260 { "apblock", APBLOCK_MAJOR << 8},
261 { "ddv", DDV_MAJOR << 8},
262 { "jsfd", JSFD_MAJOR << 8},
263 #ifdef CONFIG_MDISK
264 { "mnda", (MDISK_MAJOR << MINORBITS)},
265 { "mndb", (MDISK_MAJOR << MINORBITS) + 1},
266 { "mndc", (MDISK_MAJOR << MINORBITS) + 2},
267 { "mndd", (MDISK_MAJOR << MINORBITS) + 3},
268 { "mnde", (MDISK_MAJOR << MINORBITS) + 4},
269 { "mndf", (MDISK_MAJOR << MINORBITS) + 5},
270 { "mndg", (MDISK_MAJOR << MINORBITS) + 6},
271 { "mndh", (MDISK_MAJOR << MINORBITS) + 7},
272 #endif
273 #ifdef CONFIG_DASD
274 { "dasda", (DASD_MAJOR << MINORBITS) },
275 { "dasdb", (DASD_MAJOR << MINORBITS) + (1 << 2) },
276 { "dasdc", (DASD_MAJOR << MINORBITS) + (2 << 2) },
277 { "dasdd", (DASD_MAJOR << MINORBITS) + (3 << 2) },
278 { "dasde", (DASD_MAJOR << MINORBITS) + (4 << 2) },
279 { "dasdf", (DASD_MAJOR << MINORBITS) + (5 << 2) },
280 { "dasdg", (DASD_MAJOR << MINORBITS) + (6 << 2) },
281 { "dasdh", (DASD_MAJOR << MINORBITS) + (7 << 2) },
282 #endif
283 { NULL, 0 }
286 kdev_t __init name_to_kdev_t(char *line)
288 int base = 0;
290 if (strncmp(line,"/dev/",5) == 0) {
291 struct dev_name_struct *dev = root_dev_names;
292 line += 5;
293 do {
294 int len = strlen(dev->name);
295 if (strncmp(line,dev->name,len) == 0) {
296 line += len;
297 base = dev->num;
298 break;
300 dev++;
301 } while (dev->name);
303 return to_kdev_t(base + simple_strtoul(line,NULL,base?10:16));
306 static int __init root_dev_setup(char *line)
308 int i;
309 char ch;
311 ROOT_DEV = name_to_kdev_t(line);
312 memset (root_device_name, 0, sizeof root_device_name);
313 if (strncmp (line, "/dev/", 5) == 0) line += 5;
314 for (i = 0; i < sizeof root_device_name - 1; ++i)
316 ch = line[i];
317 if ( isspace (ch) || (ch == ',') || (ch == '\0') ) break;
318 root_device_name[i] = ch;
320 return 1;
323 __setup("root=", root_dev_setup);
325 static int __init checksetup(char *line)
327 struct kernel_param *p;
329 p = &__setup_start;
330 do {
331 int n = strlen(p->str);
332 if (!strncmp(line,p->str,n)) {
333 if (p->setup_func(line+n))
334 return 1;
336 p++;
337 } while (p < &__setup_end);
338 return 0;
341 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
342 still work even if initially too large, it will just take slightly longer */
343 unsigned long loops_per_sec = (1<<12);
345 /* This is the number of bits of precision for the loops_per_second. Each
346 bit takes on average 1.5/HZ seconds. This (like the original) is a little
347 better than 1% */
348 #define LPS_PREC 8
350 void __init calibrate_delay(void)
352 unsigned long ticks, loopbit;
353 int lps_precision = LPS_PREC;
355 loops_per_sec = (1<<12);
357 printk("Calibrating delay loop... ");
358 while (loops_per_sec <<= 1) {
359 /* wait for "start of" clock tick */
360 ticks = jiffies;
361 while (ticks == jiffies)
362 /* nothing */;
363 /* Go .. */
364 ticks = jiffies;
365 __delay(loops_per_sec);
366 ticks = jiffies - ticks;
367 if (ticks)
368 break;
371 /* Do a binary approximation to get loops_per_second set to equal one clock
372 (up to lps_precision bits) */
373 loops_per_sec >>= 1;
374 loopbit = loops_per_sec;
375 while ( lps_precision-- && (loopbit >>= 1) ) {
376 loops_per_sec |= loopbit;
377 ticks = jiffies;
378 while (ticks == jiffies);
379 ticks = jiffies;
380 __delay(loops_per_sec);
381 if (jiffies != ticks) /* longer than 1 tick */
382 loops_per_sec &= ~loopbit;
385 /* finally, adjust loops per second in terms of seconds instead of clocks */
386 loops_per_sec *= HZ;
387 /* Round the value and print it */
388 printk("%lu.%02lu BogoMIPS\n",
389 (loops_per_sec+2500)/500000,
390 ((loops_per_sec+2500)/5000) % 100);
393 static int __init readonly(char *str)
395 if (*str)
396 return 0;
397 root_mountflags |= MS_RDONLY;
398 return 1;
401 static int __init readwrite(char *str)
403 if (*str)
404 return 0;
405 root_mountflags &= ~MS_RDONLY;
406 return 1;
409 static int __init debug_kernel(char *str)
411 if (*str)
412 return 0;
413 console_loglevel = 10;
414 return 1;
417 static int __init quiet_kernel(char *str)
419 if (*str)
420 return 0;
421 console_loglevel = 4;
422 return 1;
425 __setup("ro", readonly);
426 __setup("rw", readwrite);
427 __setup("debug", debug_kernel);
428 __setup("quiet", quiet_kernel);
431 * This is a simple kernel command line parsing function: it parses
432 * the command line, and fills in the arguments/environment to init
433 * as appropriate. Any cmd-line option is taken to be an environment
434 * variable if it contains the character '='.
436 * This routine also checks for options meant for the kernel.
437 * These options are not given to init - they are for internal kernel use only.
439 static void __init parse_options(char *line)
441 char *next,*quote;
442 int args, envs;
444 if (!*line)
445 return;
446 args = 0;
447 envs = 1; /* TERM is set to 'linux' by default */
448 next = line;
449 while ((line = next) != NULL) {
450 quote = strchr(line,'"');
451 next = strchr(line, ' ');
452 while (next != NULL && quote != NULL && quote < next) {
453 /* we found a left quote before the next blank
454 * now we have to find the matching right quote
456 next = strchr(quote+1, '"');
457 if (next != NULL) {
458 quote = strchr(next+1, '"');
459 next = strchr(next+1, ' ');
462 if (next != NULL)
463 *next++ = 0;
464 if (!strncmp(line,"init=",5)) {
465 line += 5;
466 execute_command = line;
467 /* In case LILO is going to boot us with default command line,
468 * it prepends "auto" before the whole cmdline which makes
469 * the shell think it should execute a script with such name.
470 * So we ignore all arguments entered _before_ init=... [MJ]
472 args = 0;
473 continue;
475 if (checksetup(line))
476 continue;
479 * Then check if it's an environment variable or
480 * an option.
482 if (strchr(line,'=')) {
483 if (envs >= MAX_INIT_ENVS)
484 break;
485 envp_init[++envs] = line;
486 } else {
487 if (args >= MAX_INIT_ARGS)
488 break;
489 if(*line)
490 argv_init[++args] = line;
493 argv_init[args+1] = NULL;
494 envp_init[envs+1] = NULL;
498 extern void setup_arch(char **);
499 extern void cpu_idle(void);
501 #ifndef CONFIG_SMP
503 #ifdef CONFIG_X86_IO_APIC
504 static void __init smp_init(void)
506 IO_APIC_init_uniprocessor();
508 #else
509 #define smp_init() do { } while (0)
510 #endif
512 #else
514 /* Called by boot processor to activate the rest. */
515 static void __init smp_init(void)
517 /* Get other processors into their bootup holding patterns. */
518 smp_boot_cpus();
519 smp_threads_ready=1;
520 smp_commence();
523 #endif
526 * Activate the first processor.
529 asmlinkage void __init start_kernel(void)
531 char * command_line;
532 unsigned long mempages;
534 * Interrupts are still disabled. Do necessary setups, then
535 * enable them
537 lock_kernel();
538 printk(linux_banner);
539 setup_arch(&command_line);
540 trap_init();
541 init_IRQ();
542 sched_init();
543 time_init();
544 softirq_init();
545 parse_options(command_line);
548 * HACK ALERT! This is early. We're enabling the console before
549 * we've done PCI setups etc, and console_init() must be aware of
550 * this. But we do want output early, in case something goes wrong.
552 console_init();
553 #ifdef CONFIG_MODULES
554 init_modules();
555 #endif
556 if (prof_shift) {
557 unsigned int size;
558 /* only text is profiled */
559 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
560 prof_len >>= prof_shift;
562 size = prof_len * sizeof(unsigned int) + PAGE_SIZE-1;
563 prof_buffer = (unsigned int *) alloc_bootmem(size);
566 kmem_cache_init();
567 sti();
568 calibrate_delay();
569 #ifdef CONFIG_BLK_DEV_INITRD
570 if (initrd_start && !initrd_below_start_ok &&
571 initrd_start < min_low_pfn << PAGE_SHIFT) {
572 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
573 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
574 initrd_start = 0;
576 #endif
577 #ifdef CONFIG_BINFMT_IRIX
578 init_inventory();
579 #endif
580 mem_init();
581 kmem_cache_sizes_init();
582 #ifdef CONFIG_3215_CONSOLE
583 con3215_activate();
584 #endif
585 #ifdef CONFIG_PROC_FS
586 proc_root_init();
587 #endif
588 mempages = num_physpages;
590 fork_init(mempages);
591 filescache_init();
592 dcache_init(mempages);
593 vma_init();
594 buffer_init(mempages);
595 page_cache_init(mempages);
596 kiobuf_setup();
597 signals_init();
598 bdev_init();
599 inode_init(mempages);
600 file_table_init();
601 #if defined(CONFIG_SYSVIPC)
602 ipc_init();
603 #endif
604 #if defined(CONFIG_QUOTA)
605 dquot_init_hash();
606 #endif
607 check_bugs();
608 printk("POSIX conformance testing by UNIFIX\n");
611 * We count on the initial thread going ok
612 * Like idlers init is an unlocked kernel thread, which will
613 * make syscalls (and thus be locked).
615 smp_init();
616 kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
617 unlock_kernel();
618 current->need_resched = 1;
619 cpu_idle();
622 #ifdef CONFIG_BLK_DEV_INITRD
623 static int do_linuxrc(void * shell)
625 static char *argv[] = { "linuxrc", NULL, };
627 close(0);close(1);close(2);
628 setsid();
629 (void) open("/dev/console",O_RDWR,0);
630 (void) dup(0);
631 (void) dup(0);
632 return execve(shell, argv, envp_init);
635 #endif
637 struct task_struct *child_reaper = &init_task;
639 static void __init do_initcalls(void)
641 initcall_t *call;
643 call = &__initcall_start;
644 do {
645 (*call)();
646 call++;
647 } while (call < &__initcall_end);
651 * Ok, the machine is now initialized. None of the devices
652 * have been touched yet, but the CPU subsystem is up and
653 * running, and memory and process management works.
655 * Now we can finally start doing some real work..
657 static void __init do_basic_setup(void)
659 #ifdef CONFIG_BLK_DEV_INITRD
660 int real_root_mountflags;
661 #endif
664 * Tell the world that we're going to be the grim
665 * reaper of innocent orphaned children.
667 * We don't want people to have to make incorrect
668 * assumptions about where in the task array this
669 * can be found.
671 child_reaper = current;
673 #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
675 * We should probably create some architecture-dependent "fixup after
676 * everything is up" style function where this would belong better
677 * than in init/main.c..
679 mtrr_init();
680 #endif
682 #ifdef CONFIG_SYSCTL
683 sysctl_init();
684 #endif
687 * Ok, at this point all CPU's should be initialized, so
688 * we can start looking into devices..
690 #ifdef CONFIG_PCI
691 pci_init();
692 #endif
693 #ifdef CONFIG_SBUS
694 sbus_init();
695 #endif
696 #if defined(CONFIG_PPC)
697 ppc_init();
698 #endif
699 #ifdef CONFIG_MCA
700 mca_init();
701 #endif
702 #ifdef CONFIG_ARCH_ACORN
703 ecard_init();
704 #endif
705 #ifdef CONFIG_ZORRO
706 zorro_init();
707 #endif
708 #ifdef CONFIG_DIO
709 dio_init();
710 #endif
711 #ifdef CONFIG_MAC
712 nubus_init();
713 #endif
714 #ifdef CONFIG_ISAPNP
715 isapnp_init();
716 #endif
717 #ifdef CONFIG_USB
718 usb_init(); /* Do this before doing initcalls, so that we can make
719 usbcore initialize here, and all drivers initialize later */
720 #endif
721 #ifdef CONFIG_TC
722 tc_init();
723 #endif
725 /* Networking initialization needs a process context */
726 sock_init();
728 #ifdef CONFIG_BLK_DEV_INITRD
729 real_root_dev = ROOT_DEV;
730 real_root_mountflags = root_mountflags;
731 if (initrd_start && mount_initrd) root_mountflags &= ~MS_RDONLY;
732 else mount_initrd =0;
733 #endif
735 do_initcalls();
737 /* .. filesystems .. */
738 filesystem_setup();
740 #ifdef CONFIG_IRDA
741 irda_device_init(); /* Must be done after protocol initialization */
742 #endif
743 #ifdef CONFIG_PCMCIA
744 init_pcmcia_ds(); /* Do this last */
745 #endif
746 /* Mount the root filesystem.. */
747 mount_root();
749 mount_devfs_fs ();
751 #ifdef CONFIG_BLK_DEV_INITRD
752 root_mountflags = real_root_mountflags;
753 if (mount_initrd && MAJOR(ROOT_DEV) == RAMDISK_MAJOR && MINOR(ROOT_DEV) == 0) {
754 int error;
755 int i, pid;
757 pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
758 if (pid>0)
759 while (pid != wait(&i));
760 if (MAJOR(real_root_dev) != RAMDISK_MAJOR
761 || MINOR(real_root_dev) != 0) {
762 #ifdef CONFIG_BLK_DEV_MD
763 autodetect_raid();
764 #endif
765 error = change_root(real_root_dev,"/initrd");
766 if (error)
767 printk(KERN_ERR "Change root to /initrd: "
768 "error %d\n",error);
771 #endif
774 static int init(void * unused)
776 lock_kernel();
777 do_basic_setup();
780 * Ok, we have completed the initial bootup, and
781 * we're essentially up and running. Get rid of the
782 * initmem segments and start the user-mode stuff..
784 free_initmem();
785 unlock_kernel();
787 if (open("/dev/console", O_RDWR, 0) < 0)
788 printk("Warning: unable to open an initial console.\n");
790 (void) dup(0);
791 (void) dup(0);
794 * We try each of these until one succeeds.
796 * The Bourne shell can be used instead of init if we are
797 * trying to recover a really broken machine.
800 if (execute_command)
801 execve(execute_command,argv_init,envp_init);
802 execve("/sbin/init",argv_init,envp_init);
803 execve("/etc/init",argv_init,envp_init);
804 execve("/bin/init",argv_init,envp_init);
805 execve("/bin/sh",argv_init,envp_init);
806 panic("No init found. Try passing init= option to kernel.");