Skip to contentSkip to conversation

Add Secure TSC support for SNP guests

PATCH Superseded Nikunj A Dadhania 5 participants 38 messages Dec 20, 2023 – Feb 6, 2024

Subsystems (5)
x86 KVM KVM x86 x86 MM +1 more subsystems

v7 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v7 cover Add Secure TSC support for SNP guests

Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC".

During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at
https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info"

Use a minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the AMD Security Processor which is available at early boot.

SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before smp cpus are started, move most of the sev-guest driver code to kernel/sev.c and provide well defined APIs to the sev-guest driver to use the interface to avoid code-duplication.

Patches:
01-08: Preparation and movement of sev-guest driver code 09-16: SecureTSC enablement patches.

Testing SecureTSC
-----------------

SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series:
https://github.com/nikunjad/linux/tree/snp-host-latest-securetsc_v5

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp_securetsc_v5

QEMU commandline SEV-SNP-UPM with SecureTSC:

  qemu-system-x86_64 -cpu EPYC-Milan-v2,+secure-tsc,+invtsc -smp 4 \
    -object memory-backend-memfd-private,id=ram1,size=1G,share=true \
    -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \
    -machine q35,confidential-guest-support=sev0,memory-backend=ram1,kvm-type=snp \
    ...

Changelog:
----------
v7:
* Drop mutex from the snp_dev and add snp_guest_cmd_{lock,unlock} API
* Added comments for secrets page failure
* Added define for maximum supported VMPCK
* Updated comments why sev_status is used directly instead of cpu_feature_enabled()

v6:
* Add synthetic SecureTSC x86 feature bit
* Drop {__enc,dec}_payload() as they are pretty small and has one caller.
* Instead of a pointer use data_npages as variable
* Beautify struct snp_guest_req
* Make vmpck_id as unsigned int in snp_assign_vmpck()
* Move most of the functions to end of sev.c file
* Update commit/comments/error messages
* Mark free_shared_pages and alloc_shared_pages as inline
* Free snp_dev->certs_data when guest driver is removed
* Add lockdep assert in snp_inc_msg_seqno()
* Drop redundant enc_init NULL check
* Move SNP_TSC_INFO_REQ_SZ define out of structure
* Rename guest_tsc_{scale,offset} to snp_tsc_{scale,offset}
* Add new linux termination error code GHCB_TERM_SECURE_TSC
* Initialize and use cmd_mutex in snp_get_tsc_info()
* Set TSC as reliable in sme_early_init()
* Do not print firmware bug for Secure TSC enabled guests

https://lore.kernel.org/lkml/[email protected]/

v5:
* Rebased on v6.6 kernel
* Dropped link tag in first patch
* Dropped get_ctx_authsize() as it was redundant

https://lore.kernel.org/lkml/[email protected]/

v4:
* Drop handle_guest_request() and handle_guest_request_ext()
* Drop NULL check for key
* Corrected commit subject
* Added Reviewed-by from Tom

https://lore.kernel.org/lkml/[email protected]/

v3:
* Updated commit messages
* Made snp_setup_psp_messaging() generic that is accessed by both the kernel and the driver
* Moved most of the context information to sev.c, sev-guest driver does not need to know the secrets page layout anymore
* Add CC_ATTR_GUEST_SECURE_TSC early in the series therefore it can be used in later patches.
* Removed data_gpa and data_npages from struct snp_req_data, as certs_data and its size is passed to handle_guest_request_ext()
* Make vmpck_id as unsigned int
* Dropped unnecessary usage of memzero_explicit()
* Cache secrets_pa instead of remapping the cc_blob always
* Rebase on top of v6.4 kernel
https://lore.kernel.org/lkml/[email protected]/

v2:
* Rebased on top of v6.3-rc3 that has Boris's sev-guest cleanup series
https://lore.kernel.org/r/[email protected]/

v1: https://lore.kernel.org/r/[email protected]/

Nikunj A Dadhania (16):
virt: sev-guest: Use AES GCM crypto library
virt: sev-guest: Replace dev_dbg with pr_debug
virt: sev-guest: Add SNP guest request structure
virt: sev-guest: Add vmpck_id to snp_guest_dev struct x86/sev: Cache the secrets page address
virt: sev-guest: Move SNP Guest command mutex
x86/sev: Move and reorganize sev guest request api x86/mm: Add generic guest initialization hook x86/cpufeatures: Add synthetic Secure TSC bit
x86/sev: Add Secure TSC support for SNP guests x86/sev: Change TSC MSR behavior for Secure TSC enabled guests x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled
guests
x86/kvmclock: Skip kvmclock when Secure TSC is available x86/sev: Mark Secure TSC as reliable
x86/cpu/amd: Do not print FW_BUG for Secure TSC x86/sev: Enable Secure TSC for SNP guests

 arch/x86/Kconfig                        |   1 +
 arch/x86/boot/compressed/sev.c          |   3 +-
 arch/x86/include/asm/cpufeatures.h      |   1 +
 arch/x86/include/asm/sev-common.h       |   1 +
 arch/x86/include/asm/sev-guest.h        | 190 +++++++
 arch/x86/include/asm/sev.h              |  21 +-
 arch/x86/include/asm/svm.h              |   6 +-
 arch/x86/include/asm/x86_init.h         |   2 +
 arch/x86/kernel/cpu/amd.c               |   3 +-
 arch/x86/kernel/kvmclock.c              |   2 +-
 arch/x86/kernel/sev-shared.c            |  10 +
 arch/x86/kernel/sev.c                   | 649 +++++++++++++++++++++--
 arch/x86/kernel/x86_init.c              |   2 +
 arch/x86/mm/mem_encrypt.c               |  12 +-
 arch/x86/mm/mem_encrypt_amd.c           |  12 +
 drivers/virt/coco/sev-guest/Kconfig     |   3 -
 drivers/virt/coco/sev-guest/sev-guest.c | 668 +++---------------------
 drivers/virt/coco/sev-guest/sev-guest.h |  63 ---
 18 files changed, 917 insertions(+), 732 deletions(-)
 create mode 100644 arch/x86/include/asm/sev-guest.h
 delete mode 100644 drivers/virt/coco/sev-guest/sev-guest.h

base-commit: ceb6a6f023fd3e8b07761ed900352ef574010bcb

Signature · 1 line
2.34.1
#1 lore raw

PATCH v7 1/16 virt: sev-guest: Use AES GCM crypto library

The sev-guest driver encryption code uses Crypto API for SNP guest messaging to interact with AMD Security processor. For enabling SecureTSC, SEV-SNP guests need to send a TSC_INFO request guest message before the smpboot phase starts. Details from the TSC_INFO response will be used to program the VMSA before the secondary CPUs are brought up. The Crypto API is not available this early in the boot phase.

In preparation of moving the encryption code out of sev-guest driver to support SecureTSC and make reviewing the diff easier, start using AES GCM library implementation instead of Crypto API.

Drop __enc_payload() and dec_payload() helpers as both are pretty small and can be moved to the respective callers.

---
 drivers/virt/coco/sev-guest/Kconfig     |   4 +-
 drivers/virt/coco/sev-guest/sev-guest.c | 175 ++++++------------------
 drivers/virt/coco/sev-guest/sev-guest.h |   3 +
 3 files changed, 43 insertions(+), 139 deletions(-)
drivers/virt/coco/sev-guest/Kconfig +1 -3
index 1cffc72c41cb..0b772bd921d8 100644
@@ -2,9 +2,7 @@ config SEV_GUEST
 	tristate "AMD SEV Guest driver"
 	default m
 	depends on AMD_MEM_ENCRYPT
-	select CRYPTO
-	select CRYPTO_AEAD2
-	select CRYPTO_GCM
+	select CRYPTO_LIB_AESGCM
 	select TSM_REPORTS
 	help
 	  SEV-SNP firmware provides the guest a mechanism to communicate with
drivers/virt/coco/sev-guest/sev-guest.c +39 -136

295 lines not on this page · raw message

drivers/virt/coco/sev-guest/sev-guest.h +3 -0
index 21bda26fdb95..ceb798a404d6 100644
@@ -13,6 +13,9 @@
 #include <linux/types.h>
 
 #define MAX_AUTHTAG_LEN		32
+#define AUTHTAG_LEN		16
+#define AAD_LEN			48
+#define MSG_HDR_VER		1
 
 /* See SNP spec SNP_GUEST_REQUEST section for the structure */
 enum msg_type {
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v7 01/16] virt: sev-guest: Use AES GCM crypto library

drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 204

Quoted 11 lines · Nikunj A Dadhania
drivers/virt/coco/sev-guest/sev-guest.c +2 -2
@@ -307,11 +197,16 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,
 	 * If the message size is greater than our buffer length then return
 	 * an error.
 	 */
-	if (unlikely((resp_hdr->msg_sz + crypto->a_len) > sz))
+	if (unlikely((resp_hdr->msg_sz + ctx->authsize) > sz))
 		return -EBADMSG;
 
 	/* Decrypt the payload */
-	return dec_payload(snp_dev, resp, payload, resp_hdr->msg_sz + crypto->a_len);
+	memcpy(iv, &resp_hdr->msg_seqno, sizeof(resp_hdr->msg_seqno));

sizeof(iv) != sizeof(resp_hdr->msg_seqno) and it fits now.

However, for protection against future bugs, this should be:

	memcpy(iv, &resp_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_hdr->msg_seqno)));
Quoted 18 lines
drivers/virt/coco/sev-guest/sev-guest.c +7 -0
+	if (!aesgcm_decrypt(ctx, payload, resp->payload, resp_hdr->msg_sz,
+			    &resp_hdr->algo, AAD_LEN, iv, resp_hdr->authtag))
+		return -EBADMSG;
+
+	return 0;
 }
 
 static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type,
@@ -319,6 +214,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 {
 	struct snp_guest_msg *req = &snp_dev->secret_request;
 	struct snp_guest_msg_hdr *hdr = &req->hdr;
+	struct aesgcm_ctx *ctx = snp_dev->ctx;
+	u8 iv[GCM_AES_IV_SIZE] = {};
 
 	memset(req, 0, sizeof(*req));
 

drivers/virt/coco/sev-guest/sev-guest.c static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 · line 241

Quoted 8 lines
drivers/virt/coco/sev-guest/sev-guest.c +4 -1
@@ -338,7 +235,14 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 	dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n",
 		hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
 
-	return __enc_payload(snp_dev, req, payload, sz);
+	if (WARN_ON((sz + ctx->authsize) > sizeof(req->payload)))
+		return -EBADMSG;
+
+	memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno));

Ditto.

Quoted 4 lines
drivers/virt/coco/sev-guest/sev-guest.c +4 -0
+	aesgcm_encrypt(ctx, req->payload, payload, sz, &hdr->algo, AAD_LEN,
+		       iv, hdr->authtag);
+
+	return 0;
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw

Re: [PATCH v7 01/16] virt: sev-guest: Use AES GCM crypto library

Quoted 15 lines · Borislav Petkov

drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 204

