Re: [PATCH] x86/sev: Skip DR7 write during kexec when it would trigger an unserviceable #VC

From: Borislav Petkov

Date: Fri Nov 13 2026 - 19:07:13 EST


On Fri, Aug 21, 2026 at 08:14:32PM +0000, Ashish Kalra wrote:
> From: Ashish Kalra <ashish.kalra@xxxxxxx>
>
> machine_kexec() calls hw_breakpoint_disable(), which writes DR7. On
> SEV-ES/SNP guests that do not have DebugSwap enabled, DR7 accesses are
> intercepted and delivered to the guest as a #VC exception, which must be
> serviced through the GHCB.
>
> By the time machine_kexec() runs, snp_kexec_finish() (called from
> native_machine_shutdown()) has already converted the per-CPU GHCBs and
> the boot GHCB back to private and set boot_ghcb to NULL, tearing down the
> GHCB infrastructure. The DR7 write therefore raises a #VC that
> dereferences a NULL GHCB pointer, causing a page fault and the kexec to
> fail.
>
> The NULL boot_ghcb pointer is a result of commit 3645eb7e3915 ("x86/fred:
> Fix early boot failures on SEV-ES/SNP guests"): that change makes
> __sev_get_ghcb() return the now-NULL boot_ghcb in this window, turning
> the previously-benign teardown-window #VC (a bogus VMGEXIT the host
> rejected but survived) into a NULL dereference.
>
> Skip the DR7 write in this case. Its value is not needed across kexec
> because the new kernel re-initializes DR7. Guests with DebugSwap enabled
> context-switch DR7 in hardware and do not intercept the write, and
> non-encrypted guests are unaffected, so both continue to call
> hw_breakpoint_disable() as before.
>
> Add a CC_ATTR_GUEST_DEBUG_VIRT confidential-computing attribute, so the
> debug-register-virtualization state (AMD DebugSwap) is queried through
> cc_platform_has().

Do not explain the patch in your commit message.

> diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
> index c3f4a389992d..60d25ceada03 100644
> --- a/arch/x86/kernel/machine_kexec_64.c
> +++ b/arch/x86/kernel/machine_kexec_64.c
> @@ -398,7 +398,16 @@ void __nocfi machine_kexec(struct kimage *image)
>
> /* Interrupts aren't acceptable while we reboot */
> local_irq_disable();
> - hw_breakpoint_disable();
> +
> + /*
> + * On SEV-ES/SEV-SNP guests without debug virtualization enabled, DR7
> + * writes are intercepted and generate a #VC. The GHCBs have already
> + * been torn down at this point so the #VC cannot be handled. Skip the
> + * DR7 write as the new kernel re-initializes DR7 during boot.
> + */
> + if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT) ||
> + cc_platform_has(CC_ATTR_GUEST_DEBUG_VIRT))
> + hw_breakpoint_disable();
> cet_disable();

hw_breakpoint_disable() does

if (snp_hw_breakpoint_disable())
return;

and this way all SNP-specific code is contained in sev.c or so.

Not the other way around.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette