This source file includes following definitions.
- verify_area
- copy_mem
- find_empty_process
- sys_fork
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/stddef.h>
19
20 #include <asm/segment.h>
21 #include <asm/system.h>
22
23 #define MAX_TASKS_PER_USER ((NR_TASKS/4)*3)
24
25 long last_pid=0;
26
27 void verify_area(void * addr,int size)
28 {
29 unsigned long start;
30
31 start = (unsigned long) addr;
32 size += start & 0xfff;
33 start &= 0xfffff000;
34 start += get_base(current->ldt[2]);
35 while (size>0) {
36 size -= 4096;
37 write_verify(start);
38 start += 4096;
39 }
40 }
41
42 int copy_mem(int nr,struct task_struct * p)
43 {
44 unsigned long old_data_base,new_data_base,data_limit;
45 unsigned long old_code_base,new_code_base,code_limit;
46
47 code_limit = get_limit(0x0f);
48 data_limit = get_limit(0x17);
49 old_code_base = get_base(current->ldt[1]);
50 old_data_base = get_base(current->ldt[2]);
51 if (old_data_base != old_code_base) {
52 printk("ldt[0]: %08x %08x\n",current->ldt[0].a,current->ldt[0].b);
53 printk("ldt[1]: %08x %08x\n",current->ldt[1].a,current->ldt[1].b);
54 printk("ldt[2]: %08x %08x\n",current->ldt[2].a,current->ldt[2].b);
55 panic("We don't support separate I&D");
56 }
57 if (data_limit < code_limit)
58 panic("Bad data_limit");
59 new_data_base = old_data_base;
60 new_code_base = old_code_base;
61 p->start_code = new_code_base;
62 set_base(p->ldt[1],new_code_base);
63 set_base(p->ldt[2],new_data_base);
64 return copy_page_tables(p);
65 }
66
67 static int find_empty_process(void)
68 {
69 int i, task_nr;
70 int this_user_tasks = 0;
71
72 repeat:
73 if ((++last_pid) & 0xffff0000)
74 last_pid=1;
75 for(i=0 ; i < NR_TASKS ; i++) {
76 if (!task[i])
77 continue;
78 if (task[i]->uid == current->uid)
79 this_user_tasks++;
80 if (task[i]->pid == last_pid || task[i]->pgrp == last_pid)
81 goto repeat;
82 }
83 if (this_user_tasks > MAX_TASKS_PER_USER && !suser())
84 return -EAGAIN;
85
86 task_nr = 0;
87 for(i=1 ; i<NR_TASKS ; i++)
88 if (!task[i])
89 if (task_nr)
90 return task_nr;
91 else
92 task_nr = i;
93 if (task_nr && suser())
94 return task_nr;
95 return -EAGAIN;
96 }
97
98
99
100
101
102
103 int sys_fork(long ebx,long ecx,long edx,
104 long esi, long edi, long ebp, long eax, long ds,
105 long es, long fs, long gs, long orig_eax,
106 long eip,long cs,long eflags,long esp,long ss)
107 {
108 struct task_struct *p;
109 int i,nr;
110 struct file *f;
111
112 p = (struct task_struct *) get_free_page(GFP_KERNEL);
113 if (!p)
114 return -EAGAIN;
115 nr = find_empty_process();
116 if (nr < 0) {
117 free_page((unsigned long) p);
118 return nr;
119 }
120 task[nr] = p;
121 *p = *current;
122 p->wait.task = p;
123 p->wait.next = NULL;
124 p->state = TASK_UNINTERRUPTIBLE;
125 p->flags &= ~PF_PTRACED;
126 p->pid = last_pid;
127 if (p->pid > 1)
128 p->swappable = 1;
129 p->p_pptr = p->p_opptr = current;
130 p->p_cptr = NULL;
131 SET_LINKS(p);
132 p->counter = p->priority;
133 p->signal = 0;
134 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
135 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
136 p->leader = 0;
137 p->utime = p->stime = 0;
138 p->cutime = p->cstime = 0;
139 p->min_flt = p->maj_flt = 0;
140 p->cmin_flt = p->cmaj_flt = 0;
141 p->start_time = jiffies;
142 p->tss.back_link = 0;
143 p->tss.esp0 = PAGE_SIZE + (long) p;
144 p->tss.ss0 = 0x10;
145 p->tss.eip = eip;
146 p->tss.eflags = eflags & 0xffffcfff;
147 p->tss.eax = 0;
148 p->tss.ecx = ecx;
149 p->tss.edx = edx;
150 p->tss.ebx = ebx;
151 p->tss.esp = esp;
152 p->tss.ebp = ebp;
153 p->tss.esi = esi;
154 p->tss.edi = edi;
155 p->tss.es = es & 0xffff;
156 p->tss.cs = cs & 0xffff;
157 p->tss.ss = ss & 0xffff;
158 p->tss.ds = ds & 0xffff;
159 p->tss.fs = fs & 0xffff;
160 p->tss.gs = gs & 0xffff;
161 p->tss.ldt = _LDT(nr);
162 p->tss.trace_bitmap = offsetof(struct tss_struct,io_bitmap) << 16;
163 for (i = 0; i<IO_BITMAP_SIZE ; i++)
164 p->tss.io_bitmap[i] = ~0;
165 if (last_task_used_math == current)
166 __asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
167 if (copy_mem(nr,p)) {
168 task[nr] = NULL;
169 REMOVE_LINKS(p);
170 free_page((long) p);
171 return -EAGAIN;
172 }
173 for (i=0; i<NR_OPEN;i++)
174 if (f=p->filp[i])
175 f->f_count++;
176 if (current->pwd)
177 current->pwd->i_count++;
178 if (current->root)
179 current->root->i_count++;
180 if (current->executable)
181 current->executable->i_count++;
182 for (i=0; i < current->numlibraries ; i++)
183 if (current->libraries[i].library)
184 current->libraries[i].library->i_count++;
185 set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
186 set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
187 p->state = TASK_RUNNING;
188 return p->pid;
189 }