sgx: Reset the vEPC regions during VM reboot

For bare-metal SGX on real hardware, the hardware provides guarantees
SGX state at reboot.  For instance, all pages start out uninitialized.
The vepc driver provides a similar guarantee today for freshly-opened
vepc instances, but guests such as Windows expect all pages to be in
uninitialized state on startup, including after every guest reboot.

Qemu can invoke the ioctl to bring its vEPC pages back to uninitialized
state. There is a possibility that some pages fail to be removed if they
are SECS pages, and the child and SECS pages could be in separate vEPC
regions.  Therefore, the ioctl returns the number of EREMOVE failures,
telling Qemu to try the ioctl again after it's done with all vEPC regions.

The related kernel patches:
Link: https://lkml.kernel.org/r/20211021201155.1523989-3-pbonzini@redhat.com

Signed-off-by: Yang Zhong <yang.zhong@intel.com>
Message-Id: <20211101162009.62161-6-yang.zhong@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Yang Zhong 2021-11-01 12:20:09 -04:00 committed by Paolo Bonzini
parent bd989ed44f
commit 2c3132279b
1 changed files with 50 additions and 0 deletions

View File

@ -21,6 +21,8 @@
#include "qapi/qapi-commands-misc-target.h"
#include "exec/address-spaces.h"
#include "sysemu/hw_accel.h"
#include "sysemu/reset.h"
#include <sys/ioctl.h>
#define SGX_MAX_EPC_SECTIONS 8
#define SGX_CPUID_EPC_INVALID 0x0
@ -29,6 +31,11 @@
#define SGX_CPUID_EPC_SECTION 0x1
#define SGX_CPUID_EPC_MASK 0xF
#define SGX_MAGIC 0xA4
#define SGX_IOC_VEPC_REMOVE_ALL _IO(SGX_MAGIC, 0x04)
#define RETRY_NUM 2
static uint64_t sgx_calc_section_metric(uint64_t low, uint64_t high)
{
return (low & MAKE_64BIT_MASK(12, 20)) +
@ -59,6 +66,46 @@ static uint64_t sgx_calc_host_epc_section_size(void)
return size;
}
static void sgx_epc_reset(void *opaque)
{
PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
HostMemoryBackend *hostmem;
SGXEPCDevice *epc;
int failures;
int fd, i, j, r;
static bool warned = false;
/*
* The second pass is needed to remove SECS pages that could not
* be removed during the first.
*/
for (i = 0; i < RETRY_NUM; i++) {
failures = 0;
for (j = 0; j < pcms->sgx_epc.nr_sections; j++) {
epc = pcms->sgx_epc.sections[j];
hostmem = MEMORY_BACKEND(epc->hostmem);
fd = memory_region_get_fd(host_memory_backend_get_memory(hostmem));
r = ioctl(fd, SGX_IOC_VEPC_REMOVE_ALL);
if (r == -ENOTTY && !warned) {
warned = true;
warn_report("kernel does not support SGX_IOC_VEPC_REMOVE_ALL");
warn_report("SGX might operate incorrectly in the guest after reset");
break;
} else if (r > 0) {
/* SECS pages remain */
failures++;
if (i == 1) {
error_report("cannot reset vEPC section %d", j);
}
}
}
if (!failures) {
break;
}
}
}
SGXInfo *qmp_query_sgx_capabilities(Error **errp)
{
SGXInfo *info = NULL;
@ -190,4 +237,7 @@ void pc_machine_init_sgx_epc(PCMachineState *pcms)
}
memory_region_set_size(&sgx_epc->mr, sgx_epc->size);
/* register the reset callback for sgx epc */
qemu_register_reset(sgx_epc_reset, NULL);
}