Quoted 11 lines · Nikunj A Dadhania
drivers/virt/coco/sev-guest/sev-guest.c +2 -2
@@ -307,11 +197,16 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,
 	 * If the message size is greater than our buffer length then return
 	 * an error.
 	 */
-	if (unlikely((resp_hdr->msg_sz + crypto->a_len) > sz))
+	if (unlikely((resp_hdr->msg_sz + ctx->authsize) > sz))
 		return -EBADMSG;
 
 	/* Decrypt the payload */
-	return dec_payload(snp_dev, resp, payload, resp_hdr->msg_sz + crypto->a_len);
+	memcpy(iv, &resp_hdr->msg_seqno, sizeof(resp_hdr->msg_seqno));

sizeof(iv) != sizeof(resp_hdr->msg_seqno) and it fits now.

Quoted 3 lines

However, for protection against future bugs, this should be:

	memcpy(iv, &resp_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_hdr->msg_seqno)));

Sure, will change.

Quoted 22 lines
Quoted 22 lines
drivers/virt/coco/sev-guest/sev-guest.c +7 -0
+	if (!aesgcm_decrypt(ctx, payload, resp->payload, resp_hdr->msg_sz,
+			    &resp_hdr->algo, AAD_LEN, iv, resp_hdr->authtag))
+		return -EBADMSG;
+
+	return 0;
 }
 
 static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type,
@@ -319,6 +214,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 {
 	struct snp_guest_msg *req = &snp_dev->secret_request;
 	struct snp_guest_msg_hdr *hdr = &req->hdr;
+	struct aesgcm_ctx *ctx = snp_dev->ctx;
+	u8 iv[GCM_AES_IV_SIZE] = {};
 
 	memset(req, 0, sizeof(*req));
 
@@ -338,7 +235,14 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 	dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n",
 		hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
 
Quoted 7 lines

drivers/virt/coco/sev-guest/sev-guest.c static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 · line 241

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +4 -1
-	return __enc_payload(snp_dev, req, payload, sz);
+	if (WARN_ON((sz + ctx->authsize) > sizeof(req->payload)))
+		return -EBADMSG;
+
+	memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno));

Ditto.

Sure.

Quoted 6 lines

drivers/virt/coco/sev-guest/sev-guest.c static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 · line 245

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +4 -0
+	aesgcm_encrypt(ctx, req->payload, payload, sz, &hdr->algo, AAD_LEN,
+		       iv, hdr->authtag);
+
+	return 0;

Thanks,
Nikunj

#4 lore raw

PATCH v7 2/16 virt: sev-guest: Replace dev_dbg with pr_debug

In reply to Nikunj A Dadhania

In preparation of moving code to arch/x86/kernel/sev.c, replace dev_dbg with pr_debug.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +5 -4
index aedc842781b6..469e10d9bf35 100644
@@ -178,8 +178,9 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,
 	struct aesgcm_ctx *ctx = snp_dev->ctx;
 	u8 iv[GCM_AES_IV_SIZE] = {};
 
-	dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n",
-		resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz);
+	pr_debug("response [seqno %lld type %d version %d sz %d]\n",
+		 resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version,
+		 resp_hdr->msg_sz);
 
 	/* Copy response from shared memory to encrypted memory. */
 	memcpy(resp, snp_dev->response, sizeof(*resp));
@@ -232,8 +233,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 	if (!hdr->msg_seqno)
 		return -ENOSR;
 
-	dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n",
-		hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
+	pr_debug("request [seqno %lld type %d version %d sz %d]\n",
+		 hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
 
 	if (WARN_ON((sz + ctx->authsize) > sizeof(req->payload)))
 		return -EBADMSG;
Signature · 1 line
2.34.1
#5 lore raw

PATCH v7 3/16 virt: sev-guest: Add SNP guest request structure

In reply to Nikunj A Dadhania

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters.

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev_guest.h.

---
 .../x86/include/asm}/sev-guest.h              |  18 +++
 arch/x86/include/asm/sev.h                    |   8 --
 arch/x86/kernel/sev.c                         |  15 ++-
 drivers/virt/coco/sev-guest/sev-guest.c       | 108 +++++++++++-------
 4 files changed, 93 insertions(+), 56 deletions(-)
 rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (78%)
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +18 -0
similarity index 78%
rename from drivers/virt/coco/sev-guest/sev-guest.h
rename to arch/x86/include/asm/sev-guest.h
index ceb798a404d6..27cc15ad6131 100644
@@ -63,4 +63,22 @@ struct snp_guest_msg {
 	u8 payload[4000];
 } __packed;
 
+struct snp_guest_req {
+	void *req_buf;
+	size_t req_sz;
+
+	void *resp_buf;
+	size_t resp_sz;
+
+	void *data;
+	size_t data_npages;
+
+	u64 exit_code;
+	unsigned int vmpck_id;
+	u8 msg_version;
+	u8 msg_type;
+};
+
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio);
 #endif /* __VIRT_SEVGUEST_H__ */
arch/x86/include/asm/sev.h +0 -8
index 5b4a1ce3d368..78465a8c7dc6 100644
@@ -97,8 +97,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
 struct snp_req_data {
 	unsigned long req_gpa;
 	unsigned long resp_gpa;
-	unsigned long data_gpa;
-	unsigned int data_npages;
 };
 
 struct sev_guest_platform_data {
@@ -209,7 +207,6 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
 void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __init __noreturn snp_abort(void);
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
 u64 snp_get_unsupported_features(u64 status);
 u64 sev_get_status(void);
@@ -233,11 +230,6 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npa
 static inline void snp_set_wakeup_secondary_cpu(void) { }
 static inline bool snp_init(struct boot_params *bp) { return false; }
 static inline void snp_abort(void) { }
-static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
-{
-	return -ENOTTY;
-}
-
 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
 static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
 static inline u64 sev_get_status(void) { return 0; }
arch/x86/kernel/sev.c +11 -4
index c67285824e82..fd89aca22f6a 100644
@@ -28,6 +28,7 @@
 #include <asm/cpu_entry_area.h>
 #include <asm/stacktrace.h>
 #include <asm/sev.h>
+#include <asm/sev-guest.h>
 #include <asm/insn-eval.h>
 #include <asm/fpu/xcr.h>
 #include <asm/processor.h>
@@ -2170,15 +2171,21 @@ static int __init init_sev_config(char *str)
 }
 __setup("sev=", init_sev_config);
 
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
 	unsigned long flags;
 	struct ghcb *ghcb;
+	u64 exit_code;
 	int ret;
 
 	rio->exitinfo2 = SEV_RET_NO_FW_CALL;
+	if (!req)
+		return -EINVAL;
+
+	exit_code = req->exit_code;
 
 	/*
 	 * __sev_get_ghcb() needs to run with IRQs disabled because it is using
@@ -2195,8 +2202,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn
 	vc_ghcb_invalidate(ghcb);
 
 	if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
-		ghcb_set_rax(ghcb, input->data_gpa);
-		ghcb_set_rbx(ghcb, input->data_npages);
+		ghcb_set_rax(ghcb, __pa(req->data));
+		ghcb_set_rbx(ghcb, req->data_npages);
 	}
 
 	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa);
@@ -2215,7 +2222,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn
 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN):
 		/* Number of expected pages are returned in RBX */
 		if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
-			input->data_npages = ghcb_get_rbx(ghcb);
+			req->data_npages = ghcb_get_rbx(ghcb);
 			ret = -ENOSPC;
 			break;
 		}
drivers/virt/coco/sev-guest/sev-guest.c +64 -44

261 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#6 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

arch/x86/kernel/sev.c static int __init init_sev_config(char *str) · line 2181

Quoted 9 lines · Nikunj A Dadhania
arch/x86/kernel/sev.c +3 -1
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
 	unsigned long flags;
 	struct ghcb *ghcb;
+	u64 exit_code;

Silly local vars. Just use req->exit_code everywhere instead.

arch/x86/kernel/sev.c static int __init init_sev_config(char *str) · line 2186

Quoted 5 lines
arch/x86/kernel/sev.c +2 -0
 	int ret;
 
 	rio->exitinfo2 = SEV_RET_NO_FW_CALL;
+	if (!req)
+		return -EINVAL;

Such tests are done under the variable which is assigned, not randomly.

Also, what's the point in testing req? Will that ever be NULL? What are you actually protecting against here?

Quoted 15 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -2
index 469e10d9bf35..5cafbd1c42cb 100644
@@ -27,8 +27,7 @@
 
 #include <asm/svm.h>
 #include <asm/sev.h>
-
-#include "sev-guest.h"
+#include <asm/sev-guest.h>
 
 #define DEVICE_NAME	"sev-guest"
 

drivers/virt/coco/sev-guest/sev-guest.c static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) · line 171

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
@@ -169,7 +168,7 @@ static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
 	return ctx;
 }
 
-static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz)
+static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req)

So we call the request everywhere "req". But you've called it "guest_req" here because...

drivers/virt/coco/sev-guest/sev-guest.c static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) · line 174

Quoted 3 lines
drivers/virt/coco/sev-guest/sev-guest.c +0 -0
 {
 	struct snp_guest_msg *resp = &snp_dev->secret_response;
 	struct snp_guest_msg *req = &snp_dev->secret_request;

... there already is a "req" variable which is not a guest request thing but a guest message. So why don't you call it "req_msg" instead and the "resp" "resp_msg" so that it is clear what is what?

And then you can call the actual request var "req" and then the code becomes more readable...

...

drivers/virt/coco/sev-guest/sev-guest.c struct snp_req_resp { · line 390

Quoted 4 lines

static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) {

drivers/virt/coco/sev-guest/sev-guest.c +1 -0
 	struct snp_report_req *req = &snp_dev->req.report;
+	struct snp_guest_req guest_req = {0};

You have the same issue here.

If we aim at calling the local vars in every function the same, the code becomes automatically much more readable.

And so on...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#7 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 12 lines · Borislav Petkov

arch/x86/kernel/sev.c static int __init init_sev_config(char *str) · line 2181

Quoted 9 lines · Nikunj A Dadhania
arch/x86/kernel/sev.c +3 -1
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
 	unsigned long flags;
 	struct ghcb *ghcb;
+	u64 exit_code;

Silly local vars. Just use req->exit_code everywhere instead.

Sure, will change.

Quoted 11 lines

arch/x86/kernel/sev.c static int __init init_sev_config(char *str) · line 2186

Quoted 6 lines
arch/x86/kernel/sev.c +2 -0
 	int ret;
 
 	rio->exitinfo2 = SEV_RET_NO_FW_CALL;
+	if (!req)
+		return -EINVAL;

Such tests are done under the variable which is assigned, not randomly.

Also, what's the point in testing req? Will that ever be NULL? What are you actually protecting against here?

Right, and in the later code, this is checked at snp_send_guest_request() API. So this is redundant.

Quoted 18 lines
Quoted 18 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -2
index 469e10d9bf35..5cafbd1c42cb 100644
@@ -27,8 +27,7 @@
 
 #include <asm/svm.h>
 #include <asm/sev.h>
-
-#include "sev-guest.h"
+#include <asm/sev-guest.h>
 
 #define DEVICE_NAME	"sev-guest"
 
@@ -169,7 +168,7 @@ static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
 	return ctx;
 }
 
Quoted 5 lines

drivers/virt/coco/sev-guest/sev-guest.c static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) · line 171

