root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. _syscall0
  2. time_init
  3. get_options
  4. checksetup
  5. calibrate_delay
  6. parse_options
  7. copro_timeout
  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/mktime.h>
  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 
  26 extern unsigned long * prof_buffer;
  27 extern unsigned long prof_len;
  28 extern char edata, end;
  29 extern char *linux_banner;
  30 
  31 /*
  32  * we need this inline - forking from kernel space will result
  33  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  34  * is no problem, but for the stack. This is handled by not letting
  35  * main() use the stack at all after fork(). Thus, no function
  36  * calls - which means inline code for fork too, as otherwise we
  37  * would use the stack upon exit from 'fork()'.
  38  *
  39  * Actually only pause and fork are needed inline, so that there
  40  * won't be any messing with the stack from main(), but we define
  41  * some others too.
  42  */
  43 static inline _syscall0(int,idle)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 static inline _syscall0(int,fork)
  45 static inline _syscall0(int,pause)
  46 static inline _syscall1(int,setup,void *,BIOS)
  47 static inline _syscall0(int,sync)
  48 static inline _syscall0(pid_t,setsid)
  49 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  50 static inline _syscall1(int,dup,int,fd)
  51 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  52 static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  53 static inline _syscall1(int,close,int,fd)
  54 static inline _syscall1(int,exit,int,exitcode)
  55 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  56 
  57 static inline pid_t wait(int * wait_stat)
  58 {
  59         return waitpid(-1,wait_stat,0);
  60 }
  61 
  62 static char printbuf[1024];
  63 
  64 extern char empty_zero_page[4096];
  65 extern int vsprintf(char *,const char *,va_list);
  66 extern void init(void);
  67 extern void init_IRQ(void);
  68 extern long blk_dev_init(long,long);
  69 extern long chr_dev_init(long,long);
  70 extern void floppy_init(void);
  71 extern void sock_init(void);
  72 extern long rd_init(long mem_start, int length);
  73 extern long kernel_mktime(struct mktime * time);
  74 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  75 
  76 extern void hd_setup(char *str, int *ints);
  77 extern void bmouse_setup(char *str, int *ints);
  78 extern void eth_setup(char *str, int *ints);
  79 
  80 #ifdef CONFIG_SYSVIPC
  81 extern void ipc_init(void);
  82 #endif
  83 #ifdef CONFIG_SCSI
  84 extern unsigned long scsi_dev_init(unsigned long, unsigned long);
  85 #endif
  86 
  87 /*
  88  * This is set up by the setup-routine at boot-time
  89  */
  90 #define PARAM   empty_zero_page
  91 #define EXT_MEM_K (*(unsigned short *) (PARAM+2))
  92 #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
  93 #define SCREEN_INFO (*(struct screen_info *) (PARAM+0))
  94 #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
  95 #define RAMDISK_SIZE (*(unsigned short *) (PARAM+0x1F8))
  96 #define ORIG_ROOT_DEV (*(unsigned short *) (PARAM+0x1FC))
  97 #define AUX_DEVICE_INFO (*(unsigned char *) (PARAM+0x1FF))
  98 
  99 /*
 100  * Boot command-line arguments
 101  */
 102 #define MAX_INIT_ARGS 8
 103 #define MAX_INIT_ENVS 8
 104 #define COMMAND_LINE ((char *) (PARAM+2048))
 105 
 106 /*
 107  * Yeah, yeah, it's ugly, but I cannot find how to do this correctly
 108  * and this seems to work. I anybody has more info on the real-time
 109  * clock I'd be interested. Most of this was trial and error, and some
 110  * bios-listing reading. Urghh.
 111  */
 112 
 113 #define CMOS_READ(addr) ({ \
 114 outb_p(addr,0x70); \
 115 inb_p(0x71); \
 116 })
 117 
 118 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
 119 
 120 static void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122         struct mktime time;
 123         int i;
 124 
 125         for (i = 0 ; i < 1000000 ; i++)
 126                 if (!(CMOS_READ(10) & 0x80))
 127                         break;
 128         do {
 129                 time.sec = CMOS_READ(0);
 130                 time.min = CMOS_READ(2);
 131                 time.hour = CMOS_READ(4);
 132                 time.day = CMOS_READ(7);
 133                 time.mon = CMOS_READ(8);
 134                 time.year = CMOS_READ(9);
 135         } while (time.sec != CMOS_READ(0));
 136         BCD_TO_BIN(time.sec);
 137         BCD_TO_BIN(time.min);
 138         BCD_TO_BIN(time.hour);
 139         BCD_TO_BIN(time.day);
 140         BCD_TO_BIN(time.mon);
 141         BCD_TO_BIN(time.year);
 142         time.mon--;
 143         startup_time = kernel_mktime(&time);
 144 }
 145 
 146 static unsigned long memory_start = 0;  /* After mem_init, stores the */
 147                                         /* amount of free user memory */
 148 static unsigned long memory_end = 0;
 149 static unsigned long low_memory_start = 0;
 150 
 151 static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 152 static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=console", NULL, };
 153 
 154 static char * argv_rc[] = { "/bin/sh", NULL };
 155 static char * envp_rc[] = { "HOME=/", "TERM=console", NULL };
 156 
 157 static char * argv[] = { "-/bin/sh",NULL };
 158 static char * envp[] = { "HOME=/usr/root", "TERM=console", NULL };
 159 
 160 struct drive_info_struct { char dummy[32]; } drive_info;
 161 struct screen_info screen_info;
 162 
 163 unsigned char aux_device_present;
 164 int ramdisk_size;
 165 int root_mountflags = 0;
 166 
 167 static char fpu_error = 0;
 168 
 169 static char command_line[80] = { 0, };
 170 
 171 char *get_options(char *str, int *ints) 
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         char *cur = str;
 174         int i=1;
 175 
 176         while (cur && isdigit(*cur) && i <= 10) {
 177                 ints[i++] = simple_strtoul(cur,NULL,0);
 178                 if ((cur = strchr(cur,',')) != NULL)
 179                         cur++;
 180         }
 181         ints[0] = i-1;
 182         return(cur);
 183 }
 184 
 185 struct {
 186         char *str;
 187         void (*setup_func)(char *, int *);
 188 } bootsetups[] = {
 189 #ifdef CONFIG_INET
 190         { "ether=", eth_setup },
 191 #endif
 192 #ifdef CONFIG_BLK_DEV_HD
 193         { "hd=", hd_setup },
 194 #endif
 195 #ifdef CONFIG_BUSMOUSE
 196         { "bmouse=", bmouse_setup },
 197 #endif
 198         { 0, 0 }
 199 };
 200 
 201 int checksetup(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203         int i = 0;
 204         int ints[11];
 205 
 206         while (bootsetups[i].str) {
 207                 int n = strlen(bootsetups[i].str);
 208                 if (!strncmp(line,bootsetups[i].str,n)) {
 209                         bootsetups[i].setup_func(get_options(line+n,ints), ints);
 210                         return(0);
 211                 }
 212                 i++;
 213         }
 214         return(1);
 215 }
 216 
 217 unsigned long loops_per_sec = 1;
 218 
 219 static void calibrate_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         int ticks;
 222 
 223         printk("Calibrating delay loop.. ");
 224         while (loops_per_sec <<= 1) {
 225                 ticks = jiffies;
 226                 __delay(loops_per_sec);
 227                 ticks = jiffies - ticks;
 228                 if (ticks >= HZ) {
 229                         __asm__("mull %1 ; divl %2"
 230                                 :"=a" (loops_per_sec)
 231                                 :"d" (HZ),
 232                                  "r" (ticks),
 233                                  "0" (loops_per_sec)
 234                                 :"dx");
 235                         printk("ok - %d.%02d BogoMips (tm)\n",
 236                                 loops_per_sec/500000,
 237                                 (loops_per_sec/5000) % 100);
 238                         return;
 239                 }
 240         }
 241         printk("failed\n");
 242 }
 243         
 244 
 245 /*
 246  * This is a simple kernel command line parsing function: it parses
 247  * the command line, and fills in the arguments/environment to init
 248  * as appropriate. Any cmd-line option is taken to be an environment
 249  * variable if it contains the character '='.
 250  *
 251  *
 252  * This routine also checks for options meant for the kernel - currently
 253  * only the "root=XXXX" option is recognized. These options are not given
 254  * to init - they are for internal kernel use only.
 255  */
 256 static void parse_options(char *line)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258         char *next;
 259         char *devnames[] = { "hda", "hdb", "sda", "sdb", "sdc", "sdd", "sde", "fd", NULL };
 260         int devnums[]    = { 0x300, 0x340, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0};
 261         int args, envs;
 262 
 263         if (!*line)
 264                 return;
 265         args = 0;
 266         envs = 1;       /* TERM is set to 'console' by default */
 267         next = line;
 268         while ((line = next) != NULL) {
 269                 if ((next = strchr(line,' ')) != NULL)
 270                         *next++ = 0;
 271                 /*
 272                  * check for kernel options first..
 273                  */
 274                 if (!strncmp(line,"root=",5)) {
 275                         int n;
 276                         line += 5;
 277                         if (strncmp(line,"/dev/",5)) {
 278                                 ROOT_DEV = simple_strtoul(line,NULL,16);
 279                                 continue;
 280                         }
 281                         line += 5;
 282                         for (n = 0 ; devnames[n] ; n++) {
 283                                 int len = strlen(devnames[n]);
 284                                 if (!strncmp(line,devnames[n],len)) {
 285                                         ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,16);
 286                                         break;
 287                                 }
 288                         }
 289                 } else if (!strcmp(line,"ro"))
 290                         root_mountflags |= MS_RDONLY;
 291                 else if (!strcmp(line,"rw"))
 292                         root_mountflags &= ~MS_RDONLY;
 293                 else if (!strcmp(line,"no387")) {
 294                         hard_math = 0;
 295                         __asm__("movl %%cr0,%%eax\n\t"
 296                                 "orl $0xE,%%eax\n\t"
 297                                 "movl %%eax,%%cr0\n\t" : : : "ax");
 298                 } else
 299                         checksetup(line);
 300                 /*
 301                  * Then check if it's an environment variable or
 302                  * an option.
 303                  */     
 304                 if (strchr(line,'=')) {
 305                         if (envs >= MAX_INIT_ENVS)
 306                                 break;
 307                         envp_init[++envs] = line;
 308                 } else {
 309                         if (args >= MAX_INIT_ARGS)
 310                                 break;
 311                         argv_init[++args] = line;
 312                 }
 313         }
 314         argv_init[args+1] = NULL;
 315         envp_init[envs+1] = NULL;
 316 }
 317 
 318 static void copro_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 319 {
 320         fpu_error = 1;
 321         timer_table[COPRO_TIMER].expires = jiffies+100;
 322         timer_active |= 1<<COPRO_TIMER;
 323         printk("387 failed: trying to reset\n");
 324         send_sig(SIGFPE, last_task_used_math, 1);
 325         outb_p(0,0xf1);
 326         outb_p(0,0xf0);
 327 }
 328 
 329 extern "C" void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 330 {
 331 /*
 332  * Interrupts are still disabled. Do necessary setups, then
 333  * enable them
 334  */
 335         ROOT_DEV = ORIG_ROOT_DEV;
 336         drive_info = DRIVE_INFO;
 337         screen_info = SCREEN_INFO;
 338         aux_device_present = AUX_DEVICE_INFO;
 339         memory_end = (1<<20) + (EXT_MEM_K<<10);
 340         memory_end &= 0xfffff000;
 341         ramdisk_size = RAMDISK_SIZE;
 342         strcpy(command_line,COMMAND_LINE);
 343 #ifdef CONFIG_MAX_16M
 344         if (memory_end > 16*1024*1024)
 345                 memory_end = 16*1024*1024;
 346 #endif
 347         if (MOUNT_ROOT_RDONLY)
 348                 root_mountflags |= MS_RDONLY;
 349         if ((unsigned long)&end >= (1024*1024)) {
 350                 memory_start = (unsigned long) &end;
 351                 low_memory_start = 4096;
 352         } else {
 353                 memory_start = 1024*1024;
 354                 low_memory_start = (unsigned long) &end;
 355         }
 356         low_memory_start += 0xfff;
 357         low_memory_start &= 0xfffff000;
 358         memory_start = paging_init(memory_start,memory_end);
 359         trap_init();
 360         init_IRQ();
 361         sched_init();
 362         parse_options(command_line);
 363 #ifdef CONFIG_PROFILE
 364         prof_buffer = (unsigned long *) memory_start;
 365         prof_len = (unsigned long) &end;
 366         prof_len >>= 2;
 367         memory_start += prof_len * sizeof(unsigned long);
 368 #endif
 369         memory_start = chr_dev_init(memory_start,memory_end);
 370         memory_start = blk_dev_init(memory_start,memory_end);
 371 #ifdef CONFIG_SCSI
 372         memory_start = scsi_dev_init(memory_start,memory_end);
 373 #endif
 374         memory_start = inode_init(memory_start,memory_end);
 375         memory_start = file_table_init(memory_start,memory_end);
 376         mem_init(low_memory_start,memory_start,memory_end);
 377         buffer_init();
 378         time_init();
 379         floppy_init();
 380         sock_init();
 381 #ifdef CONFIG_SYSVIPC
 382         ipc_init();
 383 #endif
 384         sti();
 385         calibrate_delay();
 386         /*
 387          * check if exception 16 works correctly.. This is truly evil
 388          * code: it disables the high 8 interrupts to make sure that
 389          * the irq13 doesn't happen. But as this will lead to a lockup
 390          * if no exception16 arrives, it depends on the fact that the
 391          * high 8 interrupts will be re-enabled by the next timer tick.
 392          * So the irq13 will happen eventually, but the exception 16
 393          * should get there first..
 394          */
 395         if (hard_math) {
 396                 unsigned short control_word;
 397 
 398                 printk("Checking 386/387 coupling... ");
 399                 timer_table[COPRO_TIMER].expires = jiffies+50;
 400                 timer_table[COPRO_TIMER].fn = copro_timeout;
 401                 timer_active |= 1<<COPRO_TIMER;
 402                 __asm__("clts ; fninit ; fnstcw %0 ; fwait":"=m" (*&control_word));
 403                 control_word &= 0xffc0;
 404                 __asm__("fldcw %0 ; fwait": :"m" (*&control_word));
 405                 outb_p(inb_p(0x21) | (1 << 2), 0x21);
 406                 __asm__("fldz ; fld1 ; fdiv %st,%st(1) ; fwait");
 407                 timer_active &= ~(1<<COPRO_TIMER);
 408                 if (!fpu_error)
 409                         printk("Ok, fpu using %s error reporting.\n",
 410                                 ignore_irq13?"exception 16":"irq13");
 411         }
 412         move_to_user_mode();
 413         if (!fork())            /* we count on this going ok */
 414                 init();
 415 /*
 416  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 417  * it might do some general things like count free pages or it could be
 418  * used to implement a reasonable LRU algorithm for the paging routines:
 419  * anything that can be useful, but shouldn't take time from the real
 420  * processes.
 421  *
 422  * Right now task[0] just does a infinite idle loop.
 423  */
 424         for(;;)
 425                 idle();
 426 }
 427 
 428 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430         va_list args;
 431         int i;
 432 
 433         va_start(args, fmt);
 434         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 435         va_end(args);
 436         return i;
 437 }
 438 
 439 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 440 {
 441         int pid,i;
 442 
 443         setup((void *) &drive_info);
 444         (void) open("/dev/tty1",O_RDWR,0);
 445         (void) dup(0);
 446         (void) dup(0);
 447 
 448         printf(linux_banner);
 449         execve("/etc/init",argv_init,envp_init);
 450         execve("/bin/init",argv_init,envp_init);
 451         execve("/sbin/init",argv_init,envp_init);
 452         /* if this fails, fall through to original stuff */
 453 
 454         if (!(pid=fork())) {
 455                 close(0);
 456                 if (open("/etc/rc",O_RDONLY,0))
 457                         exit(1);
 458                 execve("/bin/sh",argv_rc,envp_rc);
 459                 exit(2);
 460         }
 461         if (pid>0)
 462                 while (pid != wait(&i))
 463                         /* nothing */;
 464         while (1) {
 465                 if ((pid = fork()) < 0) {
 466                         printf("Fork failed in init\n\r");
 467                         continue;
 468                 }
 469                 if (!pid) {
 470                         close(0);close(1);close(2);
 471                         setsid();
 472                         (void) open("/dev/tty1",O_RDWR,0);
 473                         (void) dup(0);
 474                         (void) dup(0);
 475                         exit(execve("/bin/sh",argv,envp));
 476                 }
 477                 while (1)
 478                         if (pid == wait(&i))
 479                                 break;
 480                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 481                 sync();
 482         }
 483         exit(0);
 484 }

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