linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "tip-bot2 for Brijesh Singh" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Brijesh Singh <brijesh.singh@amd.com>,
	Borislav Petkov <bp@suse.de>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: x86/sev] x86/compressed: Add helper for validating pages in the decompression stage
Date: Fri, 08 Apr 2022 09:09:06 -0000	[thread overview]
Message-ID: <164940894646.389.17136891705750729768.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20220307213356.2797205-16-brijesh.singh@amd.com>

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     4f9c403e44e5e88feb27d5e617d1adc9cc7ef684
Gitweb:        https://git.kernel.org/tip/4f9c403e44e5e88feb27d5e617d1adc9cc7ef684
Author:        Brijesh Singh <brijesh.singh@amd.com>
AuthorDate:    Wed, 09 Feb 2022 12:10:09 -06:00
Committer:     Borislav Petkov <bp@suse.de>
CommitterDate: Wed, 06 Apr 2022 13:10:40 +02:00

x86/compressed: Add helper for validating pages in the decompression stage

Many of the integrity guarantees of SEV-SNP are enforced through the
Reverse Map Table (RMP). Each RMP entry contains the GPA at which a
particular page of DRAM should be mapped. The VMs can request the
hypervisor to add pages in the RMP table via the Page State Change
VMGEXIT defined in the GHCB specification.

Inside each RMP entry is a Validated flag; this flag is automatically
cleared to 0 by the CPU hardware when a new RMP entry is created for a
guest. Each VM page can be either validated or invalidated, as indicated
by the Validated flag in the RMP entry. Memory access to a private page
that is not validated generates a #VC. A VM must use the PVALIDATE
instruction to validate a private page before using it.

To maintain the security guarantee of SEV-SNP guests, when transitioning
pages from private to shared, the guest must invalidate the pages before
asking the hypervisor to change the page state to shared in the RMP table.

After the pages are mapped private in the page table, the guest must
issue a page state change VMGEXIT to mark the pages private in the RMP
table and validate them.

Upon boot, BIOS should have validated the entire system memory.
During the kernel decompression stage, early_setup_ghcb() uses
set_page_decrypted() to make the GHCB page shared (i.e. clear encryption
attribute). And while exiting from the decompression, it calls
set_page_encrypted() to make the page private.

Add snp_set_page_{private,shared}() helpers that are used by
set_page_{decrypted,encrypted}() to change the page state in the RMP
table.

  [ bp: Massage commit message and comments. ]

Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lore.kernel.org/r/20220307213356.2797205-16-brijesh.singh@amd.com
---
 arch/x86/boot/compressed/ident_map_64.c | 18 ++++++++-
 arch/x86/boot/compressed/misc.h         |  4 ++-
 arch/x86/boot/compressed/sev.c          | 46 ++++++++++++++++++++++++-
 arch/x86/include/asm/sev-common.h       | 26 ++++++++++++++-
 4 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c
