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