root/init/main.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _syscall0
  2. get_options
  3. ramdisk_setup
  4. checksetup
  5. calibrate_delay
  6. parse_options
  7. copy_options
  8. copro_timeout
  9. start_kernel
  10. printf
  11. init

   1 /*
   2  *  linux/init/main.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <stdarg.h>
   8 
   9 #include <asm/system.h>
  10 #include <asm/io.h>
  11 
  12 #include <linux/types.h>
  13 #include <linux/fcntl.h>
  14 #include <linux/config.h>
  15 #include <linux/sched.h>
  16 #include <linux/tty.h>
  17 #include <linux/head.h>
  18 #include <linux/unistd.h>
  19 #include <linux/string.h>
  20 #include <linux/timer.h>
  21 #include <linux/fs.h>
  22 #include <linux/ctype.h>
  23 #include <linux/delay.h>
  24 #include <linux/utsname.h>
  25 #include <linux/ioport.h>
  26 
  27 extern unsigned long * prof_buffer;
  28 extern unsigned long prof_len;
  29 extern char edata, end;
  30 extern char *linux_banner;
  31 asmlinkage void lcall7(void);
  32 struct desc_struct default_ldt;
  33 
  34 /*
  35  * we need this inline - forking from kernel space will result
  36  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  37  * is no problem, but for the stack. This is handled by not letting
  38  * main() use the stack at all after fork(). Thus, no function
  39  * calls - which means inline code for fork too, as otherwise we
  40  * would use the stack upon exit from 'fork()'.
  41  *
  42  * Actually only pause and fork are needed inline, so that there
  43  * won't be any messing with the stack from main(), but we define
  44  * some others too.
  45  */
  46 #define __NR__exit __NR_exit
  47 static inline _syscall0(int,idle)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 static inline _syscall0(int,fork)
  49 static inline _syscall0(int,pause)
  50 static inline _syscall0(int,setup)
  51 static inline _syscall0(int,sync)
  52 static inline _syscall0(pid_t,setsid)
  53 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  54 static inline _syscall1(int,dup,int,fd)
  55 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  56 static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  57 static inline _syscall1(int,close,int,fd)
  58 static inline _syscall1(int,_exit,int,exitcode)
  59 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  60 
  61 static inline pid_t wait(int * wait_stat)
  62 {
  63         return waitpid(-1,wait_stat,0);
  64 }
  65 
  66 static char printbuf[1024];
  67 
  68 extern int console_loglevel;
  69 
  70 extern char empty_zero_page[PAGE_SIZE];
  71 extern void init(void);
  72 extern void init_IRQ(void);
  73 extern void init_modules(void);
  74 extern long console_init(long, long);
  75 extern long kmalloc_init(long,long);
  76 extern long blk_dev_init(long,long);
  77 extern long chr_dev_init(long,long);
  78 extern void floppy_init(void);
  79 extern void sock_init(void);
  80 extern long rd_init(long mem_start, int length);
  81 unsigned long net_dev_init(unsigned long, unsigned long);
  82 extern long bios32_init(long, long);
  83 
  84 extern void hd_setup(char *str, int *ints);
  85 extern void bmouse_setup(char *str, int *ints);
  86 extern void eth_setup(char *str, int *ints);
  87 extern void xd_setup(char *str, int *ints);
  88 extern void mcd_setup(char *str, int *ints);
  89 extern void st_setup(char *str, int *ints);
  90 extern void st0x_setup(char *str, int *ints);
  91 extern void tmc8xx_setup(char *str, int *ints);
  92 extern void t128_setup(char *str, int *ints);
  93 extern void pas16_setup(char *str, int *ints);
  94 extern void generic_NCR5380_setup(char *str, int *intr);
  95 extern void aha152x_setup(char *str, int *ints);
  96 extern void aha274x_setup(char *str, int *ints);
  97 extern void scsi_luns_setup(char *str, int *ints);
  98 extern void sound_setup(char *str, int *ints);
  99 #ifdef CONFIG_SBPCD
 100 extern void sbpcd_setup(char *str, int *ints);
 101 #endif CONFIG_SBPCD
 102 #ifdef CONFIG_CDU31A
 103 extern void cdu31a_setup(char *str, int *ints);
 104 #endif CONFIG_CDU31A
 105 void ramdisk_setup(char *str, int *ints);
 106 
 107 #ifdef CONFIG_SYSVIPC
 108 extern void ipc_init(void);
 109 #endif
 110 #ifdef CONFIG_SCSI
 111 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
 112 #endif
 113 
 114 /*
 115  * This is set up by the setup-routine at boot-time
 116  */
 117 #define PARAM   empty_zero_page
 118 #define EXT_MEM_K (*(unsigned short *) (PARAM+2))
 119 #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
 120 #define SCREEN_INFO (*(struct screen_info *) (PARAM+0))
 121 #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
 122 #define RAMDISK_SIZE (*(unsigned short *) (PARAM+0x1F8))
 123 #define ORIG_ROOT_DEV (*(unsigned short *) (PARAM+0x1FC))
 124 #define AUX_DEVICE_INFO (*(unsigned char *) (PARAM+0x1FF))
 125 
 126 /*
 127  * Boot command-line arguments
 128  */
 129 #define MAX_INIT_ARGS 8
 130 #define MAX_INIT_ENVS 8
 131 #define COMMAND_LINE ((char *) (PARAM+2048))
 132 #define COMMAND_LINE_SIZE 256
 133 
 134 extern void time_init(void);
 135 
 136 static unsigned long memory_start = 0;  /* After mem_init, stores the */
 137                                         /* amount of free user memory */
 138 static unsigned long memory_end = 0;
 139 static unsigned long low_memory_start = 0;
 140 
 141 static char term[21];
 142 int rows, cols;
 143 
 144 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 145 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", term, NULL, };
 146 
 147 static char * argv_rc[] = { "/bin/sh", NULL };
 148 static char * envp_rc[] = { "HOME=/", term, NULL };
 149 
 150 static char * argv[] = { "-/bin/sh",NULL };
 151 static char * envp[] = { "HOME=/usr/root", term, NULL };
 152 
 153 struct drive_info_struct { char dummy[32]; } drive_info;
 154 struct screen_info screen_info;
 155 
 156 unsigned char aux_device_present;
 157 int ramdisk_size;
 158 int root_mountflags = 0;
 159 
 160 static char fpu_error = 0;
 161 
 162 static char command_line[COMMAND_LINE_SIZE] = { 0, };
 163 
 164 char *get_options(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166         char *cur = str;
 167         int i=1;
 168 
 169         while (cur && isdigit(*cur) && i <= 10) {
 170                 ints[i++] = simple_strtoul(cur,NULL,0);
 171                 if ((cur = strchr(cur,',')) != NULL)
 172                         cur++;
 173         }
 174         ints[0] = i-1;
 175         return(cur);
 176 }
 177 
 178 struct {
 179         char *str;
 180         void (*setup_func)(char *, int *);
 181 } bootsetups[] = {
 182         { "reserve=", reserve_setup },
 183         { "ramdisk=", ramdisk_setup },
 184 #ifdef CONFIG_INET
 185         { "ether=", eth_setup },
 186 #endif
 187 #ifdef CONFIG_SCSI
 188         { "max_scsi_luns=", scsi_luns_setup },
 189 #endif
 190 #ifdef CONFIG_BLK_DEV_HD
 191         { "hd=", hd_setup },
 192 #endif
 193 #ifdef CONFIG_CHR_DEV_ST
 194         { "st=", st_setup },
 195 #endif
 196 #ifdef CONFIG_BUSMOUSE
 197         { "bmouse=", bmouse_setup },
 198 #endif
 199 #ifdef CONFIG_SCSI_SEAGATE
 200         { "st0x=", st0x_setup },
 201         { "tmc8xx=", tmc8xx_setup },
 202 #endif
 203 #ifdef CONFIG_SCSI_T128
 204         { "t128=", t128_setup },
 205 #endif
 206 #ifdef CONFIG_SCSI_PAS16
 207         { "pas16=", pas16_setup },
 208 #endif
 209 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 210         { "ncr5380=", generic_NCR5380_setup },
 211 #endif
 212 #ifdef CONFIG_SCSI_AHA152X
 213         { "aha152x=", aha152x_setup},
 214 #endif
 215 #ifdef CONFIG_SCSI_AHA274X
 216         { "aha274x=", aha274x_setup},
 217 #endif
 218 #ifdef CONFIG_BLK_DEV_XD
 219         { "xd=", xd_setup },
 220 #endif
 221 #ifdef CONFIG_MCD
 222         { "mcd=", mcd_setup },
 223 #endif
 224 #ifdef CONFIG_SOUND
 225         { "sound=", sound_setup },
 226 #endif
 227 #ifdef CONFIG_SBPCD
 228         { "sbpcd=", sbpcd_setup },
 229 #endif CONFIG_SBPCD
 230 #ifdef CONFIG_CDU31A
 231         { "cdu31a=", cdu31a_setup },
 232 #endif CONFIG_CDU31A
 233         { 0, 0 }
 234 };
 235 
 236 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 237 {
 238    if (ints[0] > 0 && ints[1] >= 0)
 239       ramdisk_size = ints[1];
 240 }
 241 
 242 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244         int i = 0;
 245         int ints[11];
 246 
 247         while (bootsetups[i].str) {
 248                 int n = strlen(bootsetups[i].str);
 249                 if (!strncmp(line,bootsetups[i].str,n)) {
 250                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 251                         return 1;
 252                 }
 253                 i++;
 254         }
 255         return 0;
 256 }
 257 
 258 unsigned long loops_per_sec = 1;
 259 
 260 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         int ticks;
 263 
 264         printk("Calibrating delay loop.. ");
 265         while (loops_per_sec <<= 1) {
 266                 /* wait for "start of" clock tick */
 267                 ticks = jiffies;
 268                 while (ticks == jiffies)
 269                         /* nothing */;
 270                 /* Go .. */
 271                 ticks = jiffies;
 272                 __delay(loops_per_sec);
 273                 ticks = jiffies - ticks;
 274                 if (ticks >= HZ) {
 275                         __asm__("mull %1 ; divl %2"
 276                                 :"=a" (loops_per_sec)
 277                                 :"d" (HZ),
 278                                  "r" (ticks),
 279                                  "0" (loops_per_sec)
 280                                 :"dx");
 281                         printk("ok - %lu.%02lu BogoMips\n",
 282                                 loops_per_sec/500000,
 283                                 (loops_per_sec/5000) % 100);
 284                         return;
 285                 }
 286         }
 287         printk("failed\n");
 288 }
 289 
 290 
 291 /*
 292  * This is a simple kernel command line parsing function: it parses
 293  * the command line, and fills in the arguments/environment to init
 294  * as appropriate. Any cmd-line option is taken to be an environment
 295  * variable if it contains the character '='.
 296  *
 297  *
 298  * This routine also checks for options meant for the kernel - currently
 299  * only the "root=XXXX" option is recognized. These options are not given
 300  * to init - they are for internal kernel use only.
 301  */
 302 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {
 304         char *next;
 305         char *devnames[] = { "hda", "hdb", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 306         int devnums[]    = { 0x300, 0x340, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 307         int args, envs;
 308 
 309         if (!*line)
 310                 return;
 311         args = 0;
 312         envs = 1;       /* TERM is set to 'console' by default */
 313         next = line;
 314         while ((line = next) != NULL) {
 315                 if ((next = strchr(line,' ')) != NULL)
 316                         *next++ = 0;
 317                 /*
 318                  * check for kernel options first..
 319                  */
 320                 if (!strncmp(line,"root=",5)) {
 321                         int n;
 322                         line += 5;
 323                         if (strncmp(line,"/dev/",5)) {
 324                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 325                                 continue;
 326                         }
 327                         line += 5;
 328                         for (n = 0 ; devnames[n] ; n++) {
 329                                 int len = strlen(devnames[n]);
 330                                 if (!strncmp(line,devnames[n],len)) {
 331                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,0);
 332                                         break;
 333                                 }
 334                         }
 335                         continue;
 336                 }
 337                 if (!strcmp(line,"ro")) {
 338                         root_mountflags |= MS_RDONLY;
 339                         continue;
 340                 }
 341                 if (!strcmp(line,"rw")) {
 342                         root_mountflags &= ~MS_RDONLY;
 343                         continue;
 344                 }
 345                 if (!strcmp(line,"debug")) {
 346                         console_loglevel = 10;
 347                         continue;
 348                 }
 349                 if (!strcmp(line,"no-hlt")) {
 350                         hlt_works_ok = 0;
 351                         continue;
 352                 }
 353                 if (!strcmp(line,"no387")) {
 354                         hard_math = 0;
 355                         __asm__("movl %%cr0,%%eax\n\t"
 356                                 "orl $0xE,%%eax\n\t"
 357                                 "movl %%eax,%%cr0\n\t" : : : "ax");
 358                         continue;
 359                 }
 360                 if (checksetup(line))
 361                         continue;
 362                 /*
 363                  * Then check if it's an environment variable or
 364                  * an option.
 365                  */
 366                 if (strchr(line,'=')) {
 367                         if (envs >= MAX_INIT_ENVS)
 368                                 break;
 369                         envp_init[++envs] = line;
 370                 } else {
 371                         if (args >= MAX_INIT_ARGS)
 372                                 break;
 373                         argv_init[++args] = line;
 374                 }
 375         }
 376         argv_init[args+1] = NULL;
 377         envp_init[envs+1] = NULL;
 378 }
 379 
 380 static void copy_options(char * to, char * from)
     /* [previous][next][first][last][top][bottom][index][help] */
 381 {
 382         char c = ' ';
 383         int len = 0;
 384 
 385         for (;;) {
 386                 if (c == ' ' && *(unsigned long *)from == *(unsigned long *)"mem=")
 387                         memory_end = simple_strtoul(from+4, &from, 0);
 388                 c = *(from++);
 389                 if (!c)
 390                         break;
 391                 if (COMMAND_LINE_SIZE <= ++len)
 392                         break;
 393                 *(to++) = c;
 394         }
 395         *to = '\0';
 396 }
 397 
 398 static void copro_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 399 {
 400         fpu_error = 1;
 401         timer_table[COPRO_TIMER].expires = jiffies+100;
 402         timer_active |= 1<<COPRO_TIMER;
 403         printk("387 failed: trying to reset\n");
 404         send_sig(SIGFPE, last_task_used_math, 1);
 405         outb_p(0,0xf1);
 406         outb_p(0,0xf0);
 407 }
 408 
 409 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 410 {
 411 /*
 412  * Interrupts are still disabled. Do necessary setups, then
 413  * enable them
 414  */
 415         set_call_gate(&default_ldt,lcall7);
 416         ROOT_DEV = ORIG_ROOT_DEV;
 417         drive_info = DRIVE_INFO;
 418         screen_info = SCREEN_INFO;
 419         aux_device_present = AUX_DEVICE_INFO;
 420         memory_end = (1<<20) + (EXT_MEM_K<<10);
 421         memory_end &= PAGE_MASK;
 422         ramdisk_size = RAMDISK_SIZE;
 423         copy_options(command_line,COMMAND_LINE);
 424 #ifdef CONFIG_MAX_16M
 425         if (memory_end > 16*1024*1024)
 426                 memory_end = 16*1024*1024;
 427 #endif
 428         if (MOUNT_ROOT_RDONLY)
 429                 root_mountflags |= MS_RDONLY;
 430         if ((unsigned long)&end >= (1024*1024)) {
 431                 memory_start = (unsigned long) &end;
 432                 low_memory_start = PAGE_SIZE;
 433         } else {
 434                 memory_start = 1024*1024;
 435                 low_memory_start = (unsigned long) &end;
 436         }
 437         low_memory_start = PAGE_ALIGN(low_memory_start);
 438         memory_start = paging_init(memory_start,memory_end);
 439         if (strncmp((char*)0x0FFFD9, "EISA", 4) == 0)
 440                 EISA_bus = 1;
 441         trap_init();
 442         init_IRQ();
 443         sched_init();
 444         parse_options(command_line);
 445         init_modules();
 446 #ifdef CONFIG_PROFILE
 447         prof_buffer = (unsigned long *) memory_start;
 448         prof_len = (unsigned long) &end;
 449         prof_len >>= 2;
 450         memory_start += prof_len * sizeof(unsigned long);
 451 #endif
 452         memory_start = console_init(memory_start,memory_end);
 453         memory_start = bios32_init(memory_start,memory_end);
 454         memory_start = kmalloc_init(memory_start,memory_end);
 455         memory_start = chr_dev_init(memory_start,memory_end);
 456         memory_start = blk_dev_init(memory_start,memory_end);
 457         sti();
 458         calibrate_delay();
 459 #ifdef CONFIG_INET
 460         memory_start = net_dev_init(memory_start,memory_end);
 461 #endif
 462 #ifdef CONFIG_SCSI
 463         memory_start = scsi_dev_init(memory_start,memory_end);
 464 #endif
 465         memory_start = inode_init(memory_start,memory_end);
 466         memory_start = file_table_init(memory_start,memory_end);
 467         memory_start = name_cache_init(memory_start,memory_end);
 468         mem_init(low_memory_start,memory_start,memory_end);
 469         buffer_init();
 470         time_init();
 471         floppy_init();
 472         sock_init();
 473 #ifdef CONFIG_SYSVIPC
 474         ipc_init();
 475 #endif
 476         sti();
 477 
 478         /*
 479          * check if exception 16 works correctly.. This is truly evil
 480          * code: it disables the high 8 interrupts to make sure that
 481          * the irq13 doesn't happen. But as this will lead to a lockup
 482          * if no exception16 arrives, it depends on the fact that the
 483          * high 8 interrupts will be re-enabled by the next timer tick.
 484          * So the irq13 will happen eventually, but the exception 16
 485          * should get there first..
 486          */
 487         if (hard_math) {
 488                 unsigned short control_word;
 489 
 490                 printk("Checking 386/387 coupling... ");
 491                 timer_table[COPRO_TIMER].expires = jiffies+50;
 492                 timer_table[COPRO_TIMER].fn = copro_timeout;
 493                 timer_active |= 1<<COPRO_TIMER;
 494                 __asm__("clts ; fninit ; fnstcw %0 ; fwait":"=m" (*&control_word));
 495                 control_word &= 0xffc0;
 496                 __asm__("fldcw %0 ; fwait": :"m" (*&control_word));
 497                 outb_p(inb_p(0x21) | (1 << 2), 0x21);
 498                 __asm__("fldz ; fld1 ; fdiv %st,%st(1) ; fwait");
 499                 timer_active &= ~(1<<COPRO_TIMER);
 500                 if (!fpu_error)
 501                         printk("Ok, fpu using %s error reporting.\n",
 502                                 ignore_irq13?"exception 16":"irq13");
 503         }
 504 #ifndef CONFIG_MATH_EMULATION
 505         else {
 506                 printk("No coprocessor found and no math emulation present.\n");
 507                 printk("Giving up.\n");
 508                 for (;;) ;
 509         }
 510 #endif
 511         if (hlt_works_ok) {
 512                 printk("Checking 'hlt' instruction... ");
 513                 __asm__ __volatile__("hlt ; hlt ; hlt ; hlt");
 514                 printk("Ok.\n");
 515         }
 516 
 517         system_utsname.machine[1] = '0' + x86;
 518         printk(linux_banner);
 519 
 520         move_to_user_mode();
 521         if (!fork())            /* we count on this going ok */
 522                 init();
 523 /*
 524  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 525  * it might do some general things like count free pages or it could be
 526  * used to implement a reasonable LRU algorithm for the paging routines:
 527  * anything that can be useful, but shouldn't take time from the real
 528  * processes.
 529  *
 530  * Right now task[0] just does a infinite idle loop.
 531  */
 532         for(;;)
 533                 idle();
 534 }
 535 
 536 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 537 {
 538         va_list args;
 539         int i;
 540 
 541         va_start(args, fmt);
 542         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 543         va_end(args);
 544         return i;
 545 }
 546 
 547 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549         int pid,i;
 550 
 551         setup();
 552         sprintf(term, "TERM=con%dx%d", ORIG_VIDEO_COLS, ORIG_VIDEO_LINES);
 553 
 554         #ifdef CONFIG_UMSDOS_FS
 555         {
 556                 /*
 557                         When mounting a umsdos fs as root, we detect
 558                         the pseudo_root (/linux) and initialise it here.
 559                         pseudo_root is defined in fs/umsdos/inode.c
 560                 */
 561                 extern struct inode *pseudo_root;
 562                 if (pseudo_root != NULL){
 563                         current->fs->root = pseudo_root;
 564                         current->fs->pwd  = pseudo_root;
 565                 }
 566         }
 567         #endif
 568 
 569         (void) open("/dev/tty1",O_RDWR,0);
 570         (void) dup(0);
 571         (void) dup(0);
 572 
 573         execve("/etc/init",argv_init,envp_init);
 574         execve("/bin/init",argv_init,envp_init);
 575         execve("/sbin/init",argv_init,envp_init);
 576         /* if this fails, fall through to original stuff */
 577 
 578         if (!(pid=fork())) {
 579                 close(0);
 580                 if (open("/etc/rc",O_RDONLY,0))
 581                         _exit(1);
 582                 execve("/bin/sh",argv_rc,envp_rc);
 583                 _exit(2);
 584         }
 585         if (pid>0)
 586                 while (pid != wait(&i))
 587                         /* nothing */;
 588         while (1) {
 589                 if ((pid = fork()) < 0) {
 590                         printf("Fork failed in init\n\r");
 591                         continue;
 592                 }
 593                 if (!pid) {
 594                         close(0);close(1);close(2);
 595                         setsid();
 596                         (void) open("/dev/tty1",O_RDWR,0);
 597                         (void) dup(0);
 598                         (void) dup(0);
 599                         _exit(execve("/bin/sh",argv,envp));
 600                 }
 601                 while (1)
 602                         if (pid == wait(&i))
 603                                 break;
 604                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 605                 sync();
 606         }
 607         _exit(0);
 608 }

/* [previous][next][first][last][top][bottom][index][help] */