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

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

unix.superglobalmegacorp.com

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