index f7213d0..613367e 100644
--- a/arch/x86/boot/compressed/ident_map_64.c
+++ b/arch/x86/boot/compressed/ident_map_64.c
@@ -275,15 +275,31 @@ static int set_clr_page_flags(struct x86_mapping_info *info,
 	 * Changing encryption attributes of a page requires to flush it from
 	 * the caches.
 	 */
-	if ((set | clr) & _PAGE_ENC)
+	if ((set | clr) & _PAGE_ENC) {
 		clflush_page(address);
 
+		/*
+		 * If the encryption attribute is being cleared, change the page state
+		 * to shared in the RMP table.
+		 */
+		if (clr)
+			snp_set_page_shared(__pa(address & PAGE_MASK));
+	}
+
 	/* Update PTE */
 	pte = *ptep;
 	pte = pte_set_flags(pte, set);
 	pte = pte_clear_flags(pte, clr);
 	set_pte(ptep, pte);
 
+	/*
+	 * If the encryption attribute is being set, then change the page state to
+	 * private in the RMP entry. The page state change must be done after the PTE
+	 * is updated.
+	 */
+	if (set & _PAGE_ENC)
+		snp_set_page_private(__pa(address & PAGE_MASK));
+
 	/* Flush TLB after changing encryption attribute */
 	write_cr3(top_level_pgt);
 
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index 23e0e39..01cc13c 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -124,6 +124,8 @@ static inline void console_init(void)
 void sev_enable(struct boot_params *bp);
 void sev_es_shutdown_ghcb(void);
 extern bool sev_es_check_ghcb_fault(unsigned long address);
+void snp_set_page_private(unsigned long paddr);
+void snp_set_page_shared(unsigned long paddr);
 #else
 static inline void sev_enable(struct boot_params *bp) { }
 static inline void sev_es_shutdown_ghcb(void) { }
@@ -131,6 +133,8 @@ static inline bool sev_es_check_ghcb_fault(unsigned long address)
 {
 	return false;
 }
+static inline void snp_set_page_private(unsigned long paddr) { }
+static inline void snp_set_page_shared(unsigned long paddr) { }
 #endif
 
 /* acpi.c */
diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c
index eb42178..5f2c268 100644
--- a/arch/x86/boot/compressed/sev.c
+++ b/arch/x86/boot/compressed/sev.c
@@ -116,6 +116,52 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
 /* Include code for early handlers */
 #include "../../kernel/sev-shared.c"
 
+static inline bool sev_snp_enabled(void)
+{
+	return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
+}
+
+static void __page_state_change(unsigned long paddr, enum psc_op op)
+{
+	u64 val;
+
+	if (!sev_snp_enabled())
+		return;
+
+	/*
+	 * If private -> shared then invalidate the page before requesting the
+	 * state change in the RMP table.
+	 */
+	if (op == SNP_PAGE_STATE_SHARED && pvalidate(paddr, RMP_PG_SIZE_4K, 0))
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
+
+	/* Issue VMGEXIT to change the page state in RMP table. */
+	sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
+	VMGEXIT();
+
+	/* Read the response of the VMGEXIT. */
+	val = sev_es_rd_ghcb_msr();
+	if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
+
+	/*
+	 * Now that page state is changed in the RMP table, validate it so that it is
+	 * consistent with the RMP entry.
+	 */
+	if (op == SNP_PAGE_STATE_PRIVATE && pvalidate(paddr, RMP_PG_SIZE_4K, 1))
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
+}
+
+void snp_set_page_private(unsigned long paddr)
+{
+	__page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
+}
+
+void snp_set_page_shared(unsigned long paddr)
+{
+	__page_state_change(paddr, SNP_PAGE_STATE_SHARED);
+}
+
 static bool early_setup_ghcb(void)
 {
 	if (set_page_decrypted((unsigned long)&boot_ghcb_page))
diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
index 7ac5842..fe7fe16 100644
--- a/arch/x86/include/asm/sev-common.h
+++ b/arch/x86/include/asm/sev-common.h
@@ -57,6 +57,32 @@
 #define GHCB_MSR_AP_RESET_HOLD_REQ	0x006
 #define GHCB_MSR_AP_RESET_HOLD_RESP	0x007
 
+/*
+ * SNP Page State Change Operation
+ *
+ * GHCBData[55:52] - Page operation:
+ *   0x0001	Page assignment, Private
+ *   0x0002	Page assignment, Shared
+ */
+enum psc_op {
+	SNP_PAGE_STATE_PRIVATE = 1,
+	SNP_PAGE_STATE_SHARED,
+};
+
+#define GHCB_MSR_PSC_REQ		0x014
+#define GHCB_MSR_PSC_REQ_GFN(gfn, op)			\
+	/* GHCBData[55:52] */				\
+	(((u64)((op) & 0xf) << 52) |			\
+	/* GHCBData[51:12] */				\
+	((u64)((gfn) & GENMASK_ULL(39, 0)) << 12) |	\
+	/* GHCBData[11:0] */				\
+	GHCB_MSR_PSC_REQ)
+
+#define GHCB_MSR_PSC_RESP		0x015
+#define GHCB_MSR_PSC_RESP_VAL(val)			\
+	/* GHCBData[63:32] */				\
+	(((u64)(val) & GENMASK_ULL(63, 32)) >> 32)
+
 /* GHCB Hypervisor Feature Request/Response */
 #define GHCB_MSR_HV_FT_REQ		0x080
 #define GHCB_MSR_HV_FT_RESP		0x081

  reply	other threads:[~2022-04-08  9:14 UTC|newest]

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

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=164940894646.389.17136891705750729768.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=bp@suse.de \
    --cc=brijesh.singh@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --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;
as well as URLs for NNTP newsgroup(s).