Skip to content

Commit c3bf02a

Browse files
authored
Refactor the control_check_pc API. (#1234)
This makes it return just an optional exception instead of also a possibly modified target address. The more general API was once used for an earlier CHERI model, but now the generality is not used. Addresses #1205.
1 parent 1e8bb6b commit c3bf02a

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

model/riscv_addr_checks.sail

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ function ext_handle_fetch_check_error(err : ext_fetch_addr_error) -> unit =
2626

2727
type ext_control_addr_error = unit
2828

29-
/* these functions return the address to use as the target for
30-
* the control transfer. any address translation or other errors
31-
* are reported for the original value, not the returned value.
29+
/* This function returns an optional exception for the address to use
30+
* as the target for the control transfer. The control address is
31+
* derived from the PC register, e.g. in JAL.
3232
*
3333
* NOTE: the input value does *not* have bit[0] set to 0, to enable
34-
* more accurate bounds checking. There is no constraint on the output
35-
* value, which will have bit[0] cleared by the caller if needed.
34+
* more accurate bounds checking.
3635
*/
3736

38-
/* the control address is derived from the PC register, e.g. in JAL */
39-
function ext_control_check_pc(pc : xlenbits) -> Ext_ControlAddr_Check(ext_control_addr_error) =
40-
Ext_ControlAddr_OK(Virtaddr(pc))
37+
function ext_control_check_pc(pc : xlenbits) -> option(ext_control_addr_error) =
38+
None()
4139

4240
function ext_handle_control_check_error(err : ext_control_addr_error) -> unit =
4341
()

model/riscv_addr_checks_common.sail

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
* extensions would need to define their own functions to override them.
1515
*/
1616

17-
union Ext_ControlAddr_Check ('a : Type) = {
18-
Ext_ControlAddr_OK : virtaddr, /* PC value to use for the target of the control operation */
19-
Ext_ControlAddr_Error : 'a
20-
}
21-
2217
union Ext_DataAddr_Check ('a : Type) = {
2318
Ext_DataAddr_OK : virtaddr, /* Address to use for the data access */
2419
Ext_DataAddr_Error : 'a

model/riscv_insts_base.sail

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,21 @@ mapping clause assembly = UTYPE(imm, rd, op)
4848
function jump_to(target : xlenbits) -> ExecutionResult = {
4949
// Extensions get the first checks on the prospective target address.
5050
match ext_control_check_pc(target) {
51-
Ext_ControlAddr_Error(e) => Ext_ControlAddr_Check_Failure(e),
52-
Ext_ControlAddr_OK(target) => {
53-
// Perform standard alignment check.
54-
let target_bits = bits_of(target);
55-
// Check target is at least 2-byte aligned (callers must ensure
56-
// this so it can be an assertion).
57-
assert(target_bits[0] == bitzero);
58-
// If it is not 4-byte aligned and compressed instructions are
59-
// not enabled then raise an alignment exception.
60-
if bit_to_bool(target_bits[1]) & not(currentlyEnabled(Ext_Zca)) then {
61-
Memory_Exception(target, E_Fetch_Addr_Align())
62-
} else {
63-
set_next_pc(target_bits);
64-
RETIRE_SUCCESS
65-
}
66-
}
67-
}
51+
Some(e) => return Ext_ControlAddr_Check_Failure(e),
52+
None() => (),
53+
};
54+
55+
// Perform standard alignment check.
56+
// Check target is at least 2-byte aligned (callers must ensure
57+
// this so it can be an assertion).
58+
assert(target[0] == bitzero);
59+
// If it is not 4-byte aligned and compressed instructions are
60+
// not enabled then raise an alignment exception.
61+
if bit_to_bool(target[1]) & not(currentlyEnabled(Ext_Zca))
62+
then return Memory_Exception(Virtaddr(target), E_Fetch_Addr_Align());
63+
64+
set_next_pc(target);
65+
RETIRE_SUCCESS
6866
}
6967

7068
/* ****************************************************************** */

0 commit comments

Comments
 (0)