public inbox for linux-coco@lists.linux.dev
 help / color / mirror / Atom feed
From: "Huang, Kai" <kai.huang@intel.com>
To: "seanjc@google.com" <seanjc@google.com>
Cc: "Bae, Chang Seok" <chang.seok.bae@intel.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"kas@kernel.org" <kas@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-coco@lists.linux.dev" <linux-coco@lists.linux.dev>,
	"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 23:03:59 +0000	[thread overview]
Message-ID: <d6f05e5fa781ccb465d27a4fb1c7c1ac1e9e95ff.camel@intel.com> (raw)
In-Reply-To: <ad0DrDUsUKTMfrDW@google.com>

On Mon, 2026-04-13 at 07:54 -0700, Sean Christopherson wrote:
> 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.  
> 

Fine to me. :-)

> 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.

Just want to understand:

I thought the fix could be we simply remove the wrong GPRs from the list. 
Not sure how fixing TDX will shift 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.  
> 

Hmm I think RCX conveys the shared GPRs and VMM can read.  Per "Table 5.323:
TDH.VP.ENTER Output Operands Format #5 Definition: On TDCALL(TDG.VP.VMCALL)
Following a TD Entry":

  RCX   ...
	Bit(s) Name         Description

	31:0   PARAMS_MASK  Value as passed into TDCALL(TDG.VP.VMCALL) by
			    the guest TD: indicates which part of the guest
			    TD GPR and XMM state is passed as-is to the
VMM 
			    and back. For details, see the description of
			    TDG.VP.VMCALL in 5.5.26.

I think the problem is, as said previously, currently KVM TDX code uses
KVM's existing infrastructure to emulate MSR, KVM hypercall etc,  but
TDVMCALL has a different ABI, thus there's a mismatch here.

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

Thread overview: 21+ 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-14 12:31   ` Xiaoyao Li
2026-04-14 13:59     ` Chang S. Bae
2026-04-14 15:37       ` Sean Christopherson
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
2026-04-13 23:03       ` Huang, Kai [this message]
2026-04-14  2:12         ` Xiaoyao Li
2026-04-14 14:04           ` Sean Christopherson
2026-04-14 15:48         ` Sean Christopherson
2026-04-14 22:21           ` Huang, Kai
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=d6f05e5fa781ccb465d27a4fb1c7c1ac1e9e95ff.camel@intel.com \
    --to=kai.huang@intel.com \
    --cc=chang.seok.bae@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=seanjc@google.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