Skip to content

Commit 4d04efa

Browse files
rmn30nwf-msr
authored andcommitted
Trap if reading or writing upper 16 registers
1 parent 4b215f1 commit 4d04efa

File tree

3 files changed

+24
-71
lines changed

3 files changed

+24
-71
lines changed

model/riscv_regs.sail

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ register x12 : regtype
3131
register x13 : regtype
3232
register x14 : regtype
3333
register x15 : regtype
34-
register x16 : regtype
35-
register x17 : regtype
36-
register x18 : regtype
37-
register x19 : regtype
38-
register x20 : regtype
39-
register x21 : regtype
40-
register x22 : regtype
41-
register x23 : regtype
42-
register x24 : regtype
43-
register x25 : regtype
44-
register x26 : regtype
45-
register x27 : regtype
46-
register x28 : regtype
47-
register x29 : regtype
48-
register x30 : regtype
49-
register x31 : regtype
5034

5135
val rX : forall 'n, 0 <= 'n < 32. regno('n) -> xlenbits
5236
function rX r = {
@@ -68,23 +52,7 @@ function rX r = {
6852
13 => x13,
6953
14 => x14,
7054
15 => x15,
71-
16 => x16,
72-
17 => x17,
73-
18 => x18,
74-
19 => x19,
75-
20 => x20,
76-
21 => x21,
77-
22 => x22,
78-
23 => x23,
79-
24 => x24,
80-
25 => x25,
81-
26 => x26,
82-
27 => x27,
83-
28 => x28,
84-
29 => x29,
85-
30 => x30,
86-
31 => x31,
87-
_ => {assert(false, "invalid register number"); zero_reg}
55+
_ => throw Error_not_rv32e_register()
8856
};
8957
regval_from_reg(v)
9058
}
@@ -121,23 +89,7 @@ function wX (r, in_v) = {
12189
13 => x13 = v,
12290
14 => x14 = v,
12391
15 => x15 = v,
124-
16 => x16 = v,
125-
17 => x17 = v,
126-
18 => x18 = v,
127-
19 => x19 = v,
128-
20 => x20 = v,
129-
21 => x21 = v,
130-
22 => x22 = v,
131-
23 => x23 = v,
132-
24 => x24 = v,
133-
25 => x25 = v,
134-
26 => x26 = v,
135-
27 => x27 = v,
136-
28 => x28 = v,
137-
29 => x29 = v,
138-
30 => x30 = v,
139-
31 => x31 = v,
140-
_ => assert(false, "invalid register number")
92+
_ => throw Error_not_rv32e_register()
14193
};
14294
if (r != 0) then {
14395
rvfi_wX(r, in_v);
@@ -262,21 +214,5 @@ function init_base_regs () = {
262214
x12 = zero_reg;
263215
x13 = zero_reg;
264216
x14 = zero_reg;
265-
x15 = zero_reg;
266-
x16 = zero_reg;
267-
x17 = zero_reg;
268-
x18 = zero_reg;
269-
x19 = zero_reg;
270-
x20 = zero_reg;
271-
x21 = zero_reg;
272-
x22 = zero_reg;
273-
x23 = zero_reg;
274-
x24 = zero_reg;
275-
x25 = zero_reg;
276-
x26 = zero_reg;
277-
x27 = zero_reg;
278-
x28 = zero_reg;
279-
x29 = zero_reg;
280-
x30 = zero_reg;
281-
x31 = zero_reg
217+
x15 = zero_reg
282218
}

model/riscv_step.sail

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
/* SPDX-License-Identifier: BSD-2-Clause */
77
/*=======================================================================================*/
88

9+
function try_execute (x : ast) -> Retired = {
10+
try {
11+
execute(x)
12+
} catch {
13+
Error_not_rv32e_register() => { handle_illegal(); RETIRE_FAIL },
14+
e => throw e /* Rethrow other execptions */
15+
}
16+
}
17+
918
/* The emulator fetch-execute-interrupt dispatch loop. */
1019

1120
/* returns whether to increment the step count in the trace */
@@ -56,7 +65,7 @@ function step(step_no : int) -> bool = {
5665
/* check for RVC once here instead of every RVC execute clause. */
5766
if haveRVC() then {
5867
nextPC = PC + 2;
59-
(execute(ast), true)
68+
(try_execute(ast), true)
6069
} else {
6170
handle_illegal();
6271
(RETIRE_FAIL, true)
@@ -70,7 +79,7 @@ function step(step_no : int) -> bool = {
7079
print_instr("[" ^ dec_str(step_no) ^ "] [" ^ to_str(cur_privilege) ^ "]: " ^ BitStr(PC) ^ " (" ^ BitStr(w) ^ ") " ^ to_str(ast));
7180
};
7281
nextPC = PC + 4;
73-
(execute(ast), true)
82+
(try_execute(ast), true)
7483
}
7584
}
7685
}

model/riscv_types.sail

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ function arch_to_bits(a : Architecture) -> arch_xlen =
7272
/* model-internal exceptions */
7373

7474
union exception = {
75-
Error_not_implemented : string,
76-
Error_internal_error : unit
75+
/*
76+
* This is not the proper way to do this, but it is surely the simplest: we
77+
* we allow instructions to object to their operands rather than patching the
78+
* encdec mapping to do the right thing. (That would be extensive, because
79+
* the `ast` type does not have an encapsulated notion of a register selector
80+
* distinct from the underlying bit slice of the instruction.)
81+
*/
82+
Error_not_rv32e_register : unit,
83+
Error_not_implemented : string,
84+
Error_internal_error : unit
7785
}
7886

7987
val not_implemented : forall ('a : Type). string -> 'a

0 commit comments

Comments
 (0)