kern

view src/vm.c @ 22:7ece008f09c5

writing the vm
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 03 Apr 2011 18:42:19 +0300
parents 8be069e6bb05
children 5454cee245a3
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 ATTR_PGDIR_MASK 0x3f
15 #define ATTR_PGTBL_MASK 0x1ff
16 #define ADDR_PGENT_MASK 0xfffff000
18 #define PAGEFAULT 14
21 struct page_range {
22 int start, end;
23 struct page_range *next;
24 };
26 /* defined in vm-asm.S */
27 void enable_paging(void);
28 void set_pgdir_addr(uint32_t addr);
29 uint32_t get_fault_addr(void);
31 static void pgfault(int inum, uint32_t err);
32 static struct page_range *alloc_node(void);
33 static void free_node(struct page_range *node);
35 /* page directory */
36 static uint32_t *pgdir;
38 /* 2 lists of free ranges, for kernel memory and user memory */
39 static struct page_range *pglist[2];
40 /* list of free page_range structures to be used in the lists */
41 static struct page_range *node_pool;
44 void init_vm(struct mboot_info *mb)
45 {
46 uint32_t idmap_end;
48 init_mem(mb);
50 pgdir = (uint32_t*)alloc_phys_page();
51 memset(pgdir, 0, sizeof pgdir);
53 /* map the video memory and kernel code 1-1 */
54 get_kernel_mem_range(0, &idmap_end);
55 map_mem_range(IDMAP_START, idmap_end - IDMAP_START, IDMAP_START, 0);
57 interrupt(PAGEFAULT, pgfault);
59 set_pgdir_addr((int32_t)pgdir);
60 enable_paging();
61 }
63 void map_page(int vpage, int ppage, unsigned int attr)
64 {
65 uint32_t *pgtbl;
66 int diridx = PAGE_TO_PGTBL(vpage);
67 int pgidx = PAGE_TO_PGTBL_PG(vpage);
69 if(!(pgdir[diridx] & PG_PRESENT)) {
70 uint32_t addr = alloc_phys_page();
71 pgtbl = (uint32_t*)addr;
72 memset(pgtbl, 0, PGSIZE);
74 pgdir[diridx] = addr | (attr & ATTR_PGDIR_MASK) | PG_PRESENT;
75 } else {
76 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
77 }
79 pgtbl[pgidx] = PAGE_TO_ADDR(ppage) | (attr & ATTR_PGTBL_MASK) | PG_PRESENT;
80 }
82 void unmap_page(int vpage)
83 {
84 uint32_t *pgtbl;
85 int diridx = PAGE_TO_PGTBL(vpage);
86 int pgidx = PAGE_TO_PGTBL_PG(vpage);
88 if(!(pgdir[diridx] & PG_PRESENT)) {
89 goto err;
90 }
91 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
93 if(!(pgtbl[pgidx] & PG_PRESENT)) {
94 goto err;
95 }
96 pgtbl[pgidx] = 0;
98 return;
99 err:
100 printf("unmap_page(%d): page already not mapped\n", vpage);
101 }
103 /* if ppg_start is -1, we allocate physical pages to map with alloc_phys_page() */
104 void map_page_range(int vpg_start, int pgcount, int ppg_start, unsigned int attr)
105 {
106 int i;
108 for(i=0; i<pgcount; i++) {
109 uint32_t paddr = ppg_start == -1 ? alloc_phys_page() : ppg_start + i;
111 map_page(vpg_start + i, paddr, attr);
112 }
113 }
115 void map_mem_range(uint32_t vaddr, size_t sz, uint32_t paddr, unsigned int attr)
116 {
117 int vpg_start, ppg_start, num_pages;
119 if(!sz) return;
121 if(ADDR_TO_PGOFFS(paddr)) {
122 panic("map_mem_range called with unaligned physical address: %x\n", paddr);
123 }
125 vpg_start = ADDR_TO_PAGE(vaddr);
126 ppg_start = ADDR_TO_PAGE(paddr);
127 num_pages = ADDR_TO_PAGE(sz) + 1;
129 map_page_range(vpg_start, num_pages, ppg_start, attr);
130 }
132 uint32_t virt_to_phys(uint32_t vaddr)
133 {
134 uint32_t pgaddr, *pgtbl;
135 int diridx = ADDR_TO_PGTBL(vaddr);
136 int pgidx = ADDR_TO_PGTBL_PG(vaddr);
138 if(!(pgdir[diridx] & PG_PRESENT)) {
139 panic("virt_to_phys(%x): page table %d not present\n", vaddr, diridx);
140 }
141 pgtbl = (uint32_t*)(pgdir[diridx] & PGENT_ADDR_MASK);
143 if(!(pgtbl[pgidx] & PG_PRESENT)) {
144 panic("virt_to_phys(%x): page %d not present\n", vaddr, ADDR_TO_PAGE(vaddr));
145 }
146 pgaddr = pgtbl[pgidx] & PGENT_ADDR_MASK;
148 return pgaddr | ADDR_TO_PGOFFS(vaddr);
149 }
151 /* allocate a contiguous block of virtual memory pages along with
152 * backing physical memory for them, and update the page table.
153 */
154 int pgalloc(int num, int area)
155 {
156 int ret = -1;
157 struct page_range *node, *prev, dummy;
159 dummy.next = pglist[area];
160 node = pglist[area];
161 prev = &dummy;
163 while(node) {
164 if(node->end - node->start >= num) {
165 ret = node->start;
166 node->start += num;
168 if(node->start == node->end) {
169 prev->next = node->next;
170 node->next = 0;
172 if(node == pglist[area]) {
173 pglist[area] = 0;
174 }
175 free_node(node);
176 }
177 break;
178 }
180 prev = node;
181 node = node->next;
182 }
184 if(ret >= 0) {
185 /* allocate physical storage and map them */
186 map_page_range(ret, num, -1, 0);
187 }
189 return ret;
190 }
192 void pgfree(int start, int num)
193 {
194 /* TODO */
195 }
197 static void pgfault(int inum, uint32_t err)
198 {
199 printf("~~~~ PAGE FAULT ~~~~\n");
201 printf("fault address: %x\n", get_fault_addr());
203 if(err & PG_PRESENT) {
204 if(err & 8) {
205 printf("reserved bit set in some paging structure\n");
206 } else {
207 printf("%s protection violation ", (err & PG_WRITABLE) ? "write" : "read");
208 printf("in %s mode\n", err & PG_USER ? "user" : "kernel");
209 }
210 } else {
211 printf("page not present\n");
212 }
214 panic("unhandled page fault\n");
215 }
217 /* --- page range list node management --- */
218 static struct page_range *alloc_node(void)
219 {
220 struct page_range *node;
221 uint32_t paddr;
223 if(node_pool) {
224 node = node_pool;
225 node_pool = node_pool->next;
226 return node;
227 }
229 /* no node structures in the pool, we need to allocate and map
230 * a page, split it up into node structures, add them in the pool
231 * and allocate one of them.
232 */
233 if(!(paddr = alloc_phys_page())) {
234 panic("ran out of physical memory while allocating VM range structures\n");
235 }
237 /* TODO cont. */
238 return 0;
239 }
241 static void free_node(struct page_range *node)
242 {
243 node->next = node_pool;
244 node_pool = node;
245 }