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/unistd.h>
17 #include <linux/string.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20 #include <linux/utsname.h>
21 #include <linux/ioport.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/blk.h>
25 #include <linux/hdreg.h>
26 #include <linux/iobuf.h>
32 #include <linux/pci.h>
36 #include <linux/dio.h>
40 #include <linux/zorro.h>
44 # include <asm/mtrr.h>
48 #include <linux/apm_bios.h>
52 extern void nubus_init(void);
56 * Versions of gcc older than that listed below may actually compile
57 * and link okay, but the end product can have subtle run time bugs.
58 * To avoid associated bogus bug reports, we flatly refuse to compile
59 * with a gcc that is known to be too old from the very beginning.
61 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
62 #error sorry, your GCC is too old. It builds incorrect kernels.
65 extern char _stext
, _etext
;
66 extern char *linux_banner
;
68 extern int console_loglevel
;
70 static int init(void *);
72 extern void init_IRQ(void);
73 extern void init_modules(void);
74 extern long console_init(long, long);
75 extern void sock_init(void);
76 extern void fork_init(unsigned long);
77 extern void mca_init(void);
78 extern void sbus_init(void);
79 extern void ppc_init(void);
80 extern void sysctl_init(void);
81 extern void filescache_init(void);
82 extern void signals_init(void);
84 extern void free_initmem(void);
85 extern void filesystem_setup(void);
87 #ifdef CONFIG_ARCH_ACORN
88 extern void ecard_init(void);
91 #if defined(CONFIG_SYSVIPC)
92 extern void ipc_init(void);
94 #if defined(CONFIG_QUOTA)
95 extern void dquot_init_hash(void);
99 * Boot command-line arguments
101 #define MAX_INIT_ARGS 8
102 #define MAX_INIT_ENVS 8
104 extern void time_init(void);
106 static unsigned long memory_start
= 0;
107 static unsigned long memory_end
= 0;
111 #ifdef CONFIG_BLK_DEV_INITRD
112 kdev_t real_root_dev
;
115 int root_mountflags
= MS_RDONLY
;
116 char *execute_command
= NULL
;
118 static char * argv_init
[MAX_INIT_ARGS
+2] = { "init", NULL
, };
119 static char * envp_init
[MAX_INIT_ENVS
+2] = { "HOME=/", "TERM=linux", NULL
, };
122 * Read an int from an option string; if available accept a subsequent
126 * 0 : no int in string
127 * 1 : int found, no subsequent comma
128 * 2 : int found including a subsequent comma
130 int get_option(char **str
, int *pint
)
134 if (!cur
|| !(*cur
)) return 0;
135 *pint
= simple_strtol(cur
,str
,0);
136 if (cur
==*str
) return 0;
145 char *get_options(char *str
, int nints
, int *ints
)
150 res
= get_option(&str
, ints
+i
);
159 static int __init
profile_setup(char *str
)
162 if (get_option(&str
,&par
)) prof_shift
= par
;
166 __setup("profile=", profile_setup
);
169 static struct dev_name_struct
{
172 } root_dev_names
[] __initdata
= {
222 { "cdu535", 0x1800 },
223 { "sonycd", 0x1800 },
225 { "cm206cd", 0x2000 },
236 { "apblock", APBLOCK_MAJOR
<< 8},
237 { "ddv", DDV_MAJOR
<< 8},
241 kdev_t __init
name_to_kdev_t(char *line
)
244 if (strncmp(line
,"/dev/",5) == 0) {
245 struct dev_name_struct
*dev
= root_dev_names
;
248 int len
= strlen(dev
->name
);
249 if (strncmp(line
,dev
->name
,len
) == 0) {
257 return to_kdev_t(base
+ simple_strtoul(line
,NULL
,base
?10:16));
260 static int __init
root_dev_setup(char *line
)
262 ROOT_DEV
= name_to_kdev_t(line
);
266 __setup("root=", root_dev_setup
);
268 static int __init
checksetup(char *line
)
270 struct kernel_param
*p
;
274 int n
= strlen(p
->str
);
275 if (!strncmp(line
,p
->str
,n
)) {
276 if (p
->setup_func(line
+n
))
280 } while (p
< &__setup_end
);
284 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
285 still work even if initially too large, it will just take slightly longer */
286 unsigned long loops_per_sec
= (1<<12);
288 /* This is the number of bits of precision for the loops_per_second. Each
289 bit takes on average 1.5/HZ seconds. This (like the original) is a little
293 void __init
calibrate_delay(void)
295 unsigned long ticks
, loopbit
;
296 int lps_precision
= LPS_PREC
;
298 loops_per_sec
= (1<<12);
300 printk("Calibrating delay loop... ");
301 while (loops_per_sec
<<= 1) {
302 /* wait for "start of" clock tick */
304 while (ticks
== jiffies
)
308 __delay(loops_per_sec
);
309 ticks
= jiffies
- ticks
;
314 /* Do a binary approximation to get loops_per_second set to equal one clock
315 (up to lps_precision bits) */
317 loopbit
= loops_per_sec
;
318 while ( lps_precision
-- && (loopbit
>>= 1) ) {
319 loops_per_sec
|= loopbit
;
321 while (ticks
== jiffies
);
323 __delay(loops_per_sec
);
324 if (jiffies
!= ticks
) /* longer than 1 tick */
325 loops_per_sec
&= ~loopbit
;
328 /* finally, adjust loops per second in terms of seconds instead of clocks */
330 /* Round the value and print it */
331 printk("%lu.%02lu BogoMIPS\n",
332 (loops_per_sec
+2500)/500000,
333 ((loops_per_sec
+2500)/5000) % 100);
336 static int __init
readonly(char *str
)
340 root_mountflags
|= MS_RDONLY
;
344 static int __init
readwrite(char *str
)
348 root_mountflags
&= ~MS_RDONLY
;
352 static int __init
debug_kernel(char *str
)
356 console_loglevel
= 10;
360 __setup("ro", readonly
);
361 __setup("rw", readwrite
);
362 __setup("debug", debug_kernel
);
365 * This is a simple kernel command line parsing function: it parses
366 * the command line, and fills in the arguments/environment to init
367 * as appropriate. Any cmd-line option is taken to be an environment
368 * variable if it contains the character '='.
370 * This routine also checks for options meant for the kernel.
371 * These options are not given to init - they are for internal kernel use only.
373 static void __init
parse_options(char *line
)
381 envs
= 1; /* TERM is set to 'linux' by default */
383 while ((line
= next
) != NULL
) {
384 if ((next
= strchr(line
,' ')) != NULL
)
386 if (!strncmp(line
,"init=",5)) {
388 execute_command
= line
;
389 /* In case LILO is going to boot us with default command line,
390 * it prepends "auto" before the whole cmdline which makes
391 * the shell think it should execute a script with such name.
392 * So we ignore all arguments entered _before_ init=... [MJ]
397 if (checksetup(line
))
401 * Then check if it's an environment variable or
404 if (strchr(line
,'=')) {
405 if (envs
>= MAX_INIT_ENVS
)
407 envp_init
[++envs
] = line
;
409 if (args
>= MAX_INIT_ARGS
)
411 argv_init
[++args
] = line
;
414 argv_init
[args
+1] = NULL
;
415 envp_init
[envs
+1] = NULL
;
419 extern void setup_arch(char **, unsigned long *, unsigned long *);
420 extern void cpu_idle(void);
424 #define smp_init() do { } while (0)
428 /* Called by boot processor to activate the rest. */
429 static void __init
smp_init(void)
431 /* Get other processors into their bootup holding patterns. */
439 extern void initialize_secondary(void);
442 * Activate the first processor.
445 asmlinkage
void __init
start_kernel(void)
450 static int boot_cpu
= 1;
451 /* "current" has been set up, we need to load it now */
453 initialize_secondary();
458 * Interrupts are still disabled. Do necessary setups, then
462 printk(linux_banner
);
463 setup_arch(&command_line
, &memory_start
, &memory_end
);
464 memory_start
= paging_init(memory_start
,memory_end
);
469 parse_options(command_line
);
472 * HACK ALERT! This is early. We're enabling the console before
473 * we've done PCI setups etc, and console_init() must be aware of
474 * this. But we do want output early, in case something goes wrong.
476 memory_start
= console_init(memory_start
,memory_end
);
477 #ifdef CONFIG_MODULES
481 prof_buffer
= (unsigned int *) memory_start
;
482 /* only text is profiled */
483 prof_len
= (unsigned long) &_etext
- (unsigned long) &_stext
;
484 prof_len
>>= prof_shift
;
485 memory_start
+= prof_len
* sizeof(unsigned int);
486 memset(prof_buffer
, 0, prof_len
* sizeof(unsigned int));
489 memory_start
= kmem_cache_init(memory_start
, memory_end
);
492 #ifdef CONFIG_BLK_DEV_INITRD
493 if (initrd_start
&& !initrd_below_start_ok
&& initrd_start
< memory_start
) {
494 printk(KERN_CRIT
"initrd overwritten (0x%08lx < 0x%08lx) - "
495 "disabling it.\n",initrd_start
,memory_start
);
499 mem_init(memory_start
,memory_end
);
500 kmem_cache_sizes_init();
501 #ifdef CONFIG_PROC_FS
504 fork_init(memory_end
-memory_start
);
508 buffer_init(memory_end
-memory_start
);
509 page_cache_init(memory_end
-memory_start
);
514 #if defined(CONFIG_SYSVIPC)
517 #if defined(CONFIG_QUOTA)
521 printk("POSIX conformance testing by UNIFIX\n");
524 * We count on the initial thread going ok
525 * Like idlers init is an unlocked kernel thread, which will
526 * make syscalls (and thus be locked).
529 kernel_thread(init
, NULL
, CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
531 current
->need_resched
= 1;
535 #ifdef CONFIG_BLK_DEV_INITRD
536 static int do_linuxrc(void * shell
)
538 static char *argv
[] = { "linuxrc", NULL
, };
540 close(0);close(1);close(2);
542 (void) open("/dev/console",O_RDWR
,0);
545 return execve(shell
, argv
, envp_init
);
548 static int __init
no_initrd(char *s
)
554 __setup("noinitrd", no_initrd
);
558 struct task_struct
*child_reaper
= &init_task
;
560 static void __init
do_initcalls(void)
564 call
= &__initcall_start
;
568 } while (call
< &__initcall_end
);
572 * Ok, the machine is now initialized. None of the devices
573 * have been touched yet, but the CPU subsystem is up and
574 * running, and memory and process management works.
576 * Now we can finally start doing some real work..
578 static void __init
do_basic_setup(void)
580 #ifdef CONFIG_BLK_DEV_INITRD
581 int real_root_mountflags
;
585 * Tell the world that we're going to be the grim
586 * reaper of innocent orphaned children.
588 * We don't want people to have to make incorrect
589 * assumptions about where in the task array this
592 child_reaper
= current
;
594 #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
596 * We should probably create some architecture-dependent "fixup after
597 * everything is up" style function where this would belong better
598 * than in init/main.c..
608 * Ok, at this point all CPU's should be initialized, so
609 * we can start looking into devices..
617 #if defined(CONFIG_PPC)
623 #ifdef CONFIG_ARCH_ACORN
636 /* Networking initialization needs a process context */
641 #ifdef CONFIG_BLK_DEV_INITRD
643 real_root_dev
= ROOT_DEV
;
644 real_root_mountflags
= root_mountflags
;
645 if (initrd_start
&& mount_initrd
) root_mountflags
&= ~MS_RDONLY
;
646 else mount_initrd
=0;
649 /* .. filesystems .. */
652 /* Mount the root filesystem.. */
655 #ifdef CONFIG_BLK_DEV_INITRD
656 root_mountflags
= real_root_mountflags
;
657 if (mount_initrd
&& ROOT_DEV
!= real_root_dev
658 && MAJOR(ROOT_DEV
) == RAMDISK_MAJOR
&& MINOR(ROOT_DEV
) == 0) {
662 pid
= kernel_thread(do_linuxrc
, "/linuxrc", SIGCHLD
);
664 while (pid
!= wait(&i
));
665 if (MAJOR(real_root_dev
) != RAMDISK_MAJOR
666 || MINOR(real_root_dev
) != 0) {
667 error
= change_root(real_root_dev
,"/initrd");
669 printk(KERN_ERR
"Change root to /initrd: "
676 static int init(void * unused
)
682 * Ok, we have completed the initial bootup, and
683 * we're essentially up and running. Get rid of the
684 * initmem segments and start the user-mode stuff..
689 if (open("/dev/console", O_RDWR
, 0) < 0)
690 printk("Warning: unable to open an initial console.\n");
696 * We try each of these until one succeeds.
698 * The Bourne shell can be used instead of init if we are
699 * trying to recover a really broken machine.
703 execve(execute_command
,argv_init
,envp_init
);
704 execve("/sbin/init",argv_init
,envp_init
);
705 execve("/etc/init",argv_init
,envp_init
);
706 execve("/bin/init",argv_init
,envp_init
);
707 execve("/bin/sh",argv_init
,envp_init
);
708 panic("No init found. Try passing init= option to kernel.");