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. 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 #include <linux/hdreg.h>
  27 #include <linux/mm.h>
  28 
  29 #include <asm/bugs.h>
  30 
  31 extern unsigned long * prof_buffer;
  32 extern unsigned long prof_len;
  33 extern char etext, end;
  34 extern char *linux_banner;
  35 
  36 /*
  37  * we need this inline - forking from kernel space will result
  38  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  39  * is no problem, but for the stack. This is handled by not letting
  40  * main() use the stack at all after fork(). Thus, no function
  41  * calls - which means inline code for fork too, as otherwise we
  42  * would use the stack upon exit from 'fork()'.
  43  *
  44  * Actually only pause and fork are needed inline, so that there
  45  * won't be any messing with the stack from main(), but we define
  46  * some others too.
  47  */
  48 #define __NR__exit __NR_exit
  49 static inline _syscall0(int,idle)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 static inline _syscall0(int,fork)
  51 static inline _syscall0(int,pause)
  52 static inline _syscall0(int,setup)
  53 static inline _syscall0(int,sync)
  54 static inline _syscall0(pid_t,setsid)
  55 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  56 static inline _syscall1(int,dup,int,fd)
  57 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  58 static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  59 static inline _syscall1(int,close,int,fd)
  60 static inline _syscall1(int,_exit,int,exitcode)
  61 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  62 
  63 static inline pid_t wait(int * wait_stat)
  64 {
  65         return waitpid(-1,wait_stat,0);
  66 }
  67 
  68 static char printbuf[1024];
  69 
  70 extern int console_loglevel;
  71 
  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 aztcd_setup(char *str, int *ints);
  90 extern void st_setup(char *str, int *ints);
  91 extern void st0x_setup(char *str, int *ints);
  92 extern void tmc8xx_setup(char *str, int *ints);
  93 extern void t128_setup(char *str, int *ints);
  94 extern void pas16_setup(char *str, int *ints);
  95 extern void generic_NCR5380_setup(char *str, int *intr);
  96 extern void aha152x_setup(char *str, int *ints);
  97 extern void aha1542_setup(char *str, int *ints);
  98 extern void aha274x_setup(char *str, int *ints);
  99 extern void buslogic_setup(char *str, int *ints);
 100 extern void scsi_luns_setup(char *str, int *ints);
 101 extern void sound_setup(char *str, int *ints);
 102 #ifdef CONFIG_SBPCD
 103 extern void sbpcd_setup(char *str, int *ints);
 104 #endif CONFIG_SBPCD
 105 #ifdef CONFIG_CDU31A
 106 extern void cdu31a_setup(char *str, int *ints);
 107 #endif CONFIG_CDU31A
 108 #ifdef CONFIG_CDU535
 109 extern void sonycd535_setup(char *str, int *ints);
 110 #endif CONFIG_CDU535
 111 void ramdisk_setup(char *str, int *ints);
 112 
 113 #ifdef CONFIG_SYSVIPC
 114 extern void ipc_init(void);
 115 #endif
 116 #ifdef CONFIG_SCSI
 117 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
 118 #endif
 119 
 120 /*
 121  * Boot command-line arguments
 122  */
 123 #define MAX_INIT_ARGS 8
 124 #define MAX_INIT_ENVS 8
 125 
 126 extern void time_init(void);
 127 
 128 static unsigned long memory_start = 0;
 129 static unsigned long memory_end = 0;
 130 
 131 static char term[21];
 132 int rows, cols;
 133 
 134 int ramdisk_size;
 135 int root_mountflags = 0;
 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 char *get_options(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         char *cur = str;
 149         int i=1;
 150 
 151         while (cur && isdigit(*cur) && i <= 10) {
 152                 ints[i++] = simple_strtoul(cur,NULL,0);
 153                 if ((cur = strchr(cur,',')) != NULL)
 154                         cur++;
 155         }
 156         ints[0] = i-1;
 157         return(cur);
 158 }
 159 
 160 struct {
 161         char *str;
 162         void (*setup_func)(char *, int *);
 163 } bootsetups[] = {
 164         { "reserve=", reserve_setup },
 165         { "ramdisk=", ramdisk_setup },
 166 #ifdef CONFIG_BUGi386
 167         { "no-hlt", no_halt },
 168         { "no387", no_387 },
 169 #endif
 170 #ifdef CONFIG_INET
 171         { "ether=", eth_setup },
 172 #endif
 173 #ifdef CONFIG_SCSI
 174         { "max_scsi_luns=", scsi_luns_setup },
 175 #endif
 176 #ifdef CONFIG_BLK_DEV_IDE
 177         { "hda=", hda_setup },
 178         { "hdb=", hdb_setup },
 179         { "hdc=", hdc_setup },
 180         { "hdd=", hdd_setup },
 181         { "hd=",  ide_setup },
 182 #elif defined(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_PAS16
 199         { "pas16=", pas16_setup },
 200 #endif
 201 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 202         { "ncr5380=", generic_NCR5380_setup },
 203 #endif
 204 #ifdef CONFIG_SCSI_AHA152X
 205         { "aha152x=", aha152x_setup},
 206 #endif
 207 #ifdef CONFIG_SCSI_AHA1542
 208         { "aha1542=", aha1542_setup},
 209 #endif
 210 #ifdef CONFIG_SCSI_AHA274X
 211         { "aha274x=", aha274x_setup},
 212 #endif
 213 #ifdef CONFIG_SCSI_BUSLOGIC
 214         { "buslogic=", buslogic_setup},
 215 #endif
 216 #ifdef CONFIG_BLK_DEV_XD
 217         { "xd=", xd_setup },
 218 #endif
 219 #ifdef CONFIG_BLK_DEV_FD
 220         { "floppy=", floppy_setup },
 221 #endif
 222 #ifdef CONFIG_MCD
 223         { "mcd=", mcd_setup },
 224 #endif
 225 #ifdef CONFIG_AZTCD
 226         { "aztcd=", aztcd_setup },
 227 #endif
 228 #ifdef CONFIG_CDU535
 229         { "sonycd535=", sonycd535_setup },
 230 #endif CONFIG_CDU535
 231 #ifdef CONFIG_SOUND
 232         { "sound=", sound_setup },
 233 #endif
 234 #ifdef CONFIG_SBPCD
 235         { "sbpcd=", sbpcd_setup },
 236 #endif CONFIG_SBPCD
 237 #ifdef CONFIG_CDU31A
 238         { "cdu31a=", cdu31a_setup },
 239 #endif CONFIG_CDU31A
 240         { 0, 0 }
 241 };
 242 
 243 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245    if (ints[0] > 0 && ints[1] >= 0)
 246       ramdisk_size = ints[1];
 247 }
 248 
 249 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         int i = 0;
 252         int ints[11];
 253 
 254         while (bootsetups[i].str) {
 255                 int n = strlen(bootsetups[i].str);
 256                 if (!strncmp(line,bootsetups[i].str,n)) {
 257                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 258                         return 1;
 259                 }
 260                 i++;
 261         }
 262         return 0;
 263 }
 264 
 265 unsigned long loops_per_sec = 1;
 266 
 267 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         int ticks;
 270 
 271         printk("Calibrating delay loop.. ");
 272         while (loops_per_sec <<= 1) {
 273                 /* wait for "start of" clock tick */
 274                 ticks = jiffies;
 275                 while (ticks == jiffies)
 276                         /* nothing */;
 277                 /* Go .. */
 278                 ticks = jiffies;
 279                 __delay(loops_per_sec);
 280                 ticks = jiffies - ticks;
 281                 if (ticks >= HZ) {
 282                         loops_per_sec = muldiv(loops_per_sec, HZ, ticks);
 283                         printk("ok - %lu.%02lu BogoMips\n",
 284                                 loops_per_sec/500000,
 285                                 (loops_per_sec/5000) % 100);
 286                         return;
 287                 }
 288         }
 289         printk("failed\n");
 290 }
 291 
 292 
 293 /*
 294  * This is a simple kernel command line parsing function: it parses
 295  * the command line, and fills in the arguments/environment to init
 296  * as appropriate. Any cmd-line option is taken to be an environment
 297  * variable if it contains the character '='.
 298  *
 299  *
 300  * This routine also checks for options meant for the kernel.
 301  * These options are not given to init - they are for internal kernel use only.
 302  */
 303 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305         char *next;
 306         char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 307         int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 308         int args, envs;
 309 
 310         if (!*line)
 311                 return;
 312         args = 0;
 313         envs = 1;       /* TERM is set to 'console' by default */
 314         next = line;
 315         while ((line = next) != NULL) {
 316                 if ((next = strchr(line,' ')) != NULL)
 317                         *next++ = 0;
 318                 /*
 319                  * check for kernel options first..
 320                  */
 321                 if (!strncmp(line,"root=",5)) {
 322                         int n;
 323                         line += 5;
 324                         if (strncmp(line,"/dev/",5)) {
 325                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 326                                 continue;
 327                         }
 328                         line += 5;
 329                         for (n = 0 ; devnames[n] ; n++) {
 330                                 int len = strlen(devnames[n]);
 331                                 if (!strncmp(line,devnames[n],len)) {
 332                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,0);
 333                                         break;
 334                                 }
 335                         }
 336                         continue;
 337                 }
 338                 if (!strcmp(line,"ro")) {
 339                         root_mountflags |= MS_RDONLY;
 340                         continue;
 341                 }
 342                 if (!strcmp(line,"rw")) {
 343                         root_mountflags &= ~MS_RDONLY;
 344                         continue;
 345                 }
 346                 if (!strcmp(line,"debug")) {
 347                         console_loglevel = 10;
 348                         continue;
 349                 }
 350                 if (checksetup(line))
 351                         continue;
 352                 /*
 353                  * Then check if it's an environment variable or
 354                  * an option.
 355                  */
 356                 if (strchr(line,'=')) {
 357                         if (envs >= MAX_INIT_ENVS)
 358                                 break;
 359                         envp_init[++envs] = line;
 360                 } else {
 361                         if (args >= MAX_INIT_ARGS)
 362                                 break;
 363                         argv_init[++args] = line;
 364                 }
 365         }
 366         argv_init[args+1] = NULL;
 367         envp_init[envs+1] = NULL;
 368 }
 369 
 370 extern void check_bugs(void);
 371 extern void setup_arch(char **, unsigned long *, unsigned long *);
 372 
 373 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375         char * command_line;
 376 /*
 377  * Interrupts are still disabled. Do necessary setups, then
 378  * enable them
 379  */
 380         setup_arch(&command_line, &memory_start, &memory_end);
 381         memory_start = paging_init(memory_start,memory_end);
 382         trap_init();
 383         init_IRQ();
 384         sched_init();
 385         parse_options(command_line);
 386         init_modules();
 387 #ifdef CONFIG_PROFILE
 388         prof_buffer = (unsigned long *) memory_start;
 389         /* only text is profiled */
 390         prof_len = (unsigned long) &etext;
 391         prof_len >>= CONFIG_PROFILE_SHIFT;
 392         memory_start += prof_len * sizeof(unsigned long);
 393 #endif
 394         memory_start = console_init(memory_start,memory_end);
 395         memory_start = bios32_init(memory_start,memory_end);
 396         memory_start = kmalloc_init(memory_start,memory_end);
 397         sti();
 398         calibrate_delay();
 399         memory_start = chr_dev_init(memory_start,memory_end);
 400         memory_start = blk_dev_init(memory_start,memory_end);
 401         sti();
 402 #ifdef CONFIG_SCSI
 403         memory_start = scsi_dev_init(memory_start,memory_end);
 404 #endif
 405 #ifdef CONFIG_INET
 406         memory_start = net_dev_init(memory_start,memory_end);
 407 #endif
 408         memory_start = inode_init(memory_start,memory_end);
 409         memory_start = file_table_init(memory_start,memory_end);
 410         memory_start = name_cache_init(memory_start,memory_end);
 411         mem_init(memory_start,memory_end);
 412         buffer_init();
 413         time_init();
 414         sock_init();
 415 #ifdef CONFIG_SYSVIPC
 416         ipc_init();
 417 #endif
 418         sti();
 419         check_bugs();
 420 
 421         printk(linux_banner);
 422 
 423         if (!fork())            /* we count on this going ok */
 424                 init();
 425 /*
 426  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 427  * it might do some general things like count free pages or it could be
 428  * used to implement a reasonable LRU algorithm for the paging routines:
 429  * anything that can be useful, but shouldn't take time from the real
 430  * processes.
 431  *
 432  * Right now task[0] just does a infinite idle loop.
 433  */
 434         for(;;)
 435                 idle();
 436 }
 437 
 438 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 439 {
 440         va_list args;
 441         int i;
 442 
 443         va_start(args, fmt);
 444         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 445         va_end(args);
 446         return i;
 447 }
 448 
 449 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 450 {
 451         int pid,i;
 452 
 453         setup();
 454         sprintf(term, "TERM=con%dx%d", ORIG_VIDEO_COLS, ORIG_VIDEO_LINES);
 455 
 456         #ifdef CONFIG_UMSDOS_FS
 457         {
 458                 /*
 459                         When mounting a umsdos fs as root, we detect
 460                         the pseudo_root (/linux) and initialise it here.
 461                         pseudo_root is defined in fs/umsdos/inode.c
 462                 */
 463                 extern struct inode *pseudo_root;
 464                 if (pseudo_root != NULL){
 465                         current->fs->root = pseudo_root;
 466                         current->fs->pwd  = pseudo_root;
 467                 }
 468         }
 469         #endif
 470 
 471         (void) open("/dev/tty1",O_RDWR,0);
 472         (void) dup(0);
 473         (void) dup(0);
 474 
 475         execve("/etc/init",argv_init,envp_init);
 476         execve("/bin/init",argv_init,envp_init);
 477         execve("/sbin/init",argv_init,envp_init);
 478         /* if this fails, fall through to original stuff */
 479 
 480         if (!(pid=fork())) {
 481                 close(0);
 482                 if (open("/etc/rc",O_RDONLY,0))
 483                         _exit(1);
 484                 execve("/bin/sh",argv_rc,envp_rc);
 485                 _exit(2);
 486         }
 487         if (pid>0)
 488                 while (pid != wait(&i))
 489                         /* nothing */;
 490         while (1) {
 491                 if ((pid = fork()) < 0) {
 492                         printf("Fork failed in init\n\r");
 493                         continue;
 494                 }
 495                 if (!pid) {
 496                         close(0);close(1);close(2);
 497                         setsid();
 498                         (void) open("/dev/tty1",O_RDWR,0);
 499                         (void) dup(0);
 500                         (void) dup(0);
 501                         _exit(execve("/bin/sh",argv,envp));
 502                 }
 503                 while (1)
 504                         if (pid == wait(&i))
 505                                 break;
 506                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 507                 sync();
 508         }
 509         _exit(0);
 510 }

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