Skip to contentSkip to conversation

x86: Secure Encrypted Virtualization (AMD)

PATCH In review Brijesh Singh 5 participants 57 messages Mar 8, 2018 – Mar 13, 2018 KVM

lore
Share & tools
lore
#0 lore raw

v12 cover x86: Secure Encrypted Virtualization (AMD)

This patch series provides support for AMD's new Secure Encrypted Virtualization (SEV) feature.

SEV is an extension to the AMD-V architecture which supports running multiple VMs under the control of a hypervisor. The SEV feature allows the memory contents of a virtual machine (VM) to be transparently encrypted with a key unique to the guest VM. The memory controller contains a high performance encryption engine which can be programmed with multiple keys for use by a different VMs in the system. The programming and management of these keys is handled by the AMD Secure Processor firmware which exposes a commands for these tasks.

The KVM SEV patch series introduced a new ioctl (KVM_MEMORY_ENCRYPTION_OP) which is used by qemu to issue the SEV commands to assist performing common hypervisor activities such as a launching, running, snapshooting, migration and debugging guests.

The following links provide additional details:

AMD Memory Encryption whitepaper:

http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf

AMD64 Architecture Programmer's Manual:
http://support.amd.com/TechDocs/24593.pdf
SME is section 7.10
SEV is section 15.34

Secure Encrypted Virutualization Key Management:
http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf

KVM Forum slides:
http://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf

Video of the KVM Forum Talk:
https://www.youtube.com/watch?v=RcvQ1xN55Ew

---

The complete patch series is available :
https://github.com/codomania/qemu/tree/v12

Using these patches we have succesfully booted and tested a guest both with and without SEV enabled.

TODO:

* Add SEV guest migration support
* Add SEV guest snapshot and restore support

Changes since v11:
- fix build failure seen with mingw32

Changes since v10:
- rebase to latest qemu
- minor fixes reported during review

Changes since v9:
- move sev specific header definition in target/i386/sev_i386.h
- add new QMP query-sev-capabilities - the command will be used by libvirt to query the SEV capabilities information.
- move sev specific QMP command implementation in target/i386/monitor.c. The sev commands are disabled for non x86 architecture
- update 'info sev' command to display human readiable format for policy
- use g_new0 to allocate to structure memory
- update qemu-options.hx to include cbitspos and reduced-phys-bit field

Changes since v8:
- update 'query-sev' command to use enum type for SEV state.
- populate memory encryption cpuid when only SEV is enabled.

Changes since v7:
- move sev.c from accel/kvm to target/i386
- update query-sev-launch-measure to return error when measurement is not available
- update flatview_read to use slow path when attrs.debug=1 is set
- fix the buffer overflow
- use '-' when adding new member in SevInfo QMP structure

Changes since v6:
- add support to specify cbitpos in sev-guest object
- add 'info sev' HMP command
- add 'query-sev' and 'query-launch-measure' QMP commands
- rework the logic to query the memory encryption mask when walking guest pagetable for debug

Changes since v5:
- drop MEMTXTATTRS_DEBUG macro, caller now specify attrs.debug=1 when needed.
- drop DPRINTF and use trace points to output the debug messages

Changes since v4:
- extend sev-guest object to add new properties 'dh-cert-file', 'session-file' etc.
- emit SEV_MEASUREMENT event when measurement is available
- add migration blocker
- add memory encryption cpuid support
- rebase the series with recent qemu tree

Changes since v3:
- update to newer SEV spec (0.12 -> 0.14)
- update to newer KVM RFC and use KVM_MEMORY_ENCRYPTION_OP ioctl instead of KVM_ISSUE_SEV.
- add support to encrypt plfash

Changes since v2:
- rename ram_ops to ram_debug_ops
- use '-' rather than '_' when adding new member in KvmInfo struct
- update sev object to use link properties when referencing other objects
- use ldq_phys_debug in tlb_info_64 and mem_info_64.
- remove sev-guest-policy object, we will revisit it after basic SEV guest support is merged.
- remove kernel API from doc and add SEV guest LAUNCH model. The doc will be updated as we integerate the remaining SEV APIs.

Changes since v1:
- Added Documentation
- Added security-policy object.
- Drop sev config parsing support and create new objects to get/set SEV specific parameters
- Added sev-guest-info object.
- Added sev-launch-info object.
- Added kvm_memory_encrytion_* APIs. The idea behind this was to allow adding a non SEV memory encrytion object without modifying interfaces.
- Drop patch to load OS image at fixed location.
- updated LAUNCH_FINISH command structure. Now the structure contains just 'measurement' field. Other fields are not used and will also be removed from newer SEV firmware API spec.

Brijesh Singh (28):
memattrs: add debug attribute
exec: add ram_debug_ops support
exec: add debug version of physical memory read and write API monitor/i386: use debug APIs when accessing guest memory
machine: add -memory-encryption property
kvm: update kvm.h to include memory encryption ioctls
docs: add AMD Secure Encrypted Virtualization (SEV) target/i386: add Secure Encrypted Virtulization (SEV) object
qmp: add query-sev command
include: add psp-sev.h header file
sev/i386: add command to initialize the memory encryption context sev/i386: register the guest memory range which may contain encrypted
data
kvm: introduce memory encryption APIs
hmp: add 'info sev' command
sev/i386: add command to create launch memory encryption context sev/i386: add command to encrypt guest memory region target/i386: encrypt bios rom
sev/i386: add support to LAUNCH_MEASURE command
sev/i386: finalize the SEV guest launch flow
hw/i386: set ram_debug_ops when memory encryption is enabled sev/i386: add debug encrypt and decrypt commands target/i386: clear C-bit when walking SEV guest page table
qmp: add query-sev-launch-measure command
sev/i386: add migration blocker
cpu/i386: populate CPUID 0x8000_001F when SEV is active
qmp: add query-sev-capabilities command
sev/i386: add sev_get_capabilities()
tests/qmp-test: blacklist sev specific qmp commands

 accel/kvm/kvm-all.c            |  49 +++
 accel/stubs/kvm-stub.c         |  14 +
 cpus.c                         |   2 +-
 disas.c                        |   2 +-
 docs/amd-memory-encryption.txt | 109 ++++++
 exec.c                         |  83 +++-
 hmp-commands-info.hx           |  16 +
 hmp.h                          |   1 +
 hw/core/machine.c              |  22 ++
 hw/i386/pc.c                   |   9 +
 hw/i386/pc_sysfw.c             |  19 +
 include/exec/cpu-common.h      |  15 +
 include/exec/memattrs.h        |   2 +
 include/exec/memory.h          |  30 +-
 include/hw/boards.h            |   1 +
 include/sysemu/kvm.h           |  25 ++
 include/sysemu/sev.h           |  22 ++
 linux-headers/linux/kvm.h      |  90 +++++
 linux-headers/linux/psp-sev.h  | 142 +++++++
 monitor.c                      |  27 +-
 qapi/misc.json                 | 147 +++++++
 qemu-options.hx                |  49 ++-
 stubs/Makefile.objs            |   1 +
 stubs/sev.c                    |  30 ++
 target/i386/Makefile.objs      |   4 +-
 target/i386/cpu.c              |  13 +
 target/i386/helper.c           |  31 +-
 target/i386/monitor.c          | 193 ++++++---
 target/i386/sev-stub.c         |  51 +++
 target/i386/sev.c              | 868 +++++++++++++++++++++++++++++++++++++++++
 target/i386/sev_i386.h         |  88 +++++
 target/i386/trace-events       |  11 +
 tests/qmp-test.c               |   5 +
 33 files changed, 2086 insertions(+), 85 deletions(-)
 create mode 100644 docs/amd-memory-encryption.txt
 create mode 100644 include/sysemu/sev.h
 create mode 100644 linux-headers/linux/psp-sev.h
 create mode 100644 stubs/sev.c
 create mode 100644 target/i386/sev-stub.c
 create mode 100644 target/i386/sev.c
 create mode 100644 target/i386/sev_i386.h
Signature · 1 line
2.14.3
#1 lore raw

PATCH v12 1/28 memattrs: add debug attribute

Extend the MemTxAttrs to include 'debug' flag. The flag can be used as general indicator that operation was triggered by the debugger.

Later in the patch series we set the debug=1 when issuing a memory access from the gdbstub or HMP commands. This patch is prerequisite to support debugging the encrypted guest. If we see request with debug=1 then we will need to use encryption APIs to access the guest memory.

Cc Alistair Francis <[email protected]>
Cc Peter Maydell <[email protected]>
Cc "Edgar E. Iglesias" <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 include/exec/memattrs.h | 2 ++
 1 file changed, 2 insertions(+)
include/exec/memattrs.h +2 -0
index d4a16420984b..08099e4f7e72 100644
@@ -37,6 +37,8 @@ typedef struct MemTxAttrs {
     unsigned int user:1;
     /* Requester ID (for MSI for example) */
     unsigned int requester_id:16;
+    /* Memory access request from the debugger */
+    unsigned int debug:1;
 } MemTxAttrs;
 
 /* Bus masters which don't specify any attributes will get this,
Signature · 1 line
2.14.3
#2 lore raw

PATCH v12 2/28 exec: add ram_debug_ops support

In reply to Brijesh Singh

Currently, the guest memory access for the debug purpose is performed using the memcpy(). Lets extend the 'struct MemoryRegion' to include ram_debug_ops callbacks. The ram_debug_ops can be used to override memcpy() with something else.

The feature can be used by encrypted guest -- which can register callbacks to override memcpy() with memory encryption/decryption APIs.

a typical usage:

mem_read(uint8_t *dst, uint8_t *src, uint32_t len, MemTxAttrs *attrs); mem_write(uint8_t *dst, uint8_t *src, uint32_t len, MemTxAttrs *attrs);

MemoryRegionRAMReadWriteOps ops;
ops.read = mem_read;
ops.write = mem_write;

memory_region_init_ram(mem, NULL, "memory", size, NULL); memory_region_set_ram_debug_ops(mem, ops);

Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Richard Henderson <[email protected]>
---
 exec.c                | 43 ++++++++++++++++++++++++++++++++-----------
 include/exec/memory.h | 30 +++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 12 deletions(-)
exec.c +32 -11
index 604f03c535bc..f69371d30e14 100644
@@ -3056,7 +3056,11 @@ static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
         } else {
             /* RAM case */
             ptr = qemu_ram_ptr_length(mr->ram_block, addr1, &l, false);
-            memcpy(ptr, buf, l);
+            if (attrs.debug && mr->ram_debug_ops) {
+                mr->ram_debug_ops->write(ptr, buf, l, attrs);
+            } else {
+                memcpy(ptr, buf, l);
+            }
             invalidate_and_set_dirty(mr, addr1, l);
         }
 
@@ -3144,7 +3148,11 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
         } else {
             /* RAM case */
             ptr = qemu_ram_ptr_length(mr->ram_block, addr1, &l, false);
-            memcpy(buf, ptr, l);
+            if (attrs.debug && mr->ram_debug_ops) {
+                mr->ram_debug_ops->read(buf, ptr, l, attrs);
+            } else {
+                memcpy(buf, ptr, l);
+            }
         }
 
         if (release_lock) {
@@ -3237,7 +3245,8 @@ enum write_rom_type {
 };
 
 static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
-    hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
+    hwaddr addr, const uint8_t *buf, int len, MemTxAttrs attrs,
+    enum write_rom_type type)
 {
     hwaddr l;
     uint8_t *ptr;
@@ -3257,7 +3266,11 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
             ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
             switch (type) {
             case WRITE_DATA:
-                memcpy(ptr, buf, l);
+                if (mr->ram_debug_ops) {
+                    mr->ram_debug_ops->write(ptr, buf, l, attrs);
+                } else {
+                    memcpy(ptr, buf, l);
+                }
                 invalidate_and_set_dirty(mr, addr1, l);
                 break;
             case FLUSH_CACHE:
@@ -3276,7 +3289,9 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
 void cpu_physical_memory_write_rom(AddressSpace *as, hwaddr addr,
                                    const uint8_t *buf, int len)
 {
-    cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA);
+    cpu_physical_memory_write_rom_internal(as, addr, buf, len,
+                                           MEMTXATTRS_UNSPECIFIED,
+                                           WRITE_DATA);
 }
 
 void cpu_flush_icache_range(hwaddr start, int len)
@@ -3291,8 +3306,9 @@ void cpu_flush_icache_range(hwaddr start, int len)
         return;
     }
 
-    cpu_physical_memory_write_rom_internal(&address_space_memory,
-                                           start, NULL, len, FLUSH_CACHE);
+    cpu_physical_memory_write_rom_internal(&address_space_memory, start, NULL,
+                                           len, MEMTXATTRS_UNSPECIFIED,
+                                           FLUSH_CACHE);
 }
 
 typedef struct {
@@ -3612,6 +3628,10 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
         page = addr & TARGET_PAGE_MASK;
         phys_addr = cpu_get_phys_page_attrs_debug(cpu, page, &attrs);
         asidx = cpu_asidx_from_attrs(cpu, attrs);
+
+        /* set debug attrs to indicate memory access is from the debugger */
+        attrs.debug = 1;
+
         /* if no physical page mapped, return an error */
         if (phys_addr == -1)
             return -1;
@@ -3620,13 +3640,14 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
             l = len;
         phys_addr += (addr & ~TARGET_PAGE_MASK);
         if (is_write) {
-            cpu_physical_memory_write_rom(cpu->cpu_ases[asidx].as,
-                                          phys_addr, buf, l);
+            cpu_physical_memory_write_rom_internal(cpu->cpu_ases[asidx].as,
+                                                   phys_addr, buf, l, attrs,
+                                                   WRITE_DATA);
         } else {
             address_space_rw(cpu->cpu_ases[asidx].as, phys_addr,
-                             MEMTXATTRS_UNSPECIFIED,
-                             buf, l, 0);
+                             attrs, buf, l, 0);
         }
+
         len -= l;
         buf += l;
         addr += l;
include/exec/memory.h +29 -1
index 31eae0a64062..875d39a125c6 100644
@@ -223,6 +223,18 @@ typedef struct IOMMUMemoryRegionClass {
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
 
+/* Memory Region RAM debug callback */
+typedef struct MemoryRegionRAMReadWriteOps MemoryRegionRAMReadWriteOps;
+
+struct MemoryRegionRAMReadWriteOps {
+    /* Write data into guest memory */
+    int (*write) (uint8_t *dest, const uint8_t *src,
+                  uint32_t len, MemTxAttrs attrs);
+    /* Read data from guest memory */
+    int (*read) (uint8_t *dest, const uint8_t *src,
+                 uint32_t len, MemTxAttrs attrs);
+};
+
 struct MemoryRegion {
     Object parent_obj;
 
@@ -262,6 +274,7 @@ struct MemoryRegion {
     const char *name;
     unsigned ioeventfd_nb;
     MemoryRegionIoeventfd *ioeventfds;
+    const MemoryRegionRAMReadWriteOps *ram_debug_ops;
 };
 
 struct IOMMUMemoryRegion {
@@ -675,6 +688,21 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
                                              uint64_t size,
                                              Error **errp);
 
+/**
+ * memory_region_set_ram_debug_ops: Set debug access ops for a given memory
+ * region.
+ *
+ * @mr: the #MemoryRegion to be initialized
+ * @ops: a function that will be used for when accessing @target region during
+ *       debug
+ */
+static inline void
+memory_region_set_ram_debug_ops(MemoryRegion *mr,
+                                const MemoryRegionRAMReadWriteOps *ops)
+{
+    mr->ram_debug_ops = ops;
+}
+
 /**
  * memory_region_init_reservation: Initialize a memory region that reserves
  *                                 I/O space.
@@ -1959,7 +1987,7 @@ MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
     MemoryRegion *mr;
     FlatView *fv;
 
-    if (__builtin_constant_p(len)) {
+    if (__builtin_constant_p(len) && !attrs.debug) {
         if (len) {
             rcu_read_lock();
             fv = address_space_to_flatview(as);
Signature · 1 line
2.14.3
#3 lore raw

PATCH v12 3/28 exec: add debug version of physical memory read and write API

In reply to Brijesh Singh

Adds the following new APIs
- cpu_physical_memory_read_debug
- cpu_physical_memory_write_debug
- cpu_physical_memory_rw_debug
- ldl_phys_debug
- ldq_phys_debug

Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Richard Henderson <[email protected]>
---
 exec.c                    | 40 ++++++++++++++++++++++++++++++++++++++++
 include/exec/cpu-common.h | 15 +++++++++++++++
 2 files changed, 55 insertions(+)
exec.c +40 -0
index f69371d30e14..ca81a2f3244f 100644
@@ -3612,6 +3612,46 @@ void address_space_cache_destroy(MemoryRegionCache *cache)
 #define RCU_READ_UNLOCK()        rcu_read_unlock()
 #include "memory_ldst.inc.c"
 
+uint32_t ldl_phys_debug(CPUState *cpu, hwaddr addr)
+{
+    MemTxAttrs attrs;
+    int asidx = cpu_asidx_from_attrs(cpu, attrs);
+    uint32_t val;
+
+    /* set debug attrs to indicate memory access is from the debugger */
+    attrs.debug = 1;
+
+    address_space_rw(cpu->cpu_ases[asidx].as, addr, attrs,
+                     (void *) &val, 4, 0);
+
+    return tswap32(val);
+}
+
+uint64_t ldq_phys_debug(CPUState *cpu, hwaddr addr)
+{
+    MemTxAttrs attrs;
+    int asidx = cpu_asidx_from_attrs(cpu, attrs);
+    uint64_t val;
+
+    /* set debug attrs to indicate memory access is from the debugger */
+    attrs.debug = 1;
+
+    address_space_rw(cpu->cpu_ases[asidx].as, addr, attrs,
+                     (void *) &val, 8, 0);
+    return val;
+}
+
+void cpu_physical_memory_rw_debug(hwaddr addr, uint8_t *buf,
+                                  int len, int is_write)
+{
+    MemTxAttrs attrs;
+
+    /* set debug attrs to indicate memory access is from the debugger */
+    attrs.debug = 1;
+
+    address_space_rw(&address_space_memory, addr, attrs, buf, len, is_write);
+}
+
 /* virtual memory access for debug (includes writing to ROM) */
 int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
                         uint8_t *buf, int len, int is_write)
