-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathVirtMem.hpp
484 lines (355 loc) · 12.1 KB
/
VirtMem.hpp
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
// Copyright 2020 Western Digital Corporation or its affiliates.
//
// 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 <iosfwd>
#include "trapEnums.hpp"
#include "Memory.hpp"
#include "Tlb.hpp"
namespace WdRiscv
{
/// Structure to unpack the fields of a 32-bit page table entry.
struct Pte32Bits
{
unsigned valid_ : 1;
unsigned read_ : 1;
unsigned write_ : 1;
unsigned exec_ : 1;
unsigned user_ : 1;
unsigned global_ : 1;
unsigned accessed_ : 1;
unsigned dirty_ : 1;
unsigned rsw_ : 2; // Reserved for supervisor.
unsigned ppn0_ : 10; // Physical page num
unsigned ppn1_ : 12; // Physical page num
} __attribute__((packed));
/// 32-bit page table entry.
union Pte32
{
Pte32Bits bits_;
uint32_t data_ = 0;
Pte32(uint32_t word) : data_(word)
{ }
bool valid() const { return bits_.valid_; }
bool read() const { return bits_.read_; }
bool write() const { return bits_.write_; }
bool exec() const { return bits_.exec_; }
bool user() const { return bits_.user_; }
bool global() const { return bits_.global_; }
bool accessed() const { return bits_.accessed_; }
bool dirty() const { return bits_.dirty_; }
uint32_t ppn() const { return ppn0() | (ppn1() << 10); }
uint32_t ppn0() const { return bits_.ppn0_; }
uint32_t ppn1() const { return bits_.ppn1_; }
uint32_t levels() const { return 2; }
uint32_t size() const { return sizeof(data_); }
uint32_t ppn(int i) const
{
if (i == 0) return ppn0();
if (i == 1) return ppn1();
assert(0); return 0;
}
uint32_t paPpnShift(int i) const
{
if (i == 0) return 12;
if (i == 1) return 22;
assert(0); return 0;
}
};
/// Struct to unpack the fields of a page table entry for Sv39.
struct Pte39Bits
{
unsigned valid_ : 1;
unsigned read_ : 1;
unsigned write_ : 1;
unsigned exec_ : 1;
unsigned user_ : 1;
unsigned global_ : 1;
unsigned accessed_ : 1;
unsigned dirty_ : 1;
unsigned rsw_ : 2; // Reserved for supervisor.
unsigned ppn0_ : 9; // Physical page num
unsigned ppn1_ : 9; // Physical page num
unsigned ppn2_ : 26; // Physical page num
unsigned res_ : 10; // Reserved
} __attribute((packed));
/// Page table entry for Sv39
union Pte39
{
Pte39Bits bits_;
uint64_t data_ = 0;
Pte39(uint64_t data) : data_(data)
{ }
bool valid() const { return bits_.valid_; }
bool read() const { return bits_.read_; }
bool write() const { return bits_.write_; }
bool exec() const { return bits_.exec_; }
bool user() const { return bits_.user_; }
bool global() const { return bits_.global_; }
bool accessed() const { return bits_.accessed_; }
bool dirty() const { return bits_.dirty_; }
uint64_t ppn() const { return ppn0() | (ppn1() << 9) | (ppn2() << 18); }
uint64_t ppn0() const { return bits_.ppn0_; }
uint64_t ppn1() const { return bits_.ppn1_; }
uint64_t ppn2() const { return bits_.ppn2_; }
uint32_t levels() const { return 3; }
uint32_t size() const { return sizeof(data_); }
uint64_t ppn(int i) const
{
if (i == 0) { return ppn0(); }
if (i == 1) { return ppn1(); }
if (i == 2) { return ppn2(); }
assert(0);
return 0;
}
uint32_t paPpnShift(int i) const
{
if (i == 0) { return 12; }
if (i == 1) { return 21; }
if (i == 2) { return 30; }
assert(0);
return 0;
}
};
struct Pte48Bits
{
unsigned valid_ : 1;
unsigned read_ : 1;
unsigned write_ : 1;
unsigned exec_ : 1;
unsigned user_ : 1;
unsigned global_ : 1;
unsigned accessed_ : 1;
unsigned dirty_ : 1;
unsigned rsw_ : 2; // Reserved for supervisor.
unsigned ppn0_ : 9; // Physical page num
unsigned ppn1_ : 9; // Physical page num
unsigned ppn2_ : 9; // Physical page num
unsigned ppn3_ : 17; // Physical page num
unsigned res_ : 10; // Reserved
} __attribute__((packed));
/// Page table entry for Sv48
union Pte48
{
Pte48Bits bits_;
uint64_t data_ = 0;
Pte48(uint64_t data) : data_(data)
{ }
bool valid() const { return bits_.valid_; }
bool read() const { return bits_.read_; }
bool write() const { return bits_.write_; }
bool exec() const { return bits_.exec_; }
bool user() const { return bits_.user_; }
bool global() const { return bits_.global_; }
bool accessed() const { return bits_.accessed_; }
bool dirty() const { return bits_.dirty_; }
uint64_t ppn() const { return ppn0() | (ppn1() << 9) | (ppn2() << 18) | (ppn3() << 27); }
uint64_t ppn0() const { return bits_.ppn0_; }
uint64_t ppn1() const { return bits_.ppn1_; }
uint64_t ppn2() const { return bits_.ppn2_; }
uint64_t ppn3() const { return bits_.ppn3_; }
uint32_t levels() const { return 4; }
uint32_t size() const { return sizeof(data_); }
uint64_t ppn(int i) const
{
if (i == 0) { return ppn0(); }
if (i == 1) { return ppn1(); }
if (i == 2) { return ppn2(); }
if (i == 3) { return ppn3(); }
assert(0);
return 0;
}
uint32_t paPpnShift(int i) const
{
if (i == 0) { return 12; }
if (i == 1) { return 21; }
if (i == 2) { return 30; }
if (i == 3) { return 39; }
assert(0);
return 0;
}
};
/// Structure to unpack the fields of 32-bit virtual address.
struct Va32Bits
{
unsigned offset_ : 12;
unsigned vpn0_ : 10;
unsigned vpn1_ : 10;
} __attribute__((packed));
/// 32-bit virtual address.
union Va32
{
Va32Bits bits_;
uint32_t data_ = 0;
Va32(uint32_t word) : data_(word)
{ }
uint32_t offset() const { return bits_.offset_; }
uint32_t vpn0() const { return bits_.vpn0_; }
uint32_t vpn1() const { return bits_.vpn1_; }
uint32_t vpn(int i) const
{
if (i == 0) return vpn0();
if (i == 1) return vpn1();
assert(0);
return 0;
}
};
/// Structure to unpack the fields of Sv39 virtual address.
struct Va39Bits
{
unsigned offset_ : 12;
unsigned vpn0_ : 9;
unsigned vpn1_ : 9;
unsigned vpn2_ : 9;
} __attribute__((packed));
/// 39-bit virtual address.
union Va39
{
Va39Bits bits_;
uint64_t data_ = 0;
Va39(uint64_t data) : data_(data)
{ }
uint64_t offset() const { return bits_.offset_; }
uint64_t vpn0() const { return bits_.vpn0_; }
uint64_t vpn1() const { return bits_.vpn1_; }
uint64_t vpn2() const { return bits_.vpn2_; }
uint64_t vpn(int i) const
{
if (i == 0) return vpn0();
if (i == 1) return vpn1();
if (i == 2) return vpn2();
assert(0);
return 0;
}
};
/// Structure to unpack the fields of Sv48 virtual address.
struct Va48Bits
{
unsigned offset_ : 12;
unsigned vpn0_ : 9;
unsigned vpn1_ : 9;
unsigned vpn2_ : 9;
unsigned vpn3_ : 9;
} __attribute__((packed));
/// 48-bit virtual address.
union Va48
{
Va48Bits bits_;
uint64_t data_ = 0;
Va48(uint64_t data) : data_(data)
{ }
uint64_t offset() const { return bits_.offset_; }
uint64_t vpn0() const { return bits_.vpn0_; }
uint64_t vpn1() const { return bits_.vpn1_; }
uint64_t vpn2() const { return bits_.vpn2_; }
uint64_t vpn3() const { return bits_.vpn2_; }
uint64_t vpn(int i) const
{
if (i == 0) return vpn0();
if (i == 1) return vpn1();
if (i == 2) return vpn2();
if (i == 3) return vpn3();
assert(0);
return 0;
}
};
template <typename URV>
class Hart;
class PmpManager;
class VirtMem
{
public:
friend class Hart<uint32_t>;
friend class Hart<uint64_t>;
enum Mode { Bare = 0, Sv32 = 1, Sv39 = 8, Sv48 = 9, Sv57 = 10, Sv64 = 11 };
VirtMem(unsigned hartIx, Memory& memory, unsigned pageSize,
PmpManager& pmpMgr, unsigned tlbSize);
/// Perform virtual to physical memory address translation and
/// check for read access if the read flag is true (similary also
/// check for write access is the write flag is true ...). Return
/// encoutered exception on failure or ExceptionType::NONE on
/// success.
ExceptionCause translate(uint64_t va, PrivilegeMode pm, bool read,
bool write, bool exec, uint64_t& pa);
/// Same as translate but only check for execute access.
ExceptionCause translateForFetch(uint64_t va, PrivilegeMode pm, uint64_t& pa);
/// Same as translate but only check for read access.
ExceptionCause translateForLoad(uint64_t va, PrivilegeMode pm, uint64_t& pa);
/// Same as translate but only check for write access.
ExceptionCause translateForStore(uint64_t va, PrivilegeMode pm, uint64_t& pa);
/// Return page size.
unsigned pageSize() const
{ return pageSize_; }
/// Return the address of the first byte in the page containing
/// the given address.
uint64_t pageStartAddress(uint64_t address) const
{ return (address >> pageBits_) << pageBits_; }
/// Debug method: Print all the entries in the page table.
void printPageTable(std::ostream& os) const;
/// Print all the page table entries at or below the page table
/// page rooted at the given address. This is a helper to
/// printPageTable.
template <typename PTE, typename VA>
void printEntries(std::ostream& os, uint64_t addr, std::string path) const;
protected:
/// Helper to translate method.
template <typename PTE, typename VA>
ExceptionCause pageTableWalk(uint64_t va, PrivilegeMode pm, bool read, bool write,
bool exec, uint64_t& pa, TlbEntry& tlbEntry);
/// Helper to translate method.
ExceptionCause pageTableWalkUpdateTlb(uint64_t va, PrivilegeMode pm, bool read,
bool write, bool exec, uint64_t& pa);
/// Set the page table root page: The root page is placed in
/// physical memory at address root * page_size
void setPageTableRootPage(uint64_t root)
{ pageTableRootPage_ = root; }
// Change the translation mode to m. Page size is reset to 4096.
void setMode(Mode m)
{
mode_ = m;
pageSize_ = 4096;
pageBits_ = 12;
}
/// Set the address space id (asid).
void setAddressSpace(uint32_t asid)
{ asid_ = asid; }
/// Make executable pages also readable (supports MXR bit in MSTATUS).
void setExecReadable(bool flag)
{ execReadable_ = flag; }
/// Allow supervisor-mode code to access user-mode pages (supports SUM
/// bit in MSTATUS).
void setSupervisorAccessUser(bool flag)
{ supervisorOk_ = flag; }
/// Return true if successful and false if page size is not supported.
bool setPageSize(uint64_t size);
/// Return current address space id.
uint32_t addressSpace() const
{ return asid_; }
private:
Memory& memory_;
uint64_t pageTableRootPage_ = 0;
Mode mode_ = Bare;
uint32_t asid_ = 0;
unsigned pageSize_ = 4096;
unsigned pageBits_ = 12;
uint64_t pageMask_ = 0xfff;
unsigned hartIx_ = 0;
uint64_t time_ = 0; // Access order
// Cached mstatus bits
bool execReadable_ = false; // MXR bit
bool supervisorOk_ = false; // SUM bit
bool faultOnFirstAccess_ = true; // Make this configurable.
PmpManager& pmpMgr_;
Tlb tlb_;
};
}