Quoted 2 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
-static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz)
+static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req)

So we call the request everywhere "req". But you've called it "guest_req" here because...

Yes, I was thinking about it and came up with this.

Quoted 9 lines

drivers/virt/coco/sev-guest/sev-guest.c static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) · line 174

Quoted 4 lines
drivers/virt/coco/sev-guest/sev-guest.c +0 -0
 {
 	struct snp_guest_msg *resp = &snp_dev->secret_response;
 	struct snp_guest_msg *req = &snp_dev->secret_request;

... there already is a "req" variable which is not a guest request thing but a guest message. So why don't you call it "req_msg" instead and the "resp" "resp_msg" so that it is clear what is what?

This naming is much better, thanks.

Quoted 12 lines

And then you can call the actual request var "req" and then the code becomes more readable...

...

drivers/virt/coco/sev-guest/sev-guest.c struct snp_req_resp { · line 390

Quoted 4 lines

static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) {

drivers/virt/coco/sev-guest/sev-guest.c +1 -0
 	struct snp_report_req *req = &snp_dev->req.report;
+	struct snp_guest_req guest_req = {0};

You have the same issue here.

Quoted 4 lines

If we aim at calling the local vars in every function the same, the code becomes automatically much more readable.

And so on...

Will change accordingly,

Regards
Nikunj

author Tested-by Peter Gonda
#8 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

In reply to Borislav Petkov
Quoted 68 lines · Borislav Petkov

Review

Quoted 9 lines · Nikunj A Dadhania
(quoted diff) +3 -1
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
 	unsigned long flags;
 	struct ghcb *ghcb;
+	u64 exit_code;

Silly local vars. Just use req->exit_code everywhere instead.

Review

Quoted 5 lines
(quoted diff) +2 -0
 	int ret;
 
 	rio->exitinfo2 = SEV_RET_NO_FW_CALL;
+	if (!req)
+		return -EINVAL;

Such tests are done under the variable which is assigned, not randomly.

Also, what's the point in testing req? Will that ever be NULL? What are you actually protecting against here?

Quoted 15 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -2
index 469e10d9bf35..5cafbd1c42cb 100644
@@ -27,8 +27,7 @@
 
 #include <asm/svm.h>
 #include <asm/sev.h>
-
-#include "sev-guest.h"
+#include <asm/sev-guest.h>
 
 #define DEVICE_NAME	"sev-guest"
 

drivers/virt/coco/sev-guest/sev-guest.c static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) · line 171

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
@@ -169,7 +168,7 @@ static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
 	return ctx;
 }
 
-static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz)
+static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req)

So we call the request everywhere "req". But you've called it "guest_req" here because...

drivers/virt/coco/sev-guest/sev-guest.c static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) · line 174

Quoted 3 lines
drivers/virt/coco/sev-guest/sev-guest.c +0 -0
 {
 	struct snp_guest_msg *resp = &snp_dev->secret_response;
 	struct snp_guest_msg *req = &snp_dev->secret_request;

... there already is a "req" variable which is not a guest request thing but a guest message. So why don't you call it "req_msg" instead and the "resp" "resp_msg" so that it is clear what is what?

And then you can call the actual request var "req" and then the code becomes more readable...

...

drivers/virt/coco/sev-guest/sev-guest.c

Quoted 4 lines

static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) {

drivers/virt/coco/sev-guest/sev-guest.c +1 -0
 	struct snp_report_req *req = &snp_dev->req.report;
+	struct snp_guest_req guest_req = {0};

You have the same issue here.

Quoted 5 lines

If we aim at calling the local vars in every function the same, the code becomes automatically much more readable.

And so on...

Changed to "req" for all the guest request throughout the file. Other "req" usage are renamed appropriately.

Subject: [PATCH] virt: sev-guest: Add SNP guest request structure

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters. Use "req" as variable name for guest req throughout the file and rename other variables appropriately.

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev_guest.h.

---
 .../x86/include/asm}/sev-guest.h              |  18 ++
 arch/x86/include/asm/sev.h                    |   8 -
 arch/x86/kernel/sev.c                         |  16 +-
 drivers/virt/coco/sev-guest/sev-guest.c       | 195 ++++++++++--------
 4 files changed, 135 insertions(+), 102 deletions(-)
 rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (78%)
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +18 -0
similarity index 78%
rename from drivers/virt/coco/sev-guest/sev-guest.h
rename to arch/x86/include/asm/sev-guest.h
index ceb798a404d6..27cc15ad6131 100644
@@ -63,4 +63,22 @@ struct snp_guest_msg {
        u8 payload[4000];
 } __packed;
 
+struct snp_guest_req {
+       void *req_buf;
+       size_t req_sz;
+
+       void *resp_buf;
+       size_t resp_sz;
+
+       void *data;
+       size_t data_npages;
+
+       u64 exit_code;
+       unsigned int vmpck_id;
+       u8 msg_version;
+       u8 msg_type;
+};
+
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+                           struct snp_guest_request_ioctl *rio);
 #endif /* __VIRT_SEVGUEST_H__ */
arch/x86/include/asm/sev.h +0 -8
index 5b4a1ce3d368..78465a8c7dc6 100644
@@ -97,8 +97,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
 struct snp_req_data {
        unsigned long req_gpa;
        unsigned long resp_gpa;
-       unsigned long data_gpa;
-       unsigned int data_npages;
 };
 
 struct sev_guest_platform_data {
@@ -209,7 +207,6 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
 void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __init __noreturn snp_abort(void);
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
 u64 snp_get_unsupported_features(u64 status);
 u64 sev_get_status(void);
@@ -233,11 +230,6 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npa
 static inline void snp_set_wakeup_secondary_cpu(void) { }
 static inline bool snp_init(struct boot_params *bp) { return false; }
 static inline void snp_abort(void) { }
-static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
-{
-       return -ENOTTY;
-}
-
 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
 static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
 static inline u64 sev_get_status(void) { return 0; }
arch/x86/kernel/sev.c +9 -7
index c67285824e82..43ffd307731f 100644
@@ -28,6 +28,7 @@
 #include <asm/cpu_entry_area.h>
 #include <asm/stacktrace.h>
 #include <asm/sev.h>
+#include <asm/sev-guest.h>
 #include <asm/insn-eval.h>
 #include <asm/fpu/xcr.h>
 #include <asm/processor.h>
@@ -2170,7 +2171,8 @@ static int __init init_sev_config(char *str)
 }
 __setup("sev=", init_sev_config);
 
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+                           struct snp_guest_request_ioctl *rio)
 {
        struct ghcb_state state;
        struct es_em_ctxt ctxt;
@@ -2194,12 +2196,12 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn
 
        vc_ghcb_invalidate(ghcb);
 
-       if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
-               ghcb_set_rax(ghcb, input->data_gpa);
-               ghcb_set_rbx(ghcb, input->data_npages);
+       if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
+               ghcb_set_rax(ghcb, __pa(req->data));
+               ghcb_set_rbx(ghcb, req->data_npages);
        }
 
-       ret = sev_es_ghcb_hv_call(ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa);
+       ret = sev_es_ghcb_hv_call(ghcb, &ctxt, req->exit_code, input->req_gpa, input->resp_gpa);
        if (ret)
                goto e_put;
 
@@ -2214,8 +2216,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn
 
        case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN):
                /* Number of expected pages are returned in RBX */
-               if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
-                       input->data_npages = ghcb_get_rbx(ghcb);
+               if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
+                       req->data_npages = ghcb_get_rbx(ghcb);
                        ret = -ENOSPC;
                        break;
                }
drivers/virt/coco/sev-guest/sev-guest.c +108 -87

407 lines not on this page · raw message

--
2.34.1

maintainer
#9 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 2 lines · Nikunj A. Dadhania

Changed to "req" for all the guest request throughout the file. Other "req" usage are renamed appropriately.

Yes, better from what I can tell.

However, I can't apply this patch in order to have a better look, it is mangled. Next time, before you send a patch this way, send it yourself first and try applying it.

If it doesn't work, throw away your mailer and use a proper one:

Documentation/process/email-clients.rst

Quoted 7 lines

Subject: [PATCH] virt: sev-guest: Add SNP guest request structure

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters. Use "req" as variable name for guest req throughout the file and rename other variables appropriately.

Quoted 5 lines

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev_guest.h.

Tested-by: tags must be dropped if you change a patch in a non-trivial way. And this change is not that trivial I'd say.

Quoted 7 lines
---
 .../x86/include/asm}/sev-guest.h              |  18 ++
 arch/x86/include/asm/sev.h                    |   8 -
 arch/x86/kernel/sev.c                         |  16 +-
 drivers/virt/coco/sev-guest/sev-guest.c       | 195 ++++++++++--------
 4 files changed, 135 insertions(+), 102 deletions(-)
 rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (78%)

I didn't notice this before: why am I getting a sev-guest.h header in arch/x86/?

Lemme quote again the file paths we agreed upon:

https://lore.kernel.org/all/[email protected]/

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#10 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Nikunj A. Dadhania

Changed to "req" for all the guest request throughout the file. Other "req" usage are renamed appropriately.

Yes, better from what I can tell.

Quoted 7 lines

However, I can't apply this patch in order to have a better look, it is mangled. Next time, before you send a patch this way, send it yourself first and try applying it.

If it doesn't work, throw away your mailer and use a proper one:

Documentation/process/email-clients.rst

Sorry for that, will fix it.

Quoted 25 lines
Quoted 8 lines

Subject: [PATCH] virt: sev-guest: Add SNP guest request structure

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters. Use "req" as variable name for guest req throughout the file and rename other variables appropriately.

Quoted 5 lines

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev_guest.h.

Tested-by: tags must be dropped if you change a patch in a non-trivial way. And this change is not that trivial I'd say.

Quoted 7 lines
---
 .../x86/include/asm}/sev-guest.h              |  18 ++
 arch/x86/include/asm/sev.h                    |   8 -
 arch/x86/kernel/sev.c                         |  16 +-
 drivers/virt/coco/sev-guest/sev-guest.c       | 195 ++++++++++--------
 4 files changed, 135 insertions(+), 102 deletions(-)
 rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (78%)
Quoted 6 lines

I didn't notice this before: why am I getting a sev-guest.h header in arch/x86/?

Lemme quote again the file paths we agreed upon:

https://lore.kernel.org/all/[email protected]/

I will move it to arch/x86/coco/sev, do we need a separate "include" directory ?

As we are doing this movement, should we move guest messaging related code to arch/x86/coco/sev/guest-msg.c ?

Regards
Nikunj

maintainer
#11 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 1 line · Nikunj A. Dadhania

I will move it to arch/x86/coco/sev, do we need a separate "include" directory ?

I still don't understand why you need to move it at all?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#12 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 5 lines · Borislav Petkov
Quoted 1 line · Nikunj A. Dadhania

I will move it to arch/x86/coco/sev, do we need a separate "include" directory ?

