All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Roth <michael.roth@amd.com>
To: Borislav Petkov <bp@alien8.de>
Cc: Brijesh Singh <brijesh.singh@amd.com>, <x86@kernel.org>,
	<linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
	<linux-efi@vger.kernel.org>,
	<platform-driver-x86@vger.kernel.org>,
	<linux-coco@lists.linux.dev>, <linux-mm@kvack.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Joerg Roedel <jroedel@suse.de>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	"Vitaly Kuznetsov" <vkuznets@redhat.com>,
	Jim Mattson <jmattson@google.com>,
	"Andy Lutomirski" <luto@kernel.org>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Sergio Lopez <slp@redhat.com>, Peter Gonda <pgonda@google.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>,
	David Rientjes <rientjes@google.com>,
	Dov Murik <dovmurik@linux.ibm.com>,
	Tobin Feldman-Fitzthum <tobin@ibm.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	"Kirill A . Shutemov" <kirill@shutemov.name>,
	Andi Kleen <ak@linux.intel.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
	<brijesh.ksingh@gmail.com>, <tony.luck@intel.com>,
	<marcorr@google.com>,
	<sathyanarayanan.kuppuswamy@linux.intel.com>
Subject: Re: [PATCH v9 31/43] x86/compressed/64: Add support for SEV-SNP CPUID table in #VC handlers
Date: Sat, 5 Feb 2022 09:42:43 -0600	[thread overview]
Message-ID: <20220205154243.s33gwghqwbb4efyl@amd.com> (raw)
In-Reply-To: <Yf5XScto3mDXnl9u@zn.tnic>

On Sat, Feb 05, 2022 at 11:54:01AM +0100, Borislav Petkov wrote:
> On Fri, Jan 28, 2022 at 11:17:52AM -0600, Brijesh Singh wrote:
> > +/*
> > + * Individual entries of the SEV-SNP CPUID table, as defined by the SEV-SNP
> > + * Firmware ABI, Revision 0.9, Section 7.1, Table 14.
> > + */
> > +struct snp_cpuid_fn {
> > +	u32 eax_in;
> > +	u32 ecx_in;
> > +	u64 xcr0_in;
> > +	u64 xss_in;
> 
> So what's the end result here:
> 
> -+	u64 __unused;
> -+	u64 __unused2;
> ++	u64 xcr0_in;
> ++	u64 xss_in;
> 
> those are not unused fields anymore but xcr0 and xss input values?
> 
> Looking at the FW abi doc, they're only mentioned in "Table 14.
> CPUID_FUNCTION Structure" that they're XCR0 and XSS at the time of the
> CPUID execution.
> 
> But those values are input values to what exactly, guest or firmware?

These are inputs to firmware, either through a MSG_CPUID_REQ guest
message that gets passed along by the HV to firmware for validation,
or, in this case, through SNP_LAUNCH_UPDATE when the HV passes the
initial CPUID table contents to firmware for validation before it
gets mapped into guest memory.

> 
> There's a typo in the FW doc, btw:
> 
> "The guest constructs an MSG_CPUID_REQ message as defined in Table 13.
> This message contains an array of CPUID function structures as defined
> in Table 13."
> 
> That second "Table" is 14 not 13.
> 
> So, if an array CPUID_FUNCTION[] is passed as part of an MSG_CPUID_REQ
> command, then, the two _IN variables contain what the guest received
> from the HV for XCR0 and XSS values. Which means, this is the guest
> asking the FW whether those values the HV gave the guest are kosher.
> 
> Am I close?

Most of that documentation is written in the context of the
MSG_CPUID_REQ guest message interface. In that case, a guest has been
provided a certain sets of CPUID values by KVM CPUID instruction
emulation (or potentially the CPUID table), and wants firmware to
validate whether all/some of them are valid for that particular
system/configuration.

In the case of 0xD subfunction 0x0 and 0x1, the CPUID leaf on real
hardware will change depending on what the guest sets in its actual
XCR0 control register (APM Volume 2 11.5.2) or IA32_XSS_MSR register,
whose combined bits are OR'd to form the XFEATURE_ENABLED_MASK.

CPUID leaf 0xD subfunctions 0x0 and 0x1 advertise the size of the
XSAVE area required for the features currently enabled in
XFEATURE_ENABLED_MASK via the EBX return value from the corresponding
CPUID instruction, so for firmware to validate whether that EBX value
is valid for a particular system/configuration, it needs to know
what the guest's currently XFEATURE_ENABLED_MASK is, so it needs to
know what the guest has set in its XCR0 and IA32_XSS_MSR registers.

That's where the XCR0_IN and XSS_IN inputs come into play: they are
inputs to the firmware so it can do the proper validation based on
the guest's current state / XFEATURE_ENABLED_MASK.

But that guest message interface will likely be deprecated at some
point in favor of the static CPUID table (SNP FW ABI 8.14.2.6),
since all the firmware validation has already been done in advance,
and any additional validation is redundant, so this series relies
purely on the CPUID table.

In the case of the CPUID table, we don't know what the guest's
XFEATURE_ENABLED_MASK is going to be at the time a CPUID instruction
is issued for 0xD:0x0,0xD,0x1, and those values will change as the
guest boots and begins enabling additional XSAVE features. The only
thing that's certain is that there needs to be at least one CPUID
table entry for 0xD:0x0 and 0xD:0x1 corresponding to the initial
XFEATURE_ENABLED_MASK, which will correspond to XCR0_IN=1/XSS_IN=0,
or XCR0_IN=3/XSS_IN=0 depending on the hypervisor/guest
implementation.

What to do about other combinations of XCR0_IN/XSS_IN gets a bit
weird, since encoding all those permutations would not scale well,
and alternative methods of trying to encode them in the table would
almost certainly result in lots of different incompatibile guest
implementations floating around.

So our recommendation has been for hypervisors to encode only the
0xD:0x0/0xD:0x1 entries corresponding to these initial guest states
in the CPUID table, and for guests to ignore the XCR0_IN/XSS_IN
values completely, and instead have the guest calculate the XSAVE
save area size dynamically (EBX). This has multiple benefits:

1) Guests that implement this approach would be compatible even
   with hypervisors that try to encode additional permutations of
   XCR0_IN/XSS_IN in the table based on their read of the spec
