summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2026-11-16 10:47:01 +0800
committerGitHub <[email protected]>2026-11-16 10:47:01 +0800
commit43791bbf2e618414fa6d59c56831b6909d05f611 (patch)
tree24d79bf26f14e1d8109cc1e74eca78e4b3fef61c /components
parentebf206d823d9267c5534105f1ef8e6cb8d702a8b (diff)
[drivers][ivshmem] Validate the shared memory window #11993
Diffstat (limited to 'components')
-rw-r--r--components/drivers/ivshmem/Kconfig12
-rw-r--r--components/drivers/ivshmem/ivshmem.c117
2 files changed, 122 insertions, 7 deletions
diff --git a/components/drivers/ivshmem/Kconfig b/components/drivers/ivshmem/Kconfig
index b042022911..faa3286151 100644
--- a/components/drivers/ivshmem/Kconfig
+++ b/components/drivers/ivshmem/Kconfig
@@ -8,3 +8,15 @@ config RT_IVSHMEM_QEMU
bool "QEMU"
depends on RT_USING_IVSHMEM
default n
+
+config RT_IVSHMEM_JAILHOUSE
+ bool "JailHouse shared memory in PCI config space"
+ depends on RT_USING_IVSHMEM
+ default n
+ help
+ Take the shared memory address and size from the device's
+ vendor-specific PCI config space (0x40..0x4f) when BAR2 is not
+ available. These fields are controlled by the device and are not
+ validated by the PCI core, so enable this only when running as a
+ JailHouse guest whose hypervisor and PCI devices are trusted.
+ Without this option, a device must provide the shared memory as BAR2.
diff --git a/components/drivers/ivshmem/ivshmem.c b/components/drivers/ivshmem/ivshmem.c
index fc1efa3047..5b1d80edd6 100644
--- a/components/drivers/ivshmem/ivshmem.c
+++ b/components/drivers/ivshmem/ivshmem.c
@@ -127,6 +127,83 @@ rt_err_t ivshmem_install_intx_vector(struct ivshmem_device *ivdev, const char *n
return err;
}
+static rt_bool_t ivshmem_shmem_range_valid(rt_uint64_t addr, rt_uint64_t size)
+{
+ rt_uint64_t max_addr = (rt_uint64_t)(rt_ubase_t)-1;
+#ifdef RT_PAGE_AFFINITY_BLOCK_SIZE
+ rt_uint64_t lo_off;
+#endif
+
+ /*
+ * A zero-sized window, a length that cannot be represented by rt_size_t
+ * or an address out of native range is invalid.
+ */
+ if (!size || size > (rt_uint64_t)(rt_size_t)-1 || addr > max_addr)
+ {
+ return RT_FALSE;
+ }
+
+ /* The whole window must be representable with native pointer types */
+ if (size - 1 > max_addr - addr)
+ {
+ return RT_FALSE;
+ }
+
+#ifdef RT_PAGE_AFFINITY_BLOCK_SIZE
+ /*
+ * rt_ioremap() aligns the mapping on a page block. Make sure that both the
+ * offset inside the first block and the aligned length fit in rt_size_t.
+ */
+ lo_off = addr & (RT_PAGE_AFFINITY_BLOCK_SIZE - 1);
+
+ if ((size > (rt_uint64_t)(rt_size_t)-1 - lo_off) ||
+ (size + lo_off > (rt_uint64_t)(rt_size_t)-1 - (RT_PAGE_AFFINITY_BLOCK_SIZE - 1)))
+ {
+ return RT_FALSE;
+ }
+#endif
+
+ return RT_TRUE;
+}
+
+#ifdef RT_IVSHMEM_JAILHOUSE
+static rt_err_t ivshmem_shmem_from_config(struct rt_pci_device *pdev,
+ void **addr, rt_size_t *size)
+{
+ rt_err_t err;
+ rt_uint32_t addr_lo = 0, addr_hi = 0, size_lo = 0, size_hi = 0;
+ rt_uint64_t shmem_addr, shmem_size;
+
+ err = rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_LO, &addr_lo);
+ err |= rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_HI, &addr_hi);
+ err |= rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_LO, &size_lo);
+ err |= rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_HI, &size_hi);
+
+ if (err)
+ {
+ LOG_E("IVSHMEM<%p> read shared memory config space failed", pdev);
+
+ return -RT_EIO;
+ }
+
+ shmem_addr = ((rt_uint64_t)addr_hi << 32) | addr_lo;
+ shmem_size = ((rt_uint64_t)size_hi << 32) | size_lo;
+
+ if (!ivshmem_shmem_range_valid(shmem_addr, shmem_size))
+ {
+ LOG_E("IVSHMEM<%p> invalid config space window: addr = 0x%lx, size = 0x%lx",
+ pdev, (rt_ubase_t)shmem_addr, (rt_ubase_t)shmem_size);
+
+ return -RT_EINVAL;
+ }
+
+ *addr = (void *)(rt_ubase_t)shmem_addr;
+ *size = (rt_size_t)shmem_size;
+
+ return RT_EOK;
+}
+#endif /* RT_IVSHMEM_JAILHOUSE */
+
static void ivshmem_free_resource(struct ivshmem_device *ivdev)
{
if (ivdev->pdev)
@@ -148,6 +225,7 @@ static void ivshmem_free_resource(struct ivshmem_device *ivdev)
rt_err_t ivshmem_pci_probe(struct rt_pci_device *pdev, struct ivshmem_device *ivdev)
{
rt_err_t err = RT_EOK;
+ struct rt_pci_bus_resource *shmem_res;
if (!ivdev || !pdev || pdev->irq < 0)
{
@@ -172,16 +250,41 @@ rt_err_t ivshmem_pci_probe(struct rt_pci_device *pdev, struct ivshmem_device *iv
/* Set all masks to on */
HWREG32(ivdev->reg + IVSHMEM_INTR_MASK) = 0xffffffff;
- ivdev->shmem_size = pdev->resource[IVSHMEM_SHARE_MEM_BAR].size;
- ivdev->shmem = (void *)pdev->resource[IVSHMEM_SHARE_MEM_BAR].base;
+ shmem_res = &pdev->resource[IVSHMEM_SHARE_MEM_BAR];
- if (!ivdev->shmem_size)
+ /*
+ * The BAR is allocated by the PCI host bridge, so its address is trusted.
+ * Only memory BARs describe the shared memory; an I/O BAR must never be
+ * passed to rt_ioremap() or exposed as a shared memory window.
+ * The JailHouse config words are controlled by the device and are only
+ * used when the platform explicitly opts in.
+ */
+ if ((shmem_res->flags == PCI_BUS_REGION_F_MEM ||
+ shmem_res->flags == PCI_BUS_REGION_F_PREFETCH) &&
+ ivshmem_shmem_range_valid(shmem_res->base, shmem_res->size))
{
- rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_LO, (rt_uint32_t *)&ivdev->shmem);
- rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_HI, (rt_uint32_t *)&ivdev->shmem + 1);
- rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_LO, (rt_uint32_t *)&ivdev->shmem_size);
- rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_HI, (rt_uint32_t *)&ivdev->shmem_size + 1);
+ ivdev->shmem = (void *)shmem_res->base;
+ ivdev->shmem_size = shmem_res->size;
+ }
+#ifdef RT_IVSHMEM_JAILHOUSE
+ else
+ {
+ err = ivshmem_shmem_from_config(pdev, &ivdev->shmem, &ivdev->shmem_size);
+
+ if (err)
+ {
+ goto _fail;
+ }
+ }
+#else
+ else
+ {
+ LOG_E("IVSHMEM<%p> no valid shared memory BAR", ivdev);
+
+ err = -RT_EIO;
+ goto _fail;
}
+#endif
ivdev->shmem = rt_ioremap(ivdev->shmem, ivdev->shmem_size);