I still don't understand why you need to move it at all?

To support Secure TSC, SNP guest messages need to be used during the early boot. Most of the guest messaging code is currently part of sev-guest driver and header. I have opportunistically moved the header in this patch as I was adding guest request structure. Movement of rest of the functions implementation from sev-guest.c => kernel/sev.c is done in patch 7/16.

As per https://lore.kernel.org/all/[email protected]/, I can move the snp guest messaging code implementation to arch/x86/coco/sev/guest-msg.[ch]

Regards
Nikunj

maintainer
#13 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 3 lines · Nikunj A. Dadhania

I have opportunistically moved the header in this patch as I was adding guest request structure. Movement of rest of the functions implementation from sev-guest.c => kernel/sev.c is done in patch 7/16.

And kernel/sev.c has a corresponding header arch/x86/include/asm/sev.h which is kinda *begging* to collect all the stuff that sev.c is using instead of introducing a sev-guest.h thing which doesn't make a lot of sense, TU-wise.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#14 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Nikunj A. Dadhania

I have opportunistically moved the header in this patch as I was adding guest request structure. Movement of rest of the functions implementation from sev-guest.c => kernel/sev.c is done in patch 7/16.

And kernel/sev.c has a corresponding header arch/x86/include/asm/sev.h which is kinda *begging* to collect all the stuff that sev.c is using instead of introducing a sev-guest.h thing which doesn't make a lot of sense, TU-wise.

Sure, below is the updated patch. Complete series is pushed here

https://github.com/AMDESE/linux-kvm/commits/sectsc-guest-latest/

Subject: virt: sev-guest: Add SNP guest request structure

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters.

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev.h.

---
 arch/x86/include/asm/sev.h              |  75 ++++++++-
 arch/x86/kernel/sev.c                   |  15 +-
 drivers/virt/coco/sev-guest/sev-guest.c | 194 +++++++++++++-----------
 drivers/virt/coco/sev-guest/sev-guest.h |  66 --------
 4 files changed, 186 insertions(+), 164 deletions(-)
 delete mode 100644 drivers/virt/coco/sev-guest/sev-guest.h
arch/x86/include/asm/sev.h +71 -4
index 5b4a1ce3d368..56b07c79945a 100644
@@ -97,8 +97,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
 struct snp_req_data {
 	unsigned long req_gpa;
 	unsigned long resp_gpa;
-	unsigned long data_gpa;
-	unsigned int data_npages;
 };
 
 struct sev_guest_platform_data {
@@ -140,6 +138,73 @@ struct snp_secrets_page_layout {
 	u8 rsvd3[3840];
 } __packed;
 
+#define MAX_AUTHTAG_LEN		32
+#define AUTHTAG_LEN		16
+#define AAD_LEN			48
+#define MSG_HDR_VER		1
+
+/* See SNP spec SNP_GUEST_REQUEST section for the structure */
+enum msg_type {
+	SNP_MSG_TYPE_INVALID = 0,
+	SNP_MSG_CPUID_REQ,
+	SNP_MSG_CPUID_RSP,
+	SNP_MSG_KEY_REQ,
+	SNP_MSG_KEY_RSP,
+	SNP_MSG_REPORT_REQ,
+	SNP_MSG_REPORT_RSP,
+	SNP_MSG_EXPORT_REQ,
+	SNP_MSG_EXPORT_RSP,
+	SNP_MSG_IMPORT_REQ,
+	SNP_MSG_IMPORT_RSP,
+	SNP_MSG_ABSORB_REQ,
+	SNP_MSG_ABSORB_RSP,
+	SNP_MSG_VMRK_REQ,
+	SNP_MSG_VMRK_RSP,
+
+	SNP_MSG_TYPE_MAX
+};
+
+enum aead_algo {
+	SNP_AEAD_INVALID,
+	SNP_AEAD_AES_256_GCM,
+};
+
+struct snp_guest_msg_hdr {
+	u8 authtag[MAX_AUTHTAG_LEN];
+	u64 msg_seqno;
+	u8 rsvd1[8];
+	u8 algo;
+	u8 hdr_version;
+	u16 hdr_sz;
+	u8 msg_type;
+	u8 msg_version;
+	u16 msg_sz;
+	u32 rsvd2;
+	u8 msg_vmpck;
+	u8 rsvd3[35];
+} __packed;
+
+struct snp_guest_msg {
+	struct snp_guest_msg_hdr hdr;
+	u8 payload[4000];
+} __packed;
+
+struct snp_guest_req {
+	void *req_buf;
+	size_t req_sz;
+
+	void *resp_buf;
+	size_t resp_sz;
+
+	void *data;
+	size_t data_npages;
+
+	u64 exit_code;
+	unsigned int vmpck_id;
+	u8 msg_version;
+	u8 msg_type;
+};
+
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 extern void __sev_es_ist_enter(struct pt_regs *regs);
 extern void __sev_es_ist_exit(void);
@@ -209,7 +274,8 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
 void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __init __noreturn snp_abort(void);
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio);
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
 u64 snp_get_unsupported_features(u64 status);
 u64 sev_get_status(void);
@@ -233,7 +299,8 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npa
 static inline void snp_set_wakeup_secondary_cpu(void) { }
 static inline bool snp_init(struct boot_params *bp) { return false; }
 static inline void snp_abort(void) { }
-static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+static inline int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+					  struct snp_guest_request_ioctl *rio)
 {
 	return -ENOTTY;
 }
arch/x86/kernel/sev.c +8 -7
index c67285824e82..3d6429321536 100644
@@ -2170,7 +2170,8 @@ static int __init init_sev_config(char *str)
 }
 __setup("sev=", init_sev_config);
 
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
@@ -2194,12 +2195,12 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn
 
 	vc_ghcb_invalidate(ghcb);
 
-	if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
-		ghcb_set_rax(ghcb, input->data_gpa);
-		ghcb_set_rbx(ghcb, input->data_npages);
+	if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
+		ghcb_set_rax(ghcb, __pa(req->data));
+		ghcb_set_rbx(ghcb, req->data_npages);
 	}
 
-	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa);
+	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, req->exit_code, input->req_gpa, input->resp_gpa);
 	if (ret)
 		goto e_put;
 
@@ -2214,8 +2215,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn
 
 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN):
 		/* Number of expected pages are returned in RBX */
-		if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
-			input->data_npages = ghcb_get_rbx(ghcb);
+		if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
+			req->data_npages = ghcb_get_rbx(ghcb);
 			ret = -ENOSPC;
 			break;
 		}
drivers/virt/coco/sev-guest/sev-guest.c +107 -87

406 lines not on this page · raw message

drivers/virt/coco/sev-guest/sev-guest.h deleted +0 -66

66 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

In reply to Nikunj A Dadhania
Quoted 36 lines · Nikunj A Dadhania

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters.

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev_guest.h.

---
  .../x86/include/asm}/sev-guest.h              |  18 +++
  arch/x86/include/asm/sev.h                    |   8 --
  arch/x86/kernel/sev.c                         |  15 ++-
  drivers/virt/coco/sev-guest/sev-guest.c       | 108 +++++++++++-------
  4 files changed, 93 insertions(+), 56 deletions(-)

rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (78%)

arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +8 -0
similarity index 78%
rename from drivers/virt/coco/sev-guest/sev-guest.h
rename to arch/x86/include/asm/sev-guest.h
index ceb798a404d6..27cc15ad6131 100644
@@ -63,4 +63,22 @@ struct snp_guest_msg {
  	u8 payload[4000];
  } __packed;
 
+struct snp_guest_req {
+	void *req_buf;
+	size_t req_sz;
+
+	void *resp_buf;
+	size_t resp_sz;
+
+	void *data;

arch/x86/include/asm/sev-guest.h struct snp_guest_msg { · line 83

Quoted 10 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +10 -0
+	size_t data_npages;
+
+	u64 exit_code;
+	unsigned int vmpck_id;
+	u8 msg_version;
+	u8 msg_type;
+};
+
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+			    struct snp_guest_request_ioctl *rio);

This seems odd to have in this file. It's arch/x86/kernel/sev.c that exports the call and so this should probably stay in arch/x86/include/asm/sev.h and put the struct there, too, no?

Thanks,
Tom

Quoted 34 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +0 -0
  #endif /* __VIRT_SEVGUEST_H__ */
arch/x86/include/asm/sev.h +0 -8
index 5b4a1ce3d368..78465a8c7dc6 100644
@@ -97,8 +97,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
  struct snp_req_data {
  	unsigned long req_gpa;
  	unsigned long resp_gpa;
-	unsigned long data_gpa;
-	unsigned int data_npages;
  };
 
  struct sev_guest_platform_data {
@@ -209,7 +207,6 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
  void snp_set_wakeup_secondary_cpu(void);
  bool snp_init(struct boot_params *bp);
  void __init __noreturn snp_abort(void);
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio);
  void snp_accept_memory(phys_addr_t start, phys_addr_t end);
  u64 snp_get_unsupported_features(u64 status);
  u64 sev_get_status(void);
@@ -233,11 +230,6 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npa
  static inline void snp_set_wakeup_secondary_cpu(void) { }
  static inline bool snp_init(struct boot_params *bp) { return false; }
  static inline void snp_abort(void) { }
-static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
-{
-	return -ENOTTY;
-}
-
  static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
  static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
  static inline u64 sev_get_status(void) { return 0; }
#17 lore raw

Re: [PATCH v7 03/16] virt: sev-guest: Add SNP guest request structure

Quoted 40 lines · Tom Lendacky
Quoted 39 lines · Nikunj A Dadhania

Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters.

Update snp_issue_guest_request() prototype to include the new guest request structure and move the prototype to sev_guest.h.

---
  .../x86/include/asm}/sev-guest.h              |  18 +++ arch/x86/include/asm/sev.h                    |   8 -- arch/x86/kernel/sev.c                         |  15 ++- drivers/virt/coco/sev-guest/sev-guest.c       | 108 +++++++++++-------

  4 files changed, 93 insertions(+), 56 deletions(-)

  rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (78%)

arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +11 -0
similarity index 78%
rename from drivers/virt/coco/sev-guest/sev-guest.h
rename to arch/x86/include/asm/sev-guest.h
index ceb798a404d6..27cc15ad6131 100644
@@ -63,4 +63,22 @@ struct snp_guest_msg {
       u8 payload[4000];
   } __packed;
   +struct snp_guest_req {
+    void *req_buf;
+    size_t req_sz;
+
+    void *resp_buf;
+    size_t resp_sz;
+
+    void *data;
+    size_t data_npages;
+
+    u64 exit_code;
+    unsigned int vmpck_id;
Quoted 8 lines

arch/x86/include/asm/sev-guest.h struct snp_guest_msg { · line 83

Quoted 6 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +6 -0
+    u8 msg_version;
+    u8 msg_type;
+};
+
+int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+                struct snp_guest_request_ioctl *rio);

This seems odd to have in this file. It's arch/x86/kernel/sev.c that exports the call and so this should probably stay in arch/x86/include/asm/sev.h and put the struct there, too, no?

