kern

view src/proc.c @ 49:50730d42d2d3

fuck yeah, now do priviledge levels and TSS
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Jul 2011 07:21:54 +0300
parents f65b348780e3
children 1d8877d12de0
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);*/
35 img_start = test_proc;
37 /* instruction pointer at the beginning of the process image */
38 proc[0].ctx.instr_ptr = (uint32_t)img_start;
40 /* allocate the first page of the process stack */
41 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
42 if(pgalloc_vrange(stack_pg, 1) == -1) {
43 panic("failed to allocate user stack page\n");
44 }
45 proc[0].ctx.stack_ptr = PAGE_TO_ADDR(stack_pg) + PGSIZE;
47 /* create the virtual address space for this process */
48 proc[0].ctx.pgtbl_paddr = clone_vm();
50 /* we don't need the image and the stack in this address space */
51 /*unmap_page_range(img_start_pg, proc_size_pg);
52 pgfree(img_start_pg, proc_size_pg);*/
54 unmap_page(stack_pg);
55 pgfree(stack_pg, 1);
58 /* switch to it by calling a function that takes the context
59 * of the current process, plugs the values into the interrupt
60 * stack, and calls iret.
61 * (should also set ss0/sp0 in TSS before returning)
62 */
63 context_switch(0);
64 /* XXX this will never return */
65 }
68 void context_switch(int pid)
69 {
70 struct intr_frame ifrm;
71 struct context *ctx = &proc[pid].ctx;
74 cur_pid = pid;
76 ifrm.inum = ifrm.err = 0;
78 ifrm.regs = ctx->regs;
79 ifrm.eflags = ctx->flags;
81 ifrm.err = 0xbadf00d;
83 asm volatile (
84 "pushf\n\t"
85 "popl %0\n\t"
86 : "=a" (ifrm.eflags)
87 );
89 ifrm.eip = ctx->instr_ptr;
90 ifrm.cs = selector(SEGM_KCODE, 0); /* XXX change this when we setup the TSS */
91 ifrm.esp = 0;/*ctx->stack_ptr; /* this will only be used when we switch to userspace */
92 ifrm.regs.esp = ctx->stack_ptr; /* ... until then... */
93 ifrm.ss = 0;/*selector(SEGM_KDATA, 0); /* XXX */
95 /* switch to the vm of the process */
96 set_pgdir_addr(ctx->pgtbl_paddr);
98 intr_ret(ifrm);
99 }