2) It is just as secure, since it requires only that the guest
   trust itself and the APM specification, which is the ultimate
   authority on what firmware would be validationa against
3) The other leaf registers, EAX/ECX/EDX are not dependent on
   XCR0_IN/XSS_IN/XFEATURE_ENABLED_MASK, and so any
   0xD:0x0/0xD:0x1 entry will do as the starting point for the
   calculation.

That's why in v8 these fields were documented as unused1/unused2.
But when you suggested that we should enforce 0 in that case, it
became clear we should adjust our recommendation to go ahead and
check for the specific XCR0_IN={1,3}/XSS_IN=0 values when
searching for the base 0xD:0x0/0xD:0x1 entry to use, because a
hypervisor that chooses to use XCR0_IN/XSS_IN is not out-of-spec,
and should be supported, and there isn't really any read of the
spec from the hypervisor perspective where XCR0_IN=0/XSS_IN=0
would be valid.

So for v9 I went ahead and added the XCR0_IN/XSS_IN checks, and
updated our recommendations (will send an updated version to
SNP mailing list by Monday) to not ignore them in the guest.

I added some comments in snp_cpuid_get_validated_func() and
snp_cpuid_calc_xsave_size() to clarify how XCR0_IN/XSS_IN are
being used there, but let me know if those need to be beefed up
a bit.

-Mike

  reply	other threads:[~2022-02-05 16:24 UTC|newest]

