Add Secure TSC support for SNP guests
v9 cover Add Secure TSC support for SNP guests
This patchset is also available at:
https://github.com/AMDESE/linux-kvm/tree/sectsc-guest-latest
and is based on commit: 1613e604df0c ("Linux 6.10-rc1")
Overview
--------
Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC".
During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at
https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info"
Use a minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the AMD Security Processor which is available at early boot.
SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before smp cpus are started, move most of the sev-guest driver code to kernel/sev.c and provide well defined APIs to the sev-guest driver to use the interface to avoid code-duplication.
Patches:
01-09: Preparatory patches for code movement and general cleanup/fixups 10-13: Patches moving SNP guest messaging code from SEV guest driver to
SEV common code
14-16: Error handling and caching secrets page
17-24: SecureTSC enablement patches.
Testing SecureTSC
-----------------
SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series:
https://github.com/AMDESE/linux-kvm/tree/sectsc-host-latest
QEMU changes:
https://github.com/nikunjad/qemu/tree/snp-securetsc-latest
QEMU commandline SEV-SNP with SecureTSC:
qemu-system-x86_64 -cpu EPYC-Milan-v2,+invtsc -smp 4 \
-object memory-backend-memfd,id=ram1,size=1G,share=true,prealloc=false,reserve=false \
-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \
-machine q35,confidential-guest-support=sev0,memory-backend=ram1 \
...Changelog:
----------
v9:
* Added Acked-by/Reviewed-by
* Removed Reviewed-by/Tested-by from patches that had significant changes
* Added patch to make payload a variable length array in snp_guest_msg
* Fix all your user-visible strings (vmpck => VMPCK) and readabilty.
* Separated patch for message sequence handling
* Handle failures from snp_init() in sme_enable()
* Preparatory patches:
* Carved out simplify VMPCK and OS message sequence changes
* Carved out SNP guest messaging init/exit for proper initialization and cleanup.
* Move SNP command mutex down and make it private to sev.c
* Pure code movement patch and subsequent code changes patches
* Drop patches adding guest initiation hook enc_init() and call snp_secure_tsc_prepare() from mem_encrypt.c
* Use CC_ATTR_GUEST_SECURE_TSC and drop synthetic SecureTSC feature bit
v8: https://lore.kernel.org/lkml/[email protected]/
* Rebased on top of tip/x86/sev
* Use minimum size of IV or msg_seqno in memcpy
* Use arch/x86/include/asm/sev.h instead of sev-guest.h
* Use DEFINE_MUTEX for snp_guest_cmd_mutex
* Added Reviewed-by from Tom.
* Dropped Tested-by from patch 3/16
v7: https://lore.kernel.org/kvm/[email protected]/
v6: https://lore.kernel.org/lkml/[email protected]/
v5: https://lore.kernel.org/lkml/[email protected]/
v4: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/r/[email protected]/
v1: https://lore.kernel.org/r/[email protected]
Nikunj A Dadhania (24):
virt: sev-guest: Use AES GCM crypto library
virt: sev-guest: Replace dev_dbg with pr_debug
virt: sev-guest: Make payload a variable length array
virt: sev-guest: Add SNP guest request structure
virt: sev-guest: Fix user-visible strings
virt: sev-guest: Simplify VMPCK and sequence number assignments
virt: sev-guest: Store VMPCK index to SNP guest device structure
virt: sev-guest: Take mutex in snp_send_guest_request()
virt: sev-guest: Carve out SNP guest messaging init/exit x86/sev: Move core SEV guest driver routines to common code x86/sev: Replace dev_[err,alert] with pr_[err,alert] x86/sev: Make snp_issue_guest_request() static x86/sev: Make sev-guest driver functional again x86/sev: Handle failures from snp_init()
x86/sev: Cache the secrets page address
x86/sev: Drop sev_guest_platform_data structure x86/cc: Add CC_ATTR_GUEST_SECURE_TSC
x86/sev: Add Secure TSC support for SNP guests x86/sev: Change TSC MSR behavior for Secure TSC enabled guests x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled
guests
x86/kvmclock: Skip kvmclock when Secure TSC is available x86/sev: Mark Secure TSC as reliable
x86/cpu/amd: Do not print FW_BUG for Secure TSC x86/sev: Enable Secure TSC for SNP guests
arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 197 ++++++- arch/x86/include/asm/svm.h | 6 +- drivers/virt/coco/sev-guest/sev-guest.h | 63 --- include/linux/cc_platform.h | 8 + arch/x86/boot/compressed/sev.c | 3 +- arch/x86/coco/core.c | 3 + arch/x86/kernel/cpu/amd.c | 3 +- arch/x86/kernel/kvmclock.c | 2 +- arch/x86/kernel/sev-shared.c | 10 + arch/x86/kernel/sev.c | 581 +++++++++++++++++-- arch/x86/mm/mem_encrypt.c | 4 + arch/x86/mm/mem_encrypt_amd.c | 4 + arch/x86/mm/mem_encrypt_identity.c | 7 + drivers/virt/coco/sev-guest/sev-guest.c | 711 +++--------------------- arch/x86/Kconfig | 1 + drivers/virt/coco/sev-guest/Kconfig | 3 - 17 files changed, 843 insertions(+), 764 deletions(-) delete mode 100644 drivers/virt/coco/sev-guest/sev-guest.h
base-commit: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0
Signature · 1 line
2.34.1
PATCH v9 1/24 virt: sev-guest: Use AES GCM crypto library
The sev-guest driver encryption code uses Crypto API for SNP guest messaging to interact with AMD Security processor. For enabling SecureTSC, SEV-SNP guests need to send a TSC_INFO request guest message before the smpboot phase starts. Details from the TSC_INFO response will be used to program the VMSA before the secondary CPUs are brought up. The Crypto API is not available this early in the boot phase.
In preparation of moving the encryption code out of sev-guest driver to support SecureTSC and make reviewing the diff easier, start using AES GCM library implementation instead of Crypto API.
Drop __enc_payload() and dec_payload() helpers as both are pretty small and can be moved to the respective callers.
--- drivers/virt/coco/sev-guest/sev-guest.h | 3 + drivers/virt/coco/sev-guest/sev-guest.c | 175 ++++++------------------ drivers/virt/coco/sev-guest/Kconfig | 4 +- 3 files changed, 43 insertions(+), 139 deletions(-)
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 {
drivers/virt/coco/sev-guest/sev-guest.c +39 -136
295 lines not on this page · raw message
drivers/virt/coco/sev-guest/Kconfig +1 -3
@@ -2,9 +2,7 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - select CRYPTO - select CRYPTO_AEAD2 - select CRYPTO_GCM + select CRYPTO_LIB_AESGCM select TSM_REPORTS help SEV-SNP firmware provides the guest a mechanism to communicate with
Signature · 1 line
2.34.1
Borislav Petkov
maintainerRe: [PATCH v9 01/24] virt: sev-guest: Use AES GCM crypto library
Quoted 7 lines · Nikunj A Dadhania
The sev-guest driver encryption code uses Crypto API for SNP guest messaging to interact with AMD Security processor. For enabling SecureTSC, SEV-SNP guests need to send a
TSC_INFOrequest guest message before the smpboot phase starts. Details from theTSC_INFOresponse will be used to program the VMSA before the secondary CPUs are brought up. The Crypto API is not available this early in the boot phase.
Quoted 6 lines
In preparation of moving the encryption code out of sev-guest driver to support SecureTSC and make reviewing the diff easier, start using AES GCM library implementation instead of Crypto API.
Drop
__enc_payload()anddec_payload()helpers as both are pretty small and can be moved to the respective callers.
Please use this streamlined commit message for your next submission:
"The sev-guest driver encryption code uses the crypto API for SNP guest messaging with the AMD Security processor. In order to enable secure TSC, SEV-SNP guests need to send such a TSC_INFO message before the APs are booted. Details from the TSC_INFO response will then be used to program the VMSA before the APs are brought up.
However, the crypto API is not available this early in the boot process.
In preparation for moving the encryption code out of sev-guest to support secure TSC and to ease review, switch to using the AES GCM library implementation instead.
Drop __enc_payload() and dec_payload() helpers as both are small and can be moved to the respective callers."
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v9 01/24] virt: sev-guest: Use AES GCM crypto library
Quoted 23 lines · Borislav Petkov
Quoted 7 lines · Nikunj A Dadhania
The sev-guest driver encryption code uses Crypto API for SNP guest messaging to interact with AMD Security processor. For enabling SecureTSC, SEV-SNP guests need to send a
TSC_INFOrequest guest message before the smpboot phase starts. Details from theTSC_INFOresponse will be used to program the VMSA before the secondary CPUs are brought up. The Crypto API is not available this early in the boot phase.Quoted 6 lines
In preparation of moving the encryption code out of sev-guest driver to support SecureTSC and make reviewing the diff easier, start using AES GCM library implementation instead of Crypto API.
Drop
__enc_payload()anddec_payload()helpers as both are pretty small and can be moved to the respective callers.Please use this streamlined commit message for your next submission:
"The sev-guest driver encryption code uses the crypto API for SNP guest messaging with the AMD Security processor. In order to enable secure TSC, SEV-SNP guests need to send such a
TSC_INFOmessage before the APs are booted. Details from theTSC_INFOresponse will then be used to program the VMSA before the APs are brought up.
Quoted 9 lines
However, the crypto API is not available this early in the boot process.
In preparation for moving the encryption code out of sev-guest to support secure TSC and to ease review, switch to using the AES GCM library implementation instead.
Drop
__enc_payload()anddec_payload()helpers as both are small and can be moved to the respective callers."
Sure
Regards
Nikunj
PATCH v9 2/24 virt: sev-guest: Replace dev_dbg with pr_debug
In reply to Nikunj A DadhaniaIn preparation of moving code to arch/x86/kernel/sev.c, replace dev_dbg with pr_debug.
--- drivers/virt/coco/sev-guest/sev-guest.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +5 -4
@@ -178,8 +178,9 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload,struct aesgcm_ctx *ctx = snp_dev->ctx;u8 iv[GCM_AES_IV_SIZE ] = {}; -dev_dbg (snp_dev->dev,"response [seqno %lld type %d version %d sz %d]\n" , - resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz); +pr_debug ("response [seqno %lld type %d version %d sz %d]\n" , + resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, + resp_hdr->msg_sz);/* Copy response from shared memory to encrypted memory. */ memcpy (resp, snp_dev->response,sizeof (*resp));
@@ -232,8 +233,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8if (!hdr->msg_seqno)return -ENOSR ; -dev_dbg (snp_dev->dev,"request [seqno %lld type %d version %d sz %d]\n" , - hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); +pr_debug ("request [seqno %lld type %d version %d sz %d]\n" , + hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);if (WARN_ON ((sz + ctx->authsize) >sizeof (req->payload)))return -EBADMSG ;
Signature · 1 line
2.34.1
PATCH v9 3/24 virt: sev-guest: Make payload a variable length array
In reply to Nikunj A DadhaniaCurrently, guest message is PAGE_SIZE bytes and payload is hard-coded to 4000 bytes, assuming snp_guest_msg_hdr structure as 96 bytes.
Remove the structure size assumption and hard-coding of payload size and instead use variable length array.
While at it, rename the local guest message variables for clarity.
--- drivers/virt/coco/sev-guest/sev-guest.h | 5 +- drivers/virt/coco/sev-guest/sev-guest.c | 74 +++++++++++++++---------- 2 files changed, 48 insertions(+), 31 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.h +4 -1
@@ -60,7 +60,10 @@ struct snp_guest_msg_hdr {struct snp_guest_msg {struct snp_guest_msg_hdr hdr; -u8 payload[4000 ]; +u8 payload[]; } __packed; +#define SNP_GUEST_MSG_SIZE 4096 +#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg)) +#endif /* __VIRT_SEVGUEST_H__ */
drivers/virt/coco/sev-guest/sev-guest.c +44 -30
158 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 10 lines · Nikunj A Dadhania
Currently, guest message is
PAGE_SIZEbytes and payload is hard-coded to 4000 bytes, assumingsnp_guest_msg_hdrstructure as 96 bytes.Remove the structure size assumption and hard-coding of payload size and instead use variable length array.
While at it, rename the local guest message variables for clarity.
Signed-off-by Nikunj A Dadhania <[email protected]>Suggested-by Tom Lendacky <[email protected]>
Quoted 5 lines
--- drivers/virt/coco/sev-guest/sev-guest.h | 5 +- drivers/virt/coco/sev-guest/sev-guest.c | 74 +++++++++++++++---------- 2 files changed, 48 insertions(+), 31 deletions(-)
Borislav Petkov
maintainerRe: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
In reply to Nikunj A DadhaniaQuoted 5 lines · Nikunj A Dadhania
Currently, guest message is
PAGE_SIZEbytes and payload is hard-coded to 4000 bytes, assumingsnp_guest_msg_hdrstructure as 96 bytes.Remove the structure size assumption and hard-coding of payload size and instead use variable length array.
I don't understand here what hard-coding is being removed?
It is simply done differently:
from
Quoted 1 line
-
snp_dev->request=alloc_shared_pages(dev, sizeof(struct snp_guest_msg));
to
Quoted 1 line
+
snp_dev->request=alloc_shared_pages(dev, SNP_GUEST_MSG_SIZE);
Maybe I'm missing the point here but do you mean by removing the hard-coding this:
+#define SNP_GUEST_MSG_SIZE 4096
+#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg))
where the msg payload size will get computed at build time and you won't have to do that 4000 in the struct definition:
u8 payload[4000];
?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 25 lines · Borislav Petkov
Quoted 5 lines · Nikunj A Dadhania
Currently, guest message is
PAGE_SIZEbytes and payload is hard-coded to 4000 bytes, assumingsnp_guest_msg_hdrstructure as 96 bytes.Remove the structure size assumption and hard-coding of payload size and instead use variable length array.
I don't understand here what hard-coding is being removed?
It is simply done differently:
from
Quoted 1 line
-
snp_dev->request=alloc_shared_pages(dev, sizeof(struct snp_guest_msg));to
Quoted 1 line
+
snp_dev->request=alloc_shared_pages(dev, SNP_GUEST_MSG_SIZE);Maybe I'm missing the point here but do you mean by removing the hard-coding this:
drivers/virt/coco/sev-guest/sev-guest.h +2
-0+#define SNP_GUEST_MSG_SIZE 4096 +#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg))
Quoted 6 lines
where the msg payload size will get computed at build time and you won't have to do that 4000 in the struct definition:
u8 payload[4000];?
Yes, payload was earlier fixed at 4000 bytes, without considering the size of snp_guest_msg.
Regards
Nikunj
Borislav Petkov
maintainerRe: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 2 lines · Nikunj A. Dadhania
Yes, payload was earlier fixed at 4000 bytes, without considering the size of
snp_guest_msg.
Sorry, you'd need to try explaining this again. Who wasn't considering the size of snp_guest_msg?
AFAICT, the code currently does sizeof(struct snp_guest_msg) which contains both the header *and* the payload.
What could help is if you structure your commit message this way:
1. Prepare the context for the explanation briefly.
2. Explain the problem at hand.
3. "It happens because of <...>"
4. "Fix it by doing X"
5. "(Potentially do Y)."
And some of those above are optional depending on the issue being explained.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Nikunj A. Dadhania
Yes, payload was earlier fixed at 4000 bytes, without considering the size of
snp_guest_msg.Sorry, you'd need to try explaining this again. Who wasn't considering the size of
snp_guest_msg?
Sorry, I meant snp_guest_msg_hdr here.
snp_guest_msg includes header and payload. There is an implicit assumption that the snp_guest_msg_hdr will always be 96 bytes, and with that assumption the payload array size is set to 4000 bytes magic number.
Quoted 4 lines
AFAICT, the code currently does
sizeof(struct snp_guest_msg)which contains both the header *and* the payload.What could help is if you structure your commit message this way:
How about the below commit message:
-----------------------------------------------------------------------
Currently, snp_guest_msg includes a message header (96 bytes) and a payload (4000 bytes). There is an implicit assumption here that the SNP message header will always be 96 bytes, and with that assumption the payload array size has been set to 4000 bytes magic number. If any new member is added to the SNP message header, the SNP guest message will span more than a page.
Instead of using magic number '4000' for the payload array in the snp_guest_msg structure, use a variable length array for payload. Allocate snp_guest_msg of constant size (SNP_GUEST_MSG_SIZE=4096). This will ensure that message size won't grow beyond the page size even if the message header size increases. Also, add SNP_GUEST_MSG_PAYLOAD_SIZE for checking buffer over runs.
While at it, rename the local guest message variables for clarity.
-----------------------------------------------------------------------
Regards
Nikunj
Re: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
In reply to Nikunj A. DadhaniaQuoted 26 lines · Nikunj A. Dadhania
Quoted 25 lines · Borislav Petkov
Quoted 5 lines · Nikunj A Dadhania
Currently, guest message is
PAGE_SIZEbytes and payload is hard-coded to 4000 bytes, assumingsnp_guest_msg_hdrstructure as 96 bytes.Remove the structure size assumption and hard-coding of payload size and instead use variable length array.
I don't understand here what hard-coding is being removed?
It is simply done differently:
from
Quoted 1 line
-
snp_dev->request=alloc_shared_pages(dev, sizeof(struct snp_guest_msg));to
Quoted 1 line
+
snp_dev->request=alloc_shared_pages(dev, SNP_GUEST_MSG_SIZE);Maybe I'm missing the point here but do you mean by removing the hard-coding this:
drivers/virt/coco/sev-guest/sev-guest.h +2
-0+#define SNP_GUEST_MSG_SIZE 4096 +#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg))
Quoted 8 lines
Quoted 6 lines
where the msg payload size will get computed at build time and you won't have to do that 4000 in the struct definition:
u8 payload[4000];?
Yes, payload was earlier fixed at 4000 bytes, without considering the size of
snp_guest_msg.
An alternative to the #defines would be something like:
struct snp_guest_msg {
struct snp_guest_msg_hdr hdr;
u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)]; } __packed;
Not sure it matters, but does reduce the changes while ensuring the payload plus header doesn't exceed a page.
Thanks,
Tom
Quoted 3 lines
Regards
Nikunj
Borislav Petkov
maintainerRe: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 9 lines · Tom Lendacky
An alternative to the #defines would be something like:
struct snp_guest_msg { struct snp_guest_msg_hdr hdr; u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];}
__packed;Not sure it matters, but does reduce the changes while ensuring the payload plus header doesn't exceed a page.
Yeah, because that would've been my next question - the requirement to keep it <= PAGE_SIZE.
So yeah, Nikunj, please do that also and add a
BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE);
somewhere in the driver to catch all kinds of funky stuff.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 10 lines · Borislav Petkov
Quoted 9 lines · Tom Lendacky
An alternative to the #defines would be something like:
struct snp_guest_msg { struct snp_guest_msg_hdr hdr; u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];}
__packed;Not sure it matters, but does reduce the changes while ensuring the payload plus header doesn't exceed a page.
Yes, it does reduce a lot of churn.
Quoted 9 lines
Yeah, because that would've been my next question - the requirement to keep it <=
PAGE_SIZE.So yeah, Nikunj, please do that also and add a
BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE);somewhere in the driver to catch all kinds of funky stuff.
Sure, here is the new patch. I have separated the variable name changes to a new patch.
Subject: [PATCH] virt: sev-guest: Ensure the SNP guest messages do not exceed
a page
Currently, snp_guest_msg includes a message header (96 bytes) and a payload (4000 bytes). There is an implicit assumption here that the SNP message header will always be 96 bytes, and with that assumption the payload array size has been set to 4000 bytes magic number. If any new member is added to the SNP message header, the SNP guest message will span more than a page.
Instead of using magic number '4000' for the payload, declare the snp_guest_msg in a way that payload plus the message header do not exceed a page.
--- drivers/virt/coco/sev-guest/sev-guest.h | 2 +- drivers/virt/coco/sev-guest/sev-guest.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/virt/coco/sev-guest/sev-guest.h +1 -1
@@ -60,7 +60,7 @@ struct snp_guest_msg_hdr {struct snp_guest_msg {struct snp_guest_msg_hdr hdr; -u8 payload[4000 ]; +u8 payload[PAGE_SIZE -sizeof (struct snp_guest_msg_hdr )]; } __packed;#endif /* __VIRT_SEVGUEST_H__ */
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
@@ -1033,6 +1033,9 @@ static int __init sev_guest_probe(struct platform_device *pdev) snp_dev->dev = dev; snp_dev->secrets = secrets; +/* Ensure SNP guest messages do not span more than a page */ +BUILD_BUG_ON (sizeof (struct snp_guest_msg ) >PAGE_SIZE ); +/* 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)
Signature · 1 line
2.34.1
Re: [PATCH v9 03/24] virt: sev-guest: Make payload a variable length array
Quoted 1 line · Nikunj A. Dadhania
Sure, here is the new patch. I have separated the variable name changes to a new patch.
Right, please next time you send your set, sort the cleanups to sev-guest first so that I can pick them up separately.
Quoted 41 lines
Subject: [PATCH] virt: sev-guest: Ensure the SNP guest messages do not exceed
a pageCurrently,
snp_guest_msgincludes a message header (96 bytes) and a payload (4000 bytes). There is an implicit assumption here that the SNP message header will always be 96 bytes, and with that assumption the payload array size has been set to 4000 bytes magic number. If any new member is added to the SNP message header, the SNP guest message will span more than a page.Instead of using magic number '4000' for the payload, declare the
snp_guest_msgin a way that payload plus the message header do not exceed a page.Signed-off-by Nikunj A Dadhania <[email protected]>Suggested-by Tom Lendacky <[email protected]>--- drivers/virt/coco/sev-guest/sev-guest.h | 2 +- drivers/virt/coco/sev-guest/sev-guest.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)drivers/virt/coco/sev-guest/sev-guest.h +1
-1@@ -60,7 +60,7 @@ struct snp_guest_msg_hdr {struct snp_guest_msg {struct snp_guest_msg_hdr hdr; -u8 payload[4000 ]; +u8 payload[PAGE_SIZE -sizeof (struct snp_guest_msg_hdr )]; } __packed;#endif /* __VIRT_SEVGUEST_H__ */ drivers/virt/coco/sev-guest/sev-guest.c +0
-0
drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 1041
Quoted 9 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-0@@ -1033,6 +1033,9 @@ static int __init sev_guest_probe(struct platform_device *pdev) snp_dev->dev = dev; snp_dev->secrets = secrets; +/* Ensure SNP guest messages do not span more than a page */ +BUILD_BUG_ON (sizeof (struct snp_guest_msg ) >PAGE_SIZE ); +/* 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)
Yap, that's exactly how stuff like that should be done:
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v9 4/24 virt: sev-guest: Add SNP guest request structure
In reply to Nikunj A DadhaniaAdd a snp_guest_req structure to simplify the function arguments. This structure will be used to call the SNP Guest message request API instead of passing a long list of parameters.
Update the snp_issue_guest_request() prototype to include the new guest request structure and move all the sev-guest.h header content to sev.h.
--- arch/x86/include/asm/sev.h | 78 ++++++++++- drivers/virt/coco/sev-guest/sev-guest.h | 69 ---------- arch/x86/kernel/sev.c | 15 ++- drivers/virt/coco/sev-guest/sev-guest.c | 169 +++++++++++++----------- 4 files changed, 177 insertions(+), 154 deletions(-) delete mode 100644 drivers/virt/coco/sev-guest/sev-guest.h
arch/x86/include/asm/sev.h +74 -4
@@ -112,8 +112,6 @@ struct rmp_state {struct snp_req_data {unsigned long req_gpa;unsigned long resp_gpa; -unsigned long data_gpa; -unsigned int data_npages; };struct sev_guest_platform_data {
@@ -155,6 +153,76 @@ struct snp_secrets_page {u8 rsvd3[3840 ]; } __packed; +#define MAX_AUTHTAG_LEN 32 +#define AUTHTAG_LEN 16 +#define AAD_LEN 48 +#define MSG_HDR_VER 1 + +/* See SNP spec SNP_GUEST_REQUEST section for the structure */ +enum msg_type { +SNP_MSG_TYPE_INVALID =0 , +SNP_MSG_CPUID_REQ , +SNP_MSG_CPUID_RSP , +SNP_MSG_KEY_REQ , +SNP_MSG_KEY_RSP , +SNP_MSG_REPORT_REQ , +SNP_MSG_REPORT_RSP , +SNP_MSG_EXPORT_REQ , +SNP_MSG_EXPORT_RSP , +SNP_MSG_IMPORT_REQ , +SNP_MSG_IMPORT_RSP , +SNP_MSG_ABSORB_REQ , +SNP_MSG_ABSORB_RSP , +SNP_MSG_VMRK_REQ , +SNP_MSG_VMRK_RSP , + +SNP_MSG_TYPE_MAX +}; + +enum aead_algo { +SNP_AEAD_INVALID , +SNP_AEAD_AES_256_GCM , +}; + +struct snp_guest_msg_hdr { +u8 authtag[MAX_AUTHTAG_LEN ]; +u64 msg_seqno; +u8 rsvd1[8 ]; +u8 algo; +u8 hdr_version; +u16 hdr_sz; +u8 msg_type; +u8 msg_version; +u16 msg_sz; +u32 rsvd2; +u8 msg_vmpck; +u8 rsvd3[35 ]; +} __packed; + +struct snp_guest_msg { +struct snp_guest_msg_hdr hdr; +u8 payload[]; +} __packed; + +#define SNP_GUEST_MSG_SIZE 4096 +#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg)) + +struct snp_guest_req { +void *req_buf; +size_t req_sz; + +void *resp_buf; +size_t resp_sz; + +void *data; +size_t data_npages; + +u64 exit_code; +unsigned int vmpck_id; +u8 msg_version; +u8 msg_type; +}; +#ifdef CONFIG_AMD_MEM_ENCRYPT extern void __sev_es_ist_enter (struct pt_regs *regs);extern void __sev_es_ist_exit (void );
@@ -224,7 +292,8 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void __noreturn snp_abort (void );void snp_dmi_setup (void ); -int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,struct snp_guest_request_ioctl *rio); +int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, +struct snp_guest_request_ioctl *rio);void snp_accept_memory (phys_addr_t start,phys_addr_t end);u64 snp_get_unsupported_features (u64 status);u64 sev_get_status (void );
@@ -249,7 +318,8 @@ static inline void snp_set_wakeup_secondary_cpu(void) { }static inline bool snp_init (struct boot_params *bp) {return false; }static inline void snp_abort (void ) { }static inline void snp_dmi_setup (void ) { } -static inline int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,struct snp_guest_request_ioctl *rio) +static inline int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, +struct snp_guest_request_ioctl *rio) {return -ENOTTY ; }
drivers/virt/coco/sev-guest/sev-guest.h deleted +0 -69
69 lines not on this page · raw message
arch/x86/kernel/sev.c +8 -7
@@ -2191,7 +2191,8 @@ static int __init init_sev_config(char *str) }__setup ("sev=" , init_sev_config); -int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,struct snp_guest_request_ioctl *rio) +int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, +struct snp_guest_request_ioctl *rio) {struct ghcb_state state;struct es_em_ctxt ctxt;
@@ -2215,12 +2216,12 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snvc_ghcb_invalidate (ghcb); -if (exit_code ==SVM_VMGEXIT_EXT_GUEST_REQUEST ) { -ghcb_set_rax (ghcb, input->data_gpa); -ghcb_set_rbx (ghcb, input->data_npages); +if (req->exit_code ==SVM_VMGEXIT_EXT_GUEST_REQUEST ) { +ghcb_set_rax (ghcb,__pa (req->data)); +ghcb_set_rbx (ghcb, req->data_npages); } - ret =sev_es_ghcb_hv_call (ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa); + ret =sev_es_ghcb_hv_call (ghcb, &ctxt, req->exit_code, input->req_gpa, input->resp_gpa);if (ret) goto e_put;
@@ -2235,8 +2236,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sncase SNP_GUEST_VMM_ERR (SNP_GUEST_VMM_ERR_INVALID_LEN ):/* Number of expected pages are returned in RBX */ -if (exit_code ==SVM_VMGEXIT_EXT_GUEST_REQUEST ) { - input->data_npages =ghcb_get_rbx (ghcb); +if (req->exit_code ==SVM_VMGEXIT_EXT_GUEST_REQUEST ) { + req->data_npages =ghcb_get_rbx (ghcb); ret = -ENOSPC ;break ; }
drivers/virt/coco/sev-guest/sev-guest.c +95 -74
372 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v9 04/24] virt: sev-guest: Add SNP guest request structure
Quoted 8 lines · Nikunj A Dadhania
Add a
snp_guest_reqstructure to simplify the function arguments. This structure will be used to call the SNP Guest message request API instead of passing a long list of parameters.Update the
snp_issue_guest_request()prototype to include the new guest request structure and move all thesev-guest.hheader content tosev.h.Signed-off-by Nikunj A Dadhania <[email protected]>
Quoted 8 lines
--- arch/x86/include/asm/sev.h | 78 ++++++++++- drivers/virt/coco/sev-guest/sev-guest.h | 69 ---------- arch/x86/kernel/sev.c | 15 ++- drivers/virt/coco/sev-guest/sev-guest.c | 169 +++++++++++++----------- 4 files changed, 177 insertions(+), 154 deletions(-) delete mode 100644 drivers/virt/coco/sev-guest/sev-guest.h
PATCH v9 5/24 virt: sev-guest: Fix user-visible strings
In reply to Nikunj A DadhaniaUser-visible abbreviations should be in capitals, ensure messages are readable and clear.
No functional change.
--- drivers/virt/coco/sev-guest/sev-guest.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +4 -4
@@ -95,7 +95,7 @@ static bool is_vmpck_empty(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" , +dev_alert (snp_dev->dev,"Disabling VMPCK%d to prevent IV reuse.\n" , vmpck_id);memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); snp_dev->vmpck =NULL ;
@@ -849,13 +849,13 @@ static int __init sev_guest_probe(struct platform_device *pdev) ret = -EINVAL ; snp_dev->vmpck =get_vmpck (vmpck_id, secrets, &snp_dev->os_area_msg_seqno);if (!snp_dev->vmpck) { -dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); +dev_err (dev,"Invalid VMPCK%d communication key\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ if (is_vmpck_empty (snp_dev)) { -dev_err (dev,"vmpck id %d is null\n" , vmpck_id); +dev_err (dev,"Empty VMPCK%d communication key\n" , vmpck_id); goto e_unmap; }
@@ -911,7 +911,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)if (ret) goto e_free_ctx; -dev_info (dev,"Initialized SEV guest driver (using vmpck_id %d)\n" , vmpck_id); +dev_info (dev,"Initialized SEV guest driver (using VMPCK%d communication key)\n" , vmpck_id);return 0 ; e_free_ctx:
Signature · 1 line
2.34.1
Re: [PATCH v9 05/24] virt: sev-guest: Fix user-visible strings
Quoted 15 lines · Nikunj A Dadhania
User-visible abbreviations should be in capitals, ensure messages are readable and clear.
No functional change.
Signed-off-by Nikunj A Dadhania <[email protected]>--- drivers/virt/coco/sev-guest/sev-guest.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/virt/coco/sev-guest/sev-guest.c +0
-0
drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 98
Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +1
-1@@ -95,7 +95,7 @@ static bool is_vmpck_empty(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", + dev_alert(snp_dev->dev, "Disabling VMPCK%d to prevent IV reuse.\n",
You use "communication key" after each VMPCK%d below. I prefer this shorter notation, but whichever way it goes, they should all be the same.
Thanks,
Tom
Quoted 28 lines
drivers/virt/coco/sev-guest/sev-guest.c +3
-3vmpck_id);memzero_explicit (snp_dev->vmpck,VMPCK_KEY_LEN ); snp_dev->vmpck =NULL ;@@ -849,13 +849,13 @@ static int __init sev_guest_probe(struct platform_device *pdev) ret = -EINVAL ; snp_dev->vmpck =get_vmpck (vmpck_id, secrets, &snp_dev->os_area_msg_seqno);if (!snp_dev->vmpck) { -dev_err (dev,"invalid vmpck id %d\n" , vmpck_id); +dev_err (dev,"Invalid VMPCK%d communication key\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ if (is_vmpck_empty (snp_dev)) { -dev_err (dev,"vmpck id %d is null\n" , vmpck_id); +dev_err (dev,"Empty VMPCK%d communication key\n" , vmpck_id); goto e_unmap; }@@ -911,7 +911,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)if (ret) goto e_free_ctx; -dev_info (dev,"Initialized SEV guest driver (using vmpck_id %d)\n" , vmpck_id); +dev_info (dev,"Initialized SEV guest driver (using VMPCK%d communication key)\n" , vmpck_id);return 0 ; e_free_ctx:
Re: [PATCH v9 05/24] virt: sev-guest: Fix user-visible strings
Quoted 11 lines · Tom Lendacky
Quoted 10 lines · Nikunj A Dadhania
User-visible abbreviations should be in capitals, ensure messages are readable and clear.
No functional change.
Signed-off-by Nikunj A Dadhania <[email protected]>--- drivers/virt/coco/sev-guest/sev-guest.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines
drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 98
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +1
-1@@ -95,7 +95,7 @@ static bool is_vmpck_empty(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", + dev_alert(snp_dev->dev, "Disabling VMPCK%d to prevent IV reuse.\n",You use "communication key" after each VMPCK%d below. I prefer this shorter notation, but whichever way it goes, they should all be the same.
No particular preference, I had added "communication key" from more clarity to user strings. I will add the same to the above string as well.
Regards
Nikunj
PATCH v9 6/24 virt: sev-guest: Simplify VMPCK and sequence number assignments
In reply to Nikunj A DadhaniaPreparatory patch to remove direct usage of VMPCK and message sequence number in the SEV guest driver. Use arrays for the VM platform communication key and message sequence number to simplify the function and usage.
--- arch/x86/include/asm/sev.h | 12 ++++------- drivers/virt/coco/sev-guest/sev-guest.c | 27 ++++--------------------- 2 files changed, 8 insertions(+), 31 deletions(-)
arch/x86/include/asm/sev.h +4 -8
@@ -118,6 +118,8 @@ struct sev_guest_platform_data {u64 secrets_gpa; }; +#define VMPCK_MAX_NUM 4 +/* * The secrets page contains 96-bytes of reserved field that can be used by * the guest OS. The guest OS uses the area to save the message sequence
@@ -126,10 +128,7 @@ struct sev_guest_platform_data {* See the GHCB spec section Secret page layout for the format for this area. */ struct secrets_os_area { -u32 msg_seqno_0; -u32 msg_seqno_1; -u32 msg_seqno_2; -u32 msg_seqno_3; +u32 msg_seqno[VMPCK_MAX_NUM ];u64 ap_jump_table_pa;u8 rsvd[40 ];u8 guest_usage[32 ];
@@ -145,10 +144,7 @@ struct snp_secrets_page {u32 fms;u32 rsvd2;u8 gosvw[16 ]; -u8 vmpck0[VMPCK_KEY_LEN ]; -u8 vmpck1[VMPCK_KEY_LEN ]; -u8 vmpck2[VMPCK_KEY_LEN ]; -u8 vmpck3[VMPCK_KEY_LEN ]; +u8 vmpck[VMPCK_MAX_NUM ][VMPCK_KEY_LEN ];struct secrets_os_area os_area;u8 rsvd3[3840 ]; } __packed;
drivers/virt/coco/sev-guest/sev-guest.c +4 -23
@@ -668,30 +668,11 @@ static const struct file_operations snp_guest_fops = {static u8 *get_vmpck (int id,struct snp_secrets_page *secrets,u32 **seqno) { -u8 *key =NULL ; - -switch (id) { -case 0 : - *seqno = &secrets->os_area.msg_seqno_0; - key = secrets->vmpck0; -break ; -case 1 : - *seqno = &secrets->os_area.msg_seqno_1; - key = secrets->vmpck1; -break ; -case 2 : - *seqno = &secrets->os_area.msg_seqno_2; - key = secrets->vmpck2; -break ; -case 3 : - *seqno = &secrets->os_area.msg_seqno_3; - key = secrets->vmpck3; -break ; -default : -break ; - } +if ((id +1 ) >VMPCK_MAX_NUM ) +return NULL ; -return key; + *seqno = &secrets->os_area.msg_seqno[id]; +return secrets->vmpck[id]; }struct snp_msg_report_resp_hdr {
Signature · 1 line
2.34.1
Re: [PATCH v9 06/24] virt: sev-guest: Simplify VMPCK and sequence number assignments
Quoted 6 lines · Nikunj A Dadhania
Preparatory patch to remove direct usage of VMPCK and message sequence number in the SEV guest driver. Use arrays for the VM platform communication key and message sequence number to simplify the function and usage.
Signed-off-by Nikunj A Dadhania <[email protected]>
One minor comment below, otherwise, for the general logic of using an array:
Quoted 64 lines
--- arch/x86/include/asm/sev.h | 12 ++++------- drivers/virt/coco/sev-guest/sev-guest.c | 27 ++++--------------------- 2 files changed, 8 insertions(+), 31 deletions(-)arch/x86/include/asm/sev.h +4
-8@@ -118,6 +118,8 @@ struct sev_guest_platform_data {u64 secrets_gpa; }; +#define VMPCK_MAX_NUM 4 +/* * The secrets page contains 96-bytes of reserved field that can be used by * the guest OS. The guest OS uses the area to save the message sequence @@ -126,10 +128,7 @@ struct sev_guest_platform_data {* See the GHCB spec section Secret page layout for the format for this area. */ struct secrets_os_area { -u32 msg_seqno_0; -u32 msg_seqno_1; -u32 msg_seqno_2; -u32 msg_seqno_3; +u32 msg_seqno[VMPCK_MAX_NUM ];u64 ap_jump_table_pa;u8 rsvd[40 ];u8 guest_usage[32 ];@@ -145,10 +144,7 @@ struct snp_secrets_page {u32 fms;u32 rsvd2;u8 gosvw[16 ]; -u8 vmpck0[VMPCK_KEY_LEN ]; -u8 vmpck1[VMPCK_KEY_LEN ]; -u8 vmpck2[VMPCK_KEY_LEN ]; -u8 vmpck3[VMPCK_KEY_LEN ]; +u8 vmpck[VMPCK_MAX_NUM ][VMPCK_KEY_LEN ];struct secrets_os_area os_area;u8 rsvd3[3840 ]; } __packed;drivers/virt/coco/sev-guest/sev-guest.c +0
-14@@ -668,30 +668,11 @@ static const struct file_operations snp_guest_fops = {static u8 *get_vmpck (int id,struct snp_secrets_page *secrets,u32 **seqno) { -u8 *key =NULL ; - -switch (id) { -case 0 : - *seqno = &secrets->os_area.msg_seqno_0; - key = secrets->vmpck0; -break ; -case 1 : - *seqno = &secrets->os_area.msg_seqno_1; - key = secrets->vmpck1; -break ; -case 2 : - *seqno = &secrets->os_area.msg_seqno_2; - key = secrets->vmpck2;
drivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 672
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-8-break ; -case 3 : - *seqno = &secrets->os_area.msg_seqno_3; - key = secrets->vmpck3; -break ; -default : -break ; - } +if ((id +1 ) >VMPCK_MAX_NUM ) +return NULL ;
This looks a bit confusing to me, because of the way it has to be written with the "+ 1". I wonder if something like the following would read better:
switch (id) {
case 0 ... 3:
*seqno = &secrets->os_area.msg_seqno[id];
return secrets->vmpck[id];
default:
return NULL;
}Just my opinion, if others are fine with it, then that's fine.
Thanks,
Tom
Quoted 7 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-1-return key; + *seqno = &secrets->os_area.msg_seqno[id]; +return secrets->vmpck[id]; }struct snp_msg_report_resp_hdr {
Re: [PATCH v9 06/24] virt: sev-guest: Simplify VMPCK and sequence number assignments
Quoted 99 lines · Tom Lendacky
Quoted 6 lines · Nikunj A Dadhania
Preparatory patch to remove direct usage of VMPCK and message sequence number in the SEV guest driver. Use arrays for the VM platform communication key and message sequence number to simplify the function and usage.
Signed-off-by Nikunj A Dadhania <[email protected]>One minor comment below, otherwise, for the general logic of using an array:
Reviewed-by Tom Lendacky <[email protected]>Quoted 64 lines
--- arch/x86/include/asm/sev.h | 12 ++++------- drivers/virt/coco/sev-guest/sev-guest.c | 27 ++++--------------------- 2 files changed, 8 insertions(+), 31 deletions(-)arch/x86/include/asm/sev.h +4
-8@@ -118,6 +118,8 @@ struct sev_guest_platform_data {u64 secrets_gpa; }; +#define VMPCK_MAX_NUM 4 +/* * The secrets page contains 96-bytes of reserved field that can be used by * the guest OS. The guest OS uses the area to save the message sequence @@ -126,10 +128,7 @@ struct sev_guest_platform_data {* See the GHCB spec section Secret page layout for the format for this area. */ struct secrets_os_area { -u32 msg_seqno_0; -u32 msg_seqno_1; -u32 msg_seqno_2; -u32 msg_seqno_3; +u32 msg_seqno[VMPCK_MAX_NUM ];u64 ap_jump_table_pa;u8 rsvd[40 ];u8 guest_usage[32 ];@@ -145,10 +144,7 @@ struct snp_secrets_page {u32 fms;u32 rsvd2;u8 gosvw[16 ]; -u8 vmpck0[VMPCK_KEY_LEN ]; -u8 vmpck1[VMPCK_KEY_LEN ]; -u8 vmpck2[VMPCK_KEY_LEN ]; -u8 vmpck3[VMPCK_KEY_LEN ]; +u8 vmpck[VMPCK_MAX_NUM ][VMPCK_KEY_LEN ];struct secrets_os_area os_area;u8 rsvd3[3840 ]; } __packed;drivers/virt/coco/sev-guest/sev-guest.c +0
-14@@ -668,30 +668,11 @@ static const struct file_operations snp_guest_fops = {static u8 *get_vmpck (int id,struct snp_secrets_page *secrets,u32 **seqno) { -u8 *key =NULL ; - -switch (id) { -case 0 : - *seqno = &secrets->os_area.msg_seqno_0; - key = secrets->vmpck0; -break ; -case 1 : - *seqno = &secrets->os_area.msg_seqno_1; - key = secrets->vmpck1; -break ; -case 2 : - *seqno = &secrets->os_area.msg_seqno_2; - key = secrets->vmpck2;drivers/virt/coco/sev-guest/sev-guest.c static const struct file_operations snp_guest_fops = { · line 672
Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +2
-8-break ; -case 3 : - *seqno = &secrets->os_area.msg_seqno_3; - key = secrets->vmpck3; -break ; -default : -break ; - } +if ((id +1 ) >VMPCK_MAX_NUM ) +return NULL ;This looks a bit confusing to me, because of the way it has to be written with the "+ 1". I wonder if something like the following would read better:
switch (id) { case 0 ... 3: *seqno = &secrets->os_area.msg_seqno[id]; return secrets->vmpck[id]; default: return NULL; }
Quoted 1 line
Just my opinion, if others are fine with it, then that's fine.
I have separated patch 6 and 7 for better code review and modular changes.
The next patch simplifes this further to:
static inline u8 *get_vmpck(struct snp_guest_dev *snp_dev)
{
return snp_dev->secrets->vmpck[snp_dev->vmpck_id];
}static bool assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id)
{
if ((vmpck_id + 1) > VMPCK_MAX_NUM)
return false;
dev->vmpck_id = vmpck_id;
return true;
}Regards
Nikunj
Re: [PATCH v9 06/24] virt: sev-guest: Simplify VMPCK and sequence number assignments
Quoted 8 lines · Nikunj A. Dadhania
Quoted 1 line · Tom Lendacky
On 5/30/24 23:30, Nikunj A Dadhania wrote:
I have separated patch 6 and 7 for better code review and modular changes.
The next patch simplifes this further to:
Quoted 8 lines
static inline u8 *get_vmpck(struct
snp_guest_dev*snp_dev){ return snp_dev->secrets->vmpck[snp_dev->vmpck_id]; }static bool
assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id){ if ((vmpck_id + 1) > VMPCK_MAX_NUM)
Ok, this still has the "+ 1" thing (and it should be >=, right?). How about:
if (!(vmpck_id < VMPCK_MAX_NUM)) return false;
Just makes it easier to read for me, but if no one else has an issue, don't worry about it.
Thanks,
Tom
Quoted 10 lines
return false; dev->vmpck_id = vmpck_id; return true; }Regards
Nikunj
Re: [PATCH v9 06/24] virt: sev-guest: Simplify VMPCK and sequence number assignments
Quoted 14 lines · Tom Lendacky
Quoted 13 lines · Nikunj A. Dadhania
Quoted 1 line · Tom Lendacky
On 5/30/24 23:30, Nikunj A Dadhania wrote:
I have separated patch 6 and 7 for better code review and modular changes.
The next patch simplifes this further to:
static inline u8 *get_vmpck(struct
snp_guest_dev*snp_dev){ return snp_dev->secrets->vmpck[snp_dev->vmpck_id]; }
Quoted 5 lines
Quoted 3 lines
static bool
assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id){ if ((vmpck_id + 1) > VMPCK_MAX_NUM)Ok, this still has the "+ 1" thing (and it should be >=, right?). How about:
For vmpck_id=3 which is valid, ((3 + 1) >= 4) will exit, which is not correct.
Quoted 4 lines
if (!(vmpck_id < VMPCK_MAX_NUM)) return false;
Sure, this is better.
Quoted 2 lines
Just makes it easier to read for me, but if no one else has an issue, don't worry about it.
Thanks,
Nikunj
PATCH v9 7/24 virt: sev-guest: Store VMPCK index to SNP guest device structure
In reply to Nikunj A DadhaniaCurrently, SEV guest driver retrieves the pointers to VMPCK and os_area_msg_seqno from the secrets page. In order to get rid of this dependency, use vmpck_id to index the appropriate key and the corresponding message sequence number.
--- drivers/virt/coco/sev-guest/sev-guest.c | 67 ++++++++++++------------- 1 file changed, 33 insertions(+), 34 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +33 -34
@@ -55,8 +55,7 @@ struct snp_guest_dev {struct snp_derived_key_req derived_key;struct snp_ext_report_req ext_report; } req; -u32 *os_area_msg_seqno; -u8 *vmpck; +unsigned int vmpck_id; };static u32 vmpck_id;
@@ -66,14 +65,17 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP./* Mutex to serialize the shared buffer access and command handling. */ static DEFINE_MUTEX (snp_cmd_mutex); +static inline u8 *get_vmpck (struct snp_guest_dev *snp_dev) +{ +return snp_dev->secrets->vmpck[snp_dev->vmpck_id]; +} +static bool is_vmpck_empty (struct snp_guest_dev *snp_dev) {char zero_key[VMPCK_KEY_LEN ] = {0 }; +u8 *key =get_vmpck (snp_dev); -if (snp_dev->vmpck) -return !memcmp (snp_dev->vmpck, zero_key,VMPCK_KEY_LEN ); - -return true; +return !memcmp (key, zero_key,VMPCK_KEY_LEN ); }/*
@@ -95,28 +97,23 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)*/ static void snp_disable_vmpck (struct snp_guest_dev *snp_dev) { -dev_alert (snp_dev->dev,"Disabling VMPCK%d to prevent IV reuse.\n" , - 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_cmd_mutex); +u8 *key =get_vmpck (snp_dev); -/* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; +if (is_vmpck_empty (snp_dev)) +return ; -return count +1 ; +dev_alert (snp_dev->dev,"Disabling VMPCK%u to prevent IV reuse.\n" , snp_dev->vmpck_id); +memzero_explicit (key,VMPCK_KEY_LEN ); }/* 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); +u64 count; + +lockdep_assert_held (&snp_cmd_mutex); + + count = snp_dev->secrets->os_area.msg_seqno[snp_dev->vmpck_id] +1 ;/* * The message sequence counter for the SNP guest request is a 64-bit
@@ -140,7 +137,7 @@ 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 ; + snp_dev->secrets->os_area.msg_seqno[snp_dev->vmpck_id] +=2 ; }static inline struct snp_guest_dev *to_snp_dev (struct file *file)
@@ -150,15 +147,17 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto (u8 *key,size_t keylen) +static struct aesgcm_ctx *snp_init_crypto (struct snp_guest_dev *snp_dev) {struct aesgcm_ctx *ctx; +u8 *key; ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ; -if (aesgcm_expandkey (ctx, key, keylen,AUTHTAG_LEN )) { + key =get_vmpck (snp_dev); +if (aesgcm_expandkey (ctx, key,VMPCK_KEY_LEN ,AUTHTAG_LEN )) {pr_err ("Crypto context initialization failed\n" );kfree (ctx);return NULL ;
@@ -666,13 +665,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck (int id,struct snp_secrets_page *secrets,u32 **seqno) +static bool assign_vmpck (struct snp_guest_dev *dev,unsigned int vmpck_id) { -if ((id +1 ) >VMPCK_MAX_NUM ) -return NULL ; +if ((vmpck_id +1 ) >VMPCK_MAX_NUM ) +return false; + + dev->vmpck_id = vmpck_id; - *seqno = &secrets->os_area.msg_seqno[id]; -return secrets->vmpck[id]; +return true; }struct snp_msg_report_resp_hdr {
@@ -828,21 +828,20 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL ; - snp_dev->vmpck =get_vmpck (vmpck_id, secrets, &snp_dev->os_area_msg_seqno); -if (!snp_dev->vmpck) { + snp_dev->secrets = secrets; +if (!assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"Invalid VMPCK%d communication key\n" , vmpck_id); goto e_unmap; }/* Verify that VMPCK is not zero. */ if (is_vmpck_empty (snp_dev)) { -dev_err (dev,"Empty VMPCK%d communication key\n" , vmpck_id); +dev_err (dev,"Empty VMPCK%d communication key\n" , snp_dev->vmpck_id); goto e_unmap; }platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; - snp_dev->secrets = secrets;/* Allocate secret request and response message for double buffering */ snp_dev->secret_request =kzalloc (SNP_GUEST_MSG_SIZE ,GFP_KERNEL );
@@ -867,7 +866,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO ; - snp_dev->ctx =snp_init_crypto (snp_dev->vmpck,VMPCK_KEY_LEN ); + snp_dev->ctx =snp_init_crypto (snp_dev);if (!snp_dev->ctx) goto e_free_cert_data;
Signature · 1 line
2.34.1
PATCH v9 8/24 virt: sev-guest: Take mutex in snp_send_guest_request()
In reply to Nikunj A DadhaniaSNP command mutex is used to serialize access to the shared buffer, command handling and message sequence number races.
As part of the preparation for moving SEV guest driver common code and making mutex private, take the mutex in snp_send_guest_request() instead of snp_guest_ioctl(). This will result in locking behavior change as detailed below:
Current locking behaviour:
snp_guest_ioctl()
mutex_lock(&snp_cmd_mutex)
get_report()/get_derived_key()/get_ext_report()
snp_send_guest_request()
...
mutex_unlock(&snp_cmd_mutex)New locking behaviour:
snp_guest_ioctl()
get_report()/get_derived_key()/get_ext_report()
snp_send_guest_request()
guard(mutex)(&snp_cmd_mutex)
...Remove multiple lockdep check in the sev-guest driver as they are redundant now.
--- drivers/virt/coco/sev-guest/sev-guest.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +2 -15
@@ -111,8 +111,6 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) {u64 count; -lockdep_assert_held (&snp_cmd_mutex); - count = snp_dev->secrets->os_area.msg_seqno[snp_dev->vmpck_id] +1 ;/*
@@ -334,6 +332,8 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guesu64 seqno;int rc; +guard (mutex)(&snp_cmd_mutex); +/* Get message sequence and verify that its a non-zero */ seqno =snp_get_msg_seqno (snp_dev);if (!seqno)
@@ -388,8 +388,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_iostruct snp_report_resp *report_resp;int rc, resp_len; -lockdep_assert_held (&snp_cmd_mutex); -if (!arg->req_data || !arg->resp_data)return -EINVAL ;
@@ -436,8 +434,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque/* Response data is 64 bytes and max authsize for GCM is 16 bytes. */ u8 buf[64 +16 ]; -lockdep_assert_held (&snp_cmd_mutex); -if (!arg->req_data || !arg->resp_data)return -EINVAL ;
@@ -488,8 +484,6 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_requessockptr_t certs_address;int ret, resp_len; -lockdep_assert_held (&snp_cmd_mutex); -if (sockptr_is_null (io->req_data) ||sockptr_is_null (io->resp_data))return -EINVAL ;
@@ -585,12 +579,9 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longif (!input.msg_version)return -EINVAL ; -mutex_lock (&snp_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);return -ENOTTY ; }
@@ -615,8 +606,6 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longbreak ; } -mutex_unlock (&snp_cmd_mutex); -if (input.exitinfo2 &©_to_user (argp, &input,sizeof (input)))return -EFAULT ;
@@ -705,8 +694,6 @@ static int sev_report_new(struct tsm_report *report, void *data)if (!buf)return -ENOMEM ; -guard (mutex)(&snp_cmd_mutex); -/* Check if the VMPCK is not empty */ if (is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );
Signature · 1 line
2.34.1
PATCH v9 9/24 virt: sev-guest: Carve out SNP guest messaging init/exit
In reply to Nikunj A DadhaniaIn preparation for the movement of common code required for both SEV guest driver and Secure TSC, carve out initializaton and cleanup routines.
While at it, the device pointer in alloc_shared_pages() is used only in the print routines, replace dev_err() with pr_err() and drop the device pointer from the function prototype.
--- drivers/virt/coco/sev-guest/sev-guest.c | 152 ++++++++++++++---------- 1 file changed, 88 insertions(+), 64 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +88 -64
@@ -150,6 +150,11 @@ static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev)struct aesgcm_ctx *ctx;u8 *key; +if (is_vmpck_empty (snp_dev)) { +pr_err ("VM communication key VMPCK%u is invalid\n" , snp_dev->vmpck_id); +return NULL ; + } + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ;
@@ -629,7 +634,7 @@ static void free_shared_pages(void *buf, size_t sz)__free_pages (virt_to_page (buf),get_order (sz)); } -static void *alloc_shared_pages (struct device *dev,size_t sz) +static void *alloc_shared_pages (size_t sz) {unsigned int npages =PAGE_ALIGN (sz) >>PAGE_SHIFT ;struct page *page;
@@ -641,7 +646,7 @@ static void *alloc_shared_pages(struct device *dev, size_t sz) ret =set_memory_decrypted ((unsigned long )page_address (page), npages);if (ret) { -dev_err (dev,"failed to mark page shared, ret=%d\n" , ret); +pr_err ("failed to mark page shared, ret=%d\n" , ret);__free_pages (page,get_order (sz));return NULL ; }
@@ -786,14 +791,80 @@ static void unregister_sev_tsm(void *data)tsm_unregister (&sev_tsm_ops); } +static int snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) +{ +int ret = -ENOMEM ; + + snp_dev->secrets = (__force void *)ioremap_encrypted (secrets_gpa,PAGE_SIZE ); +if (!snp_dev->secrets) { +pr_err ("Failed to map SNP secrets page.\n" ); +return ret; + } + +/* Allocate secret request and response message for double buffering */ + snp_dev->secret_request =kzalloc (SNP_GUEST_MSG_SIZE ,GFP_KERNEL ); +if (!snp_dev->secret_request) + goto e_unmap; + + snp_dev->secret_response =kzalloc (SNP_GUEST_MSG_SIZE ,GFP_KERNEL ); +if (!snp_dev->secret_response) + goto e_free_secret_req; + +/* Allocate the shared page used for the request and response message. */ + snp_dev->request =alloc_shared_pages (SNP_GUEST_MSG_SIZE ); +if (!snp_dev->request) + goto e_free_secret_resp; + + snp_dev->response =alloc_shared_pages (SNP_GUEST_MSG_SIZE ); +if (!snp_dev->response) + goto e_free_request; + +/* Initialize the input addresses for guest request */ + snp_dev->input.req_gpa =__pa (snp_dev->request); + snp_dev->input.resp_gpa =__pa (snp_dev->response); + + ret = -EIO ; + snp_dev->ctx =snp_init_crypto (snp_dev); +if (!snp_dev->ctx) { +pr_err ("SNP crypto context initialization failed\n" ); + goto e_free_response; + } + +return 0 ; + +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_free_secret_resp: +kfree (snp_dev->secret_response); +e_free_secret_req: +kfree (snp_dev->secret_request); +e_unmap: +iounmap (snp_dev->secrets); + +return ret; +} + +static void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev) +{ +if (!snp_dev) +return ; + +kfree (snp_dev->ctx); +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->secret_response); +kfree (snp_dev->secret_request); +iounmap (snp_dev->secrets); +} +static int __init sev_guest_probe (struct platform_device *pdev) {struct sev_guest_platform_data *data; -struct snp_secrets_page *secrets;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 ))
@@ -803,69 +874,36 @@ static int __init sev_guest_probe(struct platform_device *pdev)return -ENODEV ; data = (struct sev_guest_platform_data *)dev->platform_data; - mapping =ioremap_encrypted (data->secrets_gpa,PAGE_SIZE ); -if (!mapping) -return -ENODEV ; - - secrets = (__force void *)mapping; - ret = -ENOMEM ; 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->secrets = secrets;if (!assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"Invalid VMPCK%d communication key\n" , vmpck_id); - goto e_unmap; +return ret; } -/* Verify that VMPCK is not zero. */ -if (is_vmpck_empty (snp_dev)) { -dev_err (dev,"Empty VMPCK%d communication key\n" , snp_dev->vmpck_id); - goto e_unmap; +if (snp_guest_messaging_init (snp_dev, data->secrets_gpa)) { +dev_err (dev,"Unable to setup SNP Guest messaging using VMPCK%u\n" , + snp_dev->vmpck_id); +return ret; }platform_set_drvdata (pdev, snp_dev); snp_dev->dev = dev; -/* Allocate secret request and response message for double buffering */ - snp_dev->secret_request =kzalloc (SNP_GUEST_MSG_SIZE ,GFP_KERNEL ); -if (!snp_dev->secret_request) - goto e_unmap; - - snp_dev->secret_response =kzalloc (SNP_GUEST_MSG_SIZE ,GFP_KERNEL ); -if (!snp_dev->secret_response) - goto e_free_secret_req; - -/* Allocate the shared page used for the request and response message. */ - snp_dev->request =alloc_shared_pages (dev,SNP_GUEST_MSG_SIZE ); -if (!snp_dev->request) - goto e_free_secret_resp; - - snp_dev->response =alloc_shared_pages (dev,SNP_GUEST_MSG_SIZE ); -if (!snp_dev->response) - goto e_free_request; - - snp_dev->certs_data =alloc_shared_pages (dev,SEV_FW_BLOB_MAX_SIZE ); + ret = -ENOMEM ; + snp_dev->certs_data =alloc_shared_pages (SEV_FW_BLOB_MAX_SIZE );if (!snp_dev->certs_data) - goto e_free_response; - - ret = -EIO ; - snp_dev->ctx =snp_init_crypto (snp_dev); -if (!snp_dev->ctx) - goto e_free_cert_data; + goto e_cleanup_msg_init; misc = &snp_dev->misc; misc->minor =MISC_DYNAMIC_MINOR ; misc->name =DEVICE_NAME ; misc->fops = &snp_guest_fops; -/* Initialize the input addresses for guest request */ - snp_dev->input.req_gpa =__pa (snp_dev->request); - snp_dev->input.resp_gpa =__pa (snp_dev->response); - ret =tsm_register (&sev_tsm_ops, snp_dev, &tsm_report_extra_type);if (ret) goto e_free_cert_data;
@@ -876,25 +914,15 @@ static int __init sev_guest_probe(struct platform_device *pdev) ret =misc_register (misc);if (ret) - goto e_free_ctx; + goto e_free_cert_data;dev_info (dev,"Initialized SEV guest driver (using VMPCK%d communication key)\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,SNP_GUEST_MSG_SIZE ); -e_free_request: -free_shared_pages (snp_dev->request,SNP_GUEST_MSG_SIZE ); -e_free_secret_resp: -kfree (snp_dev->secret_response); -e_free_secret_req: -kfree (snp_dev->secret_request); -e_unmap: -iounmap (mapping); +e_cleanup_msg_init: +snp_guest_messaging_exit (snp_dev);return ret; }
@@ -903,11 +931,7 @@ static void __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,SNP_GUEST_MSG_SIZE ); -free_shared_pages (snp_dev->request,SNP_GUEST_MSG_SIZE ); -kfree (snp_dev->secret_response); -kfree (snp_dev->secret_request); -kfree (snp_dev->ctx); +snp_guest_messaging_exit (snp_dev);misc_deregister (&snp_dev->misc); }
Signature · 1 line
2.34.1
PATCH v9 10/24 x86/sev: Move core SEV guest driver routines to common code
In reply to Nikunj A DadhaniaIn order to enable Secure TSC, SEV-SNP guests need to communicate with the AMD Security Processor during early boot. However, many of the necessary SNP guest messaging functions are currently implemented in the SEV guest driver and are not available during early boot. As a result, these core SNP guest messaging functions need to be relocated to SEV common code. Later, APIs will be provided to the SEV guest driver to initialize and send SNP guest messages.
Some functions in sev.c are marked __maybe_unused to ensure compilation does not break. Similarly, a few functions are stubbed out in SEV guest driver.
This is pure code movement, SEV guest driver is broken after this patch.
--- arch/x86/include/asm/sev.h | 70 ++++ arch/x86/kernel/sev.c | 393 +++++++++++++++++++++ drivers/virt/coco/sev-guest/sev-guest.c | 442 +----------------------- arch/x86/Kconfig | 1 + drivers/virt/coco/sev-guest/Kconfig | 1 - 5 files changed, 473 insertions(+), 434 deletions(-)
arch/x86/include/asm/sev.h +70 -0
@@ -10,10 +10,12 @@#include <linux/types.h> #include <linux/sev-guest.h> +#include <linux/miscdevice.h> #include <asm/insn.h> #include <asm/sev-common.h> #include <asm/coco.h> +#include <asm/set_memory.h> #define GHCB_PROTOCOL_MIN 1ULL#define GHCB_PROTOCOL_MAX 2ULL
@@ -149,6 +151,9 @@ struct snp_secrets_page {u8 rsvd3[3840 ]; } __packed; +#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
@@ -203,6 +208,31 @@ struct snp_guest_msg {#define SNP_GUEST_MSG_SIZE 4096#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg)) +struct snp_guest_dev { +struct device *dev; +struct miscdevice misc; + +void *certs_data; +struct aesgcm_ctx *ctx; +/* request and response are in unencrypted memory */ +struct snp_guest_msg *request, *response; + +/* +* Avoid information leakage by double-buffering shared messages +* in fields that are in regular encrypted memory. +*/ +struct snp_guest_msg *secret_request, *secret_response; + +struct snp_secrets_page *secrets; +struct snp_req_data input; +union { +struct snp_report_req report; +struct snp_derived_key_req derived_key; +struct snp_ext_report_req ext_report; + } req; +unsigned int vmpck_id; +}; +struct snp_guest_req {void *req_buf;size_t req_sz;
@@ -294,6 +324,44 @@ void snp_accept_memory(phys_addr_t start, phys_addr_t end);u64 snp_get_unsupported_features (u64 status);u64 sev_get_status (void );void sev_show_status (void ); + +static inline 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 inline 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 ("failed to mark page shared, ret=%d\n" , ret); +__free_pages (page,get_order (sz)); +return NULL ; + } + +return page_address (page); +} +#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -324,6 +392,8 @@ static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }static inline u64 snp_get_unsupported_features (u64 status) {return 0 ; }static inline u64 sev_get_status (void ) {return 0 ; }static inline void sev_show_status (void ) { } +static inline void free_shared_pages (void *buf,size_t sz) { } +static inline void *alloc_shared_pages (size_t sz) {return NULL ; }#endif #ifdef CONFIG_KVM_AMD_SEV
arch/x86/kernel/sev.c +393 -0
402 lines not on this page · raw message
drivers/virt/coco/sev-guest/sev-guest.c +9 -433
486 lines not on this page · raw message
arch/x86/Kconfig +1 -0
@@ -1544,6 +1544,7 @@ config AMD_MEM_ENCRYPT select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select UNACCEPTED_MEMORY + select CRYPTO_LIB_AESGCM help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
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 select TSM_REPORTS help SEV-SNP firmware provides the guest a mechanism to communicate with
Signature · 1 line
2.34.1
PATCH v9 11/24 x86/sev: Replace dev_[err,alert] with pr_[err,alert]
In reply to Nikunj A DadhaniaReplace the dev_err/alert with pr_err/alert after the sev-guest driver movement.
No functional change.
--- arch/x86/kernel/sev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
arch/x86/kernel/sev.c +4 -6
@@ -2352,7 +2352,7 @@ static void snp_disable_vmpck(struct snp_guest_dev *snp_dev)if (is_vmpck_empty (snp_dev))return ; -dev_alert (snp_dev->dev,"Disabling VMPCK%u to prevent IV reuse.\n" , snp_dev->vmpck_id); +pr_alert ("Disabling VMPCK%u to prevent IV reuse.\n" , snp_dev->vmpck_id);memzero_explicit (key,VMPCK_KEY_LEN ); }
@@ -2372,7 +2372,7 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev)* invalid number and will fail the message request. */ if (count >=UINT_MAX ) { -dev_err (snp_dev->dev,"request message sequence counter overflow\n" ); +pr_err ("request message sequence counter overflow\n" );return 0 ; }
@@ -2608,8 +2608,7 @@ static int __maybe_unused snp_send_guest_request(struct snp_guest_dev *snp_dev, rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN))return rc; -dev_alert (snp_dev->dev, -"Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n" , +pr_alert ("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n" , rc, rio->exitinfo2);snp_disable_vmpck (snp_dev);return rc;
@@ -2617,8 +2616,7 @@ static int __maybe_unused snp_send_guest_request(struct snp_guest_dev *snp_dev, rc =verify_and_dec_payload (snp_dev, req);if (rc) { -dev_alert (snp_dev->dev,"Detected unexpected decode failure from ASP. rc: %d\n" , - rc); +pr_alert ("Detected unexpected decode failure from ASP. rc: %d\n" , rc);snp_disable_vmpck (snp_dev);return rc; }
Signature · 1 line
2.34.1
PATCH v9 12/24 x86/sev: Make snp_issue_guest_request() static
In reply to Nikunj A DadhaniaAs there is no external caller for snp_issue_guest_request() anymore, make it static and drop the prototype from the header.
No functional change.
--- arch/x86/include/asm/sev.h | 8 -------- arch/x86/kernel/sev.c | 3 +-- 2 files changed, 1 insertion(+), 10 deletions(-)
arch/x86/include/asm/sev.h +0 -8
@@ -318,8 +318,6 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void __noreturn snp_abort (void );void snp_dmi_setup (void ); -int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, -struct snp_guest_request_ioctl *rio);void snp_accept_memory (phys_addr_t start,phys_addr_t end);u64 snp_get_unsupported_features (u64 status);u64 sev_get_status (void );
@@ -382,12 +380,6 @@ static inline void snp_set_wakeup_secondary_cpu(void) { }static inline bool snp_init (struct boot_params *bp) {return false; }static inline void snp_abort (void ) { }static inline void snp_dmi_setup (void ) { } -static inline int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, -struct snp_guest_request_ioctl *rio) -{ -return -ENOTTY ; -} -static inline void snp_accept_memory (phys_addr_t start,phys_addr_t end) { }static inline u64 snp_get_unsupported_features (u64 status) {return 0 ; }static inline u64 sev_get_status (void ) {return 0 ; }
arch/x86/kernel/sev.c +1 -2
@@ -2192,7 +2192,7 @@ static int __init init_sev_config(char *str) }__setup ("sev=" , init_sev_config); -int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input, +static int snp_issue_guest_request (struct snp_guest_req *req,struct snp_req_data *input,struct snp_guest_request_ioctl *rio) {struct ghcb_state state;
@@ -2255,7 +2255,6 @@ int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *inpureturn ret; } -EXPORT_SYMBOL_GPL (snp_issue_guest_request);static struct platform_device sev_guest_device = { .name ="sev-guest" ,
Signature · 1 line
2.34.1
PATCH v9 13/24 x86/sev: Make sev-guest driver functional again
In reply to Nikunj A DadhaniaAfter the pure mechanical code movement of core SEV guest driver routines, SEV guest driver is not yet functional. Export SNP guest messaging APIs for the sev-guest driver. Drop the stubbed routines in sev-guest driver and use the newly exported APIs
--- arch/x86/include/asm/sev.h | 14 ++++++++++ arch/x86/kernel/sev.c | 23 +++++++++------ drivers/virt/coco/sev-guest/sev-guest.c | 37 ++----------------------- 3 files changed, 31 insertions(+), 43 deletions(-)
arch/x86/include/asm/sev.h +14 -0
@@ -322,6 +322,12 @@ void snp_accept_memory(phys_addr_t start, phys_addr_t end);u64 snp_get_unsupported_features (u64 status);u64 sev_get_status (void );void sev_show_status (void ); +bool snp_assign_vmpck (struct snp_guest_dev *snp_dev,unsigned int vmpck_id); +bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev); +int snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa); +void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev); +int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, +struct snp_guest_request_ioctl *rio);static inline void free_shared_pages (void *buf,size_t sz) {
@@ -384,6 +390,14 @@ static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }static inline u64 snp_get_unsupported_features (u64 status) {return 0 ; }static inline u64 sev_get_status (void ) {return 0 ; }static inline void sev_show_status (void ) { } +static inline bool snp_assign_vmpck (struct snp_guest_dev *snp_dev, +unsigned int vmpck_id) {return false; } +static inline bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) {return true; } +static inline int +snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) {return -EINVAL ; } +static inline void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev) { } +static inline int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, +struct snp_guest_request_ioctl *rio) {return -EINVAL ; }static inline void free_shared_pages (void *buf,size_t sz) { }static inline void *alloc_shared_pages (size_t sz) {return NULL ; }#endif
arch/x86/kernel/sev.c +14 -9
@@ -2309,7 +2309,7 @@ static inline u8 *get_vmpck(struct snp_guest_dev *snp_dev)return snp_dev->secrets->vmpck[snp_dev->vmpck_id]; } -static bool __maybe_unused assign_vmpck (struct snp_guest_dev *dev,unsigned int vmpck_id) +bool snp_assign_vmpck (struct snp_guest_dev *dev,unsigned int vmpck_id) {if ((vmpck_id +1 ) >VMPCK_MAX_NUM )return false;
@@ -2318,14 +2318,16 @@ static bool __maybe_unused assign_vmpck(struct snp_guest_dev *dev, unsigned intreturn true; } +EXPORT_SYMBOL_GPL (snp_assign_vmpck); -static bool is_vmpck_empty (struct snp_guest_dev *snp_dev) +bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) {char zero_key[VMPCK_KEY_LEN ] = {0 };u8 *key =get_vmpck (snp_dev);return !memcmp (key, zero_key,VMPCK_KEY_LEN ); } +EXPORT_SYMBOL_GPL (snp_is_vmpck_empty);/* * If an error is received from the host or AMD Secure Processor (ASP) there
@@ -2348,7 +2350,7 @@ static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) {u8 *key =get_vmpck (snp_dev); -if (is_vmpck_empty (snp_dev)) +if (snp_is_vmpck_empty (snp_dev))return ;pr_alert ("Disabling VMPCK%u to prevent IV reuse.\n" , snp_dev->vmpck_id);
@@ -2392,7 +2394,7 @@ static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev)struct aesgcm_ctx *ctx;u8 *key; -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {pr_err ("VM communication key VMPCK%u is invalid\n" , snp_dev->vmpck_id);return NULL ; }
@@ -2573,9 +2575,9 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, struct snp_guesreturn rc; } -static int __maybe_unused snp_send_guest_request (struct snp_guest_dev *snp_dev, -struct snp_guest_req *req, -struct snp_guest_request_ioctl *rio) +int snp_send_guest_request (struct snp_guest_dev *snp_dev, +struct snp_guest_req *req, +struct snp_guest_request_ioctl *rio) {u64 seqno;int rc;
@@ -2622,8 +2624,9 @@ static int __maybe_unused snp_send_guest_request(struct snp_guest_dev *snp_dev,return 0 ; } +EXPORT_SYMBOL_GPL (snp_send_guest_request); -static int __maybe_unused snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) +int snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) {int ret = -ENOMEM ;
@@ -2677,8 +2680,9 @@ static int __maybe_unused snp_guest_messaging_init(struct snp_guest_dev *snp_devreturn ret; } +EXPORT_SYMBOL_GPL (snp_guest_messaging_init); -static void __maybe_unused snp_guest_messaging_exit (struct snp_guest_dev *snp_dev) +void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev) {if (!snp_dev)return ;
@@ -2690,3 +2694,4 @@ static void __maybe_unused snp_guest_messaging_exit(struct snp_guest_dev *snp_dekfree (snp_dev->secret_request);iounmap (snp_dev->secrets); } +EXPORT_SYMBOL_GPL (snp_guest_messaging_exit);
drivers/virt/coco/sev-guest/sev-guest.c +3 -34
@@ -34,12 +34,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." ); -static bool is_vmpck_empty (struct snp_guest_dev *snp_dev) -{ -/* Place holder function to be removed after code movement */ -return true; -} -static inline struct snp_guest_dev *to_snp_dev (struct file *file) {struct miscdevice *dev = file->private_data;
@@ -47,13 +41,6 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req, -struct snp_guest_request_ioctl *rio) -{ -/* Place holder function to be removed after code movement */ -return -EIO ; -} -struct snp_req_resp {sockptr_t req_data;sockptr_t resp_data;
@@ -258,7 +245,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned longreturn -EINVAL ;/* Check if the VMPCK is not empty */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );return -ENOTTY ; }
@@ -295,12 +282,6 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static bool assign_vmpck (struct snp_guest_dev *dev,unsigned int vmpck_id) -{ -/* Place holder function to be removed after code movement */ -return false; -} -struct snp_msg_report_resp_hdr {u32 status;u32 report_size;
@@ -332,7 +313,7 @@ static int sev_report_new(struct tsm_report *report, void *data)return -ENOMEM ;/* Check if the VMPCK is not empty */ -if (is_vmpck_empty (snp_dev)) { +if (snp_is_vmpck_empty (snp_dev)) {dev_err_ratelimited (snp_dev->dev,"VMPCK is disabled\n" );return -ENOTTY ; }
@@ -423,18 +404,6 @@ static void unregister_sev_tsm(void *data)tsm_unregister (&sev_tsm_ops); } -static int snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) -{ -/* Place holder function to be removed after code movement */ -return 0 ; -} - -static void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev) -{ -/* Place holder function to be removed after code movement */ -return ; -} -static int __init sev_guest_probe (struct platform_device *pdev) {struct sev_guest_platform_data *data;
@@ -456,7 +425,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)return -ENOMEM ; ret = -EINVAL ; -if (!assign_vmpck (snp_dev, vmpck_id)) { +if (!snp_assign_vmpck (snp_dev, vmpck_id)) {dev_err (dev,"Invalid VMPCK%d communication key\n" , vmpck_id);return ret; }
Signature · 1 line
2.34.1
PATCH v9 14/24 x86/sev: Handle failures from snp_init()
In reply to Nikunj A DadhaniaFailures from the snp_init() are currently being ignored by sme_enable(). Add missing error handling for cases where snp_init() fails to retrieve SEV-SNP CC blob or encounters issues while parsing CC blob. SNP guests will error out early with this change.
--- arch/x86/mm/mem_encrypt_identity.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/mm/mem_encrypt_identity.c +7 -0
@@ -535,6 +535,13 @@ void __head sme_enable(struct boot_params *bp)if (snp && !(msr &MSR_AMD64_SEV_SNP_ENABLED ))snp_abort (); +/* +* The SEV-SNP CC blob should be present and parsing CC blob should +* succeed when SEV-SNP is enabled. +*/ +if (!snp && (msr &MSR_AMD64_SEV_SNP_ENABLED )) +snp_abort (); +/* Check if memory encryption is enabled */ if (feature_mask ==AMD_SME_BIT ) {if (!(bp->hdr.xloadflags &XLF_MEM_ENCRYPTION ))
Signature · 1 line
2.34.1
PATCH v9 15/24 x86/sev: Cache the secrets page address
In reply to Nikunj A DadhaniaInstead of calling get_secrets_page() that parses the CC blob every time for getting the secrets page physical address(secrets_pa), save the secrets page physical address during snp_init() from the CC blob. Now that there are no users of get_secrets_page() drop the function.
--- arch/x86/kernel/sev.c | 51 ++++++++++--------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-)
arch/x86/kernel/sev.c +11 -40
@@ -93,6 +93,9 @@ static struct ghcb *boot_ghcb __section(".data");/* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features__ro_after_init ; +/* Secrets page physical address from the CC blob */ +static u64 secrets_pa__ro_after_init ; +/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;
@@ -619,45 +622,13 @@ void noinstr __sev_es_nmi_complete(void)__sev_put_ghcb (&state); } -static u64 __init get_secrets_page (void ) -{ -u64 pa_data = boot_params.cc_blob_address; -struct cc_blob_sev_info info; -void *map; - -/* -* The CC blob contains the address of the secrets page, check if the -* blob is present. -*/ -if (!pa_data) -return 0 ; - - map =early_memremap (pa_data,sizeof (info)); -if (!map) { -pr_err ("Unable to locate SNP secrets page: failed to map the Confidential Computing blob.\n" ); -return 0 ; - } -memcpy (&info, map,sizeof (info)); -early_memunmap (map,sizeof (info)); - -/* smoke-test the secrets page passed */ -if (!info.secrets_phys || info.secrets_len !=PAGE_SIZE ) -return 0 ; - -return info.secrets_phys; -} -static u64 __init get_snp_jump_table_addr (void ) {struct snp_secrets_page *secrets;void __iomem *mem; -u64 pa, addr; - - pa =get_secrets_page (); -if (!pa) -return 0 ; +u64 addr; - mem =ioremap_encrypted (pa,PAGE_SIZE ); + mem =ioremap_encrypted (secrets_pa,PAGE_SIZE );if (!mem) {pr_err ("Unable to locate AP jump table address: failed to map the SNP secrets page.\n" );return 0 ;
@@ -2107,6 +2078,11 @@ bool __head snp_init(struct boot_params *bp)if (!cc_info)return false; +if (cc_info->secrets_phys && cc_info->secrets_len ==PAGE_SIZE ) + secrets_pa = cc_info->secrets_phys; +else +return false; +setup_cpuid_table (cc_info);/*
@@ -2264,16 +2240,11 @@ static struct platform_device sev_guest_device = {static int __init snp_init_platform_device (void ) {struct sev_guest_platform_data data; -u64 gpa;if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))return -ENODEV ; - gpa =get_secrets_page (); -if (!gpa) -return -ENODEV ; - - data.secrets_gpa = gpa; + data.secrets_gpa = secrets_pa;if (platform_device_add_data (&sev_guest_device, &data,sizeof (data)))return -ENODEV ;
Signature · 1 line
2.34.1
PATCH v9 16/24 x86/sev: Drop sev_guest_platform_data structure
In reply to Nikunj A DadhaniaSEV guest platform data structure was used to pass the secrets page physical address. As the SNP guest messaging initialization routines are local and secrets page address is cached in sev.c, use that instead of sending it to the SEV guest driver and getting the same address back.
--- arch/x86/include/asm/sev.h | 9 ++------- arch/x86/kernel/sev.c | 10 ++-------- drivers/virt/coco/sev-guest/sev-guest.c | 8 +------- 3 files changed, 5 insertions(+), 22 deletions(-)
arch/x86/include/asm/sev.h +2 -7
@@ -116,10 +116,6 @@ struct snp_req_data {unsigned long resp_gpa; }; -struct sev_guest_platform_data { -u64 secrets_gpa; -}; -#define VMPCK_MAX_NUM 4/*
@@ -324,7 +320,7 @@ u64 sev_get_status(void);void sev_show_status (void );bool snp_assign_vmpck (struct snp_guest_dev *snp_dev,unsigned int vmpck_id);bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev); -int snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa); +int snp_guest_messaging_init (struct snp_guest_dev *snp_dev);void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev);int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req,struct snp_guest_request_ioctl *rio);
@@ -393,8 +389,7 @@ static inline void sev_show_status(void) { }static inline bool snp_assign_vmpck (struct snp_guest_dev *snp_dev,unsigned int vmpck_id) {return false; }static inline bool snp_is_vmpck_empty (struct snp_guest_dev *snp_dev) {return true; } -static inline int -snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) {return -EINVAL ; } +static inline int snp_guest_messaging_init (struct snp_guest_dev *snp_dev) {return -EINVAL ; }static inline void snp_guest_messaging_exit (struct snp_guest_dev *snp_dev) { }static inline int snp_send_guest_request (struct snp_guest_dev *snp_dev,struct snp_guest_req *req,struct snp_guest_request_ioctl *rio) {return -EINVAL ; }
arch/x86/kernel/sev.c +2 -8
@@ -2239,15 +2239,9 @@ static struct platform_device sev_guest_device = {static int __init snp_init_platform_device (void ) { -struct sev_guest_platform_data data; -if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))return -ENODEV ; - data.secrets_gpa = secrets_pa; -if (platform_device_add_data (&sev_guest_device, &data,sizeof (data))) -return -ENODEV ; -if (platform_device_register (&sev_guest_device))return -ENODEV ;
@@ -2597,11 +2591,11 @@ int snp_send_guest_request(struct snp_guest_dev *snp_dev, }EXPORT_SYMBOL_GPL (snp_send_guest_request); -int snp_guest_messaging_init (struct snp_guest_dev *snp_dev,u64 secrets_gpa) +int snp_guest_messaging_init (struct snp_guest_dev *snp_dev) {int ret = -ENOMEM ; - snp_dev->secrets = (__force void *)ioremap_encrypted (secrets_gpa,PAGE_SIZE ); + snp_dev->secrets = (__force void *)ioremap_encrypted (secrets_pa,PAGE_SIZE );if (!snp_dev->secrets) {pr_err ("Failed to map SNP secrets page.\n" );return ret;
drivers/virt/coco/sev-guest/sev-guest.c +1 -7
@@ -406,7 +406,6 @@ static void unregister_sev_tsm(void *data)static int __init sev_guest_probe (struct platform_device *pdev) { -struct sev_guest_platform_data *data;struct device *dev = &pdev->dev;struct snp_guest_dev *snp_dev;struct miscdevice *misc;
@@ -415,11 +414,6 @@ static int __init sev_guest_probe(struct platform_device *pdev)if (!cc_platform_has (CC_ATTR_GUEST_SEV_SNP ))return -ENODEV ; -if (!dev->platform_data) -return -ENODEV ; - - data = (struct sev_guest_platform_data *)dev->platform_data; - snp_dev =devm_kzalloc (&pdev->dev,sizeof (struct snp_guest_dev ),GFP_KERNEL );if (!snp_dev)return -ENOMEM ;
@@ -430,7 +424,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)return ret; } -if (snp_guest_messaging_init (snp_dev, data->secrets_gpa)) { +if (snp_guest_messaging_init (snp_dev)) {dev_err (dev,"Unable to setup SNP Guest messaging using VMPCK%u\n" , snp_dev->vmpck_id);return ret;
Signature · 1 line
2.34.1
PATCH v9 17/24 x86/cc: Add CC_ATTR_GUEST_SECURE_TSC
In reply to Nikunj A DadhaniaAdd confidential compute platform attribute CC_ATTR_GUEST_SECURE_TSC that can be used by the guest to query whether the Secure TSC feature is active.
--- include/linux/cc_platform.h | 8 ++++++++ arch/x86/coco/core.c | 3 +++ 2 files changed, 11 insertions(+)
include/linux/cc_platform.h +8 -0
@@ -98,6 +98,14 @@ enum cc_attr {* enabled to run SEV-SNP guests. */ CC_ATTR_HOST_SEV_SNP , + +/** +* @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
arch/x86/coco/core.c +3 -0
@@ -101,6 +101,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)case CC_ATTR_HOST_SEV_SNP :return cc_flags.host_sev_snp; +case CC_ATTR_GUEST_SECURE_TSC : +return sev_status &MSR_AMD64_SNP_SECURE_TSC ; +default :return false; }
Signature · 1 line
2.34.1
PATCH v9 18/24 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 the hypervisor once the guest is launched.
Secure TSC enabled guests need to query TSC info from the AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal AES GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP.
Use the mem_encrypt_init() to fetch SNP TSC info from the AMD Security Processor and initialize the snp_tsc_scale and snp_tsc_offset. During secondary CPU initialization set VMSA fields GUEST_TSC_SCALE (offset 2F0h) and GUEST_TSC_OFFSET(offset 2F8h) with snp_tsc_scale and snp_tsc_offset respectively.
--- arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 22 ++++++++ arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 90 +++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 4 ++ 5 files changed, 121 insertions(+), 2 deletions(-)
arch/x86/include/asm/sev-common.h +1 -0
@@ -163,6 +163,7 @@ struct snp_psc_desc {#define GHCB_TERM_NOT_VMPL0 3/* SNP guest is not running at VMPL-0 */ #define GHCB_TERM_CPUID 4/* CPUID-validation failure */ #define GHCB_TERM_CPUID_HV 5/* CPUID failure during hypervisor fallback */ +#define GHCB_TERM_SECURE_TSC 6/* Secure TSC initialization failed */ #define GHCB_RESP_CODE (v) ((v) & GHCB_MSR_INFO_MASK)
arch/x86/include/asm/sev.h +22 -0
@@ -173,6 +173,9 @@ enum msg_type {SNP_MSG_VMRK_REQ ,SNP_MSG_VMRK_RSP , +SNP_MSG_TSC_INFO_REQ =17 , +SNP_MSG_TSC_INFO_RSP , + SNP_MSG_TYPE_MAX };
@@ -204,6 +207,22 @@ struct snp_guest_msg {#define SNP_GUEST_MSG_SIZE 4096#define SNP_GUEST_MSG_PAYLOAD_SIZE (SNP_GUEST_MSG_SIZE - sizeof(struct snp_guest_msg)) +#define SNP_TSC_INFO_REQ_SZ 128 +#define SNP_TSC_INFO_RESP_SZ 128 + +struct snp_tsc_info_req { +u8 rsvd[SNP_TSC_INFO_REQ_SZ ]; +} __packed; + +struct snp_tsc_info_resp { +u32 status; +u32 rsvd1; +u64 tsc_scale; +u64 tsc_offset; +u32 tsc_factor; +u8 rsvd2[100 ]; +} __packed; +struct snp_guest_dev {struct device *dev;struct miscdevice misc;
@@ -225,6 +244,7 @@ struct snp_guest_dev {struct snp_report_req report;struct snp_derived_key_req derived_key;struct snp_ext_report_req ext_report; +struct snp_tsc_info_req tsc_info; } req;unsigned int vmpck_id; };
@@ -362,6 +382,7 @@ static inline void *alloc_shared_pages(size_t sz) return page_address(page); } +void __init snp_secure_tsc_prepare (void );#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -395,6 +416,7 @@ static inline int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct s struct snp_guest_request_ioctl *rio) { return -EINVAL; }static inline void free_shared_pages (void *buf,size_t sz) { }static inline void *alloc_shared_pages (size_t sz) {return NULL ; } +static inline void __init snp_secure_tsc_prepare (void ) { }#endif #ifdef CONFIG_KVM_AMD_SEV
arch/x86/include/asm/svm.h +4 -2
@@ -410,7 +410,9 @@ struct sev_es_save_area {u8 reserved_0x298[80 ];u32 pkru;u32 tsc_aux; -u8 reserved_0x2f0[24 ]; +u64 tsc_scale; +u64 tsc_offset; +u8 reserved_0x300[8 ];u64 rcx;u64 rdx;u64 rbx;
@@ -542,7 +544,7 @@ static inline void __unused_size_checks(void)BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x1c0 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x248 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x298 ); -BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x2f0 ); +BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x300 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x320 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x380 );BUILD_BUG_RESERVED_OFFSET (sev_es_save_area,0x3f0 );
arch/x86/kernel/sev.c +90 -0
105 lines not on this page · raw message
arch/x86/mm/mem_encrypt.c +4 -0
@@ -94,6 +94,10 @@ void __init mem_encrypt_init(void)/* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes (); +/* Initialize SNP Secure TSC */ +if (cc_platform_has (CC_ATTR_GUEST_SEV_SNP )) +snp_secure_tsc_prepare (); +print_mem_encrypt_feature_info (); }
Signature · 1 line
2.34.1
PATCH v9 19/24 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
@@ -1191,6 +1191,30 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)/* Is it a WRMSR? */ exit_info_1 = (ctxt->insn.opcode.bytes[1 ] ==0x30 ) ?1 :0 ; +/* +* TSC related accesses should not exit to the hypervisor when a +* guest is executing with SecureTSC enabled, so special handling +* is required for accesses of MSR_IA32_TSC: +* +* Writes: Writing to MSR_IA32_TSC can cause subsequent reads +* of the TSC to return undefined values, so ignore all +* writes. +* Reads: Reads of MSR_IA32_TSC should return the current TSC +* value, use the value returned by RDTSC. +*/ +if (regs->cx ==MSR_IA32_TSC &&cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) { +u64 tsc; + +if (exit_info_1) +return ES_OK ; + + tsc =rdtsc (); + regs->ax =UINT_MAX & tsc; + regs->dx =UINT_MAX & (tsc >>32 ); + +return ES_OK ; + } +ghcb_set_rcx (ghcb, regs->cx);if (exit_info_1) {ghcb_set_rax (ghcb, regs->ax);
Signature · 1 line
2.34.1
PATCH v9 20/24 x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests
In reply to Nikunj A DadhaniaThe hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions are being intercepted. If this should occur and Secure TSC is enabled, terminate guest execution.
--- arch/x86/kernel/sev-shared.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kernel/sev-shared.c +10 -0
@@ -998,6 +998,16 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,bool rdtscp = (exit_code ==SVM_EXIT_RDTSCP );enum es_result ret; +/* +* RDTSC and RDTSCP should not be intercepted when Secure TSC is +* enabled. Terminate the SNP guest when the interception is enabled. +* This file is included from kernel/sev.c and boot/compressed/sev.c, +* use sev_status here as cc_platform_has() is not available when +* compiling boot/compressed/sev.c. +*/ +if (sev_status &MSR_AMD64_SNP_SECURE_TSC ) +return ES_VMM_ERROR ; + ret =sev_es_ghcb_hv_call (ghcb, ctxt, exit_code,0 ,0 );if (ret !=ES_OK )return ret;
Signature · 1 line
2.34.1
PATCH v9 21/24 x86/kvmclock: Skip kvmclock when Secure TSC is available
In reply to Nikunj A DadhaniaFor AMD SNP guests having Secure TSC enabled, skip using the kvmclock. The guest kernel will fallback and use Secure TSC based clocksource.
--- arch/x86/kernel/kvmclock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kvmclock.c +1 -1
@@ -289,7 +289,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
PATCH v9 22/24 x86/sev: Mark Secure TSC as reliable
In reply to Nikunj A DadhaniaAMD SNP guests may have Secure TSC feature enabled. Use the Secure TSC as the only reliable clock source in SEV-SNP guests when enabled, bypassing unstable calibration.
--- arch/x86/mm/mem_encrypt_amd.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/mm/mem_encrypt_amd.c +4 -0
@@ -510,6 +510,10 @@ void __init sme_early_init(void)*/ x86_init.resources.dmi_setup = snp_dmi_setup; } + +/* Mark the TSC as reliable when Secure TSC is enabled */ +if (sev_status &MSR_AMD64_SNP_SECURE_TSC ) +setup_force_cpu_cap (X86_FEATURE_TSC_RELIABLE ); }void __init mem_encrypt_free_decrypted_mem (void )
Signature · 1 line
2.34.1
PATCH v9 23/24 x86/cpu/amd: Do not print FW_BUG for Secure TSC
In reply to Nikunj A DadhaniaWhen SecureTSC is enabled and TscInvariant (bit 8) in CPUID_8000_0007_edx is set, kernel complains with the below firmware bug:
[Firmware Bug]: TSC doesn't count with P0 frequency!
Secure TSC need not run at P0 frequency, the TSC frequency is set by the VMM as part of the SNP_LAUNCH_START command. Avoid the check when Secure TSC is enabled
--- arch/x86/kernel/cpu/amd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +2 -1
@@ -370,7 +370,8 @@ static void bsp_determine_snp(struct cpuinfo_x86 *c)static void bsp_init_amd (struct cpuinfo_x86 *c) { -if (cpu_has (c,X86_FEATURE_CONSTANT_TSC )) { +if (cpu_has (c,X86_FEATURE_CONSTANT_TSC ) && + !cc_platform_has (CC_ATTR_GUEST_SECURE_TSC )) {if (c->x86 >0x10 || (c->x86 ==0x10 && c->x86_model >=0x2 )) {
Signature · 1 line
2.34.1
PATCH v9 24/24 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.
--- arch/x86/boot/compressed/sev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/sev.c +2 -1
@@ -363,7 +363,8 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)* by the guest kernel. As and when a new feature is implemented in the * guest kernel, a corresponding bit should be added to the mask. */ -#define SNP_FEATURES_PRESENT MSR_AMD64_SNP_DEBUG_SWAP +#define SNP_FEATURES_PRESENT (MSR_AMD64_SNP_DEBUG_SWAP | \ + MSR_AMD64_SNP_SECURE_TSC)u64 snp_get_unsupported_features (u64 status) {
Signature · 1 line
2.34.1