kern

view src/proc.c @ 55:88a6c4e192f9

Fixed most important task switching bugs. Now it seems that I can switch in and out of user space reliably.
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 15 Aug 2011 04:03:39 +0300
parents 4eaecb14fe31
children 0be4615594df
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "config.h"
5 #include "proc.h"
6 #include "tss.h"
7 #include "vm.h"
8 #include "segm.h"
9 #include "intr.h"
10 #include "panic.h"
11 #include "syscall.h"
12 #include "sched.h"
13 #include "tss.h"
15 #define FLAGS_INTR_BIT (1 << 9)
17 static void start_first_proc(void);
19 /* defined in proc-asm.S */
20 uint32_t switch_stack(uint32_t new_stack);
22 /* defined in test_proc.S */
23 void test_proc(void);
24 void test_proc_end(void);
26 static struct process proc[MAX_PROC];
27 static int cur_pid;
29 static struct task_state *tss;
32 void init_proc(void)
33 {
34 int tss_page;
36 /* allocate a page for the task state segment, to make sure
37 * it doesn't cross page boundaries
38 */
39 if((tss_page = pgalloc(1, MEM_KERNEL)) == -1) {
40 panic("failed to allocate memory for the task state segment\n");
41 }
42 tss = (struct task_state*)PAGE_TO_ADDR(tss_page);
44 /* the kernel stack segment never changes so we might as well set it now
45 * the only other thing that we use in the tss is the kernel stack pointer
46 * which is different for each process, and thus managed by context_switch
47 */
48 memset(tss, 0, sizeof *tss);
49 tss->ss0 = selector(SEGM_KDATA, 0);
51 set_tss((uint32_t)tss);
53 /* initialize system call handler (see syscall.c) */
54 init_syscall();
56 start_first_proc(); /* XXX never returns */
57 }
60 static void start_first_proc(void)
61 {
62 struct process *p;
63 int proc_size_pg, img_start_pg, stack_pg;
64 uint32_t img_start_addr;
65 struct intr_frame ifrm;
67 /* prepare the first process */
68 p = proc + 1;
69 p->id = 1;
70 p->parent = 0; /* no parent for init */
72 p->ticks_left = TIMESLICE_TICKS;
73 p->next = p->prev = 0;
75 /* the first process may keep this existing page table */
76 p->ctx.pgtbl_paddr = get_pgdir_addr();
78 /* allocate a chunk of memory for the process image
79 * and copy the code of test_proc there.
80 */
81 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
82 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
83 panic("failed to allocate space for the init process image\n");
84 }
85 img_start_addr = PAGE_TO_ADDR(img_start_pg);
86 memcpy((void*)img_start_addr, test_proc, proc_size_pg * PGSIZE);
87 printf("copied init process at: %x\n", img_start_addr);
89 /* allocate the first page of the process stack */
90 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
91 if(pgalloc_vrange(stack_pg, 1) == -1) {
92 panic("failed to allocate user stack page\n");
93 }
94 p->user_stack_pg = stack_pg;
96 /* allocate a kernel stack for this process */
97 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
98 panic("failed to allocate kernel stack for the init process\n");
99 }
100 /* when switching from user space to kernel space, the ss0:esp0 from TSS
101 * will be used to switch to the per-process kernel stack, so we need to
102 * set it correctly before switching to user space.
103 * tss->ss0 is already set in init_proc above.
104 */
105 tss->esp0 = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
108 /* now we need to fill in the fake interrupt stack frame */
109 memset(&ifrm, 0, sizeof ifrm);
110 /* after the priviledge switch, this ss:esp will be used in userspace */
111 ifrm.esp = PAGE_TO_ADDR(stack_pg) + PGSIZE;
112 ifrm.ss = selector(SEGM_UDATA, 3);
113 /* instruction pointer at the beginning of the process image */
114 ifrm.eip = img_start_addr;
115 ifrm.cs = selector(SEGM_UCODE, 3);
116 /* make sure the user will run with interrupts enabled */
117 ifrm.eflags = FLAGS_INTR_BIT;
118 /* user data selectors should all be the same */
119 ifrm.ds = ifrm.es = ifrm.fs = ifrm.gs = ifrm.ss;
121 /* add it to the scheduler queues */
122 add_proc(p->id);
124 cur_pid = p->id; /* make it current */
126 /* execute a fake return from interrupt with the fake stack frame */
127 intr_ret(ifrm);
128 }
131 void context_switch(int pid)
132 {
133 struct process *prev, *new;
135 assert(get_intr_state() == 0);
137 if(cur_pid == pid) {
138 return; /* nothing to be done */
139 }
140 prev = proc + cur_pid;
141 new = proc + pid;
143 /* push all registers onto the stack before switching stacks */
144 push_regs();
146 prev->ctx.stack_ptr = switch_stack(new->ctx.stack_ptr);
148 /* restore registers from the new stack */
149 pop_regs();
151 /* switch to the new process' address space */
152 set_pgdir_addr(new->ctx.pgtbl_paddr);
154 /* make sure we'll return to the correct kernel stack next time
155 * we enter from userspace
156 */
157 tss->esp0 = PAGE_TO_ADDR(new->kern_stack_pg) + KERN_STACK_SIZE;
158 }
160 int get_current_pid(void)
161 {
162 return cur_pid;
163 }
165 struct process *get_current_proc(void)
166 {
167 return cur_pid ? &proc[cur_pid] : 0;
168 }
170 struct process *get_process(int pid)
171 {
172 return &proc[pid];
173 }