-
Notifications
You must be signed in to change notification settings - Fork 0
/
page32.c
316 lines (269 loc) · 10.8 KB
/
page32.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*****************************************************************************/
/* File: page32.c */
/* */
/* Description: Source file for 32 bit page table handling code. */
/* */
/* Author: Shoily O Rahman <shoily@gmail.com> */
/* */
/* Date: July 9, 2020 */
/* */
/*****************************************************************************/
#include "page32.h"
#include "system.h"
#include "lock.h"
#include "memory.h"
#include "debug.h"
extern addr_t _kernel_pg_table_0;
extern spinlock spinlock_smp;
extern int smp_bits;
extern int smp_on_hold;
extern pgd_t _master_kernel_pg_dir;
// locks for each GDT entry in kernel space shared among all the CPUs.
spinlock lock_pgd_kernel[(PAGE_SIZE-KERNEL_PGDIR_OFFSET)/sizeof(pgd_t)];
// locks in userspace. One lock per CPU for all GDT entries .
spinlock lock_pgd_user[MAX_NUM_SMPS];
#define LOCK_PGD(cpuid, addr, flags) { \
if (!((flags) & (MAP_USER | MAP_LOCAL_CPU)) && \
(addr) >= (addr_t)KERNEL_VIRT_ADDR) { \
spinlock_lock(&lock_pgd_kernel[((((addr)-KERNEL_VIRT_ADDR) >> PGD_SHIFT) & 0x3ff)]); \
} else { \
spinlock_lock(&lock_pgd_user[(cpuid)]); \
} \
}
#define UNLOCK_PGD(cpuid, addr, flags) { \
if (!((flags) & (MAP_USER | MAP_LOCAL_CPU)) && \
(addr) >= (addr_t)KERNEL_VIRT_ADDR) { \
spinlock_unlock(&lock_pgd_kernel[((((addr)-KERNEL_VIRT_ADDR) >> PGD_SHIFT) & 0x3ff)]); \
} else { \
spinlock_unlock(&lock_pgd_user[(cpuid)]); \
} \
}
void build_pagetable(u32 cpuid, pte_t **pgtable, addr_t phys_addr, addr_t start, u32 length, u32 pgd_flags, u32 pte_flags, u32 map_flags) {
int idx = 0;
bool locked = map_flags & MAP_PGD_LOCKED;
pgd_t *pgdir = (map_flags & (MAP_USER | MAP_LOCAL_CPU)) ? GET_CPU_PGDIR(cpuid) : &_master_kernel_pg_dir;
pgd_t *pgd = pgdir + ((start >> PGD_SHIFT) & 0x3ff);
pte_t *pte = pgtable[idx] + ((start >> PAGE_SHIFT) & 0x3ff);
pte_t *last_pte = (pte_t*)(((long)pte + PAGE_SIZE) & PAGE_MASK);
addr_t end = start + length;
if (!locked)
LOCK_PGD(cpuid, start, map_flags);
if (!*pgd) {
*pgd = ((long)pgtable[idx] - KERNEL_VIRT_ADDR) | pgd_flags;
} else if (((*pgd & PGD_FLAG_MASK) ^ pgd_flags) != 0) {
*pgd |= pgd_flags;
}
if (!locked)
UNLOCK_PGD(cpuid, start, map_flags);
while (start < end) {
if (pte == last_pte) {
idx++;
pte = pgtable[idx];
last_pte = (pte_t*)(((long)pte + PAGE_SIZE) & PAGE_MASK);
pgd++;
if (!locked)
LOCK_PGD(cpuid, start, map_flags);
if (!*pgd) {
*pgd = (((long)pgtable[idx] - KERNEL_VIRT_ADDR) | pgd_flags);
} else if (((*pgd & PGD_FLAG_MASK) ^ pgd_flags) != 0) {
*pgd |= pgd_flags;
}
if (!locked)
UNLOCK_PGD(cpuid, start, map_flags);
}
*pte = (phys_addr | pte_flags);
pte++;
start += PAGE_SIZE;
phys_addr += PAGE_SIZE;
}
}
void unmap_pagetable(u32 cpuid, pte_t **pgtable, addr_t start, u32 length, u32 map_flags) {
int idx = 0;
bool locked = map_flags & MAP_PGD_LOCKED;
pgd_t *pgdir = (map_flags & (MAP_USER | MAP_LOCAL_CPU)) ? GET_CPU_PGDIR(cpuid) : &_master_kernel_pg_dir;
pgd_t *pgd = pgdir + ((start >> PGD_SHIFT) & 0x3ff);
pte_t *pte = pgtable[idx] + ((start >> PAGE_SHIFT) & 0x3ff);
pte_t *last_pte = (pte_t*)(((long)pte + PAGE_SIZE) & PAGE_MASK);
addr_t end = start + length;
if (!locked)
LOCK_PGD(cpuid, start, map_flags);
if (!*pgd) {
start += ((last_pte-pte) << PAGE_SHIFT);
pte = last_pte;
}
if (!locked)
UNLOCK_PGD(cpuid, start, map_flags);
while (start < end) {
if (pte == last_pte) {
idx++;
pte = pgtable[idx];
last_pte = (pte_t*)(((long)pte + PAGE_SIZE) & PAGE_MASK);
pgd++;
if (!locked)
LOCK_PGD(cpuid, start + PAGE_SIZE, map_flags);
if (!*pgd) {
pte = last_pte;
if (!locked)
UNLOCK_PGD(cpuid, start + PAGE_SIZE, map_flags);
start += PGD_SIZE;
continue;
}
if (!locked)
UNLOCK_PGD(cpuid, start + PAGE_SIZE, map_flags);
}
*pte = 0;
pte++;
start += PAGE_SIZE;
}
}
void map_kernel_with_pagetable(addr_t virt_addr, addr_t phys_addr, u32 length, u32 pte_flags, u32 map_flags) {
pte_t *pgtbl[1];
pte_t *pte;
addr_t end_virt_addr;
if ((virt_addr < (addr_t)KERNEL_VIRT_ADDR) || (map_flags & MAP_USER))
return;
end_virt_addr = ALIGN_PAGE(virt_addr + length);
virt_addr = virt_addr & PAGE_MASK;
pte = (pte_t*)ADDPTRS(&_kernel_pg_table_0, (((VIRT_TO_PHYS_ADDR_LINEAR(virt_addr) >> PGD_SHIFT) & 0x3ff) << PAGE_SHIFT));
pte += (((long)(virt_addr) >> PAGE_SHIFT) & 0x3ff);
while (virt_addr != end_virt_addr) {
pgtbl[0] = (pte_t*)((long)pte & PAGE_MASK);
build_pagetable(CUR_CPU,
pgtbl,
phys_addr,
virt_addr,
PAGE_SIZE,
PGD_PRESENT | PGD_WRITE,
pte_flags | PTE_PRESENT,
map_flags);
virt_addr = ADDPTRS(virt_addr, PAGE_SIZE);
phys_addr = ADDPTRS(phys_addr, PAGE_SIZE);
pte++;
}
}
void unmap_kernel_with_pagetable(addr_t virt_addr, u32 length, u32 map_flags) {
pte_t *pgtbl[1];
pte_t *pte;
addr_t end_virt_addr;
if ((virt_addr < (addr_t)KERNEL_VIRT_ADDR) || (map_flags & MAP_USER))
return;
end_virt_addr = ALIGN_PAGE(virt_addr + length);
virt_addr = virt_addr & PAGE_MASK;
pte = (pte_t*)ADDPTRS(&_kernel_pg_table_0, (((VIRT_TO_PHYS_ADDR_LINEAR(virt_addr) >> PGD_SHIFT) & 0x3ff) << PAGE_SHIFT));
pte += (((long)(virt_addr) >> PAGE_SHIFT) & 0x3ff);
while (virt_addr != end_virt_addr) {
pgtbl[0] = (pte_t*)((long)pte & PAGE_MASK);
unmap_pagetable(CUR_CPU, pgtbl, virt_addr, PAGE_SIZE, map_flags);
virt_addr = ADDPTRS(virt_addr, PAGE_SIZE);
pte++;
}
}
int alloc_user_page(addr_t virt_addr, u32 length, u32 pte_flags) {
u32 pgtbl_idx;
addr_t addr, phys_addr;
struct kpage *page_buf;
struct kpage *page_pgtbl = NULL;
pte_t *pgtbl[1];
pgd_t *pgd;
u32 nr_pgtbls, prev_nr_pgtbls;
u32 pgd_len;
pgd_t *pgdir = GET_CPU_PGDIR(CUR_CPU);
if (!virt_addr)
return ERR_NOT_SUPPORTED;
if (virt_addr & (PAGE_SIZE-1))
return ERR_INVALID;
length = ALIGN_PAGE(length);
if ((virt_addr + length) >= (addr_t)KERNEL_VIRT_ADDR)
return ERR_INVALID;
page_buf = page_alloc(NPAGES_TO_ORDER(length >> PAGE_SHIFT));
if (!page_buf)
return ERR_NOMEM;
prev_nr_pgtbls = 0;
retry_pgtbl_read:
nr_pgtbls = 0;
addr = virt_addr;
pgd_len = ALIGN_PGD(length);
LOCK_PGD(CUR_CPU, 0, MAP_USER | MAP_LOCAL_CPU);
while (pgd_len) {
pgd = pgdir + ((addr >> PGD_SHIFT) & 0x3ff);
if (!*pgd)
nr_pgtbls++;
pgd_len -= PGD_SIZE;
addr = ADDPTRS(addr, PGD_SIZE);
}
if (prev_nr_pgtbls != nr_pgtbls) {
UNLOCK_PGD(CUR_CPU, addr, MAP_USER | MAP_LOCAL_CPU);
if (page_pgtbl) {
page_add_to_slot(page_pgtbl, NULL);
page_pgtbl = NULL;
}
if (nr_pgtbls) {
page_pgtbl = page_alloc(NPAGES_TO_ORDER(nr_pgtbls));
if (!page_pgtbl) {
UNLOCK_PGD(CUR_CPU, addr, MAP_USER | MAP_LOCAL_CPU);
page_add_to_slot(page_buf, NULL);
return ERR_NOMEM;
}
}
prev_nr_pgtbls = nr_pgtbls;
goto retry_pgtbl_read;
}
phys_addr = (addr_t)KPAGE_TO_PHYS_ADDR(page_buf);
pgtbl_idx = 0;
while (length) {
pgd = pgdir + ((virt_addr >> PGD_SHIFT) & 0x3ff);
if (*pgd) {
pgtbl[0] = (pte_t*)ADDPTRS((*pgd & PAGE_MASK), KERNEL_VIRT_ADDR);
} else {
pgtbl[0] = (pte_t*)ADDPTRS(ADDPTRS(KPAGE_TO_PHYS_ADDR(page_pgtbl), pgtbl_idx << PAGE_SHIFT), KERNEL_VIRT_ADDR);
pgtbl_idx++;
}
map_kernel_linear_with_pagetable((addr_t)pgtbl[0],
PAGE_SIZE,
PTE_WRITE,
MAP_LOCAL_CPU | MAP_PGD_LOCKED);
build_pagetable(CUR_CPU,
pgtbl,
phys_addr,
virt_addr,
PAGE_SIZE,
PGD_PRESENT | PGD_USER | ((pte_flags & PTE_WRITE) ? PGD_WRITE : 0),
PTE_PRESENT | PTE_USER | pte_flags,
MAP_USER | MAP_PGD_LOCKED);
unmap_kernel_with_pagetable((addr_t)pgtbl[0],
PAGE_SIZE,
MAP_LOCAL_CPU | MAP_PGD_LOCKED);
virt_addr = ADDPTRS(virt_addr, PAGE_SIZE);
phys_addr = ADDPTRS(phys_addr, PAGE_SIZE);
length -= PAGE_SIZE;
}
UNLOCK_PGD(CUR_CPU, 0, MAP_USER | MAP_LOCAL_CPU);
return 0;
}
pfn_t page_getpfn(void *addr) {
pgd_t *pgd = GET_CURCPU_PGDIR + (((long)(addr) >> PGD_SHIFT) & 0x3ff);
pte_t *pte = ((pte_t*)ADDPTRS(*pgd, KERNEL_VIRT_ADDR)) + (((long)(addr) >> PAGE_SHIFT) & 0x3ff);
return (*pte & PAGE_MASK);
}
void pgd_lock_init() {
u32 i;
for(i = 0; i < (sizeof(lock_pgd_kernel)/sizeof(spinlock)); i++)
INIT_SPIN_LOCK(&lock_pgd_kernel[i]);
for(i = 0; i < MAX_NUM_SMPS; i++)
INIT_SPIN_LOCK(&lock_pgd_user[i]);
}
void pgd_kernel_lock_all() {
u32 i;
for(i=0;i<sizeof(lock_pgd_kernel)/sizeof(spinlock);i++)
spinlock_lock(&lock_pgd_kernel[i]);
}
void pgd_kernel_unlock_all() {
u32 i;
for(i=0;i<sizeof(lock_pgd_kernel)/sizeof(spinlock);i++)
spinlock_unlock(&lock_pgd_kernel[i]);
}
void pagetable_delete_entry(u32 pfn, u32 pages) {
for (u32 i = 0; i < pages; i++, pfn++) {
//clear_tlb(pfn);
}
}