kern

annotate src/vm.c @ 17:098b1cb5eeaa

forgot to add a shitload of files
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Mar 2011 21:39:14 +0200
parents
children 096807345aa2
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 /* defined in vm-asm.S */
nuclear@17 12 void enable_paging(void);
nuclear@17 13 void set_pgdir_addr(uint32_t addr);
nuclear@17 14 uint32_t get_fault_addr(void);
nuclear@17 15
nuclear@17 16 static void pgfault(int inum, uint32_t err);
nuclear@17 17
nuclear@17 18 /* page directory */
nuclear@17 19 static uint32_t pgdir[1024];
nuclear@17 20
nuclear@17 21 #define KMEM_START 0xc0000000
nuclear@17 22 #define IDMAP_START 0xa0000
nuclear@17 23
nuclear@17 24 #define ATTR_PGDIR_MASK 0x3f
nuclear@17 25 #define ATTR_PGTBL_MASK 0x1ff
nuclear@17 26 #define ADDR_PGENT_MASK 0xfffff000
nuclear@17 27
nuclear@17 28 #define PAGEFAULT 14
nuclear@17 29
nuclear@17 30 void init_vm(struct mboot_info *mb)
nuclear@17 31 {
nuclear@17 32 init_mem(mb);
nuclear@17 33
nuclear@17 34 memset(pgdir, 0, sizeof pgdir);
nuclear@17 35
nuclear@17 36 /* map the video memory and kernel code 1-1 */
nuclear@17 37 map_mem_range(IDMAP_START, MAX_BRK - IDMAP_START, IDMAP_START, 0);
nuclear@17 38
nuclear@17 39 interrupt(PAGEFAULT, pgfault);
nuclear@17 40
nuclear@17 41 set_pgdir_addr((int32_t)pgdir);
nuclear@17 42 enable_paging();
nuclear@17 43 }
nuclear@17 44
nuclear@17 45 void map_page(int vpage, int ppage, unsigned int attr)
nuclear@17 46 {
nuclear@17 47 uint32_t *pgtbl;
nuclear@17 48 int diridx = PAGE_TO_PGTBL(vpage);
nuclear@17 49 int pgidx = PAGE_TO_PGTBL_PG(vpage);
nuclear@17 50
nuclear@17 51 if(!(pgdir[diridx] & PG_PRESENT)) {
nuclear@17 52 uint32_t addr = alloc_phys_page();
nuclear@17 53 pgtbl = (uint32_t*)addr;
nuclear@17 54 pgdir[diridx] = addr | (attr & ATTR_PGDIR_MASK) | PG_PRESENT;
nuclear@17 55 } else {
nuclear@17 56 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
nuclear@17 57 }
nuclear@17 58
nuclear@17 59 pgtbl[pgidx] = PAGE_TO_ADDR(ppage) | (attr & ATTR_PGTBL_MASK) | PG_PRESENT;
nuclear@17 60 }
nuclear@17 61
nuclear@17 62 void unmap_page(int vpage)
nuclear@17 63 {
nuclear@17 64 uint32_t *pgtbl;
nuclear@17 65 int diridx = PAGE_TO_PGTBL(vpage);
nuclear@17 66 int pgidx = PAGE_TO_PGTBL_PG(vpage);
nuclear@17 67
nuclear@17 68 if(!(pgdir[diridx] & PG_PRESENT)) {
nuclear@17 69 goto err;
nuclear@17 70 }
nuclear@17 71 pgtbl = (uint32_t*)(pgdir[diridx] & ADDR_PGENT_MASK);
nuclear@17 72
nuclear@17 73 if(!(pgtbl[pgidx] & PG_PRESENT)) {
nuclear@17 74 goto err;
nuclear@17 75 }
nuclear@17 76 pgtbl[pgidx] = 0;
nuclear@17 77
nuclear@17 78 return;
nuclear@17 79 err:
nuclear@17 80 printf("unmap_page(%d): page already not mapped\n", vpage);
nuclear@17 81 }
nuclear@17 82
nuclear@17 83 void map_page_range(int vpg_start, int pgcount, int ppg_start, unsigned int attr)
nuclear@17 84 {
nuclear@17 85 int i;
nuclear@17 86
nuclear@17 87 for(i=0; i<pgcount; i++) {
nuclear@17 88 map_page(vpg_start + i, ppg_start + i, attr);
nuclear@17 89 }
nuclear@17 90 }
nuclear@17 91
nuclear@17 92 void map_mem_range(uint32_t vaddr, size_t sz, uint32_t paddr, unsigned int attr)
nuclear@17 93 {
nuclear@17 94 int vpg_start, ppg_start, num_pages;
nuclear@17 95
nuclear@17 96 if(!sz) return;
nuclear@17 97
nuclear@17 98 if(ADDR_TO_PGOFFS(paddr)) {
nuclear@17 99 panic("map_mem_range called with unaligned physical address: %x\n", paddr);
nuclear@17 100 }
nuclear@17 101
nuclear@17 102 vpg_start = ADDR_TO_PAGE(vaddr);
nuclear@17 103 ppg_start = ADDR_TO_PAGE(paddr);
nuclear@17 104 num_pages = ADDR_TO_PAGE(sz) + 1;
nuclear@17 105
nuclear@17 106 map_page_range(vpg_start, num_pages, ppg_start, attr);
nuclear@17 107 }
nuclear@17 108
nuclear@17 109
nuclear@17 110 /* if(mb->flags & MB_MMAP) {
nuclear@17 111 struct mboot_mmap *mem, *mmap_end;
nuclear@17 112
nuclear@17 113 mem = mb->mmap;
nuclear@17 114 mmap_end = (struct mboot_mmap*)((char*)mb->mmap + mb->mmap_len);
nuclear@17 115
nuclear@17 116 printf("memory map:\n");
nuclear@17 117 while(mem < mmap_end) {
nuclear@17 118 unsigned int end = mem->base_low + mem->length_low;
nuclear@17 119 char *type = mem->type == MB_MEM_VALID ? "free:" : "hole:";
nuclear@17 120
nuclear@17 121 printf(" %s %x - %x (%u bytes)\n", type, mem->base_low, end, mem->length_low);
nuclear@17 122 mem = (struct mboot_mmap*)((char*)mem + mem->skip + sizeof mem->skip);
nuclear@17 123 }
nuclear@17 124 }
nuclear@17 125
nuclear@17 126 if(mb->flags & MB_MEM) {
nuclear@17 127 printf("lower memory: %ukb, upper mem: %ukb\n", mb->mem_lower, mb->mem_upper);
nuclear@17 128 }
nuclear@17 129 */
nuclear@17 130
nuclear@17 131 static void pgfault(int inum, uint32_t err)
nuclear@17 132 {
nuclear@17 133 printf("~~~~ PAGE FAULT ~~~~\n");
nuclear@17 134
nuclear@17 135 printf("fault address: %x\n", get_fault_addr());
nuclear@17 136
nuclear@17 137 if(err & PG_PRESENT) {
nuclear@17 138 if(err & 8) {
nuclear@17 139 printf("reserved bit set in some paging structure\n");
nuclear@17 140 } else {
nuclear@17 141 printf("%s protection violation ", (err & PG_WRITABLE) ? "write" : "read");
nuclear@17 142 printf("in %s mode\n", err & PG_USER ? "user" : "kernel");
nuclear@17 143 }
nuclear@17 144 } else {
nuclear@17 145 printf("page not present\n");
nuclear@17 146 }
nuclear@17 147 }