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