-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpage.c
55 lines (51 loc) · 1.4 KB
/
page.c
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
#include "page.h"
extern void kbss_end;
uint64_t phys_next = (uint64_t)(&kbss_end - 0xffff800000000000ull);
uint64_t phys_alloc() {
if (phys_next & 0xfff) {
phys_next |= 0xfff;
phys_next++;
}
uint64_t res = phys_next;
phys_next += 0x1000;
return res;
}
void map_page(uint64_t virt, uint64_t phys, int flags) {
uint64_t p_ptl4 = read_cr3() & ~0xfffull;
uint64_t *v_ptl4 = P2V(p_ptl4);
int idx1 = virt >> 12 & 0x1ff;
int idx2 = virt >> 21 & 0x1ff;
int idx3 = virt >> 30 & 0x1ff;
int idx4 = virt >> 39 & 0x1ff;
if (!v_ptl4[idx4]) {
v_ptl4[idx4] = phys_alloc() | 7;
uint64_t p_ptl3 = v_ptl4[idx4] & ~0xfffull;
uint64_t *v_ptl3 = P2V(p_ptl3);
memset(v_ptl3, 0, 0x1000);
}
uint64_t p_ptl3 = v_ptl4[idx4] & ~0xfffull;
uint64_t *v_ptl3 = P2V(p_ptl3);
if (!v_ptl3[idx3]) {
v_ptl3[idx3] = phys_alloc() | 7;
uint64_t p_ptl2 = v_ptl3[idx3] & ~0xfffull;
uint64_t *v_ptl2 = P2V(p_ptl2);
memset(v_ptl2, 0, 0x1000);
}
uint64_t p_ptl2 = v_ptl3[idx3] & ~0xfffull;
uint64_t *v_ptl2 = P2V(p_ptl2);
if (!v_ptl2[idx2]) {
v_ptl2[idx2] = phys_alloc() | 7;
uint64_t p_ptl1 = v_ptl2[idx2] & ~0xfffull;
uint64_t *v_ptl1 = P2V(p_ptl1);
memset(v_ptl1, 0, 0x1000);
}
uint64_t p_ptl1 = v_ptl2[idx2] & ~0xfffull;
uint64_t *v_ptl1 = P2V(p_ptl1);
v_ptl1[idx1] = phys | flags;
}
void init_pg() {
uint64_t p_ptl4 = read_cr3() & ~0xfffull;
uint64_t *v_ptl4 = P2V(p_ptl4);
v_ptl4[0] = 0;
write_cr3(read_cr3());
}