Annotation of linux/init/main.c, revision 1.1.1.8

1.1.1.2   root        1: /*
                      2:  *  linux/init/main.c
                      3:  *
                      4:  *  (C) 1991  Linus Torvalds
                      5:  */
                      6: 
1.1.1.7   root        7: #include <stddef.h>
                      8: #include <stdarg.h>
                      9: #include <fcntl.h>
1.1       root       10: #include <time.h>
                     11: 
1.1.1.7   root       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: 
1.1       root       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)
                     36: static inline _syscall0(int,pause)
1.1.1.2   root       37: static inline _syscall1(int,setup,void *,BIOS)
1.1       root       38: static inline _syscall0(int,sync)
1.1.1.7   root       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)
1.1       root       45: 
1.1.1.7   root       46: static inline pid_t wait(int * wait_stat)
                     47: {
                     48:        return waitpid(-1,wait_stat,0);
                     49: }
1.1.1.4   root       50: 
1.1       root       51: static char printbuf[1024];
                     52: 
                     53: extern int vsprintf();
                     54: extern void init(void);
1.1.1.2   root       55: extern void blk_dev_init(void);
1.1.1.8 ! root       56: extern long chr_dev_init(long);
1.1       root       57: extern void hd_init(void);
1.1.1.2   root       58: extern void floppy_init(void);
1.1.1.7   root       59: extern void sock_init(void);
1.1.1.2   root       60: extern void mem_init(long start, long end);
1.1.1.3   root       61: extern long rd_init(long mem_start, int length);
1.1       root       62: extern long kernel_mktime(struct tm * tm);
1.1.1.4   root       63: 
1.1.1.7   root       64: #ifdef CONFIG_SCSI
                     65: extern void scsi_dev_init(void);
                     66: #endif
                     67: 
1.1.1.4   root       68: static int sprintf(char * str, const char *fmt, ...)
                     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: }
1.1       root       78: 
                     79: /*
1.1.1.2   root       80:  * This is set up by the setup-routine at boot-time
                     81:  */
                     82: #define EXT_MEM_K (*(unsigned short *)0x90002)
1.1.1.4   root       83: #define CON_ROWS ((*(unsigned short *)0x9000e) & 0xff)
                     84: #define CON_COLS (((*(unsigned short *)0x9000e) & 0xff00) >> 8)
1.1.1.2   root       85: #define DRIVE_INFO (*(struct drive_info *)0x90080)
                     86: #define ORIG_ROOT_DEV (*(unsigned short *)0x901FC)
                     87: 
                     88: /*
1.1       root       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)
                    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);
1.1.1.2   root      111:                time.tm_mon = CMOS_READ(8);
1.1       root      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);
1.1.1.2   root      120:        time.tm_mon--;
1.1       root      121:        startup_time = kernel_mktime(&time);
                    122: }
                    123: 
1.1.1.2   root      124: static long memory_end = 0;
                    125: static long buffer_memory_end = 0;
1.1.1.3   root      126: static long main_memory_start = 0;
1.1.1.4   root      127: static char term[32];
                    128: 
1.1.1.5   root      129: static char * argv_init[] = { "/bin/init", NULL };
                    130: static char * envp_init[] = { "HOME=/", NULL, NULL };
                    131: 
1.1.1.4   root      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 };
1.1.1.2   root      137: 
                    138: struct drive_info { char dummy[32]; } drive_info;
                    139: 
1.1.1.5   root      140: void start_kernel(void)
                    141: {
1.1       root      142: /*
                    143:  * Interrupts are still disabled. Do necessary setups, then
                    144:  * enable them
                    145:  */
1.1.1.2   root      146:        ROOT_DEV = ORIG_ROOT_DEV;
1.1.1.4   root      147:        sprintf(term, "TERM=con%dx%d", CON_COLS, CON_ROWS);
                    148:        envp[1] = term; 
                    149:        envp_rc[1] = term;
1.1.1.5   root      150:        envp_init[1] = term;
1.1.1.2   root      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;
1.1.1.5   root      156:        if (memory_end >= 12*1024*1024) 
1.1.1.3   root      157:                buffer_memory_end = 4*1024*1024;
1.1.1.5   root      158:        else if (memory_end >= 6*1024*1024)
1.1.1.2   root      159:                buffer_memory_end = 2*1024*1024;
1.1.1.5   root      160:        else if (memory_end >= 4*1024*1024)
                    161:                buffer_memory_end = 3*512*1024;
1.1.1.2   root      162:        else
                    163:                buffer_memory_end = 1*1024*1024;
1.1.1.3   root      164:        main_memory_start = buffer_memory_end;
                    165: #ifdef RAMDISK
                    166:        main_memory_start += rd_init(main_memory_start, RAMDISK*1024);
                    167: #endif
1.1       root      168:        trap_init();
1.1.1.7   root      169:        sched_init();
1.1.1.8 ! root      170:        main_memory_start = chr_dev_init(main_memory_start);
1.1.1.7   root      171:        blk_dev_init();
1.1.1.8 ! root      172:        mem_init(main_memory_start,memory_end);
1.1.1.2   root      173:        time_init();
1.1.1.7   root      174:        printk("Linux version " UTS_RELEASE " " __DATE__ " " __TIME__ "\n");
1.1.1.2   root      175:        buffer_init(buffer_memory_end);
1.1       root      176:        hd_init();
1.1.1.2   root      177:        floppy_init();
1.1.1.7   root      178:        sock_init();
1.1       root      179:        sti();
1.1.1.7   root      180: #ifdef CONFIG_SCSI
                    181:        scsi_dev_init();
                    182: #endif
1.1       root      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:  */
1.1.1.4   root      194:        for(;;)
                    195:                __asm__("int $0x80"::"a" (__NR_pause):"ax");
1.1       root      196: }
                    197: 
                    198: static int printf(const char *fmt, ...)
                    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)
                    210: {
1.1.1.3   root      211:        int pid,i;
1.1       root      212: 
1.1.1.2   root      213:        setup((void *) &drive_info);
1.1.1.4   root      214:        (void) open("/dev/tty1",O_RDWR,0);
1.1       root      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);
1.1.1.3   root      219:        printf("Free mem: %d bytes\n\r",memory_end-main_memory_start);
1.1.1.5   root      220: 
1.1.1.6   root      221:        execve("/etc/init",argv_init,envp_init);
1.1.1.5   root      222:        execve("/bin/init",argv_init,envp_init);
                    223:        /* if this fails, fall through to original stuff */
                    224: 
1.1.1.3   root      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();
1.1.1.4   root      243:                        (void) open("/dev/tty1",O_RDWR,0);
1.1.1.3   root      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();
1.1       root      253:        }
                    254:        _exit(0);       /* NOTE! _exit, not exit() */
                    255: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.