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

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