kern

view src/proc.c @ 73:b4b7198986a6

fixed a potential null dereference when deleting a bug in the redblack tree
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 15 Oct 2011 08:06:10 +0300
parents b45e2d5f0ae1
children 8b21fe04ba2c
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <errno.h>
5 #include "config.h"
6 #include "proc.h"
7 #include "tss.h"
8 #include "vm.h"
9 #include "segm.h"
10 #include "intr.h"
11 #include "panic.h"
12 #include "syscall.h"
13 #include "sched.h"
14 #include "tss.h"
15 #include "kdef.h"
17 #define FLAGS_INTR_BIT (1 << 9)
19 static void start_first_proc(void);
21 /* defined in proc-asm.S */
22 uint32_t switch_stack(uint32_t new_stack, uint32_t *old_stack);
23 void just_forked(void);
25 /* defined in test_proc.S */
26 void test_proc(void);
27 void test_proc_end(void);
29 static struct process proc[MAX_PROC];
31 /* cur_pid: pid of the currently executing process.
32 * when we're in the idle process cur_pid will be 0.
33 * last_pid: pid of the last real process that was running, this should
34 * never become 0. Essentially this defines the active kernel stack.
35 */
36 static int cur_pid, last_pid;
38 static struct task_state *tss;
41 void init_proc(void)
42 {
43 int tss_page;
45 /* allocate a page for the task state segment, to make sure
46 * it doesn't cross page boundaries
47 */
48 if((tss_page = pgalloc(1, MEM_KERNEL)) == -1) {
49 panic("failed to allocate memory for the task state segment\n");
50 }
51 tss = (struct task_state*)PAGE_TO_ADDR(tss_page);
53 /* the kernel stack segment never changes so we might as well set it now
54 * the only other thing that we use in the tss is the kernel stack pointer
55 * which is different for each process, and thus managed by context_switch
56 */
57 memset(tss, 0, sizeof *tss);
58 tss->ss0 = selector(SEGM_KDATA, 0);
60 set_tss((uint32_t)tss);
62 /* initialize system call handler (see syscall.c) */
63 init_syscall();
65 start_first_proc(); /* XXX never returns */
66 }
68 static void start_first_proc(void)
69 {
70 struct process *p;
71 int proc_size_pg, img_start_pg, stack_pg;
72 uint32_t img_start_addr;
73 struct intr_frame ifrm;
75 /* prepare the first process */
76 p = proc + 1;
77 p->id = 1;
78 p->parent = 0; /* no parent for init */
80 p->ticks_left = TIMESLICE_TICKS;
81 p->next = p->prev = 0;
83 /* the first process may keep this existing page table */
84 p->ctx.pgtbl_paddr = get_pgdir_addr();
86 /* allocate a chunk of memory for the process image
87 * and copy the code of test_proc there.
88 */
89 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
90 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
91 panic("failed to allocate space for the init process image\n");
92 }
93 img_start_addr = PAGE_TO_ADDR(img_start_pg);
94 memcpy((void*)img_start_addr, test_proc, proc_size_pg * PGSIZE);
95 printf("copied init process at: %x\n", img_start_addr);
97 /* allocate the first page of the user stack */
98 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
99 if(pgalloc_vrange(stack_pg, 1) == -1) {
100 panic("failed to allocate user stack page\n");
101 }
102 p->user_stack_pg = stack_pg;
104 /* allocate a kernel stack for this process */
105 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
106 panic("failed to allocate kernel stack for the init process\n");
107 }
108 /* when switching from user space to kernel space, the ss0:esp0 from TSS
109 * will be used to switch to the per-process kernel stack, so we need to
110 * set it correctly before switching to user space.
111 * tss->ss0 is already set in init_proc above.
112 */
113 tss->esp0 = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
116 /* now we need to fill in the fake interrupt stack frame */
117 memset(&ifrm, 0, sizeof ifrm);
118 /* after the priviledge switch, this ss:esp will be used in userspace */
119 ifrm.esp = PAGE_TO_ADDR(stack_pg) + PGSIZE;
120 ifrm.ss = selector(SEGM_UDATA, 3);
121 /* instruction pointer at the beginning of the process image */
122 ifrm.eip = img_start_addr;
123 ifrm.cs = selector(SEGM_UCODE, 3);
124 /* make sure the user will run with interrupts enabled */
125 ifrm.eflags = FLAGS_INTR_BIT;
126 /* user data selectors should all be the same */
127 ifrm.ds = ifrm.es = ifrm.fs = ifrm.gs = ifrm.ss;
129 /* add it to the scheduler queues */
130 add_proc(p->id);
132 /* make it current */
133 set_current_pid(p->id);
135 /* build the current vm map */
136 cons_vmmap(&p->vmmap);
138 /* execute a fake return from interrupt with the fake stack frame */
139 intr_ret(ifrm);
140 }
142 int sys_fork(void)
143 {
144 int i, pid;
145 struct process *p, *parent;
147 disable_intr();
149 /* find a free process slot */
150 /* TODO don't search up to MAX_PROC if uid != 0 */
151 pid = -1;
152 for(i=1; i<MAX_PROC; i++) {
153 if(proc[i].id == 0) {
154 pid = i;
155 break;
156 }
157 }
159 if(pid == -1) {
160 /* process table full */
161 return -EAGAIN;
162 }
165 p = proc + pid;
166 parent = get_current_proc();
168 /* allocate a kernel stack for the new process */
169 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
170 return -EAGAIN;
171 }
172 p->ctx.stack_ptr = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
173 /* we need to copy the current interrupt frame to the new kernel stack so
174 * that the new process will return to the same point as the parent, just
175 * after the fork syscall.
176 */
177 p->ctx.stack_ptr -= sizeof(struct intr_frame);
178 memcpy((void*)p->ctx.stack_ptr, get_intr_frame(), sizeof(struct intr_frame));
179 /* child's return from fork returns 0 */
180 ((struct intr_frame*)p->ctx.stack_ptr)->regs.eax = 0;
182 /* we also need the address of just_forked in the stack, so that switch_stacks
183 * called from context_switch, will return to just_forked when we first switch
184 * to a newly forked process. just_forked then just calls intr_ret to return to
185 * userspace with the already constructed interrupt frame (see above).
186 */
187 p->ctx.stack_ptr -= 4;
188 *(uint32_t*)p->ctx.stack_ptr = (uint32_t)just_forked;
190 /* initialize the rest of the process structure */
191 p->id = pid;
192 p->parent = parent->id;
193 p->child_list = 0;
194 p->next = p->prev = 0;
196 /* add to the child list */
197 p->sib_next = parent->child_list;
198 parent->child_list = p;
200 /* will be copied on write */
201 p->user_stack_pg = parent->user_stack_pg;
203 /* clone the parent's virtual memory */
204 clone_vm(p, parent, CLONE_COW);
206 /* done, now let's add it to the scheduler runqueue */
207 add_proc(p->id);
209 return pid;
210 }
212 int sys_exit(int status)
213 {
214 struct process *p, *child;
216 p = get_current_proc();
218 /* TODO deliver SIGCHLD to the parent */
220 /* find any child processes and make init adopt them */
221 child = p->child_list;
222 while(child) {
223 child->parent = 1;
224 child = child->sib_next;
225 }
227 cleanup_vm(p);
229 /* remove it from the runqueue */
230 remove_proc(p->id);
232 /* make it a zombie until its parent reaps it */
233 p->state = STATE_ZOMBIE;
234 p->exit_status = (status & _WSTATUS_MASK) | (_WREASON_EXITED << _WREASON_SHIFT);
236 /* wakeup any processes waiting for it
237 * we're waking up the parent's address, because waitpid waits
238 * on it's own process struct, not knowing which child will die
239 * first.
240 */
241 wakeup(get_process(p->parent));
242 return 0;
243 }
245 int sys_waitpid(int pid, int *status, int opt)
246 {
247 struct process *p, *child;
249 p = get_current_proc();
251 restart:
252 if(pid <= 0) {
253 /* search for zombie children */
254 child = p->child_list;
255 while(child) {
256 if(child->state == STATE_ZOMBIE) {
257 break;
258 }
259 child = child->sib_next;
260 }
261 } else {
262 if(!(child = get_process(pid)) || child->parent != p->id) {
263 return -ECHILD;
264 }
265 if(child->state != STATE_ZOMBIE) {
266 child = 0;
267 }
268 }
270 /* found ? */
271 if(child) {
272 int res;
273 struct process *prev, dummy;
275 if(status) {
276 *status = child->exit_status;
277 }
278 res = child->id;
280 /* remove it from our children list */
281 dummy.sib_next = p->child_list;
282 prev = &dummy;
283 while(prev->next) {
284 if(prev->next == child) {
285 prev->next = child->next;
286 break;
287 }
288 }
289 p->child_list = dummy.next;
291 /* invalidate the id */
292 child->id = 0;
293 return res;
294 }
296 /* not found, wait or sod off */
297 if(!(opt & WNOHANG)) {
298 /* wait on our own process struct because
299 * we have no way of knowing which child will
300 * die first.
301 * exit will wakeup the parent structure...
302 */
303 wait(p);
304 /* done waiting, restart waitpid */
305 goto restart;
306 }
308 return 0; /* he's not dead jim */
309 }
311 void context_switch(int pid)
312 {
313 static struct process *prev, *new;
315 assert(get_intr_state() == 0);
316 assert(pid > 0);
317 assert(last_pid > 0);
319 prev = proc + last_pid;
320 new = proc + pid;
322 if(last_pid != pid) {
323 set_current_pid(new->id);
325 /* switch to the new process' address space */
326 set_pgdir_addr(new->ctx.pgtbl_paddr);
328 /* make sure we'll return to the correct kernel stack next time
329 * we enter from userspace
330 */
331 tss->esp0 = PAGE_TO_ADDR(new->kern_stack_pg) + KERN_STACK_SIZE;
333 /* push all registers onto the stack before switching stacks */
334 push_regs();
336 /* XXX: when switching to newly forked processes this switch_stack call
337 * WILL NOT RETURN HERE. It will return to just_forked instead. So the
338 * rest of this function will not run.
339 */
340 switch_stack(new->ctx.stack_ptr, &prev->ctx.stack_ptr);
342 /* restore registers from the new stack */
343 pop_regs();
344 } else {
345 set_current_pid(new->id);
346 }
347 }
350 void set_current_pid(int pid)
351 {
352 cur_pid = pid;
353 if(pid > 0) {
354 last_pid = pid;
355 }
356 }
358 int get_current_pid(void)
359 {
360 return cur_pid;
361 }
363 struct process *get_current_proc(void)
364 {
365 return cur_pid > 0 ? &proc[cur_pid] : 0;
366 }
368 struct process *get_process(int pid)
369 {
370 struct process *p = proc + pid;
371 if(p->id != pid) {
372 printf("get_process called with invalid pid: %d\n", pid);
373 return 0;
374 }
375 return p;
376 }
378 int sys_getpid(void)
379 {
380 return cur_pid;
381 }
383 int sys_getppid(void)
384 {
385 struct process *p = get_current_proc();
387 if(!p) {
388 return 0;
389 }
390 return p->parent;
391 }