This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RvMem.cpp
76 lines (67 loc) · 1.88 KB
/
RvMem.cpp
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
#include "RvMem.h"
RvMem::RvMem()
{
return;
}
uint32_t RvMem::fetch(uint64_t addr)
{
if (addr & 1)
throw RvMisAlign(addr);
try {
auto &entry = page_table.at(addr >> 12);
if (!(entry.perm & P_EXEC))
throw RvAccVio(addr);
return *reinterpret_cast<uint32_t *>(reinterpret_cast<char *>(entry.addr) + (addr & 0xfff));
}
catch (std::out_of_range) {
throw RvAccVio(addr);
}
}
bool RvMem::new_page(uint64_t addr_hint, int perm) {
if (page_table.find(addr_hint >> 12) != page_table.end())
return false;
void *buf = ::malloc(1 << 12);
if (!buf)
return false;
owned_page.insert(buf);
return map_page(addr_hint, perm, buf);
}
bool RvMem::map_page(uint64_t addr_hint, int perm, void *phy_addr) {
if (page_table.find(addr_hint >> 12) != page_table.end())
return false;
page_table.insert({ addr_hint >> 12, {phy_addr, perm} });
return true;
}
bool RvMem::delete_page(uint64_t addr) {
if (page_table.find(addr >> 12) == page_table.end())
return false;
if (owned_page.find(page_table[addr >> 12].addr) == owned_page.end())
return false;
::free(page_table[addr >> 12].addr);
owned_page.erase(page_table[addr >> 12].addr);
page_table.erase(addr >> 12);
return true;
}
bool RvMem::unmap_page(uint64_t addr) {
if (page_table.find(addr >> 12) == page_table.end())
return false;
page_table.erase(addr >> 12);
return true;
}
RvMem::MemWrapper RvMem::operator[](uint64_t addr)
{
if (page_table.find(addr >> 12) == page_table.end())
throw RvAccVio(0);
auto &entry{ page_table[addr >> 12] };
return MemWrapper(reinterpret_cast<char *>(entry.addr) + (addr & 0xfff), entry.perm);
}
// Return last memory access time
uint64_t RvMem::mem_cycle()
{
return 1;
}
RvMem::~RvMem()
{
for (void *i : owned_page)
::free(i);
}