kern

annotate src/vm.c @ 39:92297f65aaef

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