-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvm.c
More file actions
204 lines (157 loc) · 3.79 KB
/
vm.c
File metadata and controls
204 lines (157 loc) · 3.79 KB
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
#include "u.h"
#include "arm64.h"
#include "defs.h"
#include "mem.h"
#include "proc.h"
static Pte *kpgmap;
static Pte *emptymap; // used to unmap all userspace addresses when we're in the scheduler.
// paging structure entry -> physical address
uintptr
pte_addr(Pte entry)
{
return entry & 0x0000FFFFFFFFF000;
}
int
pte_flags(Pte entry)
{
return entry & 0xFFFF000000000FFF;
}
int
uvmperm(void)
{
return PTE_AF | PTE_ISH | PTE_CACHEABLE | PTE_W | PTE_U;
}
static Pte *
pgmapget(Pte *table, int offset, int alloc)
{
Pte *innertab;
Pte *entry = &table[offset];
if (*entry & PTE_P) {
innertab = (u64 *)p2v(pte_addr(*entry));
} else {
if (!alloc || (innertab = kalloc()) == nil)
return nil;
memzero(innertab, PGSIZE);
*entry = v2p(innertab) | PTE_TABLE | PTE_P;
}
return innertab;
}
// number of bits to shift in order to get the offset for the nth table entry.
// 0th entry isn't used.
static int shifts[] = { 0, 12, 21, 30, 39 };
static int
idx(void *va, int level)
{
uintptr a = (uintptr)va;
return (a >> shifts[level]) & 0x1FF;
}
Pte *
walkpgmap(Pte *pgmap, void *va, int alloc)
{
Pte *pgtab = pgmap;
int i;
for (i = 4; i > 1; i--) {
pgtab = pgmapget(pgtab, idx(va, i), alloc);
if (pgtab == nil)
return nil;
}
return &pgtab[idx(va, 1)];
}
int
mappages(Pte *pgmap, void *va, usize size, uintptr pa, int perm)
{
char *a, *last;
Pte *pte;
checkalign((void *)pa, PGSIZE, "mappages - physical addr not page aligned: pa = 0x%p", pa);
a = pgfloor(va);
last = pgfloor((void *)((uintptr)va + size - 1));
for (;;) {
pte = walkpgmap(pgmap, a, 1);
if (pte == nil)
return -1;
if (*pte & PTE_P)
panic("remap, va: 0x%x, pa: 0x%x", va, pa);
*pte = pa | perm | PTE_PAGE | PTE_P;
if (a == last)
break;
a += PGSIZE;
pa += PGSIZE;
}
return 0;
}
typedef struct Kmap Kmap;
static struct Kmap {
void *addr;
uintptr phys_start;
uintptr phys_end;
int perm;
} kmap[] = {
{(void *)KERNBASE, 0, V2P(data), PTE_AF | PTE_ISH | PTE_CACHEABLE | PTE_RO}, // kernel text and read only data
{(void *)data, V2P(data), PHYSTOP, PTE_AF | PTE_ISH | PTE_CACHEABLE}, // kernel data + physical pages
{(void *)(KERNBASE+DEVSPACE), DEVSPACE, DEVTOP, PTE_AF | PTE_ISH | PTE_DEVICE_nGnRnE}, // SoC peripherals
{(void *)(KERNBASE+LSPACE), LSPACE, LTOP, PTE_AF | PTE_ISH | PTE_DEVICE_nGnRnE} // Local peripherals
};
Pte *
setupkvm()
{
Pte *pgmap = kalloc();
Kmap *k;
if (pgmap == nil)
return nil;
memzero(pgmap, PGSIZE);
for (k = kmap; k < &kmap[nelem(kmap)]; k++) {
if (mappages(pgmap, k->addr, k->phys_end-k->phys_start, k->phys_start, k->perm) < 0)
return nil;
}
return pgmap;
}
void
switchkvm(void)
{
lttbr1(v2p(kpgmap));
lttbr0(v2p(emptymap));
dsb(); // Make sure page-table updates are done
tlbi();
dsb(); // make sure tlb invalidation is done
isb();
}
void
switchuvm(Proc *p)
{
push_off();
lttbr1(v2p(kpgmap));
lttbr0(v2p(p->pgmap));
dsb();
tlbi();
dsb();
isb();
pop_off();
}
Pte *
allocpgmap(void)
{
Pte *pgmap = kalloc();
if (pgmap == nil)
return nil;
memzero(pgmap, PGSIZE);
return pgmap;
}
void
kvmalloc(void)
{
if ((kpgmap = setupkvm()) == nil) {
panic("kvmalloc - kpgmap");
}
if ((emptymap = allocpgmap()) == nil) {
panic("kvmalloc - emptymap");
}
switchkvm();
}
void
clearpteu(Pte *pgmap, void *addr)
{
Pte *pte = walkpgmap(pgmap, addr, 0);
if (pte == nil)
panic("clearpteu");
*pte &= ~PTE_U;
}