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                 ticks = jiffies;
 257                 __delay(loops_per_sec);
 258                 ticks = jiffies - ticks;
 259                 if (ticks >= HZ) {
 260                         __asm__("mull %1 ; divl %2"
 261                                 :"=a" (loops_per_sec)
 262                                 :"d" (HZ),
 263                                  "r" (ticks),
 264                                  "0" (loops_per_sec)
 265                                 :"dx");
 266                         printk("ok - %lu.%02lu BogoMips\n",
 267                                 loops_per_sec/500000,
 268                                 (loops_per_sec/5000) % 100);
 269                         return;
 270                 }
 271         }
 272         printk("failed\n");
 273 }
 274         
 275 
 276 /*
 277  * This is a simple kernel command line parsing function: it parses
 278  * the command line, and fills in the arguments/environment to init
 279  * as appropriate. Any cmd-line option is taken to be an environment
 280  * variable if it contains the character '='.
 281  *
 282  *
 283  * This routine also checks for options meant for the kernel - currently
 284  * only the "root=XXXX" option is recognized. These options are not given
 285  * to init - they are for internal kernel use only.
 286  */
 287 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         char *next;
 290         char *devnames[] = { "hda", "hdb", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 291         int devnums[]    = { 0x300, 0x340, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 292         int args, envs;
 293 
 294         if (!*line)
 295                 return;
 296         args = 0;
 297         envs = 1;       /* TERM is set to 'console' by default */
 298         next = line;
 299         while ((line = next) != NULL) {
 300                 if ((next = strchr(line,' ')) != NULL)
 301                         *next++ = 0;
 302                 /*
 303                  * check for kernel options first..
 304                  */
 305                 if (!strncmp(line,"root=",5)) {
 306                         int n;
 307                         line += 5;
 308                         if (strncmp(line,"/dev/",5)) {
 309                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 310                                 continue;
 311                         }
 312                         line += 5;
 313                         for (n = 0 ; devnames[n] ; n++) {
 314                                 int len = strlen(devnames[n]);
 315                                 if (!strncmp(line,devnames[n],len)) {
 316                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,16);
 317                                         break;
 318                                 }
 319                         }
 320                         continue;
 321                 }
 322                 if (!strcmp(line,"ro")) {
 323                         root_mountflags |= MS_RDONLY;
 324                         continue;
 325                 }
 326                 if (!strcmp(line,"rw")) {
 327                         root_mountflags &= ~MS_RDONLY;
 328                         continue;
 329                 }
 330                 if (!strcmp(line,"debug")) {
 331                         console_loglevel = 10;
 332                         continue;
 333                 }
 334                 if (!strcmp(line,"no-hlt")) {
 335                         hlt_works_ok = 0;
 336                         continue;
 337                 }
 338                 if (!strcmp(line,"no387")) {
 339                         hard_math = 0;
 340                         __asm__("movl %%cr0,%%eax\n\t"
 341                                 "orl $0xE,%%eax\n\t"
 342                                 "movl %%eax,%%cr0\n\t" : : : "ax");
 343                         continue;
 344                 }
 345                 if (checksetup(line))
 346                         continue;
 347                 /*
 348                  * Then check if it's an environment variable or
 349                  * an option.
 350                  */     
 351                 if (strchr(line,'=')) {
 352                         if (envs >= MAX_INIT_ENVS)
 353                                 break;
 354                         envp_init[++envs] = line;
 355                 } else {
 356                         if (args >= MAX_INIT_ARGS)
 357                                 break;
 358                         argv_init[++args] = line;
 359                 }
 360         }
 361         argv_init[args+1] = NULL;
 362         envp_init[envs+1] = NULL;
 363 }
 364 
 365 static void copy_options(char * to, char * from)
     /* [previous][next][first][last][top][bottom][index][help] */
 366 {
 367         char c = ' ';
 368         int len = 0;
 369 
 370         for (;;) {
 371                 if (c == ' ' && *(unsigned long *)from == *(unsigned long *)"mem=")
 372                         memory_end = simple_strtoul(from+4, &from, 0);
 373                 c = *(from++);
 374                 if (!c)
 375                         break;
 376                 if (COMMAND_LINE_SIZE <= ++len)
 377                         break;
 378                 *(to++) = c;
 379         }
 380         *to = '\0';
 381 }
 382 
 383 static void copro_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 384 {
 385         fpu_error = 1;
 386         timer_table[COPRO_TIMER].expires = jiffies+100;
 387         timer_active |= 1<<COPRO_TIMER;
 388         printk("387 failed: trying to reset\n");
 389         send_sig(SIGFPE, last_task_used_math, 1);
 390         outb_p(0,0xf1);
 391         outb_p(0,0xf0);
 392 }
 393 
 394 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 395 {
 396 /*
 397  * Interrupts are still disabled. Do necessary setups, then
 398  * enable them
 399  */
 400         set_call_gate(&default_ldt,lcall7);
 401         ROOT_DEV = ORIG_ROOT_DEV;
 402         drive_info = DRIVE_INFO;
 403         screen_info = SCREEN_INFO;
 404         aux_device_present = AUX_DEVICE_INFO;
 405         memory_end = (1<<20) + (EXT_MEM_K<<10);
 406         memory_end &= PAGE_MASK;
 407         ramdisk_size = RAMDISK_SIZE;
 408         copy_options(command_line,COMMAND_LINE);
 409 #ifdef CONFIG_MAX_16M
 410         if (memory_end > 16*1024*1024)
 411                 memory_end = 16*1024*1024;
 412 #endif
 413         if (MOUNT_ROOT_RDONLY)
 414                 root_mountflags |= MS_RDONLY;
 415         if ((unsigned long)&end >= (1024*1024)) {
 416                 memory_start = (unsigned long) &end;
 417                 low_memory_start = PAGE_SIZE;
 418         } else {
 419                 memory_start = 1024*1024;
 420                 low_memory_start = (unsigned long) &end;
 421         }
 422         low_memory_start = PAGE_ALIGN(low_memory_start);
 423         memory_start = paging_init(memory_start,memory_end);
 424         if (strncmp((char*)0x0FFFD9, "EISA", 4) == 0)
 425                 EISA_bus = 1;
 426         trap_init();
 427         init_IRQ();
 428         sched_init();
 429         parse_options(command_line);
 430         init_modules();
 431 #ifdef CONFIG_PROFILE
 432         prof_buffer = (unsigned long *) memory_start;
 433         prof_len = (unsigned long) &end;
 434         prof_len >>= 2;
 435         memory_start += prof_len * sizeof(unsigned long);
 436 #endif
 437         memory_start = console_init(memory_start,memory_end);
 438         memory_start = bios32_init(memory_start,memory_end);
 439         memory_start = kmalloc_init(memory_start,memory_end);
 440         memory_start = chr_dev_init(memory_start,memory_end);
 441         memory_start = blk_dev_init(memory_start,memory_end);
 442         sti();
 443         calibrate_delay();
 444 #ifdef CONFIG_INET
 445         memory_start = net_dev_init(memory_start,memory_end);
 446 #endif
 447 #ifdef CONFIG_SCSI
 448         memory_start = scsi_dev_init(memory_start,memory_end);
 449 #endif
 450         memory_start = inode_init(memory_start,memory_end);
 451         memory_start = file_table_init(memory_start,memory_end);
 452         memory_start = name_cache_init(memory_start,memory_end);
 453         mem_init(low_memory_start,memory_start,memory_end);
 454         buffer_init();
 455         time_init();
 456         floppy_init();
 457         sock_init();
 458 #ifdef CONFIG_SYSVIPC
 459         ipc_init();
 460 #endif
 461         sti();
 462         
 463         /*
 464          * check if exception 16 works correctly.. This is truly evil
 465          * code: it disables the high 8 interrupts to make sure that
 466          * the irq13 doesn't happen. But as this will lead to a lockup
 467          * if no exception16 arrives, it depends on the fact that the
 468          * high 8 interrupts will be re-enabled by the next timer tick.
 469          * So the irq13 will happen eventually, but the exception 16
 470          * should get there first..
 471          */
 472         if (hard_math) {
 473                 unsigned short control_word;
 474 
 475                 printk("Checking 386/387 coupling... ");
 476                 timer_table[COPRO_TIMER].expires = jiffies+50;
 477                 timer_table[COPRO_TIMER].fn = copro_timeout;
 478                 timer_active |= 1<<COPRO_TIMER;
 479                 __asm__("clts ; fninit ; fnstcw %0 ; fwait":"=m" (*&control_word));
 480                 control_word &= 0xffc0;
 481                 __asm__("fldcw %0 ; fwait": :"m" (*&control_word));
 482                 outb_p(inb_p(0x21) | (1 << 2), 0x21);
 483                 __asm__("fldz ; fld1 ; fdiv %st,%st(1) ; fwait");
 484                 timer_active &= ~(1<<COPRO_TIMER);
 485                 if (!fpu_error)
 486                         printk("Ok, fpu using %s error reporting.\n",
 487                                 ignore_irq13?"exception 16":"irq13");
 488         }
 489 #ifndef CONFIG_MATH_EMULATION
 490         else {
 491                 printk("No coprocessor found and no math emulation present.\n");
 492                 printk("Giving up.\n");
 493                 for (;;) ;
 494         }
 495 #endif
 496         if (hlt_works_ok) {
 497                 printk("Checking 'hlt' instruction... ");
 498                 __asm__ __volatile__("hlt ; hlt ; hlt ; hlt");
 499                 printk("Ok.\n");
 500         }
 501 
 502         system_utsname.machine[1] = '0' + x86;
 503         printk(linux_banner);
 504 
 505         move_to_user_mode();
 506         if (!fork())            /* we count on this going ok */
 507                 init();
 508 /*
 509  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 510  * it might do some general things like count free pages or it could be
 511  * used to implement a reasonable LRU algorithm for the paging routines:
 512  * anything that can be useful, but shouldn't take time from the real
 513  * processes.
 514  *
 515  * Right now task[0] just does a infinite idle loop.
 516  */
 517         for(;;)
 518                 idle();
 519 }
 520 
 521 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 522 {
 523         va_list args;
 524         int i;
 525 
 526         va_start(args, fmt);
 527         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 528         va_end(args);
 529         return i;
 530 }
 531 
 532 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 533 {
 534         int pid,i;
 535 
 536         setup();
 537         sprintf(term, "TERM=con%dx%d", ORIG_VIDEO_COLS, ORIG_VIDEO_LINES);
 538 
 539         #ifdef CONFIG_UMSDOS_FS
 540         {
 541                 /*
 542                         When mounting a umsdos fs as root, we detect
 543                         the pseudo_root (/linux) and initialise it here.
 544                         pseudo_root is defined in fs/umsdos/inode.c
 545                 */
 546                 extern struct inode *pseudo_root;
 547                 if (pseudo_root != NULL){
 548                         current->fs->root = pseudo_root;
 549                         current->fs->pwd  = pseudo_root;
 550                 }
 551         }
 552         #endif
 553 
 554         (void) open("/dev/tty1",O_RDWR,0);
 555         (void) dup(0);
 556         (void) dup(0);
 557 
 558         execve("/etc/init",argv_init,envp_init);
 559         execve("/bin/init",argv_init,envp_init);
 560         execve("/sbin/init",argv_init,envp_init);
 561         /* if this fails, fall through to original stuff */
 562 
 563         if (!(pid=fork())) {
 564                 close(0);
 565                 if (open("/etc/rc",O_RDONLY,0))
 566                         _exit(1);
 567                 execve("/bin/sh",argv_rc,envp_rc);
 568                 _exit(2);
 569         }
 570         if (pid>0)
 571                 while (pid != wait(&i))
 572                         /* nothing */;
 573         while (1) {
 574                 if ((pid = fork()) < 0) {
 575                         printf("Fork failed in init\n\r");
 576                         continue;
 577                 }
 578                 if (!pid) {
 579                         close(0);close(1);close(2);
 580                         setsid();
 581                         (void) open("/dev/tty1",O_RDWR,0);
 582                         (void) dup(0);
 583                         (void) dup(0);
 584                         _exit(execve("/bin/sh",argv,envp));
 585                 }
 586                 while (1)
 587                         if (pid == wait(&i))
 588                                 break;
 589                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 590                 sync();
 591         }
 592         _exit(0);
 593 }

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