On Thu, 2026-10-22 at 22:22 +0200, Ralf Ramsauer wrote:
> A PLIC context is addressed by its position in the 'interrupts-
> extended'
> property, so the device tree and the device have to agree on which
> harts
> contribute a context and in which order.  On the virt machine they do
> not: the device is built from riscv_plic_hart_config_string(), which
> gives a hart a supervisor context only if it has supervisor mode,
> while
> the device tree lists a machine and a supervisor context for every
> hart,
> unconditionally.  Boot with
> 
>     -cpu rv64,h=false,s=false,u=false -smp 4
> 
> and the PLIC implements four contexts while the device tree
> advertises
> eight.  A guest that takes its context number from the property then
> gets
> it wrong for every hart but the first.  This is what an M-mode kernel
> on
> virt runs into.  sifive_u hardcodes the fu540 topology instead and
> has
> the same latent problem, reachable with -cpu since the board
> restricts
> neither the CPU type nor its extensions.
> 
> 1 to 3 are the fix, 4 is cleanup and can be dropped, the rest does
> not
> depend on it.
> 
> Tested by comparing the PLIC's 'interrupts-extended' against the
> hart-config string of the PLIC device over 14 configurations of both
> boards: 7 disagree before the series, all agree after, and the device
> tree is unchanged wherever the default CPU is used.  The KVM paths
> could
> not be exercised, for lack of hardware.
> 
> v3:
>  - 4/4: the fdt helper takes has_m_mode from the board rather than
>    calling kvm_enabled() itself, keeping accelerator considerations
> in
>    the boards (Daniel)
> 
> v2:
>  - ask riscv_cpu_has_supervisor_mode() instead of reading RVS out of
> the
>    hart's CPURISCVState (Daniel)
>  - fold the KVM branch into the common loop, it describes the same
> rule
>  - fix sifive_u as well, and share the cell building between both
> boards
> 
> v1:
> https://lore.kernel.org/qemu-devel/[email protected]/
> v2:
> https://lore.kernel.org/qemu-devel/[email protected]/
> 
> Ralf Ramsauer (4):
>   target/riscv: add riscv_cpu_has_supervisor_mode()
>   hw/riscv/virt: only describe PLIC contexts that exist
>   hw/riscv/sifive_u: derive PLIC contexts from the harts
>   hw/riscv: share the PLIC interrupt cells between boards

Thanks!

Applied to riscv-to-apply.next

Alistair

> 
>  hw/riscv/fdt-common.c         | 18 ++++++++++++++++++
>  hw/riscv/sifive_u.c           | 19 ++++++++-----------
>  hw/riscv/virt.c               | 26 +++++++++-----------------
>  include/hw/riscv/fdt-common.h |  3 +++
>  target/riscv/cpu.c            | 14 ++++++++++++++
>  target/riscv/cpu.h            |  1 +
>  6 files changed, 53 insertions(+), 28 deletions(-)
> 
> 
> base-commit: 4f4225874d4d3747ec1142e935dbd314218992fa

Reply via email to