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

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