kern
view src/proc.c @ 68:0a205396e1a0
- added a generic red-black tree data structure
- added a VM map as an red-black tree of vm_pages in the process structure
- constructed the vm map of the memory passed by the kernel initially to the first process.
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Mon, 10 Oct 2011 04:16:01 +0300 |
parents | f44bec97a0ec |
children | b45e2d5f0ae1 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <errno.h>
5 #include "config.h"
6 #include "proc.h"
7 #include "tss.h"
8 #include "vm.h"
9 #include "segm.h"
10 #include "intr.h"
11 #include "panic.h"
12 #include "syscall.h"
13 #include "sched.h"
14 #include "tss.h"
16 #define FLAGS_INTR_BIT (1 << 9)
18 static void start_first_proc(void);
20 /* defined in proc-asm.S */
21 uint32_t switch_stack(uint32_t new_stack, uint32_t *old_stack);
22 void just_forked(void);
24 /* defined in test_proc.S */
25 void test_proc(void);
26 void test_proc_end(void);
28 static struct process proc[MAX_PROC];
30 /* cur_pid: pid of the currently executing process.
31 * when we're in the idle process cur_pid will be 0.
32 * last_pid: pid of the last real process that was running, this should
33 * never become 0. Essentially this defines the active kernel stack.
34 */
35 static int cur_pid, last_pid;
37 static struct task_state *tss;
40 void init_proc(void)
41 {
42 int tss_page;
44 /* allocate a page for the task state segment, to make sure
45 * it doesn't cross page boundaries
46 */
47 if((tss_page = pgalloc(1, MEM_KERNEL)) == -1) {
48 panic("failed to allocate memory for the task state segment\n");
49 }
50 tss = (struct task_state*)PAGE_TO_ADDR(tss_page);
52 /* the kernel stack segment never changes so we might as well set it now
53 * the only other thing that we use in the tss is the kernel stack pointer
54 * which is different for each process, and thus managed by context_switch
55 */
56 memset(tss, 0, sizeof *tss);
57 tss->ss0 = selector(SEGM_KDATA, 0);
59 set_tss((uint32_t)tss);
61 /* initialize system call handler (see syscall.c) */
62 init_syscall();
64 start_first_proc(); /* XXX never returns */
65 }
67 static void start_first_proc(void)
68 {
69 struct process *p;
70 int proc_size_pg, img_start_pg, stack_pg;
71 uint32_t img_start_addr;
72 struct intr_frame ifrm;
74 /* prepare the first process */
75 p = proc + 1;
76 p->id = 1;
77 p->parent = 0; /* no parent for init */
79 p->ticks_left = TIMESLICE_TICKS;
80 p->next = p->prev = 0;
82 /* the first process may keep this existing page table */
83 p->ctx.pgtbl_paddr = get_pgdir_addr();
85 /* allocate a chunk of memory for the process image
86 * and copy the code of test_proc there.
87 */
88 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
89 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
90 panic("failed to allocate space for the init process image\n");
91 }
92 img_start_addr = PAGE_TO_ADDR(img_start_pg);
93 memcpy((void*)img_start_addr, test_proc, proc_size_pg * PGSIZE);
94 printf("copied init process at: %x\n", img_start_addr);
96 /* allocate the first page of the process stack */
97 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
98 if(pgalloc_vrange(stack_pg, 1) == -1) {
99 panic("failed to allocate user stack page\n");
100 }
101 p->user_stack_pg = stack_pg;
103 /* allocate a kernel stack for this process */
104 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
105 panic("failed to allocate kernel stack for the init process\n");
106 }
107 /* when switching from user space to kernel space, the ss0:esp0 from TSS
108 * will be used to switch to the per-process kernel stack, so we need to
109 * set it correctly before switching to user space.
110 * tss->ss0 is already set in init_proc above.
111 */
112 tss->esp0 = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
115 /* now we need to fill in the fake interrupt stack frame */
116 memset(&ifrm, 0, sizeof ifrm);
117 /* after the priviledge switch, this ss:esp will be used in userspace */
118 ifrm.esp = PAGE_TO_ADDR(stack_pg) + PGSIZE;
119 ifrm.ss = selector(SEGM_UDATA, 3);
120 /* instruction pointer at the beginning of the process image */
121 ifrm.eip = img_start_addr;
122 ifrm.cs = selector(SEGM_UCODE, 3);
123 /* make sure the user will run with interrupts enabled */
124 ifrm.eflags = FLAGS_INTR_BIT;
125 /* user data selectors should all be the same */
126 ifrm.ds = ifrm.es = ifrm.fs = ifrm.gs = ifrm.ss;
128 /* add it to the scheduler queues */
129 add_proc(p->id);
131 /* make it current */
132 set_current_pid(p->id);
134 /* build the current vm map */
135 cons_vmmap(&p->vmmap);
137 /* execute a fake return from interrupt with the fake stack frame */
138 intr_ret(ifrm);
139 }
141 int fork(void)
142 {
143 int i, pid;
144 struct process *p, *parent;
146 disable_intr();
148 /* find a free process slot */
149 /* TODO don't search up to MAX_PROC if uid != 0 */
150 pid = -1;
151 for(i=1; i<MAX_PROC; i++) {
152 if(proc[i].id == 0) {
153 pid = i;
154 break;
155 }
156 }
158 if(pid == -1) {
159 /* process table full */
160 return -EAGAIN;
161 }
164 p = proc + pid;
165 parent = get_current_proc();
167 /* allocate a kernel stack for the new process */
168 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
169 return -EAGAIN;
170 }
171 p->ctx.stack_ptr = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
172 /* we need to copy the current interrupt frame to the new kernel stack so
173 * that the new process will return to the same point as the parent, just
174 * after the fork syscall.
175 */
176 p->ctx.stack_ptr -= sizeof(struct intr_frame);
177 memcpy((void*)p->ctx.stack_ptr, get_intr_frame(), sizeof(struct intr_frame));
178 /* child's return from fork returns 0 */
179 ((struct intr_frame*)p->ctx.stack_ptr)->regs.eax = 0;
181 /* we also need the address of just_forked in the stack, so that switch_stacks
182 * called from context_switch, will return to just_forked when we first switch
183 * to a newly forked process. just_forked then just calls intr_ret to return to
184 * userspace with the already constructed interrupt frame (see above).
185 */
186 p->ctx.stack_ptr -= 4;
187 *(uint32_t*)p->ctx.stack_ptr = (uint32_t)just_forked;
189 /* initialize the rest of the process structure */
190 p->id = pid;
191 p->parent = parent->id;
192 p->next = p->prev = 0;
194 /* will be copied on write */
195 p->user_stack_pg = parent->user_stack_pg;
197 p->ctx.pgtbl_paddr = clone_vm(CLONE_COW);
199 /* done, now let's add it to the scheduler runqueue */
200 add_proc(p->id);
202 return pid;
203 }
205 void context_switch(int pid)
206 {
207 static struct process *prev, *new;
209 assert(get_intr_state() == 0);
210 assert(pid > 0);
211 assert(last_pid > 0);
213 prev = proc + last_pid;
214 new = proc + pid;
216 if(last_pid != pid) {
217 set_current_pid(new->id);
219 /* switch to the new process' address space */
220 set_pgdir_addr(new->ctx.pgtbl_paddr);
222 /* make sure we'll return to the correct kernel stack next time
223 * we enter from userspace
224 */
225 tss->esp0 = PAGE_TO_ADDR(new->kern_stack_pg) + KERN_STACK_SIZE;
227 /* push all registers onto the stack before switching stacks */
228 push_regs();
230 /* XXX: when switching to newly forked processes this switch_stack call
231 * WILL NOT RETURN HERE. It will return to just_forked instead. So the
232 * rest of this function will not run.
233 */
234 switch_stack(new->ctx.stack_ptr, &prev->ctx.stack_ptr);
236 /* restore registers from the new stack */
237 pop_regs();
238 } else {
239 set_current_pid(new->id);
240 }
241 }
244 void set_current_pid(int pid)
245 {
246 cur_pid = pid;
247 if(pid > 0) {
248 last_pid = pid;
249 }
250 }
252 int get_current_pid(void)
253 {
254 return cur_pid;
255 }
257 struct process *get_current_proc(void)
258 {
259 return cur_pid > 0 ? &proc[cur_pid] : 0;
260 }
262 struct process *get_process(int pid)
263 {
264 return &proc[pid];
265 }