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

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