Commit c3af4e0af1 for qemu.org

commit c3af4e0af1d7c6cd10a7c824096f73009c8250a4
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Thu Nov 5 10:58:16 2026 +0000

    hw/i386/amd_iommu: Mask address when calculating large PTE page size

    The amd_iommu handling of the "large PTE page size" (where Next Level
    bits are 0b111) case is not correct.  We calculate the page size
    using the address in the descriptor (as per table 14 of the spec),
    but we don't explicitly pick out the Page Address field, instead
    passing the whole PTE word to cto64().  This means that a PTE which
    incorrectly sets all the reserved high bits can cause cto64() to
    return 63 or 64, and we will try to shift by an out of range amount,
    which is undefined behaviour.

    Mask the PTE with AMDVI_DEV_PT_ROOT_MASK to get the page address
    (matching how we do it to get the address for the next page table
    level in NEXT_PTE_ADDR).

    Cc: qemu-stable@nongnu.org
    Fixes: a74bb3110a5b ("amd_iommu: Add helpers to walk AMD v1 Page Table format")
    Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3618
    Reported-by: huntr bubble <https://gitlab.com/bubblehuntr>
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Reviewed-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
    Reviewed-by: Sairaj kodilkar <sarunkod@amd.com>
    Acked-by: Michael S. Tsirkin <mst@redhat.com>
    Message-id: 20261105105816.3012283-1-peter.maydell@linaro.org

diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
index 948d8611b1..008e882724 100644
--- a/hw/i386/amd_iommu.h
+++ b/hw/i386/amd_iommu.h
@@ -216,7 +216,8 @@
 /*
  * Take a PTE value with mode=0x07 and return the page size it encodes.
  */
-#define PTE_LARGE_PAGE_SIZE(pte)    (1ULL << (1 + cto64(((pte) | 0xfffULL))))
+#define PTE_LARGE_PAGE_SIZE(pte) \
+    (1ULL << (1 + cto64((((pte) & AMDVI_DEV_PT_ROOT_MASK) | 0xfffULL))))

 /* Return number of PTEs to use for a given page size (expected power of 2) */
 #define PAGE_SIZE_PTE_COUNT(pgsz)       (1ULL << ((ctz64(pgsz) - 12) % 9))