blob: 4d0aa94d722755380c525f8b16c6ff09e9666cf5 [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
swissChili825d46b2021-02-21 10:14:16 -08008/* frames bitset, 0 = free, 1 = used */
swissChilie4f01992021-02-25 15:38:12 -08009static uint frames[0xffffffff / 0x1000 / 32];
swissChili825d46b2021-02-21 10:14:16 -080010static ulong num_frames;
11
12static uint first_page_table[1024] __attribute__((aligned(4096)));
swissChilie9289ee2021-03-20 21:54:28 -070013uint kernel_page_directory[1024] __attribute__((aligned(4096)));
swissChili825d46b2021-02-21 10:14:16 -080014
15/* frame utils */
16
17#define BITS 32
18
19static void set_frame(size_t frame_addr)
20{
21 uint frame = frame_addr / 0x1000; // page aligned
22 frames[frame / BITS] |= 1 << (frame % BITS);
23}
24
25static bool test_frame(size_t frame_addr)
26{
27 uint frame = frame_addr / 0x1000; // page aligned
28 return frames[frame / BITS] & 1 << (frame % BITS);
29}
30
31static void clear_frame(size_t frame_addr)
32{
33 uint frame = frame_addr / 0x1000; // page aligned
34 frames[frame / BITS] &= ~(1 << (frame % BITS));
35}
36
37static uint first_free_frame()
38{
39 for (int i = 0; i < num_frames / BITS; i++)
40 {
41 /*
42 * If there are any zeroes, ~ will yield a non-zero result,
43 * meaning that there are pages free. Otherwise, check next set
44 */
45 if (!~frames[i])
46 continue;
47
48 for (int j = 0; j < BITS; j++)
49 {
50 if ((frames[i] & 1 << j) == 0)
51 {
52 /* found unused frame */
53 return i * BITS + j;
54 }
55 }
56 }
57
58 /* did not find a free frame, panic */
59 kpanic("first_free_frame failed! no free frames");
60}
61
swissChilie4f01992021-02-25 15:38:12 -080062void alloc_frame(uint *page_table_entry, bool user, bool writable)
swissChili825d46b2021-02-21 10:14:16 -080063{
swissChilie4f01992021-02-25 15:38:12 -080064 if (*page_table_entry >> 12)
swissChili825d46b2021-02-21 10:14:16 -080065 return; /* frame already allocated */
66
67 uint frame = first_free_frame();
68 // kprintf("first_free_frame found %d\n", frame);
69 set_frame(frame * 0x1000); /* mark as mapped */
swissChilie4f01992021-02-25 15:38:12 -080070 *page_table_entry = frame | 1 | writable << 1 | user << 2;
swissChili825d46b2021-02-21 10:14:16 -080071}
72
73void free_frame(uint page)
74{
75 clear_frame(page / 0x1000);
76}
77
swissChilidc25b2b2021-02-23 17:07:13 -080078void map_4mb(uint *dir, size_t virt_start, size_t phys_start, bool user,
79 bool rw)
swissChilie0a79bb2021-02-22 19:54:48 -080080{
81 uint page = virt_start / 0x1000;
82 uint table = virt_start >> 22;
83
84 for (uint i = 0; i < 1024 * 0x1000; i += 0x1000)
85 {
86 set_frame(page + i);
87 }
88
swissChilidc25b2b2021-02-23 17:07:13 -080089 dir[table] = 0b10000011;
90}
91
92uint *get_or_create_table(uint *dir, uint table, bool user, bool rw)
93{
94 if (dir[table] >> 12)
95 {
96 return (uint *)(size_t)(dir[table] ^ 0xfff);
97 }
98
99 uint *page_table = kmalloc_a(sizeof(uint[1024]));
100 dir[table] = (uint)page_table | 1 | rw << 1 | user << 2;
101 return page_table;
102}
103
swissChilie9289ee2021-03-20 21:54:28 -0700104void unmap_page(uint *dir, void *virt)
105{
106 uint page = ((size_t)virt / 0x1000) % 1024;
107 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
108
109 table[page] = 0;
110}
111
112void map_page_to(uint *dir, void *virt, void *frame_p, bool writable, bool user)
113{
114 uint page = ((size_t)virt / 0x1000) % 1024;
115 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
116
117 table[page] = (((uint)frame_p) ^ 0xfff) | 1 | writable << 1 | user << 2;
118}
119
swissChilie4f01992021-02-25 15:38:12 -0800120void alloc_kernel_page(uint *virt)
121{
swissChilie9289ee2021-03-20 21:54:28 -0700122 alloc_page(kernel_page_directory, virt);
123}
124
125void alloc_page(uint *dir, uint *virt)
126{
swissChilie4f01992021-02-25 15:38:12 -0800127 // Page number % pages per table
128 uint page = ((size_t)virt / 0x1000) % 1024;
swissChilie9289ee2021-03-20 21:54:28 -0700129 uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
swissChilie4f01992021-02-25 15:38:12 -0800130
131 alloc_frame(&table[page], false, false);
132}
133
134void alloc_kernel_page_range(uint *from, uint *to)
135{
swissChilie9289ee2021-03-20 21:54:28 -0700136 uint f = (size_t)from / 0x1000, t = (size_t)to / 0x1000;
swissChilie4f01992021-02-25 15:38:12 -0800137
138 do
139 {
140 alloc_kernel_page((uint *)(size_t)t);
141 t += 0x1000; // next page
142 } while (f < t);
143}
144
swissChilidc25b2b2021-02-23 17:07:13 -0800145void map_page(uint *dir, size_t virt_start, bool user, bool rw)
146{
swissChilie4f01992021-02-25 15:38:12 -0800147 // Page number % pages per table
148 uint page = (virt_start / 0x1000) % 1024;
swissChilidc25b2b2021-02-23 17:07:13 -0800149 uint table = virt_start >> 22;
150 uint frame = first_free_frame();
151
152 // If 4mb map OR (maps to table AND table maps page)
153 if ((dir[table] & 1 && dir[table] & 1 << 7) ||
154 (dir[table] >> 12 && ((uint *)(size_t)dir[table])[page] & 1))
155 {
156 return;
157 }
158
159 set_frame(frame);
160 uint *t = get_or_create_table(dir, table, user, rw);
161 alloc_frame(t + page, user, rw);
swissChilie0a79bb2021-02-22 19:54:48 -0800162}
163
swissChili825d46b2021-02-21 10:14:16 -0800164/* paging stuff */
165
swissChilie0a79bb2021-02-22 19:54:48 -0800166void init_paging()
swissChili825d46b2021-02-21 10:14:16 -0800167{
swissChilie4f01992021-02-25 15:38:12 -0800168 memset(kernel_page_directory, 0, 1024 * 4);
169 map_4mb(kernel_page_directory, (size_t)KERNEL_VIRTUAL_BASE, 0, false,
170 false);
swissChili825d46b2021-02-21 10:14:16 -0800171
swissChilie4f01992021-02-25 15:38:12 -0800172 load_page_directory((uint)kernel_page_directory - 0xC0000000);
swissChili825d46b2021-02-21 10:14:16 -0800173 add_interrupt_handler(14, page_fault);
174}
175
176void page_fault(struct registers *regs)
177{
178 kprintf("Page fault! eip = %d\n", regs->eip);
179 kpanic("Page fault");
180}