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