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

From: Kalra, Ashish

Date: Mon Nov 23 2026 - 19:22:20 EST


Hello Boris,

On 11/23/2026 5:43 PM, Borislav Petkov wrote:
> On Mon, Nov 16, 2026 at 11:03:35PM +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.
>
> So, before that patch from Nikunj, __sev_get_ghcb() would return the per-CPU
> GHCBs which are not initialized. That was fine for the kexec case - I don't
> understand the bogus VMGEXIT thing - but apparently it worked there.
>
> For Nikunj, though, that didn't work because FRED can't have early #VCs with
> those uninitialized GHCBs so he falls back to the boot_ghcb.
>
> Now, however, boot_ghcb is already cleared and DR7 writes cause #VCs.
>
> So something about the lifetime of those things is still not good enough.
>
> So it looks like this, AFAICT:
>
> ---
> cpu_hotplug_enable();
> pr_notice("Starting new kernel\n");
> machine_shutdown(); <--- destroy GHCBs
> }
>
> kmsg_dump(KMSG_DUMP_SHUTDOWN);
> machine_kexec(kexec_image); <--- DR7 write needing a GHCB
> ---

Right, the window is: snp_kexec_finish() tears down the GHCBs in machine_shutdown(), and machine_kexec() still executes instructions that #VC.

On the "bogus VMGEXIT": before Nikunj's change, __sev_get_ghcb() returned the per-CPU GHCB, which snp_kexec_finish() had already converted back to private. The host mapped that GPA, read ciphertext (exit code 0) and returned GHCB_ERR_MISSING_INPUT, but couldn't write the result into the private page, so the guest saw its own stale contents, treated the call as successful and moved on. The DR7 write was simply dropped. It worked by accident.

>
> And snp_kexec_finish() does this:
>
> /*
> * Switch to using the MSR protocol to change per-CPU GHCBs to
> * private. All the per-CPU GHCBs have been switched back to private,
> * so can't do any more GHCB calls to the hypervisor beyond this point
> * until the kexec'ed kernel starts running.
> */
> boot_ghcb = NULL;
> sev_cfg.ghcbs_initialized = false;
>
> So here we've switched to the MSR protocol.>
> And the MSR protocol is supposed to be valid at any time, exactly for cases
> like that.
>
> And we do have a rudimentary do_vc_no_ghcb() for when no GHCB has been
> configured yet. After all, that's the point of the MSR protocol, no?
>
> So, long story short, how about switching to that no_ghcb #VC handler instead
> and dealing with the DR7 VC exception that way and actually using that MSR
> protocol for what it has been designed?

As for using do_vc_no_ghcb(): the GHCB MSR protocol requests are meant for specific guest-initiated operations (protocol negotiation, GHCB registration, page state changes, termination, etc.). The only one that corresponds to an intercepted instruction is CPUID, which is why do_vc_no_ghcb() handles only the CPUID exit code and terminates the guest for anything else. There's no MSR-protocol equivalent of a DR7 write, so the no-GHCB #VC handler can't handle it.

Thanks,
Ashish

>
> Instead of piling more cards on the already unstable house of cards?
>
> Hmmm.
>