include/exec/cpu-common.h +15 -0
index 74341b19d26a..fa01385d4f1b 100644
@@ -77,11 +77,26 @@ size_t qemu_ram_pagesize_largest(void);
 
 void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
                             int len, int is_write);
+void cpu_physical_memory_rw_debug(hwaddr addr, uint8_t *buf,
+                                  int len, int is_write);
 static inline void cpu_physical_memory_read(hwaddr addr,
                                             void *buf, int len)
 {
     cpu_physical_memory_rw(addr, buf, len, 0);
 }
+static inline void cpu_physical_memory_read_debug(hwaddr addr,
+                                                  void *buf, int len)
+{
+    cpu_physical_memory_rw_debug(addr, buf, len, 0);
+}
+static inline void cpu_physical_memory_write_debug(hwaddr addr,
+                                                   const void *buf, int len)
+{
+    cpu_physical_memory_rw_debug(addr, (void *)buf, len, 1);
+}
+uint32_t ldl_phys_debug(CPUState *cpu, hwaddr addr);
+uint64_t ldq_phys_debug(CPUState *cpu, hwaddr addr);
+
 static inline void cpu_physical_memory_write(hwaddr addr,
                                              const void *buf, int len)
 {
Signature · 1 line
2.14.3
#4 lore raw

PATCH v12 4/28 monitor/i386: use debug APIs when accessing guest memory

In reply to Brijesh Singh

Updates HMP commands to use the debug version of APIs when accessing the guest memory.

Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Richard Henderson <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 cpus.c                |  2 +-
 disas.c               |  2 +-
 monitor.c             |  6 +++---
 target/i386/helper.c  | 14 ++++++------
 target/i386/monitor.c | 60 +++++++++++++++++++++++++++------------------------
 5 files changed, 44 insertions(+), 40 deletions(-)
cpus.c +1 -1
index 9bcff7d63c12..0dd506bcfee3 100644
@@ -2255,7 +2255,7 @@ void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
         l = sizeof(buf);
         if (l > size)
             l = size;
-        cpu_physical_memory_read(addr, buf, l);
+        cpu_physical_memory_read_debug(addr, buf, l);
         if (fwrite(buf, 1, l, f) != l) {
             error_setg(errp, QERR_IO_ERROR);
             goto exit;
disas.c +1 -1
index d4ad1089efb3..fcedbf263302 100644
@@ -586,7 +586,7 @@ static int
 physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
                      struct disassemble_info *info)
 {
-    cpu_physical_memory_read(memaddr, myaddr, length);
+    cpu_physical_memory_read_debug(memaddr, myaddr, length);
     return 0;
 }
 
monitor.c +3 -3
index a4417f26cde3..3f4ba902266b 100644
@@ -1368,7 +1368,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
         if (l > line_size)
             l = line_size;
         if (is_physical) {
-            cpu_physical_memory_read(addr, buf, l);
+            cpu_physical_memory_read_debug(addr, buf, l);
         } else {
             if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
                 monitor_printf(mon, " Cannot access memory\n");
@@ -1574,8 +1574,8 @@ static void hmp_sum(Monitor *mon, const QDict *qdict)
 
     sum = 0;
     for(addr = start; addr < (start + size); addr++) {
-        uint8_t val = address_space_ldub(&address_space_memory, addr,
-                                         MEMTXATTRS_UNSPECIFIED, NULL);
+        uint8_t val;
+        cpu_physical_memory_read_debug(addr, &val, 1);
         /* BSD sum algorithm ('sum' Unix command) */
         sum = (sum >> 1) | (sum << 15);
         sum += val;
target/i386/helper.c +7 -7
index 9fba146b7fb0..58fb6eec562a 100644
@@ -757,7 +757,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
             if (la57) {
                 pml5e_addr = ((env->cr[3] & ~0xfff) +
                         (((addr >> 48) & 0x1ff) << 3)) & a20_mask;
-                pml5e = x86_ldq_phys(cs, pml5e_addr);
+                pml5e = ldq_phys_debug(cs, pml5e_addr);
                 if (!(pml5e & PG_PRESENT_MASK)) {
                     return -1;
                 }
@@ -767,7 +767,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
 
             pml4e_addr = ((pml5e & PG_ADDRESS_MASK) +
                     (((addr >> 39) & 0x1ff) << 3)) & a20_mask;
-            pml4e = x86_ldq_phys(cs, pml4e_addr);
+            pml4e = ldq_phys_debug(cs, pml4e_addr);
             if (!(pml4e & PG_PRESENT_MASK)) {
                 return -1;
             }
@@ -788,14 +788,14 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
         {
             pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
                 a20_mask;
-            pdpe = x86_ldq_phys(cs, pdpe_addr);
+            pdpe = ldq_phys_debug(cs, pdpe_addr);
             if (!(pdpe & PG_PRESENT_MASK))
                 return -1;
         }
 
         pde_addr = ((pdpe & PG_ADDRESS_MASK) +
                     (((addr >> 21) & 0x1ff) << 3)) & a20_mask;
-        pde = x86_ldq_phys(cs, pde_addr);
+        pde = ldq_phys_debug(cs, pde_addr);
         if (!(pde & PG_PRESENT_MASK)) {
             return -1;
         }
@@ -808,7 +808,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
             pte_addr = ((pde & PG_ADDRESS_MASK) +
                         (((addr >> 12) & 0x1ff) << 3)) & a20_mask;
             page_size = 4096;
-            pte = x86_ldq_phys(cs, pte_addr);
+            pte = ldq_phys_debug(cs, pte_addr);
         }
         if (!(pte & PG_PRESENT_MASK)) {
             return -1;
@@ -818,7 +818,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
 
         /* page directory entry */
         pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & a20_mask;
-        pde = x86_ldl_phys(cs, pde_addr);
+        pde = ldl_phys_debug(cs, pde_addr);
         if (!(pde & PG_PRESENT_MASK))
             return -1;
         if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
@@ -827,7 +827,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
         } else {
             /* page directory entry */
             pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & a20_mask;
-            pte = x86_ldl_phys(cs, pte_addr);
+            pte = ldl_phys_debug(cs, pte_addr);
             if (!(pte & PG_PRESENT_MASK)) {
                 return -1;
             }
target/i386/monitor.c +32 -28

200 lines not on this page · raw message

Signature · 1 line
2.14.3
#5 lore raw

PATCH v12 5/28 machine: add -memory-encryption property

In reply to Brijesh Singh

When CPU supports memory encryption feature, the property can be used to specify the encryption object to use when launching an encrypted guest.

Cc Paolo Bonzini <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
---
 hw/core/machine.c   | 22 ++++++++++++++++++++++
 include/hw/boards.h |  1 +
 qemu-options.hx     |  5 ++++-
 3 files changed, 27 insertions(+), 1 deletion(-)
hw/core/machine.c +22 -0
index 5e2bbcdacedb..2040177664d5 100644
@@ -334,6 +334,22 @@ static bool machine_get_enforce_config_section(Object *obj, Error **errp)
     return ms->enforce_config_section;
 }
 
+static char *machine_get_memory_encryption(Object *obj, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    return g_strdup(ms->memory_encryption);
+}
+
+static void machine_set_memory_encryption(Object *obj, const char *value,
+                                        Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    g_free(ms->memory_encryption);
+    ms->memory_encryption = g_strdup(value);
+}
+
 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
 {
     strList *item = g_new0(strList, 1);
@@ -612,6 +628,12 @@ static void machine_class_init(ObjectClass *oc, void *data)
         &error_abort);
     object_class_property_set_description(oc, "enforce-config-section",
         "Set on to enforce configuration section migration", &error_abort);
+
+    object_class_property_add_str(oc, "memory-encryption",
+        machine_get_memory_encryption, machine_set_memory_encryption,
+        &error_abort);
+    object_class_property_set_description(oc, "memory-encryption",
+        "Set memory encyption object to use", &error_abort);
 }
 
 static void machine_class_base_init(ObjectClass *oc, void *data)
include/hw/boards.h +1 -0
index efb0a9edfdf1..8ce9a7a21d3d 100644
@@ -243,6 +243,7 @@ struct MachineState {
     bool suppress_vmdesc;
     bool enforce_config_section;
     bool enable_graphics;
+    char *memory_encryption;
 
     ram_addr_t ram_size;
     ram_addr_t maxram_size;
qemu-options.hx +4 -1
index 6585058c6cde..4c280142c52c 100644
@@ -43,7 +43,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                suppress-vmdesc=on|off disables self-describing migration (default=off)\n"
     "                nvdimm=on|off controls NVDIMM support (default=off)\n"
     "                enforce-config-section=on|off enforce configuration section migration (default=off)\n"
-    "                s390-squash-mcss=on|off (deprecated) controls support for squashing into default css (default=off)\n",
+    "                s390-squash-mcss=on|off (deprecated) controls support for squashing into default css (default=off)\n"
+    "                memory-encryption=@var{} memory encryption object to use (default=none)\n",
     QEMU_ARCH_ALL)
 STEXI
 @item -machine [type=]@var{name}[,prop=@var{value}[,...]]
@@ -110,6 +111,8 @@ code to send configuration section even if the machine-type sets the
 @option{migration.send-configuration} property to @var{off}.
 NOTE: this parameter is deprecated. Please use @option{-global}
 @option{migration.send-configuration}=@var{on|off} instead.
+@item memory-encryption=@var{}
+Memory encryption object to use. The default is none.
 @end table
 ETEXI
 
Signature · 1 line
2.14.3
#6 lore raw

Re: [PATCH v12 05/28] machine: add -memory-encryption property

Nitpick: the subject makes it seem like you're adding "-memory-encryption" as a CLI arg, but actually its adding support for 'memory-encryption=$VAL' property to the '-machine' CLI arg.

Sufficient to just s/-memory-encryption/"memory-encryption"/

Quoted 92 lines · Brijesh Singh

When CPU supports memory encryption feature, the property can be used to specify the encryption object to use when launching an encrypted guest.

Cc Paolo Bonzini <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
---
 hw/core/machine.c   | 22 ++++++++++++++++++++++
 include/hw/boards.h |  1 +
 qemu-options.hx     |  5 ++++-
 3 files changed, 27 insertions(+), 1 deletion(-)
hw/core/machine.c +22 -0
index 5e2bbcdacedb..2040177664d5 100644
@@ -334,6 +334,22 @@ static bool machine_get_enforce_config_section(Object *obj, Error **errp)
     return ms->enforce_config_section;
 }
 
+static char *machine_get_memory_encryption(Object *obj, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    return g_strdup(ms->memory_encryption);
+}
+
+static void machine_set_memory_encryption(Object *obj, const char *value,
+                                        Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    g_free(ms->memory_encryption);
+    ms->memory_encryption = g_strdup(value);
+}
+
 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
 {
     strList *item = g_new0(strList, 1);
@@ -612,6 +628,12 @@ static void machine_class_init(ObjectClass *oc, void *data)
         &error_abort);
     object_class_property_set_description(oc, "enforce-config-section",
         "Set on to enforce configuration section migration", &error_abort);
+
+    object_class_property_add_str(oc, "memory-encryption",
+        machine_get_memory_encryption, machine_set_memory_encryption,
+        &error_abort);
+    object_class_property_set_description(oc, "memory-encryption",
+        "Set memory encyption object to use", &error_abort);
 }
 
 static void machine_class_base_init(ObjectClass *oc, void *data)
include/hw/boards.h +1 -0
index efb0a9edfdf1..8ce9a7a21d3d 100644
@@ -243,6 +243,7 @@ struct MachineState {
     bool suppress_vmdesc;
     bool enforce_config_section;
     bool enable_graphics;
+    char *memory_encryption;
 
     ram_addr_t ram_size;
     ram_addr_t maxram_size;
qemu-options.hx +4 -1
index 6585058c6cde..4c280142c52c 100644
@@ -43,7 +43,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                suppress-vmdesc=on|off disables self-describing migration (default=off)\n"
     "                nvdimm=on|off controls NVDIMM support (default=off)\n"
     "                enforce-config-section=on|off enforce configuration section migration (default=off)\n"
-    "                s390-squash-mcss=on|off (deprecated) controls support for squashing into default css (default=off)\n",
+    "                s390-squash-mcss=on|off (deprecated) controls support for squashing into default css (default=off)\n"
+    "                memory-encryption=@var{} memory encryption object to use (default=none)\n",
     QEMU_ARCH_ALL)
 STEXI
 @item -machine [type=]@var{name}[,prop=@var{value}[,...]]
@@ -110,6 +111,8 @@ code to send configuration section even if the machine-type sets the
 @option{migration.send-configuration} property to @var{off}.
 NOTE: this parameter is deprecated. Please use @option{-global}
 @option{migration.send-configuration}=@var{on|off} instead.
+@item memory-encryption=@var{}
+Memory encryption object to use. The default is none.
 @end table
 ETEXI
 
Signature · 1 line
2.14.3

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw

PATCH v12 6/28 kvm: update kvm.h to include memory encryption ioctls

In reply to Brijesh Singh

Updates kmv.h to include memory encryption ioctls and SEV commands.

Cc Christian Borntraeger <[email protected]>
Cc Cornelia Huck <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 linux-headers/linux/kvm.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
linux-headers/linux/kvm.h +90 -0
index d92c9b2f0ed2..aed22309950d 100644
@@ -1362,6 +1362,96 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_S390_CMMA_MIGRATION */
 #define KVM_S390_GET_CMMA_BITS      _IOWR(KVMIO, 0xb8, struct kvm_s390_cmma_log)
 #define KVM_S390_SET_CMMA_BITS      _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log)
+/* Memory Encryption Commands */
+#define KVM_MEMORY_ENCRYPT_OP      _IOWR(KVMIO, 0xba, unsigned long)
+
+struct kvm_enc_region {
+	__u64 addr;
+	__u64 size;
+};
+
+#define KVM_MEMORY_ENCRYPT_REG_REGION    _IOR(KVMIO, 0xbb, struct kvm_enc_region)
+#define KVM_MEMORY_ENCRYPT_UNREG_REGION  _IOR(KVMIO, 0xbc, struct kvm_enc_region)
+
+/* Secure Encrypted Virtualization command */
+enum sev_cmd_id {
+	/* Guest initialization commands */
+	KVM_SEV_INIT = 0,
+	KVM_SEV_ES_INIT,
+	/* Guest launch commands */
+	KVM_SEV_LAUNCH_START,
+	KVM_SEV_LAUNCH_UPDATE_DATA,
+	KVM_SEV_LAUNCH_UPDATE_VMSA,
+	KVM_SEV_LAUNCH_SECRET,
+	KVM_SEV_LAUNCH_MEASURE,
+	KVM_SEV_LAUNCH_FINISH,
+	/* Guest migration commands (outgoing) */
+	KVM_SEV_SEND_START,
+	KVM_SEV_SEND_UPDATE_DATA,
+	KVM_SEV_SEND_UPDATE_VMSA,
+	KVM_SEV_SEND_FINISH,
+	/* Guest migration commands (incoming) */
+	KVM_SEV_RECEIVE_START,
+	KVM_SEV_RECEIVE_UPDATE_DATA,
+	KVM_SEV_RECEIVE_UPDATE_VMSA,
+	KVM_SEV_RECEIVE_FINISH,
+	/* Guest status and debug commands */
+	KVM_SEV_GUEST_STATUS,
+	KVM_SEV_DBG_DECRYPT,
+	KVM_SEV_DBG_ENCRYPT,
+	/* Guest certificates commands */
+	KVM_SEV_CERT_EXPORT,
+
+	KVM_SEV_NR_MAX,
+};
+
+struct kvm_sev_cmd {
+	__u32 id;
+	__u64 data;
+	__u32 error;
+	__u32 sev_fd;
+};
+
+struct kvm_sev_launch_start {
+	__u32 handle;
+	__u32 policy;
+	__u64 dh_uaddr;
+	__u32 dh_len;
+	__u64 session_uaddr;
+	__u32 session_len;
+};
+
+struct kvm_sev_launch_update_data {
+	__u64 uaddr;
+	__u32 len;
+};
+
+
+struct kvm_sev_launch_secret {
+	__u64 hdr_uaddr;
+	__u32 hdr_len;
+	__u64 guest_uaddr;
+	__u32 guest_len;
+	__u64 trans_uaddr;
+	__u32 trans_len;
+};
+
+struct kvm_sev_launch_measure {
+	__u64 uaddr;
+	__u32 len;
+};
+
+struct kvm_sev_guest_status {
+	__u32 handle;
+	__u32 policy;
+	__u32 state;
+};
+
+struct kvm_sev_dbg {
+	__u64 src_uaddr;
+	__u64 dst_uaddr;
+	__u32 len;
+};
 
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
Signature · 1 line
2.14.3
#8 lore raw

