kern

view src/proc.c @ 52:fa65b4f45366

picking this up again, let's fix it
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Aug 2011 06:42:00 +0300
parents b1e8c8251884
children 23abbeea4d5f
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "proc.h"
5 #include "tss.h"
6 #include "vm.h"
7 #include "segm.h"
8 #include "intr.h"
9 #include "panic.h"
10 #include "syscall.h"
11 #include "sched.h"
13 #define FLAGS_INTR_BIT 9
15 /* defined in test_proc.S */
16 void test_proc(void);
17 void test_proc_end(void);
19 static struct process proc[MAX_PROC];
20 static int cur_pid;
22 void init_proc(void)
23 {
24 int proc_size_pg, img_start_pg, stack_pg;
25 void *img_start;
26 cur_pid = 0;
28 init_syscall();
30 /* prepare the first process */
31 proc[1].id = 1;
32 proc[1].parent = 0;
34 /* allocate a chunk of memory for the process image
35 * and copy the code of test_proc there.
36 * (should be mapped at a fixed address)
37 */
38 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
39 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
40 panic("failed to allocate space for the init process image\n");
41 }
42 img_start = (void*)PAGE_TO_ADDR(img_start_pg);
43 memcpy(img_start, test_proc, proc_size_pg * PGSIZE);
45 printf("copied init process at: %x\n", (unsigned int)img_start);
47 /* instruction pointer at the beginning of the process image */
48 proc[1].ctx.instr_ptr = (uint32_t)img_start;
50 /* allocate the first page of the process stack */
51 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
52 if(pgalloc_vrange(stack_pg, 1) == -1) {
53 panic("failed to allocate user stack page\n");
54 }
55 proc[1].ctx.stack_ptr = PAGE_TO_ADDR(stack_pg) + PGSIZE;
57 proc[1].stack_end = KMEM_START;
58 proc[1].stack_start = KMEM_START - PGSIZE;
60 /* create the virtual address space for this process */
61 proc[1].ctx.pgtbl_paddr = clone_vm();
63 /* we don't need the image and the stack in this address space */
64 unmap_page_range(img_start_pg, proc_size_pg);
65 pgfree(img_start_pg, proc_size_pg);
67 unmap_page(stack_pg);
68 pgfree(stack_pg, 1);
70 /* add it to the scheduler queues */
71 add_proc(1, STATE_RUNNING);
73 /* switch to the initial process, this never returns */
74 context_switch(1);
75 }
78 void context_switch(int pid)
79 {
80 struct context *ctx;
81 struct intr_frame ifrm;
82 struct intr_frame *cur_frame = get_intr_frame();
84 assert(0);
86 if(cur_pid == pid) {
87 assert(cur_frame);
88 intr_ret(*cur_frame);
89 }
91 ctx = &proc[pid].ctx;
92 cur_pid = pid;
94 ifrm.inum = ifrm.err = 0;
96 ifrm.regs = ctx->regs;
97 ifrm.eflags = ctx->flags | (1 << FLAGS_INTR_BIT);
99 ifrm.eip = ctx->instr_ptr;
100 ifrm.cs = selector(SEGM_KCODE, 0); /* XXX change this when we setup the TSS */
101 /*ifrm.regs.esp = ctx->stack_ptr;*/ /* ... until then... */
102 ifrm.esp = ctx->stack_ptr; /* this will only be used when we switch to userspace */
103 ifrm.ss = selector(SEGM_KDATA, 0);
105 /* switch to the vm of the process */
106 set_pgdir_addr(ctx->pgtbl_paddr);
107 intr_ret(ifrm);
108 }
110 int get_current_pid(void)
111 {
112 return cur_pid;
113 }
115 struct process *get_current_proc(void)
116 {
117 return cur_pid ? &proc[cur_pid] : 0;
118 }
120 struct process *get_process(int pid)
121 {
122 return &proc[pid];
123 }