The prototype is removed in 7/16, I have it here to make sure that compilation does not break with minimal churn.

Regards
Nikunj

#18 lore raw

PATCH v7 4/16 virt: sev-guest: Add vmpck_id to snp_guest_dev struct

In reply to Nikunj A Dadhania

Drop vmpck and os_area_msg_seqno pointers so that secret page layout does not need to be exposed to the sev-guest driver after the rework. Instead, add helper APIs to access vmpck and os_area_msg_seqno when needed. Added define for maximum supported VMPCK.

Also, change function is_vmpck_empty() to snp_is_vmpck_empty() in preparation for moving to sev.c.

---
 arch/x86/include/asm/sev.h              |  1 +
 drivers/virt/coco/sev-guest/sev-guest.c | 95 ++++++++++++-------------
 2 files changed, 48 insertions(+), 48 deletions(-)
arch/x86/include/asm/sev.h +1 -0
index 78465a8c7dc6..97ff8c28d3b8 100644
@@ -121,6 +121,7 @@ struct secrets_os_area {
 } __packed;
 
 #define VMPCK_KEY_LEN		32
+#define VMPCK_MAX_NUM		4
 
 /* See the SNP spec version 0.9 for secrets page format */
 struct snp_secrets_page_layout {
drivers/virt/coco/sev-guest/sev-guest.c +47 -48

196 lines not on this page · raw message

Signature · 1 line
2.34.1
#19 lore raw

PATCH v7 5/16 x86/sev: Cache the secrets page address

In reply to Nikunj A Dadhania

Save the secrets page address during snp_init() from the CC blob. Use secrets_pa instead of calling get_secrets_page() that remaps the CC blob for getting the secrets page every time.

---
 arch/x86/kernel/sev.c | 54 +++++++++++++++----------------------------
 1 file changed, 18 insertions(+), 36 deletions(-)
arch/x86/kernel/sev.c +18 -36
index fd89aca22f6a..6aa0bdf8a7a0 100644
@@ -72,6 +72,9 @@ static struct ghcb *boot_ghcb __section(".data");
 /* Bitmap of SEV features supported by the hypervisor */
 static u64 sev_hv_features __ro_after_init;
 
+/* Secrets page physical address from the CC blob */
+static u64 secrets_pa __ro_after_init;
+
 /* #VC handler runtime per-CPU data */
 struct sev_es_runtime_data {
 	struct ghcb ghcb_page;
@@ -598,45 +601,16 @@ void noinstr __sev_es_nmi_complete(void)
 	__sev_put_ghcb(&state);
 }
 
-static u64 __init get_secrets_page(void)
-{
-	u64 pa_data = boot_params.cc_blob_address;
-	struct cc_blob_sev_info info;
-	void *map;
-
-	/*
-	 * The CC blob contains the address of the secrets page, check if the
-	 * blob is present.
-	 */
-	if (!pa_data)
-		return 0;
-
-	map = early_memremap(pa_data, sizeof(info));
-	if (!map) {
-		pr_err("Unable to locate SNP secrets page: failed to map the Confidential Computing blob.\n");
-		return 0;
-	}
-	memcpy(&info, map, sizeof(info));
-	early_memunmap(map, sizeof(info));
-
-	/* smoke-test the secrets page passed */
-	if (!info.secrets_phys || info.secrets_len != PAGE_SIZE)
-		return 0;
-
-	return info.secrets_phys;
-}
-
 static u64 __init get_snp_jump_table_addr(void)
 {
 	struct snp_secrets_page_layout *layout;
 	void __iomem *mem;
-	u64 pa, addr;
+	u64 addr;
 
-	pa = get_secrets_page();
-	if (!pa)
+	if (!secrets_pa)
 		return 0;
 
-	mem = ioremap_encrypted(pa, PAGE_SIZE);
+	mem = ioremap_encrypted(secrets_pa, PAGE_SIZE);
 	if (!mem) {
 		pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n");
 		return 0;
@@ -2086,6 +2060,12 @@ static __init struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
 	return cc_info;
 }
 
+static void __init set_secrets_pa(const struct cc_blob_sev_info *cc_info)
+{
+	if (cc_info && cc_info->secrets_phys && cc_info->secrets_len == PAGE_SIZE)
+		secrets_pa = cc_info->secrets_phys;
+}
+
 bool __init snp_init(struct boot_params *bp)
 {
 	struct cc_blob_sev_info *cc_info;
@@ -2097,6 +2077,8 @@ bool __init snp_init(struct boot_params *bp)
 	if (!cc_info)
 		return false;
 
+	set_secrets_pa(cc_info);
+
 	setup_cpuid_table(cc_info);
 
 	/*
@@ -2249,16 +2231,16 @@ static struct platform_device sev_guest_device = {
 static int __init snp_init_platform_device(void)
 {
 	struct sev_guest_platform_data data;
-	u64 gpa;
 
 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
 		return -ENODEV;
 
-	gpa = get_secrets_page();
-	if (!gpa)
+	if (!secrets_pa) {
+		pr_err("SNP secrets page not found\n");
 		return -ENODEV;
+	}
 
-	data.secrets_gpa = gpa;
+	data.secrets_gpa = secrets_pa;
 	if (platform_device_add_data(&sev_guest_device, &data, sizeof(data)))
 		return -ENODEV;
 
Signature · 1 line
2.34.1
#20 lore raw

PATCH v7 6/16 virt: sev-guest: Move SNP Guest command mutex

In reply to Nikunj A Dadhania

SNP command mutex is used to serialize the shared buffer access, command handling and message sequence number races. Move the SNP guest command mutex out of the sev guest driver and provide accessors to sev-guest driver. Remove multiple lockdep check in sev-guest driver, next patch adds a single lockdep check in snp_send_guest_request().

---
 arch/x86/include/asm/sev-guest.h        |  3 +++
 arch/x86/kernel/sev.c                   | 21 +++++++++++++++++++++
 drivers/virt/coco/sev-guest/sev-guest.c | 23 +++++++----------------
 3 files changed, 31 insertions(+), 16 deletions(-)
arch/x86/include/asm/sev-guest.h +3 -0
index 27cc15ad6131..2f3cceb88396 100644
@@ -81,4 +81,7 @@ struct snp_guest_req {
 
 int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
 			    struct snp_guest_request_ioctl *rio);
+void snp_guest_cmd_lock(void);
+void snp_guest_cmd_unlock(void);
+
 #endif /* __VIRT_SEVGUEST_H__ */
arch/x86/kernel/sev.c +21 -0
index 6aa0bdf8a7a0..191193924b22 100644
@@ -941,6 +941,21 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
 		free_page((unsigned long)vmsa);
 }
 
+/*  SNP Guest command mutex to serialize the shared buffer access and command handling. */
+static struct mutex snp_guest_cmd_mutex;
+
+void snp_guest_cmd_lock(void)
+{
+	mutex_lock(&snp_guest_cmd_mutex);
+}
+EXPORT_SYMBOL_GPL(snp_guest_cmd_lock);
+
+void snp_guest_cmd_unlock(void)
+{
+	mutex_unlock(&snp_guest_cmd_mutex);
+}
+EXPORT_SYMBOL_GPL(snp_guest_cmd_unlock);
+
 static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
 {
 	struct sev_es_save_area *cur_vmsa, *vmsa;
@@ -2240,6 +2255,12 @@ static int __init snp_init_platform_device(void)
 		return -ENODEV;
 	}
 
+	/*
+	 * Initialize snp command mutex that is used to serialize the shared
+	 * buffer access and use of the vmpck and message sequence number
+	 */
+	mutex_init(&snp_guest_cmd_mutex);
+
 	data.secrets_gpa = secrets_pa;
 	if (platform_device_add_data(&sev_guest_device, &data, sizeof(data)))
 		return -ENODEV;
drivers/virt/coco/sev-guest/sev-guest.c +7 -16
index 9c0ff69a16da..bd30a9ff82c1 100644
@@ -63,9 +63,6 @@ static u32 vmpck_id;
 module_param(vmpck_id, uint, 0444);
 MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
 
-/* Mutex to serialize the shared buffer access and command handling. */
-static DEFINE_MUTEX(snp_cmd_mutex);
-
 static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
 {
 	return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
@@ -115,8 +112,6 @@ static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev)
 	u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev);
 	u64 count;
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
 	/* Read the current message sequence counter from secrets pages */
 	count = *os_area_msg_seqno;
 
@@ -409,8 +404,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io
 	struct snp_report_resp *resp;
 	int rc, resp_len;
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
 	if (!arg->req_data || !arg->resp_data)
 		return -EINVAL;
 
@@ -457,8 +450,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque
 	/* Response data is 64 bytes and max authsize for GCM is 16 bytes. */
 	u8 buf[64 + 16];
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
 	if (!arg->req_data || !arg->resp_data)
 		return -EINVAL;
 
@@ -507,8 +498,6 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
 	sockptr_t certs_address;
 	int ret, resp_len;
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
 	if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data))
 		return -EINVAL;
 
@@ -604,12 +593,12 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
 	if (!input.msg_version)
 		return -EINVAL;
 
-	mutex_lock(&snp_cmd_mutex);
+	snp_guest_cmd_lock();
 
 	/* Check if the VMPCK is not empty */
 	if (snp_is_vmpck_empty(snp_dev)) {
 		dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
-		mutex_unlock(&snp_cmd_mutex);
+		snp_guest_cmd_unlock();
 		return -ENOTTY;
 	}
 
@@ -634,7 +623,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
 		break;
 	}
 
-	mutex_unlock(&snp_cmd_mutex);
+	snp_guest_cmd_unlock();
 
 	if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input)))
 		return -EFAULT;
@@ -724,14 +713,14 @@ static int sev_report_new(struct tsm_report *report, void *data)
 	if (!buf)
 		return -ENOMEM;
 
-	guard(mutex)(&snp_cmd_mutex);
-
 	/* Check if the VMPCK is not empty */
 	if (snp_is_vmpck_empty(snp_dev)) {
 		dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
 		return -ENOTTY;
 	}
 
+	snp_guest_cmd_lock();
+
 	cert_table = buf + report_size;
 	struct snp_ext_report_req ext_req = {
 		.data = { .vmpl = desc->privlevel },
@@ -752,6 +741,8 @@ static int sev_report_new(struct tsm_report *report, void *data)
 	};
 
 	ret = get_ext_report(snp_dev, &input, &io);
+	snp_guest_cmd_unlock();
+
 	if (ret)
 		return ret;
 
Signature · 1 line
2.34.1
Reviewed-by on v7 6/16
#21 lore raw

Re: [PATCH v7 06/16] virt: sev-guest: Move SNP Guest command mutex

Quoted 30 lines · Nikunj A Dadhania

SNP command mutex is used to serialize the shared buffer access, command handling and message sequence number races. Move the SNP guest command mutex out of the sev guest driver and provide accessors to sev-guest driver. Remove multiple lockdep check in sev-guest driver, next patch adds a single lockdep check in snp_send_guest_request().

