mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-05 16:53:55 -06:00
target/riscv: Add support to record CTR entries.
This commit adds logic to records CTR entries of different types and adds required hooks in TCG and interrupt/Exception logic to record events. This commit also adds support to invoke freeze CTR logic for breakpoint exceptions and counter overflow interrupts. Signed-off-by: Rajnesh Kanwal <rkanwal@rivosinc.com> Acked-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20250205-b4-ctr_upstream_v6-v6-4-439d8e06c8ef@rivosinc.com> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
This commit is contained in:
parent
c48bd18eae
commit
4ff7a27adc
8 changed files with 430 additions and 0 deletions
|
@ -875,6 +875,247 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv,
|
|||
}
|
||||
}
|
||||
|
||||
static void riscv_ctr_freeze(CPURISCVState *env, uint64_t freeze_mask,
|
||||
bool virt)
|
||||
{
|
||||
uint64_t ctl = virt ? env->vsctrctl : env->mctrctl;
|
||||
|
||||
assert((freeze_mask & (~(XCTRCTL_BPFRZ | XCTRCTL_LCOFIFRZ))) == 0);
|
||||
|
||||
if (ctl & freeze_mask) {
|
||||
env->sctrstatus |= SCTRSTATUS_FROZEN;
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t riscv_ctr_priv_to_mask(target_ulong priv, bool virt)
|
||||
{
|
||||
switch (priv) {
|
||||
case PRV_M:
|
||||
return MCTRCTL_M;
|
||||
case PRV_S:
|
||||
if (virt) {
|
||||
return XCTRCTL_S;
|
||||
}
|
||||
return XCTRCTL_S;
|
||||
case PRV_U:
|
||||
if (virt) {
|
||||
return XCTRCTL_U;
|
||||
}
|
||||
return XCTRCTL_U;
|
||||
}
|
||||
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
static uint64_t riscv_ctr_get_control(CPURISCVState *env, target_long priv,
|
||||
bool virt)
|
||||
{
|
||||
switch (priv) {
|
||||
case PRV_M:
|
||||
return env->mctrctl;
|
||||
case PRV_S:
|
||||
case PRV_U:
|
||||
if (virt) {
|
||||
return env->vsctrctl;
|
||||
}
|
||||
return env->mctrctl;
|
||||
}
|
||||
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
/*
|
||||
* This function assumes that src privilege and target privilege are not same
|
||||
* and src privilege is less than target privilege. This includes the virtual
|
||||
* state as well.
|
||||
*/
|
||||
static bool riscv_ctr_check_xte(CPURISCVState *env, target_long src_prv,
|
||||
bool src_virt)
|
||||
{
|
||||
target_long tgt_prv = env->priv;
|
||||
bool res = true;
|
||||
|
||||
/*
|
||||
* VS and U mode are same in terms of xTE bits required to record an
|
||||
* external trap. See 6.1.2. External Traps, table 8 External Trap Enable
|
||||
* Requirements. This changes VS to U to simplify the logic a bit.
|
||||
*/
|
||||
if (src_virt && src_prv == PRV_S) {
|
||||
src_prv = PRV_U;
|
||||
} else if (env->virt_enabled && tgt_prv == PRV_S) {
|
||||
tgt_prv = PRV_U;
|
||||
}
|
||||
|
||||
/* VU mode is an outlier here. */
|
||||
if (src_virt && src_prv == PRV_U) {
|
||||
res &= !!(env->vsctrctl & XCTRCTL_STE);
|
||||
}
|
||||
|
||||
switch (src_prv) {
|
||||
case PRV_U:
|
||||
if (tgt_prv == PRV_U) {
|
||||
break;
|
||||
}
|
||||
res &= !!(env->mctrctl & XCTRCTL_STE);
|
||||
/* fall-through */
|
||||
case PRV_S:
|
||||
if (tgt_prv == PRV_S) {
|
||||
break;
|
||||
}
|
||||
res &= !!(env->mctrctl & MCTRCTL_MTE);
|
||||
/* fall-through */
|
||||
case PRV_M:
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* Special cases for traps and trap returns:
|
||||
*
|
||||
* 1- Traps, and trap returns, between enabled modes are recorded as normal.
|
||||
* 2- Traps from an inhibited mode to an enabled mode, and trap returns from an
|
||||
* enabled mode back to an inhibited mode, are partially recorded. In such
|
||||
* cases, the PC from the inhibited mode (source PC for traps, and target PC
|
||||
* for trap returns) is 0.
|
||||
*
|
||||
* 3- Trap returns from an inhibited mode to an enabled mode are not recorded.
|
||||
* Traps from an enabled mode to an inhibited mode, known as external traps,
|
||||
* receive special handling.
|
||||
* By default external traps are not recorded, but a handshake mechanism exists
|
||||
* to allow partial recording. Software running in the target mode of the trap
|
||||
* can opt-in to allowing CTR to record traps into that mode even when the mode
|
||||
* is inhibited. The MTE, STE, and VSTE bits allow M-mode, S-mode, and VS-mode,
|
||||
* respectively, to opt-in. When an External Trap occurs, and xTE=1, such that
|
||||
* x is the target privilege mode of the trap, will CTR record the trap. In such
|
||||
* cases, the target PC is 0.
|
||||
*/
|
||||
/*
|
||||
* CTR arrays are implemented as circular buffers and new entry is stored at
|
||||
* sctrstatus.WRPTR, but they are presented to software as moving circular
|
||||
* buffers. Which means, software get's the illusion that whenever a new entry
|
||||
* is added the whole buffer is moved by one place and the new entry is added at
|
||||
* the start keeping new entry at idx 0 and older ones follow.
|
||||
*
|
||||
* Depth = 16.
|
||||
*
|
||||
* buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F]
|
||||
* WRPTR W
|
||||
* entry 7 6 5 4 3 2 1 0 F E D C B A 9 8
|
||||
*
|
||||
* When a new entry is added:
|
||||
* buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F]
|
||||
* WRPTR W
|
||||
* entry 8 7 6 5 4 3 2 1 0 F E D C B A 9
|
||||
*
|
||||
* entry here denotes the logical entry number that software can access
|
||||
* using ctrsource, ctrtarget and ctrdata registers. So xiselect 0x200
|
||||
* will return entry 0 i-e buffer[8] and 0x201 will return entry 1 i-e
|
||||
* buffer[7]. Here is how we convert entry to buffer idx.
|
||||
*
|
||||
* entry = isel - CTR_ENTRIES_FIRST;
|
||||
* idx = (sctrstatus.WRPTR - entry - 1) & (depth - 1);
|
||||
*/
|
||||
void riscv_ctr_add_entry(CPURISCVState *env, target_long src, target_long dst,
|
||||
enum CTRType type, target_ulong src_priv, bool src_virt)
|
||||
{
|
||||
bool tgt_virt = env->virt_enabled;
|
||||
uint64_t src_mask = riscv_ctr_priv_to_mask(src_priv, src_virt);
|
||||
uint64_t tgt_mask = riscv_ctr_priv_to_mask(env->priv, tgt_virt);
|
||||
uint64_t src_ctrl = riscv_ctr_get_control(env, src_priv, src_virt);
|
||||
uint64_t tgt_ctrl = riscv_ctr_get_control(env, env->priv, tgt_virt);
|
||||
uint64_t depth, head;
|
||||
bool ext_trap = false;
|
||||
|
||||
/*
|
||||
* Return immediately if both target and src recording is disabled or if
|
||||
* CTR is in frozen state.
|
||||
*/
|
||||
if ((!(src_ctrl & src_mask) && !(tgt_ctrl & tgt_mask)) ||
|
||||
env->sctrstatus & SCTRSTATUS_FROZEN) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* With RAS Emul enabled, only allow Indirect, direct calls, Function
|
||||
* returns and Co-routine swap types.
|
||||
*/
|
||||
if (tgt_ctrl & XCTRCTL_RASEMU &&
|
||||
type != CTRDATA_TYPE_INDIRECT_CALL &&
|
||||
type != CTRDATA_TYPE_DIRECT_CALL &&
|
||||
type != CTRDATA_TYPE_RETURN &&
|
||||
type != CTRDATA_TYPE_CO_ROUTINE_SWAP) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == CTRDATA_TYPE_EXCEPTION || type == CTRDATA_TYPE_INTERRUPT) {
|
||||
/* Case 2 for traps. */
|
||||
if (!(src_ctrl & src_mask)) {
|
||||
src = 0;
|
||||
} else if (!(tgt_ctrl & tgt_mask)) {
|
||||
/* Check if target priv-mode has allowed external trap recording. */
|
||||
if (!riscv_ctr_check_xte(env, src_priv, src_virt)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ext_trap = true;
|
||||
dst = 0;
|
||||
}
|
||||
} else if (type == CTRDATA_TYPE_EXCEP_INT_RET) {
|
||||
/*
|
||||
* Case 3 for trap returns. Trap returns from inhibited mode are not
|
||||
* recorded.
|
||||
*/
|
||||
if (!(src_ctrl & src_mask)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Case 2 for trap returns. */
|
||||
if (!(tgt_ctrl & tgt_mask)) {
|
||||
dst = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Ignore filters in case of RASEMU mode or External trap. */
|
||||
if (!(tgt_ctrl & XCTRCTL_RASEMU) && !ext_trap) {
|
||||
/*
|
||||
* Check if the specific type is inhibited. Not taken branch filter is
|
||||
* an enable bit and needs to be checked separatly.
|
||||
*/
|
||||
bool check = tgt_ctrl & BIT_ULL(type + XCTRCTL_INH_START);
|
||||
if ((type == CTRDATA_TYPE_NONTAKEN_BRANCH && !check) ||
|
||||
(type != CTRDATA_TYPE_NONTAKEN_BRANCH && check)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
head = get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK);
|
||||
|
||||
depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK);
|
||||
if (tgt_ctrl & XCTRCTL_RASEMU && type == CTRDATA_TYPE_RETURN) {
|
||||
head = (head - 1) & (depth - 1);
|
||||
|
||||
env->ctr_src[head] &= ~CTRSOURCE_VALID;
|
||||
env->sctrstatus =
|
||||
set_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK, head);
|
||||
return;
|
||||
}
|
||||
|
||||
/* In case of Co-routine SWAP we overwrite latest entry. */
|
||||
if (tgt_ctrl & XCTRCTL_RASEMU && type == CTRDATA_TYPE_CO_ROUTINE_SWAP) {
|
||||
head = (head - 1) & (depth - 1);
|
||||
}
|
||||
|
||||
env->ctr_src[head] = src | CTRSOURCE_VALID;
|
||||
env->ctr_dst[head] = dst & ~CTRTARGET_MISP;
|
||||
env->ctr_data[head] = set_field(0, CTRDATA_TYPE_MASK, type);
|
||||
|
||||
head = (head + 1) & (depth - 1);
|
||||
|
||||
env->sctrstatus = set_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK, head);
|
||||
}
|
||||
|
||||
void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool virt_en)
|
||||
{
|
||||
g_assert(newpriv <= PRV_M && newpriv != PRV_RESERVED);
|
||||
|
@ -1993,10 +2234,13 @@ void riscv_cpu_do_interrupt(CPUState *cs)
|
|||
!(env->mip & (1ULL << cause));
|
||||
bool smode_double_trap = false;
|
||||
uint64_t hdeleg = async ? env->hideleg : env->hedeleg;
|
||||
const bool prev_virt = env->virt_enabled;
|
||||
const target_ulong prev_priv = env->priv;
|
||||
target_ulong tval = 0;
|
||||
target_ulong tinst = 0;
|
||||
target_ulong htval = 0;
|
||||
target_ulong mtval2 = 0;
|
||||
target_ulong src;
|
||||
int sxlen = 0;
|
||||
int mxlen = 16 << riscv_cpu_mxl(env);
|
||||
bool nnmi_excep = false;
|
||||
|
@ -2182,6 +2426,8 @@ void riscv_cpu_do_interrupt(CPUState *cs)
|
|||
env->pc = (env->stvec >> 2 << 2) +
|
||||
((async && (env->stvec & 3) == 1) ? cause * 4 : 0);
|
||||
riscv_cpu_set_mode(env, PRV_S, virt);
|
||||
|
||||
src = env->sepc;
|
||||
} else {
|
||||
/*
|
||||
* If the hart encounters an exception while executing in M-mode
|
||||
|
@ -2266,6 +2512,19 @@ void riscv_cpu_do_interrupt(CPUState *cs)
|
|||
((async && (env->mtvec & 3) == 1) ? cause * 4 : 0);
|
||||
}
|
||||
riscv_cpu_set_mode(env, PRV_M, virt);
|
||||
src = env->mepc;
|
||||
}
|
||||
|
||||
if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) {
|
||||
if (async && cause == IRQ_PMU_OVF) {
|
||||
riscv_ctr_freeze(env, XCTRCTL_LCOFIFRZ, virt);
|
||||
} else if (!async && cause == RISCV_EXCP_BREAKPOINT) {
|
||||
riscv_ctr_freeze(env, XCTRCTL_BPFRZ, virt);
|
||||
}
|
||||
|
||||
riscv_ctr_add_entry(env, src, env->pc,
|
||||
async ? CTRDATA_TYPE_INTERRUPT : CTRDATA_TYPE_EXCEPTION,
|
||||
prev_priv, prev_virt);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue