kern

view src/proc.c @ 56:0be4615594df

finally, runqueues, blocking, waking up, idle loop etc, all seem to work fine on a single user process... Next up: try forking another one :)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 15 Aug 2011 06:17:58 +0300
parents 88a6c4e192f9
children 437360696883
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];
28 /* cur_pid: pid of the currently executing process.
29 * when we're in the idle process cur_pid will be 0.
30 * last_pid: pid of the last real process that was running, this should
31 * never become 0. Essentially this defines the active kernel stack.
32 */
33 static int cur_pid, last_pid;
35 static struct task_state *tss;
38 void init_proc(void)
39 {
40 int tss_page;
42 /* allocate a page for the task state segment, to make sure
43 * it doesn't cross page boundaries
44 */
45 if((tss_page = pgalloc(1, MEM_KERNEL)) == -1) {
46 panic("failed to allocate memory for the task state segment\n");
47 }
48 tss = (struct task_state*)PAGE_TO_ADDR(tss_page);
50 /* the kernel stack segment never changes so we might as well set it now
51 * the only other thing that we use in the tss is the kernel stack pointer
52 * which is different for each process, and thus managed by context_switch
53 */
54 memset(tss, 0, sizeof *tss);
55 tss->ss0 = selector(SEGM_KDATA, 0);
57 set_tss((uint32_t)tss);
59 /* initialize system call handler (see syscall.c) */
60 init_syscall();
62 start_first_proc(); /* XXX never returns */
63 }
66 static void start_first_proc(void)
67 {
68 struct process *p;
69 int proc_size_pg, img_start_pg, stack_pg;
70 uint32_t img_start_addr;
71 struct intr_frame ifrm;
73 /* prepare the first process */
74 p = proc + 1;
75 p->id = 1;
76 p->parent = 0; /* no parent for init */
78 p->ticks_left = TIMESLICE_TICKS;
79 p->next = p->prev = 0;
81 /* the first process may keep this existing page table */
82 p->ctx.pgtbl_paddr = get_pgdir_addr();
84 /* allocate a chunk of memory for the process image
85 * and copy the code of test_proc there.
86 */
87 proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
88 if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
89 panic("failed to allocate space for the init process image\n");
90 }
91 img_start_addr = PAGE_TO_ADDR(img_start_pg);
92 memcpy((void*)img_start_addr, test_proc, proc_size_pg * PGSIZE);
93 printf("copied init process at: %x\n", img_start_addr);
95 /* allocate the first page of the process stack */
96 stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
97 if(pgalloc_vrange(stack_pg, 1) == -1) {
98 panic("failed to allocate user stack page\n");
99 }
100 p->user_stack_pg = stack_pg;
102 /* allocate a kernel stack for this process */
103 if((p->kern_stack_pg = pgalloc(KERN_STACK_SIZE / PGSIZE, MEM_KERNEL)) == -1) {
104 panic("failed to allocate kernel stack for the init process\n");
105 }
106 /* when switching from user space to kernel space, the ss0:esp0 from TSS
107 * will be used to switch to the per-process kernel stack, so we need to
108 * set it correctly before switching to user space.
109 * tss->ss0 is already set in init_proc above.
110 */
111 tss->esp0 = PAGE_TO_ADDR(p->kern_stack_pg) + KERN_STACK_SIZE;
114 /* now we need to fill in the fake interrupt stack frame */
115 memset(&ifrm, 0, sizeof ifrm);
116 /* after the priviledge switch, this ss:esp will be used in userspace */
117 ifrm.esp = PAGE_TO_ADDR(stack_pg) + PGSIZE;
118 ifrm.ss = selector(SEGM_UDATA, 3);
119 /* instruction pointer at the beginning of the process image */
120 ifrm.eip = img_start_addr;
121 ifrm.cs = selector(SEGM_UCODE, 3);
122 /* make sure the user will run with interrupts enabled */
123 ifrm.eflags = FLAGS_INTR_BIT;
124 /* user data selectors should all be the same */
125 ifrm.ds = ifrm.es = ifrm.fs = ifrm.gs = ifrm.ss;
127 /* add it to the scheduler queues */
128 add_proc(p->id);
130 /* make it current */
131 set_current_pid(p->id);
133 /* execute a fake return from interrupt with the fake stack frame */
134 intr_ret(ifrm);
135 }
138 void context_switch(int pid)
139 {
140 static struct process *prev, *new;
142 assert(get_intr_state() == 0);
143 assert(pid > 0);
144 assert(last_pid > 0);
146 prev = proc + last_pid;
147 new = proc + pid;
149 if(last_pid != pid) {
150 /* push all registers onto the stack before switching stacks */
151 push_regs();
153 prev->ctx.stack_ptr = switch_stack(new->ctx.stack_ptr);
155 /* restore registers from the new stack */
156 pop_regs();
158 /* switch to the new process' address space */
159 set_pgdir_addr(new->ctx.pgtbl_paddr);
161 /* make sure we'll return to the correct kernel stack next time
162 * we enter from userspace
163 */
164 tss->esp0 = PAGE_TO_ADDR(new->kern_stack_pg) + KERN_STACK_SIZE;
165 }
167 set_current_pid(new->id);
168 }
171 void set_current_pid(int pid)
172 {
173 cur_pid = pid;
174 if(pid > 0) {
175 last_pid = pid;
176 }
177 }
179 int get_current_pid(void)
180 {
181 return cur_pid;
182 }
184 struct process *get_current_proc(void)
185 {
186 return cur_pid > 0 ? &proc[cur_pid] : 0;
187 }
189 struct process *get_process(int pid)
190 {
191 return &proc[pid];
192 }