C
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/types.h>
16 #include <linux/module.h>
17 #include <linux/proc_fs.h>
18 #include <linux/devfs_fs_kernel.h>
19 #include <linux/kernel.h>
20 #include <linux/syscalls.h>
21 #include <linux/string.h>
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/utsname.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/smp_lock.h>
28 #include <linux/initrd.h>
29 #include <linux/hdreg.h>
30 #include <linux/bootmem.h>
31 #include <linux/tty.h>
32 #include <linux/gfp.h>
33 #include <linux/percpu.h>
34 #include <linux/kmod.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/security.h>
37 #include <linux/workqueue.h>
38 #include <linux/profile.h>
39 #include <linux/rcupdate.h>
40 #include <linux/moduleparam.h>
41 #include <linux/kallsyms.h>
42 #include <linux/writeback.h>
43 #include <linux/cpu.h>
44 #include <linux/efi.h>
45 #include <linux/unistd.h>
46 #include <linux/rmap.h>
47 #include <linux/mempolicy.h>
48 #include <linux/key.h>
49
50 #include <asm/io.h>
51 #include <asm/bugs.h>
52 #include <asm/setup.h>
53
54 /*
55 * This is one of the first .c files built. Error out early
56 * if we have compiler trouble..
57 */
58 #if __GNUC__ == 2 && __GNUC_MINOR__ == 96
59 #ifdef CONFIG_FRAME_POINTER
60 #error This compiler cannot compile correctly with frame pointers enabled
61 #endif
62 #endif
63
64 #ifdef CONFIG_X86_LOCAL_APIC
65 #include <asm/smp.h>
66 #endif
67
68 /*
69 * Versions of gcc older than that listed below may actually compile
70 * and link okay, but the end product can have subtle run time bugs.
71 * To avoid associated bogus bug reports, we flatly refuse to compile
72 * with a gcc that is known to be too old from the very beginning.
73 */
74 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 95)
75 #error Sorry, your GCC is too old. It builds incorrect kernels.
76 #endif
77
78 static int init(void *);
79
80 extern void init_IRQ(void);
81 extern void sock_init(void);
82 extern void fork_init(unsigned long);
83 extern void mca_init(void);
84 extern void sbus_init(void);
85 extern void sysctl_init(void);
86 extern void signals_init(void);
87 extern void buffer_init(void);
88 extern void pidhash_init(void);
89 extern void pidmap_init(void);
90 extern void prio_tree_init(void);
91 extern void radix_tree_init(void);
92 extern void free_initmem(void);
93 extern void populate_rootfs(void);
94 extern void driver_init(void);
95 extern void prepare_namespace(void);
96 #ifdef CONFIG_ACPI
97 extern void acpi_early_init(void);
98 #else
99 static inline void acpi_early_init(void) { }
100 #endif
101
102 #ifdef CONFIG_TC
103 extern void tc_init(void);
104 #endif
105
106 enum system_states system_state;
107 EXPORT_SYMBOL(system_state);
108
109 /*
110 * Boot command-line arguments
111 */
112 #define MAX_INIT_ARGS 32
113 #define MAX_INIT_ENVS 32
114
115 extern void time_init(void);
116 /* Default late time init is NULL. archs can override this later. */
117 void (*late_time_init)(void);
118 extern void softirq_init(void);
119
120 /* Untouched command line (eg. for /proc) saved by arch-specific code. */
121 char saved_command_line[COMMAND_LINE_SIZE];
122
123 static char *execute_command;
124
125 /* Setup configured maximum number of CPUs to activate */
126 static unsigned int max_cpus = NR_CPUS;
127
128 /*
129 * Setup routine for controlling SMP activation
130 *
131 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
132 * activation entirely (the MPS table probe still happens, though).
133 *
134 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
135 * greater than 0, limits the maximum number of CPUs activated in
136 * SMP mode to <NUM>.
137 */
138 static int __init nosmp(char *str)
139 {
140 max_cpus = 0;
141 return 1;
142 }
143
144 __setup("nosmp", nosmp);
145
146 static int __init maxcpus(char *str)
147 {
148 get_option(&str, &max_cpus);
149 return 1;
150 }
151
152 __setup("maxcpus=", maxcpus);
153
154 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
155 char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
156 static const char *panic_later, *panic_param;
157
158 extern struct obs_kernel_param __setup_start[], __setup_end[];
159
160 static int __init obsolete_checksetup(char *line)
161 {
162 struct obs_kernel_param *p;
163
164 p = __setup_start;
165 do {
166 int n = strlen(p->str);
167 if (!strncmp(line, p->str, n)) {
168 if (p->early) {
169 /* Already done in parse_early_param? (Needs
170 * exact match on param part) */
171 if (line[n] == '\0' || line[n] == '=')
172 return 1;
173 } else if (!p->setup_func) {
174 printk(KERN_WARNING "Parameter %s is obsolete,"
175 " ignored\n", p->str);
176 return 1;
177 } else if (p->setup_func(line + n))
178 return 1;
179 }
180 p++;
181 } while (p < __setup_end);
182 return 0;
183 }
184
185 /*
186 * This should be approx 2 Bo*oMips to start (note initial shift), and will
187 * still work even if initially too large, it will just take slightly longer
188 */
189 unsigned long loops_per_jiffy = (1<<12);
190
191 EXPORT_SYMBOL(loops_per_jiffy);
192
193 static int __init debug_kernel(char *str)
194 {
195 if (*str)
196 return 0;
197 console_loglevel = 10;
198 return 1;
199 }
200
201 static int __init quiet_kernel(char *str)
202 {
203 if (*str)
204 return 0;
205 console_loglevel = 4;
206 return 1;
207 }
208
209 __setup("debug", debug_kernel);
210 __setup("quiet", quiet_kernel);
211
212 /*
213 * Unknown boot options get handed to init, unless they look like
214 * failed parameters
215 */
216 static int __init unknown_bootoption(char *param, char *val)
217 {
218 /* Change NUL term back to "=", to make "param" the whole string. */
219 if (val) {
220 /* param=val or param="val"? */
221 if (val == param+strlen(param)+1)
222 val[-1] = '=';
223 else if (val == param+strlen(param)+2) {
224 val[-2] = '=';
225 memmove(val-1, val, strlen(val)+1);
226 val--;
227 } else
228 BUG();
229 }
230
231 /* Handle obsolete-style parameters */
232 if (obsolete_checksetup(param))
233 return 0;
234
235 /*
236 * Preemptive maintenance for "why didn't my mispelled command
237 * line work?"
238 */
239 if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
240 printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
241 return 0;
242 }
243
244 if (panic_later)
245 return 0;
246
247 if (val) {
248 /* Environment option */
249 unsigned int i;
250 for (i = 0; envp_init[i]; i++) {
251 if (i == MAX_INIT_ENVS) {
252 panic_later = "Too many boot env vars at `%s'";
253 panic_param = param;
254 }
255 if (!strncmp(param, envp_init[i], val - param))
256 break;
257 }
258 envp_init[i] = param;
259 } else {
260 /* Command line option */
261 unsigned int i;
262 for (i = 0; argv_init[i]; i++) {
263 if (i == MAX_INIT_ARGS) {
264 panic_later = "Too many boot init vars at `%s'";
265 panic_param = param;
266 }
267 }
268 argv_init[i] = param;
269 }
270 return 0;
271 }
272
273 static int __init init_setup(char *str)
274 {
275 unsigned int i;
276
277 execute_command = str;
278 /*
279 * In case LILO is going to boot us with default command line,
280 * it prepends "auto" before the whole cmdline which makes
281 * the shell think it should execute a script with such name.
282 * So we ignore all arguments entered _before_ init=... [MJ]
283 */
284 for (i = 1; i < MAX_INIT_ARGS; i++)
285 argv_init[i] = NULL;
286 return 1;
287 }
288 __setup("init=", init_setup);
289
290 extern void setup_arch(char **);
291
292 #ifndef CONFIG_SMP
293
294 #ifdef CONFIG_X86_LOCAL_APIC
295 static void __init smp_init(void)
296 {
297 APIC_init_uniprocessor();
298 }
299 #else
300 #define smp_init() do { } while (0)
301 #endif
302
303 static inline void setup_per_cpu_areas(void) { }
304 static inline void smp_prepare_cpus(unsigned int maxcpus) { }
305
306 #else
307
308 #ifdef __GENERIC_PER_CPU
309 unsigned long __per_cpu_offset[NR_CPUS];
310
311 EXPORT_SYMBOL(__per_cpu_offset);
312
313 static void __init setup_per_cpu_areas(void)
314 {
315 unsigned long size, i;
316 char *ptr;
317 /* Created by linker magic */
318 extern char __per_cpu_start[], __per_cpu_end[];
319
320 /* Copy section for each CPU (we discard the original) */
321 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
322 #ifdef CONFIG_MODULES
323 if (size < PERCPU_ENOUGH_ROOM)
324 size = PERCPU_ENOUGH_ROOM;
325 #endif
326
327 ptr = alloc_bootmem(size * NR_CPUS);
328
329 for (i = 0; i < NR_CPUS; i++, ptr += size) {
330 __per_cpu_offset[i] = ptr - __per_cpu_start;
331 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
332 }
333 }
334 #endif /* !__GENERIC_PER_CPU */
335
336 /* Called by boot processor to activate the rest. */
337 static void __init smp_init(void)
338 {
339 unsigned int i;
340
341 /* FIXME: This should be done in userspace --RR */
342 for_each_present_cpu(i) {
343 if (num_online_cpus() >= max_cpus)
344 break;
345 if (!cpu_online(i))
346 cpu_up(i);
347 }
348
349 /* Any cleanup work */
350 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
351 smp_cpus_done(max_cpus);
352 #if 0
353 /* Get other processors into their bootup holding patterns. */
354
355 smp_threads_ready=1;
356 smp_commence();
357 #endif
358 }
359
360 #endif
361
362 /*
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
366 * cpu_idle.
367 *
368 * gcc-3.4 accidentally inlines this function, so use noinline.
369 */
370
371 static void noinline rest_init(void)
372 __releases(kernel_lock)
373 {
374 kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
375 numa_default_policy();
376 unlock_kernel();
377 preempt_enable_no_resched();
378 cpu_idle();
379 }
380
381 /* Check for early params. */
382 static int __init do_early_param(char *param, char *val)
383 {
384 struct obs_kernel_param *p;
385
386 for (p = __setup_start; p < __setup_end; p++) {
387 if (p->early && strcmp(param, p->str) == 0) {
388 if (p->setup_func(val) != 0)
389 printk(KERN_WARNING
390 "Malformed early option '%s'\n", param);
391 }
392 }
393 /* We accept everything at this stage. */
394 return 0;
395 }
396
397 /* Arch code calls this early on, or if not, just before other parsing. */
398 void __init parse_early_param(void)
399 {
400 static __initdata int done = 0;
401 static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
402
403 if (done)
404 return;
405
406 /* All fall through to do_early_param. */
407 strlcpy(tmp_cmdline, saved_command_line, COMMAND_LINE_SIZE);
408 parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
409 done = 1;
410 }
411
412 /*
413 * Activate the first processor.
414 */
415
416 asmlinkage void __init start_kernel(void)
417 {
418 char * command_line;
419 extern struct kernel_param __start___param[], __stop___param[];
420 /*
421 * Interrupts are still disabled. Do necessary setups, then
422 * enable them
423 */
424 lock_kernel();
425 page_address_init();
426 printk(linux_banner);
427 setup_arch(&command_line);
428 setup_per_cpu_areas();
429
430 /*
431 * Mark the boot cpu "online" so that it can call console drivers in
432 * printk() and can access its per-cpu storage.
433 */
434 smp_prepare_boot_cpu();
435
436 /*
437 * Set up the scheduler prior starting any interrupts (such as the
438 * timer interrupt). Full topology setup happens at smp_init()
439 * time - but meanwhile we still have a functioning scheduler.
440 */
441 sched_init();
442 /*
443 * Disable preemption - early bootup scheduling is extremely
444 * fragile until we cpu_idle() for the first time.
445 */
446 preempt_disable();
447 build_all_zonelists();
448 page_alloc_init();
449 printk("Kernel command line: %s\n", saved_command_line);
450 parse_early_param();
451 parse_args("Booting kernel", command_line, __start___param,
452 __stop___param - __start___param,
453 &unknown_bootoption);
454 sort_main_extable();
455 trap_init();
456 rcu_init();
457 init_IRQ();
458 pidhash_init();
459 init_timers();
460 softirq_init();
461 time_init();
462
463 /*
464 * HACK ALERT! This is early. We're enabling the console before
465 * we've done PCI setups etc, and console_init() must be aware of
466 * this. But we do want output early, in case something goes wrong.
467 */
468 console_init();
469 if (panic_later)
470 panic(panic_later, panic_param);
471 profile_init();
472 local_irq_enable();
473 #ifdef CONFIG_BLK_DEV_INITRD
474 if (initrd_start && !initrd_below_start_ok &&
475 initrd_start < min_low_pfn << PAGE_SHIFT) {
476 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
477 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
478 initrd_start = 0;
479 }
480 #endif
481 vfs_caches_init_early();
482 mem_init();
483 kmem_cache_init();
484 numa_policy_init();
485 if (late_time_init)
486 late_time_init();
487 calibrate_delay();
488 pidmap_init();
489 pgtable_cache_init();
490 prio_tree_init();
491 anon_vma_init();
492 #ifdef CONFIG_X86
493 if (efi_enabled)
494 efi_enter_virtual_mode();
495 #endif
496 fork_init(num_physpages);
497 proc_caches_init();
498 buffer_init();
499 unnamed_dev_init();
500 security_init();
501 vfs_caches_init(num_physpages);
502 radix_tree_init();
503 signals_init();
504 /* rootfs populating might need page-writeback */
505 page_writeback_init();
506 #ifdef CONFIG_PROC_FS
507 proc_root_init();
508 #endif
509 check_bugs();
510
511 acpi_early_init(); /* before LAPIC and SMP init */
512
513 /* Do the rest non-__init'ed, we're now alive */
514 rest_init();
515 }
516
517 static int __initdata initcall_debug;
518
519 static int __init initcall_debug_setup(char *str)
520 {
521 initcall_debug = 1;
522 return 1;
523 }
524 __setup("initcall_debug", initcall_debug_setup);
525
526 struct task_struct *child_reaper = &init_task;
527
528 extern initcall_t __initcall_start[], __initcall_end[];
529
530 static void __init do_initcalls(void)
531 {
532 initcall_t *call;
533 int count = preempt_count();
534
535 for (call = __initcall_start; call < __initcall_end; call++) {
536 char *msg;
537
538 if (initcall_debug) {
539 printk(KERN_DEBUG "Calling initcall 0x%p", *call);
540 print_fn_descriptor_symbol(": %s()", (unsigned long) *call);
541 printk("\n");
542 }
543
544 (*call)();
545
546 msg = NULL;
547 if (preempt_count() != count) {
548 msg = "preemption imbalance";
549 preempt_count() = count;
550 }
551 if (irqs_disabled()) {
552 msg = "disabled interrupts";
553 local_irq_enable();
554 }
555 if (msg) {
556 printk("error in initcall at 0x%p: "
557 "returned with %s\n", *call, msg);
558 }
559 }
560
561 /* Make sure there is no pending stuff from the initcall sequence */
562 flush_scheduled_work();
563 }
564
565 /*
566 * Ok, the machine is now initialized. None of the devices
567 * have been touched yet, but the CPU subsystem is up and
568 * running, and memory and process management works.
569 *
570 * Now we can finally start doing some real work..
571 */
572 static void __init do_basic_setup(void)
573 {
574 /* drivers will send hotplug events */
575 init_workqueues();
576 usermodehelper_init();
577 key_init();
578 driver_init();
579
580 #ifdef CONFIG_SYSCTL
581 sysctl_init();
582 #endif
583
584 /* Networking initialization needs a process context */
585 sock_init();
586
587 do_initcalls();
588 }
589
590 static void do_pre_smp_initcalls(void)
591 {
592 extern int spawn_ksoftirqd(void);
593 #ifdef CONFIG_SMP
594 extern int migration_init(void);
595
596 migration_init();
597 #endif
598 spawn_ksoftirqd();
599 }
600
601 static void run_init_process(char *init_filename)
602 {
603 argv_init[0] = init_filename;
604 execve(init_filename, argv_init, envp_init);
605 }
606
607 static inline void fixup_cpu_present_map(void)
608 {
609 #ifdef CONFIG_SMP
610 int i;
611
612 /*
613 * If arch is not hotplug ready and did not populate
614 * cpu_present_map, just make cpu_present_map same as cpu_possible_map
615 * for other cpu bringup code to function as normal. e.g smp_init() etc.
616 */
617 if (cpus_empty(cpu_present_map)) {
618 for_each_cpu(i) {
619 cpu_set(i, cpu_present_map);
620 }
621 }
622 #endif
623 }
624
625 static int init(void * unused)
626 {
627 lock_kernel();
628 /*
629 * Tell the world that we're going to be the grim
630 * reaper of innocent orphaned children.
631 *
632 * We don't want people to have to make incorrect
633 * assumptions about where in the task array this
634 * can be found.
635 */
636 child_reaper = current;
637
638 /* Sets up cpus_possible() */
639 smp_prepare_cpus(max_cpus);
640
641 do_pre_smp_initcalls();
642
643 fixup_cpu_present_map();
644 smp_init();
645 sched_init_smp();
646
647 /*
648 * Do this before initcalls, because some drivers want to access
649 * firmware files.
650 */
651 populate_rootfs();
652
653 do_basic_setup();
654
655 /*
656 * check if there is an early userspace init. If yes, let it do all
657 * the work
658 */
659 if (sys_access((const char __user *) "/init", 0) == 0)
660 execute_command = "/init";
661 else
662 prepare_namespace();
663
664 /*
665 * Ok, we have completed the initial bootup, and
666 * we're essentially up and running. Get rid of the
667 * initmem segments and start the user-mode stuff..
668 */
669 free_initmem();
670 unlock_kernel();
671 system_state = SYSTEM_RUNNING;
672 numa_default_policy();
673
674 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
675 printk("Warning: unable to open an initial console.\n");
676
677 (void) sys_dup(0);
678 (void) sys_dup(0);
679
680 /*
681 * We try each of these until one succeeds.
682 *
683 * The Bourne shell can be used instead of init if we are
684 * trying to recover a really broken machine.
685 */
686
687 if (execute_command)
688 run_init_process(execute_command);
689
690 run_init_process("/sbin/init");
691 run_init_process("/etc/init");
692 run_init_process("/bin/init");
693 run_init_process("/bin/sh");
694
695 panic("No init found. Try passing init= option to kernel.");
696 }
697
o sensacionalismo �© a �ºltima fun�§�£o.
o funcionalismo insensibiliza.
o ultimato raspa o prato.
sensacional!
minha amargura
n�£o sai
no Di�¡rio Oficial!