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

Peter Maydell posted 1 patch 2 weeks, 3 days ago
Maintainers: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>, Sairaj Kodilkar <sarunkod@amd.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, "Michael S. Tsirkin" <mst@redhat.com>
hw/i386/amd_iommu.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] hw/i386/amd_iommu: Mask address when calculating large PTE page size
Posted by Peter Maydell 2 weeks, 3 days ago
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
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>
---
Side note: we don't seem to enforce the spec's requirement that
the page size must be larger than the default for the level
and smaller than the default for the next level up, but that's
a different bug.
---
 hw/i386/amd_iommu.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
index 948d8611b15..008e882724c 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))
-- 
2.43.0
Re: [PATCH] hw/i386/amd_iommu: Mask address when calculating large PTE page size
Posted by Peter Maydell 5 days, 16 hours ago
On Thu, 5 Nov 2026 at 10:58, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> 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
> 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>
> ---

As this is a small fix that's been reviewed and not yet picked
up, I'll take it through target-arm.next. I have a small set
of "oops I forgot about that" patches that I'm planning to
send today.

thanks
-- PMM
Re: [PATCH] hw/i386/amd_iommu: Mask address when calculating large PTE page size
Posted by Michael S. Tsirkin 5 days, 16 hours ago
On Tue, Nov 17, 2026 at 11:40:27AM +0000, Peter Maydell wrote:
> On Thu, 5 Nov 2026 at 10:58, Peter Maydell <peter.maydell@linaro.org> wrote:
> >
> > 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
> > 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>
> > ---
> 
> As this is a small fix that's been reviewed and not yet picked
> up, I'll take it through target-arm.next. I have a small set
> of "oops I forgot about that" patches that I'm planning to
> send today.
> 
> thanks
> -- PMM


yea have a small set too.

Sure, take this.

Acked-by: Michael S. Tsirkin <mst@redhat.com>
Re: [PATCH] hw/i386/amd_iommu: Mask address when calculating large PTE page size
Posted by Sairaj Kodilkar 2 weeks, 2 days ago

On 11/5/2026 4:28 PM, Peter Maydell wrote:
> 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
> 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>
> ---
> Side note: we don't seem to enforce the spec's requirement that
> the page size must be larger than the default for the level
> and smaller than the default for the next level up, but that's
> a different bug.
> ---
>  hw/i386/amd_iommu.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
> index 948d8611b15..008e882724c 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))

Reviewed-by: Sairaj kodilkar <sarunkod@amd.com>

Thanks
Sairaj
Re: [PATCH] hw/i386/amd_iommu: Mask address when calculating large PTE page size
Posted by Alejandro Jimenez 2 weeks, 3 days ago
nit: For consistency, please use "amd_iommu" as the prefix instead of the
full "hw/i386/amd_iommu" path.

On 11/5/26 5:58 AM, Peter Maydell wrote:
> 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
> 
> 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).
> 

Fixes: a74bb3110a5b ("amd_iommu: Add helpers to walk AMD v1 Page Table format")

> Cc: qemu-stable@nongnu.org
> 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>

> ---
> Side note: we don't seem to enforce the spec's requirement that
> the page size must be larger than the default for the level
> and smaller than the default for the next level up, but that's
> a different bug.

ACK, will work on a follow up to address those types of issues.

Thank you,
Alejandro

> ---
>  hw/i386/amd_iommu.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
> index 948d8611b15..008e882724c 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))
> -- 
> 2.43.0
>