PATCH v12 7/28 docs: add AMD Secure Encrypted Virtualization (SEV)

In reply to Brijesh Singh

Create a documentation entry to describe the AMD Secure Encrypted Virtualization (SEV) feature.

---
 docs/amd-memory-encryption.txt | 92 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 docs/amd-memory-encryption.txt
docs/amd-memory-encryption.txt new file +92 -0
new file mode 100644
index 000000000000..72a92b6c6353
@@ -0,0 +1,92 @@
+Secure Encrypted Virtualization (SEV) is a feature found on AMD processors.
+
+SEV is an extension to the AMD-V architecture which supports running encrypted
+virtual machine (VMs) under the control of KVM. Encrypted VMs have their pages
+(code and data) secured such that only the guest itself has access to the
+unencrypted version. Each encrypted VM is associated with a unique encryption
+key; if its data is accessed to a different entity using a different key the
+encrypted guests data will be incorrectly decrypted, leading to unintelligible
+data.
+
+The key management of this feature is handled by separate processor known as
+AMD secure processor (AMD-SP) which is present in AMD SOCs. Firmware running
+inside the AMD-SP provide commands to support common VM lifecycle. This
+includes commands for launching, snapshotting, migrating and debugging the
+encrypted guest. Those SEV command can be issued via KVM_MEMORY_ENCRYPT_OP
+ioctls.
+
+Launching
+---------
+Boot images (such as bios) must be encrypted before guest can be booted.
+MEMORY_ENCRYPT_OP ioctl provides commands to encrypt the images :LAUNCH_START,
+LAUNCH_UPDATE_DATA, LAUNCH_MEASURE and LAUNCH_FINISH. These four commands
+together generate a fresh memory encryption key for the VM, encrypt the boot
+images and provide a measurement than can be used as an attestation of the
+successful launch.
+
+LAUNCH_START is called first to create a cryptographic launch context within
+the firmware. To create this context, guest owner must provides guest policy,
+its public Diffie-Hellman key (PDH) and session parameters. These inputs
+should be treated as binary blob and must be passed as-is to the SEV firmware.
+
+The guest policy is passed as plaintext and hypervisor may able to read it
+but should not modify it (any modification of the policy bits will result
+in bad measurement). The guest policy is a 4-byte data structure containing
+several flags that restricts what can be done on running SEV guest.
+See KM Spec section 3 and 6.2 for more details.
+
+Guest owners provided DH certificate and session parameters will be used to
+establish a cryptographic session with the guest owner to negotiate keys used
+for the attestation.
+
+LAUNCH_UPDATE_DATA encrypts the memory region using the cryptographic context
+created via LAUNCH_START command. If required, this command can be called
+multiple times to encrypt different memory regions. The command also calculates
+the measurement of the memory contents as it encrypts.
+
+LAUNCH_MEASURE command can be used to retrieve the measurement of encrypted
+memory. This 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. The guest owner may wait to provide the guest
+confidential information until it can verify the attestation measurement.
+Since the guest owner knows the initial contents of the guest at boot, the
+attestation measurement can be verified by comparing it to what the guest owner
+expects.
+
+LAUNCH_FINISH command finalizes the guest launch and destroy's the cryptographic
+context.
+
+See SEV KM API Spec [1] 'Launching a guest' usage flow (Appendix A) for the
+complete flow chart.
+
+Debugging
+-----------
+Since memory contents of SEV guest is encrypted hence hypervisor access to the
+guest memory will get a cipher text. If guest policy allows debugging, then
+hypervisor can use DEBUG_DECRYPT and DEBUG_ENCRYPT commands access the guest
+memory region for debug purposes.
+
+Snapshot/Restore
+-----------------
+TODO
+
+Live Migration
+----------------
+TODO
+
+References
+-----------------
+
+AMD Memory Encryption whitepaper:
+http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf
+
+Secure Encrypted Virutualization Key Management:
+[1] http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf
+
+KVM Forum slides:
+http://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf
+
+AMD64 Architecture Programmer's Manual:
+   http://support.amd.com/TechDocs/24593.pdf
+   SME is section 7.10
+   SEV is section 15.34
Signature · 1 line
2.14.3
#9 lore raw

PATCH v12 8/28 target/i386: add Secure Encrypted Virtulization (SEV) object

In reply to Brijesh Singh

Add a new memory encryption object 'sev-guest'. The object will be used to create enrypted VMs on AMD EPYC CPU. The object provides the properties to pass guest owner's public Diffie-hellman key, guest policy and session information required to create the memory encryption context within the SEV firmware.

e.g to launch SEV guest

 # $QEMU \
    -object sev-guest,id=sev0 \
    -machine ....,memory-encryption=sev0
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 docs/amd-memory-encryption.txt |  17 +++
 qemu-options.hx                |  44 ++++++++
 target/i386/Makefile.objs      |   2 +-
 target/i386/sev.c              | 228 +++++++++++++++++++++++++++++++++++++++++
 target/i386/sev_i386.h         |  61 +++++++++++
 5 files changed, 351 insertions(+), 1 deletion(-)
 create mode 100644 target/i386/sev.c
 create mode 100644 target/i386/sev_i386.h
docs/amd-memory-encryption.txt +17 -0
index 72a92b6c6353..05266fd41b23 100644
@@ -35,10 +35,21 @@ in bad measurement). The guest policy is a 4-byte data structure containing
 several flags that restricts what can be done on running SEV guest.
 See KM Spec section 3 and 6.2 for more details.
 
+The guest policy can be provided via the 'policy' property (see below)
+
+# ${QEMU} \
+   sev-guest,id=sev0,policy=0x1...\
+
 Guest owners provided DH certificate and session parameters will be used to
 establish a cryptographic session with the guest owner to negotiate keys used
 for the attestation.
 
+The DH certificate and session blob can be provided via 'dh-cert-file' and
+'session-file' property (see below
+
+# ${QEMU} \
+     sev-guest,id=sev0,dh-cert-file=<file1>,session-file=<file2>
+
 LAUNCH_UPDATE_DATA encrypts the memory region using the cryptographic context
 created via LAUNCH_START command. If required, this command can be called
 multiple times to encrypt different memory regions. The command also calculates
@@ -59,6 +70,12 @@ context.
 See SEV KM API Spec [1] 'Launching a guest' usage flow (Appendix A) for the
 complete flow chart.
 
+To launch a SEV guest
+
+# ${QEMU} \
+    -machine ...,memory-encryption=sev0 \
+    -object sev-guest,id=sev0,cbitpos=47,reduced-phys-bits=1
+
 Debugging
 -----------
 Since memory contents of SEV guest is encrypted hence hypervisor access to the
qemu-options.hx +44 -0

50 lines not on this page · raw message

target/i386/Makefile.objs +1 -1
index f5c6ef20a7bb..76aeaeae2750 100644
@@ -4,7 +4,7 @@ obj-$(CONFIG_TCG) += bpt_helper.o cc_helper.o excp_helper.o fpu_helper.o
 obj-$(CONFIG_TCG) += int_helper.o mem_helper.o misc_helper.o mpx_helper.o
 obj-$(CONFIG_TCG) += seg_helper.o smm_helper.o svm_helper.o
 obj-$(CONFIG_SOFTMMU) += machine.o arch_memory_mapping.o arch_dump.o monitor.o
-obj-$(CONFIG_KVM) += kvm.o hyperv.o
+obj-$(CONFIG_KVM) += kvm.o hyperv.o sev.o
 obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
 # HAX support
 ifdef CONFIG_WIN32
target/i386/sev.c new file +228 -0

228 lines not on this page · raw message

target/i386/sev_i386.h new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.14.3
#10 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 29 lines · Brijesh Singh

Add a new memory encryption object 'sev-guest'. The object will be used to create enrypted VMs on AMD EPYC CPU. The object provides the properties to pass guest owner's public Diffie-hellman key, guest policy and session information required to create the memory encryption context within the SEV firmware.

e.g to launch SEV guest

 # $QEMU \
    -object sev-guest,id=sev0 \
    -machine ....,memory-encryption=sev0
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
qemu-options.hx +4 -0
index 4c280142c52c..6113bce08a8c 100644
@@ -4353,6 +4353,50 @@ contents of @code{iv.b64} to the second secret
          data=$SECRET,iv=$(<iv.b64)
 @end example
 
+@item -object sev-guest,id=@var{id},cbitpos=@var{cbitpos},reduced-phys-bits=@var{val},[sev-device=@var{string},policy=@var{policy},handle=@var{handle},dh-cert-file=@var{file},session-file=@var{file}]
+
+Create a Secure Encrypted Virtualization (SEV) guest object, which can be used
+to provide the guest memory encryption support on AMD processors.

qemu-options.hx contents of @code{iv.b64} to the second secret · line 4369

Quoted 10 lines
qemu-options.hx +10 -0
+
+When memory encryption is enabled, one of the physical address bit (aka the
+C-bit) is utilized to mark if a memory page is protected. The @option{cbitpos}
+is used to provide the C-bit position. The C-bit position is Host family dependent
+hence user must provide this value. On EPYC, the value should be 47.
+
+When memory encryption is enabled, we loose certain bits in physical address space.
+The @option{reduced-phys-bits} is used to provide the number of bits we loose in
+physical address space. Similar to C-bit, the value is Host family dependent.
+On EPYC, the value should be 5.

Is it valid to specify a different value for either of these properties ? eg what happens if I pass cbitpos=45 instead of 47 on an EPYC host ?

In particular I thinking about possible migration scenario, where EPYC uses 47 by default but some $NEXT AMD CPU uses 48 by default. In that case we might want to use '47' on both CPUs if we need ability to live migrate between different host CPU generations. Would that be valid ?

On the flip side, if the value really it strictly tied to the host CPU family and no deviation is permitted, could the kernel not just pick the right value automatically avoiding the config option ?

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#11 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 25 lines · Daniel P. Berrangé
Quoted 24 lines · Brijesh Singh

Add a new memory encryption object 'sev-guest'. The object will be used to create enrypted VMs on AMD EPYC CPU. The object provides the properties to pass guest owner's public Diffie-hellman key, guest policy and session information required to create the memory encryption context within the SEV firmware.

e.g to launch SEV guest

 # $QEMU \
    -object sev-guest,id=sev0 \
    -machine ....,memory-encryption=sev0
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
qemu-options.hx +0 -0
index 4c280142c52c..6113bce08a8c 100644
@@ -4353,6 +4353,50 @@ contents of @code{iv.b64} to the second secret
          data=$SECRET,iv=$(<iv.b64)
 @end example
 
Quoted 16 lines
Quoted 4 lines
qemu-options.hx +4 -0
+@item -object sev-guest,id=@var{id},cbitpos=@var{cbitpos},reduced-phys-bits=@var{val},[sev-device=@var{string},policy=@var{policy},handle=@var{handle},dh-cert-file=@var{file},session-file=@var{file}]
+
+Create a Secure Encrypted Virtualization (SEV) guest object, which can be used
+to provide the guest memory encryption support on AMD processors.

qemu-options.hx contents of @code{iv.b64} to the second secret · line 4369

Quoted 10 lines
qemu-options.hx +10 -0
+
+When memory encryption is enabled, one of the physical address bit (aka the
+C-bit) is utilized to mark if a memory page is protected. The @option{cbitpos}
+is used to provide the C-bit position. The C-bit position is Host family dependent
+hence user must provide this value. On EPYC, the value should be 47.
+
+When memory encryption is enabled, we loose certain bits in physical address space.
+The @option{reduced-phys-bits} is used to provide the number of bits we loose in
+physical address space. Similar to C-bit, the value is Host family dependent.
+On EPYC, the value should be 5.

Is it valid to specify a different value for either of these properties ? eg what happens if I pass cbitpos=45 instead of 47 on an EPYC host ?

On EPYC, passing anything other than 47 will trigger error during SEV guest initialization. The value of Cbit position is host dependent, the value is readonly and can be obtained through the host CPUID.  The cbitpos must be same between guest and host. Please note that the pte's in guest page table will need to use the cbitpos  information to mark the pages as encrypted. If cbit position given to the guest is different from the host then guest will fail to execute.

Quoted 5 lines

In particular I thinking about possible migration scenario, where EPYC uses 47 by default but some $NEXT AMD CPU uses 48 by default. In that case we might want to use '47' on both CPUs if we need ability to live migrate between different host CPU generations. Would that be valid ?

We will not be able to migrate SEV guests if cbit position does not match between the source and destination hosts. Since during migration, the destination guest is launched with same QEMU cli as source hence cbitpos check in QEMU will catch it and fail the new launch. Optionally, user can call query-sev-capabilities on both source and destination to see if cbitpos is compatible before attempting to migrate the guest.

Quoted 4 lines

On the flip side, if the value really it strictly tied to the host CPU family and no deviation is permitted, could the kernel not just pick the right value automatically avoiding the config option ?

I think doing so will be an issue for the migration. Consider your above use case, a SEV guest is running on EPYC with cbitpos=47 and if we migrate to some $NEXT AMD CPU which uses need to use cbitpos=48 and we will fail to resume the guest on destination after migrating.

Quoted 3 lines

Regards,
Daniel

#12 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 71 lines · Brijesh Singh
Quoted 25 lines · Daniel P. Berrangé
Quoted 24 lines · Brijesh Singh

Add a new memory encryption object 'sev-guest'. The object will be used to create enrypted VMs on AMD EPYC CPU. The object provides the properties to pass guest owner's public Diffie-hellman key, guest policy and session information required to create the memory encryption context within the SEV firmware.

e.g to launch SEV guest

 # $QEMU \
    -object sev-guest,id=sev0 \
    -machine ....,memory-encryption=sev0
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
qemu-options.hx +0 -0
index 4c280142c52c..6113bce08a8c 100644
@@ -4353,6 +4353,50 @@ contents of @code{iv.b64} to the second secret
          data=$SECRET,iv=$(<iv.b64)
 @end example
 
Quoted 16 lines
Quoted 4 lines
qemu-options.hx +4 -0
+@item -object sev-guest,id=@var{id},cbitpos=@var{cbitpos},reduced-phys-bits=@var{val},[sev-device=@var{string},policy=@var{policy},handle=@var{handle},dh-cert-file=@var{file},session-file=@var{file}]
+
+Create a Secure Encrypted Virtualization (SEV) guest object, which can be used
+to provide the guest memory encryption support on AMD processors.

qemu-options.hx contents of @code{iv.b64} to the second secret · line 4369

Quoted 10 lines
qemu-options.hx +10 -0
+
+When memory encryption is enabled, one of the physical address bit (aka the
+C-bit) is utilized to mark if a memory page is protected. The @option{cbitpos}
+is used to provide the C-bit position. The C-bit position is Host family dependent
+hence user must provide this value. On EPYC, the value should be 47.
+
+When memory encryption is enabled, we loose certain bits in physical address space.
+The @option{reduced-phys-bits} is used to provide the number of bits we loose in
+physical address space. Similar to C-bit, the value is Host family dependent.
+On EPYC, the value should be 5.

Is it valid to specify a different value for either of these properties ? eg what happens if I pass cbitpos=45 instead of 47 on an EPYC host ?

On EPYC, passing anything other than 47 will trigger error during SEV guest initialization. The value of Cbit position is host dependent, the value is readonly and can be obtained through the host CPUID.  The cbitpos must be same between guest and host. Please note that the pte's in guest page table will need to use the cbitpos  information to mark the pages as encrypted. If cbit position given to the guest is different from the host then guest will fail to execute.

Quoted 5 lines

In particular I thinking about possible migration scenario, where EPYC uses 47 by default but some $NEXT AMD CPU uses 48 by default. In that case we might want to use '47' on both CPUs if we need ability to live migrate between different host CPU generations. Would that be valid ?

We will not be able to migrate SEV guests if cbit position does not match between the source and destination hosts. Since during migration, the destination guest is launched with same QEMU cli as source hence cbitpos check in QEMU will catch it and fail the new launch. Optionally, user can call query-sev-capabilities on both source and destination to see if cbitpos is compatible before attempting to migrate the guest.

Quoted 4 lines

On the flip side, if the value really it strictly tied to the host CPU family and no deviation is permitted, could the kernel not just pick the right value automatically avoiding the config option ?

Quoted 4 lines

I think doing so will be an issue for the migration. Consider your above use case, a SEV guest is running on EPYC with cbitpos=47 and if we migrate to some $NEXT AMD CPU which uses need to use cbitpos=48 and we will fail to resume the guest on destination after migrating.

Exactly, in other words these two options are part of the guest ABI, and QEMU promises to never make the guest ABI depend on the host hardware unless you're using "-cpu host".

In theory we could make QEMU choose the right values automatically if we document very clearly that the default behavior is unsafe. But I would rather not take that risk and force management software to be aware of the gotchas involved in using SEV + live-migration.

Signature · 1 line
Eduardo
maintainer
#13 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 8 lines · Eduardo Habkost
Quoted 4 lines

I think doing so will be an issue for the migration. Consider your above use case, a SEV guest is running on EPYC with cbitpos=47 and if we migrate to some $NEXT AMD CPU which uses need to use cbitpos=48 and we will fail to resume the guest on destination after migrating.

Exactly, in other words these two options are part of the guest ABI, and QEMU promises to never make the guest ABI depend on the host hardware unless you're using "-cpu host".

This is not entirely true; while MAXPHYADDR is constant downstream unless using "-cpu host", in practice that behavior is wrong and a guest could misbehave if passed a MAXPHYADDR that is different from the host's.

I think this is the same, and management software will have to live with it.

Paolo

Quoted 5 lines

In theory we could make QEMU choose the right values automatically if we document very clearly that the default behavior is unsafe. But I would rather not take that risk and force management software to be aware of the gotchas involved in using SEV + live-migration.

#14 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Eduardo Habkost
Quoted 4 lines

I think doing so will be an issue for the migration. Consider your above use case, a SEV guest is running on EPYC with cbitpos=47 and if we migrate to some $NEXT AMD CPU which uses need to use cbitpos=48 and we will fail to resume the guest on destination after migrating.

Exactly, in other words these two options are part of the guest ABI, and QEMU promises to never make the guest ABI depend on the host hardware unless you're using "-cpu host".

Quoted 6 lines

This is not entirely true; while MAXPHYADDR is constant downstream unless using "-cpu host", in practice that behavior is wrong and a guest could misbehave if passed a MAXPHYADDR that is different from the host's.

I think this is the same, and management software will have to live with it.

I think they are very far from being equivalent.

In practice guests don't seem to mind if we don't perfectly emulate behavior that depend on MAXPHYADDR, and live-migration between hosts with different MAXPHYADDR works.

But if you tell the guest the wrong C-bit location, guests are likely to rely on it and break. Migration between hosts with different C-bit locations won't work, will it?

Signature · 1 line
Eduardo
maintainer
#15 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 5 lines · Eduardo Habkost
Quoted 5 lines
Quoted 4 lines

Exactly, in other words these two options are part of the guest ABI, and QEMU promises to never make the guest ABI depend on the host hardware unless you're using "-cpu host".

Quoted 7 lines
Quoted 5 lines

This is not entirely true; while MAXPHYADDR is constant downstream unless using "-cpu host", in practice that behavior is wrong and a guest could misbehave if passed a MAXPHYADDR that is different from the host's.

I think this is the same, and management software will have to live with it.

I think they are very far from being equivalent.

Right, I only meant to say that guest ABI actually does depend on the host hardware, even outside of "-cpu host".

Quoted 3 lines

But if you tell the guest the wrong C-bit location, guests are likely to rely on it and break. Migration between hosts with different C-bit locations won't work, will it?

It won't---but as long as the destination hosts fails fast when the C-bit location is wrong, it's okay. What matters is that we don't run guest code with the wrong C bit, as you noted.

Paolo

#16 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 17 lines · Paolo Bonzini
Quoted 5 lines · Eduardo Habkost
Quoted 5 lines
Quoted 4 lines

Exactly, in other words these two options are part of the guest ABI, and QEMU promises to never make the guest ABI depend on the host hardware unless you're using "-cpu host".

Quoted 7 lines
Quoted 5 lines

This is not entirely true; while MAXPHYADDR is constant downstream unless using "-cpu host", in practice that behavior is wrong and a guest could misbehave if passed a MAXPHYADDR that is different from the host's.

I think this is the same, and management software will have to live with it.

I think they are very far from being equivalent.

Right, I only meant to say that guest ABI actually does depend on the host hardware, even outside of "-cpu host".

Quoted 7 lines
Quoted 3 lines

But if you tell the guest the wrong C-bit location, guests are likely to rely on it and break. Migration between hosts with different C-bit locations won't work, will it?

It won't---but as long as the destination hosts fails fast when the C-bit location is wrong, it's okay. What matters is that we don't run guest code with the wrong C bit, as you noted.

Are you proposing we change the default to simply use cbitpos from the host?

I would agree with this only if we make QEMU able to prevent live migration to a host with mismatching cbitpos.

Signature · 1 line
Eduardo
#17 lore raw

Re: [PATCH v12 08/28] target/i386: add Secure Encrypted Virtulization (SEV) object

Quoted 22 lines · * Eduardo Habkost ([email protected]) wrote:
Quoted 21 lines · Paolo Bonzini
Quoted 5 lines · Eduardo Habkost
Quoted 5 lines
Quoted 4 lines

Exactly, in other words these two options are part of the guest ABI, and QEMU promises to never make the guest ABI depend on the host hardware unless you're using "-cpu host".

Quoted 7 lines
Quoted 5 lines

This is not entirely true; while MAXPHYADDR is constant downstream unless using "-cpu host", in practice that behavior is wrong and a guest could misbehave if passed a MAXPHYADDR that is different from the host's.

I think this is the same, and management software will have to live with it.

I think they are very far from being equivalent.

Right, I only meant to say that guest ABI actually does depend on the host hardware, even outside of "-cpu host".

Quoted 3 lines

But if you tell the guest the wrong C-bit location, guests are likely to rely on it and break. Migration between hosts with different C-bit locations won't work, will it?

Quoted 6 lines
Quoted 3 lines

It won't---but as long as the destination hosts fails fast when the C-bit location is wrong, it's okay. What matters is that we don't run guest code with the wrong C bit, as you noted.

Are you proposing we change the default to simply use cbitpos from the host?

Hmm I don't like that idea; as an option that's fine, as the only way it's not.

Quoted 2 lines

I would agree with this only if we make QEMU able to prevent live migration to a host with mismatching cbitpos.

Yeh; especially since I suspect debugging stuff with a failed SEV migration like that is going to be really hard.

Dave

Quoted 2 lines
Signature · 1 line
Eduardo

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#18 lore raw

PATCH v12 9/28 qmp: add query-sev command

In reply to Brijesh Singh

The QMP query command can used to retrieve the SEV information when memory encryption is enabled on AMD platform.

Cc Eric Blake <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++
 qapi/misc.json        | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/monitor.c |  8 ++++++
 3 files changed, 91 insertions(+)
monitor.c +7 -0
index 3f4ba902266b..2225cf5030dc 100644
@@ -983,6 +983,7 @@ static void qmp_unregister_commands_hack(void)
 #endif
 #ifndef TARGET_I386
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
+    qmp_unregister_command(&qmp_commands, "query-sev");
 #endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
@@ -4103,6 +4104,12 @@ void qmp_rtc_reset_reinjection(Error **errp)
 {
     error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
 }
+
+SevInfo *qmp_query_sev(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
+    return NULL;
+}
 #endif
 
 #ifndef TARGET_S390X
qapi/misc.json +76 -0

79 lines not on this page · raw message

target/i386/monitor.c +8 -0
index 55ea10deb8ef..27b99adf395b 100644
@@ -30,6 +30,8 @@
 #include "hw/i386/pc.h"
 #include "sysemu/kvm.h"
 #include "hmp.h"
+#include "sev_i386.h"
+#include "qapi/qapi-commands-misc.h"
 
 
 static void print_pte(Monitor *mon, CPUArchState *env, hwaddr addr,
@@ -665,3 +667,9 @@ void hmp_info_io_apic(Monitor *mon, const QDict *qdict)
         ioapic_dump_state(mon, qdict);
     }
 }
