-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos.c
More file actions
48 lines (35 loc) · 821 Bytes
/
os.c
File metadata and controls
48 lines (35 loc) · 821 Bytes
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
#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 void* 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;
}
void* phys_to_virt(uint64_t phys_addr)
{
uint64_t ppn = phys_addr >> 12;
uint64_t off = phys_addr & 0xfff;
void* va = NULL;
if (ppn < NPAGES)
va = pages[ppn] + off;
return va;
}