kern
view src/proc.c @ 51:b1e8c8251884
lalalala
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 01 Aug 2011 06:45:29 +0300 |
parents | 1d8877d12de0 |
children | fa65b4f45366 |
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"
8 #include "syscall.h"
9 #include "sched.h"
12 /* defined in test_proc.S */
13 void test_proc(void);
14 void test_proc_end(void);
16 static struct process proc[MAX_PROC];
17 static int cur_pid;
19 void init_proc(void)
20 {
21 int proc_size_pg, img_start_pg, stack_pg;
22 void *img_start;
23 cur_pid = 0;
25 init_syscall();
27 /* prepare the first process */
28 proc[1].id = 1;
29 proc[1].parent = 0;
31 /* allocate a chunk of memory for the process image
32 * and copy the code of test_proc there.
33 * (should be mapped at a fixed address)
34 */
35 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
36 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
37 panic("failed to allocate space for the init process image\n");
38 }
39 img_start = (void*)PAGE_TO_ADDR(img_start_pg);
40 memcpy(img_start, test_proc, proc_size_pg * PGSIZE);
42 /* instruction pointer at the beginning of the process image */
43 proc[1].ctx.instr_ptr = (uint32_t)img_start;
45 /* allocate the first page of the process stack */
46 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
47 if(pgalloc_vrange(stack_pg, 1) == -1) {
48 panic("failed to allocate user stack page\n");
49 }
50 proc[1].ctx.stack_ptr = PAGE_TO_ADDR(stack_pg) + PGSIZE;
52 /* create the virtual address space for this process */
53 proc[1].ctx.pgtbl_paddr = clone_vm();
55 /* we don't need the image and the stack in this address space */
56 unmap_page_range(img_start_pg, proc_size_pg);
57 pgfree(img_start_pg, proc_size_pg);
59 unmap_page(stack_pg);
60 pgfree(stack_pg, 1);
62 /* add it to the scheduler queues */
63 //add_proc(1, STATE_RUNNING);
65 /* switch to the initial process, this never returns */
66 context_switch(1);
67 }
70 void context_switch(int pid)
71 {
72 struct intr_frame ifrm;
73 struct context *ctx = &proc[pid].ctx;
76 cur_pid = pid;
78 ifrm.inum = ifrm.err = 0;
80 ifrm.regs = ctx->regs;
81 ifrm.eflags = ctx->flags | (1 << 9);
83 ifrm.eip = ctx->instr_ptr;
84 ifrm.cs = selector(SEGM_KCODE, 0); /* XXX change this when we setup the TSS */
85 ifrm.esp = 0;/*ctx->stack_ptr; /* this will only be used when we switch to userspace */
86 ifrm.regs.esp = ctx->stack_ptr; /* ... until then... */
87 ifrm.ss = 0;/*selector(SEGM_KDATA, 0); /* XXX */
89 /* switch to the vm of the process */
90 set_pgdir_addr(ctx->pgtbl_paddr);
92 intr_ret(ifrm);
93 }
95 int get_current_pid(void)
96 {
97 return cur_pid;
98 }
100 struct process *get_current_proc(void)
101 {
102 return cur_pid ? &proc[cur_pid] : 0;
103 }
105 struct process *get_process(int pid)
106 {
107 return &proc[pid];
108 }