[PATCH v3 0/4] hw/riscv: describe only the PLIC contexts that exist

Ralf Ramsauer posted 4 patches 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/cover.1792700406.git.ralf@vmexit.de
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
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(-)
[PATCH v3 0/4] hw/riscv: describe only the PLIC contexts that exist
Posted by Ralf Ramsauer 1 month ago
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/20260927231838.236616-1-ralf@vmexit.de/
v2: https://lore.kernel.org/qemu-devel/20261021110838.3097959-1-ralf@vmexit.de/

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

 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
-- 
2.54.0
Re: [PATCH v3 0/4] hw/riscv: describe only the PLIC contexts that exist
Posted by Alistair Francis 1 week, 4 days ago
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/20260927231838.236616-1-ralf@vmexit.de/
> v2:
> https://lore.kernel.org/qemu-devel/20261021110838.3097959-1-ralf@vmexit.de/
> 
> 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