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

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