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

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