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/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>
40 #if defined(CONFIG_ARCH_S390)
41 #include <asm/s390mach.h>
44 #ifdef CONFIG_X86_LOCAL_APIC
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.
54 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
55 #error Sorry, your GCC is too old. It builds incorrect kernels.
58 extern char *linux_banner;
60 static int init(void *);
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);
78 extern void tc_init(void);
81 #if defined(CONFIG_SYSVIPC)
82 extern void ipc_init(void);
86 * Are we up and running (ie do we have all the infrastructure
89 int system_running = 0;
92 * Boot command-line arguments
94 #define MAX_INIT_ARGS 8
95 #define MAX_INIT_ENVS 8
97 extern void time_init(void);
98 extern void softirq_init(void);
102 char *execute_command;
104 /* Setup configured maximum number of CPUs to activate */
105 static unsigned int max_cpus = UINT_MAX;
108 * Setup routine for controlling SMP activation
110 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
111 * activation entirely (the MPS table probe still happens, though).
113 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
114 * greater than 0, limits the maximum number of CPUs activated in
117 static int __init nosmp(char *str)
123 __setup("nosmp", nosmp);
125 static int __init maxcpus(char *str)
127 get_option(&str, &max_cpus);
131 __setup("maxcpus=", maxcpus);
133 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
134 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
136 __setup("profile=", profile_setup);
138 static int __init checksetup(char *line)
140 struct kernel_param *p;
144 int n = strlen(p->str);
145 if (!strncmp(line,p->str,n)) {
146 if (p->setup_func(line+n))
150 } while (p < &__setup_end);
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);
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
163 void __init calibrate_delay(void)
165 unsigned long ticks, loopbit;
166 int lps_precision = LPS_PREC;
168 loops_per_jiffy = (1<<12);
170 printk("Calibrating delay loop... ");
171 while (loops_per_jiffy <<= 1) {
172 /* wait for "start of" clock tick */
174 while (ticks == jiffies)
178 __delay(loops_per_jiffy);
179 ticks = jiffies - ticks;
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;
191 while (ticks == jiffies);
193 __delay(loops_per_jiffy);
194 if (jiffies != ticks) /* longer than 1 tick */
195 loops_per_jiffy &= ~loopbit;
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);
204 static int __init debug_kernel(char *str)
208 console_loglevel = 10;
212 static int __init quiet_kernel(char *str)
216 console_loglevel = 4;
220 __setup("debug", debug_kernel);
221 __setup("quiet", quiet_kernel);
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 '='.
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.
232 static void __init parse_options(char *line)
240 envs = 1; /* TERM is set to 'linux' by default */
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
249 next = strchr(quote+1, '"');
251 quote = strchr(next+1, '"');
252 next = strchr(next+1, ' ');
257 if (!strncmp(line,"init=",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]
268 if (checksetup(line))
272 * Then check if it's an environment variable or
275 if (strchr(line,'=')) {
276 if (envs >= MAX_INIT_ENVS)
278 envp_init[++envs] = line;
280 if (args >= MAX_INIT_ARGS)
283 argv_init[++args] = line;
286 argv_init[args+1] = NULL;
287 envp_init[envs+1] = NULL;
291 extern void setup_arch(char **);
292 extern void cpu_idle(void);
296 #ifdef CONFIG_X86_LOCAL_APIC
297 static void __init smp_init(void)
299 APIC_init_uniprocessor();
302 #define smp_init() do { } while (0)
305 static inline void setup_per_cpu_areas(void) { }
306 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
310 #ifdef __GENERIC_PER_CPU
311 unsigned long __per_cpu_offset[NR_CPUS];
313 static void __init setup_per_cpu_areas(void)
315 unsigned long size, i;
317 /* Created by linker magic */
318 extern char __per_cpu_start[], __per_cpu_end[];
320 /* Copy section for each CPU (we discard the original) */
321 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
325 ptr = alloc_bootmem(size * NR_CPUS);
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);
332 #endif /* !__GENERIC_PER_CPU */
334 /* Called by boot processor to activate the rest. */
335 static void __init smp_init(void)
339 /* FIXME: This should be done in userspace --RR */
340 for (i = 0; i < NR_CPUS; i++) {
341 if (num_online_cpus() >= max_cpus)
343 if (cpu_possible(i) && !cpu_online(i)) {
344 printk("Bringing up %i\n", i);
349 /* Any cleanup work */
350 printk("CPUS done %u\n", max_cpus);
351 smp_cpus_done(max_cpus);
353 /* Get other processors into their bootup holding patterns. */
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
369 static void rest_init(void)
371 kernel_thread(init, NULL, CLONE_KERNEL);
377 * Activate the first processor.
380 asmlinkage void __init start_kernel(void)
383 extern char saved_command_line[];
385 * Interrupts are still disabled. Do necessary setups, then
389 printk(linux_banner);
390 setup_arch(&command_line);
391 setup_per_cpu_areas();
392 build_all_zonelists();
394 printk("Kernel command line: %s\n", saved_command_line);
395 parse_options(command_line);
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.
409 #ifdef CONFIG_MODULES
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);
426 kmem_cache_sizes_init();
428 pgtable_cache_init();
430 fork_init(num_physpages);
432 security_scaffolding_startup();
434 vfs_caches_init(num_physpages);
438 #ifdef CONFIG_PROC_FS
441 #if defined(CONFIG_SYSVIPC)
445 printk("POSIX conformance testing by UNIFIX\n");
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).
452 init_idle(current, smp_processor_id());
454 /* Do the rest non-__init'ed, we're now alive */
458 struct task_struct *child_reaper = &init_task;
460 static void __init do_initcalls(void)
464 call = &__initcall_start;
468 } while (call < &__initcall_end);
470 /* Make sure there is no pending stuff from the initcall sequence */
471 flush_scheduled_work();
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.
479 * Now we can finally start doing some real work..
481 static void __init do_basic_setup(void)
487 /* Networking initialization needs a process context */
494 static void do_pre_smp_initcalls(void)
496 extern int spawn_ksoftirqd(void);
498 extern int migration_init(void);
505 extern void prepare_namespace(void);
507 static int init(void * unused)
509 static char * argv_sh[] = { "sh", NULL, };
513 * Tell the world that we're going to be the grim
514 * reaper of innocent orphaned children.
516 * We don't want people to have to make incorrect
517 * assumptions about where in the task array this
520 child_reaper = current;
522 /* Sets up cpus_possible() */
523 smp_prepare_cpus(max_cpus);
525 do_pre_smp_initcalls();
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..
541 if (open("/dev/console", O_RDWR, 0) < 0)
542 printk("Warning: unable to open an initial console.\n");
548 * We try each of these until one succeeds.
550 * The Bourne shell can be used instead of init if we are
551 * trying to recover a really broken machine.
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.");