root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_options
  2. profile_setup
  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 #define __KERNEL_SYSCALLS__
   8 #include <stdarg.h>
   9 
  10 #include <asm/system.h>
  11 #include <asm/io.h>
  12 
  13 #include <linux/types.h>
  14 #include <linux/fcntl.h>
  15 #include <linux/config.h>
  16 #include <linux/sched.h>
  17 #include <linux/tty.h>
  18 #include <linux/head.h>
  19 #include <linux/unistd.h>
  20 #include <linux/string.h>
  21 #include <linux/timer.h>
  22 #include <linux/fs.h>
  23 #include <linux/ctype.h>
  24 #include <linux/delay.h>
  25 #include <linux/utsname.h>
  26 #include <linux/ioport.h>
  27 #include <linux/hdreg.h>
  28 #include <linux/mm.h>
  29 
  30 #include <asm/bugs.h>
  31 
  32 extern char _stext, _etext;
  33 extern char *linux_banner;
  34 
  35 static char printbuf[1024];
  36 
  37 extern int console_loglevel;
  38 
  39 extern void init(void);
  40 extern void init_IRQ(void);
  41 extern void init_modules(void);
  42 extern long console_init(long, long);
  43 extern long kmalloc_init(long,long);
  44 extern long blk_dev_init(long,long);
  45 extern long chr_dev_init(long,long);
  46 extern void sock_init(void);
  47 extern long rd_init(long mem_start, int length);
  48 unsigned long net_dev_init(unsigned long, unsigned long);
  49 extern long pci_init(long, long);
  50 
  51 extern void bmouse_setup(char *str, int *ints);
  52 extern void eth_setup(char *str, int *ints);
  53 extern void xd_setup(char *str, int *ints);
  54 extern void floppy_setup(char *str, int *ints);
  55 extern void st_setup(char *str, int *ints);
  56 extern void st0x_setup(char *str, int *ints);
  57 extern void tmc8xx_setup(char *str, int *ints);
  58 extern void t128_setup(char *str, int *ints);
  59 extern void pas16_setup(char *str, int *ints);
  60 extern void generic_NCR5380_setup(char *str, int *intr);
  61 extern void aha152x_setup(char *str, int *ints);
  62 extern void aha1542_setup(char *str, int *ints);
  63 extern void aic7xxx_setup(char *str, int *ints);
  64 extern void buslogic_setup(char *str, int *ints);
  65 extern void fdomain_setup(char *str, int *ints);
  66 extern void scsi_luns_setup(char *str, int *ints);
  67 extern void sound_setup(char *str, int *ints);
  68 #ifdef CONFIG_CDU31A
  69 extern void cdu31a_setup(char *str, int *ints);
  70 #endif CONFIG_CDU31A
  71 #ifdef CONFIG_MCD
  72 extern void mcd_setup(char *str, int *ints);
  73 #endif CONFIG_MCD
  74 #ifdef CONFIG_MCDX
  75 extern void mcdx_setup(char *str, int *ints);
  76 #endif CONFIG_MCDX
  77 #ifdef CONFIG_SBPCD
  78 extern void sbpcd_setup(char *str, int *ints);
  79 #endif CONFIG_SBPCD
  80 #ifdef CONFIG_AZTCD
  81 extern void aztcd_setup(char *str, int *ints);
  82 #endif CONFIG_AZTCD
  83 #ifdef CONFIG_CDU535
  84 extern void sonycd535_setup(char *str, int *ints);
  85 #endif CONFIG_CDU535
  86 #ifdef CONFIG_GSCD
  87 extern void gscd_setup(char *str, int *ints);
  88 #endif CONFIG_GSCD
  89 #ifdef CONFIG_CM206
  90 extern void cm206_setup(char *str, int *ints);
  91 #endif CONFIG_CM206
  92 #ifdef CONFIG_OPTCD
  93 extern void optcd_setup(char *str, int *ints);
  94 #endif CONFIG_OPTCD
  95 #ifdef CONFIG_SJCD
  96 extern void sjcd_setup(char *str, int *ints);
  97 #endif CONFIG_SJCD
  98 void ramdisk_setup(char *str, int *ints);
  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  * Boot command-line arguments
 109  */
 110 #define MAX_INIT_ARGS 8
 111 #define MAX_INIT_ENVS 8
 112 
 113 extern void time_init(void);
 114 
 115 static unsigned long memory_start = 0;
 116 static unsigned long memory_end = 0;
 117 
 118 int rows, cols;
 119 
 120 int ramdisk_size;
 121 int root_mountflags = 0;
 122 
 123 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 124 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
 125 
 126 static char * argv_rc[] = { "/bin/sh", NULL };
 127 static char * envp_rc[] = { "HOME=/", "TERM=linux", NULL };
 128 
 129 static char * argv[] = { "-/bin/sh",NULL };
 130 static char * envp[] = { "HOME=/usr/root", "TERM=linux", NULL };
 131 
 132 char *get_options(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134         char *cur = str;
 135         int i=1;
 136 
 137         while (cur && isdigit(*cur) && i <= 10) {
 138                 ints[i++] = simple_strtoul(cur,NULL,0);
 139                 if ((cur = strchr(cur,',')) != NULL)
 140                         cur++;
 141         }
 142         ints[0] = i-1;
 143         return(cur);
 144 }
 145 
 146 static void profile_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         if (ints[0] > 0)
 149                 prof_shift = (unsigned long) ints[1];
 150         else
 151 #ifdef CONFIG_PROFILE_SHIFT
 152                 prof_shift = CONFIG_PROFILE_SHIFT;
 153 #else
 154                 prof_shift = 2;
 155 #endif
 156 }
 157 
 158 struct {
 159         const char *str;
 160         void (*setup_func)(char *, int *);
 161 } bootsetups[] = {
 162         { "reserve=", reserve_setup },
 163         { "profile=", profile_setup },
 164         { "ramdisk=", ramdisk_setup },
 165 #ifdef CONFIG_BUGi386
 166         { "no-hlt", no_halt },
 167         { "no387", no_387 },
 168 #endif
 169 #ifdef CONFIG_INET
 170         { "ether=", eth_setup },
 171 #endif
 172 #ifdef CONFIG_SCSI
 173         { "max_scsi_luns=", scsi_luns_setup },
 174 #endif
 175 #if defined(CONFIG_BLK_DEV_HD)
 176         { "hd=", hd_setup },
 177 #endif
 178 #ifdef CONFIG_CHR_DEV_ST
 179         { "st=", st_setup },
 180 #endif
 181 #ifdef CONFIG_BUSMOUSE
 182         { "bmouse=", bmouse_setup },
 183 #endif
 184 #ifdef CONFIG_SCSI_SEAGATE
 185         { "st0x=", st0x_setup },
 186         { "tmc8xx=", tmc8xx_setup },
 187 #endif
 188 #ifdef CONFIG_SCSI_T128
 189         { "t128=", t128_setup },
 190 #endif
 191 #ifdef CONFIG_SCSI_PAS16
 192         { "pas16=", pas16_setup },
 193 #endif
 194 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 195         { "ncr5380=", generic_NCR5380_setup },
 196 #endif
 197 #ifdef CONFIG_SCSI_AHA152X
 198         { "aha152x=", aha152x_setup},
 199 #endif
 200 #ifdef CONFIG_SCSI_AHA1542
 201         { "aha1542=", aha1542_setup},
 202 #endif
 203 #ifdef CONFIG_SCSI_AIC7XXX
 204         { "aic7xxx=", aic7xxx_setup},
 205 #endif
 206 #ifdef CONFIG_SCSI_BUSLOGIC
 207         { "buslogic=", buslogic_setup},
 208 #endif
 209 #ifdef CONFIG_SCSI_FUTURE_DOMAIN
 210         { "fdomain=", fdomain_setup},
 211 #endif
 212 #ifdef CONFIG_BLK_DEV_XD
 213         { "xd=", xd_setup },
 214 #endif
 215 #ifdef CONFIG_BLK_DEV_FD
 216         { "floppy=", floppy_setup },
 217 #endif
 218 #ifdef CONFIG_CDU31A
 219         { "cdu31a=", cdu31a_setup },
 220 #endif CONFIG_CDU31A
 221 #ifdef CONFIG_MCD
 222         { "mcd=", mcd_setup },
 223 #endif CONFIG_MCD
 224 #ifdef CONFIG_MCDX
 225         { "mcdx=", mcdx_setup },
 226 #endif CONFIG_MCDX
 227 #ifdef CONFIG_SBPCD
 228         { "sbpcd=", sbpcd_setup },
 229 #endif CONFIG_SBPCD
 230 #ifdef CONFIG_AZTCD
 231         { "aztcd=", aztcd_setup },
 232 #endif CONFIG_AZTCD
 233 #ifdef CONFIG_CDU535
 234         { "sonycd535=", sonycd535_setup },
 235 #endif CONFIG_CDU535
 236 #ifdef CONFIG_GSCD
 237         { "gscd=", gscd_setup },
 238 #endif CONFIG_GSCD
 239 #ifdef CONFIG_CM206
 240         { "cm206=", cm206_setup },
 241 #endif CONFIG_CM206
 242 #ifdef CONFIG_OPTCD
 243         { "optcd=", optcd_setup },
 244 #endif CONFIG_OPTCD
 245 #ifdef CONFIG_SJCD
 246         { "sjcd=", sjcd_setup },
 247 #endif CONFIG_SJCD
 248 #ifdef CONFIG_SOUND
 249         { "sound=", sound_setup },
 250 #endif
 251         { 0, 0 }
 252 };
 253 
 254 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256    if (ints[0] > 0 && ints[1] >= 0)
 257       ramdisk_size = ints[1];
 258 }
 259 
 260 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         int i = 0;
 263         int ints[11];
 264 
 265 #ifdef CONFIG_BLK_DEV_IDE
 266         /* ide driver needs the basic string, rather than pre-processed values */
 267         if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '=')) {
 268                 ide_setup(line);
 269                 return 1;
 270         }
 271 #endif
 272         while (bootsetups[i].str) {
 273                 int n = strlen(bootsetups[i].str);
 274                 if (!strncmp(line,bootsetups[i].str,n)) {
 275                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 276                         return 1;
 277                 }
 278                 i++;
 279         }
 280         return 0;
 281 }
 282 
 283 /* this should be approx 2 Bo*oMips to start (note initial shift), and will
 284    still work even if initally too large, it will just take slightly longer */
 285 unsigned long loops_per_sec = (1<<12);
 286 
 287 /* This is the number of bits of precision for the loops_per_second.  Each
 288    bit takes on average 1.5/HZ seconds.  This (like the original) is a little
 289    better than 1% */
 290 #define LPS_PREC 8
 291 
 292 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294         int ticks;
 295         int loopbit;
 296         int lps_precision = LPS_PREC;
 297 
 298         printk("Calibrating delay loop.. ");
 299         while (loops_per_sec <<= 1) {
 300                 /* wait for "start of" clock tick */
 301                 ticks = jiffies;
 302                 while (ticks == jiffies)
 303                         /* nothing */;
 304                 /* Go .. */
 305                 ticks = jiffies;
 306                 __delay(loops_per_sec);
 307                 ticks = jiffies - ticks;
 308                 if (ticks)
 309                         break;
 310                 }
 311 
 312 /* Do a binary approximation to get loops_per_second set to equal one clock
 313    (up to lps_precision bits) */
 314         loops_per_sec >>= 1;
 315         loopbit = loops_per_sec;
 316         while ( lps_precision-- && (loopbit >>= 1) ) {
 317                 loops_per_sec |= loopbit;
 318                 ticks = jiffies;
 319                 while (ticks == jiffies);
 320                 ticks = jiffies;
 321                 __delay(loops_per_sec);
 322                 if (jiffies != ticks)   /* longer than 1 tick */
 323                         loops_per_sec &= ~loopbit;
 324         }
 325 
 326 /* finally, adjust loops per second in terms of seconds instead of clocks */    
 327         loops_per_sec *= HZ;
 328 /* Round the value and print it */      
 329         printk("ok - %lu.%02lu BogoMIPS\n",
 330                 (loops_per_sec+2500)/500000,
 331                 ((loops_per_sec+2500)/5000) % 100);
 332 }
 333 
 334 /*
 335  * This is a simple kernel command line parsing function: it parses
 336  * the command line, and fills in the arguments/environment to init
 337  * as appropriate. Any cmd-line option is taken to be an environment
 338  * variable if it contains the character '='.
 339  *
 340  *
 341  * This routine also checks for options meant for the kernel.
 342  * These options are not given to init - they are for internal kernel use only.
 343  */
 344 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         char *next;
 347         static const char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 348         static int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 349         int args, envs;
 350 
 351         if (!*line)
 352                 return;
 353         args = 0;
 354         envs = 1;       /* TERM is set to 'linux' by default */
 355         next = line;
 356         while ((line = next) != NULL) {
 357                 if ((next = strchr(line,' ')) != NULL)
 358                         *next++ = 0;
 359                 /*
 360                  * check for kernel options first..
 361                  */
 362                 if (!strncmp(line,"root=",5)) {
 363                         int n;
 364                         line += 5;
 365                         if (strncmp(line,"/dev/",5)) {
 366                                 ROOT_DEV = to_kdev_t(
 367                                              simple_strtoul(line,NULL,16));
 368                                 continue;
 369                         }
 370                         line += 5;
 371                         for (n = 0 ; devnames[n] ; n++) {
 372                                 int len = strlen(devnames[n]);
 373                                 if (!strncmp(line,devnames[n],len)) {
 374                                         ROOT_DEV = to_kdev_t(devnums[n]+
 375                                              simple_strtoul(line+len,NULL,0));
 376                                         break;
 377                                 }
 378                         }
 379                         continue;
 380                 }
 381                 if (!strcmp(line,"ro")) {
 382                         root_mountflags |= MS_RDONLY;
 383                         continue;
 384                 }
 385                 if (!strcmp(line,"rw")) {
 386                         root_mountflags &= ~MS_RDONLY;
 387                         continue;
 388                 }
 389                 if (!strcmp(line,"debug")) {
 390                         console_loglevel = 10;
 391                         continue;
 392                 }
 393                 if (checksetup(line))
 394                         continue;
 395                 /*
 396                  * Then check if it's an environment variable or
 397                  * an option.
 398                  */
 399                 if (strchr(line,'=')) {
 400                         if (envs >= MAX_INIT_ENVS)
 401                                 break;
 402                         envp_init[++envs] = line;
 403                 } else {
 404                         if (args >= MAX_INIT_ARGS)
 405                                 break;
 406                         argv_init[++args] = line;
 407                 }
 408         }
 409         argv_init[args+1] = NULL;
 410         envp_init[envs+1] = NULL;
 411 }
 412 
 413 extern void setup_arch(char **, unsigned long *, unsigned long *);
 414 
 415 static char init_stack[PAGE_SIZE];
 416 
 417 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 418 {
 419         char * command_line;
 420 /*
 421  * Interrupts are still disabled. Do necessary setups, then
 422  * enable them
 423  */
 424         setup_arch(&command_line, &memory_start, &memory_end);
 425         memory_start = paging_init(memory_start,memory_end);
 426         trap_init();
 427         init_IRQ();
 428         sched_init();
 429         parse_options(command_line);
 430         init_modules();
 431 #ifdef CONFIG_PROFILE
 432         if (!prof_shift)
 433 #ifdef CONFIG_PROFILE_SHIFT
 434                 prof_shift = CONFIG_PROFILE_SHIFT;
 435 #else
 436                 prof_shift = 2;
 437 #endif
 438 #endif
 439         if (prof_shift) {
 440                 prof_buffer = (unsigned long *) memory_start;
 441                 /* only text is profiled */
 442                 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
 443                 prof_len >>= prof_shift;
 444                 memory_start += prof_len * sizeof(unsigned long);
 445         }
 446         memory_start = console_init(memory_start,memory_end);
 447 #ifdef CONFIG_PCI
 448         memory_start = pci_init(memory_start,memory_end);
 449 #endif
 450         memory_start = kmalloc_init(memory_start,memory_end);
 451         sti();
 452         calibrate_delay();
 453         memory_start = chr_dev_init(memory_start,memory_end);
 454         memory_start = blk_dev_init(memory_start,memory_end);
 455         sti();
 456 #ifdef CONFIG_SCSI
 457         memory_start = scsi_dev_init(memory_start,memory_end);
 458 #endif
 459 #ifdef CONFIG_INET
 460         memory_start = net_dev_init(memory_start,memory_end);
 461 #endif
 462         memory_start = inode_init(memory_start,memory_end);
 463         memory_start = file_table_init(memory_start,memory_end);
 464         memory_start = name_cache_init(memory_start,memory_end);
 465         mem_init(memory_start,memory_end);
 466         buffer_init();
 467         time_init();
 468         sock_init();
 469 #ifdef CONFIG_SYSVIPC
 470         ipc_init();
 471 #endif
 472         sti();
 473         check_bugs();
 474 
 475         printk(linux_banner);
 476 
 477         /* we count on the clone going ok */
 478         if (!clone(CLONE_VM, init_stack+sizeof(init_stack)))
 479                 init();
 480 /*
 481  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 482  * it might do some general things like count free pages or it could be
 483  * used to implement a reasonable LRU algorithm for the paging routines:
 484  * anything that can be useful, but shouldn't take time from the real
 485  * processes.
 486  *
 487  * Right now task[0] just does a infinite idle loop.
 488  */
 489         for(;;)
 490                 idle();
 491 }
 492 
 493 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {
 495         va_list args;
 496         int i;
 497 
 498         va_start(args, fmt);
 499         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 500         va_end(args);
 501         return i;
 502 }
 503 
 504 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 505 {
 506         int pid,i;
 507 
 508         setup();
 509 
 510         #ifdef CONFIG_UMSDOS_FS
 511         {
 512                 /*
 513                         When mounting a umsdos fs as root, we detect
 514                         the pseudo_root (/linux) and initialise it here.
 515                         pseudo_root is defined in fs/umsdos/inode.c
 516                 */
 517                 extern struct inode *pseudo_root;
 518                 if (pseudo_root != NULL){
 519                         current->fs->root = pseudo_root;
 520                         current->fs->pwd  = pseudo_root;
 521                 }
 522         }
 523         #endif
 524 
 525         (void) open("/dev/tty1",O_RDWR,0);
 526         (void) dup(0);
 527         (void) dup(0);
 528 
 529         execve("/etc/init",argv_init,envp_init);
 530         execve("/bin/init",argv_init,envp_init);
 531         execve("/sbin/init",argv_init,envp_init);
 532         /* if this fails, fall through to original stuff */
 533 
 534         if (!(pid=fork())) {
 535                 close(0);
 536                 if (open("/etc/rc",O_RDONLY,0))
 537                         _exit(1);
 538                 execve("/bin/sh",argv_rc,envp_rc);
 539                 _exit(2);
 540         }
 541         if (pid>0)
 542                 while (pid != wait(&i))
 543                         /* nothing */;
 544         while (1) {
 545                 if ((pid = fork()) < 0) {
 546                         printf("Fork failed in init\n\r");
 547                         continue;
 548                 }
 549                 if (!pid) {
 550                         close(0);close(1);close(2);
 551                         setsid();
 552                         (void) open("/dev/tty1",O_RDWR,0);
 553                         (void) dup(0);
 554                         (void) dup(0);
 555                         _exit(execve("/bin/sh",argv,envp));
 556                 }
 557                 while (1)
 558                         if (pid == wait(&i))
 559                                 break;
 560                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 561                 sync();
 562         }
 563         _exit(0);
 564 }

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