1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#include <lib/lib.h>
#include <memory/alloc.h>
#include <memory/paging.h>
#include <stdint.h>
#include <system.h>
u32 kernel_page_directory[PAGE_COUNT] __attribute__((aligned(PAGE_ALIGN)));
u32 kernel_page_tables[PAGE_COUNT][PAGE_COUNT] __attribute__((aligned(PAGE_ALIGN)));
u32 *temp_dir[PAGE_COUNT] __attribute__((aligned(PAGE_ALIGN)));
u32 **page_directory __attribute__((aligned(PAGE_ALIGN)));
void paging_init(u32 **dir)
{
//log("%d", paging_get_used_pages());
for (u32 i = 0; i < PAGE_COUNT; i++) {
dir[i] = kernel_page_tables[i];
for (u32 j = 0; j < PAGE_COUNT; j++) {
dir[i][j] = kernel_page_tables[i][j];
}
}
}
void paging_kernel_init()
{
for (u32 i = 0; i < PAGE_COUNT; i++)
for (u32 j = 0; j < PAGE_COUNT; j++)
kernel_page_tables[i][j] =
((j * PAGE_ALIGN) + (i * PAGE_SIZE)) | PT_RW | PT_USER;
for (u32 i = 0; i < PAGE_COUNT; i++)
kernel_page_directory[i] =
((u32)kernel_page_tables[i]) | PD_RW | PD_PRESENT | PD_USER;
page_directory = temp_dir;
for (u32 i = 0; i < PAGE_COUNT; i++) {
page_directory[i] = kernel_page_tables[i];
for (u32 j = 0; j < PAGE_COUNT; j++) {
page_directory[i][j] = kernel_page_tables[i][j];
}
}
}
void paging_install()
{
paging_switch_directory((u32 **)kernel_page_directory);
paging_kernel_init();
if (!memory_init())
paging_set_present(0, memory_get_all() >> 3);
paging_set_used(0, ((u32)&kernel_end >> 12) + 1);
paging_enable();
log("Installed paging");
// Test!
u32 *a = malloc(PAGE_ALIGN);
u32 *b = malloc(PAGE_ALIGN);
free(b);
free(a);
u32 *c = malloc(2048);
assert(a == c);
c[42] = 0x4242;
assert(c[42] == 0x4242);
free(c);
info("Malloc test succeeded!");
}
u32 **paging_make_directory()
{
/* u32 **dir = valloc(PAGE_COUNT * sizeof(*dir)); */
/* dir[0] = malloc(PAGE_COUNT * PAGE_COUNT * sizeof(**dir)); */
/* for (u32 i = 1; i < PAGE_COUNT; i++) */
/* dir[i] = dir[0] + i * PAGE_COUNT; */
u32 **dir = valloc(PAGE_COUNT * sizeof(*dir));
for (u32 i = 0; i < PAGE_COUNT; i++)
dir[i] = malloc(PAGE_COUNT + sizeof(**dir));
for (u32 i = 0; i < PAGE_COUNT; i++) {
dir[i] = kernel_page_directory[i];
/* for (u32 j = 0; j < PAGE_COUNT; j++) { */
/* dir[i][j] = (&kernel_page_directory[i])[j]; */
/* } */
}
paging_switch_directory(dir);
log("%d", 42);
return dir;
}
void paging_remove_directory(u32 **dir)
{
// TODO: Fix freeing of directory by calculating aligned offset
free(dir[0]);
free(dir);
}
void paging_disable()
{
u32 cr0;
asm("mov %%cr0, %0" : "=r"(cr0));
cr0 &= 0x7fffffff;
asm("mov %0, %%cr0" ::"r"(cr0));
paging_enabled = 0;
}
void paging_enable()
{
u32 cr0;
asm("mov %%cr0, %0" : "=r"(cr0));
cr0 |= 0x80000000;
asm("mov %0, %%cr0" ::"r"(cr0));
paging_enabled = 1;
}
void paging_switch_directory(u32 **dir)
{
page_directory = dir;
asm("mov %0, %%cr3" ::"r"(dir));
}
void invlpg(u32 addr)
{
asm("invlpg (%0)" ::"r"(addr) : "memory");
}
void paging_map(u32 phy, u32 virt, u16 flags)
{
u32 pdi = virt >> 22;
u32 pti = virt >> 12 & 0x03FF;
page_directory[pdi][pti] = phy | flags;
invlpg(virt);
}
void paging_map_user(u32 phy, u32 virt)
{
u32 pdi = virt >> 22;
/* u32 pti = virt >> 12 & 0x03FF; */
for (int i = 0; i < 1024; i++) {
page_directory[pdi][i] = phy | PT_RW | PT_PRESENT | PT_USER;
phy += 4096;
}
invlpg(virt);
}
u32 paging_get_phys(u32 virt)
{
u32 pdi = virt >> 22;
u32 pti = (virt >> 12) & 0x03FF;
return page_directory[pdi][pti] & 0xFFFFF000;
}
u16 paging_get_flags(u32 virt)
{
u32 pdi = virt >> 22;
u32 pti = (virt >> 12) & 0x03FF;
return page_directory[pdi][pti] & 0xFFF;
}
void paging_set_flag_up(u32 virt, u32 count, u32 flag)
{
//debug("Setting flag %b for %d tables", flag, count);
u32 page_n = virt / PAGE_ALIGN;
for (u32 i = page_n; i < page_n + count; i++) {
page_directory[i / PAGE_COUNT][i % PAGE_COUNT] |= flag;
invlpg(i * PAGE_ALIGN);
}
}
void paging_set_flag_down(u32 virt, u32 count, u32 flag)
{
u32 page_n = virt / PAGE_ALIGN;
for (u32 i = page_n; i < page_n + count; i++) {
page_directory[i / PAGE_COUNT][i % PAGE_COUNT] &= ~flag;
invlpg(i * PAGE_ALIGN);
}
}
void paging_set_present(u32 virt, u32 count)
{
paging_set_flag_up(virt, count, PT_PRESENT);
}
void paging_set_absent(u32 virt, u32 count)
{
paging_set_flag_down(virt, count, PT_PRESENT);
}
void paging_set_used(u32 virt, u32 count)
{
paging_set_flag_up(virt, count, PT_USED);
}
void paging_set_free(u32 virt, u32 count)
{
paging_set_flag_down(virt, count, PT_USED);
}
u32 paging_find_pages(u32 count)
{
u32 continuous = 0;
u32 start_dir = 0;
u32 start_page = 0;
for (u32 i = 0; i < PAGE_COUNT; i++) {
for (u32 j = 0; j < PAGE_COUNT; j++) {
if (!(page_directory[i][j] & PT_PRESENT) ||
(page_directory[i][j] & PT_USED)) {
continuous = 0;
start_dir = i;
start_page = j + 1;
} else {
if (++continuous == count)
return (start_dir * PAGE_SIZE) + (start_page * PAGE_ALIGN);
}
}
}
panic("Out of memory!");
return 0;
}
u32 paging_alloc_pages(u32 count)
{
u32 ptr = paging_find_pages(count);
paging_set_used(ptr, count);
return ptr;
}
u32 paging_get_used_pages()
{
u32 n = 0;
for (u32 i = 0; i < PAGE_COUNT; i++)
for (u32 j = 0; j < PAGE_COUNT; j++)
if (page_directory[i][j] & PT_USED)
n++;
return n;
}
|