STOPI CSR Incorrect Value for Virtual Supervisor External Interrupt in QEMU (AIA Extension)
Description of problem
We are developing a RISC-V core and have implemented tests for the AIA extension. One of these tests reveals a bug when running in QEMU in a bare-metal environment. The issue relates to the value of the STOPI CSR. Although the CSR behaves correctly in most cases, when a virtual supervisor external interrupt is generated using the IMSIC (i.e., both hip.vseip and hie.vseip are set to 1) while executing in supervisor mode with hideleg.vseip set to 0, the STOPI CSR always returns the value 0. However, according to the specifications, it should return 10, which is the major identity number for the virtual supervisor external interrupt.
According to the RISC-V specs:
The value of stopi is zero unless: (a) there is an interrupt that is both pending in sip and enabled in sie, or, if the hypervisor extension is implemented, both pending in hip and enabled in hie; and (b) the interrupt is not delegated to a lower privilege level (by hideleg, if the hypervisor extension is implemented). When there is a pending-and-enabled major interrupt for supervisor level, field IID is the major identity number of the highest-priority interrupt, and field IPRIO indicates its priority.
Upon reviewing the QEMU AIA code, I found the following snippet in qemu/target/riscv/cpu_helper.c that might be causing the problem:
int riscv_cpu_sirq_pending(CPURISCVState *env)
{
uint64_t irqs = riscv_cpu_all_pending(env) & env->mideleg &
~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP);
uint64_t irqs_f = env->mvip & env->mvien & ~env->mideleg & env->sie;
return riscv_cpu_pending_to_irq(env, IRQ_S_EXT, IPRIO_DEFAULT_S,
irqs | irqs_f, env->siprio);
}
It appears that virtual supervisor (VS) interrupts are being masked (via the ~(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP) operation) when they likely should not be, which could explain why the STOPI CSR does not reflect the correct value.
Thank you for your time, and please let me know if any details require further clarification or if my interpretation is incorrect.