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

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