root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. _syscall0
  2. time_init
  3. get_options
  4. checksetup
  5. calibrate_delay
  6. parse_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/mktime.h>
  13 #include <linux/types.h>
  14 #include <linux/fcntl.h>
  15 #include <linux/config.h>
  16 #include <linux/sched.h>
  17 #include <linux/tty.h>
  18 #include <linux/head.h>
  19 #include <linux/unistd.h>
  20 #include <linux/string.h>
  21 #include <linux/timer.h>
  22 #include <linux/fs.h>
  23 #include <linux/ctype.h>
  24 #include <linux/delay.h>
  25 #include <linux/utsname.h>
  26 #include <linux/ioport.h>
  27 
  28 extern unsigned long * prof_buffer;
  29 extern unsigned long prof_len;
  30 extern char edata, end;
  31 extern char *linux_banner;
  32 asmlinkage void lcall7(void);
  33 struct desc_struct default_ldt;
  34 
  35 /*
  36  * we need this inline - forking from kernel space will result
  37  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  38  * is no problem, but for the stack. This is handled by not letting
  39  * main() use the stack at all after fork(). Thus, no function
  40  * calls - which means inline code for fork too, as otherwise we
  41  * would use the stack upon exit from 'fork()'.
  42  *
  43  * Actually only pause and fork are needed inline, so that there
  44  * won't be any messing with the stack from main(), but we define
  45  * some others too.
  46  */
  47 #define __NR__exit __NR_exit
  48 static inline _syscall0(int,idle)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 static inline _syscall0(int,fork)
  50 static inline _syscall0(int,pause)
  51 static inline _syscall1(int,setup,void *,BIOS)
  52 static inline _syscall0(int,sync)
  53 static inline _syscall0(pid_t,setsid)
  54 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  55 static inline _syscall1(int,dup,int,fd)
  56 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  57 static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  58 static inline _syscall1(int,close,int,fd)
  59 static inline _syscall1(int,_exit,int,exitcode)
  60 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  61 
  62 static inline pid_t wait(int * wait_stat)
  63 {
  64         return waitpid(-1,wait_stat,0);
  65 }
  66 
  67 static char printbuf[1024];
  68 
  69 extern char empty_zero_page[PAGE_SIZE];
  70 extern int vsprintf(char *,const char *,va_list);
  71 extern void init(void);
  72 extern void init_IRQ(void);
  73 extern long blk_dev_init(long,long);
  74 extern long chr_dev_init(long,long);
  75 extern void floppy_init(void);
  76 extern void sock_init(void);
  77 extern long rd_init(long mem_start, int length);
  78 extern long kernel_mktime(struct mktime * time);
  79 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  80 
  81 extern void hd_setup(char *str, int *ints);
  82 extern void bmouse_setup(char *str, int *ints);
  83 extern void eth_setup(char *str, int *ints);
  84 
  85 #ifdef CONFIG_SYSVIPC
  86 extern void ipc_init(void);
  87 #endif
  88 #ifdef CONFIG_SCSI
  89 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
  90 #endif
  91 
  92 /*
  93  * This is set up by the setup-routine at boot-time
  94  */
  95 #define PARAM   empty_zero_page
  96 #define EXT_MEM_K (*(unsigned short *) (PARAM+2))
  97 #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
  98 #define SCREEN_INFO (*(struct screen_info *) (PARAM+0))
  99 #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
 100 #define RAMDISK_SIZE (*(unsigned short *) (PARAM+0x1F8))
 101 #define ORIG_ROOT_DEV (*(unsigned short *) (PARAM+0x1FC))
 102 #define AUX_DEVICE_INFO (*(unsigned char *) (PARAM+0x1FF))
 103 
 104 /*
 105  * Boot command-line arguments
 106  */
 107 #define MAX_INIT_ARGS 8
 108 #define MAX_INIT_ENVS 8
 109 #define COMMAND_LINE ((char *) (PARAM+2048))
 110 
 111 /*
 112  * Yeah, yeah, it's ugly, but I cannot find how to do this correctly
 113  * and this seems to work. I anybody has more info on the real-time
 114  * clock I'd be interested. Most of this was trial and error, and some
 115  * bios-listing reading. Urghh.
 116  */
 117 
 118 #define CMOS_READ(addr) ({ \
 119 outb_p(addr,0x70); \
 120 inb_p(0x71); \
 121 })
 122 
 123 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
 124 
 125 static void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127         struct mktime time;
 128         int i;
 129 
 130         for (i = 0 ; i < 1000000 ; i++)
 131                 if (!(CMOS_READ(10) & 0x80))
 132                         break;
 133         do {
 134                 time.sec = CMOS_READ(0);
 135                 time.min = CMOS_READ(2);
 136                 time.hour = CMOS_READ(4);
 137                 time.day = CMOS_READ(7);
 138                 time.mon = CMOS_READ(8);
 139                 time.year = CMOS_READ(9);
 140         } while (time.sec != CMOS_READ(0));
 141         BCD_TO_BIN(time.sec);
 142         BCD_TO_BIN(time.min);
 143         BCD_TO_BIN(time.hour);
 144         BCD_TO_BIN(time.day);
 145         BCD_TO_BIN(time.mon);
 146         BCD_TO_BIN(time.year);
 147         time.mon--;
 148         startup_time = kernel_mktime(&time);
 149 }
 150 
 151 static unsigned long memory_start = 0;  /* After mem_init, stores the */
 152                                         /* amount of free user memory */
 153 static unsigned long memory_end = 0;
 154 static unsigned long low_memory_start = 0;
 155 
 156 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 157 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=console", NULL, };
 158 
 159 static char * argv_rc[] = { "/bin/sh", NULL };
 160 static char * envp_rc[] = { "HOME=/", "TERM=console", NULL };
 161 
 162 static char * argv[] = { "-/bin/sh",NULL };
 163 static char * envp[] = { "HOME=/usr/root", "TERM=console", NULL };
 164 
 165 struct drive_info_struct { char dummy[32]; } drive_info;
 166 struct screen_info screen_info;
 167 
 168 unsigned char aux_device_present;
 169 int ramdisk_size;
 170 int root_mountflags = 0;
 171 
 172 static char fpu_error = 0;
 173 
 174 static char command_line[80] = { 0, };
 175 
 176 char *get_options(char *str, int *ints) 
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178         char *cur = str;
 179         int i=1;
 180 
 181         while (cur && isdigit(*cur) && i <= 10) {
 182                 ints[i++] = simple_strtoul(cur,NULL,0);
 183                 if ((cur = strchr(cur,',')) != NULL)
 184                         cur++;
 185         }
 186         ints[0] = i-1;
 187         return(cur);
 188 }
 189 
 190 struct {
 191         char *str;
 192         void (*setup_func)(char *, int *);
 193 } bootsetups[] = {
 194         { "reserve=", reserve_setup },
 195 #ifdef CONFIG_INET
 196         { "ether=", eth_setup },
 197 #endif
 198 #ifdef CONFIG_BLK_DEV_HD
 199         { "hd=", hd_setup },
 200 #endif
 201 #ifdef CONFIG_BUSMOUSE
 202         { "bmouse=", bmouse_setup },
 203 #endif
 204         { 0, 0 }
 205 };
 206 
 207 int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 208 {
 209         int i = 0;
 210         int ints[11];
 211 
 212         while (bootsetups[i].str) {
 213                 int n = strlen(bootsetups[i].str);
 214                 if (!strncmp(line,bootsetups[i].str,n)) {
 215                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 216                         return(0);
 217                 }
 218                 i++;
 219         }
 220         return(1);
 221 }
 222 
 223 unsigned long loops_per_sec = 1;
 224 
 225 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 226 {
 227         int ticks;
 228 
 229         printk("Calibrating delay loop.. ");
 230         while (loops_per_sec <<= 1) {
 231                 ticks = jiffies;
 232                 __delay(loops_per_sec);
 233                 ticks = jiffies - ticks;
 234                 if (ticks >= HZ) {
 235                         __asm__("mull %1 ; divl %2"
 236                                 :"=a" (loops_per_sec)
 237                                 :"d" (HZ),
 238                                  "r" (ticks),
 239                                  "0" (loops_per_sec)
 240                                 :"dx");
 241                         printk("ok - %d.%02d BogoMips (tm)\n",
 242                                 loops_per_sec/500000,
 243                                 (loops_per_sec/5000) % 100);
 244                         return;
 245                 }
 246         }
 247         printk("failed\n");
 248 }
 249         
 250 
 251 /*
 252  * This is a simple kernel command line parsing function: it parses
 253  * the command line, and fills in the arguments/environment to init
 254  * as appropriate. Any cmd-line option is taken to be an environment
 255  * variable if it contains the character '='.
 256  *
 257  *
 258  * This routine also checks for options meant for the kernel - currently
 259  * only the "root=XXXX" option is recognized. These options are not given
 260  * to init - they are for internal kernel use only.
 261  */
 262 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264         char *next;
 265         char *devnames[] = { "hda", "hdb", "sda", "sdb", "sdc", "sdd", "sde", "fd", NULL };
 266         int devnums[]    = { 0x300, 0x340, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0};
 267         int args, envs;
 268 
 269         if (!*line)
 270                 return;
 271         args = 0;
 272         envs = 1;       /* TERM is set to 'console' by default */
 273         next = line;
 274         while ((line = next) != NULL) {
 275                 if ((next = strchr(line,' ')) != NULL)
 276                         *next++ = 0;
 277                 /*
 278                  * check for kernel options first..
 279                  */
 280                 if (!strncmp(line,"root=",5)) {
 281                         int n;
 282                         line += 5;
 283                         if (strncmp(line,"/dev/",5)) {
 284                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 285                                 continue;
 286                         }
 287                         line += 5;
 288                         for (n = 0 ; devnames[n] ; n++) {
 289                                 int len = strlen(devnames[n]);
 290                                 if (!strncmp(line,devnames[n],len)) {
 291                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,16);
 292                                         break;
 293                                 }
 294                         }
 295                 } else if (!strcmp(line,"ro"))
 296                         root_mountflags |= MS_RDONLY;
 297                 else if (!strcmp(line,"rw"))
 298                         root_mountflags &= ~MS_RDONLY;
 299                 else if (!strcmp(line,"no387")) {
 300                         hard_math = 0;
 301                         __asm__("movl %%cr0,%%eax\n\t"
 302                                 "orl $0xE,%%eax\n\t"
 303                                 "movl %%eax,%%cr0\n\t" : : : "ax");
 304                 } else
 305                         checksetup(line);
 306                 /*
 307                  * Then check if it's an environment variable or
 308                  * an option.
 309                  */     
 310                 if (strchr(line,'=')) {
 311                         if (envs >= MAX_INIT_ENVS)
 312                                 break;
 313                         envp_init[++envs] = line;
 314                 } else {
 315                         if (args >= MAX_INIT_ARGS)
 316                                 break;
 317                         argv_init[++args] = line;
 318                 }
 319         }
 320         argv_init[args+1] = NULL;
 321         envp_init[envs+1] = NULL;
 322 }
 323 
 324 static void copro_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326         fpu_error = 1;
 327         timer_table[COPRO_TIMER].expires = jiffies+100;
 328         timer_active |= 1<<COPRO_TIMER;
 329         printk("387 failed: trying to reset\n");
 330         send_sig(SIGFPE, last_task_used_math, 1);
 331         outb_p(0,0xf1);
 332         outb_p(0,0xf0);
 333 }
 334 
 335 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337 /*
 338  * Interrupts are still disabled. Do necessary setups, then
 339  * enable them
 340  */
 341         set_call_gate(&default_ldt,lcall7);
 342         ROOT_DEV = ORIG_ROOT_DEV;
 343         drive_info = DRIVE_INFO;
 344         screen_info = SCREEN_INFO;
 345         aux_device_present = AUX_DEVICE_INFO;
 346         memory_end = (1<<20) + (EXT_MEM_K<<10);
 347         memory_end &= PAGE_MASK;
 348         ramdisk_size = RAMDISK_SIZE;
 349         strcpy(command_line,COMMAND_LINE);
 350 #ifdef CONFIG_MAX_16M
 351         if (memory_end > 16*1024*1024)
 352                 memory_end = 16*1024*1024;
 353 #endif
 354         if (MOUNT_ROOT_RDONLY)
 355                 root_mountflags |= MS_RDONLY;
 356         if ((unsigned long)&end >= (1024*1024)) {
 357                 memory_start = (unsigned long) &end;
 358                 low_memory_start = PAGE_SIZE;
 359         } else {
 360                 memory_start = 1024*1024;
 361                 low_memory_start = (unsigned long) &end;
 362         }
 363         low_memory_start = PAGE_ALIGN(low_memory_start);
 364         memory_start = paging_init(memory_start,memory_end);
 365         trap_init();
 366         init_IRQ();
 367         sched_init();
 368         parse_options(command_line);
 369 #ifdef CONFIG_PROFILE
 370         prof_buffer = (unsigned long *) memory_start;
 371         prof_len = (unsigned long) &end;
 372         prof_len >>= 2;
 373         memory_start += prof_len * sizeof(unsigned long);
 374 #endif
 375         memory_start = chr_dev_init(memory_start,memory_end);
 376         memory_start = blk_dev_init(memory_start,memory_end);
 377 #ifdef CONFIG_SCSI
 378         memory_start = scsi_dev_init(memory_start,memory_end);
 379 #endif
 380         memory_start = inode_init(memory_start,memory_end);
 381         memory_start = file_table_init(memory_start,memory_end);
 382         mem_init(low_memory_start,memory_start,memory_end);
 383         buffer_init();
 384         time_init();
 385         floppy_init();
 386         sock_init();
 387 #ifdef CONFIG_SYSVIPC
 388         ipc_init();
 389 #endif
 390         sti();
 391         calibrate_delay();
 392         /*
 393          * check if exception 16 works correctly.. This is truly evil
 394          * code: it disables the high 8 interrupts to make sure that
 395          * the irq13 doesn't happen. But as this will lead to a lockup
 396          * if no exception16 arrives, it depends on the fact that the
 397          * high 8 interrupts will be re-enabled by the next timer tick.
 398          * So the irq13 will happen eventually, but the exception 16
 399          * should get there first..
 400          */
 401         if (hard_math) {
 402                 unsigned short control_word;
 403 
 404                 printk("Checking 386/387 coupling... ");
 405                 timer_table[COPRO_TIMER].expires = jiffies+50;
 406                 timer_table[COPRO_TIMER].fn = copro_timeout;
 407                 timer_active |= 1<<COPRO_TIMER;
 408                 __asm__("clts ; fninit ; fnstcw %0 ; fwait":"=m" (*&control_word));
 409                 control_word &= 0xffc0;
 410                 __asm__("fldcw %0 ; fwait": :"m" (*&control_word));
 411                 outb_p(inb_p(0x21) | (1 << 2), 0x21);
 412                 __asm__("fldz ; fld1 ; fdiv %st,%st(1) ; fwait");
 413                 timer_active &= ~(1<<COPRO_TIMER);
 414                 if (!fpu_error)
 415                         printk("Ok, fpu using %s error reporting.\n",
 416                                 ignore_irq13?"exception 16":"irq13");
 417         }
 418 #ifndef CONFIG_MATH_EMULATION
 419         else {
 420                 printk("No coprocessor found and no math emulation present.\n");
 421                 printk("Giving up.\n");
 422                 for (;;) ;
 423         }
 424 #endif
 425         move_to_user_mode();
 426         if (!fork())            /* we count on this going ok */
 427                 init();
 428 /*
 429  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 430  * it might do some general things like count free pages or it could be
 431  * used to implement a reasonable LRU algorithm for the paging routines:
 432  * anything that can be useful, but shouldn't take time from the real
 433  * processes.
 434  *
 435  * Right now task[0] just does a infinite idle loop.
 436  */
 437         for(;;)
 438                 idle();
 439 }
 440 
 441 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 442 {
 443         va_list args;
 444         int i;
 445 
 446         va_start(args, fmt);
 447         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 448         va_end(args);
 449         return i;
 450 }
 451 
 452 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 453 {
 454         int pid,i;
 455 
 456         setup((void *) &drive_info);
 457         (void) open("/dev/tty1",O_RDWR,0);
 458         (void) dup(0);
 459         (void) dup(0);
 460 
 461         system_utsname.machine[1] = '0' + x86;
 462         printf(linux_banner);
 463 
 464         execve("/etc/init",argv_init,envp_init);
 465         execve("/bin/init",argv_init,envp_init);
 466         execve("/sbin/init",argv_init,envp_init);
 467         /* if this fails, fall through to original stuff */
 468 
 469         if (!(pid=fork())) {
 470                 close(0);
 471                 if (open("/etc/rc",O_RDONLY,0))
 472                         _exit(1);
 473                 execve("/bin/sh",argv_rc,envp_rc);
 474                 _exit(2);
 475         }
 476         if (pid>0)
 477                 while (pid != wait(&i))
 478                         /* nothing */;
 479         while (1) {
 480                 if ((pid = fork()) < 0) {
 481                         printf("Fork failed in init\n\r");
 482                         continue;
 483                 }
 484                 if (!pid) {
 485                         close(0);close(1);close(2);
 486                         setsid();
 487                         (void) open("/dev/tty1",O_RDWR,0);
 488                         (void) dup(0);
 489                         (void) dup(0);
 490                         _exit(execve("/bin/sh",argv,envp));
 491                 }
 492                 while (1)
 493                         if (pid == wait(&i))
 494                                 break;
 495                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 496                 sync();
 497         }
 498         _exit(0);
 499 }

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