+
+SevInfo *qmp_query_sev(Error **errp)
+{
+    error_setg(errp, "SEV feature is not available");
+    return NULL;
+}
Signature · 1 line
2.14.3
#19 lore raw

Re: [PATCH v12 09/28] qmp: add query-sev command

Quoted 24 lines · Brijesh Singh

The QMP query command can used to retrieve the SEV information when memory encryption is enabled on AMD platform.

Cc Eric Blake <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++
 qapi/misc.json        | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/monitor.c |  8 ++++++
 3 files changed, 91 insertions(+)
qapi/misc.json +1 -0
index bd04469a4ba2..14681729f8fc 100644
@@ -3201,3 +3201,79 @@
 # Since: 2.9
 ##
 { 'command': 'query-vm-generation-id', 'returns': 'GuidInfo' }
+

qapi/misc.json line 3214

Quoted 10 lines
qapi/misc.json +10 -0
+
+##
+# @SevState:
+#
+# An enumeration of SEV state information used during @query-sev.
+#
+# @uninit: The guest is uninitialized.
+#
+# @lupdate: The guest is currently being launched and plaintext and
+#           reigster state is being imported.

Typo s/reigster/register/

Quoted 4 lines
qapi/misc.json +4 -0
+#
+# @lsecret: The guest is currently being launched and ciphertext data
+#           are being imported.
+#

qapi/misc.json line 3228

Quoted 10 lines
qapi/misc.json +10 -0
+# @running: The guest is fully launched or migrated in.
+#
+# @supdate: The guest is currently being migrated out to another machine.
+#
+# @rupdate: The guest is currently being migrated from another machine.
+#
+# Since: 2.12
+##
+{ 'enum': 'SevState',
+  'data': ['uninit', 'lupdate', 'lsecret', 'running', 'supdate', 'rupdate' ] }

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#20 lore raw

PATCH v12 10/28 include: add psp-sev.h header file

In reply to Brijesh Singh

The header file provide the ioctl command and structure to communicate with /dev/sev device.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 linux-headers/linux/psp-sev.h | 142 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
 create mode 100644 linux-headers/linux/psp-sev.h
linux-headers/linux/psp-sev.h new file +142 -0
new file mode 100644
index 000000000000..33e247471ae0
@@ -0,0 +1,142 @@
+/*
+ * Userspace interface for AMD Secure Encrypted Virtualization (SEV)
+ * platform management commands.
+ *
+ * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
+ *
+ * Author: Brijesh Singh <[email protected]>
+ *
+ * SEV spec 0.14 is available at:
+ * http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __PSP_SEV_USER_H__
+#define __PSP_SEV_USER_H__
+
+#include <linux/types.h>
+
+/**
+ * SEV platform commands
+ */
+enum {
+	SEV_FACTORY_RESET = 0,
+	SEV_PLATFORM_STATUS,
+	SEV_PEK_GEN,
+	SEV_PEK_CSR,
+	SEV_PDH_GEN,
+	SEV_PDH_CERT_EXPORT,
+	SEV_PEK_CERT_IMPORT,
+
+	SEV_MAX,
+};
+
+/**
+ * SEV Firmware status code
+ */
+typedef enum {
+	SEV_RET_SUCCESS = 0,
+	SEV_RET_INVALID_PLATFORM_STATE,
+	SEV_RET_INVALID_GUEST_STATE,
+	SEV_RET_INAVLID_CONFIG,
+	SEV_RET_INVALID_LEN,
+	SEV_RET_ALREADY_OWNED,
+	SEV_RET_INVALID_CERTIFICATE,
+	SEV_RET_POLICY_FAILURE,
+	SEV_RET_INACTIVE,
+	SEV_RET_INVALID_ADDRESS,
+	SEV_RET_BAD_SIGNATURE,
+	SEV_RET_BAD_MEASUREMENT,
+	SEV_RET_ASID_OWNED,
+	SEV_RET_INVALID_ASID,
+	SEV_RET_WBINVD_REQUIRED,
+	SEV_RET_DFFLUSH_REQUIRED,
+	SEV_RET_INVALID_GUEST,
+	SEV_RET_INVALID_COMMAND,
+	SEV_RET_ACTIVE,
+	SEV_RET_HWSEV_RET_PLATFORM,
+	SEV_RET_HWSEV_RET_UNSAFE,
+	SEV_RET_UNSUPPORTED,
+	SEV_RET_MAX,
+} sev_ret_code;
+
+/**
+ * struct sev_user_data_status - PLATFORM_STATUS command parameters
+ *
+ * @major: major API version
+ * @minor: minor API version
+ * @state: platform state
+ * @flags: platform config flags
+ * @build: firmware build id for API version
+ * @guest_count: number of active guests
+ */
+struct sev_user_data_status {
+	__u8 api_major;				/* Out */
+	__u8 api_minor;				/* Out */
+	__u8 state;				/* Out */
+	__u32 flags;				/* Out */
+	__u8 build;				/* Out */
+	__u32 guest_count;			/* Out */
+} __attribute__((packed));
+
+/**
+ * struct sev_user_data_pek_csr - PEK_CSR command parameters
+ *
+ * @address: PEK certificate chain
+ * @length: length of certificate
+ */
+struct sev_user_data_pek_csr {
+	__u64 address;				/* In */
+	__u32 length;				/* In/Out */
+} __attribute__((packed));
+
+/**
+ * struct sev_user_data_cert_import - PEK_CERT_IMPORT command parameters
+ *
+ * @pek_address: PEK certificate chain
+ * @pek_len: length of PEK certificate
+ * @oca_address: OCA certificate chain
+ * @oca_len: length of OCA certificate
+ */
+struct sev_user_data_pek_cert_import {
+	__u64 pek_cert_address;			/* In */
+	__u32 pek_cert_len;			/* In */
+	__u64 oca_cert_address;			/* In */
+	__u32 oca_cert_len;			/* In */
+} __attribute__((packed));
+
+/**
+ * struct sev_user_data_pdh_cert_export - PDH_CERT_EXPORT command parameters
+ *
+ * @pdh_address: PDH certificate address
+ * @pdh_len: length of PDH certificate
+ * @cert_chain_address: PDH certificate chain
+ * @cert_chain_len: length of PDH certificate chain
+ */
+struct sev_user_data_pdh_cert_export {
+	__u64 pdh_cert_address;			/* In */
+	__u32 pdh_cert_len;			/* In/Out */
+	__u64 cert_chain_address;		/* In */
+	__u32 cert_chain_len;			/* In/Out */
+} __attribute__((packed));
+
+/**
+ * struct sev_issue_cmd - SEV ioctl parameters
+ *
+ * @cmd: SEV commands to execute
+ * @opaque: pointer to the command structure
+ * @error: SEV FW return code on failure
+ */
+struct sev_issue_cmd {
+	__u32 cmd;				/* In */
+	__u64 data;				/* In */
+	__u32 error;				/* Out */
+} __attribute__((packed));
+
+#define SEV_IOC_TYPE		'S'
+#define SEV_ISSUE_CMD	_IOWR(SEV_IOC_TYPE, 0x0, struct sev_issue_cmd)
+
+#endif /* __PSP_USER_SEV_H */
Signature · 1 line
2.14.3
#21 lore raw

Re: [PATCH v12 10/28] include: add psp-sev.h header file

Quoted 23 lines · Brijesh Singh

The header file provide the ioctl command and structure to communicate with /dev/sev device.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 linux-headers/linux/psp-sev.h | 142 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
 create mode 100644 linux-headers/linux/psp-sev.h
linux-headers/linux/psp-sev.h new file +5 -0
new file mode 100644
index 000000000000..33e247471ae0
@@ -0,0 +1,142 @@
+/*
+ * Userspace interface for AMD Secure Encrypted Virtualization (SEV)
+ * platform management commands.
+ *
+ * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.

linux-headers/linux/psp-sev.h line 15

Quoted 10 lines
linux-headers/linux/psp-sev.h new file +10 -0
+ *
+ * Author: Brijesh Singh <[email protected]>
+ *
+ * SEV spec 0.14 is available at:
+ * http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */

Most of the files you've added are GPLv2-or-later. Is this one intentionally GPLv2-only instead ?

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#22 lore raw

Re: [PATCH v12 10/28] include: add psp-sev.h header file

Quoted 29 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 28 lines · Brijesh Singh

The header file provide the ioctl command and structure to communicate with /dev/sev device.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 linux-headers/linux/psp-sev.h | 142 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
 create mode 100644 linux-headers/linux/psp-sev.h
linux-headers/linux/psp-sev.h new file +10 -0
new file mode 100644
index 000000000000..33e247471ae0
@@ -0,0 +1,142 @@
+/*
+ * Userspace interface for AMD Secure Encrypted Virtualization (SEV)
+ * platform management commands.
+ *
+ * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
+ *
+ * Author: Brijesh Singh <[email protected]>
+ *
+ * SEV spec 0.14 is available at:
+ * http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Quoted 8 lines

linux-headers/linux/psp-sev.h line 15

Quoted 5 lines
linux-headers/linux/psp-sev.h new file +5 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */

