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

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