-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.c
More file actions
496 lines (390 loc) · 11 KB
/
Copy pathvm.c
File metadata and controls
496 lines (390 loc) · 11 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "param.h"
#include "types.h"
#include "defs.h"
#include "arm.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "spinlock.h"
#include "elf.h"
extern char data[]; // defined by kernel.ld
pde_t *kpgdir; // for use in scheduler()
// Xv6 can only allocate memory in 4KB blocks. This is fine
// for x86. ARM's page table and page directory (for 28-bit
// user address) have a size of 1KB. kpt_alloc/free is used
// as a wrapper to support allocating page tables during boot
// (use the initial kernel map, and during runtime, use buddy
// memory allocator.
struct run {
struct run *next;
};
struct {
struct spinlock lock;
struct run *freelist;
} kpt_mem;
void init_vmm (void)
{
initlock(&kpt_mem.lock, "vm");
kpt_mem.freelist = NULL;
}
static void _kpt_free (char *v)
{
struct run *r;
r = (struct run*) v;
r->next = kpt_mem.freelist;
kpt_mem.freelist = r;
}
static void kpt_free (char *v)
{
if (v >= (char*)P2V(INIT_KERNMAP)) {
kfree(v, PT_ORDER);
return;
}
acquire(&kpt_mem.lock);
_kpt_free (v);
release(&kpt_mem.lock);
}
// add some memory used for page tables (initialization code)
void kpt_freerange (uint32 low, uint32 hi)
{
while (low < hi) {
_kpt_free ((char*)low);
low += PT_SZ;
}
}
void* kpt_alloc (void)
{
struct run *r;
acquire(&kpt_mem.lock);
if ((r = kpt_mem.freelist) != NULL ) {
kpt_mem.freelist = r->next;
}
release(&kpt_mem.lock);
// Allocate a PT page if no inital pages is available
if ((r == NULL) && ((r = kmalloc (PT_ORDER)) == NULL)) {
panic("oom: kpt_alloc");
}
memset(r, 0, PT_SZ);
return (char*) r;
}
// Return the address of the PTE in page directory that corresponds to
// virtual address va. If alloc!=0, create any required page table pages.
static pte_t* walkpgdir (pde_t *pgdir, const void *va, int alloc)
{
pde_t *pde;
pte_t *pgtab;
// pgdir points to the page directory, get the page direcotry entry (pde)
pde = &pgdir[PDE_IDX(va)];
if (*pde & PE_TYPES) {
pgtab = (pte_t*) p2v(PT_ADDR(*pde));
} else {
if (!alloc || (pgtab = (pte_t*) kpt_alloc()) == 0) {
return 0;
}
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PT_SZ);
// The permissions here are overly generous, but they can
// be further restricted by the permissions in the page table
// entries, if necessary.
*pde = v2p(pgtab) | UPDE_TYPE;
}
return &pgtab[PTE_IDX(va)];
}
// Create PTEs for virtual addresses starting at va that refer to
// physical addresses starting at pa. va and size might not
// be page-aligned.
static int mappages (pde_t *pgdir, void *va, uint size, uint pa, int ap)
{
char *a, *last;
pte_t *pte;
a = (char*) align_dn(va, PTE_SZ);
last = (char*) align_dn((uint)va + size - 1, PTE_SZ);
for (;;) {
if ((pte = walkpgdir(pgdir, a, 1)) == 0) {
return -1;
}
if (*pte & PE_TYPES) {
panic("remap");
}
*pte = pa | ((ap & 0x3) << 4) | PE_CACHE | PE_BUF | PTE_TYPE;
if (a == last) {
break;
}
a += PTE_SZ;
pa += PTE_SZ;
}
return 0;
}
// flush all TLB
static void flush_tlb (void)
{
uint val = 0;
asm("MCR p15, 0, %[r], c8, c7, 0" : :[r]"r" (val):);
// invalid entire data and instruction cache
asm ("MCR p15,0,%[r],c7,c10,0": :[r]"r" (val):);
asm ("MCR p15,0,%[r],c7,c11,0": :[r]"r" (val):);
}
// Switch to the user page table (TTBR0)
void switchuvm (struct proc *p)
{
uint val;
pushcli();
if (p->pgdir == 0) {
panic("switchuvm: no pgdir");
}
val = (uint) V2P(p->pgdir) | 0x00;
asm("MCR p15, 0, %[v], c2, c0, 0": :[v]"r" (val):);
flush_tlb();
popcli();
}
// Load the initcode into address 0 of pgdir. sz must be less than a page.
void inituvm (pde_t *pgdir, char *init, uint sz)
{
char *mem;
if (sz >= PTE_SZ) {
panic("inituvm: more than a page");
}
mem = alloc_page();
memset(mem, 0, PTE_SZ);
mappages(pgdir, 0, PTE_SZ, v2p(mem), AP_KU);
memmove(mem, init, sz);
}
// Load a program segment into pgdir. addr must be page-aligned
// and the pages from addr to addr+sz must already be mapped.
int loaduvm (pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
{
uint i, pa, n;
pte_t *pte;
if ((uint) addr % PTE_SZ != 0) {
panic("loaduvm: addr must be page aligned");
}
for (i = 0; i < sz; i += PTE_SZ) {
if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) {
panic("loaduvm: address should exist");
}
pa = PTE_ADDR(*pte);
if (sz - i < PTE_SZ) {
n = sz - i;
} else {
n = PTE_SZ;
}
if (readi(ip, p2v(pa), offset + i, n) != n) {
return -1;
}
}
return 0;
}
int handle_page_fault (pde_t *pgdir, uint va, uint sz){
char *mem;
uint aligned_va = align_dn(va, PTE_SZ);
pte_t *pte;
//check if aligned_va is within the process's address space
if(aligned_va >= sz){
return -1;
}
// Check if page is already mapped
pte = walkpgdir(pgdir, (void*)aligned_va, 0);
if(pte != 0 && (*pte & PE_TYPES)) {
return 0; // Already mapped, nothing to do
}
if((mem = alloc_page()) == 0) {
return -1;
}
memset(mem, 0, PTE_SZ);
if(mappages(pgdir, (char *)aligned_va, PTE_SZ, v2p(mem), AP_KU) < 0) {
free_page(mem); // Don't leak memory
return -1;
}
flush_tlb();
return 0;
}
// Allocate page tables and physical memory to grow process from oldsz to
// newsz, which need not be page aligned. Returns new size or 0 on error.
int allocuvm (pde_t *pgdir, uint oldsz, uint newsz)
{
char *mem;
uint a;
if (newsz >= UADDR_SZ) {
return 0;
}
if (newsz < oldsz) {
return oldsz;
}
a = align_up(oldsz, PTE_SZ);
for (; a < newsz; a += PTE_SZ) {
mem = alloc_page();
if (mem == 0) {
cprintf("allocuvm out of memory\n");
deallocuvm(pgdir, newsz, oldsz);
return 0;
}
memset(mem, 0, PTE_SZ);
if((mappages(pgdir, (char*) a, PTE_SZ, v2p(mem), AP_KU)) < 0) {
return -1;
}
}
return newsz;
}
// for demand paging, we do not allocate physical memory here
// we just return the new size to the caller
int allocuvm_demand(pde_t *pgdir, uint oldsz, uint newsz)
{
if (newsz >= UADDR_SZ) {
return 0;
}
if (newsz < oldsz) {
return oldsz;
}
return newsz;
}
// Deallocate user pages to bring the process size from oldsz to
// newsz. oldsz and newsz need not be page-aligned, nor does newsz
// need to be less than oldsz. oldsz can be larger than the actual
// process size. Returns the new process size.
int deallocuvm (pde_t *pgdir, uint oldsz, uint newsz)
{
pte_t *pte;
uint a;
uint pa;
if (newsz >= oldsz) {
return oldsz;
}
for (a = align_up(newsz, PTE_SZ); a < oldsz; a += PTE_SZ) {
pte = walkpgdir(pgdir, (char*) a, 0);
if (!pte) {
// pte == 0 --> no page table for this entry
// round it up to the next page directory
a = align_up (a, PDE_SZ);
} else if ((*pte & PE_TYPES) != 0) {
pa = PTE_ADDR(*pte);
if (pa == 0) {
panic("deallocuvm");
}
free_page(p2v(pa));
*pte = 0;
}
}
return newsz;
}
// Free a page table and all the physical memory pages
// in the user part.
void freevm (pde_t *pgdir)
{
uint i;
char *v;
if (pgdir == 0) {
panic("freevm: no pgdir");
}
// release the user space memroy, but not page tables
deallocuvm(pgdir, UADDR_SZ, 0);
// release the page tables
for (i = 0; i < NUM_UPDE; i++) {
if (pgdir[i] & PE_TYPES) {
v = p2v(PT_ADDR(pgdir[i]));
kpt_free(v);
}
}
kpt_free((char*) pgdir);
}
// Clear PTE_U on a page. Used to create an inaccessible page beneath
// the user stack (to trap stack underflow).
void clearpteu (pde_t *pgdir, char *uva)
{
pte_t *pte;
pte = walkpgdir(pgdir, uva, 0);
if (pte == 0) {
panic("clearpteu");
}
// in ARM, we change the AP field (ap & 0x3) << 4)
*pte = (*pte & ~(0x03 << 4)) | AP_KO << 4;
}
// Given a parent process's page table, create a copy
// of it for a child.
pde_t* copyuvm (pde_t *pgdir, uint sz)
{
pde_t *d;
pte_t *pte;
uint pa, i, ap;
char *mem;
// allocate a new first level page directory
d = kpt_alloc();
if (d == NULL ) {
return NULL ;
}
// copy the whole address space over (no COW)
for (i = 0; i < sz; i += PTE_SZ) {
if ((pte = walkpgdir(pgdir, (void *) i, 0)) == 0) {
// No PTE exists - this is a demand-allocated page that hasn't been accessed yet
// Skip it - the child will create the page on its own page fault
continue;
}
if (!(*pte & PE_TYPES)) {
// PTE exists but page not present - this should not happen
panic("copyuvm: page not present");
}
pa = PTE_ADDR (*pte);
ap = PTE_AP (*pte);
if ((mem = alloc_page()) == 0) {
goto bad;
}
memmove(mem, (char*) p2v(pa), PTE_SZ);
if (mappages(d, (void*) i, PTE_SZ, v2p(mem), ap) < 0) {
goto bad;
}
}
return d;
bad: freevm(d);
return 0;
}
//PAGEBREAK!
// Map user virtual address to kernel address.
char* uva2ka (pde_t *pgdir, char *uva)
{
pte_t *pte;
pte = walkpgdir(pgdir, uva, 0);
// make sure it exists
if ((*pte & PE_TYPES) == 0) {
return 0;
}
// make sure it is a user page
if (PTE_AP(*pte) != AP_KU) {
return 0;
}
return (char*) p2v(PTE_ADDR(*pte));
}
// Copy len bytes from p to user address va in page table pgdir.
// Most useful when pgdir is not the current page table.
// uva2ka ensures this only works for user pages.
int copyout (pde_t *pgdir, uint va, void *p, uint len)
{
char *buf, *pa0;
uint n, va0;
buf = (char*) p;
while (len > 0) {
va0 = align_dn(va, PTE_SZ);
pa0 = uva2ka(pgdir, (char*) va0);
if (pa0 == 0) {
return -1;
}
n = PTE_SZ - (va - va0);
if (n > len) {
n = len;
}
memmove(pa0 + (va - va0), buf, n);
len -= n;
buf += n;
va = va0 + PTE_SZ;
}
return 0;
}
// 1:1 map the memory [phy_low, phy_hi] in kernel. We need to
// use 2-level mapping for this block of memory. The rumor has
// it that ARMv6's small brain cannot handle the case that memory
// be mapped in both 1-level page table and 2-level page. For
// initial kernel, we use 1MB mapping, other memory needs to be
// mapped as 4KB pages
void paging_init (uint phy_low, uint phy_hi)
{
mappages (P2V(&_kernel_pgtbl), P2V(phy_low), phy_hi - phy_low, phy_low, AP_KU);
flush_tlb ();
}