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