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                 if (!strcmp(line,"no387")) {
 203                         hard_math = 0;
 204                         __asm__("movl %%cr0,%%eax\n\t"
 205                                 "andl $0xFFFFFFF9,%%eax\n\t"
 206                                 "orl $0x4,%%eax\n\t"
 207                                 "movl %%eax,%%cr0\n\t" ::: "ax");
 208                         continue;
 209                 }
 210                 /*
 211                  * Then check if it's an environment variable or
 212                  * an option.
 213                  */     
 214                 if (strchr(line,'=')) {
 215                         if (envs >= MAX_INIT_ENVS)
 216                                 break;
 217                         envp_init[++envs] = line;
 218                 } else {
 219                         if (args >= MAX_INIT_ARGS)
 220                                 break;
 221                         argv_init[++args] = line;
 222                 }
 223         }
 224         argv_init[args+1] = NULL;
 225         envp_init[envs+1] = NULL;
 226 }
 227 
 228 static void copro_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 229 {
 230         fpu_error = 1;
 231         timer_table[COPRO_TIMER].expires = jiffies+100;
 232         timer_active |= 1<<COPRO_TIMER;
 233         printk("387 failed: trying to reset\n");
 234         send_sig(SIGFPE, last_task_used_math, 1);
 235         outb_p(0,0xf1);
 236         outb_p(0,0xf0);
 237 }
 238 
 239 void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241 /*
 242  * Interrupts are still disabled. Do necessary setups, then
 243  * enable them
 244  */
 245         ROOT_DEV = ORIG_ROOT_DEV;
 246         drive_info = DRIVE_INFO;
 247         screen_info = SCREEN_INFO;
 248         aux_device_present = AUX_DEVICE_INFO;
 249         memory_end = (1<<20) + (EXT_MEM_K<<10);
 250         memory_end &= 0xfffff000;
 251         ramdisk_size = RAMDISK_SIZE;
 252 #ifdef CONFIG_MAX_16M
 253         if (memory_end > 16*1024*1024)
 254                 memory_end = 16*1024*1024;
 255 #endif
 256         if (MOUNT_ROOT_RDONLY)
 257                 root_mountflags |= MS_RDONLY;
 258         if ((unsigned long)&end >= (1024*1024)) {
 259                 memory_start = (unsigned long) &end;
 260                 low_memory_start = 4096;
 261         } else {
 262                 memory_start = 1024*1024;
 263                 low_memory_start = (unsigned long) &end;
 264         }
 265         low_memory_start += 0xfff;
 266         low_memory_start &= 0xfffff000;
 267         memory_start = paging_init(memory_start,memory_end);
 268         if (CL_MAGIC_ADDR == CL_MAGIC)
 269                 strcpy(command_line,CL_BASE_ADDR+CL_OFFSET);
 270         trap_init();
 271         init_IRQ();
 272         sched_init();
 273         parse_options(command_line);
 274 #ifdef CONFIG_PROFILE
 275         prof_buffer = (unsigned long *) memory_start;
 276         prof_len = (unsigned long) &end;
 277         prof_len >>= 2;
 278         memory_start += prof_len * sizeof(unsigned long);
 279 #endif
 280         memory_start = chr_dev_init(memory_start,memory_end);
 281         memory_start = blk_dev_init(memory_start,memory_end);
 282 #ifdef CONFIG_SCSI
 283         memory_start = scsi_dev_init(memory_start,memory_end);
 284 #endif
 285         memory_start = inode_init(memory_start,memory_end);
 286         memory_start = file_table_init(memory_start,memory_end);
 287         mem_init(low_memory_start,memory_start,memory_end);
 288         buffer_init();
 289         time_init();
 290         floppy_init();
 291         sock_init();
 292         sti();
 293         /*
 294          * check if exception 16 works correctly.. This is truly evil
 295          * code: it disables the high 8 interrupts to make sure that
 296          * the irq13 doesn't happen. But as this will lead to a lockup
 297          * if no exception16 arrives, it depends on the fact that the
 298          * high 8 interrupts will be re-enabled by the next timer tick.
 299          * So the irq13 will happen eventually, but the exception 16
 300          * should get there first..
 301          */
 302         if (hard_math) {
 303                 unsigned short control_word;
 304 
 305                 timer_table[COPRO_TIMER].expires = jiffies+50;
 306                 timer_table[COPRO_TIMER].fn = copro_timeout;
 307                 timer_active |= 1<<COPRO_TIMER;
 308                 printk("You have a bad 386/387 coupling.\r");
 309                 __asm__("clts ; fninit ; fnstcw %0 ; fwait":"=m" (*&control_word));
 310                 control_word &= 0xffc0;
 311                 __asm__("fldcw %0 ; fwait"::"m" (*&control_word));
 312                 outb_p(inb_p(0x21) | (1 << 2), 0x21);
 313                 __asm__("fldz ; fld1 ; fdiv %st,%st(1) ; fwait");
 314                 timer_active &= ~(1<<COPRO_TIMER);
 315                 if (!fpu_error)
 316                         printk("Math coprocessor using %s error reporting.\n",
 317                                 ignore_irq13?"exception 16":"irq13");
 318         }
 319         move_to_user_mode();
 320         if (!fork())            /* we count on this going ok */
 321                 init();
 322 /*
 323  * task[0] is meant to be used as an "idle" task: it may not sleep, but
 324  * it might do some general things like count free pages or it could be
 325  * used to implement a reasonable LRU algorithm for the paging routines:
 326  * anything that can be useful, but shouldn't take time from the real
 327  * processes.
 328  *
 329  * Right now task[0] just does a infinite idle loop.
 330  */
 331         for(;;)
 332                 idle();
 333 }
 334 
 335 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337         va_list args;
 338         int i;
 339 
 340         va_start(args, fmt);
 341         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 342         va_end(args);
 343         return i;
 344 }
 345 
 346 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 347 {
 348         int pid,i;
 349 
 350         setup((void *) &drive_info);
 351         (void) open("/dev/tty1",O_RDWR,0);
 352         (void) dup(0);
 353         (void) dup(0);
 354 
 355         printf(linux_banner);
 356         execve("/etc/init",argv_init,envp_init);
 357         execve("/bin/init",argv_init,envp_init);
 358         execve("/sbin/init",argv_init,envp_init);
 359         /* if this fails, fall through to original stuff */
 360 
 361         if (!(pid=fork())) {
 362                 close(0);
 363                 if (open("/etc/rc",O_RDONLY,0))
 364                         _exit(1);
 365                 execve("/bin/sh",argv_rc,envp_rc);
 366                 _exit(2);
 367         }
 368         if (pid>0)
 369                 while (pid != wait(&i))
 370                         /* nothing */;
 371         while (1) {
 372                 if ((pid = fork()) < 0) {
 373                         printf("Fork failed in init\n\r");
 374                         continue;
 375                 }
 376                 if (!pid) {
 377                         close(0);close(1);close(2);
 378                         setsid();
 379                         (void) open("/dev/tty1",O_RDWR,0);
 380                         (void) dup(0);
 381                         (void) dup(0);
 382                         _exit(execve("/bin/sh",argv,envp));
 383                 }
 384                 while (1)
 385                         if (pid == wait(&i))
 386                                 break;
 387                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 388                 sync();
 389         }
 390         _exit(0);       /* NOTE! _exit, not exit() */
 391 }

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