Thread overview: 115+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-28 17:17 [PATCH v9 00/43] Add AMD Secure Nested Paging (SEV-SNP) Guest Support Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 01/43] KVM: SVM: Define sev_features and vmpl field in the VMSA Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 02/43] KVM: SVM: Create a separate mapping for the SEV-ES save area Brijesh Singh
2022-02-01 13:02   ` Borislav Petkov
2022-02-09 15:02     ` Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 03/43] KVM: SVM: Create a separate mapping for the GHCB " Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 04/43] KVM: SVM: Update the SEV-ES save area mapping Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 05/43] x86/compressed/64: Detect/setup SEV/SME features earlier in boot Brijesh Singh
2022-02-01 18:08   ` Borislav Petkov
2022-02-01 20:35     ` Michael Roth
2022-02-01 21:28       ` Borislav Petkov
2022-02-02  0:52         ` Michael Roth
2022-02-02  6:09           ` Borislav Petkov
2022-02-02 17:28             ` Michael Roth
2022-02-02 18:57               ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 06/43] x86/sev: " Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 07/43] x86/mm: Extend cc_attr to include AMD SEV-SNP Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 08/43] x86/sev: Define the Linux specific guest termination reasons Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 09/43] x86/sev: Save the negotiated GHCB version Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 10/43] x86/sev: Check SEV-SNP features support Brijesh Singh
2022-02-01 19:59   ` Borislav Petkov
2022-02-02 14:28     ` Brijesh Singh
2022-02-02 15:37       ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 11/43] x86/sev: Add a helper for the PVALIDATE instruction Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 12/43] x86/sev: Check the vmpl level Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 13/43] x86/compressed: Add helper for validating pages in the decompression stage Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 14/43] x86/compressed: Register GHCB memory when SEV-SNP is active Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 15/43] x86/sev: " Brijesh Singh
2022-02-02 10:34   ` Borislav Petkov
2022-02-02 14:29     ` Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 16/43] x86/sev: Add helper for validating pages in early enc attribute changes Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 17/43] x86/kernel: Make the .bss..decrypted section shared in RMP table Brijesh Singh
2022-02-02 11:06   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 18/43] x86/kernel: Validate ROM memory before accessing when SEV-SNP is active Brijesh Singh
2022-02-02 15:41   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 19/43] x86/mm: Add support to validate memory when changing C-bit Brijesh Singh
2022-02-02 16:10   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 20/43] x86/sev: Use SEV-SNP AP creation to start secondary CPUs Brijesh Singh
2022-02-03  6:50   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 21/43] x86/head/64: Re-enable stack protection Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 22/43] x86/sev: Move MSR-based VMGEXITs for CPUID to helper Brijesh Singh
2022-02-03 13:59   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 23/43] KVM: x86: Move lookup of indexed CPUID leafs " Brijesh Singh
2022-02-03 15:16   ` Borislav Petkov
2022-02-03 16:44     ` Michael Roth
2022-02-05 12:58       ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 24/43] x86/compressed/acpi: Move EFI detection " Brijesh Singh
2022-02-03 14:39   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 25/43] x86/compressed/acpi: Move EFI system table lookup " Brijesh Singh
2022-02-03 14:48   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 26/43] x86/compressed/acpi: Move EFI config " Brijesh Singh
2022-02-03 15:13   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 27/43] x86/compressed/acpi: Move EFI vendor " Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 28/43] x86/compressed/acpi: Move EFI kexec handling into common code Brijesh Singh
2022-02-04 16:09   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 29/43] x86/boot: Add Confidential Computing type to setup_data Brijesh Singh
2022-02-04 16:21   ` Borislav Petkov
2022-02-04 17:41     ` Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 30/43] KVM: SEV: Add documentation for SEV-SNP CPUID Enforcement Brijesh Singh
2022-02-07 23:48   ` Sean Christopherson
2022-02-08 14:54     ` Michael Roth
2022-02-08 15:11     ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 31/43] x86/compressed/64: Add support for SEV-SNP CPUID table in #VC handlers Brijesh Singh
2022-02-05 10:54   ` Borislav Petkov
2022-02-05 15:42     ` Michael Roth [this message]
2022-02-05 16:22     ` Michael Roth
2022-02-06 13:37       ` Borislav Petkov
2022-02-07 15:37         ` Michael Roth
2022-02-07 17:52           ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 32/43] x86/boot: Add a pointer to Confidential Computing blob in bootparams Brijesh Singh
2022-02-05 13:07   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 33/43] x86/compressed: Add SEV-SNP feature detection/setup Brijesh Singh
2022-02-06 16:41   ` Borislav Petkov
2022-02-08 13:50     ` Michael Roth
2022-02-08 15:02       ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 34/43] x86/compressed: Use firmware-validated CPUID leaves for SEV-SNP guests Brijesh Singh
2022-01-28 17:17 ` [PATCH v9 35/43] x86/compressed: Export and rename add_identity_map() Brijesh Singh
2022-02-06 19:01   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 36/43] x86/compressed/64: Add identity mapping for Confidential Computing blob Brijesh Singh
2022-02-06 19:21   ` Borislav Petkov
2022-01-28 17:17 ` [PATCH v9 37/43] x86/sev: Add SEV-SNP feature detection/setup Brijesh Singh
2022-02-06 19:38   ` Borislav Petkov
2022-02-08  5:25     ` Michael Roth
2022-01-28 17:17 ` [PATCH v9 38/43] x86/sev: Use firmware-validated CPUID for SEV-SNP guests Brijesh Singh
2022-02-05 17:19   ` Michael Roth
2022-02-06 15:46     ` Borislav Petkov
2022-02-07 17:00       ` Michael Roth
2022-02-07 18:43         ` Borislav Petkov
2022-02-06 19:50   ` Borislav Petkov
2022-01-28 17:18 ` [PATCH v9 39/43] x86/sev: Provide support for SNP guest request NAEs Brijesh Singh
2022-02-01 20:17   ` Peter Gonda
2022-03-03 14:53     ` Brijesh Singh
2022-01-28 17:18 ` [PATCH v9 40/43] x86/sev: Register SEV-SNP guest request platform device Brijesh Singh
2022-02-01 20:21   ` Peter Gonda
2022-02-02 16:27     ` Brijesh Singh
2022-02-06 20:05   ` Borislav Petkov
2022-01-28 17:18 ` [PATCH v9 41/43] virt: Add SEV-SNP guest driver Brijesh Singh
2022-02-01 20:33   ` Peter Gonda
2022-02-06 22:39   ` Borislav Petkov
2022-02-07 14:41     ` Brijesh Singh
2022-02-07 15:22       ` Borislav Petkov
2022-01-28 17:18 ` [PATCH v9 42/43] virt: sevguest: Add support to derive key Brijesh Singh
2022-02-01 20:39   ` Peter Gonda
2022-02-02 22:31     ` Brijesh Singh
2022-02-07  8:52   ` Borislav Petkov
2022-02-07 16:23     ` Brijesh Singh
2022-02-07 19:09       ` Dov Murik
2022-02-07 20:08         ` Brijesh Singh
2022-02-07 20:28           ` Borislav Petkov
2022-02-08  7:56           ` Dov Murik
2022-02-08 10:51             ` Borislav Petkov
2022-02-08 14:14             ` Brijesh Singh
2022-01-28 17:18 ` [PATCH v9 43/43] virt: sevguest: Add support to get extended report Brijesh Singh
2022-02-01 20:43   ` Peter Gonda
2022-02-07  9:16   ` Borislav Petkov

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=20220205154243.s33gwghqwbb4efyl@amd.com \
    --to=michael.roth@amd.com \
    --cc=ak@linux.intel.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=brijesh.ksingh@gmail.com \
    --cc=brijesh.singh@amd.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dgilbert@redhat.com \
    --cc=dovmurik@linux.ibm.com \
    --cc=hpa@zytor.com \
    --cc=jmattson@google.com \
    --cc=jroedel@suse.de \
    --cc=kirill@shutemov.name \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=marcorr@google.com \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pgonda@google.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rientjes@google.com \
    --cc=sathyanarayanan.kuppuswamy@linux.intel.com \
    --cc=seanjc@google.com \
    --cc=slp@redhat.com \
    --cc=srinivas.pandruvada@linux.intel.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=tobin@ibm.com \
    --cc=tony.luck@intel.com \
    --cc=vbabka@suse.cz \
    --cc=vkuznets@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.