-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.h
255 lines (237 loc) · 7.63 KB
/
page.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
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
#include <cstdint>
#include <cstdlib>
#include <list>
#include <cstdio>
#include <iostream>
#include "SV39.h"
class Pages{
public:
//const static uint64_t PageSize = 4096;
uint8_t *data = nullptr;
Pages(uint64_t size){
//uint64_t page_num = (size + PageSize - 1) / PageSize;
data = new uint8_t[size];
}
~Pages(){
if(data != nullptr)
delete []data;
}
};
class Block{
public:
Pages* pages;
uint64_t addr;
uint64_t size;
Block(uint64_t addr, uint64_t size, bool create_pages = true):
addr(addr), size(size){
if(create_pages) {
uint64_t num = (size + SV39::PageSize - 1) / SV39::PageSize;
pages = new Pages(num * SV39::PageSize);
}
else
pages = nullptr;
}
Block(const Block &blk):
addr(blk.addr), size(blk.size){
printf("Copy Cosntructor\n");
if(blk.pages != nullptr){
pages = new Pages(blk.size);
for(uint64_t i = 0; i < size; i++){
pages->data[i] = blk.pages->data[i];
}
}
else
pages = nullptr;
}
Block(Block &&blk):
addr(blk.addr), size(blk.size), pages(blk.pages){
//printf("Move Constructor\n");
blk.pages = nullptr;
}
~Block(){
if(pages != nullptr)
delete pages;
}
bool operator<(const Block &another){
return this->end() <= another.addr;
}
bool operator>(const Block &another){
return this->addr >= another.end();
}
bool operator<(const uint64_t &pos){
uint64_t end = this->addr + this->size;
return end <= pos;
}
bool operator>(const uint64_t &pos){
return this->addr > pos;
}
bool operator==(const uint64_t &pos){
return this->addr <= pos && (this->addr + this->size) > pos;
}
uint64_t end() const{
return addr + size;
}
int write(uint64_t base, uint64_t length, const void *data){ // 0 <= base < size
if(base + length > size){
printf("Input 0x%016lx + 0x%016lx is too long for block @0x%016lx\n", base, length, addr);
return -1;
}
uint8_t *in = (uint8_t *)data;
for(uint64_t i = 0ull; i < length && base + i < size; i++){
pages->data[base + i] = in[i];
}
return 0;
}
int read(uint64_t base, uint64_t length, void *data){
if(base + length > size){
printf("Input 0x%016lx + 0x%016lx is too long for block @0x%016lx\n", base, length, addr);
return -1;
}
uint8_t *out = (uint8_t *)data;
for(uint64_t i = 0ull; i < length && base + i < size; i++){
out[i] = pages->data[base + i];
}
return 0;
}
};
class PhysicalMemory{
public:
uint64_t max_range;
std::list<Block>* blocks;
std::list<Block>::iterator current_block;
PhysicalMemory(uint64_t range){
range = (range <= SV39::MaxPhyRange) ? range : SV39::MaxPhyRange;
max_range = (range + SV39::PageSize - 1) / SV39::PageSize * SV39::PageSize;
//max_addr = max_range - 1ull;
blocks = new std::list<Block>;
blocks->push_back(Block(0, SV39::PageSize * 1, false));
current_block = blocks->begin();
blocks->push_back(Block(max_range, SV39::PageSize * 1, false));
}
~PhysicalMemory(){
delete blocks;
}
//return 0: success, -1: fail
int insertBlock(Block &&blk){
if(*current_block > blk){
while(current_block != blocks->begin()){
current_block--;
if(*current_block < blk){
blocks->emplace(std::next(current_block), std::move(blk));
return 0;
}
}
}
else if(*current_block < blk){
while(current_block != blocks->end()){
current_block++;
if(*current_block > blk){
blocks->emplace(current_block, std::move(blk));
return 0;
}
}
}
else{
printf("Block address conflict: 0x%016lx\n", blk.addr);
return -1;
}
printf("Insert failed by unknown reason\n");
return -1;
}
int removeBlock(uint64_t addr){
for(auto iter = blocks->begin(); iter != blocks->end(); iter++){
if(iter->addr <= addr && iter->addr + iter->size > addr){
blocks->erase(iter);
return 0;
}
if(iter->addr > addr)
return -1;
}
//printf("Block address not exist: 0x%016lx\n", addr);
return -1;
}
int writeData(uint64_t addr, uint64_t length, const void *data){
for(auto &iter: *blocks){
if(addr >= iter.addr &&
addr + length <= iter.addr + iter.size){ // valid address
if(iter.pages== nullptr){
printf("No pages in this block: 0x%016lx\n", addr);
return -1;
}
iter.write(addr - iter.addr, length, data);
return 0;
}
}
printf("Invalid address or too long data length: 0x%016lx\n", addr);
return -1;
}
template<class T> int writeWord(uint64_t addr, const T in){
T dat = in;
return writeData(addr, sizeof(T), &dat);
}
template<class T> int writeWords(uint64_t addr, uint64_t num, const T* in, const void *mask = nullptr){
if(mask == nullptr){
writeData(addr, num*sizeof(T), in);
}
else{
for(uint64_t i = 0; i < num; i++){
uint8_t maskbit = (((uint8_t *)mask)[i/8] >> (i%8)) & '\x01';
if(maskbit == '\x01'){
writeWord<T>(addr + i * sizeof(T), in[i]);
}
}
}
return 0;
}
int readData(uint64_t addr, uint64_t length, void *data){
for(auto &iter: *blocks){
if(addr >= iter.addr &&
addr + length <= iter.addr + iter.size){
if(iter.pages== nullptr){
printf("No pages in this block: 0x%016lx\n", addr);
return -1;
}
iter.read(addr - iter.addr, length, data);
return 0;
}
}
printf("Invalid address or too long data length: 0x%016lx\n", addr);
return -1;
}
template<class T> T readWord(uint64_t addr){
T out;
int flag = readData(addr, sizeof(T), &out);
if(!flag)
return out;
else
return 0;
}
template<class T> int readWords(uint64_t addr, uint64_t num, T *data, const void *mask = nullptr){
readData(addr, num * sizeof(T), data);
if(mask != nullptr){
for(uint64_t i = 0; i < num; i++){
uint8_t maskbit = (((uint8_t *)mask)[i/8] >> (i%8)) & '\x01';
if(maskbit == '\x00'){
data[i] = 0;
}
}
}
return 0;
}
uint64_t findUsable(uint64_t size){
// not support megapage / gigapage yet
size = (size + SV39::PageSize - 1) / SV39::PageSize * SV39::PageSize;
uint64_t usable = 0ull;
for(auto iter = blocks->begin(); std::next(iter) != blocks->end(); iter++){
usable = usable <= iter->end() ? iter->end() : usable;
if(*std::next(iter) > usable + size - 1){ // find ?
if(usable + size <= max_range){ // find !
return usable;
}else{
break; // fail
}
}
}
return 0ull;
}
};