---
  arch/x86/include/asm/sev-guest.h        |  3 +++
  arch/x86/kernel/sev.c                   | 21 +++++++++++++++++++++
  drivers/virt/coco/sev-guest/sev-guest.c | 23 +++++++----------------
  3 files changed, 31 insertions(+), 16 deletions(-)
arch/x86/include/asm/sev-guest.h +3 -0
index 27cc15ad6131..2f3cceb88396 100644
@@ -81,4 +81,7 @@ struct snp_guest_req {
 
  int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
  			    struct snp_guest_request_ioctl *rio);
+void snp_guest_cmd_lock(void);
+void snp_guest_cmd_unlock(void);
+
  #endif /* __VIRT_SEVGUEST_H__ */
arch/x86/kernel/sev.c +0 -0
index 6aa0bdf8a7a0..191193924b22 100644

arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 945

Quoted 5 lines
arch/x86/kernel/sev.c +2 -0
@@ -941,6 +941,21 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
  		free_page((unsigned long)vmsa);
  }
 
+/*  SNP Guest command mutex to serialize the shared buffer access and command handling. */
+static struct mutex snp_guest_cmd_mutex;

You should probably use:

static DEFINE_MUTEX(snp_guest_cmd_mutex);

That way you can avoid the initialization in snp_init_platform_device().

With that:

Quoted 129 lines
arch/x86/kernel/sev.c +19 -0
+
+void snp_guest_cmd_lock(void)
+{
+	mutex_lock(&snp_guest_cmd_mutex);
+}
+EXPORT_SYMBOL_GPL(snp_guest_cmd_lock);
+
+void snp_guest_cmd_unlock(void)
+{
+	mutex_unlock(&snp_guest_cmd_mutex);
+}
+EXPORT_SYMBOL_GPL(snp_guest_cmd_unlock);
+
  static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
  {
  	struct sev_es_save_area *cur_vmsa, *vmsa;
@@ -2240,6 +2255,12 @@ static int __init snp_init_platform_device(void)
  		return -ENODEV;
  	}
 
+	/*
+	 * Initialize snp command mutex that is used to serialize the shared
+	 * buffer access and use of the vmpck and message sequence number
+	 */
+	mutex_init(&snp_guest_cmd_mutex);
+
  	data.secrets_gpa = secrets_pa;
  	if (platform_device_add_data(&sev_guest_device, &data, sizeof(data)))
  		return -ENODEV;
drivers/virt/coco/sev-guest/sev-guest.c +7 -16
index 9c0ff69a16da..bd30a9ff82c1 100644
@@ -63,9 +63,6 @@ static u32 vmpck_id;
  module_param(vmpck_id, uint, 0444);
  MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
 
-/* Mutex to serialize the shared buffer access and command handling. */
-static DEFINE_MUTEX(snp_cmd_mutex);
-
  static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
  {
  	return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
@@ -115,8 +112,6 @@ static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev)
  	u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev);
  	u64 count;
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
  	/* Read the current message sequence counter from secrets pages */
  	count = *os_area_msg_seqno;
 
@@ -409,8 +404,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io
  	struct snp_report_resp *resp;
  	int rc, resp_len;
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
  	if (!arg->req_data || !arg->resp_data)
  		return -EINVAL;
 
@@ -457,8 +450,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque
  	/* Response data is 64 bytes and max authsize for GCM is 16 bytes. */
  	u8 buf[64 + 16];
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
  	if (!arg->req_data || !arg->resp_data)
  		return -EINVAL;
 
@@ -507,8 +498,6 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
  	sockptr_t certs_address;
  	int ret, resp_len;
 
-	lockdep_assert_held(&snp_cmd_mutex);
-
  	if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data))
  		return -EINVAL;
 
@@ -604,12 +593,12 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
  	if (!input.msg_version)
  		return -EINVAL;
 
-	mutex_lock(&snp_cmd_mutex);
+	snp_guest_cmd_lock();
 
  	/* Check if the VMPCK is not empty */
  	if (snp_is_vmpck_empty(snp_dev)) {
  		dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
-		mutex_unlock(&snp_cmd_mutex);
+		snp_guest_cmd_unlock();
  		return -ENOTTY;
  	}
 
@@ -634,7 +623,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
  		break;
  	}
 
-	mutex_unlock(&snp_cmd_mutex);
+	snp_guest_cmd_unlock();
 
  	if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input)))
  		return -EFAULT;
@@ -724,14 +713,14 @@ static int sev_report_new(struct tsm_report *report, void *data)
  	if (!buf)
  		return -ENOMEM;
 
-	guard(mutex)(&snp_cmd_mutex);
-
  	/* Check if the VMPCK is not empty */
  	if (snp_is_vmpck_empty(snp_dev)) {
  		dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
  		return -ENOTTY;
  	}
 
+	snp_guest_cmd_lock();
+
  	cert_table = buf + report_size;
  	struct snp_ext_report_req ext_req = {
  		.data = { .vmpl = desc->privlevel },
@@ -752,6 +741,8 @@ static int sev_report_new(struct tsm_report *report, void *data)
  	};
 
  	ret = get_ext_report(snp_dev, &input, &io);
+	snp_guest_cmd_unlock();
+
  	if (ret)
  		return ret;
 
#22 lore raw

Re: [PATCH v7 06/16] virt: sev-guest: Move SNP Guest command mutex

Quoted 37 lines · Tom Lendacky
Quoted 31 lines · Nikunj A Dadhania

SNP command mutex is used to serialize the shared buffer access, command handling and message sequence number races. Move the SNP guest command mutex out of the sev guest driver and provide accessors to sev-guest driver. Remove multiple lockdep check in sev-guest driver, next patch adds a single lockdep check in snp_send_guest_request().

---
  arch/x86/include/asm/sev-guest.h        |  3 +++ arch/x86/kernel/sev.c                   | 21 +++++++++++++++++++++ drivers/virt/coco/sev-guest/sev-guest.c | 23 +++++++----------------

  3 files changed, 31 insertions(+), 16 deletions(-)
arch/x86/include/asm/sev-guest.h +3 -0
index 27cc15ad6131..2f3cceb88396 100644
@@ -81,4 +81,7 @@ struct snp_guest_req {
     int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
                   struct snp_guest_request_ioctl *rio);
+void snp_guest_cmd_lock(void);
+void snp_guest_cmd_unlock(void);
+
   #endif /* __VIRT_SEVGUEST_H__ */
arch/x86/kernel/sev.c +0 -0
index 6aa0bdf8a7a0..191193924b22 100644
@@ -941,6 +941,21 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
           free_page((unsigned long)vmsa);
   }
Quoted 2 lines

  +/*  SNP Guest command mutex to serialize the shared buffer access and command handling. */ +static struct mutex snp_guest_cmd_mutex;

You should probably use:

Quoted 7 lines

static DEFINE_MUTEX(snp_guest_cmd_mutex);

That way you can avoid the initialization in snp_init_platform_device().

With that:

Sure, will change.

Regards
Nikunj

#23 lore raw

PATCH v7 7/16 x86/sev: Move and reorganize sev guest request api

In reply to Nikunj A Dadhania

For enabling Secure TSC, SEV-SNP guests need to communicate with the AMD Security Processor early during boot. Many of the required functions are implemented in the sev-guest driver and therefore not available at early boot. Move the required functions and provide API to the sev guest driver for sending guest message and vmpck routines.

As there is no external caller for snp_issue_guest_request() anymore, make it static and drop the prototype from sev-guest.h.

---
 arch/x86/Kconfig                        |   1 +
 arch/x86/include/asm/sev-guest.h        |  87 ++++-
 arch/x86/include/asm/sev.h              |  10 -
 arch/x86/kernel/sev.c                   | 448 +++++++++++++++++++++-
 drivers/virt/coco/sev-guest/Kconfig     |   1 -
 drivers/virt/coco/sev-guest/sev-guest.c | 478 ++----------------------
 6 files changed, 546 insertions(+), 479 deletions(-)
arch/x86/Kconfig +1 -0
index 3762f41bb092..b8f374ec5651 100644
@@ -1534,6 +1534,7 @@ config AMD_MEM_ENCRYPT
 	select ARCH_HAS_CC_PLATFORM
 	select X86_MEM_ENCRYPT
 	select UNACCEPTED_MEMORY
+	select CRYPTO_LIB_AESGCM
 	help
 	  Say yes to enable support for the encryption of system memory.
 	  This requires an AMD processor that supports Secure Memory
arch/x86/include/asm/sev-guest.h +85 -2

111 lines not on this page · raw message

arch/x86/include/asm/sev.h +0 -10
index 97ff8c28d3b8..f8377b49b88d 100644
@@ -93,16 +93,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
 
 #define RMPADJUST_VMSA_PAGE_BIT		BIT(16)
 
-/* SNP Guest message request */
-struct snp_req_data {
-	unsigned long req_gpa;
-	unsigned long resp_gpa;
-};
-
-struct sev_guest_platform_data {
-	u64 secrets_gpa;
-};
-
 /*
  * The secrets page contains 96-bytes of reserved field that can be used by
  * the guest OS. The guest OS uses the area to save the message sequence
arch/x86/kernel/sev.c +437 -11

488 lines not on this page · raw message

drivers/virt/coco/sev-guest/Kconfig +0 -1
index 0b772bd921d8..a6405ab6c2c3 100644
@@ -2,7 +2,6 @@ config SEV_GUEST
 	tristate "AMD SEV Guest driver"
 	default m
 	depends on AMD_MEM_ENCRYPT
-	select CRYPTO_LIB_AESGCM
 	select TSM_REPORTS
 	help
 	  SEV-SNP firmware provides the guest a mechanism to communicate with
drivers/virt/coco/sev-guest/sev-guest.c +23 -455

567 lines not on this page · raw message

Signature · 1 line
2.34.1
#24 lore raw

PATCH v7 8/16 x86/mm: Add generic guest initialization hook

In reply to Nikunj A Dadhania

Add generic enc_init guest hook for performing any type of initialization that is vendor specific. Generic enc_init hook can be used for early guest feature initialization before secondary processors are up.

---
 arch/x86/include/asm/x86_init.h | 2 ++
 arch/x86/kernel/x86_init.c      | 2 ++
 arch/x86/mm/mem_encrypt.c       | 2 ++
 3 files changed, 6 insertions(+)
arch/x86/include/asm/x86_init.h +2 -0
index c878616a18b8..8095553e14a7 100644
@@ -148,12 +148,14 @@ struct x86_init_acpi {
  * @enc_status_change_finish	Notify HV after the encryption status of a range is changed
  * @enc_tlb_flush_required	Returns true if a TLB flush is needed before changing page encryption status
  * @enc_cache_flush_required	Returns true if a cache flush is needed before changing page encryption status
+ * @enc_init			Prepare and initialize encryption features
  */
 struct x86_guest {
 	bool (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc);
 	bool (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc);
 	bool (*enc_tlb_flush_required)(bool enc);
 	bool (*enc_cache_flush_required)(void);
+	void (*enc_init)(void);
 };
 
 /**
arch/x86/kernel/x86_init.c +2 -0
index a37ebd3b4773..a07985a96ca5 100644
@@ -136,6 +136,7 @@ static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool
 static bool enc_tlb_flush_required_noop(bool enc) { return false; }
 static bool enc_cache_flush_required_noop(void) { return false; }
 static bool is_private_mmio_noop(u64 addr) {return false; }
+static void enc_init_noop(void) { }
 
 struct x86_platform_ops x86_platform __ro_after_init = {
 	.calibrate_cpu			= native_calibrate_cpu_early,
@@ -158,6 +159,7 @@ struct x86_platform_ops x86_platform __ro_after_init = {
 		.enc_status_change_finish  = enc_status_change_finish_noop,
 		.enc_tlb_flush_required	   = enc_tlb_flush_required_noop,
 		.enc_cache_flush_required  = enc_cache_flush_required_noop,
+		.enc_init		   = enc_init_noop,
 	},
 };
 
arch/x86/mm/mem_encrypt.c +2 -0
index c290c55b632b..d5bcd63211de 100644
@@ -85,6 +85,8 @@ void __init mem_encrypt_init(void)
 	/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
 	swiotlb_update_mem_attributes();
 
+	x86_platform.guest.enc_init();
+
 	print_mem_encrypt_feature_info();
 }
 
Signature · 1 line
2.34.1
#25 lore raw

PATCH v7 9/16 x86/cpufeatures: Add synthetic Secure TSC bit

In reply to Nikunj A Dadhania

Add support for the synthetic CPUID flag which indicates that the SNP guest is running with secure tsc enabled (MSR_AMD64_SEV Bit 11 - SecureTsc_Enabled) . This flag is there so that this capability in the guests can be detected easily without reading MSRs every time accessors.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 4af140cf5719..e9dafc8cd9dc 100644
@@ -237,6 +237,7 @@
 #define X86_FEATURE_PVUNLOCK		( 8*32+20) /* "" PV unlock function */
 #define X86_FEATURE_VCPUPREEMPT		( 8*32+21) /* "" PV vcpu_is_preempted function */
 #define X86_FEATURE_TDX_GUEST		( 8*32+22) /* Intel Trust Domain Extensions Guest */
