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

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