Annotation of linux/kernel/fork.c, revision 1.1.1.5

1.1       root        1: /*
1.1.1.2   root        2:  *  linux/kernel/fork.c
                      3:  *
                      4:  *  (C) 1991  Linus Torvalds
                      5:  */
                      6: 
                      7: /*
1.1       root        8:  *  'fork.c' contains the help-routines for the 'fork' system call
                      9:  * (see also system_call.s), and some misc functions ('verify_area').
                     10:  * Fork is rather simple, once you get the hang of it, but the memory
                     11:  * management can be a bitch. See 'mm/mm.c': 'copy_page_tables()'
                     12:  */
                     13: #include <errno.h>
                     14: 
                     15: #include <linux/sched.h>
                     16: #include <linux/kernel.h>
                     17: #include <asm/segment.h>
                     18: #include <asm/system.h>
                     19: 
                     20: extern void write_verify(unsigned long address);
                     21: 
                     22: long last_pid=0;
                     23: 
                     24: void verify_area(void * addr,int size)
                     25: {
                     26:        unsigned long start;
                     27: 
                     28:        start = (unsigned long) addr;
                     29:        size += start & 0xfff;
                     30:        start &= 0xfffff000;
                     31:        start += get_base(current->ldt[2]);
                     32:        while (size>0) {
                     33:                size -= 4096;
                     34:                write_verify(start);
                     35:                start += 4096;
                     36:        }
                     37: }
                     38: 
                     39: int copy_mem(int nr,struct task_struct * p)
                     40: {
                     41:        unsigned long old_data_base,new_data_base,data_limit;
                     42:        unsigned long old_code_base,new_code_base,code_limit;
                     43: 
                     44:        code_limit=get_limit(0x0f);
                     45:        data_limit=get_limit(0x17);
                     46:        old_code_base = get_base(current->ldt[1]);
                     47:        old_data_base = get_base(current->ldt[2]);
1.1.1.5 ! root       48:        if (old_data_base != old_code_base) {
        !            49:                printk("ldt[0]: %08x %08x\n",current->ldt[0].a,current->ldt[0].b);
        !            50:                printk("ldt[1]: %08x %08x\n",current->ldt[1].a,current->ldt[1].b);
        !            51:                printk("ldt[2]: %08x %08x\n",current->ldt[2].a,current->ldt[2].b);
1.1       root       52:                panic("We don't support separate I&D");
1.1.1.5 ! root       53:        }
1.1       root       54:        if (data_limit < code_limit)
                     55:                panic("Bad data_limit");
1.1.1.4   root       56:        new_data_base = new_code_base = nr * TASK_SIZE;
1.1.1.3   root       57:        p->start_code = new_code_base;
1.1       root       58:        set_base(p->ldt[1],new_code_base);
                     59:        set_base(p->ldt[2],new_data_base);
                     60:        if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
                     61:                free_page_tables(new_data_base,data_limit);
                     62:                return -ENOMEM;
                     63:        }
                     64:        return 0;
                     65: }
                     66: 
1.1.1.5 ! root       67: static int find_empty_process(void)
        !            68: {
        !            69:        int i;
        !            70: 
        !            71:        repeat:
        !            72:                if ((++last_pid)<0) last_pid=1;
        !            73:                for(i=0 ; i<NR_TASKS ; i++)
        !            74:                        if (task[i] && ((task[i]->pid == last_pid) ||
        !            75:                                        (task[i]->pgrp == last_pid)))
        !            76:                                goto repeat;
        !            77:        for(i=1 ; i<NR_TASKS ; i++)
        !            78:                if (!task[i])
        !            79:                        return i;
        !            80:        return -EAGAIN;
        !            81: }
        !            82: 
1.1       root       83: /*
                     84:  *  Ok, this is the main fork-routine. It copies the system process
                     85:  * information (task[nr]) and sets up the necessary registers. It
                     86:  * also copies the data segment in it's entirety.
                     87:  */
