4efd7b84f9e6748ec143397bcd69421594220aaa
[linux-2.6.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/types.h>
16 #include <linux/module.h>
17 #include <linux/proc_fs.h>
18 #include <linux/devfs_fs_kernel.h>
19 #include <linux/kernel.h>
20 #include <linux/syscalls.h>
21 #include <linux/string.h>
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/utsname.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/smp_lock.h>
28 #include <linux/initrd.h>
29 #include <linux/hdreg.h>
30 #include <linux/bootmem.h>
31 #include <linux/tty.h>
32 #include <linux/gfp.h>
33 #include <linux/percpu.h>
34 #include <linux/kmod.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/security.h>
37 #include <linux/workqueue.h>
38 #include <linux/profile.h>
39 #include <linux/rcupdate.h>
40 #include <linux/moduleparam.h>
41 #include <linux/kallsyms.h>
42 #include <linux/writeback.h>
43 #include <linux/cpu.h>
44 #include <linux/efi.h>
45 #include <linux/unistd.h>
46 #include <linux/rmap.h>
47 #include <linux/mempolicy.h>
48 #include <linux/key.h>
49 #include <linux/ckrm_events.h>
50 #include <linux/ckrm_sched.h>
51
52 #include <asm/io.h>
53 #include <asm/bugs.h>
54 #include <asm/setup.h>
55
56 /*
57  * This is one of the first .c files built. Error out early
58  * if we have compiler trouble..
59  */
60 #if __GNUC__ == 2 && __GNUC_MINOR__ == 96
61 #ifdef CONFIG_FRAME_POINTER
62 #error This compiler cannot compile correctly with frame pointers enabled
63 #endif
64 #endif
65
66 #ifdef CONFIG_X86_LOCAL_APIC
67 #include <asm/smp.h>
68 #endif
69
70 /*
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.
75  */
76 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 95)
77 #error Sorry, your GCC is too old. It builds incorrect kernels.
78 #endif
79
80 extern char *linux_banner;
81
82 static int init(void *);
83
84 extern void init_IRQ(void);
85 extern void sock_init(void);
86 extern void fork_init(unsigned long);
87 extern void mca_init(void);
88 extern void sbus_init(void);
89 extern void sysctl_init(void);
90 extern void signals_init(void);
91 extern void buffer_init(void);
92 extern void pidhash_init(void);
93 extern void pidmap_init(void);
94 extern void prio_tree_init(void);
95 extern void radix_tree_init(void);
96 extern void free_initmem(void);
97 extern void populate_rootfs(void);
98 extern void driver_init(void);
99 extern void prepare_namespace(void);
100 #ifdef  CONFIG_ACPI
101 extern void acpi_early_init(void);
102 #else
103 static inline void acpi_early_init(void) { }
104 #endif
105
106 #ifdef CONFIG_TC
107 extern void tc_init(void);
108 #endif
109
110 enum system_states system_state;
111 EXPORT_SYMBOL(system_state);
112
113 /*
114  * The kernel_magic value represents the address of _end, which allows
115  * namelist tools to "match" each other respectively.  That way a tool
116  * that looks at /dev/mem can verify that it is using the right System.map
117  * file -- if kernel_magic doesn't equal the namelist value of _end,
118  * something's wrong.
119  */
120 extern unsigned long _end;
121 unsigned long *kernel_magic = &_end;
122
123 /*
124  * Boot command-line arguments
125  */
126 #define MAX_INIT_ARGS 32
127 #define MAX_INIT_ENVS 32
128
129 extern void time_init(void);
130 /* Default late time init is NULL. archs can override this later. */
131 void (*late_time_init)(void);
132 extern void softirq_init(void);
133
134 /* Untouched command line (eg. for /proc) saved by arch-specific code. */
135 char saved_command_line[COMMAND_LINE_SIZE];
136
137 static char *execute_command;
138
139 /* Setup configured maximum number of CPUs to activate */
140 static unsigned int max_cpus = NR_CPUS;
141
142 /*
143  * Setup routine for controlling SMP activation
144  *
145  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
146  * activation entirely (the MPS table probe still happens, though).
147  *
148  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
149  * greater than 0, limits the maximum number of CPUs activated in
150  * SMP mode to <NUM>.
151  */
152 static int __init nosmp(char *str)
153 {
154         max_cpus = 0;
155         return 1;
156 }
157
158 __setup("nosmp", nosmp);
159
160 static int __init maxcpus(char *str)
161 {
162         get_option(&str, &max_cpus);
163         return 1;
164 }
165
166 __setup("maxcpus=", maxcpus);
167
168 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
169 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
170 static const char *panic_later, *panic_param;
171
172 static int __init obsolete_checksetup(char *line)
173 {
174         struct obs_kernel_param *p;
175         extern struct obs_kernel_param __setup_start, __setup_end;
176
177         p = &__setup_start;
178         do {
179                 int n = strlen(p->str);
180                 if (!strncmp(line, p->str, n)) {
181                         if (p->early) {
182                                 /* Already done in parse_early_param?  (Needs
183                                  * exact match on param part) */
184                                 if (line[n] == '\0' || line[n] == '=')
185                                         return 1;
186                         } else if (!p->setup_func) {
187                                 printk(KERN_WARNING "Parameter %s is obsolete,"
188                                        " ignored\n", p->str);
189                                 return 1;
190                         } else if (p->setup_func(line + n))
191                                 return 1;
192                 }
193                 p++;
194         } while (p < &__setup_end);
195         return 0;
196 }
197
198 static unsigned long preset_lpj;
199 static int __init lpj_setup(char *str)
200 {
201         preset_lpj = simple_strtoul(str,NULL,0);
202         return 1;
203 }
204
205 __setup("lpj=", lpj_setup);
206
207 /*
208  * This should be approx 2 Bo*oMips to start (note initial shift), and will
209  * still work even if initially too large, it will just take slightly longer
210  */
211 unsigned long loops_per_jiffy = (1<<12);
212
213 EXPORT_SYMBOL(loops_per_jiffy);
214
215 /*
216  * This is the number of bits of precision for the loops_per_jiffy.  Each
217  * bit takes on average 1.5/HZ seconds.  This (like the original) is a little
218  * better than 1%
219  */
220 #define LPS_PREC 8
221
222 void __devinit calibrate_delay(void)
223 {
224         unsigned long ticks, loopbit;
225         int lps_precision = LPS_PREC;
226
227         if (preset_lpj) {
228                 loops_per_jiffy = preset_lpj;
229                 printk("Calibrating delay loop (skipped)... "
230                         "%lu.%02lu BogoMIPS preset\n",
231                         loops_per_jiffy/(500000/HZ),
232                         (loops_per_jiffy/(5000/HZ)) % 100);
233         } else {
234                 loops_per_jiffy = (1<<12);
235
236                 printk(KERN_DEBUG "Calibrating delay loop... ");
237                 while ((loops_per_jiffy <<= 1) != 0) {
238                         /* wait for "start of" clock tick */
239                         ticks = jiffies;
240                         while (ticks == jiffies)
241                                 /* nothing */;
242                         /* Go .. */
243                         ticks = jiffies;
244                         __delay(loops_per_jiffy);
245                         ticks = jiffies - ticks;
246                         if (ticks)
247                                 break;
248                 }
249
250                 /*
251                  * Do a binary approximation to get loops_per_jiffy set to
252                  * equal one clock (up to lps_precision bits)
253                  */
254                 loops_per_jiffy >>= 1;
255                 loopbit = loops_per_jiffy;
256                 while (lps_precision-- && (loopbit >>= 1)) {
257                         loops_per_jiffy |= loopbit;
258                         ticks = jiffies;
259                         while (ticks == jiffies)
260                                 /* nothing */;
261                         ticks = jiffies;
262                         __delay(loops_per_jiffy);
263                         if (jiffies != ticks)   /* longer than 1 tick */
264                                 loops_per_jiffy &= ~loopbit;
265                 }
266
267                 /* Round the value and print it */
268                 printk("%lu.%02lu BogoMIPS (lpj=%lu)\n",
269                         loops_per_jiffy/(500000/HZ),
270                         (loops_per_jiffy/(5000/HZ)) % 100,
271                         loops_per_jiffy);
272         }
273
274 }
275
276 static int __init debug_kernel(char *str)
277 {
278         if (*str)
279                 return 0;
280         console_loglevel = 10;
281         return 1;
282 }
283
284 static int __init quiet_kernel(char *str)
285 {
286         if (*str)
287                 return 0;
288         console_loglevel = 4;
289         return 1;
290 }
291
292 __setup("debug", debug_kernel);
293 __setup("quiet", quiet_kernel);
294
295 /*
296  * Unknown boot options get handed to init, unless they look like
297  * failed parameters
298  */
299 static int __init unknown_bootoption(char *param, char *val)
300 {
301         /* Change NUL term back to "=", to make "param" the whole string. */
302         if (val) {
303                 /* param=val or param="val"? */
304                 if (val == param+strlen(param)+1)
305                         val[-1] = '=';
306                 else if (val == param+strlen(param)+2) {
307                         val[-2] = '=';
308                         memmove(val-1, val, strlen(val)+1);
309                         val--;
310                 } else
311                         BUG();
312         }
313
314         /* Handle obsolete-style parameters */
315         if (obsolete_checksetup(param))
316                 return 0;
317
318         /*
319          * Preemptive maintenance for "why didn't my mispelled command
320          * line work?"
321          */
322         if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
323                 printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
324                 return 0;
325         }
326
327         if (panic_later)
328                 return 0;
329
330         if (val) {
331                 /* Environment option */
332                 unsigned int i;
333                 for (i = 0; envp_init[i]; i++) {
334                         if (i == MAX_INIT_ENVS) {
335                                 panic_later = "Too many boot env vars at `%s'";
336                                 panic_param = param;
337                         }
338                         if (!strncmp(param, envp_init[i], val - param))
339                                 break;
340                 }
341                 envp_init[i] = param;
342         } else {
343                 /* Command line option */
344                 unsigned int i;
345                 for (i = 0; argv_init[i]; i++) {
346                         if (i == MAX_INIT_ARGS) {
347                                 panic_later = "Too many boot init vars at `%s'";
348                                 panic_param = param;
349                         }
350                 }
351                 argv_init[i] = param;
352         }
353         return 0;
354 }
355
356 static int __init init_setup(char *str)
357 {
358         unsigned int i;
359
360         execute_command = str;
361         /*
362          * In case LILO is going to boot us with default command line,
363          * it prepends "auto" before the whole cmdline which makes
364          * the shell think it should execute a script with such name.
365          * So we ignore all arguments entered _before_ init=... [MJ]
366          */
367         for (i = 1; i < MAX_INIT_ARGS; i++)
368                 argv_init[i] = NULL;
369         return 1;
370 }
371 __setup("init=", init_setup);
372
373 extern void setup_arch(char **);
374 extern void cpu_idle(void);
375
376 #ifndef CONFIG_SMP
377
378 #ifdef CONFIG_X86_LOCAL_APIC
379 static void __init smp_init(void)
380 {
381         APIC_init_uniprocessor();
382 }
383 #else
384 #define smp_init()      do { } while (0)
385 #endif
386
387 static inline void setup_per_cpu_areas(void) { }
388 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
389
390 #else
391
392 #ifdef __GENERIC_PER_CPU
393 unsigned long __per_cpu_offset[NR_CPUS];
394
395 EXPORT_SYMBOL(__per_cpu_offset);
396
397 static void __init setup_per_cpu_areas(void)
398 {
399         unsigned long size, i;
400         char *ptr;
401         /* Created by linker magic */
402         extern char __per_cpu_start[], __per_cpu_end[];
403
404         /* Copy section for each CPU (we discard the original) */
405         size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
406 #ifdef CONFIG_MODULES
407         if (size < PERCPU_ENOUGH_ROOM)
408                 size = PERCPU_ENOUGH_ROOM;
409 #endif
410
411         ptr = alloc_bootmem(size * NR_CPUS);
412
413         for (i = 0; i < NR_CPUS; i++, ptr += size) {
414                 __per_cpu_offset[i] = ptr - __per_cpu_start;
415                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
416         }
417 }
418 #endif /* !__GENERIC_PER_CPU */
419
420 /* Called by boot processor to activate the rest. */
421 static void __init smp_init(void)
422 {
423         unsigned int i;
424
425         /* FIXME: This should be done in userspace --RR */
426         for_each_present_cpu(i) {
427                 if (num_online_cpus() >= max_cpus)
428                         break;
429                 if (!cpu_online(i))
430                         cpu_up(i);
431         }
432
433         /* Any cleanup work */
434         printk("Brought up %ld CPUs\n", (long)num_online_cpus());
435         smp_cpus_done(max_cpus);
436 #if 0
437         /* Get other processors into their bootup holding patterns. */
438
439         smp_threads_ready=1;
440         smp_commence();
441 #endif
442 }
443
444 #endif
445
446 /*
447  * We need to finalize in a non-__init function or else race conditions
448  * between the root thread and the init thread may cause start_kernel to
449  * be reaped by free_initmem before the root thread has proceeded to
450  * cpu_idle.
451  *
452  * gcc-3.4 accidentally inlines this function, so use noinline.
453  */
454
455 static void noinline rest_init(void)
456         __releases(kernel_lock)
457 {
458         kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
459         numa_default_policy();
460         unlock_kernel();
461         cpu_idle();
462
463
464 /* Check for early params. */
465 static int __init do_early_param(char *param, char *val)
466 {
467         struct obs_kernel_param *p;
468         extern struct obs_kernel_param __setup_start, __setup_end;
469
470         for (p = &__setup_start; p < &__setup_end; p++) {
471                 if (p->early && strcmp(param, p->str) == 0) {
472                         if (p->setup_func(val) != 0)
473                                 printk(KERN_WARNING
474                                        "Malformed early option '%s'\n", param);
475                 }
476         }
477         /* We accept everything at this stage. */
478         return 0;
479 }
480
481 /* Arch code calls this early on, or if not, just before other parsing. */
482 void __init parse_early_param(void)
483 {
484         static __initdata int done = 0;
485         static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
486
487         if (done)
488                 return;
489
490         /* All fall through to do_early_param. */
491         strlcpy(tmp_cmdline, saved_command_line, COMMAND_LINE_SIZE);
492         parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
493         done = 1;
494 }
495
496 /*
497  *      Activate the first processor.
498  */
499
500 asmlinkage void __init start_kernel(void)
501 {
502         char * command_line;
503         extern struct kernel_param __start___param[], __stop___param[];
504 /*
505  * Interrupts are still disabled. Do necessary setups, then
506  * enable them
507  */
508         lock_kernel();
509         page_address_init();
510         printk(linux_banner);
511         setup_arch(&command_line);
512         setup_per_cpu_areas();
513
514         /*
515          * Mark the boot cpu "online" so that it can call console drivers in
516          * printk() and can access its per-cpu storage.
517          */
518         smp_prepare_boot_cpu();
519
520         /*
521          * Set up the scheduler prior starting any interrupts (such as the
522          * timer interrupt). Full topology setup happens at smp_init()
523          * time - but meanwhile we still have a functioning scheduler.
524          */
525         sched_init();
526         build_all_zonelists();
527         page_alloc_init();
528         printk("Kernel command line: %s\n", saved_command_line);
529         parse_early_param();
530         parse_args("Booting kernel", command_line, __start___param,
531                    __stop___param - __start___param,
532                    &unknown_bootoption);
533         sort_main_extable();
534         trap_init();
535         rcu_init();
536         init_IRQ();
537         pidhash_init();
538         ckrm_init();
539         init_timers();
540         softirq_init();
541         time_init();
542
543         /*
544          * HACK ALERT! This is early. We're enabling the console before
545          * we've done PCI setups etc, and console_init() must be aware of
546          * this. But we do want output early, in case something goes wrong.
547          */
548         console_init();
549         if (panic_later)
550                 panic(panic_later, panic_param);
551         profile_init();
552         local_irq_enable();
553 #ifdef CONFIG_BLK_DEV_INITRD
554         if (initrd_start && !initrd_below_start_ok &&
555                         initrd_start < min_low_pfn << PAGE_SHIFT) {
556                 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
557                     "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
558                 initrd_start = 0;
559         }
560 #endif
561         vfs_caches_init_early();
562         mem_init();
563         kmem_cache_init();
564         numa_policy_init();
565         if (late_time_init)
566                 late_time_init();
567         calibrate_delay();
568         pidmap_init();
569         pgtable_cache_init();
570         prio_tree_init();
571         anon_vma_init();
572 #ifdef CONFIG_X86
573         if (efi_enabled)
574                 efi_enter_virtual_mode();
575 #endif
576         fork_init(num_physpages);
577         proc_caches_init();
578         buffer_init();
579         unnamed_dev_init();
580         security_init();
581         vfs_caches_init(num_physpages);
582         radix_tree_init();
583         signals_init();
584         /* rootfs populating might need page-writeback */
585         page_writeback_init();
586 #ifdef CONFIG_PROC_FS
587         proc_root_init();
588 #endif
589         check_bugs();
590
591         acpi_early_init(); /* before LAPIC and SMP init */
592
593         /* Do the rest non-__init'ed, we're now alive */
594         rest_init();
595 }
596
597 static int __initdata initcall_debug;
598
599 static int __init initcall_debug_setup(char *str)
600 {
601         initcall_debug = 1;
602         return 1;
603 }
604 __setup("initcall_debug", initcall_debug_setup);
605
606 struct task_struct *child_reaper = &init_task;
607
608 extern initcall_t __initcall_start, __initcall_end;
609
610 static void __init do_initcalls(void)
611 {
612         initcall_t *call;
613         int count = preempt_count();
614
615         for (call = &__initcall_start; call < &__initcall_end; call++) {
616                 char *msg;
617
618                 if (initcall_debug) {
619                         printk(KERN_DEBUG "Calling initcall 0x%p", *call);
620                         print_fn_descriptor_symbol(": %s()", (unsigned long) *call);
621                         printk("\n");
622                 }
623
624                 (*call)();
625
626                 msg = NULL;
627                 if (preempt_count() != count) {
628                         msg = "preemption imbalance";
629                         preempt_count() = count;
630                 }
631                 if (irqs_disabled()) {
632                         msg = "disabled interrupts";
633                         local_irq_enable();
634                 }
635                 if (msg) {
636                         printk("error in initcall at 0x%p: "
637                                 "returned with %s\n", *call, msg);
638                 }
639         }
640
641         /* Make sure there is no pending stuff from the initcall sequence */
642         flush_scheduled_work();
643 }
644
645 /*
646  * Ok, the machine is now initialized. None of the devices
647  * have been touched yet, but the CPU subsystem is up and
648  * running, and memory and process management works.
649  *
650  * Now we can finally start doing some real work..
651  */
652 static void __init do_basic_setup(void)
653 {
654         /* drivers will send hotplug events */
655         init_workqueues();
656         usermodehelper_init();
657         key_init();
658         driver_init();
659
660 #ifdef CONFIG_SYSCTL
661         sysctl_init();
662 #endif
663
664         /* Networking initialization needs a process context */ 
665         sock_init();
666
667         do_initcalls();
668 }
669
670 static void do_pre_smp_initcalls(void)
671 {
672         extern int spawn_ksoftirqd(void);
673 #ifdef CONFIG_SMP
674         extern int migration_init(void);
675
676         migration_init();
677 #endif
678         spawn_ksoftirqd();
679 }
680
681 static void run_init_process(char *init_filename)
682 {
683         argv_init[0] = init_filename;
684         execve(init_filename, argv_init, envp_init);
685 }
686
687 static inline void fixup_cpu_present_map(void)
688 {
689 #ifdef CONFIG_SMP
690         int i;
691
692         /*
693          * If arch is not hotplug ready and did not populate
694          * cpu_present_map, just make cpu_present_map same as cpu_possible_map
695          * for other cpu bringup code to function as normal. e.g smp_init() etc.
696          */
697         if (cpus_empty(cpu_present_map)) {
698                 for_each_cpu(i) {
699                         cpu_set(i, cpu_present_map);
700                 }
701         }
702 #endif
703 }
704
705 static int init(void * unused)
706 {
707         lock_kernel();
708         /*
709          * Tell the world that we're going to be the grim
710          * reaper of innocent orphaned children.
711          *
712          * We don't want people to have to make incorrect
713          * assumptions about where in the task array this
714          * can be found.
715          */
716         child_reaper = current;
717
718         /* Sets up cpus_possible() */
719         smp_prepare_cpus(max_cpus);
720
721         do_pre_smp_initcalls();
722
723         fixup_cpu_present_map();
724         smp_init();
725
726         /*
727          * Do this before initcalls, because some drivers want to access
728          * firmware files.
729          */
730         populate_rootfs();
731         do_basic_setup();
732
733         init_ckrm_sched_res();
734
735         sched_init_smp();
736
737         /*
738          * check if there is an early userspace init.  If yes, let it do all
739          * the work
740          */
741         if (sys_access((const char __user *) "/init", 0) == 0)
742                 execute_command = "/init";
743         else
744                 prepare_namespace();
745
746         /*
747          * Ok, we have completed the initial bootup, and
748          * we're essentially up and running. Get rid of the
749          * initmem segments and start the user-mode stuff..
750          */
751         free_initmem();
752         unlock_kernel();
753         system_state = SYSTEM_RUNNING;
754         numa_default_policy();
755
756         if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
757                 printk("Warning: unable to open an initial console.\n");
758
759         (void) sys_dup(0);
760         (void) sys_dup(0);
761         
762         /*
763          * We try each of these until one succeeds.
764          *
765          * The Bourne shell can be used instead of init if we are 
766          * trying to recover a really broken machine.
767          */
768
769         if (execute_command)
770                 run_init_process(execute_command);
771
772         run_init_process("/sbin/init");
773         run_init_process("/etc/init");
774         run_init_process("/bin/init");
775         run_init_process("/bin/sh");
776
777         panic("No init found.  Try passing init= option to kernel.");
778 }
779
780 static int early_param_test(char *rest)
781 {
782         printk("early_parm_test: %s\n", rest ?: "(null)");
783         return rest ? 0 : -EINVAL;
784 }
785 early_param("testsetup", early_param_test);
786 static int early_setup_test(char *rest)
787 {
788         printk("early_setup_test: %s\n", rest ?: "(null)");
789         return 0;
790 }
791 __setup("testsetup_long", early_setup_test);