[PATCH v2] lib: utils: irqchip: handle IMSIC mask boundaries

Pengpeng Hou pengpeng at iscas.ac.cn
Wed Dec 2 18:42:46 PST 2026


imsic_data_check() computes an address mask by shifting a one past the
top of the field, which is undefined when that shift equals the word
size. It also validates the group shift when no group field exists.

Form guest, hart and group masks with a checked sbi_make_field_mask()
helper. A zero-width field has an empty mask; non-empty fields must fit
in the word before GENMASK is used. Reuse the guest mask plus page
offset bits for the regset size alignment check. Exclude the page
offset from address comparisons as before; those comparisons identify
the fixed part of the address, not the alignment of each regset.

Fixes: 9f73669959dd ("lib: utils/irqchip: Add IMSIC library")
Signed-off-by: Pengpeng Hou <pengpeng at iscas.ac.cn>
---
Changes since v1:
Use the shared bitops helper, field-specific names and GENMASK as
suggested by Anup.
Previous version: https://lore.kernel.org/opensbi/20260827115534.49509-1-pengpeng@iscas.ac.cn

Codex assisted with patch preparation and review.
This OpenSBI revision was not built.

 include/sbi/sbi_bitops.h  | 16 ++++++++++++
 lib/utils/irqchip/imsic.c | 62 ++++++++++++++++++++---------------------------
 2 files changed, 42 insertions(+), 36 deletions(-)

diff --git a/include/sbi/sbi_bitops.h b/include/sbi/sbi_bitops.h
index d7825e81ea05fb19a37a7493940a3ae32dbd121f..51978c846eb472a787bc17631b60ad838c286a75 100644
--- a/include/sbi/sbi_bitops.h
+++ b/include/sbi/sbi_bitops.h
@@ -10,6 +10,7 @@
 #ifndef __SBI_BITOPS_H__
 #define __SBI_BITOPS_H__
 
+#include <sbi/sbi_error.h>
 #include <sbi/sbi_types.h>
 
 #define BITS_PER_LONG		(8 * __SIZEOF_LONG__)
@@ -37,6 +38,21 @@
 
 #define GENMASK_ULL(h, l) \
 	(((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
+
+/* Form a shifted field mask without shifting by the word size. */
+static inline int sbi_make_field_mask(u32 bits, u32 shift, unsigned long *mask)
+{
+	if (!bits) {
+		*mask = 0;
+		return 0;
+	}
+	if (shift >= BITS_PER_LONG || bits > BITS_PER_LONG - shift)
+		return SBI_EINVAL;
+
+	*mask = GENMASK(shift + bits - 1, shift);
+	return 0;
+}
+
 /**
  * sbi_ffs - find first (less-significant) set bit in a long word.
  * @word: The word to search
diff --git a/lib/utils/irqchip/imsic.c b/lib/utils/irqchip/imsic.c
index 41cfadeb982d9f04b01c623597d5b91fe392df09..e24d80b2f87fbd2825940536595d951a6cc550e0 100644
--- a/lib/utils/irqchip/imsic.c
+++ b/lib/utils/irqchip/imsic.c
@@ -11,6 +11,7 @@
 #include <sbi/riscv_asm.h>
 #include <sbi/riscv_io.h>
 #include <sbi/riscv_encoding.h>
+#include <sbi/sbi_bitops.h>
 #include <sbi/sbi_console.h>
 #include <sbi/sbi_csr_detect.h>
 #include <sbi/sbi_domain.h>
@@ -302,8 +303,10 @@ static int imsic_warm_irqchip_init(struct sbi_irqchip_device *dev)
 
 int imsic_data_check(struct imsic_data *imsic)
 {
-	u32 i, tmp;
-	unsigned long base_addr, addr, mask;
+	unsigned long guest_mask, hart_mask, group_mask;
+	unsigned long fixed_mask, base_addr, addr;
+	u32 i;
+	int rc;
 
 	/* Sanity checks */
 	if (!imsic ||
@@ -311,27 +314,24 @@ int imsic_data_check(struct imsic_data *imsic)
 	    (IMSIC_MAX_ID < imsic->num_ids))
 		return SBI_EINVAL;
 
-	tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT;
-	if (tmp < imsic->guest_index_bits)
-		return SBI_EINVAL;
-
-	tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
-	      imsic->guest_index_bits;
-	if (tmp < imsic->hart_index_bits)
-		return SBI_EINVAL;
-
-	tmp = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
-	      imsic->guest_index_bits - imsic->hart_index_bits;
-	if (tmp < imsic->group_index_bits)
-		return SBI_EINVAL;
+	rc = sbi_make_field_mask(imsic->guest_index_bits, IMSIC_MMIO_PAGE_SHIFT,
+				 &guest_mask);
+	if (rc)
+		return rc;
+	rc = sbi_make_field_mask(imsic->hart_index_bits,
+				 IMSIC_MMIO_PAGE_SHIFT + imsic->guest_index_bits,
+				 &hart_mask);
+	if (rc)
+		return rc;
 
-	tmp = IMSIC_MMIO_PAGE_SHIFT + imsic->guest_index_bits +
-	      imsic->hart_index_bits;
-	if (imsic->group_index_shift < tmp)
-		return SBI_EINVAL;
-	tmp = imsic->group_index_bits + imsic->group_index_shift - 1;
-	if (tmp >= BITS_PER_LONG)
+	if (imsic->group_index_bits &&
+	    imsic->group_index_shift < IMSIC_MMIO_PAGE_SHIFT +
+				       imsic->guest_index_bits + imsic->hart_index_bits)
 		return SBI_EINVAL;
+	rc = sbi_make_field_mask(imsic->group_index_bits, imsic->group_index_shift,
+				 &group_mask);
+	if (rc)
+		return rc;
 
 	/*
 	 * Number of interrupt identities should be 1 less than
@@ -345,24 +345,14 @@ int imsic_data_check(struct imsic_data *imsic)
 		return SBI_EINVAL;
 
 	/* Match patter of each regset */
-	base_addr = imsic->regs[0].addr;
-	base_addr &= ~((1UL << (imsic->guest_index_bits +
-				 imsic->hart_index_bits +
-				 IMSIC_MMIO_PAGE_SHIFT)) - 1);
-	base_addr &= ~(((1UL << imsic->group_index_bits) - 1) <<
-			imsic->group_index_shift);
+	fixed_mask = ~(group_mask | hart_mask | guest_mask |
+		       (IMSIC_MMIO_PAGE_SZ - 1));
+	base_addr = imsic->regs[0].addr & fixed_mask;
 	for (i = 0; i < IMSIC_MAX_REGS && imsic->regs[i].size; i++) {
-		mask = (1UL << imsic->guest_index_bits) * IMSIC_MMIO_PAGE_SZ;
-		mask -= 1UL;
-		if (imsic->regs[i].size & mask)
+		if (imsic->regs[i].size & (guest_mask | (IMSIC_MMIO_PAGE_SZ - 1)))
 			return SBI_EINVAL;
 
-		addr = imsic->regs[i].addr;
-		addr &= ~((1UL << (imsic->guest_index_bits +
-					 imsic->hart_index_bits +
-					 IMSIC_MMIO_PAGE_SHIFT)) - 1);
-		addr &= ~(((1UL << imsic->group_index_bits) - 1) <<
-				imsic->group_index_shift);
+		addr = imsic->regs[i].addr & fixed_mask;
 		if (base_addr != addr)
 			return SBI_EINVAL;
 	}

base-commit: d0139edf3168bb63df9b2a499e9031a1a8edd65e




More information about the opensbi mailing list