[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots





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@xxxxxxxxxx>
---
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@xxxxxxxxxx
---
  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@xxxxxxxxx>

~ Oleksii



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.