Most of the files you've added are GPLv2-or-later. Is this one intentionally GPLv2-only instead ?

This is just a sync'd kernel header (which should just get sync'd with the sync script).

Dave

Quoted 6 lines

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#23 lore raw

Re: [PATCH v12 10/28] include: add psp-sev.h header file

Quoted 39 lines · Dr. David Alan Gilbert
Quoted 29 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 28 lines · Brijesh Singh

The header file provide the ioctl command and structure to communicate with /dev/sev device.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 linux-headers/linux/psp-sev.h | 142 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
 create mode 100644 linux-headers/linux/psp-sev.h
linux-headers/linux/psp-sev.h new file +10 -0
new file mode 100644
index 000000000000..33e247471ae0
@@ -0,0 +1,142 @@
+/*
+ * Userspace interface for AMD Secure Encrypted Virtualization (SEV)
+ * platform management commands.
+ *
+ * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
+ *
+ * Author: Brijesh Singh <[email protected]>
+ *
+ * SEV spec 0.14 is available at:
+ * http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Quoted 8 lines

linux-headers/linux/psp-sev.h line 15

Quoted 5 lines
linux-headers/linux/psp-sev.h new file +5 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */

Most of the files you've added are GPLv2-or-later. Is this one intentionally GPLv2-only instead ?

Quoted 2 lines

This is just a sync'd kernel header (which should just get sync'd with the sync script).

Ok, no problem then.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#24 lore raw

PATCH v12 11/28 sev/i386: add command to initialize the memory encryption context

In reply to Brijesh Singh

When memory encryption is enabled, KVM_SEV_INIT command is used to initialize the platform. The command loads the SEV related persistent data from non-volatile storage and initializes the platform context. This command should be first issued before invoking any other guest commands provided by the SEV firmware.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 accel/kvm/kvm-all.c       |  16 ++++
 include/sysemu/sev.h      |  22 +++++
 stubs/Makefile.objs       |   1 +
 stubs/sev.c               |  21 +++++
 target/i386/Makefile.objs |   2 +-
 target/i386/monitor.c     |  11 ++-
 target/i386/sev-stub.c    |  41 +++++++++
 target/i386/sev.c         | 224 ++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/sev_i386.h    |  24 +++++
 target/i386/trace-events  |   3 +
 10 files changed, 362 insertions(+), 3 deletions(-)
 create mode 100644 include/sysemu/sev.h
 create mode 100644 stubs/sev.c
 create mode 100644 target/i386/sev-stub.c
accel/kvm/kvm-all.c +16 -0
index b91fcb7160d3..a6473522be11 100644
@@ -38,6 +38,7 @@
 #include "qemu/event_notifier.h"
 #include "trace.h"
 #include "hw/irq.h"
+#include "sysemu/sev.h"
 
 #include "hw/boards.h"
 
@@ -103,6 +104,9 @@ struct KVMState
 #endif
     KVMMemoryListener memory_listener;
     QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus;
+
+    /* memory encryption */
+    void *memcrypt_handle;
 };
 
 KVMState *kvm_state;
@@ -1636,6 +1640,18 @@ static int kvm_init(MachineState *ms)
 
     kvm_state = s;
 
+    /*
+     * if memory encryption object is specified then initialize the memory
+     * encryption context.
+     */
+    if (ms->memory_encryption) {
+        kvm_state->memcrypt_handle = sev_guest_init(ms->memory_encryption);
+        if (!kvm_state->memcrypt_handle) {
+            ret = -1;
+            goto err;
+        }
+    }
+
     ret = kvm_arch_init(ms, s);
     if (ret < 0) {
         goto err;
include/sysemu/sev.h new file +22 -0
new file mode 100644
index 000000000000..3f6a26e92789
@@ -0,0 +1,22 @@
+/*
+ * QEMU Secure Encrypted Virutualization (SEV) support
+ *
+ * Copyright: Advanced Micro Devices, 2016-2018
+ *
+ * Authors:
+ *  Brijesh Singh <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_SEV_H
+#define QEMU_SEV_H
+
+#include "sysemu/kvm.h"
+
+void *sev_guest_init(const char *id);
+int sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len);
+void sev_set_debug_ops(void *handle, MemoryRegion *mr);
+#endif
stubs/Makefile.objs +1 -0
index 2d59d8409162..31b36fdfdb88 100644
@@ -43,3 +43,4 @@ stub-obj-y += xen-common.o
 stub-obj-y += xen-hvm.o
 stub-obj-y += pci-host-piix.o
 stub-obj-y += ram-block.o
+stub-obj-y += sev.o
stubs/sev.c new file +21 -0
new file mode 100644
index 000000000000..4a5cc5569e5f
@@ -0,0 +1,21 @@
+/*
+ * QEMU SEV stub
+ *
+ * Copyright Advanced Micro Devices 2018
+ *
+ * Authors:
+ *      Brijesh Singh <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "sysemu/sev.h"
+
+void *sev_guest_init(const char *id)
+{
+    return NULL;
+}
target/i386/Makefile.objs +1 -1
index 76aeaeae2750..741cb080eb17 100644
@@ -5,7 +5,7 @@ obj-$(CONFIG_TCG) += int_helper.o mem_helper.o misc_helper.o mpx_helper.o
 obj-$(CONFIG_TCG) += seg_helper.o smm_helper.o svm_helper.o
 obj-$(CONFIG_SOFTMMU) += machine.o arch_memory_mapping.o arch_dump.o monitor.o
 obj-$(CONFIG_KVM) += kvm.o hyperv.o sev.o
-obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
+obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o sev-stub.o
 # HAX support
 ifdef CONFIG_WIN32
 obj-$(CONFIG_HAX) += hax-all.o hax-mem.o hax-windows.o
target/i386/monitor.c +9 -2
index 27b99adf395b..29de61996371 100644
@@ -670,6 +670,13 @@ void hmp_info_io_apic(Monitor *mon, const QDict *qdict)
 
 SevInfo *qmp_query_sev(Error **errp)
 {
-    error_setg(errp, "SEV feature is not available");
-    return NULL;
+    SevInfo *info;
+
+    info = sev_get_info();
+    if (!info) {
+        error_setg(errp, "SEV feature is not available");
+        return NULL;
+    }
+
+    return info;
 }
target/i386/sev-stub.c new file +41 -0

41 lines not on this page · raw message

target/i386/sev.c +224 -0

246 lines not on this page · raw message

target/i386/sev_i386.h +24 -0
index caf879c3b874..924cebcab02d 100644
@@ -17,7 +17,9 @@
 #include "qom/object.h"
 #include "qapi/error.h"
 #include "sysemu/kvm.h"
+#include "sysemu/sev.h"
 #include "qemu/error-report.h"
+#include "qapi/qapi-commands-misc.h"
 
 #define SEV_POLICY_NODBG        0x1
 #define SEV_POLICY_NOKS         0x2
@@ -30,6 +32,12 @@
 #define QSEV_GUEST_INFO(obj)                  \
     OBJECT_CHECK(QSevGuestInfo, (obj), TYPE_QSEV_GUEST_INFO)
 
+extern bool sev_enabled(void);
+extern uint64_t sev_get_me_mask(void);
+extern SevInfo *sev_get_info(void);
+extern uint32_t sev_get_cbit_position(void);
+extern uint32_t sev_get_reduced_phys_bits(void);
+
 typedef struct QSevGuestInfo QSevGuestInfo;
 typedef struct QSevGuestInfoClass QSevGuestInfoClass;
 
@@ -58,4 +66,20 @@ struct QSevGuestInfoClass {
     ObjectClass parent_class;
 };
 
+struct SEVState {
+    QSevGuestInfo *sev_info;
+    uint8_t api_major;
+    uint8_t api_minor;
+    uint8_t build_id;
+    uint32_t policy;
+    uint64_t me_mask;
+    uint32_t cbitpos;
+    uint32_t reduced_phys_bits;
+    uint32_t handle;
+    int sev_fd;
+    SevState state;
+};
+
+typedef struct SEVState SEVState;
+
 #endif
target/i386/trace-events +3 -0
index 3153fd445488..797b716751b7 100644
@@ -5,3 +5,6 @@ kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %"
 kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d"
 kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d"
 kvm_x86_update_msi_routes(int num) "Updated %d MSI routes"
+
+# target/i386/sev.c
+kvm_sev_init(void) ""
Signature · 1 line
2.14.3
#25 lore raw

Re: [PATCH v12 11/28] sev/i386: add command to initialize the memory encryption context

Quoted 51 lines · Brijesh Singh

When memory encryption is enabled, KVM_SEV_INIT command is used to initialize the platform. The command loads the SEV related persistent data from non-volatile storage and initializes the platform context. This command should be first issued before invoking any other guest commands provided by the SEV firmware.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 accel/kvm/kvm-all.c       |  16 ++++
 include/sysemu/sev.h      |  22 +++++
 stubs/Makefile.objs       |   1 +
 stubs/sev.c               |  21 +++++
 target/i386/Makefile.objs |   2 +-
 target/i386/monitor.c     |  11 ++-
 target/i386/sev-stub.c    |  41 +++++++++
 target/i386/sev.c         | 224 ++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/sev_i386.h    |  24 +++++
 target/i386/trace-events  |   3 +
 10 files changed, 362 insertions(+), 3 deletions(-)
 create mode 100644 include/sysemu/sev.h
 create mode 100644 stubs/sev.c
 create mode 100644 target/i386/sev-stub.c
target/i386/sev.c +24 -0
+static const char *const sev_fw_errlist[] = {
+    "",
+    "Platform state is invalid",
+    "Guest state is invalid",
+    "Platform configuration is invalid",
+    "Buffer too small",
+    "Platform is already owned",
+    "Certificate is invalid",
+    "Policy is not allowed",
+    "Guest is not active",
+    "Invalid address",
+    "Bad signature",
+    "Bad measurement",
+    "Asid is already owned",
+    "Invalid ASID",
+    "WBINVD is required",
+    "DF_FLUSH is required",
+    "Guest handle is invalid",
+    "Invalid command",
+    "Guest is active",
+    "Hardware error",
+    "Hardware unsafe",
+    "Feature not supported",
+    "Invalid parameter"

target/i386/sev.c line 102

Quoted 10 lines
target/i386/sev.c +9 -0
+};
+
+#define SEV_FW_MAX_ERROR      ARRAY_SIZE(sev_fw_errlist)
+
 
+static const char *
+fw_error_to_str(int code)
+{
+    if (code >= SEV_FW_MAX_ERROR) {
+        return "unknown error";

Seems '0' is not an error, and negative numbers are invalid
too, so augment this with '|| code <= 0' to avoid bad array
access on negative numbers

target/i386/sev.c line 106

Quoted 4 lines
target/i386/sev.c +4 -0
+    }
+
+    return sev_fw_errlist[code];
+}

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#26 lore raw

PATCH v12 12/28 sev/i386: register the guest memory range which may contain encrypted data

In reply to Brijesh Singh

When SEV is enabled, the hardware encryption engine uses a tweak such that the two identical plaintext at different location will have a different ciphertexts. So swapping or moving a ciphertexts of two guest pages will not result in plaintexts being swapped. Hence relocating a physical backing pages of the SEV guest will require some additional steps in KVM driver. The KVM_MEMORY_ENCRYPT_{UN,}REG_REGION ioctl can be used to register/unregister the guest memory region which may contain the encrypted data. KVM driver will internally handle the relocating physical backing pages of registered memory regions.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/sev.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 target/i386/trace-events |  2 ++
 2 files changed, 44 insertions(+)
target/i386/sev.c +42 -0
index 288612e1aa46..4f85035d5203 100644
@@ -105,6 +105,46 @@ fw_error_to_str(int code)
     return sev_fw_errlist[code];
 }
 
+static void
+sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size)
+{
+    int r;
+    struct kvm_enc_region range;
+
+    range.addr = (__u64)host;
+    range.size = size;
+
+    trace_kvm_memcrypt_register_region(host, size);
+    r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_REG_REGION, &range);
+    if (r) {
+        error_report("%s: failed to register region (%p+%#lx) error '%s'",
+                     __func__, host, size, strerror(errno));
+        exit(1);
+    }
+}
+
+static void
+sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size)
+{
+    int r;
+    struct kvm_enc_region range;
+
+    range.addr = (__u64)host;
+    range.size = size;
+
+    trace_kvm_memcrypt_unregister_region(host, size);
+    r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_UNREG_REGION, &range);
+    if (r) {
+        error_report("%s: failed to unregister region (%p+%#lx)",
+                     __func__, host, size);
+    }
+}
+
+static struct RAMBlockNotifier sev_ram_notifier = {
+    .ram_block_added = sev_ram_block_added,
+    .ram_block_removed = sev_ram_block_removed,
+};
+
 static void
 qsev_guest_finalize(Object *obj)
 {
@@ -436,6 +476,8 @@ sev_guest_init(const char *id)
         goto err;
     }
 
+    ram_block_notifier_add(&sev_ram_notifier);
+
     return s;
 err:
     g_free(sev_state);
target/i386/trace-events +2 -0
index 797b716751b7..ffa3d2250425 100644
@@ -8,3 +8,5 @@ kvm_x86_update_msi_routes(int num) "Updated %d MSI routes"
 
 # target/i386/sev.c
 kvm_sev_init(void) ""
+kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%lu"
+kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu"
Signature · 1 line
2.14.3
#27 lore raw

PATCH v12 13/28 kvm: introduce memory encryption APIs

In reply to Brijesh Singh

Inorder to integerate the Secure Encryption Virtualization (SEV) support add few high-level memory encryption APIs which can be used for encrypting the guest memory region.

---
 accel/kvm/kvm-all.c    | 30 ++++++++++++++++++++++++++++++
 accel/stubs/kvm-stub.c | 14 ++++++++++++++
 include/sysemu/kvm.h   | 25 +++++++++++++++++++++++++
 3 files changed, 69 insertions(+)
accel/kvm/kvm-all.c +30 -0
index a6473522be11..975ba3845234 100644
@@ -107,6 +107,8 @@ struct KVMState
 
     /* memory encryption */
     void *memcrypt_handle;
+    int (*memcrypt_encrypt_data)(void *handle, uint8_t *ptr, uint64_t len);
+    void (*memcrypt_debug_ops)(void *handle, MemoryRegion *mr);
 };
 
 KVMState *kvm_state;
@@ -142,6 +144,34 @@ int kvm_get_max_memslots(void)
     return s->nr_slots;
 }
 
+bool kvm_memcrypt_enabled(void)
+{
+    if (kvm_state && kvm_state->memcrypt_handle) {
+        return true;
+    }
+
+    return false;
+}
+
+int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len)
+{
+    if (kvm_state->memcrypt_handle &&
+        kvm_state->memcrypt_encrypt_data) {
+        return kvm_state->memcrypt_encrypt_data(kvm_state->memcrypt_handle,
+                                              ptr, len);
+    }
+
+    return 1;
+}
+
+void kvm_memcrypt_set_debug_ops(MemoryRegion *mr)
+{
+    if (kvm_state->memcrypt_handle &&
+        kvm_state->memcrypt_debug_ops) {
+        kvm_state->memcrypt_debug_ops(kvm_state->memcrypt_handle, mr);
+    }
+}
+
 static KVMSlot *kvm_get_free_slot(KVMMemoryListener *kml)
 {
     KVMState *s = kvm_state;
accel/stubs/kvm-stub.c +14 -0
index c964af3e1c97..5739712a67e3 100644
@@ -105,6 +105,20 @@ int kvm_on_sigbus(int code, void *addr)
     return 1;
 }
 
+bool kvm_memcrypt_enabled(void)
+{
+    return false;
+}
+
+int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len)
+{
+  return 1;
+}
+
+void kvm_memcrypt_set_debug_ops(MemoryRegion *mr)
+{
+}
+
 #ifndef CONFIG_USER_ONLY
 int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev)
 {
include/sysemu/kvm.h +25 -0
index 85002ac49a54..d69bd1ff2b07 100644
@@ -231,6 +231,31 @@ int kvm_destroy_vcpu(CPUState *cpu);
  */
 bool kvm_arm_supports_user_irq(void);
 
+/**
+ * kvm_memcrypt_enabled - return boolean indicating whether memory encryption
+ *                        is enabled
+ * Returns: 1 memory encryption is enabled
+ *          0 memory encryption is disabled
+ */
+bool kvm_memcrypt_enabled(void);
+
+/**
+ * kvm_memcrypt_encrypt_data: encrypt the memory range
+ *
+ * Return: 1 failed to encrypt the range
+ *         0 succesfully encrypted memory region
+ */
+int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len);
+
+/**
+ * kvm_memcrypt_set_debug_ram_ops: set debug_ram_ops callback
+ *
+ * When debug_ram_ops is set, debug access to this memory region will use
+ * memory encryption APIs.
+ */
+void kvm_memcrypt_set_debug_ops(MemoryRegion *mr);
+
+
 #ifdef NEED_CPU_H
 #include "cpu.h"
 
