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

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