Add Secure TSC support for SNP guests
Subsystems (4)
v2 cover Add Secure TSC support for SNP 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. 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 Security processor (PSP). This communication channel is encrypted between the security processor and the guest, hypervisor is just the conduit to deliver the guest messages to the 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 minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP which is available at earlyboot.
SEV-guest driver has the implementation for guest and security coprocessor 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 as part to kernel/sev.c and provide well defined APIs to the sev-guest driver to use the interface to avoid code-duplication.
Patches:
01-05: Preparation and movement of sev-guest driver code
06: Adds generic guest initialization hook
07-11: SecureTSC enablement patches.
Testing SecureTSC
-----------------
SecureTSC hypervisor patches based on top of SEV-SNP UPM series:
https://github.com/nikunjad/linux/tree/upmv10-snpv8_securetsc
QEMU changes:
https://github.com/nikunjad/qemu/tree/upmv10b-snpv3-wip-securetsc
QEMU commandline SEV-SNP-UPM with SecureTSC:
qemu-system-x86_64 -cpu EPYC-Milan-v2 \
-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=protected \
...Changelog:
----------
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 (11):
virt: sev-guest: Use AES GCM crypto library
virt: sev-guest: Move mutex to SNP guest device structure
virt: sev-guest: Add snp_guest_req structure
virt: sev-guest: Add simplified helper to assign vmpck x86/sev: Move and reorganize sev guest request api x86/mm: Add generic guest initialization hook x86/sev: Change TSC MSR behavior for Secure TSC enabled guests x86/sev: Add Secure TSC support for SNP guests x86/kvmclock: Use Secure TSC as clock if 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 | 2 +-
arch/x86/coco/core.c | 3 +
.../x86/include/asm}/sev-guest.h | 46 ++
arch/x86/include/asm/sev.h | 24 +
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 | 541 +++++++++++++++-
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 | 2 -
drivers/virt/coco/sev-guest/sev-guest.c | 592 ++----------------
include/linux/cc_platform.h | 8 +
17 files changed, 708 insertions(+), 551 deletions(-)
rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (54%)base-commit: e8d018dd0257f744ca50a729e3d042cf2ec9da65
Signature · 1 line
2.34.1
PATCH v2 1/11 virt: sev-guest: Use AES GCM crypto library
SEV-SNP guests with SecureTSC enabled need to send a TSC_INFO SNP Guest message to the AMD security processor before the smpboot phase starts. Details from the TSC_INFO response have to be programmed in the VMSA before the secondary CPUs are brought up.
Start using the AES GCM library implementation as the crypto API is not available yet this early.
--- drivers/virt/coco/sev-guest/Kconfig | 3 +- drivers/virt/coco/sev-guest/sev-guest.c | 172 +++++++----------------- drivers/virt/coco/sev-guest/sev-guest.h | 3 + 3 files changed, 53 insertions(+), 125 deletions(-)
drivers/virt/coco/sev-guest/Kconfig +1 -2
@@ -2,8 +2,7 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - 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 +49 -123
305 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 v2 01/11] virt: sev-guest: Use AES GCM crypto library
Quoted 7 lines · Nikunj A Dadhania
SEV-SNP guests with SecureTSC enabled need to send a
TSC_INFOSNP Guest message to the AMD security processor before the smpboot phase starts. Details from theTSC_INFOresponse have to be programmed in the VMSA before the secondary CPUs are brought up.Start using the AES GCM library implementation as the crypto API is not available yet this early.
This isn't quite true, yet. You should add that the encryption code will be moved out of sev-guest to support Secure TSC, but to make the diffs easier to review, convert the Crypto API usage over to AES GCM library usage before moving it.
Thanks,
Tom
Quoted 362 lines
CC Ard Biesheuvel <[email protected]>Signed-off-by Nikunj A Dadhania <[email protected]>--- drivers/virt/coco/sev-guest/Kconfig | 3 +- drivers/virt/coco/sev-guest/sev-guest.c | 172 +++++++----------------- drivers/virt/coco/sev-guest/sev-guest.h | 3 + 3 files changed, 53 insertions(+), 125 deletions(-)drivers/virt/coco/sev-guest/Kconfig +1
-2@@ -2,8 +2,7 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - 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 +49
-123@@ -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;struct snp_guest_msg *request, *response;struct snp_secrets_page_layout *layout;struct snp_req_data input;@@ -60,6 +51,15 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP./* Mutex to serialize the shared buffer access and command handling. */ static DEFINE_MUTEX (snp_cmd_mutex); +static inline unsigned int get_ctx_authsize (struct snp_guest_dev *snp_dev) +{ +if (snp_dev && snp_dev->ctx) +return snp_dev->ctx->authsize; + +WARN_ONCE (1 ,"Unable to get crypto authsize\n" ); +return 0 ; +} +static bool is_vmpck_empty (struct snp_guest_dev *snp_dev) {char zero_key[VMPCK_KEY_LEN ] = {0 };@@ -144,132 +144,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 ; +return ctx; } -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; -} - -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); -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 ; } -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 */ -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 ; }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->response;struct snp_guest_msg *req = snp_dev->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);@@ -287,11 +214,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,@@ -318,7 +245,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,__u64 *fw_err)@@ -446,7 +373,6 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, instatic 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;@@ -464,7 +390,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) +get_ctx_authsize (snp_dev); resp =kzalloc (resp_len,GFP_KERNEL_ACCOUNT );if (!resp)return -ENOMEM ;@@ -485,7 +411,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;@@ -502,7 +427,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) +get_ctx_authsize (snp_dev);if (sizeof (buf) < resp_len)return -ENOMEM ;@@ -527,7 +452,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;@@ -565,7 +489,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) +get_ctx_authsize (snp_dev); resp =kzalloc (resp_len,GFP_KERNEL_ACCOUNT );if (!resp)return -ENOMEM ;@@ -777,8 +701,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;@@ -793,11 +717,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:@@ -816,7 +742,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 v2 01/11] virt: sev-guest: Use AES GCM crypto library
Quoted 11 lines · Tom Lendacky
Quoted 7 lines · Nikunj A Dadhania
SEV-SNP guests with SecureTSC enabled need to send a
TSC_INFOSNP Guest message to the AMD security processor before the smpboot phase starts. Details from theTSC_INFOresponse have to be programmed in the VMSA before the secondary CPUs are brought up.Start using the AES GCM library implementation as the crypto API is not available yet this early.
This isn't quite true, yet. You should add that the encryption code will
be moved out of sev-guest to support Secure TSC, but to make the diffs> easier to review, convert the Crypto API usage over to AES GCM library > usage before moving it.
Yes, will change it accordingly.
Regards
Nikunj
PATCH v2 2/11 virt: sev-guest: Move mutex to SNP guest device structure
In reply to Nikunj A DadhaniaIn preparation to provide new API for sending SNP guest message to the sev-guest driver, move the SNP command mutex to the snp_guest_dev structure.
--- drivers/virt/coco/sev-guest/sev-guest.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +11 -10
@@ -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;struct snp_guest_msg *request, *response;
@@ -48,9 +51,6 @@ static u32 vmpck_id;module_param (vmpck_id, uint,0444 );MODULE_PARM_DESC (vmpck_id,"The VMPCK ID to use when communicating with the PSP." ); -/* Mutex to serialize the shared buffer access and command handling. */ -static DEFINE_MUTEX (snp_cmd_mutex); -static inline unsigned int get_ctx_authsize (struct snp_guest_dev *snp_dev) {if (snp_dev && snp_dev->ctx)
@@ -99,7 +99,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;
@@ -377,7 +377,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 ;
@@ -417,7 +417,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 ;
@@ -456,7 +456,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 ;
@@ -541,12 +541,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 ; }
@@ -564,7 +564,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.fw_err &©_to_user (argp, &input,sizeof (input)))return -EFAULT ;
@@ -683,6 +683,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
Re: [PATCH v2 02/11] virt: sev-guest: Move mutex to SNP guest device structure
Quoted 3 lines · Nikunj A Dadhania
In preparation to provide new API for sending SNP guest message to the sev-guest driver, move the SNP command mutex to the
snp_guest_devstructure.
In preparation for providing a new API to the sev-guest driver for sending an SNP guest message, ...
Quoted 2 lines
Signed-off-by Nikunj A Dadhania <[email protected]>
Quoted 96 lines
--- drivers/virt/coco/sev-guest/sev-guest.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +11
-10@@ -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;struct snp_guest_msg *request, *response;@@ -48,9 +51,6 @@ static u32 vmpck_id;module_param (vmpck_id, uint,0444 );MODULE_PARM_DESC (vmpck_id,"The VMPCK ID to use when communicating with the PSP." ); -/* Mutex to serialize the shared buffer access and command handling. */ -static DEFINE_MUTEX (snp_cmd_mutex); -static inline unsigned int get_ctx_authsize (struct snp_guest_dev *snp_dev) {if (snp_dev && snp_dev->ctx)@@ -99,7 +99,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;@@ -377,7 +377,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 ;@@ -417,7 +417,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 ;@@ -456,7 +456,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 ;@@ -541,12 +541,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 ; }@@ -564,7 +564,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.fw_err &©_to_user (argp, &input,sizeof (input)))return -EFAULT ;@@ -683,6 +683,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;
Re: [PATCH v2 02/11] virt: sev-guest: Move mutex to SNP guest device structure
Quoted 6 lines · Tom Lendacky
Quoted 3 lines · Nikunj A Dadhania
In preparation to provide new API for sending SNP guest message to the sev-guest driver, move the SNP command mutex to the
snp_guest_devstructure.In preparation for providing a new API to the sev-guest driver for sending an SNP guest message, ...
Sure.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Nikunj A Dadhania <[email protected]>Reviewed-by Tom Lendacky <[email protected]>
Regards
Nikunj
PATCH v2 3/11 virt: sev-guest: Add snp_guest_req 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.
--- drivers/virt/coco/sev-guest/sev-guest.c | 87 ++++++++++++++----------- drivers/virt/coco/sev-guest/sev-guest.h | 19 ++++++ 2 files changed, 68 insertions(+), 38 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +49 -38
@@ -60,16 +60,6 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)return 0 ; } -static bool is_vmpck_empty (struct snp_guest_dev *snp_dev) -{ -char zero_key[VMPCK_KEY_LEN ] = {0 }; - -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); - -return true; -} -/* * 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
@@ -198,8 +188,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);/* Verify that the sequence counter is incremented by 1 */ if (unlikely (resp_hdr->msg_seqno != (req_hdr->msg_seqno +1 )))
@@ -221,34 +212,34 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,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, -void *payload,size_t sz) +static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno, +struct snp_guest_req *req,u8 __vmpck_id) { -struct snp_guest_msg *req = snp_dev->request; -struct snp_guest_msg_hdr *hdr = &req->hdr; +struct snp_guest_msg *msg = snp_dev->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 = __vmpck_id; + hdr->msg_sz = req->req_sz;/* Verify the sequence number is non-zero */ if (!hdr->msg_seqno)return -ENOSR ; -dev_dbg (snp_dev->dev,"request [seqno %lld type %d version %d sz %d]\n" , +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,__u64 *fw_err) +static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) {unsigned long err =0xff , override_err =0 ;unsigned long req_start = jiffies;
@@ -262,7 +253,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, &err); + rc =snp_issue_guest_request (req->exit_code, &snp_dev->input, &err);switch (rc) {case -ENOSPC :/*
@@ -273,7 +264,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* IV reuse. */ override_npages = snp_dev->input.data_npages; - exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + req->exit_code =SVM_VMGEXIT_GUEST_REQUEST ;/* * Override the error to inform callers the given extended
@@ -314,8 +305,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,*/ snp_inc_msg_seqno (snp_dev); -if (fw_err) - *fw_err = override_err ?: err; +if (req->fw_err) + *req->fw_err = override_err ?: err;if (override_npages) snp_dev->input.data_npages = override_npages;
@@ -332,13 +323,14 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,return rc; } -static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,int msg_ver, -u8 type,void *req_buf,size_t req_sz,void *resp_buf, -u32 resp_sz,__u64 *fw_err) +static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) {u64 seqno;int rc; +if (!snp_dev || !req) +return -ENODEV ; +/* Get message sequence and verify that its a non-zero */ seqno =snp_get_msg_seqno (snp_dev);if (!seqno)
@@ -347,21 +339,22 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, inmemset (snp_dev->response,0 ,sizeof (struct snp_guest_msg ));/* Encrypt the userspace provided payload */ - rc =enc_payload (snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc =enc_payload (snp_dev, seqno, req, vmpck_id);if (rc)return rc; - rc =__handle_guest_request (snp_dev, exit_code, fw_err); + rc =__handle_guest_request (snp_dev, req);if (rc) { -if (rc == -EIO && *fw_err ==SNP_GUEST_REQ_INVALID_LEN ) +if (rc == -EIO && *req->fw_err ==SNP_GUEST_REQ_INVALID_LEN )return rc; -dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, fw_err: %llu\n" , rc, *fw_err); +dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, fw_err: %llu\n" , + rc, *req->fw_err);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->resp_buf, req->resp_sz);if (rc) {dev_alert (snp_dev->dev,"Detected unexpected decode failure from ASP. rc: %d\n" , rc);snp_disable_vmpck (snp_dev);
@@ -371,6 +364,24 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, inreturn 0 ; } + +static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,u8 msg_version, +u8 msg_type,void *req_buf,size_t req_sz,void *resp_buf, +u32 resp_sz,__u64 *fw_err) +{ +struct snp_guest_req guest_req = { + .msg_version = msg_version, + .msg_type = msg_type, + .req_buf = req_buf, + .req_sz = req_sz, + .resp_buf = resp_buf, + .resp_sz = resp_sz, + .fw_err = fw_err, + .exit_code = exit_code, + }; +return snp_send_guest_request (snp_dev, &guest_req); +} +static int get_report (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) {struct snp_report_resp *resp;
@@ -544,7 +555,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 (is_vmpck_empty (snp_dev->vmpck)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ;
@@ -678,7 +689,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) }/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { +if (is_vmpck_empty (snp_dev->vmpck)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); goto e_unmap; }
drivers/virt/coco/sev-guest/sev-guest.h +19 -0
@@ -63,4 +63,23 @@ struct snp_guest_msg { u8 payload[4000]; } __packed; +struct snp_guest_req { +void *req_buf, *resp_buf; +size_t req_sz, resp_sz; +u64 exit_code; +u64 *fw_err; +u8 msg_version; +u8 msg_type; +}; + +static inline bool is_vmpck_empty (u8 *vmpck) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; + +if (vmpck) +return !memcmp (vmpck, zero_key,VMPCK_KEY_LEN ); + +return true; +} +#endif /* __VIRT_SEVGUEST_H__ */
Signature · 1 line
2.34.1
Re: [PATCH v2 03/11] virt: sev-guest: Add snp_guest_req structure
Quoted 18 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]>--- drivers/virt/coco/sev-guest/sev-guest.c | 87 ++++++++++++++----------- drivers/virt/coco/sev-guest/sev-guest.h | 19 ++++++ 2 files changed, 68 insertions(+), 38 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +0
-0@@ -60,16 +60,6 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)return 0 ; }
drivers/virt/coco/sev-guest/sev-guest.c static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) · line 72
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +0
-10-static bool is_vmpck_empty (struct snp_guest_dev *snp_dev) -{ -char zero_key[VMPCK_KEY_LEN ] = {0 }; - -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); - -return true; -} -
This change seems separate from the changes for snp_guest_req.
drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 193
Quoted 12 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2/* * 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 @@ -198,8 +188,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);
Again, not related to the purpose of this patch.
drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 216
Quoted 11 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-2/* Verify that the sequence counter is incremented by 1 */ if (unlikely (resp_hdr->msg_seqno != (req_hdr->msg_seqno +1 )))@@ -221,34 +212,34 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,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, -void *payload,size_t sz) +static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno, +struct snp_guest_req *req,u8 __vmpck_id)
Can the vmpck_id be part of the snp_guest_req structure?
Quoted 18 lines
drivers/virt/coco/sev-guest/sev-guest.c +5
-6{ -struct snp_guest_msg *req = snp_dev->request; -struct snp_guest_msg_hdr *hdr = &req->hdr; +struct snp_guest_msg *msg = snp_dev->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;
drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 236
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2- hdr->msg_sz = sz; + hdr->msg_vmpck = __vmpck_id; + hdr->msg_sz = req->req_sz;/* Verify the sequence number is non-zero */ if (!hdr->msg_seqno)return -ENOSR ; - dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", + pr_debug("request [seqno %lld type %d version %d sz %d]\n",
Unrelated change.
Quoted 44 lines
drivers/virt/coco/sev-guest/sev-guest.c +6
-6hdr->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,__u64 *fw_err) +static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) {unsigned long err =0xff , override_err =0 ;unsigned long req_start = jiffies;@@ -262,7 +253,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, &err); + rc =snp_issue_guest_request (req->exit_code, &snp_dev->input, &err);switch (rc) {case -ENOSPC :/* @@ -273,7 +264,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* IV reuse. */ override_npages = snp_dev->input.data_npages; - exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + req->exit_code =SVM_VMGEXIT_GUEST_REQUEST ;/* * Override the error to inform callers the given extended @@ -314,8 +305,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,*/ snp_inc_msg_seqno (snp_dev); -if (fw_err) - *fw_err = override_err ?: err; +if (req->fw_err) + *req->fw_err = override_err ?: err;if (override_npages) snp_dev->input.data_npages = override_npages;@@ -332,13 +323,14 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,return rc; }
drivers/virt/coco/sev-guest/sev-guest.c static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, · line 332
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-3-static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,int msg_ver, -u8 type,void *req_buf,size_t req_sz,void *resp_buf, -u32 resp_sz,__u64 *fw_err) +static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) {u64 seqno;int rc; +if (!snp_dev || !req) +return -ENODEV ;
This seems unrelated, at least the check for snp_dev. And looking at the only caller, a guest request is always provided. So this seems unnecessary - at least at this point in the series.
Quoted 19 lines
drivers/virt/coco/sev-guest/sev-guest.c +4
-3+/* Get message sequence and verify that its a non-zero */ seqno =snp_get_msg_seqno (snp_dev);if (!seqno)@@ -347,21 +339,22 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, inmemset (snp_dev->response,0 ,sizeof (struct snp_guest_msg ));/* Encrypt the userspace provided payload */ - rc =enc_payload (snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc =enc_payload (snp_dev, seqno, req, vmpck_id);if (rc)return rc; - rc =__handle_guest_request (snp_dev, exit_code, fw_err); + rc =__handle_guest_request (snp_dev, req);if (rc) { -if (rc == -EIO && *fw_err ==SNP_GUEST_REQ_INVALID_LEN ) +if (rc == -EIO && *req->fw_err ==SNP_GUEST_REQ_INVALID_LEN )return rc;
drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 357
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2-dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, fw_err: %llu\n" , rc, *fw_err); +dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, fw_err: %llu\n" , + rc, *req->fw_err);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->resp_buf, req->resp_sz);
Can't you just pass req here?
Quoted 12 lines
drivers/virt/coco/sev-guest/sev-guest.c +5
-0if (rc) {dev_alert (snp_dev->dev,"Detected unexpected decode failure from ASP. rc: %d\n" , rc);snp_disable_vmpck (snp_dev);@@ -371,6 +364,24 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, inreturn 0 ; } + +static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,u8 msg_version, +u8 msg_type,void *req_buf,size_t req_sz,void *resp_buf, +u32 resp_sz,__u64 *fw_err) +{
drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 381
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +10
-0+struct snp_guest_req guest_req = { + .msg_version = msg_version, + .msg_type = msg_type, + .req_buf = req_buf, + .req_sz = req_sz, + .resp_buf = resp_buf, + .resp_sz = resp_sz, + .fw_err = fw_err, + .exit_code = exit_code, + };
Add a blank line here.
Thanks,
Tom
Quoted 52 lines
drivers/virt/coco/sev-guest/sev-guest.c +5
-2+return snp_send_guest_request (snp_dev, &guest_req); +} +static int get_report (struct snp_guest_dev *snp_dev,struct snp_guest_request_ioctl *arg) {struct snp_report_resp *resp;@@ -544,7 +555,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 (is_vmpck_empty (snp_dev->vmpck)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );mutex_unlock (&snp_dev->cmd_mutex);return -ENOTTY ;@@ -678,7 +689,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) }/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { +if (is_vmpck_empty (snp_dev->vmpck)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); goto e_unmap; }drivers/virt/coco/sev-guest/sev-guest.h +19
-0@@ -63,4 +63,23 @@ struct snp_guest_msg { u8 payload[4000]; } __packed; +struct snp_guest_req { +void *req_buf, *resp_buf; +size_t req_sz, resp_sz; +u64 exit_code; +u64 *fw_err; +u8 msg_version; +u8 msg_type; +}; + +static inline bool is_vmpck_empty (u8 *vmpck) +{ +char zero_key[VMPCK_KEY_LEN ] = {0 }; + +if (vmpck) +return !memcmp (vmpck, zero_key,VMPCK_KEY_LEN ); + +return true; +} +#endif /* __VIRT_SEVGUEST_H__ */
Re: [PATCH v2 03/11] virt: sev-guest: Add snp_guest_req structure
Quoted 22 lines · Tom Lendacky
Quoted 21 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]>---
drivers/virt/coco/sev-guest/sev-guest.c| 87 ++++++++++++++-----------drivers/virt/coco/sev-guest/sev-guest.h| 19 ++++++2 files changed, 68 insertions(+), 38 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +0
-3@@ -60,16 +60,6 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) return 0; } -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) -{ - char zero_key[VMPCK_KEY_LEN] = {0}; -
Quoted 8 lines
drivers/virt/coco/sev-guest/sev-guest.c static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) · line 72
Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +0
-6- if (snp_dev->vmpck) - return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); - - return true; -} -This change seems separate from the changes for
snp_guest_req.
Sure, will create a separate patch.
Quoted 14 lines
drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 193
Quoted 12 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-1/** 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 @@ -198,8 +188,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);Again, not related to the purpose of this patch.
The idea was to get rid of dev_dbg for the movement, will do in a separate patch.
Quoted 12 lines
drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 216
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-1/* Verify that the sequence counter is incremented by 1 */ if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1)))@@ -221,34 +212,34 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, 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, - void *payload, size_t sz) +static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno, + struct snp_guest_req *req, u8 __vmpck_id)Can the
vmpck_idbe part of thesnp_guest_reqstructure?
Sure, that should be possible.
Quoted 26 lines
Quoted 15 lines
drivers/virt/coco/sev-guest/sev-guest.c +5
-4{ - struct snp_guest_msg *req = snp_dev->request; - struct snp_guest_msg_hdr *hdr = &req->hdr; + struct snp_guest_msg *msg = snp_dev->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;drivers/virt/coco/sev-guest/sev-guest.c static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, · line 236
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-2hdr->msg_seqno = seqno; - hdr->msg_vmpck = vmpck_id; - hdr->msg_sz = sz; + hdr->msg_vmpck = __vmpck_id; + hdr->msg_sz = req->req_sz; /* Verify the sequence number is non-zero */ if (!hdr->msg_seqno) return -ENOSR; - dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", + pr_debug("request [seqno %lld type %d version %d sz %d]\n",
Quoted 1 line
Unrelated change.
Will move to a separate patch.
Quoted 42 lines
Quoted 42 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-1hdr->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, __u64 *fw_err) +static int __handle_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req) { unsigned long err = 0xff, override_err = 0; unsigned long req_start = jiffies;@@ -262,7 +253,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, &err); + rc = snp_issue_guest_request(req->exit_code, &snp_dev->input, &err); switch (rc) {case
-ENOSPC:
/*drivers/virt/coco/sev-guest/sev-guest.c +3
-4@@ -273,7 +264,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,* IV reuse. */ override_npages = snp_dev->input.data_npages; - exit_code = SVM_VMGEXIT_GUEST_REQUEST; + req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; /** Override the error to inform callers the given extended @@ -314,8 +305,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,*/ snp_inc_msg_seqno(snp_dev); - if (fw_err) - *fw_err = override_err ?: err; + if (req->fw_err) + *req->fw_err = override_err ?: err; if (override_npages) snp_dev->input.data_npages = override_npages;@@ -332,13 +323,14 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, return rc; } -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, int msg_ver, - u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err)
Quoted 8 lines
drivers/virt/coco/sev-guest/sev-guest.c static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, · line 332
Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-0+static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) { u64 seqno; int rc; + if (!snp_dev || !req) + return -ENODEV;This seems unrelated, at least the check for
snp_dev. And looking at the only caller, a guest request is always provided. So this seems unnecessary - at least at this point in the series.
Right, not necessary here, but will be needed when sev-guest driver calls this after the movement to sev.c. Otherwise, I will need to add this in the movement patch 5/11.
Quoted 20 lines
Quoted 20 lines
drivers/virt/coco/sev-guest/sev-guest.c +5
-2+ /* Get message sequence and verify that its a non-zero */ seqno = snp_get_msg_seqno(snp_dev); if (!seqno)@@ -347,21 +339,22 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload */ - rc = enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc = enc_payload(snp_dev, seqno, req, vmpck_id); if (rc) return rc; - rc = __handle_guest_request(snp_dev, exit_code, fw_err); + rc = __handle_guest_request(snp_dev, req); if (rc) { - if (rc == -EIO && *fw_err == SNP_GUEST_REQ_INVALID_LEN) + if (rc == -EIO && *req->fw_err == SNP_GUEST_REQ_INVALID_LEN) return rc; - dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", rc, *fw_err); + dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n",
Quoted 8 lines
drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 357
Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-0+ rc, *req->fw_err); 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->resp_buf, req->resp_sz);Can't you just pass req here?
Yes, can do that.
Quoted 16 lines
Quoted 16 lines
drivers/virt/coco/sev-guest/sev-guest.c +0
-0if (rc) { dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); snp_disable_vmpck(snp_dev);@@ -371,6 +364,24 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in return 0; }+
drivers/virt/coco/sev-guest/sev-guest.c +8
-0+static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,u8 msg_version, + u8 msg_type, void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz, __u64 *fw_err) +{ + struct snp_guest_req guest_req = { + .msg_version = msg_version, + .msg_type = msg_type, + .req_buf = req_buf,
Quoted 8 lines
Quoted 6 lines
+ .req_sz =
req_sz,
+ .resp_buf =resp_buf,
+ .resp_sz =resp_sz,
+ .fw_err =fw_err,
+ .exit_code =exit_code,
+ };Add a blank line here.
Sure.
Regards
Nikunj
PATCH v2 4/11 virt: sev-guest: Add simplified helper to assign vmpck
In reply to Nikunj A DadhaniaSimplify get_vmpck and prepare it to be used as an API. Update the snp_guest_dev structure in snp_assign_vmpck(). Added vmpck_id to the snp_guest_dev structure which can be used in SNP guest request API and will remove direct use of vmpck_id command line parameter.
--- drivers/virt/coco/sev-guest/sev-guest.c | 41 ++++++++----------------- 1 file changed, 12 insertions(+), 29 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +12 -29
@@ -45,6 +45,7 @@ struct snp_guest_dev {struct snp_req_data input;u32 *os_area_msg_seqno;u8 *vmpck; +u8 vmpck_id; };static u32 vmpck_id;
@@ -80,7 +81,7 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) {dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - vmpck_id); + snp_dev->vmpck_id);memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); snp_dev->vmpck =NULL ; }
@@ -339,7 +340,7 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guesmemset (snp_dev->response,0 ,sizeof (struct snp_guest_msg ));/* Encrypt the userspace provided payload */ - rc =enc_payload (snp_dev, seqno, req, vmpck_id); + rc =enc_payload (snp_dev, seqno, req, snp_dev->vmpck_id);if (rc)return rc;
@@ -364,7 +365,6 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guesreturn 0 ; } -static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,u8 msg_version,u8 msg_type,void *req_buf,size_t req_sz,void *resp_buf,u32 resp_sz,__u64 *fw_err)
@@ -625,32 +625,16 @@ 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; + dev->vmpck = dev->layout->vmpck0 + vmpck_id *VMPCK_KEY_LEN ; + dev->os_area_msg_seqno = &dev->layout->os_area.msg_seqno_0 + vmpck_id; -return key; +return true; }static int __init sev_guest_probe (struct platform_device *pdev)
@@ -682,8 +666,8 @@ 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; }
@@ -697,7 +681,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 ));
Signature · 1 line
2.34.1
Re: [PATCH v2 04/11] virt: sev-guest: Add simplified helper to assign vmpck
Quoted 4 lines · Nikunj A Dadhania
Simplify
get_vmpckand prepare it to be used as an API. Update thesnp_guest_devstructure insnp_assign_vmpck(). Addedvmpck_idto thesnp_guest_devstructure which can be used in SNP guest request API and will remove direct use ofvmpck_idcommand line parameter.
This needs to say more about why you're doing this. The first two sentences need more explanation as they don't really stand on their own.
The last sentence talks about removing direct use of vmpck_id, so you should probably do that in this patch since it seems to be the real reason for all this.
Thanks,
Tom
Quoted 103 lines
Signed-off-by Nikunj A Dadhania <[email protected]>--- drivers/virt/coco/sev-guest/sev-guest.c | 41 ++++++++----------------- 1 file changed, 12 insertions(+), 29 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +12
-29@@ -45,6 +45,7 @@ struct snp_guest_dev {struct snp_req_data input;u32 *os_area_msg_seqno;u8 *vmpck; +u8 vmpck_id; };static u32 vmpck_id;@@ -80,7 +81,7 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) {dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - vmpck_id); + snp_dev->vmpck_id);memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); snp_dev->vmpck =NULL ; }@@ -339,7 +340,7 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guesmemset (snp_dev->response,0 ,sizeof (struct snp_guest_msg ));/* Encrypt the userspace provided payload */ - rc =enc_payload (snp_dev, seqno, req, vmpck_id); + rc =enc_payload (snp_dev, seqno, req, snp_dev->vmpck_id);if (rc)return rc;@@ -364,7 +365,6 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guesreturn 0 ; } -static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,u8 msg_version,u8 msg_type,void *req_buf,size_t req_sz,void *resp_buf,u32 resp_sz,__u64 *fw_err)@@ -625,32 +625,16 @@ 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; + dev->vmpck = dev->layout->vmpck0 + vmpck_id *VMPCK_KEY_LEN ; + dev->os_area_msg_seqno = &dev->layout->os_area.msg_seqno_0 + vmpck_id; -return key; +return true; }static int __init sev_guest_probe (struct platform_device *pdev)@@ -682,8 +666,8 @@ 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; }@@ -697,7 +681,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 ));
Re: [PATCH v2 04/11] virt: sev-guest: Add simplified helper to assign vmpck
Quoted 9 lines · Tom Lendacky
Quoted 4 lines · Nikunj A Dadhania
Simplify
get_vmpckand prepare it to be used as an API. Update thesnp_guest_devstructure insnp_assign_vmpck(). Addedvmpck_idto thesnp_guest_devstructure which can be used in SNP guest request API and will remove direct use ofvmpck_idcommand line parameter.This needs to say more about why you're doing this. The first two sentences need more explanation as they don't really stand on their own. How about the this:
Add vmpck_id to snp_guest_dev struct instead of directly using vmpck_id command line parameter. Each SNP guest has four VMPCK, which the firmware generates and provides to the guest in a special secrets page. Each VMPCK has a sequence number. Simplify assignment of key and sequence number instead of using a switch case.
Quoted 3 lines
The last sentence talks about removing direct use of
vmpck_id, so you should probably do that in this patch since it seems to be the real reason for all this.
I did change all places using vmpck_id to snp_dev->vmpck_id. This is the only place the command line parameter is being used.
+ if (!snp_assign_vmpck(snp_dev, vmpck_id)) {
Regards
Nikunj
Quoted 97 lines
Thanks,
TomQuoted 93 lines
Signed-off-by Nikunj A Dadhania <[email protected]>---
drivers/virt/coco/sev-guest/sev-guest.c| 41 ++++++++-----------------1 file changed, 12 insertions(+), 29 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +3
-2@@ -45,6 +45,7 @@ struct snp_guest_dev { struct snp_req_data input; u32 *os_area_msg_seqno; u8 *vmpck; + u8 vmpck_id; }; static u32 vmpck_id;@@ -80,7 +81,7 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) { dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", - vmpck_id); + snp_dev->vmpck_id); memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); snp_dev->vmpck = NULL; }@@ -339,7 +340,7 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_gues memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload */ - rc = enc_payload(snp_dev, seqno, req, vmpck_id); + rc = enc_payload(snp_dev, seqno, req, snp_dev->vmpck_id); if (rc) return rc;@@ -364,7 +365,6 @@ static int snp_send_guest_request(struct
snp_guest_dev*snp_dev, structsnp_guesreturn 0;
}
-
static int handle_guest_request(structsnp_guest_dev*snp_dev, u64exit_code, u8msg_version, u8msg_type, void*req_buf,size_treq_sz, void*resp_buf,
u32resp_sz,__u64*fw_err)drivers/virt/coco/sev-guest/sev-guest.c +9
-23@@ -625,32 +625,16 @@ 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; + dev->vmpck = dev->layout->vmpck0 + vmpck_id * VMPCK_KEY_LEN; + dev->os_area_msg_seqno = &dev->layout->os_area.msg_seqno_0 + vmpck_id; - return key; + return true; } static int __init sev_guest_probe(struct platform_device *pdev)@@ -682,8 +666,8 @@ 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; }@@ -697,7 +681,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));
Re: [PATCH v2 04/11] virt: sev-guest: Add simplified helper to assign vmpck
In reply to Nikunj A Dadhaniadrivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 631
Quoted 7 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;
Should vmpck_id be a uint to match the module parameter or should we check if vmpck_id < 0 ?
Re: [PATCH v2 04/11] virt: sev-guest: Add simplified helper to assign vmpck
Quoted 10 lines · Peter Gonda
drivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 631
Quoted 7 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;Should
vmpck_idbe a uint to match the module parameter or should we check ifvmpck_id< 0 ?
Thanks for pointing that out, I will make vmpck_id as a uint.
Regards
Nikunj
PATCH v2 5/11 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 security coprocessor really early during boot. Lot of the required functions are implemented in the sev-guest driver. Move the required functions and provide API to the driver to assign VM communications key and send guest request.
---
arch/x86/Kconfig | 1 +
.../x86/include/asm}/sev-guest.h | 6 +
arch/x86/include/asm/sev.h | 22 +
arch/x86/kernel/sev.c | 437 ++++++++++++++++-
drivers/virt/coco/sev-guest/Kconfig | 1 -
drivers/virt/coco/sev-guest/sev-guest.c | 455 ++----------------
6 files changed, 487 insertions(+), 435 deletions(-)
rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (86%)arch/x86/Kconfig +1 -0
@@ -1545,6 +1545,7 @@ config AMD_MEM_ENCRYPT select INSTRUCTION_DECODER select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT + 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 renamed from drivers/virt/coco/sev-guest/sev-guest.h +6 -0
@@ -12,6 +12,9 @@#include <linux/types.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#define AAD_LEN 48
@@ -72,6 +75,9 @@ struct snp_guest_req {u8 msg_type; }; +int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req); +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id); +static inline bool is_vmpck_empty (u8 *vmpck) {char zero_key[VMPCK_KEY_LEN ] = {0 };
arch/x86/include/asm/sev.h +22 -0
@@ -9,6 +9,7 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> +#include <linux/miscdevice.h> #include <asm/insn.h> #include <asm/sev-common.h> #include <asm/bootparam.h>
@@ -96,6 +97,27 @@ struct snp_req_data {struct sev_guest_platform_data {u64 secrets_gpa; + +void *certs_data; +struct aesgcm_ctx *ctx; +struct snp_guest_msg *req, *resp; +struct snp_secrets_page_layout *layout; +struct snp_req_data input; +u8 *vmpck0; +}; + +struct snp_guest_dev { +struct device *dev; +struct miscdevice misc; + +/* Mutex to serialize the shared buffer access and command handling. */ +struct mutex cmd_mutex; + +struct sev_guest_platform_data *pdata; + +u32 *os_area_msg_seqno; +u8 *vmpck; +u8 vmpck_id; };/*
arch/x86/kernel/sev.c +430 -7
464 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 +28 -427
573 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 05/11] x86/sev: Move and reorganize sev guest request api
Quoted 2 lines · Nikunj A Dadhania
For enabling Secure TSC, SEV-SNP guests need to communicate with the security coprocessor really early during boot. Lot of the required
s/security coprocessor really/AMD Secure Processor/
s/Lot/Many/
Quoted 1 line
functions are implemented in the sev-guest driver. Move the required
... in the sev-guest driver and therefore not available at early boot.
Quoted 1 line
functions and provide API to the driver to assign VM communications
s/provide API/provide an API/
s/to assign.*//
Quoted 65 lines
key and send guest request.
Signed-off-by Nikunj A Dadhania <[email protected]>--- arch/x86/Kconfig | 1 + .../x86/include/asm}/sev-guest.h | 6 + arch/x86/include/asm/sev.h | 22 + arch/x86/kernel/sev.c | 437 ++++++++++++++++- drivers/virt/coco/sev-guest/Kconfig | 1 - drivers/virt/coco/sev-guest/sev-guest.c | 455 ++---------------- 6 files changed, 487 insertions(+), 435 deletions(-)rename {
drivers/virt/coco/sev-guest=>arch/x86/include/asm}/sev-guest.h(86%)arch/x86/Kconfig +1
-0@@ -1545,6 +1545,7 @@ config AMD_MEM_ENCRYPT select INSTRUCTION_DECODER select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT + 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 renamed from drivers/virt/coco/sev-guest/sev-guest.h +6
-0@@ -12,6 +12,9 @@#include <linux/types.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#define AAD_LEN 48@@ -72,6 +75,9 @@ struct snp_guest_req {u8 msg_type; }; +int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req); +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id); +static inline bool is_vmpck_empty (u8 *vmpck) {char zero_key[VMPCK_KEY_LEN ] = {0 };arch/x86/include/asm/sev.h +1
-0@@ -9,6 +9,7 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> +#include <linux/miscdevice.h> #include <asm/insn.h> #include <asm/sev-common.h> #include <asm/bootparam.h>
arch/x86/include/asm/sev.h struct snp_req_data { · line 106
Quoted 10 lines
arch/x86/include/asm/sev.h +7
-0@@ -96,6 +97,27 @@ struct snp_req_data {struct sev_guest_platform_data {u64 secrets_gpa; + +void *certs_data; +struct aesgcm_ctx *ctx; +struct snp_guest_msg *req, *resp; +struct snp_secrets_page_layout *layout; +struct snp_req_data input; +u8 *vmpck0;
Isn't this unneeded? You have the vmpck and vmpck_id in the snp_guest_dev struct which will be set based on the module parameter, so vmpck0 and associated checks shouldn't be needed.
Quoted 470 lines
arch/x86/include/asm/sev.h +14
-0+}; + +struct snp_guest_dev { +struct device *dev; +struct miscdevice misc; + +/* Mutex to serialize the shared buffer access and command handling. */ +struct mutex cmd_mutex; + +struct sev_guest_platform_data *pdata; + +u32 *os_area_msg_seqno; +u8 *vmpck; +u8 vmpck_id; };/* arch/x86/kernel/sev.c +424
-3@@ -22,10 +22,13 @@#include <linux/efi.h> #include <linux/platform_device.h> #include <linux/io.h> +#include <linux/psp-sev.h> +#include <crypto/gcm.h> #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> @@ -982,6 +985,427 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)free_page ((unsigned long )vmsa); } +static struct sev_guest_platform_data *platform_data; + +/* +* 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) +{ +pr_alert ("Disabling vmpck_id %d to prevent IV reuse.\n" , snp_dev->vmpck_id); +memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); + snp_dev->vmpck =NULL ; +} + +static inline u64 __snp_get_msg_seqno (struct snp_guest_dev *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; + +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) +{ +/* +* 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 ; +} + +static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) +{ +struct aesgcm_ctx *ctx; + + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT ); +if (!ctx) +return NULL ; + +if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { +pr_err ("SNP: crypto init failed\n" ); +kfree (ctx); +return NULL ; + } + +return ctx; +} + +static void free_shared_pages (void *buf,size_t sz) +{ +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); +} + +static int snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) +{ +u64 gpa; +int ret; + +if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP )) +return -ENODEV ; + + gpa =get_secrets_page (); +if (!gpa) +return -ENODEV ; + + pdata->layout = (__force void *)ioremap_encrypted (gpa,PAGE_SIZE ); +if (!pdata->layout) { +pr_err ("Unable to locate AP jump table address: failed to map the SNP secrets page.\n" ); +return -ENODEV ; + } + + ret = -EINVAL ; + pdata->vmpck0 = pdata->layout->vmpck0; + +/* Verify that VMPCK is not zero. */ +if (is_vmpck_empty (pdata->vmpck0)) { +pr_err ("vmpck id 0 is null\n" ); + goto e_unmap; + } + + ret = -ENOMEM ; +/* Allocate the shared page used for the request and response message. */ + pdata->req =alloc_shared_pages (sizeof (struct snp_guest_msg )); +if (!pdata->req) + goto e_unmap; + + pdata->resp =alloc_shared_pages (sizeof (struct snp_guest_msg )); +if (!pdata->resp) + goto e_free_request; + + pdata->certs_data =alloc_shared_pages (SEV_FW_BLOB_MAX_SIZE ); +if (!pdata->certs_data) + goto e_free_response; + + ret = -EIO ; + pdata->ctx =snp_init_crypto (pdata->vmpck0,VMPCK_KEY_LEN ); +if (!pdata->ctx) + goto e_free_cert_data; + +/* initial the input address for guest request */ + pdata->input.req_gpa =__pa (pdata->req); + pdata->input.resp_gpa =__pa (pdata->resp); + pdata->input.data_gpa =__pa (pdata->certs_data); + +return 0 ; + +e_free_cert_data: +free_shared_pages (pdata->certs_data,SEV_FW_BLOB_MAX_SIZE ); +e_free_response: +free_shared_pages (pdata->resp,sizeof (struct snp_guest_msg )); +e_free_request: +free_shared_pages (pdata->req,sizeof (struct snp_guest_msg )); +e_unmap: +iounmap (pdata->layout); + +return ret; +} + +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 sev_guest_platform_data *pdata, +void *payload,u32 sz) +{ +struct snp_guest_msg *resp = pdata->resp; +struct snp_guest_msg *req = pdata->req; +struct snp_guest_msg_hdr *req_hdr = &req->hdr; +struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; +struct aesgcm_ctx *ctx = pdata->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); + +/* 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) > sz)) +return -EBADMSG ; + +return dec_payload (ctx, resp, payload, resp_hdr->msg_sz); +} + +static int enc_payload (struct sev_guest_platform_data *pdata,u64 seqno, +struct snp_guest_req *req,u8 vmpck_id) +{ +struct snp_guest_msg *msg = pdata->req; +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 = 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); + +return __enc_payload (pdata->ctx, msg, req->req_buf, req->req_sz); +} + +static int __handle_guest_request (struct snp_guest_dev *snp_dev, +struct sev_guest_platform_data *pdata, +struct snp_guest_req *req) +{ +unsigned long err =0xff , override_err =0 ; +unsigned long req_start = jiffies; +unsigned int override_npages =0 ; +int rc; + +retry_request: +/* +* Call firmware to process the request. In this function the encrypted +* message enters shared memory with the host. So after this call the +* sequence number must be incremented or the VMPCK must be deleted to +* prevent reuse of the IV. +*/ + rc =snp_issue_guest_request (req->exit_code, &pdata->input, &err); +switch (rc) { +case -ENOSPC : +/* +* If the extended guest request fails due to having too +* small of a certificate data buffer, retry the same +* guest request without the extended data request in +* order to increment the sequence number and thus avoid +* IV reuse. +*/ + override_npages = pdata->input.data_npages; + req->exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + +/* +* Override the error to inform callers the given extended +* request buffer size was too small and give the caller the +* required buffer size. +*/ + override_err =SNP_GUEST_REQ_INVALID_LEN ; + +/* +* If this call to the firmware succeeds, the sequence number can +* be incremented allowing for continued use of the VMPCK. If +* there is an error reflected in the return value, this value +* is checked further down and the result will be the deletion +* of the VMPCK and the error code being propagated back to the +* user as an ioctl() return code. +*/ + goto retry_request; + +/* +* The host may return SNP_GUEST_REQ_ERR_EBUSY if the request has been +* throttled. Retry in the driver to avoid returning and reusing the +* message sequence number on a different message. +*/ +case -EAGAIN : +if (jiffies - req_start >SNP_REQ_MAX_RETRY_DURATION ) { + rc = -ETIMEDOUT ; +break ; + } +schedule_timeout_killable (SNP_REQ_RETRY_DELAY ); + goto retry_request; + } + +/* +* Increment the message sequence number. There is no harm in doing +* this now because decryption uses the value stored in the response +* structure and any failure will wipe the VMPCK, preventing further +* use anyway. +*/ +snp_inc_msg_seqno (snp_dev); + +if (req->fw_err) + *req->fw_err = override_err ?: err; + +if (override_npages) + pdata->input.data_npages = override_npages; + +/* +* If an extended guest request was issued and the supplied certificate +* buffer was not large enough, a standard guest request was issued to +* prevent IV reuse. If the standard request was successful, return -EIO +* back to the caller as would have originally been returned. +*/ +if (!rc && override_err ==SNP_GUEST_REQ_INVALID_LEN ) +return -EIO ; + +return rc; +} + +int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) +{ +struct sev_guest_platform_data *pdata; +u64 seqno; +int rc; + +if (!snp_dev || !snp_dev->pdata || !req) +return -ENODEV ; + + pdata = snp_dev->pdata; + +/* Get message sequence and verify that its a non-zero */ + seqno =snp_get_msg_seqno (snp_dev); +if (!seqno) +return -EIO ; + +memset (pdata->resp,0 ,sizeof (struct snp_guest_msg )); + +/* Encrypt the provided payload */ + rc =enc_payload (pdata, seqno, req, snp_dev->vmpck_id); +if (rc) +return rc; + + rc =__handle_guest_request (snp_dev, pdata, req); +if (rc) { +if (rc == -EIO && *req->fw_err ==SNP_GUEST_REQ_INVALID_LEN ) +return rc; + +pr_alert ("Detected error from ASP request. rc: %d, fw_err: %llu\n" , + rc, *req->fw_err); +snp_disable_vmpck (snp_dev); +return rc; + } + + rc =verify_and_dec_payload (pdata, req->resp_buf, req->resp_sz); +if (rc) { +pr_alert ("Detected unexpected decode failure from ASP. rc: %d\n" , rc); +snp_disable_vmpck (snp_dev); +return rc; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (snp_send_guest_request); + +bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) +{ +struct sev_guest_platform_data *pdata = dev->pdata; + +if (!pdata ||WARN_ON (vmpck_id >3 )) +return false; + + dev->vmpck_id = vmpck_id; + dev->vmpck = pdata->layout->vmpck0 + vmpck_id *VMPCK_KEY_LEN ; + dev->os_area_msg_seqno = &pdata->layout->os_area.msg_seqno_0 + vmpck_id; + +return true; +} +EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static int wakeup_cpu_via_vmgexit (int apic_id,unsigned long start_ip) {struct sev_es_save_area *cur_vmsa, *vmsa;@@ -2247,18 +2671,17 @@ static struct platform_device sev_guest_device = {static int __init snp_init_platform_device (void ) { -struct sev_guest_platform_data data; -u64 gpa; -
arch/x86/kernel/sev.c static struct platform_device sev_guest_device = { · line 2681
Quoted 10 lines
arch/x86/kernel/sev.c +5
-2if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))return -ENODEV ; - gpa =get_secrets_page (); -if (!gpa) + platform_data =kzalloc (sizeof (*platform_data),GFP_KERNEL ); +if (!platform_data) +return -ENOMEM ; + +if (snp_setup_psp_messaging (platform_data))
This shouldn't be done here (or yet) since you only moving the routines. The sev-guest driver should call this regardless of the vmpck_id value.
Quoted 523 lines
arch/x86/kernel/sev.c +1
-2return -ENODEV ; - data.secrets_gpa = gpa; -if (platform_device_add_data (&sev_guest_device, &data,sizeof (data))) +if (platform_device_add_data (&sev_guest_device, platform_data,sizeof (*platform_data)))return -ENODEV ;if (platform_device_register (&sev_guest_device))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 +16
-387@@ -24,110 +24,25 @@#include <asm/svm.h> #include <asm/sev.h> -#include "sev-guest.h" +#include <asm/sev-guest.h> #define DEVICE_NAME "sev-guest" -#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) -#define SNP_REQ_RETRY_DELAY (2*HZ) - -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; -struct snp_guest_msg *request, *response; -struct snp_secrets_page_layout *layout; -struct snp_req_data input; -u32 *os_area_msg_seqno; -u8 *vmpck; -u8 vmpck_id; -}; -static u32 vmpck_id;module_param (vmpck_id, uint,0444 );MODULE_PARM_DESC (vmpck_id,"The VMPCK ID to use when communicating with the PSP." );static inline unsigned int get_ctx_authsize (struct snp_guest_dev *snp_dev) { -if (snp_dev && snp_dev->ctx) -return snp_dev->ctx->authsize; +struct sev_guest_platform_data *pdata = snp_dev->pdata; + +if (pdata && pdata->ctx) +return pdata->ctx->authsize;WARN_ONCE (1 ,"Unable to get crypto authsize\n" );return 0 ; } -/* -* 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) -{ -dev_alert (snp_dev->dev,"Disabling vmpck_id %d to prevent IV reuse.\n" , - snp_dev->vmpck_id); -memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); - snp_dev->vmpck =NULL ; -} - -static inline u64 __snp_get_msg_seqno (struct snp_guest_dev *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; - -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 ) { -dev_err (snp_dev->dev,"request message sequence counter overflow\n" ); -return 0 ; - } - -return count; -} - -static void snp_inc_msg_seqno (struct snp_guest_dev *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 ; -} -static inline struct snp_guest_dev *to_snp_dev (struct file *file) {struct miscdevice *dev = file->private_data;@@ -135,236 +50,6 @@ 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) -{ -struct aesgcm_ctx *ctx; - - ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT ); -if (!ctx) -return NULL ; - -if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { -pr_err ("SNP: crypto init failed\n" ); -kfree (ctx); -return NULL ; - } - -return ctx; -} - -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,void *payload,u32 sz) -{ -struct snp_guest_msg *resp = snp_dev->response; -struct snp_guest_msg *req = snp_dev->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); - -/* 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) > sz)) -return -EBADMSG ; - -/* Decrypt the payload */ -return dec_payload (ctx, resp, payload, resp_hdr->msg_sz); -} - -static int enc_payload (struct snp_guest_dev *snp_dev,u64 seqno, -struct snp_guest_req *req,u8 __vmpck_id) -{ -struct snp_guest_msg *msg = snp_dev->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 = __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); - -return __enc_payload (snp_dev->ctx, msg, req->req_buf, req->req_sz); -} - -static int __handle_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) -{ -unsigned long err =0xff , override_err =0 ; -unsigned long req_start = jiffies; -unsigned int override_npages =0 ; -int rc; - -retry_request: -/* -* Call firmware to process the request. In this function the encrypted -* message enters shared memory with the host. So after this call the -* sequence number must be incremented or the VMPCK must be deleted to -* prevent reuse of the IV. -*/ - rc =snp_issue_guest_request (req->exit_code, &snp_dev->input, &err); -switch (rc) { -case -ENOSPC : -/* -* If the extended guest request fails due to having too -* small of a certificate data buffer, retry the same -* guest request without the extended data request in -* order to increment the sequence number and thus avoid -* IV reuse. -*/ - override_npages = snp_dev->input.data_npages; - req->exit_code =SVM_VMGEXIT_GUEST_REQUEST ; - -/* -* Override the error to inform callers the given extended -* request buffer size was too small and give the caller the -* required buffer size. -*/ - override_err =SNP_GUEST_REQ_INVALID_LEN ; - -/* -* If this call to the firmware succeeds, the sequence number can -* be incremented allowing for continued use of the VMPCK. If -* there is an error reflected in the return value, this value -* is checked further down and the result will be the deletion -* of the VMPCK and the error code being propagated back to the -* user as an ioctl() return code. -*/ - goto retry_request; - -/* -* The host may return SNP_GUEST_REQ_ERR_EBUSY if the request has been -* throttled. Retry in the driver to avoid returning and reusing the -* message sequence number on a different message. -*/ -case -EAGAIN : -if (jiffies - req_start >SNP_REQ_MAX_RETRY_DURATION ) { - rc = -ETIMEDOUT ; -break ; - } -schedule_timeout_killable (SNP_REQ_RETRY_DELAY ); - goto retry_request; - } - -/* -* Increment the message sequence number. There is no harm in doing -* this now because decryption uses the value stored in the response -* structure and any failure will wipe the VMPCK, preventing further -* use anyway. -*/ -snp_inc_msg_seqno (snp_dev); - -if (req->fw_err) - *req->fw_err = override_err ?: err; - -if (override_npages) - snp_dev->input.data_npages = override_npages; - -/* -* If an extended guest request was issued and the supplied certificate -* buffer was not large enough, a standard guest request was issued to -* prevent IV reuse. If the standard request was successful, return -EIO -* back to the caller as would have originally been returned. -*/ -if (!rc && override_err ==SNP_GUEST_REQ_INVALID_LEN ) -return -EIO ; - -return rc; -} - -static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req) -{ -u64 seqno; -int rc; - -if (!snp_dev || !req) -return -ENODEV ; - -/* Get message sequence and verify that its a non-zero */ - seqno =snp_get_msg_seqno (snp_dev); -if (!seqno) -return -EIO ; - -memset (snp_dev->response,0 ,sizeof (struct snp_guest_msg )); - -/* Encrypt the userspace provided payload */ - rc =enc_payload (snp_dev, seqno, req, snp_dev->vmpck_id); -if (rc) -return rc; - - rc =__handle_guest_request (snp_dev, req); -if (rc) { -if (rc == -EIO && *req->fw_err ==SNP_GUEST_REQ_INVALID_LEN ) -return rc; - -dev_alert (snp_dev->dev,"Detected error from ASP request. rc: %d, fw_err: %llu\n" , - rc, *req->fw_err); -snp_disable_vmpck (snp_dev); -return rc; - } - - rc =verify_and_dec_payload (snp_dev, req->resp_buf, req->resp_sz); -if (rc) { -dev_alert (snp_dev->dev,"Detected unexpected decode failure from ASP. rc: %d\n" , rc); -snp_disable_vmpck (snp_dev); -return rc; - } - -return 0 ; -} -static int handle_guest_request (struct snp_guest_dev *snp_dev,u64 exit_code,u8 msg_version,u8 msg_type,void *req_buf,size_t req_sz,void *resp_buf,u32 resp_sz,__u64 *fw_err)@@ -463,12 +148,14 @@ 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 sev_guest_platform_data *pdata;struct snp_ext_report_req req;struct snp_report_resp *resp;int ret, npages =0 , resp_len;lockdep_assert_held (&snp_dev->cmd_mutex); + pdata = snp_dev->pdata;if (!arg->req_data || !arg->resp_data)return -EINVAL ;@@ -492,7 +179,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques* the host. If host does not supply any certs in it, then copy * zeros to indicate that certificate data was not provided. */ -memset (snp_dev->certs_data,0 , req.certs_len); +memset (pdata->certs_data,0 , req.certs_len); npages = req.certs_len >>PAGE_SHIFT ; cmd:/* @@ -505,14 +192,14 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_requesif (!resp)return -ENOMEM ; - snp_dev->input.data_npages = npages; + pdata->input.data_npages = npages; ret =handle_guest_request (snp_dev,SVM_VMGEXIT_EXT_GUEST_REQUEST , arg->msg_version,SNP_MSG_REPORT_REQ , &req.data,sizeof (req.data), resp->data, resp_len, &arg->fw_err);/* If certs length is invalid then copy the returned length */ if (arg->fw_err ==SNP_GUEST_REQ_INVALID_LEN ) { - req.certs_len = snp_dev->input.data_npages <<PAGE_SHIFT ; + req.certs_len = pdata->input.data_npages <<PAGE_SHIFT ;if (copy_to_user ((void __user *)arg->req_data, &req,sizeof (req))) ret = -EFAULT ;@@ -522,7 +209,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques goto e_free;if (npages && -copy_to_user ((void __user *)req.certs_address, snp_dev->certs_data, +copy_to_user ((void __user *)req.certs_address, pdata->certs_data, req.certs_len)) { ret = -EFAULT ; goto e_free;@@ -583,68 +270,17 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longreturn ret; } -static void free_shared_pages (void *buf,size_t sz) -{ -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 (struct device *dev,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) { -dev_err (dev,"failed to mark page shared, ret=%d\n" , ret); -__free_pages (page,get_order (sz)); -return NULL ; - } - -return page_address (page); -} -static const struct file_operations snp_guest_fops = { .owner =THIS_MODULE , .unlocked_ioctl = snp_guest_ioctl, }; -bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id) -{ -if (WARN_ON (vmpck_id >3 )) -return false; - - dev->vmpck_id = vmpck_id; - dev->vmpck = dev->layout->vmpck0 + vmpck_id *VMPCK_KEY_LEN ; - dev->os_area_msg_seqno = &dev->layout->os_area.msg_seqno_0 + vmpck_id; - -return true; -} -static int __init sev_guest_probe (struct platform_device *pdev) { -struct snp_secrets_page_layout *layout; -struct sev_guest_platform_data *data; +struct sev_guest_platform_data *pdata;struct device *dev = &pdev->dev;struct snp_guest_dev *snp_dev;struct miscdevice *misc; -void __iomem *mapping;int ret;if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))@@ -653,80 +289,48 @@ static int __init sev_guest_probe(struct platform_device *pdev)if (!dev->platform_data)return -ENODEV ; - data = (struct sev_guest_platform_data *)dev->platform_data; - mapping =ioremap_encrypted (data->secrets_gpa,PAGE_SIZE ); -if (!mapping) -return -ENODEV ; - - layout = (__force void *)mapping; - - ret = -ENOMEM ; + pdata = (struct sev_guest_platform_data *)dev->platform_data; snp_dev =devm_kzalloc (&pdev->dev,sizeof (struct snp_guest_dev ),GFP_KERNEL );if (!snp_dev) - goto e_unmap; +return -ENOMEM ; ret = -EINVAL ; - snp_dev->layout = layout; + snp_dev->pdata = pdata;if (!snp_assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); - goto e_unmap; + goto e_free_snpdev; }
drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 311
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +5
-1/* Verify that VMPCK is not zero. */ if (is_vmpck_empty (snp_dev->vmpck)) {dev_err (dev,"vmpck id %d is null\n" , vmpck_id); - goto e_unmap; + goto e_free_snpdev; + } + +/* Skip VMPCK0 initialization as the key is already initialized during early boot */ +if (vmpck_id &&aesgcm_expandkey (pdata->ctx, snp_dev->vmpck,VMPCK_KEY_LEN ,AUTHTAG_LEN )) {
See previous comment. The sev-guest driver should be setting up everything private to it no matter the vmpck to be used.
Thanks,
Tom
Quoted 73 lines
drivers/virt/coco/sev-guest/sev-guest.c +7
-39+ ret = -ENODEV ; + goto e_free_snpdev; }mutex_init (&snp_dev->cmd_mutex);platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; -/* Allocate the shared page used for the request and response message. */ - snp_dev->request =alloc_shared_pages (dev,sizeof (struct snp_guest_msg )); -if (!snp_dev->request) - goto e_unmap; - - snp_dev->response =alloc_shared_pages (dev,sizeof (struct snp_guest_msg )); -if (!snp_dev->response) - goto e_free_request; - - snp_dev->certs_data =alloc_shared_pages (dev,SEV_FW_BLOB_MAX_SIZE ); -if (!snp_dev->certs_data) - goto e_free_response; - - ret = -EIO ; - 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; misc->minor =MISC_DYNAMIC_MINOR ; misc->name =DEVICE_NAME ; misc->fops = &snp_guest_fops; -/* 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); + ret =misc_register (misc);if (ret) - goto e_free_ctx; + goto e_free_snpdev;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: -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_snpdev: +kfree (snp_dev);return ret; }@@ -734,11 +338,8 @@ 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 ); -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);return 0 ; }
Re: [PATCH v2 05/11] x86/sev: Move and reorganize sev guest request api
Quoted 10 lines · Tom Lendacky
Quoted 2 lines · Nikunj A Dadhania
For enabling Secure TSC, SEV-SNP guests need to communicate with the security coprocessor really early during boot. Lot of the required
s/security coprocessor really/AMD Secure Processor/
s/Lot/Many/
Quoted 1 line
functions are implemented in the sev-guest driver. Move the required
Quoted 9 lines
... in the sev-guest driver and therefore not available at early boot.
Quoted 1 line
functions and provide API to the driver to assign VM communications
s/provide API/provide an API/
s/to assign.*//
Quoted 1 line
key and send guest request.
Sure will change.
Quoted 4 lines
Quoted 4 lines
arch/x86/include/asm/sev.h +1
-0@@ -96,6 +97,27 @@ struct snp_req_data { struct sev_guest_platform_data { u64 secrets_gpa; +
Quoted 8 lines
arch/x86/include/asm/sev.h struct snp_req_data { · line 106
Quoted 6 lines
arch/x86/include/asm/sev.h +6
-0+ void *certs_data; + struct aesgcm_ctx *ctx; + struct snp_guest_msg *req, *resp; + struct snp_secrets_page_layout *layout; + struct snp_req_data input; + u8 *vmpck0;Isn't this unneeded? You have the vmpck and
vmpck_idin thesnp_guest_devstruct which will be set based on the module parameter, so vmpck0 and associated checks shouldn't be needed.
Yes, this can be removed.
Quoted 7 lines
arch/x86/kernel/sev.c static struct platform_device sev_guest_device = { · line 2681
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+ platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); + if (!platform_data) + return -ENOMEM; + + if (snp_setup_psp_messaging(platform_data))This shouldn't be done here (or yet) since you only moving the routines. The sev-guest driver should call this regardless of the
vmpck_idvalue.
I am moving the routines and also making sure that sev-guest driver works with this change. So sev-guest driver will not need to call snp_setup_psp_messaging().
Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 311
Quoted 4 lines
drivers/virt/coco/sev-guest/sev-guest.c +4
-0+ } + + /* Skip VMPCK0 initialization as the key is already initialized during early boot */ + if (vmpck_id && aesgcm_expandkey(pdata->ctx, snp_dev->vmpck, VMPCK_KEY_LEN, AUTHTAG_LEN)) {See previous comment. The sev-guest driver should be setting up everything private to it no matter the vmpck to be used.
I will try this out, as for secure tsc the vmpck will be used once and the sequence number would have incremented. And VMPCK0 will be initialized twice.
Regards
Nikunj
PATCH v2 6/11 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 {void (*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
@@ -134,6 +134,7 @@ static void enc_status_change_prepare_noop(unsigned long vaddr, int npages, boolstatic bool enc_status_change_finish_noop (unsigned long vaddr,int npages,bool enc) {return false; }static bool enc_tlb_flush_required_noop (bool enc) {return false; }static bool enc_cache_flush_required_noop (void ) {return false; } +static void enc_init_noop (void ) { }struct x86_platform_ops x86_platform__ro_after_init = { .calibrate_cpu = native_calibrate_cpu_early,
@@ -155,6 +156,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
PATCH v2 7/11 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
@@ -1642,6 +1642,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 && (sev_status &MSR_AMD64_SNP_SECURE_TSC )) { +u64 tsc; + +if (exit_info_1) +return ES_OK ; + + tsc =rdtsc (); + regs->ax =UINT_MAX & tsc; + regs->dx =UINT_MAX & (tsc >>32 ); + +return ES_OK ; + } +ghcb_set_rcx (ghcb, regs->cx);if (exit_info_1) {ghcb_set_rax (ghcb, regs->ax);
Signature · 1 line
2.34.1
Re: [PATCH v2 07/11] x86/sev: Change TSC MSR behavior for Secure TSC enabled guests
Quoted 10 lines · Nikunj A Dadhania
Secure TSC enabled guests should not write
MSR_IA32_TSC(10H)register as the subsequent TSC value reads are undefined.MSR_IA32_TSCrelated accesses should not exit to the hypervisor for such guests.Accesses to
MSR_IA32_TSCneeds special handling in the #VC handler for the guests with Secure TSC enabled. Writes toMSR_IA32_TSCshould be ignored, and reads ofMSR_IA32_TSCshould return the result of the RDTSC instruction.Signed-off-by Nikunj A Dadhania <[email protected]>
Quoted 39 lines
--- arch/x86/kernel/sev.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)arch/x86/kernel/sev.c +24
-0@@ -1642,6 +1642,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 && (sev_status &MSR_AMD64_SNP_SECURE_TSC )) { +u64 tsc; + +if (exit_info_1) +return ES_OK ; + + tsc =rdtsc (); + regs->ax =UINT_MAX & tsc; + regs->dx =UINT_MAX & (tsc >>32 ); + +return ES_OK ; + } +ghcb_set_rcx (ghcb, regs->cx);if (exit_info_1) {ghcb_set_rax (ghcb, regs->ax);
PATCH v2 8/11 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 Security processor (PSP). This communication channel is encrypted between the security processor and the guest, hypervisor is just the conduit to deliver the guest messages to the security processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.
Moreover, the hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions are being intercepted. If this should occur and Secure TSC is enabled, terminate guest execution.
--- arch/x86/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev-shared.c | 7 +++ arch/x86/kernel/sev.c | 92 +++++++++++++++++++++++++++++--- arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 +++ 7 files changed, 131 insertions(+), 8 deletions(-)
arch/x86/include/asm/sev-guest.h +18 -0
@@ -37,6 +37,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 };
@@ -75,6 +77,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; +u64 tsc_factor; +u8 rsvd2[96 ]; +} __packed; +int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req);bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id);
arch/x86/include/asm/sev.h +2 -0
@@ -218,6 +218,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages);void snp_set_wakeup_secondary_cpu (void );bool snp_init (struct boot_params *bp);void __init __noreturn snp_abort (void ); +bool __init snp_secure_tsc_prepare (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }
@@ -238,6 +239,7 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npagstatic 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 bool __init snp_secure_tsc_prepare (void ) {return false; }static inline int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err) {
arch/x86/include/asm/svm.h +4 -2
@@ -404,7 +404,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;
@@ -536,7 +538,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-shared.c +7 -0
@@ -912,6 +912,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;
arch/x86/kernel/sev.c +86 -6
122 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 ();
@@ -501,6 +506,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; }void __init mem_encrypt_free_decrypted_mem (void )
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 v2 08/11] x86/sev: Add Secure TSC support for SNP guests
Quoted 7 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 Security processor (PSP).
s/Security processor (PSP)/AMD Secure Processor/
Quoted 1 line
This communication channel is encrypted between the security
here as well.
Quoted 1 line
processor and the guest, hypervisor is just the conduit to
s/hypervisor/the hypervisor/
Quoted 9 lines
deliver the guest messages to the security processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.
Moreover, 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.
This seems like a separate patch.
Quoted 32 lines
Signed-off-by Nikunj A Dadhania <[email protected]>--- 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-shared.c | 7 +++ arch/x86/kernel/sev.c | 92 +++++++++++++++++++++++++++++--- arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 +++ 7 files changed, 131 insertions(+), 8 deletions(-)arch/x86/include/asm/sev-guest.h +5
-0@@ -37,6 +37,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 };@@ -75,6 +77,22 @@ struct snp_guest_req {u8 msg_type; }; +struct snp_tsc_info_req { +#define SNP_TSC_INFO_REQ_SZ 128 +/* Must be zero filled */
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 92
Quoted 10 lines
arch/x86/include/asm/sev-guest.h +10
-0+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; +u64 tsc_factor;
This should be a u32 ...
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 93
Quoted 1 line
arch/x86/include/asm/sev-guest.h +1
-0+u8 rsvd2[96 ];
Which then makes this 100.
Quoted 151 lines
arch/x86/include/asm/sev-guest.h +2
-0+} __packed; +int snp_send_guest_request (struct snp_guest_dev *dev,struct snp_guest_req *req);bool snp_assign_vmpck (struct snp_guest_dev *dev,int vmpck_id);arch/x86/include/asm/sev.h +2
-0@@ -218,6 +218,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages);void snp_set_wakeup_secondary_cpu (void );bool snp_init (struct boot_params *bp);void __init __noreturn snp_abort (void ); +bool __init snp_secure_tsc_prepare (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }@@ -238,6 +239,7 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npagstatic 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 bool __init snp_secure_tsc_prepare (void ) {return false; }static inline int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err) {arch/x86/include/asm/svm.h +4
-2@@ -404,7 +404,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;@@ -536,7 +538,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-shared.c +7
-0@@ -912,6 +912,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;arch/x86/kernel/sev.c +61
-1@@ -72,6 +72,10 @@ static struct ghcb *boot_ghcb __section(".data");/* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features__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;@@ -1107,7 +1111,7 @@ static void *alloc_shared_pages(size_t sz)return page_address (page); } -static int snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) +static int __init snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) {u64 gpa;int ret;@@ -1406,6 +1410,80 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) }EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static int __init snp_get_tsc_info (void ) +{ +u8 buf[SNP_TSC_INFO_REQ_SZ +AUTHTAG_LEN ]; +struct snp_tsc_info_resp tsc_resp = {0 }; +struct snp_tsc_info_req tsc_req; +struct snp_guest_req req; +struct snp_guest_dev dev; +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 ; + +/* Zero the tsc_info_req */ +memzero_explicit (&tsc_req,sizeof (tsc_req)); +memzero_explicit (&req,sizeof (req)); + + dev.pdata = platform_data; +if (!snp_assign_vmpck (&dev,0 )) +return -EINVAL ; + + req.msg_version =MSG_HDR_VER ; + req.msg_type =SNP_MSG_TSC_INFO_REQ ; + req.req_buf = &tsc_req; + req.req_sz =sizeof (tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.fw_err =NULL ; + req.exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + rc =snp_send_guest_request (&dev, &req); +if (rc) + goto err_req; + +memcpy (&tsc_resp, buf,sizeof (tsc_resp)); +pr_debug ("%s: Valid response status %x scale %llx offset %llx factor %llx\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; +} + +bool __init snp_secure_tsc_prepare (void )
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1478
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+{ + platform_data =kzalloc (sizeof (*platform_data),GFP_KERNEL ); +if (!platform_data) +return false; + +/* Initialize the PSP channel to send snp messages */ +if (snp_setup_psp_messaging (platform_data)) +sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED ); + +if (cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) {
Should this be checked before allocating memory and calling snp_setup_psp_messaging()?
Also, I notice here you use the cc_platform_has() function but in previous patches you check sev_status directly. And you don't implement support for CC_ATTR_GUEST_SECURE_TSC until the last patch instead of now.
You can't get here until SNP_FEATURES_PRESENT is updated.
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1483
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+if (snp_get_tsc_info ()) +sev_es_terminate (SEV_TERM_SET_GEN ,GHCB_SNP_UNSUPPORTED ); + +pr_info ("SecureTSC enabled\n" ); + }
Blank line.
Thanks,
Tom
Quoted 75 lines
arch/x86/kernel/sev.c +10
-5+return true; +} +static int wakeup_cpu_via_vmgexit (int apic_id,unsigned long start_ip) {struct sev_es_save_area *cur_vmsa, *vmsa;@@ -1506,6 +1584,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) {@@ -2698,11 +2782,7 @@ static int __init snp_init_platform_device(void)if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))return -ENODEV ; - platform_data =kzalloc (sizeof (*platform_data),GFP_KERNEL ); -if (!platform_data) -return -ENOMEM ; - -if (snp_setup_psp_messaging (platform_data)) +if (!platform_data->ctx)return -ENODEV ;if (platform_device_add_data (&sev_guest_device, platform_data,sizeof (*platform_data)))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 ();@@ -501,6 +506,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; }void __init mem_encrypt_free_decrypted_mem (void )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
Re: [PATCH v2 08/11] x86/sev: Add Secure TSC support for SNP guests
Quoted 11 lines · Tom Lendacky
Quoted 7 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 Security processor (PSP).
s/Security processor (PSP)/AMD Secure Processor/
Quoted 8 lines
Quoted 1 line
This communication channel is encrypted between the security
here as well.
Quoted 1 line
processor and the guest, hypervisor is just the conduit to
s/hypervisor/the hypervisor/
Sure, will change commit message
Quoted 11 lines
Quoted 9 lines
deliver the guest messages to the security processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.
Moreover, 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.
This seems like a separate patch.
Sure.
Quoted 9 lines
Quoted 8 lines
Signed-off-by Nikunj A Dadhania <[email protected]>---
arch/x86/include/asm/sev-guest.h +0
-0
Quoted 15 lines
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 92
Quoted 12 lines
arch/x86/include/asm/sev-guest.h +12
-0+#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; + u64 tsc_factor;This should be a u32 ...
Right, will correct.
Quoted 3 lines
Quoted 1 line
+ u8 rsvd2[96];
Which then makes this 100.
Yes.
Quoted 5 lines
Quoted 4 lines
arch/x86/kernel/sev.c +0
-0
Quoted 13 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1478
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+bool __init snp_secure_tsc_prepare (void ) +{ + platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); + if (!platform_data) + return false; + + /* Initialize the PSP channel to send snp messages */ + if (snp_setup_psp_messaging(platform_data)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) {Should this be checked before allocating memory and calling
snp_setup_psp_messaging()?
No, as we need guest messaging to work without Secure TSC as well.
Quoted 2 lines
Also, I notice here you use the
cc_platform_has()function but in previous patches you checksev_statusdirectly.
For sev-shared.c, cc_platform_has() is not available when compiling boot/compressed.
I will change the below call site to cc_platfrom_has() after testing.
arch/x86/kernel/sev.c: if (regs->cx == MSR_IA32_TSC && (sev_status & MSR_AMD64_SNP_SECURE_TSC)) {
Quoted 2 lines
And you don't implement support for
CC_ATTR_GUEST_SECURE_TSCuntil the last patch instead of now.
This is to make sure that SECURE_TSC is enabled only after the last patch. As cc_platform_has() is being used at multiple places to enable the feature.
Quoted 9 lines
You can't get here until
SNP_FEATURES_PRESENTis updated.Quoted 5 lines
+ if (
snp_get_tsc_info())
+sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); +
+pr_info("SecureTSC enabled\n");
+ }Blank line.
Sure
Regards
Nikunj
Re: [PATCH v2 08/11] x86/sev: Add Secure TSC support for SNP guests
Quoted 89 lines · Nikunj A. Dadhania
Quoted 11 lines · Tom Lendacky
Quoted 7 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 Security processor (PSP).
s/Security processor (PSP)/AMD Secure Processor/
Quoted 8 lines
Quoted 1 line
This communication channel is encrypted between the security
here as well.
Quoted 1 line
processor and the guest, hypervisor is just the conduit to
s/hypervisor/the hypervisor/
Sure, will change commit message
Quoted 11 lines
Quoted 9 lines
deliver the guest messages to the security processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.
Moreover, 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.
This seems like a separate patch.
Sure.
Quoted 9 lines
Quoted 8 lines
Signed-off-by Nikunj A Dadhania <[email protected]>---
arch/x86/include/asm/sev-guest.h +0
-0Quoted 15 lines
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 92
Quoted 12 lines
arch/x86/include/asm/sev-guest.h +12
-0+#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; + u64 tsc_factor;This should be a u32 ...
Right, will correct.
Quoted 3 lines
Quoted 1 line
+ u8 rsvd2[96];
Which then makes this 100.
Yes.
Quoted 5 lines
Quoted 4 lines
arch/x86/kernel/sev.c +0
-0Quoted 13 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1478
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+bool __init snp_secure_tsc_prepare (void ) +{ + platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); + if (!platform_data) + return false; + + /* Initialize the PSP channel to send snp messages */ + if (snp_setup_psp_messaging(platform_data)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) {Should this be checked before allocating memory and calling
snp_setup_psp_messaging()?
Quoted 1 line
No, as we need guest messaging to work without Secure TSC as well.
My take is that the sev-guest driver should establish the key it is going to use at the time the driver is loaded. snp_setup_psp_messaging() should be called by whatever is going to use guest messaging.
Having a generic routine that is accessed by both the kernel and the driver should be the goal. Maybe it is best to only have the vmpck_id be part of any structure and then the appropriate key and sequence number are used based on that id when the request is made.
The sev_guest_platform_data struct can just hold context information (it doesn't need the secrets_gpa any more since everything is now in sev.c which knows what that value is) for whatever is using guest messaging.
Quoted 9 lines
Quoted 4 lines
Also, I notice here you use the
cc_platform_has()function but in previous patches you checksev_statusdirectly.For
sev-shared.c,cc_platform_has()is not available when compiling boot/compressed.I will change the below call site to
cc_platfrom_has()after testing.
Quoted 7 lines
arch/x86/kernel/sev.c: if (regs->cx==MSR_IA32_TSC&& (sev_status&MSR_AMD64_SNP_SECURE_TSC)) {Quoted 2 lines
And you don't implement support for
CC_ATTR_GUEST_SECURE_TSCuntil the last patch instead of now.This is to make sure that
SECURE_TSCis enabled only after the last patch. Ascc_platform_has()is being used at multiple places to enable the feature.
But you terminate long before that in snp_check_features() since you don't update SNP_FEATURES_PRESENT with SECURE_TSC until the last patch, right?
Thanks,
Tom
Quoted 16 lines
Quoted 10 lines
You can't get here until
SNP_FEATURES_PRESENTis updated.Quoted 5 lines
+ if (
snp_get_tsc_info())
+sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); +
+pr_info("SecureTSC enabled\n");
+ }Blank line.
Sure
Regards
Nikunj
Re: [PATCH v2 08/11] x86/sev: Add Secure TSC support for SNP guests
Quoted 90 lines · Tom Lendacky
Quoted 89 lines · Nikunj A. Dadhania
Quoted 11 lines · Tom Lendacky
Quoted 7 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 Security processor (PSP).
s/Security processor (PSP)/AMD Secure Processor/
Quoted 8 lines
Quoted 1 line
This communication channel is encrypted between the security
here as well.
Quoted 1 line
processor and the guest, hypervisor is just the conduit to
s/hypervisor/the hypervisor/
Sure, will change commit message
Quoted 11 lines
Quoted 9 lines
deliver the guest messages to the security processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.
Moreover, 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.
This seems like a separate patch.
Sure.
Quoted 9 lines
Quoted 8 lines
Signed-off-by Nikunj A Dadhania <[email protected]>---
arch/x86/include/asm/sev-guest.h +0
-0Quoted 15 lines
arch/x86/include/asm/sev-guest.h struct snp_guest_req { · line 92
Quoted 12 lines
arch/x86/include/asm/sev-guest.h +12
-0+#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; + u64 tsc_factor;This should be a u32 ...
Right, will correct.
Quoted 3 lines
Quoted 1 line
+ u8 rsvd2[96];
Which then makes this 100.
Yes.
Quoted 18 lines
Quoted 6 lines
arch/x86/kernel/sev.c +1
-0+bool __init snp_secure_tsc_prepare (void )arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1478
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+{ + platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); + if (!platform_data) + return false; + + /* Initialize the PSP channel to send snp messages */ + if (snp_setup_psp_messaging(platform_data)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) {Should this be checked before allocating memory and calling
snp_setup_psp_messaging()?
Quoted 3 lines
Quoted 1 line
No, as we need guest messaging to work without Secure TSC as well.
My take is that the sev-guest driver should establish the key it is going to use at the time the driver is loaded. >
snp_setup_psp_messaging()should be called by whatever is going to use guest messaging.
Should sev-guest also call snp_setup_psp_messaging() ?
As per the current design:
* snp_secure_tsc_prepare populates platform_data (probably I should rename it snp_prepare_psp_messaging)
* In snp_init_platform_device() platform_data is copied using platform_device_add_data().
sev-guest driver in sev_guest_probe() uses this platform_data populated by snp_secure_tsc_prepare().
Quoted 2 lines
Having a generic routine that is accessed by both the kernel and the driver should be the goal. Maybe it is best to only have the
vmpck_idbe part of any structure and then the appropriate key and sequence number are used based on that id when the request is made.
I understand what you are suggesting, let me think over this and I will come back.
Quoted 1 line
The
sev_guest_platform_datastruct can just hold context information (it doesn't need thesecrets_gpaany more since everything is now insev.cwhich knows what that value is) for whatever is using guest messaging.
We are using snp_assign_vmpck() in the sev-guest driver that is dependent on secrets_gpa, we can get rid of that dependency I think.
Quoted 11 lines
Quoted 11 lines
Quoted 2 lines
Also, I notice here you use the
cc_platform_has()function but in previous patches you checksev_statusdirectly.For
sev-shared.c,cc_platform_has()is not available when compiling boot/compressed.I will change the below call site to
cc_platfrom_has()after testing.arch/x86/kernel/sev.c: if (
regs->cx==MSR_IA32_TSC&& (sev_status&MSR_AMD64_SNP_SECURE_TSC)) {
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
And you don't implement support for
CC_ATTR_GUEST_SECURE_TSCuntil the last patch instead of now.This is to make sure that
SECURE_TSCis enabled only after the last patch. Ascc_platform_has()is being used at multiple places to enable the feature.But you terminate long before that in
snp_check_features()since you don't updateSNP_FEATURES_PRESENTwithSECURE_TSCuntil the last patch, right?
Yes, that correct. I will implement full support for CC_ATTR_GUEST_SECURE_TSC in this patch.
Regards
Nikunj
Re: [PATCH v2 08/11] x86/sev: Add Secure TSC support for SNP guests
In reply to Nikunj A DadhaniaQuoted 28 lines
arch/x86/kernel/sev.c +13
-1+/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -1107,7 +1111,7 @@ static void *alloc_shared_pages(size_t sz)return page_address (page); } -static int snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) +static int __init snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) {u64 gpa;int ret;@@ -1406,6 +1410,80 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) }EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static int __init snp_get_tsc_info (void ) +{ +u8 buf[SNP_TSC_INFO_REQ_SZ +AUTHTAG_LEN ]; +struct snp_tsc_info_resp tsc_resp = {0 }; +struct snp_tsc_info_req tsc_req; +struct snp_guest_req req; +struct snp_guest_dev dev; +int rc, resp_len; + +/* +* The intermediate response buffer is used while decrypting the
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1433
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+* 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 ; + +/* Zero the tsc_info_req */ +memzero_explicit (&tsc_req,sizeof (tsc_req)); +memzero_explicit (&req,sizeof (req));
Whats the guidance on when we should use memzero_explicit() vs just something like: snp_tsc_info_resp tsc_resp = {0};?
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1445
Quoted 12 lines
arch/x86/kernel/sev.c +12
-0+ + dev.pdata = platform_data; +if (!snp_assign_vmpck (&dev,0 )) +return -EINVAL ; + + req.msg_version =MSG_HDR_VER ; + req.msg_type =SNP_MSG_TSC_INFO_REQ ; + req.req_buf = &tsc_req; + req.req_sz =sizeof (tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.fw_err =NULL ;
Why do we not want the FW error code?
Quoted 22 lines
arch/x86/kernel/sev.c +22
-0+ req.exit_code =SVM_VMGEXIT_GUEST_REQUEST ; + rc =snp_send_guest_request (&dev, &req); +if (rc) + goto err_req; + +memcpy (&tsc_resp, buf,sizeof (tsc_resp)); +pr_debug ("%s: Valid response status %x scale %llx offset %llx factor %llx\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; +} +
Re: [PATCH v2 08/11] x86/sev: Add Secure TSC support for SNP guests
Quoted 33 lines · Peter Gonda
Quoted 33 lines
arch/x86/kernel/sev.c +18
-1+/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -1107,7 +1111,7 @@ static void *alloc_shared_pages(size_t sz)return page_address (page); } -static int snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) +static int __init snp_setup_psp_messaging (struct sev_guest_platform_data *pdata) {u64 gpa;int ret;@@ -1406,6 +1410,80 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) }EXPORT_SYMBOL_GPL (snp_assign_vmpck); +static int __init snp_get_tsc_info (void ) +{ +u8 buf[SNP_TSC_INFO_REQ_SZ +AUTHTAG_LEN ]; +struct snp_tsc_info_resp tsc_resp = {0 }; +struct snp_tsc_info_req tsc_req; +struct snp_guest_req req; +struct snp_guest_dev dev; +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)
Quoted 8 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1433
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+return -EINVAL ; + +/* Zero the tsc_info_req */ +memzero_explicit (&tsc_req,sizeof (tsc_req)); +memzero_explicit (&req,sizeof (req));Whats the guidance on when we should use
memzero_explicit()vs just something like:snp_tsc_info_resp tsc_resp = {0};?
Going over the history of memzero_explicit, it seems it was introduce to explicitly zero sensitive information before the variable goes out of scope. GCC was optimizing out the memset in these cases:
d4c5efdb9777 ("random: add and use memzero_explicit() for clearing data")
https://bugzilla.kernel.org/show_bug.cgi?id=82041
With the above detail, IMHO, we do not need the memzero_explicit() for both case.
Quoted 15 lines
arch/x86/kernel/sev.c bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) · line 1445
Quoted 13 lines
arch/x86/kernel/sev.c +12
-0+ + dev.pdata = platform_data; +if (!snp_assign_vmpck (&dev,0 )) +return -EINVAL ; + + req.msg_version =MSG_HDR_VER ; + req.msg_type =SNP_MSG_TSC_INFO_REQ ; + req.req_buf = &tsc_req; + req.req_sz =sizeof (tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.fw_err =NULL ;Why do we not want the FW error code?
I will add the FW error code.
Regards
Nikunj
PATCH v2 9/11 x86/kvmclock: Use Secure TSC as clock if available
In reply to Nikunj A DadhaniaFor AMD SNP guests having Secure TSC enabled, use Secure TSC based clocksource instead of kvmclock.
--- 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 v2 09/11] x86/kvmclock: Use Secure TSC as clock if available
Quoted 2 lines · Nikunj A Dadhania
For AMD SNP guests having Secure TSC enabled, use Secure TSC based clocksource instead of kvmclock.
This patch only results in skipping the initialization of KVM clock, it doesn't cause Secure TSC to be used as the clocksource.
Thanks,
Tom
Quoted 19 lines
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 +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 )) {
Re: [PATCH v2 09/11] x86/kvmclock: Use Secure TSC as clock if available
Quoted 6 lines · Tom Lendacky
Quoted 2 lines · Nikunj A Dadhania
For AMD SNP guests having Secure TSC enabled, use Secure TSC based clocksource instead of kvmclock.
This patch only results in skipping the initialization of KVM clock, it doesn't cause Secure TSC to be used as the clocksource.
Will update the commit message and reorder with 10/11. After making TSC reliable and skipping kvmclock, secure tsc is used as clocksource.
Regards
Nikunj
PATCH v2 10/11 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
@@ -1221,7 +1221,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
PATCH v2 11/11 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 | 2 +- arch/x86/coco/core.c | 3 +++ arch/x86/mm/mem_encrypt.c | 10 ++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev.c +1 -1
@@ -315,7 +315,7 @@ 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 (0) +#define SNP_FEATURES_PRESENT (MSR_AMD64_SNP_SECURE_TSC)void snp_check_features (void ) {
arch/x86/coco/core.c +3 -0
@@ -68,6 +68,9 @@ static bool 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/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