Signature · 1 line
2.14.3
#28 lore raw

PATCH v12 14/28 hmp: add 'info sev' command

In reply to Brijesh Singh

The command can be used to show the SEV information when memory encryption is enabled on AMD platform.

Cc Eric Blake <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 hmp-commands-info.hx  | 16 ++++++++++++++++
 hmp.h                 |  1 +
 target/i386/monitor.c | 20 ++++++++++++++++++++
 3 files changed, 37 insertions(+)
hmp-commands-info.hx +16 -0
index ad590a4ffb2b..ddfcd5adcca6 100644
@@ -867,6 +867,22 @@ Display the amount of initially allocated and present hotpluggable (if
 enabled) memory in bytes.
 ETEXI
 
+#if defined(TARGET_I386)
+    {
+        .name       = "sev",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show SEV information",
+        .cmd        = hmp_info_sev,
+    },
+#endif
+
+STEXI
+@item info sev
+@findex info sev
+Show SEV information.
+ETEXI
+
 STEXI
 @end table
 ETEXI
hmp.h +1 -0
index b89733876de1..4e2ec375b0f4 100644
@@ -143,5 +143,6 @@ void hmp_info_ramblock(Monitor *mon, const QDict *qdict);
 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict);
 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict);
 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict);
+void hmp_info_sev(Monitor *mon, const QDict *qdict);
 
 #endif
target/i386/monitor.c +20 -0
index 29de61996371..4d9019626031 100644
@@ -29,6 +29,7 @@
 #include "qapi/qmp/qdict.h"
 #include "hw/i386/pc.h"
 #include "sysemu/kvm.h"
+#include "sysemu/sev.h"
 #include "hmp.h"
 #include "sev_i386.h"
 #include "qapi/qapi-commands-misc.h"
@@ -680,3 +681,22 @@ SevInfo *qmp_query_sev(Error **errp)
 
     return info;
 }
+
+void hmp_info_sev(Monitor *mon, const QDict *qdict)
+{
+    SevInfo *info = sev_get_info();
+
+    if (info && info->enabled) {
+        monitor_printf(mon, "handle: %d\n", info->handle);
+        monitor_printf(mon, "state: %s\n", SevState_str(info->state));
+        monitor_printf(mon, "build: %d\n", info->build_id);
+        monitor_printf(mon, "api version: %d.%d\n",
+                       info->api_major, info->api_minor);
+        monitor_printf(mon, "debug: %s\n",
+                       info->policy & SEV_POLICY_NODBG ? "off" : "on");
+        monitor_printf(mon, "key-sharing: %s\n",
+                       info->policy & SEV_POLICY_NOKS ? "off" : "on");
+    } else {
+        monitor_printf(mon, "SEV is not enabled\n");
+    }
+}
Signature · 1 line
2.14.3
#29 lore raw

PATCH v12 15/28 sev/i386: add command to create launch memory encryption context

In reply to Brijesh Singh

The KVM_SEV_LAUNCH_START command creates a new VM encryption key (VEK). The encryption key created with the command will be used for encrypting the bootstrap images (such as guest bios).

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/sev.c        | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/trace-events |  2 ++
 2 files changed, 88 insertions(+)
target/i386/sev.c +86 -0
index 4f85035d5203..eee693745103 100644
@@ -105,6 +105,17 @@ fw_error_to_str(int code)
     return sev_fw_errlist[code];
 }
 
+static void
+sev_set_guest_state(SevState new_state)
+{
+    assert(new_state < SEV_STATE__MAX);
+    assert(sev_state);
+
+    trace_kvm_sev_change_state(SevState_str(sev_state->state),
+                               SevState_str(new_state));
+    sev_state->state = new_state;
+}
+
 static void
 sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size)
 {
@@ -406,6 +417,75 @@ sev_get_info(void)
     return info;
 }
 
+static int
+sev_read_file_base64(const char *filename, guchar **data, gsize *len)
+{
+    gsize sz;
+    gchar *base64;
+    GError *error = NULL;
+
+    if (!g_file_get_contents(filename, &base64, &sz, &error)) {
+        error_report("failed to read '%s' (%s)", filename, error->message);
+        return -1;
+    }
+
+    *data = g_base64_decode(base64, len);
+    return 0;
+}
+
+static int
+sev_launch_start(SEVState *s)
+{
+    gsize sz;
+    int ret = 1;
+    int fw_error;
+    QSevGuestInfo *sev = s->sev_info;
+    struct kvm_sev_launch_start *start;
+    guchar *session = NULL, *dh_cert = NULL;
+
+    start = g_new0(struct kvm_sev_launch_start, 1);
+
+    start->handle = object_property_get_int(OBJECT(sev), "handle",
+                                            &error_abort);
+    start->policy = object_property_get_int(OBJECT(sev), "policy",
+                                            &error_abort);
+    if (sev->session_file) {
+        if (sev_read_file_base64(sev->session_file, &session, &sz) < 0) {
+            return 1;
+        }
+        start->session_uaddr = (unsigned long)session;
+        start->session_len = sz;
+    }
+
+    if (sev->dh_cert_file) {
+        if (sev_read_file_base64(sev->dh_cert_file, &dh_cert, &sz) < 0) {
+            return 1;
+        }
+        start->dh_uaddr = (unsigned long)dh_cert;
+        start->dh_len = sz;
+    }
+
+    trace_kvm_sev_launch_start(start->policy, session, dh_cert);
+    ret = sev_ioctl(s->sev_fd, KVM_SEV_LAUNCH_START, start, &fw_error);
+    if (ret < 0) {
+        error_report("%s: LAUNCH_START ret=%d fw_error=%d '%s'",
+                __func__, ret, fw_error, fw_error_to_str(fw_error));
+        return 1;
+    }
+
+    object_property_set_int(OBJECT(sev), start->handle, "handle",
+                            &error_abort);
+    sev_set_guest_state(SEV_STATE_LUPDATE);
+    s->handle = start->handle;
+    s->policy = start->policy;
+
+    g_free(start);
+    g_free(session);
+    g_free(dh_cert);
+
+    return 0;
+}
+
 void *
 sev_guest_init(const char *id)
 {
@@ -476,6 +556,12 @@ sev_guest_init(const char *id)
         goto err;
     }
 
+    ret = sev_launch_start(s);
+    if (ret) {
+        error_report("%s: failed to create encryption context", __func__);
+        goto err;
+    }
+
     ram_block_notifier_add(&sev_ram_notifier);
 
     return s;
target/i386/trace-events +2 -0
index ffa3d2250425..9402251e9991 100644
@@ -10,3 +10,5 @@ kvm_x86_update_msi_routes(int num) "Updated %d MSI routes"
 kvm_sev_init(void) ""
 kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%lu"
 kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu"
+kvm_sev_change_state(const char *old, const char *new) "%s -> %s"
+kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p"
Signature · 1 line
2.14.3
#30 lore raw

PATCH v12 16/28 sev/i386: add command to encrypt guest memory region

In reply to Brijesh Singh

The KVM_SEV_LAUNCH_UPDATE_DATA command is used to encrypt a guest memory region using the VM Encryption Key created using LAUNCH_START.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 accel/kvm/kvm-all.c      |  2 ++
 stubs/sev.c              |  5 +++++
 target/i386/sev.c        | 43 +++++++++++++++++++++++++++++++++++++++++++
 target/i386/trace-events |  1 +
 4 files changed, 51 insertions(+)
accel/kvm/kvm-all.c +2 -0
index 975ba3845234..411aa87719e6 100644
@@ -1680,6 +1680,8 @@ static int kvm_init(MachineState *ms)
             ret = -1;
             goto err;
         }
+
+        kvm_state->memcrypt_encrypt_data = sev_encrypt_data;
     }
 
     ret = kvm_arch_init(ms, s);
stubs/sev.c +5 -0
index 4a5cc5569e5f..2e20f3b73a5b 100644
@@ -15,6 +15,11 @@
 #include "qemu-common.h"
 #include "sysemu/sev.h"
 
+int sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len)
+{
+    return 1;
+}
+
 void *sev_guest_init(const char *id)
 {
     return NULL;
target/i386/sev.c +43 -0

61 lines not on this page · raw message

target/i386/trace-events +1 -0
index 9402251e9991..c0cd8e93217f 100644
@@ -12,3 +12,4 @@ kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%lu"
 kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu"
 kvm_sev_change_state(const char *old, const char *new) "%s -> %s"
 kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p"
+kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64
Signature · 1 line
2.14.3
#31 lore raw

PATCH v12 17/28 target/i386: encrypt bios rom

In reply to Brijesh Singh

SEV requires that guest bios must be encrypted before booting the guest.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 hw/i386/pc_sysfw.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/i386/pc_sysfw.c +13 -0
index 4325575e7d82..73ac783f2055 100644
@@ -113,6 +113,8 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)
     pflash_t *system_flash;
     MemoryRegion *flash_mem;
     char name[64];
+    void *flash_ptr;
+    int ret, flash_size;
 
     sector_bits = 12;
     sector_size = 1 << sector_bits;
@@ -169,6 +171,17 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)
         if (unit == 0) {
             flash_mem = pflash_cfi01_get_memory(system_flash);
             pc_isa_bios_init(rom_memory, flash_mem, size);
+
+            /* Encrypt the pflash boot ROM */
+            if (kvm_memcrypt_enabled()) {
+                flash_ptr = memory_region_get_ram_ptr(flash_mem);
+                flash_size = memory_region_size(flash_mem);
+                ret = kvm_memcrypt_encrypt_data(flash_ptr, flash_size);
+                if (ret) {
+                    error_report("failed to encrypt pflash rom");
+                    exit(1);
+                }
+            }
         }
     }
 }
Signature · 1 line
2.14.3
#32 lore raw

PATCH v12 18/28 sev/i386: add support to LAUNCH_MEASURE command

In reply to Brijesh Singh

During machine creation we encrypted the guest bios image, the LAUNCH_MEASURE command can be used to retrieve the measurement of the encrypted memory region. This 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. VM management tools like libvirt can query the measurement using query-sev-launch-measure QMP command.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/sev-stub.c   |  5 ++++
 target/i386/sev.c        | 63 ++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/sev_i386.h   |  2 ++
 target/i386/trace-events |  1 +
 4 files changed, 71 insertions(+)
target/i386/sev-stub.c +5 -0
index c86d8c139237..2f61c32ec975 100644
@@ -39,3 +39,8 @@ uint32_t sev_get_reduced_phys_bits(void)
 {
     return 0;
 }
+
+char *sev_get_launch_measurement(void)
+{
+    return NULL;
+}
target/i386/sev.c +63 -0

75 lines not on this page · raw message

target/i386/sev_i386.h +2 -0
index 924cebcab02d..6e370775770e 100644
@@ -37,6 +37,7 @@ extern uint64_t sev_get_me_mask(void);
 extern SevInfo *sev_get_info(void);
 extern uint32_t sev_get_cbit_position(void);
 extern uint32_t sev_get_reduced_phys_bits(void);
+extern char *sev_get_launch_measurement(void);
 
 typedef struct QSevGuestInfo QSevGuestInfo;
 typedef struct QSevGuestInfoClass QSevGuestInfoClass;
@@ -78,6 +79,7 @@ struct SEVState {
     uint32_t handle;
     int sev_fd;
     SevState state;
+    gchar *measurement;
 };
 
 typedef struct SEVState SEVState;
target/i386/trace-events +1 -0
index c0cd8e93217f..f7a1a1e6b85c 100644
@@ -13,3 +13,4 @@ kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu"
 kvm_sev_change_state(const char *old, const char *new) "%s -> %s"
 kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p"
 kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64
+kvm_sev_launch_measurement(const char *value) "data %s"
Signature · 1 line
2.14.3
#33 lore raw

PATCH v12 19/28 sev/i386: finalize the SEV guest launch flow

In reply to Brijesh Singh

SEV launch flow requires us to issue LAUNCH_FINISH command before guest is ready to run.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/sev.c        | 29 +++++++++++++++++++++++++++++
 target/i386/trace-events |  1 +
 2 files changed, 30 insertions(+)
target/i386/sev.c +29 -0
index 7ad7eaf600a7..ce199d259f7a 100644
@@ -578,6 +578,34 @@ static Notifier sev_machine_done_notify = {
     .notify = sev_launch_get_measure,
 };
 
+static void
+sev_launch_finish(SEVState *s)
+{
+    int ret, error;
+
+    trace_kvm_sev_launch_finish();
+    ret = sev_ioctl(sev_state->sev_fd, KVM_SEV_LAUNCH_FINISH, 0, &error);
+    if (ret) {
+        error_report("%s: LAUNCH_FINISH ret=%d fw_error=%d '%s'",
+                     __func__, ret, error, fw_error_to_str(error));
+        exit(1);
+    }
+
+    sev_set_guest_state(SEV_STATE_RUNNING);
+}
+
+static void
+sev_vm_state_change(void *opaque, int running, RunState state)
+{
+    SEVState *s = opaque;
+
+    if (running) {
+        if (!sev_check_state(SEV_STATE_RUNNING)) {
+            sev_launch_finish(s);
+        }
+    }
+}
+
 void *
 sev_guest_init(const char *id)
 {
@@ -656,6 +684,7 @@ sev_guest_init(const char *id)
 
     ram_block_notifier_add(&sev_ram_notifier);
     qemu_add_machine_init_done_notifier(&sev_machine_done_notify);
+    qemu_add_vm_change_state_handler(sev_vm_state_change, s);
 
     return s;
 err:
target/i386/trace-events +1 -0
index f7a1a1e6b85c..b1fbde6e40fe 100644
@@ -14,3 +14,4 @@ kvm_sev_change_state(const char *old, const char *new) "%s -> %s"
 kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p"
 kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64
 kvm_sev_launch_measurement(const char *value) "data %s"
+kvm_sev_launch_finish(void) ""
Signature · 1 line
2.14.3
#34 lore raw

PATCH v12 20/28 hw/i386: set ram_debug_ops when memory encryption is enabled

In reply to Brijesh Singh

When memory encryption is enabled, the guest RAM and boot flash ROM will contain the encrypted data. By setting the debug ops allow us to invoke encryption APIs when accessing the memory for the debug purposes.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
---
 hw/i386/pc.c       | 9 +++++++++
 hw/i386/pc_sysfw.c | 6 ++++++
 2 files changed, 15 insertions(+)
hw/i386/pc.c +9 -0
index 35fcb6efdfb9..69364b6856b5 100644
@@ -1360,6 +1360,15 @@ void pc_memory_init(PCMachineState *pcms,
         e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
     }
 
+    /*
+     * When memory encryption is enabled, the guest RAM will be encrypted with
+     * a guest unique key. Set the debug ops so that any debug access to the
+     * guest RAM will go through the memory encryption APIs.
+     */
+    if (kvm_memcrypt_enabled()) {
+        kvm_memcrypt_set_debug_ops(ram);
+    }
+
     if (!pcmc->has_reserved_memory &&
         (machine->ram_slots ||
          (machine->maxram_size > machine->ram_size))) {
hw/i386/pc_sysfw.c +6 -0
index 73ac783f2055..845240f97293 100644
@@ -181,6 +181,12 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)
                     error_report("failed to encrypt pflash rom");
                     exit(1);
                 }
+
+                /*
+                 * The pflash ROM is encrypted, set the debug ops so that any
+                 * debug accesses will use memory encryption APIs.
+                 */
+                kvm_memcrypt_set_debug_ops(flash_mem);
             }
         }
     }
Signature · 1 line
2.14.3
#35 lore raw

PATCH v12 21/28 sev/i386: add debug encrypt and decrypt commands

In reply to Brijesh Singh

KVM_SEV_DBG_DECRYPT and KVM_SEV_DBG_ENCRYPT commands are used for decrypting and encrypting guest memory region. The command works only if the guest policy allows the debugging.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 accel/kvm/kvm-all.c      |  1 +
 stubs/sev.c              |  4 ++++
 target/i386/sev.c        | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/trace-events |  1 +
 4 files changed, 63 insertions(+)
accel/kvm/kvm-all.c +1 -0
index 411aa87719e6..8089173491dd 100644
@@ -1682,6 +1682,7 @@ static int kvm_init(MachineState *ms)
         }
 
         kvm_state->memcrypt_encrypt_data = sev_encrypt_data;
+        kvm_state->memcrypt_debug_ops = sev_set_debug_ops;
     }
 
     ret = kvm_arch_init(ms, s);
stubs/sev.c +4 -0
index 2e20f3b73a5b..73f5c7f93a67 100644
@@ -15,6 +15,10 @@
 #include "qemu-common.h"
 #include "sysemu/sev.h"
 
