|
|
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.1.8 ! root 110: p->flags &= ~PF_PTRACED;
1.1 root 111: p->pid = last_pid;
1.1.1.8 ! root 112: p->p_pptr = p->p_opptr = current;
1.1.1.7 root 113: p->p_cptr = NULL;
1.1.1.8 ! root 114: SET_LINKS(p);
1.1 root 115: p->counter = p->priority;
116: p->signal = 0;
1.1.1.8 ! root 117: p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
! 118: p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
1.1 root 119: p->leader = 0; /* process leadership doesn't inherit */
120: p->utime = p->stime = 0;
121: p->cutime = p->cstime = 0;
1.1.1.6 root 122: p->min_flt = p->maj_flt = 0;
123: p->cmin_flt = p->cmaj_flt = 0;
1.1 root 124: p->start_time = jiffies;
125: p->tss.back_link = 0;
126: p->tss.esp0 = PAGE_SIZE + (long) p;
127: p->tss.ss0 = 0x10;
128: p->tss.eip = eip;
129: p->tss.eflags = eflags;
130: p->tss.eax = 0;
131: p->tss.ecx = ecx;
132: p->tss.edx = edx;
133: p->tss.ebx = ebx;
134: p->tss.esp = esp;
135: p->tss.ebp = ebp;
136: p->tss.esi = esi;
137: p->tss.edi = edi;
138: p->tss.es = es & 0xffff;
139: p->tss.cs = cs & 0xffff;
140: p->tss.ss = ss & 0xffff;
141: p->tss.ds = ds & 0xffff;
142: p->tss.fs = fs & 0xffff;
143: p->tss.gs = gs & 0xffff;
144: p->tss.ldt = _LDT(nr);
1.1.1.7 root 145: p->tss.trace_bitmap = offsetof(struct tss_struct,io_bitmap) << 16;
146: for (i = 0; i<IO_BITMAP_SIZE ; i++)
147: p->tss.io_bitmap[i] = ~0;
1.1 root 148: if (last_task_used_math == current)
1.1.1.4 root 149: __asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
1.1 root 150: if (copy_mem(nr,p)) {
1.1.1.3 root 151: task[nr] = NULL;
1.1.1.8 ! root 152: REMOVE_LINKS(p);
1.1 root 153: free_page((long) p);
154: return -EAGAIN;
155: }
156: for (i=0; i<NR_OPEN;i++)
157: if (f=p->filp[i])
158: f->f_count++;
159: if (current->pwd)
160: current->pwd->i_count++;
161: if (current->root)
162: current->root->i_count++;
1.1.1.3 root 163: if (current->executable)
164: current->executable->i_count++;
1.1.1.7 root 165: for (i=0; i < current->numlibraries ; i++)
166: if (current->libraries[i].library)
167: current->libraries[i].library->i_count++;
1.1 root 168: set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
169: set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
1.1.1.3 root 170: p->state = TASK_RUNNING; /* do this last, just in case */
1.1.1.5 root 171: return p->pid;
1.1 root 172: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.