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