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

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