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

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