kern

view src/proc.c @ 54:4eaecb14fe31

bringing the task switching thing into shape with proper per-process kernel stacks and shit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 14 Aug 2011 16:57:23 +0300
parents 23abbeea4d5f
children 88a6c4e192f9
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"
12 #include "tss.h"
14 #define FLAGS_INTR_BIT 9
16 static void start_first_proc(void);
19 /* defined in test_proc.S */
20 void test_proc(void);
21 void test_proc_end(void);
23 static struct process proc[MAX_PROC];
24 static int cur_pid;
26 static struct task_state *tss;
29 void init_proc(void)
30 {
31 int tss_page;
33 /* allocate a page for the task state segment, to make sure
34 * it doesn't cross page boundaries
35 */
36 if((tss_page = pgalloc(1, MEM_KERNEL)) == -1) {
37 panic("failed to allocate memory for the task state segment\n");
38 }
39 tss = (struct tss*)PAGE_TO_ADDR(tss_page);
41 /* the kernel stack segment never changes so we might as well set it now
42 * the only other thing that we use in the tss is the kernel stack pointer
43 * which is different for each process, and thus managed by context_switch
44 */
45 memset(tss, 0, sizeof *tss);
46 tss->ss0 = selector(SEGM_KDATA, 0);
48 set_tss((uint32_t)virt_to_phys(tss));
50 /* initialize system call handler (see syscall.c) */
51 init_syscall();
53 start_first_proc(); /* XXX never returns */
54 }
57 static void start_first_proc(void)
58 {
59 struct process *p;
60 int proc_size_pg, img_start_pg, stack_pg;
61 uint32_t img_start_addr, ustack_addr;
62 struct intr_frame ifrm;
64 /* prepare the first process */
65 p = proc + 1;
66 p->id = 1;
67 p->parent = 0; /* no parent for init */
69 /* allocate a chunk of memory for the process image
70 * and copy the code of test_proc there.
71 */
72 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
73 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
74 panic("failed to allocate space for the init process image\n");
75 }
76 img_start_addr = PAGE_TO_ADDR(img_start_pg);
77 memcpy((void*)img_start_addr, test_proc, proc_size_pg * PGSIZE);
78 printf("copied init process at: %x\n", img_start_addr);
80 /* allocate the first page of the process stack */
81 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
82 if(pgalloc_vrange(stack_pg, 1) == -1) {
83 panic("failed to allocate user stack page\n");
84 }
85 p->user_stack_pg = stack_pg;
87 /* allocate a kernel stack for this process */
88 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
89 panic("failed to allocate kernel stack for the init process\n");
90 }
91 /* when switching from user space to kernel space, the ss0:esp0 from TSS
92 * will be used to switch to the per-process kernel stack, so we need to
93 * set it correctly before switching to user space.
94 * tss->ss0 is already set in init_proc above.
95 */
96 tss->esp0 = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
99 /* now we need to fill in the fake interrupt stack frame */
100 memset(&ifrm, 0, sizeof ifrm);
101 /* after the priviledge switch, this ss:esp will be used in userspace */
102 ifrm.esp = PAGE_TO_ADDR(stack_pg) + PGSIZE;
103 ifrm.ss = selector(SEGM_UDATA, 3);
104 /* instruction pointer at the beginning of the process image */
105 ifrm.regs.eip = img_start_addr;
106 ifrm.cs = selector(SEGM_UCODE, 3);
107 /* make sure the user will run with interrupts enabled */
108 ifrm.eflags = FLAGS_INTR_BIT;
109 /* user data selectors should all be the same */
110 ifrm.ds = ifrm.es = ifrm.fs = ifrm.gs = ifrm.ss;
112 /* add it to the scheduler queues */
113 add_proc(p->id, STATE_RUNNABLE);
115 /* execute a fake return from interrupt with the fake stack frame */
116 intr_ret(ifrm);
117 }
120 void context_switch(int pid)
121 {
122 struct process *prev, *new;
124 if(cur_pid == pid) {
125 return; /* nothing to be done */
126 }
127 prev = proc + cur_pid;
128 new = proc + pid;
130 /* push all registers onto the stack before switching stacks */
131 push_regs();
133 prev->ctx.stack_ptr = switch_stack(new->ctx.stack_ptr);
135 /* restore registers from the new stack */
136 pop_regs();
138 /* switch to the new process' address space */
139 set_pgdir_addr(new->ctx.pgtbl_paddr);
141 /* make sure we'll return to the correct kernel stack next time
142 * we enter from userspace
143 */
144 tss->esp0 = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
145 }
147 int get_current_pid(void)
148 {
149 return cur_pid;
150 }
152 struct process *get_current_proc(void)
153 {
154 return cur_pid ? &proc[cur_pid] : 0;
155 }
157 struct process *get_process(int pid)
158 {
159 return &proc[pid];
160 }