Skip to content

Commit b6504cd

Browse files
KevinRSXalistair23
authored andcommitted
target/riscv: Add select value range check for counter delegation
This adds checks in ops performed on xireg and xireg2-xireg6 so that the counter delegation function will receive a valid xiselect value with the proper extensions enabled. Co-developed-by: Atish Patra <[email protected]> Signed-off-by: Kaiwen Xue <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Signed-off-by: Atish Patra <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent e84af93 commit b6504cd

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

target/riscv/csr.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,11 @@ static bool xiselect_aia_range(target_ulong isel)
21592159
(ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST);
21602160
}
21612161

2162+
static bool xiselect_cd_range(target_ulong isel)
2163+
{
2164+
return (ISELECT_CD_FIRST <= isel && isel <= ISELECT_CD_LAST);
2165+
}
2166+
21622167
static int rmw_iprio(target_ulong xlen,
21632168
target_ulong iselect, uint8_t *iprio,
21642169
target_ulong *val, target_ulong new_val,
@@ -2284,6 +2289,17 @@ static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno,
22842289
return RISCV_EXCP_NONE;
22852290
}
22862291

2292+
static int rmw_xireg_cd(CPURISCVState *env, int csrno,
2293+
target_ulong isel, target_ulong *val,
2294+
target_ulong new_val, target_ulong wr_mask)
2295+
{
2296+
if (!riscv_cpu_cfg(env)->ext_smcdeleg) {
2297+
return RISCV_EXCP_ILLEGAL_INST;
2298+
}
2299+
/* TODO: Implement the functionality later */
2300+
return RISCV_EXCP_NONE;
2301+
}
2302+
22872303
/*
22882304
* rmw_xireg_csrind: Perform indirect access to xireg and xireg2-xireg6
22892305
*
@@ -2295,7 +2311,25 @@ static int rmw_xireg_csrind(CPURISCVState *env, int csrno,
22952311
target_ulong isel, target_ulong *val,
22962312
target_ulong new_val, target_ulong wr_mask)
22972313
{
2298-
return -EINVAL;
2314+
int ret = -EINVAL;
2315+
bool virt = csrno == CSR_VSIREG ? true : false;
2316+
2317+
if (xiselect_cd_range(isel)) {
2318+
ret = rmw_xireg_cd(env, csrno, isel, val, new_val, wr_mask);
2319+
} else {
2320+
/*
2321+
* As per the specification, access to unimplented region is undefined
2322+
* but recommendation is to raise illegal instruction exception.
2323+
*/
2324+
return RISCV_EXCP_ILLEGAL_INST;
2325+
}
2326+
2327+
if (ret) {
2328+
return (env->virt_enabled && virt) ?
2329+
RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
2330+
}
2331+
2332+
return RISCV_EXCP_NONE;
22992333
}
23002334

23012335
static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val,

0 commit comments

Comments
 (0)