forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPmpManager.cpp
107 lines (83 loc) · 2.4 KB
/
PmpManager.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
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
// 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.
#include <iostream>
#include <cmath>
#include <cassert>
#include <fstream>
#include "PmpManager.hpp"
using namespace WdRiscv;
PmpManager::PmpManager(uint64_t /*memSize*/, uint64_t /*sectionSize*/)
{
}
PmpManager::~PmpManager() = default;
void
PmpManager::reset()
{
regions_.clear();
fastRegion_.reset();
}
void
PmpManager::defineRegion(uint64_t a0, uint64_t a1, Pmp::Type type,
Pmp::Mode mode, unsigned pmpIx, bool lock)
{
a0 = (a0 >> 2) << 2; // Make word aligned.
a1 = (a1 >> 2) << 2; // Make word aligned.
Pmp pmp(mode, pmpIx, lock, type);
Region region{a0, a1, pmp};
regions_.push_back(region);
}
void
PmpManager::printRegion(std::ostream& os, Region region) const
{
const auto& pmp = region.pmp_;
os << "pmp ix: " << std::dec << pmp.pmpIndex() << "\n";
os << "base addr: " << std::hex << region.firstAddr_ << "\n";
os << "last addr: " << std::hex << region.lastAddr_ << "\n";
os << "rwx: " << Pmp::toString(Pmp::Mode(pmp.mode_)) << "\n";
os << "matching: " << Pmp::toString(Pmp::Type(pmp.type_)) << "\n";
}
void
PmpManager::printPmps(std::ostream& os, uint64_t addr) const
{
auto region = getRegion(addr);
printRegion(os, region);
}
void
PmpManager::printPmps(std::ostream& os) const
{
for (const auto& region : regions_)
printRegion(os, region);
}
std::string
Pmp::toString(Pmp::Type type)
{
switch (type)
{
case Pmp::Type::Off: return "off";
case Pmp::Type::Tor: return "tor";
case Pmp::Type::Na4: return "na4";
case Pmp::Type::Napot: return "napot";
default: return "?";
}
return "";
}
std::string
Pmp::toString(Pmp::Mode mode)
{
std::string result;
result += (mode & Mode::Read) ? "r" : "-";
result += (mode & Mode::Write) ? "w" : "-";
result += (mode & Mode::Exec) ? "x" : "-";
return result;
}