swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 1 | #include "paging.h" |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 2 | #include "alloc.h" |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 3 | #include "io.h" |
| 4 | #include "kint.h" |
| 5 | #include "log.h" |
| 6 | #include "pic.h" |
| 7 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 8 | #define NUM_FRAMES 0xffffffff / 0x1000 / 32 |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 9 | /* frames bitset, 0 = free, 1 = used */ |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 10 | static uint frames[NUM_FRAMES]; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 11 | |
| 12 | static uint first_page_table[1024] __attribute__((aligned(4096))); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 13 | uint kernel_page_directory[1024] __attribute__((aligned(4096))); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 14 | |
| 15 | /* frame utils */ |
| 16 | |
| 17 | #define BITS 32 |
| 18 | |
| 19 | static void set_frame(size_t frame_addr) |
| 20 | { |
| 21 | uint frame = frame_addr / 0x1000; // page aligned |
| 22 | frames[frame / BITS] |= 1 << (frame % BITS); |
| 23 | } |
| 24 | |
| 25 | static bool test_frame(size_t frame_addr) |
| 26 | { |
| 27 | uint frame = frame_addr / 0x1000; // page aligned |
| 28 | return frames[frame / BITS] & 1 << (frame % BITS); |
| 29 | } |
| 30 | |
| 31 | static void clear_frame(size_t frame_addr) |
| 32 | { |
| 33 | uint frame = frame_addr / 0x1000; // page aligned |
| 34 | frames[frame / BITS] &= ~(1 << (frame % BITS)); |
| 35 | } |
| 36 | |
| 37 | static uint first_free_frame() |
| 38 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 39 | for (int i = 0; i < NUM_FRAMES / BITS; i++) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 40 | { |
| 41 | /* |
| 42 | * If there are any zeroes, ~ will yield a non-zero result, |
| 43 | * meaning that there are pages free. Otherwise, check next set |
| 44 | */ |
| 45 | if (!~frames[i]) |
| 46 | continue; |
| 47 | |
| 48 | for (int j = 0; j < BITS; j++) |
| 49 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 50 | if ((frames[i] & (1 << j)) == 0) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 51 | { |
| 52 | /* found unused frame */ |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 53 | uint frame = i * BITS + j; |
| 54 | kprintf("first_free_frame returning %d\n", frame); |
| 55 | // kpanic("asdf"); |
| 56 | return frame; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* did not find a free frame, panic */ |
| 62 | kpanic("first_free_frame failed! no free frames"); |
| 63 | } |
| 64 | |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 65 | void alloc_frame(uint *page_table_entry, bool user, bool writable) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 66 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 67 | if (*page_table_entry & 1) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 68 | return; /* frame already allocated */ |
| 69 | |
| 70 | uint frame = first_free_frame(); |
| 71 | // kprintf("first_free_frame found %d\n", frame); |
| 72 | set_frame(frame * 0x1000); /* mark as mapped */ |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 73 | *page_table_entry = frame | 1 | writable << 1 | user << 2; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void free_frame(uint page) |
| 77 | { |
| 78 | clear_frame(page / 0x1000); |
| 79 | } |
| 80 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 81 | void map_4mb(uint *dir, size_t virt_start, size_t phys_start, bool user, |
| 82 | bool rw) |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 83 | { |
| 84 | uint page = virt_start / 0x1000; |
| 85 | uint table = virt_start >> 22; |
| 86 | |
| 87 | for (uint i = 0; i < 1024 * 0x1000; i += 0x1000) |
| 88 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 89 | set_frame(i); |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 90 | } |
| 91 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 92 | dir[table] = 0b10000011; |
| 93 | } |
| 94 | |
| 95 | uint *get_or_create_table(uint *dir, uint table, bool user, bool rw) |
| 96 | { |
| 97 | if (dir[table] >> 12) |
| 98 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 99 | return (uint *)(size_t)PHYS_TO_VIRT((dir[table] ^ 0xfff)); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | uint *page_table = kmalloc_a(sizeof(uint[1024])); |
| 103 | dir[table] = (uint)page_table | 1 | rw << 1 | user << 2; |
| 104 | return page_table; |
| 105 | } |
| 106 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 107 | void unmap_page(uint *dir, void *virt) |
| 108 | { |
| 109 | uint page = ((size_t)virt / 0x1000) % 1024; |
| 110 | uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false); |
| 111 | |
| 112 | table[page] = 0; |
| 113 | } |
| 114 | |
| 115 | void map_page_to(uint *dir, void *virt, void *frame_p, bool writable, bool user) |
| 116 | { |
| 117 | uint page = ((size_t)virt / 0x1000) % 1024; |
| 118 | uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false); |
| 119 | |
| 120 | table[page] = (((uint)frame_p) ^ 0xfff) | 1 | writable << 1 | user << 2; |
| 121 | } |
| 122 | |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 123 | void alloc_kernel_page(uint *virt) |
| 124 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 125 | alloc_page(kernel_page_directory, virt); |
| 126 | } |
| 127 | |
| 128 | void alloc_page(uint *dir, uint *virt) |
| 129 | { |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 130 | // Page number % pages per table |
| 131 | uint page = ((size_t)virt / 0x1000) % 1024; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 132 | uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false); |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 133 | |
| 134 | alloc_frame(&table[page], false, false); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame^] | 135 | |
| 136 | kprintf("alloc_page table[page] = %d (0x%x)\n", table[page], table[page]); |
| 137 | return; |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void alloc_kernel_page_range(uint *from, uint *to) |
| 141 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 142 | uint f = (size_t)from / 0x1000, t = (size_t)to / 0x1000; |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 143 | |
| 144 | do |
| 145 | { |
| 146 | alloc_kernel_page((uint *)(size_t)t); |
| 147 | t += 0x1000; // next page |
| 148 | } while (f < t); |
| 149 | } |
| 150 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 151 | void map_page(uint *dir, size_t virt_start, bool user, bool rw) |
| 152 | { |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 153 | // Page number % pages per table |
| 154 | uint page = (virt_start / 0x1000) % 1024; |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 155 | uint table = virt_start >> 22; |
| 156 | uint frame = first_free_frame(); |
| 157 | |
| 158 | // If 4mb map OR (maps to table AND table maps page) |
| 159 | if ((dir[table] & 1 && dir[table] & 1 << 7) || |
| 160 | (dir[table] >> 12 && ((uint *)(size_t)dir[table])[page] & 1)) |
| 161 | { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | set_frame(frame); |
| 166 | uint *t = get_or_create_table(dir, table, user, rw); |
| 167 | alloc_frame(t + page, user, rw); |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 168 | } |
| 169 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 170 | /* paging stuff */ |
| 171 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 172 | void init_paging() |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 173 | { |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 174 | memset(kernel_page_directory, 0, 1024 * 4); |
| 175 | map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false, |
| 176 | false); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 177 | |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 178 | load_page_directory((uint)kernel_page_directory - 0xC0000000); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 179 | add_interrupt_handler(14, page_fault); |
| 180 | } |
| 181 | |
| 182 | void page_fault(struct registers *regs) |
| 183 | { |
| 184 | kprintf("Page fault! eip = %d\n", regs->eip); |
| 185 | kpanic("Page fault"); |
| 186 | } |