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 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 15 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 16 | /* frame utils */ |
| 17 | |
| 18 | #define BITS 32 |
| 19 | |
| 20 | static void set_frame(size_t frame_addr) |
| 21 | { |
| 22 | uint frame = frame_addr / 0x1000; // page aligned |
| 23 | frames[frame / BITS] |= 1 << (frame % BITS); |
| 24 | } |
| 25 | |
| 26 | static bool test_frame(size_t frame_addr) |
| 27 | { |
| 28 | uint frame = frame_addr / 0x1000; // page aligned |
| 29 | return frames[frame / BITS] & 1 << (frame % BITS); |
| 30 | } |
| 31 | |
| 32 | static void clear_frame(size_t frame_addr) |
| 33 | { |
| 34 | uint frame = frame_addr / 0x1000; // page aligned |
| 35 | frames[frame / BITS] &= ~(1 << (frame % BITS)); |
| 36 | } |
| 37 | |
| 38 | static uint first_free_frame() |
| 39 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 40 | for (int i = 0; i < NUM_FRAMES / BITS; i++) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 41 | { |
| 42 | /* |
| 43 | * If there are any zeroes, ~ will yield a non-zero result, |
| 44 | * meaning that there are pages free. Otherwise, check next set |
| 45 | */ |
| 46 | if (!~frames[i]) |
| 47 | continue; |
| 48 | |
| 49 | for (int j = 0; j < BITS; j++) |
| 50 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 51 | if ((frames[i] & (1 << j)) == 0) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 52 | { |
| 53 | /* found unused frame */ |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 54 | uint frame = i * BITS + j; |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 55 | kprintf(DEBUG "first_free_frame returning %d\n", frame); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 56 | // kpanic("asdf"); |
| 57 | return frame; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* did not find a free frame, panic */ |
| 63 | kpanic("first_free_frame failed! no free frames"); |
| 64 | } |
| 65 | |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 66 | void alloc_frame(uint *page_table_entry, bool user, bool writable) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 67 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 68 | if (*page_table_entry & 1) |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 69 | return; /* frame already allocated */ |
| 70 | |
| 71 | uint frame = first_free_frame(); |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 72 | // kprintf(DEBUG "first_free_frame found %d\n", frame); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 73 | set_frame(frame * 0x1000); /* mark as mapped */ |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 74 | *page_table_entry = frame | 1 | writable << 1 | user << 2; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void free_frame(uint page) |
| 78 | { |
| 79 | clear_frame(page / 0x1000); |
| 80 | } |
| 81 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 82 | void map_4mb(uint *dir, size_t virt_start, size_t phys_start, bool user, |
| 83 | bool rw) |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 84 | { |
| 85 | uint page = virt_start / 0x1000; |
| 86 | uint table = virt_start >> 22; |
| 87 | |
| 88 | for (uint i = 0; i < 1024 * 0x1000; i += 0x1000) |
| 89 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 90 | set_frame(i); |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 91 | } |
| 92 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 93 | dir[table] = 0b10000011; |
| 94 | } |
| 95 | |
| 96 | uint *get_or_create_table(uint *dir, uint table, bool user, bool rw) |
| 97 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 98 | // If used AND NOT 4mb page (see figure 4-4, page 115 of Intel |
| 99 | // manual volume 3) |
| 100 | if (dir[table] & 1 && dir[table] ^ 1 << 7) |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 101 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 102 | return (uint *)(size_t)PHYS_TO_VIRT((dir[table] & ~0xfff)); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 103 | } |
| 104 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 105 | uint *page_table = malloc(sizeof(uint[1024])); |
swissChili | 7be3274 | 2021-04-03 21:17:24 -0700 | [diff] [blame] | 106 | dir[table] = VIRT_TO_PHYS(page_table) | 1 | rw << 1 | user << 2; |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 107 | return page_table; |
| 108 | } |
| 109 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 110 | void unmap_all_frames(uint page_table_p) |
| 111 | { |
| 112 | uint *table = (uint *)PHYS_TO_VIRT(page_table_p); |
| 113 | |
| 114 | for (int i = 0; i < 1024; i++) |
| 115 | { |
| 116 | if (table[i] & 1) |
| 117 | { |
| 118 | clear_frame(table[i] >> 12); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void destroy_page_table_if_exists(uint *dir, uint table) |
| 124 | { |
| 125 | // If used AND NOT 4mb page |
| 126 | if (dir[table] & 1 && dir[table] ^ 1 << 7) |
| 127 | { |
| 128 | unmap_all_frames(dir[table] >> 12); |
| 129 | free((void *)PHYS_TO_VIRT(dir[table] >> 12)); |
| 130 | } |
| 131 | } |
| 132 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 133 | void unmap_page(uint *dir, void *virt) |
| 134 | { |
| 135 | uint page = ((size_t)virt / 0x1000) % 1024; |
| 136 | uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false); |
| 137 | |
| 138 | table[page] = 0; |
| 139 | } |
| 140 | |
| 141 | void map_page_to(uint *dir, void *virt, void *frame_p, bool writable, bool user) |
| 142 | { |
| 143 | uint page = ((size_t)virt / 0x1000) % 1024; |
| 144 | uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false); |
| 145 | |
| 146 | table[page] = (((uint)frame_p) ^ 0xfff) | 1 | writable << 1 | user << 2; |
| 147 | } |
| 148 | |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 149 | void alloc_kernel_page(uint *virt) |
| 150 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 151 | alloc_page(kernel_page_directory, virt); |
| 152 | } |
| 153 | |
| 154 | void alloc_page(uint *dir, uint *virt) |
| 155 | { |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 156 | // Page number % pages per table |
| 157 | uint page = ((size_t)virt / 0x1000) % 1024; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 158 | uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false); |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 159 | kprintf(DEBUG "table = 0x%x (virt)\n", table); |
| 160 | kprintf(DEBUG "dir entry = 0x%x\n", dir[(size_t)virt >> 22]); |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 161 | |
| 162 | alloc_frame(&table[page], false, false); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 163 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 164 | kprintf(DEBUG "alloc_page table[page] = %d (0x%x)\n", table[page], table[page]); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 165 | return; |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void alloc_kernel_page_range(uint *from, uint *to) |
| 169 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 170 | uint f = (size_t)from / 0x1000, t = (size_t)to / 0x1000; |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 171 | |
| 172 | do |
| 173 | { |
| 174 | alloc_kernel_page((uint *)(size_t)t); |
| 175 | t += 0x1000; // next page |
| 176 | } while (f < t); |
| 177 | } |
| 178 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 179 | void map_page(uint *dir, size_t virt_start, bool user, bool rw) |
| 180 | { |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 181 | // Page number % pages per table |
| 182 | uint page = (virt_start / 0x1000) % 1024; |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 183 | uint table = virt_start >> 22; |
| 184 | uint frame = first_free_frame(); |
| 185 | |
| 186 | // If 4mb map OR (maps to table AND table maps page) |
| 187 | if ((dir[table] & 1 && dir[table] & 1 << 7) || |
| 188 | (dir[table] >> 12 && ((uint *)(size_t)dir[table])[page] & 1)) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | set_frame(frame); |
| 194 | uint *t = get_or_create_table(dir, table, user, rw); |
| 195 | alloc_frame(t + page, user, rw); |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 196 | } |
| 197 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 198 | /* paging stuff */ |
| 199 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 200 | uint *new_page_directory_v() |
| 201 | { |
| 202 | // Only call this AFTER allocator + paging are initialized! |
| 203 | uint *dir = malloc(1024 * 4); |
| 204 | map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false, |
| 205 | false); |
| 206 | |
| 207 | return dir; |
| 208 | } |
| 209 | |
| 210 | void free_page_directory_v(uint *dir_v) |
| 211 | { |
| 212 | for (int i = 0; i < 1024; i++) |
| 213 | { |
| 214 | destroy_page_table_if_exists(dir_v, i); |
| 215 | } |
| 216 | |
| 217 | free(dir_v); |
| 218 | } |
| 219 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 220 | void init_paging() |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 221 | { |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 222 | memset(kernel_page_directory, 0, 1024 * 4); |
| 223 | map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false, |
| 224 | false); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 225 | |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 226 | load_page_directory((uint)kernel_page_directory - 0xC0000000); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 227 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 228 | add_interrupt_handler(14, page_fault); |
| 229 | } |
| 230 | |
| 231 | void page_fault(struct registers *regs) |
| 232 | { |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 233 | kprintf(ERROR "Page fault! eip = %d\n", regs->eip); |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 234 | kpanic("Page fault"); |
| 235 | } |