-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos.c
More file actions
68 lines (51 loc) · 1.53 KB
/
os.c
File metadata and controls
68 lines (51 loc) · 1.53 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
#define _GNU_SOURCE
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <sys/mman.h>
#include "os.h"
/* 2^20 pages ought to be enough for anybody */
#define NPAGES (1024*1024)
static char* pages[NPAGES];
uint64_t alloc_page_frame(void)
{
static uint64_t nalloc;
uint64_t ppn;
void* va;
if (nalloc == NPAGES)
errx(1, "out of physical memory");
/* OS memory management isn't really this simple */
ppn = nalloc;
nalloc++;
va = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (va == MAP_FAILED)
err(1, "mmap failed");
pages[ppn] = va;
return ppn + 0xbaaaaaad;
}
void* phys_to_virt(uint64_t phys_addr)
{
uint64_t ppn = (phys_addr >> 12) - 0xbaaaaaad;
uint64_t off = phys_addr & 0xfff;
char* va = NULL;
if (ppn < NPAGES)
va = pages[ppn] + off;
return va;
}
int main(int argc, char **argv)
{
uint64_t pt = alloc_page_frame();
assert(page_table_query(pt, 0xcafecafeeee) == NO_MAPPING);
assert(page_table_query(pt, 0xfffecafeeee) == NO_MAPPING);
assert(page_table_query(pt, 0xcafecafeeff) == NO_MAPPING);
page_table_update(pt, 0xcafecafeeee, 0xf00d);
assert(page_table_query(pt, 0xcafecafeeee) == 0xf00d);
assert(page_table_query(pt, 0xfffecafeeee) == NO_MAPPING);
assert(page_table_query(pt, 0xcafecafeeff) == NO_MAPPING);
page_table_update(pt, 0xcafecafeeee, NO_MAPPING);
assert(page_table_query(pt, 0xcafecafeeee) == NO_MAPPING);
assert(page_table_query(pt, 0xfffecafeeee) == NO_MAPPING);
assert(page_table_query(pt, 0xcafecafeeff) == NO_MAPPING);
return 0;
}