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.h
74 lines (69 loc) · 1.83 KB
/
RvMem.h
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
#pragma once
#include <cstdint>
#include <map>
#include <set>
#include <concepts>
#include "RvExcept.hpp"
class RvMem {
struct pg_entry {
void *addr;
int perm;
};
std::map<uint64_t, pg_entry> page_table;
std::set<void *> owned_page;
RvMem(const RvMem &) = delete;
RvMem(RvMem &&) = delete;
RvMem &operator=(const RvMem &) = delete;
RvMem &operator=(RvMem &&) = delete;
public:
enum {
P_READ = 1,
P_WRITE = 2,
P_EXEC = 4
};
class MemWrapper {
friend class RvMem;
void *data;
int perm;
MemWrapper(void *data, int perm)
: data{ data }
, perm{ perm }
{
return;
}
MemWrapper(const MemWrapper &) = delete;
MemWrapper(MemWrapper &&) = delete;
MemWrapper &operator=(const MemWrapper &) = delete;
MemWrapper &operator=(MemWrapper &&) = delete;
public:
template<std::integral T>
T &operator=(T data)
{
if (!(perm & P_WRITE))
throw RvAccVio(0);
return *reinterpret_cast<T *>(this->data) = data;
}
template <std::integral T>
operator T()
{
if (!(perm & P_READ))
throw RvAccVio(0);
return *reinterpret_cast<T *>(this->data);
}
};
RvMem();
uint32_t fetch(uint64_t addr);
// RvMem owns the newly allocated page
bool new_page(uint64_t addr_hint, int perm);
// RvMem doesn't own the page
bool map_page(uint64_t addr_hint, int perm, void *phy_addr);
// remove mapping and delete
bool delete_page(uint64_t addr);
// just unmap
bool unmap_page(uint64_t addr);
MemWrapper operator[](uint64_t addr);
virtual uint64_t mem_cycle();
~RvMem();
};
class RvMemWithCache : public RvMem {
};