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/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>
35 //#define REX_DBG_MAIN
37 #include <asm/machdep.h>
38 extern struct machdep_calls ppc_md;
41 #if defined(CONFIG_ARCH_S390)
42 #include <asm/s390mach.h>
43 #include <asm/ccwcache.h>
47 #include <linux/pci.h>
51 #include <linux/dio.h>
55 #include <linux/zorro.h>
59 # include <asm/mtrr.h>
63 #include <linux/nubus.h>
67 #include <linux/isapnp.h>
71 extern int irda_proto_init(void);
72 extern int irda_device_init(void);
75 #ifdef CONFIG_X86_LOCAL_APIC
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.
84 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
85 #error Sorry, your GCC is too old. It builds incorrect kernels.
88 extern char _stext, _etext;
89 extern char *linux_banner;
91 static int init(void *);
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);
104 extern void free_initmem(void);
107 extern void tc_init(void);
110 extern void ecard_init(void);
112 #if defined(CONFIG_SYSVIPC)
113 extern void ipc_init(void);
117 * Boot command-line arguments
119 #define MAX_INIT_ARGS 8
120 #define MAX_INIT_ENVS 8
122 extern void time_init(void);
123 extern void softirq_init(void);
127 char *execute_command;
129 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
130 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
132 static int __init profile_setup(char *str)
135 if (get_option(&str,&par)) prof_shift = par;
139 __setup("profile=", profile_setup);
141 static int __init checksetup(char *line)
143 struct kernel_param *p;
147 int n = strlen(p->str);
148 if (!strncmp(line,p->str,n)) {
149 if (p->setup_func(line+n))
153 } while (p < &__setup_end);
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);
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
166 void __init calibrate_delay(void)
168 unsigned long ticks, loopbit;
169 int lps_precision = LPS_PREC;
171 loops_per_jiffy = (1<<12);
173 printk("Calibrating delay loop... ");
174 while (loops_per_jiffy <<= 1) {
175 /* wait for "start of" clock tick */
177 while (ticks == jiffies)
181 __delay(loops_per_jiffy);
182 ticks = jiffies - ticks;
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;
194 while (ticks == jiffies);
196 __delay(loops_per_jiffy);
197 if (jiffies != ticks) /* longer than 1 tick */
198 loops_per_jiffy &= ~loopbit;
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);
207 static int __init debug_kernel(char *str)
211 console_loglevel = 10;
215 static int __init quiet_kernel(char *str)
219 console_loglevel = 4;
223 __setup("debug", debug_kernel);
224 __setup("quiet", quiet_kernel);
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 '='.
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.
235 static void __init parse_options(char *line)
243 envs = 1; /* TERM is set to 'linux' by default */
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
252 next = strchr(quote+1, '"');
254 quote = strchr(next+1, '"');
255 next = strchr(next+1, ' ');
260 if (!strncmp(line,"init=",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]
271 if (checksetup(line))
275 * Then check if it's an environment variable or
278 if (strchr(line,'=')) {
279 if (envs >= MAX_INIT_ENVS)
281 envp_init[++envs] = line;
283 if (args >= MAX_INIT_ARGS)
286 argv_init[++args] = line;
289 argv_init[args+1] = NULL;
290 envp_init[envs+1] = NULL;
294 extern void setup_arch(char **);
295 extern void cpu_idle(void);
297 unsigned long wait_init_idle;
301 #ifdef CONFIG_X86_LOCAL_APIC
302 static void __init smp_init(void)
304 APIC_init_uniprocessor();
307 #define smp_init() do { } while (0)
313 /* Called by boot processor to activate the rest. */
314 static void __init smp_init(void)
316 /* Get other processors into their bootup holding patterns. */
318 wait_init_idle = cpu_online_map;
319 clear_bit(current->processor, &wait_init_idle); /* Don't wait on me! */
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) {
330 printk("All processors have done init_idle\n");
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
342 static void rest_init(void)
344 kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);
346 current->need_resched = 1;
351 * Activate the first processor.
354 asmlinkage void __init start_kernel(void)
357 extern char saved_command_line[];
359 * Interrupts are still disabled. Do necessary setups, then
363 printk(linux_banner);
364 setup_arch(&command_line);
365 printk("Kernel command line: %s\n", saved_command_line);
366 parse_options(command_line);
371 if (ppc_md.progress) ppc_md.progress("main.c: finish sched_init()",0x100);
375 if (ppc_md.progress) ppc_md.progress("main.c: finish softirq_init()",0x101);
379 if (ppc_md.progress) ppc_md.progress("main.c: finish time_init()",0x102);
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.
389 if (ppc_md.progress) ppc_md.progress("main.c: finish console_init()",0x103);
392 #ifdef CONFIG_MODULES
397 /* only text is profiled */
398 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
399 prof_len >>= prof_shift;
401 size = prof_len * sizeof(unsigned int) + PAGE_SIZE-1;
402 prof_buffer = (unsigned int *) alloc_bootmem(size);
407 if (ppc_md.progress) ppc_md.progress("main.c: finish kmen_cache_init()",0x103);
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);
421 kmem_cache_sizes_init();
422 pgtable_cache_init();
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
431 if (num_mappedpages == 0)
432 num_mappedpages = num_physpages;
434 fork_init(num_mappedpages);
436 vfs_caches_init(num_physpages);
437 buffer_init(num_physpages);
438 page_cache_init(num_physpages);
439 #if defined(CONFIG_ARCH_S390)
443 #ifdef CONFIG_PROC_FS
446 #if defined(CONFIG_SYSVIPC)
450 printk("POSIX conformance testing by UNIFIX\n");
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).
461 struct task_struct *child_reaper = &init_task;
463 static void __init do_initcalls(void)
467 call = &__initcall_start;
471 } while (call < &__initcall_end);
473 /* Make sure there is no pending stuff from the initcall sequence */
474 flush_scheduled_tasks();
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.
482 * Now we can finally start doing some real work..
484 static void __init do_basic_setup(void)
488 * Tell the world that we're going to be the grim
489 * reaper of innocent orphaned children.
491 * We don't want people to have to make incorrect
492 * assumptions about where in the task array this
495 child_reaper = current;
497 #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
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..
511 * Ok, at this point all CPU's should be initialized, so
512 * we can start looking into devices..
514 #if defined(CONFIG_ARCH_S390)
515 s390_init_machine_check();
524 #if defined(CONFIG_PPC)
530 #ifdef CONFIG_ARCH_ACORN
549 /* Networking initialization needs a process context */
552 start_context_thread();
557 irda_device_init(); /* Must be done after protocol initialization */
560 init_pcmcia_ds(); /* Do this last */
564 extern void prepare_namespace(void);
566 static int init(void * unused)
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..
581 if (open("/dev/console", O_RDWR, 0) < 0)
582 printk("Warning: unable to open an initial console.\n");
588 * We try each of these until one succeeds.
590 * The Bourne shell can be used instead of init if we are
591 * trying to recover a really broken machine.
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.");