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

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

unix.superglobalmegacorp.com

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