Add Secure TSC support for SNP guests
v5 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-07: Preparation and movement of sev-guest driver code 08-14: SecureTSC enablement patches.
Testing SecureTSC
-----------------
SecureTSC hypervisor patches based on top of SEV-SNP UPM 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 -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:
----------
v5:
* Rebased on v6.6 kernel
* Dropped link tag in first patch
* Dropped get_ctx_authsize() as it was redundant
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 (14):
virt: sev-guest: Use AES GCM crypto library
virt: sev-guest: Move mutex to SNP guest device structure
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
x86/sev: Move and reorganize sev guest request api x86/mm: Add generic guest initialization hook
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/tsc: Mark Secure TSC as reliable clocksource x86/sev: Enable Secure TSC for SNP guests
arch/x86/Kconfig | 1 + arch/x86/boot/compressed/sev.c | 3 +- arch/x86/coco/core.c | 3 + arch/x86/include/asm/sev-guest.h | 175 +++++++ arch/x86/include/asm/sev.h | 20 +- arch/x86/include/asm/svm.h | 6 +- arch/x86/include/asm/x86_init.h | 2 + arch/x86/kernel/kvmclock.c | 2 +- arch/x86/kernel/sev-shared.c | 7 + arch/x86/kernel/sev.c | 631 +++++++++++++++++++++-- arch/x86/kernel/tsc.c | 2 +- arch/x86/kernel/x86_init.c | 2 + arch/x86/mm/mem_encrypt.c | 13 +- arch/x86/mm/mem_encrypt_amd.c | 6 + drivers/virt/coco/sev-guest/Kconfig | 3 - drivers/virt/coco/sev-guest/sev-guest.c | 646 +++--------------------- drivers/virt/coco/sev-guest/sev-guest.h | 63 --- include/linux/cc_platform.h | 8 + 18 files changed, 877 insertions(+), 716 deletions(-) create mode 100644 arch/x86/include/asm/sev-guest.h delete mode 100644 drivers/virt/coco/sev-guest/sev-guest.h
base-commit: ffc253263a1375a65fa6c9f62a893e9767fbebfa
Signature · 1 line
2.34.1
PATCH v5 1/14 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.
--- drivers/virt/coco/sev-guest/Kconfig | 4 +- drivers/virt/coco/sev-guest/sev-guest.c | 163 ++++++------------------ drivers/virt/coco/sev-guest/sev-guest.h | 3 + 3 files changed, 44 insertions(+), 126 deletions(-)
drivers/virt/coco/sev-guest/Kconfig +1 -3
@@ -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 help SEV-SNP firmware provides the guest a mechanism to communicate with the PSP without risk from a malicious hypervisor who wishes to read,
drivers/virt/coco/sev-guest/sev-guest.c +40 -123
290 lines not on this page · raw message
drivers/virt/coco/sev-guest/sev-guest.h +3 -0
@@ -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
Re: [PATCH v5 01/14] virt: sev-guest: Use AES GCM crypto library
Quoted 7 lines · Nikunj A Dadhania
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_INFOrequest guest message before the smpboot phase starts. Details from theTSC_INFOresponse 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.
Quoted 7 lines
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.
CC Ard Biesheuvel <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>Reviewed-by Tom Lendacky <[email protected]>
I just a few nit comments that might be nice to cover if you have to do a v6...
Quoted 177 lines
--- drivers/virt/coco/sev-guest/Kconfig | 4 +- drivers/virt/coco/sev-guest/sev-guest.c | 163 ++++++------------------ drivers/virt/coco/sev-guest/sev-guest.h | 3 + 3 files changed, 44 insertions(+), 126 deletions(-)drivers/virt/coco/sev-guest/Kconfig +1
-3@@ -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 help SEV-SNP firmware provides the guest a mechanism to communicate with the PSP without risk from a malicious hypervisor who wishes to read,drivers/virt/coco/sev-guest/sev-guest.c +13
-100@@ -16,8 +16,7 @@#include <linux/miscdevice.h> #include <linux/set_memory.h> #include <linux/fs.h> -#include <crypto/aead.h> -#include <linux/scatterlist.h> +#include <crypto/gcm.h> #include <linux/psp-sev.h> #include <uapi/linux/sev-guest.h> #include <uapi/linux/psp-sev.h> @@ -28,24 +27,16 @@#include "sev-guest.h" #define DEVICE_NAME "sev-guest" -#define AAD_LEN 48 -#define MSG_HDR_VER 1#define SNP_REQ_MAX_RETRY_DURATION (60*HZ)#define SNP_REQ_RETRY_DELAY (2*HZ) -struct snp_guest_crypto { -struct crypto_aead *tfm; -u8 *iv, *authtag; -int iv_len, a_len; -}; -struct snp_guest_dev {struct device *dev;struct miscdevice misc;void *certs_data; -struct snp_guest_crypto *crypto; +struct aesgcm_ctx *ctx;/* request and response are in unencrypted memory */ struct snp_guest_msg *request, *response;@@ -152,132 +143,59 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct snp_guest_crypto *init_crypto (struct snp_guest_dev *snp_dev,u8 *key,size_t keylen) +static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) { -struct snp_guest_crypto *crypto; +struct aesgcm_ctx *ctx; - crypto =kzalloc (sizeof (*crypto),GFP_KERNEL_ACCOUNT ); -if (!crypto) + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT ); +if (!ctx)return NULL ; - crypto->tfm =crypto_alloc_aead ("gcm(aes)" ,0 ,0 ); -if (IS_ERR (crypto->tfm)) - goto e_free; - -if (crypto_aead_setkey (crypto->tfm, key, keylen)) - goto e_free_crypto; - - crypto->iv_len =crypto_aead_ivsize (crypto->tfm); - crypto->iv =kmalloc (crypto->iv_len,GFP_KERNEL_ACCOUNT ); -if (!crypto->iv) - goto e_free_crypto; - -if (crypto_aead_authsize (crypto->tfm) >MAX_AUTHTAG_LEN ) { -if (crypto_aead_setauthsize (crypto->tfm,MAX_AUTHTAG_LEN )) { -dev_err (snp_dev->dev,"failed to set authsize to %d\n" ,MAX_AUTHTAG_LEN ); - goto e_free_iv; - } +if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { +pr_err ("SNP: crypto init failed\n" ); +kfree (ctx); +return NULL ; } - crypto->a_len =crypto_aead_authsize (crypto->tfm); - crypto->authtag =kmalloc (crypto->a_len,GFP_KERNEL_ACCOUNT ); -if (!crypto->authtag) - goto e_free_iv; - -return crypto; - -e_free_iv: -kfree (crypto->iv); -e_free_crypto: -crypto_free_aead (crypto->tfm); -e_free: -kfree (crypto); - -return NULL ; -} - -static void deinit_crypto (struct snp_guest_crypto *crypto) -{ -crypto_free_aead (crypto->tfm); -kfree (crypto->iv); -kfree (crypto->authtag); -kfree (crypto); -} - -static int enc_dec_message (struct snp_guest_crypto *crypto,struct snp_guest_msg *msg, -u8 *src_buf,u8 *dst_buf,size_t len,bool enc) -{ -struct snp_guest_msg_hdr *hdr = &msg->hdr; -struct scatterlist src[3 ], dst[3 ]; -DECLARE_CRYPTO_WAIT (wait); -struct aead_request *req; -int ret; - - req =aead_request_alloc (crypto->tfm,GFP_KERNEL ); -if (!req) -return -ENOMEM ; - -/* -* AEAD memory operations: -* +------ AAD -------+------- DATA -----+---- AUTHTAG----+ -* | msg header | plaintext | hdr->authtag | -* | bytes 30h - 5Fh | or | | -* | | cipher | | -* +------------------+------------------+----------------+ -*/ -sg_init_table (src,3 ); -sg_set_buf (&src[0 ], &hdr->algo,AAD_LEN ); -sg_set_buf (&src[1 ], src_buf, hdr->msg_sz); -sg_set_buf (&src[2 ], hdr->authtag, crypto->a_len); - -sg_init_table (dst,3 ); -sg_set_buf (&dst[0 ], &hdr->algo,AAD_LEN ); -sg_set_buf (&dst[1 ], dst_buf, hdr->msg_sz); -sg_set_buf (&dst[2 ], hdr->authtag, crypto->a_len); - -aead_request_set_ad (req,AAD_LEN ); -aead_request_set_tfm (req, crypto->tfm); -aead_request_set_callback (req,0 , crypto_req_done, &wait); - -aead_request_set_crypt (req, src, dst, len, crypto->iv); - ret =crypto_wait_req (enc ?crypto_aead_encrypt (req) :crypto_aead_decrypt (req), &wait); - -aead_request_free (req); -return ret; +return ctx; } -static int __enc_payload (struct snp_guest_dev *snp_dev,struct snp_guest_msg *msg, +static int __enc_payload (struct aesgcm_ctx *ctx,struct snp_guest_msg *msg,void *plaintext,size_t len) { -struct snp_guest_crypto *crypto = snp_dev->crypto;struct snp_guest_msg_hdr *hdr = &msg->hdr; +u8 iv[GCM_AES_IV_SIZE ] = {}; -memset (crypto->iv,0 , crypto->iv_len);
drivers/virt/coco/sev-guest/sev-guest.c static inline struct snp_guest_dev *to_snp_dev(struct file *file) · line 176
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +6
-2-memcpy (crypto->iv, &hdr->msg_seqno,sizeof (hdr->msg_seqno)); +if (WARN_ON ((hdr->msg_sz + ctx->authsize) >sizeof (msg->payload))) +return -EBADMSG ; -return enc_dec_message (crypto, msg, plaintext, msg->payload, len, true); +memcpy (iv, &hdr->msg_seqno,sizeof (hdr->msg_seqno)); +aesgcm_encrypt (ctx, msg->payload, plaintext, len, &hdr->algo,AAD_LEN , + iv, hdr->authtag); +return 0 ; }
__enc_payload() is pretty small now and can probably just be part of the only function that calls it, enc_payload().
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-3-static int dec_payload (struct snp_guest_dev *snp_dev,struct snp_guest_msg *msg, +static int dec_payload (struct aesgcm_ctx *ctx,struct snp_guest_msg *msg,void *plaintext,size_t len) { -struct snp_guest_crypto *crypto = snp_dev->crypto;struct snp_guest_msg_hdr *hdr = &msg->hdr; +u8 iv[GCM_AES_IV_SIZE ] = {}; -/* Build IV with response buffer sequence number */
drivers/virt/coco/sev-guest/sev-guest.c static inline struct snp_guest_dev *to_snp_dev(struct file *file) · line 189
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +6
-4-memset (crypto->iv,0 , crypto->iv_len); -memcpy (crypto->iv, &hdr->msg_seqno,sizeof (hdr->msg_seqno)); - -return enc_dec_message (crypto, msg, msg->payload, plaintext, len, false); +memcpy (iv, &hdr->msg_seqno,sizeof (hdr->msg_seqno)); +if (aesgcm_decrypt (ctx, plaintext, msg->payload, len, &hdr->algo, +AAD_LEN , iv, hdr->authtag)) +return 0 ; +else +return -EBADMSG ;
This would look cleaner / read easier to me to have as:
if (!aesgcm_decrypt(...)) return -EBADMSG; return 0;
But just my opinion.
And ditto here on the size now, can probably just be part of verify_and_dec_payload() now.
Thanks,
Tom
Quoted 136 lines
drivers/virt/coco/sev-guest/sev-guest.c +13
-14}static int verify_and_dec_payload (struct snp_guest_dev *snp_dev,void *payload,u32 sz) { -struct snp_guest_crypto *crypto = snp_dev->crypto;struct snp_guest_msg *resp = &snp_dev->secret_response;struct snp_guest_msg *req = &snp_dev->secret_request;struct snp_guest_msg_hdr *req_hdr = &req->hdr;struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; +struct aesgcm_ctx *ctx = snp_dev->ctx;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);@@ -298,11 +216,11 @@ 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); +return dec_payload (ctx, resp, payload, resp_hdr->msg_sz); } static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type,@@ -329,7 +247,7 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8dev_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); +return __enc_payload (snp_dev->ctx, req, payload, sz); } static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,@@ -472,7 +390,6 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,static int get_report (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) { -struct snp_guest_crypto *crypto = snp_dev->crypto;struct snp_report_resp *resp;struct snp_report_req req;int rc, resp_len;@@ -490,7 +407,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io* response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len =sizeof (resp->data) + crypto->a_len; + resp_len =sizeof (resp->data) + snp_dev->ctx->authsize; resp =kzalloc (resp_len,GFP_KERNEL_ACCOUNT );if (!resp)return -ENOMEM ;@@ -511,7 +428,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_iostatic int get_derived_key (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) { -struct snp_guest_crypto *crypto = snp_dev->crypto;struct snp_derived_key_resp resp = {0 };struct snp_derived_key_req req;int rc, resp_len;@@ -528,7 +444,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque* response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len =sizeof (resp.data) + crypto->a_len; + resp_len =sizeof (resp.data) + snp_dev->ctx->authsize;if (sizeof (buf) < resp_len)return -ENOMEM ;@@ -552,7 +468,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_requestatic int get_ext_report (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) { -struct snp_guest_crypto *crypto = snp_dev->crypto;struct snp_ext_report_req req;struct snp_report_resp *resp;int ret, npages =0 , resp_len;@@ -590,7 +505,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques* response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len =sizeof (resp->data) + crypto->a_len; + resp_len =sizeof (resp->data) + snp_dev->ctx->authsize; resp =kzalloc (resp_len,GFP_KERNEL_ACCOUNT );if (!resp)return -ENOMEM ;@@ -802,8 +717,8 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO ; - snp_dev->crypto =init_crypto (snp_dev, snp_dev->vmpck,VMPCK_KEY_LEN ); -if (!snp_dev->crypto) + snp_dev->ctx =snp_init_crypto (snp_dev->vmpck,VMPCK_KEY_LEN ); +if (!snp_dev->ctx) goto e_free_cert_data; misc = &snp_dev->misc;@@ -818,11 +733,13 @@ static int __init sev_guest_probe(struct platform_device *pdev) ret =misc_register (misc);if (ret) - goto e_free_cert_data; + goto e_free_ctx;dev_info (dev,"Initialized SEV guest driver (using vmpck_id %d)\n" , vmpck_id);return 0 ; +e_free_ctx: +kfree (snp_dev->ctx); e_free_cert_data:free_shared_pages (snp_dev->certs_data,SEV_FW_BLOB_MAX_SIZE ); e_free_response:@@ -841,7 +758,7 @@ static int __exit sev_guest_remove(struct platform_device *pdev)free_shared_pages (snp_dev->certs_data,SEV_FW_BLOB_MAX_SIZE );free_shared_pages (snp_dev->response,sizeof (struct snp_guest_msg ));free_shared_pages (snp_dev->request,sizeof (struct snp_guest_msg )); -deinit_crypto (snp_dev->crypto); +kfree (snp_dev->ctx);misc_deregister (&snp_dev->misc);return 0 ;drivers/virt/coco/sev-guest/sev-guest.h +3
-0@@ -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 {
Re: [PATCH v5 01/14] virt: sev-guest: Use AES GCM crypto library
Quoted 12 lines · Tom Lendacky
Quoted 11 lines · Nikunj A Dadhania
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_INFOrequest guest message before the smpboot phase starts. Details from theTSC_INFOresponse 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.
Quoted 5 lines
Quoted 3 lines
CC Ard Biesheuvel <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>Reviewed-by Tom Lendacky <[email protected]>I just a few nit comments that might be nice to cover if you have to do a v6...
Sure, I will address them in v6.
Quoted 44 lines
Quoted 9 lines
-static int __enc_payload(struct
snp_guest_dev*snp_dev, structsnp_guest_msg*msg, +static int__enc_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, void *plaintext, size_t len)
{
- structsnp_guest_crypto*crypto =snp_dev->crypto;
structsnp_guest_msg_hdr*hdr =&msg->hdr;
+ u8 iv[GCM_AES_IV_SIZE] = {};
-memset(crypto->iv, 0, crypto->iv_len);
-memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno));Quoted 8 lines
+ if (
WARN_ON((hdr->msg_sz + ctx->authsize) > sizeof(msg->payload))) + return-EBADMSG;
- returnenc_dec_message(crypto, msg, plaintext, msg->payload, len, true); +memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno));
+ aesgcm_encrypt(ctx,msg->payload, plaintext, len,&hdr->algo,AAD_LEN, + iv,hdr->authtag);
+ return 0;
}
__enc_payload()is pretty small now and can probably just be part of the only function that calls it,enc_payload().Quoted 10 lines
-static int dec_payload(struct
snp_guest_dev*snp_dev, structsnp_guest_msg*msg, +static intdec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, void *plaintext, size_t len)
{
- structsnp_guest_crypto*crypto =snp_dev->crypto;
structsnp_guest_msg_hdr*hdr =&msg->hdr;
+ u8 iv[GCM_AES_IV_SIZE] = {};
- /* Build IV with response buffer sequence number */
-memset(crypto->iv, 0, crypto->iv_len);
-memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno));Quoted 8 lines
-
- returnenc_dec_message(crypto, msg, msg->payload, plaintext, len, false); +memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno));
+ if (aesgcm_decrypt(ctx, plaintext,msg->payload, len,&hdr->algo, +AAD_LEN, iv,hdr->authtag))
+ return 0;
+ else
+ return-EBADMSG;This would look cleaner / read easier to me to have as:
if (!aesgcm_decrypt(...))
return-EBADMSG;
Quoted 8 lines
return 0;
But just my opinion.
And ditto here on the size now, can probably just be part of
verify_and_dec_payload()now.Thanks,
Tom
Regards
Nikunj
PATCH v5 2/14 virt: sev-guest: Move mutex to SNP guest device structure
In reply to Nikunj A DadhaniaIn preparation for providing a new API to the sev-guest driver for sending an SNP guest message, move the SNP command mutex to the snp_guest_dev structure.
--- drivers/virt/coco/sev-guest/sev-guest.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +11 -7
@@ -35,6 +35,9 @@ struct snp_guest_dev {struct device *dev;struct miscdevice misc; +/* Mutex to serialize the shared buffer access and command handling. */ +struct mutex cmd_mutex; +void *certs_data;struct aesgcm_ctx *ctx;/* request and response are in unencrypted memory */
@@ -98,7 +101,7 @@ static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) {u64 count; -lockdep_assert_held (&snp_cmd_mutex); +lockdep_assert_held (&snp_dev->cmd_mutex);/* Read the current message sequence counter from secrets pages */ count = *snp_dev->os_area_msg_seqno;
@@ -394,7 +397,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_iostruct snp_report_req req;int rc, resp_len; -lockdep_assert_held (&snp_cmd_mutex); +lockdep_assert_held (&snp_dev->cmd_mutex);if (!arg->req_data || !arg->resp_data)return -EINVAL ;
@@ -434,7 +437,7 @@ 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); +lockdep_assert_held (&snp_dev->cmd_mutex);if (!arg->req_data || !arg->resp_data)return -EINVAL ;
@@ -472,7 +475,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_requesstruct snp_report_resp *resp;int ret, npages =0 , resp_len; -lockdep_assert_held (&snp_cmd_mutex); +lockdep_assert_held (&snp_dev->cmd_mutex);if (!arg->req_data || !arg->resp_data)return -EINVAL ;
@@ -557,12 +560,12 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longif (!input.msg_version)return -EINVAL ; -mutex_lock (&snp_cmd_mutex); +mutex_lock (&snp_dev->cmd_mutex);/* Check if the VMPCK is not empty */ if (is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" ); -mutex_unlock (&snp_cmd_mutex); +mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ; }
@@ -580,7 +583,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longbreak ; } -mutex_unlock (&snp_cmd_mutex); +mutex_unlock (&snp_dev->cmd_mutex);if (input.exitinfo2 &©_to_user (argp, &input,sizeof (input)))return -EFAULT ;
@@ -699,6 +702,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; } +mutex_init (&snp_dev->cmd_mutex);platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; snp_dev->layout = layout;
Signature · 1 line
2.34.1
PATCH v5 3/14 virt: sev-guest: Replace dev_dbg with pr_debug
In reply to Nikunj A DadhaniaIn 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
@@ -200,8 +200,9 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,struct snp_guest_msg_hdr *resp_hdr = &resp->hdr;struct aesgcm_ctx *ctx = snp_dev->ctx; -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));
@@ -247,8 +248,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8if (!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);return __enc_payload (snp_dev->ctx, req, payload, sz); }
Signature · 1 line
2.34.1
PATCH v5 4/14 virt: sev-guest: Add SNP guest request structure
In reply to Nikunj A DadhaniaAdd 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.
---
.../x86/include/asm}/sev-guest.h | 11 ++
arch/x86/include/asm/sev.h | 8 --
arch/x86/kernel/sev.c | 15 ++-
drivers/virt/coco/sev-guest/sev-guest.c | 103 +++++++++++-------
4 files changed, 84 insertions(+), 53 deletions(-)
rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (80%)arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +11 -0
@@ -63,4 +63,15 @@ struct snp_guest_msg { u8 payload[4000]; } __packed; +struct snp_guest_req { +void *req_buf, *resp_buf, *data; +size_t req_sz, resp_sz, *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
@@ -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 npastatic 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
@@ -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>
@@ -2167,15 +2168,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
@@ -2192,8 +2199,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snvc_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);
@@ -2212,7 +2219,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sncase 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 +62 -41
238 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 04/14] virt: sev-guest: Add SNP guest request structure
Quoted 5 lines · Nikunj A Dadhania
Add a
snp_guest_reqstructure 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.Signed-off-by Nikunj A Dadhania <[email protected]>
Some minor comments below.
Quoted 16 lines
--- .../x86/include/asm}/sev-guest.h | 11 ++ arch/x86/include/asm/sev.h | 8 -- arch/x86/kernel/sev.c | 15 ++- drivers/virt/coco/sev-guest/sev-guest.c | 103 +++++++++++------- 4 files changed, 84 insertions(+), 53 deletions(-)rename {
drivers/virt/coco/sev-guest=>arch/x86/include/asm}/sev-guest.h(80%)arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +0
-0
arch/x86/include/asm/sev-guest.h struct snp_guest_msg { · line 68
Quoted 6 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +3
-0@@ -63,4 +63,15 @@ struct snp_guest_msg {u8 payload[4000 ]; } __packed; +struct snp_guest_req { +void *req_buf, *resp_buf, *data; +size_t req_sz, resp_sz, *data_npages;
For structures like this, I find it easier to group things and keep it one item per line, e.g.:
void *req_buf; size_t req_sz; void *resp_buf; size_t resp_sz; void *data; size_t *data_npages;
And does data_npages have to be a pointer? It looks like you can just use this variable as the address on the GHCB call and then set it appropriately without all the indirection, right?
Quoted 31 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +8
-0+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
-3@@ -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 );
arch/x86/include/asm/sev.h static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npa · line 240
Quoted 8 lines
arch/x86/include/asm/sev.h +0
-5@@ -233,11 +230,6 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npastatic 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 ; -} -
May want to mention in the commit message why this can be deleted vs changed.
Quoted 261 lines
arch/x86/include/asm/sev.h +0
-0static 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@@ -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> @@ -2167,15 +2168,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 @@ -2192,8 +2199,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snvc_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);@@ -2212,7 +2219,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sncase 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 +45
-33@@ -23,8 +23,7 @@#include <asm/svm.h> #include <asm/sev.h> - -#include "sev-guest.h" +#include <asm/sev-guest.h> #define DEVICE_NAME "sev-guest"@@ -192,7 +191,7 @@ static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg,return -EBADMSG ; } -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) {struct snp_guest_msg *resp = &snp_dev->secret_response;struct snp_guest_msg *req = &snp_dev->secret_request;@@ -220,29 +219,28 @@ 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 + ctx->authsize) > sz)) +if (unlikely ((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz))return -EBADMSG ;/* Decrypt the payload */ -return dec_payload (ctx, resp, payload, resp_hdr->msg_sz); +return dec_payload (ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); } -static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno,int version,u8 type, -void *payload,size_t sz) +static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno,struct snp_guest_req *req) { -struct snp_guest_msg *req = &snp_dev->secret_request; -struct snp_guest_msg_hdr *hdr = &req->hdr; +struct snp_guest_msg *msg = &snp_dev->secret_request; +struct snp_guest_msg_hdr *hdr = &msg->hdr; -memset (req,0 ,sizeof (*req)); +memset (msg,0 ,sizeof (*msg)); hdr->algo =SNP_AEAD_AES_256_GCM ; hdr->hdr_version =MSG_HDR_VER ; hdr->hdr_sz =sizeof (*hdr); - hdr->msg_type = type; - hdr->msg_version = version; + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; hdr->msg_seqno = seqno; - hdr->msg_vmpck = vmpck_id; - hdr->msg_sz = sz; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz;/* Verify the sequence number is non-zero */ if (!hdr->msg_seqno)@@ -251,10 +249,10 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8pr_debug ("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->ctx, req, payload, sz); +return __enc_payload (snp_dev->ctx, msg, req->req_buf, req->req_sz); } -static int __handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code, +static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req,struct snp_guest_request_ioctl *rio) {unsigned long req_start = jiffies;@@ -269,7 +267,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* sequence number must be incremented or the VMPCK must be deleted to * prevent reuse of the IV. */ - rc =snp_issue_guest_request (exit_code, &snp_dev->input, rio); + rc =snp_issue_guest_request (req, &snp_dev->input, rio);switch (rc) {case -ENOSPC :/* @@ -279,8 +277,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* order to increment the sequence number and thus avoid * IV reuse. */ - override_npages = snp_dev->input.data_npages; - exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + override_npages = *req->data_npages; + req->exit_code =SVM_VMGEXIT_GUEST_REQUEST ;/* * Override the error to inform callers the given extended @@ -335,15 +333,13 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, }if (override_npages) - snp_dev->input.data_npages = override_npages; + *req->data_npages = override_npages;return rc; } -static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code, -struct snp_guest_request_ioctl *rio,u8 type, -void *req_buf,size_t req_sz,void *resp_buf, -u32 resp_sz) +static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, +struct snp_guest_request_ioctl *rio) {u64 seqno;int rc;@@ -357,7 +353,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,memset (snp_dev->response,0 ,sizeof (struct snp_guest_msg ));/* Encrypt the userspace provided payload in snp_dev->secret_request. */ - rc =enc_payload (snp_dev, seqno, rio->msg_version, type, req_buf, req_sz); + rc =enc_payload (snp_dev, seqno, req);if (rc)return rc;@@ -368,7 +364,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,memcpy (snp_dev->request, &snp_dev->secret_request,sizeof (snp_dev->secret_request)); - rc =__handle_guest_request (snp_dev, exit_code, rio); + rc =__handle_guest_request (snp_dev, req, rio);if (rc) {if (rc == -EIO && rio->exitinfo2 ==SNP_GUEST_VMM_ERR (SNP_GUEST_VMM_ERR_INVALID_LEN ))@@ -377,12 +373,11 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n" , rc, rio->exitinfo2); -snp_disable_vmpck (snp_dev);return rc; } - rc =verify_and_dec_payload (snp_dev, resp_buf, resp_sz); + rc =verify_and_dec_payload (snp_dev, req);if (rc) {dev_alert (snp_dev->dev,"Detected unexpected decode failure from ASP. rc: %d\n" , rc);snp_disable_vmpck (snp_dev);@@ -394,6 +389,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,static int get_report (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) { +struct snp_guest_req guest_req = {0 };struct snp_report_resp *resp;struct snp_report_req req;int rc, resp_len;@@ -416,9 +412,16 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioif (!resp)return -ENOMEM ; - rc =handle_guest_request (snp_dev,SVM_VMGEXIT_GUEST_REQUEST , arg, -SNP_MSG_REPORT_REQ , &req,sizeof (req), resp->data, - resp_len); + guest_req.msg_version = arg->msg_version; + guest_req.msg_type =SNP_MSG_REPORT_REQ ; + guest_req.vmpck_id = vmpck_id; + guest_req.req_buf = &req; + guest_req.req_sz =sizeof (req); + guest_req.resp_buf = resp->data; + guest_req.resp_sz = resp_len; + guest_req.exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + + rc =snp_send_guest_request (snp_dev, &guest_req, arg);if (rc) goto e_free;@@ -433,6 +436,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_iostatic int get_derived_key (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) {struct snp_derived_key_resp resp = {0 }; +struct snp_guest_req guest_req = {0 };struct snp_derived_key_req req;int rc, resp_len;/* Response data is 64 bytes and max authsize for GCM is 16 bytes. */ @@ -455,8 +459,16 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_requeif (copy_from_user (&req, (void __user *)arg->req_data,sizeof (req)))return -EFAULT ; - rc =handle_guest_request (snp_dev,SVM_VMGEXIT_GUEST_REQUEST , arg, -SNP_MSG_KEY_REQ , &req,sizeof (req), buf, resp_len); + guest_req.msg_version = arg->msg_version; + guest_req.msg_type =SNP_MSG_KEY_REQ ; + guest_req.vmpck_id = vmpck_id; + guest_req.req_buf = &req; + guest_req.req_sz =sizeof (req); + guest_req.resp_buf = buf; + guest_req.resp_sz = resp_len; + guest_req.exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + + rc =snp_send_guest_request (snp_dev, &guest_req, arg);if (rc)return rc;
drivers/virt/coco/sev-guest/sev-guest.c static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque · line 491
Quoted 9 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-1@@ -472,9 +484,11 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_requestatic int get_ext_report (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) { +struct snp_guest_req guest_req = {0 };struct snp_ext_report_req req;struct snp_report_resp *resp; -int ret, npages =0 , resp_len; +int ret, resp_len; +size_t npages =0 ;
This becomes unnecessary if you don't define data_npages as a pointer in the snp_guest_req structure.
Thanks,
Tom
Quoted 48 lines
drivers/virt/coco/sev-guest/sev-guest.c +14
-7lockdep_assert_held (&snp_dev->cmd_mutex);@@ -514,14 +528,22 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_requesif (!resp)return -ENOMEM ; - snp_dev->input.data_npages = npages; - ret =handle_guest_request (snp_dev,SVM_VMGEXIT_EXT_GUEST_REQUEST , arg, -SNP_MSG_REPORT_REQ , &req.data, -sizeof (req.data), resp->data, resp_len); + guest_req.msg_version = arg->msg_version; + guest_req.msg_type =SNP_MSG_REPORT_REQ ; + guest_req.vmpck_id = vmpck_id; + guest_req.req_buf = &req.data; + guest_req.req_sz =sizeof (req.data); + guest_req.resp_buf = resp->data; + guest_req.resp_sz = resp_len; + guest_req.exit_code =SVM_VMGEXIT_EXT_GUEST_REQUEST ; + guest_req.data = snp_dev->certs_data; + guest_req.data_npages = &npages; + + ret =snp_send_guest_request (snp_dev, &guest_req, arg);/* If certs length is invalid then copy the returned length */ if (arg->vmm_error ==SNP_GUEST_VMM_ERR_INVALID_LEN ) { - req.certs_len = snp_dev->input.data_npages <<PAGE_SHIFT ; + req.certs_len = npages <<PAGE_SHIFT ;if (copy_to_user ((void __user *)arg->req_data, &req,sizeof (req))) ret = -EFAULT ;@@ -530,7 +552,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_requesif (ret) goto e_free; -if (npages && +if (npages && req.certs_len &©_to_user ((void __user *)req.certs_address, snp_dev->certs_data, req.certs_len)) { ret = -EFAULT ;@@ -734,7 +756,6 @@ static int __init sev_guest_probe(struct platform_device *pdev)/* initial the input address for guest request */ snp_dev->input.req_gpa =__pa (snp_dev->request); snp_dev->input.resp_gpa =__pa (snp_dev->response); - snp_dev->input.data_gpa =__pa (snp_dev->certs_data); ret =misc_register (misc);if (ret)
Re: [PATCH v5 04/14] virt: sev-guest: Add SNP guest request structure
Quoted 17 lines · Tom Lendacky
Quoted 5 lines · Nikunj A Dadhania
Add a
snp_guest_reqstructure 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.Signed-off-by Nikunj A Dadhania <[email protected]>Some minor comments below.
Quoted 8 lines
---
.../x86/include/asm}/sev-guest.h | 11 ++ arch/x86/include/asm/sev.h | 8 -- arch/x86/kernel/sev.c | 15 ++- drivers/virt/coco/sev-guest/sev-guest.c | 103 +++++++++++-------4 files changed, 84 insertions(+), 53 deletions(-)rename {
drivers/virt/coco/sev-guest=>arch/x86/include/asm}/sev-guest.h(80%)
Quoted 15 lines
arch/x86/include/asm/sev-guest.h struct snp_guest_msg { · line 67
Quoted 13 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +2
-0@@ -63,4 +63,15 @@ struct snp_guest_msg { u8 payload[4000]; } __packed; +struct snp_guest_req { + void *req_buf, *resp_buf, *data; + size_t req_sz, resp_sz, *data_npages;For structures like this, I find it easier to group things and keep it one item per line, e.g.:
Ok, I will change that.
Quoted 10 lines
void
*req_buf;
size_treq_sz;
void*resp_buf;
size_tresp_sz;void *data;
size_t*data_npages;And does
data_npageshave to be a pointer?
Going through the code again, you are right, it need not be a pointer.
Quoted 1 line
It looks like you can just use this variable as the address on the GHCB call and then set it appropriately without all the indirection, right?
I can use the data_npages value directly in the GHCB call, am I missing something.
@@ -2192,8 +2199,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);
@@ -2212,7 +2219,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;
}Quoted 33 lines
Quoted 33 lines
arch/x86/include/asm/sev-guest.h renamed from drivers/virt/coco/sev-guest/sev-guest.h +8
-0+ 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
-3@@ -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; }
Quoted 8 lines
arch/x86/include/asm/sev.h static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npa · line 240
Quoted 6 lines
arch/x86/include/asm/sev.h +0
-5static 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; -} -May want to mention in the commit message why this can be deleted vs changed.
Sure will do. It has been moved to sev-guest.h now.
Quoted 235 lines
Quoted 235 lines
arch/x86/include/asm/sev.h +0
-0static 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 +10
-2@@ -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>@@ -2167,15 +2168,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 @@ -2192,8 +2199,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);@@ -2212,7 +2219,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sncase
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 +1
-2@@ -23,8 +23,7 @@ #include <asm/svm.h> #include <asm/sev.h> - -#include "sev-guest.h" +#include <asm/sev-guest.h> #define DEVICE_NAME "sev-guest" @@ -192,7 +191,7 @@ static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, return -EBADMSG; }-static int
verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz)+static intverify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req){
structsnp_guest_msg*resp =&snp_dev->secret_response;
structsnp_guest_msg*req =&snp_dev->secret_request;drivers/virt/coco/sev-guest/sev-guest.c +13
-10@@ -220,29 +219,28 @@ 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 + ctx->authsize) > sz)) + if (unlikely((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz)) return -EBADMSG; /* Decrypt the payload */ - return dec_payload(ctx, resp, payload, resp_hdr->msg_sz); + return dec_payload(ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); } -static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type, - void *payload, size_t sz) +static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, struct snp_guest_req *req) { - struct snp_guest_msg *req = &snp_dev->secret_request; - struct snp_guest_msg_hdr *hdr = &req->hdr; + struct snp_guest_msg *msg = &snp_dev->secret_request; + struct snp_guest_msg_hdr *hdr = &msg->hdr; - memset(req, 0, sizeof(*req)); + memset(msg, 0, sizeof(*msg)); hdr->algo = SNP_AEAD_AES_256_GCM; hdr->hdr_version = MSG_HDR_VER; hdr->hdr_sz = sizeof(*hdr); - hdr->msg_type = type; - hdr->msg_version = version; + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; hdr->msg_seqno = seqno; - hdr->msg_vmpck = vmpck_id; - hdr->msg_sz = sz; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz; /* Verify the sequence number is non-zero */ if (!hdr->msg_seqno)@@ -251,10 +249,10 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 pr_debug("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->ctx, req, payload, sz); + return __enc_payload(snp_dev->ctx, msg, req->req_buf, req->req_sz); } -static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, +static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, struct snp_guest_request_ioctl *rio) { unsigned long req_start = jiffies;@@ -269,7 +267,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* sequence number must be incremented or the VMPCK must be deleted to * prevent reuse of the IV. */ - rc = snp_issue_guest_request(exit_code, &snp_dev->input, rio); + rc = snp_issue_guest_request(req, &snp_dev->input, rio); switch (rc) {case
-ENOSPC:
/*drivers/virt/coco/sev-guest/sev-guest.c +6
-7@@ -279,8 +277,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* order to increment the sequence number and thus avoid * IV reuse. */ - override_npages = snp_dev->input.data_npages; - exit_code = SVM_VMGEXIT_GUEST_REQUEST; + override_npages = *req->data_npages; + req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; /** Override the error to inform callers the given extended @@ -335,15 +333,13 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, } if (override_npages) - snp_dev->input.data_npages = override_npages; + *req->data_npages = override_npages; return rc; } -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, - struct snp_guest_request_ioctl *rio, u8 type, - void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz) +static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio) { u64 seqno; int rc;@@ -357,7 +353,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload in snp_dev->secret_request. */ - rc = enc_payload(snp_dev, seqno, rio->msg_version, type, req_buf, req_sz); + rc = enc_payload(snp_dev, seqno, req); if (rc) return rc; @@ -368,7 +364,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, memcpy(snp_dev->request, &snp_dev->secret_request,
sizeof(snp_dev->secret_request));
- rc =__handle_guest_request(snp_dev, exit_code, rio);
+ rc =__handle_guest_request(snp_dev, req, rio);
if (rc) {
if (rc ==-EIO&&
rio->exitinfo2==SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN))drivers/virt/coco/sev-guest/sev-guest.c +12
-3@@ -377,12 +373,11 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", rc, rio->exitinfo2); - snp_disable_vmpck(snp_dev); return rc; } - rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz); + rc = verify_and_dec_payload(snp_dev, req); if (rc) { dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); snp_disable_vmpck(snp_dev);@@ -394,6 +389,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { + struct snp_guest_req guest_req = {0}; struct snp_report_resp *resp; struct snp_report_req req; int rc, resp_len;@@ -416,9 +412,16 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io if (!resp) return -ENOMEM; - rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, - SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data, - resp_len); + guest_req.msg_version = arg->msg_version; + guest_req.msg_type = SNP_MSG_REPORT_REQ; + guest_req.vmpck_id = vmpck_id; + guest_req.req_buf = &req; + guest_req.req_sz = sizeof(req); + guest_req.resp_buf = resp->data; + guest_req.resp_sz = resp_len; + guest_req.exit_code = SVM_VMGEXIT_GUEST_REQUEST; + + rc = snp_send_guest_request(snp_dev, &guest_req, arg); if (rc) goto e_free;@@ -433,6 +436,7 @@ static int get_report(struct
snp_guest_dev*snp_dev, structsnp_guest_request_iostatic intget_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg){
structsnp_derived_key_respresp = {0};
+ structsnp_guest_reqguest_req= {0};
structsnp_derived_key_reqreq;
int rc,resp_len;
/* Response data is 64 bytes and max authsize for GCM is 16 bytes. */drivers/virt/coco/sev-guest/sev-guest.c +10
-1@@ -455,8 +459,16 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) return -EFAULT; - rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg, - SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len); + guest_req.msg_version = arg->msg_version; + guest_req.msg_type = SNP_MSG_KEY_REQ; + guest_req.vmpck_id = vmpck_id; + guest_req.req_buf = &req; + guest_req.req_sz = sizeof(req); + guest_req.resp_buf = buf; + guest_req.resp_sz = resp_len; + guest_req.exit_code = SVM_VMGEXIT_GUEST_REQUEST; + + rc = snp_send_guest_request(snp_dev, &guest_req, arg); if (rc) return rc;@@ -472,9 +484,11 @@ static int get_derived_key(struct
snp_guest_dev*snp_dev, structsnp_guest_requestatic intget_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg){
Quoted 8 lines
Quoted 6 lines
+ struct
snp_guest_reqguest_req= {0};
structsnp_ext_report_reqreq;
structsnp_report_resp*resp;
- int ret, npages = 0,resp_len;
+ int ret,resp_len;
+size_tnpages = 0;This becomes unnecessary if you don't define
data_npagesas a pointer in thesnp_guest_reqstructure.
Right, I will change that.
Quoted 3 lines
Thanks,
Tom
Thanks
Nikunj
PATCH v5 5/14 virt: sev-guest: Add vmpck_id to snp_guest_dev struct
In reply to Nikunj A DadhaniaDrop 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.
Also, change function is_vmpck_empty() to snp_is_vmpck_empty() in preparation for moving to sev.c.
--- drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- 1 file changed, 42 insertions(+), 43 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +42 -43
@@ -50,8 +50,7 @@ struct snp_guest_dev {struct snp_secrets_page_layout *layout;struct snp_req_data input; -u32 *os_area_msg_seqno; -u8 *vmpck; +unsigned int vmpck_id; };static u32 vmpck_id;
@@ -61,14 +60,22 @@ 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 bool is_vmpck_empty (struct snp_guest_dev *snp_dev) +static inline u8 *snp_get_vmpck (struct snp_guest_dev *snp_dev) { -char zero_key[VMPCK_KEY_LEN ] = {0 }; +return snp_dev->layout->vmpck0 + snp_dev->vmpck_id *VMPCK_KEY_LEN ; +} -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); +static inline u32 *snp_get_os_area_msg_seqno (struct snp_guest_dev *snp_dev) +{ +return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; +} -return true; +static bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; +u8 *key =snp_get_vmpck (snp_dev); + +return !memcmp (key, zero_key,VMPCK_KEY_LEN ); }/*
@@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)*/ static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) { +u8 *key =snp_get_vmpck (snp_dev); +dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - vmpck_id); -memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); - snp_dev->vmpck =NULL ; + snp_dev->vmpck_id); +memzero_explicit (key,VMPCK_KEY_LEN ); }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_dev->cmd_mutex);/* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; + count = *os_area_msg_seqno;return count +1 ; }
@@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev)static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) { +u32 *os_area_msg_seqno =snp_get_os_area_msg_seqno (snp_dev); +/* * The counter is also incremented by the PSP, so increment it by 2 * and save in secrets page. */ - *snp_dev->os_area_msg_seqno +=2 ; + *os_area_msg_seqno +=2 ; }static inline struct snp_guest_dev *to_snp_dev (struct file *file)
@@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) +static struct aesgcm_ctx *snp_init_crypto (struct snp_guest_dev *snp_dev) {struct aesgcm_ctx *ctx; +u8 *key; + +if (snp_is_vmpck_empty (snp_dev)) { +pr_err ("SNP: vmpck id %d is null\n" , snp_dev->vmpck_id); +return NULL ; + } ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ; -if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { + key =snp_get_vmpck (snp_dev); +if (aesgcm_expandkey (ctx, key,VMPCK_KEY_LEN ,AUTHTAG_LEN )) {pr_err ("SNP: crypto init failed\n" );kfree (ctx);return NULL ;
@@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longmutex_lock (&snp_dev->cmd_mutex);/* Check if the VMPCK is not empty */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ;
@@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck (int id,struct snp_secrets_page_layout *layout,u32 **seqno) +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) { -u8 *key =NULL ; +if (WARN_ON (vmpck_id >3 )) +return false; -switch (id) { -case 0 : - *seqno = &layout->os_area.msg_seqno_0; - key = layout->vmpck0; -break ; -case 1 : - *seqno = &layout->os_area.msg_seqno_1; - key = layout->vmpck1; -break ; -case 2 : - *seqno = &layout->os_area.msg_seqno_2; - key = layout->vmpck2; -break ; -case 3 : - *seqno = &layout->os_area.msg_seqno_3; - key = layout->vmpck3; -break ; -default : -break ; - } + dev->vmpck_id = vmpck_id; -return key; +return true; }static int __init sev_guest_probe (struct platform_device *pdev)
@@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL ; - snp_dev->vmpck =get_vmpck (vmpck_id, layout, &snp_dev->os_area_msg_seqno); -if (!snp_dev->vmpck) { + snp_dev->layout = layout; +if (!snp_assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); goto e_unmap; }
@@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev)mutex_init (&snp_dev->cmd_mutex);platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; - snp_dev->layout = layout;/* Allocate the shared page used for the request and response message. */ snp_dev->request =alloc_shared_pages (dev,sizeof (struct snp_guest_msg ));
@@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO ; - snp_dev->ctx =snp_init_crypto (snp_dev->vmpck,VMPCK_KEY_LEN ); + snp_dev->ctx =snp_init_crypto (snp_dev);if (!snp_dev->ctx) goto e_free_cert_data;
Signature · 1 line
2.34.1
Re: [PATCH v5 05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct
Quoted 59 lines · Nikunj A Dadhania
Drop vmpck and
os_area_msg_seqnopointers 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 andos_area_msg_seqnowhen needed.Also, change function
is_vmpck_empty()tosnp_is_vmpck_empty()in preparation for moving tosev.c.Signed-off-by Nikunj A Dadhania <[email protected]>--- drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- 1 file changed, 42 insertions(+), 43 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +14
-7@@ -50,8 +50,7 @@ struct snp_guest_dev {struct snp_secrets_page_layout *layout;struct snp_req_data input; -u32 *os_area_msg_seqno; -u8 *vmpck; +unsigned int vmpck_id; };static u32 vmpck_id;@@ -61,14 +60,22 @@ 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 bool is_vmpck_empty (struct snp_guest_dev *snp_dev) +static inline u8 *snp_get_vmpck (struct snp_guest_dev *snp_dev) { -char zero_key[VMPCK_KEY_LEN ] = {0 }; +return snp_dev->layout->vmpck0 + snp_dev->vmpck_id *VMPCK_KEY_LEN ; +} -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); +static inline u32 *snp_get_os_area_msg_seqno (struct snp_guest_dev *snp_dev) +{ +return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; +} -return true; +static bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; +u8 *key =snp_get_vmpck (snp_dev); + +return !memcmp (key, zero_key,VMPCK_KEY_LEN ); }/* @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)*/ static void snp_disable_vmpck (struct snp_guest_dev *snp_dev)
drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 105
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +4
-3{ +u8 *key =snp_get_vmpck (snp_dev); +dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - vmpck_id); -memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); - snp_dev->vmpck =NULL ; + snp_dev->vmpck_id); +memzero_explicit (key,VMPCK_KEY_LEN ); }
We disable the VMPCK because we believe the guest to be under attack, but this only clears a single key. Shouldn't we clear all VMPCK keys in the secrets page for good measure? If at VMPCK > 0, most likely the 0..VMPCK-1 keys have been zeroed by whatever was prior in the boot stack, but still better to be safe.
Quoted 64 lines
drivers/virt/coco/sev-guest/sev-guest.c +15
-5static 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_dev->cmd_mutex);/* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; + count = *os_area_msg_seqno;return count +1 ; }@@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev)static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) { +u32 *os_area_msg_seqno =snp_get_os_area_msg_seqno (snp_dev); +/* * The counter is also incremented by the PSP, so increment it by 2 * and save in secrets page. */ - *snp_dev->os_area_msg_seqno +=2 ; + *os_area_msg_seqno +=2 ; }static inline struct snp_guest_dev *to_snp_dev (struct file *file)@@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) +static struct aesgcm_ctx *snp_init_crypto (struct snp_guest_dev *snp_dev) {struct aesgcm_ctx *ctx; +u8 *key; + +if (snp_is_vmpck_empty (snp_dev)) { +pr_err ("SNP: vmpck id %d is null\n" , snp_dev->vmpck_id); +return NULL ; + } ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ; -if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { + key =snp_get_vmpck (snp_dev); +if (aesgcm_expandkey (ctx, key,VMPCK_KEY_LEN ,AUTHTAG_LEN )) {pr_err ("SNP: crypto init failed\n" );kfree (ctx);return NULL ;@@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longmutex_lock (&snp_dev->cmd_mutex);/* Check if the VMPCK is not empty */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ;
drivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 680
Quoted 9 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2@@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck (int id,struct snp_secrets_page_layout *layout,u32 **seqno) +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) { -u8 *key =NULL ; +if (WARN_ON (vmpck_id >3 )) +return false;
The vmpck_id is an int for some reason, so < 0 is also a problem. Can we not use unsigned int?
Quoted 66 lines
drivers/virt/coco/sev-guest/sev-guest.c +6
-27-switch (id) { -case 0 : - *seqno = &layout->os_area.msg_seqno_0; - key = layout->vmpck0; -break ; -case 1 : - *seqno = &layout->os_area.msg_seqno_1; - key = layout->vmpck1; -break ; -case 2 : - *seqno = &layout->os_area.msg_seqno_2; - key = layout->vmpck2; -break ; -case 3 : - *seqno = &layout->os_area.msg_seqno_3; - key = layout->vmpck3; -break ; -default : -break ; - } + dev->vmpck_id = vmpck_id; -return key; +return true; }static int __init sev_guest_probe (struct platform_device *pdev)@@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL ; - snp_dev->vmpck =get_vmpck (vmpck_id, layout, &snp_dev->os_area_msg_seqno); -if (!snp_dev->vmpck) { + snp_dev->layout = layout; +if (!snp_assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); goto e_unmap; }@@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev)mutex_init (&snp_dev->cmd_mutex);platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; - snp_dev->layout = layout;/* Allocate the shared page used for the request and response message. */ snp_dev->request =alloc_shared_pages (dev,sizeof (struct snp_guest_msg ));@@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO ; - snp_dev->ctx =snp_init_crypto (snp_dev->vmpck,VMPCK_KEY_LEN ); + snp_dev->ctx =snp_init_crypto (snp_dev);if (!snp_dev->ctx) goto e_free_cert_data; --2.34.1
Signature · 1 line
-Dionna Glaze, PhD (she/her)
Re: [PATCH v5 05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct
Quoted 68 lines · Dionna Amalie Glaze
Quoted 67 lines · Nikunj A Dadhania
Drop vmpck and
os_area_msg_seqnopointers 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 andos_area_msg_seqnowhen needed.Also, change function
is_vmpck_empty()tosnp_is_vmpck_empty()in preparation for moving tosev.c.Signed-off-by Nikunj A Dadhania <[email protected]>--- drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- 1 file changed, 42 insertions(+), 43 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +17
-10@@ -50,8 +50,7 @@ struct snp_guest_dev {struct snp_secrets_page_layout *layout;struct snp_req_data input; -u32 *os_area_msg_seqno; -u8 *vmpck; +unsigned int vmpck_id; };static u32 vmpck_id;@@ -61,14 +60,22 @@ 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 bool is_vmpck_empty (struct snp_guest_dev *snp_dev) +static inline u8 *snp_get_vmpck (struct snp_guest_dev *snp_dev) { -char zero_key[VMPCK_KEY_LEN ] = {0 }; +return snp_dev->layout->vmpck0 + snp_dev->vmpck_id *VMPCK_KEY_LEN ; +} -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); +static inline u32 *snp_get_os_area_msg_seqno (struct snp_guest_dev *snp_dev) +{ +return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; +} -return true; +static bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; +u8 *key =snp_get_vmpck (snp_dev); + +return !memcmp (key, zero_key,VMPCK_KEY_LEN ); }/* @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)*/ static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) { +u8 *key =snp_get_vmpck (snp_dev); +dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - vmpck_id); -memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); - snp_dev->vmpck =NULL ; + snp_dev->vmpck_id);
Quoted 8 lines
drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 105
Quoted 2 lines
drivers/virt/coco/sev-guest/sev-guest.c +1
-0+memzero_explicit (key,VMPCK_KEY_LEN ); }We disable the VMPCK because we believe the guest to be under attack, but this only clears a single key. Shouldn't we clear all VMPCK keys in the secrets page for good measure? If at VMPCK > 0, most likely the 0..VMPCK-1 keys have been zeroed by whatever was prior in the boot stack, but still better to be safe.
Doing that would be a separate patch series and isn't appropriate here.
Thanks,
Tom
Quoted 146 lines
Quoted 65 lines
drivers/virt/coco/sev-guest/sev-guest.c +15
-5static 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_dev->cmd_mutex);/* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; + count = *os_area_msg_seqno;return count +1 ; }@@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev)static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) { +u32 *os_area_msg_seqno =snp_get_os_area_msg_seqno (snp_dev); +/* * The counter is also incremented by the PSP, so increment it by 2 * and save in secrets page. */ - *snp_dev->os_area_msg_seqno +=2 ; + *os_area_msg_seqno +=2 ; }static inline struct snp_guest_dev *to_snp_dev (struct file *file)@@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) +static struct aesgcm_ctx *snp_init_crypto (struct snp_guest_dev *snp_dev) {struct aesgcm_ctx *ctx; +u8 *key; + +if (snp_is_vmpck_empty (snp_dev)) { +pr_err ("SNP: vmpck id %d is null\n" , snp_dev->vmpck_id); +return NULL ; + } ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ; -if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { + key =snp_get_vmpck (snp_dev); +if (aesgcm_expandkey (ctx, key,VMPCK_KEY_LEN ,AUTHTAG_LEN )) {pr_err ("SNP: crypto init failed\n" );kfree (ctx);return NULL ;@@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longmutex_lock (&snp_dev->cmd_mutex);/* Check if the VMPCK is not empty */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ;drivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 680
Quoted 9 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2@@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck (int id,struct snp_secrets_page_layout *layout,u32 **seqno) +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) { -u8 *key =NULL ; +if (WARN_ON (vmpck_id >3 )) +return false;The
vmpck_idis an int for some reason, so < 0 is also a problem. Can we not use unsigned int?Quoted 66 lines
drivers/virt/coco/sev-guest/sev-guest.c +6
-27-switch (id) { -case 0 : - *seqno = &layout->os_area.msg_seqno_0; - key = layout->vmpck0; -break ; -case 1 : - *seqno = &layout->os_area.msg_seqno_1; - key = layout->vmpck1; -break ; -case 2 : - *seqno = &layout->os_area.msg_seqno_2; - key = layout->vmpck2; -break ; -case 3 : - *seqno = &layout->os_area.msg_seqno_3; - key = layout->vmpck3; -break ; -default : -break ; - } + dev->vmpck_id = vmpck_id; -return key; +return true; }static int __init sev_guest_probe (struct platform_device *pdev)@@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL ; - snp_dev->vmpck =get_vmpck (vmpck_id, layout, &snp_dev->os_area_msg_seqno); -if (!snp_dev->vmpck) { + snp_dev->layout = layout; +if (!snp_assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); goto e_unmap; }@@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev)mutex_init (&snp_dev->cmd_mutex);platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; - snp_dev->layout = layout;/* Allocate the shared page used for the request and response message. */ snp_dev->request =alloc_shared_pages (dev,sizeof (struct snp_guest_msg ));@@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO ; - snp_dev->ctx =snp_init_crypto (snp_dev->vmpck,VMPCK_KEY_LEN ); + snp_dev->ctx =snp_init_crypto (snp_dev);if (!snp_dev->ctx) goto e_free_cert_data; --2.34.1
Re: [PATCH v5 05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct
Quoted 7 lines · Tom Lendacky
Quoted 6 lines · Dionna Amalie Glaze
Quoted 4 lines · Nikunj A Dadhania
drivers/virt/coco/sev-guest/sev-guest.c +0
-0@@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, };
Quoted 9 lines
Quoted 9 lines
drivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 680
Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2-static u8 *get_vmpck (int id,struct snp_secrets_page_layout *layout,u32 **seqno) +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) { - u8 *key = NULL; + if (WARN_ON(vmpck_id > 3)) + return false;The
vmpck_idis an int for some reason, so < 0 is also a problem. Can we not use unsigned int?
Yes, I will update that in my next revision,
Thanks
Nikunj
Re: [PATCH v5 05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct
In reply to Nikunj A DadhaniaQuoted 9 lines · Nikunj A Dadhania
Drop vmpck and
os_area_msg_seqnopointers 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 andos_area_msg_seqnowhen needed.Also, change function
is_vmpck_empty()tosnp_is_vmpck_empty()in preparation for moving tosev.c.Signed-off-by Nikunj A Dadhania <[email protected]>
With the fix to the snp_assign_vmpck() to change the int to an unsigned int as requested by Dionna...
Quoted 195 lines
--- drivers/virt/coco/sev-guest/sev-guest.c | 85 ++++++++++++------------- 1 file changed, 42 insertions(+), 43 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +42
-43@@ -50,8 +50,7 @@ struct snp_guest_dev {struct snp_secrets_page_layout *layout;struct snp_req_data input; -u32 *os_area_msg_seqno; -u8 *vmpck; +unsigned int vmpck_id; };static u32 vmpck_id;@@ -61,14 +60,22 @@ 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 bool is_vmpck_empty (struct snp_guest_dev *snp_dev) +static inline u8 *snp_get_vmpck (struct snp_guest_dev *snp_dev) { -char zero_key[VMPCK_KEY_LEN ] = {0 }; +return snp_dev->layout->vmpck0 + snp_dev->vmpck_id *VMPCK_KEY_LEN ; +} -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); +static inline u32 *snp_get_os_area_msg_seqno (struct snp_guest_dev *snp_dev) +{ +return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; +} -return true; +static bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; +u8 *key =snp_get_vmpck (snp_dev); + +return !memcmp (key, zero_key,VMPCK_KEY_LEN ); }/* @@ -90,20 +97,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)*/ static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) { +u8 *key =snp_get_vmpck (snp_dev); +dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - vmpck_id); -memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); - snp_dev->vmpck =NULL ; + snp_dev->vmpck_id); +memzero_explicit (key,VMPCK_KEY_LEN ); }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_dev->cmd_mutex);/* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; + count = *os_area_msg_seqno;return count +1 ; }@@ -131,11 +140,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev)static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) { +u32 *os_area_msg_seqno =snp_get_os_area_msg_seqno (snp_dev); +/* * The counter is also incremented by the PSP, so increment it by 2 * and save in secrets page. */ - *snp_dev->os_area_msg_seqno +=2 ; + *os_area_msg_seqno +=2 ; }static inline struct snp_guest_dev *to_snp_dev (struct file *file)@@ -145,15 +156,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) +static struct aesgcm_ctx *snp_init_crypto (struct snp_guest_dev *snp_dev) {struct aesgcm_ctx *ctx; +u8 *key; + +if (snp_is_vmpck_empty (snp_dev)) { +pr_err ("SNP: vmpck id %d is null\n" , snp_dev->vmpck_id); +return NULL ; + } ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ; -if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { + key =snp_get_vmpck (snp_dev); +if (aesgcm_expandkey (ctx, key,VMPCK_KEY_LEN ,AUTHTAG_LEN )) {pr_err ("SNP: crypto init failed\n" );kfree (ctx);return NULL ;@@ -586,7 +604,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longmutex_lock (&snp_dev->cmd_mutex);/* Check if the VMPCK is not empty */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ;@@ -656,32 +674,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck (int id,struct snp_secrets_page_layout *layout,u32 **seqno) +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) { -u8 *key =NULL ; +if (WARN_ON (vmpck_id >3 )) +return false; -switch (id) { -case 0 : - *seqno = &layout->os_area.msg_seqno_0; - key = layout->vmpck0; -break ; -case 1 : - *seqno = &layout->os_area.msg_seqno_1; - key = layout->vmpck1; -break ; -case 2 : - *seqno = &layout->os_area.msg_seqno_2; - key = layout->vmpck2; -break ; -case 3 : - *seqno = &layout->os_area.msg_seqno_3; - key = layout->vmpck3; -break ; -default : -break ; - } + dev->vmpck_id = vmpck_id; -return key; +return true; }static int __init sev_guest_probe (struct platform_device *pdev)@@ -713,14 +713,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL ; - snp_dev->vmpck =get_vmpck (vmpck_id, layout, &snp_dev->os_area_msg_seqno); -if (!snp_dev->vmpck) { + snp_dev->layout = layout; +if (!snp_assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); goto e_unmap; }@@ -728,7 +728,6 @@ static int __init sev_guest_probe(struct platform_device *pdev)mutex_init (&snp_dev->cmd_mutex);platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; - snp_dev->layout = layout;/* Allocate the shared page used for the request and response message. */ snp_dev->request =alloc_shared_pages (dev,sizeof (struct snp_guest_msg ));@@ -744,7 +743,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO ; - snp_dev->ctx =snp_init_crypto (snp_dev->vmpck,VMPCK_KEY_LEN ); + snp_dev->ctx =snp_init_crypto (snp_dev);if (!snp_dev->ctx) goto e_free_cert_data;
PATCH v5 6/14 x86/sev: Cache the secrets page address
In reply to Nikunj A DadhaniaSave 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 | 52 +++++++++++++------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-)
arch/x86/kernel/sev.c +16 -36
@@ -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 ;
@@ -2083,6 +2057,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;
@@ -2094,6 +2074,8 @@ bool __init snp_init(struct boot_params *bp)if (!cc_info)return false; +set_secrets_pa (cc_info); +setup_cpuid_table (cc_info);/*
@@ -2246,16 +2228,14 @@ 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)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
PATCH v5 7/14 x86/sev: Move and reorganize sev guest request api
In reply to Nikunj A DadhaniaFor 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 an API to the driver to assign key and send guest request.
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/sev-guest.h | 84 +++- arch/x86/include/asm/sev.h | 10 - arch/x86/kernel/sev.c | 466 ++++++++++++++++++++++- drivers/virt/coco/sev-guest/Kconfig | 1 - drivers/virt/coco/sev-guest/sev-guest.c | 486 +----------------------- 6 files changed, 555 insertions(+), 493 deletions(-)
arch/x86/Kconfig +1 -0
@@ -1509,6 +1509,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 +82 -2
105 lines not on this page · raw message
arch/x86/include/asm/sev.h +0 -10
@@ -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 +454 -12
499 lines not on this page · raw message
drivers/virt/coco/sev-guest/Kconfig +0 -1
@@ -2,7 +2,6 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - select CRYPTO_LIB_AESGCM help SEV-SNP firmware provides the guest a mechanism to communicate with the PSP without risk from a malicious hypervisor who wishes to read,
drivers/virt/coco/sev-guest/sev-guest.c +18 -468
565 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 07/14] x86/sev: Move and reorganize sev guest request api
Quoted 93 lines · 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 an API to the driver to assign key and send guest request.
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/include/asm/sev-guest.h | 84 +++- arch/x86/include/asm/sev.h | 10 - arch/x86/kernel/sev.c | 466 ++++++++++++++++++++++- drivers/virt/coco/sev-guest/Kconfig | 1 - drivers/virt/coco/sev-guest/sev-guest.c | 486 +----------------------- 6 files changed, 555 insertions(+), 493 deletions(-)arch/x86/Kconfig +1
-0@@ -1509,6 +1509,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 Memoryarch/x86/include/asm/sev-guest.h +39
-0@@ -11,6 +11,11 @@#define __VIRT_SEVGUEST_H__#include <linux/types.h> +#include <linux/miscdevice.h> +#include <asm/sev.h> + +#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) +#define SNP_REQ_RETRY_DELAY (2*HZ)#define MAX_AUTHTAG_LEN 32#define AUTHTAG_LEN 16@@ -58,11 +63,45 @@ struct snp_guest_msg_hdr {u8 rsvd3[35 ]; } __packed; +/* SNP Guest message request */ +struct snp_req_data { +unsigned long req_gpa; +unsigned long resp_gpa; +}; +struct snp_guest_msg {struct snp_guest_msg_hdr hdr;u8 payload[4000 ]; } __packed; +struct sev_guest_platform_data { +/* request and response are in unencrypted memory */ +struct snp_guest_msg *request, *response; + +struct snp_secrets_page_layout *layout; +struct snp_req_data input; +}; + +struct snp_guest_dev { +struct device *dev; +struct miscdevice misc; + +/* Mutex to serialize the shared buffer access and command handling. */ +struct mutex cmd_mutex; + +void *certs_data; +struct aesgcm_ctx *ctx; + +/* +* Avoid information leakage by double-buffering shared messages +* in fields that are in regular encrypted memory +*/ +struct snp_guest_msg secret_request, secret_response; + +struct sev_guest_platform_data *pdata; +unsigned int vmpck_id; +}; +struct snp_guest_req {void *req_buf, *resp_buf, *data;size_t req_sz, resp_sz, *data_npages;@@ -72,6 +111,47 @@ struct snp_guest_req {u8 msg_type; };
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 120
Quoted 10 lines
arch/x86/include/asm/sev-guest.h +7
-2-int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, -struct snp_guest_request_ioctl *rio); +int snp_setup_psp_messaging (struct snp_guest_dev *snp_dev); +int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req, +struct snp_guest_request_ioctl *rio); +bool snp_assign_vmpck (struct snp_guest_dev *dev,unsigned int vmpck_id); +bool snp_is_vmpck_empty (unsigned int vmpck_id); + +static void free_shared_pages (void *buf,size_t sz)
These should probably be marked __inline if you're going to define them in a header file.
Quoted 166 lines
arch/x86/include/asm/sev-guest.h +36
-0+{ +unsigned int npages =PAGE_ALIGN (sz) >>PAGE_SHIFT ; +int ret; + +if (!buf) +return ; + + ret =set_memory_encrypted ((unsigned long )buf, npages); +if (ret) { +WARN_ONCE (ret,"failed to restore encryption mask (leak it)\n" ); +return ; + } + +__free_pages (virt_to_page (buf),get_order (sz)); +} + +static void *alloc_shared_pages (size_t sz) +{ +unsigned int npages =PAGE_ALIGN (sz) >>PAGE_SHIFT ; +struct page *page; +int ret; + + page =alloc_pages (GFP_KERNEL_ACCOUNT ,get_order (sz)); +if (!page) +return NULL ; + + ret =set_memory_decrypted ((unsigned long )page_address (page), npages); +if (ret) { +pr_err ("%s: failed to mark page shared, ret=%d\n" , __func__, ret); +__free_pages (page,get_order (sz)); +return NULL ; + } + +return page_address (page); +} +#endif /* __VIRT_SEVGUEST_H__ */ arch/x86/include/asm/sev.h +0
-10@@ -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 +93
-0@@ -24,6 +24,7 @@#include <linux/io.h> #include <linux/psp-sev.h> #include <uapi/linux/sev-guest.h> +#include <crypto/gcm.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> @@ -941,6 +942,457 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)free_page ((unsigned long )vmsa); } +static struct sev_guest_platform_data *platform_data; + +static inline u8 *snp_get_vmpck (unsigned int vmpck_id) +{ +if (!platform_data) +return NULL ; + +return platform_data->layout->vmpck0 + vmpck_id *VMPCK_KEY_LEN ; +} + +static inline u32 *snp_get_os_area_msg_seqno (unsigned int vmpck_id) +{ +if (!platform_data) +return NULL ; + +return &platform_data->layout->os_area.msg_seqno_0 + vmpck_id; +} + +bool snp_is_vmpck_empty (unsigned int vmpck_id) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; +u8 *key =snp_get_vmpck (vmpck_id); + +if (key) +return !memcmp (key, zero_key,VMPCK_KEY_LEN ); + +return true; +} +EXPORT_SYMBOL_GPL (snp_is_vmpck_empty); + +/* +* If an error is received from the host or AMD Secure Processor (ASP) there +* are two options. Either retry the exact same encrypted request or discontinue +* using the VMPCK. +* +* This is because in the current encryption scheme GHCB v2 uses AES-GCM to +* encrypt the requests. The IV for this scheme is the sequence number. GCM +* cannot tolerate IV reuse. +* +* The ASP FW v1.51 only increments the sequence numbers on a successful +* guest<->ASP back and forth and only accepts messages at its exact sequence +* number. +* +* So if the sequence number were to be reused the encryption scheme is +* vulnerable. If the sequence number were incremented for a fresh IV the ASP +* will reject the request. +*/ +static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) +{ +u8 *key =snp_get_vmpck (snp_dev->vmpck_id); + +pr_alert ("Disabling vmpck_id %d to prevent IV reuse.\n" , snp_dev->vmpck_id); +memzero_explicit (key,VMPCK_KEY_LEN ); +} + +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->vmpck_id); +u64 count; + +if (!os_area_msg_seqno) { +pr_err ("SNP unable to get message sequence counter\n" ); +return 0 ; + } + +lockdep_assert_held (&snp_dev->cmd_mutex); + +/* Read the current message sequence counter from secrets pages */ + count = *os_area_msg_seqno; + +return count +1 ; +} + +/* Return a non-zero on success */ +static u64 snp_get_msg_seqno (struct snp_guest_dev *snp_dev) +{ +u64 count =__snp_get_msg_seqno (snp_dev); + +/* +* The message sequence counter for the SNP guest request is a 64-bit +* value but the version 2 of GHCB specification defines a 32-bit storage +* for it. If the counter exceeds the 32-bit value then return zero. +* The caller should check the return value, but if the caller happens to +* not check the value and use it, then the firmware treats zero as an +* invalid number and will fail the message request. +*/ +if (count >=UINT_MAX ) { +pr_err ("SNP request message sequence counter overflow\n" ); +return 0 ; + } + +return count;
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1046
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+} + +static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) +{ +u32 *os_area_msg_seqno =snp_get_os_area_msg_seqno (snp_dev->vmpck_id); + +if (!os_area_msg_seqno) { +pr_err ("SNP unable to get message sequence counter\n" ); +return ; + }
I probably missed this in the other patch or even when the driver was first created, but shouldn't we have a lockdep_assert_held() here, too, before updating the count?
Quoted 39 lines
arch/x86/kernel/sev.c +39
-0+ +/* +* The counter is also incremented by the PSP, so increment it by 2 +* and save in secrets page. +*/ + *os_area_msg_seqno +=2 ; +} + +static struct aesgcm_ctx *snp_init_crypto (unsigned int vmpck_id) +{ +struct aesgcm_ctx *ctx; +u8 *key; + +if (snp_is_vmpck_empty (vmpck_id)) { +pr_err ("SNP: vmpck id %d is null\n" , vmpck_id); +return NULL ; + } + + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT ); +if (!ctx) +return NULL ; + + key =snp_get_vmpck (vmpck_id); +if (aesgcm_expandkey (ctx, key,VMPCK_KEY_LEN ,AUTHTAG_LEN )) { +pr_err ("SNP: crypto init failed\n" ); +kfree (ctx); +return NULL ; + } + +return ctx; +} + +int snp_setup_psp_messaging (struct snp_guest_dev *snp_dev) +{ +struct sev_guest_platform_data *pdata; +int ret; + +if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP )) { +pr_err ("SNP not supported\n" );
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1095
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+return 0 ; + } + +if (platform_data) { +pr_debug ("SNP platform data already initialized.\n" ); + goto create_ctx; + } + +if (!secrets_pa) { +pr_err ("SNP no secrets page\n" );
Maybe "SNP secrets page not found\n" ?
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1101
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+return -ENODEV ; + } + + pdata =kzalloc (sizeof (struct sev_guest_platform_data ),GFP_KERNEL ); +if (!pdata) { +pr_err ("SNP alloc failed\n" );
Maybe "Allocation of SNP guest platform data failed\n" ?
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1107
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+return -ENOMEM ; + } + + pdata->layout = (__force void *)ioremap_encrypted (secrets_pa,PAGE_SIZE ); +if (!pdata->layout) { +pr_err ("Unable to locate AP jump table address: failed to map the SNP secrets page.\n" );
Maybe "Failed to map SNP secrets page\n" ? Not sure where the AP jump table came in on this...
Quoted 13 lines
arch/x86/kernel/sev.c +13
-0+ goto e_free_pdata; + } + + ret = -ENOMEM ; +/* Allocate the shared page used for the request and response message. */ + pdata->request =alloc_shared_pages (sizeof (struct snp_guest_msg )); +if (!pdata->request) + goto e_unmap; + + pdata->response =alloc_shared_pages (sizeof (struct snp_guest_msg )); +if (!pdata->response) + goto e_free_request; +
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1130
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+/* initial the input address for guest request */ + pdata->input.req_gpa =__pa (pdata->request); + pdata->input.resp_gpa =__pa (pdata->response); + platform_data = pdata; + +create_ctx: + ret = -EIO ; + snp_dev->ctx =snp_init_crypto (snp_dev->vmpck_id); +if (!snp_dev->ctx) { +pr_err ("SNP init crypto failed\n" );
Maybe "SNP crypto context initialization failed\n" ?
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1135
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+ platform_data =NULL ; + goto e_free_response; + } + + snp_dev->pdata = platform_data;
Add a blank line here.
Quoted 97 lines
arch/x86/kernel/sev.c +97
-0+return 0 ; + +e_free_response: +free_shared_pages (pdata->response,sizeof (struct snp_guest_msg )); +e_free_request: +free_shared_pages (pdata->request,sizeof (struct snp_guest_msg )); +e_unmap: +iounmap (pdata->layout); +e_free_pdata: +kfree (pdata); + +return ret; +} +EXPORT_SYMBOL_GPL (snp_setup_psp_messaging); + +static int __enc_payload (struct aesgcm_ctx *ctx,struct snp_guest_msg *msg, +void *plaintext,size_t len) +{ +struct snp_guest_msg_hdr *hdr = &msg->hdr; +u8 iv[GCM_AES_IV_SIZE ] = {}; + +if (WARN_ON ((hdr->msg_sz + ctx->authsize) >sizeof (msg->payload))) +return -EBADMSG ; + +memcpy (iv, &hdr->msg_seqno,sizeof (hdr->msg_seqno)); +aesgcm_encrypt (ctx, msg->payload, plaintext, len, &hdr->algo,AAD_LEN , + iv, hdr->authtag); +return 0 ; +} + +static int dec_payload (struct aesgcm_ctx *ctx,struct snp_guest_msg *msg, +void *plaintext,size_t len) +{ +struct snp_guest_msg_hdr *hdr = &msg->hdr; +u8 iv[GCM_AES_IV_SIZE ] = {}; + +memcpy (iv, &hdr->msg_seqno,sizeof (hdr->msg_seqno)); +if (aesgcm_decrypt (ctx, plaintext, msg->payload, len, &hdr->algo, +AAD_LEN , iv, hdr->authtag)) +return 0 ; +else +return -EBADMSG ; +} + +static int verify_and_dec_payload (struct snp_guest_dev *snp_dev,struct snp_guest_req *guest_req, +struct sev_guest_platform_data *pdata) +{ +struct snp_guest_msg *resp = &snp_dev->secret_response; +struct snp_guest_msg *req = &snp_dev->secret_request; +struct snp_guest_msg_hdr *req_hdr = &req->hdr; +struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; +struct aesgcm_ctx *ctx = snp_dev->ctx; + +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, pdata->response,sizeof (*resp)); + +/* Verify that the sequence counter is incremented by 1 */ +if (unlikely (resp_hdr->msg_seqno != (req_hdr->msg_seqno +1 ))) +return -EBADMSG ; + +/* Verify response message type and version number. */ +if (resp_hdr->msg_type != (req_hdr->msg_type +1 ) || + resp_hdr->msg_version != req_hdr->msg_version) +return -EBADMSG ; + +/* +* If the message size is greater than our buffer length then return +* an error. +*/ +if (unlikely ((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz)) +return -EBADMSG ; + +return dec_payload (ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); +} + +static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno,struct snp_guest_req *req) +{ +struct snp_guest_msg *msg = &snp_dev->secret_request; +struct snp_guest_msg_hdr *hdr = &msg->hdr; + +memset (msg,0 ,sizeof (*msg)); + + hdr->algo =SNP_AEAD_AES_256_GCM ; + hdr->hdr_version =MSG_HDR_VER ; + hdr->hdr_sz =sizeof (*hdr); + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; + hdr->msg_seqno = seqno; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz; + +/* Verify the sequence number is non-zero */ +if (!hdr->msg_seqno)
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1242
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+return -ENOSR ; + +pr_debug ("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->ctx, msg, req->req_buf, req->req_sz); +} + +static int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, +struct snp_guest_request_ioctl *rio);
Could all of these routines been moved down closer to the bottom of the file to avoid this forward declaration?
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, +struct snp_guest_request_ioctl *rio, +struct sev_guest_platform_data *pdata) +{ +unsigned long req_start = jiffies; +unsigned int override_npages =0 ; +u64 override_err =0 ; +int rc; +
...
Quoted 19 lines
arch/x86/kernel/sev.c +4
-8-e_free_ctx: -kfree (snp_dev->ctx); e_free_cert_data:free_shared_pages (snp_dev->certs_data,SEV_FW_BLOB_MAX_SIZE ); -e_free_response: -free_shared_pages (snp_dev->response,sizeof (struct snp_guest_msg )); -e_free_request: -free_shared_pages (snp_dev->request,sizeof (struct snp_guest_msg )); -e_unmap: -iounmap (mapping); + e_free_ctx: +kfree (snp_dev->ctx); +e_free_snpdev: +kfree (snp_dev);return ret; }
arch/x86/kernel/sev.c static int __exit sev_guest_remove(struct platform_device *pdev) · line 783
Quoted 4 lines
arch/x86/kernel/sev.c +0
-1@@ -780,11 +332,9 @@ static int __exit sev_guest_remove(struct platform_device *pdev) {struct snp_guest_dev *snp_dev =platform_get_drvdata (pdev); -free_shared_pages (snp_dev->certs_data,SEV_FW_BLOB_MAX_SIZE );
Looks like this one should still be here, right?
Thanks,
Tom
Quoted 9 lines
arch/x86/kernel/sev.c +2
-3-free_shared_pages (snp_dev->response,sizeof (struct snp_guest_msg )); -free_shared_pages (snp_dev->request,sizeof (struct snp_guest_msg )); -kfree (snp_dev->ctx);misc_deregister (&snp_dev->misc); +kfree (snp_dev->ctx); +kfree (snp_dev);return 0 ; }
Re: [PATCH v5 07/14] x86/sev: Move and reorganize sev guest request api
Quoted 10 lines · Tom Lendacky
Quoted 8 lines · 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 an API to the driver to assign key and send guest request.
Signed-off-by Nikunj A Dadhania <[email protected]>---
Quoted 14 lines
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 120
Quoted 12 lines
arch/x86/include/asm/sev-guest.h +7
-1@@ -72,6 +111,47 @@ struct snp_guest_req { u8 msg_type; }; -int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, - struct snp_guest_request_ioctl *rio); +int snp_setup_psp_messaging(struct snp_guest_dev *snp_dev); +int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio); +bool snp_assign_vmpck (struct snp_guest_dev *dev,unsigned int vmpck_id); +bool snp_is_vmpck_empty (unsigned int vmpck_id); + +static void free_shared_pages (void *buf,size_t sz)These should probably be marked
__inlineif you're going to define them in a header file.
Sure, I will udpate both free_shared_pages() and alloc_shared_pages().
Quoted 167 lines
Quoted 167 lines
arch/x86/include/asm/sev-guest.h +36
-0+{ + unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + int ret; + + if (!buf) + return; + + ret = set_memory_encrypted((unsigned long)buf, npages); + if (ret) { + WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n"); + return; + } + + __free_pages(virt_to_page(buf), get_order(sz)); +} + +static void *alloc_shared_pages (size_t sz) +{ + unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + struct page *page; + int ret; + + page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz)); + if (!page) + return NULL; + + ret = set_memory_decrypted((unsigned long)page_address(page), npages); + if (ret) { + pr_err("%s: failed to mark page shared, ret=%d\n", __func__, ret); + __free_pages(page, get_order(sz)); + return NULL; + } + + return page_address(page); +} + #endif /* __VIRT_SEVGUEST_H__ */arch/x86/include/asm/sev.h +0
-0@@ -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 */
arch/x86/include/asm/sev.h +0
-9-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 sequencearch/x86/kernel/sev.c +96
-0@@ -24,6 +24,7 @@ #include <linux/io.h> #include <linux/psp-sev.h> #include <uapi/linux/sev-guest.h> +#include <crypto/gcm.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h>@@ -941,6 +942,457 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) free_page((unsigned long)vmsa); } +static struct sev_guest_platform_data *platform_data; + +static inline u8 *snp_get_vmpck (unsigned int vmpck_id) +{ + if (!platform_data) + return NULL; + + return platform_data->layout->vmpck0 + vmpck_id * VMPCK_KEY_LEN; +} + +static inline u32 *snp_get_os_area_msg_seqno (unsigned int vmpck_id) +{ + if (!platform_data) + return NULL; + + return &platform_data->layout->os_area.msg_seqno_0 + vmpck_id; +} + +bool snp_is_vmpck_empty (unsigned int vmpck_id) +{ + char zero_key[VMPCK_KEY_LEN] = {0}; + u8 *key = snp_get_vmpck(vmpck_id); + + if (key) + return !memcmp(key, zero_key, VMPCK_KEY_LEN); + + return true; +} +EXPORT_SYMBOL_GPL (snp_is_vmpck_empty); + +/* +* If an error is received from the host or AMD Secure Processor (ASP) there +* are two options. Either retry the exact same encrypted request or discontinue +* using the VMPCK. +* +* This is because in the current encryption scheme GHCB v2 uses AES-GCM to +* encrypt the requests. The IV for this scheme is the sequence number. GCM +* cannot tolerate IV reuse. +* +* The ASP FW v1.51 only increments the sequence numbers on a successful +* guest<->ASP back and forth and only accepts messages at its exact sequence +* number. +* +* So if the sequence number were to be reused the encryption scheme is +* vulnerable. If the sequence number were incremented for a fresh IV the ASP +* will reject the request. +*/ +static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) +{ + u8 *key = snp_get_vmpck(snp_dev->vmpck_id); + + pr_alert("Disabling vmpck_id %d to prevent IV reuse.\n", snp_dev->vmpck_id); + memzero_explicit(key, VMPCK_KEY_LEN); +} + +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->vmpck_id); + u64 count; + + if (!os_area_msg_seqno) { + pr_err("SNP unable to get message sequence counter\n"); + return 0; + } + + lockdep_assert_held(&snp_dev->cmd_mutex); + + /* Read the current message sequence counter from secrets pages */ + count = *os_area_msg_seqno; + + return count + 1; +} + +/* Return a non-zero on success */ +static u64 snp_get_msg_seqno (struct snp_guest_dev *snp_dev) +{ + u64 count = __snp_get_msg_seqno(snp_dev); + + /* + * The message sequence counter for the SNP guest request is a 64-bit + * value but the version 2 of GHCB specification defines a 32-bit storage + * for it. If the counter exceeds the 32-bit value then return zero. + * The caller should check the return value, but if the caller happens to + * not check the value and use it, then the firmware treats zero as an + * invalid number and will fail the message request. + */ + if (count >= UINT_MAX) { + pr_err("SNP request message sequence counter overflow\n"); + return 0; + } + + return count; +} + +static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) +{
Quoted 8 lines
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1046
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+ u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev->vmpck_id); + + if (!os_area_msg_seqno) { + pr_err("SNP unable to get message sequence counter\n"); + return; + }I probably missed this in the other patch or even when the driver was first created, but shouldn't we have a
lockdep_assert_held()here, too, before updating the count?
As per the current code flow, snp_get_msg_seqno() is always called before snp_inc_msg_seqno(), maybe because of that the check wasnt there. It still makes sense to have a lockdep_assert_held() in snp_inc_msg_seqno().
Should I add this change as a separate fix ?
Quoted 206 lines
Quoted 40 lines
arch/x86/kernel/sev.c +39
-0+ + /* + * The counter is also incremented by the PSP, so increment it by 2 + * and save in secrets page. + */ + *os_area_msg_seqno += 2; +} + +static struct aesgcm_ctx *snp_init_crypto (unsigned int vmpck_id) +{ + struct aesgcm_ctx *ctx; + u8 *key; + + if (snp_is_vmpck_empty(vmpck_id)) { + pr_err("SNP: vmpck id %d is null\n", vmpck_id); + return NULL; + } + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); + if (!ctx) + return NULL; + + key = snp_get_vmpck(vmpck_id); + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { + pr_err("SNP: crypto init failed\n"); + kfree(ctx); + return NULL; + } + + return ctx; +} + +int snp_setup_psp_messaging (struct snp_guest_dev *snp_dev) +{ + struct sev_guest_platform_data *pdata; + int ret; + + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { + pr_err("SNP not supported\n");arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1095
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ return 0; + } + + if (platform_data) { + pr_debug("SNP platform data already initialized.\n"); + goto create_ctx; + } + + if (!secrets_pa) { + pr_err("SNP no secrets page\n");Maybe "SNP secrets page not found\n" ?
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1101
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+ return -ENODEV; + } + + pdata = kzalloc(sizeof(struct sev_guest_platform_data), GFP_KERNEL); + if (!pdata) { + pr_err("SNP alloc failed\n");Maybe "Allocation of SNP guest platform data failed\n" ?
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1107
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+ return -ENOMEM; + } + + pdata->layout = (__force void *)ioremap_encrypted(secrets_pa, PAGE_SIZE); + if (!pdata->layout) { + pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n");Maybe "Failed to map SNP secrets page\n" ? Not sure where the AP jump table came in on this...
Quoted 13 lines
arch/x86/kernel/sev.c +13
-0+ goto e_free_pdata; + } + + ret = -ENOMEM; + /* Allocate the shared page used for the request and response message. */ + pdata->request = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!pdata->request) + goto e_unmap; + + pdata->response = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!pdata->response) + goto e_free_request; +arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1130
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ /* initial the input address for guest request */ + pdata->input.req_gpa = __pa(pdata->request); + pdata->input.resp_gpa = __pa(pdata->response); + platform_data = pdata; + +create_ctx: + ret = -EIO; + snp_dev->ctx = snp_init_crypto(snp_dev->vmpck_id); + if (!snp_dev->ctx) { + pr_err("SNP init crypto failed\n");Maybe "SNP crypto context initialization failed\n" ?
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1135
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+ platform_data = NULL; + goto e_free_response; + } + + snp_dev->pdata = platform_data;Add a blank line here.
Quoted 101 lines
arch/x86/kernel/sev.c +101
-0+ return 0; + +e_free_response: + free_shared_pages(pdata->response, sizeof(struct snp_guest_msg)); +e_free_request: + free_shared_pages(pdata->request, sizeof(struct snp_guest_msg)); +e_unmap: + iounmap(pdata->layout); +e_free_pdata: + kfree(pdata); + + return ret; +} +EXPORT_SYMBOL_GPL (snp_setup_psp_messaging); + +static int __enc_payload (struct aesgcm_ctx *ctx,struct snp_guest_msg *msg, + void *plaintext, size_t len) +{ + struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; + + if (WARN_ON((hdr->msg_sz + ctx->authsize) > sizeof(msg->payload))) + return -EBADMSG; + + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + aesgcm_encrypt(ctx, msg->payload, plaintext, len, &hdr->algo, AAD_LEN, + iv, hdr->authtag); + return 0; +} + +static int dec_payload (struct aesgcm_ctx *ctx,struct snp_guest_msg *msg, + void *plaintext, size_t len) +{ + struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; + + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + if (aesgcm_decrypt(ctx, plaintext, msg->payload, len, &hdr->algo, + AAD_LEN, iv, hdr->authtag)) + return 0; + else + return -EBADMSG; +} + +static int verify_and_dec_payload (struct snp_guest_dev *snp_dev,struct snp_guest_req *guest_req, + struct sev_guest_platform_data *pdata) +{ + struct snp_guest_msg *resp = &snp_dev->secret_response; + struct snp_guest_msg *req = &snp_dev->secret_request; + struct snp_guest_msg_hdr *req_hdr = &req->hdr; + struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; + struct aesgcm_ctx *ctx = snp_dev->ctx; + + 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, pdata->response, sizeof(*resp)); + + /* Verify that the sequence counter is incremented by 1 */ + if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1))) + return -EBADMSG; + + /* Verify response message type and version number. */ + if (resp_hdr->msg_type != (req_hdr->msg_type + 1) || + resp_hdr->msg_version != req_hdr->msg_version) + return -EBADMSG; + + /* + * If the message size is greater than our buffer length then return + * an error. + */ + if (unlikely((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz)) + return -EBADMSG; + + return dec_payload(ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); +} + +static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno,struct snp_guest_req *req) +{ + struct snp_guest_msg *msg = &snp_dev->secret_request; + struct snp_guest_msg_hdr *hdr = &msg->hdr; + + memset(msg, 0, sizeof(*msg)); + + hdr->algo = SNP_AEAD_AES_256_GCM; + hdr->hdr_version = MSG_HDR_VER; + hdr->hdr_sz = sizeof(*hdr); + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; + hdr->msg_seqno = seqno; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz; + + /* Verify the sequence number is non-zero */ + if (!hdr->msg_seqno) + return -ENOSR; + + pr_debug("request [seqno %lld type %d version %d sz %d]\n", + hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
Quoted 8 lines
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1242
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+ + return __enc_payload(snp_dev->ctx, msg, req->req_buf, req->req_sz); +} + +static int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, + struct snp_guest_request_ioctl *rio);Could all of these routines been moved down closer to the bottom of the file to avoid this forward declaration?
Looks possible, I will try it out.
Quoted 28 lines
Quoted 11 lines
arch/x86/kernel/sev.c +10
-0+ +static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio, + struct sev_guest_platform_data *pdata) +{ + unsigned long req_start = jiffies; + unsigned int override_npages = 0; + u64 override_err = 0; + int rc; +...
Quoted 14 lines
-e_free_ctx:
-kfree(snp_dev->ctx);
e_free_cert_data:
free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE);arch/x86/kernel/sev.c +4
-6-e_free_response: - free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); -e_free_request: - free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); -e_unmap: - iounmap(mapping); + e_free_ctx: + kfree(snp_dev->ctx); +e_free_snpdev: + kfree(snp_dev);
Quoted 8 lines
Quoted 6 lines
return ret;
}
@@ -780,11 +332,9 @@ static int__exitsev_guest_remove(struct platform_device *pdev){
structsnp_guest_dev*snp_dev=platform_get_drvdata(pdev);
-free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE);Looks like this one should still be here, right?
Yes, this should still be there.
Quoted 3 lines
Thanks,
Tom
Regards
Nikunj
Re: [PATCH v5 07/14] x86/sev: Move and reorganize sev guest request api
Quoted 22 lines · Nikunj A. Dadhania
Quoted 10 lines · Tom Lendacky
Quoted 8 lines · 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 an API to the driver to assign key and send guest request.
Signed-off-by Nikunj A Dadhania <[email protected]>---
Quoted 10 lines
arch/x86/kernel/sev.c static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) · line 1046
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+static void snp_inc_msg_seqno (struct snp_guest_dev *snp_dev) +{ + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev->vmpck_id); + + if (!os_area_msg_seqno) { + pr_err("SNP unable to get message sequence counter\n"); + return; + }I probably missed this in the other patch or even when the driver was first created, but shouldn't we have a
lockdep_assert_held()here, too, before updating the count?
Quoted 3 lines
As per the current code flow,
snp_get_msg_seqno()is always called beforesnp_inc_msg_seqno(), maybe because of that the check wasnt there. It still makes sense to have alockdep_assert_held()insnp_inc_msg_seqno().Should I add this change as a separate fix ?
It can be sent as a separate patch (I don't think it is a fix) either before or after this series.
Thanks,
Tom
Quoted 2 lines
Quoted 2 lines
PATCH v5 8/14 x86/mm: Add generic guest initialization hook
In reply to Nikunj A DadhaniaAdd generic enc_init guest hook for performing any type of initialization that is vendor specific.
--- arch/x86/include/asm/x86_init.h | 2 ++ arch/x86/kernel/x86_init.c | 2 ++ arch/x86/mm/mem_encrypt.c | 3 +++ 3 files changed, 7 insertions(+)
arch/x86/include/asm/x86_init.h +2 -0
@@ -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
@@ -136,6 +136,7 @@ static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, boolstatic 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 +3 -0
@@ -84,5 +84,8 @@ void __init mem_encrypt_init(void)/* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes (); +if (x86_platform.guest.enc_init) + x86_platform.guest.enc_init (); +print_mem_encrypt_feature_info (); }
Signature · 1 line
2.34.1
Re: [PATCH v5 08/14] x86/mm: Add generic guest initialization hook
Quoted 25 lines · Nikunj A Dadhania
arch/x86/kernel/x86_init.c +2
-0@@ -136,6 +136,7 @@ static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, boolstatic 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 +0
-0
arch/x86/mm/mem_encrypt.c void __init mem_encrypt_init(void) · line 91
Quoted 8 lines
arch/x86/mm/mem_encrypt.c +3
-0@@ -84,5 +84,8 @@ void __init mem_encrypt_init(void)/* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes (); +if (x86_platform.guest.enc_init) + x86_platform.guest.enc_init (); +print_mem_encrypt_feature_info (); }
How does '.enc_init' ever get set to NULL? Isn't the point of having and using a 'noop' function so that you don't have to do NULL checks?
Re: [PATCH v5 08/14] x86/mm: Add generic guest initialization hook
Quoted 29 lines · Dave Hansen
Quoted 28 lines · Nikunj A Dadhania
arch/x86/kernel/x86_init.c +2
-0@@ -136,6 +136,7 @@ static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, boolstatic 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 +0
-0@@ -84,5 +84,8 @@ void __init mem_encrypt_init(void)/* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes ();
Quoted 8 lines
arch/x86/mm/mem_encrypt.c void __init mem_encrypt_init(void) · line 91
Quoted 5 lines
arch/x86/mm/mem_encrypt.c +3
-0+if (x86_platform.guest.enc_init) + x86_platform.guest.enc_init (); +print_mem_encrypt_feature_info (); }How does '.enc_init' ever get set to NULL? Isn't the point of having and using a 'noop' function so that you don't have to do NULL checks?
True, I will remove the check.
Regards
Nikunj
Re: [PATCH v5 08/14] x86/mm: Add generic guest initialization hook
In reply to Nikunj A DadhaniaQuoted 2 lines · Nikunj A Dadhania
Add generic
enc_initguest hook for performing any type of initialization that is vendor specific.
I think this commit message should be expanded on a bit... like when it is intended to be called, etc.
Thanks,
Tom
Quoted 60 lines
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/include/asm/x86_init.h | 2 ++ arch/x86/kernel/x86_init.c | 2 ++ arch/x86/mm/mem_encrypt.c | 3 +++ 3 files changed, 7 insertions(+)arch/x86/include/asm/x86_init.h +2
-0@@ -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@@ -136,6 +136,7 @@ static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, boolstatic 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 +3
-0@@ -84,5 +84,8 @@ void __init mem_encrypt_init(void)/* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes (); +if (x86_platform.guest.enc_init) + x86_platform.guest.enc_init (); +print_mem_encrypt_feature_info (); }
Re: [PATCH v5 08/14] x86/mm: Add generic guest initialization hook
Quoted 5 lines · Tom Lendacky
Quoted 2 lines · Nikunj A Dadhania
Add generic
enc_initguest hook for performing any type of initialization that is vendor specific.I think this commit message should be expanded on a bit... like when it is intended to be called, etc.
Sure
Regards
Nikunj
PATCH v5 9/14 x86/sev: Add Secure TSC support for SNP guests
In reply to Nikunj A DadhaniaAdd 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.
--- arch/x86/coco/core.c | 3 ++ arch/x86/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 ++++ 7 files changed, 123 insertions(+), 2 deletions(-)
arch/x86/coco/core.c +3 -0
@@ -89,6 +89,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)case CC_ATTR_GUEST_SEV_SNP :return sev_status &MSR_AMD64_SEV_SNP_ENABLED ; +case CC_ATTR_GUEST_SECURE_TSC : +return sev_status &MSR_AMD64_SNP_SECURE_TSC ; +default :return false; }
arch/x86/include/asm/sev-guest.h +18 -0
@@ -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 };
@@ -111,6 +113,22 @@ struct snp_guest_req {u8 msg_type; }; +struct snp_tsc_info_req { +#define SNP_TSC_INFO_REQ_SZ 128 +/* 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; +int snp_setup_psp_messaging (struct snp_guest_dev *snp_dev);int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req,struct snp_guest_request_ioctl *rio);
arch/x86/include/asm/sev.h +2 -0
@@ -200,6 +200,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 ) { }
@@ -223,6 +224,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
@@ -414,7 +414,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;
@@ -546,7 +548,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 +82 -0
100 lines not on this page · raw message
arch/x86/mm/mem_encrypt_amd.c +6 -0
@@ -215,6 +215,11 @@ void __init sme_map_bootdata(char *real_mode_data)__sme_early_map_unmap_mem (__va (cmdline_paddr),COMMAND_LINE_SIZE , true); } +void __init amd_enc_init (void ) +{ +snp_secure_tsc_prepare (); +} +void __init sev_setup_arch (void ) {phys_addr_t total_mem =memblock_phys_mem_size ();
@@ -502,6 +507,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
include/linux/cc_platform.h +8 -0
@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED , + +/** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature. +*/ + CC_ATTR_GUEST_SECURE_TSC, };#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
Signature · 1 line
2.34.1
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 155 lines · 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.
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/coco/core.c | 3 ++ arch/x86/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 ++++ 7 files changed, 123 insertions(+), 2 deletions(-)arch/x86/coco/core.c +3
-0@@ -89,6 +89,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)case CC_ATTR_GUEST_SEV_SNP :return sev_status &MSR_AMD64_SEV_SNP_ENABLED ; +case CC_ATTR_GUEST_SECURE_TSC : +return sev_status &MSR_AMD64_SNP_SECURE_TSC ; +default :return false; }arch/x86/include/asm/sev-guest.h +18
-0@@ -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 };@@ -111,6 +113,22 @@ struct snp_guest_req {u8 msg_type; }; +struct snp_tsc_info_req { +#define SNP_TSC_INFO_REQ_SZ 128 +/* 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; +int snp_setup_psp_messaging (struct snp_guest_dev *snp_dev);int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req,struct snp_guest_request_ioctl *rio);arch/x86/include/asm/sev.h +2
-0@@ -200,6 +200,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 ) { }@@ -223,6 +224,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@@ -414,7 +414,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;@@ -546,7 +548,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 +22
-0@@ -76,6 +76,10 @@ static u64 sev_hv_features __ro_after_init;/* Secrets page physical address from the CC blob */ static u64 secrets_pa__ro_after_init ; +/* Secure TSC values read using TSC_INFO SNP Guest request */ +static u64 guest_tsc_scale__ro_after_init ; +static u64 guest_tsc_offset__ro_after_init ; +/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -1393,6 +1397,78 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) }EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static struct snp_guest_dev tsc_snp_dev__initdata ; + +static int __init snp_get_tsc_info (void ) +{ +static u8 buf[SNP_TSC_INFO_REQ_SZ +AUTHTAG_LEN ]; +struct snp_guest_request_ioctl rio; +struct snp_tsc_info_resp tsc_resp; +struct snp_tsc_info_req tsc_req; +struct snp_guest_req req; +int rc, resp_len; + +/* +* The intermediate response buffer is used while decrypting the +* response payload. Make sure that it has enough space to cover the +* authtag. +*/ + resp_len =sizeof (tsc_resp) +AUTHTAG_LEN ; +if (sizeof (buf) < resp_len)
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1427
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+return -EINVAL ; + +memset (&tsc_req,0 ,sizeof (tsc_req)); +memset (&req,0 ,sizeof (req)); +memset (&rio,0 ,sizeof (rio)); +memset (buf,0 ,sizeof (buf)); + +if (!snp_assign_vmpck (&tsc_snp_dev,0 )) +return -EINVAL ; +
I don't see a requirement for VMPL0 in the API docs. I just see "When a guest creates its own VMSA, it must query the PSP for information with the TSC_INFO message to determine the correct values to write into GUEST_TSC_SCALE and GUEST_TSC_OFFSET". In that case, I don't see a particular use for this request in Linux. I would expect it either in the UEFI or in SVSM. Is this code path explicitly for direct boot to Linux? If so, did I miss that documentation in this patch series?
Quoted 106 lines
arch/x86/kernel/sev.c +50
-0+/* Initialize the PSP channel to send snp messages */ +if (snp_setup_psp_messaging (&tsc_snp_dev)) +sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED ); + + req.msg_version =MSG_HDR_VER ; + req.msg_type =SNP_MSG_TSC_INFO_REQ ; + req.vmpck_id = tsc_snp_dev.vmpck_id; + req.req_buf = &tsc_req; + req.req_sz =sizeof (tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + rc =snp_send_guest_request (&tsc_snp_dev, &req, &rio); +if (rc) + goto err_req; + +memcpy (&tsc_resp, buf,sizeof (tsc_resp)); +pr_debug ("%s: Valid response status %x scale %llx offset %llx factor %x\n" , + __func__, tsc_resp.status, tsc_resp.tsc_scale, tsc_resp.tsc_offset, + tsc_resp.tsc_factor); + + guest_tsc_scale = tsc_resp.tsc_scale; + guest_tsc_offset = tsc_resp.tsc_offset; + +err_req: +/* The response buffer contains the sensitive data, explicitly clear it. */ +memzero_explicit (buf,sizeof (buf)); +memzero_explicit (&tsc_resp,sizeof (tsc_resp)); +memzero_explicit (&req,sizeof (req)); + +return rc; +} + +void __init snp_secure_tsc_prepare (void ) +{ +if (!cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) +return ; + +if (snp_get_tsc_info ()) +sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED ); + +pr_debug ("SecureTSC enabled\n" ); +} +static int wakeup_cpu_via_vmgexit (int apic_id,unsigned long start_ip) {struct sev_es_save_area *cur_vmsa, *vmsa;@@ -1493,6 +1569,12 @@ static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) vmsa->vmpl =0 ; vmsa->sev_features = sev_status >>2 ; +/* Setting Secure TSC parameters */ +if (cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) { + vmsa->tsc_scale = guest_tsc_scale; + vmsa->tsc_offset = guest_tsc_offset; + } +/* Switch the page over to a VMSA page now that it is initialized */ ret =snp_set_vmsa (vmsa, true);if (ret) {arch/x86/mm/mem_encrypt_amd.c +6
-0@@ -215,6 +215,11 @@ void __init sme_map_bootdata(char *real_mode_data)__sme_early_map_unmap_mem (__va (cmdline_paddr),COMMAND_LINE_SIZE , true); } +void __init amd_enc_init (void ) +{ +snp_secure_tsc_prepare (); +} +void __init sev_setup_arch (void ) {phys_addr_t total_mem =memblock_phys_mem_size ();@@ -502,6 +507,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 include/linux/cc_platform.h +8
-1@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED , + +/** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature. +*/ + CC_ATTR_GUEST_SECURE_TSC, };#ifdef CONFIG_ARCH_HAS_CC_PLATFORM --2.34.1
Signature · 1 line
-Dionna Glaze, PhD (she/her)
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 32 lines · Dionna Amalie Glaze
Quoted 31 lines · Nikunj A Dadhania
arch/x86/kernel/sev.c +25
-0@@ -1393,6 +1397,78 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) }EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static struct snp_guest_dev tsc_snp_dev__initdata ; + +static int __init snp_get_tsc_info (void ) +{ +static u8 buf[SNP_TSC_INFO_REQ_SZ +AUTHTAG_LEN ]; +struct snp_guest_request_ioctl rio; +struct snp_tsc_info_resp tsc_resp; +struct snp_tsc_info_req tsc_req; +struct snp_guest_req req; +int rc, resp_len; + +/* +* The intermediate response buffer is used while decrypting the +* response payload. Make sure that it has enough space to cover the +* authtag. +*/ + resp_len =sizeof (tsc_resp) +AUTHTAG_LEN ; +if (sizeof (buf) < resp_len) +return -EINVAL ; + +memset (&tsc_req,0 ,sizeof (tsc_req)); +memset (&req,0 ,sizeof (req)); +memset (&rio,0 ,sizeof (rio)); +memset (buf,0 ,sizeof (buf)); +
Quoted 8 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1427
Quoted 3 lines
arch/x86/kernel/sev.c +3
-0+if (!snp_assign_vmpck (&tsc_snp_dev,0 )) +return -EINVAL ; +I don't see a requirement for VMPL0 in the API docs. I just see "When a guest creates its own VMSA, it must query the PSP for information with the
TSC_INFOmessage to determine the correct values to write intoGUEST_TSC_SCALEandGUEST_TSC_OFFSET".
The request should work irrespective of the VMPL level.
Quoted 4 lines
In that case, I don't see
a particular use for this request in Linux. I would expect it either in the UEFI or in SVSM. Is this code path explicitly for direct boot to Linux? If so, did I miss that documentation in this patch series?
This works with UEFI boot. I havent tried this with SVSM yet.
Thanks
Nikunj
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
In reply to Nikunj A DadhaniaQuoted 11 lines · 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.
Add to this commit message that you're using the enc_init hook to perform some Secure TSC initialization and why you have to do that.
Quoted 40 lines
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/coco/core.c | 3 ++ arch/x86/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 ++++ 7 files changed, 123 insertions(+), 2 deletions(-)arch/x86/coco/core.c +3
-0@@ -89,6 +89,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)case CC_ATTR_GUEST_SEV_SNP :return sev_status &MSR_AMD64_SEV_SNP_ENABLED ; +case CC_ATTR_GUEST_SECURE_TSC : +return sev_status &MSR_AMD64_SNP_SECURE_TSC ; +default :return false; }arch/x86/include/asm/sev-guest.h +2
-0@@ -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 };
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 117
Quoted 5 lines
arch/x86/include/asm/sev-guest.h +2
-0@@ -111,6 +113,22 @@ struct snp_guest_req {u8 msg_type; }; +struct snp_tsc_info_req { +#define SNP_TSC_INFO_REQ_SZ 128
Please move this to before the struct definition.
Quoted 66 lines
arch/x86/include/asm/sev-guest.h +14
-0+/* 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; +int snp_setup_psp_messaging (struct snp_guest_dev *snp_dev);int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req,struct snp_guest_request_ioctl *rio);arch/x86/include/asm/sev.h +2
-0@@ -200,6 +200,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 ) { }@@ -223,6 +224,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@@ -414,7 +414,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;@@ -546,7 +548,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 +0
-0
arch/x86/kernel/sev.c static u64 sev_hv_features __ro_after_init; · line 81
Quoted 6 lines
arch/x86/kernel/sev.c +3
-0@@ -76,6 +76,10 @@ static u64 sev_hv_features __ro_after_init;/* Secrets page physical address from the CC blob */ static u64 secrets_pa__ro_after_init ; +/* Secure TSC values read using TSC_INFO SNP Guest request */ +static u64 guest_tsc_scale__ro_after_init ; +static u64 guest_tsc_offset__ro_after_init ;
s/guest_/snp_/
Quoted 29 lines
arch/x86/kernel/sev.c +22
-0+/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -1393,6 +1397,78 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) }EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static struct snp_guest_dev tsc_snp_dev__initdata ; + +static int __init snp_get_tsc_info (void ) +{ +static u8 buf[SNP_TSC_INFO_REQ_SZ +AUTHTAG_LEN ]; +struct snp_guest_request_ioctl rio; +struct snp_tsc_info_resp tsc_resp; +struct snp_tsc_info_req tsc_req; +struct snp_guest_req req; +int rc, resp_len; + +/* +* The intermediate response buffer is used while decrypting the +* response payload. Make sure that it has enough space to cover the +* authtag. +*/ + resp_len =sizeof (tsc_resp) +AUTHTAG_LEN ; +if (sizeof (buf) < resp_len) +return -EINVAL ; + +memset (&tsc_req,0 ,sizeof (tsc_req));
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1430
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+memset (&req,0 ,sizeof (req)); +memset (&rio,0 ,sizeof (rio)); +memset (buf,0 ,sizeof (buf)); + +if (!snp_assign_vmpck (&tsc_snp_dev,0 )) +return -EINVAL ; + +/* Initialize the PSP channel to send snp messages */ +if (snp_setup_psp_messaging (&tsc_snp_dev)) +sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED );
This should just return the non-zero return code from snp_setup_psp_messaging(), no?
rc = snp_setup_psp_messaging(&tsc_snp_dev); if (rc) return rc;
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1440
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ + req.msg_version =MSG_HDR_VER ; + req.msg_type =SNP_MSG_TSC_INFO_REQ ; + req.vmpck_id = tsc_snp_dev.vmpck_id; + req.req_buf = &tsc_req; + req.req_sz =sizeof (tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + rc =snp_send_guest_request (&tsc_snp_dev, &req, &rio);
Aren't you supposed to hold a mutex before calling this since it will eventually call the message sequence number functions?
Quoted 17 lines
arch/x86/kernel/sev.c +17
-0+if (rc) + goto err_req; + +memcpy (&tsc_resp, buf,sizeof (tsc_resp)); +pr_debug ("%s: Valid response status %x scale %llx offset %llx factor %x\n" , + __func__, tsc_resp.status, tsc_resp.tsc_scale, tsc_resp.tsc_offset, + tsc_resp.tsc_factor); + + guest_tsc_scale = tsc_resp.tsc_scale; + guest_tsc_offset = tsc_resp.tsc_offset; + +err_req: +/* The response buffer contains the sensitive data, explicitly clear it. */ +memzero_explicit (buf,sizeof (buf)); +memzero_explicit (&tsc_resp,sizeof (tsc_resp)); +memzero_explicit (&req,sizeof (req)); +
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1467
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+return rc; +} + +void __init snp_secure_tsc_prepare (void ) +{ +if (!cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) +return ; + +if (snp_get_tsc_info ()) +sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED );
How about using SEV_TERM_SET_LINUX and a new GHCB_TERM_SECURE_TSC_INFO.
Quoted 49 lines
arch/x86/kernel/sev.c +10
-0+ +pr_debug ("SecureTSC enabled\n" ); +} +static int wakeup_cpu_via_vmgexit (int apic_id,unsigned long start_ip) {struct sev_es_save_area *cur_vmsa, *vmsa;@@ -1493,6 +1569,12 @@ static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) vmsa->vmpl =0 ; vmsa->sev_features = sev_status >>2 ; +/* Setting Secure TSC parameters */ +if (cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) { + vmsa->tsc_scale = guest_tsc_scale; + vmsa->tsc_offset = guest_tsc_offset; + } +/* Switch the page over to a VMSA page now that it is initialized */ ret =snp_set_vmsa (vmsa, true);if (ret) {arch/x86/mm/mem_encrypt_amd.c +6
-0@@ -215,6 +215,11 @@ void __init sme_map_bootdata(char *real_mode_data)__sme_early_map_unmap_mem (__va (cmdline_paddr),COMMAND_LINE_SIZE , true); } +void __init amd_enc_init (void ) +{ +snp_secure_tsc_prepare (); +} +void __init sev_setup_arch (void ) {phys_addr_t total_mem =memblock_phys_mem_size ();@@ -502,6 +507,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 include/linux/cc_platform.h +0
-0
include/linux/cc_platform.h enum cc_attr { · line 98
Quoted 9 lines
include/linux/cc_platform.h +6
-0@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED, + +/** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature.
I think TDX also has a secure TSC like feature, so can this be generic?
Thanks,
Tom
Quoted 5 lines
include/linux/cc_platform.h +2
-0+*/ + CC_ATTR_GUEST_SECURE_TSC, };#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 13 lines · Tom Lendacky
Quoted 11 lines · 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.
Quoted 1 line
Add to this commit message that you're using the
enc_inithook to perform some Secure TSC initialization and why you have to do that.
Sure, will add.
Quoted 159 lines
Quoted 38 lines
Signed-off-by Nikunj A Dadhania <[email protected]>---
arch/x86/coco/core.c | 3 ++arch/x86/include/asm/sev-guest.h| 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 ++++7 files changed, 123 insertions(+), 2 deletions(-)arch/x86/coco/core.c +0
-0@@ -89,6 +89,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)case
CC_ATTR_GUEST_SEV_SNP:
returnsev_status&MSR_AMD64_SEV_SNP_ENABLED; + caseCC_ATTR_GUEST_SECURE_TSC:
+ returnsev_status&MSR_AMD64_SNP_SECURE_TSC; +
default:
return false;
}arch/x86/include/asm/sev-guest.h +2
-0@@ -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
};arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 116
Quoted 4 lines
arch/x86/include/asm/sev-guest.h +1
-0@@ -111,6 +113,22 @@ struct snp_guest_req { u8 msg_type; }; +struct snp_tsc_info_req { +#define SNP_TSC_INFO_REQ_SZ 128Please move this to before the struct definition.
Quoted 68 lines
arch/x86/include/asm/sev-guest.h +14
-0+ /* 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; + int snp_setup_psp_messaging(struct snp_guest_dev *snp_dev); int snp_send_guest_request(struct snp_guest_dev *dev, struct snp_guest_req *req, struct snp_guest_request_ioctl *rio);arch/x86/include/asm/sev.h +2
-0@@ -200,6 +200,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) { }@@ -223,6 +224,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 #endifarch/x86/include/asm/svm.h +4
-2@@ -414,7 +414,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;@@ -546,7 +548,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 +0
-0@@ -76,6 +76,10 @@ static u64 sev_hv_features __ro_after_init; /* Secrets page physical address from the CC blob */ static u64 secrets_pa __ro_after_init;+/* Secure TSC values read using
TSC_INFOSNP Guest request */arch/x86/kernel/sev.c static u64 sev_hv_features __ro_after_init; · line 81
Quoted 2 lines
arch/x86/kernel/sev.c +2
-0+static u64 guest_tsc_scale__ro_after_init ; +static u64 guest_tsc_offset__ro_after_init ;s/guest_/snp_/
Quoted 28 lines
+
/* #VC handler runtime per-CPU data */
structsev_es_runtime_data{
struct ghcbghcb_page;arch/x86/kernel/sev.c +20
-0@@ -1393,6 +1397,78 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) } EXPORT_SYMBOL_GPL(snp_assign_vmpck); +static struct snp_guest_dev tsc_snp_dev __initdata; + +static int __init snp_get_tsc_info (void ) +{ + static u8 buf[SNP_TSC_INFO_REQ_SZ + AUTHTAG_LEN]; + struct snp_guest_request_ioctl rio; + struct snp_tsc_info_resp tsc_resp; + struct snp_tsc_info_req tsc_req; + struct snp_guest_req req; + int rc, resp_len; + + /* + * The intermediate response buffer is used while decrypting the + * response payload. Make sure that it has enough space to cover the + * authtag. + */ + resp_len = sizeof(tsc_resp) + AUTHTAG_LEN; + if (sizeof(buf) < resp_len) + return -EINVAL; + + memset(&tsc_req, 0, sizeof(tsc_req));arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1429
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ memset(&req, 0, sizeof(req)); + memset(&rio, 0, sizeof(rio)); + memset(buf, 0, sizeof(buf)); + + if (!snp_assign_vmpck(&tsc_snp_dev, 0)) + return -EINVAL; + + /* Initialize the PSP channel to send snp messages */ + if (snp_setup_psp_messaging(&tsc_snp_dev)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);This should just return the non-zero return code from
snp_setup_psp_messaging(), no?
Quoted 3 lines
rc =
snp_setup_psp_messaging(&tsc_snp_dev);
if (rc)
return rc;
Yes, that will also have the same behaviour, snp_get_tsc_info() will send the termination request.
Quoted 11 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1440
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ + req.msg_version = MSG_HDR_VER; + req.msg_type = SNP_MSG_TSC_INFO_REQ; + req.vmpck_id = tsc_snp_dev.vmpck_id; + req.req_buf = &tsc_req; + req.req_sz = sizeof(tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.exit_code = SVM_VMGEXIT_GUEST_REQUEST; + rc = snp_send_guest_request(&tsc_snp_dev, &req, &rio);
Quoted 1 line
Aren't you supposed to hold a mutex before calling this since it will eventually call the message sequence number functions?
Yes, I will need to otherwise lockdep will complain. This is being called from boot processor, so there is no parallel execution.
Quoted 21 lines
Quoted 21 lines
arch/x86/kernel/sev.c +21
-0+ if (rc) + goto err_req; + + memcpy(&tsc_resp, buf, sizeof(tsc_resp)); + pr_debug("%s: Valid response status %x scale %llx offset %llx factor %x\n", + __func__, tsc_resp.status, tsc_resp.tsc_scale, tsc_resp.tsc_offset, + tsc_resp.tsc_factor); + + guest_tsc_scale = tsc_resp.tsc_scale; + guest_tsc_offset = tsc_resp.tsc_offset; + +err_req: + /* The response buffer contains the sensitive data, explicitly clear it. */ + memzero_explicit(buf, sizeof(buf)); + memzero_explicit(&tsc_resp, sizeof(tsc_resp)); + memzero_explicit(&req, sizeof(req)); + + return rc; +} + +void __init snp_secure_tsc_prepare (void )
Quoted 8 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1467
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+{ + if (!cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) + return; + + if (snp_get_tsc_info()) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);How about using
SEV_TERM_SET_LINUXand a newGHCB_TERM_SECURE_TSC_INFO.
Yes, we can do that, I remember you had said this will required GHCB spec change and then thought of sticking with the current return code.
Quoted 34 lines
Quoted 34 lines
arch/x86/kernel/sev.c +4
-0+ + pr_debug("SecureTSC enabled\n"); +} + static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) { struct sev_es_save_area *cur_vmsa, *vmsa;@@ -1493,6 +1569,12 @@ static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) vmsa->vmpl = 0; vmsa->sev_features = sev_status >> 2;+ /* Setting Secure TSC parameters */
+ if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) {
+vmsa->tsc_scale=guest_tsc_scale;
+vmsa->tsc_offset=guest_tsc_offset;
+ }
+
/* Switch the page over to a VMSA page now that it is initialized */ ret =snp_set_vmsa(vmsa, true);
if (ret) {arch/x86/mm/mem_encrypt_amd.c +4
-0@@ -215,6 +215,11 @@ void __init sme_map_bootdata(char *real_mode_data) __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true); } +void __init amd_enc_init(void) +{ + snp_secure_tsc_prepare(); +} + void __init sev_setup_arch(void) {
Quoted 8 lines
arch/x86/mm/mem_encrypt_amd.c void __init sme_early_init(void) · line 512
Quoted 8 lines
arch/x86/mm/mem_encrypt_amd.c +1
-0phys_addr_t total_mem = memblock_phys_mem_size();@@ -502,6 +507,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
Regards
Nikunj
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 41 lines · Nikunj A. Dadhania
Quoted 13 lines · Tom Lendacky
Quoted 11 lines · 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.
Quoted 1 line
Add to this commit message that you're using the
enc_inithook to perform some Secure TSC initialization and why you have to do that.Sure, will add.
Quoted 13 lines
Quoted 12 lines
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/coco/core.c | 3 ++ arch/x86/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 ++++ 7 files changed, 123 insertions(+), 2 deletions(-)Quoted 9 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) · line 1467
Quoted 7 lines
arch/x86/kernel/sev.c +7
-0+void __init snp_secure_tsc_prepare (void ) +{ + if (!cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) + return; + + if (snp_get_tsc_info()) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);How about using
SEV_TERM_SET_LINUXand a newGHCB_TERM_SECURE_TSC_INFO.
Quoted 1 line
Yes, we can do that, I remember you had said this will required GHCB spec change and then thought of sticking with the current return code.
No spec change needed. The base SNP support is already using it, so not an issue to add a new error code.
Thanks,
Tom
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
In reply to Tom LendackyQuoted 8 lines · Tom Lendacky
Quoted 8 lines
include/linux/cc_platform.h +0
-0@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED,
Quoted 8 lines
include/linux/cc_platform.h enum cc_attr { · line 98
Quoted 6 lines
include/linux/cc_platform.h +6
-0+ + /** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature. I think TDX also has a secure TSC like feature, so can this be generic?
Yes, we can do that. In SNP case SecureTSC is an optional feature, not sure if that is the case for TDX as well.
Kirill any inputs ?
Quoted 8 lines
Thanks,
TomQuoted 4 lines
include/linux/cc_platform.h +2
-0+*/ + CC_ATTR_GUEST_SECURE_TSC, }; #ifdef CONFIG_ARCH_HAS_CC_PLATFORM
Regards
Nikunj
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 18 lines · Nikunj A. Dadhania
Quoted 8 lines · Tom Lendacky
Quoted 8 lines
include/linux/cc_platform.h +0
-0@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED,Quoted 8 lines
include/linux/cc_platform.h enum cc_attr { · line 98
Quoted 6 lines
include/linux/cc_platform.h +6
-0+ + /** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature. I think TDX also has a secure TSC like feature, so can this be generic?
Quoted 3 lines
Yes, we can do that. In SNP case SecureTSC is an optional feature, not sure if that is the case for TDX as well.
Kirill any inputs ?
We have several X86_FEATURE_ flags to indicate quality of TSC. Do we really need a CC_ATTR on top of that? Maybe SEV code could just set X86_FEATURE_ according to what its TSC can do?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 19 lines · Kirill A. Shutemov
Quoted 18 lines · Nikunj A. Dadhania
Quoted 16 lines · Tom Lendacky
Quoted 5 lines
include/linux/cc_platform.h +0
-0include/linux/cc_platform.h enum cc_attr { · line 98
Quoted 9 lines
include/linux/cc_platform.h +6
-0@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED, + + /** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature. I think TDX also has a secure TSC like feature, so can this be generic?
Quoted 7 lines
Quoted 3 lines
Yes, we can do that. In SNP case SecureTSC is an optional feature, not sure if that is the case for TDX as well.
Kirill any inputs ?
We have several
X86_FEATURE_flags to indicate quality of TSC. Do we really need aCC_ATTRon top of that? Maybe SEV code could just setX86_FEATURE_according to what its TSC can do?
For SEV-SNP, SEV_STATUS MSR has the information of various features that have been enabled by the hypervisor. We will need a CC_ATTR for these optional features.
Regards
Nikunj
Re: [PATCH v5 09/14] x86/sev: Add Secure TSC support for SNP guests
Quoted 24 lines · Nikunj A. Dadhania
Quoted 23 lines · Kirill A. Shutemov
Quoted 21 lines · Nikunj A. Dadhania
Quoted 8 lines · Tom Lendacky
Quoted 8 lines
include/linux/cc_platform.h +0
-0@@ -90,6 +90,14 @@ enum cc_attr {* Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED,Quoted 8 lines
include/linux/cc_platform.h enum cc_attr { · line 98
Quoted 6 lines
include/linux/cc_platform.h +6
-0+ + /** +* @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. +* +* The platform/OS is running as a guest/virtual machine and actively +* using AMD SEV-SNP Secure TSC feature. I think TDX also has a secure TSC like feature, so can this be generic?
Yes, we can do that. In SNP case SecureTSC is an optional feature, not sure if that is the case for TDX as well.
Kirill any inputs ?
Quoted 7 lines
Quoted 3 lines
We have several
X86_FEATURE_flags to indicate quality of TSC. Do we really need aCC_ATTRon top of that? Maybe SEV code could just setX86_FEATURE_according to what its TSC can do?For SEV-SNP,
SEV_STATUSMSR has the information of various features that have been enabled by the hypervisor. We will need aCC_ATTRfor these optional features.
If all users of the attribute is withing x86, I would rather add synthetic X86_FEATURE_ flags than CC_ATTR_. We have better instrumentation around features.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v5 10/14 x86/sev: Change TSC MSR behavior for Secure TSC enabled guests
In reply to Nikunj A DadhaniaSecure 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
@@ -1711,6 +1711,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 &&cc_platform_has (CC_ATTR_GUEST_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
PATCH v5 11/14 x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests
In reply to Nikunj A DadhaniaThe 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 | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kernel/sev-shared.c +7 -0
@@ -991,6 +991,13 @@ 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. +*/ +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
Re: [PATCH v5 11/14] x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests
Quoted 15 lines · 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.
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/kernel/sev-shared.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kernel/sev-shared.c +0
-0
arch/x86/kernel/sev-shared.c static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, · line 998
Quoted 8 lines
arch/x86/kernel/sev-shared.c +5
-0@@ -991,6 +991,13 @@ 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. +*/ +if (sev_status &MSR_AMD64_SNP_SECURE_TSC )
If you have to use sev_status, then please document why cc_platform_has() can't be used in the comment above.
Thanks,
Tom
Quoted 5 lines
arch/x86/kernel/sev-shared.c +2
-0+return ES_VMM_ERROR ; + ret =sev_es_ghcb_hv_call (ghcb, ctxt, exit_code,0 ,0 );if (ret !=ES_OK )return ret;
Re: [PATCH v5 11/14] x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests
Quoted 11 lines · Tom Lendacky
Quoted 10 lines · 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.
Signed-off-by Nikunj A Dadhania <[email protected]>---
arch/x86/kernel/sev-shared.c| 7 +++++++1 file changed, 7 insertions(+)
Quoted 14 lines
Quoted 7 lines
arch/x86/kernel/sev-shared.c +0
-0@@ -991,6 +991,13 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, bool rdtscp = (exit_code == SVM_EXIT_RDTSCP); enum es_result ret;Quoted 5 lines
+ /*
+ * RDTSC and RDTSCP should not be intercepted when Secure TSC is + * enabled. Terminate the SNP guest when the interception is enabled. + */
+ if (sev_status&MSR_AMD64_SNP_SECURE_TSC)If you have to use
sev_status, then please document whycc_platform_has()can't be used in the comment above.
Right, for sev-shared.c, cc_platform_has() is not available when compiling boot/compressed.
Regards
Nikunj
PATCH v5 12/14 x86/kvmclock: Skip kvmclock when Secure TSC is available
In reply to Nikunj A DadhaniaFor 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
@@ -288,7 +288,7 @@ void __init kvmclock_init(void) {u8 flags; -if (!kvm_para_available () || !kvmclock) +if (!kvm_para_available () || !kvmclock ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC ))return ;if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 )) {
Signature · 1 line
2.34.1
Re: [PATCH v5 12/14] x86/kvmclock: Skip kvmclock when Secure TSC is available
Quoted 13 lines · 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.
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/kernel/kvmclock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/kvmclock.c +0
-0
arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 291
Quoted 5 lines
arch/x86/kernel/kvmclock.c +1
-1@@ -288,7 +288,7 @@ void __init kvmclock_init(void) {u8 flags; -if (!kvm_para_available () || !kvmclock) +if (!kvm_para_available () || !kvmclock ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC ))
And is setting X86_FEATURE_TSC_RELIABLE, as Dave Hansen suggests, enough to prevent usage of kvmclock?
There was a discussion here:
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Tom
Quoted 3 lines
arch/x86/kernel/kvmclock.c +0
-0return ;if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 )) {
Re: [PATCH v5 12/14] x86/kvmclock: Skip kvmclock when Secure TSC is available
Quoted 9 lines · Tom Lendacky
Quoted 8 lines · 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.
Signed-off-by Nikunj A Dadhania <[email protected]>---
arch/x86/kernel/kvmclock.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 291
Quoted 9 lines
arch/x86/kernel/kvmclock.c +1
-0@@ -288,7 +288,7 @@ void __init kvmclock_init(void) { u8 flags; - if (!kvm_para_available() || !kvmclock) + if (!kvm_para_available() || !kvmclock || cc_platform_has(CC_ATTR_GUEST_SECURE_TSC))And is setting
X86_FEATURE_TSC_RELIABLE, as Dave Hansen suggests, enough to prevent usage of kvmclock?
No that wasn't sufficient. kvmclock was always selected before SecureTSC even when X86_FEATURE_TSC_RELIABLE was selected.
Quoted 6 lines
There was a discussion here:
https://lore.kernel.org/lkml/[email protected]/Thanks,
Tom
Regards
Nikunj
PATCH v5 13/14 x86/tsc: Mark Secure TSC as reliable clocksource
In reply to Nikunj A DadhaniaAMD SNP guests may have Secure TSC feature enabled. Secure TSC as clocksource is wrongly marked as unstable, mark Secure TSC as reliable.
--- arch/x86/kernel/tsc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/tsc.c +1 -1
@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;/*
Signature · 1 line
2.34.1
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...
arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;
Why can't you just set X86_FEATURE_TSC_RELIABLE?
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 15 lines · Dave Hansen
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;Why can't you just set
X86_FEATURE_TSC_RELIABLE?
Last time when I tried, I had removed my kvmclock changes and I had set the X86_FEATURE_TSC_RELIABLE similar to Kirill's patch[1], this did not select the SecureTSC.
Let me try setting X86_FEATURE_TSC_RELIABLE and retaining my patch for skipping kvmclock.
Regards
Nikunj
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 17 lines · Nikunj A. Dadhania
Quoted 15 lines · Dave Hansen
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;Why can't you just set
X86_FEATURE_TSC_RELIABLE?
Quoted 6 lines
Last time when I tried, I had removed my kvmclock changes and I had set the
X86_FEATURE_TSC_RELIABLEsimilar to Kirill's patch[1], this did not select the SecureTSC.Let me try setting
X86_FEATURE_TSC_RELIABLEand retaining my patch for skipping kvmclock.
kvmclock lowers its rating if TSC is good enough:
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && boot_cpu_has(X86_FEATURE_NONSTOP_TSC) && !check_tsc_unstable()) kvm_clock.rating = 299;
Does your TSC meet the requirements?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 25 lines · Kirill A. Shutemov
Quoted 17 lines · Nikunj A. Dadhania
Quoted 15 lines · Dave Hansen
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;Why can't you just set
X86_FEATURE_TSC_RELIABLE?Quoted 6 lines
Last time when I tried, I had removed my kvmclock changes and I had set the
X86_FEATURE_TSC_RELIABLEsimilar to Kirill's patch[1], this did not select the SecureTSC.Let me try setting
X86_FEATURE_TSC_RELIABLEand retaining my patch for skipping kvmclock.
Quoted 8 lines
kvmclock lowers its rating if TSC is good enough:
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && boot_cpu_has(X86_FEATURE_NONSTOP_TSC) && !check_tsc_unstable()) kvm_clock.rating = 299;Does your TSC meet the requirements?
I have set TscInvariant (bit 8) in CPUID_8000_0007_edx and TSC is set as reliable.
With this I see kvm_clock rating being lowered, but kvm-clock is still being picked as clock-source.
[ 0.000834] kvmclock_init: lowering kvm_clock rating [ 0.002623] clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns [ 2.295082] clocksource: Switched to clocksource kvm-clock
Regards
Nikunj
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 33 lines · Nikunj A. Dadhania
Quoted 32 lines · Kirill A. Shutemov
Quoted 17 lines · Nikunj A. Dadhania
Quoted 15 lines · Dave Hansen
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;Why can't you just set
X86_FEATURE_TSC_RELIABLE?Quoted 6 lines
Last time when I tried, I had removed my kvmclock changes and I had set the
X86_FEATURE_TSC_RELIABLEsimilar to Kirill's patch[1], this did not select the SecureTSC.Let me try setting
X86_FEATURE_TSC_RELIABLEand retaining my patch for skipping kvmclock.kvmclock lowers its rating if TSC is good enough:
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && boot_cpu_has(X86_FEATURE_NONSTOP_TSC) && !check_tsc_unstable()) kvm_clock.rating = 299;
Quoted 5 lines
Quoted 1 line
Does your TSC meet the requirements?
I have set TscInvariant (bit 8) in
CPUID_8000_0007_edxand TSC is set as reliable.With this I see
kvm_clockrating being lowered, but kvm-clock is still being picked as clock-source.
Ah.. at later point TSC is picked up, is this expected ?
[ 2.564052] clocksource: Switched to clocksource kvm-clock [ 2.678136] clocksource: Switched to clocksource tsc
Regards
Nikunj
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 36 lines · Nikunj A. Dadhania
Quoted 35 lines · Nikunj A. Dadhania
Quoted 33 lines · Kirill A. Shutemov
Quoted 17 lines · Nikunj A. Dadhania
Quoted 15 lines · Dave Hansen
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;Why can't you just set
X86_FEATURE_TSC_RELIABLE?Quoted 6 lines
Last time when I tried, I had removed my kvmclock changes and I had set the
X86_FEATURE_TSC_RELIABLEsimilar to Kirill's patch[1], this did not select the SecureTSC.Let me try setting
X86_FEATURE_TSC_RELIABLEand retaining my patch for skipping kvmclock.kvmclock lowers its rating if TSC is good enough:
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && boot_cpu_has(X86_FEATURE_NONSTOP_TSC) && !check_tsc_unstable()) kvm_clock.rating = 299;Does your TSC meet the requirements?
Quoted 8 lines
Quoted 3 lines
I have set TscInvariant (bit 8) in
CPUID_8000_0007_edxand TSC is set as reliable.With this I see
kvm_clockrating being lowered, but kvm-clock is still being picked as clock-source.Ah.. at later point TSC is picked up, is this expected ?
[ 2.564052] clocksource: Switched to clocksource kvm-clock [ 2.678136] clocksource: Switched to clocksource tsc
On bare metal I see switch from tsc-early to tsc. tsc-early rating is equal to kvmclock rating after it gets lowered.
Maybe kvmclock rating has to be even lower after detecting sane TSC?
Sean, Paolo, any comments?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 41 lines · Kirill A. Shutemov
Quoted 40 lines · Nikunj A. Dadhania
Quoted 38 lines · Nikunj A. Dadhania
Quoted 25 lines · Kirill A. Shutemov
Quoted 17 lines · Nikunj A. Dadhania
Quoted 15 lines · Dave Hansen
On 10/29/23 23:36, Nikunj A Dadhania wrote:
...arch/x86/kernel/tsc.c static void __init check_system_tsc_reliable(void) · line 1245
Quoted 11 lines
arch/x86/kernel/tsc.c +1
-1@@ -1241,7 +1241,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; }#endif -if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE )) +if (boot_cpu_has (X86_FEATURE_TSC_RELIABLE ) ||cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) tsc_clocksource_reliable =1 ;Why can't you just set
X86_FEATURE_TSC_RELIABLE?Quoted 6 lines
Last time when I tried, I had removed my kvmclock changes and I had set the
X86_FEATURE_TSC_RELIABLEsimilar to Kirill's patch[1], this did not select the SecureTSC.Let me try setting
X86_FEATURE_TSC_RELIABLEand retaining my patch for skipping kvmclock.Quoted 8 lines
kvmclock lowers its rating if TSC is good enough:
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && boot_cpu_has(X86_FEATURE_NONSTOP_TSC) && !check_tsc_unstable()) kvm_clock.rating = 299;Does your TSC meet the requirements?
I have set TscInvariant (bit 8) in
CPUID_8000_0007_edxand TSC is set as reliable.With this I see
kvm_clockrating being lowered, but kvm-clock is still being picked as clock-source.
Quoted 7 lines
Quoted 4 lines
Ah.. at later point TSC is picked up, is this expected ?
[ 2.564052] clocksource: Switched to clocksource kvm-clock [ 2.678136] clocksource: Switched to clocksource tscOn bare metal I see switch from tsc-early to tsc. tsc-early rating is equal to kvmclock rating after it gets lowered.
For SNP guest with secure tsc enabled, kvm-clock and tsc-early both are at 299. Initially, kvm-clock is selected as clocksource and when tsc with 300 rating is enqueued, clocksource then switches to tsc.
[ 0.004231] clocksource: clocksource_enqueue: name kvm-clock rating 299 [...] [ 2.046319] clocksource: clocksource_enqueue: name tsc-early rating 299 [...] [ 3.399179] clocksource: Switched to clocksource kvm-clock [...] [ 3.513652] clocksource: clocksource_enqueue: name tsc rating 300 [ 3.517314] clocksource: Switched to clocksource tsc
Quoted 1 line
Maybe kvmclock rating has to be even lower after detecting sane TSC?
If I set kvmclock rating to 298, I do see exact behavior as you have seen on the bare-metal.
[ 0.004520] clocksource: clocksource_enqueue: name kvm-clock rating 298 [...] [ 1.827422] clocksource: clocksource_enqueue: name tsc-early rating 299 [...] [ 3.485059] clocksource: Switched to clocksource tsc-early [...] [ 3.623625] clocksource: clocksource_enqueue: name tsc rating 300 [ 3.628954] clocksource: Switched to clocksource tsc
Regards
Nikunj
Re: [PATCH v5 13/14] x86/tsc: Mark Secure TSC as reliable clocksource
Quoted 4 lines · Nikunj A. Dadhania
Quoted 1 line
Maybe kvmclock rating has to be even lower after detecting sane TSC?
If I set kvmclock rating to 298, I do see exact behavior as you have seen on the bare-metal.
Quoted 8 lines
[ 0.004520] clocksource: clocksource_enqueue: name kvm-clock rating 298 [...] [ 1.827422] clocksource: clocksource_enqueue: name tsc-early rating 299 [...] [ 3.485059] clocksource: Switched to clocksource tsc-early [...] [ 3.623625] clocksource: clocksource_enqueue: name tsc rating 300 [ 3.628954] clocksource: Switched to clocksource tsc
This looks more reasonable to me. But I don't really understand timekeeping. It would be nice to hear from someone who knows what he saying.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v5 14/14 x86/sev: Enable Secure TSC for SNP guests
In reply to Nikunj A DadhaniaNow that all the required plumbing is done for enabling SNP Secure TSC feature, add Secure TSC to snp features present list.
The CC_ATTR_GUEST_SECURE_TSC can be used by the guest to query whether the SNP guest has Secure TSC feature active.
--- arch/x86/boot/compressed/sev.c | 3 ++- arch/x86/mm/mem_encrypt.c | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev.c +2 -1
@@ -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
@@ -69,8 +69,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 (cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) +pr_cont (" SECURE-TSC" ); + }pr_cont ("\n" ); }
Signature · 1 line
2.34.1