kern
view src/proc.c @ 47:f65b348780e3
continuing with the process implementation. not done yet, panics.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 28 Jul 2011 05:43:04 +0300 |
parents | b8f02479e3f4 |
children | 50730d42d2d3 |
line source
1 #include <string.h>
2 #include "proc.h"
3 #include "tss.h"
4 #include "vm.h"
5 #include "segm.h"
6 #include "intr.h"
7 #include "panic.h"
10 /* defined in test_proc.S */
11 void test_proc(void);
12 void test_proc_end(void);
14 static struct process proc[MAX_PROC];
15 static int cur_pid;
17 void init_proc(void)
18 {
19 int proc_size_pg, img_start_pg, stack_pg;
20 void *img_start;
21 cur_pid = -1;
23 /* prepare the first process */
25 /* allocate a chunk of memory for the process image
26 * and copy the code of test_proc there.
27 * (should be mapped at a fixed address)
28 */
29 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
30 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
31 panic("failed to allocate space for the init process image\n");
32 }
33 img_start = (void*)PAGE_TO_ADDR(img_start_pg);
34 memcpy(img_start, test_proc, proc_size_pg * PGSIZE);
36 /* instruction pointer at the beginning of the process image */
37 proc[0].ctx.instr_ptr = (uint32_t)img_start;
39 /* allocate the first page of the process stack */
40 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
41 if(pgalloc_vrange(stack_pg, 1) == -1) {
42 panic("failed to allocate user stack page\n");
43 }
44 proc[0].ctx.stack_ptr = PAGE_TO_ADDR(stack_pg) + PGSIZE;
46 /* create the virtual address space for this process */
47 proc[0].ctx.pgtbl_paddr = clone_vm();
49 /* we don't need the image and the stack in this address space */
50 unmap_page_range(img_start_pg, proc_size_pg);
51 pgfree(img_start_pg, proc_size_pg);
53 unmap_page(stack_pg);
54 pgfree(stack_pg, 1);
57 /* switch to it by calling a function that takes the context
58 * of the current process, plugs the values into the interrupt
59 * stack, and calls iret.
60 * (should also set ss0/sp0 in TSS before returning)
61 */
62 context_switch(0);
63 /* XXX this will never return */
64 }
67 void context_switch(int pid)
68 {
69 struct intr_frame ifrm;
70 struct context *ctx = &proc[pid].ctx;
72 cur_pid = pid;
74 ifrm.inum = ifrm.err = 0;
76 ifrm.regs = ctx->regs;
77 ifrm.eflags = ctx->flags;
79 ifrm.eip = ctx->instr_ptr;
80 ifrm.cs = SEGM_KCODE; /* XXX change this when we setup the TSS */
81 ifrm.esp = ctx->stack_ptr;
82 ifrm.ss = SEGM_KDATA; /* XXX */
84 /* switch to the vm of the process */
85 set_pgdir_addr(ctx->pgtbl_paddr);
87 intr_ret(ifrm);
88 }