Minimal initramfs support (based on Al Viro's work).
[linux-flexiantxendom0-3.2.10.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/bootmem.h>
28 #include <linux/tty.h>
29 #include <linux/gfp.h>
30 #include <linux/percpu.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/security.h>
33 #include <linux/workqueue.h>
34 #include <linux/profile.h>
35 #include <linux/rcupdate.h>
36
37 #include <asm/io.h>
38 #include <asm/bugs.h>
39
40 #if defined(CONFIG_ARCH_S390)
41 #include <asm/s390mach.h>
42 #endif
43
44 #ifdef CONFIG_X86_LOCAL_APIC
45 #include <asm/smp.h>
46 #endif
47
48 /*
49  * Versions of gcc older than that listed below may actually compile
50  * and link okay, but the end product can have subtle run time bugs.
51  * To avoid associated bogus bug reports, we flatly refuse to compile
52  * with a gcc that is known to be too old from the very beginning.
53  */
54 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
55 #error Sorry, your GCC is too old. It builds incorrect kernels.
56 #endif
57
58 extern char *linux_banner;
59
60 static int init(void *);
61
62 extern void init_IRQ(void);
63 extern void init_modules(void);
64 extern void sock_init(void);
65 extern void fork_init(unsigned long);
66 extern void mca_init(void);
67 extern void sbus_init(void);
68 extern void sysctl_init(void);
69 extern void signals_init(void);
70 extern void buffer_init(void);
71 extern void pidhash_init(void);
72 extern void pte_chain_init(void);
73 extern void radix_tree_init(void);
74 extern void free_initmem(void);
75 extern void populate_rootfs(void);
76
77 #ifdef CONFIG_TC
78 extern void tc_init(void);
79 #endif
80
81 #if defined(CONFIG_SYSVIPC)
82 extern void ipc_init(void);
83 #endif
84
85 /*
86  * Are we up and running (ie do we have all the infrastructure
87  * set up)
88  */
89 int system_running = 0;
90
91 /*
92  * Boot command-line arguments
93  */
94 #define MAX_INIT_ARGS 8
95 #define MAX_INIT_ENVS 8
96
97 extern void time_init(void);
98 extern void softirq_init(void);
99
100 int rows, cols;
101
102 char *execute_command;
103
104 /* Setup configured maximum number of CPUs to activate */
105 static unsigned int max_cpus = UINT_MAX;
106
107 /*
108  * Setup routine for controlling SMP activation
109  *
110  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
111  * activation entirely (the MPS table probe still happens, though).
112  *
113  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
114  * greater than 0, limits the maximum number of CPUs activated in
115  * SMP mode to <NUM>.
116  */
117 static int __init nosmp(char *str)
118 {
119         max_cpus = 0;
120         return 1;
121 }
122
123 __setup("nosmp", nosmp);
124
125 static int __init maxcpus(char *str)
126 {
127         get_option(&str, &max_cpus);
128         return 1;
129 }
130
131 __setup("maxcpus=", maxcpus);
132
133 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
134 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
135
136 __setup("profile=", profile_setup);
137
138 static int __init checksetup(char *line)
139 {
140         struct kernel_param *p;
141
142         p = &__setup_start;
143         do {
144                 int n = strlen(p->str);
145                 if (!strncmp(line,p->str,n)) {
146                         if (p->setup_func(line+n))
147                                 return 1;
148                 }
149                 p++;
150         } while (p < &__setup_end);
151         return 0;
152 }
153
154 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
155    still work even if initially too large, it will just take slightly longer */
156 unsigned long loops_per_jiffy = (1<<12);
157
158 /* This is the number of bits of precision for the loops_per_jiffy.  Each
159    bit takes on average 1.5/HZ seconds.  This (like the original) is a little
160    better than 1% */
161 #define LPS_PREC 8
162
163 void __init calibrate_delay(void)
164 {
165         unsigned long ticks, loopbit;
166         int lps_precision = LPS_PREC;
167
168         loops_per_jiffy = (1<<12);
169
170         printk("Calibrating delay loop... ");
171         while (loops_per_jiffy <<= 1) {
172                 /* wait for "start of" clock tick */
173                 ticks = jiffies;
174                 while (ticks == jiffies)
175                         /* nothing */;
176                 /* Go .. */
177                 ticks = jiffies;
178                 __delay(loops_per_jiffy);
179                 ticks = jiffies - ticks;
180                 if (ticks)
181                         break;
182         }
183
184 /* Do a binary approximation to get loops_per_jiffy set to equal one clock
185    (up to lps_precision bits) */
186         loops_per_jiffy >>= 1;
187         loopbit = loops_per_jiffy;
188         while ( lps_precision-- && (loopbit >>= 1) ) {
189                 loops_per_jiffy |= loopbit;
190                 ticks = jiffies;
191                 while (ticks == jiffies);
192                 ticks = jiffies;
193                 __delay(loops_per_jiffy);
194                 if (jiffies != ticks)   /* longer than 1 tick */
195                         loops_per_jiffy &= ~loopbit;
196         }
197
198 /* Round the value and print it */      
199         printk("%lu.%02lu BogoMIPS\n",
200                 loops_per_jiffy/(500000/HZ),
201                 (loops_per_jiffy/(5000/HZ)) % 100);
202 }
203
204 static int __init debug_kernel(char *str)
205 {
206         if (*str)
207                 return 0;
208         console_loglevel = 10;
209         return 1;
210 }
211
212 static int __init quiet_kernel(char *str)
213 {
214         if (*str)
215                 return 0;
216         console_loglevel = 4;
217         return 1;
218 }
219
220 __setup("debug", debug_kernel);
221 __setup("quiet", quiet_kernel);
222
223 /*
224  * This is a simple kernel command line parsing function: it parses
225  * the command line, and fills in the arguments/environment to init
226  * as appropriate. Any cmd-line option is taken to be an environment
227  * variable if it contains the character '='.
228  *
229  * This routine also checks for options meant for the kernel.
230  * These options are not given to init - they are for internal kernel use only.
231  */
232 static void __init parse_options(char *line)
233 {
234         char *next,*quote;
235         int args, envs;
236
237         if (!*line)
238                 return;
239         args = 0;
240         envs = 1;       /* TERM is set to 'linux' by default */
241         next = line;
242         while ((line = next) != NULL) {
243                 quote = strchr(line,'"');
244                 next = strchr(line, ' ');
245                 while (next != NULL && quote != NULL && quote < next) {
246                         /* we found a left quote before the next blank
247                          * now we have to find the matching right quote
248                          */
249                         next = strchr(quote+1, '"');
250                         if (next != NULL) {
251                                 quote = strchr(next+1, '"');
252                                 next = strchr(next+1, ' ');
253                         }
254                 }
255                 if (next != NULL)
256                         *next++ = 0;
257                 if (!strncmp(line,"init=",5)) {
258                         line += 5;
259                         execute_command = line;
260                         /* In case LILO is going to boot us with default command line,
261                          * it prepends "auto" before the whole cmdline which makes
262                          * the shell think it should execute a script with such name.
263                          * So we ignore all arguments entered _before_ init=... [MJ]
264                          */
265                         args = 0;
266                         continue;
267                 }
268                 if (checksetup(line))
269                         continue;
270                 
271                 /*
272                  * Then check if it's an environment variable or
273                  * an option.
274                  */
275                 if (strchr(line,'=')) {
276                         if (envs >= MAX_INIT_ENVS)
277                                 break;
278                         envp_init[++envs] = line;
279                 } else {
280                         if (args >= MAX_INIT_ARGS)
281                                 break;
282                         if (*line)
283                                 argv_init[++args] = line;
284                 }
285         }
286         argv_init[args+1] = NULL;
287         envp_init[envs+1] = NULL;
288 }
289
290
291 extern void setup_arch(char **);
292 extern void cpu_idle(void);
293
294 #ifndef CONFIG_SMP
295
296 #ifdef CONFIG_X86_LOCAL_APIC
297 static void __init smp_init(void)
298 {
299         APIC_init_uniprocessor();
300 }
301 #else
302 #define smp_init()      do { } while (0)
303 #endif
304
305 static inline void setup_per_cpu_areas(void) { }
306 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
307
308 #else
309
310 #ifdef __GENERIC_PER_CPU
311 unsigned long __per_cpu_offset[NR_CPUS];
312
313 static void __init setup_per_cpu_areas(void)
314 {
315         unsigned long size, i;
316         char *ptr;
317         /* Created by linker magic */
318         extern char __per_cpu_start[], __per_cpu_end[];
319
320         /* Copy section for each CPU (we discard the original) */
321         size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
322         if (!size)
323                 return;
324
325         ptr = alloc_bootmem(size * NR_CPUS);
326
327         for (i = 0; i < NR_CPUS; i++, ptr += size) {
328                 __per_cpu_offset[i] = ptr - __per_cpu_start;
329                 memcpy(ptr, __per_cpu_start, size);
330         }
331 }
332 #endif /* !__GENERIC_PER_CPU */
333
334 /* Called by boot processor to activate the rest. */
335 static void __init smp_init(void)
336 {
337         unsigned int i;
338
339         /* FIXME: This should be done in userspace --RR */
340         for (i = 0; i < NR_CPUS; i++) {
341                 if (num_online_cpus() >= max_cpus)
342                         break;
343                 if (cpu_possible(i) && !cpu_online(i)) {
344                         printk("Bringing up %i\n", i);
345                         cpu_up(i);
346                 }
347         }
348
349         /* Any cleanup work */
350         printk("CPUS done %u\n", max_cpus);
351         smp_cpus_done(max_cpus);
352 #if 0
353         /* Get other processors into their bootup holding patterns. */
354
355         smp_threads_ready=1;
356         smp_commence();
357 #endif
358 }
359
360 #endif
361
362 /*
363  * We need to finalize in a non-__init function or else race conditions
364  * between the root thread and the init thread may cause start_kernel to
365  * be reaped by free_initmem before the root thread has proceeded to
366  * cpu_idle.
367  */
368
369 static void rest_init(void)
370 {
371         kernel_thread(init, NULL, CLONE_KERNEL);
372         unlock_kernel();
373         cpu_idle();
374
375
376 /*
377  *      Activate the first processor.
378  */
379
380 asmlinkage void __init start_kernel(void)
381 {
382         char * command_line;
383         extern char saved_command_line[];
384 /*
385  * Interrupts are still disabled. Do necessary setups, then
386  * enable them
387  */
388         lock_kernel();
389         printk(linux_banner);
390         setup_arch(&command_line);
391         setup_per_cpu_areas();
392         build_all_zonelists();
393         page_alloc_init();
394         printk("Kernel command line: %s\n", saved_command_line);
395         parse_options(command_line);
396         trap_init();
397         rcu_init();
398         init_IRQ();
399         sched_init();
400         softirq_init();
401         time_init();
402
403         /*
404          * HACK ALERT! This is early. We're enabling the console before
405          * we've done PCI setups etc, and console_init() must be aware of
406          * this. But we do want output early, in case something goes wrong.
407          */
408         console_init();
409 #ifdef CONFIG_MODULES
410         init_modules();
411 #endif
412         profile_init();
413         kmem_cache_init();
414         local_irq_enable();
415         calibrate_delay();
416 #ifdef CONFIG_BLK_DEV_INITRD
417         if (initrd_start && !initrd_below_start_ok &&
418                         initrd_start < min_low_pfn << PAGE_SHIFT) {
419                 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
420                     "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
421                 initrd_start = 0;
422         }
423 #endif
424         page_address_init();
425         mem_init();
426         kmem_cache_sizes_init();
427         pidhash_init();
428         pgtable_cache_init();
429         pte_chain_init();
430         fork_init(num_physpages);
431         proc_caches_init();
432         security_scaffolding_startup();
433         buffer_init();
434         vfs_caches_init(num_physpages);
435         radix_tree_init();
436         signals_init();
437         populate_rootfs();
438 #ifdef CONFIG_PROC_FS
439         proc_root_init();
440 #endif
441 #if defined(CONFIG_SYSVIPC)
442         ipc_init();
443 #endif
444         check_bugs();
445         printk("POSIX conformance testing by UNIFIX\n");
446
447         /* 
448          *      We count on the initial thread going ok 
449          *      Like idlers init is an unlocked kernel thread, which will
450          *      make syscalls (and thus be locked).
451          */
452         init_idle(current, smp_processor_id());
453
454         /* Do the rest non-__init'ed, we're now alive */
455         rest_init();
456 }
457
458 struct task_struct *child_reaper = &init_task;
459
460 static void __init do_initcalls(void)
461 {
462         initcall_t *call;
463
464         call = &__initcall_start;
465         do {
466                 (*call)();
467                 call++;
468         } while (call < &__initcall_end);
469
470         /* Make sure there is no pending stuff from the initcall sequence */
471         flush_scheduled_work();
472 }
473
474 /*
475  * Ok, the machine is now initialized. None of the devices
476  * have been touched yet, but the CPU subsystem is up and
477  * running, and memory and process management works.
478  *
479  * Now we can finally start doing some real work..
480  */
481 static void __init do_basic_setup(void)
482 {
483 #ifdef CONFIG_SYSCTL
484         sysctl_init();
485 #endif
486
487         /* Networking initialization needs a process context */ 
488         sock_init();
489
490         init_workqueues();
491         do_initcalls();
492 }
493
494 static void do_pre_smp_initcalls(void)
495 {
496         extern int spawn_ksoftirqd(void);
497 #ifdef CONFIG_SMP
498         extern int migration_init(void);
499
500         migration_init();
501 #endif
502         spawn_ksoftirqd();
503 }
504
505 extern void prepare_namespace(void);
506
507 static int init(void * unused)
508 {
509         static char * argv_sh[] = { "sh", NULL, };
510
511         lock_kernel();
512         /*
513          * Tell the world that we're going to be the grim
514          * reaper of innocent orphaned children.
515          *
516          * We don't want people to have to make incorrect
517          * assumptions about where in the task array this
518          * can be found.
519          */
520         child_reaper = current;
521
522         /* Sets up cpus_possible() */
523         smp_prepare_cpus(max_cpus);
524
525         do_pre_smp_initcalls();
526
527         smp_init();
528         do_basic_setup();
529
530         prepare_namespace();
531
532         /*
533          * Ok, we have completed the initial bootup, and
534          * we're essentially up and running. Get rid of the
535          * initmem segments and start the user-mode stuff..
536          */
537         free_initmem();
538         unlock_kernel();
539         system_running = 1;
540
541         if (open("/dev/console", O_RDWR, 0) < 0)
542                 printk("Warning: unable to open an initial console.\n");
543
544         (void) dup(0);
545         (void) dup(0);
546         
547         /*
548          * We try each of these until one succeeds.
549          *
550          * The Bourne shell can be used instead of init if we are 
551          * trying to recover a really broken machine.
552          */
553
554         if (execute_command)
555                 execve(execute_command,argv_init,envp_init);
556         execve("/sbin/init",argv_init,envp_init);
557         execve("/etc/init",argv_init,envp_init);
558         execve("/bin/init",argv_init,envp_init);
559         execve("/bin/sh",argv_sh,envp_init);
560         panic("No init found.  Try passing init= option to kernel.");
561 }