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

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