1.1.1.5 ! root       88: int sys_fork(long ebx,long ecx,long edx,
        !            89:                long esi, long edi, long ebp, long eax, long ds,
        !            90:                long es, long fs, long gs, long orig_eax,
1.1       root       91:                long eip,long cs,long eflags,long esp,long ss)
                     92: {
                     93:        struct task_struct *p;
1.1.1.5 ! root       94:        int i,nr;
1.1       root       95:        struct file *f;
                     96: 
                     97:        p = (struct task_struct *) get_free_page();
                     98:        if (!p)
                     99:                return -EAGAIN;
1.1.1.5 ! root      100:        nr = find_empty_process();
        !           101:        if (nr < 0) {
        !           102:                free_page((unsigned long) p);
        !           103:                return nr;
        !           104:        }
1.1.1.3   root      105:        task[nr] = p;
1.1       root      106:        *p = *current;  /* NOTE! this doesn't copy the supervisor stack */
1.1.1.3   root      107:        p->state = TASK_UNINTERRUPTIBLE;
1.1       root      108:        p->pid = last_pid;
                    109:        p->counter = p->priority;
                    110:        p->signal = 0;
                    111:        p->alarm = 0;
                    112:        p->leader = 0;          /* process leadership doesn't inherit */
                    113:        p->utime = p->stime = 0;
                    114:        p->cutime = p->cstime = 0;
                    115:        p->start_time = jiffies;
                    116:        p->tss.back_link = 0;
                    117:        p->tss.esp0 = PAGE_SIZE + (long) p;
                    118:        p->tss.ss0 = 0x10;
                    119:        p->tss.eip = eip;
                    120:        p->tss.eflags = eflags;
                    121:        p->tss.eax = 0;
                    122:        p->tss.ecx = ecx;
                    123:        p->tss.edx = edx;
                    124:        p->tss.ebx = ebx;
                    125:        p->tss.esp = esp;
                    126:        p->tss.ebp = ebp;
                    127:        p->tss.esi = esi;
                    128:        p->tss.edi = edi;
                    129:        p->tss.es = es & 0xffff;
                    130:        p->tss.cs = cs & 0xffff;
                    131:        p->tss.ss = ss & 0xffff;
                    132:        p->tss.ds = ds & 0xffff;
                    133:        p->tss.fs = fs & 0xffff;
                    134:        p->tss.gs = gs & 0xffff;
                    135:        p->tss.ldt = _LDT(nr);
                    136:        p->tss.trace_bitmap = 0x80000000;
                    137:        if (last_task_used_math == current)
1.1.1.4   root      138:                __asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
1.1       root      139:        if (copy_mem(nr,p)) {
1.1.1.3   root      140:                task[nr] = NULL;
1.1       root      141:                free_page((long) p);
                    142:                return -EAGAIN;
                    143:        }
                    144:        for (i=0; i<NR_OPEN;i++)
                    145:                if (f=p->filp[i])
                    146:                        f->f_count++;
                    147:        if (current->pwd)
                    148:                current->pwd->i_count++;
                    149:        if (current->root)
                    150:                current->root->i_count++;
1.1.1.3   root      151:        if (current->executable)
                    152:                current->executable->i_count++;
1.1.1.4   root      153:        if (current->library)
                    154:                current->library->i_count++;
1.1       root      155:        set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
                    156:        set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
1.1.1.4   root      157:        p->p_pptr = current;
                    158:        p->p_cptr = 0;
                    159:        p->p_ysptr = 0;
                    160:        p->p_osptr = current->p_cptr;
                    161:        if (p->p_osptr)
                    162:                p->p_osptr->p_ysptr = p;
                    163:        current->p_cptr = p;
1.1.1.3   root      164:        p->state = TASK_RUNNING;        /* do this last, just in case */
1.1.1.5 ! root      165:        return p->pid;
1.1       root      166: }

unix.superglobalmegacorp.com

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