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

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