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

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