|
|
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]);
48: if (old_data_base != old_code_base)
49: panic("We don't support separate I&D");
50: if (data_limit < code_limit)
51: panic("Bad data_limit");
1.1.1.4 ! root 52: new_data_base = new_code_base = nr * TASK_SIZE;
1.1.1.3 root 53: p->start_code = new_code_base;
1.1 root 54: set_base(p->ldt[1],new_code_base);
55: set_base(p->ldt[2],new_data_base);
56: if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
57: free_page_tables(new_data_base,data_limit);
58: return -ENOMEM;
59: }
60: return 0;
61: }
62:
63: /*
64: * Ok, this is the main fork-routine. It copies the system process
65: * information (task[nr]) and sets up the necessary registers. It
66: * also copies the data segment in it's entirety.
67: */
68: int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
1.1.1.4 ! root 69: long ebx,long ecx,long edx, long orig_eax,
1.1 root 70: long fs,long es,long ds,
71: long eip,long cs,long eflags,long esp,long ss)
72: {
73: struct task_struct *p;
74: int i;
75: struct file *f;
76:
77: p = (struct task_struct *) get_free_page();
78: if (!p)
79: return -EAGAIN;
1.1.1.3 root 80: task[nr] = p;
1.1 root 81: *p = *current; /* NOTE! this doesn't copy the supervisor stack */
1.1.1.3 root 82: p->state = TASK_UNINTERRUPTIBLE;
1.1 root 83: p->pid = last_pid;
84: p->counter = p->priority;
85: p->signal = 0;
86: p->alarm = 0;
87: p->leader = 0; /* process leadership doesn't inherit */
88: p->utime = p->stime = 0;
89: p->cutime = p->cstime = 0;
90: p->start_time = jiffies;
91: p->tss.back_link = 0;
92: p->tss.esp0 = PAGE_SIZE + (long) p;
93: p->tss.ss0 = 0x10;
94: p->tss.eip = eip;
95: p->tss.eflags = eflags;
96: p->tss.eax = 0;
97: p->tss.ecx = ecx;
98: p->tss.edx = edx;
99: p->tss.ebx = ebx;
100: p->tss.esp = esp;
101: p->tss.ebp = ebp;
102: p->tss.esi = esi;
103: p->tss.edi = edi;
104: p->tss.es = es & 0xffff;
105: p->tss.cs = cs & 0xffff;
106: p->tss.ss = ss & 0xffff;
107: p->tss.ds = ds & 0xffff;
108: p->tss.fs = fs & 0xffff;
109: p->tss.gs = gs & 0xffff;
110: p->tss.ldt = _LDT(nr);
111: p->tss.trace_bitmap = 0x80000000;
112: if (last_task_used_math == current)
1.1.1.4 ! root 113: __asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
1.1 root 114: if (copy_mem(nr,p)) {
1.1.1.3 root 115: task[nr] = NULL;
1.1 root 116: free_page((long) p);
117: return -EAGAIN;
118: }
119: for (i=0; i<NR_OPEN;i++)
120: if (f=p->filp[i])
121: f->f_count++;
122: if (current->pwd)
123: current->pwd->i_count++;
124: if (current->root)
125: current->root->i_count++;
1.1.1.3 root 126: if (current->executable)
127: current->executable->i_count++;
1.1.1.4 ! root 128: if (current->library)
! 129: current->library->i_count++;
1.1 root 130: set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
131: set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
1.1.1.4 ! root 132: p->p_pptr = current;
! 133: p->p_cptr = 0;
! 134: p->p_ysptr = 0;
! 135: p->p_osptr = current->p_cptr;
! 136: if (p->p_osptr)
! 137: p->p_osptr->p_ysptr = p;
! 138: current->p_cptr = p;
1.1.1.3 root 139: p->state = TASK_RUNNING; /* do this last, just in case */
1.1 root 140: return last_pid;
141: }
142:
143: int find_empty_process(void)
144: {
145: int i;
146:
147: repeat:
148: if ((++last_pid)<0) last_pid=1;
149: for(i=0 ; i<NR_TASKS ; i++)
1.1.1.4 ! root 150: if (task[i] && ((task[i]->pid == last_pid) ||
! 151: (task[i]->pgrp == last_pid)))
! 152: goto repeat;
1.1 root 153: for(i=1 ; i<NR_TASKS ; i++)
154: if (!task[i])
155: return i;
156: return -EAGAIN;
157: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.