Commit dc78319021 for qemu.org
commit dc78319021ca4dfc2a99b6e49fa07764df559fb0
Author: Peter Maydell <peter.maydell@linaro.org>
Date: Thu Oct 22 21:42:23 2026 +0100
hw/intc/riscv_aplic: Check config value bounds in realize
The riscv aplic interrupt controller has various configurable
properties for things like num-harts and num-irqs, which have
architectural upper limits.
Currently these are checked in riscv_aplic_create(), by assert().
QOM devices should check this kind of thing in their realize method,
and fail realize for out of range values (which in this case will
cause riscv_aplic_create() to make QEMU exit with an error, since it
uses &error_fatal). This isn't a problem currently as no SoC creates
and configures the TYPE_RISCV_APLIC device itself, but it avoids one
for the future.
Move the checks to the realize method. This requires slight
adjustments in some cases because:
* property num-irqs is set to num_sources + 1
* property iprio-mask is set to (1U << iprio_bits) - 1
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-ID: <20261022204223.854640-1-peter.maydell@linaro.org>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
index 03a0852b8b..b29a22a455 100644
--- a/hw/intc/riscv_aplic.c
+++ b/hw/intc/riscv_aplic.c
@@ -954,6 +954,32 @@ static void riscv_aplic_realize(DeviceState *dev, Error **errp)
uint32_t i;
RISCVAPLICState *aplic = RISCV_APLIC(dev);
+ if (aplic->num_harts >= APLIC_MAX_IDC) {
+ error_setg(errp, "num-harts must be less than %d", (int)APLIC_MAX_IDC);
+ return;
+ }
+ if ((APLIC_IDC_BASE + (aplic->num_harts * APLIC_IDC_SIZE)) >
+ aplic->aperture_size) {
+ error_setg(errp, "aperture-size 0x%x too small for num-harts %d",
+ aplic->aperture_size, aplic->num_harts);
+ return;
+ }
+ if (aplic->num_irqs > APLIC_MAX_SOURCE) {
+ error_setg(errp, "num-irqs must be less than or equal to %d",
+ APLIC_MAX_SOURCE);
+ return;
+ }
+ if (!is_power_of_2(aplic->iprio_mask + 1)) {
+ error_setg(errp, "iprio-mask must have all lower bits set");
+ return;
+ }
+ if (aplic->iprio_mask < ((1U << APLIC_MIN_IPRIO_BITS) - 1) ||
+ aplic->iprio_mask > ((1U << APLIC_MAX_IPRIO_BITS) - 1)) {
+ error_setg(errp, "iprio-mask must specify between %d and %d bits of "
+ "priority", APLIC_MIN_IPRIO_BITS, APLIC_MAX_IPRIO_BITS);
+ return;
+ }
+
if (riscv_use_emulated_aplic(aplic->msimode)) {
/* Create output IRQ lines for non-MSI mode */
if (!aplic->msimode) {
@@ -1119,12 +1145,6 @@ DeviceState *riscv_aplic_create(MemoryRegion *container,
DeviceState *dev = qdev_new(TYPE_RISCV_APLIC);
uint32_t i;
- assert(num_harts < APLIC_MAX_IDC);
- assert((APLIC_IDC_BASE + (num_harts * APLIC_IDC_SIZE)) <= size);
- assert(num_sources < APLIC_MAX_SOURCE);
- assert(APLIC_MIN_IPRIO_BITS <= iprio_bits);
- assert(iprio_bits <= APLIC_MAX_IPRIO_BITS);
-
qdev_prop_set_uint32(dev, "aperture-size", size);
qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
qdev_prop_set_uint32(dev, "num-harts", num_harts);