kern
view src/vm.c @ 30:a2c6110bd24b
refactored the code a bit, and merged with the malloc implementation branch
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Fri, 27 May 2011 22:09:10 +0300 |
parents | 9939a6d7a45a |
children | 373a9f50b4e6 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <inttypes.h>
4 #include "vm.h"
5 #include <stdio.h>
6 #include "intr.h"
7 #include "mem.h"
8 #include "panic.h"
11 #define KMEM_START 0xc0000000
12 #define IDMAP_START 0xa0000
14 #define PGDIR_ADDR 0xfffff000
15 #define PGTBL_BASE (0xffffffff - 4096 * 1024 + 1)
16 #define PGTBL(x) ((uint32_t*)(PGTBL_BASE + PGSIZE * (x)))
18 #define ATTR_PGDIR_MASK 0x3f
19 #define ATTR_PGTBL_MASK 0x1ff
20 #define ADDR_PGENT_MASK 0xfffff000
22 #define PAGEFAULT 14
25 struct page_range {
26 int start, end;
27 struct page_range *next;
28 };
30 /* defined in vm-asm.S */
31 void enable_paging(void);
32 void disable_paging(void);
33 int get_paging_status(void);
34 void set_pgdir_addr(uint32_t addr);
35 void flush_tlb(void);
36 void flush_tlb_addr(uint32_t addr);
37 #define flush_tlb_page(p) flush_tlb_addr(PAGE_TO_ADDR(p))
38 uint32_t get_fault_addr(void);
40 static void coalesce(struct page_range *low, struct page_range *mid, struct page_range *high);
41 static void pgfault(int inum, uint32_t err);
42 static struct page_range *alloc_node(void);
43 static void free_node(struct page_range *node);
45 /* page directory */
46 static uint32_t *pgdir;
48 /* 2 lists of free ranges, for kernel memory and user memory */
49 static struct page_range *pglist[2];
50 /* list of free page_range structures to be used in the lists */
51 static struct page_range *node_pool;
52 /* the first page range for the whole kernel address space, to get things started */
53 static struct page_range first_node;
56 void init_vm(void)
57 {
58 uint32_t idmap_end;
60 /* setup the page tables */
61 pgdir = (uint32_t*)alloc_phys_page();
62 memset(pgdir, 0, PGSIZE);
63 set_pgdir_addr((uint32_t)pgdir);
65 /* map the video memory and kernel code 1-1 */
66 get_kernel_mem_range(0, &idmap_end);
67 map_mem_range(IDMAP_START, idmap_end - IDMAP_START, IDMAP_START, 0);
69 /* make the last page directory entry point to the page directory */
70 pgdir[1023] = ((uint32_t)pgdir & ADDR_PGENT_MASK) | PG_PRESENT;
71 pgdir = (uint32_t*)PGDIR_ADDR;
73 /* set the page fault handler */
74 interrupt(PAGEFAULT, pgfault);
76 /* we can enable paging now */
77 enable_paging();
79 /* initialize the virtual page allocator */
80 node_pool = 0;
82 first_node.start = ADDR_TO_PAGE(KMEM_START);
83 first_node.end = ADDR_TO_PAGE(PGTBL_BASE);
84 first_node.next = 0;
85 pglist[MEM_KERNEL] = &first_node;
87 pglist[MEM_USER] = alloc_node();
88 pglist[MEM_USER]->start = ADDR_TO_PAGE(idmap_end);
89 pglist[MEM_USER]->end = ADDR_TO_PAGE(KMEM_START);
90 pglist[MEM_USER]->next = 0;
91 }
93 /* if ppage == -1 we allocate a physical page by calling alloc_phys_page */
94 int map_page(int vpage, int ppage, unsigned int attr)
95 {
96 uint32_t *pgtbl;
97 int diridx, pgidx, pgon, intr_state;
99 intr_state = get_intr_state();
100 disable_intr();
102 pgon = get_paging_status();
104 if(ppage < 0) {
105 uint32_t addr = alloc_phys_page();
106 if(!addr) {
107 set_intr_state(intr_state);
108 return -1;
109 }
110 ppage = ADDR_TO_PAGE(addr);
111 }
113 diridx = PAGE_TO_PGTBL(vpage);
114 pgidx = PAGE_TO_PGTBL_PG(vpage);
116 if(!(pgdir[diridx] & PG_PRESENT)) {
117 uint32_t addr = alloc_phys_page();
118 pgdir[diridx] = addr | (attr & ATTR_PGDIR_MASK) | PG_PRESENT;
120 pgtbl = pgon ? PGTBL(diridx) : (uint32_t*)addr;
121 memset(pgtbl, 0, PGSIZE);
122 } else {
123 if(pgon) {
124 pgtbl = PGTBL(diridx);
125 } else {
126 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
127 }
128 }
130 pgtbl[pgidx] = PAGE_TO_ADDR(ppage) | (attr & ATTR_PGTBL_MASK) | PG_PRESENT;
131 flush_tlb_page(vpage);
133 set_intr_state(intr_state);
134 return 0;
135 }
137 void unmap_page(int vpage)
138 {
139 uint32_t *pgtbl;
140 int diridx = PAGE_TO_PGTBL(vpage);
141 int pgidx = PAGE_TO_PGTBL_PG(vpage);
143 int intr_state = get_intr_state();
144 disable_intr();
146 if(!(pgdir[diridx] & PG_PRESENT)) {
147 goto err;
148 }
149 pgtbl = PGTBL(diridx);
151 if(!(pgtbl[pgidx] & PG_PRESENT)) {
152 goto err;
153 }
154 pgtbl[pgidx] = 0;
155 flush_tlb_page(vpage);
157 if(0) {
158 err:
159 printf("unmap_page(%d): page already not mapped\n", vpage);
160 }
161 set_intr_state(intr_state);
162 }
164 /* if ppg_start is -1, we allocate physical pages to map with alloc_phys_page() */
165 int map_page_range(int vpg_start, int pgcount, int ppg_start, unsigned int attr)
166 {
167 int i, phys_pg;
169 for(i=0; i<pgcount; i++) {
170 phys_pg = ppg_start < 0 ? -1 : ppg_start + i;
171 map_page(vpg_start + i, phys_pg, attr);
172 }
173 return 0;
174 }
176 /* if paddr is 0, we allocate physical pages with alloc_phys_page() */
177 int map_mem_range(uint32_t vaddr, size_t sz, uint32_t paddr, unsigned int attr)
178 {
179 int vpg_start, ppg_start, num_pages;
181 if(!sz) return -1;
183 if(ADDR_TO_PGOFFS(paddr)) {
184 panic("map_mem_range called with unaligned physical address: %x\n", paddr);
185 }
187 vpg_start = ADDR_TO_PAGE(vaddr);
188 ppg_start = paddr > 0 ? ADDR_TO_PAGE(paddr) : -1;
189 num_pages = ADDR_TO_PAGE(sz) + 1;
191 return map_page_range(vpg_start, num_pages, ppg_start, attr);
192 }
194 uint32_t virt_to_phys(uint32_t vaddr)
195 {
196 uint32_t pgaddr, *pgtbl;
197 int diridx = ADDR_TO_PGTBL(vaddr);
198 int pgidx = ADDR_TO_PGTBL_PG(vaddr);
200 if(!(pgdir[diridx] & PG_PRESENT)) {
201 panic("virt_to_phys(%x): page table %d not present\n", vaddr, diridx);
202 }
203 pgtbl = PGTBL(diridx);
205 if(!(pgtbl[pgidx] & PG_PRESENT)) {
206 panic("virt_to_phys(%x): page %d not present\n", vaddr, ADDR_TO_PAGE(vaddr));
207 }
208 pgaddr = pgtbl[pgidx] & PGENT_ADDR_MASK;
210 return pgaddr | ADDR_TO_PGOFFS(vaddr);
211 }
213 /* allocate a contiguous block of virtual memory pages along with
214 * backing physical memory for them, and update the page table.
215 */
216 int pgalloc(int num, int area)
217 {
218 int intr_state, ret = -1;
219 struct page_range *node, *prev, dummy;
221 intr_state = get_intr_state();
222 disable_intr();
224 dummy.next = pglist[area];
225 node = pglist[area];
226 prev = &dummy;
228 while(node) {
229 if(node->end - node->start >= num) {
230 ret = node->start;
231 node->start += num;
233 if(node->start == node->end) {
234 prev->next = node->next;
235 node->next = 0;
237 if(node == pglist[area]) {
238 pglist[area] = 0;
239 }
240 free_node(node);
241 }
242 break;
243 }
245 prev = node;
246 node = node->next;
247 }
249 if(ret >= 0) {
250 /* allocate physical storage and map */
251 if(map_page_range(ret, num, -1, 0) == -1) {
252 ret = -1;
253 }
254 }
256 set_intr_state(intr_state);
257 return ret;
258 }
260 void pgfree(int start, int num)
261 {
262 int i, area, end, intr_state;
263 struct page_range *node, *new, *prev, *next;
265 intr_state = get_intr_state();
266 disable_intr();
268 for(i=0; i<num; i++) {
269 uint32_t phys = virt_to_phys(PAGE_TO_ADDR(start + i));
270 if(phys) {
271 free_phys_page(ADDR_TO_PAGE(phys));
272 }
273 }
275 if(!(new = alloc_node())) {
276 panic("pgfree: can't allocate new page_range node to add the freed pages\n");
277 }
278 new->start = start;
279 end = new->end = start + num;
281 area = PAGE_TO_ADDR(start) >= KMEM_START ? MEM_KERNEL : MEM_USER;
283 if(!pglist[area] || pglist[area]->start > start) {
284 next = new->next = pglist[area];
285 pglist[area] = new;
286 prev = 0;
288 } else {
290 prev = 0;
291 node = pglist[area];
292 next = node ? node->next : 0;
294 while(node) {
295 if(!next || next->start > start) {
296 /* place here, after node */
297 new->next = next;
298 node->next = new;
299 prev = node; /* needed by coalesce after the loop */
300 break;
301 }
303 prev = node;
304 node = next;
305 next = node ? node->next : 0;
306 }
307 }
309 coalesce(prev, new, next);
310 set_intr_state(intr_state);
311 }
313 static void coalesce(struct page_range *low, struct page_range *mid, struct page_range *high)
314 {
315 if(high) {
316 if(mid->end == high->start) {
317 mid->end = high->end;
318 mid->next = high->next;
319 free_node(high);
320 }
321 }
323 if(low) {
324 if(low->end == mid->start) {
325 low->end += mid->end;
326 low->next = mid->next;
327 free_node(mid);
328 }
329 }
330 }
332 static void pgfault(int inum, uint32_t err)
333 {
334 printf("~~~~ PAGE FAULT ~~~~\n");
336 printf("fault address: %x\n", get_fault_addr());
338 if(err & PG_PRESENT) {
339 if(err & 8) {
340 printf("reserved bit set in some paging structure\n");
341 } else {
342 printf("%s protection violation ", (err & PG_WRITABLE) ? "write" : "read");
343 printf("in %s mode\n", err & PG_USER ? "user" : "kernel");
344 }
345 } else {
346 printf("page not present\n");
347 }
349 panic("unhandled page fault\n");
350 }
352 /* --- page range list node management --- */
353 #define NODES_IN_PAGE (PGSIZE / sizeof(struct page_range))
355 static struct page_range *alloc_node(void)
356 {
357 struct page_range *node;
358 int pg, i;
360 if(node_pool) {
361 node = node_pool;
362 node_pool = node_pool->next;
363 printf("alloc_node -> %x\n", (unsigned int)node);
364 return node;
365 }
367 /* no node structures in the pool, we need to allocate a new page,
368 * split it up into node structures, add them in the pool, and
369 * allocate one of them.
370 */
371 if(!(pg = pgalloc(1, MEM_KERNEL))) {
372 panic("ran out of physical memory while allocating VM range structures\n");
373 }
374 node_pool = (struct page_range*)PAGE_TO_ADDR(pg);
376 /* link them up, skip the first as we'll just allocate it anyway */
377 for(i=2; i<NODES_IN_PAGE; i++) {
378 node_pool[i - 1].next = node_pool + i;
379 }
380 node_pool[NODES_IN_PAGE - 1].next = 0;
382 /* grab the first and return it */
383 node = node_pool++;
384 printf("alloc_node -> %x\n", (unsigned int)node);
385 return node;
386 }
388 static void free_node(struct page_range *node)
389 {
390 node->next = node_pool;
391 node_pool = node;
392 printf("free_node\n");
393 }
396 void dbg_print_vm(int area)
397 {
398 struct page_range *node;
399 int last, intr_state;
401 intr_state = get_intr_state();
402 disable_intr();
404 node = pglist[area];
405 last = area == MEM_USER ? 0 : ADDR_TO_PAGE(KMEM_START);
407 printf("%s vm space\n", area == MEM_USER ? "user" : "kernel");
409 while(node) {
410 if(node->start > last) {
411 printf(" vm-used: %x -> %x\n", PAGE_TO_ADDR(last), PAGE_TO_ADDR(node->start));
412 }
414 printf(" vm-free: %x -> ", PAGE_TO_ADDR(node->start));
415 if(node->end >= PAGE_COUNT) {
416 printf("END\n");
417 } else {
418 printf("%x\n", PAGE_TO_ADDR(node->end));
419 }
421 last = node->end;
422 node = node->next;
423 }
425 set_intr_state(intr_state);
426 }