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