public inbox for linux-coco@lists.linux.dev
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Kai Huang <kai.huang@intel.com>
Cc: "pbonzini@redhat.com" <pbonzini@redhat.com>,
	"kas@kernel.org" <kas@kernel.org>,
	 "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	 "linux-coco@lists.linux.dev" <linux-coco@lists.linux.dev>,
	Chang Seok Bae <chang.seok.bae@intel.com>,
	 "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"x86@kernel.org" <x86@kernel.org>
Subject: Re: [PATCH v2 5/6] KVM: x86: Track available/dirty register masks as "unsigned long" values
Date: Mon, 13 Apr 2026 07:54:36 -0700	[thread overview]
Message-ID: <ad0DrDUsUKTMfrDW@google.com> (raw)
In-Reply-To: <e6e82905e6b9b1637ccb640097e67d21793f5895.camel@intel.com>

On Mon, Apr 13, 2026, Kai Huang wrote:
> On Thu, 2026-04-09 at 15:42 -0700, Sean Christopherson wrote:
> > -#define TDX_REGS_AVAIL_SET	(BIT_ULL(VCPU_REG_EXIT_INFO_1) | \
> > -				 BIT_ULL(VCPU_REG_EXIT_INFO_2) | \
> > -				 BIT_ULL(VCPU_REGS_RAX) | \
> > -				 BIT_ULL(VCPU_REGS_RBX) | \
> > -				 BIT_ULL(VCPU_REGS_RCX) | \
> > -				 BIT_ULL(VCPU_REGS_RDX) | \
> > -				 BIT_ULL(VCPU_REGS_RBP) | \
> > -				 BIT_ULL(VCPU_REGS_RSI) | \
> > -				 BIT_ULL(VCPU_REGS_RDI) | \
> > -				 BIT_ULL(VCPU_REGS_R8) | \
> > -				 BIT_ULL(VCPU_REGS_R9) | \
> > -				 BIT_ULL(VCPU_REGS_R10) | \
> > -				 BIT_ULL(VCPU_REGS_R11) | \
> > -				 BIT_ULL(VCPU_REGS_R12) | \
> > -				 BIT_ULL(VCPU_REGS_R13) | \
> > -				 BIT_ULL(VCPU_REGS_R14) | \
> > -				 BIT_ULL(VCPU_REGS_R15))
> > +#define TDX_REGS_AVAIL_SET	(BIT(VCPU_REG_EXIT_INFO_1) | \
> > +				 BIT(VCPU_REG_EXIT_INFO_2) | \
> > +				 BIT(VCPU_REGS_RAX) | \
> > +				 BIT(VCPU_REGS_RBX) | \
> > +				 BIT(VCPU_REGS_RCX) | \
> > +				 BIT(VCPU_REGS_RDX) | \
> > +				 BIT(VCPU_REGS_RBP) | \
> > +				 BIT(VCPU_REGS_RSI) | \
> > +				 BIT(VCPU_REGS_RDI) | \
> > +				 BIT(VCPU_REGS_R8) | \
> > +				 BIT(VCPU_REGS_R9) | \
> > +				 BIT(VCPU_REGS_R10) | \
> > +				 BIT(VCPU_REGS_R11) | \
> > +				 BIT(VCPU_REGS_R12) | \
> > +				 BIT(VCPU_REGS_R13) | \
> > +				 BIT(VCPU_REGS_R14) | \
> > +				 BIT(VCPU_REGS_R15))
> >  
> 
> Not related to this series, but this made me look into whether these
> registers are truly needed to be set as available for TDX.
> 
> Firstly, all the listed registers are marked as available immediately after
> exiting from tdh_vp_enter(), but except VCPU_REG_EXIT_INFO_1 and
> VCPU_REG_EXIT_INFO_2 are immediately saved to the common 'struct vcpu_vt',
> all other GPRs are not saved to vcpu->arch.regs[], which means marking GPRs
> available immediately doesn't quite make sense.
> 
> In fact, IIUC other than when the TD exits with TDVMCALL on which TD shares
> couple of GPRs with KVM, KVM has no way to get TD's GPRs.  So perhaps it
> makes more sense is to mark the shared GPRs available upon TDVMCALL.
> 
> But even that does not make sense from KVM's "GPR available" perspective,
> because TDVMCALL has a different ABI from KVM's existing infrastructure for
> e.g., CPUID/MSR emulation.  E.g.,  KVM uses RCX/RAX/RDX for MSR emulation,
> but TDVMCALL<MSR.WRITE> uses R12 and R13 to convey MSR index/value:
> 
>         case EXIT_REASON_MSR_WRITE:                 
>                 kvm_rcx_write(vcpu, tdx->vp_enter_args.r12);         
>                 kvm_rax_write(vcpu, tdx->vp_enter_args.r13 & -1u);   
>                 kvm_rdx_write(vcpu, tdx->vp_enter_args.r13 >> 32);
> 
> So I think the most accurate way is to explicitly mark the relevant GPRs
> available for each type of TDVMCALL. I am not sure whether it's worth to do
> though, because AFAICT there's no real bug in the existing code, other than
> "marking GPRs not in vcpu->arch.regs[] as available looks wrong".
> 
> A less invasive way is to mark all possible GPRs that can be used in
> TDVMCALL emulation available once after TD exits.  AFAICT the KVM hypercall
> uses most GPRs (RAX/RBX/RCX/RDX/RSI) and all other TDVMCALLs only use a
> subset, so maybe we can remove other GPRs from the available list (the diff
> in [*] passed my test of booting/destroying TD).
> 
> Bug again, not sure whether it's worth doing.

