* [PATCH v4] x86/sev: Handle DR7 accesses in the #VC handler after GHCB teardown
@ 2026-12-01 21:00 Ashish Kalra
2026-12-02 12:46 ` Aithal, Srikanth
0 siblings, 1 reply; 2+ messages in thread
From: Ashish Kalra @ 2026-12-01 21:00 UTC (permalink / raw)
To: tglx, mingo, bp, dave.hansen, x86, hpa, thomas.lendacky, nikunj,
Srikanth.Aithal
Cc: kvm, linux-coco, linux-kernel
From: Ashish Kalra <ashish.kalra@amd.com>
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 is
normally 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, set boot_ghcb to NULL and cleared
sev_cfg.ghcbs_initialized. The DR7 write therefore raises a #VC that
dereferences a NULL GHCB pointer and the kexec fails.
Before commit 3645eb7e3915 ("x86/fred: Fix early boot failures on
SEV-ES/SNP guests"), __sev_get_ghcb() returned the per-CPU GHCB in this
window. That page had already been converted back to private, so the
hypervisor could not read the request nor write a response, and the
guest treated its own stale GHCB contents as a successful reply. The DR7
write was silently dropped and the kexec only worked by accident. Since
that commit, __sev_get_ghcb() returns the now NULL boot_ghcb instead.
The GHCB MSR protocol cannot help here: apart from CPUID, it has no
request that corresponds to an intercepted instruction, so a DR7 access
cannot be forwarded to the hypervisor without a GHCB.
Instead, once the GHCBs have been torn down, handle the #VC without a
GHCB: verify that it is a genuine DR7 access and complete it locally,
returning the reset value for reads and dropping writes. This is safe
because, without DebugSwap, the hypervisor only tracks the DR7 value and
cannot load it into the encrypted VMSA, and the kexec'ed kernel writes
DR7 again in cpu_init(). Only DR7 accesses are currently handled this
way; any other #VC in that window terminates the guest with a new,
specific reason code instead of dereferencing a NULL GHCB.
Fixes: 3074152e56c9 ("x86/sev: Convert shared memory back to private on kexec")
Cc: stable@vger.kernel.org
Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
---
v4:
- Check that the instruction really is a DR7 access with
vc_check_opcode_bytes(). (Tom, Sashiko AI review)
- Terminate if DebugSwap is enabled, as DR7 accesses are not
intercepted then. (Sashiko AI review)
- Handle the NULL GHCB returned by __sev_get_ghcb() instead of
open-coding the teardown check before it. (Tom)
- Rename vc_handle_exception_no_ghcb() to vc_handle_no_ghcb() and
make it return bool; vc_no_ghcb is already the early-boot #VC
entry point. (Tom)
- Rename GHCB_TERM_NO_GHCB to GHCB_TERM_VC_NO_GHCB. (Tom)
v3:
- Handle the DR7 access in the #VC handler once the GHCBs have been
torn down (decode it and complete it locally) instead of skipping
the DR7 write at the hw_breakpoint_disable() call site. Any other
#VC in that window terminates the guest with a new reason code.
(Boris, Nikunj)
- Drop the Suggested-by/Reported-by/Tested-by tags since the fix was
reworked.
v2:
- Move the check into hw_breakpoint_disable() through a new
snp_hw_breakpoint_disable() helper.
- Drop CC_ATTR_GUEST_DEBUG_VIRT and read DebugSwap from sev_status.
arch/x86/coco/sev/vc-handle.c | 35 +++++++++++++++++++++++++++++++
arch/x86/include/asm/sev-common.h | 1 +
2 files changed, 36 insertions(+)
diff --git a/arch/x86/coco/sev/vc-handle.c b/arch/x86/coco/sev/vc-handle.c
index 954df836ac65..a233ea5c867e 100644
--- a/arch/x86/coco/sev/vc-handle.c
+++ b/arch/x86/coco/sev/vc-handle.c
@@ -873,6 +873,39 @@ static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
}
+static bool vc_handle_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
+{
+ struct es_em_ctxt ctxt;
+ long *reg;
+
+ /* Without a GHCB, only DR7 accesses are currently handled */
+ if (exit_code != SVM_EXIT_READ_DR7 && exit_code != SVM_EXIT_WRITE_DR7)
+ goto fail;
+
+ if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
+ goto fail;
+
+ if (vc_init_em_ctxt(&ctxt, regs, exit_code) != ES_OK)
+ goto fail;
+
+ if (vc_check_opcode_bytes(&ctxt, exit_code) != ES_OK)
+ goto fail;
+
+ reg = vc_insn_get_rm(&ctxt);
+ if (!reg)
+ goto fail;
+
+ if (exit_code == SVM_EXIT_READ_DR7)
+ *reg = DR7_RESET_VALUE;
+
+ vc_finish_insn(&ctxt);
+
+ return true;
+
+fail:
+ sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_NO_GHCB);
+}
+
static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
{
struct ghcb_state state;
@@ -882,6 +915,8 @@ static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_co
bool ret = true;
ghcb = __sev_get_ghcb(&state);
+ if (!ghcb)
+ return vc_handle_no_ghcb(regs, error_code);
vc_ghcb_invalidate(ghcb);
result = vc_init_em_ctxt(&ctxt, regs, error_code);
diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
index 01a6e4dbe423..b0a64a49875c 100644
--- a/arch/x86/include/asm/sev-common.h
+++ b/arch/x86/include/asm/sev-common.h
@@ -209,6 +209,7 @@ struct snp_psc_desc {
#define GHCB_TERM_SECURE_TSC 10 /* Secure TSC initialization failed */
#define GHCB_TERM_SVSM_CA_REMAP_FAIL 11 /* SVSM is present but CA could not be remapped */
#define GHCB_TERM_SAVIC_FAIL 12 /* Secure AVIC-specific failure */
+#define GHCB_TERM_VC_NO_GHCB 13 /* #VC that can't be handled without a GHCB */
#define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK)
base-commit: 75b8262a97abcb972ef897dad5f94d425157b635
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v4] x86/sev: Handle DR7 accesses in the #VC handler after GHCB teardown
2026-12-01 21:00 [PATCH v4] x86/sev: Handle DR7 accesses in the #VC handler after GHCB teardown Ashish Kalra
@ 2026-12-02 12:46 ` Aithal, Srikanth
0 siblings, 0 replies; 2+ messages in thread
From: Aithal, Srikanth @ 2026-12-02 12:46 UTC (permalink / raw)
To: Ashish Kalra, tglx, mingo, bp, dave.hansen, x86, hpa,
thomas.lendacky, nikunj
Cc: kvm, linux-coco, linux-kernel
On 12/2/2026 2:30 AM, Ashish Kalra wrote:
> From: Ashish Kalra <ashish.kalra@amd.com>
>
> 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 is
> normally 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, set boot_ghcb to NULL and cleared
> sev_cfg.ghcbs_initialized. The DR7 write therefore raises a #VC that
> dereferences a NULL GHCB pointer and the kexec fails.
>
> Before commit 3645eb7e3915 ("x86/fred: Fix early boot failures on
> SEV-ES/SNP guests"), __sev_get_ghcb() returned the per-CPU GHCB in this
> window. That page had already been converted back to private, so the
> hypervisor could not read the request nor write a response, and the
> guest treated its own stale GHCB contents as a successful reply. The DR7
> write was silently dropped and the kexec only worked by accident. Since
> that commit, __sev_get_ghcb() returns the now NULL boot_ghcb instead.
>
> The GHCB MSR protocol cannot help here: apart from CPUID, it has no
> request that corresponds to an intercepted instruction, so a DR7 access
> cannot be forwarded to the hypervisor without a GHCB.
>
> Instead, once the GHCBs have been torn down, handle the #VC without a
> GHCB: verify that it is a genuine DR7 access and complete it locally,
> returning the reset value for reads and dropping writes. This is safe
> because, without DebugSwap, the hypervisor only tracks the DR7 value and
> cannot load it into the encrypted VMSA, and the kexec'ed kernel writes
> DR7 again in cpu_init(). Only DR7 accesses are currently handled this
> way; any other #VC in that window terminates the guest with a new,
> specific reason code instead of dereferencing a NULL GHCB.
>
> Fixes: 3074152e56c9 ("x86/sev: Convert shared memory back to private on kexec")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
> ---
> v4:
> - Check that the instruction really is a DR7 access with
> vc_check_opcode_bytes(). (Tom, Sashiko AI review)
> - Terminate if DebugSwap is enabled, as DR7 accesses are not
> intercepted then. (Sashiko AI review)
> - Handle the NULL GHCB returned by __sev_get_ghcb() instead of
> open-coding the teardown check before it. (Tom)
> - Rename vc_handle_exception_no_ghcb() to vc_handle_no_ghcb() and
> make it return bool; vc_no_ghcb is already the early-boot #VC
> entry point. (Tom)
> - Rename GHCB_TERM_NO_GHCB to GHCB_TERM_VC_NO_GHCB. (Tom)
>
> v3:
> - Handle the DR7 access in the #VC handler once the GHCBs have been
> torn down (decode it and complete it locally) instead of skipping
> the DR7 write at the hw_breakpoint_disable() call site. Any other
> #VC in that window terminates the guest with a new reason code.
> (Boris, Nikunj)
> - Drop the Suggested-by/Reported-by/Tested-by tags since the fix was
> reworked.
>
> v2:
> - Move the check into hw_breakpoint_disable() through a new
> snp_hw_breakpoint_disable() helper.
> - Drop CC_ATTR_GUEST_DEBUG_VIRT and read DebugSwap from sev_status.
>
> arch/x86/coco/sev/vc-handle.c | 35 +++++++++++++++++++++++++++++++
> arch/x86/include/asm/sev-common.h | 1 +
> 2 files changed, 36 insertions(+)
>
> diff --git a/arch/x86/coco/sev/vc-handle.c b/arch/x86/coco/sev/vc-handle.c
> index 954df836ac65..a233ea5c867e 100644
> --- a/arch/x86/coco/sev/vc-handle.c
> +++ b/arch/x86/coco/sev/vc-handle.c
> @@ -873,6 +873,39 @@ static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
> return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
> }
>
> +static bool vc_handle_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
> +{
> + struct es_em_ctxt ctxt;
> + long *reg;
> +
> + /* Without a GHCB, only DR7 accesses are currently handled */
> + if (exit_code != SVM_EXIT_READ_DR7 && exit_code != SVM_EXIT_WRITE_DR7)
> + goto fail;
> +
> + if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
> + goto fail;
> +
> + if (vc_init_em_ctxt(&ctxt, regs, exit_code) != ES_OK)
> + goto fail;
> +
> + if (vc_check_opcode_bytes(&ctxt, exit_code) != ES_OK)
> + goto fail;
> +
> + reg = vc_insn_get_rm(&ctxt);
> + if (!reg)
> + goto fail;
> +
> + if (exit_code == SVM_EXIT_READ_DR7)
> + *reg = DR7_RESET_VALUE;
> +
> + vc_finish_insn(&ctxt);
> +
> + return true;
> +
> +fail:
> + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_NO_GHCB);
> +}
> +
> static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
> {
> struct ghcb_state state;
> @@ -882,6 +915,8 @@ static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_co
> bool ret = true;
>
> ghcb = __sev_get_ghcb(&state);
> + if (!ghcb)
> + return vc_handle_no_ghcb(regs, error_code);
>
> vc_ghcb_invalidate(ghcb);
> result = vc_init_em_ctxt(&ctxt, regs, error_code);
> diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
> index 01a6e4dbe423..b0a64a49875c 100644
> --- a/arch/x86/include/asm/sev-common.h
> +++ b/arch/x86/include/asm/sev-common.h
> @@ -209,6 +209,7 @@ struct snp_psc_desc {
> #define GHCB_TERM_SECURE_TSC 10 /* Secure TSC initialization failed */
> #define GHCB_TERM_SVSM_CA_REMAP_FAIL 11 /* SVSM is present but CA could not be remapped */
> #define GHCB_TERM_SAVIC_FAIL 12 /* Secure AVIC-specific failure */
> +#define GHCB_TERM_VC_NO_GHCB 13 /* #VC that can't be handled without a GHCB */
>
> #define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK)
>
>
> base-commit: 75b8262a97abcb972ef897dad5f94d425157b635
Tested v4 on top of 7.4.0-rc5[fe6ff8d]; kexec and kdump works fine with
this patch.
Reported-by: Srikanth Aithal <Srikanth.Aithal@amd.com>
Tested-by: Srikanth Aithal <Srikanth.Aithal@amd.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 12:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 21:00 [PATCH v4] x86/sev: Handle DR7 accesses in the #VC handler after GHCB teardown Ashish Kalra
2026-12-02 12:46 ` Aithal, Srikanth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox