root/init/main.c

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

DEFINITIONS

This source file includes following definitions.
  1. _syscall0
  2. sprintf
  3. time_init
  4. start_kernel
  5. printf
  6. init

   1 /*
   2  *  linux/init/main.c
   3  *
   4  *  (C) 1991  Linus Torvalds
   5  */
   6 
   7 #include <stddef.h>
   8 #include <stdarg.h>
   9 #include <fcntl.h>
  10 #include <time.h>
  11 
  12 #include <sys/types.h>
  13 
  14 #include <asm/system.h>
  15 #include <asm/io.h>
  16 
  17 #include <linux/config.h>
  18 #include <linux/sched.h>
  19 #include <linux/tty.h>
  20 #include <linux/head.h>
  21 #include <linux/unistd.h>
  22 
  23 /*
  24  * we need this inline - forking from kernel space will result
  25  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  26  * is no problem, but for the stack. This is handled by not letting
  27  * main() use the stack at all after fork(). Thus, no function
  28  * calls - which means inline code for fork too, as otherwise we
  29  * would use the stack upon exit from 'fork()'.
  30  *
  31  * Actually only pause and fork are needed inline, so that there
  32  * won't be any messing with the stack from main(), but we define
  33  * some others too.
  34  */
  35 static inline _syscall0(int,fork)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 static inline _syscall0(int,pause)
  37 static inline _syscall1(int,setup,void *,BIOS)
  38 static inline _syscall0(int,sync)
  39 static inline _syscall0(pid_t,setsid)
  40 static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  41 static inline _syscall1(int,dup,int,fd)
  42 static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  43 static inline _syscall1(int,close,int,fd)
  44 static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  45 
  46 static inline pid_t wait(int * wait_stat)
  47 {
  48         return waitpid(-1,wait_stat,0);
  49 }
  50 
  51 static char printbuf[1024];
  52 
  53 extern int vsprintf();
  54 extern void init(void);
  55 extern void blk_dev_init(void);
  56 extern void chr_dev_init(void);
  57 extern void hd_init(void);
  58 extern void floppy_init(void);
  59 extern void sock_init(void);
  60 extern void mem_init(long start, long end);
  61 extern long rd_init(long mem_start, int length);
  62 extern long kernel_mktime(struct tm * tm);
  63 
  64 #ifdef CONFIG_SCSI
  65 extern void scsi_dev_init(void);
  66 #endif
  67 
  68 static int sprintf(char * str, const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70         va_list args;
  71         int i;
  72 
  73         va_start(args, fmt);
  74         i = vsprintf(str, fmt, args);
  75         va_end(args);
  76         return i;
  77 }
  78 
  79 /*
  80  * This is set up by the setup-routine at boot-time
  81  */
  82 #define EXT_MEM_K (*(unsigned short *)0x90002)
  83 #define CON_ROWS ((*(unsigned short *)0x9000e) & 0xff)
  84 #define CON_COLS (((*(unsigned short *)0x9000e) & 0xff00) >> 8)
  85 #define DRIVE_INFO (*(struct drive_info *)0x90080)
  86 #define ORIG_ROOT_DEV (*(unsigned short *)0x901FC)
  87 
  88 /*
  89  * Yeah, yeah, it's ugly, but I cannot find how to do this correctly
  90  * and this seems to work. I anybody has more info on the real-time
  91  * clock I'd be interested. Most of this was trial and error, and some
  92  * bios-listing reading. Urghh.
  93  */
  94 
  95 #define CMOS_READ(addr) ({ \
  96 outb_p(0x80|addr,0x70); \
  97 inb_p(0x71); \
  98 })
  99 
 100 #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
 101 
 102 static void time_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104         struct tm time;
 105 
 106         do {
 107                 time.tm_sec = CMOS_READ(0);
 108                 time.tm_min = CMOS_READ(2);
 109                 time.tm_hour = CMOS_READ(4);
 110                 time.tm_mday = CMOS_READ(7);
 111                 time.tm_mon = CMOS_READ(8);
 112                 time.tm_year = CMOS_READ(9);
 113         } while (time.tm_sec != CMOS_READ(0));
 114         BCD_TO_BIN(time.tm_sec);
 115         BCD_TO_BIN(time.tm_min);
 116         BCD_TO_BIN(time.tm_hour);
 117         BCD_TO_BIN(time.tm_mday);
 118         BCD_TO_BIN(time.tm_mon);
 119         BCD_TO_BIN(time.tm_year);
 120         time.tm_mon--;
 121         startup_time = kernel_mktime(&time);
 122 }
 123 
 124 static long memory_end = 0;
 125 static long buffer_memory_end = 0;
 126 static long main_memory_start = 0;
 127 static char term[32];
 128 
 129 static char * argv_init[] = { "/bin/init", NULL };
 130 static char * envp_init[] = { "HOME=/", NULL, NULL };
 131 
 132 static char * argv_rc[] = { "/bin/sh", NULL };
 133 static char * envp_rc[] = { "HOME=/", NULL ,NULL };
 134 
 135 static char * argv[] = { "-/bin/sh",NULL };
 136 static char * envp[] = { "HOME=/usr/root", NULL, NULL };
 137 
 138 struct drive_info { char dummy[32]; } drive_info;
 139 
 140 void start_kernel(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142 /*
 143  * Interrupts are still disabled. Do necessary setups, then
 144  * enable them
 145  */
 146         ROOT_DEV = ORIG_ROOT_DEV;
 147         sprintf(term, "TERM=con%dx%d", CON_COLS, CON_ROWS);
 148         envp[1] = term; 
 149         envp_rc[1] = term;
 150         envp_init[1] = term;
 151         drive_info = DRIVE_INFO;
 152         memory_end = (1<<20) + (EXT_MEM_K<<10);
 153         memory_end &= 0xfffff000;
 154         if (memory_end > 16*1024*1024)
 155                 memory_end = 16*1024*1024;
 156         if (memory_end >= 12*1024*1024) 
 157                 buffer_memory_end = 4*1024*1024;
 158         else if (memory_end >= 6*1024*1024)
 159                 buffer_memory_end = 2*1024*1024;
 160         else if (memory_end >= 4*1024*1024)
 161                 buffer_memory_end = 3*512*1024;
 162         else
 163                 buffer_memory_end = 1*1024*1024;
 164         main_memory_start = buffer_memory_end;
 165 #ifdef RAMDISK
 166         main_memory_start += rd_init(main_memory_start, RAMDISK*1024);
 167 #endif
 168         mem_init(main_memory_start,memory_end);
 169         trap_init();
 170         sched_init();
 171         chr_dev_init();
 172         blk_dev_init();
 173         time_init();
 174         printk("Linux version " UTS_RELEASE " " __DATE__ " " __TIME__ "\n");
 175         buffer_init(buffer_memory_end);
 176         hd_init();
 177         floppy_init();
 178         sock_init();
 179         sti();
 180 #ifdef CONFIG_SCSI
 181         scsi_dev_init();
 182 #endif
 183         move_to_user_mode();
 184         if (!fork()) {          /* we count on this going ok */
 185                 init();
 186         }
 187 /*
 188  *   NOTE!!   For any other task 'pause()' would mean we have to get a
 189  * signal to awaken, but task0 is the sole exception (see 'schedule()')
 190  * as task 0 gets activated at every idle moment (when no other tasks
 191  * can run). For task0 'pause()' just means we go check if some other
 192  * task can run, and if not we return here.
 193  */
 194         for(;;)
 195                 __asm__("int $0x80"::"a" (__NR_pause):"ax");
 196 }
 197 
 198 static int printf(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 199 {
 200         va_list args;
 201         int i;
 202 
 203         va_start(args, fmt);
 204         write(1,printbuf,i=vsprintf(printbuf, fmt, args));
 205         va_end(args);
 206         return i;
 207 }
 208 
 209 void init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211         int pid,i;
 212 
 213         setup((void *) &drive_info);
 214         (void) open("/dev/tty1",O_RDWR,0);
 215         (void) dup(0);
 216         (void) dup(0);
 217         printf("%d buffers = %d bytes buffer space\n\r",NR_BUFFERS,
 218                 NR_BUFFERS*BLOCK_SIZE);
 219         printf("Free mem: %d bytes\n\r",memory_end-main_memory_start);
 220 
 221         execve("/etc/init",argv_init,envp_init);
 222         execve("/bin/init",argv_init,envp_init);
 223         /* if this fails, fall through to original stuff */
 224 
 225         if (!(pid=fork())) {
 226                 close(0);
 227                 if (open("/etc/rc",O_RDONLY,0))
 228                         _exit(1);
 229                 execve("/bin/sh",argv_rc,envp_rc);
 230                 _exit(2);
 231         }
 232         if (pid>0)
 233                 while (pid != wait(&i))
 234                         /* nothing */;
 235         while (1) {
 236                 if ((pid=fork())<0) {
 237                         printf("Fork failed in init\r\n");
 238                         continue;
 239                 }
 240                 if (!pid) {
 241                         close(0);close(1);close(2);
 242                         setsid();
 243                         (void) open("/dev/tty1",O_RDWR,0);
 244                         (void) dup(0);
 245                         (void) dup(0);
 246                         _exit(execve("/bin/sh",argv,envp));
 247                 }
 248                 while (1)
 249                         if (pid == wait(&i))
 250                                 break;
 251                 printf("\n\rchild %d died with code %04x\n\r",pid,i);
 252                 sync();
 253         }
 254         _exit(0);       /* NOTE! _exit, not exit() */
 255 }

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