root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. _syscall0
  2. get_options
  3. ramdisk_setup
  4. checksetup
  5. calibrate_delay
  6. parse_options
  7. copy_options
  8. start_kernel
  9. printf
  10. init

   1 /*
   2  *  linux/init/main.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <stdarg.h>
   8 
   9 #include <asm/system.h>
  10 #include <asm/io.h>
  11 
  12 #include <linux/types.h>
  13 #include <linux/fcntl.h>
  14 #include <linux/config.h>
  15 #include <linux/sched.h>
  16 #include <linux/tty.h>
  17 #include <linux/head.h>
  18 #include <linux/unistd.h>
  19 #include <linux/string.h>
  20 #include <linux/timer.h>
  21 #include <linux/fs.h>
  22 #include <linux/ctype.h>
  23 #include <linux/delay.h>
  24 #include <linux/utsname.h>
  25 #include <linux/ioport.h>
  26 #include <linux/hdreg.h>
  27 
  28 #include <asm/bugs.h>
  29 
  30 extern unsigned long * prof_buffer;
  31 extern unsigned long prof_len;
  32 extern char etext, end;
  33 extern char *linux_banner;
  34 
  35 /*
  36  * we need this inline - forking from kernel space will result
  37  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  38  * is no problem, but for the stack. This is handled by not letting
  39  * main() use the stack at all after fork(). Thus, no function
  40  * calls - which means inline code for fork too, as otherwise we
  41  * would use the stack upon exit from 'fork()'.
  42  *
  43  * Actually only pause and fork are needed inline, so that there
  44  * won't be any messing with the stack from main(), but we define
  45  * some others too.
  46  */
  47 #define __NR__exit __NR_exit
  48 static inline _syscall0(int,idle)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 static inline _syscall0(int,fork)
  50 static inline _syscall0(int,pause)
  51 static inline _syscall0(int,setup)
  52 static inline _syscall0(int,sync)
  53 static inline _syscall0(pid_t,setsid)
  54 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  55 static inline _syscall1(int,dup,int,fd)
  56 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  57 static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  58 static inline _syscall1(int,close,int,fd)
  59 static inline _syscall1(int,_exit,int,exitcode)
  60 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  61 
  62 static inline pid_t wait(int * wait_stat)
  63 {
  64         return waitpid(-1,wait_stat,0);
  65 }
  66 
  67 static char printbuf[1024];
  68 
  69 extern int console_loglevel;
  70 
  71 extern char empty_zero_page[PAGE_SIZE];
  72 extern void init(void);
  73 extern void init_IRQ(void);
  74 extern void init_modules(void);
  75 extern long console_init(long, long);
  76 extern long kmalloc_init(long,long);
  77 extern long blk_dev_init(long,long);
  78 extern long chr_dev_init(long,long);
  79 extern void sock_init(void);
  80 extern long rd_init(long mem_start, int length);
  81 unsigned long net_dev_init(unsigned long, unsigned long);
  82 extern long bios32_init(long, long);
  83 
  84 #ifdef CONFIG_BLK_DEV_IDE
  85 extern void ide_setup(char *str, int *ints);
  86 extern void hda_setup(char *str, int *ints);
  87 extern void hdb_setup(char *str, int *ints);
  88 extern void hdc_setup(char *str, int *ints);
  89 extern void hdd_setup(char *str, int *ints);
  90 #else
  91 extern void hd_setup(char *str, int *ints);
  92 #endif /* CONFIG_BLK_DEV_IDE */
  93 extern void bmouse_setup(char *str, int *ints);
  94 extern void eth_setup(char *str, int *ints);
  95 extern void xd_setup(char *str, int *ints);
  96 extern void floppy_setup(char *str, int *ints);
  97 extern void mcd_setup(char *str, int *ints);
  98 extern void st_setup(char *str, int *ints);
  99 extern void st0x_setup(char *str, int *ints);
 100 extern void tmc8xx_setup(char *str, int *ints);
 101 extern void t128_setup(char *str, int *ints);
 102 extern void pas16_setup(char *str, int *ints);
 103 extern void generic_NCR5380_setup(char *str, int *intr);
 104 extern void aha152x_setup(char *str, int *ints);
 105 extern void aha1542_setup(char *str, int *ints);
 106 extern void aha274x_setup(char *str, int *ints);
 107 extern void buslogic_setup(char *str, int *ints);
 108 extern void scsi_luns_setup(char *str, int *ints);
 109 extern void sound_setup(char *str, int *ints);
 110 #ifdef CONFIG_SBPCD
 111 extern void sbpcd_setup(char *str, int *ints);
 112 #endif CONFIG_SBPCD
 113 #ifdef CONFIG_CDU31A
 114 extern void cdu31a_setup(char *str, int *ints);
 115 #endif CONFIG_CDU31A
 116 void ramdisk_setup(char *str, int *ints);
 117 
 118 #ifdef CONFIG_SYSVIPC
 119 extern void ipc_init(void);
 120 #endif
 121 #ifdef CONFIG_SCSI
 122 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
 123 #endif
 124 
 125 /*
 126  * This is set up by the setup-routine at boot-time
 127  */
 128 #define PARAM   empty_zero_page
 129 #define EXT_MEM_K (*(unsigned short *) (PARAM+2))
 130 #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
 131 #define SCREEN_INFO (*(struct screen_info *) (PARAM+0))
 132 #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
 133 #define RAMDISK_SIZE (*(unsigned short *) (PARAM+0x1F8))
 134 #define ORIG_ROOT_DEV (*(unsigned short *) (PARAM+0x1FC))
 135 #define AUX_DEVICE_INFO (*(unsigned char *) (PARAM+0x1FF))
 136 
 137 /*
 138  * Boot command-line arguments
 139  */
 140 #define MAX_INIT_ARGS 8
 141 #define MAX_INIT_ENVS 8
 142 #define COMMAND_LINE ((char *) (PARAM+2048))
 143 #define COMMAND_LINE_SIZE 256
 144 
 145 extern void time_init(void);
 146 
 147 static unsigned long memory_start = 0;  /* After mem_init, stores the */
 148                                         /* amount of free user memory */
 149 static unsigned long memory_end = 0;
 150 static unsigned long low_memory_start = 0;
 151 
 152 static char term[21];
 153 int rows, cols;
 154 
 155 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 156 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", term, NULL, };
 157 
 158 static char * argv_rc[] = { "/bin/sh", NULL };
 159 static char * envp_rc[] = { "HOME=/", term, NULL };
 160 
 161 static char * argv[] = { "-/bin/sh",NULL };
 162 static char * envp[] = { "HOME=/usr/root", term, NULL };
 163 
 164 struct drive_info_struct { char dummy[32]; } drive_info;
 165 struct screen_info screen_info;
 166 
 167 unsigned char aux_device_present;
 168 int ramdisk_size;
 169 int root_mountflags = 0;
 170 
 171 static char command_line[COMMAND_LINE_SIZE] = { 0, };
 172 
 173 char *get_options(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175         char *cur = str;
 176         int i=1;
 177 
 178         while (cur && isdigit(*cur) && i <= 10) {
 179                 ints[i++] = simple_strtoul(cur,NULL,0);
 180                 if ((cur = strchr(cur,',')) != NULL)
 181                         cur++;
 182         }
 183         ints[0] = i-1;
 184         return(cur);
 185 }
 186 
 187 struct {
 188         char *str;
 189         void (*setup_func)(char *, int *);
 190 } bootsetups[] = {
 191         { "reserve=", reserve_setup },
 192         { "ramdisk=", ramdisk_setup },
 193 #ifdef CONFIG_BUGi386
 194         { "no-hlt", no_halt },
 195         { "no387", no_387 },
 196 #endif
 197 #ifdef CONFIG_INET
 198         { "ether=", eth_setup },
 199 #endif
 200 #ifdef CONFIG_SCSI
 201         { "max_scsi_luns=", scsi_luns_setup },
 202 #endif
 203 #ifdef CONFIG_BLK_DEV_IDE
 204         { "hda=", hda_setup },
 205         { "hdb=", hdb_setup },
 206         { "hdc=", hdc_setup },
 207         { "hdd=", hdd_setup },
 208         { "hd=",  ide_setup },
 209 #elif defined(CONFIG_BLK_DEV_HD)
 210         { "hd=", hd_setup },
 211 #endif
 212 #ifdef CONFIG_CHR_DEV_ST
 213         { "st=", st_setup },
 214 #endif
 215 #ifdef CONFIG_BUSMOUSE
 216         { "bmouse=", bmouse_setup },
 217 #endif
 218 #ifdef CONFIG_SCSI_SEAGATE
 219         { "st0x=", st0x_setup },
 220         { "tmc8xx=", tmc8xx_setup },
 221 #endif
 222 #ifdef CONFIG_SCSI_T128
 223         { "t128=", t128_setup },
 224 #endif
 225 #ifdef CONFIG_SCSI_PAS16
 226         { "pas16=", pas16_setup },
 227 #endif
 228 #ifdef CONFIG_SCSI_GENERIC_NCR5380
 229         { "ncr5380=", generic_NCR5380_setup },
 230 #endif
 231 #ifdef CONFIG_SCSI_AHA152X
 232         { "aha152x=", aha152x_setup},
 233 #endif
 234 #ifdef CONFIG_SCSI_AHA1542
 235         { "aha1542=", aha1542_setup},
 236 #endif
 237 #ifdef CONFIG_SCSI_AHA274X
 238         { "aha274x=", aha274x_setup},
 239 #endif
 240 #ifdef CONFIG_SCSI_BUSLOGIC
 241         { "buslogic=", buslogic_setup},
 242 #endif
 243 #ifdef CONFIG_BLK_DEV_XD
 244         { "xd=", xd_setup },
 245 #endif
 246 #ifdef CONFIG_BLK_DEV_FD
 247         { "floppy=", floppy_setup },
 248 #endif
 249 #ifdef CONFIG_MCD
 250         { "mcd=", mcd_setup },
 251 #endif
 252 #ifdef CONFIG_SOUND
 253         { "sound=", sound_setup },
 254 #endif
 255 #ifdef CONFIG_SBPCD
 256         { "sbpcd=", sbpcd_setup },
 257 #endif CONFIG_SBPCD
 258 #ifdef CONFIG_CDU31A
 259         { "cdu31a=", cdu31a_setup },
 260 #endif CONFIG_CDU31A
 261         { 0, 0 }
 262 };
 263 
 264 void ramdisk_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266    if (ints[0] > 0 && ints[1] >= 0)
 267       ramdisk_size = ints[1];
 268 }
 269 
 270 static int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272         int i = 0;
 273         int ints[11];
 274 
 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 unsigned long loops_per_sec = 1;
 287 
 288 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 289 {
 290         int ticks;
 291 
 292         printk("Calibrating delay loop.. ");
 293         while (loops_per_sec <<= 1) {
 294                 /* wait for "start of" clock tick */
 295                 ticks = jiffies;
 296                 while (ticks == jiffies)
 297                         /* nothing */;
 298                 /* Go .. */
 299                 ticks = jiffies;
 300                 __delay(loops_per_sec);
 301                 ticks = jiffies - ticks;
 302                 if (ticks >= HZ) {
 303                         loops_per_sec = muldiv(loops_per_sec, HZ, ticks);
 304                         printk("ok - %lu.%02lu BogoMips\n",
 305                                 loops_per_sec/500000,
 306                                 (loops_per_sec/5000) % 100);
 307                         return;
 308                 }
 309         }
 310         printk("failed\n");
 311 }
 312 
 313 
 314 /*
 315  * This is a simple kernel command line parsing function: it parses
 316  * the command line, and fills in the arguments/environment to init
 317  * as appropriate. Any cmd-line option is taken to be an environment
 318  * variable if it contains the character '='.
 319  *
 320  *
 321  * This routine also checks for options meant for the kernel.
 322  * These options are not given to init - they are for internal kernel use only.
 323  */
 324 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326         char *next;
 327         char *devnames[] = { "hda", "hdb", "hdc", "hdd", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
 328         int devnums[]    = { 0x300, 0x340, 0x1600, 0x1640, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xD00, 0xD40, 0};
 329         int args, envs;
 330 
 331         if (!*line)
 332                 return;
 333         args = 0;
 334         envs = 1;       /* TERM is set to 'console' by default */
 335         next = line;
 336         while ((line = next) != NULL) {
 337                 if ((next = strchr(line,' ')) != NULL)
 338                         *next++ = 0;
 339                 /*
 340                  * check for kernel options first..
 341                  */
 342                 if (!strncmp(line,"root=",5)) {
 343                         int n;
 344                         line += 5;
 345                         if (strncmp(line,"/dev/",5)) {
 346                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 347                                 continue;
 348                         }
 349                         line += 5;
 350                         for (n = 0 ; devnames[n] ; n++) {
 351                                 int len = strlen(devnames[n]);
 352                                 if (!strncmp(line,devnames[n],len)) {
 353                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,0);
 354                                         break;
 355                                 }
 356                         }
 357                         continue;
 358                 }
 359                 if (!strcmp(line,"ro")) {
 360                         root_mountflags |= MS_RDONLY;
 361                         continue;
 362                 }
 363                 if (!strcmp(line,"rw")) {
 364                         root_mountflags &= ~MS_RDONLY;
 365                         continue;
 366                 }
 367                 if (!strcmp(line,"debug")) {
 368                         console_loglevel = 10;
 369                         continue;
 370                 }
 371                 if (checksetup(line))
 372                         continue;
 373                 /*
 374                  * Then check if it's an environment variable or
 375                  * an option.
 376                  */
 377                 if (strchr(line,'=')) {
 378                         if (envs >= MAX_INIT_ENVS)
 379                                 break;
 380                         envp_init[++envs] = line;
 381                 } else {
 382                         if (args >= MAX_INIT_ARGS)
 383                                 break;
 384                         argv_init[++args] = line;
 385                 }
 386         }
 387         argv_init[args+1] = NULL;
 388         envp_init[envs+1] = NULL;
 389 }
 390 
 391 static void copy_options(char * to, char * from)
     /* [previous][next][first][last][top][bottom][index][help] */
 392 {
 393         char c = ' ';
 394         int len = 0;
 395 
 396         for (;;) {
 397                 if (c == ' ' && *(unsigned long *)from == *(unsigned long *)"mem=") {
 398                         memory_end = simple_strtoul(from+4, &from, 0);
 399                         if ( *from == 'K' || *from == 'k' ) {
 400                                 memory_end = memory_end << 10;
 401                                 from++;
 402                         } else if ( *from == 'M' || *from == 'm' ) {
 403                                 memory_end = memory_end << 20;
 404                                 from++;
 405                         }
 406                 }
 407                 c = *(from++);
 408                 if (!c)
 409                         break;
 410                 if (COMMAND_LINE_SIZE <= ++len)
 411                         break;
 412                 *(to++) = c;
 413         }
 414         *to = '\0';
 415 }
 416 
 417 extern void check_bugs(void);
 418 
 419 asmlinkage void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 {
 421 /*
 422  * Interrupts are still disabled. Do necessary setups, then
 423  * enable them
 424  */
 425         ROOT_DEV = ORIG_ROOT_DEV;
 426         drive_info = DRIVE_INFO;
 427         screen_info = SCREEN_INFO;
 428         aux_device_present = AUX_DEVICE_INFO;
 429         memory_end = (1<<20) + (EXT_MEM_K<<10);
 430         memory_end &= PAGE_MASK;
 431         ramdisk_size = RAMDISK_SIZE;
 432         copy_options(command_line,COMMAND_LINE);
 433 #ifdef CONFIG_MAX_16M
 434         if (memory_end > 16*1024*1024)
 435                 memory_end = 16*1024*1024;
 436 #endif
 437         if (MOUNT_ROOT_RDONLY)
 438                 root_mountflags |= MS_RDONLY;
 439         if ((unsigned long)&end >= (1024*1024)) {
 440                 memory_start = (unsigned long) &end;
 441                 low_memory_start = PAGE_SIZE;
 442         } else {
 443                 memory_start = 1024*1024;
 444                 low_memory_start = (unsigned long) &end;
 445         }
 446         low_memory_start = PAGE_ALIGN(low_memory_start);
 447         memory_start = paging_init(memory_start,memory_end);
 448         if (strncmp((char*)0x0FFFD9, "EISA", 4) == 0)
 449                 EISA_bus = 1;
 450         trap_init();
 451         init_IRQ();
 452         sched_init();
 453         parse_options(command_line);
 454         init_modules();
 455 #ifdef CONFIG_PROFILE
 456         prof_buffer = (unsigned long *) memory_start;
 457         /* only text is profiled */
 458         prof_len = (unsigned long) &etext;
 459         prof_len >>= CONFIG_PROFILE_SHIFT;
 460         memory_start += prof_len * sizeof(unsigned long);
 461 #endif
 462         memory_start = console_init(memory_start,memory_end);
 463         memory_start = bios32_init(memory_start,memory_end);
 464         memory_start = kmalloc_init(memory_start,memory_end);
 465         sti();
 466         calibrate_delay();
 467         cli();
 468         memory_start = chr_dev_init(memory_start,memory_end);
 469         memory_start = blk_dev_init(memory_start,memory_end);
 470         sti();
 471 #ifdef CONFIG_SCSI
 472         memory_start = scsi_dev_init(memory_start,memory_end);
 473 #endif
 474 #ifdef CONFIG_INET
 475         memory_start = net_dev_init(memory_start,memory_end);
 476 #endif
 477         memory_start = inode_init(memory_start,memory_end);
 478         memory_start = file_table_init(memory_start,memory_end);
 479         memory_start = name_cache_init(memory_start,memory_end);
 480         mem_init(low_memory_start,memory_start,memory_end);
 481         buffer_init();
 482         time_init();
 483         sock_init();
 484 #ifdef CONFIG_SYSVIPC
 485         ipc_init();
 486 #endif
 487         sti();
 488         check_bugs();
 489 
 490         printk(linux_banner);
 491 
 492         move_to_user_mode();
 493         if (!fork())            /* we count on this going ok */
 494                 init();
 495 /*
 496  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 497  * it might do some general things like count free pages or it could be
 498  * used to implement a reasonable LRU algorithm for the paging routines:
 499  * anything that can be useful, but shouldn't take time from the real
 500  * processes.
 501  *
 502  * Right now task[0] just does a infinite idle loop.
 503  */
 504         for(;;)
 505                 idle();
 506 }
 507 
 508 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 509 {
 510         va_list args;
 511         int i;
 512 
 513         va_start(args, fmt);
 514         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 515         va_end(args);
 516         return i;
 517 }
 518 
 519 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         int pid,i;
 522 
 523         setup();
 524         sprintf(term, "TERM=con%dx%d", ORIG_VIDEO_COLS, ORIG_VIDEO_LINES);
 525 
 526         #ifdef CONFIG_UMSDOS_FS
 527         {
 528                 /*
 529                         When mounting a umsdos fs as root, we detect
 530                         the pseudo_root (/linux) and initialise it here.
 531                         pseudo_root is defined in fs/umsdos/inode.c
 532                 */
 533                 extern struct inode *pseudo_root;
 534                 if (pseudo_root != NULL){
 535                         current->fs->root = pseudo_root;
 536                         current->fs->pwd  = pseudo_root;
 537                 }
 538         }
 539         #endif
 540 
 541         (void) open("/dev/tty1",O_RDWR,0);
 542         (void) dup(0);
 543         (void) dup(0);
 544 
 545         execve("/etc/init",argv_init,envp_init);
 546         execve("/bin/init",argv_init,envp_init);
 547         execve("/sbin/init",argv_init,envp_init);
 548         /* if this fails, fall through to original stuff */
 549 
 550         if (!(pid=fork())) {
 551                 close(0);
 552                 if (open("/etc/rc",O_RDONLY,0))
 553                         _exit(1);
 554                 execve("/bin/sh",argv_rc,envp_rc);
 555                 _exit(2);
 556         }
 557         if (pid>0)
 558                 while (pid != wait(&i))
 559                         /* nothing */;
 560         while (1) {
 561                 if ((pid = fork()) < 0) {
 562                         printf("Fork failed in init\n\r");
 563                         continue;
 564                 }
 565                 if (!pid) {
 566                         close(0);close(1);close(2);
 567                         setsid();
 568                         (void) open("/dev/tty1",O_RDWR,0);
 569                         (void) dup(0);
 570                         (void) dup(0);
 571                         _exit(execve("/bin/sh",argv,envp));
 572                 }
 573                 while (1)
 574                         if (pid == wait(&i))
 575                                 break;
 576                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 577                 sync();
 578         }
 579         _exit(0);
 580 }

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