* [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
@ 2026-11-16 15:29 Baptiste Le Duc
2026-11-16 15:36 ` Jan Beulich
2026-11-16 16:32 ` Oleksii Kurochko
0 siblings, 2 replies; 7+ messages in thread
From: Baptiste Le Duc @ 2026-11-16 15:29 UTC (permalink / raw)
To: xen-devel
Cc: Alistair Francis, Connor Davis, Oleksii Kurochko, Andrew Cooper,
Anthony PERARD, Michal Orzel, Jan Beulich, Julien Grall,
Roger Pau Monné, Stefano Stabellini, Baptiste Le Duc
imsic_init() requires every MMIO register set to be exactly
IMSIC_HART_SIZE(guest_index_bits) * nr_parent_irqs bytes, i.e. to cover
the interrupt files of all harts listed in interrupts-extended. This only
holds when the DT describes a single register set.
When harts are split into groups (riscv,group-index-bits > 0), each group
has its own register set, which only covers the harts of that group. For
example, the SpacemiT V100 IMSIC node describes two 2 MiB register sets,
each holding 64 hart slots of 32 KiB, with 128 parent interrupts in total.
Xen expects 4 MiB per register set and panics with:
IMSIC MMIO size is incorrect 2097152, expected MMIO size: 4194304
Check instead that a register set does not run past the end of the group it
starts in, i.e. BIT(hart_index_bits) hart slots. A register set may be
smaller than that, e.g. when a group holds a non-power-of-two number of
harts.
Parent interrupt i gets the i-th hart slot of the register sets, taken in
order. A slot with no hart behind it can be:
- at the very end of the last register set, e.g. when a DT describes a
group by its full capacity: harmless, no interrupt is left to take it.
- anywhere else: every later interrupt is shifted one slot early, onto the
previous hart's interrupt file.
Both show up as more slots than parent interrupts, so counting can't tell
them apart. Refusing to boot would reject the valid DTs of the first case,
so only warn: they keep booting, and broken ones get a hint as to why their
interrupts are misrouted.
While at it, print the MMIO sizes in hex to match the DT reg property.
Fixes: c9bd8b322ecb ("xen/riscv: imsic_init() implementation")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Baptiste Le Duc <baptiste.le-duc@vates.tech>
---
Changes in v2:
- Bound a register set by the end of the group it starts in (offset +
size), not only by the group size.
- Warn when the register sets hold more hart slots than parent
interrupts, as an unused slot before the end of the last register set
misroutes every following hart.
- Reuse the group size for the base address mask.
- Link to v1: https://patch.msgid.link/20261113-fix-imsic-mmio-dt-v1-1-10efb6c28de4@vates.tech
---
xen/arch/riscv/imsic.c | 39 +++++++++++++++++++++++++++++----------
1 file changed, 29 insertions(+), 10 deletions(-)
diff --git a/xen/arch/riscv/imsic.c b/xen/arch/riscv/imsic.c
index 44b8640ea3..48acc02440 100644
--- a/xen/arch/riscv/imsic.c
+++ b/xen/arch/riscv/imsic.c
@@ -381,7 +381,7 @@ void cf_check vcpu_imsic_deinit(struct vcpu *v)
int __init imsic_init(const struct dt_device_node *node)
{
int rc;
- unsigned long reloff, hartid;
+ unsigned long reloff, hartid, nr_slots = 0;
unsigned int nr_parent_irqs, index, nr_handlers = 0;
paddr_t base_addr;
unsigned int nr_mmios;
@@ -415,9 +415,11 @@ int __init imsic_init(const struct dt_device_node *node)
/* Check MMIO register sets */
for ( unsigned int i = 0; i < nr_mmios; i++ )
{
- unsigned int guest_bits = imsic_cfg.guest_index_bits;
- unsigned long expected_mmio_size =
- IMSIC_HART_SIZE(guest_bits) * nr_parent_irqs;
+ /* Address space of one group: BIT(hart_index_bits) hart slots. */
+ unsigned long group_size = BIT(imsic_cfg.guest_index_bits +
+ imsic_cfg.hart_index_bits +
+ IMSIC_MMIO_PAGE_SHIFT, UL);
+ unsigned long offset;
rc = dt_device_get_address(node, i, &mmios[i].base_addr,
&mmios[i].size);
@@ -429,9 +431,7 @@ int __init imsic_init(const struct dt_device_node *node)
}
base_addr = mmios[i].base_addr;
- base_addr &= ~(BIT(guest_bits +
- imsic_cfg.hart_index_bits +
- IMSIC_MMIO_PAGE_SHIFT, UL) - 1);
+ base_addr &= ~(group_size - 1);
base_addr &= ~((BIT(imsic_cfg.group_index_bits, UL) - 1) <<
imsic_cfg.group_index_shift);
if ( base_addr != imsic_cfg.base_addr )
@@ -442,15 +442,34 @@ int __init imsic_init(const struct dt_device_node *node)
goto imsic_init_err;
}
- if ( mmios[i].size != expected_mmio_size )
+ /* A register set must not run past the end of its group. */
+ offset = mmios[i].base_addr & (group_size - 1);
+ if ( mmios[i].size + offset > group_size )
{
rc = -EINVAL;
- printk(XENLOG_ERR "%s: IMSIC MMIO size is incorrect %ld, expected MMIO size: %ld\n",
- node->name, mmios[i].size, expected_mmio_size);
+ printk(XENLOG_ERR
+ "%s: IMSIC MMIO regset %u (offset %#lx + size %#lx) exceeds group size %#lx\n",
+ node->name, i, offset, mmios[i].size, group_size);
goto imsic_init_err;
}
+
+ /* The last slot may be cut short, see the lookup below. */
+ nr_slots += DIV_ROUND_UP(mmios[i].size,
+ IMSIC_HART_SIZE(imsic_cfg.guest_index_bits));
}
+ /*
+ * Parent irq i is given hart slot i, counting slots across the register
+ * sets in order. A slot with no hart behind it therefore shifts every
+ * later irq one slot early, onto the previous hart's interrupt file.
+ * Counting can't tell that from harmless unused slots at the end, hence
+ * only a warning.
+ */
+ if ( nr_slots > nr_parent_irqs )
+ printk(XENLOG_WARNING
+ "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
+ node->name, nr_slots, nr_parent_irqs);
+
/* Configure handlers for target CPUs */
for ( unsigned int i = 0; i < nr_parent_irqs; i++ )
{
---
base-commit: 97bf370375113caf92fccf04e2877fc6b5ff887f
change-id: 20261113-fix-imsic-mmio-dt-c3317ac2efec
Best regards,
--
Baptiste Le Duc <baptiste.le-duc@vates.tech>
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
2026-11-16 15:29 [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots Baptiste Le Duc
@ 2026-11-16 15:36 ` Jan Beulich
2026-11-16 15:45 ` Baptiste Le Duc
2026-11-16 16:32 ` Oleksii Kurochko
1 sibling, 1 reply; 7+ messages in thread
From: Jan Beulich @ 2026-11-16 15:36 UTC (permalink / raw)
To: Baptiste Le Duc
Cc: Alistair Francis, Connor Davis, Oleksii Kurochko, Andrew Cooper,
Anthony PERARD, Michal Orzel, Julien Grall, Roger Pau Monné,
Stefano Stabellini, xen-devel
On 16.11.2026 16:29, Baptiste Le Duc wrote:
> @@ -442,15 +442,34 @@ int __init imsic_init(const struct dt_device_node *node)
> goto imsic_init_err;
> }
>
> - if ( mmios[i].size != expected_mmio_size )
> + /* A register set must not run past the end of its group. */
> + offset = mmios[i].base_addr & (group_size - 1);
> + if ( mmios[i].size + offset > group_size )
> {
> rc = -EINVAL;
> - printk(XENLOG_ERR "%s: IMSIC MMIO size is incorrect %ld, expected MMIO size: %ld\n",
> - node->name, mmios[i].size, expected_mmio_size);
> + printk(XENLOG_ERR
> + "%s: IMSIC MMIO regset %u (offset %#lx + size %#lx) exceeds group size %#lx\n",
> + node->name, i, offset, mmios[i].size, group_size);
> goto imsic_init_err;
> }
> +
> + /* The last slot may be cut short, see the lookup below. */
> + nr_slots += DIV_ROUND_UP(mmios[i].size,
> + IMSIC_HART_SIZE(imsic_cfg.guest_index_bits));
> }
>
> + /*
> + * Parent irq i is given hart slot i, counting slots across the register
> + * sets in order. A slot with no hart behind it therefore shifts every
> + * later irq one slot early, onto the previous hart's interrupt file.
> + * Counting can't tell that from harmless unused slots at the end, hence
> + * only a warning.
> + */
> + if ( nr_slots > nr_parent_irqs )
> + printk(XENLOG_WARNING
> + "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
I'll leave full review to Oleksii, just a remark here: This wants shortening some.
Not only for source line length (we allow going past 80 cols, but please not this
much), but also for what actually ends up on a serial console. For example, I
wonder whether what follows after the comma really needs to go to the log. If the
message appears, context info can be obtained by consulting the source.
Jan
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
2026-11-16 15:36 ` Jan Beulich
@ 2026-11-16 15:45 ` Baptiste Le Duc
2026-11-16 15:47 ` Jan Beulich
0 siblings, 1 reply; 7+ messages in thread
From: Baptiste Le Duc @ 2026-11-16 15:45 UTC (permalink / raw)
To: xen-devel
>> + /*
>> + * Parent irq i is given hart slot i, counting slots across the register
>> + * sets in order. A slot with no hart behind it therefore shifts every
>> + * later irq one slot early, onto the previous hart's interrupt file.
>> + * Counting can't tell that from harmless unused slots at the end, hence
>> + * only a warning.
>> + */
>> + if ( nr_slots > nr_parent_irqs )
>> + printk(XENLOG_WARNING
>> + "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
>
> I'll leave full review to Oleksii, just a remark here: This wants shortening some.
> Not only for source line length (we allow going past 80 cols, but please not this
> much), but also for what actually ends up on a serial console. For example, I
> wonder whether what follows after the comma really needs to go to the log. If the
> message appears, context info can be obtained by consulting the source.
>
I kept the literal on one line so it can be grepped, as CODING_STYLE
asks, but I see the point is rather to keep the message itself short.
I'll drop the part after the comma, the comment above already explains
it:
printk(XENLOG_WARNING "%s: %lu hart slots for %u parent irqs\n",
node->name, nr_slots, nr_parent_irqs);
Thanks for highlighting that.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
2026-11-16 15:45 ` Baptiste Le Duc
@ 2026-11-16 15:47 ` Jan Beulich
0 siblings, 0 replies; 7+ messages in thread
From: Jan Beulich @ 2026-11-16 15:47 UTC (permalink / raw)
To: Baptiste Le Duc; +Cc: xen-devel
On 16.11.2026 16:45, Baptiste Le Duc wrote:
>>> + /*
>>> + * Parent irq i is given hart slot i, counting slots across the register
>>> + * sets in order. A slot with no hart behind it therefore shifts every
>>> + * later irq one slot early, onto the previous hart's interrupt file.
>>> + * Counting can't tell that from harmless unused slots at the end, hence
>>> + * only a warning.
>>> + */
>>> + if ( nr_slots > nr_parent_irqs )
>>> + printk(XENLOG_WARNING
>>> + "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
>>
>> I'll leave full review to Oleksii, just a remark here: This wants shortening some.
>> Not only for source line length (we allow going past 80 cols, but please not this
>> much), but also for what actually ends up on a serial console. For example, I
>> wonder whether what follows after the comma really needs to go to the log. If the
>> message appears, context info can be obtained by consulting the source.
>>
> I kept the literal on one line so it can be grepped, as CODING_STYLE
> asks, but I see the point is rather to keep the message itself short.
> I'll drop the part after the comma, the comment above already explains
> it:
>
> printk(XENLOG_WARNING "%s: %lu hart slots for %u parent irqs\n",
> node->name, nr_slots, nr_parent_irqs);
And just to clarify - no need to send a v3 just for this. If no other
change is needed before this can go in, the change can be made while
committing.
Jan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
2026-11-16 15:29 [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots Baptiste Le Duc
2026-11-16 15:36 ` Jan Beulich
@ 2026-11-16 16:32 ` Oleksii Kurochko
2026-11-16 16:42 ` Baptiste Le Duc
1 sibling, 1 reply; 7+ messages in thread
From: Oleksii Kurochko @ 2026-11-16 16:32 UTC (permalink / raw)
To: Baptiste Le Duc, xen-devel
Cc: Alistair Francis, Connor Davis, Andrew Cooper, Anthony PERARD,
Michal Orzel, Jan Beulich, Julien Grall, Roger Pau Monné,
Stefano Stabellini
On 11/16/26 4:29 PM, Baptiste Le Duc wrote:
> imsic_init() requires every MMIO register set to be exactly
> IMSIC_HART_SIZE(guest_index_bits) * nr_parent_irqs bytes, i.e. to cover
> the interrupt files of all harts listed in interrupts-extended. This only
> holds when the DT describes a single register set.
>
> When harts are split into groups (riscv,group-index-bits > 0), each group
> has its own register set, which only covers the harts of that group. For
> example, the SpacemiT V100 IMSIC node describes two 2 MiB register sets,
> each holding 64 hart slots of 32 KiB, with 128 parent interrupts in total.
> Xen expects 4 MiB per register set and panics with:
>
> IMSIC MMIO size is incorrect 2097152, expected MMIO size: 4194304
>
> Check instead that a register set does not run past the end of the group it
> starts in, i.e. BIT(hart_index_bits) hart slots. A register set may be
> smaller than that, e.g. when a group holds a non-power-of-two number of
> harts.
>
> Parent interrupt i gets the i-th hart slot of the register sets, taken in
> order. A slot with no hart behind it can be:
> - at the very end of the last register set, e.g. when a DT describes a
> group by its full capacity: harmless, no interrupt is left to take it.
> - anywhere else: every later interrupt is shifted one slot early, onto the
> previous hart's interrupt file.
>
> Both show up as more slots than parent interrupts, so counting can't tell
> them apart. Refusing to boot would reject the valid DTs of the first case,
> so only warn: they keep booting, and broken ones get a hint as to why their
> interrupts are misrouted.
>
> While at it, print the MMIO sizes in hex to match the DT reg property.
>
> Fixes: c9bd8b322ecb ("xen/riscv: imsic_init() implementation")
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Baptiste Le Duc <baptiste.le-duc@vates.tech>
> ---
> Changes in v2:
> - Bound a register set by the end of the group it starts in (offset +
> size), not only by the group size.
> - Warn when the register sets hold more hart slots than parent
> interrupts, as an unused slot before the end of the last register set
> misroutes every following hart.
> - Reuse the group size for the base address mask.
> - Link to v1: https://patch.msgid.link/20261113-fix-imsic-mmio-dt-v1-1-10efb6c28de4@vates.tech
> ---
> xen/arch/riscv/imsic.c | 39 +++++++++++++++++++++++++++++----------
> 1 file changed, 29 insertions(+), 10 deletions(-)
>
> diff --git a/xen/arch/riscv/imsic.c b/xen/arch/riscv/imsic.c
> index 44b8640ea3..48acc02440 100644
> --- a/xen/arch/riscv/imsic.c
> +++ b/xen/arch/riscv/imsic.c
> @@ -381,7 +381,7 @@ void cf_check vcpu_imsic_deinit(struct vcpu *v)
> int __init imsic_init(const struct dt_device_node *node)
> {
> int rc;
> - unsigned long reloff, hartid;
> + unsigned long reloff, hartid, nr_slots = 0;
> unsigned int nr_parent_irqs, index, nr_handlers = 0;
> paddr_t base_addr;
> unsigned int nr_mmios;
> @@ -415,9 +415,11 @@ int __init imsic_init(const struct dt_device_node *node)
> /* Check MMIO register sets */
> for ( unsigned int i = 0; i < nr_mmios; i++ )
> {
> - unsigned int guest_bits = imsic_cfg.guest_index_bits;
> - unsigned long expected_mmio_size =
> - IMSIC_HART_SIZE(guest_bits) * nr_parent_irqs;
> + /* Address space of one group: BIT(hart_index_bits) hart slots. */
> + unsigned long group_size = BIT(imsic_cfg.guest_index_bits +
> + imsic_cfg.hart_index_bits +
> + IMSIC_MMIO_PAGE_SHIFT, UL);
> + unsigned long offset;
>
> rc = dt_device_get_address(node, i, &mmios[i].base_addr,
> &mmios[i].size);
> @@ -429,9 +431,7 @@ int __init imsic_init(const struct dt_device_node *node)
> }
>
> base_addr = mmios[i].base_addr;
> - base_addr &= ~(BIT(guest_bits +
> - imsic_cfg.hart_index_bits +
> - IMSIC_MMIO_PAGE_SHIFT, UL) - 1);
> + base_addr &= ~(group_size - 1);
> base_addr &= ~((BIT(imsic_cfg.group_index_bits, UL) - 1) <<
> imsic_cfg.group_index_shift);
> if ( base_addr != imsic_cfg.base_addr )
> @@ -442,15 +442,34 @@ int __init imsic_init(const struct dt_device_node *node)
> goto imsic_init_err;
> }
>
> - if ( mmios[i].size != expected_mmio_size )
> + /* A register set must not run past the end of its group. */
> + offset = mmios[i].base_addr & (group_size - 1);
> + if ( mmios[i].size + offset > group_size )
This can overflow for a bogus, huge size from the DT and then pass. As
offset is always below group_size,
if ( mmios[i].size > group_size - offset )
can't.
> {
> rc = -EINVAL;
> - printk(XENLOG_ERR "%s: IMSIC MMIO size is incorrect %ld, expected MMIO size: %ld\n",
> - node->name, mmios[i].size, expected_mmio_size);
> + printk(XENLOG_ERR
> + "%s: IMSIC MMIO regset %u (offset %#lx + size %#lx) exceeds group size %#lx\n",
> + node->name, i, offset, mmios[i].size, group_size);
> goto imsic_init_err;
> }
> +
> + /* The last slot may be cut short, see the lookup below. */
> + nr_slots += DIV_ROUND_UP(mmios[i].size,
> + IMSIC_HART_SIZE(imsic_cfg.guest_index_bits));
> }
>
> + /*
> + * Parent irq i is given hart slot i, counting slots across the register
> + * sets in order. A slot with no hart behind it therefore shifts every
> + * later irq one slot early, onto the previous hart's interrupt file.
> + * Counting can't tell that from harmless unused slots at the end, hence
> + * only a warning.
> + */
> + if ( nr_slots > nr_parent_irqs )
> + printk(XENLOG_WARNING
> + "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
> + node->name, nr_slots, nr_parent_irqs);
> +
With the overflow addressed and fixing a length of the string like (took
from Baptiste response):
printk(XENLOG_WARNING "%s: %lu hart slots for %u parent irqs\n",
node->name, nr_slots, nr_parent_irqs);
Reviewed-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
~ Oleksii
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
2026-11-16 16:32 ` Oleksii Kurochko
@ 2026-11-16 16:42 ` Baptiste Le Duc
2026-11-16 16:50 ` Jan Beulich
0 siblings, 1 reply; 7+ messages in thread
From: Baptiste Le Duc @ 2026-11-16 16:42 UTC (permalink / raw)
To: xen-devel
On 11/16/26 5:34 PM, Oleksii Kurochko wrote:
>> - if ( mmios[i].size != expected_mmio_size )
>> + /* A register set must not run past the end of its group. */
>> + offset = mmios[i].base_addr & (group_size - 1);
>> + if ( mmios[i].size + offset > group_size )
>
> This can overflow for a bogus, huge size from the DT and then pass. As
> offset is always below group_size,
Oh yeah, thanks.
>
> if ( mmios[i].size > group_size - offset )
>
> can't.
Jan, do you think it needs a new version for this overflow?
>
>
> With the overflow addressed and fixing a length of the string like (took
> from Baptiste response):
>
> printk(XENLOG_WARNING "%s: %lu hart slots for %u parent irqs\n",
> node->name, nr_slots, nr_parent_irqs);
>
> Reviewed-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
Thanks.
>
> ~ Oleksii
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
2026-11-16 16:42 ` Baptiste Le Duc
@ 2026-11-16 16:50 ` Jan Beulich
0 siblings, 0 replies; 7+ messages in thread
From: Jan Beulich @ 2026-11-16 16:50 UTC (permalink / raw)
To: Baptiste Le Duc; +Cc: xen-devel
On 16.11.2026 17:42, Baptiste Le Duc wrote:
> On 11/16/26 5:34 PM, Oleksii Kurochko wrote:
>
>>> - if ( mmios[i].size != expected_mmio_size )
>>> + /* A register set must not run past the end of its group. */
>>> + offset = mmios[i].base_addr & (group_size - 1);
>>> + if ( mmios[i].size + offset > group_size )
>>
>> This can overflow for a bogus, huge size from the DT and then pass. As
>> offset is always below group_size,
> Oh yeah, thanks.
>>
>> if ( mmios[i].size > group_size - offset )
>>
>> can't.
> Jan, do you think it needs a new version for this overflow?
I can do this adjustment as well while committing.
Jan
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-16 16:50 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 15:29 [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots Baptiste Le Duc
2026-11-16 15:36 ` Jan Beulich
2026-11-16 15:45 ` Baptiste Le Duc
2026-11-16 15:47 ` Jan Beulich
2026-11-16 16:32 ` Oleksii Kurochko
2026-11-16 16:42 ` Baptiste Le Duc
2026-11-16 16:50 ` Jan Beulich
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.