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