kern

view src/proc.c @ 91:f83f50c17c3b

continuing with the fs added strtol and strstr to klibc
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 15:29:54 +0200
parents 8b21fe04ba2c
children 07fe6a614185
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 /* copy file table */
169 memcpy(p->files, parent->files, sizeof p->files);
171 /* allocate a kernel stack for the new process */
172 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
173 return -EAGAIN;
174 }
175 p->ctx.stack_ptr = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
176 /* we need to copy the current interrupt frame to the new kernel stack so
177 * that the new process will return to the same point as the parent, just
178 * after the fork syscall.
179 */
180 p->ctx.stack_ptr -= sizeof(struct intr_frame);
181 memcpy((void*)p->ctx.stack_ptr, get_intr_frame(), sizeof(struct intr_frame));
182 /* child's return from fork returns 0 */
183 ((struct intr_frame*)p->ctx.stack_ptr)->regs.eax = 0;
185 /* we also need the address of just_forked in the stack, so that switch_stacks
186 * called from context_switch, will return to just_forked when we first switch
187 * to a newly forked process. just_forked then just calls intr_ret to return to
188 * userspace with the already constructed interrupt frame (see above).
189 */
190 p->ctx.stack_ptr -= 4;
191 *(uint32_t*)p->ctx.stack_ptr = (uint32_t)just_forked;
193 /* initialize the rest of the process structure */
194 p->id = pid;
195 p->parent = parent->id;
196 p->child_list = 0;
197 p->next = p->prev = 0;
199 /* add to the child list */
200 p->sib_next = parent->child_list;
201 parent->child_list = p;
203 /* will be copied on write */
204 p->user_stack_pg = parent->user_stack_pg;
206 /* clone the parent's virtual memory */
207 clone_vm(p, parent, CLONE_COW);
209 /* done, now let's add it to the scheduler runqueue */
210 add_proc(p->id);
212 return pid;
213 }
215 int sys_exit(int status)
216 {
217 struct process *p, *child;
219 p = get_current_proc();
221 printf("process %d exit(%d)\n", p->id, status);
223 /* TODO deliver SIGCHLD to the parent */
225 /* find any child processes and make init adopt them */
226 child = p->child_list;
227 while(child) {
228 child->parent = 1;
229 child = child->sib_next;
230 }
232 cleanup_vm(p);
234 /* remove it from the runqueue */
235 remove_proc(p->id);
237 /* make it a zombie until its parent reaps it */
238 p->state = STATE_ZOMBIE;
239 p->exit_status = (status & _WSTATUS_MASK) | (_WREASON_EXITED << _WREASON_SHIFT);
241 /* wakeup any processes waiting for it
242 * we're waking up the parent's address, because waitpid waits
243 * on it's own process struct, not knowing which child will die
244 * first.
245 */
246 wakeup(get_process(p->parent));
247 return 0;
248 }
250 int sys_waitpid(int pid, int *status, int opt)
251 {
252 struct process *p, *child;
254 p = get_current_proc();
256 restart:
257 if(pid <= 0) {
258 /* search for zombie children */
259 child = p->child_list;
260 while(child) {
261 if(child->state == STATE_ZOMBIE) {
262 break;
263 }
264 child = child->sib_next;
265 }
266 } else {
267 if(!(child = get_process(pid)) || child->parent != p->id) {
268 return -ECHILD;
269 }
270 if(child->state != STATE_ZOMBIE) {
271 child = 0;
272 }
273 }
275 /* found ? */
276 if(child) {
277 int res;
278 struct process *prev, dummy;
280 if(status) {
281 *status = child->exit_status;
282 }
283 res = child->id;
285 /* remove it from our children list */
286 dummy.sib_next = p->child_list;
287 prev = &dummy;
288 while(prev->next) {
289 if(prev->next == child) {
290 prev->next = child->next;
291 break;
292 }
293 }
294 p->child_list = dummy.next;
296 /* invalidate the id */
297 child->id = 0;
298 return res;
299 }
301 /* not found, wait or sod off */
302 if(!(opt & WNOHANG)) {
303 /* wait on our own process struct because
304 * we have no way of knowing which child will
305 * die first.
306 * exit will wakeup the parent structure...
307 */
308 wait(p);
309 /* done waiting, restart waitpid */
310 goto restart;
311 }
313 return 0; /* he's not dead jim */
314 }
316 void context_switch(int pid)
317 {
318 static struct process *prev, *new;
320 assert(get_intr_state() == 0);
321 assert(pid > 0);
322 assert(last_pid > 0);
324 prev = proc + last_pid;
325 new = proc + pid;
327 if(last_pid != pid) {
328 set_current_pid(new->id);
330 /* switch to the new process' address space */
331 set_pgdir_addr(new->ctx.pgtbl_paddr);
333 /* make sure we'll return to the correct kernel stack next time
334 * we enter from userspace
335 */
336 tss->esp0 = PAGE_TO_ADDR(new->kern_stack_pg) + KERN_STACK_SIZE;
338 /* push all registers onto the stack before switching stacks */
339 push_regs();
341 /* XXX: when switching to newly forked processes this switch_stack call
342 * WILL NOT RETURN HERE. It will return to just_forked instead. So the
343 * rest of this function will not run.
344 */
345 switch_stack(new->ctx.stack_ptr, &prev->ctx.stack_ptr);
347 /* restore registers from the new stack */
348 pop_regs();
349 } else {
350 set_current_pid(new->id);
351 }
352 }
355 void set_current_pid(int pid)
356 {
357 cur_pid = pid;
358 if(pid > 0) {
359 last_pid = pid;
360 }
361 }
363 int get_current_pid(void)
364 {
365 return cur_pid;
366 }
368 struct process *get_current_proc(void)
369 {
370 return cur_pid > 0 ? &proc[cur_pid] : 0;
371 }
373 struct process *get_process(int pid)
374 {
375 struct process *p = proc + pid;
376 if(p->id != pid) {
377 printf("get_process called with invalid pid: %d\n", pid);
378 return 0;
379 }
380 return p;
381 }
383 int sys_getpid(void)
384 {
385 return cur_pid;
386 }
388 int sys_getppid(void)
389 {
390 struct process *p = get_current_proc();
392 if(!p) {
393 return 0;
394 }
395 return p->parent;
396 }