target/i386/tcg/system/misc_helper.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Guest MOV CR0 under x86 TCG can complete without the architecturally
required #GP(0) for three invalid writes:
1. Setting any bit in CR0[63:32] from 64-bit code. The operand reaches
cpu_x86_update_cr0() through a uint32_t parameter, silently discarding
these bits.
2. Writing CR0.PG=1 with CR0.PE=0.
3. Clearing CR0.PG while executing in 64-bit code (CS.L=1).
This patch addresses a specific CR0 subset of the broader CR0/CR4
validation problem reported in:
https://gitlab.com/qemu-project/qemu/-/issues/3294
That issue already identifies the lack of input validation in
cpu_x86_update_cr0() and cpu_x86_update_cr4(), explicitly including
the invalid PG=1 with PE=0 case handled here.
Implementation:
Adds nine lines in target/i386/tcg/system/misc_helper.c, inside the
CR0 case of HELPER(write_crN).
Three checks reject non-zero CR0[63:32], PG=1 with PE=0, and clearing
PG in 64-bit code. They trigger #GP(0) after the existing SVM selective
intercept and before the actual CR0 update.
- The high-half check is compiled only for TARGET_X86_64.
- Uses HF_CS64_MASK for the paging-clear condition. Checking LMA alone
would incorrectly reject a legitimate compatibility-mode exit from
long mode.
- Raises EXCP0D_GPF with error code zero and GETPC() before updating
architectural state, ensuring atomicity.
For example, MOV CR0, RAX that clears PG from 64-bit code now faults at
that instruction, leaving CR0/CR3/CR4/EFER and the source RAX unchanged.
Switching to compatibility mode before clearing PG still succeeds.
Architectural reference:
Intel 64 and IA-32 Architectures Software Developer's Manual, combined
volumes, 325462-093US, September 2026:
- MOV control-register instruction: combined-PDF pages 1321 and 1323.
- Volume 3A, section 5.1.2: combined-PDF page 3263.
Validation:
Base: 97c88c61d45dee6e1f9bc160a338048189846d28 (11.1.90)
Tested commit: 01ffa5267193c62e1100091e8f9c83b237303fa3
Host/build: native Windows, x86_64-softmmu TCG, MSYS2 UCRT64 toolchain.
- Invalid-write differential (102 cases): across max/Haswell configs
with 4/5-level paging, 102/102 probes now correctly raise #GP(0);
the baseline missed all 102. ROM SHA256 was compared to verify
identical inputs. Targets include a noncanonical-access #GP(0)
positive control, no-op CR0 writes and legal WP toggles, checking
error code zero, exact fault RIP, retained source RAX and unchanged
CR0/CR3/CR4/EFER.
- Legal-behavior differential (6 VM runs): all passed, covering
compatibility-mode MOV CR0,EAX with nonzero RAX[63:32], WP toggles,
legal compatibility-mode PG clear, legal PE clear after PG=0, and
normal long-mode entry/exit. Guest output and exit status matched.
- Project tests: 180 passed, 4 skipped, 1 failed. checkpatch.pl reported
zero errors and zero warnings. git diff --check passed.
The single Meson failure is netdev-socket (Broken pipe in the Unix
stream reconnect subtest), reproduced identically on the unmodified
baseline and on a clean separate checkout. It uses -M none -accel
qtest without guest CPU execution and is unrelated to this CR0 change.
Review boundaries:
Please review the fault conditions, compatibility-mode operand width,
fault atomicity and intercept ordering. Standalone i386-softmmu, nested
SVM, physical hardware/KVM and other invalid CR0 combinations remain
unverified.
Signed-off-by: NeuroSaki987 <neurosaki987@gmail.com>
---
target/i386/tcg/system/misc_helper.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/target/i386/tcg/system/misc_helper.c
b/target/i386/tcg/system/misc_helper.c
index a77da59..eebc4b3 100644
--- a/target/i386/tcg/system/misc_helper.c
+++ b/target/i386/tcg/system/misc_helper.c
@@ -87,6 +87,15 @@ void HELPER(write_crN)(CPUX86State *env, int reg,
target_ulong t0)
((env->cr[0] ^ t0) & ~(CR0_TS_MASK | CR0_MP_MASK))) {
cpu_vmexit(env, SVM_EXIT_CR0_SEL_WRITE, 0, GETPC());
}
+#ifdef TARGET_X86_64
+ if (t0 & ~0xffffffffULL) {
+ raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
+ }
+#endif
+ if (((t0 & CR0_PG_MASK) && !(t0 & CR0_PE_MASK)) ||
+ ((env->hflags & HF_CS64_MASK) && !(t0 & CR0_PG_MASK))) {
+ raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
+ }
cpu_x86_update_cr0(env, t0);
break;
case 3:
--
2.39.2
On 11/17/26 13:54, Neuro wrote:
> Guest MOV CR0 under x86 TCG can complete without the architecturally
> required #GP(0) for three invalid writes:
>
> 1. Setting any bit in CR0[63:32] from 64-bit code. The operand reaches
> cpu_x86_update_cr0() through a uint32_t parameter, silently discarding
> these bits.
> 2. Writing CR0.PG=1 with CR0.PE=0.
> 3. Clearing CR0.PG while executing in 64-bit code (CS.L=1).
>
> This patch addresses a specific CR0 subset of the broader CR0/CR4
> validation problem reported in:
> https://gitlab.com/qemu-project/qemu/-/issues/3294
>
> That issue already identifies the lack of input validation in
> cpu_x86_update_cr0() and cpu_x86_update_cr4(), explicitly including
> the invalid PG=1 with PE=0 case handled here.
>
> Implementation:
>
> Adds nine lines in target/i386/tcg/system/misc_helper.c, inside the
> CR0 case of HELPER(write_crN).
>
> Three checks reject non-zero CR0[63:32], PG=1 with PE=0, and clearing
> PG in 64-bit code. They trigger #GP(0) after the existing SVM selective
> intercept and before the actual CR0 update.
>
> - The high-half check is compiled only for TARGET_X86_64.
> - Uses HF_CS64_MASK for the paging-clear condition. Checking LMA alone
> would incorrectly reject a legitimate compatibility-mode exit from
> long mode.
> - Raises EXCP0D_GPF with error code zero and GETPC() before updating
> architectural state, ensuring atomicity.
>
> For example, MOV CR0, RAX that clears PG from 64-bit code now faults at
> that instruction, leaving CR0/CR3/CR4/EFER and the source RAX unchanged.
> Switching to compatibility mode before clearing PG still succeeds.
>
> Architectural reference:
>
> Intel 64 and IA-32 Architectures Software Developer's Manual, combined
> volumes, 325462-093US, September 2026:
> - MOV control-register instruction: combined-PDF pages 1321 and 1323.
> - Volume 3A, section 5.1.2: combined-PDF page 3263.
>
> Validation:
>
> Base: 97c88c61d45dee6e1f9bc160a338048189846d28 (11.1.90)
> Tested commit: 01ffa5267193c62e1100091e8f9c83b237303fa3
> Host/build: native Windows, x86_64-softmmu TCG, MSYS2 UCRT64 toolchain.
>
> - Invalid-write differential (102 cases): across max/Haswell configs
> with 4/5-level paging, 102/102 probes now correctly raise #GP(0);
> the baseline missed all 102. ROM SHA256 was compared to verify
> identical inputs. Targets include a noncanonical-access #GP(0)
> positive control, no-op CR0 writes and legal WP toggles, checking
> error code zero, exact fault RIP, retained source RAX and unchanged
> CR0/CR3/CR4/EFER.
> - Legal-behavior differential (6 VM runs): all passed, covering
> compatibility-mode MOV CR0,EAX with nonzero RAX[63:32], WP toggles,
> legal compatibility-mode PG clear, legal PE clear after PG=0, and
> normal long-mode entry/exit. Guest output and exit status matched.
> - Project tests: 180 passed, 4 skipped, 1 failed. checkpatch.pl reported
> zero errors and zero warnings. git diff --check passed.
> The single Meson failure is netdev-socket (Broken pipe in the Unix
> stream reconnect subtest), reproduced identically on the unmodified
> baseline and on a clean separate checkout. It uses -M none -accel
> qtest without guest CPU execution and is unrelated to this CR0 change.
>
> Review boundaries:
>
> Please review the fault conditions, compatibility-mode operand width,
> fault atomicity and intercept ordering. Standalone i386-softmmu, nested
> SVM, physical hardware/KVM and other invalid CR0 combinations remain
> unverified.
>
> Signed-off-by: NeuroSaki987 <neurosaki987@gmail.com>
It must have taken so long to write all this text...
Paolo
> ---
> target/i386/tcg/system/misc_helper.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/target/i386/tcg/system/misc_helper.c
> b/target/i386/tcg/system/misc_helper.c
> index a77da59..eebc4b3 100644
> --- a/target/i386/tcg/system/misc_helper.c
> +++ b/target/i386/tcg/system/misc_helper.c
> @@ -87,6 +87,15 @@ void HELPER(write_crN)(CPUX86State *env, int reg,
> target_ulong t0)
> ((env->cr[0] ^ t0) & ~(CR0_TS_MASK | CR0_MP_MASK))) {
> cpu_vmexit(env, SVM_EXIT_CR0_SEL_WRITE, 0, GETPC());
> }
> +#ifdef TARGET_X86_64
> + if (t0 & ~0xffffffffULL) {
> + raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
> + }
> +#endif
> + if (((t0 & CR0_PG_MASK) && !(t0 & CR0_PE_MASK)) ||
> + ((env->hflags & HF_CS64_MASK) && !(t0 & CR0_PG_MASK))) {
> + raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
> + }
> cpu_x86_update_cr0(env, t0);
> break;
> case 3:
> --
> 2.39.2
© 2016 - 2026 Red Hat, Inc.