+void sev_set_debug_ops(void *handle, MemoryRegion *mr)
+{
+}
+
 int sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len)
 {
     return 1;
target/i386/sev.c +57 -0

75 lines not on this page · raw message

target/i386/trace-events +1 -0
index b1fbde6e40fe..00aa6e98d810 100644
@@ -15,3 +15,4 @@ kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session
 kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64
 kvm_sev_launch_measurement(const char *value) "data %s"
 kvm_sev_launch_finish(void) ""
+kvm_sev_debug(const char *op, const uint8_t *src, uint8_t *dst, int len) "(%s) src %p dst %p len %d"
Signature · 1 line
2.14.3
#36 lore raw

PATCH v12 22/28 target/i386: clear C-bit when walking SEV guest page table

In reply to Brijesh Singh

In SEV-enabled guest the pte entry will have C-bit set, we need to clear the C-bit when walking the page table.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/helper.c  | 31 +++++++++++++----------
 target/i386/monitor.c | 68 +++++++++++++++++++++++++++++++++------------------
 2 files changed, 62 insertions(+), 37 deletions(-)
target/i386/helper.c +18 -13
index 58fb6eec562a..dc5c7005cf13 100644
@@ -21,6 +21,7 @@
 #include "cpu.h"
 #include "exec/exec-all.h"
 #include "sysemu/kvm.h"
+#include "sev_i386.h"
 #include "kvm_i386.h"
 #ifndef CONFIG_USER_ONLY
 #include "sysemu/sysemu.h"
@@ -732,6 +733,9 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
     int32_t a20_mask;
     uint32_t page_offset;
     int page_size;
+    uint64_t me_mask;
+
+    me_mask = sev_get_me_mask();
 
     a20_mask = x86_get_a20_mask(env);
     if (!(env->cr[0] & CR0_PG_MASK)) {
@@ -755,25 +759,25 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
             }
 
             if (la57) {
-                pml5e_addr = ((env->cr[3] & ~0xfff) +
+                pml5e_addr = ((env->cr[3] & ~0xfff & me_mask) +
                         (((addr >> 48) & 0x1ff) << 3)) & a20_mask;
-                pml5e = ldq_phys_debug(cs, pml5e_addr);
+                pml5e = ldq_phys_debug(cs, pml5e_addr) & me_mask;
                 if (!(pml5e & PG_PRESENT_MASK)) {
                     return -1;
                 }
             } else {
-                pml5e = env->cr[3];
+                pml5e = env->cr[3] & me_mask;
             }
 
             pml4e_addr = ((pml5e & PG_ADDRESS_MASK) +
                     (((addr >> 39) & 0x1ff) << 3)) & a20_mask;
-            pml4e = ldq_phys_debug(cs, pml4e_addr);
+            pml4e = ldq_phys_debug(cs, pml4e_addr) & me_mask;
             if (!(pml4e & PG_PRESENT_MASK)) {
                 return -1;
             }
             pdpe_addr = ((pml4e & PG_ADDRESS_MASK) +
                          (((addr >> 30) & 0x1ff) << 3)) & a20_mask;
-            pdpe = x86_ldq_phys(cs, pdpe_addr);
+            pdpe = ldq_phys_debug(cs, pdpe_addr) & me_mask;
             if (!(pdpe & PG_PRESENT_MASK)) {
                 return -1;
             }
@@ -786,16 +790,16 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
         } else
 #endif
         {
-            pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
-                a20_mask;
-            pdpe = ldq_phys_debug(cs, pdpe_addr);
+            pdpe_addr = ((env->cr[3] & ~0x1f & me_mask) + ((addr >> 27) & 0x18))
+                          & a20_mask;
+            pdpe = ldq_phys_debug(cs, pdpe_addr) & me_mask;
             if (!(pdpe & PG_PRESENT_MASK))
                 return -1;
         }
 
         pde_addr = ((pdpe & PG_ADDRESS_MASK) +
                     (((addr >> 21) & 0x1ff) << 3)) & a20_mask;
-        pde = ldq_phys_debug(cs, pde_addr);
+        pde = ldq_phys_debug(cs, pde_addr) & me_mask;
         if (!(pde & PG_PRESENT_MASK)) {
             return -1;
         }
@@ -808,7 +812,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
             pte_addr = ((pde & PG_ADDRESS_MASK) +
                         (((addr >> 12) & 0x1ff) << 3)) & a20_mask;
             page_size = 4096;
-            pte = ldq_phys_debug(cs, pte_addr);
+            pte = ldq_phys_debug(cs, pte_addr) & me_mask;
         }
         if (!(pte & PG_PRESENT_MASK)) {
             return -1;
@@ -817,8 +821,9 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
         uint32_t pde;
 
         /* page directory entry */
-        pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & a20_mask;
-        pde = ldl_phys_debug(cs, pde_addr);
+        pde_addr = ((env->cr[3] & ~0xfff & me_mask) + ((addr >> 20) & 0xffc))
+                     & a20_mask;
+        pde = ldl_phys_debug(cs, pde_addr) & me_mask;
         if (!(pde & PG_PRESENT_MASK))
             return -1;
         if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
@@ -827,7 +832,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
         } else {
             /* page directory entry */
             pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & a20_mask;
-            pte = ldl_phys_debug(cs, pte_addr);
+            pte = ldl_phys_debug(cs, pte_addr) & me_mask;
             if (!(pte & PG_PRESENT_MASK)) {
                 return -1;
             }
target/i386/monitor.c +44 -24

209 lines not on this page · raw message

Signature · 1 line
2.14.3
#37 lore raw

PATCH v12 23/28 qmp: add query-sev-launch-measure command

In reply to Brijesh Singh

The command can be used by libvirt to retrieve the measurement of SEV guest. This measurement is a signature of the memory contents that was encrypted through the LAUNCH_UPDATE_DATA.

Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++++
 qapi/misc.json        | 29 +++++++++++++++++++++++++++++
 target/i386/monitor.c | 17 +++++++++++++++++
 3 files changed, 53 insertions(+)
monitor.c +7 -0
index 2225cf5030dc..d53ecc5ddab3 100644
@@ -984,6 +984,7 @@ static void qmp_unregister_commands_hack(void)
 #ifndef TARGET_I386
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
     qmp_unregister_command(&qmp_commands, "query-sev");
+    qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
 #endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
@@ -4110,6 +4111,12 @@ SevInfo *qmp_query_sev(Error **errp)
     error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
     return NULL;
 }
+
+SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
+    return NULL;
+}
 #endif
 
 #ifndef TARGET_S390X
qapi/misc.json +29 -0
index 14681729f8fc..a39c43aa64b1 100644
@@ -3277,3 +3277,32 @@
 #
 ##
 { 'command': 'query-sev', 'returns': 'SevInfo' }
+
+##
+# @SevLaunchMeasureInfo:
+#
+# SEV Guest Launch measurement information
+#
+# @data: the measurement value encoded in base64
+#
+# Since: 2.12
+#
+##
+{ 'struct': 'SevLaunchMeasureInfo', 'data': {'data': 'str'} }
+
+##
+# @query-sev-launch-measure:
+#
+# Query the SEV guest launch information.
+#
+# Returns: The @SevLaunchMeasureInfo for the guest
+#
+# Since: 2.12
+#
+# Example:
+#
+# -> { "execute": "query-sev-launch-measure" }
+# <- { "return": { "data": "4l8LXeNlSPUDlXPJG5966/8%YZ" } }
+#
+##
+{ 'command': 'query-sev-launch-measure', 'returns': 'SevLaunchMeasureInfo' }
target/i386/monitor.c +17 -0
index 7df31c3cdf1b..e5596bbc0fc2 100644
@@ -720,3 +720,20 @@ void hmp_info_sev(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "SEV is not enabled\n");
     }
 }
+
+SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
+{
+    char *data;
+    SevLaunchMeasureInfo *info;
+
+    data = sev_get_launch_measurement();
+    if (!data) {
+        error_setg(errp, "Measurement is not available");
+        return NULL;
+    }
+
+    info = g_malloc0(sizeof(*info));
+    info->data = data;
+
+    return info;
+}
Signature · 1 line
2.14.3
Reviewed-by on v12 23/28
#38 lore raw

Re: [PATCH v12 23/28] qmp: add query-sev-launch-measure command

Quoted 88 lines · Brijesh Singh

The command can be used by libvirt to retrieve the measurement of SEV guest. This measurement is a signature of the memory contents that was encrypted through the LAUNCH_UPDATE_DATA.

Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++++
 qapi/misc.json        | 29 +++++++++++++++++++++++++++++
 target/i386/monitor.c | 17 +++++++++++++++++
 3 files changed, 53 insertions(+)
monitor.c +7 -0
index 2225cf5030dc..d53ecc5ddab3 100644
@@ -984,6 +984,7 @@ static void qmp_unregister_commands_hack(void)
 #ifndef TARGET_I386
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
     qmp_unregister_command(&qmp_commands, "query-sev");
+    qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
 #endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
@@ -4110,6 +4111,12 @@ SevInfo *qmp_query_sev(Error **errp)
     error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
     return NULL;
 }
+
+SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
+    return NULL;
+}
 #endif
 
 #ifndef TARGET_S390X
qapi/misc.json +29 -0
index 14681729f8fc..a39c43aa64b1 100644
@@ -3277,3 +3277,32 @@
 #
 ##
 { 'command': 'query-sev', 'returns': 'SevInfo' }
+
+##
+# @SevLaunchMeasureInfo:
+#
+# SEV Guest Launch measurement information
+#
+# @data: the measurement value encoded in base64
+#
+# Since: 2.12
+#
+##
+{ 'struct': 'SevLaunchMeasureInfo', 'data': {'data': 'str'} }
+
+##
+# @query-sev-launch-measure:
+#
+# Query the SEV guest launch information.
+#
+# Returns: The @SevLaunchMeasureInfo for the guest
+#
+# Since: 2.12
+#
+# Example:
+#
+# -> { "execute": "query-sev-launch-measure" }
+# <- { "return": { "data": "4l8LXeNlSPUDlXPJG5966/8%YZ" } }
+#
+##
+{ 'command': 'query-sev-launch-measure', 'returns': 'SevLaunchMeasureInfo' }
target/i386/monitor.c +3 -0
index 7df31c3cdf1b..e5596bbc0fc2 100644
@@ -720,3 +720,20 @@ void hmp_info_sev(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "SEV is not enabled\n");
     }
 }
+
+SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
+{

target/i386/monitor.c void hmp_info_sev(Monitor *mon, const QDict *qdict) · line 735

Quoted 10 lines
target/i386/monitor.c +10 -0
+    char *data;
+    SevLaunchMeasureInfo *info;
+
+    data = sev_get_launch_measurement();
+    if (!data) {
+        error_setg(errp, "Measurement is not available");
+        return NULL;
+    }
+
+    info = g_malloc0(sizeof(*info));

Minor preference for g_new0(SevLaunchMeasureInfo, 1);

target/i386/monitor.c void hmp_info_sev(Monitor *mon, const QDict *qdict) · line 739

Quoted 4 lines
target/i386/monitor.c +4 -0
+    info->data = data;
+
+    return info;
+}

Regardless though

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#39 lore raw

PATCH v12 24/28 sev/i386: add migration blocker

In reply to Brijesh Singh

SEV guest migration is not implemented yet.

---
 target/i386/sev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
target/i386/sev.c +13 -0
index f687e9e40e32..b9bfce95246a 100644
@@ -24,12 +24,14 @@
 #include "sev_i386.h"
 #include "sysemu/sysemu.h"
 #include "trace.h"
+#include "migration/blocker.h"
 
 #define DEFAULT_GUEST_POLICY    0x1 /* disable debug */
 #define DEFAULT_SEV_DEVICE      "/dev/sev"
 
 static SEVState *sev_state;
 static MemoryRegionRAMReadWriteOps  sev_ops;
+static Error *sev_mig_blocker;
 
 static const char *const sev_fw_errlist[] = {
     "",
@@ -583,6 +585,7 @@ static void
 sev_launch_finish(SEVState *s)
 {
     int ret, error;
+    Error *local_err = NULL;
 
     trace_kvm_sev_launch_finish();
     ret = sev_ioctl(sev_state->sev_fd, KVM_SEV_LAUNCH_FINISH, 0, &error);
@@ -593,6 +596,16 @@ sev_launch_finish(SEVState *s)
     }
 
     sev_set_guest_state(SEV_STATE_RUNNING);
+
+    /* add migration blocker */
+    error_setg(&sev_mig_blocker,
+               "SEV: Migration is not implemented");
+    ret = migrate_add_blocker(sev_mig_blocker, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        error_free(sev_mig_blocker);
+        exit(1);
+    }
 }
 
 static void
Signature · 1 line
2.14.3
maintainer
#40 lore raw

Re: [PATCH v12 24/28] sev/i386: add migration blocker

target/i386/sev.c sev_launch_finish(SEVState *s) · line 609

Quoted 12 lines · Brijesh Singh
     sev_set_guest_state(SEV_STATE_RUNNING);
target/i386/sev.c +10 -0
+
+    /* add migration blocker */
+    error_setg(&sev_mig_blocker,
+               "SEV: Migration is not implemented");
+    ret = migrate_add_blocker(sev_mig_blocker, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        error_free(sev_mig_blocker);
+        exit(1);
+    }
 }

I think this should be in sev_guest_init instead? Does migration transfer the measurement, or is it lost forever? Not a blocker though.

Paolo

#41 lore raw

Re: [PATCH v12 24/28] sev/i386: add migration blocker

Quoted 13 lines · Paolo Bonzini

target/i386/sev.c sev_launch_finish(SEVState *s) · line 609

Quoted 12 lines · Brijesh Singh
     sev_set_guest_state(SEV_STATE_RUNNING);
target/i386/sev.c +10 -0
+
+    /* add migration blocker */
+    error_setg(&sev_mig_blocker,
+               "SEV: Migration is not implemented");
+    ret = migrate_add_blocker(sev_mig_blocker, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        error_free(sev_mig_blocker);
+        exit(1);
+    }
 }
Quoted 2 lines

I think this should be in sev_guest_init instead? Does migration transfer the measurement, or is it lost forever? Not a blocker though.

The launch measurement does not get transferred during the migration. During migration we get totally different measurement which is wrapped with transport key etc and that need to send to destination. IIRC, in my first attempt I was adding this blocker in sev_guest_init() but migration_add_blocker() was failing because  sev_guest_init() is called before the migration_object_init().

Quoted 2 lines

Paolo

maintainer
#42 lore raw

Re: [PATCH v12 24/28] sev/i386: add migration blocker

Quoted 19 lines · Brijesh Singh
Quoted 13 lines · Paolo Bonzini

target/i386/sev.c sev_launch_finish(SEVState *s) · line 609

Quoted 12 lines · Brijesh Singh
     sev_set_guest_state(SEV_STATE_RUNNING);
target/i386/sev.c +10 -0
+
+    /* add migration blocker */
+    error_setg(&sev_mig_blocker,
+               "SEV: Migration is not implemented");
+    ret = migrate_add_blocker(sev_mig_blocker, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        error_free(sev_mig_blocker);
+        exit(1);
+    }
 }
Quoted 2 lines

I think this should be in sev_guest_init instead? Does migration transfer the measurement, or is it lost forever? Not a blocker though.

Quoted 6 lines

The launch measurement does not get transferred during the migration. During migration we get totally different measurement which is wrapped with transport key etc and that need to send to destination. IIRC, in my first attempt I was adding this blocker in sev_guest_init() but migration_add_blocker() was failing because  sev_guest_init() is called before the migration_object_init().

I see, thanks. I'm not going to touch this part anyway. :)

We can fix it during the freeze period.

Paolo

#43 lore raw

PATCH v12 25/28 cpu/i386: populate CPUID 0x8000_001F when SEV is active

In reply to Brijesh Singh

When SEV is enabled, CPUID 0x8000_001F should provide additional information regarding the feature (such as which page table bit is used to mark the pages as encrypted etc).

The details for memory encryption CPUID is available in AMD APM
(https://support.amd.com/TechDocs/24594.pdf) Section E.4.17

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/cpu.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
target/i386/cpu.c +13 -0
index 2c04645ceac9..647f792ba123 100644
@@ -26,6 +26,7 @@
 #include "sysemu/hvf.h"
 #include "sysemu/cpus.h"
 #include "kvm_i386.h"
+#include "sev_i386.h"
 
 #include "qemu/error-report.h"
 #include "qemu/option.h"
@@ -3612,6 +3613,13 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
         *ecx = 0;
         *edx = 0;
         break;
+    case 0x8000001F:
+        *eax = sev_enabled() ? 0x2 : 0;
+        *ebx = sev_get_cbit_position();
+        *ebx |= sev_get_reduced_phys_bits() << 6;
+        *ecx = 0;
+        *edx = 0;
+        break;
     default:
         /* reserved values: zero */
         *eax = 0;
@@ -4041,6 +4049,11 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
         if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
             x86_cpu_adjust_level(cpu, &env->cpuid_min_xlevel, 0x8000000A);
         }
+
+        /* SEV requires CPUID[0x8000001F] */
+        if (sev_enabled()) {
+            x86_cpu_adjust_level(cpu, &env->cpuid_min_xlevel, 0x8000001F);
+        }
     }
 
     /* Set cpuid_*level* based on cpuid_min_*level, if not explicitly set */
