From: Peter Maydell <[email protected]> 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 <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]> --- hw/intc/riscv_aplic.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c index 03a0852b8b88..b29a22a455a1 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); -- 2.55.0
