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
function is_aligned_paddr(
Physaddr(
addr) :
physaddr,
width :
nat1) ->
bool =
unsigned(
addr)
% width == 0
function is_aligned_vaddr(
Virtaddr(
addr) :
virtaddr,
width :
nat1) ->
bool =
unsigned(
addr)
% width == 0
function is_aligned_bits(
vaddr :
xlenbits,
width :
word_width) ->
bool =
match width {
1 =>
true,
2 =>
vaddr[
0..
0]
== zeros(),
4 =>
vaddr[
1..
0]
== zeros(),
8 =>
vaddr[
2..
0]
== zeros(),
}
overload is_aligned_addr = {
is_aligned_paddr,
is_aligned_vaddr,
is_aligned_bits}
function read_kind_of_flags (
aq :
bool,
rl :
bool,
res :
bool) ->
option(
read_kind) =
match (
aq,
rl,
res) {
(
false,
false,
false) =>
Some(
Read_plain),
(
true,
false,
false) =>
Some(
Read_RISCV_acquire),
(
true,
true,
false) =>
Some(
Read_RISCV_strong_acquire),
(
false,
false,
true) =>
Some(
Read_RISCV_reserved),
(
true,
false,
true) =>
Some(
Read_RISCV_reserved_acquire),
(
true,
true,
true) =>
Some(
Read_RISCV_reserved_strong_acquire),
(
false,
true,
false) =>
None(), (
false,
true,
true) =>
None()
}
function write_kind_of_flags (
aq :
bool,
rl :
bool,
con :
bool) ->
write_kind =
match (
aq,
rl,
con) {
(
false,
false,
false) =>
Write_plain,
(
false,
true,
false) =>
Write_RISCV_release,
(
false,
false,
true) =>
Write_RISCV_conditional,
(
false,
true ,
true) =>
Write_RISCV_conditional_release,
(
true,
true,
false) =>
Write_RISCV_strong_release,
(
true,
true ,
true) =>
Write_RISCV_conditional_strong_release,
(
true,
false,
false) =>
throw(
Error_not_implemented(
"store.aq")),
(
true,
false,
true) =>
throw(
Error_not_implemented(
"sc.aq"))
}
function phys_mem_read forall 'n,
0 < 'n <= max_mem_access . (
t :
AccessType(
ext_access_type),
paddr :
physaddr,
width :
int(
'n),
aq :
bool,
rl:
bool,
res :
bool,
meta :
bool) ->
MemoryOpResult((
bits(
8 * 'n),
mem_meta)) = {
let result = (
match read_kind_of_flags(
aq,
rl,
res) {
Some(
rk) =>
Some(
read_ram(
rk,
paddr,
width,
meta)),
None() =>
None()
}) :
option((
bits(
8 * 'n),
mem_meta));
match (
t,
result) {
(
InstructionFetch(),
None()) =>
Err(
E_Fetch_Access_Fault()),
(
Read(
Data),
None()) =>
Err(
E_Load_Access_Fault()),
(_,
None()) =>
Err(
E_SAMO_Access_Fault()),
(_,
Some(
v,
m)) =>
Ok(
v,
m),
}
}
val phys_access_check :
forall 'n,
0 < 'n <= max_mem_access . (
AccessType(
ext_access_type),
Privilege,
physaddr,
int(
'n)) ->
option(
ExceptionType)
function phys_access_check (
t,
p,
paddr,
width) = {
let pmpError :
option(
ExceptionType) =
if sys_pmp_count == 0 then None()
else pmpCheck(
paddr,
width,
t,
p);
pmpError
}
function checked_mem_read forall 'n,
0 < 'n <= max_mem_access . (
t :
AccessType(
ext_access_type),
priv :
Privilege,
paddr :
physaddr,
width :
int(
'n),
aq :
bool,
rl :
bool,
res:
bool,
meta :
bool,
) ->
MemoryOpResult((
bits(
8 * 'n),
mem_meta)) =
match phys_access_check(
t,
priv,
paddr,
width) {
Some(
e) =>
Err(
e),
None() => {
if within_mmio_readable(
paddr,
width)
then MemoryOpResult_add_meta(
mmio_read(
t,
paddr,
width),
default_meta)
else if within_phys_mem(
paddr,
width)
then phys_mem_read(
t,
paddr,
width,
aq,
rl,
res,
meta)
else match t {
InstructionFetch() =>
Err(
E_Fetch_Access_Fault()),
Read(
Data) =>
Err(
E_Load_Access_Fault()),
_ =>
Err(
E_SAMO_Access_Fault())
}
}
}
val mem_read :
forall 'n,
0 < 'n <= max_mem_access . (
AccessType(
ext_access_type),
physaddr,
int(
'n),
bool,
bool,
bool) ->
MemoryOpResult(
bits(
8 * 'n))
val mem_read_priv :
forall 'n,
0 < 'n <= max_mem_access . (
AccessType(
ext_access_type),
Privilege,
physaddr,
int(
'n),
bool,
bool,
bool) ->
MemoryOpResult(
bits(
8 * 'n))
val mem_read_meta :
forall 'n,
0 < 'n <= max_mem_access . (
AccessType(
ext_access_type),
physaddr,
int(
'n),
bool,
bool,
bool,
bool) ->
MemoryOpResult((
bits(
8 * 'n),
mem_meta))
val mem_read_priv_meta :
forall 'n,
0 < 'n <= max_mem_access . (
AccessType(
ext_access_type),
Privilege,
physaddr,
int(
'n),
bool,
bool,
bool,
bool) ->
MemoryOpResult((
bits(
8 * 'n),
mem_meta))
function mem_read_priv_meta (
typ,
priv,
paddr,
width,
aq,
rl,
res,
meta) = {
let result :
MemoryOpResult((
bits(
8 * 'n),
mem_meta)) =
if (
aq | res)
& not(
is_aligned_addr(
paddr,
width))
then Err(
E_Load_Addr_Align())
else match (
aq,
rl,
res) {
(
false,
true,
false) =>
throw(
Error_not_implemented(
"load.rl")),
(
false,
true,
true) =>
throw(
Error_not_implemented(
"lr.rl")),
(_, _, _) =>
checked_mem_read(
typ,
priv,
paddr,
width,
aq,
rl,
res,
meta)
};
match result {
Ok(
value, _) =>
mem_read_callback(
to_str(
typ),
bits_of(
paddr),
width,
value),
Err(
e) =>
mem_exception_callback(
bits_of(
paddr),
exceptionType_bits(
e)),
};
result
}
function mem_read_meta (
typ,
paddr,
width,
aq,
rl,
res,
meta) =
mem_read_priv_meta(
typ,
effectivePrivilege(
typ,
mstatus,
cur_privilege),
paddr,
width,
aq,
rl,
res,
meta)
function mem_read_priv (
typ,
priv,
paddr,
width,
aq,
rl,
res) =
MemoryOpResult_drop_meta(
mem_read_priv_meta(
typ,
priv,
paddr,
width,
aq,
rl,
res,
false))
function mem_read (
typ,
paddr,
width,
aq,
rel,
res) =
mem_read_priv(
typ,
effectivePrivilege(
typ,
mstatus,
cur_privilege),
paddr,
width,
aq,
rel,
res)
val mem_write_ea :
forall 'n,
0 < 'n <= max_mem_access . (
physaddr,
int(
'n),
bool,
bool,
bool) ->
MemoryOpResult(
unit)
function mem_write_ea (
addr,
width,
aq,
rl,
con) =
if (
rl | con)
& not(
is_aligned_addr(
addr,
width))
then Err(
E_SAMO_Addr_Align())
else Ok(
write_ram_ea(
write_kind_of_flags(
aq,
rl,
con),
addr,
width))
function phys_mem_write forall 'n,
0 < 'n <= max_mem_access . (
wk :
write_kind,
paddr :
physaddr,
width :
int(
'n),
data :
bits(
8 * 'n),
meta :
mem_meta) ->
MemoryOpResult(
bool) =
Ok(
write_ram(
wk,
paddr,
width,
data,
meta))
function checked_mem_write forall 'n,
0 < 'n <= max_mem_access . (
paddr :
physaddr,
width :
int(
'n),
data:
bits(
8 * 'n),
typ :
AccessType(
ext_access_type),
priv :
Privilege,
meta:
mem_meta,
aq :
bool,
rl :
bool,
con :
bool,
) ->
MemoryOpResult(
bool) =
match phys_access_check(
typ,
priv,
paddr,
width) {
Some(
e) =>
Err(
e),
None() => {
if within_mmio_writable(
paddr,
width)
then mmio_write(
paddr,
width,
data)
else if within_phys_mem(
paddr,
width)
then {
let wk =
write_kind_of_flags(
aq,
rl,
con);
phys_mem_write(
wk,
paddr,
width,
data,
meta)
}
else Err(
E_SAMO_Access_Fault())
}
}
val mem_write_value_priv_meta :
forall 'n,
0 < 'n <= max_mem_access . (
physaddr,
int(
'n),
bits(
8 * 'n),
AccessType(
ext_access_type),
Privilege,
mem_meta,
bool,
bool,
bool) ->
MemoryOpResult(
bool)
function mem_write_value_priv_meta (
paddr,
width,
value,
typ,
priv,
meta,
aq,
rl,
con) = {
if (
rl | con)
& not(
is_aligned_addr(
paddr,
width))
then Err(
E_SAMO_Addr_Align())
else {
let result =
checked_mem_write(
paddr,
width,
value,
typ,
priv,
meta,
aq,
rl,
con);
match result {
Ok(_) =>
mem_write_callback(
to_str(
typ),
bits_of(
paddr),
width,
value),
Err(
e) =>
mem_exception_callback(
bits_of(
paddr),
exceptionType_bits(
e)),
};
result
}
}
val mem_write_value_priv :
forall 'n,
0 < 'n <= max_mem_access . (
physaddr,
int(
'n),
bits(
8 * 'n),
Privilege,
bool,
bool,
bool) ->
MemoryOpResult(
bool)
function mem_write_value_priv (
paddr,
width,
value,
priv,
aq,
rl,
con) =
mem_write_value_priv_meta(
paddr,
width,
value,
Write(
default_write_acc),
priv,
default_meta,
aq,
rl,
con)
val mem_write_value_meta :
forall 'n,
0 < 'n <= max_mem_access . (
physaddr,
int(
'n),
bits(
8 * 'n),
ext_access_type,
mem_meta,
bool,
bool,
bool) ->
MemoryOpResult(
bool)
function mem_write_value_meta (
paddr,
width,
value,
ext_acc,
meta,
aq,
rl,
con) = {
let typ =
Write(
ext_acc);
let ep =
effectivePrivilege(
typ,
mstatus,
cur_privilege);
mem_write_value_priv_meta(
paddr,
width,
value,
typ,
ep,
meta,
aq,
rl,
con)
}
val mem_write_value :
forall 'n,
0 < 'n <= max_mem_access . (
physaddr,
int(
'n),
bits(
8 * 'n),
bool,
bool,
bool) ->
MemoryOpResult(
bool)
function mem_write_value (
paddr,
width,
value,
aq,
rl,
con) = {
mem_write_value_meta(
paddr,
width,
value,
default_write_acc,
default_meta,
aq,
rl,
con)
}