From: Richard Henderson <richard.henderson@linaro.org>
To: Michael Brown <mcb30@ipxe.org>, qemu-devel@nongnu.org
Cc: qemu-stable@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>,
Eduardo Habkost <eduardo@habkost.net>
Subject: Re: [PATCH] target/i386: Fix physical address truncation when PAE is enabled
Date: Wed, 20 Dec 2023 15:22:53 +1100 [thread overview]
Message-ID: <49047cf7-de12-4761-9082-dc5638d249a2@linaro.org> (raw)
In-Reply-To: <0102018c7d0026c1-fd5f0b50-48fd-4552-be0a-cbb6070b5e14-000000@eu-west-1.amazonses.com>
On 12/18/23 23:56, Michael Brown wrote:
> The address translation logic in get_physical_address() will currently
> truncate physical addresses to 32 bits unless long mode is enabled.
> This is incorrect when using physical address extensions (PAE) outside
> of long mode, with the result that a 32-bit operating system using PAE
> to access memory above 4G will experience undefined behaviour.
>
> The truncation code was originally introduced in commit 33dfdb5 ("x86:
> only allow real mode to access 32bit without LMA"), where it applied
> only to translations performed while paging is disabled (and so cannot
> affect guests using PAE).
>
> Commit 9828198 ("target/i386: Add MMU_PHYS_IDX and MMU_NESTED_IDX")
> rearranged the code such that the truncation also applied to the use
> of MMU_PHYS_IDX and MMU_NESTED_IDX. Commit 4a1e9d4 ("target/i386: Use
> atomic operations for pte updates") brought this truncation into scope
> for page table entry accesses, and is the first commit for which a
> Windows 10 32-bit guest will reliably fail to boot if memory above 4G
> is present.
>
> Fix by testing for PAE being enabled via the relevant bit in CR4,
> instead of testing for long mode being enabled. PAE must be enabled
> as a prerequisite of long mode, and so this is a generalisation of the
> current test.
>
> Remove the #ifdef TARGET_X86_64 check since PAE exists in both 32-bit
> and 64-bit processors, and both should exhibit the same truncation
> behaviour when PAE is disabled.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2040
> Signed-off-by: Michael Brown <mcb30@ipxe.org>
> ---
> target/i386/tcg/sysemu/excp_helper.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/target/i386/tcg/sysemu/excp_helper.c b/target/i386/tcg/sysemu/excp_helper.c
> index 5b86f439ad..3d0d0d78d7 100644
> --- a/target/i386/tcg/sysemu/excp_helper.c
> +++ b/target/i386/tcg/sysemu/excp_helper.c
> @@ -582,12 +582,10 @@ static bool get_physical_address(CPUX86State *env, vaddr addr,
>
> /* Translation disabled. */
> out->paddr = addr & x86_get_a20_mask(env);
> -#ifdef TARGET_X86_64
> - if (!(env->hflags & HF_LMA_MASK)) {
> - /* Without long mode we can only address 32bits in real mode */
> + if (!(env->cr[4] & CR4_PAE_MASK)) {
> + /* Without PAE we can address only 32 bits */
> out->paddr = (uint32_t)out->paddr;
> }
> -#endif
This is not the correct refactoring.
I agree that what we're currently doing is wrong, esp for MMU_PHYS_IDX, but for the
default case, if CR0.PG == 0, then CR4.PAE is ignored (vol 3, section 4.1.1).
I suspect the correct fix is to have MMU_PHYS_IDX pass through the input address
unchanged, and it is the responsibility of the higher level paging mmu_idx to truncate
physical addresses per PG_MODE_* before recursing.
r~
r~
> out->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
> out->page_size = TARGET_PAGE_SIZE;
> return true;
next prev parent reply other threads:[~2023-12-20 4:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-18 12:56 [PATCH] target/i386: Fix physical address truncation when PAE is enabled Michael Brown
2023-12-20 4:22 ` Richard Henderson [this message]
2023-12-20 11:03 ` Michael Brown
2023-12-20 21:51 ` Richard Henderson
2023-12-21 15:20 ` Michael Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=49047cf7-de12-4761-9082-dc5638d249a2@linaro.org \
--to=richard.henderson@linaro.org \
--cc=eduardo@habkost.net \
--cc=mcb30@ipxe.org \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-stable@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).