+#define X86_FEATURE_SNP_SECURE_TSC	( 8*32+23) /* "" AMD SNP Secure TSC */
 
 /* Intel-defined CPU features, CPUID level 0x00000007:0 (EBX), word 9 */
 #define X86_FEATURE_FSGSBASE		( 9*32+ 0) /* RDFSBASE, WRFSBASE, RDGSBASE, WRGSBASE instructions*/
Signature · 1 line
2.34.1
#26 lore raw

PATCH v7 10/16 x86/sev: Add Secure TSC support for SNP guests

In reply to Nikunj A Dadhania

Add support for Secure TSC in SNP enabled guests. Secure TSC allows guest to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched.

During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal AES GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.

Use the guest enc_init hook to fetch SNP TSC info from the AMD Security Processor and initialize the snp_tsc_scale and snp_tsc_offset. During secondary CPU initialization set VMSA fields GUEST_TSC_SCALE (offset 2F0h) and GUEST_TSC_OFFSET(offset 2F8h) with snp_tsc_scale and snp_tsc_offset respectively.

---
 arch/x86/include/asm/sev-common.h |   1 +
 arch/x86/include/asm/sev-guest.h  |  20 ++++++
 arch/x86/include/asm/sev.h        |   2 +
 arch/x86/include/asm/svm.h        |   6 +-
 arch/x86/kernel/sev.c             | 107 ++++++++++++++++++++++++++++--
 arch/x86/mm/mem_encrypt_amd.c     |   6 ++
 6 files changed, 133 insertions(+), 9 deletions(-)
arch/x86/include/asm/sev-common.h +1 -0
index b463fcbd4b90..6adc8e27feeb 100644
@@ -159,6 +159,7 @@ struct snp_psc_desc {
 #define GHCB_TERM_NOT_VMPL0		3	/* SNP guest is not running at VMPL-0 */
 #define GHCB_TERM_CPUID			4	/* CPUID-validation failure */
 #define GHCB_TERM_CPUID_HV		5	/* CPUID failure during hypervisor fallback */
+#define GHCB_TERM_SECURE_TSC		6	/* Secure TSC initialization failed */
 
 #define GHCB_RESP_CODE(v)		((v) & GHCB_MSR_INFO_MASK)
 
arch/x86/include/asm/sev-guest.h +20 -0
index ed5c158ec29b..c82c78571020 100644
@@ -39,6 +39,8 @@ enum msg_type {
 	SNP_MSG_ABSORB_RSP,
 	SNP_MSG_VMRK_REQ,
 	SNP_MSG_VMRK_RSP,
+	SNP_MSG_TSC_INFO_REQ = 17,
+	SNP_MSG_TSC_INFO_RSP,
 
 	SNP_MSG_TYPE_MAX
 };
@@ -83,6 +85,23 @@ struct sev_guest_platform_data {
 	struct snp_req_data input;
 };
 
+#define SNP_TSC_INFO_REQ_SZ 128
+
+struct snp_tsc_info_req {
+	/* Must be zero filled */
+	u8 rsvd[SNP_TSC_INFO_REQ_SZ];
+} __packed;
+
+struct snp_tsc_info_resp {
+	/* Status of TSC_INFO message */
+	u32 status;
+	u32 rsvd1;
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u32 tsc_factor;
+	u8 rsvd2[100];
+} __packed;
+
 struct snp_guest_dev {
 	struct device *dev;
 	struct miscdevice misc;
@@ -102,6 +121,7 @@ struct snp_guest_dev {
 		struct snp_report_req report;
 		struct snp_derived_key_req derived_key;
 		struct snp_ext_report_req ext_report;
+		struct snp_tsc_info_req tsc_info;
 	} req;
 	unsigned int vmpck_id;
 };
arch/x86/include/asm/sev.h +2 -0
index f8377b49b88d..880cfee3c3ad 100644
@@ -201,6 +201,7 @@ void __init __noreturn snp_abort(void);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
 u64 snp_get_unsupported_features(u64 status);
 u64 sev_get_status(void);
+void __init snp_secure_tsc_prepare(void);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
@@ -224,6 +225,7 @@ static inline void snp_abort(void) { }
 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
 static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
 static inline u64 sev_get_status(void) { return 0; }
+static inline void __init snp_secure_tsc_prepare(void) { }
 #endif
 
 #endif
arch/x86/include/asm/svm.h +4 -2
index 87a7b917d30e..3a8294bbd109 100644
@@ -410,7 +410,9 @@ struct sev_es_save_area {
 	u8 reserved_0x298[80];
 	u32 pkru;
 	u32 tsc_aux;
-	u8 reserved_0x2f0[24];
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u8 reserved_0x300[8];
 	u64 rcx;
 	u64 rdx;
 	u64 rbx;
@@ -542,7 +544,7 @@ static inline void __unused_size_checks(void)
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298);
-	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0);
+	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x300);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
arch/x86/kernel/sev.c +100 -7

142 lines not on this page · raw message

arch/x86/mm/mem_encrypt_amd.c +6 -0
index 70b91de2e053..c81b57ca03b6 100644
@@ -214,6 +214,11 @@ void __init sme_map_bootdata(char *real_mode_data)
 	__sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
 }
 
+static void __init amd_enc_init(void)
+{
+	snp_secure_tsc_prepare();
+}
+
 static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)
 {
 	unsigned long pfn = 0;
@@ -467,6 +472,7 @@ void __init sme_early_init(void)
 	x86_platform.guest.enc_status_change_finish  = amd_enc_status_change_finish;
 	x86_platform.guest.enc_tlb_flush_required    = amd_enc_tlb_flush_required;
 	x86_platform.guest.enc_cache_flush_required  = amd_enc_cache_flush_required;
+	x86_platform.guest.enc_init                  = amd_enc_init;
 
 	/*
 	 * AMD-SEV-ES intercepts the RDMSR to read the X2APIC ID in the
Signature · 1 line
2.34.1
maintainer
#27 lore raw

Re: [PATCH v7 10/16] x86/sev: Add Secure TSC support for SNP guests

arch/x86/kernel/sev.c static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip) · line 1145

Quoted 5 lines · Nikunj A Dadhania
arch/x86/kernel/sev.c +5 -0
+       /* Setting Secure TSC parameters */
+       if (cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC)) {
+               vmsa->tsc_scale = snp_tsc_scale;
+               vmsa->tsc_offset = snp_tsc_offset;
+       }

This needs to use guest_cpu_has, otherwise updating the hypervisor or processor will change the initial VMSA and any measurement derived from there.

In fact, the same issue exists for DEBUG_SWAP and I will shortly post a series to allow enabling/disabling DEBUG_SWAP per-VM, so that updating the kernel does not break existing measurements.

Paolo

maintainer
#28 lore raw

Re: [PATCH v7 10/16] x86/sev: Add Secure TSC support for SNP guests

Quoted 9 lines · Paolo Bonzini

arch/x86/kernel/sev.c static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip) · line 1145

Quoted 5 lines · Nikunj A Dadhania
arch/x86/kernel/sev.c +5 -0
+       /* Setting Secure TSC parameters */
+       if (cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC)) {
+               vmsa->tsc_scale = snp_tsc_scale;
+               vmsa->tsc_offset = snp_tsc_offset;
+       }
Quoted 7 lines

This needs to use guest_cpu_has, otherwise updating the hypervisor or processor will change the initial VMSA and any measurement derived from there.

In fact, the same issue exists for DEBUG_SWAP and I will shortly post a series to allow enabling/disabling DEBUG_SWAP per-VM, so that updating the kernel does not break existing measurements.

Nevermind, I keep confusing guest and host-side patches.

/me goes in the corner.

Paolo

#29 lore raw

PATCH v7 11/16 x86/sev: Change TSC MSR behavior for Secure TSC enabled guests

In reply to Nikunj A Dadhania

Secure TSC enabled guests should not write MSR_IA32_TSC(10H) register as the subsequent TSC value reads are undefined. MSR_IA32_TSC related accesses should not exit to the hypervisor for such guests.

Accesses to MSR_IA32_TSC needs special handling in the #VC handler for the guests with Secure TSC enabled. Writes to MSR_IA32_TSC should be ignored, and reads of MSR_IA32_TSC should return the result of the RDTSC instruction.

---
 arch/x86/kernel/sev.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/x86/kernel/sev.c +24 -0
index 1d6200b57643..393d3be13934 100644
@@ -1280,6 +1280,30 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	/* Is it a WRMSR? */
 	exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;
 
