1
2
3
4
5
6
7
8
9
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
49#include <asm/io.h>
50#include <asm/bugs.h>
51#include <asm/setup.h>
52#include <asm/timex.h>
53
54
55
56
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
70
71
72
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
78extern char *linux_banner;
79
80static int init(void *);
81
82extern void init_IRQ(void);
83extern void sock_init(void);
84extern void fork_init(unsigned long);
85extern void mca_init(void);
86extern void sbus_init(void);
87extern void sysctl_init(void);
88extern void signals_init(void);
89extern void buffer_init(void);
90extern void pidhash_init(void);
91extern void pidmap_init(void);
92extern void prio_tree_init(void);
93extern void radix_tree_init(void);
94extern void free_initmem(void);
95extern void populate_rootfs(void);
96extern void driver_init(void);
97extern void prepare_namespace(void);
98#ifdef CONFIG_ACPI
99extern void acpi_early_init(void);
100#else
101static inline void acpi_early_init(void) { }
102#endif
103
104#ifdef CONFIG_TC
105extern void tc_init(void);
106#endif
107
108enum system_states system_state;
109EXPORT_SYMBOL(system_state);
110
111
112
113
114#define MAX_INIT_ARGS 32
115#define MAX_INIT_ENVS 32
116
117extern void time_init(void);
118
119void (*late_time_init)(void);
120extern void softirq_init(void);
121
122
123char saved_command_line[COMMAND_LINE_SIZE];
124
125static char *execute_command;
126
127
128static unsigned int max_cpus = NR_CPUS;
129
130
131
132
133
134
135
136
137
138
139
140static int __init nosmp(char *str)
141{
142 max_cpus = 0;
143 return 1;
144}
145
146__setup("nosmp", nosmp);
147
148static int __init maxcpus(char *str)
149{
150 get_option(&str, &max_cpus);
151 return 1;
152}
153
154__setup("maxcpus=", maxcpus);
155
156static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
157char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
158static const char *panic_later, *panic_param;
159
160static int __init obsolete_checksetup(char *line)
161{
162 struct obs_kernel_param *p;
163 extern struct obs_kernel_param __setup_start, __setup_end;
164
165 p = &__setup_start;
166 do {
167 int n = strlen(p->str);
168 if (!strncmp(line, p->str, n)) {
169 if (p->early) {
170
171
172 if (line[n] == '\0' || line[n] == '=')
173 return 1;
174 } else if (!p->setup_func) {
175 printk(KERN_WARNING "Parameter %s is obsolete,"
176 " ignored\n", p->str);
177 return 1;
178 } else if (p->setup_func(line + n))
179 return 1;
180 }
181 p++;
182 } while (p < &__setup_end);
183 return 0;
184}
185
186static unsigned long preset_lpj;
187static int __init lpj_setup(char *str)
188{
189 preset_lpj = simple_strtoul(str,NULL,0);
190 return 1;
191}
192
193__setup("lpj=", lpj_setup);
194
195
196
197
198
199unsigned long loops_per_jiffy = (1<<12);
200
201EXPORT_SYMBOL(loops_per_jiffy);
202
203#ifdef ARCH_HAS_READ_CURRENT_TIMER
204
205
206
207
208
209
210#define DELAY_CALIBRATION_TICKS ((HZ < 100) ? 1 : (HZ/100))
211#define MAX_DIRECT_CALIBRATION_RETRIES 5
212
213static unsigned long __devinit calibrate_delay_direct(void)
214{
215 unsigned long pre_start, start, post_start;
216 unsigned long pre_end, end, post_end;
217 unsigned long start_jiffies;
218 unsigned long tsc_rate_min, tsc_rate_max;
219 unsigned long good_tsc_sum = 0;
220 unsigned long good_tsc_count = 0;
221 unsigned long delay_calibration_ticks = ((REAL_HZ < 100) ? 1 : (REAL_HZ/100));
222 int i;
223
224 if (read_current_timer(&pre_start) < 0 )
225 return 0;
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++) {
247 pre_start = 0;
248 read_current_timer(&start);
249 start_jiffies = jiffies;
250 while (jiffies <= (start_jiffies + tick_divider)) {
251 pre_start = start;
252 read_current_timer(&start);
253 }
254 read_current_timer(&post_start);
255
256 pre_end = 0;
257 end = post_start;
258 while (jiffies <=
259 (start_jiffies + tick_divider *
260 (1 + delay_calibration_ticks))) {
261 pre_end = end;
262 read_current_timer(&end);
263 }
264 read_current_timer(&post_end);
265
266 tsc_rate_max = (post_end - pre_start) / delay_calibration_ticks;
267 tsc_rate_min = (pre_end - post_start) / delay_calibration_ticks;
268
269 tsc_rate_max /= tick_divider;
270 tsc_rate_min /= tick_divider;
271
272
273
274
275
276 if (pre_start != 0 && pre_end != 0 &&
277 (tsc_rate_max - tsc_rate_min) < (tsc_rate_max >> 3)) {
278 good_tsc_count++;
279 good_tsc_sum += tsc_rate_max;
280 }
281 }
282
283 if (good_tsc_count)
284 return (good_tsc_sum/good_tsc_count);
285
286 printk(KERN_WARNING "calibrate_delay_direct() failed to get a good "
287 "estimate for loops_per_jiffy.\nProbably due to long platform interrupts. Consider using \"lpj=\" boot option.\n");
288 return 0;
289}
290#else
291static unsigned long __devinit calibrate_delay_direct(void) {return 0;}
292#endif
293
294
295
296
297
298
299#define LPS_PREC 8
300
301void __devinit calibrate_delay(void)
302{
303 unsigned long ticks, loopbit;
304 int lps_precision = LPS_PREC;
305
306 if (preset_lpj) {
307 loops_per_jiffy = preset_lpj;
308 printk("Calibrating delay loop (skipped)... "
309 "%lu.%02lu BogoMIPS preset\n",
310 loops_per_jiffy/(500000/HZ),
311 (loops_per_jiffy/(5000/HZ)) % 100);
312 } else if ((loops_per_jiffy = calibrate_delay_direct()) != 0) {
313 printk("Calibrating delay using timer specific routine.. ");
314 printk("%lu.%02lu BogoMIPS (lpj=%lu)\n",
315 loops_per_jiffy/(500000/HZ),
316 (loops_per_jiffy/(5000/HZ)) % 100,
317 loops_per_jiffy);
318 } else {
319 loops_per_jiffy = (1<<12);
320
321 printk(KERN_DEBUG "Calibrating delay loop... ");
322 while ((loops_per_jiffy <<= 1) != 0) {
323
324 ticks = jiffies;
325 while (ticks == jiffies)
326 ;
327
328 ticks = jiffies;
329 __delay(loops_per_jiffy);
330 ticks = jiffies - ticks;
331 if (ticks)
332 break;
333 }
334
335
336
337
338
339 loops_per_jiffy >>= 1;
340 loopbit = loops_per_jiffy;
341 while (lps_precision-- && (loopbit >>= 1)) {
342 loops_per_jiffy |= loopbit;
343 ticks = jiffies;
344 while (ticks == jiffies)
345 ;
346 ticks = jiffies;
347 __delay(loops_per_jiffy);
348 if (jiffies != ticks)
349 loops_per_jiffy &= ~loopbit;
350 }
351
352
353 printk("%lu.%02lu BogoMIPS (lpj=%lu)\n",
354 loops_per_jiffy/(500000/HZ),
355 (loops_per_jiffy/(5000/HZ)) % 100,
356 loops_per_jiffy);
357 }
358
359}
360
361static int __init debug_kernel(char *str)
362{
363 if (*str)
364 return 0;
365 console_loglevel = 10;
366 return 1;
367}
368
369static int __init quiet_kernel(char *str)
370{
371 if (*str)
372 return 0;
373 console_loglevel = 4;
374 return 1;
375}
376
377__setup("debug", debug_kernel);
378__setup("quiet", quiet_kernel);
379
380
381
382
383
384static int __init unknown_bootoption(char *param, char *val)
385{
386
387 if (val) {
388 if (val[-1] == '"') val[-2] = '=';
389 else val[-1] = '=';
390 }
391
392
393 if (obsolete_checksetup(param))
394 return 0;
395
396
397
398
399
400 if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
401 printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
402 return 0;
403 }
404
405 if (panic_later)
406 return 0;
407
408 if (val) {
409
410 unsigned int i;
411 for (i = 0; envp_init[i]; i++) {
412 if (i == MAX_INIT_ENVS) {
413 panic_later = "Too many boot env vars at `%s'";
414 panic_param = param;
415 }
416 if (!strncmp(param, envp_init[i], val - param))
417 break;
418 }
419 envp_init[i] = param;
420 } else {
421
422 unsigned int i;
423 for (i = 0; argv_init[i]; i++) {
424 if (i == MAX_INIT_ARGS) {
425 panic_later = "Too many boot init vars at `%s'";
426 panic_param = param;
427 }
428 }
429 argv_init[i] = param;
430 }
431 return 0;
432}
433
434static int __init init_setup(char *str)
435{
436 unsigned int i;
437
438 execute_command = str;
439
440
441
442
443
444
445 for (i = 1; i < MAX_INIT_ARGS; i++)
446 argv_init[i] = NULL;
447 return 1;
448}
449__setup("init=", init_setup);
450
451extern void setup_arch(char **);
452extern void cpu_idle(void);
453
454#ifndef CONFIG_SMP
455
456#ifdef CONFIG_X86_LOCAL_APIC
457static void __init smp_init(void)
458{
459 APIC_init_uniprocessor();
460}
461#else
462#define smp_init() do { } while (0)
463#endif
464
465static inline void setup_per_cpu_areas(void) { }
466static inline void smp_prepare_cpus(unsigned int maxcpus) { }
467
468#else
469
470#ifdef __GENERIC_PER_CPU
471unsigned long __per_cpu_offset[NR_CPUS];
472
473EXPORT_SYMBOL(__per_cpu_offset);
474
475static void __init setup_per_cpu_areas(void)
476{
477 unsigned long size, i;
478 char *ptr;
479
480 extern char __per_cpu_start[], __per_cpu_end[];
481
482
483 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
484#ifdef CONFIG_MODULES
485 if (size < PERCPU_ENOUGH_ROOM)
486 size = PERCPU_ENOUGH_ROOM;
487#endif
488
489 ptr = alloc_bootmem(size * NR_CPUS);
490
491 for (i = 0; i < NR_CPUS; i++, ptr += size) {
492 __per_cpu_offset[i] = ptr - __per_cpu_start;
493 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
494 }
495}
496#endif
497
498
499static void __init smp_init(void)
500{
501 unsigned int i;
502
503
504 for_each_present_cpu(i) {
505 if (num_online_cpus() >= max_cpus)
506 break;
507 if (!cpu_online(i))
508 cpu_up(i);
509 }
510
511
512 printk("Brought up %ld CPUs\n", (long)num_online_cpus());
513 smp_cpus_done(max_cpus);
514#if 0
515
516
517 smp_threads_ready=1;
518 smp_commence();
519#endif
520}
521
522#endif
523
524
525
526
527
528
529
530
531
532
533static void noinline rest_init(void)
534{
535 kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
536 numa_default_policy();
537 system_state = SYSTEM_BOOTING_SCHEDULER_OK;
538 unlock_kernel();
539 cpu_idle();
540}
541
542
543static int __init do_early_param(char *param, char *val)
544{
545 struct obs_kernel_param *p;
546 extern struct obs_kernel_param __setup_start, __setup_end;
547
548 for (p = &__setup_start; p < &__setup_end; p++) {
549 if (p->early && strcmp(param, p->str) == 0) {
550 if (p->setup_func(val) != 0)
551 printk(KERN_WARNING
552 "Malformed early option '%s'\n", param);
553 }
554 }
555
556 return 0;
557}
558
559
560void __init parse_early_param(void)
561{
562 static __initdata int done = 0;
563 static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
564
565 if (done)
566 return;
567
568
569 strlcpy(tmp_cmdline, saved_command_line, COMMAND_LINE_SIZE);
570 parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
571 done = 1;
572}
573
574
575
576
577
578asmlinkage void __init start_kernel(void)
579{
580 char * command_line;
581 extern struct kernel_param __start___param[], __stop___param[];
582
583
584
585
586 lock_kernel();
587 page_address_init();
588 printk(linux_banner);
589 setup_arch(&command_line);
590 setup_per_cpu_areas();
591
592
593
594
595
596 smp_prepare_boot_cpu();
597
598
599
600
601
602
603 sched_init();
604 build_all_zonelists();
605 page_alloc_init();
606 printk("Kernel command line: %s\n", saved_command_line);
607 parse_early_param();
608 parse_args("Booting kernel", command_line, __start___param,
609 __stop___param - __start___param,
610 &unknown_bootoption);
611 sort_main_extable();
612 trap_init();
613 rcu_init();
614 init_IRQ();
615 pidhash_init();
616 init_timers();
617 softirq_init();
618 time_init();
619
620
621
622
623
624
625 console_init();
626 if (panic_later)
627 panic(panic_later, panic_param);
628 profile_init();
629 local_irq_enable();
630#ifdef CONFIG_BLK_DEV_INITRD
631 if (initrd_start && !initrd_below_start_ok &&
632 initrd_start < min_low_pfn << PAGE_SHIFT) {
633 printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
634 "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
635 initrd_start = 0;
636 }
637#endif
638 vfs_caches_init_early();
639 mem_init();
640 kmem_cache_init();
641 numa_policy_init();
642 if (late_time_init)
643 late_time_init();
644 calibrate_delay();
645 pidmap_init();
646 pgtable_cache_init();
647 prio_tree_init();
648 anon_vma_init();
649#ifdef CONFIG_X86
650 if (efi_enabled)
651 efi_enter_virtual_mode();
652#endif
653 fork_init(num_physpages);
654 proc_caches_init();
655 buffer_init();
656 unnamed_dev_init();
657 key_init();
658 security_scaffolding_startup();
659 vfs_caches_init(num_physpages);
660 radix_tree_init();
661 signals_init();
662
663 page_writeback_init();
664#ifdef CONFIG_PROC_FS
665 proc_root_init();
666#endif
667 check_bugs();
668
669 acpi_early_init();
670
671
672 rest_init();
673}
674
675static int __initdata initcall_debug;
676
677static int __init initcall_debug_setup(char *str)
678{
679 initcall_debug = 1;
680 return 1;
681}
682__setup("initcall_debug", initcall_debug_setup);
683
684struct task_struct *child_reaper = &init_task;
685
686extern initcall_t __initcall_start, __initcall_end;
687
688static void __init do_initcalls(void)
689{
690 initcall_t *call;
691 int count = preempt_count();
692
693 for (call = &__initcall_start; call < &__initcall_end; call++) {
694 char *msg;
695
696 if (initcall_debug) {
697 printk(KERN_DEBUG "Calling initcall 0x%p", *call);
698 print_fn_descriptor_symbol(": %s()",
699 (unsigned long) *call);
700 printk("\n");
701 }
702
703 (*call)();
704
705 msg = NULL;
706 if (preempt_count() != count) {
707 msg = "preemption imbalance";
708 preempt_count() = count;
709 }
710 if (irqs_disabled()) {
711 msg = "disabled interrupts";
712 local_irq_enable();
713 }
714 if (msg) {
715 printk("error in initcall at 0x%p: "
716 "returned with %s\n", *call, msg);
717 }
718 }
719
720
721 flush_scheduled_work();
722}
723
724
725
726
727
728
729
730
731static void __init do_basic_setup(void)
732{
733
734 init_workqueues();
735 usermodehelper_init();
736
737 driver_init();
738
739#ifdef CONFIG_SYSCTL
740 sysctl_init();
741#endif
742
743
744 sock_init();
745
746 do_initcalls();
747}
748
749static void do_pre_smp_initcalls(void)
750{
751 extern int spawn_ksoftirqd(void);
752#ifdef CONFIG_SMP
753 extern int migration_init(void);
754
755 migration_init();
756#endif
757 spawn_ksoftirqd();
758}
759
760static void run_init_process(char *init_filename)
761{
762 argv_init[0] = init_filename;
763 execve(init_filename, argv_init, envp_init);
764}
765
766static inline void fixup_cpu_present_map(void)
767{
768#ifdef CONFIG_SMP
769 int i;
770
771
772
773
774
775
776 if (cpus_empty(cpu_present_map)) {
777 for_each_cpu(i) {
778 cpu_set(i, cpu_present_map);
779 }
780 }
781#endif
782}
783
784static int init(void * unused)
785{
786 lock_kernel();
787
788
789
790
791
792
793
794
795 child_reaper = current;
796
797
798 smp_prepare_cpus(max_cpus);
799
800 do_pre_smp_initcalls();
801
802 fixup_cpu_present_map();
803 smp_init();
804
805
806
807
808
809 populate_rootfs();
810
811 do_basic_setup();
812
813 sched_init_smp();
814
815
816
817
818
819 if (sys_access((const char __user *) "/init", 0) == 0)
820 execute_command = "/init";
821 else
822 prepare_namespace();
823
824
825
826
827
828
829 free_initmem();
830 unlock_kernel();
831 system_state = SYSTEM_RUNNING;
832 numa_default_policy();
833
834 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
835 printk("Warning: unable to open an initial console.\n");
836
837 (void) sys_dup(0);
838 (void) sys_dup(0);
839
840
841
842
843
844
845
846
847 if (execute_command)
848 run_init_process(execute_command);
849
850 run_init_process("/sbin/init");
851 run_init_process("/etc/init");
852 run_init_process("/bin/init");
853 run_init_process("/bin/sh");
854
855 panic("No init found. Try passing init= option to kernel.");
856}
857
858static int early_param_test(char *rest)
859{
860 printk("early_parm_test: %s\n", rest ?: "(null)");
861 return rest ? 0 : -EINVAL;
862}
863early_param("testsetup", early_param_test);
864static int early_setup_test(char *rest)
865{
866 printk("early_setup_test: %s\n", rest ?: "(null)");
867 return 0;
868}
869__setup("testsetup_long", early_setup_test);
870