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