kern
view src/vm.c @ 46:b793b8fcba7d
apparently free_phys_page was never tested. the check for double-freeing a page
was inverted.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 28 Jul 2011 05:33:59 +0300 |
parents | 5f6c5751ae05 |
children | f65b348780e3 |
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 int unmap_page(int vpage)
138 {
139 uint32_t *pgtbl;
140 int res = 0;
141 int diridx = PAGE_TO_PGTBL(vpage);
142 int pgidx = PAGE_TO_PGTBL_PG(vpage);
144 int intr_state = get_intr_state();
145 disable_intr();
147 if(!(pgdir[diridx] & PG_PRESENT)) {
148 goto err;
149 }
150 pgtbl = PGTBL(diridx);
152 if(!(pgtbl[pgidx] & PG_PRESENT)) {
153 goto err;
154 }
155 pgtbl[pgidx] = 0;
156 flush_tlb_page(vpage);
158 if(0) {
159 err:
160 printf("unmap_page(%d): page already not mapped\n", vpage);
161 res = -1;
162 }
163 set_intr_state(intr_state);
164 return res;
165 }
167 /* if ppg_start is -1, we allocate physical pages to map with alloc_phys_page() */
168 int map_page_range(int vpg_start, int pgcount, int ppg_start, unsigned int attr)
169 {
170 int i, phys_pg;
172 for(i=0; i<pgcount; i++) {
173 phys_pg = ppg_start < 0 ? -1 : ppg_start + i;
174 map_page(vpg_start + i, phys_pg, attr);
175 }
176 return 0;
177 }
179 int unmap_page_range(int vpg_start, int pgcount)
180 {
181 int i, res = 0;
183 for(i=0; i<pgcount; i++) {
184 if(unmap_page(vpg_start + i) == -1) {
185 res = -1;
186 }
187 }
188 return res;
189 }
191 /* if paddr is 0, we allocate physical pages with alloc_phys_page() */
192 int map_mem_range(uint32_t vaddr, size_t sz, uint32_t paddr, unsigned int attr)
193 {
194 int vpg_start, ppg_start, num_pages;
196 if(!sz) return -1;
198 if(ADDR_TO_PGOFFS(paddr)) {
199 panic("map_mem_range called with unaligned physical address: %x\n", paddr);
200 }
202 vpg_start = ADDR_TO_PAGE(vaddr);
203 ppg_start = paddr > 0 ? ADDR_TO_PAGE(paddr) : -1;
204 num_pages = ADDR_TO_PAGE(sz) + 1;
206 return map_page_range(vpg_start, num_pages, ppg_start, attr);
207 }
209 uint32_t virt_to_phys(uint32_t vaddr)
210 {
211 int pg;
212 uint32_t pgaddr;
214 if((pg = virt_to_phys_page(ADDR_TO_PAGE(vaddr))) == -1) {
215 return 0;
216 }
217 pgaddr = PAGE_TO_ADDR(pg);
219 return pgaddr | ADDR_TO_PGOFFS(vaddr);
220 }
222 int virt_to_phys_page(int vpg)
223 {
224 uint32_t pgaddr, *pgtbl;
225 int diridx, pgidx;
227 if(vpg < 0 || vpg >= PAGE_COUNT) {
228 return -1;
229 }
231 diridx = PAGE_TO_PGTBL(vpg);
232 pgidx = PAGE_TO_PGTBL_PG(vpg);
234 if(!(pgdir[diridx] & PG_PRESENT)) {
235 return -1;
236 }
237 pgtbl = PGTBL(diridx);
239 if(!(pgtbl[pgidx] & PG_PRESENT)) {
240 return -1;
241 }
242 pgaddr = pgtbl[pgidx] & PGENT_ADDR_MASK;
243 return ADDR_TO_PAGE(pgaddr);
244 }
246 /* allocate a contiguous block of virtual memory pages along with
247 * backing physical memory for them, and update the page table.
248 */
249 int pgalloc(int num, int area)
250 {
251 int intr_state, ret = -1;
252 struct page_range *node, *prev, dummy;
253 unsigned int attr = 0; /* TODO */
255 intr_state = get_intr_state();
256 disable_intr();
258 dummy.next = pglist[area];
259 node = pglist[area];
260 prev = &dummy;
262 while(node) {
263 if(node->end - node->start >= num) {
264 ret = node->start;
265 node->start += num;
267 if(node->start == node->end) {
268 prev->next = node->next;
269 node->next = 0;
271 if(node == pglist[area]) {
272 pglist[area] = 0;
273 }
274 free_node(node);
275 }
276 break;
277 }
279 prev = node;
280 node = node->next;
281 }
283 if(ret >= 0) {
284 /* allocate physical storage and map */
285 if(map_page_range(ret, num, -1, attr) == -1) {
286 ret = -1;
287 }
288 }
290 set_intr_state(intr_state);
291 return ret;
292 }
294 int pgalloc_vrange(int start, int num)
295 {
296 struct page_range *node, *prev, dummy;
297 int area, intr_state, ret = -1;
298 unsigned int attr = 0; /* TODO */
300 area = (start >= ADDR_TO_PAGE(KMEM_START)) ? MEM_KERNEL : MEM_USER;
301 if(area == KMEM_USER && start + num > ADDR_TO_PAGE(KMEM_START)) {
302 printf("pgalloc_vrange: invalid range request crossing user/kernel split\n");
303 return -1;
304 }
306 intr_state = get_intr_state();
307 disable_intr();
309 dummy.next = pglist[area];
310 node = pglist[area];
311 prev = &dummy;
313 /* check to see if the requested VM range is available */
314 node = pglist[area];
315 while(node) {
316 if(start >= node->start && start + num <= node->end) {
317 ret = node->start;
318 node->start += num;
320 if(node->start == node->end) {
321 prev->next = node->next;
322 node->next = 0;
324 if(node == pglist[area]) {
325 pglist[area] = 0;
326 }
327 free_node(node);
328 }
329 break;
330 }
332 prev = node;
333 node = node->next;
334 }
336 if(ret >= 0) {
337 /* allocate physical storage and map */
338 if(map_page_range(ret, num, -1, attr) == -1) {
339 ret = -1;
340 }
341 }
343 set_intr_state(intr_state);
344 return ret;
345 }
347 void pgfree(int start, int num)
348 {
349 int i, area, intr_state;
350 struct page_range *node, *new, *prev, *next;
352 intr_state = get_intr_state();
353 disable_intr();
355 for(i=0; i<num; i++) {
356 int phys_pg = virt_to_phys_page(start + i);
357 if(phys_pg != -1) {
358 free_phys_page(phys_pg);
359 }
360 }
362 if(!(new = alloc_node())) {
363 panic("pgfree: can't allocate new page_range node to add the freed pages\n");
364 }
365 new->start = start;
366 new->end = start + num;
368 area = PAGE_TO_ADDR(start) >= KMEM_START ? MEM_KERNEL : MEM_USER;
370 if(!pglist[area] || pglist[area]->start > start) {
371 next = new->next = pglist[area];
372 pglist[area] = new;
373 prev = 0;
375 } else {
377 prev = 0;
378 node = pglist[area];
379 next = node ? node->next : 0;
381 while(node) {
382 if(!next || next->start > start) {
383 /* place here, after node */
384 new->next = next;
385 node->next = new;
386 prev = node; /* needed by coalesce after the loop */
387 break;
388 }
390 prev = node;
391 node = next;
392 next = node ? node->next : 0;
393 }
394 }
396 coalesce(prev, new, next);
397 set_intr_state(intr_state);
398 }
400 static void coalesce(struct page_range *low, struct page_range *mid, struct page_range *high)
401 {
402 if(high) {
403 if(mid->end == high->start) {
404 mid->end = high->end;
405 mid->next = high->next;
406 free_node(high);
407 }
408 }
410 if(low) {
411 if(low->end == mid->start) {
412 low->end += mid->end;
413 low->next = mid->next;
414 free_node(mid);
415 }
416 }
417 }
419 static void pgfault(int inum, uint32_t err)
420 {
421 printf("~~~~ PAGE FAULT ~~~~\n");
423 printf("fault address: %x\n", get_fault_addr());
425 if(err & PG_PRESENT) {
426 if(err & 8) {
427 printf("reserved bit set in some paging structure\n");
428 } else {
429 printf("%s protection violation ", (err & PG_WRITABLE) ? "write" : "read");
430 printf("in %s mode\n", err & PG_USER ? "user" : "kernel");
431 }
432 } else {
433 printf("page not present\n");
434 }
436 panic("unhandled page fault\n");
437 }
439 /* --- page range list node management --- */
440 #define NODES_IN_PAGE (PGSIZE / sizeof(struct page_range))
442 static struct page_range *alloc_node(void)
443 {
444 struct page_range *node;
445 int pg, i;
447 if(node_pool) {
448 node = node_pool;
449 node_pool = node_pool->next;
450 printf("alloc_node -> %x\n", (unsigned int)node);
451 return node;
452 }
454 /* no node structures in the pool, we need to allocate a new page,
455 * split it up into node structures, add them in the pool, and
456 * allocate one of them.
457 */
458 if(!(pg = pgalloc(1, MEM_KERNEL))) {
459 panic("ran out of physical memory while allocating VM range structures\n");
460 }
461 node_pool = (struct page_range*)PAGE_TO_ADDR(pg);
463 /* link them up, skip the first as we'll just allocate it anyway */
464 for(i=2; i<NODES_IN_PAGE; i++) {
465 node_pool[i - 1].next = node_pool + i;
466 }
467 node_pool[NODES_IN_PAGE - 1].next = 0;
469 /* grab the first and return it */
470 node = node_pool++;
471 printf("alloc_node -> %x\n", (unsigned int)node);
472 return node;
473 }
475 static void free_node(struct page_range *node)
476 {
477 node->next = node_pool;
478 node_pool = node;
479 printf("free_node\n");
480 }
483 /* clone_vmem makes a copy of the current page tables, thus duplicating
484 * the virtual address space.
485 *
486 * Returns the physical address of the new page directory.
487 */
488 uint32_t clone_vmem(void)
489 {
490 int i, dirpg, tblpg;
491 uint32_t paddr;
492 uint32_t *ndir, *ntbl;
494 if((dirpg = pgalloc(1, MEM_KERNEL)) == -1) {
495 panic("clone_vmem: failed to allocate page directory page\n");
496 }
497 ndir = (uint32_t*)PAGE_TO_ADDR(dirpg);
499 if((tblpg = pgalloc(1, MEM_KERNEL)) == -1) {
500 panic("clone_vmem: failed to allocate page table page\n");
501 }
502 ntbl = (uint32_t*)PAGE_TO_ADDR(tblpg);
504 /* we will allocate physical pages and map them to this virtual page
505 * as needed in the loop below.
506 */
507 free_phys_page(virt_to_phys(tblpg));
509 for(i=0; i<1024; i++) {
510 if(pgdir[i] & PG_PRESENT) {
511 paddr = alloc_phys_page();
512 map_page(tblpg, ADDR_TO_PAGE(paddr), 0);
514 /* copy the page table */
515 memcpy(ntbl, PGTBL(i), PGSIZE);
517 /* set the new page directory entry */
518 ndir[i] = paddr | (pgdir[i] & PGOFFS_MASK);
519 } else {
520 ndir[i] = 0;
521 }
522 }
524 paddr = virt_to_phys(dirpg);
526 /* unmap before freeing to avoid deallocating the physical pages */
527 unmap_page(dirpg);
528 unmap_page(tblpg);
530 pgfree(dirpg, 1);
531 pgfree(tblpg, 1);
533 return paddr;
534 }
537 void dbg_print_vm(int area)
538 {
539 struct page_range *node;
540 int last, intr_state;
542 intr_state = get_intr_state();
543 disable_intr();
545 node = pglist[area];
546 last = area == MEM_USER ? 0 : ADDR_TO_PAGE(KMEM_START);
548 printf("%s vm space\n", area == MEM_USER ? "user" : "kernel");
550 while(node) {
551 if(node->start > last) {
552 printf(" vm-used: %x -> %x\n", PAGE_TO_ADDR(last), PAGE_TO_ADDR(node->start));
553 }
555 printf(" vm-free: %x -> ", PAGE_TO_ADDR(node->start));
556 if(node->end >= PAGE_COUNT) {
557 printf("END\n");
558 } else {
559 printf("%x\n", PAGE_TO_ADDR(node->end));
560 }
562 last = node->end;
563 node = node->next;
564 }
566 set_intr_state(intr_state);
567 }