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

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