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