This source file includes following definitions.
- get_options
- profile_setup
- ramdisk_setup
- checksetup
- calibrate_delay
- parse_options
- start_secondary
- smp_idle
- smp_init
- smp_begin
- start_kernel
- printf
- do_rc
- do_shell
- init
1
2
3
4
5
6
7
8
9 #define __KERNEL_SYSCALLS__
10 #include <stdarg.h>
11
12 #include <asm/system.h>
13 #include <asm/io.h>
14
15 #include <linux/types.h>
16 #include <linux/fcntl.h>
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/tty.h>
20 #include <linux/head.h>
21 #include <linux/unistd.h>
22 #include <linux/string.h>
23 #include <linux/timer.h>
24 #include <linux/fs.h>
25 #include <linux/ctype.h>
26 #include <linux/delay.h>
27 #include <linux/utsname.h>
28 #include <linux/ioport.h>
29 #include <linux/hdreg.h>
30 #include <linux/mm.h>
31 #include <linux/major.h>
32
33 #include <asm/bugs.h>
34
35 extern char _stext, _etext;
36 extern char *linux_banner;
37
38 static char printbuf[1024];
39
40 extern int console_loglevel;
41
42 static int init(void *);
43
44 extern void init_IRQ(void);
45 extern void init_modules(void);
46 extern long console_init(long, long);
47 extern long kmalloc_init(long,long);
48 extern void sock_init(void);
49 extern long rd_init(long mem_start, int length);
50 extern long pci_init(long, long);
51
52 extern void swap_setup(char *str, int *ints);
53 extern void buff_setup(char *str, int *ints);
54 extern void bmouse_setup(char *str, int *ints);
55 extern void eth_setup(char *str, int *ints);
56 extern void xd_setup(char *str, int *ints);
57 extern void floppy_setup(char *str, int *ints);
58 extern void st_setup(char *str, int *ints);
59 extern void st0x_setup(char *str, int *ints);
60 extern void tmc8xx_setup(char *str, int *ints);
61 extern void t128_setup(char *str, int *ints);
62 extern void pas16_setup(char *str, int *ints);
63 extern void generic_NCR5380_setup(char *str, int *intr);
64 extern void aha152x_setup(char *str, int *ints);
65 extern void aha1542_setup(char *str, int *ints);
66 extern void aic7xxx_setup(char *str, int *ints);
67 extern void BusLogic_Setup(char *str, int *ints);
68 extern void fdomain_setup(char *str, int *ints);
69 extern void NCR53c406a_setup(char *str, int *ints);
70 extern void scsi_luns_setup(char *str, int *ints);
71 extern void sound_setup(char *str, int *ints);
72 #ifdef CONFIG_CDU31A
73 extern void cdu31a_setup(char *str, int *ints);
74 #endif CONFIG_CDU31A
75 #ifdef CONFIG_MCD
76 extern void mcd_setup(char *str, int *ints);
77 #endif CONFIG_MCD
78 #ifdef CONFIG_MCDX
79 extern void mcdx_setup(char *str, int *ints);
80 #endif CONFIG_MCDX
81 #ifdef CONFIG_SBPCD
82 extern void sbpcd_setup(char *str, int *ints);
83 #endif CONFIG_SBPCD
84 #ifdef CONFIG_AZTCD
85 extern void aztcd_setup(char *str, int *ints);
86 #endif CONFIG_AZTCD
87 #ifdef CONFIG_CDU535
88 extern void sonycd535_setup(char *str, int *ints);
89 #endif CONFIG_CDU535
90 #ifdef CONFIG_GSCD
91 extern void gscd_setup(char *str, int *ints);
92 #endif CONFIG_GSCD
93 #ifdef CONFIG_CM206
94 extern void cm206_setup(char *str, int *ints);
95 #endif CONFIG_CM206
96 #ifdef CONFIG_OPTCD
97 extern void optcd_setup(char *str, int *ints);
98 #endif CONFIG_OPTCD
99 #ifdef CONFIG_SJCD
100 extern void sjcd_setup(char *str, int *ints);
101 #endif CONFIG_SJCD
102 static void ramdisk_setup(char *str, int *ints);
103
104 #ifdef CONFIG_SYSVIPC
105 extern void ipc_init(void);
106 #endif
107
108
109
110
111 #define MAX_INIT_ARGS 8
112 #define MAX_INIT_ENVS 8
113
114 extern void time_init(void);
115
116 static unsigned long memory_start = 0;
117 static unsigned long memory_end = 0;
118
119 int rows, cols;
120
121 int ramdisk_size;
122 int root_mountflags = MS_RDONLY;
123 char *execute_command = 0;
124
125 #ifdef CONFIG_ROOT_NFS
126 char nfs_root_name[256] = { NFS_ROOT };
127 char nfs_root_addrs[128] = { "" };
128 #endif
129
130 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
131 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
132
133 static char * argv_rc[] = { "/bin/sh", NULL };
134 static char * envp_rc[] = { "HOME=/", "TERM=linux", NULL };
135
136 static char * argv[] = { "-/bin/sh",NULL };
137 static char * envp[] = { "HOME=/usr/root", "TERM=linux", NULL };
138
139 char *get_options(char *str, int *ints)
140 {
141 char *cur = str;
142 int i=1;
143
144 while (cur && isdigit(*cur) && i <= 10) {
145 ints[i++] = simple_strtoul(cur,NULL,0);
146 if ((cur = strchr(cur,',')) != NULL)
147 cur++;
148 }
149 ints[0] = i-1;
150 return(cur);
151 }
152
153 static void profile_setup(char *str, int *ints)
154 {
155 if (ints[0] > 0)
156 prof_shift = (unsigned long) ints[1];
157 else
158 #ifdef CONFIG_PROFILE_SHIFT
159 prof_shift = CONFIG_PROFILE_SHIFT;
160 #else
161 prof_shift = 2;
162 #endif
163 }
164
165 struct {
166 const char *str;
167 void (*setup_func)(char *, int *);
168 } bootsetups[] = {
169 { "reserve=", reserve_setup },
170 { "profile=", profile_setup },
171 { "ramdisk=", ramdisk_setup },
172 { "swap=", swap_setup },
173 { "buff=", buff_setup },
174 #ifdef CONFIG_BUGi386
175 { "no-hlt", no_halt },
176 { "no387", no_387 },
177 #endif
178 #ifdef CONFIG_INET
179 { "ether=", eth_setup },
180 #endif
181 #ifdef CONFIG_SCSI
182 { "max_scsi_luns=", scsi_luns_setup },
183 #endif
184 #if defined(CONFIG_BLK_DEV_HD)
185 { "hd=", hd_setup },
186 #endif
187 #ifdef CONFIG_CHR_DEV_ST
188 { "st=", st_setup },
189 #endif
190 #ifdef CONFIG_BUSMOUSE
191 { "bmouse=", bmouse_setup },
192 #endif
193 #ifdef CONFIG_SCSI_SEAGATE
194 { "st0x=", st0x_setup },
195 { "tmc8xx=", tmc8xx_setup },
196 #endif
197 #ifdef CONFIG_SCSI_T128
198 { "t128=", t128_setup },
199 #endif
200 #ifdef CONFIG_SCSI_PAS16
201 { "pas16=", pas16_setup },
202 #endif
203 #ifdef CONFIG_SCSI_GENERIC_NCR5380
204 { "ncr5380=", generic_NCR5380_setup },
205 #endif
206 #ifdef CONFIG_SCSI_AHA152X
207 { "aha152x=", aha152x_setup},
208 #endif
209 #ifdef CONFIG_SCSI_AHA1542
210 { "aha1542=", aha1542_setup},
211 #endif
212 #ifdef CONFIG_SCSI_AIC7XXX
213 { "aic7xxx=", aic7xxx_setup},
214 #endif
215 #ifdef CONFIG_SCSI_BUSLOGIC
216 { "BusLogic=", BusLogic_Setup},
217 #endif
218 #ifdef CONFIG_SCSI_NCR53C406A
219 { "ncr53c406a=", NCR53c406a_setup},
220 #endif
221 #ifdef CONFIG_SCSI_FUTURE_DOMAIN
222 { "fdomain=", fdomain_setup},
223 #endif
224 #ifdef CONFIG_BLK_DEV_XD
225 { "xd=", xd_setup },
226 #endif
227 #ifdef CONFIG_BLK_DEV_FD
228 { "floppy=", floppy_setup },
229 #endif
230 #ifdef CONFIG_CDU31A
231 { "cdu31a=", cdu31a_setup },
232 #endif CONFIG_CDU31A
233 #ifdef CONFIG_MCD
234 { "mcd=", mcd_setup },
235 #endif CONFIG_MCD
236 #ifdef CONFIG_MCDX
237 { "mcdx=", mcdx_setup },
238 #endif CONFIG_MCDX
239 #ifdef CONFIG_SBPCD
240 { "sbpcd=", sbpcd_setup },
241 #endif CONFIG_SBPCD
242 #ifdef CONFIG_AZTCD
243 { "aztcd=", aztcd_setup },
244 #endif CONFIG_AZTCD
245 #ifdef CONFIG_CDU535
246 { "sonycd535=", sonycd535_setup },
247 #endif CONFIG_CDU535
248 #ifdef CONFIG_GSCD
249 { "gscd=", gscd_setup },
250 #endif CONFIG_GSCD
251 #ifdef CONFIG_CM206
252 { "cm206=", cm206_setup },
253 #endif CONFIG_CM206
254 #ifdef CONFIG_OPTCD
255 { "optcd=", optcd_setup },
256 #endif CONFIG_OPTCD
257 #ifdef CONFIG_SJCD
258 { "sjcd=", sjcd_setup },
259 #endif CONFIG_SJCD
260 #ifdef CONFIG_SOUND
261 { "sound=", sound_setup },
262 #endif
263 { 0, 0 }
264 };
265
266 static void ramdisk_setup(char *str, int *ints)
267 {
268 if (ints[0] > 0 && ints[1] >= 0)
269 ramdisk_size = ints[1];
270 }
271
272 static int checksetup(char *line)
273 {
274 int i = 0;
275 int ints[11];
276
277 #ifdef CONFIG_BLK_DEV_IDE
278
279 if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '=')) {
280 ide_setup(line);
281 return 1;
282 }
283 #endif
284 while (bootsetups[i].str) {
285 int n = strlen(bootsetups[i].str);
286 if (!strncmp(line,bootsetups[i].str,n)) {
287 bootsetups[i].setup_func(get_options(line+n,ints), ints);
288 return 1;
289 }
290 i++;
291 }
292 return 0;
293 }
294
295
296
297 unsigned long loops_per_sec = (1<<12);
298
299
300
301
302 #define LPS_PREC 8
303
304 void calibrate_delay(void)
305 {
306 int ticks;
307 int loopbit;
308 int lps_precision = LPS_PREC;
309
310 loops_per_sec = (1<<12);
311
312 printk("Calibrating delay loop.. ");
313 while (loops_per_sec <<= 1) {
314
315 ticks = jiffies;
316 while (ticks == jiffies)
317 ;
318
319 ticks = jiffies;
320 __delay(loops_per_sec);
321 ticks = jiffies - ticks;
322 if (ticks)
323 break;
324 }
325
326
327
328 loops_per_sec >>= 1;
329 loopbit = loops_per_sec;
330 while ( lps_precision-- && (loopbit >>= 1) ) {
331 loops_per_sec |= loopbit;
332 ticks = jiffies;
333 while (ticks == jiffies);
334 ticks = jiffies;
335 __delay(loops_per_sec);
336 if (jiffies != ticks)
337 loops_per_sec &= ~loopbit;
338 }
339
340
341 loops_per_sec *= HZ;
342
343 printk("ok - %lu.%02lu BogoMIPS\n",
344 (loops_per_sec+2500)/500000,
345 ((loops_per_sec+2500)/5000) % 100);
346 }
347
348
349
350
351
352
353
354
355
356
357
358 static void parse_options(char *line)
359 {
360 char *next;
361 #ifdef CONFIG_ROOT_NFS
362 char *devnames[] = { "nfs", "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
363 int devnums[] = { 0x0FF, 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
364 #else
365 static const char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
366 static int devnums[] = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
367 #endif
368 int args, envs;
369 if (!*line)
370 return;
371 args = 0;
372 envs = 1;
373 next = line;
374 while ((line = next) != NULL) {
375 if ((next = strchr(line,' ')) != NULL)
376 *next++ = 0;
377
378
379
380 if (!strncmp(line,"root=",5)) {
381 int n;
382 line += 5;
383 if (strncmp(line,"/dev/",5)) {
384 ROOT_DEV = to_kdev_t(
385 simple_strtoul(line,NULL,16));
386 continue;
387 }
388 line += 5;
389 for (n = 0 ; devnames[n] ; n++) {
390 int len = strlen(devnames[n]);
391 if (!strncmp(line,devnames[n],len)) {
392 ROOT_DEV = to_kdev_t(devnums[n]+
393 simple_strtoul(line+len,NULL,0));
394 break;
395 }
396 }
397 continue;
398 }
399 #ifdef CONFIG_ROOT_NFS
400 if (!strncmp(line, "nfsroot=", 8)) {
401 int n;
402 line += 8;
403 if (line[0] == '/' || (line[0] >= '0' && line[0] <= '9')) {
404 strncpy(nfs_root_name, line, sizeof(nfs_root_name));
405 nfs_root_name[sizeof(nfs_root_name)] = '\0';
406 continue;
407 }
408 n = strlen(line) + strlen(NFS_ROOT);
409 if (n >= sizeof(nfs_root_name))
410 line[sizeof(nfs_root_name) - strlen(NFS_ROOT) - 1] = '\0';
411 sprintf(nfs_root_name, NFS_ROOT, line);
412 continue;
413 }
414 if (!strncmp(line, "nfsaddrs=", 9)) {
415 line += 9;
416 strncpy(nfs_root_addrs, line, sizeof(nfs_root_addrs));
417 nfs_root_addrs[sizeof(nfs_root_addrs)] = '\0';
418 continue;
419 }
420 #endif
421 if (!strcmp(line,"ro")) {
422 root_mountflags |= MS_RDONLY;
423 continue;
424 }
425 if (!strcmp(line,"rw")) {
426 root_mountflags &= ~MS_RDONLY;
427 continue;
428 }
429 if (!strcmp(line,"debug")) {
430 console_loglevel = 10;
431 continue;
432 }
433 if (!strncmp(line,"init=",5)) {
434 line += 5;
435 execute_command = line;
436 continue;
437 }
438 if (checksetup(line))
439 continue;
440
441
442
443
444 if (strchr(line,'=')) {
445 if (envs >= MAX_INIT_ENVS)
446 break;
447 envp_init[++envs] = line;
448 } else {
449 if (args >= MAX_INIT_ARGS)
450 break;
451 argv_init[++args] = line;
452 }
453 }
454 argv_init[args+1] = NULL;
455 envp_init[envs+1] = NULL;
456 }
457
458 extern void setup_arch(char **, unsigned long *, unsigned long *);
459
460 #ifdef __SMP__
461
462
463
464
465 asmlinkage void start_secondary(void)
466 {
467 trap_init();
468 init_IRQ();
469 smp_callin();
470 for(;;)
471 idle();
472 }
473
474 int smp_idle(void * unused)
475 {
476 for (;;)
477 idle();
478 }
479
480
481
482
483
484 static void smp_init(void)
485 {
486 int i=0;
487 smp_boot_cpus();
488
489
490
491
492
493 for(i=1;i<smp_num_cpus;i++)
494 {
495 kernel_thread(smp_idle, NULL, CLONE_PID);
496
497
498
499 current_set[i]=task[i];
500 current_set[i]->processor=i;
501 }
502 }
503
504
505
506
507
508
509
510 static void smp_begin(void)
511 {
512 smp_threads_ready=1;
513 smp_commence();
514 }
515
516 #endif
517
518
519
520
521
522 asmlinkage void start_kernel(void)
523 {
524 char * command_line;
525
526
527
528
529
530 #ifdef __SMP__
531 static int first_cpu=1;
532
533 if(!first_cpu)
534 start_secondary();
535 first_cpu=0;
536
537 #endif
538
539
540
541
542 setup_arch(&command_line, &memory_start, &memory_end);
543 memory_start = paging_init(memory_start,memory_end);
544 trap_init();
545 init_IRQ();
546 sched_init();
547 time_init();
548 parse_options(command_line);
549 init_modules();
550 #ifdef CONFIG_PROFILE
551 if (!prof_shift)
552 #ifdef CONFIG_PROFILE_SHIFT
553 prof_shift = CONFIG_PROFILE_SHIFT;
554 #else
555 prof_shift = 2;
556 #endif
557 #endif
558 if (prof_shift) {
559 prof_buffer = (unsigned long *) memory_start;
560
561 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
562 prof_len >>= prof_shift;
563 memory_start += prof_len * sizeof(unsigned long);
564 }
565 memory_start = console_init(memory_start,memory_end);
566 #ifdef CONFIG_PCI
567 memory_start = pci_init(memory_start,memory_end);
568 #endif
569 memory_start = kmalloc_init(memory_start,memory_end);
570 sti();
571 calibrate_delay();
572 memory_start = inode_init(memory_start,memory_end);
573 memory_start = file_table_init(memory_start,memory_end);
574 memory_start = name_cache_init(memory_start,memory_end);
575 if (ramdisk_size)
576 memory_start += rd_init(memory_start, ramdisk_size*1024);
577 mem_init(memory_start,memory_end);
578 buffer_init();
579 sock_init();
580 #ifdef CONFIG_SYSVIPC
581 ipc_init();
582 #endif
583 sti();
584 check_bugs();
585
586 printk(linux_banner);
587 #ifdef __SMP__
588 smp_init();
589 #endif
590
591 kernel_thread(init, NULL, 0);
592
593
594
595
596
597
598
599
600
601 for(;;)
602 idle();
603 }
604
605 static int printf(const char *fmt, ...)
606 {
607 va_list args;
608 int i;
609
610 va_start(args, fmt);
611 write(1,printbuf,i=vsprintf(printbuf, fmt, args));
612 va_end(args);
613 return i;
614 }
615
616 static int do_rc(void * rc)
617 {
618 close(0);
619 if (open(rc,O_RDONLY,0))
620 return -1;
621 return execve("/bin/sh", argv_rc, envp_rc);
622 }
623
624 static int do_shell(void * shell)
625 {
626 close(0);close(1);close(2);
627 setsid();
628 (void) open("/dev/tty1",O_RDWR,0);
629 (void) dup(0);
630 (void) dup(0);
631 return execve(shell, argv, envp);
632 }
633
634 static int init(void * unused)
635 {
636 int pid,i;
637
638 setup();
639
640 #ifdef __SMP__
641
642
643
644
645
646 smp_begin();
647 #endif
648
649 #ifdef CONFIG_UMSDOS_FS
650 {
651
652
653
654
655
656 extern struct inode *pseudo_root;
657 if (pseudo_root != NULL){
658 current->fs->root = pseudo_root;
659 current->fs->pwd = pseudo_root;
660 }
661 }
662 #endif
663
664 (void) open("/dev/tty1",O_RDWR,0);
665 (void) dup(0);
666 (void) dup(0);
667
668 if (!execute_command) {
669 execve("/etc/init",argv_init,envp_init);
670 execve("/bin/init",argv_init,envp_init);
671 execve("/sbin/init",argv_init,envp_init);
672
673
674 pid = kernel_thread(do_rc, "/etc/rc", SIGCHLD);
675 if (pid>0)
676 while (pid != wait(&i))
677 ;
678 }
679
680 while (1) {
681 pid = kernel_thread(do_shell,
682 execute_command ? execute_command : "/bin/sh",
683 SIGCHLD);
684 if (pid < 0) {
685 printf("Fork failed in init\n\r");
686 continue;
687 }
688 while (1)
689 if (pid == wait(&i))
690 break;
691 printf("\n\rchild %d died with code %04x\n\r",pid,i);
692 sync();
693 }
694 return -1;
695 }