blob: f959a9245d8c9b6555e4f7d147b16ea429b6ae8e [file] [log] [blame]
swissChili825d46b2021-02-21 10:14:16 -08001#include "paging.h"
swissChilie4f01992021-02-25 15:38:12 -08002#include "alloc.h"
swissChili825d46b2021-02-21 10:14:16 -08003#include "io.h"
4#include "kint.h"
5#include "log.h"
6#include "pic.h"
7
swissChilib58ab672022-01-17 21:18:01 -08008/**
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
swissChilicfd3c3c2021-04-03 15:04:24 -070016#define NUM_FRAMES 0xffffffff / 0x1000 / 32
swissChili825d46b2021-02-21 10:14:16 -080017/* frames bitset, 0 = free, 1 = used */
swissChilicfd3c3c2021-04-03 15:04:24 -070018static uint frames[NUM_FRAMES];
swissChili825d46b2021-02-21 10:14:16 -080019
20static uint first_page_table[1024] __attribute__((aligned(4096)));
swissChilie9289ee2021-03-20 21:54:28 -070021uint kernel_page_directory[1024] __attribute__((aligned(4096)));
swissChili825d46b2021-02-21 10:14:16 -080022
swissChili1e8b7562021-12-22 21:22:57 -080023
swissChili825d46b2021-02-21 10:14:16 -080024/* frame utils */
25
26#define BITS 32
27
28static 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
34static 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
40static 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
46static uint first_free_frame()
47{
swissChilicfd3c3c2021-04-03 15:04:24 -070048 for (int i = 0; i < NUM_FRAMES / BITS; i++)
swissChili825d46b2021-02-21 10:14:16 -080049 {
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 {
swissChilicfd3c3c2021-04-03 15:04:24 -070059 if ((frames[i] & (1 << j)) == 0)
swissChili825d46b2021-02-21 10:14:16 -080060 {
61 /* found unused frame */
swissChilicfd3c3c2021-04-03 15:04:24 -070062 uint frame = i * BITS + j;
swissChili9bd74de2021-06-15 20:30:48 -070063 kprintf(DEBUG "first_free_frame returning %d\n", frame);
swissChilicfd3c3c2021-04-03 15:04:24 -070064// kpanic("asdf");
65 return frame;
swissChili825d46b2021-02-21 10:14:16 -080066 }
67 }
68 }
69
70 /* did not find a free frame, panic */
71 kpanic("first_free_frame failed! no free frames");
72}
73
swissChilie4f01992021-02-25 15:38:12 -080074void alloc_frame(uint *page_table_entry, bool user, bool writable)
swissChili825d46b2021-02-21 10:14:16 -080075{
swissChilicfd3c3c2021-04-03 15:04:24 -070076 if (*page_table_entry & 1)
swissChili825d46b2021-02-21 10:14:16 -080077 return; /* frame already allocated */
78
79 uint frame = first_free_frame();
swissChili9bd74de2021-06-15 20:30:48 -070080 // kprintf(DEBUG "first_free_frame found %d\n", frame);
swissChili825d46b2021-02-21 10:14:16 -080081 set_frame(frame * 0x1000); /* mark as mapped */
swissChilie4f01992021-02-25 15:38:12 -080082 *page_table_entry = frame | 1 | writable << 1 | user << 2;
swissChili825d46b2021-02-21 10:14:16 -080083}
84
85void free_frame(uint page)
86{
87 clear_frame(page / 0x1000);
88}
89
swissChilidc25b2b2021-02-23 17:07:13 -080090void map_4mb(uint *dir, size_t virt_start, size_t phys_start, bool user,
91 bool rw)
swissChilie0a79bb2021-02-22 19:54:48 -080092{
93 uint page = virt_start / 0x1000;
94 uint table = virt_start >> 22;
95
96 for (uint i = 0; i < 1024 * 0x1000; i += 0x1000)
97 {
swissChilicfd3c3c2021-04-03 15:04:24 -070098 set_frame(i);
swissChilie0a79bb2021-02-22 19:54:48 -080099 }
100
swissChilidc25b2b2021-02-23 17:07:13 -0800101 dir[table] = 0b10000011;
102}
103
104uint *get_or_create_table(uint *dir, uint table, bool user, bool rw)
105{
swissChili1e8b7562021-12-22 21:22:57 -0800106 // 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)
swissChilidc25b2b2021-02-23 17:07:13 -0800109 {
swissChili1e8b7562021-12-22 21:22:57 -0800110 return (uint *)(size_t)PHYS_TO_VIRT((dir[table] & ~0xfff));
swissChilidc25b2b2021-02-23 17:07:13 -0800111 }
112
swissChili1e8b7562021-12-22 21:22:57 -0800113 uint *page_table = malloc(sizeof(uint[1024]));
swissChili7be32742021-04-03 21:17:24 -0700114 dir[table] = VIRT_TO_PHYS(page_table) | 1 | rw << 1 | user << 2;
swissChilidc25b2b2021-02-23 17:07:13 -0800115 return page_table;
116}
117
swissChili1e8b7562021-12-22 21:22:57 -0800118void 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
131void 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
swissChilie9289ee2021-03-20 21:54:28 -0700141void 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
149void 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
swissChilie4f01992021-02-25 15:38:12 -0800157void alloc_kernel_page(uint *virt)
158{
swissChilie9289ee2021-03-20 21:54:28 -0700159 alloc_page(kernel_page_directory, virt);
160}
161
162void alloc_page(uint *dir, uint *virt)
163{
swissChilie4f01992021-02-25 15:38:12 -0800164 // Page number % pages per table
165 uint page = ((size_t)virt / 0x1000) % 1024;
swissChilie9289ee2021-03-20 21:54:28 -0700166 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
swissChili9bd74de2021-06-15 20:30:48 -0700167 kprintf(DEBUG "table = 0x%x (virt)\n", table);
168 kprintf(DEBUG "dir entry = 0x%x\n", dir[(size_t)virt >> 22]);
swissChilie4f01992021-02-25 15:38:12 -0800169
170 alloc_frame(&table[page], false, false);
swissChilicfd3c3c2021-04-03 15:04:24 -0700171
swissChili9bd74de2021-06-15 20:30:48 -0700172 kprintf(DEBUG "alloc_page table[page] = %d (0x%x)\n", table[page], table[page]);
swissChilicfd3c3c2021-04-03 15:04:24 -0700173 return;
swissChilie4f01992021-02-25 15:38:12 -0800174}
175
176void alloc_kernel_page_range(uint *from, uint *to)
177{
swissChilie9289ee2021-03-20 21:54:28 -0700178 uint f = (size_t)from / 0x1000, t = (size_t)to / 0x1000;
swissChilie4f01992021-02-25 15:38:12 -0800179
180 do
181 {
182 alloc_kernel_page((uint *)(size_t)t);
183 t += 0x1000; // next page
184 } while (f < t);
185}
186
swissChilidc25b2b2021-02-23 17:07:13 -0800187void map_page(uint *dir, size_t virt_start, bool user, bool rw)
188{
swissChilie4f01992021-02-25 15:38:12 -0800189 // Page number % pages per table
190 uint page = (virt_start / 0x1000) % 1024;
swissChilidc25b2b2021-02-23 17:07:13 -0800191 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);
swissChilie0a79bb2021-02-22 19:54:48 -0800204}
205
swissChili825d46b2021-02-21 10:14:16 -0800206/* paging stuff */
207
swissChili1e8b7562021-12-22 21:22:57 -0800208uint *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
218void 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
swissChilie0a79bb2021-02-22 19:54:48 -0800228void init_paging()
swissChili825d46b2021-02-21 10:14:16 -0800229{
swissChilie4f01992021-02-25 15:38:12 -0800230 memset(kernel_page_directory, 0, 1024 * 4);
231 map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false,
232 false);
swissChili825d46b2021-02-21 10:14:16 -0800233
swissChilie4f01992021-02-25 15:38:12 -0800234 load_page_directory((uint)kernel_page_directory - 0xC0000000);
swissChili1e8b7562021-12-22 21:22:57 -0800235
swissChili825d46b2021-02-21 10:14:16 -0800236 add_interrupt_handler(14, page_fault);
237}
238
239void page_fault(struct registers *regs)
240{
swissChili9bd74de2021-06-15 20:30:48 -0700241 kprintf(ERROR "Page fault! eip = %d\n", regs->eip);
swissChili825d46b2021-02-21 10:14:16 -0800242 kpanic("Page fault");
243}