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_rc
  13. do_shell
  14. 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 = 0;
 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 CONFIG_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 CONFIG_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         smp_threads_ready=1;
 464         smp_commence();
 465 }               
 466         
 467 #endif
 468 
 469 /*
 470  *      Activate the first processor.
 471  */
 472  
 473 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 474 {
 475         char * command_line;
 476 
 477 /*
 478  *      This little check will move.
 479  */
 480 
 481 #ifdef CONFIG_SMP       
 482         static int first_cpu=1;
 483         
 484         if(!first_cpu)
 485                 start_secondary();
 486         first_cpu=0;
 487         
 488 #endif  
 489 /*
 490  * Interrupts are still disabled. Do necessary setups, then
 491  * enable them
 492  */
 493         setup_arch(&command_line, &memory_start, &memory_end);
 494         memory_start = paging_init(memory_start,memory_end);
 495         trap_init();
 496         init_IRQ();
 497         sched_init();
 498         time_init();
 499         parse_options(command_line);
 500         init_modules();
 501 #ifdef CONFIG_PROFILE
 502         if (!prof_shift)
 503 #ifdef CONFIG_PROFILE_SHIFT
 504                 prof_shift = CONFIG_PROFILE_SHIFT;
 505 #else
 506                 prof_shift = 2;
 507 #endif
 508 #endif
 509         if (prof_shift) {
 510                 prof_buffer = (unsigned long *) memory_start;
 511                 /* only text is profiled */
 512                 prof_len = (unsigned long) &_etext - (unsigned long) &_stext;
 513                 prof_len >>= prof_shift;
 514                 memory_start += prof_len * sizeof(unsigned long);
 515         }
 516         memory_start = console_init(memory_start,memory_end);
 517 #ifdef CONFIG_PCI
 518         memory_start = pci_init(memory_start,memory_end);
 519 #endif
 520         memory_start = kmalloc_init(memory_start,memory_end);
 521         sti();
 522         calibrate_delay();
 523         memory_start = inode_init(memory_start,memory_end);
 524         memory_start = file_table_init(memory_start,memory_end);
 525         memory_start = name_cache_init(memory_start,memory_end);
 526         if (ramdisk_size)
 527                 memory_start += rd_init(memory_start, ramdisk_size*1024);
 528         mem_init(memory_start,memory_end);
 529         buffer_init();
 530         sock_init();
 531 #ifdef CONFIG_SYSVIPC
 532         ipc_init();
 533 #endif
 534         sti();
 535         check_bugs();
 536 
 537         printk(linux_banner);
 538 #ifdef CONFIG_SMP       
 539         smp_init();
 540 #endif
 541         /* we count on the initial thread going ok */
 542         kernel_thread(init, NULL, 0);
 543 /*
 544  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 545  * it might do some general things like count free pages or it could be
 546  * used to implement a reasonable LRU algorithm for the paging routines:
 547  * anything that can be useful, but shouldn't take time from the real
 548  * processes.
 549  *
 550  * Right now task[0] just does a infinite idle loop.
 551  */
 552         for(;;)
 553                 idle();
 554 }
 555 
 556 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 557 {
 558         va_list args;
 559         int i;
 560 
 561         va_start(args, fmt);
 562         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 563         va_end(args);
 564         return i;
 565 }
 566 
 567 static int do_rc(void * rc)
     /* [previous][next][first][last][top][bottom][index][help] */
 568 {
 569         close(0);
 570         if (open(rc,O_RDONLY,0))
 571                 return -1;
 572         return execve("/bin/sh", argv_rc, envp_rc);
 573 }
 574 
 575 static int do_shell(void * shell)
     /* [previous][next][first][last][top][bottom][index][help] */
 576 {
 577         close(0);close(1);close(2);
 578         setsid();
 579         (void) open("/dev/tty1",O_RDWR,0);
 580         (void) dup(0);
 581         (void) dup(0);
 582         return execve(shell, argv, envp);
 583 }
 584 
 585 static int init(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 586 {
 587         int pid,i;
 588 
 589         setup();
 590 
 591         #ifdef CONFIG_UMSDOS_FS
 592         {
 593                 /*
 594                         When mounting a umsdos fs as root, we detect
 595                         the pseudo_root (/linux) and initialise it here.
 596                         pseudo_root is defined in fs/umsdos/inode.c
 597                 */
 598                 extern struct inode *pseudo_root;
 599                 if (pseudo_root != NULL){
 600                         current->fs->root = pseudo_root;
 601                         current->fs->pwd  = pseudo_root;
 602                 }
 603         }
 604         #endif
 605 
 606         (void) open("/dev/tty1",O_RDWR,0);
 607         (void) dup(0);
 608         (void) dup(0);
 609 
 610         execve("/etc/init",argv_init,envp_init);
 611         execve("/bin/init",argv_init,envp_init);
 612         execve("/sbin/init",argv_init,envp_init);
 613         /* if this fails, fall through to original stuff */
 614 
 615         pid = kernel_thread(do_rc, "/etc/rc", SIGCHLD);
 616         if (pid>0)
 617                 while (pid != wait(&i))
 618                         /* nothing */;
 619         while (1) {
 620                 pid = kernel_thread(do_shell, "/bin/sh", SIGCHLD);
 621                 if (pid < 0) {
 622                         printf("Fork failed in init\n\r");
 623                         continue;
 624                 }
 625                 while (1)
 626                         if (pid == wait(&i))
 627                                 break;
 628                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 629                 sync();
 630         }
 631         return -1;
 632 }

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