blob: c77f49983c6f83780e7b8fa4fea6e4ffec30f70a [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);
swissChili14d0b842023-01-01 02:22:44 -050064
swissChilicfd3c3c2021-04-03 15:04:24 -070065 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{
swissChili14d0b842023-01-01 02:22:44 -0500106 // If used AND NOT 4mb page (see figure 4-4, page 114 of Intel
swissChili1e8b7562021-12-22 21:22:57 -0800107 // manual volume 3)
swissChili14d0b842023-01-01 02:22:44 -0500108 // bit 0 = used; bit 7 = 4MB?
109 if (dir[table] & 1 && dir[table] ^ (1 << 7))
swissChilidc25b2b2021-02-23 17:07:13 -0800110 {
swissChili14d0b842023-01-01 02:22:44 -0500111 // 12 LSBs are metadata
swissChili1e8b7562021-12-22 21:22:57 -0800112 return (uint *)(size_t)PHYS_TO_VIRT((dir[table] & ~0xfff));
swissChilidc25b2b2021-02-23 17:07:13 -0800113 }
114
swissChili1e8b7562021-12-22 21:22:57 -0800115 uint *page_table = malloc(sizeof(uint[1024]));
swissChili7be32742021-04-03 21:17:24 -0700116 dir[table] = VIRT_TO_PHYS(page_table) | 1 | rw << 1 | user << 2;
swissChilidc25b2b2021-02-23 17:07:13 -0800117 return page_table;
118}
119
swissChili1e8b7562021-12-22 21:22:57 -0800120void unmap_all_frames(uint page_table_p)
121{
122 uint *table = (uint *)PHYS_TO_VIRT(page_table_p);
123
124 for (int i = 0; i < 1024; i++)
125 {
126 if (table[i] & 1)
127 {
128 clear_frame(table[i] >> 12);
129 }
130 }
131}
132
133void destroy_page_table_if_exists(uint *dir, uint table)
134{
135 // If used AND NOT 4mb page
136 if (dir[table] & 1 && dir[table] ^ 1 << 7)
137 {
138 unmap_all_frames(dir[table] >> 12);
139 free((void *)PHYS_TO_VIRT(dir[table] >> 12));
140 }
141}
142
swissChilie9289ee2021-03-20 21:54:28 -0700143void unmap_page(uint *dir, void *virt)
144{
145 uint page = ((size_t)virt / 0x1000) % 1024;
146 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
147
148 table[page] = 0;
149}
150
swissChili14d0b842023-01-01 02:22:44 -0500151// TODO: This entire function seems wrong.
swissChilie9289ee2021-03-20 21:54:28 -0700152void map_page_to(uint *dir, void *virt, void *frame_p, bool writable, bool user)
153{
154 uint page = ((size_t)virt / 0x1000) % 1024;
155 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
156
swissChili14d0b842023-01-01 02:22:44 -0500157 // TODO: is the xor here correct?
swissChilie9289ee2021-03-20 21:54:28 -0700158 table[page] = (((uint)frame_p) ^ 0xfff) | 1 | writable << 1 | user << 2;
159}
160
swissChilie4f01992021-02-25 15:38:12 -0800161void alloc_kernel_page(uint *virt)
162{
swissChilie9289ee2021-03-20 21:54:28 -0700163 alloc_page(kernel_page_directory, virt);
164}
165
166void alloc_page(uint *dir, uint *virt)
167{
swissChilie4f01992021-02-25 15:38:12 -0800168 // Page number % pages per table
169 uint page = ((size_t)virt / 0x1000) % 1024;
swissChilie9289ee2021-03-20 21:54:28 -0700170 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
swissChili14d0b842023-01-01 02:22:44 -0500171 // kprintf(DEBUG "table = 0x%x (virt)\n", table);
172 // kprintf(DEBUG "dir entry = 0x%x\n", dir[(size_t)virt >> 22]);
swissChilie4f01992021-02-25 15:38:12 -0800173
174 alloc_frame(&table[page], false, false);
swissChilicfd3c3c2021-04-03 15:04:24 -0700175
swissChili14d0b842023-01-01 02:22:44 -0500176 // kprintf(DEBUG "alloc_page table[page] = %d (0x%x)\n", table[page], table[page]);
swissChilicfd3c3c2021-04-03 15:04:24 -0700177 return;
swissChilie4f01992021-02-25 15:38:12 -0800178}
179
180void alloc_kernel_page_range(uint *from, uint *to)
181{
swissChilie9289ee2021-03-20 21:54:28 -0700182 uint f = (size_t)from / 0x1000, t = (size_t)to / 0x1000;
swissChilie4f01992021-02-25 15:38:12 -0800183
184 do
185 {
186 alloc_kernel_page((uint *)(size_t)t);
187 t += 0x1000; // next page
188 } while (f < t);
189}
190
swissChilidc25b2b2021-02-23 17:07:13 -0800191void map_page(uint *dir, size_t virt_start, bool user, bool rw)
192{
swissChilie4f01992021-02-25 15:38:12 -0800193 // Page number % pages per table
194 uint page = (virt_start / 0x1000) % 1024;
swissChilidc25b2b2021-02-23 17:07:13 -0800195 uint table = virt_start >> 22;
196 uint frame = first_free_frame();
197
198 // If 4mb map OR (maps to table AND table maps page)
199 if ((dir[table] & 1 && dir[table] & 1 << 7) ||
200 (dir[table] >> 12 && ((uint *)(size_t)dir[table])[page] & 1))
201 {
202 return;
203 }
204
205 set_frame(frame);
206 uint *t = get_or_create_table(dir, table, user, rw);
207 alloc_frame(t + page, user, rw);
swissChilie0a79bb2021-02-22 19:54:48 -0800208}
209
swissChili825d46b2021-02-21 10:14:16 -0800210/* paging stuff */
211
swissChili1e8b7562021-12-22 21:22:57 -0800212uint *new_page_directory_v()
213{
214 // Only call this AFTER allocator + paging are initialized!
215 uint *dir = malloc(1024 * 4);
216 map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false,
217 false);
218
219 return dir;
220}
221
222void free_page_directory_v(uint *dir_v)
223{
224 for (int i = 0; i < 1024; i++)
225 {
226 destroy_page_table_if_exists(dir_v, i);
227 }
228
229 free(dir_v);
230}
231
swissChilie0a79bb2021-02-22 19:54:48 -0800232void init_paging()
swissChili825d46b2021-02-21 10:14:16 -0800233{
swissChilie4f01992021-02-25 15:38:12 -0800234 memset(kernel_page_directory, 0, 1024 * 4);
235 map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false,
236 false);
swissChili825d46b2021-02-21 10:14:16 -0800237
swissChili14d0b842023-01-01 02:22:44 -0500238 load_page_directory(VIRT_TO_PHYS(kernel_page_directory));
swissChili825d46b2021-02-21 10:14:16 -0800239}
240
swissChili14d0b842023-01-01 02:22:44 -0500241void test_paging()
swissChili825d46b2021-02-21 10:14:16 -0800242{
swissChili14d0b842023-01-01 02:22:44 -0500243 // a random page base address
244 uint *base = (uint *)0xFFFFFE000;
245
246 kprintf(INFO "Allocating page (expect frame 1024)\n");
247 alloc_page(kernel_page_directory, base);
248
249 kprintf(INFO "Writing 10 words to page\n");
250 for (int i = 0; i < 10; i++)
251 {
252 base[i] = i;
253 }
swissChili825d46b2021-02-21 10:14:16 -0800254}