+	/*
+	 * TSC related accesses should not exit to the hypervisor when a
+	 * guest is executing with SecureTSC enabled, so special handling
+	 * is required for accesses of MSR_IA32_TSC:
+	 *
+	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads
+	 *         of the TSC to return undefined values, so ignore all
+	 *         writes.
+	 * Reads:  Reads of MSR_IA32_TSC should return the current TSC
+	 *         value, use the value returned by RDTSC.
+	 */
+	if (regs->cx == MSR_IA32_TSC && cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC)) {
+		u64 tsc;
+
+		if (exit_info_1)
+			return ES_OK;
+
+		tsc = rdtsc();
+		regs->ax = UINT_MAX & tsc;
+		regs->dx = UINT_MAX & (tsc >> 32);
+
+		return ES_OK;
+	}
+
 	ghcb_set_rcx(ghcb, regs->cx);
 	if (exit_info_1) {
 		ghcb_set_rax(ghcb, regs->ax);
Signature · 1 line
2.34.1
#30 lore raw

PATCH v7 12/16 x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests

In reply to Nikunj A Dadhania

The hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions are being intercepted. If this should occur and Secure TSC is enabled, terminate guest execution.

---
 arch/x86/kernel/sev-shared.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/kernel/sev-shared.c +10 -0
index ccb0915e84e1..716aea6fc90c 100644
@@ -991,6 +991,16 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
 	bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
 	enum es_result ret;
 
+	/*
+	 * RDTSC and RDTSCP should not be intercepted when Secure TSC is
+	 * enabled. Terminate the SNP guest when the interception is enabled.
+	 * This file is included from kernel/sev.c and boot/compressed/sev.c,
+	 * use sev_status here as cpu_feature_enabled() is not available when
+	 * compiling boot/compressed/sev.c.
+	 */
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+		return ES_VMM_ERROR;
+
 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
 	if (ret != ES_OK)
 		return ret;
Signature · 1 line
2.34.1
#31 lore raw

PATCH v7 13/16 x86/kvmclock: Skip kvmclock when Secure TSC is available

In reply to Nikunj A Dadhania

For AMD SNP guests having Secure TSC enabled, skip using the kvmclock. The guest kernel will fallback and use Secure TSC based clocksource.

---
 arch/x86/kernel/kvmclock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kvmclock.c +1 -1
index fb8f52149be9..e3de354abf74 100644
@@ -288,7 +288,7 @@ void __init kvmclock_init(void)
 {
 	u8 flags;
 
-	if (!kvm_para_available() || !kvmclock)
+	if (!kvm_para_available() || !kvmclock || cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC))
 		return;
 
 	if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
Signature · 1 line
2.34.1
#32 lore raw

PATCH v7 14/16 x86/sev: Mark Secure TSC as reliable

In reply to Nikunj A Dadhania

AMD SNP guests may have Secure TSC feature enabled. Use the Secure TSC as the only reliable clock source in SEV-SNP guests when enabled, bypassing unstable calibration.

---
 arch/x86/mm/mem_encrypt_amd.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/mm/mem_encrypt_amd.c +4 -0
index c81b57ca03b6..cc936999efc8 100644
@@ -498,6 +498,10 @@ void __init sme_early_init(void)
 	 */
 	if (sev_status & MSR_AMD64_SEV_ENABLED)
 		ia32_disable();
+
+	/* Mark the TSC as reliable when Secure TSC is enabled */
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 }
 
 void __init mem_encrypt_free_decrypted_mem(void)
Signature · 1 line
2.34.1
#33 lore raw

PATCH v7 15/16 x86/cpu/amd: Do not print FW_BUG for Secure TSC

In reply to Nikunj A Dadhania

When SecureTSC is enabled and TscInvariant (bit 8) in CPUID_8000_0007_edx is set, kernel complains with the below firmware bug:

[Firmware Bug]: TSC doesn't count with P0 frequency!

Secure TSC need not run at P0 frequency, the TSC frequency is set by the VMM as part of the SNP_LAUNCH_START command. Avoid the check when Secure TSC is enabled

---
 arch/x86/kernel/cpu/amd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +2 -1
index f322ebd053a9..b4a19e3fda8d 100644
@@ -551,7 +551,8 @@ static void early_init_amd_mc(struct cpuinfo_x86 *c)
 
 static void bsp_init_amd(struct cpuinfo_x86 *c)
 {
-	if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
+	if (cpu_has(c, X86_FEATURE_CONSTANT_TSC) &&
+	    !cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC)) {
 
 		if (c->x86 > 0x10 ||
 		    (c->x86 == 0x10 && c->x86_model >= 0x2)) {
Signature · 1 line
2.34.1
#34 lore raw

PATCH v7 16/16 x86/sev: Enable Secure TSC for SNP guests

In reply to Nikunj A Dadhania

Now that all the required plumbing is done for enabling SNP Secure TSC feature, add Secure TSC to snp features present list.

Set the CPUID feature bit (X86_FEATURE_SNP_SECURE_TSC) when SNP guest is started with Secure TSC.

---
 arch/x86/boot/compressed/sev.c |  3 ++-
 arch/x86/mm/mem_encrypt.c      | 10 ++++++++--
 arch/x86/mm/mem_encrypt_amd.c  |  4 +++-
 3 files changed, 13 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/sev.c +2 -1
index 454acd7a2daf..2829908602e5 100644
@@ -375,7 +375,8 @@ static void enforce_vmpl0(void)
  * by the guest kernel. As and when a new feature is implemented in the
  * guest kernel, a corresponding bit should be added to the mask.
  */
-#define SNP_FEATURES_PRESENT	MSR_AMD64_SNP_DEBUG_SWAP
+#define SNP_FEATURES_PRESENT	(MSR_AMD64_SNP_DEBUG_SWAP |	\
+				 MSR_AMD64_SNP_SECURE_TSC)
 
 u64 snp_get_unsupported_features(u64 status)
 {
arch/x86/mm/mem_encrypt.c +8 -2
index d5bcd63211de..b0db76dc4a9d 100644
@@ -70,8 +70,14 @@ static void print_mem_encrypt_feature_info(void)
 		pr_cont(" SEV-ES");
 
 	/* Secure Nested Paging */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		pr_cont(" SEV-SNP");
+	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
+		pr_cont(" SEV-SNP\n");
+		pr_cont("SNP Features active: ");
+
+		/* SNP Secure TSC */
+		if (cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC))
+			pr_cont(" SECURE-TSC");
+	}
 
 	pr_cont("\n");
 }
arch/x86/mm/mem_encrypt_amd.c +3 -1
index cc936999efc8..7ee0a537a22e 100644
@@ -500,8 +500,10 @@ void __init sme_early_init(void)
 		ia32_disable();
 
 	/* Mark the TSC as reliable when Secure TSC is enabled */
-	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC) {
+		setup_force_cpu_cap(X86_FEATURE_SNP_SECURE_TSC);
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
+	}
 }
 
 void __init mem_encrypt_free_decrypted_mem(void)
Signature · 1 line
2.34.1
#35 lore raw
In reply to Nikunj A Dadhania
Quoted 43 lines · Nikunj A Dadhania

Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC".

During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at
https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info"

Use a minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the AMD Security Processor which is available at early boot.

SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before smp cpus are started, move most of the sev-guest driver code to kernel/sev.c and provide well defined APIs to the sev-guest driver to use the interface to avoid code-duplication.

Patches:
01-08: Preparation and movement of sev-guest driver code 09-16: SecureTSC enablement patches.

Testing SecureTSC
-----------------

SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series:
https://github.com/nikunjad/linux/tree/snp-host-latest-securetsc_v5

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp_securetsc_v5

QEMU commandline SEV-SNP-UPM with SecureTSC:

  qemu-system-x86_64 -cpu EPYC-Milan-v2,+secure-tsc,+invtsc -smp 4 \
    -object memory-backend-memfd-private,id=ram1,size=1G,share=true \
    -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \
    -machine q35,confidential-guest-support=sev0,memory-backend=ram1,kvm-type=snp \
    ...
Quoted 8 lines

Changelog:
----------
v7:
* Drop mutex from the snp_dev and add snp_guest_cmd_{lock,unlock} API
* Added comments for secrets page failure
* Added define for maximum supported VMPCK
* Updated comments why sev_status is used directly instead of cpu_feature_enabled()

A gentle reminder.

Regards
Nikunj

#36 lore raw
Quoted 54 lines · Nikunj A. Dadhania
Quoted 43 lines · Nikunj A Dadhania

Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC".

During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at
https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info"

Use a minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the AMD Security Processor which is available at early boot.

SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before smp cpus are started, move most of the sev-guest driver code to kernel/sev.c and provide well defined APIs to the sev-guest driver to use the interface to avoid code-duplication.

Patches:
01-08: Preparation and movement of sev-guest driver code 09-16: SecureTSC enablement patches.

Testing SecureTSC
-----------------

SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series:
https://github.com/nikunjad/linux/tree/snp-host-latest-securetsc_v5

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp_securetsc_v5

QEMU commandline SEV-SNP-UPM with SecureTSC:

  qemu-system-x86_64 -cpu EPYC-Milan-v2,+secure-tsc,+invtsc -smp 4 \
    -object memory-backend-memfd-private,id=ram1,size=1G,share=true \
    -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \
    -machine q35,confidential-guest-support=sev0,memory-backend=ram1,kvm-type=snp \
    ...
Quoted 8 lines

Changelog:
----------
v7:
* Drop mutex from the snp_dev and add snp_guest_cmd_{lock,unlock} API
* Added comments for secrets page failure
* Added define for maximum supported VMPCK
* Updated comments why sev_status is used directly instead of cpu_feature_enabled()

Quoted 2 lines

A gentle reminder.

From the Google testing side of things, we may not get to this for another while.

Quoted 3 lines

Regards
Nikunj

Signature · 1 line
-Dionna Glaze, PhD (she/her)
#37 lore raw
Quoted 46 lines · Dionna Amalie Glaze
Quoted 45 lines · Nikunj A. Dadhania
Quoted 43 lines · Nikunj A Dadhania

Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC".

During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at
https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info"

Use a minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the AMD Security Processor which is available at early boot.

SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before smp cpus are started, move most of the sev-guest driver code to kernel/sev.c and provide well defined APIs to the sev-guest driver to use the interface to avoid code-duplication.

Patches:
01-08: Preparation and movement of sev-guest driver code 09-16: SecureTSC enablement patches.

Testing SecureTSC
-----------------

SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series:
https://github.com/nikunjad/linux/tree/snp-host-latest-securetsc_v5

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp_securetsc_v5

QEMU commandline SEV-SNP-UPM with SecureTSC:

  qemu-system-x86_64 -cpu EPYC-Milan-v2,+secure-tsc,+invtsc -smp 4 \
    -object memory-backend-memfd-private,id=ram1,size=1G,share=true \
    -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \
    -machine q35,confidential-guest-support=sev0,memory-backend=ram1,kvm-type=snp \
    ...
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Changelog:
----------
v7:
* Drop mutex from the snp_dev and add snp_guest_cmd_{lock,unlock} API
* Added comments for secrets page failure
* Added define for maximum supported VMPCK
* Updated comments why sev_status is used directly instead of cpu_feature_enabled()

I missed this in the change log:

* Added Tested-by from Peter Gonda (https://lore.kernel.org/lkml/CAMkAt6pULjLVUO6Ys4Sq1a79d93_5w5URgLYNXY-aW2jSemruA@mail.gmail.com/)

Quoted 6 lines
Quoted 3 lines

A gentle reminder.

From the Google testing side of things, we may not get to this for another while.

Thanks Dionna

Regards
Nikunj