Skip to contentSkip to conversation

Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support

RFC PATCH Superseded Michael Roth 22 participants 205 messages Dec 14, 2022 – May 15, 2023 Page 2 of 2

Subsystems (12)

v7 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
maintainer
#111 lore raw

Re: [PATCH RFC v7 29/64] crypto: ccp: Handle the legacy SEV command when SNP is enabled

In reply to Alper Gun

Hello Alper,

Quoted 374 lines · Alper Gun
Quoted 373 lines · Michael Roth

From: Brijesh Singh <[email protected]>

The behavior of the SEV-legacy commands is altered when the SNP firmware is in the INIT state. When SNP is in INIT state, all the SEV-legacy commands that cause the firmware to write to memory must be in the firmware state before issuing the command..

A command buffer may contains a system physical address that the firmware may write to. There are two cases that need to be handled:

1) system physical address points to a guest memory
2) system physical address points to a host memory

To handle the case #1, change the page state to the firmware in the RMP table before issuing the command and restore the state to shared after the command completes.

For the case #2, use a bounce buffer to complete the request.

---
  drivers/crypto/ccp/sev-dev.c | 370 ++++++++++++++++++++++++++++++++++-
  drivers/crypto/ccp/sev-dev.h |  12 ++
  2 files changed, 372 insertions(+), 10 deletions(-)
drivers/crypto/ccp/sev-dev.c +315 -2
index 4c12e98a1219..5eb2e8f364d4 100644
@@ -286,6 +286,30 @@ static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, boo
         return rc;
  }
 
+static int rmp_mark_pages_shared(unsigned long paddr, unsigned int npages)
+{
+       /* Cbit maybe set in the paddr */
+       unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT;
+       int rc, n = 0, i;
+
+       for (i = 0; i < npages; i++, pfn++, n++) {
+               rc = rmp_make_shared(pfn, PG_LEVEL_4K);
+               if (rc)
+                       goto cleanup;
+       }
+
+       return 0;
+
+cleanup:
+       /*
+        * If failed to change the page state to shared, then its not safe
+        * to release the page back to the system, leak it.
+        */
+       snp_mark_pages_offline(pfn, npages - n);
+
+       return rc;
+}
+
  static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked)
  {
         unsigned long npages = 1ul << order, paddr;
@@ -487,12 +511,295 @@ static int sev_write_init_ex_file_if_required(int cmd_id)
         return sev_write_init_ex_file();
  }
 
+static int alloc_snp_host_map(struct sev_device *sev)
+{
+       struct page *page;
+       int i;
+
+       for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) {
+               struct snp_host_map *map = &sev->snp_host_map[i];
+
+               memset(map, 0, sizeof(*map));
+
+               page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(SEV_FW_BLOB_MAX_SIZE));
+               if (!page)
+                       return -ENOMEM;
+
+               map->host = page_address(page);
+       }
+
+       return 0;
+}
+
+static void free_snp_host_map(struct sev_device *sev)
+{
+       int i;
+
+       for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) {
+               struct snp_host_map *map = &sev->snp_host_map[i];
+
+               if (map->host) {
+                       __free_pages(virt_to_page(map->host), get_order(SEV_FW_BLOB_MAX_SIZE));
+                       memset(map, 0, sizeof(*map));
+               }
+       }
+}
+
+static int map_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map)
+{
+       unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT;
+
+       map->active = false;
+
+       if (!paddr || !len)
+               return 0;
+
+       map->paddr = *paddr;
+       map->len = len;
+
+       /* If paddr points to a guest memory then change the page state to firmwware. */
+       if (guest) {
+               if (rmp_mark_pages_firmware(*paddr, npages, true))
+                       return -EFAULT;
+
+               goto done;
+       }
+
+       if (!map->host)
+               return -ENOMEM;
+
+       /* Check if the pre-allocated buffer can be used to fullfil the request. */
+       if (len > SEV_FW_BLOB_MAX_SIZE)
+               return -EINVAL;
+
+       /* Transition the pre-allocated buffer to the firmware state. */
+       if (rmp_mark_pages_firmware(__pa(map->host), npages, true))
+               return -EFAULT;
+
+       /* Set the paddr to use pre-allocated firmware buffer */
+       *paddr = __psp_pa(map->host);
+
+done:
+       map->active = true;
+       return 0;
+}
+
+static int unmap_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map)
+{
+       unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT;
+
+       if (!map->active)
+               return 0;
+
+       /* If paddr points to a guest memory then restore the page state to hypervisor. */
+       if (guest) {
+               if (snp_reclaim_pages(*paddr, npages, true))
+                       return -EFAULT;
+
+               goto done;
+       }
+
+       /*
+        * Transition the pre-allocated buffer to hypervisor state before the access.
+        *
+        * This is because while changing the page state to firmware, the kernel unmaps
+        * the pages from the direct map, and to restore the direct map the pages must
+        * be transitioned back to the shared state.
+        */
+       if (snp_reclaim_pages(__pa(map->host), npages, true))
+               return -EFAULT;
+
+       /* Copy the response data firmware buffer to the callers buffer. */
+       memcpy(__va(__sme_clr(map->paddr)), map->host, min_t(size_t, len, map->len));
+       *paddr = map->paddr;
+
+done:
+       map->active = false;
+       return 0;
+}
+
+static bool sev_legacy_cmd_buf_writable(int cmd)
+{
+       switch (cmd) {
+       case SEV_CMD_PLATFORM_STATUS:
+       case SEV_CMD_GUEST_STATUS:
+       case SEV_CMD_LAUNCH_START:
+       case SEV_CMD_RECEIVE_START:
+       case SEV_CMD_LAUNCH_MEASURE:
+       case SEV_CMD_SEND_START:
+       case SEV_CMD_SEND_UPDATE_DATA:
+       case SEV_CMD_SEND_UPDATE_VMSA:
+       case SEV_CMD_PEK_CSR:
+       case SEV_CMD_PDH_CERT_EXPORT:
+       case SEV_CMD_GET_ID:
+       case SEV_CMD_ATTESTATION_REPORT:
+               return true;
+       default:
+               return false;
+       }
+}
+
+#define prep_buffer(name, addr, len, guest, map) \
+       func(&((typeof(name *))cmd_buf)->addr, ((typeof(name *))cmd_buf)->len, guest, map)
+
+static int __snp_cmd_buf_copy(int cmd, void *cmd_buf, bool to_fw, int fw_err)
+{
+       int (*func)(u64 *paddr, u32 len, bool guest, struct snp_host_map *map);
+       struct sev_device *sev = psp_master->sev_data;
+       bool from_fw = !to_fw;
+
+       /*
+        * After the command is completed, change the command buffer memory to
+        * hypervisor state.
+        *
+        * The immutable bit is automatically cleared by the firmware, so
+        * no not need to reclaim the page.
+        */
+       if (from_fw && sev_legacy_cmd_buf_writable(cmd)) {
+               if (rmp_mark_pages_shared(__pa(cmd_buf), 1))
+                       return -EFAULT;
+
+               /* No need to go further if firmware failed to execute command. */
+               if (fw_err)
+                       return 0;
+       }
+
+       if (to_fw)
+               func = map_firmware_writeable;
+       else
+               func = unmap_firmware_writeable;
+
+       /*
+        * A command buffer may contains a system physical address. If the address
+        * points to a host memory then use an intermediate firmware page otherwise
+        * change the page state in the RMP table.
+        */
+       switch (cmd) {
+       case SEV_CMD_PDH_CERT_EXPORT:
+               if (prep_buffer(struct sev_data_pdh_cert_export, pdh_cert_address,
+                               pdh_cert_len, false, &sev->snp_host_map[0]))
+                       goto err;
+               if (prep_buffer(struct sev_data_pdh_cert_export, cert_chain_address,
+                               cert_chain_len, false, &sev->snp_host_map[1]))
+                       goto err;
+               break;
+       case SEV_CMD_GET_ID:
+               if (prep_buffer(struct sev_data_get_id, address, len,
+                               false, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_PEK_CSR:
+               if (prep_buffer(struct sev_data_pek_csr, address, len,
+                               false, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_LAUNCH_UPDATE_DATA:
+               if (prep_buffer(struct sev_data_launch_update_data, address, len,
+                               true, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_LAUNCH_UPDATE_VMSA:
+               if (prep_buffer(struct sev_data_launch_update_vmsa, address, len,
+                               true, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_LAUNCH_MEASURE:
+               if (prep_buffer(struct sev_data_launch_measure, address, len,
+                               false, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_LAUNCH_UPDATE_SECRET:
+               if (prep_buffer(struct sev_data_launch_secret, guest_address, guest_len,
+                               true, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_DBG_DECRYPT:
+               if (prep_buffer(struct sev_data_dbg, dst_addr, len, false,
+                               &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_DBG_ENCRYPT:
+               if (prep_buffer(struct sev_data_dbg, dst_addr, len, true,
+                               &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_ATTESTATION_REPORT:
+               if (prep_buffer(struct sev_data_attestation_report, address, len,
+                               false, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_SEND_START:
+               if (prep_buffer(struct sev_data_send_start, session_address,
+                               session_len, false, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_SEND_UPDATE_DATA:
+               if (prep_buffer(struct sev_data_send_update_data, hdr_address, hdr_len,
+                               false, &sev->snp_host_map[0]))
+                       goto err;
+               if (prep_buffer(struct sev_data_send_update_data, trans_address,
+                               trans_len, false, &sev->snp_host_map[1]))
+                       goto err;
+               break;
+       case SEV_CMD_SEND_UPDATE_VMSA:
+               if (prep_buffer(struct sev_data_send_update_vmsa, hdr_address, hdr_len,
+                               false, &sev->snp_host_map[0]))
+                       goto err;
+               if (prep_buffer(struct sev_data_send_update_vmsa, trans_address,
+                               trans_len, false, &sev->snp_host_map[1]))
+                       goto err;
+               break;
+       case SEV_CMD_RECEIVE_UPDATE_DATA:
+               if (prep_buffer(struct sev_data_receive_update_data, guest_address,
+                               guest_len, true, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       case SEV_CMD_RECEIVE_UPDATE_VMSA:
+               if (prep_buffer(struct sev_data_receive_update_vmsa, guest_address,
+                               guest_len, true, &sev->snp_host_map[0]))
+                       goto err;
+               break;
+       default:
+               break;
+       }
+
+       /* The command buffer need to be in the firmware state. */
+       if (to_fw && sev_legacy_cmd_buf_writable(cmd)) {
+               if (rmp_mark_pages_firmware(__pa(cmd_buf), 1, true))
+                       return -EFAULT;
+       }
+
+       return 0;
+
+err:
+       return -EINVAL;
+}
+
+static inline bool need_firmware_copy(int cmd)
+{
+       struct sev_device *sev = psp_master->sev_data;
+
+       /* After SNP is INIT'ed, the behavior of legacy SEV command is changed. */
+       return ((cmd < SEV_CMD_SNP_INIT) && sev->snp_initialized) ? true : false;
+}
+
+static int snp_aware_copy_to_firmware(int cmd, void *data)
+{
+       return __snp_cmd_buf_copy(cmd, data, true, 0);
+}
+
+static int snp_aware_copy_from_firmware(int cmd, void *data, int fw_err)
+{
+       return __snp_cmd_buf_copy(cmd, data, false, fw_err);
+}
+
  static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
  {
         struct psp_device *psp = psp_master;
         struct sev_device *sev;
         unsigned int phys_lsb, phys_msb;
         unsigned int reg, ret = 0;
+       void *cmd_buf;
         int buf_len;
 
         if (!psp || !psp->sev_data)
@@ -512,12 +819,28 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
          * work for some memory, e.g. vmalloc'd addresses, and @data may not be
          * physically contiguous.
          */
-       if (data)
-               memcpy(sev->cmd_buf, data, buf_len);
+       if (data) {
+               if (sev->cmd_buf_active > 2)
+                       return -EBUSY;
+
+               cmd_buf = sev->cmd_buf_active ? sev->cmd_buf_backup : sev->cmd_buf;
+
+               memcpy(cmd_buf, data, buf_len);
+               sev->cmd_buf_active++;
Quoted 8 lines

drivers/crypto/ccp/sev-dev.c static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) · line 835

Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6 -0
+
+               /*
+                * The behavior of the SEV-legacy commands is altered when the
+                * SNP firmware is in the INIT state.
+                */
+               if (need_firmware_copy(cmd) && snp_aware_copy_to_firmware(cmd, sev->cmd_buf))

I believe this should be cmd_buf instead of sev->cmd_buf. snp_aware_copy_to_firmware(cmd, cmd_buf)

Yes, you are right, this should be cmd_buf instead of sev->cmd_buf, will fix this accordingly.

Thanks,
Ashish

Quoted 141 lines
Quoted 141 lines
drivers/crypto/ccp/sev-dev.c +39 -8
+                       return -EFAULT;
+       } else {
+               cmd_buf = sev->cmd_buf;
+       }
 
         /* Get the physical address of the command buffer */
-       phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0;
-       phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0;
+       phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0;
+       phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0;
 
         dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
                 cmd, phys_msb, phys_lsb, psp_timeout);
@@ -560,15 +883,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
                 ret = sev_write_init_ex_file_if_required(cmd);
         }
 
-       print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
-                            buf_len, false);
-
         /*
          * Copy potential output from the PSP back to data.  Do this even on
          * failure in case the caller wants to glean something from the error.
          */
-       if (data)
-               memcpy(data, sev->cmd_buf, buf_len);
+       if (data) {
+               /*
+                * Restore the page state after the command completes.
+                */
+               if (need_firmware_copy(cmd) &&
+                   snp_aware_copy_from_firmware(cmd, cmd_buf, ret))
+                       return -EFAULT;
+
+               memcpy(data, cmd_buf, buf_len);
+               sev->cmd_buf_active--;
+       }
+
+       print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
+                            buf_len, false);
 
         return ret;
  }
@@ -1579,10 +1911,12 @@ int sev_dev_init(struct psp_device *psp)
         if (!sev)
                 goto e_err;
 
-       sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
+       sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1);
         if (!sev->cmd_buf)
                 goto e_sev;
 
+       sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE;
+
         psp->sev_data = sev;
 
         sev->dev = dev;
@@ -1648,6 +1982,12 @@ static void sev_firmware_shutdown(struct sev_device *sev)
                 snp_range_list = NULL;
         }
 
+       /*
+        * The host map need to clear the immutable bit so it must be free'd before the
+        * SNP firmware shutdown.
+        */
+       free_snp_host_map(sev);
+
         sev_snp_shutdown(&error);
  }
 
@@ -1722,6 +2062,14 @@ void sev_pci_init(void)
                                 dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error);
                         }
                 }
+
+               /*
+                * Allocate the intermediate buffers used for the legacy command handling.
+                */
+               if (alloc_snp_host_map(sev)) {
+                       dev_notice(sev->dev, "Failed to alloc host map (disabling legacy SEV)\n");
+                       goto skip_legacy;
+               }
         }
 
         /* Obtain the TMR memory area for SEV-ES use */
@@ -1739,12 +2087,14 @@ void sev_pci_init(void)
                 dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n",
                         error, rc);
 
+skip_legacy:
         dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_initialized ?
                 "-SNP" : "", sev->api_major, sev->api_minor, sev->build);
 
         return;
 
  err:
+       free_snp_host_map(sev);
         psp_master->sev_data = NULL;
  }
 
drivers/crypto/ccp/sev-dev.h +12 -1
index 34767657beb5..19d79f9d4212 100644
@@ -29,11 +29,20 @@
  #define SEV_CMDRESP_CMD_SHIFT          16
  #define SEV_CMDRESP_IOC                        BIT(0)
 
+#define MAX_SNP_HOST_MAP_BUFS          2
+
  struct sev_misc_dev {
         struct kref refcount;
         struct miscdevice misc;
  };
 
+struct snp_host_map {
+       u64 paddr;
+       u32 len;
+       void *host;
+       bool active;
+};
+
  struct sev_device {
         struct device *dev;
         struct psp_device *psp;
@@ -52,8 +61,11 @@ struct sev_device {
         u8 build;
 
         void *cmd_buf;
+       void *cmd_buf_backup;
+       int cmd_buf_active;
 
         bool snp_initialized;
+       struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS];
  };
 
  int sev_dev_init(struct psp_device *psp);
--

2.25.1

#112 lore raw

RFC PATCH v7 30/64 crypto: ccp: Add the SNP_PLATFORM_STATUS command

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.

---
 Documentation/virt/coco/sev-guest.rst | 27 ++++++++++++++++
 drivers/crypto/ccp/sev-dev.c          | 45 +++++++++++++++++++++++++++
 include/uapi/linux/psp-sev.h          |  1 +
 3 files changed, 73 insertions(+)
Documentation/virt/coco/sev-guest.rst +27 -0
index bf593e88cfd9..11ea67c944df 100644
@@ -61,6 +61,22 @@ counter (e.g. counter overflow), then -EIO will be returned.
                 __u64 fw_err;
         };
 
+The host ioctl should be called to /dev/sev device. The ioctl accepts command
+id and command input structure.
+
+::
+        struct sev_issue_cmd {
+                /* Command ID */
+                __u32 cmd;
+
+                /* Command request structure */
+                __u64 data;
+
+                /* firmware error code on failure (see psp-sev.h) */
+                __u32 error;
+        };
+
+
 2.1 SNP_GET_REPORT
 ------------------
 
@@ -118,6 +134,17 @@ be updated with the expected value.
 
 See GHCB specification for further detail on how to parse the certificate blob.
 
+2.4 SNP_PLATFORM_STATUS
+-----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): struct sev_data_snp_platform_status
+:Returns (out): 0 on success, -negative on error
+
+The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The
+status includes API major, minor version and more. See the SEV-SNP
+specification for further details.
+
 3. SEV-SNP CPUID Enforcement
 ============================
 
drivers/crypto/ccp/sev-dev.c +45 -0

57 lines not on this page · raw message

include/uapi/linux/psp-sev.h +1 -0
index bed65a891223..ffd60e8b0a31 100644
@@ -28,6 +28,7 @@ enum {
 	SEV_PEK_CERT_IMPORT,
 	SEV_GET_ID,	/* This command is deprecated, use SEV_GET_ID2 */
 	SEV_GET_ID2,
+	SNP_PLATFORM_STATUS,
 
 	SEV_MAX,
 };
Signature · 1 line
2.25.1
#113 lore raw

RFC PATCH v7 31/64 crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The SEV-SNP firmware provides the SNP_CONFIG command used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.

Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.

In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the SNP_SET_EXT_CONFIG command to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.

While it is not defined in the spec, but let's add SNP_GET_EXT_CONFIG command that can be used to obtain the data programmed through the SNP_SET_EXT_CONFIG.

---
 Documentation/virt/coco/sev-guest.rst |  27 ++++++
 drivers/crypto/ccp/sev-dev.c          | 123 ++++++++++++++++++++++++++
 drivers/crypto/ccp/sev-dev.h          |   4 +
 include/uapi/linux/psp-sev.h          |  17 ++++
 4 files changed, 171 insertions(+)
Documentation/virt/coco/sev-guest.rst +27 -0
index 11ea67c944df..fad1e5639dac 100644
@@ -145,6 +145,33 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The
 status includes API major, minor version and more. See the SEV-SNP
 specification for further details.
 
+2.5 SNP_SET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): struct sev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on error
+
+The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as
+reported TCB version in the attestation report. The command is similar to
+SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the
+command also accepts an additional certificate blob defined in the GHCB
+specification.
+
+If the certs_address is zero, then the previous certificate blob will deleted.
+For more information on the certificate blob layout, see the GHCB spec
+(extended guest request message).
+
+2.6 SNP_GET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): struct sev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on error
+
+The SNP_SET_EXT_CONFIG is used to query the system-wide configuration set
+through the SNP_SET_EXT_CONFIG.
+
 3. SEV-SNP CPUID Enforcement
 ============================
 
drivers/crypto/ccp/sev-dev.c +123 -0

147 lines not on this page · raw message

drivers/crypto/ccp/sev-dev.h +4 -0
index 19d79f9d4212..41d5353d5bab 100644
@@ -66,6 +66,10 @@ struct sev_device {
 
 	bool snp_initialized;
 	struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS];
+	void *snp_certs_data;
+	u32 snp_certs_len;
+	struct mutex snp_certs_lock;
+	struct sev_user_data_snp_config snp_config;
 };
 
 int sev_dev_init(struct psp_device *psp);
include/uapi/linux/psp-sev.h +17 -0
index ffd60e8b0a31..60e7a8d1a18e 100644
@@ -29,6 +29,8 @@ enum {
 	SEV_GET_ID,	/* This command is deprecated, use SEV_GET_ID2 */
 	SEV_GET_ID2,
 	SNP_PLATFORM_STATUS,
+	SNP_SET_EXT_CONFIG,
+	SNP_GET_EXT_CONFIG,
 
 	SEV_MAX,
 };
@@ -190,6 +192,21 @@ struct sev_user_data_snp_config {
 	__u8 rsvd[52];
 } __packed;
 
+/**
+ * struct sev_data_snp_ext_config - system wide configuration value for SNP.
+ *
+ * @config_address: address of the struct sev_user_data_snp_config or 0 when
+ *		reported_tcb does not need to be updated.
+ * @certs_address: address of extended guest request certificate chain or
+ *              0 when previous certificate should be removed on SNP_SET_EXT_CONFIG.
+ * @certs_len: length of the certs
+ */
+struct sev_user_data_ext_snp_config {
+	__u64 config_address;		/* In */
+	__u64 certs_address;		/* In */
+	__u32 certs_len;		/* In */
+};
+
 /**
  * struct sev_issue_cmd - SEV ioctl parameters
  *
Signature · 1 line
2.25.1
#114 lore raw

Re: [PATCH RFC v7 31/64] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command

Hi Mike,

Quoted 54 lines · Michael Roth

From: Brijesh Singh <[email protected]>

The SEV-SNP firmware provides the SNP_CONFIG command used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.

Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.

In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the SNP_SET_EXT_CONFIG command to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.

While it is not defined in the spec, but let's add SNP_GET_EXT_CONFIG command that can be used to obtain the data programmed through the SNP_SET_EXT_CONFIG.

---
 Documentation/virt/coco/sev-guest.rst |  27 ++++++
 drivers/crypto/ccp/sev-dev.c          | 123 ++++++++++++++++++++++++++
 drivers/crypto/ccp/sev-dev.h          |   4 +
 include/uapi/linux/psp-sev.h          |  17 ++++
 4 files changed, 171 insertions(+)
Documentation/virt/coco/sev-guest.rst +15 -0
index 11ea67c944df..fad1e5639dac 100644
@@ -145,6 +145,33 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The
 status includes API major, minor version and more. See the SEV-SNP
 specification for further details.
 
+2.5 SNP_SET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): struct sev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on error
+
+The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as
+reported TCB version in the attestation report. The command is similar to
+SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the
+command also accepts an additional certificate blob defined in the GHCB
+specification.
+
+If the certs_address is zero, then the previous certificate blob will deleted.
+For more information on the certificate blob layout, see the GHCB spec

Documentation/virt/coco/sev-guest.rst The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The · line 172

Quoted 10 lines
Documentation/virt/coco/sev-guest.rst +10 -0
+(extended guest request message).
+
+2.6 SNP_GET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): struct sev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on error
+
+The SNP_SET_EXT_CONFIG is used to query the system-wide configuration set
       ^^^^^^^^^^^^^^^^^^

This should be SNP_GET_EXT_CONFIG.

-Dov

Quoted 5 lines
Documentation/virt/coco/sev-guest.rst +2 -0
+through the SNP_SET_EXT_CONFIG.
+
 3. SEV-SNP CPUID Enforcement
 ============================
 
#115 lore raw

RFC PATCH v7 32/64 crypto: ccp: Provide APIs to query extended attestation report

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the SNP_SET_EXT_CONFIG.

The snp_guest_ext_guest_request() will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.

---
 drivers/crypto/ccp/sev-dev.c | 48 ++++++++++++++++++++++++++++++++++++
 include/linux/psp-sev.h      | 33 +++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
drivers/crypto/ccp/sev-dev.c +48 -0
index d59727ac2bdd..d4f13e5a8dde 100644
@@ -27,6 +27,7 @@
 
 #include <asm/smp.h>
 #include <asm/e820/types.h>
+#include <asm/sev.h>
 
 #include "psp-dev.h"
 #include "sev-dev.h"
@@ -2016,6 +2017,53 @@ int sev_guest_df_flush(int *error)
 }
 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
 
+int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
+				unsigned long vaddr, unsigned long *npages, unsigned long *fw_err)
+{
+	unsigned long expected_npages;
+	struct sev_device *sev;
+	int rc;
+
+	if (!psp_master || !psp_master->sev_data)
+		return -ENODEV;
+
+	sev = psp_master->sev_data;
+
+	if (!sev->snp_initialized)
+		return -EINVAL;
+
+	mutex_lock(&sev->snp_certs_lock);
+	/*
+	 * Check if there is enough space to copy the certificate chain. Otherwise
+	 * return ERROR code defined in the GHCB specification.
+	 */
+	expected_npages = sev->snp_certs_len >> PAGE_SHIFT;
+	if (*npages < expected_npages) {
+		*npages = expected_npages;
+		*fw_err = SNP_GUEST_REQ_INVALID_LEN;
+		mutex_unlock(&sev->snp_certs_lock);
+		return -EINVAL;
+	}
+
+	rc = sev_do_cmd(SEV_CMD_SNP_GUEST_REQUEST, data, (int *)fw_err);
+	if (rc) {
+		mutex_unlock(&sev->snp_certs_lock);
+		return rc;
+	}
+
+	/* Copy the certificate blob */
+	if (sev->snp_certs_data) {
+		*npages = expected_npages;
+		memcpy((void *)vaddr, sev->snp_certs_data, *npages << PAGE_SHIFT);
+	} else {
+		*npages = 0;
+	}
+
+	mutex_unlock(&sev->snp_certs_lock);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(snp_guest_ext_guest_request);
+
 static void sev_exit(struct kref *ref)
 {
 	misc_deregister(&misc_dev->misc);
include/linux/psp-sev.h +33 -0
index d19744807471..81bafc049eca 100644
@@ -931,6 +931,32 @@ void snp_free_firmware_page(void *addr);
  */
 void snp_mark_pages_offline(unsigned long pfn, unsigned int npages);
 
+/**
+ * snp_guest_ext_guest_request - perform the SNP extended guest request command
+ *  defined in the GHCB specification.
+ *
+ * @data: the input guest request structure
+ * @vaddr: address where the certificate blob need to be copied.
+ * @npages: number of pages for the certificate blob.
+ *    If the specified page count is less than the certificate blob size, then the
+ *    required page count is returned with error code defined in the GHCB spec.
+ *    If the specified page count is more than the certificate blob size, then
+ *    page count is updated to reflect the amount of valid data copied in the
+ *    vaddr.
+ *
+ * @sev_ret: sev command return code
+ *
+ * Returns:
+ * 0 if the sev successfully processed the command
+ * -%ENODEV    if the sev device is not available
+ * -%ENOTSUPP  if the sev does not support SEV
+ * -%ETIMEDOUT if the sev command timed out
+ * -%EIO       if the sev returned a non-zero return code
+ */
+int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
+				unsigned long vaddr, unsigned long *npages,
+				unsigned long *error);
+
 #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
 static inline int
@@ -968,6 +994,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
 
 static inline void snp_free_firmware_page(void *addr) { }
 
+static inline int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
+					      unsigned long vaddr, unsigned long *n,
+					      unsigned long *error)
+{
+	return -ENODEV;
+}
+
 #endif	/* CONFIG_CRYPTO_DEV_SP_PSP */
 
 #endif	/* __PSP_SEV_H__ */
Signature · 1 line
2.25.1
#116 lore raw

RFC PATCH v7 33/64 KVM: SVM: Add support to handle AP reset MSR protocol

In reply to Michael Roth

From: Tom Lendacky <[email protected]>

Add support for AP Reset Hold being invoked using the GHCB MSR protocol, available in version 2 of the GHCB specification.

---
 arch/x86/include/asm/sev-common.h |  2 ++
 arch/x86/kvm/svm/sev.c            | 56 ++++++++++++++++++++++++++-----
 arch/x86/kvm/svm/svm.h            |  1 +
 3 files changed, 51 insertions(+), 8 deletions(-)
arch/x86/include/asm/sev-common.h +2 -0
index b8357d6ecd47..e15548d88f2a 100644
@@ -56,6 +56,8 @@
 /* AP Reset Hold */
 #define GHCB_MSR_AP_RESET_HOLD_REQ	0x006
 #define GHCB_MSR_AP_RESET_HOLD_RESP	0x007
+#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS	12
+#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK	GENMASK_ULL(51, 0)
 
 /* GHCB GPA Register */
 #define GHCB_MSR_REG_GPA_REQ		0x012
arch/x86/kvm/svm/sev.c +48 -8

87 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +1 -0
index c760ec51a910..cb9da04e745a 100644
@@ -198,6 +198,7 @@ struct vcpu_sev_es_state {
 	struct ghcb *ghcb;
 	struct kvm_host_map ghcb_map;
 	bool received_first_sipi;
+	unsigned int ap_reset_hold_type;
 
 	/* SEV-ES scratch area support */
 	void *ghcb_sa;
Signature · 1 line
2.25.1
#117 lore raw

RFC PATCH v7 34/64 KVM: SVM: Provide the Hypervisor Feature support VMGEXIT

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor.

Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version.

---
 arch/x86/include/asm/sev-common.h |  2 ++
 arch/x86/kvm/svm/sev.c            | 14 ++++++++++++++
 arch/x86/kvm/svm/svm.h            |  3 ++-
 3 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/include/asm/sev-common.h +2 -0
index e15548d88f2a..539de6b93420 100644
@@ -101,6 +101,8 @@ enum psc_op {
 /* GHCB Hypervisor Feature Request/Response */
 #define GHCB_MSR_HV_FT_REQ		0x080
 #define GHCB_MSR_HV_FT_RESP		0x081
+#define GHCB_MSR_HV_FT_POS		12
+#define GHCB_MSR_HV_FT_MASK		GENMASK_ULL(51, 0)
 #define GHCB_MSR_HV_FT_RESP_VAL(v)			\
 	/* GHCBData[63:12] */				\
 	(((u64)(v) & GENMASK_ULL(63, 12)) >> 12)
arch/x86/kvm/svm/sev.c +14 -0
index 244c58bd3de7..82ff96b4f04a 100644
@@ -2667,6 +2667,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 	case SVM_VMGEXIT_AP_HLT_LOOP:
 	case SVM_VMGEXIT_AP_JUMP_TABLE:
 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
+	case SVM_VMGEXIT_HV_FEATURES:
 		break;
 	default:
 		reason = GHCB_ERR_INVALID_EVENT;
@@ -2933,6 +2934,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 				  GHCB_MSR_INFO_MASK,
 				  GHCB_MSR_INFO_POS);
 		break;
+	case GHCB_MSR_HV_FT_REQ: {
+		set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED,
+				  GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS);
+		set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP,
+				  GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
+		break;
+	}
 	case GHCB_MSR_TERM_REQ: {
 		u64 reason_set, reason_code;
 
@@ -3057,6 +3065,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 		ret = 1;
 		break;
 	}
+	case SVM_VMGEXIT_HV_FEATURES: {
+		ghcb_set_sw_exit_info_2(ghcb, GHCB_HV_FT_SUPPORTED);
+
+		ret = 1;
+		break;
+	}
 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
 		vcpu_unimpl(vcpu,
 			    "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
arch/x86/kvm/svm/svm.h +2 -1
index cb9da04e745a..1f3098dff3d5 100644
@@ -654,9 +654,10 @@ void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
 
 /* sev.c */
 
-#define GHCB_VERSION_MAX	1ULL
+#define GHCB_VERSION_MAX	2ULL
 #define GHCB_VERSION_MIN	1ULL
 
+#define GHCB_HV_FT_SUPPORTED	0
 
 extern unsigned int max_sev_asid;
 
Signature · 1 line
2.25.1
#118 lore raw

RFC PATCH v7 35/64 KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.

When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This
is done for _all_ VMs, not just SNP-Active VMs.

If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.

The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled.

---
 arch/x86/include/asm/kvm-x86-ops.h |  1 +
 arch/x86/include/asm/kvm_host.h    |  2 ++
 arch/x86/kvm/lapic.c               |  5 ++++-
 arch/x86/kvm/svm/sev.c             | 33 ++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c             | 15 ++++++++++++--
 arch/x86/kvm/svm/svm.h             |  1 +
 6 files changed, 54 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index c71df44b0f02..e0015926cdf4 100644
@@ -131,6 +131,7 @@ KVM_X86_OP(msr_filter_changed)
 KVM_X86_OP(complete_emulated_msr)
 KVM_X86_OP(vcpu_deliver_sipi_vector)
 KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
 KVM_X86_OP_OPTIONAL_RET0(private_mem_enabled);
 KVM_X86_OP_OPTIONAL_RET0(fault_is_private);
 KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)
arch/x86/include/asm/kvm_host.h +2 -0
index 9ef8d73455d9..e2529415f28b 100644
@@ -1722,6 +1722,8 @@ struct kvm_x86_ops {
 	 * Returns vCPU specific APICv inhibit reasons
 	 */
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu);
 };
 
 struct kvm_x86_nested_ops {
arch/x86/kvm/lapic.c +4 -1
index 1bb63746e991..8500d1d54664 100644
@@ -2581,7 +2581,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
 
 	vcpu->arch.apic = apic;
 
-	apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
+	if (kvm_x86_ops.alloc_apic_backing_page)
+		apic->regs = static_call(kvm_x86_alloc_apic_backing_page)(vcpu);
+	else
+		apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 	if (!apic->regs) {
 		printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
 		       vcpu->vcpu_id);
arch/x86/kvm/svm/sev.c +33 -0
index 82ff96b4f04a..0e93b536dc34 100644
@@ -3234,6 +3234,39 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
 	}
 }
 
+struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu)
+{
+	unsigned long pfn;
+	struct page *p;
+
+	if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))
+		return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+
+	/*
+	 * Allocate an SNP safe page to workaround the SNP erratum where
+	 * the CPU will incorrectly signal an RMP violation  #PF if a
+	 * hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA
+	 * or AVIC backing page. The recommeded workaround is to not use the
+	 * hugepage.
+	 *
+	 * Allocate one extra page, use a page which is not 2mb aligned
+	 * and free the other.
+	 */
+	p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1);
+	if (!p)
+		return NULL;
+
+	split_page(p, 1);
+
+	pfn = page_to_pfn(p);
+	if (IS_ALIGNED(pfn, PTRS_PER_PMD))
+		__free_page(p++);
+	else
+		__free_page(p + 1);
+
+	return p;
+}
+
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault)
 {
 	gfn_t gfn = gpa_to_gfn(gpa);
arch/x86/kvm/svm/svm.c +13 -2
index fc7885869f7e..013f811c733c 100644
@@ -1360,7 +1360,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
 	svm = to_svm(vcpu);
 
 	err = -ENOMEM;
-	vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+	vmcb01_page = snp_safe_alloc_page(vcpu);
 	if (!vmcb01_page)
 		goto out;
 
@@ -1369,7 +1369,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
 		 * SEV-ES guests require a separate VMSA page used to contain
 		 * the encrypted register state of the guest.
 		 */
-		vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+		vmsa_page = snp_safe_alloc_page(vcpu);
 		if (!vmsa_page)
 			goto error_free_vmcb_page;
 
@@ -4694,6 +4694,16 @@ static int svm_vm_init(struct kvm *kvm)
 	return 0;
 }
 
+static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu)
+{
+	struct page *page = snp_safe_alloc_page(vcpu);
+
+	if (!page)
+		return NULL;
+
+	return page_address(page);
+}
+
 static int svm_private_mem_enabled(struct kvm *kvm)
 {
 	if (sev_guest(kvm))
@@ -4830,6 +4840,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 
 	.vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
 	.vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons,
+	.alloc_apic_backing_page = svm_alloc_apic_backing_page,
 
 	.fault_is_private = sev_fault_is_private,
 };
arch/x86/kvm/svm/svm.h +1 -0
index 1f3098dff3d5..ea9844546e8a 100644
@@ -684,6 +684,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm);
 void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
 void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);
 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
+struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
Signature · 1 line
2.25.1
#119 lore raw

RFC PATCH v7 36/64 KVM: SVM: Add initial SEV-SNP support

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The next generation of SEV is called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware based security protection. SEV-SNP adds strong memory encryption integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment.

The SNP feature is added incrementally, the later patches adds a new module parameters that can be used to enabled SEV-SNP in the KVM.

---
 arch/x86/kvm/svm/sev.c | 10 +++++++++-
 arch/x86/kvm/svm/svm.h |  8 ++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +9 -1
index 0e93b536dc34..f34da1203e09 100644
@@ -57,6 +57,9 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444);
 #define sev_es_enabled false
 #endif /* CONFIG_KVM_AMD_SEV */
 
+/* enable/disable SEV-SNP support */
+static bool sev_snp_enabled;
+
 #define AP_RESET_HOLD_NONE		0
 #define AP_RESET_HOLD_NAE_EVENT		1
 #define AP_RESET_HOLD_MSR_PROTO		2
@@ -2298,6 +2301,7 @@ void __init sev_hardware_setup(void)
 {
 #ifdef CONFIG_KVM_AMD_SEV
 	unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
+	bool sev_snp_supported = false;
 	bool sev_es_supported = false;
 	bool sev_supported = false;
 
@@ -2377,12 +2381,16 @@ void __init sev_hardware_setup(void)
 	if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
 		goto out;
 
-	pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
 	sev_es_supported = true;
+	sev_snp_supported = sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SEV_SNP);
+
+	pr_info("SEV-ES %ssupported: %u ASIDs\n",
+		sev_snp_supported ? "and SEV-SNP " : "", sev_es_asid_count);
 
 out:
 	sev_enabled = sev_supported;
 	sev_es_enabled = sev_es_supported;
+	sev_snp_enabled = sev_snp_supported;
 #endif
 }
 
arch/x86/kvm/svm/svm.h +8 -0
index ea9844546e8a..a48fe5d2bea5 100644
@@ -83,6 +83,7 @@ enum {
 struct kvm_sev_info {
 	bool active;		/* SEV enabled guest */
 	bool es_active;		/* SEV-ES enabled guest */
+	bool snp_active;	/* SEV-SNP enabled guest */
 	unsigned int asid;	/* ASID used for this guest */
 	unsigned int handle;	/* SEV firmware handle */
 	int fd;			/* SEV device fd */
@@ -330,6 +331,13 @@ static __always_inline bool sev_es_guest(struct kvm *kvm)
 #endif
 }
 
+static inline bool sev_snp_guest(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+
+	return sev_es_guest(kvm) && sev->snp_active;
+}
+
 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
 {
 	vmcb->control.clean = 0;
Signature · 1 line
2.25.1
#120 lore raw

RFC PATCH v7 37/64 KVM: SVM: Add KVM_SNP_INIT command

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SNP_INIT command is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of the KVM_SEV_INIT or KVM_SEV_ES_INIT.

The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).

---
 .../virt/kvm/x86/amd-memory-encryption.rst    | 27 ++++++++++++
 arch/x86/include/asm/svm.h                    |  1 +
 arch/x86/kvm/svm/sev.c                        | 44 ++++++++++++++++++-
 arch/x86/kvm/svm/svm.h                        |  4 ++
 include/uapi/linux/kvm.h                      | 13 ++++++
 5 files changed, 87 insertions(+), 2 deletions(-)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +27 -0
index 935aaeb97fe6..2432213bd0ea 100644
@@ -434,6 +434,33 @@ issued by the hypervisor to make the guest ready for execution.
 
 Returns: 0 on success, -negative on error
 
+18. KVM_SNP_INIT
+----------------
+
+The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP
+context. In a typical workflow, this command should be the first command issued.
+
+Parameters (in/out): struct kvm_snp_init
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_snp_init {
+                __u64 flags;
+        };
+
+The flags bitmap is defined as::
+
+   /* enable the restricted injection */
+   #define KVM_SEV_SNP_RESTRICTED_INJET   (1<<0)
+
+   /* enable the restricted injection timer */
+   #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET   (1<<1)
+
+If the specified flags is not supported then return -EOPNOTSUPP, and the supported
+flags are returned.
+
 References
 ==========
 
arch/x86/include/asm/svm.h +1 -0
index cb1ee53ad3b1..c18d78d5e505 100644
@@ -278,6 +278,7 @@ enum avic_ipi_failure_cause {
 #define AVIC_HPA_MASK	~((0xFFFULL << 52) | 0xFFF)
 #define VMCB_AVIC_APIC_BAR_MASK		0xFFFFFFFFFF000ULL
 
+#define SVM_SEV_FEAT_SNP_ACTIVE		BIT(0)
 
 struct vmcb_seg {
 	u16 selector;
arch/x86/kvm/svm/sev.c +42 -2

79 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +4 -0
index a48fe5d2bea5..379b253d2464 100644
@@ -80,6 +80,9 @@ enum {
 /* TPR and CR2 are always written before VMRUN */
 #define VMCB_ALWAYS_DIRTY_MASK	((1U << VMCB_INTR) | (1U << VMCB_CR2))
 
+/* Supported init feature flags */
+#define SEV_SNP_SUPPORTED_FLAGS		0x0
+
 struct kvm_sev_info {
 	bool active;		/* SEV enabled guest */
 	bool es_active;		/* SEV-ES enabled guest */
@@ -95,6 +98,7 @@ struct kvm_sev_info {
 	struct list_head mirror_entry; /* Use as a list entry of mirrors */
 	struct misc_cg *misc_cg; /* For misc cgroup accounting */
 	atomic_t migration_in_progress;
+	u64 snp_init_flags;
 };
 
 struct kvm_svm {
include/uapi/linux/kvm.h +13 -0
index cc9424ccf9b2..a6c73297a62d 100644
@@ -1938,6 +1938,9 @@ enum sev_cmd_id {
 	/* Guest Migration Extension */
 	KVM_SEV_SEND_CANCEL,
 
+	/* SNP specific commands */
+	KVM_SEV_SNP_INIT,
+
 	KVM_SEV_NR_MAX,
 };
 
@@ -2034,6 +2037,16 @@ struct kvm_sev_receive_update_data {
 	__u32 trans_len;
 };
 
+/* enable the restricted injection */
+#define KVM_SEV_SNP_RESTRICTED_INJET   (1 << 0)
+
+/* enable the restricted injection timer */
+#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET   (1 << 1)
+
+struct kvm_snp_init {
+	__u64 flags;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.25.1
#121 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

Quoted 15 lines · Michael Roth

static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) {

(quoted diff) +2 -1
 	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
@@ -260,13 +279,23 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
 		return ret;
 
 	sev->active = true;
-	sev->es_active = argp->id == KVM_SEV_ES_INIT;
+	sev->es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == KVM_SEV_SNP_INIT);
+	sev->snp_active = argp->id == KVM_SEV_SNP_INIT;
 	asid = sev_asid_new(sev);
 	if (asid < 0)
 		goto e_no_asid;
 	sev->asid = asid;
 

Review static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 297

Quoted 10 lines
(quoted diff) +9 -1
-	ret = sev_platform_init(&argp->error);
+	if (sev->snp_active) {
+		ret = verify_snp_init_flags(kvm, argp);
+		if (ret)
+			goto e_free;
+
+		ret = sev_snp_init(&argp->error, false);
+	} else {
+		ret = sev_platform_init(&argp->error);
+	}

Couldn't sev_snp_init() and sev_platform_init() be called unconditionally in order?

Since there is a hardware constraint that SNP init needs to always happen before platform init, shouldn't SNP init happen as part of __sev_platform_init_locked() instead?

I found these call sites for __sev_platform_init_locked(), none of which follow the correct call order:

* sev_guest_init()
* sev_ioctl_do_pek_csr
* sev_ioctl_do_pdh_export()
* sev_ioctl_do_pek_import()
* sev_ioctl_do_pek_pdh_gen()
* sev_pci_init()

For me it looks like a bit flakky API use to have sev_snp_init() as an API call.

I would suggest to make SNP init internal to the ccp driver and take care of the correct orchestration over there.

Also, how it currently works in this patch set, if the firmware did not load correctly, SNP init halts the whole system. The version check needs to be in all call paths.

BR, Jarkko

From c189db485a4162f401f351d2b1842c7f66f17ae6 Mon Sep 17 00:00:00 2001
From: Jarkko Sakkinen <[email protected]>
Date: Sun, 4 Dec 2022 06:17:07 +0000
Subject: [PATCH] crypto: ccp: Prevent a spurious SEV_CMD_SNP_INIT triggered by
sev_guest_init()

Move the firmware version check from sev_pci_init() to sev_snp_init().

---
 drivers/crypto/ccp/sev-dev.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/crypto/ccp/sev-dev.c +14 -14
index 6c4fdcaed72b..462c9aaa2e7e 100644
@@ -1381,6 +1381,12 @@ static int __sev_snp_init_locked(int *error)
 	if (sev->snp_initialized)
 		return 0;
 
+	if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
+		dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
+			SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
+		return -ENODEV;
+	}
+
 	/*
 	 * The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h
 	 * across all cores.
@@ -2313,25 +2319,19 @@ void sev_pci_init(void)
 		}
 	}
 
+	rc = sev_snp_init(&error, true);
+	if (rc != -ENODEV)
+		/*
+		 * Don't abort the probe if SNP INIT failed,
+		 * continue to initialize the legacy SEV firmware.
+		 */
+		dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error);
+
 	/*
 	 * If boot CPU supports SNP, then first attempt to initialize
 	 * the SNP firmware.
 	 */
 	if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) {
-		if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
-			dev_err(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
-				SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
-		} else {
-			rc = sev_snp_init(&error, true);
-			if (rc) {
-				/*
-				 * Don't abort the probe if SNP INIT failed,
-				 * continue to initialize the legacy SEV firmware.
-				 */
-				dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error);
-			}
-		}
-
 		/*
 		 * Allocate the intermediate buffers used for the legacy command handling.
 		 */
Signature · 1 line
2.38.1
#122 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

A couple of fixups.

drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error) · line 1387

Quoted 12 lines · Jarkko Sakkinen
drivers/crypto/ccp/sev-dev.c +4 -0
index 6c4fdcaed72b..462c9aaa2e7e 100644
@@ -1381,6 +1381,12 @@ static int __sev_snp_init_locked(int *error)
 	if (sev->snp_initialized)
 		return 0;
 
+	if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
+		dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
+			SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
+		return -ENODEV;
        return 0;

It is not a failure case anyway.

drivers/crypto/ccp/sev-dev.c void sev_pci_init(void) · line 2323

Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +4 -0
+	}
+
 	/*
 	 * The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h
 	 * across all cores.
@@ -2313,25 +2319,19 @@ void sev_pci_init(void)
 		}
 	}
 
+	rc = sev_snp_init(&error, true);
+	if (rc != -ENODEV)

if (rc)

Because other wise there would need to be nasty "if (rc && rc != ENODEV)" so that this does not happen:

[    9.321588] ccp 0000:49:00.1: SEV-SNP: failed to INIT error 0x0

BR, Jarkko

#123 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

Quoted 35 lines · Jarkko Sakkinen

A couple of fixups.

drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error) · line 1387

Quoted 12 lines · Jarkko Sakkinen
drivers/crypto/ccp/sev-dev.c +4 -0
index 6c4fdcaed72b..462c9aaa2e7e 100644
@@ -1381,6 +1381,12 @@ static int __sev_snp_init_locked(int *error)
 	if (sev->snp_initialized)
 		return 0;
 
+	if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
+		dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
+			SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
+		return -ENODEV;
        return 0;

It is not a failure case anyway.

drivers/crypto/ccp/sev-dev.c void sev_pci_init(void) · line 2323

Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +4 -0
+	}
+
 	/*
 	 * The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h
 	 * across all cores.
@@ -2313,25 +2319,19 @@ void sev_pci_init(void)
 		}
 	}
 
+	rc = sev_snp_init(&error, true);
+	if (rc != -ENODEV)

if (rc)

Quoted 6 lines

Because other wise there would need to be nasty "if (rc && rc != ENODEV)" so that this does not happen:

[    9.321588] ccp 0000:49:00.1: SEV-SNP: failed to INIT error 0x0

BR, Jarkko

This patch (not dependent on the series) is kind of related to my feedback. Since platform init can span from quite many locations it would be useful to get errors reported from all locations:

https://www.lkml.org/lkml/2022/12/31/175

Would be IMHO good to have this in the baseline when testing SNP init functionality.

BR, Jarkko

maintainer
#124 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

In reply to Jarkko Sakkinen

Hello Jarkko,

Quoted 30 lines · Jarkko Sakkinen
Quoted 15 lines · Michael Roth
  static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
  {
  	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
(quoted diff) +2 -1
@@ -260,13 +279,23 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
  		return ret;
 
  	sev->active = true;
-	sev->es_active = argp->id == KVM_SEV_ES_INIT;
+	sev->es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == KVM_SEV_SNP_INIT);
+	sev->snp_active = argp->id == KVM_SEV_SNP_INIT;
  	asid = sev_asid_new(sev);
  	if (asid < 0)
  		goto e_no_asid;
  	sev->asid = asid;
 

Review static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 297

Quoted 10 lines
(quoted diff) +9 -1
-	ret = sev_platform_init(&argp->error);
+	if (sev->snp_active) {
+		ret = verify_snp_init_flags(kvm, argp);
+		if (ret)
+			goto e_free;
+
+		ret = sev_snp_init(&argp->error, false);
+	} else {
+		ret = sev_platform_init(&argp->error);
+	}

Couldn't sev_snp_init() and sev_platform_init() be called unconditionally in order?

Quoted 4 lines

Since there is a hardware constraint that SNP init needs to always happen before platform init, shouldn't SNP init happen as part of __sev_platform_init_locked() instead?

On Genoa there is currently an issue that if we do an SNP_INIT before an SEV_INIT and then attempt to launch a SEV guest that may fail, so we need to keep SNP INIT and SEV INIT separate.

We need to provide a way to run (existing) SEV guests on a system that supports SNP without doing an SNP_INIT at all.

This is done using psp_init_on_probe parameter of the CCP module to avoid doing either SNP/SEV firmware initialization during module load and then defer the firmware initialization till someone launches a guest of one flavor or the other.

And then sev_guest_init() does either SNP or SEV firmware init depending on the type of the guest being launched.

Quoted 4 lines

I found these call sites for __sev_platform_init_locked(), none of which follow the correct call order:

* sev_guest_init()

As explained above, this call site is important for deferring the firmware initialization to an actual guest launch.

Quoted 6 lines

* sev_ioctl_do_pek_csr
* sev_ioctl_do_pdh_export()
* sev_ioctl_do_pek_import()
* sev_ioctl_do_pek_pdh_gen()
* sev_pci_init()

Quoted 6 lines

For me it looks like a bit flakky API use to have sev_snp_init() as an API call.

I would suggest to make SNP init internal to the ccp driver and take care of the correct orchestration over there.

Due to Genoa issue, we may still need SNP init and SEV init to be invoked separately outside the CCP driver.

Quoted 4 lines

Also, how it currently works in this patch set, if the firmware did not load correctly, SNP init halts the whole system. The version check needs to be in all call paths.

Yes, i agree with that.

Thanks,
Ashish

#125 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

Quoted 43 lines · Ashish

Hello Jarkko,

Quoted 28 lines · Jarkko Sakkinen
Quoted 13 lines · Michael Roth
  static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
  {
  	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
(quoted diff) +2 -1
@@ -260,13 +279,23 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
  		return ret;
  	sev->active = true;
-	sev->es_active = argp->id == KVM_SEV_ES_INIT;
+	sev->es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == KVM_SEV_SNP_INIT);
+	sev->snp_active = argp->id == KVM_SEV_SNP_INIT;
  	asid = sev_asid_new(sev);
  	if (asid < 0)
  		goto e_no_asid;
  	sev->asid = asid;

Review static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 295

Quoted 10 lines
(quoted diff) +9 -1
-	ret = sev_platform_init(&argp->error);
+	if (sev->snp_active) {
+		ret = verify_snp_init_flags(kvm, argp);
+		if (ret)
+			goto e_free;
+
+		ret = sev_snp_init(&argp->error, false);
+	} else {
+		ret = sev_platform_init(&argp->error);
+	}

Couldn't sev_snp_init() and sev_platform_init() be called unconditionally in order?

Quoted 4 lines

Since there is a hardware constraint that SNP init needs to always happen before platform init, shouldn't SNP init happen as part of __sev_platform_init_locked() instead?

On Genoa there is currently an issue that if we do an SNP_INIT before an SEV_INIT and then attempt to launch a SEV guest that may fail, so we need to keep SNP INIT and SEV INIT separate.

We need to provide a way to run (existing) SEV guests on a system that supports SNP without doing an SNP_INIT at all.

Quoted 7 lines

This is done using psp_init_on_probe parameter of the CCP module to avoid doing either SNP/SEV firmware initialization during module load and then defer the firmware initialization till someone launches a guest of one flavor or the other.

And then sev_guest_init() does either SNP or SEV firmware init depending on the type of the guest being launched.

OK, got it, thank you. I have not noticed the init_on_probe for sev_snp_init() before. Was it in earlier patch set version?

The benefit of having everything in __sev_platform_init_lock() would be first less risk of shooting yourself into foot, and also no need to pass init_on_probe to sev_snp_init() as it would be internal to sev-dev.c, and no need for special cases for callers. It is in my opinion internals of the SEV driver to guarantee the order.

E.g. changes to svm/sev.c would be then quite trivial.

Quoted 12 lines
Quoted 4 lines

I found these call sites for __sev_platform_init_locked(), none of which follow the correct call order:

* sev_guest_init()

As explained above, this call site is important for deferring the firmware initialization to an actual guest launch.

Quoted 4 lines

* sev_ioctl_do_pek_csr
* sev_ioctl_do_pdh_export()
* sev_ioctl_do_pek_import()
* sev_ioctl_do_pek_pdh_gen()

What happens if any of these are called before sev_guest_init()? They only call __sev_platform_init_locked().

Quoted 12 lines
Quoted 8 lines

* sev_pci_init()

For me it looks like a bit flakky API use to have sev_snp_init() as an API call.

I would suggest to make SNP init internal to the ccp driver and take care of the correct orchestration over there.

Due to Genoa issue, we may still need SNP init and SEV init to be invoked separately outside the CCP driver.

Quoted 6 lines
Quoted 4 lines

Also, how it currently works in this patch set, if the firmware did not load correctly, SNP init halts the whole system. The version check needs to be in all call paths.

Yes, i agree with that.

Attached the fix I sent in private earlier.

Quoted 2 lines

Thanks,
Ashish

BR, Jarkko

From f24054af9eeeb0314bbd0c37bd97ff38e2ded717 Mon Sep 17 00:00:00 2001
From: Jarkko Sakkinen <[email protected]>
Date: Sun, 4 Dec 2022 06:17:07 +0000
Subject: [PATCH] crypto: ccp: Prevent a spurious SEV_CMD_SNP_INIT triggered by
sev_guest_init()

Move the firmware version check from sev_pci_init() to sev_snp_init().

---
 drivers/crypto/ccp/sev-dev.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/crypto/ccp/sev-dev.c +14 -14
index 7c5698bde655..08787d998f15 100644
@@ -1387,6 +1387,12 @@ static int __sev_snp_init_locked(int *error)
 	if (sev->snp_initialized)
 		return 0;
 
+	if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
+		dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
+			SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
+		return 0;
+	}
+
 	/*
 	 * The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h
 	 * across all cores.
@@ -2319,25 +2325,19 @@ void sev_pci_init(void)
 		}
 	}
 
+	rc = sev_snp_init(&error, true);
+	if (rc)
+		/*
+		 * Don't abort the probe if SNP INIT failed,
+		 * continue to initialize the legacy SEV firmware.
+		 */
+		dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error);
+
 	/*
 	 * If boot CPU supports SNP, then first attempt to initialize
 	 * the SNP firmware.
 	 */
 	if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) {
-		if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
-			dev_err(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
-				SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
-		} else {
-			rc = sev_snp_init(&error, true);
-			if (rc) {
-				/*
-				 * Don't abort the probe if SNP INIT failed,
-				 * continue to initialize the legacy SEV firmware.
-				 */
-				dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error);
-			}
-		}
-
 		/*
 		 * Allocate the intermediate buffers used for the legacy command handling.
 		 */
Signature · 1 line
2.38.1
maintainer
#126 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

In reply to Kalra, Ashish

There was an early firmware issue on Genoa which supported only SNP_INIT or SEV_INIT, but this issue is resolved now.

Now, the main constraints are that SNP_INIT is always required before SEV_INIT in case we want to launch SNP guests. In other words, if only SEV_INIT is done on a platform which supports SNP we won't be able to launch SNP guests after that.

So once we have RMP table setup (in BIOS) we will always do an SNP_INIT and SEV_INIT will be ideally done only (on demand) when an SEV guest is launched.

Thanks,
Ashish

Quoted 77 lines · Ashish

Hello Jarkko,

Quoted 30 lines · Jarkko Sakkinen
Quoted 15 lines · Michael Roth

  static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) {
      struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;

(quoted diff) +2 -1
@@ -260,13 +279,23 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           return ret;
       sev->active = true;
-    sev->es_active = argp->id == KVM_SEV_ES_INIT;
+    sev->es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == 
 KVM_SEV_SNP_INIT);
+    sev->snp_active = argp->id == KVM_SEV_SNP_INIT;
       asid = sev_asid_new(sev);
       if (asid < 0)
           goto e_no_asid;
       sev->asid = asid;

Review static int sev_guest_init(struct kvm *kvm, · line 297

Quoted 10 lines
(quoted diff) +9 -1
-    ret = sev_platform_init(&argp->error);
+    if (sev->snp_active) {
+        ret = verify_snp_init_flags(kvm, argp);
+        if (ret)
+            goto e_free;
+
+        ret = sev_snp_init(&argp->error, false);
+    } else {
+        ret = sev_platform_init(&argp->error);
+    }

Couldn't sev_snp_init() and sev_platform_init() be called unconditionally in order?

Quoted 4 lines

Since there is a hardware constraint that SNP init needs to always happen before platform init, shouldn't SNP init happen as part of __sev_platform_init_locked() instead?

On Genoa there is currently an issue that if we do an SNP_INIT before an SEV_INIT and then attempt to launch a SEV guest that may fail, so we need to keep SNP INIT and SEV INIT separate.

We need to provide a way to run (existing) SEV guests on a system that supports SNP without doing an SNP_INIT at all.

This is done using psp_init_on_probe parameter of the CCP module to avoid doing either SNP/SEV firmware initialization during module load and then defer the firmware initialization till someone launches a guest of one flavor or the other.

And then sev_guest_init() does either SNP or SEV firmware init depending on the type of the guest being launched.

Quoted 4 lines

I found these call sites for __sev_platform_init_locked(), none of which follow the correct call order:

* sev_guest_init()

As explained above, this call site is important for deferring the firmware initialization to an actual guest launch.

Quoted 6 lines

* sev_ioctl_do_pek_csr
* sev_ioctl_do_pdh_export()
* sev_ioctl_do_pek_import()
* sev_ioctl_do_pek_pdh_gen()
* sev_pci_init()

Quoted 6 lines

For me it looks like a bit flakky API use to have sev_snp_init() as an API call.

I would suggest to make SNP init internal to the ccp driver and take care of the correct orchestration over there.

Due to Genoa issue, we may still need SNP init and SEV init to be invoked separately outside the CCP driver.

Quoted 4 lines

Also, how it currently works in this patch set, if the firmware did not load correctly, SNP init halts the whole system. The version check needs to be in all call paths.

Yes, i agree with that.

Thanks,
Ashish

#127 lore raw

Re: [PATCH RFC v7 37/64] KVM: SVM: Add KVM_SNP_INIT command

Quoted 11 lines · Ashish

There was an early firmware issue on Genoa which supported only SNP_INIT or SEV_INIT, but this issue is resolved now.

Now, the main constraints are that SNP_INIT is always required before SEV_INIT in case we want to launch SNP guests. In other words, if only SEV_INIT is done on a platform which supports SNP we won't be able to launch SNP guests after that.

So once we have RMP table setup (in BIOS) we will always do an SNP_INIT and SEV_INIT will be ideally done only (on demand) when an SEV guest is launched.

OK, thanks for the clarification!

BR, Jarkko

#128 lore raw

RFC PATCH v7 38/64 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. If the guest is expected to be migrated, the command also binds a migration agent (MA) to the guest.

For more information see the SEV-SNP specification.

---
 .../virt/kvm/x86/amd-memory-encryption.rst    |  24 ++++
 arch/x86/kvm/svm/sev.c                        | 121 +++++++++++++++++-
 arch/x86/kvm/svm/svm.h                        |   1 +
 include/uapi/linux/kvm.h                      |  10 ++
 4 files changed, 153 insertions(+), 3 deletions(-)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +24 -0
index 2432213bd0ea..58971fc02a15 100644
@@ -461,6 +461,30 @@ The flags bitmap is defined as::
 If the specified flags is not supported then return -EOPNOTSUPP, and the supported
 flags are returned.
 
+19. KVM_SNP_LAUNCH_START
+------------------------
+
+The KVM_SNP_LAUNCH_START command is used for creating the memory encryption
+context for the SEV-SNP guest. To create the encryption context, user must
+provide a guest policy, migration agent (if any) and guest OS visible
+workarounds value as defined SEV-SNP specification.
+
+Parameters (in): struct  kvm_snp_launch_start
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_snp_launch_start {
+                __u64 policy;           /* Guest policy to use. */
+                __u64 ma_uaddr;         /* userspace address of migration agent */
+                __u8 ma_en;             /* 1 if the migration agent is enabled */
+                __u8 imi_en;            /* set IMI to 1. */
+                __u8 gosvw[16];         /* guest OS visible workarounds */
+        };
+
+See the SEV-SNP specification for further detail on the launch input.
+
 References
 ==========
 
arch/x86/kvm/svm/sev.c +118 -3

167 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +1 -0
index 379b253d2464..17200c1ad20e 100644
@@ -99,6 +99,7 @@ struct kvm_sev_info {
 	struct misc_cg *misc_cg; /* For misc cgroup accounting */
 	atomic_t migration_in_progress;
 	u64 snp_init_flags;
+	void *snp_context;      /* SNP guest context page */
 };
 
 struct kvm_svm {
include/uapi/linux/kvm.h +10 -0
index a6c73297a62d..b2311e0abeef 100644
@@ -1940,6 +1940,7 @@ enum sev_cmd_id {
 
 	/* SNP specific commands */
 	KVM_SEV_SNP_INIT,
+	KVM_SEV_SNP_LAUNCH_START,
 
 	KVM_SEV_NR_MAX,
 };
@@ -2047,6 +2048,15 @@ struct kvm_snp_init {
 	__u64 flags;
 };
 
+struct kvm_sev_snp_launch_start {
+	__u64 policy;
+	__u64 ma_uaddr;
+	__u8 ma_en;
+	__u8 imi_en;
+	__u8 gosvw[16];
+	__u8 pad[6];
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.25.1
#129 lore raw

Re: [PATCH RFC v7 38/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command

Quoted 44 lines
arch/x86/kvm/svm/sev.c +43 -0
+/*
+ * The guest context contains all the information, keys and metadata
+ * associated with the guest that the firmware tracks to implement SEV
+ * and SNP features. The firmware stores the guest context in hypervisor
+ * provide page via the SNP_GCTX_CREATE command.
+ */
+static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+       struct sev_data_snp_addr data = {};
+       void *context;
+       int rc;
+
+       /* Allocate memory for context page */
+       context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
+       if (!context)
+               return NULL;
+
+       data.gctx_paddr = __psp_pa(context);
+       rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
+       if (rc) {
+               snp_free_firmware_page(context);
+               return NULL;
+       }
+
+       return context;
+}
+
+static int snp_bind_asid(struct kvm *kvm, int *error)
+{
+       struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+       struct sev_data_snp_activate data = {0};
+
+       data.gctx_paddr = __psp_pa(sev->snp_context);
+       data.asid   = sev_get_asid(kvm);
+       return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error);
+}
+
+static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+       struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+       struct sev_data_snp_launch_start start = {0};
+       struct kvm_sev_snp_launch_start params;
+       int rc;

arch/x86/kvm/svm/sev.c int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) · line 2066

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+       if (!sev_snp_guest(kvm))
+               return -ENOTTY;
+
+       if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
+               return -EFAULT;
+
+       sev->snp_context = snp_context_create(kvm, argp);
+       if (!sev->snp_context)
+               return -ENOTTY;

This was reported-by josheads@. Its possible that userspace can repeatedly call snp_launch_start() causing the leak of memory from repeated snp_context_create() calls, leaking SNP contexts in the ASP, and leaking ASIDs.

A possible solution could be to just error out if snp_context already exists?

+ if (sev->snp_context)
+ return -EINVAL;
+

Quoted 21 lines
arch/x86/kvm/svm/sev.c +21 -0
+
+       start.gctx_paddr = __psp_pa(sev->snp_context);
+       start.policy = params.policy;
+       memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw));
+       rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error);
+       if (rc)
+               goto e_free_context;
+
+       sev->fd = argp->sev_fd;
+       rc = snp_bind_asid(kvm, &argp->error);
+       if (rc)
+               goto e_free_context;
+
+       return 0;
+
+e_free_context:
+       snp_decommission_context(kvm);
+
+       return rc;
+}
+
#130 lore raw

RFC PATCH v7 39/64 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START.

In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page.

While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them.

For more information see the SEV-SNP specification.

---
 .../virt/kvm/x86/amd-memory-encryption.rst    |  29 ++++
 arch/x86/kvm/svm/sev.c                        | 161 ++++++++++++++++++
 include/uapi/linux/kvm.h                      |  19 +++
 3 files changed, 209 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +29 -0
index 58971fc02a15..c94be8e6d657 100644
@@ -485,6 +485,35 @@ Returns: 0 on success, -negative on error
 
 See the SEV-SNP specification for further detail on the launch input.
 
+20. KVM_SNP_LAUNCH_UPDATE
+-------------------------
+
+The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also
+calculates a measurement of the memory contents. The measurement is a signature
+of the memory contents that can be sent to the guest owner as an attestation
+that the memory was encrypted correctly by the firmware.
+
+Parameters (in): struct  kvm_snp_launch_update
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_snp_launch_update {
+                __u64 start_gfn;        /* Guest page number to start from. */
+                __u64 uaddr;            /* userspace address need to be encrypted */
+                __u32 len;              /* length of memory region */
+                __u8 imi_page;          /* 1 if memory is part of the IMI */
+                __u8 page_type;         /* page type */
+                __u8 vmpl3_perms;       /* VMPL3 permission mask */
+                __u8 vmpl2_perms;       /* VMPL2 permission mask */
+                __u8 vmpl1_perms;       /* VMPL1 permission mask */
+        };
+
+See the SEV-SNP spec for further details on how to build the VMPL permission
+mask and page type.
+
+
 References
 ==========
 
arch/x86/kvm/svm/sev.c +161 -0

179 lines not on this page · raw message

include/uapi/linux/kvm.h +19 -0
index b2311e0abeef..9b6c95cc62a8 100644
@@ -1941,6 +1941,7 @@ enum sev_cmd_id {
 	/* SNP specific commands */
 	KVM_SEV_SNP_INIT,
 	KVM_SEV_SNP_LAUNCH_START,
+	KVM_SEV_SNP_LAUNCH_UPDATE,
 
 	KVM_SEV_NR_MAX,
 };
@@ -2057,6 +2058,24 @@ struct kvm_sev_snp_launch_start {
 	__u8 pad[6];
 };
 
+#define KVM_SEV_SNP_PAGE_TYPE_NORMAL		0x1
+#define KVM_SEV_SNP_PAGE_TYPE_VMSA		0x2
+#define KVM_SEV_SNP_PAGE_TYPE_ZERO		0x3
+#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED	0x4
+#define KVM_SEV_SNP_PAGE_TYPE_SECRETS		0x5
+#define KVM_SEV_SNP_PAGE_TYPE_CPUID		0x6
+
+struct kvm_sev_snp_launch_update {
+	__u64 start_gfn;
+	__u64 uaddr;
+	__u32 len;
+	__u8 imi_page;
+	__u8 page_type;
+	__u8 vmpl3_perms;
+	__u8 vmpl2_perms;
+	__u8 vmpl1_perms;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.25.1
#131 lore raw

Re: [PATCH RFC v7 39/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command

Quoted 185 lines · Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START.

In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page.

While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them.

For more information see the SEV-SNP specification.

---
 .../virt/kvm/x86/amd-memory-encryption.rst    |  29 ++++
 arch/x86/kvm/svm/sev.c                        | 161 ++++++++++++++++++
 include/uapi/linux/kvm.h                      |  19 +++
 3 files changed, 209 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +29 -0
index 58971fc02a15..c94be8e6d657 100644
@@ -485,6 +485,35 @@ Returns: 0 on success, -negative on error
 
 See the SEV-SNP specification for further detail on the launch input.
 
+20. KVM_SNP_LAUNCH_UPDATE
+-------------------------
+
+The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also
+calculates a measurement of the memory contents. The measurement is a signature
+of the memory contents that can be sent to the guest owner as an attestation
+that the memory was encrypted correctly by the firmware.
+
+Parameters (in): struct  kvm_snp_launch_update
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_snp_launch_update {
+                __u64 start_gfn;        /* Guest page number to start from. */
+                __u64 uaddr;            /* userspace address need to be encrypted */
+                __u32 len;              /* length of memory region */
+                __u8 imi_page;          /* 1 if memory is part of the IMI */
+                __u8 page_type;         /* page type */
+                __u8 vmpl3_perms;       /* VMPL3 permission mask */
+                __u8 vmpl2_perms;       /* VMPL2 permission mask */
+                __u8 vmpl1_perms;       /* VMPL1 permission mask */
+        };
+
+See the SEV-SNP spec for further details on how to build the VMPL permission
+mask and page type.
+
+
 References
 ==========
 
arch/x86/kvm/svm/sev.c +105 -0
index 6d1d0e424f76..379e61a9226a 100644
@@ -238,6 +238,37 @@ static void sev_decommission(unsigned int handle)
 	sev_guest_decommission(&decommission, NULL);
 }
 
+static int snp_page_reclaim(u64 pfn)
+{
+	struct sev_data_snp_page_reclaim data = {0};
+	int err, rc;
+
+	data.paddr = __sme_set(pfn << PAGE_SHIFT);
+	rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err);
+	if (rc) {
+		/*
+		 * If the reclaim failed, then page is no longer safe
+		 * to use.
+		 */
+		snp_mark_pages_offline(pfn,
+				       page_level_size(PG_LEVEL_4K) >> PAGE_SHIFT);
+	}
+
+	return rc;
+}
+
+static int host_rmp_make_shared(u64 pfn, enum pg_level level, bool leak)
+{
+	int rc;
+
+	rc = rmp_make_shared(pfn, level);
+	if (rc && leak)
+		snp_mark_pages_offline(pfn,
+				       page_level_size(level) >> PAGE_SHIFT);
+
+	return rc;
+}
+
 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
 {
 	struct sev_data_deactivate deactivate;
@@ -2085,6 +2116,133 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return rc;
 }
 
+static int snp_launch_update_gfn_handler(struct kvm *kvm,
+					 struct kvm_gfn_range *range,
+					 void *opaque)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_memory_slot *memslot = range->slot;
+	struct sev_data_snp_launch_update data = {0};
+	struct kvm_sev_snp_launch_update params;
+	struct kvm_sev_cmd *argp = opaque;
+	int *error = &argp->error;
+	int i, n = 0, ret = 0;
+	unsigned long npages;
+	kvm_pfn_t *pfns;
+	gfn_t gfn;
+
+	if (!kvm_slot_can_be_private(memslot)) {
+		pr_err("SEV-SNP requires restricted memory.\n");
+		return -EINVAL;
+	}
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params))) {
+		pr_err("Failed to copy user parameters for SEV-SNP launch.\n");
+		return -EFAULT;
+	}
+
+	data.gctx_paddr = __psp_pa(sev->snp_context);
+
+	npages = range->end - range->start;
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL_ACCOUNT);
+	if (!pfns)
+		return -ENOMEM;
+
+	pr_debug("%s: GFN range 0x%llx-0x%llx, type %d\n", __func__,
+		 range->start, range->end, params.page_type);
+
+	for (gfn = range->start, i = 0; gfn < range->end; gfn++, i++) {
+		int order, level;
+		void *kvaddr;
+
+		ret = kvm_restricted_mem_get_pfn(memslot, gfn, &pfns[i], &order);
+		if (ret)
+			goto e_release;
+
+		n++;
+		ret = snp_lookup_rmpentry((u64)pfns[i], &level);
+		if (ret) {
+			pr_err("Failed to ensure GFN 0x%llx is in initial shared state, ret: %d\n",
+			       gfn, ret);
+			return -EFAULT;
+		}
+
+		kvaddr = pfn_to_kaddr(pfns[i]);
+		if (!virt_addr_valid(kvaddr)) {
+			pr_err("Invalid HVA 0x%llx for GFN 0x%llx\n", (uint64_t)kvaddr, gfn);
+			ret = -EINVAL;
+			goto e_release;
+		}
+
+		ret = kvm_read_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE);
+		if (ret) {
+			pr_err("Guest read failed, ret: 0x%x\n", ret);
+			goto e_release;
+		}
+
+		ret = rmp_make_private(pfns[i], gfn << PAGE_SHIFT, PG_LEVEL_4K,
+				       sev_get_asid(kvm), true);
+		if (ret) {
+			ret = -EFAULT;
+			goto e_release;
+		}
+
+		data.address = __sme_set(pfns[i] << PAGE_SHIFT);
+		data.page_size = X86_TO_RMP_PG_LEVEL(PG_LEVEL_4K);
+		data.page_type = params.page_type;

arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2202

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+		data.vmpl3_perms = params.vmpl3_perms;
+		data.vmpl2_perms = params.vmpl2_perms;
+		data.vmpl1_perms = params.vmpl1_perms;
+		ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
+				      &data, error);
+		if (ret) {
+			pr_err("SEV-SNP launch update failed, ret: 0x%x, fw_error: 0x%x\n",
+			       ret, *error);
+			snp_page_reclaim(pfns[i]);
+			goto e_release;

When a launch update fails for a CPUID page with error INVALID_PARAM the firmware writes back corrected values. We should probably write these values back to userspace. Before UPM was introduced this happened automatically because we didn't copy the page to private memory and did the update completly in place.

Quoted 94 lines
arch/x86/kvm/svm/sev.c +46 -0
+		}
+	}
+
+	kvm_vm_set_region_attr(kvm, range->start, range->end, KVM_MEMORY_ATTRIBUTE_PRIVATE);
+
+e_release:
+	/* Content of memory is updated, mark pages dirty */
+	for (i = 0; i < n; i++) {
+		set_page_dirty(pfn_to_page(pfns[i]));
+		mark_page_accessed(pfn_to_page(pfns[i]));
+
+		/*
+		 * If its an error, then update RMP entry to change page ownership
+		 * to the hypervisor.
+		 */
+		if (ret)
+			host_rmp_make_shared(pfns[i], PG_LEVEL_4K, true);
+
+		put_page(pfn_to_page(pfns[i]));
+	}
+
+	kvfree(pfns);
+	return ret;
+}
+
+static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_sev_snp_launch_update params;
+
+	if (!sev_snp_guest(kvm))
+		return -ENOTTY;
+
+	if (!sev->snp_context)
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
+		return -EFAULT;
+
+	return kvm_vm_do_hva_range_op(kvm, params.uaddr, params.uaddr + params.len,
+				      snp_launch_update_gfn_handler, argp);
+}
+
 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -2178,6 +2336,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_SNP_LAUNCH_START:
 		r = snp_launch_start(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_SNP_LAUNCH_UPDATE:
+		r = snp_launch_update(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
include/uapi/linux/kvm.h +19 -1
index b2311e0abeef..9b6c95cc62a8 100644
@@ -1941,6 +1941,7 @@ enum sev_cmd_id {
 	/* SNP specific commands */
 	KVM_SEV_SNP_INIT,
 	KVM_SEV_SNP_LAUNCH_START,
+	KVM_SEV_SNP_LAUNCH_UPDATE,
 
 	KVM_SEV_NR_MAX,
 };
@@ -2057,6 +2058,24 @@ struct kvm_sev_snp_launch_start {
 	__u8 pad[6];
 };
 
+#define KVM_SEV_SNP_PAGE_TYPE_NORMAL		0x1
+#define KVM_SEV_SNP_PAGE_TYPE_VMSA		0x2
+#define KVM_SEV_SNP_PAGE_TYPE_ZERO		0x3
+#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED	0x4
+#define KVM_SEV_SNP_PAGE_TYPE_SECRETS		0x5
+#define KVM_SEV_SNP_PAGE_TYPE_CPUID		0x6
+
+struct kvm_sev_snp_launch_update {
+	__u64 start_gfn;
+	__u64 uaddr;
+	__u32 len;
+	__u8 imi_page;
+	__u8 page_type;
+	__u8 vmpl3_perms;
+	__u8 vmpl2_perms;
+	__u8 vmpl1_perms;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
--
Quoted 2 lines

2.25.1

Regards, Tom

#132 lore raw

Re: [PATCH RFC v7 39/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command

Am 11.01.23 um 14:56 schrieb Tom Dohrmann:

Quoted 194 lines · Michael Roth
Quoted 193 lines

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START.

In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page.

While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them.

For more information see the SEV-SNP specification.

---
  .../virt/kvm/x86/amd-memory-encryption.rst    |  29 ++++
  arch/x86/kvm/svm/sev.c                        | 161 ++++++++++++++++++
  include/uapi/linux/kvm.h                      |  19 +++
  3 files changed, 209 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +29 -0
index 58971fc02a15..c94be8e6d657 100644
@@ -485,6 +485,35 @@ Returns: 0 on success, -negative on error
 
  See the SEV-SNP specification for further detail on the launch input.
 
+20. KVM_SNP_LAUNCH_UPDATE
+-------------------------
+
+The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also
+calculates a measurement of the memory contents. The measurement is a signature
+of the memory contents that can be sent to the guest owner as an attestation
+that the memory was encrypted correctly by the firmware.
+
+Parameters (in): struct  kvm_snp_launch_update
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_snp_launch_update {
+                __u64 start_gfn;        /* Guest page number to start from. */
+                __u64 uaddr;            /* userspace address need to be encrypted */
+                __u32 len;              /* length of memory region */
+                __u8 imi_page;          /* 1 if memory is part of the IMI */
+                __u8 page_type;         /* page type */
+                __u8 vmpl3_perms;       /* VMPL3 permission mask */
+                __u8 vmpl2_perms;       /* VMPL2 permission mask */
+                __u8 vmpl1_perms;       /* VMPL1 permission mask */
+        };
+
+See the SEV-SNP spec for further details on how to build the VMPL permission
+mask and page type.
+
+
  References
  ==========
 
arch/x86/kvm/svm/sev.c +113 -0
index 6d1d0e424f76..379e61a9226a 100644
@@ -238,6 +238,37 @@ static void sev_decommission(unsigned int handle)
  	sev_guest_decommission(&decommission, NULL);
  }
 
+static int snp_page_reclaim(u64 pfn)
+{
+	struct sev_data_snp_page_reclaim data = {0};
+	int err, rc;
+
+	data.paddr = __sme_set(pfn << PAGE_SHIFT);
+	rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err);
+	if (rc) {
+		/*
+		 * If the reclaim failed, then page is no longer safe
+		 * to use.
+		 */
+		snp_mark_pages_offline(pfn,
+				       page_level_size(PG_LEVEL_4K) >> PAGE_SHIFT);
+	}
+
+	return rc;
+}
+
+static int host_rmp_make_shared(u64 pfn, enum pg_level level, bool leak)
+{
+	int rc;
+
+	rc = rmp_make_shared(pfn, level);
+	if (rc && leak)
+		snp_mark_pages_offline(pfn,
+				       page_level_size(level) >> PAGE_SHIFT);
+
+	return rc;
+}
+
  static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
  {
  	struct sev_data_deactivate deactivate;
@@ -2085,6 +2116,133 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
  	return rc;
  }
 
+static int snp_launch_update_gfn_handler(struct kvm *kvm,
+					 struct kvm_gfn_range *range,
+					 void *opaque)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_memory_slot *memslot = range->slot;
+	struct sev_data_snp_launch_update data = {0};
+	struct kvm_sev_snp_launch_update params;
+	struct kvm_sev_cmd *argp = opaque;
+	int *error = &argp->error;
+	int i, n = 0, ret = 0;
+	unsigned long npages;
+	kvm_pfn_t *pfns;
+	gfn_t gfn;
+
+	if (!kvm_slot_can_be_private(memslot)) {
+		pr_err("SEV-SNP requires restricted memory.\n");
+		return -EINVAL;
+	}
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params))) {
+		pr_err("Failed to copy user parameters for SEV-SNP launch.\n");
+		return -EFAULT;
+	}
+
+	data.gctx_paddr = __psp_pa(sev->snp_context);
+
+	npages = range->end - range->start;
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL_ACCOUNT);
+	if (!pfns)
+		return -ENOMEM;
+
+	pr_debug("%s: GFN range 0x%llx-0x%llx, type %d\n", __func__,
+		 range->start, range->end, params.page_type);
+
+	for (gfn = range->start, i = 0; gfn < range->end; gfn++, i++) {
+		int order, level;
+		void *kvaddr;
+
+		ret = kvm_restricted_mem_get_pfn(memslot, gfn, &pfns[i], &order);
+		if (ret)
+			goto e_release;
+
+		n++;
+		ret = snp_lookup_rmpentry((u64)pfns[i], &level);
+		if (ret) {
+			pr_err("Failed to ensure GFN 0x%llx is in initial shared state, ret: %d\n",
+			       gfn, ret);
+			return -EFAULT;
+		}
+
+		kvaddr = pfn_to_kaddr(pfns[i]);
+		if (!virt_addr_valid(kvaddr)) {
+			pr_err("Invalid HVA 0x%llx for GFN 0x%llx\n", (uint64_t)kvaddr, gfn);
+			ret = -EINVAL;
+			goto e_release;
+		}
+
+		ret = kvm_read_guest_page(kvm, gfn, kvaddr, 0, PAGE_SIZE);
+		if (ret) {
+			pr_err("Guest read failed, ret: 0x%x\n", ret);
+			goto e_release;
+		}
+
+		ret = rmp_make_private(pfns[i], gfn << PAGE_SHIFT, PG_LEVEL_4K,
+				       sev_get_asid(kvm), true);
+		if (ret) {
+			ret = -EFAULT;
+			goto e_release;
+		}
+
+		data.address = __sme_set(pfns[i] << PAGE_SHIFT);
+		data.page_size = X86_TO_RMP_PG_LEVEL(PG_LEVEL_4K);
+		data.page_type = params.page_type;
+		data.vmpl3_perms = params.vmpl3_perms;
+		data.vmpl2_perms = params.vmpl2_perms;
+		data.vmpl1_perms = params.vmpl1_perms;
+		ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
+				      &data, error);
+		if (ret) {
+			pr_err("SEV-SNP launch update failed, ret: 0x%x, fw_error: 0x%x\n",
+			       ret, *error);
Quoted 9 lines

arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2202

Quoted 2 lines
arch/x86/kvm/svm/sev.c +2 -0
+			snp_page_reclaim(pfns[i]);
+			goto e_release;

When a launch update fails for a CPUID page with error INVALID_PARAM the firmware writes back corrected values. We should probably write these values back to userspace. Before UPM was introduced this happened automatically because we didn't copy the page to private memory and did the update completly in place.

Yes, pretty please!

#133 lore raw

RFC PATCH v7 40/64 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch.

While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages.

If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.

---
 .../virt/kvm/x86/amd-memory-encryption.rst    |  22 ++++
 arch/x86/kvm/svm/sev.c                        | 119 ++++++++++++++++++
 include/uapi/linux/kvm.h                      |  14 +++
 3 files changed, 155 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +22 -0
index c94be8e6d657..e4b42aaab1de 100644
@@ -513,6 +513,28 @@ Returns: 0 on success, -negative on error
 See the SEV-SNP spec for further details on how to build the VMPL permission
 mask and page type.
 
+21. KVM_SNP_LAUNCH_FINISH
+-------------------------
+
+After completion of the SNP guest launch flow, the KVM_SNP_LAUNCH_FINISH command can be
+issued to make the guest ready for the execution.
+
+Parameters (in): struct kvm_sev_snp_launch_finish
+
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_snp_launch_finish {
+                __u64 id_block_uaddr;
+                __u64 id_auth_uaddr;
+                __u8 id_block_en;
+                __u8 auth_key_en;
+                __u8 host_data[32];
+        };
+
+
+See SEV-SNP specification for further details on launch finish input parameters.
 
 References
 ==========
arch/x86/kvm/svm/sev.c +119 -0

142 lines not on this page · raw message

include/uapi/linux/kvm.h +14 -0
index 9b6c95cc62a8..c468adc1f147 100644
@@ -1942,6 +1942,7 @@ enum sev_cmd_id {
 	KVM_SEV_SNP_INIT,
 	KVM_SEV_SNP_LAUNCH_START,
 	KVM_SEV_SNP_LAUNCH_UPDATE,
+	KVM_SEV_SNP_LAUNCH_FINISH,
 
 	KVM_SEV_NR_MAX,
 };
@@ -2076,6 +2077,19 @@ struct kvm_sev_snp_launch_update {
 	__u8 vmpl1_perms;
 };
 
+#define KVM_SEV_SNP_ID_BLOCK_SIZE	96
+#define KVM_SEV_SNP_ID_AUTH_SIZE	4096
+#define KVM_SEV_SNP_FINISH_DATA_SIZE	32
+
+struct kvm_sev_snp_launch_finish {
+	__u64 id_block_uaddr;
+	__u64 id_auth_uaddr;
+	__u8 id_block_en;
+	__u8 auth_key_en;
+	__u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE];
+	__u8 pad[6];
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.25.1
maintainer
#134 lore raw

Re: [PATCH RFC v7 40/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command

Quoted 39 lines · Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch.

While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages.

If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.

---
  .../virt/kvm/x86/amd-memory-encryption.rst    |  22 ++++
  arch/x86/kvm/svm/sev.c                        | 119 ++++++++++++++++++
  include/uapi/linux/kvm.h                      |  14 +++
  3 files changed, 155 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +8 -0
index c94be8e6d657..e4b42aaab1de 100644
@@ -513,6 +513,28 @@ Returns: 0 on success, -negative on error
  See the SEV-SNP spec for further details on how to build the VMPL permission
  mask and page type.
 
+21. KVM_SNP_LAUNCH_FINISH
+-------------------------
+
+After completion of the SNP guest launch flow, the KVM_SNP_LAUNCH_FINISH command can be
+issued to make the guest ready for the execution.
+
+Parameters (in): struct kvm_sev_snp_launch_finish
+

Documentation/virt/kvm/x86/amd-memory-encryption.rst Returns: 0 on success, -negative on error · line 533

Quoted 10 lines
Documentation/virt/kvm/x86/amd-memory-encryption.rst +10 -0
+Returns: 0 on success, -negative on error
+
+::
+
+        struct kvm_sev_snp_launch_finish {
+                __u64 id_block_uaddr;
+                __u64 id_auth_uaddr;
+                __u8 id_block_en;
+                __u8 auth_key_en;
+                __u8 host_data[32];

This is missing the 6 bytes of padding at the end of the struct.

Quoted 92 lines
Documentation/virt/kvm/x86/amd-memory-encryption.rst +4 -0
+        };
+
+
+See SEV-SNP specification for further details on launch finish input parameters.
 
  References
  ==========
arch/x86/kvm/svm/sev.c +77 -0
index 379e61a9226a..6f901545bed9 100644
@@ -2243,6 +2243,106 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
  				      snp_launch_update_gfn_handler, argp);
  }
 
+static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct sev_data_snp_launch_update data = {};
+	int i, ret;
+
+	data.gctx_paddr = __psp_pa(sev->snp_context);
+	data.page_type = SNP_PAGE_TYPE_VMSA;
+
+	for (i = 0; i < kvm->created_vcpus; i++) {
+		struct vcpu_svm *svm = to_svm(xa_load(&kvm->vcpu_array, i));
+		u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
+
+		/* Perform some pre-encryption checks against the VMSA */
+		ret = sev_es_sync_vmsa(svm);
+		if (ret)
+			return ret;
+
+		/* Transition the VMSA page to a firmware state. */
+		ret = rmp_make_private(pfn, -1, PG_LEVEL_4K, sev->asid, true);
+		if (ret)
+			return ret;
+
+		/* Issue the SNP command to encrypt the VMSA */
+		data.address = __sme_pa(svm->sev_es.vmsa);
+		ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
+				      &data, &argp->error);
+		if (ret) {
+			snp_page_reclaim(pfn);
+			return ret;
+		}
+
+		svm->vcpu.arch.guest_state_protected = true;
+	}
+
+	return 0;
+}
+
+static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_sev_snp_launch_finish params;
+	struct sev_data_snp_launch_finish *data;
+	void *id_block = NULL, *id_auth = NULL;
+	int ret;
+
+	if (!sev_snp_guest(kvm))
+		return -ENOTTY;
+
+	if (!sev->snp_context)
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
+		return -EFAULT;
+
+	/* Measure all vCPUs using LAUNCH_UPDATE before finalizing the launch flow. */
+	ret = snp_launch_update_vmsa(kvm, argp);
+	if (ret)
+		return ret;
+
+	data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
+	if (!data)
+		return -ENOMEM;
+
+	if (params.id_block_en) {
+		id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE);
+		if (IS_ERR(id_block)) {
+			ret = PTR_ERR(id_block);
+			goto e_free;
+		}
+
+		data->id_block_en = 1;
+		data->id_block_paddr = __sme_pa(id_block);
+
+		id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE);
+		if (IS_ERR(id_auth)) {
+			ret = PTR_ERR(id_auth);

arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2332

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+			goto e_free_id_block;
+		}
+
+		data->id_auth_paddr = __sme_pa(id_auth);
+
+		if (params.auth_key_en)
+			data->auth_key_en = 1;
+	}
+
+	data->gctx_paddr = __psp_pa(sev->snp_context);

This is missing the copying of the params.host_data field into the data->host_data field. This is needed so that the host_data shows up in the attestation report.

Thanks,
Tom

Quoted 86 lines
arch/x86/kvm/svm/sev.c +32 -0
+	ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error);
+
+	kfree(id_auth);
+
+e_free_id_block:
+	kfree(id_block);
+
+e_free:
+	kfree(data);
+
+	return ret;
+}
+
  int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
  {
  	struct kvm_sev_cmd sev_cmd;
@@ -2339,6 +2439,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
  	case KVM_SEV_SNP_LAUNCH_UPDATE:
  		r = snp_launch_update(kvm, &sev_cmd);
  		break;
+	case KVM_SEV_SNP_LAUNCH_FINISH:
+		r = snp_launch_finish(kvm, &sev_cmd);
+		break;
  	default:
  		r = -EINVAL;
  		goto out;
@@ -2794,11 +2897,27 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
 
  	svm = to_svm(vcpu);
 
+	/*
+	 * If its an SNP guest, then VMSA was added in the RMP entry as
+	 * a guest owned page. Transition the page to hypervisor state
+	 * before releasing it back to the system.
+	 * Also the page is removed from the kernel direct map, so flush it
+	 * later after it is transitioned back to hypervisor state and
+	 * restored in the direct map.
+	 */
+	if (sev_snp_guest(vcpu->kvm)) {
+		u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
+
+		if (host_rmp_make_shared(pfn, PG_LEVEL_4K, true))
+			goto skip_vmsa_free;
+	}
+
  	if (vcpu->arch.guest_state_protected)
  		sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa);
 
  	__free_page(virt_to_page(svm->sev_es.vmsa));
 
+skip_vmsa_free:
  	if (svm->sev_es.ghcb_sa_free)
  		kvfree(svm->sev_es.ghcb_sa);
  }
include/uapi/linux/kvm.h +14 -0
index 9b6c95cc62a8..c468adc1f147 100644
@@ -1942,6 +1942,7 @@ enum sev_cmd_id {
  	KVM_SEV_SNP_INIT,
  	KVM_SEV_SNP_LAUNCH_START,
  	KVM_SEV_SNP_LAUNCH_UPDATE,
+	KVM_SEV_SNP_LAUNCH_FINISH,
 
  	KVM_SEV_NR_MAX,
  };
@@ -2076,6 +2077,19 @@ struct kvm_sev_snp_launch_update {
  	__u8 vmpl1_perms;
  };
 
+#define KVM_SEV_SNP_ID_BLOCK_SIZE	96
+#define KVM_SEV_SNP_ID_AUTH_SIZE	4096
+#define KVM_SEV_SNP_FINISH_DATA_SIZE	32
+
+struct kvm_sev_snp_launch_finish {
+	__u64 id_block_uaddr;
+	__u64 id_auth_uaddr;
+	__u8 id_block_en;
+	__u8 auth_key_en;
+	__u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE];
+	__u8 pad[6];
+};
+
  #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
  #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
  #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
maintainer
#135 lore raw

Re: [PATCH RFC v7 40/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command

Hello Tom,

Quoted 44 lines · Tom Lendacky
Quoted 43 lines · Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch.

While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages.

If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.

---
  .../virt/kvm/x86/amd-memory-encryption.rst    |  22 ++++ arch/x86/kvm/svm/sev.c                        | 119 ++++++++++++++++++ include/uapi/linux/kvm.h                      |  14 +++

  3 files changed, 155 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +0 -0
index c94be8e6d657..e4b42aaab1de 100644
@@ -513,6 +513,28 @@ Returns: 0 on success, -negative on error

  See the SEV-SNP spec for further details on how to build the VMPL permission mask and page type.
+21. KVM_SNP_LAUNCH_FINISH
+-------------------------

Documentation/virt/kvm/x86/amd-memory-encryption.rst +2 -0
+
+After completion of the SNP guest launch flow, the 

KVM_SNP_LAUNCH_FINISH command can be

Documentation/virt/kvm/x86/amd-memory-encryption.rst +8 -0
+issued to make the guest ready for the execution.
+
+Parameters (in): struct kvm_sev_snp_launch_finish
+
+Returns: 0 on success, -negative on error
+
+::
+
Quoted 9 lines
Quoted 6 lines

+        struct kvm_sev_snp_launch_finish {
+                __u64 id_block_uaddr;
+                __u64 id_auth_uaddr;
+                __u8 id_block_en;
+                __u8 auth_key_en;
+                __u8 host_data[32];

This is missing the 6 bytes of padding at the end of the struct.

Yes will fix this, the documentation is missing that, the structure defination in include/uapi/linux/kvm.h includes it.

But why do we need this padding ?

Quoted 101 lines
Quoted 101 lines

+        };

Documentation/virt/kvm/x86/amd-memory-encryption.rst +3 -0
+
+
+See SEV-SNP specification for further details on launch finish input 

parameters.
  References
  ==========

arch/x86/kvm/svm/sev.c +56 -0
index 379e61a9226a..6f901545bed9 100644
@@ -2243,6 +2243,106 @@ static int snp_launch_update(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
                         snp_launch_update_gfn_handler, argp);
   }
+static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd 
 *argp)
+{
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+    struct sev_data_snp_launch_update data = {};
+    int i, ret;
+
+    data.gctx_paddr = __psp_pa(sev->snp_context);
+    data.page_type = SNP_PAGE_TYPE_VMSA;
+
+    for (i = 0; i < kvm->created_vcpus; i++) {
+        struct vcpu_svm *svm = to_svm(xa_load(&kvm->vcpu_array, i));
+        u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
+
+        /* Perform some pre-encryption checks against the VMSA */
+        ret = sev_es_sync_vmsa(svm);
+        if (ret)
+            return ret;
+
+        /* Transition the VMSA page to a firmware state. */
+        ret = rmp_make_private(pfn, -1, PG_LEVEL_4K, sev->asid, true);
+        if (ret)
+            return ret;
+
+        /* Issue the SNP command to encrypt the VMSA */
+        data.address = __sme_pa(svm->sev_es.vmsa);
+        ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
+                      &data, &argp->error);
+        if (ret) {
+            snp_page_reclaim(pfn);
+            return ret;
+        }
+
+        svm->vcpu.arch.guest_state_protected = true;
+    }
+
+    return 0;
+}
+
+static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+    struct kvm_sev_snp_launch_finish params;
+    struct sev_data_snp_launch_finish *data;
+    void *id_block = NULL, *id_auth = NULL;
+    int ret;
+
+    if (!sev_snp_guest(kvm))
+        return -ENOTTY;
+
+    if (!sev->snp_context)
+        return -EINVAL;
+
+    if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, 
 sizeof(params)))
+        return -EFAULT;
+
+    /* Measure all vCPUs using LAUNCH_UPDATE before finalizing the 

launch flow. */
+    ret = snp_launch_update_vmsa(kvm, argp);
+    if (ret)
+        return ret;

arch/x86/kvm/svm/sev.c +7 -0
+
+    data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
+    if (!data)
+        return -ENOMEM;
+
+    if (params.id_block_en) {
+        id_block = psp_copy_user_blob(params.id_block_uaddr, 

KVM_SEV_SNP_ID_BLOCK_SIZE);
+        if (IS_ERR(id_block)) {
+            ret = PTR_ERR(id_block);
+            goto e_free;
+        }

arch/x86/kvm/svm/sev.c +5 -0
+
+        data->id_block_en = 1;
+        data->id_block_paddr = __sme_pa(id_block);
+
+        id_auth = psp_copy_user_blob(params.id_auth_uaddr, 

KVM_SEV_SNP_ID_AUTH_SIZE);
+        if (IS_ERR(id_auth)) {
+            ret = PTR_ERR(id_auth);
+            goto e_free_id_block;
+        }
+
+        data->id_auth_paddr = __sme_pa(id_auth);

Quoted 9 lines

arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2332

Quoted 6 lines
arch/x86/kvm/svm/sev.c +6 -0
+
+        if (params.auth_key_en)
+            data->auth_key_en = 1;
+    }
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);

This is missing the copying of the params.host_data field into the data->host_data field. This is needed so that the host_data shows up in the attestation report.

Yes will fix this.

Thanks,
Ashish

Quoted 85 lines

Thanks,
Tom

Quoted 82 lines

+    ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error);

arch/x86/kvm/svm/sev.c +12 -0
+
+    kfree(id_auth);
+
+e_free_id_block:
+    kfree(id_block);
+
+e_free:
+    kfree(data);
+
+    return ret;
+}
+

  int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) {
      struct kvm_sev_cmd sev_cmd;

arch/x86/kvm/svm/sev.c +0 -0
@@ -2339,6 +2439,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void 
 __user *argp)

      case KVM_SEV_SNP_LAUNCH_UPDATE:
          r = snp_launch_update(kvm, &sev_cmd);
          break;
+    case KVM_SEV_SNP_LAUNCH_FINISH:
+        r = snp_launch_finish(kvm, &sev_cmd);
+        break;
      default:
          r = -EINVAL;
          goto out;

arch/x86/kvm/svm/sev.c +16 -0
@@ -2794,11 +2897,27 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
       svm = to_svm(vcpu);
+    /*
+     * If its an SNP guest, then VMSA was added in the RMP entry as
+     * a guest owned page. Transition the page to hypervisor state
+     * before releasing it back to the system.
+     * Also the page is removed from the kernel direct map, so flush it
+     * later after it is transitioned back to hypervisor state and
+     * restored in the direct map.
+     */
+    if (sev_snp_guest(vcpu->kvm)) {
+        u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
+
+        if (host_rmp_make_shared(pfn, PG_LEVEL_4K, true))
+            goto skip_vmsa_free;
+    }
+
       if (vcpu->arch.guest_state_protected)
           sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa);
       __free_page(virt_to_page(svm->sev_es.vmsa));
+skip_vmsa_free:
       if (svm->sev_es.ghcb_sa_free)
           kvfree(svm->sev_es.ghcb_sa);
   }
include/uapi/linux/kvm.h +14 -0
index 9b6c95cc62a8..c468adc1f147 100644
@@ -1942,6 +1942,7 @@ enum sev_cmd_id {
       KVM_SEV_SNP_INIT,
       KVM_SEV_SNP_LAUNCH_START,
       KVM_SEV_SNP_LAUNCH_UPDATE,
+    KVM_SEV_SNP_LAUNCH_FINISH,
       KVM_SEV_NR_MAX,
   };
@@ -2076,6 +2077,19 @@ struct kvm_sev_snp_launch_update {
       __u8 vmpl1_perms;
   };
+#define KVM_SEV_SNP_ID_BLOCK_SIZE    96
+#define KVM_SEV_SNP_ID_AUTH_SIZE    4096
+#define KVM_SEV_SNP_FINISH_DATA_SIZE    32
+
+struct kvm_sev_snp_launch_finish {
+    __u64 id_block_uaddr;
+    __u64 id_auth_uaddr;
+    __u8 id_block_en;
+    __u8 auth_key_en;
+    __u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE];
+    __u8 pad[6];
+};
+
   #define KVM_DEV_ASSIGN_ENABLE_IOMMU    (1 << 0)
   #define KVM_DEV_ASSIGN_PCI_2_3        (1 << 1)
   #define KVM_DEV_ASSIGN_MASK_INTX    (1 << 2)
maintainer
#136 lore raw

Re: [PATCH RFC v7 40/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command

Quoted 57 lines · Ashish

Hello Tom,

Quoted 44 lines · Tom Lendacky
Quoted 43 lines · Michael Roth

From: Brijesh Singh <[email protected]>

The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch.

While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages.

If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.

---
  .../virt/kvm/x86/amd-memory-encryption.rst    |  22 ++++ arch/x86/kvm/svm/sev.c                        | 119 ++++++++++++++++++ include/uapi/linux/kvm.h                      |  14 +++

  3 files changed, 155 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +0 -0
index c94be8e6d657..e4b42aaab1de 100644
@@ -513,6 +513,28 @@ Returns: 0 on success, -negative on error

  See the SEV-SNP spec for further details on how to build the VMPL permission mask and page type.
+21. KVM_SNP_LAUNCH_FINISH
+-------------------------

Documentation/virt/kvm/x86/amd-memory-encryption.rst +2 -0
+
+After completion of the SNP guest launch flow, the 

KVM_SNP_LAUNCH_FINISH command can be

Documentation/virt/kvm/x86/amd-memory-encryption.rst +8 -0
+issued to make the guest ready for the execution.
+
+Parameters (in): struct kvm_sev_snp_launch_finish
+
+Returns: 0 on success, -negative on error
+
+::
+
Quoted 9 lines
Quoted 6 lines

+        struct kvm_sev_snp_launch_finish {
+                __u64 id_block_uaddr;
+                __u64 id_auth_uaddr;
+                __u8 id_block_en;
+                __u8 auth_key_en;
+                __u8 host_data[32];

This is missing the 6 bytes of padding at the end of the struct.

Quoted 4 lines

Yes will fix this, the documentation is missing that, the structure defination in include/uapi/linux/kvm.h includes it.

But why do we need this padding ?

I'm assuming it was added so that any new elements added would be aligned on an 8 byte boundary (should the next element added be a __u64). I don't think that it is truly needed right now, though.

Thanks,
Tom

#137 lore raw

Re: [PATCH RFC v7 40/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command

In reply to Michael Roth

arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2255

Quoted 10 lines · Michael Roth
arch/x86/kvm/svm/sev.c +10 -0
+static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+       struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+       struct sev_data_snp_launch_update data = {};
+       int i, ret;
+
+       data.gctx_paddr = __psp_pa(sev->snp_context);
+       data.page_type = SNP_PAGE_TYPE_VMSA;
+
+       for (i = 0; i < kvm->created_vcpus; i++) {

Should be replaced with kvm_for_each_vcpu() as it was done for sev_launch_update_vmsa() in c36b16d29f3a ("KVM: SVM: Use online_vcpus, not created_vcpus, to iterate over vCPUs").
Prevents accessing uninitialized data in struct vcpu_svm.

--
Sabin.

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

maintainer
#138 lore raw

Re: [PATCH RFC v7 40/64] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command

Quoted 18 lines · Sabin Rapan

arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2255

Quoted 10 lines · Michael Roth
arch/x86/kvm/svm/sev.c +10 -0
+static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+       struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+       struct sev_data_snp_launch_update data = {};
+       int i, ret;
+
+       data.gctx_paddr = __psp_pa(sev->snp_context);
+       data.page_type = SNP_PAGE_TYPE_VMSA;
+
+       for (i = 0; i < kvm->created_vcpus; i++) {

Should be replaced with kvm_for_each_vcpu() as it was done for sev_launch_update_vmsa() in c36b16d29f3a ("KVM: SVM: Use online_vcpus, not created_vcpus, to iterate over vCPUs").
Prevents accessing uninitialized data in struct vcpu_svm.

Yes, fixed this one.

Thanks,
Ashish

#139 lore raw

RFC PATCH v7 41/64 KVM: X86: Keep the NPT and RMP page level in sync

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

When running an SEV-SNP VM, the sPA used to index the RMP entry is obtained through the NPT translation (gva->gpa->spa). The NPT page level is checked against the page level programmed in the RMP entry. If the page level does not match, then it will cause a nested page fault with the RMP bit set to indicate the RMP violation.

---
 arch/x86/include/asm/kvm-x86-ops.h |  1 +
 arch/x86/include/asm/kvm_host.h    |  2 +
 arch/x86/kvm/mmu/mmu.c             | 12 +++++-
 arch/x86/kvm/svm/sev.c             | 66 ++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c             |  2 +
 arch/x86/kvm/svm/svm.h             |  1 +
 6 files changed, 83 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index e0015926cdf4..61e31b622fce 100644
@@ -136,6 +136,7 @@ KVM_X86_OP_OPTIONAL_RET0(private_mem_enabled);
 KVM_X86_OP_OPTIONAL_RET0(fault_is_private);
 KVM_X86_OP_OPTIONAL_RET0(update_mem_attr)
 KVM_X86_OP_OPTIONAL(invalidate_restricted_mem)
+KVM_X86_OP_OPTIONAL(rmp_page_level_adjust)
 
 #undef KVM_X86_OP
 #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index e2529415f28b..b126c6ac7ce4 100644
@@ -1724,6 +1724,8 @@ struct kvm_x86_ops {
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
 
 	void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu);
+
+	void (*rmp_page_level_adjust)(struct kvm *kvm, gfn_t gfn, int *level);
 };
 
 struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +11 -1
index 2713632e5061..25db83021500 100644
@@ -3053,6 +3053,11 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,
 
 out:
 	local_irq_restore(flags);
+
+	/* Adjust the page level based on the SEV-SNP RMP page level. */
+	if (kvm_x86_ops.rmp_page_level_adjust)
+		static_call(kvm_x86_rmp_page_level_adjust)(kvm, gfn, &level);
+
 	return level;
 }
 
@@ -3070,8 +3075,13 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm,
 			break;
 	}
 
-	if (is_private)
+	pr_debug("%s: gfn: %llx max_level: %d max_huge_page_level: %d\n",
+		 __func__, gfn, max_level, max_huge_page_level);
+	if (kvm_slot_can_be_private(slot) && is_private) {
+		if (kvm_x86_ops.rmp_page_level_adjust)
+			static_call(kvm_x86_rmp_page_level_adjust)(kvm, gfn, &max_level);
 		return max_level;
+	}
 
 	if (max_level == PG_LEVEL_4K)
 		return PG_LEVEL_4K;
arch/x86/kvm/svm/sev.c +66 -0

72 lines not on this page · raw message

arch/x86/kvm/svm/svm.c +2 -0
index 013f811c733c..2dfa150bcb09 100644
@@ -4843,6 +4843,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 	.alloc_apic_backing_page = svm_alloc_apic_backing_page,
 
 	.fault_is_private = sev_fault_is_private,
+
+	.rmp_page_level_adjust = sev_rmp_page_level_adjust,
 };
 
 /*
arch/x86/kvm/svm/svm.h +1 -0
index 17200c1ad20e..ae733188cf87 100644
@@ -698,6 +698,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
 void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);
 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
 struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);
+void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
Signature · 1 line
2.25.1
#140 lore raw

RFC PATCH v7 42/64 KVM: x86: Define RMP page fault error bits for #NPF

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF).

See APM2 section 16.36.10 for more details.

---
 arch/x86/include/asm/kvm_host.h | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/include/asm/kvm_host.h +8 -0
index b126c6ac7ce4..f4bb0821757e 100644
@@ -257,9 +257,13 @@ enum x86_intercept_stage;
 #define PFERR_FETCH_BIT 4
 #define PFERR_PK_BIT 5
 #define PFERR_SGX_BIT 15
+#define PFERR_GUEST_RMP_BIT 31
 #define PFERR_GUEST_FINAL_BIT 32
 #define PFERR_GUEST_PAGE_BIT 33
 #define PFERR_IMPLICIT_ACCESS_BIT 48
+#define PFERR_GUEST_ENC_BIT 34
+#define PFERR_GUEST_SIZEM_BIT 35
+#define PFERR_GUEST_VMPL_BIT 36
 
 #define PFERR_PRESENT_MASK	BIT(PFERR_PRESENT_BIT)
 #define PFERR_WRITE_MASK	BIT(PFERR_WRITE_BIT)
@@ -271,6 +275,10 @@ enum x86_intercept_stage;
 #define PFERR_GUEST_FINAL_MASK	BIT_ULL(PFERR_GUEST_FINAL_BIT)
 #define PFERR_GUEST_PAGE_MASK	BIT_ULL(PFERR_GUEST_PAGE_BIT)
 #define PFERR_IMPLICIT_ACCESS	BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT)
+#define PFERR_GUEST_RMP_MASK	BIT_ULL(PFERR_GUEST_RMP_BIT)
+#define PFERR_GUEST_ENC_MASK	BIT_ULL(PFERR_GUEST_ENC_BIT)
+#define PFERR_GUEST_SIZEM_MASK	BIT_ULL(PFERR_GUEST_SIZEM_BIT)
+#define PFERR_GUEST_VMPL_MASK	BIT_ULL(PFERR_GUEST_VMPL_BIT)
 
 #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK |	\
 				 PFERR_WRITE_MASK |		\
Signature · 1 line
2.25.1
#141 lore raw

RFC PATCH v7 43/64 KVM: SVM: Do not use long-lived GHCB map while setting scratch area

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

The setup_vmgexit_scratch() function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping.

---
 arch/x86/kvm/svm/sev.c | 74 +++++++++++++++++++-----------------------
 arch/x86/kvm/svm/svm.h |  3 +-
 2 files changed, 36 insertions(+), 41 deletions(-)
arch/x86/kvm/svm/sev.c +34 -40
index 443c5c8aaaf3..d5c6e48055fb 100644
@@ -2918,8 +2918,7 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
 	__free_page(virt_to_page(svm->sev_es.vmsa));
 
 skip_vmsa_free:
-	if (svm->sev_es.ghcb_sa_free)
-		kvfree(svm->sev_es.ghcb_sa);
+	kvfree(svm->sev_es.ghcb_sa);
 }
 
 static void dump_ghcb(struct vcpu_svm *svm)
@@ -3007,6 +3006,9 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
 	control->exit_info_1 = ghcb_get_sw_exit_info_1(ghcb);
 	control->exit_info_2 = ghcb_get_sw_exit_info_2(ghcb);
 
+	/* Copy the GHCB scratch area GPA */
+	svm->sev_es.ghcb_sa_gpa = ghcb_get_sw_scratch(ghcb);
+
 	/* Clear the valid entries fields */
 	memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
 }
@@ -3152,23 +3154,12 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)
 	if (!svm->sev_es.ghcb)
 		return;
 
-	if (svm->sev_es.ghcb_sa_free) {
-		/*
-		 * The scratch area lives outside the GHCB, so there is a
-		 * buffer that, depending on the operation performed, may
-		 * need to be synced, then freed.
-		 */
-		if (svm->sev_es.ghcb_sa_sync) {
-			kvm_write_guest(svm->vcpu.kvm,
-					ghcb_get_sw_scratch(svm->sev_es.ghcb),
-					svm->sev_es.ghcb_sa,
-					svm->sev_es.ghcb_sa_len);
-			svm->sev_es.ghcb_sa_sync = false;
-		}
-
-		kvfree(svm->sev_es.ghcb_sa);
-		svm->sev_es.ghcb_sa = NULL;
-		svm->sev_es.ghcb_sa_free = false;
+	 /* Sync the scratch buffer area. */
+	if (svm->sev_es.ghcb_sa_sync) {
+		kvm_write_guest(svm->vcpu.kvm,
+				ghcb_get_sw_scratch(svm->sev_es.ghcb),
+				svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len);
+		svm->sev_es.ghcb_sa_sync = false;
 	}
 
 	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb);
@@ -3209,9 +3200,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 	struct ghcb *ghcb = svm->sev_es.ghcb;
 	u64 ghcb_scratch_beg, ghcb_scratch_end;
 	u64 scratch_gpa_beg, scratch_gpa_end;
-	void *scratch_va;
 
-	scratch_gpa_beg = ghcb_get_sw_scratch(ghcb);
+	scratch_gpa_beg = svm->sev_es.ghcb_sa_gpa;
 	if (!scratch_gpa_beg) {
 		pr_err("vmgexit: scratch gpa not provided\n");
 		goto e_scratch;
@@ -3241,9 +3231,6 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 			       scratch_gpa_beg, scratch_gpa_end);
 			goto e_scratch;
 		}
-
-		scratch_va = (void *)svm->sev_es.ghcb;
-		scratch_va += (scratch_gpa_beg - control->ghcb_gpa);
 	} else {
 		/*
 		 * The guest memory must be read into a kernel buffer, so
@@ -3254,29 +3241,36 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 			       len, GHCB_SCRATCH_AREA_LIMIT);
 			goto e_scratch;
 		}
-		scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT);
-		if (!scratch_va)
-			return -ENOMEM;
+	}
 
-		if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {
-			/* Unable to copy scratch area from guest */
-			pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
+	if (svm->sev_es.ghcb_sa_alloc_len < len) {
+		void *scratch_va = kvzalloc(len, GFP_KERNEL_ACCOUNT);
 
-			kvfree(scratch_va);
-			return -EFAULT;
-		}
+		if (!scratch_va)
+			return -ENOMEM;
 
 		/*
-		 * The scratch area is outside the GHCB. The operation will
-		 * dictate whether the buffer needs to be synced before running
-		 * the vCPU next time (i.e. a read was requested so the data
-		 * must be written back to the guest memory).
+		 * Free the old scratch area and switch to using newly
+		 * allocated.
 		 */
-		svm->sev_es.ghcb_sa_sync = sync;
-		svm->sev_es.ghcb_sa_free = true;
+		kvfree(svm->sev_es.ghcb_sa);
+
+		svm->sev_es.ghcb_sa_alloc_len = len;
+		svm->sev_es.ghcb_sa = scratch_va;
 	}
 
-	svm->sev_es.ghcb_sa = scratch_va;
+	if (kvm_read_guest(svm->vcpu.kvm, scratch_gpa_beg, svm->sev_es.ghcb_sa, len)) {
+		/* Unable to copy scratch area from guest */
+		pr_err("vmgexit: kvm_read_guest for scratch area failed\n");
+		return -EFAULT;
+	}
+
+	/*
+	 * The operation will dictate whether the buffer needs to be synced
+	 * before running the vCPU next time (i.e. a read was requested so
+	 * the data must be written back to the guest memory).
+	 */
+	svm->sev_es.ghcb_sa_sync = sync;
 	svm->sev_es.ghcb_sa_len = len;
 
 	return 0;
arch/x86/kvm/svm/svm.h +2 -1
index ae733188cf87..f53a41e13033 100644
@@ -209,8 +209,9 @@ struct vcpu_sev_es_state {
 	/* SEV-ES scratch area support */
 	void *ghcb_sa;
 	u32 ghcb_sa_len;
+	u64 ghcb_sa_gpa;
+	u32 ghcb_sa_alloc_len;
 	bool ghcb_sa_sync;
-	bool ghcb_sa_free;
 };
 
 struct vcpu_svm {
Signature · 1 line
2.25.1
#142 lore raw

RFC PATCH v7 44/64 KVM: SVM: Remove the long-lived GHCB host map

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

On VMGEXIT, sev_handle_vmgexit() creates a host mapping for the GHCB GPA, and unmaps it just before VM-entry. This long-lived GHCB map is used by the VMGEXIT handler through accessors such as ghcb_{set_get}_xxx().

A long-lived GHCB map can cause issue when SEV-SNP is enabled. When SEV-SNP is enabled the mapped GPA needs to be protected against a page state change.

To eliminate the long-lived GHCB mapping, update the GHCB sync operations to explicitly map the GHCB before access and unmap it after access is complete. This requires that the setting of the GHCBs sw_exit_info_{1,2} fields be done during sev_es_sync_to_ghcb(), so create two new fields in the vcpu_svm struct to hold these values when required to be set outside of the GHCB mapping.

[mdr: defer per_cpu() assignment and order it with barrier() to fix case
where kvm_vcpu_map() causes reschedule on different CPU]

---
 arch/x86/kvm/svm/sev.c | 131 ++++++++++++++++++++++++++---------------
 arch/x86/kvm/svm/svm.c |  18 +++---
 arch/x86/kvm/svm/svm.h |  24 +++++++-
 3 files changed, 116 insertions(+), 57 deletions(-)
arch/x86/kvm/svm/sev.c +83 -48
index d5c6e48055fb..6ac0cb6e3484 100644
@@ -2921,15 +2921,40 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
 	kvfree(svm->sev_es.ghcb_sa);
 }
 
+static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+	struct vmcb_control_area *control = &svm->vmcb->control;
+	u64 gfn = gpa_to_gfn(control->ghcb_gpa);
+
+	if (kvm_vcpu_map(&svm->vcpu, gfn, map)) {
+		/* Unable to map GHCB from guest */
+		pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+	kvm_vcpu_unmap(&svm->vcpu, map, true);
+}
+
 static void dump_ghcb(struct vcpu_svm *svm)
 {
-	struct ghcb *ghcb = svm->sev_es.ghcb;
+	struct kvm_host_map map;
 	unsigned int nbits;
+	struct ghcb *ghcb;
+
+	if (svm_map_ghcb(svm, &map))
+		return;
+
+	ghcb = map.hva;
 
 	/* Re-use the dump_invalid_vmcb module parameter */
 	if (!dump_invalid_vmcb) {
 		pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
-		return;
+		goto e_unmap;
 	}
 
 	nbits = sizeof(ghcb->save.valid_bitmap) * 8;
@@ -2944,12 +2969,21 @@ static void dump_ghcb(struct vcpu_svm *svm)
 	pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
 	       ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
 	pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
+
+e_unmap:
+	svm_unmap_ghcb(svm, &map);
 }
 
-static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
+static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm)
 {
 	struct kvm_vcpu *vcpu = &svm->vcpu;
-	struct ghcb *ghcb = svm->sev_es.ghcb;
+	struct kvm_host_map map;
+	struct ghcb *ghcb;
+
+	if (svm_map_ghcb(svm, &map))
+		return false;
+
+	ghcb = map.hva;
 
 	/*
 	 * The GHCB protocol so far allows for the following data
@@ -2963,13 +2997,24 @@ static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
 	ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
 	ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
 	ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
+
+	/*
+	 * Copy the return values from the exit_info_{1,2}.
+	 */
+	ghcb_set_sw_exit_info_1(ghcb, svm->sev_es.ghcb_sw_exit_info_1);
+	ghcb_set_sw_exit_info_2(ghcb, svm->sev_es.ghcb_sw_exit_info_2);
+
+	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, ghcb);
+
+	svm_unmap_ghcb(svm, &map);
+
+	return true;
 }
 
-static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
+static void sev_es_sync_from_ghcb(struct vcpu_svm *svm, struct ghcb *ghcb)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
 	struct kvm_vcpu *vcpu = &svm->vcpu;
-	struct ghcb *ghcb = svm->sev_es.ghcb;
 	u64 exit_code;
 
 	/*
@@ -3013,20 +3058,25 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
 	memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
 }
 
-static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
+static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
 {
-	struct kvm_vcpu *vcpu;
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm_host_map map;
 	struct ghcb *ghcb;
-	u64 exit_code;
 	u64 reason;
 
-	ghcb = svm->sev_es.ghcb;
+	if (svm_map_ghcb(svm, &map))
+		return -EFAULT;
+
+	ghcb = map.hva;
+
+	trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
 
 	/*
 	 * Retrieve the exit code now even though it may not be marked valid
 	 * as it could help with debugging.
 	 */
-	exit_code = ghcb_get_sw_exit_code(ghcb);
+	*exit_code = ghcb_get_sw_exit_code(ghcb);
 
 	/* Only GHCB Usage code 0 is supported */
 	if (ghcb->ghcb_usage) {
@@ -3119,6 +3169,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 		goto vmgexit_err;
 	}
 
+	sev_es_sync_from_ghcb(svm, ghcb);
+
+	svm_unmap_ghcb(svm, &map);
 	return 0;
 
 vmgexit_err:
@@ -3129,10 +3182,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 			    ghcb->ghcb_usage);
 	} else if (reason == GHCB_ERR_INVALID_EVENT) {
 		vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
-			    exit_code);
+			    *exit_code);
 	} else {
 		vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
-			    exit_code);
+			    *exit_code);
 		dump_ghcb(svm);
 	}
 
@@ -3142,6 +3195,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 	ghcb_set_sw_exit_info_1(ghcb, 2);
 	ghcb_set_sw_exit_info_2(ghcb, reason);
 
+	svm_unmap_ghcb(svm, &map);
+
 	/* Resume the guest to "return" the error code. */
 	return 1;
 }
@@ -3151,23 +3206,20 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)
 	/* Clear any indication that the vCPU is in a type of AP Reset Hold */
 	svm->sev_es.ap_reset_hold_type = AP_RESET_HOLD_NONE;
 
-	if (!svm->sev_es.ghcb)
+	if (!svm->sev_es.ghcb_in_use)
 		return;
 
 	 /* Sync the scratch buffer area. */
 	if (svm->sev_es.ghcb_sa_sync) {
 		kvm_write_guest(svm->vcpu.kvm,
-				ghcb_get_sw_scratch(svm->sev_es.ghcb),
+				svm->sev_es.ghcb_sa_gpa,
 				svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len);
 		svm->sev_es.ghcb_sa_sync = false;
 	}
 
-	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, svm->sev_es.ghcb);
-
 	sev_es_sync_to_ghcb(svm);
 
-	kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true);
-	svm->sev_es.ghcb = NULL;
+	svm->sev_es.ghcb_in_use = false;
 }
 
 void pre_sev_run(struct vcpu_svm *svm, int cpu)
@@ -3197,7 +3249,6 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)
 static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
-	struct ghcb *ghcb = svm->sev_es.ghcb;
 	u64 ghcb_scratch_beg, ghcb_scratch_end;
 	u64 scratch_gpa_beg, scratch_gpa_end;
 
@@ -3276,8 +3327,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 	return 0;
 
 e_scratch:
-	ghcb_set_sw_exit_info_1(ghcb, 2);
-	ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_SCRATCH_AREA);
+	svm_set_ghcb_sw_exit_info_1(&svm->vcpu, 2);
+	svm_set_ghcb_sw_exit_info_2(&svm->vcpu, GHCB_ERR_INVALID_SCRATCH_AREA);
 
 	return 1;
 }
@@ -3413,7 +3464,6 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct vmcb_control_area *control = &svm->vmcb->control;
 	u64 ghcb_gpa, exit_code;
-	struct ghcb *ghcb;
 	int ret;
 
 	/* Validate the GHCB */
@@ -3428,29 +3478,14 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 		return 1;
 	}
 
-	if (kvm_vcpu_map(vcpu, ghcb_gpa >> PAGE_SHIFT, &svm->sev_es.ghcb_map)) {
-		/* Unable to map GHCB from guest */
-		vcpu_unimpl(vcpu, "vmgexit: error mapping GHCB [%#llx] from guest\n",
-			    ghcb_gpa);
-
-		/* Without a GHCB, just return right back to the guest */
-		return 1;
-	}
-
-	svm->sev_es.ghcb = svm->sev_es.ghcb_map.hva;
-	ghcb = svm->sev_es.ghcb_map.hva;
-
-	trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
-
-	exit_code = ghcb_get_sw_exit_code(ghcb);
-
-	ret = sev_es_validate_vmgexit(svm);
+	ret = sev_es_validate_vmgexit(svm, &exit_code);
 	if (ret)
 		return ret;
 
-	sev_es_sync_from_ghcb(svm);
-	ghcb_set_sw_exit_info_1(ghcb, 0);
-	ghcb_set_sw_exit_info_2(ghcb, 0);
+	svm->sev_es.ghcb_in_use = true;
+
+	svm_set_ghcb_sw_exit_info_1(vcpu, 0);
+	svm_set_ghcb_sw_exit_info_2(vcpu, 0);
 
 	switch (exit_code) {
 	case SVM_VMGEXIT_MMIO_READ:
@@ -3490,20 +3525,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 			break;
 		case 1:
 			/* Get AP jump table address */
-			ghcb_set_sw_exit_info_2(ghcb, sev->ap_jump_table);
+			svm_set_ghcb_sw_exit_info_2(vcpu, sev->ap_jump_table);
 			break;
 		default:
 			pr_err("svm: vmgexit: unsupported AP jump table request - exit_info_1=%#llx\n",
 			       control->exit_info_1);
-			ghcb_set_sw_exit_info_1(ghcb, 2);
-			ghcb_set_sw_exit_info_2(ghcb, GHCB_ERR_INVALID_INPUT);
+			svm_set_ghcb_sw_exit_info_1(vcpu, 2);
+			svm_set_ghcb_sw_exit_info_2(vcpu, GHCB_ERR_INVALID_INPUT);
 		}
 
 		ret = 1;
 		break;
 	}
 	case SVM_VMGEXIT_HV_FEATURES: {
-		ghcb_set_sw_exit_info_2(ghcb, GHCB_HV_FT_SUPPORTED);
+		svm_set_ghcb_sw_exit_info_2(vcpu, GHCB_HV_FT_SUPPORTED);
 
 		ret = 1;
 		break;
@@ -3651,7 +3686,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
 		 * Return from an AP Reset Hold VMGEXIT, where the guest will
 		 * set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value.
 		 */
-		ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1);
+		svm_set_ghcb_sw_exit_info_2(vcpu, 1);
 		break;
 	case AP_RESET_HOLD_MSR_PROTO:
 		/*
arch/x86/kvm/svm/svm.c +11 -7
index 2dfa150bcb09..1826946a2f43 100644
@@ -1445,7 +1445,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
 static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-	struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
+	struct svm_cpu_data *sd;
 
 	if (sev_es_guest(vcpu->kvm))
 		sev_es_unmap_ghcb(svm);
@@ -1453,6 +1453,10 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
 	if (svm->guest_state_loaded)
 		return;
 
+	/* sev_es_unmap_ghcb() can resched, so grab per-cpu pointer afterward. */
+	barrier();
+	sd = per_cpu_ptr(&svm_data, vcpu->cpu);
+
 	/*
 	 * Save additional host state that will be restored on VMEXIT (sev-es)
 	 * or subsequent vmload of host save area.
@@ -2818,14 +2822,14 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-	if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
+	if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb_in_use))
 		return kvm_complete_insn_gp(vcpu, err);
 
-	ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1);
-	ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
-				X86_TRAP_GP |
-				SVM_EVTINJ_TYPE_EXEPT |
-				SVM_EVTINJ_VALID);
+	svm_set_ghcb_sw_exit_info_1(vcpu, 1);
+	svm_set_ghcb_sw_exit_info_2(vcpu,
+				    X86_TRAP_GP |
+				    SVM_EVTINJ_TYPE_EXEPT |
+				    SVM_EVTINJ_VALID);
 	return 1;
 }
 
arch/x86/kvm/svm/svm.h +22 -2
index f53a41e13033..c462dfac0a0d 100644
@@ -201,8 +201,7 @@ struct svm_nested_state {
 struct vcpu_sev_es_state {
 	/* SEV-ES support */
 	struct sev_es_save_area *vmsa;
-	struct ghcb *ghcb;
-	struct kvm_host_map ghcb_map;
+	bool ghcb_in_use;
 	bool received_first_sipi;
 	unsigned int ap_reset_hold_type;
 
@@ -212,6 +211,13 @@ struct vcpu_sev_es_state {
 	u64 ghcb_sa_gpa;
 	u32 ghcb_sa_alloc_len;
 	bool ghcb_sa_sync;
+
+	/*
+	 * SEV-ES support to hold the sw_exit_info return values to be
+	 * sync'ed to the GHCB when mapped.
+	 */
+	u64 ghcb_sw_exit_info_1;
+	u64 ghcb_sw_exit_info_2;
 };
 
 struct vcpu_svm {
@@ -640,6 +646,20 @@ void nested_sync_control_from_vmcb02(struct vcpu_svm *svm);
 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm);
 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
 
+static inline void svm_set_ghcb_sw_exit_info_1(struct kvm_vcpu *vcpu, u64 val)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	svm->sev_es.ghcb_sw_exit_info_1 = val;
+}
+
+static inline void svm_set_ghcb_sw_exit_info_2(struct kvm_vcpu *vcpu, u64 val)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	svm->sev_es.ghcb_sw_exit_info_2 = val;
+}
+
 extern struct kvm_x86_nested_ops svm_nested_ops;
 
 /* avic.c */
Signature · 1 line
2.25.1
#143 lore raw

Re: [PATCH RFC v7 44/64] KVM: SVM: Remove the long-lived GHCB host map

Quoted 56 lines · Michael Roth

From: Brijesh Singh <[email protected]>

On VMGEXIT, sev_handle_vmgexit() creates a host mapping for the GHCB GPA, and unmaps it just before VM-entry. This long-lived GHCB map is used by the VMGEXIT handler through accessors such as ghcb_{set_get}_xxx().

A long-lived GHCB map can cause issue when SEV-SNP is enabled. When SEV-SNP is enabled the mapped GPA needs to be protected against a page state change.

To eliminate the long-lived GHCB mapping, update the GHCB sync operations to explicitly map the GHCB before access and unmap it after access is complete. This requires that the setting of the GHCBs sw_exit_info_{1,2} fields be done during sev_es_sync_to_ghcb(), so create two new fields in the vcpu_svm struct to hold these values when required to be set outside of the GHCB mapping.

[mdr: defer per_cpu() assignment and order it with barrier() to fix case
where kvm_vcpu_map() causes reschedule on different CPU]

---
 arch/x86/kvm/svm/sev.c | 131 ++++++++++++++++++++++++++---------------
 arch/x86/kvm/svm/svm.c |  18 +++---
 arch/x86/kvm/svm/svm.h |  24 +++++++-
 3 files changed, 116 insertions(+), 57 deletions(-)
arch/x86/kvm/svm/sev.c +19 -0
index d5c6e48055fb..6ac0cb6e3484 100644
@@ -2921,15 +2921,40 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
 	kvfree(svm->sev_es.ghcb_sa);
 }
 
+static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+	struct vmcb_control_area *control = &svm->vmcb->control;
+	u64 gfn = gpa_to_gfn(control->ghcb_gpa);
+
+	if (kvm_vcpu_map(&svm->vcpu, gfn, map)) {
+		/* Unable to map GHCB from guest */
+		pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+	kvm_vcpu_unmap(&svm->vcpu, map, true);
+}
+
 static void dump_ghcb(struct vcpu_svm *svm)

arch/x86/kvm/svm/sev.c void sev_free_vcpu(struct kvm_vcpu *vcpu) · line 2952

Quoted 10 lines
arch/x86/kvm/svm/sev.c +7 -1
 {
-	struct ghcb *ghcb = svm->sev_es.ghcb;
+	struct kvm_host_map map;
 	unsigned int nbits;
+	struct ghcb *ghcb;
+
+	if (svm_map_ghcb(svm, &map))
+		return;
+
+	ghcb = map.hva;

dump_ghcb() is called from sev_es_validate_vmgexit() with the ghcb already mapped. How about passing 'struct kvm_host_map *' (or struct ghcb *) as a param to avoid double mapping?

Quoted 123 lines
arch/x86/kvm/svm/sev.c +42 -12
 	/* Re-use the dump_invalid_vmcb module parameter */
 	if (!dump_invalid_vmcb) {
 		pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
-		return;
+		goto e_unmap;
 	}
 
 	nbits = sizeof(ghcb->save.valid_bitmap) * 8;
@@ -2944,12 +2969,21 @@ static void dump_ghcb(struct vcpu_svm *svm)
 	pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
 	       ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
 	pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
+
+e_unmap:
+	svm_unmap_ghcb(svm, &map);
 }
 
-static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
+static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm)
 {
 	struct kvm_vcpu *vcpu = &svm->vcpu;
-	struct ghcb *ghcb = svm->sev_es.ghcb;
+	struct kvm_host_map map;
+	struct ghcb *ghcb;
+
+	if (svm_map_ghcb(svm, &map))
+		return false;
+
+	ghcb = map.hva;
 
 	/*
 	 * The GHCB protocol so far allows for the following data
@@ -2963,13 +2997,24 @@ static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
 	ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
 	ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
 	ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
+
+	/*
+	 * Copy the return values from the exit_info_{1,2}.
+	 */
+	ghcb_set_sw_exit_info_1(ghcb, svm->sev_es.ghcb_sw_exit_info_1);
+	ghcb_set_sw_exit_info_2(ghcb, svm->sev_es.ghcb_sw_exit_info_2);
+
+	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, ghcb);
+
+	svm_unmap_ghcb(svm, &map);
+
+	return true;
 }
 
-static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
+static void sev_es_sync_from_ghcb(struct vcpu_svm *svm, struct ghcb *ghcb)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
 	struct kvm_vcpu *vcpu = &svm->vcpu;
-	struct ghcb *ghcb = svm->sev_es.ghcb;
 	u64 exit_code;
 
 	/*
@@ -3013,20 +3058,25 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
 	memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
 }
 
-static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
+static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
 {
-	struct kvm_vcpu *vcpu;
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm_host_map map;
 	struct ghcb *ghcb;
-	u64 exit_code;
 	u64 reason;
 
-	ghcb = svm->sev_es.ghcb;
+	if (svm_map_ghcb(svm, &map))
+		return -EFAULT;
+
+	ghcb = map.hva;
+
+	trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
 
 	/*
 	 * Retrieve the exit code now even though it may not be marked valid
 	 * as it could help with debugging.
 	 */
-	exit_code = ghcb_get_sw_exit_code(ghcb);
+	*exit_code = ghcb_get_sw_exit_code(ghcb);
 
 	/* Only GHCB Usage code 0 is supported */
 	if (ghcb->ghcb_usage) {
@@ -3119,6 +3169,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 		goto vmgexit_err;
 	}
 
+	sev_es_sync_from_ghcb(svm, ghcb);
+
+	svm_unmap_ghcb(svm, &map);
 	return 0;
 
 vmgexit_err:
@@ -3129,10 +3182,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 			    ghcb->ghcb_usage);
 	} else if (reason == GHCB_ERR_INVALID_EVENT) {
 		vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
-			    exit_code);
+			    *exit_code);
 	} else {
 		vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
-			    exit_code);
+			    *exit_code);
 		dump_ghcb(svm);
 	}
 
@@ -3142,6 +3195,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
 	ghcb_set_sw_exit_info_1(ghcb, 2);
 	ghcb_set_sw_exit_info_2(ghcb, reason);
 
+	svm_unmap_ghcb(svm, &map);
+
 	/* Resume the guest to "return" the error code. */
 	return 1;
 }
#144 lore raw

Re: [PATCH RFC v7 44/64] KVM: SVM: Remove the long-lived GHCB host map

Quoted 64 lines · Jeremi Piotrowski
Quoted 62 lines · Michael Roth

From: Brijesh Singh <[email protected]>

On VMGEXIT, sev_handle_vmgexit() creates a host mapping for the GHCB GPA, and unmaps it just before VM-entry. This long-lived GHCB map is used by the VMGEXIT handler through accessors such as ghcb_{set_get}_xxx().

A long-lived GHCB map can cause issue when SEV-SNP is enabled. When SEV-SNP is enabled the mapped GPA needs to be protected against a page state change.

To eliminate the long-lived GHCB mapping, update the GHCB sync operations to explicitly map the GHCB before access and unmap it after access is complete. This requires that the setting of the GHCBs sw_exit_info_{1,2} fields be done during sev_es_sync_to_ghcb(), so create two new fields in the vcpu_svm struct to hold these values when required to be set outside of the GHCB mapping.

[mdr: defer per_cpu() assignment and order it with barrier() to fix case
where kvm_vcpu_map() causes reschedule on different CPU]

---
 arch/x86/kvm/svm/sev.c | 131 ++++++++++++++++++++++++++---------------
 arch/x86/kvm/svm/svm.c |  18 +++---
 arch/x86/kvm/svm/svm.h |  24 +++++++-
 3 files changed, 116 insertions(+), 57 deletions(-)
arch/x86/kvm/svm/sev.c +22 -1
index d5c6e48055fb..6ac0cb6e3484 100644
@@ -2921,15 +2921,40 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
      kvfree(svm->sev_es.ghcb_sa);
 }
 
+static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+     struct vmcb_control_area *control = &svm->vmcb->control;
+     u64 gfn = gpa_to_gfn(control->ghcb_gpa);
+
+     if (kvm_vcpu_map(&svm->vcpu, gfn, map)) {
+             /* Unable to map GHCB from guest */
+             pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn);
+             return -EFAULT;
+     }
+
+     return 0;
+}
+
+static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+     kvm_vcpu_unmap(&svm->vcpu, map, true);
+}
+
 static void dump_ghcb(struct vcpu_svm *svm)
 {
-     struct ghcb *ghcb = svm->sev_es.ghcb;
+     struct kvm_host_map map;
      unsigned int nbits;
+     struct ghcb *ghcb;
+
Quoted 8 lines

arch/x86/kvm/svm/sev.c void sev_free_vcpu(struct kvm_vcpu *vcpu) · line 2952

Quoted 4 lines
arch/x86/kvm/svm/sev.c +4 -0
+     if (svm_map_ghcb(svm, &map))
+             return;
+
+     ghcb = map.hva;

dump_ghcb() is called from sev_es_validate_vmgexit() with the ghcb already mapped. How about passing 'struct kvm_host_map *' (or struct ghcb *) as a param to avoid double mapping?

This also causes a soft lockup, PSC spin lock is already acquired in sev_es_validate_vmgexit. dump_ghcb will try to acquire the same lock again. So a guest can send an invalid ghcb page and cause a host soft lockup.

Quoted 125 lines
Quoted 124 lines
arch/x86/kvm/svm/sev.c +42 -12
      /* Re-use the dump_invalid_vmcb module parameter */
      if (!dump_invalid_vmcb) {
              pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
-             return;
+             goto e_unmap;
      }
 
      nbits = sizeof(ghcb->save.valid_bitmap) * 8;
@@ -2944,12 +2969,21 @@ static void dump_ghcb(struct vcpu_svm *svm)
      pr_err("%-20s%016llx is_valid: %u\n", "sw_scratch",
             ghcb->save.sw_scratch, ghcb_sw_scratch_is_valid(ghcb));
      pr_err("%-20s%*pb\n", "valid_bitmap", nbits, ghcb->save.valid_bitmap);
+
+e_unmap:
+     svm_unmap_ghcb(svm, &map);
 }
 
-static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
+static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm)
 {
      struct kvm_vcpu *vcpu = &svm->vcpu;
-     struct ghcb *ghcb = svm->sev_es.ghcb;
+     struct kvm_host_map map;
+     struct ghcb *ghcb;
+
+     if (svm_map_ghcb(svm, &map))
+             return false;
+
+     ghcb = map.hva;
 
      /*
       * The GHCB protocol so far allows for the following data
@@ -2963,13 +2997,24 @@ static void sev_es_sync_to_ghcb(struct vcpu_svm *svm)
      ghcb_set_rbx(ghcb, vcpu->arch.regs[VCPU_REGS_RBX]);
      ghcb_set_rcx(ghcb, vcpu->arch.regs[VCPU_REGS_RCX]);
      ghcb_set_rdx(ghcb, vcpu->arch.regs[VCPU_REGS_RDX]);
+
+     /*
+      * Copy the return values from the exit_info_{1,2}.
+      */
+     ghcb_set_sw_exit_info_1(ghcb, svm->sev_es.ghcb_sw_exit_info_1);
+     ghcb_set_sw_exit_info_2(ghcb, svm->sev_es.ghcb_sw_exit_info_2);
+
+     trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, ghcb);
+
+     svm_unmap_ghcb(svm, &map);
+
+     return true;
 }
 
-static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
+static void sev_es_sync_from_ghcb(struct vcpu_svm *svm, struct ghcb *ghcb)
 {
      struct vmcb_control_area *control = &svm->vmcb->control;
      struct kvm_vcpu *vcpu = &svm->vcpu;
-     struct ghcb *ghcb = svm->sev_es.ghcb;
      u64 exit_code;
 
      /*
@@ -3013,20 +3058,25 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
      memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
 }
 
-static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
+static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
 {
-     struct kvm_vcpu *vcpu;
+     struct kvm_vcpu *vcpu = &svm->vcpu;
+     struct kvm_host_map map;
      struct ghcb *ghcb;
-     u64 exit_code;
      u64 reason;
 
-     ghcb = svm->sev_es.ghcb;
+     if (svm_map_ghcb(svm, &map))
+             return -EFAULT;
+
+     ghcb = map.hva;
+
+     trace_kvm_vmgexit_enter(vcpu->vcpu_id, ghcb);
 
      /*
       * Retrieve the exit code now even though it may not be marked valid
       * as it could help with debugging.
       */
-     exit_code = ghcb_get_sw_exit_code(ghcb);
+     *exit_code = ghcb_get_sw_exit_code(ghcb);
 
      /* Only GHCB Usage code 0 is supported */
      if (ghcb->ghcb_usage) {
@@ -3119,6 +3169,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
              goto vmgexit_err;
      }
 
+     sev_es_sync_from_ghcb(svm, ghcb);
+
+     svm_unmap_ghcb(svm, &map);
      return 0;
 
 vmgexit_err:
@@ -3129,10 +3182,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
                          ghcb->ghcb_usage);
      } else if (reason == GHCB_ERR_INVALID_EVENT) {
              vcpu_unimpl(vcpu, "vmgexit: exit code %#llx is not valid\n",
-                         exit_code);
+                         *exit_code);
      } else {
              vcpu_unimpl(vcpu, "vmgexit: exit code %#llx input is not valid\n",
-                         exit_code);
+                         *exit_code);
              dump_ghcb(svm);
      }
 
@@ -3142,6 +3195,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)
      ghcb_set_sw_exit_info_1(ghcb, 2);
      ghcb_set_sw_exit_info_2(ghcb, reason);
 
+     svm_unmap_ghcb(svm, &map);
+
      /* Resume the guest to "return" the error code. */
      return 1;
 }
#145 lore raw

Re: [PATCH RFC v7 44/64] KVM: SVM: Remove the long-lived GHCB host map

Quoted 75 lines · Alper Gun
Quoted 64 lines · Jeremi Piotrowski
Quoted 62 lines · Michael Roth

From: Brijesh Singh <[email protected]>

On VMGEXIT, sev_handle_vmgexit() creates a host mapping for the GHCB GPA, and unmaps it just before VM-entry. This long-lived GHCB map is used by the VMGEXIT handler through accessors such as ghcb_{set_get}_xxx().

A long-lived GHCB map can cause issue when SEV-SNP is enabled. When SEV-SNP is enabled the mapped GPA needs to be protected against a page state change.

To eliminate the long-lived GHCB mapping, update the GHCB sync operations to explicitly map the GHCB before access and unmap it after access is complete. This requires that the setting of the GHCBs sw_exit_info_{1,2} fields be done during sev_es_sync_to_ghcb(), so create two new fields in the vcpu_svm struct to hold these values when required to be set outside of the GHCB mapping.

[mdr: defer per_cpu() assignment and order it with barrier() to fix case
where kvm_vcpu_map() causes reschedule on different CPU]

---
 arch/x86/kvm/svm/sev.c | 131 ++++++++++++++++++++++++++---------------
 arch/x86/kvm/svm/svm.c |  18 +++---
 arch/x86/kvm/svm/svm.h |  24 +++++++-
 3 files changed, 116 insertions(+), 57 deletions(-)
arch/x86/kvm/svm/sev.c +22 -1
index d5c6e48055fb..6ac0cb6e3484 100644
@@ -2921,15 +2921,40 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
      kvfree(svm->sev_es.ghcb_sa);
 }
 
+static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+     struct vmcb_control_area *control = &svm->vmcb->control;
+     u64 gfn = gpa_to_gfn(control->ghcb_gpa);
+
+     if (kvm_vcpu_map(&svm->vcpu, gfn, map)) {
+             /* Unable to map GHCB from guest */
+             pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn);
+             return -EFAULT;
+     }
+
+     return 0;
+}
+
+static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
+{
+     kvm_vcpu_unmap(&svm->vcpu, map, true);
+}
+
 static void dump_ghcb(struct vcpu_svm *svm)
 {
-     struct ghcb *ghcb = svm->sev_es.ghcb;
+     struct kvm_host_map map;
      unsigned int nbits;
+     struct ghcb *ghcb;
+
Quoted 8 lines

arch/x86/kvm/svm/sev.c void sev_free_vcpu(struct kvm_vcpu *vcpu) · line 2952

Quoted 4 lines
arch/x86/kvm/svm/sev.c +4 -0
+     if (svm_map_ghcb(svm, &map))
+             return;
+
+     ghcb = map.hva;

dump_ghcb() is called from sev_es_validate_vmgexit() with the ghcb already mapped. How about passing 'struct kvm_host_map *' (or struct ghcb *) as a param to avoid double mapping?

Quoted 4 lines

This also causes a soft lockup, PSC spin lock is already acquired in sev_es_validate_vmgexit. dump_ghcb will try to acquire the same lock again. So a guest can send an invalid ghcb page and cause a host soft lockup.

We did notice that issue with v6, and had a fix similar to what Jeremi suggested, but in this patchset the psc_lock spinlock has been replaced in favor of taking a read_lock() on kvm->mmu_lock.

The logic there is that userspace drives the page state changes via kvm_vm_ioctl_set_mem_attributes() now, and it does so while holding a write_lock() on kvm->mmu_lock, so if we want to guard the GHCB page from page state changes while the kernel is accessing it, it makes sense to use the same lock rather than relying on some separate SNP-specific lock.

And because it's now a read_lock(), I don't think the deadlock issue is present anymore in v7, but it probably does still make sense to avoid the double-mapping as Jeremi suggested, so we'll plan to make that change for v8.

Thanks,

Mike

#146 lore raw

RFC PATCH v7 45/64 KVM: SVM: Add support to handle GHCB GPA register VMGEXIT

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

SEV-SNP guests are required to perform a GHCB GPA registration. Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1.

On VMEXIT, Verify that GHCB GPA matches with the registered value. If a mismatch is detected then abort the guest.

---
 arch/x86/include/asm/sev-common.h |  8 ++++++++
 arch/x86/kvm/svm/sev.c            | 27 +++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.h            |  7 +++++++
 3 files changed, 42 insertions(+)
arch/x86/include/asm/sev-common.h +8 -0
index 539de6b93420..0a9055cdfae2 100644
@@ -59,6 +59,14 @@
 #define GHCB_MSR_AP_RESET_HOLD_RESULT_POS	12
 #define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK	GENMASK_ULL(51, 0)
 
+/* Preferred GHCB GPA Request */
+#define GHCB_MSR_PREF_GPA_REQ		0x010
+#define GHCB_MSR_GPA_VALUE_POS		12
+#define GHCB_MSR_GPA_VALUE_MASK		GENMASK_ULL(51, 0)
+
+#define GHCB_MSR_PREF_GPA_RESP		0x011
+#define GHCB_MSR_PREF_GPA_NONE		0xfffffffffffff
+
 /* GHCB GPA Register */
 #define GHCB_MSR_REG_GPA_REQ		0x012
 #define GHCB_MSR_REG_GPA_REQ_VAL(v)			\
arch/x86/kvm/svm/sev.c +27 -0
index 6ac0cb6e3484..d7b467b620aa 100644
@@ -3429,6 +3429,27 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 				  GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
 		break;
 	}
+	case GHCB_MSR_PREF_GPA_REQ: {
+		set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_NONE, GHCB_MSR_GPA_VALUE_MASK,
+				  GHCB_MSR_GPA_VALUE_POS);
+		set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_RESP, GHCB_MSR_INFO_MASK,
+				  GHCB_MSR_INFO_POS);
+		break;
+	}
+	case GHCB_MSR_REG_GPA_REQ: {
+		u64 gfn;
+
+		gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_VALUE_MASK,
+					GHCB_MSR_GPA_VALUE_POS);
+
+		svm->sev_es.ghcb_registered_gpa = gfn_to_gpa(gfn);
+
+		set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_VALUE_MASK,
+				  GHCB_MSR_GPA_VALUE_POS);
+		set_ghcb_msr_bits(svm, GHCB_MSR_REG_GPA_RESP, GHCB_MSR_INFO_MASK,
+				  GHCB_MSR_INFO_POS);
+		break;
+	}
 	case GHCB_MSR_TERM_REQ: {
 		u64 reason_set, reason_code;
 
@@ -3478,6 +3499,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 		return 1;
 	}
 
+	/* SEV-SNP guest requires that the GHCB GPA must be registered */
+	if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) {
+		vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa);
+		return -EINVAL;
+	}
+
 	ret = sev_es_validate_vmgexit(svm, &exit_code);
 	if (ret)
 		return ret;
arch/x86/kvm/svm/svm.h +7 -0
index c462dfac0a0d..a4d48c3e0f89 100644
@@ -218,6 +218,8 @@ struct vcpu_sev_es_state {
 	 */
 	u64 ghcb_sw_exit_info_1;
 	u64 ghcb_sw_exit_info_2;
+
+	u64 ghcb_registered_gpa;
 };
 
 struct vcpu_svm {
@@ -350,6 +352,11 @@ static inline bool sev_snp_guest(struct kvm *kvm)
 	return sev_es_guest(kvm) && sev->snp_active;
 }
 
+static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)
+{
+	return svm->sev_es.ghcb_registered_gpa == val;
+}
+
 static inline void vmcb_mark_all_dirty(struct vmcb *vmcb)
 {
 	vmcb->control.clean = 0;
Signature · 1 line
2.25.1
#147 lore raw

RFC PATCH v7 46/64 KVM: SVM: Add KVM_EXIT_VMGEXIT

In reply to Michael Roth

For private memslots, GHCB page state change requests will be forwarded to userspace for processing. Define a new KVM_EXIT_VMGEXIT for exits of this type.

---
 include/uapi/linux/kvm.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/uapi/linux/kvm.h +6 -0
index c468adc1f147..61b1e26ced01 100644
@@ -301,6 +301,7 @@ struct kvm_xen_exit {
 #define KVM_EXIT_RISCV_CSR        36
 #define KVM_EXIT_NOTIFY           37
 #define KVM_EXIT_MEMORY_FAULT     38
+#define KVM_EXIT_VMGEXIT          50
 
 /* For KVM_EXIT_INTERNAL_ERROR */
 /* Emulate instruction failed. */
@@ -549,6 +550,11 @@ struct kvm_run {
 			__u64 gpa;
 			__u64 size;
 		} memory;
+		/* KVM_EXIT_VMGEXIT */
+		struct {
+			__u64 ghcb_msr; /* GHCB MSR contents */
+			__u8 error; /* user -> kernel */
+		} vmgexit;
 		/* Fix the size of the union. */
 		char padding[256];
 	};
Signature · 1 line
2.25.1
#148 lore raw

RFC PATCH v7 47/64 KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification.

Forward these requests to userspace via KVM_EXIT_VMGEXIT so the VMM can issue the KVM ioctls to update the page state accordingly.

---
 arch/x86/include/asm/sev-common.h |  9 ++++++++
 arch/x86/kvm/svm/sev.c            | 25 +++++++++++++++++++++++
 arch/x86/kvm/trace.h              | 34 +++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c                |  1 +
 4 files changed, 69 insertions(+)
arch/x86/include/asm/sev-common.h +9 -0
index 0a9055cdfae2..ee38f7408470 100644
@@ -93,6 +93,10 @@ enum psc_op {
 };
 
 #define GHCB_MSR_PSC_REQ		0x014
+#define GHCB_MSR_PSC_GFN_POS		12
+#define GHCB_MSR_PSC_GFN_MASK		GENMASK_ULL(39, 0)
+#define GHCB_MSR_PSC_OP_POS		52
+#define GHCB_MSR_PSC_OP_MASK		0xf
 #define GHCB_MSR_PSC_REQ_GFN(gfn, op)			\
 	/* GHCBData[55:52] */				\
 	(((u64)((op) & 0xf) << 52) |			\
@@ -102,6 +106,11 @@ enum psc_op {
 	GHCB_MSR_PSC_REQ)
 
 #define GHCB_MSR_PSC_RESP		0x015
+#define GHCB_MSR_PSC_ERROR_POS		32
+#define GHCB_MSR_PSC_ERROR_MASK		GENMASK_ULL(31, 0)
+#define GHCB_MSR_PSC_ERROR		GENMASK_ULL(31, 0)
+#define GHCB_MSR_PSC_RSVD_POS		12
+#define GHCB_MSR_PSC_RSVD_MASK		GENMASK_ULL(19, 0)
 #define GHCB_MSR_PSC_RESP_VAL(val)			\
 	/* GHCBData[63:32] */				\
 	(((u64)(val) & GENMASK_ULL(63, 32)) >> 32)
arch/x86/kvm/svm/sev.c +25 -0
index d7b467b620aa..d7988629073b 100644
@@ -29,6 +29,7 @@
 #include "svm_ops.h"
 #include "cpuid.h"
 #include "trace.h"
+#include "mmu.h"
 
 #ifndef CONFIG_KVM_AMD_SEV
 /*
@@ -3350,6 +3351,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
 	svm->vmcb->control.ghcb_gpa = value;
 }
 
+/*
+ * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr
+ * and process that here accordingly.
+ */
+static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	set_ghcb_msr_bits(svm, 0,
+			  GHCB_MSR_PSC_ERROR_MASK, GHCB_MSR_PSC_ERROR_POS);
+
+	set_ghcb_msr_bits(svm, 0, GHCB_MSR_PSC_RSVD_MASK, GHCB_MSR_PSC_RSVD_POS);
+	set_ghcb_msr_bits(svm, GHCB_MSR_PSC_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
+
+	return 1; /* resume */
+}
+
 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3450,6 +3468,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 				  GHCB_MSR_INFO_POS);
 		break;
 	}
+	case GHCB_MSR_PSC_REQ:
+		vcpu->run->exit_reason = KVM_EXIT_VMGEXIT;
+		vcpu->run->vmgexit.ghcb_msr = control->ghcb_gpa;
+		vcpu->arch.complete_userspace_io = snp_complete_psc_msr_protocol;
+
+		ret = -1;
+		break;
 	case GHCB_MSR_TERM_REQ: {
 		u64 reason_set, reason_code;
 
arch/x86/kvm/trace.h +34 -0
index 83843379813e..65861d2d086c 100644
@@ -7,6 +7,7 @@
 #include <asm/svm.h>
 #include <asm/clocksource.h>
 #include <asm/pvclock-abi.h>
+#include <asm/sev-common.h>
 
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM kvm
@@ -1831,6 +1832,39 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit,
 		  __entry->vcpu_id, __entry->ghcb_gpa, __entry->result)
 );
 
+/*
+ * Tracepoint for the SEV-SNP page state change processing
+ */
+#define psc_operation					\
+	{SNP_PAGE_STATE_PRIVATE, "private"},		\
+	{SNP_PAGE_STATE_SHARED,  "shared"}		\
+
+TRACE_EVENT(kvm_snp_psc,
+	TP_PROTO(unsigned int vcpu_id, u64 pfn, u64 gpa, u8 op, int level),
+	TP_ARGS(vcpu_id, pfn, gpa, op, level),
+
+	TP_STRUCT__entry(
+		__field(int, vcpu_id)
+		__field(u64, pfn)
+		__field(u64, gpa)
+		__field(u8, op)
+		__field(int, level)
+	),
+
+	TP_fast_assign(
+		__entry->vcpu_id = vcpu_id;
+		__entry->pfn = pfn;
+		__entry->gpa = gpa;
+		__entry->op = op;
+		__entry->level = level;
+	),
+
+	TP_printk("vcpu %u, pfn %llx, gpa %llx, op %s, level %d",
+		  __entry->vcpu_id, __entry->pfn, __entry->gpa,
+		  __print_symbolic(__entry->op, psc_operation),
+		  __entry->level)
+);
+
 #endif /* _TRACE_KVM_H */
 
 #undef TRACE_INCLUDE_PATH
arch/x86/kvm/x86.c +1 -0
index 732f9cbbadb5..08dd1ef7e136 100644
@@ -13481,6 +13481,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_snp_psc);
 
 static int __init kvm_x86_init(void)
 {
Signature · 1 line
2.25.1
#149 lore raw

Re: [PATCH RFC v7 47/64] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT

Quoted 84 lines · Michael Roth

From: Brijesh Singh <[email protected]>

SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification.

Forward these requests to userspace via KVM_EXIT_VMGEXIT so the VMM can issue the KVM ioctls to update the page state accordingly.

---
 arch/x86/include/asm/sev-common.h |  9 ++++++++
 arch/x86/kvm/svm/sev.c            | 25 +++++++++++++++++++++++
 arch/x86/kvm/trace.h              | 34 +++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c                |  1 +
 4 files changed, 69 insertions(+)
arch/x86/include/asm/sev-common.h +9 -0
index 0a9055cdfae2..ee38f7408470 100644
@@ -93,6 +93,10 @@ enum psc_op {
 };
 
 #define GHCB_MSR_PSC_REQ		0x014
+#define GHCB_MSR_PSC_GFN_POS		12
+#define GHCB_MSR_PSC_GFN_MASK		GENMASK_ULL(39, 0)
+#define GHCB_MSR_PSC_OP_POS		52
+#define GHCB_MSR_PSC_OP_MASK		0xf
 #define GHCB_MSR_PSC_REQ_GFN(gfn, op)			\
 	/* GHCBData[55:52] */				\
 	(((u64)((op) & 0xf) << 52) |			\
@@ -102,6 +106,11 @@ enum psc_op {
 	GHCB_MSR_PSC_REQ)
 
 #define GHCB_MSR_PSC_RESP		0x015
+#define GHCB_MSR_PSC_ERROR_POS		32
+#define GHCB_MSR_PSC_ERROR_MASK		GENMASK_ULL(31, 0)
+#define GHCB_MSR_PSC_ERROR		GENMASK_ULL(31, 0)
+#define GHCB_MSR_PSC_RSVD_POS		12
+#define GHCB_MSR_PSC_RSVD_MASK		GENMASK_ULL(19, 0)
 #define GHCB_MSR_PSC_RESP_VAL(val)			\
 	/* GHCBData[63:32] */				\
 	(((u64)(val) & GENMASK_ULL(63, 32)) >> 32)
arch/x86/kvm/svm/sev.c +18 -0
index d7b467b620aa..d7988629073b 100644
@@ -29,6 +29,7 @@
 #include "svm_ops.h"
 #include "cpuid.h"
 #include "trace.h"
+#include "mmu.h"
 
 #ifndef CONFIG_KVM_AMD_SEV
 /*
@@ -3350,6 +3351,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
 	svm->vmcb->control.ghcb_gpa = value;
 }
 
+/*
+ * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr
+ * and process that here accordingly.
+ */
+static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	set_ghcb_msr_bits(svm, 0,
+			  GHCB_MSR_PSC_ERROR_MASK, GHCB_MSR_PSC_ERROR_POS);
+
+	set_ghcb_msr_bits(svm, 0, GHCB_MSR_PSC_RSVD_MASK, GHCB_MSR_PSC_RSVD_POS);
+	set_ghcb_msr_bits(svm, GHCB_MSR_PSC_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
+
+	return 1; /* resume */
+}
+
 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;

arch/x86/kvm/svm/sev.c static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) · line 3477

Quoted 10 lines
arch/x86/kvm/svm/sev.c +7 -0
@@ -3450,6 +3468,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 				  GHCB_MSR_INFO_POS);
 		break;
 	}
+	case GHCB_MSR_PSC_REQ:
+		vcpu->run->exit_reason = KVM_EXIT_VMGEXIT;
+		vcpu->run->vmgexit.ghcb_msr = control->ghcb_gpa;
+		vcpu->arch.complete_userspace_io = snp_complete_psc_msr_protocol;
+
+		ret = -1;
+		break;

What's the reasoning behind returning an error (-1) here? This error bubbles all the way up to the KVM_RUN ioctl. Would it be more appropriate to return 0? Returning 0 would cause a VM exit without indicating an error to userspace.

Quoted 68 lines
arch/x86/kvm/svm/sev.c +0 -0
 	case GHCB_MSR_TERM_REQ: {
 		u64 reason_set, reason_code;
 
arch/x86/kvm/trace.h +34 -0
index 83843379813e..65861d2d086c 100644
@@ -7,6 +7,7 @@
 #include <asm/svm.h>
 #include <asm/clocksource.h>
 #include <asm/pvclock-abi.h>
+#include <asm/sev-common.h>
 
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM kvm
@@ -1831,6 +1832,39 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit,
 		  __entry->vcpu_id, __entry->ghcb_gpa, __entry->result)
 );
 
+/*
+ * Tracepoint for the SEV-SNP page state change processing
+ */
+#define psc_operation					\
+	{SNP_PAGE_STATE_PRIVATE, "private"},		\
+	{SNP_PAGE_STATE_SHARED,  "shared"}		\
+
+TRACE_EVENT(kvm_snp_psc,
+	TP_PROTO(unsigned int vcpu_id, u64 pfn, u64 gpa, u8 op, int level),
+	TP_ARGS(vcpu_id, pfn, gpa, op, level),
+
+	TP_STRUCT__entry(
+		__field(int, vcpu_id)
+		__field(u64, pfn)
+		__field(u64, gpa)
+		__field(u8, op)
+		__field(int, level)
+	),
+
+	TP_fast_assign(
+		__entry->vcpu_id = vcpu_id;
+		__entry->pfn = pfn;
+		__entry->gpa = gpa;
+		__entry->op = op;
+		__entry->level = level;
+	),
+
+	TP_printk("vcpu %u, pfn %llx, gpa %llx, op %s, level %d",
+		  __entry->vcpu_id, __entry->pfn, __entry->gpa,
+		  __print_symbolic(__entry->op, psc_operation),
+		  __entry->level)
+);
+
 #endif /* _TRACE_KVM_H */
 
 #undef TRACE_INCLUDE_PATH
arch/x86/kvm/x86.c +1 -1
index 732f9cbbadb5..08dd1ef7e136 100644
@@ -13481,6 +13481,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_snp_psc);
 
 static int __init kvm_x86_init(void)
 {
--
Quoted 2 lines

2.25.1

Regards, Tom

#150 lore raw

RFC PATCH v7 48/64 KVM: SVM: Add support to handle Page State Change VMGEXIT

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification version 2.

Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page.

---
 arch/x86/include/asm/sev-common.h |  7 +++++++
 arch/x86/kvm/svm/sev.c            | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+)
arch/x86/include/asm/sev-common.h +7 -0
index ee38f7408470..1b111cde8c82 100644
@@ -130,6 +130,13 @@ enum psc_op {
 /* SNP Page State Change NAE event */
 #define VMGEXIT_PSC_MAX_ENTRY		253
 
+/* The page state change hdr structure in not valid */
+#define PSC_INVALID_HDR			1
+/* The hdr.cur_entry or hdr.end_entry is not valid */
+#define PSC_INVALID_ENTRY		2
+/* Page state change encountered undefined error */
+#define PSC_UNDEF_ERR			3
+
 struct psc_hdr {
 	u16 cur_entry;
 	u16 end_entry;
arch/x86/kvm/svm/sev.c +18 -0
index d7988629073b..abe6444bf5d4 100644
@@ -3164,6 +3164,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
 	case SVM_VMGEXIT_AP_JUMP_TABLE:
 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
 	case SVM_VMGEXIT_HV_FEATURES:
+	case SVM_VMGEXIT_PSC:
 		break;
 	default:
 		reason = GHCB_ERR_INVALID_EVENT;
@@ -3368,6 +3369,17 @@ static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu)
 	return 1; /* resume */
 }
 
+/*
+ * TODO: need to process the GHCB contents and report the proper error code
+ * instead of assuming success.
+ */
+static int snp_complete_psc(struct kvm_vcpu *vcpu)
+{
+	svm_set_ghcb_sw_exit_info_2(vcpu, 0);
+
+	return 1;
+}
+
 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3595,6 +3607,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 		ret = 1;
 		break;
 	}
+	case SVM_VMGEXIT_PSC:
+		/* Let userspace handling allocating/deallocating backing pages. */
+		vcpu->run->exit_reason = KVM_EXIT_VMGEXIT;
+		vcpu->run->vmgexit.ghcb_msr = ghcb_gpa;
+		vcpu->arch.complete_userspace_io = snp_complete_psc;
+		break;
 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
 		vcpu_unimpl(vcpu,
 			    "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
Signature · 1 line
2.25.1
#151 lore raw

RFC PATCH v7 49/64 KVM: SVM: Introduce ops for the post gfn map and unmap

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

When SEV-SNP is enabled in the guest VM, the guest memory pages can either be a private or shared. A write from the hypervisor goes through the RMP checks. If the CPU sees that hypervisor is attempting to write to a guest private page, then it will trigger an RMP violation #PF.

To avoid the RMP violation with GHCB pages, added new post_{map,unmap}_gfn functions to verify if its safe to map GHCB pages. Use kvm->mmu_lock to guard the GHCB against invalidations while being accessed.

Need to add generic post_{map,unmap}_gfn() ops that can be used to verify that it's safe to map a given guest page in the hypervisor.

Signed-off by: Ashish Kalra <[email protected]>
[mdr: use kvm->mmu_lock instead of a new spinlock, this should guard
GHCB page against invalidations]

---
 arch/x86/kvm/svm/sev.c | 40 ++++++++++++++++++++++++++++++++++++++--
 arch/x86/kvm/svm/svm.h |  3 +++
 2 files changed, 41 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +38 -2
index abe6444bf5d4..90b509fe1826 100644
@@ -2926,19 +2926,28 @@ static inline int svm_map_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
 	u64 gfn = gpa_to_gfn(control->ghcb_gpa);
+	struct kvm_vcpu *vcpu = &svm->vcpu;
 
-	if (kvm_vcpu_map(&svm->vcpu, gfn, map)) {
+	if (kvm_vcpu_map(vcpu, gfn, map)) {
 		/* Unable to map GHCB from guest */
 		pr_err("error mapping GHCB GFN [%#llx] from guest\n", gfn);
 		return -EFAULT;
 	}
 
+	if (sev_post_map_gfn(vcpu->kvm, map->gfn, map->pfn)) {
+		kvm_vcpu_unmap(vcpu, map, false);
+		return -EBUSY;
+	}
+
 	return 0;
 }
 
 static inline void svm_unmap_ghcb(struct vcpu_svm *svm, struct kvm_host_map *map)
 {
-	kvm_vcpu_unmap(&svm->vcpu, map, true);
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+
+	kvm_vcpu_unmap(vcpu, map, true);
+	sev_post_unmap_gfn(vcpu->kvm, map->gfn, map->pfn);
 }
 
 static void dump_ghcb(struct vcpu_svm *svm)
@@ -3875,6 +3884,33 @@ void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level)
 		 __func__, gfn, *level, rmp_level, ret);
 }
 
+int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn)
+{
+	int level;
+
+	if (!sev_snp_guest(kvm))
+		return 0;
+
+	read_lock(&(kvm)->mmu_lock);
+
+	/* If pfn is not added as private then fail */
+	if (snp_lookup_rmpentry(pfn, &level) == 1) {
+		read_unlock(&(kvm)->mmu_lock);
+		pr_err_ratelimited("failed to map private gfn 0x%llx pfn 0x%llx\n", gfn, pfn);
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
+void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn)
+{
+	if (!sev_snp_guest(kvm))
+		return;
+
+	read_unlock(&(kvm)->mmu_lock);
+}
+
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault)
 {
 	gfn_t gfn = gpa_to_gfn(gpa);
arch/x86/kvm/svm/svm.h +3 -0
index a4d48c3e0f89..aef13c120f2d 100644
@@ -100,6 +100,7 @@ struct kvm_sev_info {
 	atomic_t migration_in_progress;
 	u64 snp_init_flags;
 	void *snp_context;      /* SNP guest context page */
+	spinlock_t psc_lock;
 };
 
 struct kvm_svm {
@@ -727,6 +728,8 @@ void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);
 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
 struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);
 void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level);
+int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
+void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
Signature · 1 line
2.25.1
#152 lore raw

Re: [PATCH RFC v7 49/64] KVM: SVM: Introduce ops for the post gfn map and unmap

arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 103

Quoted 9 lines · Michael Roth
arch/x86/kvm/svm/svm.h +1 -0
index a4d48c3e0f89..aef13c120f2d 100644
@@ -100,6 +100,7 @@ struct kvm_sev_info {
        atomic_t migration_in_progress;
        u64 snp_init_flags;
        void *snp_context;      /* SNP guest context page */
+       spinlock_t psc_lock;

Looks like a leftover from v6 series.

--
Sabin.

Amazon Development Center (Romania) S.R.L. registered office: 27A Sf. Lazar Street, UBC5, floor 2, Iasi, Iasi County, 700045, Romania. Registered in Romania. Registration number J22/2621/2005.

#153 lore raw

RFC PATCH v7 50/64 KVM: x86: Export the kvm_zap_gfn_range() for the SNP use

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

While resolving the RMP page fault, there may be cases where the page level between the RMP entry and TDP does not match and the 2M RMP entry must be split into 4K RMP entries. Or a 2M TDP page need to be broken into multiple of 4K pages.

To keep the RMP and TDP page level in sync, zap the gfn range after splitting the pages in the RMP entry. The zap should force the TDP to gets rebuilt with the new page level.

---
 arch/x86/include/asm/kvm_host.h | 2 ++
 arch/x86/kvm/mmu.h              | 2 --
 arch/x86/kvm/mmu/mmu.c          | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index f4bb0821757e..15b9c678b281 100644
@@ -1838,6 +1838,8 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
 void kvm_mmu_zap_all(struct kvm *kvm);
 void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen);
 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages);
+void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);
+
 
 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
 
arch/x86/kvm/mmu.h +0 -2
index 6bdaacb6faa0..c94b620bf94b 100644
@@ -211,8 +211,6 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 	return -(u32)fault & errcode;
 }
 
-void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);
-
 int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu);
 
 int kvm_mmu_post_init_vm(struct kvm *kvm);
arch/x86/kvm/mmu/mmu.c +1 -0
index 25db83021500..02c7fb83a669 100644
@@ -6533,6 +6533,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
 
 	return need_tlb_flush;
 }
+EXPORT_SYMBOL_GPL(kvm_zap_gfn_range);
 
 static void kvm_rmap_zap_collapsible_sptes(struct kvm *kvm,
 					   const struct kvm_memory_slot *slot)
Signature · 1 line
2.25.1
#154 lore raw

RFC PATCH v7 51/64 KVM: SVM: Add support to handle the RMP nested page fault

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

When SEV-SNP is enabled in the guest, the hardware places restrictions on all memory accesses based on the contents of the RMP table. When hardware encounters RMP check failure caused by the guest memory access it raises the #NPF. The error code contains additional information on the access type. See the APM volume 2 for additional information.

Page state changes are handled by userspace, so if an RMP fault is triggered as a result of an RMP NPT fault, exit to userspace just like with explicit page-state change requests.

RMP NPT faults can also occur if the guest pvalidates a 2M page as 4K, in which case the RMP entries need to be PSMASH'd. Handle this case immediately in the kernel.

---
 arch/x86/kvm/svm/sev.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c | 21 +++++++++---
 arch/x86/kvm/svm/svm.h |  1 +
 3 files changed, 96 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +78 -0
index 90b509fe1826..5f2b2092cdae 100644
@@ -3361,6 +3361,13 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
 	svm->vmcb->control.ghcb_gpa = value;
 }
 
+static int snp_rmptable_psmash(struct kvm *kvm, kvm_pfn_t pfn)
+{
+	pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1);
+
+	return psmash(pfn);
+}
+
 /*
  * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr
  * and process that here accordingly.
@@ -3911,6 +3918,77 @@ void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn)
 	read_unlock(&(kvm)->mmu_lock);
 }
 
+void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code)
+{
+	int order, rmp_level, assigned, ret;
+	struct kvm_memory_slot *slot;
+	struct kvm *kvm = vcpu->kvm;
+	kvm_pfn_t pfn;
+	gfn_t gfn;
+
+	/*
+	 * Private memslots punt handling of implicit page state changes to
+	 * userspace, so the only RMP faults expected here for
+	 * PFERR_GUEST_SIZEM_MASK. Anything else suggests that the RMP table has
+	 * gotten out of sync with the private memslot.
+	 *
+	 * TODO: However, this case has also been noticed when an access occurs
+	 * to an NPT mapping that has just been split/PSMASHED, in which case
+	 * PFERR_GUEST_SIZEM_MASK might not be set. In those cases it should be
+	 * safe to ignore and let the guest retry, but log these just in case
+	 * for now.
+	 */
+	if (!(error_code & PFERR_GUEST_SIZEM_MASK))
+		pr_warn("Unexpected RMP fault for GPA 0x%llx, error_code 0x%llx",
+			gpa, error_code);
+
+	gfn = gpa >> PAGE_SHIFT;
+
+	/*
+	 * Only RMPADJUST/PVALIDATE should cause PFERR_GUEST_SIZEM.
+	 *
+	 * For PVALIDATE, this should only happen if a guest PVALIDATEs a 4K GFN
+	 * that is backed by a huge page in the host whose RMP entry has the
+	 * hugepage/assigned bits set. With UPM, that should only ever happen
+	 * for private pages.
+	 *
+	 * For RMPADJUST, this assumption might not hold, in which case handling
+	 * for obtaining the PFN from HVA-backed memory may be needed. For now,
+	 * just print warnings.
+	 */
+	if (!kvm_mem_is_private(kvm, gfn)) {
+		pr_warn("Unexpected RMP fault, size-mismatch for non-private GPA 0x%llx", gpa);
+		return;
+	}
+
+	slot = gfn_to_memslot(kvm, gfn);
+	if (!kvm_slot_can_be_private(slot)) {
+		pr_warn("Unexpected RMP fault, non-private slot for GPA 0x%llx", gpa);
+		return;
+	}
+
+	ret = kvm_restricted_mem_get_pfn(slot, gfn, &pfn, &order);
+	if (ret) {
+		pr_warn("Unexpected RMP fault, no private backing page for GPA 0x%llx", gpa);
+		return;
+	}
+
+	assigned = snp_lookup_rmpentry(pfn, &rmp_level);
+	if (assigned != 1) {
+		pr_warn("Unexpected RMP fault, no assigned RMP entry for GPA 0x%llx", gpa);
+		goto out;
+	}
+
+	ret = snp_rmptable_psmash(kvm, pfn);
+	if (ret)
+		pr_err_ratelimited("Unable to split RMP entries for GPA 0x%llx PFN 0x%llx ret %d\n",
+				   gpa, pfn, ret);
+
+out:
+	kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD);
+	put_page(pfn_to_page(pfn));
+}
+
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault)
 {
 	gfn_t gfn = gpa_to_gfn(gpa);
arch/x86/kvm/svm/svm.c +17 -4
index 1826946a2f43..43f04fc95a0a 100644
@@ -1968,15 +1968,28 @@ static int pf_interception(struct kvm_vcpu *vcpu)
 static int npf_interception(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
+	int rc;
 
 	u64 fault_address = svm->vmcb->control.exit_info_2;
 	u64 error_code = svm->vmcb->control.exit_info_1;
 
 	trace_kvm_page_fault(vcpu, fault_address, error_code);
-	return kvm_mmu_page_fault(vcpu, fault_address, error_code,
-			static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
-			svm->vmcb->control.insn_bytes : NULL,
-			svm->vmcb->control.insn_len);
+	rc = kvm_mmu_page_fault(vcpu, fault_address, error_code,
+				static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
+				svm->vmcb->control.insn_bytes : NULL,
+				svm->vmcb->control.insn_len);
+
+	/*
+	 * rc == 0 indicates a userspace exit is needed to handle page
+	 * transitions, so do that first before updating the RMP table.
+	 */
+	if (error_code & PFERR_GUEST_RMP_MASK) {
+		if (rc == 0)
+			return rc;
+		handle_rmp_page_fault(vcpu, fault_address, error_code);
+	}
+
+	return rc;
 }
 
 static int db_interception(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +1 -0
index aef13c120f2d..12b9f4d539fb 100644
@@ -730,6 +730,7 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu);
 void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level);
 int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
 void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
+void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
Signature · 1 line
2.25.1
#155 lore raw

RFC PATCH v7 52/64 KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
 arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++--
 arch/x86/kvm/svm/svm.h |   2 +
 2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +179 -6
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
 		if (ret)
 			goto e_free;
 
+		mutex_init(&sev->guest_req_lock);
 		ret = sev_snp_init(&argp->error, false);
 	} else {
 		ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
  */
 static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
 {
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
 	struct sev_data_snp_addr data = {};
-	void *context;
+	void *context, *certs_data;
 	int rc;
 
+	/* Allocate memory used for the certs data in SNP guest request */
+	certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+	if (!certs_data)
+		return NULL;
+
 	/* Allocate memory for context page */
 	context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
 	if (!context)
-		return NULL;
+		goto e_free;
 
 	data.gctx_paddr = __psp_pa(context);
 	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
-	if (rc) {
-		snp_free_firmware_page(context);
-		return NULL;
-	}
+	if (rc)
+		goto e_free;
+
+	sev->snp_certs_data = certs_data;
 
 	return context;
+
+e_free:
+	snp_free_firmware_page(context);
+	kfree(certs_data);
+	return NULL;
 }
 
 static int snp_bind_asid(struct kvm *kvm, int *error)
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct kvm *kvm)
 	snp_free_firmware_page(sev->snp_context);
 	sev->snp_context = NULL;
 
+	kfree(sev->snp_certs_data);
+
 	return 0;
 }
 
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
 	case SVM_VMGEXIT_HV_FEATURES:
 	case SVM_VMGEXIT_PSC:
+	case SVM_VMGEXIT_GUEST_REQUEST:
+	case SVM_VMGEXIT_EXT_GUEST_REQUEST:
 		break;
 	default:
 		reason = GHCB_ERR_INVALID_EVENT;
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+					 struct sev_data_snp_guest_request *data,
+					 gpa_t req_gpa, gpa_t resp_gpa)
+{
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	kvm_pfn_t req_pfn, resp_pfn;
+	struct kvm_sev_info *sev;
+
+	sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, PAGE_SIZE))
+		return SEV_RET_INVALID_PARAM;
+
+	req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+	if (is_error_noslot_pfn(req_pfn))
+		return SEV_RET_INVALID_ADDRESS;
+
+	resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+	if (is_error_noslot_pfn(resp_pfn))
+		return SEV_RET_INVALID_ADDRESS;
+
+	if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+		return SEV_RET_INVALID_ADDRESS;
+
+	data->gctx_paddr = __psp_pa(sev->snp_context);
+	data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+	data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+	return 0;
+}
+
+static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data, unsigned long *rc)
+{
+	u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+	int ret;
+
+	ret = snp_page_reclaim(pfn);
+	if (ret)
+		*rc = SEV_RET_INVALID_ADDRESS;
+
+	ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+	if (ret)
+		*rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{
+	struct sev_data_snp_guest_request data = {0};
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	struct kvm_sev_info *sev;
+	unsigned long rc;
+	int err;
+
+	if (!sev_snp_guest(vcpu->kvm)) {
+		rc = SEV_RET_INVALID_GUEST;
+		goto e_fail;
+	}
+
+	sev = &to_kvm_svm(kvm)->sev_info;
+
+	mutex_lock(&sev->guest_req_lock);
+
+	rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+	if (rc)
+		goto unlock;
+
+	rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);
+	if (rc)
+		/* use the firmware error code */
+		rc = err;
+
+	snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+	mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+	svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{
+	struct sev_data_snp_guest_request req = {0};
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	unsigned long data_npages;
+	struct kvm_sev_info *sev;
+	unsigned long rc, err;
+	u64 data_gpa;
+
+	if (!sev_snp_guest(vcpu->kvm)) {
+		rc = SEV_RET_INVALID_GUEST;
+		goto e_fail;
+	}
+
+	sev = &to_kvm_svm(kvm)->sev_info;
+
+	data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+	data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+	if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+		rc = SEV_RET_INVALID_ADDRESS;
+		goto e_fail;
+	}
+
+	mutex_lock(&sev->guest_req_lock);
+
+	rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+	if (rc)
+		goto unlock;
+
+	rc = snp_guest_ext_guest_request(&req, (unsigned long)sev->snp_certs_data,
+					 &data_npages, &err);
+	if (rc) {
+		/*
+		 * If buffer length is small then return the expected
+		 * length in rbx.
+		 */
+		if (err == SNP_GUEST_REQ_INVALID_LEN)
+			vcpu->arch.regs[VCPU_REGS_RBX] = data_npages;
+
+		/* pass the firmware error code */
+		rc = err;
+		goto cleanup;
+	}
+
+	/* Copy the certificate blob in the guest memory */
+	if (data_npages &&
+	    kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT))
+		rc = SEV_RET_INVALID_ADDRESS;
+
+cleanup:
+	snp_cleanup_guest_buf(&req, &rc);
+
+unlock:
+	mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+	svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
 static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 {
 	struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3629,6 +3788,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
 		vcpu->run->vmgexit.ghcb_msr = ghcb_gpa;
 		vcpu->arch.complete_userspace_io = snp_complete_psc;
 		break;
+	case SVM_VMGEXIT_GUEST_REQUEST: {
+		snp_handle_guest_request(svm, control->exit_info_1, control->exit_info_2);
+
+		ret = 1;
+		break;
+	}
+	case SVM_VMGEXIT_EXT_GUEST_REQUEST: {
+		snp_handle_ext_guest_request(svm,
+					     control->exit_info_1,
+					     control->exit_info_2);
+
+		ret = 1;
+		break;
+	}
 	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
 		vcpu_unimpl(vcpu,
 			    "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
arch/x86/kvm/svm/svm.h +2 -0
index 12b9f4d539fb..7c0f9d00950f 100644
@@ -101,6 +101,8 @@ struct kvm_sev_info {
 	u64 snp_init_flags;
 	void *snp_context;      /* SNP guest context page */
 	spinlock_t psc_lock;
+	void *snp_certs_data;
+	struct mutex guest_req_lock;
 };
 
 struct kvm_svm {
Signature · 1 line
2.25.1
#156 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 155 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +
  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +81 -6
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
  		if (ret)
  			goto e_free;
 
+		mutex_init(&sev->guest_req_lock);
  		ret = sev_snp_init(&argp->error, false);
  	} else {
  		ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
   */
  static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
  {
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
  	struct sev_data_snp_addr data = {};
-	void *context;
+	void *context, *certs_data;
  	int rc;
 
+	/* Allocate memory used for the certs data in SNP guest request */
+	certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+	if (!certs_data)
+		return NULL;
+
  	/* Allocate memory for context page */
  	context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
  	if (!context)
-		return NULL;
+		goto e_free;
 
  	data.gctx_paddr = __psp_pa(context);
  	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
-	if (rc) {
-		snp_free_firmware_page(context);
-		return NULL;
-	}
+	if (rc)
+		goto e_free;
+
+	sev->snp_certs_data = certs_data;
 
  	return context;
+
+e_free:
+	snp_free_firmware_page(context);
+	kfree(certs_data);
+	return NULL;
  }
 
  static int snp_bind_asid(struct kvm *kvm, int *error)
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct kvm *kvm)
  	snp_free_firmware_page(sev->snp_context);
  	sev->snp_context = NULL;
 
+	kfree(sev->snp_certs_data);
+
  	return 0;
  }
 
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
  	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
  	case SVM_VMGEXIT_HV_FEATURES:
  	case SVM_VMGEXIT_PSC:
+	case SVM_VMGEXIT_GUEST_REQUEST:
+	case SVM_VMGEXIT_EXT_GUEST_REQUEST:
  		break;
  	default:
  		reason = GHCB_ERR_INVALID_EVENT;
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct kvm_vcpu *vcpu)
  	return 1;
  }
 
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+					 struct sev_data_snp_guest_request *data,
+					 gpa_t req_gpa, gpa_t resp_gpa)
+{
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	kvm_pfn_t req_pfn, resp_pfn;
+	struct kvm_sev_info *sev;
+
+	sev = &to_kvm_svm(kvm)->sev_info;
+
+	if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, PAGE_SIZE))
+		return SEV_RET_INVALID_PARAM;
+
+	req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+	if (is_error_noslot_pfn(req_pfn))
+		return SEV_RET_INVALID_ADDRESS;
+
+	resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+	if (is_error_noslot_pfn(resp_pfn))
+		return SEV_RET_INVALID_ADDRESS;
+
+	if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+		return SEV_RET_INVALID_ADDRESS;
+
+	data->gctx_paddr = __psp_pa(sev->snp_context);
+	data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+	data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+	return 0;
+}
+
+static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data, unsigned long *rc)
+{
+	u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+	int ret;
+
+	ret = snp_page_reclaim(pfn);
+	if (ret)
+		*rc = SEV_RET_INVALID_ADDRESS;
+
+	ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+	if (ret)
+		*rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{
+	struct sev_data_snp_guest_request data = {0};
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	struct kvm_sev_info *sev;
+	unsigned long rc;
+	int err;
+
+	if (!sev_snp_guest(vcpu->kvm)) {
+		rc = SEV_RET_INVALID_GUEST;
+		goto e_fail;
+	}

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3483

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+	sev = &to_kvm_svm(kvm)->sev_info;
+
+	mutex_lock(&sev->guest_req_lock);
+
+	rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+	if (rc)
+		goto unlock;
+
+	rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 36 lines
arch/x86/kvm/svm/sev.c +36 -0
+	if (rc)
+		/* use the firmware error code */
+		rc = err;
+
+	snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+	mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+	svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{
+	struct sev_data_snp_guest_request req = {0};
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	unsigned long data_npages;
+	struct kvm_sev_info *sev;
+	unsigned long rc, err;
+	u64 data_gpa;
+
+	if (!sev_snp_guest(vcpu->kvm)) {
+		rc = SEV_RET_INVALID_GUEST;
+		goto e_fail;
+	}
+
+	sev = &to_kvm_svm(kvm)->sev_info;
+
+	data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+	data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+	if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+		rc = SEV_RET_INVALID_ADDRESS;
+		goto e_fail;

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3529

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+	}
+
+	mutex_lock(&sev->guest_req_lock);
+
+	rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+	if (rc)
+		goto unlock;
+
+	rc = snp_guest_ext_guest_request(&req, (unsigned long)sev->snp_certs_data,
+					 &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

Besides, is sev->fd really needed in the sev struct at all? Thanks,

Quoted 65 lines
arch/x86/kvm/svm/sev.c +42 -0
+	if (rc) {
+		/*
+		 * If buffer length is small then return the expected
+		 * length in rbx.
+		 */
+		if (err == SNP_GUEST_REQ_INVALID_LEN)
+			vcpu->arch.regs[VCPU_REGS_RBX] = data_npages;
+
+		/* pass the firmware error code */
+		rc = err;
+		goto cleanup;
+	}
+
+	/* Copy the certificate blob in the guest memory */
+	if (data_npages &&
+	    kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT))
+		rc = SEV_RET_INVALID_ADDRESS;
+
+cleanup:
+	snp_cleanup_guest_buf(&req, &rc);
+
+unlock:
+	mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+	svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
  static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
  {
  	struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3629,6 +3788,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
  		vcpu->run->vmgexit.ghcb_msr = ghcb_gpa;
  		vcpu->arch.complete_userspace_io = snp_complete_psc;
  		break;
+	case SVM_VMGEXIT_GUEST_REQUEST: {
+		snp_handle_guest_request(svm, control->exit_info_1, control->exit_info_2);
+
+		ret = 1;
+		break;
+	}
+	case SVM_VMGEXIT_EXT_GUEST_REQUEST: {
+		snp_handle_ext_guest_request(svm,
+					     control->exit_info_1,
+					     control->exit_info_2);
+
+		ret = 1;
+		break;
+	}
  	case SVM_VMGEXIT_UNSUPPORTED_EVENT:
  		vcpu_unimpl(vcpu,
  			    "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
arch/x86/kvm/svm/svm.h +2 -0
index 12b9f4d539fb..7c0f9d00950f 100644
@@ -101,6 +101,8 @@ struct kvm_sev_info {
  	u64 snp_init_flags;
  	void *snp_context;      /* SNP guest context page */
  	spinlock_t psc_lock;
+	void *snp_certs_data;
+	struct mutex guest_req_lock;
  };
 
  struct kvm_svm {
Signature · 1 line
Alexey
maintainer
#157 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 221 lines · Alexey Kardashevskiy
Quoted 155 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +20 -6
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, struct 
 kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct kvm 
 *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
       /* Allocate memory for context page */
       context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
       if (!context)
-        return NULL;
+        goto e_free;
       data.gctx_paddr = __psp_pa(context);
       rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
-    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
       return context;
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;
   }
   static int snp_bind_asid(struct kvm *kvm, int *error)
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct kvm 
 *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +59 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct kvm_vcpu 
 *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request 
 *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu · line 3486

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines
arch/x86/kvm/svm/sev.c +14 -0
+    if (rc)
+        /* use the firmware error code */
+        rc = err;
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t 

req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Thanks,
Ashish

Quoted 71 lines

Besides, is sev->fd really needed in the sev struct at all? Thanks,

Quoted 66 lines

+    if (rc) {
+        /*
+         * If buffer length is small then return the expected +         * length in rbx.
+         */
+        if (err == SNP_GUEST_REQ_INVALID_LEN)
+            vcpu->arch.regs[VCPU_REGS_RBX] = data_npages;

arch/x86/kvm/svm/sev.c +9 -0
+
+        /* pass the firmware error code */
+        rc = err;
+        goto cleanup;
+    }
+
+    /* Copy the certificate blob in the guest memory */
+    if (data_npages &&
+        kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, 

data_npages << PAGE_SHIFT))
+        rc = SEV_RET_INVALID_ADDRESS;

arch/x86/kvm/svm/sev.c +11 -0
+
+cleanup:
+    snp_cleanup_guest_buf(&req, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+

  static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) {
      struct vmcb_control_area *control = &svm->vmcb->control;

arch/x86/kvm/svm/sev.c +14 -0
@@ -3629,6 +3788,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
           vcpu->run->vmgexit.ghcb_msr = ghcb_gpa;
           vcpu->arch.complete_userspace_io = snp_complete_psc;
           break;
+    case SVM_VMGEXIT_GUEST_REQUEST: {
+        snp_handle_guest_request(svm, control->exit_info_1, 
 control->exit_info_2);
+
+        ret = 1;
+        break;
+    }
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST: {
+        snp_handle_ext_guest_request(svm,
+                         control->exit_info_1,
+                         control->exit_info_2);
+
+        ret = 1;
+        break;
+    }

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
          vcpu_unimpl(vcpu,
                  "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",

arch/x86/kvm/svm/svm.h +0 -0
index 12b9f4d539fb..7c0f9d00950f 100644
@@ -101,6 +101,8 @@ struct kvm_sev_info {
       u64 snp_init_flags;

      void *snp_context;      /* SNP guest context page */ spinlock_t psc_lock;
+    void *snp_certs_data;
+    struct mutex guest_req_lock;
  };
  struct kvm_svm {

#158 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 222 lines · Ashish
Quoted 221 lines · Alexey Kardashevskiy
Quoted 155 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +20 -6
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, struct 
 kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct kvm 
 *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
       /* Allocate memory for context page */
       context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
       if (!context)
-        return NULL;
+        goto e_free;
       data.gctx_paddr = __psp_pa(context);
       rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
-    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
       return context;
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;
   }
   static int snp_bind_asid(struct kvm *kvm, int *error)
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct kvm 
 *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +59 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct kvm_vcpu 
 *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request 
 *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu · line 3486

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines
arch/x86/kvm/svm/sev.c +14 -0
+    if (rc)
+        /* use the firmware error code */
+        rc = err;
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t 

req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
void *data, int *error)

{
	if (!filep || filep->f_op != &sev_fops)
		return -EBADF;

	return sev_do_cmd(cmd, data, error);
}

EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Quoted 74 lines

Thanks,
Ashish

Quoted 71 lines

Besides, is sev->fd really needed in the sev struct at all? Thanks,

Quoted 66 lines

+    if (rc) {
+        /*
+         * If buffer length is small then return the expected +         * length in rbx.
+         */
+        if (err == SNP_GUEST_REQ_INVALID_LEN)
+            vcpu->arch.regs[VCPU_REGS_RBX] = data_npages;

arch/x86/kvm/svm/sev.c +9 -0
+
+        /* pass the firmware error code */
+        rc = err;
+        goto cleanup;
+    }
+
+    /* Copy the certificate blob in the guest memory */
+    if (data_npages &&
+        kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, 

data_npages << PAGE_SHIFT))
+        rc = SEV_RET_INVALID_ADDRESS;

arch/x86/kvm/svm/sev.c +11 -0
+
+cleanup:
+    snp_cleanup_guest_buf(&req, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+

  static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) {
      struct vmcb_control_area *control = &svm->vmcb->control;

arch/x86/kvm/svm/sev.c +14 -0
@@ -3629,6 +3788,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
           vcpu->run->vmgexit.ghcb_msr = ghcb_gpa;
           vcpu->arch.complete_userspace_io = snp_complete_psc;
           break;
+    case SVM_VMGEXIT_GUEST_REQUEST: {
+        snp_handle_guest_request(svm, control->exit_info_1, 
 control->exit_info_2);
+
+        ret = 1;
+        break;
+    }
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST: {
+        snp_handle_ext_guest_request(svm,
+                         control->exit_info_1,
+                         control->exit_info_2);
+
+        ret = 1;
+        break;
+    }

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
          vcpu_unimpl(vcpu,
                  "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",

arch/x86/kvm/svm/svm.h +0 -0
index 12b9f4d539fb..7c0f9d00950f 100644
@@ -101,6 +101,8 @@ struct kvm_sev_info {
       u64 snp_init_flags;

      void *snp_context;      /* SNP guest context page */ spinlock_t psc_lock;
+    void *snp_certs_data;
+    struct mutex guest_req_lock;
  };
  struct kvm_svm {

Signature · 1 line
Alexey
maintainer
#159 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 247 lines · Alexey Kardashevskiy
Quoted 223 lines · Ashish
Quoted 222 lines · Alexey Kardashevskiy
Quoted 156 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +20 -6
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct kvm 
 *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
       /* Allocate memory for context page */
       context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
       if (!context)
-        return NULL;
+        goto e_free;
       data.gctx_paddr = __psp_pa(context);
       rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);
-    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
       return context;
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;
   }
   static int snp_bind_asid(struct kvm *kvm, int *error)
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct kvm 
 *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +59 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct kvm_vcpu 
 *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request 
 *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu · line 3486

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines
arch/x86/kvm/svm/sev.c +14 -0
+    if (rc)
+        /* use the firmware error code */
+        rc = err;
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
                void *data, int *error)
{
    if (!filep || filep->f_op != &sev_fops)
        return -EBADF;

Quoted 8 lines

    return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
struct device *dev = sev->dev;
int ret;

/*
* SEV feature support can be detected on multiple devices but
* the SEV FW commands must be issued on the master. During
* probe, we do not know the master hence we create /dev/sev on
* the first device probe.
* sev_do_cmd() finds the right master device to which to issue
* the command to the firmware.
*/
...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
void *data, int *error)

{
        if (!filep || filep->f_op != &sev_fops)
                return -EBADF;

..
..

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

Thanks,
Ashish

#160 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 266 lines · Ashish
Quoted 247 lines · Alexey Kardashevskiy
Quoted 224 lines · Ashish
Quoted 223 lines · Alexey Kardashevskiy
Quoted 157 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +4 -1
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct kvm 
 *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest 

request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
      /* Allocate memory for context page */
      context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
      if (!context)
-        return NULL;
+        goto e_free;
      data.gctx_paddr = __psp_pa(context);
      rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); -    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
      return context;

arch/x86/kvm/svm/sev.c +5 -0
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;

  }
  static int snp_bind_asid(struct kvm *kvm, int *error)

arch/x86/kvm/svm/sev.c +2 -0
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct 
 kvm *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +59 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct kvm_vcpu 
 *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct 
 sev_data_snp_guest_request *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu · line 3486

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines
arch/x86/kvm/svm/sev.c +14 -0
+    if (rc)
+        /* use the firmware error code */
+        rc = err;
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
     if (!filep || filep->f_op != &sev_fops)
         return -EBADF;

Quoted 8 lines

     return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
        struct device *dev = sev->dev;
        int ret;

Quoted 8 lines

        /*
         * SEV feature support can be detected on multiple devices but
         * the SEV FW commands must be issued on the master. During
         * probe, we do not know the master hence we create /dev/sev on
         * the first device probe.
         * sev_do_cmd() finds the right master device to which to issue
         * the command to the firmware.
     */

It is still a single /dev/sev node and the userspace cannot get it wrong, it does not have to choose between (for instance) /dev/sev0 and /dev/sev1 on a 2 SOC system.

Quoted 13 lines

...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
                                void *data, int *error)
{
        if (!filep || filep->f_op != &sev_fops)
                return -EBADF;
..
..

Quoted 2 lines

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

There is always just one fd which always uses psp_master, nothing from that fd is used.

More to the point, if sev->fd is still important, why is it ok to skip it for snp_handle_ext_guest_request()? Thanks,

Signature · 1 line
Alexey
maintainer
#161 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 293 lines · Alexey Kardashevskiy
Quoted 267 lines · Ashish
Quoted 248 lines · Alexey Kardashevskiy
Quoted 224 lines · Ashish
Quoted 223 lines · Alexey Kardashevskiy
Quoted 157 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +4 -1
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct 
 kvm *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest 

request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
      /* Allocate memory for context page */
      context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
      if (!context)
-        return NULL;
+        goto e_free;
      data.gctx_paddr = __psp_pa(context);
      rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); -    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
      return context;

arch/x86/kvm/svm/sev.c +5 -0
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;

  }
  static int snp_bind_asid(struct kvm *kvm, int *error)

arch/x86/kvm/svm/sev.c +2 -0
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct 
 kvm *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +59 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct 
 kvm_vcpu *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct 
 sev_data_snp_guest_request *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct · line 3486

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines
arch/x86/kvm/svm/sev.c +14 -0
+    if (rc)
+        /* use the firmware error code */
+        rc = err;
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
     if (!filep || filep->f_op != &sev_fops)
         return -EBADF;

Quoted 8 lines

     return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
         struct device *dev = sev->dev;
         int ret;

Quoted 8 lines

         /*
          * SEV feature support can be detected on multiple devices but
          * the SEV FW commands must be issued on the master. During
          * probe, we do not know the master hence we create /dev/sev on
          * the first device probe.
          * sev_do_cmd() finds the right master device to which to issue
          * the command to the firmware.
      */

It is still a single /dev/sev node and the userspace cannot get it wrong, it does not have to choose between (for instance) /dev/sev0 and /dev/sev1 on a 2 SOC system.

Quoted 13 lines

...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
         if (!filep || filep->f_op != &sev_fops)
                 return -EBADF;
..
..

Quoted 4 lines
Quoted 2 lines

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

There is always just one fd which always uses psp_master, nothing from that fd is used.

It also ensures that we can only issue commands (sev_issue_cmd) after SEV/SNP guest has launched. We don't have a valid fd to use before the guest launch. The file descriptor is passed as part of the guest launch flow, for example, in snp_launch_start().

Quoted 4 lines

More to the point, if sev->fd is still important, why is it ok to skip it for snp_handle_ext_guest_request()? Thanks,

Then, we should do the same for snp_handle_ext_guest_request().

Thanks,
Ashish

#162 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 294 lines · Ashish
Quoted 293 lines · Alexey Kardashevskiy
Quoted 267 lines · Ashish
Quoted 248 lines · Alexey Kardashevskiy
Quoted 225 lines · Ashish
Quoted 224 lines · Alexey Kardashevskiy
Quoted 167 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +4 -1
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct 
 kvm *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest 

request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
      /* Allocate memory for context page */
      context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
      if (!context)
-        return NULL;
+        goto e_free;
      data.gctx_paddr = __psp_pa(context);
      rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); -    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
      return context;

arch/x86/kvm/svm/sev.c +5 -0
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;

  }
  static int snp_bind_asid(struct kvm *kvm, int *error)

arch/x86/kvm/svm/sev.c +2 -0
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct 
 kvm *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +69 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct 
 kvm_vcpu *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct 
 sev_data_snp_guest_request *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, 
Quoted 1 line

&err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines

+    if (rc)
+        /* use the firmware error code */
+        rc = err;

arch/x86/kvm/svm/sev.c +11 -0
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
     if (!filep || filep->f_op != &sev_fops)
         return -EBADF;

Quoted 8 lines

     return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
         struct device *dev = sev->dev;
         int ret;

Quoted 8 lines

         /*
          * SEV feature support can be detected on multiple devices but
          * the SEV FW commands must be issued on the master. During
          * probe, we do not know the master hence we create /dev/sev on
          * the first device probe.
          * sev_do_cmd() finds the right master device to which to issue
          * the command to the firmware.
      */

It is still a single /dev/sev node and the userspace cannot get it wrong, it does not have to choose between (for instance) /dev/sev0 and /dev/sev1 on a 2 SOC system.

Quoted 13 lines

...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
         if (!filep || filep->f_op != &sev_fops)
                 return -EBADF;
..
..

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

There is always just one fd which always uses psp_master, nothing from that fd is used.

It also ensures that we can only issue commands (sev_issue_cmd) after SEV/SNP guest has launched.

I can open /dev/sev and start sending commands to the firmware with no KVM running at all. Oh well, we discussed this offline :)

Quoted 6 lines

We don't have a valid fd to use before the guest launch. The file descriptor is passed as part of the guest launch flow, for example, in snp_launch_start().

Quoted 4 lines

More to the point, if sev->fd is still important, why is it ok to skip it for snp_handle_ext_guest_request()? Thanks,

Then, we should do the same for snp_handle_ext_guest_request().

Okay.

This snp_handle_ext_guest_request() helper is for returning "Table 21. ATTESTATION_REPORT Structure" along with the certificate(s) used to sign the report: "This usage allows the attestation report and the certificates required to verify the report to be returned at the same time".

I can see:
1) KVM_SEV_SNP_{G,S}ET_CERTS ioctls on KVM VM and
2) SNP_{SET,GET}_EXT_CONFIG ioctls on /dev/sev
Both store the passed blob and neither communicate it to the firmware. This makes me wonder - how does the attestation report (cooked by the firmware) get signed with those certificates passed on by the HV userspace?

Also, the cached blob in /dev/sev seems redundand - the attestation report is retuned for a specific guest so having a blob in the KVM VM makes sense and KVM unconditionally reserves memory for it anyway. And for the HV itself the blob is useless (?) so why bother with caching it in /dev/sev.

And GET ioctls() return what SET passed on (not something the firware returned, for example), what is ever going to call SET? The userspace can as well cache what it passed and save a bit of the code/memory in the kernel.

btw SNP_{SET,GET}_EXT_CONFIG are documented in Documentation/virt/coco/sev-guest.rst but implemented in drivers/crypto/ccp/sev-dev.c (not sev-guest.c).

What do I miss in the big picture here? :) Thanks,

Signature · 1 line
Alexey
maintainer
#163 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 310 lines · Alexey Kardashevskiy
Quoted 293 lines · Ashish
Quoted 292 lines · Alexey Kardashevskiy
Quoted 266 lines · Ashish
Quoted 247 lines · Alexey Kardashevskiy
Quoted 224 lines · Ashish
Quoted 223 lines · Alexey Kardashevskiy
Quoted 157 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +4 -1
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct 
 kvm *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest 

request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE, GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
      /* Allocate memory for context page */
      context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
      if (!context)
-        return NULL;
+        goto e_free;
      data.gctx_paddr = __psp_pa(context);
      rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); -    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
      return context;

arch/x86/kvm/svm/sev.c +5 -0
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;

  }
  static int snp_bind_asid(struct kvm *kvm, int *error)

arch/x86/kvm/svm/sev.c +2 -0
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct 
 kvm *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +59 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct 
 kvm_vcpu *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, 
 PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct 
 sev_data_snp_guest_request *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t 
 req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct · line 3486

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines
arch/x86/kvm/svm/sev.c +14 -0
+    if (rc)
+        /* use the firmware error code */
+        rc = err;
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
     if (!filep || filep->f_op != &sev_fops)
         return -EBADF;

Quoted 8 lines

     return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
         struct device *dev = sev->dev;
         int ret;

Quoted 8 lines

         /*
          * SEV feature support can be detected on multiple devices but
          * the SEV FW commands must be issued on the master. During
          * probe, we do not know the master hence we create /dev/sev on
          * the first device probe.
          * sev_do_cmd() finds the right master device to which to issue
          * the command to the firmware.
      */

It is still a single /dev/sev node and the userspace cannot get it wrong, it does not have to choose between (for instance) /dev/sev0 and /dev/sev1 on a 2 SOC system.

Quoted 13 lines

...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
         if (!filep || filep->f_op != &sev_fops)
                 return -EBADF;
..
..

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

There is always just one fd which always uses psp_master, nothing from that fd is used.

It also ensures that we can only issue commands (sev_issue_cmd) after SEV/SNP guest has launched.

I can open /dev/sev and start sending commands to the firmware with no KVM running at all. Oh well, we discussed this offline :)

Quoted 5 lines

We don't have a valid fd to use before the guest launch. The file descriptor is passed as part of the guest launch flow, for example, in snp_launch_start().

Quoted 4 lines

More to the point, if sev->fd is still important, why is it ok to skip it for snp_handle_ext_guest_request()? Thanks,

Quoted 8 lines
Quoted 1 line

Then, we should do the same for snp_handle_ext_guest_request().

Okay.

This snp_handle_ext_guest_request() helper is for returning "Table 21. ATTESTATION_REPORT Structure" along with the certificate(s) used to sign the report: "This usage allows the attestation report and the certificates required to verify the report to be returned at the same time".

I can see:
1) KVM_SEV_SNP_{G,S}ET_CERTS ioctls on KVM VM and

This allows the VMM to (optionally) supply per-VM certificates that the guest can use to validate the attestation report, instead of the guest requesting separately.

Quoted 1 line

2) SNP_{SET,GET}_EXT_CONFIG ioctls on /dev/sev

This allows the VMM to (optionally) supply certificates used for all VMs, i.e., there is no need for per-VM certificates.

Quoted 1 line

Both store the passed blob and neither communicate it to the firmware. This makes me wonder - how does the attestation report (cooked by the firmware) get signed with those certificates passed on by the HV userspace?

These are for use by the guest to validate the attestation report. It allows the guest to obtain the certificate information without having to use another method to request the certificates.

By having this certificate store, the hypervisor can request the certificates from the KDS once, rather than every time a guest requests an attestation report.

Quoted 2 lines

Also, the cached blob in /dev/sev seems redundand - the attestation report is retuned for a specific guest so having a blob in the KVM VM makes sense and KVM unconditionally reserves memory for it anyway. And for the HV itself the blob is useless (?) so why bother with caching it in /dev/sev.

In general, the certificates are for the machine (VCEK, ASK, ARK), so they can be for all VMs on the machine. The per-VM blob allows a VMM to supply additional per-VM certficates, if it desires, but is not required.

Quoted 2 lines

And GET ioctls() return what SET passed on (not something the firware returned, for example), what is ever going to call SET? The userspace can

As stated above, the firmware already has the information needed to sign the attestation report. The SET IOCTL is used to supply the certficates to the guest for validation of the attestation report. This reduces the traffic and complexity of the guest requesting the certficates from the KDS.

Quoted 5 lines

as well cache what it passed and save a bit of the code/memory in the kernel.

btw SNP_{SET,GET}_EXT_CONFIG are documented in Documentation/virt/coco/sev-guest.rst but implemented in drivers/crypto/ccp/sev-dev.c (not sev-guest.c).

What do I miss in the big picture here? :) Thanks,

The reason for the extended request is to make the attestation request appear atomic to the guest. If you had to make two calls to request the information, in the future, when live migration is possible, there is no guarantee that the guest couldn't have been migrated in between the calls to obtain the certificates and the call to obtain the attestation report and thus validation of the attestation report could fail.

Thanks,
Tom

Quoted 2 lines
#164 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 335 lines · Tom Lendacky
Quoted 312 lines · Alexey Kardashevskiy
Quoted 295 lines · Ashish
Quoted 294 lines · Alexey Kardashevskiy
Quoted 268 lines · Ashish
Quoted 249 lines · Alexey Kardashevskiy
Quoted 226 lines · Ashish
Quoted 225 lines · Alexey Kardashevskiy
Quoted 168 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +4 -1
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct 
 kvm *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest 

request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE,
GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
      /* Allocate memory for context page */
      context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
      if (!context)
-        return NULL;
+        goto e_free;
      data.gctx_paddr = __psp_pa(context);
      rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); -    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
      return context;

arch/x86/kvm/svm/sev.c +5 -0
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;

  }
  static int snp_bind_asid(struct kvm *kvm, int *error)

arch/x86/kvm/svm/sev.c +2 -0
@@ -2653,6 +2665,8 @@ static int 
 snp_decommission_context(struct kvm *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +69 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct 
 kvm_vcpu *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || 
 !IS_ALIGNED(resp_gpa, PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct 
 sev_data_snp_guest_request *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, 
 gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, 
Quoted 1 line

&err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines

+    if (rc)
+        /* use the firmware error code */
+        rc = err;

arch/x86/kvm/svm/sev.c +11 -0
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
     if (!filep || filep->f_op != &sev_fops)
         return -EBADF;

Quoted 8 lines

     return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
         struct device *dev = sev->dev;
         int ret;

Quoted 8 lines

         /*
          * SEV feature support can be detected on multiple devices but
          * the SEV FW commands must be issued on the master. During
          * probe, we do not know the master hence we create /dev/sev on
          * the first device probe.
          * sev_do_cmd() finds the right master device to which to issue
          * the command to the firmware.
      */

It is still a single /dev/sev node and the userspace cannot get it wrong, it does not have to choose between (for instance) /dev/sev0 and /dev/sev1 on a 2 SOC system.

Quoted 13 lines

...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
         if (!filep || filep->f_op != &sev_fops)
                 return -EBADF;
..
..

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

There is always just one fd which always uses psp_master, nothing from that fd is used.

It also ensures that we can only issue commands (sev_issue_cmd) after SEV/SNP guest has launched.

I can open /dev/sev and start sending commands to the firmware with no KVM running at all. Oh well, we discussed this offline :)

Quoted 5 lines

We don't have a valid fd to use before the guest launch. The file descriptor is passed as part of the guest launch flow, for example, in snp_launch_start().

Quoted 4 lines

More to the point, if sev->fd is still important, why is it ok to skip it for snp_handle_ext_guest_request()? Thanks,

Quoted 8 lines
Quoted 1 line

Then, we should do the same for snp_handle_ext_guest_request().

Okay.

This snp_handle_ext_guest_request() helper is for returning "Table 21. ATTESTATION_REPORT Structure" along with the certificate(s) used to sign the report: "This usage allows the attestation report and the certificates required to verify the report to be returned at the same time".

I can see:
1) KVM_SEV_SNP_{G,S}ET_CERTS ioctls on KVM VM and

This allows the VMM to (optionally) supply per-VM certificates that the guest can use to validate the attestation report, instead of the guest requesting separately.

Quoted 1 line

2) SNP_{SET,GET}_EXT_CONFIG ioctls on /dev/sev

This allows the VMM to (optionally) supply certificates used for all VMs, i.e., there is no need for per-VM certificates.

Quoted 1 line

Both store the passed blob and neither communicate it to the firmware. This makes me wonder - how does the attestation report (cooked by the firmware) get signed with those certificates passed on by the HV userspace?

These are for use by the guest to validate the attestation report. It allows the guest to obtain the certificate information without having to use another method to request the certificates.

By having this certificate store, the hypervisor can request the certificates from the KDS once, rather than every time a guest requests an attestation report.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Also, the cached blob in /dev/sev seems redundand - the attestation report is retuned for a specific guest so having a blob in the KVM VM makes sense and KVM unconditionally reserves memory for it anyway. And for the HV itself the blob is useless (?) so why bother with caching it in /dev/sev.

In general, the certificates are for the machine (VCEK, ASK, ARK), so they can be for all VMs on the machine. The per-VM blob allows a VMM to supply additional per-VM certficates, if it desires, but is not required.

Quoted 2 lines

And GET ioctls() return what SET passed on (not something the firware returned, for example), what is ever going to call SET? The userspace can

As stated above, the firmware already has the information needed to sign the attestation report. The SET IOCTL is used to supply the certficates to the guest for validation of the attestation report.

Does the firmware have to have all certificates beforehand? How does the firmware choose which certificate to use for a specific VM, or just signs all reports with all certificates it knows?

Quoted 1 line

This reduces the traffic and complexity of the guest requesting the certficates from the KDS.

Guest <-> HV interaction is clear, I am only wondering about HV <-> FW.

Quoted 7 lines
Quoted 5 lines

as well cache what it passed and save a bit of the code/memory in the kernel.

btw SNP_{SET,GET}_EXT_CONFIG are documented in Documentation/virt/coco/sev-guest.rst but implemented in drivers/crypto/ccp/sev-dev.c (not sev-guest.c).

What do I miss in the big picture here? :) Thanks,

The reason for the extended request is to make the attestation request appear atomic to the guest. If you had to make two calls to request the information, in the future, when live migration is possible, there is no guarantee that the guest couldn't have been migrated in between the calls to obtain the certificates and the call to obtain the attestation report and thus validation of the attestation report could fail.

Signature · 1 line
Alexey
maintainer
#165 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 28 lines · Alexey Kardashevskiy
Quoted 27 lines · Tom Lendacky
Quoted 26 lines · Alexey Kardashevskiy
Quoted 23 lines · Ashish
Quoted 22 lines · Alexey Kardashevskiy
Quoted 21 lines · Ashish
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Ashish
Quoted 18 lines · Alexey Kardashevskiy
Quoted 17 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

And GET ioctls() return what SET passed on (not something the firware returned, for example), what is ever going to call SET? The userspace can

As stated above, the firmware already has the information needed to sign the attestation report. The SET IOCTL is used to supply the certficates to the guest for validation of the attestation report.

Does the firmware have to have all certificates beforehand? How does the firmware choose which certificate to use for a specific VM, or just signs all reports with all certificates it knows?

From the SNP API spec, the firmware uses the VCEK, which is derived from chip-unique secrets, to sign the attestation report.

The guest can then use the returned VCEK certificate, the ASK certificate and ARK certificate from the extended guest request to validate the attestation report.

Quoted 5 lines
Quoted 3 lines

This reduces the traffic and complexity of the guest requesting the certficates from the KDS.

Guest <-> HV interaction is clear, I am only wondering about HV <-> FW.

I'm not sure what you mean here. The HV doesn't put the signing key in the firmware, it is derived.

Thanks,
Tom

#166 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 31 lines · Tom Lendacky
Quoted 30 lines · Alexey Kardashevskiy
Quoted 29 lines · Tom Lendacky
Quoted 27 lines · Alexey Kardashevskiy
Quoted 19 lines · Ashish
Quoted 18 lines · Alexey Kardashevskiy
Quoted 17 lines · Ashish
Quoted 16 lines · Alexey Kardashevskiy
Quoted 15 lines · Ashish
Quoted 14 lines · Alexey Kardashevskiy
Quoted 13 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

---

And GET ioctls() return what SET passed on (not something the firware returned, for example), what is ever going to call SET? The userspace can

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

As stated above, the firmware already has the information needed to sign the attestation report. The SET IOCTL is used to supply the certficates to the guest for validation of the attestation report.

Does the firmware have to have all certificates beforehand? How does the firmware choose which certificate to use for a specific VM, or just signs all reports with all certificates it knows?

From the SNP API spec, the firmware uses the VCEK, which is derived
from chip-unique secrets, to sign the attestation report.

Does the firmware derive it? How does the guest gets to know it? (forgive me my ignorance)

Quoted 9 lines

The guest can then use the returned VCEK certificate, the ASK certificate and ARK certificate from the extended guest request to validate the attestation report.

Quoted 5 lines
Quoted 3 lines

This reduces the traffic and complexity of the guest requesting the certficates from the KDS.

Guest <-> HV interaction is clear, I am only wondering about HV <-> FW.

I'm not sure what you mean here. The HV doesn't put the signing key in the firmware, it is derived.

Those ioctls() are in the HV and they take certificates which then get sent to the guest but not to the firmware. The firmware signs a report with a key and the guest needs another half of it to verify the report. Sadly I do not know cryptography enough.

Signature · 1 line
Alexey
maintainer
#167 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 35 lines · Alexey Kardashevskiy
Quoted 34 lines · Tom Lendacky
Quoted 33 lines · Alexey Kardashevskiy
Quoted 30 lines · Tom Lendacky
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Ashish
Quoted 18 lines · Alexey Kardashevskiy
Quoted 17 lines · Ashish
Quoted 16 lines · Alexey Kardashevskiy
Quoted 15 lines · Ashish
Quoted 14 lines · Alexey Kardashevskiy
Quoted 13 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

---

And GET ioctls() return what SET passed on (not something the firware returned, for example), what is ever going to call SET? The userspace can

As stated above, the firmware already has the information needed to sign the attestation report. The SET IOCTL is used to supply the certficates to the guest for validation of the attestation report.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Does the firmware have to have all certificates beforehand? How does the firmware choose which certificate to use for a specific VM, or just signs all reports with all certificates it knows?

 From the SNP API spec, the firmware uses the VCEK, which is derived from chip-unique secrets, to sign the attestation report.

Does the firmware derive it? How does the guest gets to know it? (forgive me my ignorance)

Yes, the firmware derives the private key. The guest doesn't know the private key, it gets the VCEK certificate which has the public key and can then validate the attestation report.

Quoted 6 lines
Quoted 6 lines

The guest can then use the returned VCEK certificate, the ASK certificate and ARK certificate from the extended guest request to validate the attestation report.

Quoted 2 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

This reduces the traffic and complexity of the guest requesting the certficates from the KDS.

Guest <-> HV interaction is clear, I am only wondering about HV <-> FW.

I'm not sure what you mean here. The HV doesn't put the signing key in the firmware, it is derived.

Those ioctls() are in the HV and they take certificates which then get sent to the guest but not to the firmware. The firmware signs a report with a key and the guest needs another half of it to verify the report. Sadly I do not know cryptography enough.

Correct, no need to send the certificates to the firmware. The certs have the public key which can be used to verify the report signed with the private key.

Thanks,
Tom

Quoted 3 lines
#168 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Would it be okay to request that we add a KVM stat for how often there are GUEST_REQUEST_NAE exits? I think it'd be good for service operators to get a better idea how valued the feature is.

Signature · 1 line
-Dionna Glaze, PhD (she/her)
#169 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 3 lines · Dionna Amalie Glaze

Would it be okay to request that we add a KVM stat for how often there are GUEST_REQUEST_NAE exits? I think it'd be good for service operators to get a better idea how valued the feature is.

Heh, it's always ok to request something, but sometimes the answer will be no.

And in the case, the answer is likely "no stat for you". A year or so ago, in the context of us (Google) trying to upstream a pile of stats, we (KVM folks) came to a rough consensus that KVM should only add upstream stats if they are relatively generic and (almost) universally useful[*]. IMO, a one-off stat for a specific exit reason is too narrowly focused, e.g. collecting information on all exit reasons is superior. And no, that won't be accepted upstream either, because for some environments gathering detailed information on all exits is too much overhead (also covered in the link).

FWIW, we (GCE) plan on carrying stats like this in out-of-tree patches, i.e. your request for stats is likely something that would get accepted internally (if it isn't already captured through our generic stats collection).

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

#170 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 17 lines · Sean Christopherson
Quoted 3 lines · Dionna Amalie Glaze

Would it be okay to request that we add a KVM stat for how often there are GUEST_REQUEST_NAE exits? I think it'd be good for service operators to get a better idea how valued the feature is.

Heh, it's always ok to request something, but sometimes the answer will be no.

And in the case, the answer is likely "no stat for you". A year or so ago, in the context of us (Google) trying to upstream a pile of stats, we (KVM folks) came to a rough consensus that KVM should only add upstream stats if they are relatively generic and (almost) universally useful[*]. IMO, a one-off stat for a specific exit reason is too narrowly focused, e.g. collecting information on all exit reasons is superior. And no, that won't be accepted upstream either, because for some environments gathering detailed information on all exits is too much overhead (also covered in the link).

Quoted 5 lines

FWIW, we (GCE) plan on carrying stats like this in out-of-tree patches, i.e. your request for stats is likely something that would get accepted internally (if it isn't already captured through our generic stats collection).

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

Thanks Sean, noted :)

Signature · 1 line
-Dionna Glaze, PhD (she/her)
maintainer
#171 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

In reply to Alexey Kardashevskiy
Quoted 299 lines · Alexey Kardashevskiy
Quoted 296 lines · Ashish
Quoted 295 lines · Alexey Kardashevskiy
Quoted 269 lines · Ashish
Quoted 250 lines · Alexey Kardashevskiy
Quoted 226 lines · Ashish
Quoted 225 lines · Alexey Kardashevskiy
Quoted 168 lines · Michael Roth

From: Brijesh Singh <[email protected]>

Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification.

The SNP_EXT_GUEST_REQUEST is similar to SNP_GUEST_REQUEST with the difference of an additional certificate blob that can be passed through the SNP_SET_CONFIG ioctl defined in the CCP driver. The CCP driver provides snp_guest_ext_guest_request() that is used by the KVM to get both the report and certificate data at once.

---
  arch/x86/kvm/svm/sev.c | 185
+++++++++++++++++++++++++++++++++++++++--
  arch/x86/kvm/svm/svm.h |   2 +

  2 files changed, 181 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +4 -1
index 5f2b2092cdae..18efa70553c2 100644
@@ -331,6 +331,7 @@ static int sev_guest_init(struct kvm *kvm, 
 struct kvm_sev_cmd *argp)
           if (ret)
               goto e_free;
+        mutex_init(&sev->guest_req_lock);
           ret = sev_snp_init(&argp->error, false);
       } else {
           ret = sev_platform_init(&argp->error);
@@ -2051,23 +2052,34 @@ int sev_vm_move_enc_context_from(struct 
 kvm *kvm, unsigned int source_fd)
    */
   static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
   {
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
       struct sev_data_snp_addr data = {};
-    void *context;
+    void *context, *certs_data;
       int rc;
+    /* Allocate memory used for the certs data in SNP guest 

request */
+    certs_data = kzalloc(SEV_FW_BLOB_MAX_SIZE,
GFP_KERNEL_ACCOUNT);
+    if (!certs_data)
+        return NULL;
+
      /* Allocate memory for context page */
      context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT);
      if (!context)
-        return NULL;
+        goto e_free;
      data.gctx_paddr = __psp_pa(context);
      rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); -    if (rc) {
-        snp_free_firmware_page(context);
-        return NULL;
-    }
+    if (rc)
+        goto e_free;
+
+    sev->snp_certs_data = certs_data;
      return context;

arch/x86/kvm/svm/sev.c +5 -0
+
+e_free:
+    snp_free_firmware_page(context);
+    kfree(certs_data);
+    return NULL;

  }
  static int snp_bind_asid(struct kvm *kvm, int *error)

arch/x86/kvm/svm/sev.c +2 -0
@@ -2653,6 +2665,8 @@ static int snp_decommission_context(struct 
 kvm *kvm)
       snp_free_firmware_page(sev->snp_context);
       sev->snp_context = NULL;
+    kfree(sev->snp_certs_data);
+
       return 0;
   }
@@ -3174,6 +3188,8 @@ static int sev_es_validate_vmgexit(struct 
 vcpu_svm *svm, u64 *exit_code)

      case SVM_VMGEXIT_UNSUPPORTED_EVENT:
      case SVM_VMGEXIT_HV_FEATURES:
      case SVM_VMGEXIT_PSC:
+    case SVM_VMGEXIT_GUEST_REQUEST:
+    case SVM_VMGEXIT_EXT_GUEST_REQUEST:
          break;
      default:
          reason = GHCB_ERR_INVALID_EVENT;

arch/x86/kvm/svm/sev.c +69 -0
@@ -3396,6 +3412,149 @@ static int snp_complete_psc(struct 
 kvm_vcpu *vcpu)
       return 1;
   }
+static unsigned long snp_setup_guest_buf(struct vcpu_svm *svm,
+                     struct sev_data_snp_guest_request *data,
+                     gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    kvm_pfn_t req_pfn, resp_pfn;
+    struct kvm_sev_info *sev;
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || 
 !IS_ALIGNED(resp_gpa, PAGE_SIZE))
+        return SEV_RET_INVALID_PARAM;
+
+    req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa));
+    if (is_error_noslot_pfn(req_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa));
+    if (is_error_noslot_pfn(resp_pfn))
+        return SEV_RET_INVALID_ADDRESS;
+
+    if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true))
+        return SEV_RET_INVALID_ADDRESS;
+
+    data->gctx_paddr = __psp_pa(sev->snp_context);
+    data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT);
+    data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT);
+
+    return 0;
+}
+
+static void snp_cleanup_guest_buf(struct 
 sev_data_snp_guest_request *data, unsigned long *rc)
+{
+    u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT;
+    int ret;
+
+    ret = snp_page_reclaim(pfn);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+
+    ret = rmp_make_shared(pfn, PG_LEVEL_4K);
+    if (ret)
+        *rc = SEV_RET_INVALID_ADDRESS;
+}
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, 
 gpa_t req_gpa, gpa_t resp_gpa)
+{
+    struct sev_data_snp_guest_request data = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    struct kvm_sev_info *sev;
+    unsigned long rc;
+    int err;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &data, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, 
Quoted 1 line

&err);

This one goes via sev_issue_cmd_external_user() and uses sev-fd...

Quoted 46 lines

+    if (rc)
+        /* use the firmware error code */
+        rc = err;

arch/x86/kvm/svm/sev.c +11 -0
+
+    snp_cleanup_guest_buf(&data, &rc);
+
+unlock:
+    mutex_unlock(&sev->guest_req_lock);
+
+e_fail:
+    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+}
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, 

gpa_t req_gpa, gpa_t resp_gpa)

arch/x86/kvm/svm/sev.c +31 -0
+{
+    struct sev_data_snp_guest_request req = {0};
+    struct kvm_vcpu *vcpu = &svm->vcpu;
+    struct kvm *kvm = vcpu->kvm;
+    unsigned long data_npages;
+    struct kvm_sev_info *sev;
+    unsigned long rc, err;
+    u64 data_gpa;
+
+    if (!sev_snp_guest(vcpu->kvm)) {
+        rc = SEV_RET_INVALID_GUEST;
+        goto e_fail;
+    }
+
+    sev = &to_kvm_svm(kvm)->sev_info;
+
+    data_gpa = vcpu->arch.regs[VCPU_REGS_RAX];
+    data_npages = vcpu->arch.regs[VCPU_REGS_RBX];
+
+    if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
+        rc = SEV_RET_INVALID_ADDRESS;
+        goto e_fail;
+    }
+
+    mutex_lock(&sev->guest_req_lock);
+
+    rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa);
+    if (rc)
+        goto unlock;
+
+    rc = snp_guest_ext_guest_request(&req, (unsigned 
Quoted 2 lines

long)sev->snp_certs_data,
+                     &data_npages, &err);

but this one does not and jump straight to drivers/crypto/ccp/sev-dev.c ignoring sev->fd. Why different? Can these two be unified? sev_issue_cmd_external_user() only checks if fd is /dev/sev which is hardly useful.

Quoted 3 lines
Quoted 1 line

"[PATCH RFC v7 32/64] crypto: ccp: Provide APIs to query extended attestation report" added this one.

SNP_EXT_GUEST_REQUEST additionally returns a certificate blob and that's why it goes through the CCP driver interface snp_guest_ext_guest_request() that is used to get both the report and certificate data/blob at the same time.

True. I thought though that this calls for extending sev_issue_cmd() to take care of these extra parameters rather than just skipping the sev->fd.

Quoted 1 line

All the FW API calls on the KVM side go through sev_issue_cmd() and sev_issue_cmd_external_user() interfaces and that i believe uses sev->fd more of as a sanity check.

Does not look like it:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/crypto/ccp/sev-dev.c?h=v6.2-rc3#n1290

===
int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
     if (!filep || filep->f_op != &sev_fops)
         return -EBADF;

Quoted 8 lines

     return sev_do_cmd(cmd, data, error);
}
EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
===

The only "more" is that it requires sev->fd to be a valid open fd, what is the value in that? I may easily miss the bigger picture here. Thanks,

Have a look at following functions in drivers/crypto/ccp/sev-dev.c:
sev_dev_init() and sev_misc_init().

static int sev_misc_init(struct sev_device *sev)
{
         struct device *dev = sev->dev;
         int ret;

Quoted 8 lines

         /*
          * SEV feature support can be detected on multiple devices but
          * the SEV FW commands must be issued on the master. During
          * probe, we do not know the master hence we create /dev/sev on
          * the first device probe.
          * sev_do_cmd() finds the right master device to which to issue
          * the command to the firmware.
      */

It is still a single /dev/sev node and the userspace cannot get it wrong, it does not have to choose between (for instance) /dev/sev0 and /dev/sev1 on a 2 SOC system.

Quoted 13 lines

...
...

Hence, sev_issue_cmd_external_user() needs to ensure that the correct device (master device) is being operated upon and that's why there is the check for file operations matching sev_fops as below :

int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error)
{
         if (!filep || filep->f_op != &sev_fops)
                 return -EBADF;
..
..

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Essentially, sev->fd is the misc. device created for the master PSP device on which the SEV/SNP firmware commands are issued, hence, sev_issue_cmd() uses sev->fd.

There is always just one fd which always uses psp_master, nothing from that fd is used.

It also ensures that we can only issue commands (sev_issue_cmd) after SEV/SNP guest has launched.

I can open /dev/sev and start sending commands to the firmware with no KVM running at all. Oh well, we discussed this offline :)

Yes, and as we already discussed we need to support that to get SEV/SNP platform status (SNP_PLATFORM_STATUS) and also for legacy SEV commands like certificate generation and import/export which can be issued before a VM is launched.

Thanks,
Ashish

#172 lore raw

PATCH 1/1 KVM: SVM: Fix SVM_VMGEXIT_EXT_GUEST_REQUEST to follow the rest of API

In reply to Kalra, Ashish

When SVM VM is up, KVM uses sev_issue_cmd_external_user() with an open /dev/sev fd which ensures that the SVM initialization was done correctly. The only helper not following the scheme is snp_guest_ext_guest_request() which bypasses the fd check.

Change the SEV API to require passing a file.

Handle errors with care in the SNP Extended Guest Request handler (snp_handle_ext_guest_request()) as there are actually 3 types of errors:
- @rc: return code SEV device's sev_issue_cmd() which is int==int32;
- @err: a psp return code in sev_issue_cmd(), also int==int32 (probably a mistake but kvm_sev_cmd::error uses __u32 for some time now);
- (added by this) @exitcode: GHCB's exit code sw_exit_info_2, uint64.

Use the right types, remove cast to int* and return ENOSPC from SEV device for converting it to the GHCB's exit code SNP_GUEST_REQ_INVALID_LEN==BIT(32).

Fixes 17f1d0c995ac ("KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event")

While at this, preserve the original error in snp_cleanup_guest_buf().

---

This can easily be squashed into what it fixes.

The patch is made for
https://github.com/AMDESE/linux/commits/upmv10-host-snp-v7-rfc

---
 include/linux/psp-sev.h      | 62 +++++++++++---------
 arch/x86/kvm/svm/sev.c       | 50 +++++++++++-----
 drivers/crypto/ccp/sev-dev.c | 11 ++--
 3 files changed, 73 insertions(+), 50 deletions(-)
include/linux/psp-sev.h +33 -29
index 970a9de0ed20..466b1a6e7d7b 100644
@@ -848,6 +848,36 @@ int sev_platform_status(struct sev_user_data_status *status, int *error);
 int sev_issue_cmd_external_user(struct file *filep, unsigned int id,
 				void *data, int *error);
 
+/**
+ * sev_issue_cmd_external_user_cert - issue SEV command by other driver with a file
+ * handle and return certificates set onto SEV device via SNP_SET_EXT_CONFIG;
+ * intended for use by the SNP extended guest request command defined
+ * in the GHCB specification.
+ *
+ * @filep - SEV device file pointer
+ * @cmd - command to issue
+ * @data - command buffer
+ * @vaddr: address where the certificate blob need to be copied.
+ * @npages: number of pages for the certificate blob.
+ *    If the specified page count is less than the certificate blob size, then the
+ *    required page count is returned with ENOSPC error code.
+ *    If the specified page count is more than the certificate blob size, then
+ *    page count is updated to reflect the amount of valid data copied in the
+ *    vaddr.
+ *
+ * @error: SEV command return code
+ *
+ * Returns:
+ * 0 if the sev successfully processed the command
+ * -%ENODEV    if the sev device is not available
+ * -%ENOTSUPP  if the sev does not support SEV
+ * -%ETIMEDOUT if the sev command timed out
+ * -%EIO       if the sev returned a non-zero return code
+ * -%ENOSPC    if the specified page count is too small
+ */
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+				     unsigned long vaddr, unsigned long *npages, int *error);
+
 /**
  * sev_guest_deactivate - perform SEV DEACTIVATE command
  *
@@ -945,32 +975,6 @@ void snp_free_firmware_page(void *addr);
  */
 void snp_mark_pages_offline(unsigned long pfn, unsigned int npages);
 
-/**
- * snp_guest_ext_guest_request - perform the SNP extended guest request command
- *  defined in the GHCB specification.
- *
- * @data: the input guest request structure
- * @vaddr: address where the certificate blob need to be copied.
- * @npages: number of pages for the certificate blob.
- *    If the specified page count is less than the certificate blob size, then the
- *    required page count is returned with error code defined in the GHCB spec.
- *    If the specified page count is more than the certificate blob size, then
- *    page count is updated to reflect the amount of valid data copied in the
- *    vaddr.
- *
- * @sev_ret: sev command return code
- *
- * Returns:
- * 0 if the sev successfully processed the command
- * -%ENODEV    if the sev device is not available
- * -%ENOTSUPP  if the sev does not support SEV
- * -%ETIMEDOUT if the sev command timed out
- * -%EIO       if the sev returned a non-zero return code
- */
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-				unsigned long vaddr, unsigned long *npages,
-				unsigned long *error);
-
 #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
 static inline int
@@ -1013,9 +1017,9 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
 
 static inline void snp_free_firmware_page(void *addr) { }
 
-static inline int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-					      unsigned long vaddr, unsigned long *n,
-					      unsigned long *error)
+static inline int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd,
+						   void *data, unsigned long vaddr,
+						   unsigned long *npages, int *error)
 {
 	return -ENODEV;
 }
arch/x86/kvm/svm/sev.c +35 -15

104 lines not on this page · raw message

drivers/crypto/ccp/sev-dev.c +5 -6
index 6c4fdcaed72b..73f56c20255c 100644
@@ -2070,8 +2070,8 @@ int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *erro
 }
 EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt_page);
 
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-				unsigned long vaddr, unsigned long *npages, unsigned long *fw_err)
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+				     unsigned long vaddr, unsigned long *npages, int *error)
 {
 	unsigned long expected_npages;
 	struct sev_device *sev;
@@ -2093,12 +2093,11 @@ int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
 	expected_npages = sev->snp_certs_len >> PAGE_SHIFT;
 	if (*npages < expected_npages) {
 		*npages = expected_npages;
-		*fw_err = SNP_GUEST_REQ_INVALID_LEN;
 		mutex_unlock(&sev->snp_certs_lock);
-		return -EINVAL;
+		return -ENOSPC;
 	}
 
-	rc = sev_do_cmd(SEV_CMD_SNP_GUEST_REQUEST, data, (int *)fw_err);
+	rc = sev_issue_cmd_external_user(filep, cmd, data, error);
 	if (rc) {
 		mutex_unlock(&sev->snp_certs_lock);
 		return rc;
@@ -2115,7 +2114,7 @@ int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
 	mutex_unlock(&sev->snp_certs_lock);
 	return rc;
 }
-EXPORT_SYMBOL_GPL(snp_guest_ext_guest_request);
+EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user_cert);
 
 static void sev_exit(struct kref *ref)
 {
Signature · 1 line
2.39.1
maintainer
#173 lore raw

Re: [PATCH kernel] KVM: SVM: Fix SVM_VMGEXIT_EXT_GUEST_REQUEST to follow the rest of API

Quoted 152 lines · Alexey Kardashevskiy

When SVM VM is up, KVM uses sev_issue_cmd_external_user() with an open /dev/sev fd which ensures that the SVM initialization was done correctly. The only helper not following the scheme is snp_guest_ext_guest_request() which bypasses the fd check.

Change the SEV API to require passing a file.

Handle errors with care in the SNP Extended Guest Request handler (snp_handle_ext_guest_request()) as there are actually 3 types of errors:
- @rc: return code SEV device's sev_issue_cmd() which is int==int32;
- @err: a psp return code in sev_issue_cmd(), also int==int32 (probably a mistake but kvm_sev_cmd::error uses __u32 for some time now);
- (added by this) @exitcode: GHCB's exit code sw_exit_info_2, uint64.

Use the right types, remove cast to int* and return ENOSPC from SEV device for converting it to the GHCB's exit code SNP_GUEST_REQ_INVALID_LEN==BIT(32).

Fixes 17f1d0c995ac ("KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event")

While at this, preserve the original error in snp_cleanup_guest_buf().

---

This can easily be squashed into what it fixes.

The patch is made for
https://github.com/AMDESE/linux/commits/upmv10-host-snp-v7-rfc

---
  include/linux/psp-sev.h      | 62 +++++++++++---------
  arch/x86/kvm/svm/sev.c       | 50 +++++++++++-----
  drivers/crypto/ccp/sev-dev.c | 11 ++--
  3 files changed, 73 insertions(+), 50 deletions(-)
include/linux/psp-sev.h +33 -29
index 970a9de0ed20..466b1a6e7d7b 100644
@@ -848,6 +848,36 @@ int sev_platform_status(struct sev_user_data_status *status, int *error);
  int sev_issue_cmd_external_user(struct file *filep, unsigned int id,
  				void *data, int *error);
 
+/**
+ * sev_issue_cmd_external_user_cert - issue SEV command by other driver with a file
+ * handle and return certificates set onto SEV device via SNP_SET_EXT_CONFIG;
+ * intended for use by the SNP extended guest request command defined
+ * in the GHCB specification.
+ *
+ * @filep - SEV device file pointer
+ * @cmd - command to issue
+ * @data - command buffer
+ * @vaddr: address where the certificate blob need to be copied.
+ * @npages: number of pages for the certificate blob.
+ *    If the specified page count is less than the certificate blob size, then the
+ *    required page count is returned with ENOSPC error code.
+ *    If the specified page count is more than the certificate blob size, then
+ *    page count is updated to reflect the amount of valid data copied in the
+ *    vaddr.
+ *
+ * @error: SEV command return code
+ *
+ * Returns:
+ * 0 if the sev successfully processed the command
+ * -%ENODEV    if the sev device is not available
+ * -%ENOTSUPP  if the sev does not support SEV
+ * -%ETIMEDOUT if the sev command timed out
+ * -%EIO       if the sev returned a non-zero return code
+ * -%ENOSPC    if the specified page count is too small
+ */
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+				     unsigned long vaddr, unsigned long *npages, int *error);
+
  /**
   * sev_guest_deactivate - perform SEV DEACTIVATE command
   *
@@ -945,32 +975,6 @@ void snp_free_firmware_page(void *addr);
   */
  void snp_mark_pages_offline(unsigned long pfn, unsigned int npages);
 
-/**
- * snp_guest_ext_guest_request - perform the SNP extended guest request command
- *  defined in the GHCB specification.
- *
- * @data: the input guest request structure
- * @vaddr: address where the certificate blob need to be copied.
- * @npages: number of pages for the certificate blob.
- *    If the specified page count is less than the certificate blob size, then the
- *    required page count is returned with error code defined in the GHCB spec.
- *    If the specified page count is more than the certificate blob size, then
- *    page count is updated to reflect the amount of valid data copied in the
- *    vaddr.
- *
- * @sev_ret: sev command return code
- *
- * Returns:
- * 0 if the sev successfully processed the command
- * -%ENODEV    if the sev device is not available
- * -%ENOTSUPP  if the sev does not support SEV
- * -%ETIMEDOUT if the sev command timed out
- * -%EIO       if the sev returned a non-zero return code
- */
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-				unsigned long vaddr, unsigned long *npages,
-				unsigned long *error);
-
  #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
  static inline int
@@ -1013,9 +1017,9 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
 
  static inline void snp_free_firmware_page(void *addr) { }
 
-static inline int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-					      unsigned long vaddr, unsigned long *n,
-					      unsigned long *error)
+static inline int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd,
+						   void *data, unsigned long vaddr,
+						   unsigned long *npages, int *error)
  {
  	return -ENODEV;
  }
arch/x86/kvm/svm/sev.c +17 -0
index d0e58cffd1ed..b268c35efab4 100644
@@ -394,6 +394,23 @@ static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
  	return __sev_issue_cmd(sev->fd, id, data, error);
  }
 
+static int sev_issue_cmd_cert(struct kvm *kvm, int id, void *data,
+			      unsigned long vaddr, unsigned long *npages, int *error)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct fd f;
+	int ret;
+
+	f = fdget(sev->fd);
+	if (!f.file)
+		return -EBADF;
+
+	ret = sev_issue_cmd_external_user_cert(f.file, id, data, vaddr, npages, error);
+
+	fdput(f);
+	return ret;
+}
+
  static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
  {
  	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
@@ -3587,11 +3604,11 @@ static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data, unsig
  	int ret;
 

arch/x86/kvm/svm/sev.c static void snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data, unsig · line 3613

Quoted 10 lines
arch/x86/kvm/svm/sev.c +2 -2
  	ret = snp_page_reclaim(pfn);
-	if (ret)
+	if (ret && (*rc == SEV_RET_SUCCESS))
  		*rc = SEV_RET_INVALID_ADDRESS;
 
  	ret = rmp_make_shared(pfn, PG_LEVEL_4K);
-	if (ret)
+	if (ret && (*rc == SEV_RET_SUCCESS))
  		*rc = SEV_RET_INVALID_ADDRESS;
  }

I believe we need to fix this as per the GHCB specifications.

As per GHCB 2.0 specifications:

SW_EXITINFO2
...
State from Hypervisor: Upper 32-bits (63:32) will contain the return code from the hypervisor. Lower 32-bits (31:0) will contain the return code from the firmware call (0 = success)

So i believe the FW error code (which is the FW error code from SNP_GUEST_REQUEST or *rc here) should be contained in the lower 32-bits and the error code being returned back due to response buffer pages reclaim failure and/or failure to transisition these pages back to shared state is basically hypervisor (error) return code and that should be returned in the upper 32-bit of the exitinfo.

There is work in progress to check conformance of SNP v7 patches to GHCB 2.0 specifications, so probably this fix can be included as part of those patches.

arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3641

Quoted 6 lines
arch/x86/kvm/svm/sev.c +0 -1
@@ -3638,8 +3655,9 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp
  	struct kvm *kvm = vcpu->kvm;
  	unsigned long data_npages;
  	struct kvm_sev_info *sev;
-	unsigned long rc, err;

This needs to be looked at more carefully. The SEV firmware status code is defined as 32-bit, but is being handled as unsigned long in the KVM/SNP code and as int in the CCP driver. So this needs to be fixed consistently across, snp_setup_guest_buf() return value will need to be fixed accordingly.

arch/x86/kvm/svm/sev.c static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp · line 3690

Quoted 13 lines
arch/x86/kvm/svm/sev.c +3 -2
+	unsigned long exitcode;
  	u64 data_gpa;
+	int err, rc;
 
  	if (!sev_snp_guest(vcpu->kvm)) {
  		rc = SEV_RET_INVALID_GUEST;
@@ -3669,17 +3687,16 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp
  	 */
  	if (sev->snp_certs_len) {
  		if ((data_npages << PAGE_SHIFT) < sev->snp_certs_len) {
-			rc = -EINVAL;
-			err = SNP_GUEST_REQ_INVALID_LEN;
+			rc = -ENOSPC;

Why do we need to introduce ENOSPC error code?

If we continue to use SNP_GUEST_REQ_INVALID_LEN we don't need to map ENOSPC to SNP_GUEST_REQ_INVALID_LEN below.

And the CCP driver can return SNP_GUEST_REQ_INVALID_LEN as earlier via the fw_err parameter.

Thanks,
Ashish

Quoted 93 lines
arch/x86/kvm/svm/sev.c +13 -10
  			goto datalen;
  		}
-		rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req,
-				   (int *)&err);
+		rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, &err);
  	} else {
-		rc = snp_guest_ext_guest_request(&req,
-						 (unsigned long)sev->snp_certs_data,
-						 &data_npages, &err);
+		rc = sev_issue_cmd_cert(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req,
+					(unsigned long)sev->snp_certs_data,
+					&data_npages, &err);
  	}
+
  datalen:
  	if (sev->snp_certs_len)
  		data_npages = sev->snp_certs_len >> PAGE_SHIFT;
@@ -3689,27 +3706,30 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp
  		 * If buffer length is small then return the expected
  		 * length in rbx.
  		 */
-		if (err == SNP_GUEST_REQ_INVALID_LEN)
+		if (rc == -ENOSPC) {
  			vcpu->arch.regs[VCPU_REGS_RBX] = data_npages;
+			exitcode = SNP_GUEST_REQ_INVALID_LEN;
+			goto cleanup;
+		}
 
  		/* pass the firmware error code */
-		rc = err;
+		exitcode = err;
  		goto cleanup;
  	}
 
  	/* Copy the certificate blob in the guest memory */
  	if (data_npages &&
  	    kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT))
-		rc = SEV_RET_INVALID_ADDRESS;
+		exitcode = SEV_RET_INVALID_ADDRESS;
 
  cleanup:
-	snp_cleanup_guest_buf(&req, &rc);
+	snp_cleanup_guest_buf(&req, &exitcode);
 
  unlock:
  	mutex_unlock(&sev->guest_req_lock);
 
  e_fail:
-	svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+	svm_set_ghcb_sw_exit_info_2(vcpu, exitcode);
  }
 
  static kvm_pfn_t gfn_to_pfn_restricted(struct kvm *kvm, gfn_t gfn)
drivers/crypto/ccp/sev-dev.c +5 -6
index 6c4fdcaed72b..73f56c20255c 100644
@@ -2070,8 +2070,8 @@ int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *erro
  }
  EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt_page);
 
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-				unsigned long vaddr, unsigned long *npages, unsigned long *fw_err)
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+				     unsigned long vaddr, unsigned long *npages, int *error)
  {
  	unsigned long expected_npages;
  	struct sev_device *sev;
@@ -2093,12 +2093,11 @@ int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
  	expected_npages = sev->snp_certs_len >> PAGE_SHIFT;
  	if (*npages < expected_npages) {
  		*npages = expected_npages;
-		*fw_err = SNP_GUEST_REQ_INVALID_LEN;
  		mutex_unlock(&sev->snp_certs_lock);
-		return -EINVAL;
+		return -ENOSPC;
  	}
 
-	rc = sev_do_cmd(SEV_CMD_SNP_GUEST_REQUEST, data, (int *)fw_err);
+	rc = sev_issue_cmd_external_user(filep, cmd, data, error);
  	if (rc) {
  		mutex_unlock(&sev->snp_certs_lock);
  		return rc;
@@ -2115,7 +2114,7 @@ int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
  	mutex_unlock(&sev->snp_certs_lock);
  	return rc;
  }
-EXPORT_SYMBOL_GPL(snp_guest_ext_guest_request);
+EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user_cert);
 
  static void sev_exit(struct kref *ref)
  {
#174 lore raw

Re: [PATCH kernel] KVM: SVM: Fix SVM_VMGEXIT_EXT_GUEST_REQUEST to follow the rest of API

Quoted 189 lines · Ashish
Quoted 166 lines · Alexey Kardashevskiy

When SVM VM is up, KVM uses sev_issue_cmd_external_user() with an open /dev/sev fd which ensures that the SVM initialization was done correctly. The only helper not following the scheme is snp_guest_ext_guest_request() which bypasses the fd check.

Change the SEV API to require passing a file.

Handle errors with care in the SNP Extended Guest Request handler (snp_handle_ext_guest_request()) as there are actually 3 types of errors:
- @rc: return code SEV device's sev_issue_cmd() which is int==int32;
- @err: a psp return code in sev_issue_cmd(), also int==int32 (probably a mistake but kvm_sev_cmd::error uses __u32 for some time now);
- (added by this) @exitcode: GHCB's exit code sw_exit_info_2, uint64.

Use the right types, remove cast to int* and return ENOSPC from SEV device for converting it to the GHCB's exit code SNP_GUEST_REQ_INVALID_LEN==BIT(32).

Fixes 17f1d0c995ac ("KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event")

While at this, preserve the original error in snp_cleanup_guest_buf().

---

This can easily be squashed into what it fixes.

The patch is made for
https://github.com/AMDESE/linux/commits/upmv10-host-snp-v7-rfc
---
  include/linux/psp-sev.h      | 62 +++++++++++--------- arch/x86/kvm/svm/sev.c       | 50 +++++++++++----- drivers/crypto/ccp/sev-dev.c | 11 ++--

  3 files changed, 73 insertions(+), 50 deletions(-)
include/linux/psp-sev.h +2 -0
index 970a9de0ed20..466b1a6e7d7b 100644
@@ -848,6 +848,36 @@ int sev_platform_status(struct 
 sev_user_data_status *status, int *error);
   int sev_issue_cmd_external_user(struct file *filep, unsigned int id,
                   void *data, int *error);
+/**
+ * sev_issue_cmd_external_user_cert - issue SEV command by other 

driver with a file
+ * handle and return certificates set onto SEV device via SNP_SET_EXT_CONFIG;
+ * intended for use by the SNP extended guest request command defined
+ * in the GHCB specification.
+ *
+ * @filep - SEV device file pointer
+ * @cmd - command to issue
+ * @data - command buffer
+ * @vaddr: address where the certificate blob need to be copied.
+ * @npages: number of pages for the certificate blob.
+ *    If the specified page count is less than the certificate blob size, then the
+ *    required page count is returned with ENOSPC error code.
+ *    If the specified page count is more than the certificate blob size, then
+ *    page count is updated to reflect the amount of valid data copied in the
+ *    vaddr.
+ *
+ * @error: SEV command return code
+ *
+ * Returns:
+ * 0 if the sev successfully processed the command
+ * -%ENODEV    if the sev device is not available
+ * -%ENOTSUPP  if the sev does not support SEV
+ * -%ETIMEDOUT if the sev command timed out
+ * -%EIO       if the sev returned a non-zero return code
+ * -%ENOSPC    if the specified page count is too small
+ */
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+                     unsigned long vaddr, unsigned long *npages, int *error);
+
  /**
   * sev_guest_deactivate - perform SEV DEACTIVATE command *

include/linux/psp-sev.h +0 -2
@@ -945,32 +975,6 @@ void snp_free_firmware_page(void *addr);
    */
   void snp_mark_pages_offline(unsigned long pfn, unsigned int npages);
-/**
- * snp_guest_ext_guest_request - perform the SNP extended guest 

request command
- *  defined in the GHCB specification.
- *
- * @data: the input guest request structure
- * @vaddr: address where the certificate blob need to be copied.
- * @npages: number of pages for the certificate blob.
- *    If the specified page count is less than the certificate blob size, then the
- *    required page count is returned with error code defined in the GHCB spec.
- *    If the specified page count is more than the certificate blob size, then
- *    page count is updated to reflect the amount of valid data copied in the
- *    vaddr.
- *
- * @sev_ret: sev command return code
- *
- * Returns:
- * 0 if the sev successfully processed the command
- * -%ENODEV    if the sev device is not available
- * -%ENOTSUPP  if the sev does not support SEV
- * -%ETIMEDOUT if the sev command timed out
- * -%EIO       if the sev returned a non-zero return code
- */

include/linux/psp-sev.h +0 -4
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-                unsigned long vaddr, unsigned long *npages,
-                unsigned long *error);
-

  #else    /* !CONFIG_CRYPTO_DEV_SP_PSP */
  static inline int

include/linux/psp-sev.h +3 -3
@@ -1013,9 +1017,9 @@ static inline void 
 *snp_alloc_firmware_page(gfp_t mask)
   static inline void snp_free_firmware_page(void *addr) { }
-static inline int snp_guest_ext_guest_request(struct 
 sev_data_snp_guest_request *data,
-                          unsigned long vaddr, unsigned long *n,
-                          unsigned long *error)
+static inline int sev_issue_cmd_external_user_cert(struct file 
 *filep, unsigned int cmd,
+                           void *data, unsigned long vaddr,
+                           unsigned long *npages, int *error)
   {
       return -ENODEV;

  }

arch/x86/kvm/svm/sev.c +17 -0
index d0e58cffd1ed..b268c35efab4 100644
@@ -394,6 +394,23 @@ static int sev_issue_cmd(struct kvm *kvm, int id, 
 void *data, int *error)
       return __sev_issue_cmd(sev->fd, id, data, error);
   }
+static int sev_issue_cmd_cert(struct kvm *kvm, int id, void *data,
+                  unsigned long vaddr, unsigned long *npages, int 
 *error)
+{
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+    struct fd f;
+    int ret;
+
+    f = fdget(sev->fd);
+    if (!f.file)
+        return -EBADF;
+
+    ret = sev_issue_cmd_external_user_cert(f.file, id, data, vaddr, 
 npages, error);
+
+    fdput(f);
+    return ret;
+}
+
   static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)

  {
      struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;

arch/x86/kvm/svm/sev.c +0 -0
@@ -3587,11 +3604,11 @@ static void snp_cleanup_guest_buf(struct 

sev_data_snp_guest_request *data, unsig
      int ret;
      ret = snp_page_reclaim(pfn);

Quoted 8 lines

-    if (ret)
+    if (ret && (*rc == SEV_RET_SUCCESS))
          *rc = SEV_RET_INVALID_ADDRESS;
      ret = rmp_make_shared(pfn, PG_LEVEL_4K);
-    if (ret)
+    if (ret && (*rc == SEV_RET_SUCCESS))
          *rc = SEV_RET_INVALID_ADDRESS;
  }

I believe we need to fix this as per the GHCB specifications.

As per GHCB 2.0 specifications:

SW_EXITINFO2
...
State from Hypervisor: Upper
32-bits (63:32) will contain the
return code from the hypervisor.
Lower 32-bits (31:0) will contain
the return code from the firmware
call (0 = success)

Quoted 3 lines

So i believe the FW error code (which is the FW error code from SNP_GUEST_REQUEST or *rc here) should be contained in the lower 32-bits and the error code being returned back due to response buffer pages reclaim failure and/or failure to transisition these pages back to shared state is basically hypervisor (error) return code and that should be returned in the upper 32-bit of the exitinfo.

There is work in progress to check conformance of SNP v7 patches to GHCB 2.0 specifications, so probably this fix can be included as part of those patches.

Yes, please :)

Quoted 9 lines
Quoted 7 lines
arch/x86/kvm/svm/sev.c +0 -0
@@ -3638,8 +3655,9 @@ static void snp_handle_ext_guest_request(struct 

vcpu_svm *svm, gpa_t req_gpa, gp
      struct kvm *kvm = vcpu->kvm;
      unsigned long data_npages;
      struct kvm_sev_info *sev;
-    unsigned long rc, err;

This needs to be looked at more carefully. The SEV firmware status code is defined as 32-bit, but is being handled as unsigned long in the KVM/SNP code and as int in the CCP driver. So this needs to be fixed consistently across,

Ultimately it should be explicit u32 in SEV and u64 in GHCB because PSP and GHCB are binary interfaces and the sizes should be explicit. Error codes between KVM and CCP can be anything (unsigned long, u64) as it is the same binary.

Quoted 18 lines

snp_setup_guest_buf() return value will need to be fixed accordingly.

Quoted 6 lines

+    unsigned long exitcode;
      u64 data_gpa;
+    int err, rc;
      if (!sev_snp_guest(vcpu->kvm)) {
          rc = SEV_RET_INVALID_GUEST;

arch/x86/kvm/svm/sev.c +0 -0
@@ -3669,17 +3687,16 @@ static void 
Quoted 7 lines

snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp */
      if (sev->snp_certs_len) {
          if ((data_npages << PAGE_SHIFT) < sev->snp_certs_len) { -            rc = -EINVAL;
-            err = SNP_GUEST_REQ_INVALID_LEN;
+            rc = -ENOSPC;

Why do we need to introduce ENOSPC error code?

To distinguish it from other errors and return SNP_GUEST_REQ_INVALID_LEN when needed (the commit log mentions this).

Quoted 2 lines

If we continue to use SNP_GUEST_REQ_INVALID_LEN we don't need to map ENOSPC to SNP_GUEST_REQ_INVALID_LEN below. And the CCP driver can return SNP_GUEST_REQ_INVALID_LEN as earlier via the fw_err parameter.

imho this is a bad idea.

SNP_GUEST_REQ_INVALID_LEN is defined in the GHCB spec and GHCB is between KVM and VM, /dev/sev is neither GHCB nor KVM. err here is for the firmware errors but SNP_GUEST_REQ_INVALID_LEN is not from the firmware and for not-from-the-firmware-errors we already have "return rc" so lets just use that. Also err is 32bit across the place, in things like sev_issue_cmd() and then there is this ugly cast to int*. Thanks,

Quoted 95 lines

Thanks,
Ashish

Quoted 91 lines

              goto datalen;
          }
-        rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, -                   (int *)&err);
+        rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, &err); } else {
-        rc = snp_guest_ext_guest_request(&req,
-                         (unsigned long)sev->snp_certs_data, -                         &data_npages, &err);
+        rc = sev_issue_cmd_cert(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req, +                    (unsigned long)sev->snp_certs_data,
+                    &data_npages, &err);
      }
+
  datalen:
      if (sev->snp_certs_len)
          data_npages = sev->snp_certs_len >> PAGE_SHIFT;

arch/x86/kvm/svm/sev.c +0 -0
@@ -3689,27 +3706,30 @@ static void 

snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp
           * If buffer length is small then return the expected
           * length in rbx.
           */
-        if (err == SNP_GUEST_REQ_INVALID_LEN)
+        if (rc == -ENOSPC) {
              vcpu->arch.regs[VCPU_REGS_RBX] = data_npages;
+            exitcode = SNP_GUEST_REQ_INVALID_LEN;
+            goto cleanup;
+        }
          /* pass the firmware error code */
-        rc = err;
+        exitcode = err;
          goto cleanup;
      }
      /* Copy the certificate blob in the guest memory */
      if (data_npages &&
          kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT)) -        rc = SEV_RET_INVALID_ADDRESS;
+        exitcode = SEV_RET_INVALID_ADDRESS;
  cleanup:
-    snp_cleanup_guest_buf(&req, &rc);
+    snp_cleanup_guest_buf(&req, &exitcode);
  unlock:
      mutex_unlock(&sev->guest_req_lock);
  e_fail:
-    svm_set_ghcb_sw_exit_info_2(vcpu, rc);
+    svm_set_ghcb_sw_exit_info_2(vcpu, exitcode);
  }
  static kvm_pfn_t gfn_to_pfn_restricted(struct kvm *kvm, gfn_t gfn)

drivers/crypto/ccp/sev-dev.c +0 -0
index 6c4fdcaed72b..73f56c20255c 100644
@@ -2070,8 +2070,8 @@ int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 

src_pfn, u64 dst_pfn, int *erro
  }
  EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt_page);
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data, -                unsigned long vaddr, unsigned long *npages, unsigned long *fw_err)
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+                     unsigned long vaddr, unsigned long *npages, int *error)
  {
      unsigned long expected_npages;
      struct sev_device *sev;

drivers/crypto/ccp/sev-dev.c +3 -4
@@ -2093,12 +2093,11 @@ int snp_guest_ext_guest_request(struct 
 sev_data_snp_guest_request *data,
       expected_npages = sev->snp_certs_len >> PAGE_SHIFT;
       if (*npages < expected_npages) {
           *npages = expected_npages;
-        *fw_err = SNP_GUEST_REQ_INVALID_LEN;
           mutex_unlock(&sev->snp_certs_lock);
-        return -EINVAL;
+        return -ENOSPC;
       }
-    rc = sev_do_cmd(SEV_CMD_SNP_GUEST_REQUEST, data, (int *)fw_err);
+    rc = sev_issue_cmd_external_user(filep, cmd, data, error);
       if (rc) {
           mutex_unlock(&sev->snp_certs_lock);
           return rc;
@@ -2115,7 +2114,7 @@ int snp_guest_ext_guest_request(struct 
 sev_data_snp_guest_request *data,
       mutex_unlock(&sev->snp_certs_lock);
       return rc;
   }
-EXPORT_SYMBOL_GPL(snp_guest_ext_guest_request);
+EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user_cert);
   static void sev_exit(struct kref *ref)
   {
Signature · 1 line
Alexey
maintainer
#175 lore raw

Re: [PATCH kernel] KVM: SVM: Fix SVM_VMGEXIT_EXT_GUEST_REQUEST to follow the rest of API

Hello Alexey,

Quoted 193 lines · Alexey Kardashevskiy
Quoted 190 lines · Ashish
Quoted 167 lines · Alexey Kardashevskiy

When SVM VM is up, KVM uses sev_issue_cmd_external_user() with an open /dev/sev fd which ensures that the SVM initialization was done correctly. The only helper not following the scheme is snp_guest_ext_guest_request() which bypasses the fd check.

Change the SEV API to require passing a file.

Handle errors with care in the SNP Extended Guest Request handler (snp_handle_ext_guest_request()) as there are actually 3 types of errors:
- @rc: return code SEV device's sev_issue_cmd() which is int==int32;
- @err: a psp return code in sev_issue_cmd(), also int==int32 (probably a mistake but kvm_sev_cmd::error uses __u32 for some time now);
- (added by this) @exitcode: GHCB's exit code sw_exit_info_2, uint64.

Use the right types, remove cast to int* and return ENOSPC from SEV device for converting it to the GHCB's exit code SNP_GUEST_REQ_INVALID_LEN==BIT(32).

Fixes 17f1d0c995ac ("KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event")

While at this, preserve the original error in snp_cleanup_guest_buf().

---

This can easily be squashed into what it fixes.

The patch is made for
https://github.com/AMDESE/linux/commits/upmv10-host-snp-v7-rfc
---
  include/linux/psp-sev.h      | 62 +++++++++++--------- arch/x86/kvm/svm/sev.c       | 50 +++++++++++----- drivers/crypto/ccp/sev-dev.c | 11 ++--

  3 files changed, 73 insertions(+), 50 deletions(-)
include/linux/psp-sev.h +2 -0
index 970a9de0ed20..466b1a6e7d7b 100644
@@ -848,6 +848,36 @@ int sev_platform_status(struct 
 sev_user_data_status *status, int *error);
   int sev_issue_cmd_external_user(struct file *filep, unsigned int id,
                   void *data, int *error);
+/**
+ * sev_issue_cmd_external_user_cert - issue SEV command by other 

driver with a file
+ * handle and return certificates set onto SEV device via SNP_SET_EXT_CONFIG;
+ * intended for use by the SNP extended guest request command defined
+ * in the GHCB specification.
+ *
+ * @filep - SEV device file pointer
+ * @cmd - command to issue
+ * @data - command buffer
+ * @vaddr: address where the certificate blob need to be copied.
+ * @npages: number of pages for the certificate blob.
+ *    If the specified page count is less than the certificate blob size, then the
+ *    required page count is returned with ENOSPC error code.
+ *    If the specified page count is more than the certificate blob size, then
+ *    page count is updated to reflect the amount of valid data copied in the
+ *    vaddr.
+ *
+ * @error: SEV command return code
+ *
+ * Returns:
+ * 0 if the sev successfully processed the command
+ * -%ENODEV    if the sev device is not available
+ * -%ENOTSUPP  if the sev does not support SEV
+ * -%ETIMEDOUT if the sev command timed out
+ * -%EIO       if the sev returned a non-zero return code
+ * -%ENOSPC    if the specified page count is too small
+ */
+int sev_issue_cmd_external_user_cert(struct file *filep, unsigned int cmd, void *data,
+                     unsigned long vaddr, unsigned long *npages, int *error);
+
  /**
   * sev_guest_deactivate - perform SEV DEACTIVATE command *

include/linux/psp-sev.h +0 -2
@@ -945,32 +975,6 @@ void snp_free_firmware_page(void *addr);
    */
   void snp_mark_pages_offline(unsigned long pfn, unsigned int npages);
-/**
- * snp_guest_ext_guest_request - perform the SNP extended guest 

request command
- *  defined in the GHCB specification.
- *
- * @data: the input guest request structure
- * @vaddr: address where the certificate blob need to be copied.
- * @npages: number of pages for the certificate blob.
- *    If the specified page count is less than the certificate blob size, then the
- *    required page count is returned with error code defined in the GHCB spec.
- *    If the specified page count is more than the certificate blob size, then
- *    page count is updated to reflect the amount of valid data copied in the
- *    vaddr.
- *
- * @sev_ret: sev command return code
- *
- * Returns:
- * 0 if the sev successfully processed the command
- * -%ENODEV    if the sev device is not available
- * -%ENOTSUPP  if the sev does not support SEV
- * -%ETIMEDOUT if the sev command timed out
- * -%EIO       if the sev returned a non-zero return code
- */
-int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
-                unsigned long vaddr, unsigned long *npages, -                unsigned long *error);
-
  #else    /* !CONFIG_CRYPTO_DEV_SP_PSP */
  static inline int

include/linux/psp-sev.h +3 -3
@@ -1013,9 +1017,9 @@ static inline void 
 *snp_alloc_firmware_page(gfp_t mask)
   static inline void snp_free_firmware_page(void *addr) { }
-static inline int snp_guest_ext_guest_request(struct 
 sev_data_snp_guest_request *data,
-                          unsigned long vaddr, unsigned long *n,
-                          unsigned long *error)
+static inline int sev_issue_cmd_external_user_cert(struct file 
 *filep, unsigned int cmd,
+                           void *data, unsigned long vaddr,
+                           unsigned long *npages, int *error)
   {
       return -ENODEV;

  }

arch/x86/kvm/svm/sev.c +17 -0
index d0e58cffd1ed..b268c35efab4 100644
@@ -394,6 +394,23 @@ static int sev_issue_cmd(struct kvm *kvm, int 
 id, void *data, int *error)
       return __sev_issue_cmd(sev->fd, id, data, error);
   }
+static int sev_issue_cmd_cert(struct kvm *kvm, int id, void *data,
+                  unsigned long vaddr, unsigned long *npages, int 
 *error)
+{
+    struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+    struct fd f;
+    int ret;
+
+    f = fdget(sev->fd);
+    if (!f.file)
+        return -EBADF;
+
+    ret = sev_issue_cmd_external_user_cert(f.file, id, data, vaddr, 
 npages, error);
+
+    fdput(f);
+    return ret;
+}
+
   static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)

  {
      struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;

arch/x86/kvm/svm/sev.c +0 -0
@@ -3587,11 +3604,11 @@ static void snp_cleanup_guest_buf(struct 

sev_data_snp_guest_request *data, unsig
      int ret;
      ret = snp_page_reclaim(pfn);

Quoted 8 lines

-    if (ret)
+    if (ret && (*rc == SEV_RET_SUCCESS))
          *rc = SEV_RET_INVALID_ADDRESS;
      ret = rmp_make_shared(pfn, PG_LEVEL_4K);
-    if (ret)
+    if (ret && (*rc == SEV_RET_SUCCESS))
          *rc = SEV_RET_INVALID_ADDRESS;
  }

I believe we need to fix this as per the GHCB specifications.

As per GHCB 2.0 specifications:

SW_EXITINFO2
...
State from Hypervisor: Upper
32-bits (63:32) will contain the
return code from the hypervisor.
Lower 32-bits (31:0) will contain
the return code from the firmware
call (0 = success)

Quoted 6 lines
Quoted 3 lines

So i believe the FW error code (which is the FW error code from SNP_GUEST_REQUEST or *rc here) should be contained in the lower 32-bits and the error code being returned back due to response buffer pages reclaim failure and/or failure to transisition these pages back to shared state is basically hypervisor (error) return code and that should be returned in the upper 32-bit of the exitinfo.

There is work in progress to check conformance of SNP v7 patches to GHCB 2.0 specifications, so probably this fix can be included as part of those patches.

Yes, please :)

Yes, will address this in the GHCB specs conformance patch-set for SNP as per the following revisions of the GHCB specs:

The SNP Guest Request and SNP Extended Guest Request have been updated to expand on the use of the SW_EXITINFO2 return value to better allow for the hypervisor to return error codes.

Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c +0 -0
@@ -3638,8 +3655,9 @@ static void snp_handle_ext_guest_request(struct 

vcpu_svm *svm, gpa_t req_gpa, gp
      struct kvm *kvm = vcpu->kvm;
      unsigned long data_npages;
      struct kvm_sev_info *sev;
-    unsigned long rc, err;

This needs to be looked at more carefully. The SEV firmware status code is defined as 32-bit, but is being handled as unsigned long in the KVM/SNP code and as int in the CCP driver. So this needs to be fixed consistently across,

Quoted 2 lines

Ultimately it should be explicit u32 in SEV and u64 in GHCB because PSP and GHCB are binary interfaces and the sizes should be explicit. Error codes between KVM and CCP can be anything (unsigned long, u64) as it is the same binary.

Again, as lower 32-bits (31:0) of SW_EXITINFO2 is supposed to be set
to the return code from the firmware, so this should also be u32 in GHCB and same in KVM/SNP code.

Quoted 22 lines
Quoted 18 lines

snp_setup_guest_buf() return value will need to be fixed accordingly.

Quoted 6 lines

+    unsigned long exitcode;
      u64 data_gpa;
+    int err, rc;
      if (!sev_snp_guest(vcpu->kvm)) {
          rc = SEV_RET_INVALID_GUEST;

arch/x86/kvm/svm/sev.c +0 -0
@@ -3669,17 +3687,16 @@ static void 
Quoted 7 lines

snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp */
      if (sev->snp_certs_len) {
          if ((data_npages << PAGE_SHIFT) < sev->snp_certs_len) { -            rc = -EINVAL;
-            err = SNP_GUEST_REQ_INVALID_LEN;
+            rc = -ENOSPC;

Why do we need to introduce ENOSPC error code?

To distinguish it from other errors and return SNP_GUEST_REQ_INVALID_LEN when needed (the commit log mentions this).

Quoted 7 lines
Quoted 2 lines

If we continue to use SNP_GUEST_REQ_INVALID_LEN we don't need to map ENOSPC to SNP_GUEST_REQ_INVALID_LEN below. And the CCP driver can return SNP_GUEST_REQ_INVALID_LEN as earlier via the fw_err parameter.

imho this is a bad idea.

SNP_GUEST_REQ_INVALID_LEN is defined in the GHCB spec and GHCB is between KVM and VM, /dev/sev is neither GHCB nor KVM. err here is for the firmware errors but SNP_GUEST_REQ_INVALID_LEN is not from the firmware and for not-from-the-firmware-errors we already have "return rc" so lets just use that. Also err is 32bit across the place, in things like sev_issue_cmd() and then there is this ugly cast to int*. Thanks,

Ok, that does make sense.

Thanks,
Ashish

#176 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

In reply to Michael Roth

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3462

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{

Both regular,

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3498

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{

and extended guest requests should be subject to rate limiting, since they take a lock on the shared resource that is the AMD-SP (psp?). I proposed a mechanism with empirically chosen defaults in

[PATCH v2 0/2] kvm: sev: Add SNP guest request throttling [PATCH v2 1/2] kvm: sev: Add SEV-SNP guest request throttling [PATCH v2 2/2] kvm: sev: If ccp is busy, report throttled to guest

http://129.79.113.48/hypermail/linux/kernel/2211.2/03107.html
http://129.79.113.48/hypermail/linux/kernel/2211.2/03110.html
http://129.79.113.48/hypermail/linux/kernel/2211.2/03111.html

But I don't see these on lore. Would you like me to repost these?

Signature · 1 line
-Dionna Glaze, PhD (she/her)
maintainer
#177 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 18 lines · Dionna Amalie Glaze

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3462

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{

Both regular,

arch/x86/kvm/svm/sev.c static int snp_complete_psc(struct kvm_vcpu *vcpu) · line 3498

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
+{

and extended guest requests should be subject to rate limiting, since they take a lock on the shared resource that is the AMD-SP (psp?). I proposed a mechanism with empirically chosen defaults in

[PATCH v2 0/2] kvm: sev: Add SNP guest request throttling [PATCH v2 1/2] kvm: sev: Add SEV-SNP guest request throttling [PATCH v2 2/2] kvm: sev: If ccp is busy, report throttled to guest

Quoted 6 lines

http://129.79.113.48/hypermail/linux/kernel/2211.2/03107.html
http://129.79.113.48/hypermail/linux/kernel/2211.2/03110.html
http://129.79.113.48/hypermail/linux/kernel/2211.2/03111.html

But I don't see these on lore. Would you like me to repost these?

Yes, please.

Thanks,
Ashish

#178 lore raw

Re: [PATCH RFC v7 52/64] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event

Quoted 22 lines · Ashish
Quoted 20 lines · Dionna Amalie Glaze
Quoted 4 lines
(quoted diff) +2 -0
+
+static void snp_handle_guest_request(struct vcpu_svm *svm, gpa_t

req_gpa, gpa_t resp_gpa)
+{

Both regular,

Quoted 4 lines
(quoted diff) +2 -0
+
+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t

req_gpa, gpa_t resp_gpa)
+{

and extended guest requests should be subject to rate limiting, since they take a lock on the shared resource that is the AMD-SP (psp?). I proposed a mechanism with empirically chosen defaults in

[PATCH v2 0/2] kvm: sev: Add SNP guest request throttling [PATCH v2 1/2] kvm: sev: Add SEV-SNP guest request throttling [PATCH v2 2/2] kvm: sev: If ccp is busy, report throttled to guest

Quoted 9 lines
Quoted 6 lines

http://129.79.113.48/hypermail/linux/kernel/2211.2/03107.html
http://129.79.113.48/hypermail/linux/kernel/2211.2/03110.html
http://129.79.113.48/hypermail/linux/kernel/2211.2/03111.html

But I don't see these on lore. Would you like me to repost these?

Yes, please.

I think it's this series:

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

-Dov

#179 lore raw

RFC PATCH v7 53/64 KVM: SVM: Use a VMSA physical address variable for populating VMCB

In reply to Michael Roth

From: Tom Lendacky <[email protected]>

In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.

---
 arch/x86/kvm/svm/sev.c | 5 ++---
 arch/x86/kvm/svm/svm.c | 9 ++++++++-
 arch/x86/kvm/svm/svm.h | 1 +
 3 files changed, 11 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +2 -3
index 18efa70553c2..36c312143d12 100644
@@ -3845,10 +3845,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)
 
 	/*
 	 * An SEV-ES guest requires a VMSA area that is a separate from the
-	 * VMCB page. Do not include the encryption mask on the VMSA physical
-	 * address since hardware will access it using the guest key.
+	 * VMCB page.
 	 */
-	svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa);
+	svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa;
 
 	/* Can't intercept CR register access, HV can't modify CR registers */
 	svm_clr_intercept(svm, INTERCEPT_CR0_READ);
arch/x86/kvm/svm/svm.c +8 -1
index 43f04fc95a0a..e9317d27a01d 100644
@@ -1398,9 +1398,16 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
 	svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
 	svm_switch_vmcb(svm, &svm->vmcb01);
 
-	if (vmsa_page)
+	if (vmsa_page) {
 		svm->sev_es.vmsa = page_address(vmsa_page);
 
+		/*
+		 * Do not include the encryption mask on the VMSA physical
+		 * address since hardware will access it using the guest key.
+		 */
+		svm->sev_es.vmsa_pa = __pa(svm->sev_es.vmsa);
+	}
+
 	svm->guest_state_loaded = false;
 
 	return 0;
arch/x86/kvm/svm/svm.h +1 -0
index 7c0f9d00950f..284902e22dce 100644
@@ -204,6 +204,7 @@ struct svm_nested_state {
 struct vcpu_sev_es_state {
 	/* SEV-ES support */
 	struct sev_es_save_area *vmsa;
+	hpa_t vmsa_pa;
 	bool ghcb_in_use;
 	bool received_first_sipi;
 	unsigned int ap_reset_hold_type;
Signature · 1 line
2.25.1
#180 lore raw

RFC PATCH v7 54/64 KVM: SVM: Support SEV-SNP AP Creation NAE event

In reply to Michael Roth

From: Tom Lendacky <[email protected]>

Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI.

A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running.

For CREATE
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked.

For CREATE_ON_INIT:
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU.

For DESTROY:
The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked.

The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to the KVM_MP_STATE_UNINITIALIZED state, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left as KVM_MP_STATE_UNINITIALIZED to prevent it from being run.

[mdr: add handling for restrictedmem]
---
 arch/x86/include/asm/kvm_host.h |   1 +
 arch/x86/include/asm/svm.h      |   7 +-
 arch/x86/kvm/svm/sev.c          | 245 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c          |   3 +
 arch/x86/kvm/svm/svm.h          |   7 +
 arch/x86/kvm/x86.c              |   9 ++
 6 files changed, 271 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 15b9c678b281..5958cd93e5e6 100644
@@ -115,6 +115,7 @@
 #define KVM_REQ_HV_TLB_FLUSH \
 	KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_MEMORY_MCE		KVM_ARCH_REQ(33)
+#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE	KVM_ARCH_REQ(34)
 
 #define CR0_RESERVED_BITS                                               \
 	(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
arch/x86/include/asm/svm.h +6 -1
index c18d78d5e505..e76ad26ba64f 100644
@@ -278,7 +278,12 @@ enum avic_ipi_failure_cause {
 #define AVIC_HPA_MASK	~((0xFFFULL << 52) | 0xFFF)
 #define VMCB_AVIC_APIC_BAR_MASK		0xFFFFFFFFFF000ULL
 
-#define SVM_SEV_FEAT_SNP_ACTIVE		BIT(0)
+#define SVM_SEV_FEAT_SNP_ACTIVE			BIT(0)
+#define SVM_SEV_FEAT_RESTRICTED_INJECTION	BIT(3)
+#define SVM_SEV_FEAT_ALTERNATE_INJECTION	BIT(4)
+#define SVM_SEV_FEAT_INT_INJ_MODES		\
+	(SVM_SEV_FEAT_RESTRICTED_INJECTION |	\
+	 SVM_SEV_FEAT_ALTERNATE_INJECTION)
 
 struct vmcb_seg {
 	u16 selector;
arch/x86/kvm/svm/sev.c +245 -0

281 lines not on this page · raw message

arch/x86/kvm/svm/svm.c +3 -0
index e9317d27a01d..7f8c480dfa5e 100644
@@ -1337,6 +1337,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
 	svm->spec_ctrl = 0;
 	svm->virt_spec_ctrl = 0;
 
+	if (init_event)
+		sev_snp_init_protected_guest_state(vcpu);
+
 	init_vmcb(vcpu);
 
 	if (!init_event)
arch/x86/kvm/svm/svm.h +7 -0
index 284902e22dce..5e7cb0260dc3 100644
@@ -103,6 +103,8 @@ struct kvm_sev_info {
 	spinlock_t psc_lock;
 	void *snp_certs_data;
 	struct mutex guest_req_lock;
+
+	u64 sev_features;	/* Features set at VMSA creation */
 };
 
 struct kvm_svm {
@@ -224,6 +226,10 @@ struct vcpu_sev_es_state {
 	u64 ghcb_sw_exit_info_2;
 
 	u64 ghcb_registered_gpa;
+
+	struct mutex snp_vmsa_mutex;
+	gpa_t snp_vmsa_gpa;
+	bool snp_ap_create;
 };
 
 struct vcpu_svm {
@@ -734,6 +740,7 @@ void sev_rmp_page_level_adjust(struct kvm *kvm, gfn_t gfn, int *level);
 int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
 void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
 void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
+void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
arch/x86/kvm/x86.c +9 -0
index 08dd1ef7e136..a08601277497 100644
@@ -10387,6 +10387,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 			r = 0;
 			goto out;
 		}
+
+		if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) {
+			kvm_vcpu_reset(vcpu, true);
+			if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
+				goto out;
+		}
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
@@ -12667,6 +12673,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
 		return true;
 #endif
 
+	if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu))
+		return true;
+
 	if (kvm_arch_interrupt_allowed(vcpu) &&
 	    (kvm_cpu_has_interrupt(vcpu) ||
 	    kvm_guest_apic_has_interrupt(vcpu)))
Signature · 1 line
2.25.1
#181 lore raw

RFC PATCH v7 55/64 KVM: SVM: Add SNP-specific handling for memory attribute updates

In reply to Michael Roth

This will handle RMP table updates and direct map changes needed for page state conversions requested by userspace.

---
 arch/x86/kvm/svm/sev.c | 126 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c |   1 +
 arch/x86/kvm/svm/svm.h |   2 +
 3 files changed, 129 insertions(+)
arch/x86/kvm/svm/sev.c +126 -0
index 2f4c9f2bcf76..885a3f1da910 100644
@@ -3395,6 +3395,31 @@ static int snp_rmptable_psmash(struct kvm *kvm, kvm_pfn_t pfn)
 	return psmash(pfn);
 }
 
+static int snp_make_page_shared(struct kvm *kvm, gpa_t gpa, kvm_pfn_t pfn, int level)
+{
+	int rc, rmp_level;
+
+	rc = snp_lookup_rmpentry(pfn, &rmp_level);
+	if (rc < 0)
+		return -EINVAL;
+
+	/* If page is not assigned then do nothing */
+	if (!rc)
+		return 0;
+
+	/*
+	 * Is the page part of an existing 2MB RMP entry ? Split the 2MB into
+	 * multiple of 4K-page before making the memory shared.
+	 */
+	if (level == PG_LEVEL_4K && rmp_level == PG_LEVEL_2M) {
+		rc = snp_rmptable_psmash(kvm, pfn);
+		if (rc)
+			return rc;
+	}
+
+	return rmp_make_shared(pfn, level);
+}
+
 /*
  * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr
  * and process that here accordingly.
@@ -4428,3 +4453,104 @@ int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *priva
 out_unhandled:
 	return 0;
 }
+
+static inline u8 order_to_level(int order)
+{
+	BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G);
+
+	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G))
+		return PG_LEVEL_1G;
+
+	if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M))
+		return PG_LEVEL_2M;
+
+	return PG_LEVEL_4K;
+}
+
+int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,
+			gfn_t start, gfn_t end)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(slot->kvm)->sev_info;
+	enum psc_op op = (attr & KVM_MEMORY_ATTRIBUTE_PRIVATE) ? SNP_PAGE_STATE_PRIVATE
+							       : SNP_PAGE_STATE_SHARED;
+	gfn_t gfn = start;
+
+	pr_debug("%s: GFN 0x%llx - 0x%llx, op: %d\n", __func__, start, end, op);
+
+	if (!sev_snp_guest(slot->kvm))
+		return 0;
+
+	if (!kvm_slot_can_be_private(slot)) {
+		pr_err_ratelimited("%s: memslot for gfn: 0x%llx is not private.\n",
+				   __func__, gfn);
+		return -EPERM;
+	}
+
+	while (gfn < end) {
+		kvm_pfn_t pfn;
+		int level = PG_LEVEL_4K; /* TODO: take actual order into account */
+		gpa_t gpa = gfn_to_gpa(gfn);
+		int npages = 1;
+		int order;
+		int rc;
+
+		/*
+		 * No work to do if there was never a page allocated from private
+		 * memory. If there was a page that was deallocated previously,
+		 * the invalidation notifier should have restored the page to
+		 * shared.
+		 */
+		rc = kvm_restricted_mem_get_pfn(slot, gfn, &pfn, &order);
+		if (rc) {
+			pr_warn_ratelimited("%s: failed to retrieve gfn 0x%llx from private FD\n",
+					    __func__, gfn);
+			gfn++;
+			continue;
+		}
+
+		/*
+		 * TODO: The RMP entry's hugepage bit is ignored for
+		 * shared/unassigned pages. Either handle looping through each
+		 * sub-page as part of snp_make_page_shared(), or remove the
+		 * level argument.
+		 */
+		if (op == SNP_PAGE_STATE_PRIVATE && order &&
+		    IS_ALIGNED(gfn, 1 << order) && (gfn + (1 << order)) <= end) {
+			level = order_to_level(order);
+			npages = 1 << order;
+		}
+
+		/*
+		 * Grab the PFN from private memslot and update the RMP entry.
+		 * It may be worthwhile to go ahead and map it into the TDP at
+		 * this point if the guest is doing lazy acceptance, but for
+		 * up-front bulk shared->private conversions it's not likely
+		 * the guest will try to access the PFN any time soon, so for
+		 * now just take the let KVM MMU handle faulting it on the next
+		 * access.
+		 */
+		switch (op) {
+		case SNP_PAGE_STATE_SHARED:
+			rc = snp_make_page_shared(slot->kvm, gpa, pfn, level);
+			break;
+		case SNP_PAGE_STATE_PRIVATE:
+			rc = rmp_make_private(pfn, gpa, level, sev->asid, false);
+			break;
+		default:
+			rc = PSC_INVALID_ENTRY;
+			break;
+		}
+
+		put_page(pfn_to_page(pfn));
+
+		if (rc) {
+			pr_err_ratelimited("%s: failed op %d gpa %llx pfn %llx level %d rc %d\n",
+					   __func__, op, gpa, pfn, level, rc);
+			return -EINVAL;
+		}
+
+		gfn += npages;
+	}
+
+	return 0;
+}
arch/x86/kvm/svm/svm.c +1 -0
index 7f8c480dfa5e..6cf5b73f74c1 100644
@@ -4872,6 +4872,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 	.fault_is_private = sev_fault_is_private,
 
 	.rmp_page_level_adjust = sev_rmp_page_level_adjust,
+	.update_mem_attr = sev_update_mem_attr,
 };
 
 /*
arch/x86/kvm/svm/svm.h +2 -0
index 5e7cb0260dc3..5f315225ae4d 100644
@@ -741,6 +741,8 @@ int sev_post_map_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
 void sev_post_unmap_gfn(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn);
 void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
 void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);
+int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,
+			gfn_t start, gfn_t end);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
Signature · 1 line
2.25.1
#182 lore raw

RFC PATCH v7 56/64 KVM: x86/mmu: Generate KVM_EXIT_MEMORY_FAULT for implicit conversions for SNP

In reply to Michael Roth

SEV-SNP will set PFERR_GUEST_ENC_MASK for NPT faults for encrypted/private memory. Generally such accesses will be preceded at some point by a GHCB request to the hypervisor to put the page in the expected private/shared state, so the KVM MMU wouldn't normally need to generate KVM_EXIT_MEMORY_FAULTs to handle the updates at access time.

However, implicit conversions are also supported for SNP guests, and in those cases an KVM_EXIT_MEMORY_FAULT will be needed to put the page in the expected private/shared state.

Check for this PFERR_GUEST_ENC_MASK bit when determining whether a #NPF should be handled with restrictedmem pages or not.

---
 arch/x86/kvm/svm/sev.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +4 -1
index 885a3f1da910..0dd3d9debe48 100644
@@ -4446,7 +4446,10 @@ int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *priva
 	 * source is the only indicator of whether the fault should be treated
 	 * as private or not.
 	 */
-	*private_fault = kvm_mem_is_private(kvm, gfn);
+	if (sev_snp_guest(kvm))
+		*private_fault = (error_code & PFERR_GUEST_ENC_MASK) ? true : false;
+	else
+		*private_fault = kvm_mem_is_private(kvm, gfn);
 
 	return 1;
 
Signature · 1 line
2.25.1
#183 lore raw

RFC PATCH v7 57/64 KVM: SEV: Handle restricted memory invalidations for SNP

In reply to Michael Roth

Implement a platform hook to do the work of restoring the direct map entries and cleaning up RMP table entries for restricted memory that is being freed back to the host.

---
 arch/x86/kvm/svm/sev.c | 64 ++++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/svm/svm.c |  1 +
 arch/x86/kvm/svm/svm.h |  1 +
 3 files changed, 66 insertions(+)
arch/x86/kvm/svm/sev.c +64 -0
index 0dd3d9debe48..8783b64557e5 100644
@@ -4557,3 +4557,67 @@ int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,
 
 	return 0;
 }
+
+void sev_invalidate_private_range(struct kvm_memory_slot *slot, gfn_t start, gfn_t end)
+{
+	gfn_t gfn = start;
+
+	if (!sev_snp_guest(slot->kvm))
+		return;
+
+	if (!kvm_slot_can_be_private(slot)) {
+		pr_warn_ratelimited("SEV: Memslot for GFN: 0x%llx is not private.\n",
+				    gfn);
+		return;
+	}
+
+	while (gfn < end) {
+		gpa_t gpa = gfn_to_gpa(gfn);
+		int level = PG_LEVEL_4K;
+		int order, rc;
+		kvm_pfn_t pfn;
+
+		if (!kvm_mem_is_private(slot->kvm, gfn)) {
+			gfn++;
+			continue;
+		}
+
+		rc = kvm_restricted_mem_get_pfn(slot, gfn, &pfn, &order);
+		if (rc) {
+			pr_warn_ratelimited("SEV: Failed to retrieve restricted PFN for GFN 0x%llx, rc: %d\n",
+					    gfn, rc);
+			gfn++;
+			continue;
+		}
+
+		if (order) {
+			int rmp_level;
+
+			if (IS_ALIGNED(gpa, page_level_size(PG_LEVEL_2M)) &&
+			    gpa + page_level_size(PG_LEVEL_2M) <= gfn_to_gpa(end))
+				level = PG_LEVEL_2M;
+			else
+				pr_debug("%s: GPA 0x%llx is not aligned to 2M, skipping 2M directmap restoration\n",
+					 __func__, gpa);
+
+			/* TODO: It may still be possible to restore 2M mapping here, but keep it simple for now. */
+			if (level == PG_LEVEL_2M &&
+			    (!snp_lookup_rmpentry(pfn, &rmp_level) || rmp_level == PG_LEVEL_4K)) {
+				pr_debug("%s: PFN 0x%llx is not mapped as 2M private range, skipping 2M directmap restoration\n",
+					 __func__, pfn);
+				level = PG_LEVEL_4K;
+			}
+		}
+
+		pr_debug("%s: GPA %llx PFN %llx order %d level %d\n",
+			 __func__, gpa, pfn, order, level);
+		rc = snp_make_page_shared(slot->kvm, gpa, pfn, level);
+		if (rc)
+			pr_err("SEV: Failed to restore page to shared, GPA: 0x%llx PFN: 0x%llx order: %d rc: %d\n",
+			       gpa, pfn, order, rc);
+
+		gfn += page_level_size(level) >> PAGE_SHIFT;
+		put_page(pfn_to_page(pfn));
+		cond_resched();
+	}
+}
arch/x86/kvm/svm/svm.c +1 -0
index 6cf5b73f74c1..543261c87eb3 100644
@@ -4873,6 +4873,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 
 	.rmp_page_level_adjust = sev_rmp_page_level_adjust,
 	.update_mem_attr = sev_update_mem_attr,
+	.invalidate_restricted_mem = sev_invalidate_private_range,
 };
 
 /*
arch/x86/kvm/svm/svm.h +1 -0
index 5f315225ae4d..277f53c903c2 100644
@@ -743,6 +743,7 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
 void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);
 int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,
 			gfn_t start, gfn_t end);
+void sev_invalidate_private_range(struct kvm_memory_slot *slot, gfn_t start, gfn_t end);
 
 int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
 
Signature · 1 line
2.25.1
#184 lore raw

RFC PATCH v7 58/64 KVM: SVM: Add module parameter to enable the SEV-SNP

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported.

---
 arch/x86/kvm/svm/sev.c | 7 ++++---
 arch/x86/kvm/svm/svm.h | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +4 -3
index 8783b64557e5..b0f25ced7bcf 100644
@@ -54,14 +54,15 @@ module_param_named(sev, sev_enabled, bool, 0444);
 /* enable/disable SEV-ES support */
 static bool sev_es_enabled = true;
 module_param_named(sev_es, sev_es_enabled, bool, 0444);
+
+/* enable/disable SEV-SNP support */
+static bool sev_snp_enabled = true;
+module_param_named(sev_snp, sev_snp_enabled, bool, 0444);
 #else
 #define sev_enabled false
 #define sev_es_enabled false
 #endif /* CONFIG_KVM_AMD_SEV */
 
-/* enable/disable SEV-SNP support */
-static bool sev_snp_enabled;
-
 #define AP_RESET_HOLD_NONE		0
 #define AP_RESET_HOLD_NAE_EVENT		1
 #define AP_RESET_HOLD_MSR_PROTO		2
arch/x86/kvm/svm/svm.h +1 -1
index 277f53c903c2..4692ada13f02 100644
@@ -708,7 +708,7 @@ void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
 #define GHCB_VERSION_MAX	2ULL
 #define GHCB_VERSION_MIN	1ULL
 
-#define GHCB_HV_FT_SUPPORTED	0
+#define GHCB_HV_FT_SUPPORTED	(GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION)
 
 extern unsigned int max_sev_asid;
 
Signature · 1 line
2.25.1
#185 lore raw

RFC PATCH v7 59/64 ccp: Add support to decrypt the page

In reply to Michael Roth

From: Brijesh Singh <[email protected]>

Add support to decrypt guest encrypted memory. These API interfaces can be used for example to dump VMCBs on SNP guest exit.

[mdr: minor commit fixups]
---
 drivers/crypto/ccp/sev-dev.c | 33 +++++++++++++++++++++++++++++++++
 include/linux/psp-sev.h      | 21 ++++++++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +33 -0
index d4f13e5a8dde..052190bdb8a6 100644
@@ -2017,6 +2017,39 @@ int sev_guest_df_flush(int *error)
 }
 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
 
+int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error)
+{
+	struct sev_data_snp_dbg data = {0};
+	struct sev_device *sev;
+	int ret;
+
+	if (!psp_master || !psp_master->sev_data)
+		return -ENODEV;
+
+	sev = psp_master->sev_data;
+
+	if (!sev->snp_initialized)
+		return -EINVAL;
+
+	data.gctx_paddr = sme_me_mask | (gctx_pfn << PAGE_SHIFT);
+	data.src_addr = sme_me_mask | (src_pfn << PAGE_SHIFT);
+	data.dst_addr = sme_me_mask | (dst_pfn << PAGE_SHIFT);
+	data.len = PAGE_SIZE;
+
+	/* The destination page must be in the firmware state. */
+	if (rmp_mark_pages_firmware(data.dst_addr, 1, false))
+		return -EIO;
+
+	ret = sev_do_cmd(SEV_CMD_SNP_DBG_DECRYPT, &data, error);
+
+	/* Restore the page state */
+	if (snp_reclaim_pages(data.dst_addr, 1, false))
+		ret = -EIO;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt_page);
+
 int snp_guest_ext_guest_request(struct sev_data_snp_guest_request *data,
 				unsigned long vaddr, unsigned long *npages, unsigned long *fw_err)
 {
include/linux/psp-sev.h +20 -1
index 81bafc049eca..a1e6624540f3 100644
@@ -913,13 +913,27 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error);
  * @error: SEV command return code
  *
  * Returns:
+ * 0 if the sev successfully processed the command
+ * -%ENODEV    if the sev device is not available
+ * -%ENOTSUPP  if the sev does not support SEV
+ * -%ETIMEDOUT if the sev command timed out
+ * -%EIO       if the sev returned a non-zero return code
+ */
+int sev_do_cmd(int cmd, void *data, int *psp_ret);
+
+/**
+ * snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command
+ *
+ * @sev_ret: sev command return code
+ *
+ * Returns:
  * 0 if the SEV successfully processed the command
  * -%ENODEV    if the SEV device is not available
  * -%ENOTSUPP  if the SEV does not support SEV
  * -%ETIMEDOUT if the SEV command timed out
  * -%EIO       if the SEV returned a non-zero return code
  */
-int sev_do_cmd(int cmd, void *data, int *psp_ret);
+int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error);
 
 void *psp_copy_user_blob(u64 uaddr, u32 len);
 void *snp_alloc_firmware_page(gfp_t mask);
@@ -987,6 +1001,11 @@ static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_P
 
 void snp_mark_pages_offline(unsigned long pfn, unsigned int npages) {}
 
+static inline int snp_guest_dbg_decrypt_page(u64 gctx_pfn, u64 src_pfn, u64 dst_pfn, int *error)
+{
+	return -ENODEV;
+}
+
 static inline void *snp_alloc_firmware_page(gfp_t mask)
 {
 	return NULL;
Signature · 1 line
2.25.1
#186 lore raw

RFC PATCH v7 60/64 KVM: SVM: Sync the GHCB scratch buffer using already mapped ghcb

In reply to Michael Roth

From: Ashish Kalra <[email protected]>

Using kvm_write_guest() to sync the GHCB scratch buffer can fail due to host mapping being 2M, but RMP being 4K. The page fault handling in do_user_addr_fault() fails to split the 2M page to handle RMP fault due to it being called here in a non-preemptible context. Instead use the already kernel mapped ghcb to sync the scratch buffer when the scratch buffer is contained within the GHCB.

---
 arch/x86/kvm/svm/sev.c | 29 +++++++++++++++++++++--------
 arch/x86/kvm/svm/svm.h |  2 ++
 2 files changed, 23 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/sev.c +21 -8
index b0f25ced7bcf..4de952d1d446 100644
@@ -3036,6 +3036,24 @@ static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm)
 	ghcb_set_sw_exit_info_1(ghcb, svm->sev_es.ghcb_sw_exit_info_1);
 	ghcb_set_sw_exit_info_2(ghcb, svm->sev_es.ghcb_sw_exit_info_2);
 
+	/* Sync the scratch buffer area. */
+	if (svm->sev_es.ghcb_sa_sync) {
+		if (svm->sev_es.ghcb_sa_contained) {
+			memcpy(ghcb->shared_buffer + svm->sev_es.ghcb_sa_offset,
+			       svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len);
+		} else {
+			int ret;
+
+			ret = kvm_write_guest(svm->vcpu.kvm,
+					      svm->sev_es.ghcb_sa_gpa,
+					      svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len);
+			if (ret)
+				pr_warn_ratelimited("unmap_ghcb: kvm_write_guest failed while syncing scratch area, gpa: %llx, ret: %d\n",
+						    svm->sev_es.ghcb_sa_gpa, ret);
+		}
+		svm->sev_es.ghcb_sa_sync = false;
+	}
+
 	trace_kvm_vmgexit_exit(svm->vcpu.vcpu_id, ghcb);
 
 	svm_unmap_ghcb(svm, &map);
@@ -3248,14 +3266,6 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)
 	if (!svm->sev_es.ghcb_in_use)
 		return;
 
-	 /* Sync the scratch buffer area. */
-	if (svm->sev_es.ghcb_sa_sync) {
-		kvm_write_guest(svm->vcpu.kvm,
-				svm->sev_es.ghcb_sa_gpa,
-				svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len);
-		svm->sev_es.ghcb_sa_sync = false;
-	}
-
 	sev_es_sync_to_ghcb(svm);
 
 	svm->sev_es.ghcb_in_use = false;
@@ -3321,6 +3331,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 			       scratch_gpa_beg, scratch_gpa_end);
 			goto e_scratch;
 		}
+		svm->sev_es.ghcb_sa_contained = true;
+		svm->sev_es.ghcb_sa_offset = scratch_gpa_beg - ghcb_scratch_beg;
 	} else {
 		/*
 		 * The guest memory must be read into a kernel buffer, so
@@ -3331,6 +3343,7 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)
 			       len, GHCB_SCRATCH_AREA_LIMIT);
 			goto e_scratch;
 		}
+		svm->sev_es.ghcb_sa_contained = false;
 	}
 
 	if (svm->sev_es.ghcb_sa_alloc_len < len) {
arch/x86/kvm/svm/svm.h +2 -0
index 4692ada13f02..38aa579f6f70 100644
@@ -217,6 +217,8 @@ struct vcpu_sev_es_state {
 	u64 ghcb_sa_gpa;
 	u32 ghcb_sa_alloc_len;
 	bool ghcb_sa_sync;
+	bool ghcb_sa_contained;
+	u32 ghcb_sa_offset;
 
 	/*
 	 * SEV-ES support to hold the sw_exit_info return values to be
Signature · 1 line
2.25.1
#187 lore raw

RFC PATCH v7 61/64 KVM: SVM: Make VMSAVE target area memory allocation SNP safe

In reply to Michael Roth

From: Ashish Kalra <[email protected]>

Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of the VMSAVE target page.

When SEV-SNP is globally enabled, the CPU marks the VMSAVE target page as "InUse" while the VMSAVE instruction is executing. If another CPU writes to a different page in the same 2MB region while the VMSAVE is executing, the CPU will throw an RMP violation #PF.

Use the snp safe generic allocator for allocating the VMSA target page which will ensure that the page returned is not a hugepage, as it is already being used for the allocating the VMCB, VMSA and AVIC backing page.

---
 arch/x86/kvm/svm/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +1 -1
index 543261c87eb3..1d4a723d5def 100644
@@ -653,7 +653,7 @@ static int svm_cpu_init(int cpu)
 	int ret = -ENOMEM;
 
 	memset(sd, 0, sizeof(struct svm_cpu_data));
-	sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	sd->save_area = snp_safe_alloc_page(NULL);
 	if (!sd->save_area)
 		return ret;
 
Signature · 1 line
2.25.1
#188 lore raw

RFC PATCH v7 62/64 x86/sev: Add KVM commands for instance certs

In reply to Michael Roth

From: Dionna Glaze <[email protected]>

The /dev/sev device has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates.

This patch adds two new KVM ioctl commands: KVM_SEV_SNP_{GET,SET}_CERTS

The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel.

The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed.

Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request.

We also increase the SEV_FW_BLOB_MAX_SIZE another 4K page to allow space for an extra certificate.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 arch/x86/kvm/svm/sev.c   | 111 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.h   |   1 +
 include/linux/psp-sev.h  |   2 +-
 include/uapi/linux/kvm.h |  12 +++++
 4 files changed, 123 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +109 -2
index 4de952d1d446..d0e58cffd1ed 100644
@@ -2081,6 +2081,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
 		goto e_free;
 
 	sev->snp_certs_data = certs_data;
+	sev->snp_certs_len = 0;
 
 	return context;
 
@@ -2364,6 +2365,86 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_sev_snp_get_certs params;
+
+	if (!sev_snp_guest(kvm))
+		return -ENOTTY;
+
+	if (!sev->snp_context)
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			   sizeof(params)))
+		return -EFAULT;
+
+	/* No instance certs set. */
+	if (!sev->snp_certs_len)
+		return -ENOENT;
+
+	if (params.certs_len < sev->snp_certs_len) {
+		/* Output buffer too small. Return the required size. */
+		params.certs_len = sev->snp_certs_len;
+
+		if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
+				 sizeof(params)))
+			return -EFAULT;
+
+		return -EINVAL;
+	}
+
+	if (copy_to_user((void __user *)(uintptr_t)params.certs_uaddr,
+			 sev->snp_certs_data, sev->snp_certs_len))
+		return -EFAULT;
+
+	return 0;
+}
+
+static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	unsigned long length = SEV_FW_BLOB_MAX_SIZE;
+	void *to_certs = sev->snp_certs_data;
+	struct kvm_sev_snp_set_certs params;
+
+	if (!sev_snp_guest(kvm))
+		return -ENOTTY;
+
+	if (!sev->snp_context)
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			   sizeof(params)))
+		return -EFAULT;
+
+	if (params.certs_len > SEV_FW_BLOB_MAX_SIZE)
+		return -EINVAL;
+
+	/*
+	 * Setting a length of 0 is the same as "uninstalling" instance-
+	 * specific certificates.
+	 */
+	if (params.certs_len == 0) {
+		sev->snp_certs_len = 0;
+		return 0;
+	}
+
+	/* Page-align the length */
+	length = (params.certs_len + PAGE_SIZE - 1) & PAGE_MASK;
+
+	if (copy_from_user(to_certs,
+			   (void __user *)(uintptr_t)params.certs_uaddr,
+			   params.certs_len)) {
+		return -EFAULT;
+	}
+
+	sev->snp_certs_len = length;
+
+	return 0;
+}
+
 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;
@@ -2463,6 +2544,12 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 	case KVM_SEV_SNP_LAUNCH_FINISH:
 		r = snp_launch_finish(kvm, &sev_cmd);
 		break;
+	case KVM_SEV_SNP_GET_CERTS:
+		r = snp_get_instance_certs(kvm, &sev_cmd);
+		break;
+	case KVM_SEV_SNP_SET_CERTS:
+		r = snp_set_instance_certs(kvm, &sev_cmd);
+		break;
 	default:
 		r = -EINVAL;
 		goto out;
@@ -3575,8 +3662,28 @@ static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t req_gpa, gp
 	if (rc)
 		goto unlock;
 
-	rc = snp_guest_ext_guest_request(&req, (unsigned long)sev->snp_certs_data,
-					 &data_npages, &err);
+	/*
+	 * If the VMM has overridden the certs, then change the error message
+	 * if the size is inappropriate for the override. Otherwise, use a
+	 * regular guest request and copy back the instance certs.
+	 */
+	if (sev->snp_certs_len) {
+		if ((data_npages << PAGE_SHIFT) < sev->snp_certs_len) {
+			rc = -EINVAL;
+			err = SNP_GUEST_REQ_INVALID_LEN;
+			goto datalen;
+		}
+		rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &req,
+				   (int *)&err);
+	} else {
+		rc = snp_guest_ext_guest_request(&req,
+						 (unsigned long)sev->snp_certs_data,
+						 &data_npages, &err);
+	}
+datalen:
+	if (sev->snp_certs_len)
+		data_npages = sev->snp_certs_len >> PAGE_SHIFT;
+
 	if (rc) {
 		/*
 		 * If buffer length is small then return the expected
arch/x86/kvm/svm/svm.h +1 -0
index 38aa579f6f70..8d1ba66860a4 100644
@@ -102,6 +102,7 @@ struct kvm_sev_info {
 	void *snp_context;      /* SNP guest context page */
 	spinlock_t psc_lock;
 	void *snp_certs_data;
+	unsigned int snp_certs_len; /* Size of instance override for certs */
 	struct mutex guest_req_lock;
 
 	u64 sev_features;	/* Features set at VMSA creation */
include/linux/psp-sev.h +1 -1
index a1e6624540f3..970a9de0ed20 100644
@@ -22,7 +22,7 @@
 #define __psp_pa(x)	__pa(x)
 #endif
 
-#define SEV_FW_BLOB_MAX_SIZE	0x4000	/* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE	0x5000	/* 20KB */
 
 /**
  * SEV platform state
include/uapi/linux/kvm.h +12 -0
index 61b1e26ced01..48bcc59cf86b 100644
@@ -1949,6 +1949,8 @@ enum sev_cmd_id {
 	KVM_SEV_SNP_LAUNCH_START,
 	KVM_SEV_SNP_LAUNCH_UPDATE,
 	KVM_SEV_SNP_LAUNCH_FINISH,
+	KVM_SEV_SNP_GET_CERTS,
+	KVM_SEV_SNP_SET_CERTS,
 
 	KVM_SEV_NR_MAX,
 };
@@ -2096,6 +2098,16 @@ struct kvm_sev_snp_launch_finish {
 	__u8 pad[6];
 };
 
+struct kvm_sev_snp_get_certs {
+	__u64 certs_uaddr;
+	__u64 certs_len;
+};
+
+struct kvm_sev_snp_set_certs {
+	__u64 certs_uaddr;
+	__u64 certs_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
Signature · 1 line
2.25.1
#189 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Hi Dionna, Mike,

Quoted 121 lines · Michael Roth

From: Dionna Glaze <[email protected]>

The /dev/sev device has the ability to store host-wide certificates for the key used by the AMD-SP for SEV-SNP attestation report signing, but for hosts that want to specify additional certificates that are specific to the image launched in a VM, a different way is needed to communicate those certificates.

This patch adds two new KVM ioctl commands: KVM_SEV_SNP_{GET,SET}_CERTS

The certificates that are set with this command are expected to follow the same format as the host certificates, but that format is opaque to the kernel.

The new behavior for custom certificates is that the extended guest request command will now return the overridden certificates if they were installed for the instance. The error condition for a too small data buffer is changed to return the overridden certificate data size if there is an overridden certificate set installed.

Setting a 0 length certificate returns the system state to only return the host certificates on an extended guest request.

We also increase the SEV_FW_BLOB_MAX_SIZE another 4K page to allow space for an extra certificate.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 arch/x86/kvm/svm/sev.c   | 111 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.h   |   1 +
 include/linux/psp-sev.h  |   2 +-
 include/uapi/linux/kvm.h |  12 +++++
 4 files changed, 123 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +67 -0
index 4de952d1d446..d0e58cffd1ed 100644
@@ -2081,6 +2081,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp)
 		goto e_free;
 
 	sev->snp_certs_data = certs_data;
+	sev->snp_certs_len = 0;
 
 	return context;
 
@@ -2364,6 +2365,86 @@ static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	return ret;
 }
 
+static int snp_get_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	struct kvm_sev_snp_get_certs params;
+
+	if (!sev_snp_guest(kvm))
+		return -ENOTTY;
+
+	if (!sev->snp_context)
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			   sizeof(params)))
+		return -EFAULT;
+
+	/* No instance certs set. */
+	if (!sev->snp_certs_len)
+		return -ENOENT;
+
+	if (params.certs_len < sev->snp_certs_len) {
+		/* Output buffer too small. Return the required size. */
+		params.certs_len = sev->snp_certs_len;
+
+		if (copy_to_user((void __user *)(uintptr_t)argp->data, &params,
+				 sizeof(params)))
+			return -EFAULT;
+
+		return -EINVAL;
+	}
+
+	if (copy_to_user((void __user *)(uintptr_t)params.certs_uaddr,
+			 sev->snp_certs_data, sev->snp_certs_len))
+		return -EFAULT;
+
+	return 0;
+}
+
+static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+	struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+	unsigned long length = SEV_FW_BLOB_MAX_SIZE;
+	void *to_certs = sev->snp_certs_data;
+	struct kvm_sev_snp_set_certs params;
+
+	if (!sev_snp_guest(kvm))
+		return -ENOTTY;
+
+	if (!sev->snp_context)
+		return -EINVAL;
+
+	if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data,
+			   sizeof(params)))
+		return -EFAULT;
+
+	if (params.certs_len > SEV_FW_BLOB_MAX_SIZE)
+		return -EINVAL;
+
+	/*
+	 * Setting a length of 0 is the same as "uninstalling" instance-
+	 * specific certificates.
+	 */
+	if (params.certs_len == 0) {
+		sev->snp_certs_len = 0;
+		return 0;
+	}
+

arch/x86/kvm/svm/sev.c static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2443

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+	/* Page-align the length */
+	length = (params.certs_len + PAGE_SIZE - 1) & PAGE_MASK;
+
+	if (copy_from_user(to_certs,
+			   (void __user *)(uintptr_t)params.certs_uaddr,
+			   params.certs_len)) {
+		return -EFAULT;
+	}
+
+	sev->snp_certs_len = length;

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes. If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

Quoted 7 lines
arch/x86/kvm/svm/sev.c +4 -0
+
+	return 0;
+}
+
 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
 	struct kvm_sev_cmd sev_cmd;

[...]

include/linux/psp-sev.h line 26

Quoted 11 lines
include/linux/psp-sev.h +1 -1
index a1e6624540f3..970a9de0ed20 100644
@@ -22,7 +22,7 @@
 #define __psp_pa(x)	__pa(x)
 #endif
 
-#define SEV_FW_BLOB_MAX_SIZE	0x4000	/* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE	0x5000	/* 20KB */
 

This has effects in drivers/crypto/ccp/sev-dev.c
(for
example in alloc_snp_host_map). Is that OK?

-Dov

Quoted 32 lines
include/linux/psp-sev.h +0 -0
 /**
  * SEV platform state
include/uapi/linux/kvm.h +12 -0
index 61b1e26ced01..48bcc59cf86b 100644
@@ -1949,6 +1949,8 @@ enum sev_cmd_id {
 	KVM_SEV_SNP_LAUNCH_START,
 	KVM_SEV_SNP_LAUNCH_UPDATE,
 	KVM_SEV_SNP_LAUNCH_FINISH,
+	KVM_SEV_SNP_GET_CERTS,
+	KVM_SEV_SNP_SET_CERTS,
 
 	KVM_SEV_NR_MAX,
 };
@@ -2096,6 +2098,16 @@ struct kvm_sev_snp_launch_finish {
 	__u8 pad[6];
 };
 
+struct kvm_sev_snp_get_certs {
+	__u64 certs_uaddr;
+	__u64 certs_len;
+};
+
+struct kvm_sev_snp_set_certs {
+	__u64 certs_uaddr;
+	__u64 certs_len;
+};
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
#190 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Quoted 18 lines

arch/x86/kvm/svm/sev.c static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2406

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes. If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 5 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

Quoted 10 lines

[...]

include/linux/psp-sev.h line 25

Quoted 4 lines
include/linux/psp-sev.h +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
(for
example in alloc_snp_host_map). Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

Signature · 1 line
-Dionna Glaze, PhD (she/her)
maintainer
#191 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Quoted 35 lines · Dionna Amalie Glaze
Quoted 18 lines

arch/x86/kvm/svm/sev.c static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2406

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes. If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 5 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

Thanks,
Tom

Quoted 20 lines
Quoted 11 lines

[...]

include/linux/psp-sev.h line 25

Quoted 4 lines
include/linux/psp-sev.h +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
(for
example in alloc_snp_host_map). Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

#192 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Hi Tom,

Quoted 37 lines · Tom Lendacky
Quoted 36 lines · Dionna Amalie Glaze
Quoted 19 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct

kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes.  If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 5 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

Quoted 7 lines
Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

I looked at AMD publication 56421 SEV-ES Guest-Hypervisor Communication Block Standardization (July 2022), page 37. The table says:

--------------

NAE Event: SNP Extended Guest Request

Notes:

RAX will have the guest physical address of the page(s) to hold returned data

RBX
State to Hypervisor: will contain the number of guest contiguous pages supplied to hold returned data
State from Hypervisor: on error will contain the number of guest contiguous pages required to hold the data to be returned

...

The request page, response page and data page(s) must be assigned to the hypervisor (shared).

--------------

According to this spec, it looks like the sizes are communicated as number of pages in RBX. So the data should start at a 4KB alignment (this is verified in snp_handle_ext_guest_request()) and its length should be 4KB-aligned, as Dionna noted.

I see no reason (in the spec and in the kernel code) for the data length to be limited to 16KB (SEV_FW_BLOB_MAX_SIZE) but I might be missing some flow because Dionna ran into this limit.

-Dov

Quoted 23 lines

Thanks,
Tom

Quoted 20 lines
Quoted 11 lines

[...]

include/linux/psp-sev.h line 25

Quoted 4 lines
include/linux/psp-sev.h +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
                                                                (for example in alloc_snp_host_map).  Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

maintainer
#193 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Quoted 71 lines · Dov Murik

Hi Tom,

Quoted 37 lines · Tom Lendacky
Quoted 36 lines · Dionna Amalie Glaze
Quoted 19 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct

kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes.  If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 5 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

Quoted 7 lines
Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

I looked at AMD publication 56421 SEV-ES Guest-Hypervisor Communication Block Standardization (July 2022), page 37. The table says:

--------------

NAE Event: SNP Extended Guest Request

Notes:

RAX will have the guest physical address of the page(s) to hold returned data

RBX
State to Hypervisor: will contain the number of guest contiguous pages supplied to hold returned data
State from Hypervisor: on error will contain the number of guest contiguous pages required to hold the data to be returned

...

The request page, response page and data page(s) must be assigned to the hypervisor (shared).

Quoted 7 lines

--------------

According to this spec, it looks like the sizes are communicated as number of pages in RBX. So the data should start at a 4KB alignment (this is verified in snp_handle_ext_guest_request()) and its length should be 4KB-aligned, as Dionna noted.

That only indicates how many pages are required to hold the data, but the hypervisor only has to copy however much data is present. If the data is 20 bytes, then you only have to copy 20 bytes. If the user supplied 0 for the number of pages, then the code returns 1 in RBX to indicate that one page is required to hold the 20 bytes.

Quoted 4 lines

I see no reason (in the spec and in the kernel code) for the data length to be limited to 16KB (SEV_FW_BLOB_MAX_SIZE) but I might be missing some flow because Dionna ran into this limit.

Correct, there is no limit. I believe that SEV_FW_BLOB_MAX_SIZE is a way to keep the memory usage controlled because data is coming from userspace and it isn't expected that the data would be larger than that.

I'm not sure if that was in from the start or as a result of a review comment. Not sure what is the best approach is.

Thanks,
Tom

Quoted 29 lines

-Dov

Quoted 23 lines

Thanks,
Tom

Quoted 20 lines
Quoted 11 lines

[...]

include/linux/psp-sev.h line 25

Quoted 4 lines
include/linux/psp-sev.h +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
                                                                (for
example in alloc_snp_host_map).  Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

#194 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Quoted 92 lines · Tom Lendacky
Quoted 71 lines · Dov Murik

Hi Tom,

Quoted 37 lines · Tom Lendacky
Quoted 36 lines · Dionna Amalie Glaze
Quoted 19 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct

kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes. If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 5 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

Quoted 7 lines
Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

I looked at AMD publication 56421 SEV-ES Guest-Hypervisor Communication Block Standardization (July 2022), page 37. The table says:

--------------

NAE Event: SNP Extended Guest Request

Notes:

RAX will have the guest physical address of the page(s) to hold returned data

RBX
State to Hypervisor: will contain the number of guest contiguous pages supplied to hold returned data
State from Hypervisor: on error will contain the number of guest contiguous pages required to hold the data to be returned

...

The request page, response page and data page(s) must be assigned to the hypervisor (shared).

Quoted 7 lines

--------------

According to this spec, it looks like the sizes are communicated as number of pages in RBX. So the data should start at a 4KB alignment (this is verified in snp_handle_ext_guest_request()) and its length should be 4KB-aligned, as Dionna noted.

That only indicates how many pages are required to hold the data, but the hypervisor only has to copy however much data is present. If the data is 20 bytes, then you only have to copy 20 bytes. If the user supplied 0 for the number of pages, then the code returns 1 in RBX to indicate that one page is required to hold the 20 bytes.

Quoted 4 lines

I see no reason (in the spec and in the kernel code) for the data length to be limited to 16KB (SEV_FW_BLOB_MAX_SIZE) but I might be missing some flow because Dionna ran into this limit.

Quoted 6 lines

Correct, there is no limit. I believe that SEV_FW_BLOB_MAX_SIZE is a way to keep the memory usage controlled because data is coming from userspace and it isn't expected that the data would be larger than that.

I'm not sure if that was in from the start or as a result of a review comment. Not sure what is the best approach is.

This was discussed a bit in the guest driver changes recently too that SEV_FW_BLOB_MAX_SIZE is used in the guest driver code for the max cert length. We discussed increasing the limit there after fixing the IV reuse issue.

Maybe we could introduce SEV_CERT_BLOB_MAX_SIZE here to be more clear there is no firmware based limit? Then we could switch the guest driver to use that too. Dionna confirmed 4 pages is enough for our current usecase, Dov would you recommend something larger to start?

Quoted 33 lines

Thanks,
Tom

Quoted 29 lines

-Dov

Quoted 23 lines

Thanks,
Tom

Quoted 20 lines
Quoted 11 lines

[...]

include/linux/psp-sev.h line 25

Quoted 4 lines
include/linux/psp-sev.h +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
(for
example in alloc_snp_host_map). Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

#195 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Hi Peter,

Quoted 97 lines · Peter Gonda
Quoted 96 lines · Tom Lendacky
Quoted 71 lines · Dov Murik

Hi Tom,

Quoted 37 lines · Tom Lendacky
Quoted 36 lines · Dionna Amalie Glaze
Quoted 19 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct

kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes. If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 5 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer is never sent to the PSP.

Quoted 7 lines
Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

I looked at AMD publication 56421 SEV-ES Guest-Hypervisor Communication Block Standardization (July 2022), page 37. The table says:

--------------

NAE Event: SNP Extended Guest Request

Notes:

RAX will have the guest physical address of the page(s) to hold returned data

RBX
State to Hypervisor: will contain the number of guest contiguous pages supplied to hold returned data
State from Hypervisor: on error will contain the number of guest contiguous pages required to hold the data to be returned

...

The request page, response page and data page(s) must be assigned to the hypervisor (shared).

Quoted 7 lines

--------------

According to this spec, it looks like the sizes are communicated as number of pages in RBX. So the data should start at a 4KB alignment (this is verified in snp_handle_ext_guest_request()) and its length should be 4KB-aligned, as Dionna noted.

That only indicates how many pages are required to hold the data, but the hypervisor only has to copy however much data is present. If the data is 20 bytes, then you only have to copy 20 bytes. If the user supplied 0 for the number of pages, then the code returns 1 in RBX to indicate that one page is required to hold the 20 bytes.

Quoted 4 lines

I see no reason (in the spec and in the kernel code) for the data length to be limited to 16KB (SEV_FW_BLOB_MAX_SIZE) but I might be missing some flow because Dionna ran into this limit.

Correct, there is no limit. I believe that SEV_FW_BLOB_MAX_SIZE is a way to keep the memory usage controlled because data is coming from userspace and it isn't expected that the data would be larger than that.

Quoted 7 lines
Quoted 2 lines

I'm not sure if that was in from the start or as a result of a review comment. Not sure what is the best approach is.

This was discussed a bit in the guest driver changes recently too that SEV_FW_BLOB_MAX_SIZE is used in the guest driver code for the max cert length. We discussed increasing the limit there after fixing the IV reuse issue.

I see it now.

(Joerg, maybe we should add F:drivers/virt/coco/ to the MAINTAINERS list so that patches there are hopefully sent to linux-coco?)

Quoted 6 lines

Maybe we could introduce SEV_CERT_BLOB_MAX_SIZE here to be more clear there is no firmware based limit? Then we could switch the guest driver to use that too. Dionna confirmed 4 pages is enough for our current usecase, Dov would you recommend something larger to start?

Introducing a new constant sounds good to me (and use the same constant in the guest driver).

I think 4 pages are OK; I also don't see real harm in increasing this limit to 1 MB (if the host+guest agree to pass more stuff there, besides certificates). But maybe that's just abusing this channel, and for other data we should use other mechanisms (like vsock).

-Dov

#196 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

In reply to Tom Lendacky
Quoted 83 lines · Tom Lendacky
Quoted 74 lines · Dov Murik

Hi Tom,

Quoted 40 lines · Tom Lendacky
Quoted 39 lines · Dionna Amalie Glaze
Quoted 21 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct

kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes.  If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 6 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer
is never sent to the PSP.

Quoted 7 lines
Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

I looked at AMD publication 56421 SEV-ES Guest-Hypervisor Communication Block Standardization (July 2022), page 37.  The table says:

--------------

NAE Event: SNP Extended Guest Request

Notes:

RAX will have the guest physical address of the page(s) to hold returned data

RBX
State to Hypervisor: will contain the number of guest contiguous pages supplied to hold returned data
State from Hypervisor: on error will contain the number of guest contiguous pages required to hold the data to be returned

...

The request page, response page and data page(s) must be assigned to the hypervisor (shared).

Quoted 7 lines

--------------

According to this spec, it looks like the sizes are communicated as number of pages in RBX.  So the data should start at a 4KB alignment (this is verified in snp_handle_ext_guest_request()) and its length should be 4KB-aligned, as Dionna noted.

Quoted 6 lines

That only indicates how many pages are required to hold the data, but the hypervisor only has to copy however much data is present. If the data is 20 bytes, then you only have to copy 20 bytes. If the user supplied 0 for the number of pages, then the code returns 1 in RBX to indicate that one page is required to hold the 20 bytes.

Maybe it should only copy 20 bytes, but current implementation copies whole 4KB pages:

        if (sev->snp_certs_len)
                data_npages = sev->snp_certs_len >> PAGE_SHIFT;
        ...
        ...
        /* Copy the certificate blob in the guest memory */
        if (data_npages &&
            kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT))
                rc = SEV_RET_INVALID_ADDRESS;

(elsewhere we ensure that sev->snp_certs_len is page-aligned, so the assignment to data_npages is in fact correct even though looks off-by-one; aside, maybe it's better to use some DIV_ROUND_UP macro anywhere we calculate the number of needed pages.)

Also -- how does the guest know they got only 20 bytes and not 4096? Do they have to read all the 'struct cert_table' entries at the beginning of the received data?

-Dov

Quoted 44 lines
Quoted 4 lines

I see no reason (in the spec and in the kernel code) for the data length to be limited to 16KB (SEV_FW_BLOB_MAX_SIZE) but I might be missing some flow because Dionna ran into this limit.

Correct, there is no limit. I believe that SEV_FW_BLOB_MAX_SIZE is a way to keep the memory usage controlled because data is coming from userspace and it isn't expected that the data would be larger than that.

I'm not sure if that was in from the start or as a result of a review comment. Not sure what is the best approach is.

Thanks,
Tom

Quoted 29 lines

-Dov

Quoted 23 lines

Thanks,
Tom

Quoted 20 lines
Quoted 11 lines

[...]

include/linux/psp-sev.h line 25

Quoted 4 lines
include/linux/psp-sev.h +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
                                                                 (for example in alloc_snp_host_map).  Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

maintainer
#197 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Quoted 108 lines · Dov Murik
Quoted 83 lines · Tom Lendacky
Quoted 74 lines · Dov Murik

Hi Tom,

Quoted 40 lines · Tom Lendacky
Quoted 39 lines · Dionna Amalie Glaze
Quoted 21 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+static int snp_set_instance_certs(struct kvm *kvm, struct

kvm_sev_cmd *argp)
+{

[...]

Here we set the length to the page-aligned value, but we copy only params.cert_len bytes.  If there are two subsequent snp_set_instance_certs() calls where the second one has a shorter length, we might "keep" some leftover bytes from the first call.

Consider:
1. snp_set_instance_certs(certs_addr point to "AAA...", certs_len=8192)
2. snp_set_instance_certs(certs_addr point to "BBB...", certs_len=4097)

If I understand correctly, on the second call we'll copy 4097 "BBB..." bytes into the to_certs buffer, but length will be (4096 + PAGE_SIZE -
1) & PAGE_MASK which will be 8192.

Quoted 6 lines

Later when fetching the certs (for the extended report or in snp_get_instance_certs()) the user will get a buffer of 8192 bytes filled with 4097 BBBs and 4095 leftover AAAs.

Maybe zero sev->snp_certs_data entirely before writing to it?

Yes, I agree it should be zeroed, at least if the previous length is greater than the new length. Good catch.

Quoted 6 lines

Related question (not only for this patch) regarding snp_certs_data (host or per-instance): why is its size page-aligned at all? why is it limited by 16KB or 20KB? If I understand correctly, for SNP, this buffer
is never sent to the PSP.

Quoted 7 lines
Quoted 3 lines

The buffer is meant to be copied into the guest driver following the GHCB extended guest request protocol. The data to copy back are expected to be in 4K page granularity.

I don't think the data has to be in 4K page granularity. Why do you think it does?

I looked at AMD publication 56421 SEV-ES Guest-Hypervisor Communication Block Standardization (July 2022), page 37.  The table says:

--------------

NAE Event: SNP Extended Guest Request

Notes:

RAX will have the guest physical address of the page(s) to hold returned data

RBX
State to Hypervisor: will contain the number of guest contiguous pages supplied to hold returned data
State from Hypervisor: on error will contain the number of guest contiguous pages required to hold the data to be returned

...

The request page, response page and data page(s) must be assigned to the hypervisor (shared).

Quoted 7 lines

--------------

According to this spec, it looks like the sizes are communicated as number of pages in RBX.  So the data should start at a 4KB alignment (this is verified in snp_handle_ext_guest_request()) and its length should be 4KB-aligned, as Dionna noted.

Quoted 6 lines

That only indicates how many pages are required to hold the data, but the hypervisor only has to copy however much data is present. If the data is 20 bytes, then you only have to copy 20 bytes. If the user supplied 0 for the number of pages, then the code returns 1 in RBX to indicate that one page is required to hold the 20 bytes.

Maybe it should only copy 20 bytes, but current implementation copies whole 4KB pages:

         if (sev->snp_certs_len)
                 data_npages = sev->snp_certs_len >> PAGE_SHIFT;
         ...
         ...
         /* Copy the certificate blob in the guest memory */
         if (data_npages &&
             kvm_write_guest(kvm, data_gpa, sev->snp_certs_data, data_npages << PAGE_SHIFT))
                 rc = SEV_RET_INVALID_ADDRESS;
Quoted 4 lines

(elsewhere we ensure that sev->snp_certs_len is page-aligned, so the assignment to data_npages is in fact correct even though looks off-by-one; aside, maybe it's better to use some DIV_ROUND_UP macro anywhere we calculate the number of needed pages.)

Hmmm... yeah, not sure why it was implemented that way, I guess it can always be changed later if desired.

Quoted 3 lines

Also -- how does the guest know they got only 20 bytes and not 4096? Do they have to read all the 'struct cert_table' entries at the beginning of the received data?

Yes, they should walk the cert table entries.

Thanks,
Tom

Quoted 48 lines

-Dov

Quoted 44 lines
Quoted 4 lines

I see no reason (in the spec and in the kernel code) for the data length to be limited to 16KB (SEV_FW_BLOB_MAX_SIZE) but I might be missing some flow because Dionna ran into this limit.

Correct, there is no limit. I believe that SEV_FW_BLOB_MAX_SIZE is a way to keep the memory usage controlled because data is coming from userspace and it isn't expected that the data would be larger than that.

I'm not sure if that was in from the start or as a result of a review comment. Not sure what is the best approach is.

Thanks,
Tom

Quoted 29 lines

-Dov

Quoted 23 lines

Thanks,
Tom

Quoted 20 lines
Quoted 11 lines

[...]

Review

Quoted 4 lines
(quoted diff) +1 -1
-#define SEV_FW_BLOB_MAX_SIZE 0x4000  /* 16KB */
+#define SEV_FW_BLOB_MAX_SIZE 0x5000  /* 20KB */

This has effects in drivers/crypto/ccp/sev-dev.c
                                                                 (for
example in alloc_snp_host_map).  Is that OK?

No, this was a mistake of mine because I was using a bloated data encoding that needed 5 pages for the GUID table plus 4 small certificates. I've since fixed that in our user space code. We shouldn't change this size and instead wait for a better size negotiation protocol between the guest and host to avoid this awkward hard-coding.

#198 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

In reply to Michael Roth

arch/x86/kvm/svm/sev.c static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2436

Quoted 4 lines
arch/x86/kvm/svm/sev.c +4 -0
+
+       /* Page-align the length */
+       length = (params.certs_len + PAGE_SIZE - 1) & PAGE_MASK;
+

I believe Ashish wanted this to be PAGE_ALIGN(params.certs_len)

Signature · 1 line
-Dionna Glaze, PhD (she/her)
maintainer
#199 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Hello Dionna,

Do you also have other updates to this patch with regard to review comments from Dov ?

Thanks,
Ashish

Quoted 7 lines · Dionna Amalie Glaze

arch/x86/kvm/svm/sev.c static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2436

Quoted 4 lines
arch/x86/kvm/svm/sev.c +4 -0
+
+       /* Page-align the length */
+       length = (params.certs_len + PAGE_SIZE - 1) & PAGE_MASK;
+

I believe Ashish wanted this to be PAGE_ALIGN(params.certs_len)

#200 lore raw

Re: [PATCH RFC v7 62/64] x86/sev: Add KVM commands for instance certs

Quoted 6 lines · Ashish

Hello Dionna,

Do you also have other updates to this patch with regard to review comments from Dov ?

Apart from the PAGE_ALIGN change, the result of the whole discussion appears to only need the following immediately before the copy_from_user of certs_uaddr in the snp_set_instance_certs function:

/* The size could shrink and leave garbage at the end. */ memset(sev->snp_certs_data, 0, SEV_FW_BLOB_MAX_SIZE);

I don't believe there is an off-by-one with the page shifting for the number of pages because snp_certs_len is already rounded up to the nearest page size. Any other change wrt the way the blob size is decided between the guest and host should come later.

Signature · 1 line
-Dionna Glaze, PhD (she/her)
#201 lore raw

RFC PATCH v7 63/64 x86/sev: Document KVM_SEV_SNP_{G,S}ET_CERTS

In reply to Michael Roth

From: Dionna Glaze <[email protected]>

Update the KVM_MEMORY_ENCRYPT_OP documentation to include the new commands for overriding the host certificates that the guest receives from an extended guest request.

Cc Thomas Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 .../virt/kvm/x86/amd-memory-encryption.rst    | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
Documentation/virt/kvm/x86/amd-memory-encryption.rst +44 -0
index e4b42aaab1de..3cfe7b7cef61 100644
@@ -536,6 +536,50 @@ Returns: 0 on success, -negative on error
 
 See SEV-SNP specification for further details on launch finish input parameters.
 
+22. KVM_SEV_SNP_GET_CERTS
+-------------------------
+
+After the SNP guest launch flow has started, the KVM_SEV_SNP_GET_CERTS command
+can be issued to request the data that has been installed with the
+KVM_SEV_SNP_SET_CERTS command.
+
+Parameters (in/out): struct kvm_sev_snp_get_certs
+
+Returns: 0 on success, -negative on error
+
+::
+
+	struct kvm_sev_snp_get_certs {
+		__u64 certs_uaddr;
+		__u64 certs_len
+	};
+
+If no certs have been installed, then the return value is -ENOENT.
+If the buffer specified in the struct is too small, the certs_len field will be
+overwritten with the required bytes to receive all the certificate bytes and the
+return value will be -EINVAL.
+
+23. KVM_SEV_SNP_SET_CERTS
+-------------------------
+
+After the SNP guest launch flow has started, the KVM_SEV_SNP_SET_CERTS command
+can be issued to override the /dev/sev certs data that is returned when a
+guest issues an extended guest request. This is useful for instance-specific
+extensions to the host certificates.
+
+Parameters (in/out): struct kvm_sev_snp_set_certs
+
+Returns: 0 on success, -negative on error
+
+::
+
+	struct kvm_sev_snp_set_certs {
+		__u64 certs_uaddr;
+		__u64 certs_len
+	};
+
+The certs_len field may not exceed SEV_FW_BLOB_MAX_SIZE.
+
 References
 ==========
 
Signature · 1 line
2.25.1
#202 lore raw

RFC PATCH v7 64/64 iommu/amd: Add IOMMU_SNP_SHUTDOWN support

In reply to Michael Roth

From: Ashish Kalra <[email protected]>

Add a new IOMMU API interface amd_iommu_snp_disable() to transition IOMMU pages to Hypervisor state from Reclaim state after SNP_SHUTDOWN_EX command. Invoke this API from the CCP driver after SNP_SHUTDOWN_EX command.

---
 drivers/crypto/ccp/sev-dev.c | 20 ++++++++++++++
 drivers/iommu/amd/init.c     | 53 ++++++++++++++++++++++++++++++++++++
 include/linux/amd-iommu.h    |  1 +
 3 files changed, 74 insertions(+)
drivers/crypto/ccp/sev-dev.c +20 -0
index 052190bdb8a6..6c4fdcaed72b 100644
@@ -24,6 +24,7 @@
 #include <linux/cpufeature.h>
 #include <linux/fs.h>
 #include <linux/fs_struct.h>
+#include <linux/amd-iommu.h>
 
 #include <asm/smp.h>
 #include <asm/e820/types.h>
@@ -1503,6 +1504,25 @@ static int __sev_snp_shutdown_locked(int *error)
 		return ret;
 	}
 
+	/*
+	 * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP
+	 * enforcement by the IOMMU and also transitions all pages
+	 * associated with the IOMMU to the Reclaim state.
+	 * Firmware was transitioning the IOMMU pages to Hypervisor state
+	 * before version 1.53. But, accounting for the number of assigned
+	 * 4kB pages in a 2M page was done incorrectly by not transitioning
+	 * to the Reclaim state. This resulted in RMP #PF when later accessing
+	 * the 2M page containing those pages during kexec boot. Hence, the
+	 * firmware now transitions these pages to Reclaim state and hypervisor
+	 * needs to transition these pages to shared state. SNP Firmware
+	 * version 1.53 and above are needed for kexec boot.
+	 */
+	ret = amd_iommu_snp_disable();
+	if (ret) {
+		dev_err(sev->dev, "SNP IOMMU shutdown failed\n");
+		return ret;
+	}
+
 	sev->snp_initialized = false;
 	dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n");
 
drivers/iommu/amd/init.c +53 -0

63 lines not on this page · raw message

include/linux/amd-iommu.h +1 -0
index 953e6f12fa1c..a1b33b838842 100644
@@ -208,6 +208,7 @@ struct amd_iommu *get_amd_iommu(unsigned int idx);
 
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 int amd_iommu_snp_enable(void);
+int amd_iommu_snp_disable(void);
 #endif
 
 #endif /* _ASM_X86_AMD_IOMMU_H */
Signature · 1 line
2.25.1
maintainer
#203 lore raw
In reply to Michael Roth
Quoted 4 lines · Michael Roth

This patchset is based on top of the following patchset:

"[PATCH v10 0/9] KVM: mm: fd-based approach for supporting KVM"
https://lore.kernel.org/lkml/[email protected]/T/#me1dd3a4c295758b4e4ac8ff600f2db055bc5f987

Well, not quite.

There's also this thing which is stuck in there:

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

and I would appreciate reading that in the 0th message so that I don't scratch my head over why don't those patches apply and what else is missing...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#204 lore raw
Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Michael Roth

This patchset is based on top of the following patchset:

"[PATCH v10 0/9] KVM: mm: fd-based approach for supporting KVM"
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2F20221202061347.1070246-1-chao.p.peng%40linux.intel.com%2FT%2F%23me1dd3a4c295758b4e4ac8ff600f2db055bc5f987&data=05%7C01%7Cmichael.roth%40amd.com%7Ce778b913794d41ca7a1b08dae524f21c%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C638074244202143178%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=rhjtuPIYWrTN%2FtPHOb2HM5GGZ5cgHRCGVoqu8N1f7XY%3D&reserved=0

Well, not quite.

Quoted 7 lines

There's also this thing which is stuck in there:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fr%2F20221205232341.4131240-1-vannapurve%40google.com&data=05%7C01%7Cmichael.roth%40amd.com%7Ce778b913794d41ca7a1b08dae524f21c%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C638074244202143178%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=L20ayS1IDUCuOjrs2HzVApzf5%2BmW48PhcsnZprn1RIM%3D&reserved=0

and I would appreciate reading that in the 0th message so that I don't scratch my head over why don't those patches apply and what else is missing...

That's correct, sorry for the confusion. With UPM v9 those tests were included on top so when I ported to v10 I failed to recall it was now a separate patchset I'd added on top.

-Mike

Quoted 6 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpeople.kernel.org%2Ftglx%2Fnotes-about-netiquette&data=05%7C01%7Cmichael.roth%40amd.com%7Ce778b913794d41ca7a1b08dae524f21c%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C638074244202143178%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=q4TNZvaptVOEPnx%2B6FJpg64v%2BUya14kO6PX1U422JXE%3D&reserved=0