kern

view src/vm.c @ 18:096807345aa2

fixed the stupid mistake. forgot to page-align the page directory
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 Mar 2011 06:28:26 +0300
parents 098b1cb5eeaa
children 8be069e6bb05
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 /* defined in vm-asm.S */
12 void enable_paging(void);
13 void set_pgdir_addr(uint32_t addr);
14 uint32_t get_fault_addr(void);
16 static void pgfault(int inum, uint32_t err);
18 /* page directory */
19 static uint32_t *pgdir;
21 #define KMEM_START 0xc0000000
22 #define IDMAP_START 0xa0000
24 #define ATTR_PGDIR_MASK 0x3f
25 #define ATTR_PGTBL_MASK 0x1ff
26 #define ADDR_PGENT_MASK 0xfffff000
28 #define PAGEFAULT 14
30 void init_vm(struct mboot_info *mb)
31 {
32 init_mem(mb);
34 pgdir = (uint32_t*)alloc_phys_page();
35 memset(pgdir, 0, sizeof pgdir);
37 /* map the video memory and kernel code 1-1 */
38 map_mem_range(IDMAP_START, MAX_BRK - IDMAP_START, IDMAP_START, 0);
40 interrupt(PAGEFAULT, pgfault);
42 set_pgdir_addr((int32_t)pgdir);
43 enable_paging();
44 }
46 void map_page(int vpage, int ppage, unsigned int attr)
47 {
48 uint32_t *pgtbl;
49 int diridx = PAGE_TO_PGTBL(vpage);
50 int pgidx = PAGE_TO_PGTBL_PG(vpage);
52 if(!(pgdir[diridx] & PG_PRESENT)) {
53 uint32_t addr = alloc_phys_page();
54 pgtbl = (uint32_t*)addr;
55 memset(pgtbl, 0, PGSIZE);
57 pgdir[diridx] = addr | (attr & ATTR_PGDIR_MASK) | PG_PRESENT;
58 } else {
59 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
60 }
62 pgtbl[pgidx] = PAGE_TO_ADDR(ppage) | (attr & ATTR_PGTBL_MASK) | PG_PRESENT;
63 }
65 void unmap_page(int vpage)
66 {
67 uint32_t *pgtbl;
68 int diridx = PAGE_TO_PGTBL(vpage);
69 int pgidx = PAGE_TO_PGTBL_PG(vpage);
71 if(!(pgdir[diridx] & PG_PRESENT)) {
72 goto err;
73 }
74 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
76 if(!(pgtbl[pgidx] & PG_PRESENT)) {
77 goto err;
78 }
79 pgtbl[pgidx] = 0;
81 return;
82 err:
83 printf("unmap_page(%d): page already not mapped\n", vpage);
84 }
86 void map_page_range(int vpg_start, int pgcount, int ppg_start, unsigned int attr)
87 {
88 int i;
90 for(i=0; i<pgcount; i++) {
91 map_page(vpg_start + i, ppg_start + i, attr);
92 }
93 }
95 void map_mem_range(uint32_t vaddr, size_t sz, uint32_t paddr, unsigned int attr)
96 {
97 int vpg_start, ppg_start, num_pages;
99 if(!sz) return;
101 if(ADDR_TO_PGOFFS(paddr)) {
102 panic("map_mem_range called with unaligned physical address: %x\n", paddr);
103 }
105 vpg_start = ADDR_TO_PAGE(vaddr);
106 ppg_start = ADDR_TO_PAGE(paddr);
107 num_pages = ADDR_TO_PAGE(sz) + 1;
109 map_page_range(vpg_start, num_pages, ppg_start, attr);
110 }
113 /* if(mb->flags & MB_MMAP) {
114 struct mboot_mmap *mem, *mmap_end;
116 mem = mb->mmap;
117 mmap_end = (struct mboot_mmap*)((char*)mb->mmap + mb->mmap_len);
119 printf("memory map:\n");
120 while(mem < mmap_end) {
121 unsigned int end = mem->base_low + mem->length_low;
122 char *type = mem->type == MB_MEM_VALID ? "free:" : "hole:";
124 printf(" %s %x - %x (%u bytes)\n", type, mem->base_low, end, mem->length_low);
125 mem = (struct mboot_mmap*)((char*)mem + mem->skip + sizeof mem->skip);
126 }
127 }
129 if(mb->flags & MB_MEM) {
130 printf("lower memory: %ukb, upper mem: %ukb\n", mb->mem_lower, mb->mem_upper);
131 }
132 */
134 uint32_t virt_to_phys(uint32_t vaddr)
135 {
136 uint32_t pgaddr, *pgtbl;
137 int diridx = ADDR_TO_PGTBL(vaddr);
138 int pgidx = ADDR_TO_PGTBL_PG(vaddr);
140 if(!(pgdir[diridx] & PG_PRESENT)) {
141 panic("virt_to_phys(%x): page table %d not present\n", vaddr, diridx);
142 }
143 pgtbl = (uint32_t*)(pgdir[diridx] & PGENT_ADDR_MASK);
145 if(!(pgtbl[pgidx] & PG_PRESENT)) {
146 panic("virt_to_phys(%x): page %d not present\n", vaddr, ADDR_TO_PAGE(vaddr));
147 }
148 pgaddr = pgtbl[pgidx] & PGENT_ADDR_MASK;
150 return pgaddr | ADDR_TO_PGOFFS(vaddr);
151 }
153 static void pgfault(int inum, uint32_t err)
154 {
155 printf("~~~~ PAGE FAULT ~~~~\n");
157 printf("fault address: %x\n", get_fault_addr());
159 if(err & PG_PRESENT) {
160 if(err & 8) {
161 printf("reserved bit set in some paging structure\n");
162 } else {
163 printf("%s protection violation ", (err & PG_WRITABLE) ? "write" : "read");
164 printf("in %s mode\n", err & PG_USER ? "user" : "kernel");
165 }
166 } else {
167 printf("page not present\n");
168 }
169 }