Signature · 1 line
2.14.3
#44 lore raw

PATCH v12 26/28 qmp: add query-sev-capabilities command

In reply to Brijesh Singh

The command can be used by libvirt to query the SEV capabilities.

Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++++
 qapi/misc.json        | 42 ++++++++++++++++++++++++++++++++++++++++++
 target/i386/monitor.c |  6 ++++++
 3 files changed, 55 insertions(+)
monitor.c +7 -0
index d53ecc5ddab3..29ce695a80d5 100644
@@ -985,6 +985,7 @@ static void qmp_unregister_commands_hack(void)
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
     qmp_unregister_command(&qmp_commands, "query-sev");
     qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
+    qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
 #endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
@@ -4117,6 +4118,12 @@ SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
     error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
     return NULL;
 }
+
+SevCapability *qmp_query_sev_capabilities(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
+    return NULL;
+}
 #endif
 
 #ifndef TARGET_S390X
qapi/misc.json +42 -0

45 lines not on this page · raw message

target/i386/monitor.c +6 -0
index e5596bbc0fc2..33e6bade693b 100644
@@ -737,3 +737,9 @@ SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
 
     return info;
 }
+
+SevCapability *qmp_query_sev_capabilities(Error **errp)
+{
+    error_setg(errp, "SEV feature is not available");
+    return NULL;
+}
Signature · 1 line
2.14.3
Reviewed-by on v12 26/28
#45 lore raw

Re: [PATCH v12 26/28] qmp: add query-sev-capabilities command

Quoted 45 lines · Brijesh Singh

The command can be used by libvirt to query the SEV capabilities.

Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++++
 qapi/misc.json        | 42 ++++++++++++++++++++++++++++++++++++++++++
 target/i386/monitor.c |  6 ++++++
 3 files changed, 55 insertions(+)
monitor.c +7 -0
index d53ecc5ddab3..29ce695a80d5 100644
@@ -985,6 +985,7 @@ static void qmp_unregister_commands_hack(void)
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
     qmp_unregister_command(&qmp_commands, "query-sev");
     qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
+    qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
 #endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
@@ -4117,6 +4118,12 @@ SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
     error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
     return NULL;
 }
+
+SevCapability *qmp_query_sev_capabilities(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
+    return NULL;
+}
 #endif
 
 #ifndef TARGET_S390X
qapi/misc.json +0 -0
index a39c43aa64b1..37c89663d8f4 100644
@@ -3306,3 +3306,45 @@
 #
 ##
 { 'command': 'query-sev-launch-measure', 'returns': 'SevLaunchMeasureInfo' }

qapi/misc.json line 3318

Quoted 10 lines
qapi/misc.json +10 -0
+
+##
+# @SevCapability:
+#
+# The struct describes capability for a Secure Encrypted Virtualization
+# feature.
+#
+# @pdh:  Platform Diffie-Hellman key
+#
+# @cert-chain:  PDH certificate chain

Are either of these base64 encoded ? If so nice to document that.

qapi/misc.json line 3331

Quoted 13 lines
qapi/misc.json +13 -0
+#
+# @cbitpos: C-bit location in page table entry
+#
+# @reduced-phys-bits: Number of physical Address bit reduction when SEV is
+#                     enabled
+#
+# Since: 2.12
+##
+{ 'struct': 'SevCapability',
+  'data': { 'pdh': 'str',
+            'cert-chain': 'str',
+            'cbitpos': 'int',
+            'reduced-phys-bits': 'int'} }

Regardless of answer to above Q,

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#46 lore raw

Re: [PATCH v12 26/28] qmp: add query-sev-capabilities command

Quoted 49 lines · Daniel P. Berrangé
Quoted 48 lines · Brijesh Singh

The command can be used by libvirt to query the SEV capabilities.

Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 monitor.c             |  7 +++++++
 qapi/misc.json        | 42 ++++++++++++++++++++++++++++++++++++++++++
 target/i386/monitor.c |  6 ++++++
 3 files changed, 55 insertions(+)
monitor.c +7 -0
index d53ecc5ddab3..29ce695a80d5 100644
@@ -985,6 +985,7 @@ static void qmp_unregister_commands_hack(void)
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
     qmp_unregister_command(&qmp_commands, "query-sev");
     qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
+    qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
 #endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
@@ -4117,6 +4118,12 @@ SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
     error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
     return NULL;
 }
+
+SevCapability *qmp_query_sev_capabilities(Error **errp)
+{
+    error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
+    return NULL;
+}
 #endif
 
 #ifndef TARGET_S390X
qapi/misc.json +3 -0
index a39c43aa64b1..37c89663d8f4 100644
@@ -3306,3 +3306,45 @@
 #
 ##
 { 'command': 'query-sev-launch-measure', 'returns': 'SevLaunchMeasureInfo' }
+
+##
+# @SevCapability:
Quoted 8 lines

qapi/misc.json line 3318

Quoted 7 lines
qapi/misc.json +7 -0
+#
+# The struct describes capability for a Secure Encrypted Virtualization
+# feature.
+#
+# @pdh:  Platform Diffie-Hellman key
+#
+# @cert-chain:  PDH certificate chain

Are either of these base64 encoded ? If so nice to document that.

Yep, they are base64 encoded, I will update the doc.

Quoted 21 lines
Quoted 4 lines
qapi/misc.json +3 -0
+#
+# @cbitpos: C-bit location in page table entry
+#

qapi/misc.json line 3331

Quoted 10 lines
qapi/misc.json +10 -0
+# @reduced-phys-bits: Number of physical Address bit reduction when SEV is
+#                     enabled
+#
+# Since: 2.12
+##
+{ 'struct': 'SevCapability',
+  'data': { 'pdh': 'str',
+            'cert-chain': 'str',
+            'cbitpos': 'int',
+            'reduced-phys-bits': 'int'} }

Regardless of answer to above Q,

Regards,
Daniel

#47 lore raw

PATCH v12 27/28 sev/i386: add sev_get_capabilities()

In reply to Brijesh Singh

The function can be used to get the current SEV capabilities. The capabilities include platform diffie-hellman key (pdh) and certificate chain. The key can be provided to the external entities which wants to establish a trusted channel between SEV firmware and guest owner.

Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
---
 target/i386/monitor.c  | 11 +++++--
 target/i386/sev-stub.c |  5 +++
 target/i386/sev.c      | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/sev_i386.h |  1 +
 4 files changed, 98 insertions(+), 2 deletions(-)
target/i386/monitor.c +9 -2
index 33e6bade693b..79fa9bd7a3e3 100644
@@ -740,6 +740,13 @@ SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
 
 SevCapability *qmp_query_sev_capabilities(Error **errp)
 {
-    error_setg(errp, "SEV feature is not available");
-    return NULL;
+    SevCapability *data;
+
+    data = sev_get_capabilities();
+    if (!data) {
+        error_setg(errp, "SEV feature is not available");
+        return NULL;
+    }
+
+    return data;
 }
target/i386/sev-stub.c +5 -0
index 2f61c32ec975..59a003a4ebe6 100644
@@ -44,3 +44,8 @@ char *sev_get_launch_measurement(void)
 {
     return NULL;
 }
+
+SevCapability *sev_get_capabilities(void)
+{
+    return NULL;
+}
target/i386/sev.c +83 -0

89 lines not on this page · raw message

target/i386/sev_i386.h +1 -0
index 6e370775770e..b8622dfb1e49 100644
@@ -38,6 +38,7 @@ extern SevInfo *sev_get_info(void);
 extern uint32_t sev_get_cbit_position(void);
 extern uint32_t sev_get_reduced_phys_bits(void);
 extern char *sev_get_launch_measurement(void);
+extern SevCapability *sev_get_capabilities(void);
 
 typedef struct QSevGuestInfo QSevGuestInfo;
 typedef struct QSevGuestInfoClass QSevGuestInfoClass;
Signature · 1 line
2.14.3
#48 lore raw

PATCH v12 28/28 tests/qmp-test: blacklist sev specific qmp commands

In reply to Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 tests/qmp-test.c | 5 +++++
 1 file changed, 5 insertions(+)
tests/qmp-test.c +5 -0
index 22445d9ec258..7470c6b754bc 100644
@@ -204,6 +204,11 @@ static bool query_is_blacklisted(const char *cmd)
         "query-gic-capabilities", /* arm */
         /* Success depends on target-specific build configuration: */
         "query-pci",              /* CONFIG_PCI */
+        /* Success depends on launching SEV guest */
+        "query-sev-launch-measure",
+        /* Success depends on Host or Hypervisor SEV support */
+        "query-sev",
+        "query-sev-capabilities",
         NULL
     };
     int i;
Signature · 1 line
2.14.3
#49 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 10 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Quoted 29 lines
Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 tests/qmp-test.c | 5 +++++
 1 file changed, 5 insertions(+)
tests/qmp-test.c +5 -0
index 22445d9ec258..7470c6b754bc 100644
@@ -204,6 +204,11 @@ static bool query_is_blacklisted(const char *cmd)
         "query-gic-capabilities", /* arm */
         /* Success depends on target-specific build configuration: */
         "query-pci",              /* CONFIG_PCI */
+        /* Success depends on launching SEV guest */
+        "query-sev-launch-measure",
+        /* Success depends on Host or Hypervisor SEV support */
+        "query-sev",
+        "query-sev-capabilities",
         NULL
     };
     int i;
Signature · 1 line
2.14.3

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#50 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

Quoted 7 lines
Quoted 2 lines

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Sure, I can quickly send the updated patch series to address your this concern, but before spamming everyone's inbox I was wondering if I can get some indication whether this series will make into 2.12 merge.

Paolo, Eduardo and Richard,

Most of the changes are in x86 directory hence any thought if you are considering this series for 2.12 ? I have been testing the series with and without SEV support and so far have not ran into any issue. if you are not planning to pull this series in 2.12 then I will wait a bit longer to get more feedback before sending the updates to address Daniel's comment. thanks

Quoted 30 lines
Quoted 28 lines
Cc "Daniel P. Berrangé" <[email protected]>
Cc "Dr. David Alan Gilbert" <[email protected]>
Cc Markus Armbruster <[email protected]>
---
 tests/qmp-test.c | 5 +++++
 1 file changed, 5 insertions(+)
tests/qmp-test.c +5 -0
index 22445d9ec258..7470c6b754bc 100644
@@ -204,6 +204,11 @@ static bool query_is_blacklisted(const char *cmd)
         "query-gic-capabilities", /* arm */
         /* Success depends on target-specific build configuration: */
         "query-pci",              /* CONFIG_PCI */
+        /* Success depends on launching SEV guest */
+        "query-sev-launch-measure",
+        /* Success depends on Host or Hypervisor SEV support */
+        "query-sev",
+        "query-sev-capabilities",
         NULL
     };
     int i;
Signature · 1 line
2.14.3

Regards,
Daniel

#51 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 25 lines · Brijesh Singh
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

Quoted 7 lines
Quoted 2 lines

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Sure, I can quickly send the updated patch series to address your this concern, but before spamming everyone's inbox I was wondering if I can get some indication whether this series will make into 2.12 merge.

Quoted 8 lines

Paolo, Eduardo and Richard,

Most of the changes are in x86 directory hence any thought if you are considering this series for 2.12 ? I have been testing the series with and without SEV support and so far have not ran into any issue. if you are not planning to pull this series in 2.12 then I will wait a bit longer to get more feedback before sending the updates to address Daniel's comment. thanks

Trying to merge it before 2.12 soft freeze (next Tuesday) still looks like a reasonable goal to me. What do others think?

Signature · 1 line
Eduardo
#52 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 35 lines · Eduardo Habkost
Quoted 25 lines · Brijesh Singh
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

Quoted 7 lines
Quoted 2 lines

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Sure, I can quickly send the updated patch series to address your this concern, but before spamming everyone's inbox I was wondering if I can get some indication whether this series will make into 2.12 merge.

Quoted 8 lines

Paolo, Eduardo and Richard,

Most of the changes are in x86 directory hence any thought if you are considering this series for 2.12 ? I have been testing the series with and without SEV support and so far have not ran into any issue. if you are not planning to pull this series in 2.12 then I will wait a bit longer to get more feedback before sending the updates to address Daniel's comment. thanks

Quoted 2 lines

Trying to merge it before 2.12 soft freeze (next Tuesday) still looks like a reasonable goal to me. What do others think?

I've only really looked at the QAPI / QMP bits and they seem fine from pov of libvirt's needs - just very minor comments. So not objection from me on that area of the code.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#53 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

In reply to Eduardo Habkost
Quoted 35 lines · * Eduardo Habkost ([email protected]) wrote:
Quoted 25 lines · Brijesh Singh
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

Quoted 7 lines
Quoted 2 lines

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Sure, I can quickly send the updated patch series to address your this concern, but before spamming everyone's inbox I was wondering if I can get some indication whether this series will make into 2.12 merge.

Quoted 8 lines

Paolo, Eduardo and Richard,

Most of the changes are in x86 directory hence any thought if you are considering this series for 2.12 ? I have been testing the series with and without SEV support and so far have not ran into any issue. if you are not planning to pull this series in 2.12 then I will wait a bit longer to get more feedback before sending the updates to address Daniel's comment. thanks

Quoted 2 lines

Trying to merge it before 2.12 soft freeze (next Tuesday) still looks like a reasonable goal to me. What do others think?

I've only looked at a few general comments and things but it looks like it's getting there; I don't think it's had many comments from the KVM side yet.

Dave

Quoted 2 lines
Signature · 1 line
Eduardo

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

maintainer
#54 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 36 lines · Dr. David Alan Gilbert
Quoted 35 lines · * Eduardo Habkost ([email protected]) wrote:
Quoted 33 lines · Brijesh Singh
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

Quoted 7 lines
Quoted 2 lines

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Sure, I can quickly send the updated patch series to address your this concern, but before spamming everyone's inbox I was wondering if I can get some indication whether this series will make into 2.12 merge.

Paolo, Eduardo and Richard,

Most of the changes are in x86 directory hence any thought if you are considering this series for 2.12 ? I have been testing the series with and without SEV support and so far have not ran into any issue. if you are not planning to pull this series in 2.12 then I will wait a bit longer to get more feedback before sending the updates to address Daniel's comment. thanks

Quoted 6 lines
Quoted 2 lines

Trying to merge it before 2.12 soft freeze (next Tuesday) still looks like a reasonable goal to me. What do others think?

I've only looked at a few general comments and things but it looks like it's getting there; I don't think it's had many comments from the KVM side yet.

The KVM side is a pretty linear use of the kernel API. I'm not very happy with the debug API for MemoryRegions (but it's not really Brijesh's fault), so my plan would be to merge it without debug support.

Paolo

#55 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 34 lines · Paolo Bonzini
Quoted 33 lines · Dr. David Alan Gilbert
Quoted 32 lines · * Eduardo Habkost ([email protected]) wrote:
Quoted 31 lines · Brijesh Singh
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Brijesh Singh

Blacklist the following commands to fix the 'make check' failure.

query-sev-launch-measure: it returns meaninful data only when we launch SEV guest otherwise the command returns an error.

query-sev: it return an error when SEV is not available on host (e.g non X86 platform or KVM is disabled at the build time)

Quoted 7 lines
Quoted 2 lines

query-sev-capabilities: it returns an error when SEV feature is not available on host machine.

We generally expect 'make check' to succeed on every single patch in a series, so that 'git bisect' doesn't break.

So you should add each command to the blacklist in the same commit that introduced the failure in the first place.

Sure, I can quickly send the updated patch series to address your this concern, but before spamming everyone's inbox I was wondering if I can get some indication whether this series will make into 2.12 merge.

Paolo, Eduardo and Richard,

Most of the changes are in x86 directory hence any thought if you are considering this series for 2.12 ? I have been testing the series with and without SEV support and so far have not ran into any issue. if you are not planning to pull this series in 2.12 then I will wait a bit longer to get more feedback before sending the updates to address Daniel's comment. thanks

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Trying to merge it before 2.12 soft freeze (next Tuesday) still looks like a reasonable goal to me. What do others think?

I've only looked at a few general comments and things but it looks like it's getting there; I don't think it's had many comments from the KVM side yet.

The KVM side is a pretty linear use of the kernel API. I'm not very happy with the debug API for MemoryRegions (but it's not really Brijesh's fault), so my plan would be to merge it without debug support.

Thanks Paolo, I am working to drop the complete debug support and will send series very soon. I need to run some quick smoke test to make sure I don't break SEV guest support.

maintainer
#56 lore raw

Re: [PATCH v12 28/28] tests/qmp-test: blacklist sev specific qmp commands

Quoted 7 lines · Brijesh Singh
Quoted 3 lines

The KVM side is a pretty linear use of the kernel API. I'm not very happy with the debug API for MemoryRegions (but it's not really Brijesh's fault), so my plan would be to merge it without debug support.

Thanks Paolo, I am working to drop the complete debug support and will send series very soon. I need to run some quick smoke test to make sure I don't break SEV guest support.

Just test the pull request I'll send in less than an hour. :)

Paolo