make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / init / main.c
1 /*
2  *  linux/init/main.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
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> 
10  */
11
12 #define __KERNEL_SYSCALLS__
13
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/smp_lock.h>
25 #include <linux/blk.h>
26 #include <linux/hdreg.h>
27 #include <linux/iobuf.h>
28 #include <linux/bootmem.h>
29 #include <linux/tty.h>
30
31 #include <asm/io.h>
32 #include <asm/bugs.h>
33
34 //REX:
35 //#define REX_DBG_MAIN
36 #ifdef REX_DBG_MAIN
37 #include <asm/machdep.h>
38 extern struct machdep_calls ppc_md;
39 #endif
40
41 #if defined(CONFIG_ARCH_S390)
42 #include <asm/s390mach.h>
43 #include <asm/ccwcache.h>
44 #endif
45
46 #ifdef CONFIG_PCI
47 #include <linux/pci.h>
48 #endif
49
50 #ifdef CONFIG_DIO
51 #include <linux/dio.h>
52 #endif
53
54 #ifdef CONFIG_ZORRO
55 #include <linux/zorro.h>
56 #endif
57
58 #ifdef CONFIG_MTRR
59 #  include <asm/mtrr.h>
60 #endif
61
62 #ifdef CONFIG_NUBUS
63 #include <linux/nubus.h>
64 #endif
65
66 #ifdef CONFIG_ISAPNP
67 #include <linux/isapnp.h>
68 #endif
69
70 #ifdef CONFIG_IRDA
71 extern int irda_proto_init(void);
72 extern int irda_device_init(void);
73 #endif
74
75 #ifdef CONFIG_X86_LOCAL_APIC
76 #include <asm/smp.h>
77 #endif
78 /*
79  * Versions of gcc older than that listed below may actually compile
80  * and link okay, but the end product can have subtle run time bugs.
81  * To avoid associated bogus bug reports, we flatly refuse to compile
82  * with a gcc that is known to be too old from the very beginning.
83  */
84 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
85 #error Sorry, your GCC is too old. It builds incorrect kernels.
86 #endif
87
88 extern char _stext, _etext;
89 extern char *linux_banner;
90
91 static int init(void *);
92
93 extern void init_IRQ(void);
94 extern void init_modules(void);
95 extern void sock_init(void);
96 extern void fork_init(unsigned long);
97 extern void mca_init(void);
98 extern void sbus_init(void);
99 extern void ppc_init(void);
100 extern void sysctl_init(void);
101 extern void signals_init(void);
102 extern int init_pcmcia_ds(void);
103
104 extern void free_initmem(void);
105
106 #ifdef CONFIG_TC
107 extern void tc_init(void);
108 #endif
109
110 extern void ecard_init(void);
111
112 #if defined(CONFIG_SYSVIPC)
113 extern void ipc_init(void);
114 #endif
115
116 /*
117  * Boot command-line arguments
118  */
119 #define MAX_INIT_ARGS 8
120 #define MAX_INIT_ENVS 8
121
122 extern void time_init(void);
123 extern void softirq_init(void);
124
125 int rows, cols;
126
127 char *execute_command;
128
129 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
130 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
131
132 static int __init profile_setup(char *str)
133 {
134     int par;
135     if (get_option(&str,&par)) prof_shift = par;
136         return 1;
137 }
138
139 __setup("profile=", profile_setup);
140
141 static int __init checksetup(char *line)
142 {
143         struct kernel_param *p;
144
145         p = &__setup_start;
146         do {
147                 int n = strlen(p->str);
148                 if (!strncmp(line,p->str,n)) {
149                         if (p->setup_func(line+n))
150                                 return 1;
151                 }
152                 p++;
153         } while (p < &__setup_end);
154         return 0;
155 }
156
157 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
158    still work even if initially too large, it will just take slightly longer */
159 unsigned long loops_per_jiffy = (1<<12);
160
161 /* This is the number of bits of precision for the loops_per_jiffy.  Each
162    bit takes on average 1.5/HZ seconds.  This (like the original) is a little
163    better than 1% */
164 #define LPS_PREC 8
165
166 void __init calibrate_delay(void)
167 {
168         unsigned long ticks, loopbit;
169         int lps_precision = LPS_PREC;
170
171         loops_per_jiffy = (1<<12);
172
173         printk("Calibrating delay loop... ");
174         while (loops_per_jiffy <<= 1) {
175                 /* wait for "start of" clock tick */
176                 ticks = jiffies;
177                 while (ticks == jiffies)
178                         /* nothing */;
179                 /* Go .. */
180                 ticks = jiffies;
181                 __delay(loops_per_jiffy);
182                 ticks = jiffies - ticks;
183                 if (ticks)
184                         break;
185         }
186
187 /* Do a binary approximation to get loops_per_jiffy set to equal one clock
188    (up to lps_precision bits) */
189         loops_per_jiffy >>= 1;
190         loopbit = loops_per_jiffy;
191         while ( lps_precision-- && (loopbit >>= 1) ) {
192                 loops_per_jiffy |= loopbit;
193                 ticks = jiffies;
194                 while (ticks == jiffies);
195                 ticks = jiffies;
196                 __delay(loops_per_jiffy);
197                 if (jiffies != ticks)   /* longer than 1 tick */
198                         loops_per_jiffy &= ~loopbit;
199         }
200
201 /* Round the value and print it */      
202         printk("%lu.%02lu BogoMIPS\n",
203                 loops_per_jiffy/(500000/HZ),
204                 (loops_per_jiffy/(5000/HZ)) % 100);
205 }
206
207 static int __init debug_kernel(char *str)
208 {
209         if (*str)
210                 return 0;
211         console_loglevel = 10;
212         return 1;
213 }
214
215 static int __init quiet_kernel(char *str)
216 {
217         if (*str)
218                 return 0;
219         console_loglevel = 4;
220         return 1;
221 }
222
223 __setup("debug", debug_kernel);
224 __setup("quiet", quiet_kernel);
225
226 /*
227  * This is a simple kernel command line parsing function: it parses
228  * the command line, and fills in the arguments/environment to init
229  * as appropriate. Any cmd-line option is taken to be an environment
230  * variable if it contains the character '='.
231  *
232  * This routine also checks for options meant for the kernel.
233  * These options are not given to init - they are for internal kernel use only.
234  */
235 static void __init parse_options(char *line)
236 {
237         char *next,*quote;
238         int args, envs;
239
240         if (!*line)
241                 return;
242         args = 0;
243         envs = 1;       /* TERM is set to 'linux' by default */
244         next = line;
245         while ((line = next) != NULL) {
246                 quote = strchr(line,'"');
247                 next = strchr(line, ' ');
248                 while (next != NULL && quote != NULL && quote < next) {
249                         /* we found a left quote before the next blank
250                          * now we have to find the matching right quote
251                          */
252                         next = strchr(quote+1, '"');
253                         if (next != NULL) {
254                                 quote = strchr(next+1, '"');
255                                 next = strchr(next+1, ' ');
256                         }
257                 }
258                 if (next != NULL)
259                         *next++ = 0;
260                 if (!strncmp(line,"init=",5)) {
261                         line += 5;
262                         execute_command = line;
263                         /* In case LILO is going to boot us with default command line,
264                          * it prepends "auto" before the whole cmdline which makes
265                          * the shell think it should execute a script with such name.
266                          * So we ignore all arguments entered _before_ init=... [MJ]
267                          */
268                         args = 0;
269                         continue;
270                 }
271                 if (checksetup(line))
272                         continue;
273                 
274                 /*
275                  * Then check if it's an environment variable or
276                  * an option.
277                  */
278                 if (strchr(line,'=')) {
279                         if (envs >= MAX_INIT_ENVS)
280                                 break;
281                         envp_init[++envs] = line;
282                 } else {
283                         if (args >= MAX_INIT_ARGS)
284                                 break;
285                         if (*line)
286                                 argv_init[++args] = line;
287                 }
288         }
289         argv_init[args+1] = NULL;
290         envp_init[envs+1] = NULL;
291 }
292
293
294 extern void setup_arch(char **);
295 extern void cpu_idle(void);
296
297 unsigned long wait_init_idle;
298
299 #ifndef CONFIG_SMP
300
301 #ifdef CONFIG_X86_LOCAL_APIC
302 static void __init smp_init(void)
303 {
304         APIC_init_uniprocessor();
305 }
306 #else
307 #define smp_init()      do { } while (0)
308 #endif
309
310 #else
311
312
313 /* Called by boot processor to activate the rest. */
314 static void __init smp_init(void)
315 {
316         /* Get other processors into their bootup holding patterns. */
317         smp_boot_cpus();
318         wait_init_idle = cpu_online_map;
319         clear_bit(current->processor, &wait_init_idle); /* Don't wait on me! */
320
321         smp_threads_ready=1;
322         smp_commence();
323
324         /* Wait for the other cpus to set up their idle processes */
325         printk("Waiting on wait_init_idle (map = 0x%lx)\n", wait_init_idle);
326         while (wait_init_idle) {
327                 cpu_relax();
328                 barrier();
329         }
330         printk("All processors have done init_idle\n");
331 }
332
333 #endif
334
335 /*
336  * We need to finalize in a non-__init function or else race conditions
337  * between the root thread and the init thread may cause start_kernel to
338  * be reaped by free_initmem before the root thread has proceeded to
339  * cpu_idle.
340  */
341
342 static void rest_init(void)
343 {
344         kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);
345         unlock_kernel();
346         current->need_resched = 1;
347         cpu_idle();
348
349
350 /*
351  *      Activate the first processor.
352  */
353
354 asmlinkage void __init start_kernel(void)
355 {
356         char * command_line;
357         extern char saved_command_line[];
358 /*
359  * Interrupts are still disabled. Do necessary setups, then
360  * enable them
361  */
362         lock_kernel();
363         printk(linux_banner);
364         setup_arch(&command_line);
365         printk("Kernel command line: %s\n", saved_command_line);
366         parse_options(command_line);
367         trap_init();
368         init_IRQ();
369         sched_init();
370 #ifdef REX_DBG_MAIN
371         if (ppc_md.progress) ppc_md.progress("main.c: finish sched_init()",0x100);
372 #endif  
373         softirq_init();
374 #ifdef REX_DBG_MAIN
375         if (ppc_md.progress) ppc_md.progress("main.c: finish softirq_init()",0x101);
376 #endif  
377         time_init();
378 #ifdef REX_DBG_MAIN
379         if (ppc_md.progress) ppc_md.progress("main.c: finish time_init()",0x102);
380 #endif  
381
382         /*
383          * HACK ALERT! This is early. We're enabling the console before
384          * we've done PCI setups etc, and console_init() must be aware of
385          * this. But we do want output early, in case something goes wrong.
386          */
387         console_init();
388 #ifdef REX_DBG_MAIN
389         if (ppc_md.progress) ppc_md.progress("main.c: finish console_init()",0x103);
390 #endif
391         
392 #ifdef CONFIG_MODULES
393         init_modules();
394 #endif
395         if (prof_shift) {
396                 unsigned int size;
397                 /* only text is profiled */
398                 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
399                 prof_len >>= prof_shift;
400                 
401                 size = prof_len * sizeof(unsigned int) + PAGE_SIZE-1;
402                 prof_buffer = (unsigned int *) alloc_bootmem(size);
403         }
404
405         kmem_cache_init();
406 #ifdef REX_DBG_MAIN
407         if (ppc_md.progress) ppc_md.progress("main.c: finish kmen_cache_init()",0x103);
408 #endif
409
410         sti();
411         calibrate_delay();
412 #ifdef CONFIG_BLK_DEV_INITRD
413         if (initrd_start && !initrd_below_start_ok &&
414                         initrd_start < min_low_pfn << PAGE_SHIFT) {
415                 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
416                     "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
417                 initrd_start = 0;
418         }
419 #endif
420         mem_init();
421         kmem_cache_sizes_init();
422         pgtable_cache_init();
423
424         /*
425          * For architectures that have highmem, num_mappedpages represents
426          * the amount of memory the kernel can use.  For other architectures
427          * it's the same as the total pages.  We need both numbers because
428          * some subsystems need to initialize based on how much memory the
429          * kernel can use.
430          */
431         if (num_mappedpages == 0)
432                 num_mappedpages = num_physpages;
433   
434         fork_init(num_mappedpages);
435         proc_caches_init();
436         vfs_caches_init(num_physpages);
437         buffer_init(num_physpages);
438         page_cache_init(num_physpages);
439 #if defined(CONFIG_ARCH_S390)
440         ccwcache_init();
441 #endif
442         signals_init();
443 #ifdef CONFIG_PROC_FS
444         proc_root_init();
445 #endif
446 #if defined(CONFIG_SYSVIPC)
447         ipc_init();
448 #endif
449         check_bugs();
450         printk("POSIX conformance testing by UNIFIX\n");
451
452         /* 
453          *      We count on the initial thread going ok 
454          *      Like idlers init is an unlocked kernel thread, which will
455          *      make syscalls (and thus be locked).
456          */
457         smp_init();
458         rest_init();
459 }
460
461 struct task_struct *child_reaper = &init_task;
462
463 static void __init do_initcalls(void)
464 {
465         initcall_t *call;
466
467         call = &__initcall_start;
468         do {
469                 (*call)();
470                 call++;
471         } while (call < &__initcall_end);
472
473         /* Make sure there is no pending stuff from the initcall sequence */
474         flush_scheduled_tasks();
475 }
476
477 /*
478  * Ok, the machine is now initialized. None of the devices
479  * have been touched yet, but the CPU subsystem is up and
480  * running, and memory and process management works.
481  *
482  * Now we can finally start doing some real work..
483  */
484 static void __init do_basic_setup(void)
485 {
486
487         /*
488          * Tell the world that we're going to be the grim
489          * reaper of innocent orphaned children.
490          *
491          * We don't want people to have to make incorrect
492          * assumptions about where in the task array this
493          * can be found.
494          */
495         child_reaper = current;
496
497 #if defined(CONFIG_MTRR)        /* Do this after SMP initialization */
498 /*
499  * We should probably create some architecture-dependent "fixup after
500  * everything is up" style function where this would belong better
501  * than in init/main.c..
502  */
503         mtrr_init();
504 #endif
505
506 #ifdef CONFIG_SYSCTL
507         sysctl_init();
508 #endif
509
510         /*
511          * Ok, at this point all CPU's should be initialized, so
512          * we can start looking into devices..
513          */
514 #if defined(CONFIG_ARCH_S390)
515         s390_init_machine_check();
516 #endif
517
518 #ifdef CONFIG_PCI
519         pci_init();
520 #endif
521 #ifdef CONFIG_SBUS
522         sbus_init();
523 #endif
524 #if defined(CONFIG_PPC)
525         ppc_init();
526 #endif
527 #ifdef CONFIG_MCA
528         mca_init();
529 #endif
530 #ifdef CONFIG_ARCH_ACORN
531         ecard_init();
532 #endif
533 #ifdef CONFIG_ZORRO
534         zorro_init();
535 #endif
536 #ifdef CONFIG_DIO
537         dio_init();
538 #endif
539 #ifdef CONFIG_NUBUS
540         nubus_init();
541 #endif
542 #ifdef CONFIG_ISAPNP
543         isapnp_init();
544 #endif
545 #ifdef CONFIG_TC
546         tc_init();
547 #endif
548
549         /* Networking initialization needs a process context */ 
550         sock_init();
551
552         start_context_thread();
553         do_initcalls();
554
555 #ifdef CONFIG_IRDA
556         irda_proto_init();
557         irda_device_init(); /* Must be done after protocol initialization */
558 #endif
559 #ifdef CONFIG_PCMCIA
560         init_pcmcia_ds();               /* Do this last */
561 #endif
562 }
563
564 extern void prepare_namespace(void);
565
566 static int init(void * unused)
567 {
568         lock_kernel();
569         do_basic_setup();
570
571         prepare_namespace();
572
573         /*
574          * Ok, we have completed the initial bootup, and
575          * we're essentially up and running. Get rid of the
576          * initmem segments and start the user-mode stuff..
577          */
578         free_initmem();
579         unlock_kernel();
580
581         if (open("/dev/console", O_RDWR, 0) < 0)
582                 printk("Warning: unable to open an initial console.\n");
583
584         (void) dup(0);
585         (void) dup(0);
586         
587         /*
588          * We try each of these until one succeeds.
589          *
590          * The Bourne shell can be used instead of init if we are 
591          * trying to recover a really broken machine.
592          */
593
594         if (execute_command)
595                 execve(execute_command,argv_init,envp_init);
596         execve("/sbin/init",argv_init,envp_init);
597         execve("/etc/init",argv_init,envp_init);
598         execve("/bin/init",argv_init,envp_init);
599         execve("/bin/sh",argv_init,envp_init);
600         panic("No init found.  Try passing init= option to kernel.");
601 }