-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathmem.h
More file actions
550 lines (462 loc) · 12.6 KB
/
mem.h
File metadata and controls
550 lines (462 loc) · 12.6 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright © 2019-2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <cstdint>
#include <vector>
#include <map>
#include <unordered_map>
#include <cstdint>
#include <unordered_set>
#include <stdexcept>
#include "VX_config.h"
#ifdef VM_ENABLE
#include <unordered_set>
#include <stdexcept>
#include <cassert>
#endif
namespace vortex {
#ifdef VM_ENABLE
// VA MODE
#define BARE 0x0
#define SV32 0x1
#define SV39 0x8
enum ACCESS_TYPE {
LOAD,
STORE,
FETCH
};
class SATP_t
{
private:
uint64_t address;
uint16_t asid;
uint8_t mode;
uint64_t ppn;
uint64_t satp;
uint64_t bits(uint64_t input, uint8_t s_idx, uint8_t e_idx)
{
return (input>> s_idx) & (((uint64_t)1 << (e_idx - s_idx + 1)) - 1);
}
bool bit(uint64_t input , uint8_t idx)
{
return (input ) & ((uint64_t)1 << idx);
}
public:
SATP_t(uint64_t satp) : satp(satp)
{
#ifdef XLEN_32
mode = bit(satp, 31);
asid = bits(satp, 22, 30);
ppn = bits(satp, 0,21);
#else
mode = bits(satp, 60,63);
asid = bits(satp, 44, 59);
ppn = bits(satp, 0,43);
#endif
address = ppn << MEM_PAGE_LOG2_SIZE;
}
SATP_t(uint64_t address, uint16_t asid) : address(address), asid(asid)
{
#ifdef XLEN_32
assert((address >> 32) == 0 && "Upper 32 bits are not zero!");
#endif
mode= VM_ADDR_MODE;
// asid = 0 ;
ppn = address >> MEM_PAGE_LOG2_SIZE;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
#ifdef XLEN_32
satp = (((uint64_t)mode << 31) | ((uint64_t)asid << 22) | ppn);
#else
satp = (((uint64_t)mode << 60) | ((uint64_t)asid << 44) | ppn);
#endif
#pragma GCC diagnostic pop
}
uint8_t get_mode()
{
return mode;
}
uint16_t get_asid()
{
return asid;
}
uint64_t get_base_ppn()
{
return ppn;
}
uint64_t get_satp()
{
return satp;
}
};
class Page_Fault_Exception : public std::runtime_error /* or logic_error */
{
public:
Page_Fault_Exception(const std::string& what = "") : std::runtime_error(what) {}
uint64_t addr;
ACCESS_TYPE type;
};
#endif
class BadAddress : public std::runtime_error {
public:
BadAddress() : std::runtime_error("invalid memory address") {}
};
class OutOfRange : public std::runtime_error {
public:
OutOfRange() : std::runtime_error("out of range memory address") {}
};
class MemDevice {
public:
virtual ~MemDevice() {}
virtual uint64_t size() const = 0;
virtual void read(void* data, uint64_t addr, uint64_t size) = 0;
virtual void write(const void* data, uint64_t addr, uint64_t size) = 0;
};
///////////////////////////////////////////////////////////////////////////////
class RamMemDevice : public MemDevice {
public:
RamMemDevice(uint64_t size, uint32_t wordSize);
RamMemDevice(const char* filename, uint32_t wordSize);
~RamMemDevice() {}
void read(void* data, uint64_t addr, uint64_t size) override;
void write(const void* data, uint64_t addr, uint64_t size) override;
virtual uint64_t size() const {
return contents_.size();
};
protected:
std::vector<uint8_t> contents_;
uint32_t wordSize_;
};
///////////////////////////////////////////////////////////////////////////////
class RomMemDevice : public RamMemDevice {
public:
RomMemDevice(const char *filename, uint32_t wordSize)
: RamMemDevice(filename, wordSize)
{}
RomMemDevice(uint64_t size, uint32_t wordSize)
: RamMemDevice(size, wordSize)
{}
~RomMemDevice();
void write(const void* data, uint64_t addr, uint64_t size) override;
};
///////////////////////////////////////////////////////////////////////////////
class MemoryUnit {
public:
// HW: Expand PageFault struct to contain access_type info for debug purposes
struct PageFault {
PageFault(uint64_t a, bool nf)
: faultAddr(a)
, notFound(nf)
// , access_type(ACCESS_TYPE::LOAD)
{}
uint64_t faultAddr;
bool notFound;
// ACCESS_TYPE access_type;
};
#ifdef VM_ENABLE
MemoryUnit(uint64_t pageSize = MEM_PAGE_SIZE);
~MemoryUnit(){
if ( this->satp_ != NULL)
delete this->satp_;
};
#else
MemoryUnit(uint64_t pageSize = 0);
#endif
void attach(MemDevice &m, uint64_t start, uint64_t end);
#ifdef VM_ENABLE
void read(void* data, uint64_t addr, uint32_t size, ACCESS_TYPE type = ACCESS_TYPE::LOAD);
void write(const void* data, uint64_t addr, uint32_t size, ACCESS_TYPE type = ACCESS_TYPE::STORE);
#else
void read(void* data, uint64_t addr, uint32_t size, bool sup);
void write(const void* data, uint64_t addr, uint32_t size, bool sup);
#endif
void amo_reserve(uint64_t addr);
bool amo_check(uint64_t addr);
#ifdef VM_ENABLE
void tlbAdd(uint64_t virt, uint64_t phys, uint32_t flags, uint64_t size_bits);
uint8_t is_satp_unset();
uint64_t get_satp();
uint8_t get_mode();
uint64_t get_base_ppn();
void set_satp(uint64_t satp);
#else
void tlbAdd(uint64_t virt, uint64_t phys, uint32_t flags);
#endif
void tlbRm(uint64_t vaddr);
void tlbFlush() {
tlb_.clear();
}
private:
struct amo_reservation_t {
uint64_t addr;
bool valid;
};
class ADecoder {
public:
ADecoder() {}
void read(void* data, uint64_t addr, uint64_t size);
void write(const void* data, uint64_t addr, uint64_t size);
void map(uint64_t start, uint64_t end, MemDevice &md);
private:
struct mem_accessor_t {
MemDevice* md;
uint64_t addr;
};
struct entry_t {
MemDevice* md;
uint64_t start;
uint64_t end;
};
bool lookup(uint64_t addr, uint32_t wordSize, mem_accessor_t*);
std::vector<entry_t> entries_;
};
struct TLBEntry {
TLBEntry() {}
#ifdef VM_ENABLE
TLBEntry(uint32_t pfn, uint32_t flags, uint64_t size_bits)
: pfn(pfn)
, flags(flags)
, mru_bit(true)
, size_bits (size_bits)
{
d = bit(7);
a = bit(6);
g = bit(5);
u = bit(4);
x = bit(3);
w = bit(2);
r = bit(1);
v = bit(0);
}
bool bit(uint8_t idx)
{
return (flags) & (1 << idx);
}
uint32_t pfn;
uint32_t flags;
bool mru_bit;
uint64_t size_bits;
bool d, a, g, u, x, w, r, v;
#else
TLBEntry(uint32_t pfn, uint32_t flags)
: pfn(pfn)
, flags(flags)
{}
uint32_t pfn;
uint32_t flags;
#endif
};
#ifdef VM_ENABLE
std::pair<bool, uint64_t> tlbLookup(uint64_t vAddr, ACCESS_TYPE type, uint64_t* size_bits);
bool need_trans(uint64_t dev_pAddr);
uint64_t vAddr_to_pAddr(uint64_t vAddr, ACCESS_TYPE type);
uint64_t get_pte_address(uint64_t base_ppn, uint64_t vpn);
std::pair<uint64_t, uint8_t> page_table_walk(uint64_t vAddr_bits, ACCESS_TYPE type, uint64_t* size_bits);
#else
uint64_t toPhyAddr(uint64_t vAddr, uint32_t flagMask);
TLBEntry tlbLookup(uint64_t vAddr, uint32_t flagMask);
#endif
std::unordered_map<uint64_t, TLBEntry> tlb_;
uint64_t pageSize_;
ADecoder decoder_;
#ifndef VM_ENABLE
bool enableVM_;
#endif
amo_reservation_t amo_reservation_;
#ifdef VM_ENABLE
std::unordered_set<uint64_t> unique_translations;
uint64_t TLB_HIT, TLB_MISS, TLB_EVICT, PTW, PERF_UNIQUE_PTW;
SATP_t *satp_;
#endif
};
///////////////////////////////////////////////////////////////////////////////
class ACLManager {
public:
void set(uint64_t addr, uint64_t size, int flags);
bool check(uint64_t addr, uint64_t size, int flags) const;
private:
struct acl_entry_t {
uint64_t end;
int32_t flags;
};
std::map<uint64_t, acl_entry_t> acl_map_;
};
///////////////////////////////////////////////////////////////////////////////
class RAM : public MemDevice {
public:
RAM(uint64_t capacity, uint32_t page_size);
RAM(uint64_t capacity) : RAM(capacity, capacity) {}
~RAM();
void clear();
uint64_t size() const override;
void read(void* data, uint64_t addr, uint64_t size) override;
void write(const void* data, uint64_t addr, uint64_t size) override;
void copy (uint64_t dest_addr, uint64_t src_addr, uint64_t size);
void loadBinImage(const char* filename, uint64_t destination);
void loadHexImage(const char* filename);
uint8_t& operator[](uint64_t address) {
return *this->get(address);
}
const uint8_t& operator[](uint64_t address) const {
return *this->get(address);
}
void set_acl(uint64_t addr, uint64_t size, int flags);
void enable_acl(bool enable) {
check_acl_ = enable;
}
private:
uint8_t *get(uint64_t address) const;
uint64_t capacity_;
uint32_t page_bits_;
mutable std::unordered_map<uint64_t, uint8_t*> pages_;
mutable uint8_t* last_page_;
mutable uint64_t last_page_index_;
ACLManager acl_mngr_;
bool check_acl_;
};
#ifdef VM_ENABLE
class PTE_t
{
private:
uint64_t address;
uint64_t bits(uint64_t input, uint8_t s_idx, uint8_t e_idx)
{
return (input>> s_idx) & (((uint64_t)1 << (e_idx - s_idx + 1)) - 1);
}
bool bit(uint64_t input, uint8_t idx)
{
return (input) & ((uint64_t)1 << idx);
}
public:
#if VM_ADDR_MODE == SV39
bool N;
uint8_t PBMT;
#endif
uint64_t ppn;
uint32_t rsw;
uint32_t flags;
uint8_t level;
bool d, a, g, u, x, w, r, v;
uint64_t pte_bytes;
void set_flags (uint32_t flag)
{
this->flags = flag;
d = bit(flags,7);
a = bit(flags,6);
g = bit(flags,5);
u = bit(flags,4);
x = bit(flags,3);
w = bit(flags,2);
r = bit(flags,1);
v = bit(flags,0);
}
PTE_t(uint64_t address, uint32_t flags) : address(address)
{
#if VM_ADDR_MODE == SV39
N = 0;
PBMT = 0;
level = 3;
ppn = address >> MEM_PAGE_LOG2_SIZE;
// Reserve for Super page support
// ppn = new uint32_t [level];
// ppn[2]=bits(address,28,53);
// ppn[1]=bits(address,19,27);
// ppn[0]=bits(address,10,18);
set_flags(flags);
// pte_bytes = (N << 63) | (PBMT << 61) | (ppn <<10) | flags ;
pte_bytes = (ppn <<10) | flags ;
#else // if VM_ADDR_MODE == SV32
assert((address>> 32) == 0 && "Upper 32 bits are not zero!");
level = 2;
ppn = address >> MEM_PAGE_LOG2_SIZE;
// Reserve for Super page support
// ppn = new uint32_t[level];
// ppn[1]=bits(address,20,31);
// ppn[0]=bits(address,10,19);
set_flags(flags);
pte_bytes = ppn <<10 | flags ;
#endif
}
PTE_t(uint64_t pte_bytes) : pte_bytes(pte_bytes)
{
#if VM_ADDR_MODE == SV39
N = bit(pte_bytes,63);
PBMT = bits(pte_bytes,61,62);
level = 3;
ppn=bits(pte_bytes,10,53);
address = ppn << MEM_PAGE_LOG2_SIZE;
// Reserve for Super page support
// ppn = new uint32_t [level];
// ppn[2]=bits(pte_bytes,28,53);
// ppn[1]=bits(pte_bytes,19,27);
// ppn[0]=bits(pte_bytes,10,18);
#else //#if VM_ADDR_MODE == SV32
assert((pte_bytes >> 32) == 0 && "Upper 32 bits are not zero!");
level = 2;
ppn=bits(pte_bytes,10, 31);
address = ppn << MEM_PAGE_LOG2_SIZE;
// Reserve for Super page support
// ppn = new uint32_t[level];
// ppn[1]=bits(address, 20,31);
// ppn[0]=bits(address, 10,19);
#endif
rsw = bits(pte_bytes,8,9);
set_flags((uint32_t)(bits(pte_bytes,0,7)));
}
~PTE_t()
{
// Reserve for Super page support
// delete ppn;
}
};
class vAddr_t
{
private:
uint64_t address;
uint64_t bits(uint8_t s_idx, uint8_t e_idx)
{
return (address>> s_idx) & (((uint64_t)1 << (e_idx - s_idx + 1)) - 1);
}
bool bit( uint8_t idx)
{
return (address) & ((uint64_t)1 << idx);
}
public:
uint64_t *vpn;
uint64_t pgoff;
uint8_t level;
vAddr_t(uint64_t address) : address(address)
{
#if VM_ADDR_MODE == SV39
level = 3;
vpn = new uint64_t [level];
vpn[2] = bits(30,38);
vpn[1] = bits(21,29);
vpn[0] = bits(12,20);
pgoff = bits(0,11);
#else //#if VM_ADDR_MODE == SV32
assert((address>> 32) == 0 && "Upper 32 bits are not zero!");
level = 2;
vpn = new uint64_t [level];
vpn[1] = bits(22,31);
vpn[0] = bits(12,21);
pgoff = bits(0,11);
#endif
}
~vAddr_t()
{
delete vpn;
}
};
#endif
} // namespace vortex