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] = { 0, };
127 extern int nfs_root_init(char *name);
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 = 255 - strlen (NFS_ROOT);
402 line += 8;
403 if (line [0] == '/' || (line [0] >= '0' && line [0] <= '9')){
404 strncpy (nfs_root_name, line, 255);
405 continue;
406 }
407 if (strlen (line) >= n)
408 line [n] = 0;
409 sprintf (nfs_root_name, NFS_ROOT, line);
410 continue;
411 }
412 #endif
413 if (!strcmp(line,"ro")) {
414 root_mountflags |= MS_RDONLY;
415 continue;
416 }
417 if (!strcmp(line,"rw")) {
418 root_mountflags &= ~MS_RDONLY;
419 continue;
420 }
421 if (!strcmp(line,"debug")) {
422 console_loglevel = 10;
423 continue;
424 }
425 if (!strncmp(line,"init=",5)) {
426 line += 5;
427 execute_command = line;
428 continue;
429 }
430 if (checksetup(line))
431 continue;
432
433
434
435
436 if (strchr(line,'=')) {
437 if (envs >= MAX_INIT_ENVS)
438 break;
439 envp_init[++envs] = line;
440 } else {
441 if (args >= MAX_INIT_ARGS)
442 break;
443 argv_init[++args] = line;
444 }
445 }
446 argv_init[args+1] = NULL;
447 envp_init[envs+1] = NULL;
448 }
449
450 extern void setup_arch(char **, unsigned long *, unsigned long *);
451
452 #ifdef __SMP__
453
454
455
456
457 asmlinkage void start_secondary(void)
458 {
459 trap_init();
460 init_IRQ();
461 smp_callin();
462 for(;;)
463 idle();
464 }
465
466 int smp_idle(void * unused)
467 {
468 for (;;)
469 idle();
470 }
471
472
473
474
475
476 static void smp_init(void)
477 {
478 int i=0;
479 smp_boot_cpus();
480
481
482
483
484
485 for(i=1;i<smp_num_cpus;i++)
486 {
487 kernel_thread(smp_idle, NULL, CLONE_PID);
488
489
490
491 current_set[i]=task[i];
492 current_set[i]->processor=i;
493 }
494 }
495
496
497
498
499
500
501
502 static void smp_begin(void)
503 {
504 smp_threads_ready=1;
505 smp_commence();
506 }
507
508 #endif
509
510
511
512
513
514 asmlinkage void start_kernel(void)
515 {
516 char * command_line;
517
518
519
520
521
522 #ifdef __SMP__
523 static int first_cpu=1;
524
525 if(!first_cpu)
526 start_secondary();
527 first_cpu=0;
528
529 #endif
530
531
532
533
534 setup_arch(&command_line, &memory_start, &memory_end);
535 memory_start = paging_init(memory_start,memory_end);
536 trap_init();
537 init_IRQ();
538 sched_init();
539 time_init();
540 parse_options(command_line);
541 init_modules();
542 #ifdef CONFIG_PROFILE
543 if (!prof_shift)
544 #ifdef CONFIG_PROFILE_SHIFT
545 prof_shift = CONFIG_PROFILE_SHIFT;
546 #else
547 prof_shift = 2;
548 #endif
549 #endif
550 if (prof_shift) {
551 prof_buffer = (unsigned long *) memory_start;
552
553 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
554 prof_len >>= prof_shift;
555 memory_start += prof_len * sizeof(unsigned long);
556 }
557 memory_start = console_init(memory_start,memory_end);
558 #ifdef CONFIG_PCI
559 memory_start = pci_init(memory_start,memory_end);
560 #endif
561 memory_start = kmalloc_init(memory_start,memory_end);
562 sti();
563 calibrate_delay();
564 memory_start = inode_init(memory_start,memory_end);
565 memory_start = file_table_init(memory_start,memory_end);
566 memory_start = name_cache_init(memory_start,memory_end);
567 if (ramdisk_size)
568 memory_start += rd_init(memory_start, ramdisk_size*1024);
569 mem_init(memory_start,memory_end);
570 buffer_init();
571 sock_init();
572 #ifdef CONFIG_SYSVIPC
573 ipc_init();
574 #endif
575 sti();
576 check_bugs();
577
578 printk(linux_banner);
579 #ifdef __SMP__
580 smp_init();
581 #endif
582
583 kernel_thread(init, NULL, 0);
584
585
586
587
588
589
590
591
592
593 for(;;)
594 idle();
595 }
596
597 static int printf(const char *fmt, ...)
598 {
599 va_list args;
600 int i;
601
602 va_start(args, fmt);
603 write(1,printbuf,i=vsprintf(printbuf, fmt, args));
604 va_end(args);
605 return i;
606 }
607
608 static int do_rc(void * rc)
609 {
610 close(0);
611 if (open(rc,O_RDONLY,0))
612 return -1;
613 return execve("/bin/sh", argv_rc, envp_rc);
614 }
615
616 static int do_shell(void * shell)
617 {
618 close(0);close(1);close(2);
619 setsid();
620 (void) open("/dev/tty1",O_RDWR,0);
621 (void) dup(0);
622 (void) dup(0);
623 return execve(shell, argv, envp);
624 }
625
626 static int init(void * unused)
627 {
628 int pid,i;
629
630 setup();
631
632 #ifdef __SMP__
633
634
635
636
637
638 smp_begin();
639 #endif
640
641 #ifdef CONFIG_UMSDOS_FS
642 {
643
644
645
646
647
648 extern struct inode *pseudo_root;
649 if (pseudo_root != NULL){
650 current->fs->root = pseudo_root;
651 current->fs->pwd = pseudo_root;
652 }
653 }
654 #endif
655
656 (void) open("/dev/tty1",O_RDWR,0);
657 (void) dup(0);
658 (void) dup(0);
659
660 if (!execute_command) {
661 execve("/etc/init",argv_init,envp_init);
662 execve("/bin/init",argv_init,envp_init);
663 execve("/sbin/init",argv_init,envp_init);
664
665
666 pid = kernel_thread(do_rc, "/etc/rc", SIGCHLD);
667 if (pid>0)
668 while (pid != wait(&i))
669 ;
670 }
671
672 while (1) {
673 pid = kernel_thread(do_shell,
674 execute_command ? execute_command : "/bin/sh",
675 SIGCHLD);
676 if (pid < 0) {
677 printf("Fork failed in init\n\r");
678 continue;
679 }
680 while (1)
681 if (pid == wait(&i))
682 break;
683 printf("\n\rchild %d died with code %04x\n\r",pid,i);
684 sync();
685 }
686 return -1;
687 }