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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
// See LICENSE for license details.
#include "mmap.h"
#include "atomic.h"
#include "pk.h"
#include "boot.h"
#include "bits.h"
#include "mtrap.h"
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
uintptr_t kva2pa_offset;
typedef struct vmr_t {
struct vmr_t* next;
uintptr_t addr;
size_t length;
file_t* file;
size_t offset;
unsigned refcnt;
int prot;
} vmr_t;
static vmr_t* vmr_freelist_head;
static pte_t* root_page_table;
#define RISCV_PGLEVELS ((VA_BITS - RISCV_PGSHIFT) / RISCV_PGLEVEL_BITS)
static spinlock_t vm_lock = SPINLOCK_INIT;
static uintptr_t first_free_page;
static size_t next_free_page;
static size_t free_pages;
static size_t pages_promised;
int demand_paging = 1; // unless -p flag is given
uint64_t randomize_mapping; // set by --randomize-mapping
typedef struct freelist_node_t {
uintptr_t addr;
} freelist_node_t;
size_t page_freelist_depth;
static freelist_node_t* page_freelist_storage;
static uintptr_t free_page_addr(size_t idx)
{
return first_free_page + idx * RISCV_PGSIZE;
}
static uintptr_t __early_pgalloc_align(size_t num_pages, size_t align)
{
size_t skip_pages = (align - 1) & -(free_page_addr(next_free_page) / RISCV_PGSIZE);
num_pages += skip_pages;
if (num_pages + next_free_page < num_pages || num_pages + next_free_page > free_pages)
return 0;
uintptr_t addr = free_page_addr(next_free_page + skip_pages);
next_free_page += num_pages;
return addr;
}
static uintptr_t __early_alloc(size_t size)
{
size_t num_pages = ROUNDUP(size, RISCV_PGSIZE) / RISCV_PGSIZE;
return __early_pgalloc_align(num_pages, 1);
}
static void __maybe_fuzz_page_freelist();
static void __page_freelist_insert(freelist_node_t node)
{
__maybe_fuzz_page_freelist();
page_freelist_storage[page_freelist_depth++] = node;
}
static freelist_node_t __page_freelist_remove()
{
__maybe_fuzz_page_freelist();
return page_freelist_storage[--page_freelist_depth];
}
static bool __augment_page_freelist()
{
uintptr_t page = __early_alloc(RISCV_PGSIZE);
if (page != 0) {
freelist_node_t node = { .addr = page };
__page_freelist_insert(node);
}
return page;
}
static void __maybe_fuzz_page_freelist()
{
if (randomize_mapping) {
randomize_mapping = lfsr63(randomize_mapping);
if (randomize_mapping % 2 == 0 && page_freelist_depth) {
size_t swap_idx = randomize_mapping % page_freelist_depth;
freelist_node_t tmp = page_freelist_storage[swap_idx];
page_freelist_storage[swap_idx] = page_freelist_storage[page_freelist_depth-1];
page_freelist_storage[page_freelist_depth-1] = tmp;
}
if (randomize_mapping % 16 == 0)
__augment_page_freelist();
}
}
static bool __page_freelist_empty()
{
return page_freelist_depth == 0;
}
static size_t __num_free_pages()
{
return page_freelist_depth + (free_pages - next_free_page);
}
static uintptr_t __page_alloc()
{
if (__page_freelist_empty() && !__augment_page_freelist())
return 0;
freelist_node_t node = __page_freelist_remove();
memset((void*)pa2kva(node.addr), 0, RISCV_PGSIZE);
return node.addr;
}
static uintptr_t __page_alloc_assert()
{
uintptr_t res = __page_alloc();
if (!res)
panic("Out of memory!");
return res;
}
static void __page_free(uintptr_t addr)
{
freelist_node_t node = { .addr = addr };
__page_freelist_insert(node);
}
static vmr_t* __vmr_alloc(uintptr_t addr, size_t length, file_t* file,
size_t offset, unsigned refcnt, int prot)
{
if (vmr_freelist_head == NULL) {
vmr_t* new_vmrs = (vmr_t*)pa2kva(__page_alloc());
if (new_vmrs == NULL)
return NULL;
vmr_freelist_head = new_vmrs;
for (size_t i = 0; i < (RISCV_PGSIZE / sizeof(vmr_t)) - 1; i++)
new_vmrs[i].next = &new_vmrs[i+1];
}
vmr_t* v = vmr_freelist_head;
vmr_freelist_head = v->next;
pages_promised += refcnt;
if (file)
file_incref(file);
v->addr = addr;
v->length = length;
v->file = file;
v->offset = offset;
v->refcnt = refcnt;
v->prot = prot;
return v;
}
static void __vmr_decref(vmr_t* v, unsigned dec)
{
pages_promised -= dec;
if ((v->refcnt -= dec) == 0) {
if (v->file)
file_decref(v->file);
v->next = vmr_freelist_head;
vmr_freelist_head = v;
}
}
static size_t pte_ppn(pte_t pte)
{
return pte >> PTE_PPN_SHIFT;
}
static uintptr_t ppn(uintptr_t addr)
{
return addr >> RISCV_PGSHIFT;
}
static size_t pt_idx(uintptr_t addr, int level)
{
size_t idx = addr >> (RISCV_PGLEVEL_BITS*level + RISCV_PGSHIFT);
return idx & ((1 << RISCV_PGLEVEL_BITS) - 1);
}
static inline pte_t* __walk_internal(pte_t* t, uintptr_t addr, int create, int level)
{
for (int i = RISCV_PGLEVELS - 1; i > level; i--) {
size_t idx = pt_idx(addr, i);
if (unlikely(!(t[idx] & PTE_V))) {
if (create) {
uintptr_t new_ptd = __page_alloc();
if (!new_ptd)
return 0;
t[idx] = ptd_create(ppn(new_ptd));
} else {
return 0;
}
}
t = (pte_t*)pa2kva(pte_ppn(t[idx]) << RISCV_PGSHIFT);
}
return &t[pt_idx(addr, level)];
}
static pte_t* __walk(uintptr_t addr)
{
return __walk_internal(root_page_table, addr, 0, 0);
}
static pte_t* __walk_create(uintptr_t addr)
{
return __walk_internal(root_page_table, addr, 1, 0);
}
static int __va_avail(uintptr_t vaddr)
{
pte_t* pte = __walk(vaddr);
return pte == 0 || *pte == 0;
}
static uintptr_t __vm_alloc_at(uintptr_t start, uintptr_t end, size_t npage)
{
for (uintptr_t a = start; a <= end; a += RISCV_PGSIZE) {
if (!__va_avail(a))
continue;
uintptr_t first = a, last = a + (npage-1) * RISCV_PGSIZE;
for (a = last; a > first && __va_avail(a); a -= RISCV_PGSIZE)
;
if (a > first)
continue;
return a;
}
return 0;
}
static uintptr_t __vm_alloc(size_t npage)
{
uintptr_t end = current.mmap_max - npage * RISCV_PGSIZE;
if (current.vm_alloc_guess) {
uintptr_t ret = __vm_alloc_at(current.vm_alloc_guess, end, npage);
if (ret)
return ret;
}
return __vm_alloc_at(current.brk, end, npage);
}
static inline pte_t prot_to_type(int prot, int user)
{
pte_t pte = 0;
if (prot & PROT_READ) pte |= PTE_R | PTE_A;
if (prot & PROT_WRITE) pte |= PTE_W | PTE_A | PTE_D;
if (prot & PROT_EXEC) pte |= PTE_X | PTE_A;
if (pte == 0) pte = PTE_R;
if (user) pte |= PTE_U;
return pte;
}
int __valid_user_range(uintptr_t vaddr, size_t len)
{
uintptr_t last_vaddr = vaddr + len - 1;
if (last_vaddr < vaddr)
return 0;
return last_vaddr < current.mmap_max;
}
static void flush_tlb_entry(uintptr_t vaddr)
{
asm volatile ("sfence.vma %0" : : "r" (vaddr) : "memory");
}
static int __handle_page_fault(uintptr_t vaddr, int prot)
{
uintptr_t vpn = vaddr >> RISCV_PGSHIFT;
vaddr = vpn << RISCV_PGSHIFT;
pte_t* pte = __walk(vaddr);
if (pte == 0 || *pte == 0 || !__valid_user_range(vaddr, 1))
return -1;
else if (!(*pte & PTE_V))
{
uintptr_t ppn = __page_alloc_assert() / RISCV_PGSIZE;
uintptr_t kva = pa2kva(ppn * RISCV_PGSIZE);
vmr_t* v = (vmr_t*)*pte;
*pte = pte_create(ppn, prot_to_type(PROT_READ|PROT_WRITE, 0));
flush_tlb_entry(vaddr);
if (v->file)
{
size_t flen = MIN(RISCV_PGSIZE, v->length - (vaddr - v->addr));
ssize_t ret = file_pread(v->file, (void*)kva, flen, vaddr - v->addr + v->offset);
kassert(ret > 0);
if (ret < RISCV_PGSIZE)
memset((void*)vaddr + ret, 0, RISCV_PGSIZE - ret);
}
__vmr_decref(v, 1);
*pte = pte_create(ppn, prot_to_type(v->prot, 1));
flush_tlb_entry(vaddr);
}
pte_t perms = pte_create(0, prot_to_type(prot, 1));
pte_t pte_perms = *pte | ((*pte & PTE_W) ? PTE_R : 0); // loads to shadow-stack pages are permitted
if ((pte_perms & perms) != perms)
return -1;
return 0;
}
int handle_page_fault(uintptr_t vaddr, int prot)
{
spinlock_lock(&vm_lock);
int ret = __handle_page_fault(vaddr, prot);
spinlock_unlock(&vm_lock);
return ret;
}
static void __do_munmap(uintptr_t addr, size_t len)
{
for (uintptr_t a = addr; a < addr + len; a += RISCV_PGSIZE)
{
pte_t* pte = __walk(a);
if (pte == 0 || *pte == 0)
continue;
if (*pte & PTE_V)
__page_free(pte_ppn(*pte) << RISCV_PGSHIFT);
else
__vmr_decref((vmr_t*)*pte, 1);
*pte = 0;
flush_tlb_entry(a);
}
}
uintptr_t __do_mmap(uintptr_t addr, size_t length, int prot, int flags, file_t* f, off_t offset)
{
size_t npage = (length-1)/RISCV_PGSIZE+1;
if (npage * 17 / 16 + 16 + pages_promised >= __num_free_pages())
return (uintptr_t)-1;
if (flags & MAP_FIXED)
{
if ((addr & (RISCV_PGSIZE-1)) || !__valid_user_range(addr, length))
return (uintptr_t)-1;
}
else if ((addr = __vm_alloc(npage)) == 0)
return (uintptr_t)-1;
vmr_t* v = __vmr_alloc(addr, length, f, offset, npage, prot);
if (!v)
return (uintptr_t)-1;
for (uintptr_t a = addr; a < addr + length; a += RISCV_PGSIZE)
{
pte_t* pte = __walk_create(a);
kassert(pte);
if (*pte)
__do_munmap(a, RISCV_PGSIZE);
*pte = (pte_t)v;
}
if (!demand_paging || (flags & MAP_POPULATE))
for (uintptr_t a = addr; a < addr + length; a += RISCV_PGSIZE)
kassert(__handle_page_fault(a, prot) == 0);
current.vm_alloc_guess = addr + npage * RISCV_PGSIZE;
return addr;
}
int do_munmap(uintptr_t addr, size_t length)
{
if ((addr & (RISCV_PGSIZE-1)) || !__valid_user_range(addr, length))
return -EINVAL;
spinlock_lock(&vm_lock);
__do_munmap(addr, length);
spinlock_unlock(&vm_lock);
return 0;
}
uintptr_t do_mmap(uintptr_t addr, size_t length, int prot, int flags, int fd, off_t offset)
{
if (!(flags & MAP_PRIVATE) || length == 0 || (offset & (RISCV_PGSIZE-1)))
return -EINVAL;
file_t* f = NULL;
if (!(flags & MAP_ANONYMOUS) && (f = file_get(fd)) == NULL)
return -EBADF;
spinlock_lock(&vm_lock);
addr = __do_mmap(addr, length, prot, flags, f, offset);
if (addr < current.brk_max)
current.brk_max = addr;
spinlock_unlock(&vm_lock);
if (f) file_decref(f);
return addr;
}
uintptr_t __do_brk(size_t addr)
{
uintptr_t newbrk = addr;
if (addr < current.brk_min || addr > current.brk_max)
return current.brk;
uintptr_t newbrk_page = ROUNDUP(newbrk, RISCV_PGSIZE);
if (current.brk > newbrk_page) {
__do_munmap(newbrk_page, current.brk - newbrk_page);
} else if (current.brk < newbrk_page) {
if (__do_mmap(current.brk, newbrk_page - current.brk, -1, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0) != current.brk)
return current.brk;
}
current.brk = newbrk_page;
return newbrk;
}
uintptr_t do_brk(size_t addr)
{
spinlock_lock(&vm_lock);
addr = __do_brk(addr);
spinlock_unlock(&vm_lock);
return addr;
}
uintptr_t do_mremap(uintptr_t addr, size_t old_size, size_t new_size, int flags)
{
return -ENOSYS;
}
uintptr_t do_mprotect(uintptr_t addr, size_t length, int prot)
{
uintptr_t res = 0;
if ((addr) & (RISCV_PGSIZE-1))
return -EINVAL;
spinlock_lock(&vm_lock);
for (uintptr_t a = addr; a < addr + length; a += RISCV_PGSIZE)
{
pte_t* pte = __walk(a);
if (pte == 0 || *pte == 0) {
res = -ENOMEM;
break;
}
if (!(*pte & PTE_V)) {
vmr_t* v = (vmr_t*)*pte;
if((v->prot ^ prot) & ~v->prot){
//TODO:look at file to find perms
res = -EACCES;
break;
}
v->prot = prot;
} else {
if (!(*pte & PTE_U) ||
((prot & PROT_READ) && !(*pte & PTE_R)) ||
((prot & PROT_WRITE) && !(*pte & PTE_W)) ||
((prot & PROT_EXEC) && !(*pte & PTE_X))) {
//TODO:look at file to find perms
res = -EACCES;
break;
}
*pte = pte_create(pte_ppn(*pte), prot_to_type(prot, 1));
}
flush_tlb_entry(a);
}
spinlock_unlock(&vm_lock);
return res;
}
static inline void __map_kernel_page(uintptr_t vaddr, uintptr_t paddr, int level, int prot)
{
pte_t* pte = __walk_internal(root_page_table, vaddr, 1, level);
kassert(pte);
*pte = pte_create(paddr >> RISCV_PGSHIFT, prot_to_type(prot, 0));
}
static void __map_kernel_range(uintptr_t vaddr, uintptr_t paddr, size_t len, int prot)
{
size_t megapage_size = RISCV_PGSIZE << RISCV_PGLEVEL_BITS;
bool megapage_coaligned = (vaddr ^ paddr) % megapage_size == 0;
// could support misaligned mappings, but no need today
kassert((vaddr | paddr | len) % megapage_size == 0);
while (len > 0) {
__map_kernel_page(vaddr, paddr, 1, prot);
len -= megapage_size;
vaddr += megapage_size;
paddr += megapage_size;
}
}
void populate_mapping(const void* start, size_t size, int prot)
{
uintptr_t a0 = ROUNDDOWN((uintptr_t)start, RISCV_PGSIZE);
for (uintptr_t a = a0; a < (uintptr_t)start+size; a += RISCV_PGSIZE)
{
if (prot & PROT_WRITE)
atomic_add((int*)a, 0);
else
atomic_read((int*)a);
}
}
static void init_early_alloc()
{
// PA space must fit within half of VA space
uintptr_t user_size = -KVA_START;
mem_size = MIN(mem_size, user_size);
current.mmap_max = current.brk_max = user_size;
extern char _end;
volatile uintptr_t last_static_addr = (uintptr_t)&_end;
first_free_page = ROUNDUP(last_static_addr, RISCV_PGSIZE);
free_pages = (mem_size - (first_free_page - MEM_START)) / RISCV_PGSIZE;
}
uintptr_t pk_vm_init()
{
init_early_alloc();
size_t num_freelist_nodes = mem_size / RISCV_PGSIZE;
page_freelist_storage = (freelist_node_t*)__early_alloc(num_freelist_nodes * sizeof(freelist_node_t));
root_page_table = (void*)__page_alloc_assert();
__map_kernel_range(KVA_START, MEM_START, mem_size, PROT_READ|PROT_WRITE|PROT_EXEC);
flush_tlb();
write_csr(satp, ((uintptr_t)root_page_table >> RISCV_PGSHIFT) | SATP_MODE_CHOICE);
uintptr_t kernel_stack_top = __page_alloc_assert() + RISCV_PGSIZE;
// relocate
kva2pa_offset = KVA_START - MEM_START;
page_freelist_storage = (void*)pa2kva(page_freelist_storage);
root_page_table = (void*)pa2kva(root_page_table);
return kernel_stack_top;
}
|