Not worth doing.  Because VMX and SVM make all GRPs available immediately, except
for RSP, KVM ignores avail/dirty for GPRs.  I.e. "fixing" TDX will just shift the
"bugs" elsewhere.

More importantly, because the TDX-Module *requires* RCX (the GPR that holds the
mask of registers to expose to the VMM) to be hidden on TDVMCALL, KVM *can't*
do any kind of meaningful "available" tracking.  Versus sev_es_validate_vmgexit(),
which can at least sanity check that the registers needed to service a hypercall
have valid data.

So unfortunately, since we need to rely on testing to verify KVM's implementation
no matter what, I don't think it'd be a net positive to overhaul KVM's handling
of GPRs to support SEV-ES+'s and TDX's "sometimes available" GPR set.

  reply	other threads:[~2026-04-13 14:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-09 22:42 [PATCH v2 0/6] KVM: x86: Reg cleanups / prep work for APX Sean Christopherson
2026-04-09 22:42 ` [PATCH v2 1/6] KVM: x86: Add dedicated storage for guest RIP Sean Christopherson
2026-04-10 18:43   ` Chang S. Bae
2026-04-09 22:42 ` [PATCH v2 2/6] KVM: x86: Drop the "EX" part of "EXREG" to avoid collision with APX Sean Christopherson
2026-04-13 11:23   ` Huang, Kai
2026-04-09 22:42 ` [PATCH v2 3/6] KVM: nVMX: Do a bitwise-AND of regs_avail when switching active VMCS Sean Christopherson
2026-04-09 22:42 ` [PATCH v2 4/6] KVM: x86: Add wrapper APIs to reset dirty/available register masks Sean Christopherson
2026-04-09 22:42 ` [PATCH v2 5/6] KVM: x86: Track available/dirty register masks as "unsigned long" values Sean Christopherson
2026-04-13 11:24   ` Huang, Kai
2026-04-13 11:28   ` Huang, Kai
2026-04-13 14:54     ` Sean Christopherson [this message]
2026-04-09 22:42 ` [PATCH v2 6/6] KVM: x86: Use a proper bitmap for tracking available/dirty registers Sean Christopherson
2026-04-13 11:31 ` [PATCH v2 0/6] KVM: x86: Reg cleanups / prep work for APX Huang, Kai

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=ad0DrDUsUKTMfrDW@google.com \
    --to=seanjc@google.com \
    --cc=chang.seok.bae@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kas@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=x86@kernel.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