Merge with Linux 2.4.0-test5-pre1. This works again on Origin UP.
[linux-2.6/linux-mips.git] / init / main.c
blob2876ba242f74c86861172d9a620cd15b2dda6f95
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
119 * Boot command-line arguments
121 #define MAX_INIT_ARGS 8
122 #define MAX_INIT_ENVS 8
124 extern void time_init(void);
125 extern void softirq_init(void);
127 int rows, cols;
129 #ifdef CONFIG_BLK_DEV_INITRD
130 kdev_t real_root_dev;
131 #endif
133 int root_mountflags = MS_RDONLY;
134 char *execute_command;
135 char root_device_name[64];
138 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
139 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
141 static int __init profile_setup(char *str)
143 int par;
144 if (get_option(&str,&par)) prof_shift = par;
145 return 1;
148 __setup("profile=", profile_setup);
151 static struct dev_name_struct {
152 const char *name;
153 const int num;
154 } root_dev_names[] __initdata = {
155 { "nfs", 0x00ff },
156 { "hda", 0x0300 },
157 { "hdb", 0x0340 },
158 { "hdc", 0x1600 },
159 { "hdd", 0x1640 },
160 { "hde", 0x2100 },
161 { "hdf", 0x2140 },
162 { "hdg", 0x2200 },
163 { "hdh", 0x2240 },
164 { "hdi", 0x3800 },
165 { "hdj", 0x3840 },
166 { "hdk", 0x3900 },
167 { "hdl", 0x3940 },
168 { "hdm", 0x5800 },
169 { "hdn", 0x5840 },
170 { "hdo", 0x5900 },
171 { "hdp", 0x5940 },
172 { "hdq", 0x5A00 },
173 { "hdr", 0x5A40 },
174 { "hds", 0x5B00 },
175 { "hdt", 0x5B40 },
176 { "sda", 0x0800 },
177 { "sdb", 0x0810 },
178 { "sdc", 0x0820 },
179 { "sdd", 0x0830 },
180 { "sde", 0x0840 },
181 { "sdf", 0x0850 },
182 { "sdg", 0x0860 },
183 { "sdh", 0x0870 },
184 { "sdi", 0x0880 },
185 { "sdj", 0x0890 },
186 { "sdk", 0x08a0 },
187 { "sdl", 0x08b0 },
188 { "sdm", 0x08c0 },
189 { "sdn", 0x08d0 },
190 { "sdo", 0x08e0 },
191 { "sdp", 0x08f0 },
192 { "ada", 0x1c00 },
193 { "adb", 0x1c10 },
194 { "adc", 0x1c20 },
195 { "add", 0x1c30 },
196 { "ade", 0x1c40 },
197 { "fd", 0x0200 },
198 { "md", 0x0900 },
199 { "xda", 0x0d00 },
200 { "xdb", 0x0d40 },
201 { "ram", 0x0100 },
202 { "scd", 0x0b00 },
203 { "mcd", 0x1700 },
204 { "cdu535", 0x1800 },
205 { "sonycd", 0x1800 },
206 { "aztcd", 0x1d00 },
207 { "cm206cd", 0x2000 },
208 { "gscd", 0x1000 },
209 { "sbpcd", 0x1900 },
210 { "eda", 0x2400 },
211 { "edb", 0x2440 },
212 { "pda", 0x2d00 },
213 { "pdb", 0x2d10 },
214 { "pdc", 0x2d20 },
215 { "pdd", 0x2d30 },
216 { "pcd", 0x2e00 },
217 { "pf", 0x2f00 },
218 { "apblock", APBLOCK_MAJOR << 8},
219 { "ddv", DDV_MAJOR << 8},
220 { "jsfd", JSFD_MAJOR << 8},
221 #ifdef CONFIG_MDISK
222 { "mnda", (MDISK_MAJOR << MINORBITS)},
223 { "mndb", (MDISK_MAJOR << MINORBITS) + 1},
224 { "mndc", (MDISK_MAJOR << MINORBITS) + 2},
225 { "mndd", (MDISK_MAJOR << MINORBITS) + 3},
226 { "mnde", (MDISK_MAJOR << MINORBITS) + 4},
227 { "mndf", (MDISK_MAJOR << MINORBITS) + 5},
228 { "mndg", (MDISK_MAJOR << MINORBITS) + 6},
229 { "mndh", (MDISK_MAJOR << MINORBITS) + 7},
230 #endif
231 #ifdef CONFIG_DASD
232 { "dasda", (DASD_MAJOR << MINORBITS) },
233 { "dasdb", (DASD_MAJOR << MINORBITS) + (1 << 2) },
234 { "dasdc", (DASD_MAJOR << MINORBITS) + (2 << 2) },
235 { "dasdd", (DASD_MAJOR << MINORBITS) + (3 << 2) },
236 { "dasde", (DASD_MAJOR << MINORBITS) + (4 << 2) },
237 { "dasdf", (DASD_MAJOR << MINORBITS) + (5 << 2) },
238 { "dasdg", (DASD_MAJOR << MINORBITS) + (6 << 2) },
239 { "dasdh", (DASD_MAJOR << MINORBITS) + (7 << 2) },
240 #endif
241 #if defined(CONFIG_BLK_CPQ_DA) || defined(CONFIG_BLK_CPQ_DA_MODULE)
242 { "ida/c0d0p",0x4800 },
243 { "ida/c0d1p",0x4810 },
244 { "ida/c0d2p",0x4820 },
245 { "ida/c0d3p",0x4830 },
246 { "ida/c0d4p",0x4840 },
247 { "ida/c0d5p",0x4850 },
248 { "ida/c0d6p",0x4860 },
249 { "ida/c0d7p",0x4870 },
250 { "ida/c0d8p",0x4880 },
251 { "ida/c0d9p",0x4890 },
252 { "ida/c0d10p",0x48A0 },
253 { "ida/c0d11p",0x48B0 },
254 { "ida/c0d12p",0x48C0 },
255 { "ida/c0d13p",0x48D0 },
256 { "ida/c0d14p",0x48E0 },
257 { "ida/c0d15p",0x48F0 },
258 #endif
260 { NULL, 0 }
263 kdev_t __init name_to_kdev_t(char *line)
265 int base = 0;
267 if (strncmp(line,"/dev/",5) == 0) {
268 struct dev_name_struct *dev = root_dev_names;
269 line += 5;
270 do {
271 int len = strlen(dev->name);
272 if (strncmp(line,dev->name,len) == 0) {
273 line += len;
274 base = dev->num;
275 break;
277 dev++;
278 } while (dev->name);
280 return to_kdev_t(base + simple_strtoul(line,NULL,base?10:16));
283 static int __init root_dev_setup(char *line)
285 int i;
286 char ch;
288 ROOT_DEV = name_to_kdev_t(line);
289 memset (root_device_name, 0, sizeof root_device_name);
290 if (strncmp (line, "/dev/", 5) == 0) line += 5;
291 for (i = 0; i < sizeof root_device_name - 1; ++i)
293 ch = line[i];
294 if ( isspace (ch) || (ch == ',') || (ch == '\0') ) break;
295 root_device_name[i] = ch;
297 return 1;
300 __setup("root=", root_dev_setup);
302 static int __init checksetup(char *line)
304 struct kernel_param *p;
306 p = &__setup_start;
307 do {
308 int n = strlen(p->str);
309 if (!strncmp(line,p->str,n)) {
310 if (p->setup_func(line+n))
311 return 1;
313 p++;
314 } while (p < &__setup_end);
315 return 0;
318 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
319 still work even if initially too large, it will just take slightly longer */
320 unsigned long loops_per_sec = (1<<12);
322 /* This is the number of bits of precision for the loops_per_second. Each
323 bit takes on average 1.5/HZ seconds. This (like the original) is a little
324 better than 1% */
325 #define LPS_PREC 8
327 void __init calibrate_delay(void)
329 unsigned long ticks, loopbit;
330 int lps_precision = LPS_PREC;
332 loops_per_sec = (1<<12);
334 printk("Calibrating delay loop... ");
335 while (loops_per_sec <<= 1) {
336 /* wait for "start of" clock tick */
337 ticks = jiffies;
338 while (ticks == jiffies)
339 /* nothing */;
340 /* Go .. */
341 ticks = jiffies;
342 __delay(loops_per_sec);
343 ticks = jiffies - ticks;
344 if (ticks)
345 break;
348 /* Do a binary approximation to get loops_per_second set to equal one clock
349 (up to lps_precision bits) */
350 loops_per_sec >>= 1;
351 loopbit = loops_per_sec;
352 while ( lps_precision-- && (loopbit >>= 1) ) {
353 loops_per_sec |= loopbit;
354 ticks = jiffies;
355 while (ticks == jiffies);
356 ticks = jiffies;
357 __delay(loops_per_sec);
358 if (jiffies != ticks) /* longer than 1 tick */
359 loops_per_sec &= ~loopbit;
362 /* finally, adjust loops per second in terms of seconds instead of clocks */
363 loops_per_sec *= HZ;
364 /* Round the value and print it */
365 printk("%lu.%02lu BogoMIPS\n",
366 (loops_per_sec+2500)/500000,
367 ((loops_per_sec+2500)/5000) % 100);
370 static int __init readonly(char *str)
372 if (*str)
373 return 0;
374 root_mountflags |= MS_RDONLY;
375 return 1;
378 static int __init readwrite(char *str)
380 if (*str)
381 return 0;
382 root_mountflags &= ~MS_RDONLY;
383 return 1;
386 static int __init debug_kernel(char *str)
388 if (*str)
389 return 0;
390 console_loglevel = 10;
391 return 1;
394 static int __init quiet_kernel(char *str)
396 if (*str)
397 return 0;
398 console_loglevel = 4;
399 return 1;
402 __setup("ro", readonly);
403 __setup("rw", readwrite);
404 __setup("debug", debug_kernel);
405 __setup("quiet", quiet_kernel);
408 * This is a simple kernel command line parsing function: it parses
409 * the command line, and fills in the arguments/environment to init
410 * as appropriate. Any cmd-line option is taken to be an environment
411 * variable if it contains the character '='.
413 * This routine also checks for options meant for the kernel.
414 * These options are not given to init - they are for internal kernel use only.
416 static void __init parse_options(char *line)
418 char *next,*quote;
419 int args, envs;
421 if (!*line)
422 return;
423 args = 0;
424 envs = 1; /* TERM is set to 'linux' by default */
425 next = line;
426 while ((line = next) != NULL) {
427 quote = strchr(line,'"');
428 next = strchr(line, ' ');
429 while (next != NULL && quote != NULL && quote < next) {
430 /* we found a left quote before the next blank
431 * now we have to find the matching right quote
433 next = strchr(quote+1, '"');
434 if (next != NULL) {
435 quote = strchr(next+1, '"');
436 next = strchr(next+1, ' ');
439 if (next != NULL)
440 *next++ = 0;
441 if (!strncmp(line,"init=",5)) {
442 line += 5;
443 execute_command = line;
444 /* In case LILO is going to boot us with default command line,
445 * it prepends "auto" before the whole cmdline which makes
446 * the shell think it should execute a script with such name.
447 * So we ignore all arguments entered _before_ init=... [MJ]
449 args = 0;
450 continue;
452 if (checksetup(line))
453 continue;
456 * Then check if it's an environment variable or
457 * an option.
459 if (strchr(line,'=')) {
460 if (envs >= MAX_INIT_ENVS)
461 break;
462 envp_init[++envs] = line;
463 } else {
464 if (args >= MAX_INIT_ARGS)
465 break;
466 if(*line)
467 argv_init[++args] = line;
470 argv_init[args+1] = NULL;
471 envp_init[envs+1] = NULL;
475 extern void setup_arch(char **);
476 extern void cpu_idle(void);
478 #ifndef CONFIG_SMP
480 #ifdef CONFIG_X86_IO_APIC
481 static void __init smp_init(void)
483 IO_APIC_init_uniprocessor();
485 #else
486 #define smp_init() do { } while (0)
487 #endif
489 #else
491 /* Called by boot processor to activate the rest. */
492 static void __init smp_init(void)
494 /* Get other processors into their bootup holding patterns. */
495 smp_boot_cpus();
496 smp_threads_ready=1;
497 smp_commence();
500 #endif
503 * Activate the first processor.
506 asmlinkage void __init start_kernel(void)
508 char * command_line;
509 unsigned long mempages;
510 extern char saved_command_line[];
512 * Interrupts are still disabled. Do necessary setups, then
513 * enable them
515 lock_kernel();
516 printk(linux_banner);
517 setup_arch(&command_line);
518 printk("Kernel command line: %s\n", saved_command_line);
519 parse_options(command_line);
520 trap_init();
521 init_IRQ();
522 sched_init();
523 time_init();
524 softirq_init();
527 * HACK ALERT! This is early. We're enabling the console before
528 * we've done PCI setups etc, and console_init() must be aware of
529 * this. But we do want output early, in case something goes wrong.
531 console_init();
532 #ifdef CONFIG_MODULES
533 init_modules();
534 #endif
535 if (prof_shift) {
536 unsigned int size;
537 /* only text is profiled */
538 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
539 prof_len >>= prof_shift;
541 size = prof_len * sizeof(unsigned int) + PAGE_SIZE-1;
542 prof_buffer = (unsigned int *) alloc_bootmem(size);
545 kmem_cache_init();
546 sti();
547 calibrate_delay();
548 #ifdef CONFIG_BLK_DEV_INITRD
549 if (initrd_start && !initrd_below_start_ok &&
550 initrd_start < min_low_pfn << PAGE_SHIFT) {
551 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
552 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
553 initrd_start = 0;
555 #endif
556 #ifdef CONFIG_BINFMT_IRIX
557 init_inventory();
558 #endif
559 mem_init();
560 kmem_cache_sizes_init();
561 #ifdef CONFIG_3215_CONSOLE
562 con3215_activate();
563 #endif
564 #ifdef CONFIG_PROC_FS
565 proc_root_init();
566 #endif
567 mempages = num_physpages;
569 fork_init(mempages);
570 filescache_init();
571 dcache_init(mempages);
572 vma_init();
573 buffer_init(mempages);
574 page_cache_init(mempages);
575 kiobuf_setup();
576 signals_init();
577 bdev_init();
578 inode_init(mempages);
579 file_table_init();
580 #if defined(CONFIG_SYSVIPC)
581 ipc_init();
582 #endif
583 #if defined(CONFIG_QUOTA)
584 dquot_init_hash();
585 #endif
586 check_bugs();
587 printk("POSIX conformance testing by UNIFIX\n");
590 * We count on the initial thread going ok
591 * Like idlers init is an unlocked kernel thread, which will
592 * make syscalls (and thus be locked).
594 smp_init();
595 kmem_cpucache_init();
596 kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
597 unlock_kernel();
598 current->need_resched = 1;
599 cpu_idle();
602 #ifdef CONFIG_BLK_DEV_INITRD
603 static int do_linuxrc(void * shell)
605 static char *argv[] = { "linuxrc", NULL, };
607 close(0);close(1);close(2);
608 setsid();
609 (void) open("/dev/console",O_RDWR,0);
610 (void) dup(0);
611 (void) dup(0);
612 return execve(shell, argv, envp_init);
615 #endif
617 struct task_struct *child_reaper = &init_task;
619 static void __init do_initcalls(void)
621 initcall_t *call;
623 call = &__initcall_start;
624 do {
625 (*call)();
626 call++;
627 } while (call < &__initcall_end);
631 * Ok, the machine is now initialized. None of the devices
632 * have been touched yet, but the CPU subsystem is up and
633 * running, and memory and process management works.
635 * Now we can finally start doing some real work..
637 static void __init do_basic_setup(void)
639 #ifdef CONFIG_BLK_DEV_INITRD
640 int real_root_mountflags;
641 #endif
644 * Tell the world that we're going to be the grim
645 * reaper of innocent orphaned children.
647 * We don't want people to have to make incorrect
648 * assumptions about where in the task array this
649 * can be found.
651 child_reaper = current;
653 #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
655 * We should probably create some architecture-dependent "fixup after
656 * everything is up" style function where this would belong better
657 * than in init/main.c..
659 mtrr_init();
660 #endif
662 #ifdef CONFIG_SYSCTL
663 sysctl_init();
664 #endif
667 * Ok, at this point all CPU's should be initialized, so
668 * we can start looking into devices..
670 #ifdef CONFIG_PCI
671 pci_init();
672 #endif
673 #ifdef CONFIG_SBUS
674 sbus_init();
675 #endif
676 #if defined(CONFIG_PPC)
677 ppc_init();
678 #endif
679 #ifdef CONFIG_MCA
680 mca_init();
681 #endif
682 #ifdef CONFIG_ARCH_ACORN
683 ecard_init();
684 #endif
685 #ifdef CONFIG_ZORRO
686 zorro_init();
687 #endif
688 #ifdef CONFIG_DIO
689 dio_init();
690 #endif
691 #ifdef CONFIG_MAC
692 nubus_init();
693 #endif
694 #ifdef CONFIG_ISAPNP
695 isapnp_init();
696 #endif
697 #ifdef CONFIG_USB
698 usb_init(); /* Do this before doing initcalls, so that we can make
699 usbcore initialize here, and all drivers initialize later */
700 #endif
701 #ifdef CONFIG_TC
702 tc_init();
703 #endif
705 /* Networking initialization needs a process context */
706 sock_init();
708 #ifdef CONFIG_BLK_DEV_INITRD
709 real_root_dev = ROOT_DEV;
710 real_root_mountflags = root_mountflags;
711 if (initrd_start && mount_initrd) root_mountflags &= ~MS_RDONLY;
712 else mount_initrd =0;
713 #endif
715 do_initcalls();
717 /* .. filesystems .. */
718 filesystem_setup();
720 #ifdef CONFIG_IRDA
721 irda_device_init(); /* Must be done after protocol initialization */
722 #endif
723 #ifdef CONFIG_PCMCIA
724 init_pcmcia_ds(); /* Do this last */
725 #endif
726 /* Mount the root filesystem.. */
727 mount_root();
729 mount_devfs_fs ();
731 #ifdef CONFIG_BLK_DEV_INITRD
732 root_mountflags = real_root_mountflags;
733 if (mount_initrd && ROOT_DEV != real_root_dev
734 && MAJOR(ROOT_DEV) == RAMDISK_MAJOR && MINOR(ROOT_DEV) == 0) {
735 int error;
736 int i, pid;
738 pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD);
739 if (pid>0)
740 while (pid != wait(&i));
741 if (MAJOR(real_root_dev) != RAMDISK_MAJOR
742 || MINOR(real_root_dev) != 0) {
743 #ifdef CONFIG_BLK_DEV_MD
744 md_run_setup();
745 #endif
746 error = change_root(real_root_dev,"/initrd");
747 if (error)
748 printk(KERN_ERR "Change root to /initrd: "
749 "error %d\n",error);
752 #endif
755 static int init(void * unused)
757 lock_kernel();
758 do_basic_setup();
761 * Ok, we have completed the initial bootup, and
762 * we're essentially up and running. Get rid of the
763 * initmem segments and start the user-mode stuff..
765 free_initmem();
766 unlock_kernel();
768 if (open("/dev/console", O_RDWR, 0) < 0)
769 printk("Warning: unable to open an initial console.\n");
771 (void) dup(0);
772 (void) dup(0);
775 * We try each of these until one succeeds.
777 * The Bourne shell can be used instead of init if we are
778 * trying to recover a really broken machine.
781 if (execute_command)
782 execve(execute_command,argv_init,envp_init);
783 execve("/sbin/init",argv_init,envp_init);
784 execve("/etc/init",argv_init,envp_init);
785 execve("/bin/init",argv_init,envp_init);
786 execve("/bin/sh",argv_init,envp_init);
787 panic("No init found. Try passing init= option to kernel.");