Add SEV guest live migration support
cover Add SEV guest live migration support
AMD SEV encrypts the memory of VMs and because this encryption is done using an address tweak, the hypervisor will not be able to simply copy ciphertext between machines to migrate a VM. Instead the AMD SEV Key Management API provides a set of functions which the hypervisor can use to package a guest encrypted pages for migration, while maintaining the confidentiality provided by AMD SEV.
The patch series add the support required in Qemu to perform the SEV guest live migration. Before initiating the live migration a user should use newly added 'migrate-set-sev-info' command to pass the target machines certificate chain. See the docs/amd-memory-encryption.txt for further details.
The patch series depends on kernel patches available here:
https://marc.info/?l=kvm&m=156104873409876&w=2
The complete tree with patch is available at:
https://github.com/codomania/qemu/tree/sev-migration-rfc-v1
Brijesh Singh (12):
linux-headers: update kernel header to include SEV migration commands
kvm: introduce high-level API to support encrypted guest migration migration/ram: add support to send encrypted pages
kvm: add support to sync the page encryption state bitmap
doc: update AMD SEV API spec web link
doc: update AMD SEV to include Live migration flow target/i386: sev: do not create launch context for an incoming guest target.json: add migrate-set-sev-info command
target/i386: sev: add support to encrypt the outgoing page target/i386: sev: add support to load incoming encrypted page
migration: add support to migrate page encryption bitmap target/i386: sev: remove migration blocker
accel/kvm/kvm-all.c | 75 ++++++ accel/kvm/sev-stub.c | 28 ++ accel/stubs/kvm-stub.c | 30 +++ docs/amd-memory-encryption.txt | 46 +++- include/exec/ram_addr.h | 2 + include/sysemu/kvm.h | 33 +++ include/sysemu/sev.h | 9 + linux-headers/linux/kvm.h | 53 ++++ migration/ram.c | 121 ++++++++- qapi/target.json | 18 ++ target/i386/monitor.c | 10 + target/i386/sev-stub.c | 5 + target/i386/sev.c | 471 +++++++++++++++++++++++++++++++-- target/i386/sev_i386.h | 11 +- target/i386/trace-events | 9 + 15 files changed, 902 insertions(+), 19 deletions(-)
Signature · 1 line
2.17.1
RFC PATCH 1/12 linux-headers: update kernel header to include SEV migration commands
--- linux-headers/linux/kvm.h | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
linux-headers/linux/kvm.h +53 -0
@@ -492,6 +492,16 @@ struct kvm_dirty_log { }; }; +/* for KVM_GET_PAGE_ENC_BITMAP */ +struct kvm_page_enc_bitmap { +__u64 start; +__u64 num_pages; +union { +void *enc_bitmap;/* one bit per page */ +__u64 padding2; + }; +}; +/* for KVM_CLEAR_DIRTY_LOG */ struct kvm_clear_dirty_log {__u32 slot;
@@ -1451,6 +1461,9 @@ struct kvm_enc_region {/* Available with KVM_CAP_ARM_SVE */ #define KVM_ARM_VCPU_FINALIZE _IOW(KVMIO, 0xc2, int) +#define KVM_GET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc2, struct kvm_page_enc_bitmap) +#define KVM_SET_PAGE_ENC_BITMAP _IOW(KVMIO, 0xc3, struct kvm_page_enc_bitmap) +/* Secure Encrypted Virtualization command */ enum sev_cmd_id {/* Guest initialization commands */
@@ -1531,6 +1544,46 @@ struct kvm_sev_dbg {__u32 len; }; +struct kvm_sev_send_start { +__u32 policy; +__u64 pdh_cert_uaddr; +__u32 pdh_cert_len; +__u64 plat_cert_uaddr; +__u32 plat_cert_len; +__u64 amd_cert_uaddr; +__u32 amd_cert_len; +__u64 session_uaddr; +__u32 session_len; +}; + +struct kvm_sev_send_update_data { +__u64 hdr_uaddr; +__u32 hdr_len; +__u64 guest_uaddr; +__u32 guest_len; +__u64 trans_uaddr; +__u32 trans_len; +}; + +struct kvm_sev_receive_start { +__u32 handle; +__u32 policy; +__u64 pdh_uaddr; +__u32 pdh_len; +__u64 session_uaddr; +__u32 session_len; +}; + +struct kvm_sev_receive_update_data { +__u64 hdr_uaddr; +__u32 hdr_len; +__u64 guest_uaddr; +__u32 guest_len; +__u64 trans_uaddr; +__u32 trans_len; +}; + +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.17.1
RFC PATCH 2/12 kvm: introduce high-level API to support encrypted guest migration
In reply to Singh, BrijeshWhen memory encryption is enabled in VM, the guest pages will be encrypted with the guest-specific key, to protect the confidentiality of data in transit. To support the live migration we need to use platform specific hooks to access the guest memory.
The kvm_memcrypt_save_outgoing_page() can be used by the sender to write the encrypted pages and metadata associated with it on the socket.
The kvm_memcrypt_load_incoming_page() can be used by receiver to read the incoming encrypted pages from the socket and load into the guest memory.
Encrypted VMs have concept of private and shared memory. The private memory is encrypted with the guest-specific key, while shared memory may be encrypted with hyperivosr key. The KVM_{SET,GET}_PAGE_ENC_BITMAP ioctl can be used to get/set the bitmap from/to the hypervisor.
The kvm_memcrypt_sync_page_enc_bitmap() can be used by the sender to get the page encryption bitmap. The bitmap is used to determine the page state (private or shared).
The kvm_memcrypt_send_outgoing_page_enc_bitmap() can be used by the sender to write the page encryption bitmap on the socket.
The kvm_memcrypt_load_incoming_page_enc_bitmap() can be used by the receiver to read the page encryption bitmap from the socket.
--- accel/kvm/kvm-all.c | 68 ++++++++++++++++++++++++++++++++++++++++++ accel/kvm/sev-stub.c | 28 +++++++++++++++++ accel/stubs/kvm-stub.c | 30 +++++++++++++++++++ include/sysemu/kvm.h | 33 ++++++++++++++++++++ include/sysemu/sev.h | 9 ++++++ 5 files changed, 168 insertions(+)
accel/kvm/kvm-all.c +68 -0
@@ -109,6 +109,15 @@ struct KVMState/* memory encryption */ void *memcrypt_handle;int (*memcrypt_encrypt_data)(void *handle,uint8_t *ptr,uint64_t len); +int (*memcrypt_save_outgoing_page)(void *ehandle,QEMUFile *f, +uint8_t *ptr,uint32_t sz,uint64_t *bytes_sent); +int (*memcrypt_load_incoming_page)(void *ehandle,QEMUFile *f, +uint8_t *ptr); +int (*memcrypt_load_incoming_page_enc_bitmap)(void *ehandle,QEMUFile *f); +int (*memcrypt_save_outgoing_page_enc_bitmap)(void *ehandle,QEMUFile *f, +uint8_t *host,uint64_t length,unsigned long *bmap); +int (*memcrypt_sync_page_enc_bitmap)(void *ehandle,uint8_t *host, +uint64_t length,unsigned long *bmap); };KVMState *kvm_state;
@@ -164,6 +173,65 @@ int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len)return 1 ; } +int kvm_memcrypt_save_outgoing_page (QEMUFile *f,uint8_t *ptr, +uint32_t size,uint64_t *bytes_sent) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_save_outgoing_page) { +return kvm_state->memcrypt_save_outgoing_page (kvm_state->memcrypt_handle, + f, ptr, size, bytes_sent); + } + +return 1 ; +} + +int kvm_memcrypt_load_incoming_page (QEMUFile *f,uint8_t *ptr) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_load_incoming_page) { +return kvm_state->memcrypt_load_incoming_page (kvm_state->memcrypt_handle, + f, ptr); + } + +return 1 ; +} + +int kvm_memcrypt_load_incoming_page_enc_bitmap (QEMUFile *f) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_load_incoming_page_enc_bitmap) { +return kvm_state->memcrypt_load_incoming_page_enc_bitmap ( + kvm_state->memcrypt_handle, f); + } + +return 1 ; +} + +int kvm_memcrypt_save_outgoing_page_enc_bitmap (QEMUFile *f,uint8_t *host, +uint64_t length, +unsigned long *bmap) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_save_outgoing_page_enc_bitmap) { +return kvm_state->memcrypt_save_outgoing_page_enc_bitmap ( + kvm_state->memcrypt_handle, f, host, length, bmap); + } + +return 1 ; +} + +int kvm_memcrypt_sync_page_enc_bitmap (uint8_t *host,uint64_t length, +unsigned long *bmap) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_sync_page_enc_bitmap) { +return kvm_state->memcrypt_sync_page_enc_bitmap ( + kvm_state->memcrypt_handle, host, length, bmap); + } + +return 1 ; +} +static KVMSlot *kvm_get_free_slot (KVMMemoryListener *kml) {KVMState *s = kvm_state;
accel/kvm/sev-stub.c +28 -0
@@ -24,3 +24,31 @@ void *sev_guest_init(const char *id) {return NULL ; } + +int sev_save_outgoing_page (void *handle,QEMUFile *f,uint8_t *ptr, +uint32_t size,uint64_t *bytes_sent) +{ +return 1 ; +} + +int sev_load_incoming_page (void *handle,QEMUFile *f,uint8_t *ptr) +{ +return 1 ; +} + +int sev_load_incoming_page_enc_bitmap (void *handle,QEMUFile *f) +{ +return 1 ; +} + +int sev_save_outgoing_page_enc_bitmap (void *handle,QEMUFile *f, +unsigned long *bmap) +{ +return 1 ; +} + +int sev_sync_page_enc_bitmap (void *handle,uint8_t *host,uint64_t size, +unsigned long *bitmap) +{ +return 1 ; +}
accel/stubs/kvm-stub.c +30 -0
@@ -114,6 +114,36 @@ int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len)return 1 ; } +int kvm_memcrypt_save_outgoing_page (QEMUFile *f,uint8_t *ptr, +uint32_t size,uint64_t *bytes_sent) +{ +return 1 ; +} + +int kvm_memcrypt_load_incoming_page (QEMUFile *f,uint8_t *ptr) +{ +return 1 ; +} + +int kvm_memcrypt_load_incoming_page_enc_bitmap (QEMUFile *f) +{ +return 1 ; +} + +int kvm_memcrypt_save_outgoing_page_enc_bitmap (QEMUFile *f,uint8_t *host, +uint64_t length, +unsigned long *bmap) +{ +return 1 ; +} + +int kvm_memcrypt_sync_page_enc_bitmap (uint8_t *host,uint64_t size, +unsigned long *bitmap) +{ +return 1 ; +} + +#ifndef CONFIG_USER_ONLY int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev) {
include/sysemu/kvm.h +33 -0
@@ -246,6 +246,39 @@ bool kvm_memcrypt_enabled(void);*/ int kvm_memcrypt_encrypt_data (uint8_t *ptr,uint64_t len); +/** +* kvm_memcrypt_save_outgoing_buffer - encrypt the outgoing buffer +* and write to the wire. +*/ +int kvm_memcrypt_save_outgoing_page (QEMUFile *f,uint8_t *ptr,uint32_t size, +uint64_t *bytes_sent); + +/** +* kvm_memcrypt_load_incoming_buffer - read the encrypt incoming buffer and copy +* the buffer into the guest memory space. +*/ +int kvm_memcrypt_load_incoming_page (QEMUFile *f,uint8_t *ptr); + +/** +* kvm_memcrypt_load_incoming_page_enc_bitmap: read the page encryption bitmap +* from the socket and pass it to the hypervisor. +*/ +int kvm_memcrypt_load_incoming_page_enc_bitmap (QEMUFile *f); + +/** +* kvm_memcrypt_sync_page_enc_bitmap: sync the page encryption bitmap +* The caller is responsible to allocate/free the bitmap. +*/ +int kvm_memcrypt_sync_page_enc_bitmap (uint8_t *host,uint64_t size, +unsigned long *bitmap); + +/** +* kvm_memcrypt_save_outgoing_page_enc_bitmap: write the page encryption bitmap +* on socket. +*/ +int kvm_memcrypt_save_outgoing_page_enc_bitmap (QEMUFile *f,uint8_t *host, +uint64_t length, +unsigned long *bmap);#ifdef NEED_CPU_H #include "cpu.h"
include/sysemu/sev.h +9 -0
@@ -18,4 +18,13 @@void *sev_guest_init (const char *id);int sev_encrypt_data (void *handle,uint8_t *ptr,uint64_t len); +int sev_save_outgoing_page (void *handle,QEMUFile *f,uint8_t *ptr, +uint32_t size,uint64_t *bytes_sent); +int sev_load_incoming_page (void *handle,QEMUFile *f,uint8_t *ptr); +int sev_load_incoming_page_enc_bitmap (void *handle,QEMUFile *f); +int sev_save_outgoing_page_enc_bitmap (void *handle,QEMUFile *f, +uint8_t *host,uint64_t length, +unsigned long *bmap); +int sev_sync_page_enc_bitmap (void *handle,uint8_t *host,uint64_t size, +unsigned long *bitmap);#endif
Signature · 1 line
2.17.1
RFC PATCH 3/12 migration/ram: add support to send encrypted pages
In reply to Singh, BrijeshWhen memory encryption is enabled, the guest memory will be encrypted with the guest specific key. The patch introduces RAM_SAVE_FLAG_ENCRYPTED_PAGE flag to distinguish the encrypted data from plaintext. Encrypted pages may need special handling. The kvm_memcrypt_save_outgoing_page() is used by the sender to write the encrypted pages onto the socket, similarly the kvm_memcrypt_load_incoming_page() is used by the target to read the encrypted pages from the socket and load into the guest memory.
--- migration/ram.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-)
migration/ram.c +53 -1
@@ -57,6 +57,7 @@#include "qemu/uuid.h" #include "savevm.h" #include "qemu/iov.h" +#include "sysemu/kvm.h" /***********************************************************/ /* ram save/restore */
@@ -76,6 +77,7 @@#define RAM_SAVE_FLAG_XBZRLE 0x40/* 0x80 is reserved in migration.h start with 0x100 next */ #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 +#define RAM_SAVE_FLAG_ENCRYPTED_PAGE 0x200static inline bool is_zero_range (uint8_t *p,uint64_t size) {
@@ -460,6 +462,9 @@ static QemuCond decomp_done_cond;static bool do_compress_ram_page (QEMUFile *f,z_stream *stream,RAMBlock *block,ram_addr_t offset,uint8_t *source_buf); +static int ram_save_encrypted_page (RAMState *rs,PageSearchStatus *pss, +bool last_stage); +static void *do_data_compress (void *opaque) {CompressParam *param = opaque;
@@ -2006,6 +2011,36 @@ static int ram_save_multifd_page(RAMState *rs, RAMBlock *block,return 1 ; } +/** +* ram_save_encrypted_page - send the given encrypted page to the stream +*/ +static int ram_save_encrypted_page (RAMState *rs,PageSearchStatus *pss, +bool last_stage) +{ +int ret; +uint8_t *p; +RAMBlock *block = pss->block; +ram_addr_t offset = pss->page <<TARGET_PAGE_BITS ; +uint64_t bytes_xmit; + + p = block->host + offset; + + ram_counters.transferred += +save_page_header (rs, rs->f, block, + offset |RAM_SAVE_FLAG_ENCRYPTED_PAGE ); + + ret =kvm_memcrypt_save_outgoing_page (rs->f, p, +TARGET_PAGE_SIZE , &bytes_xmit); +if (ret) { +return -1 ; + } + + ram_counters.transferred += bytes_xmit; + ram_counters.normal++; + +return 1 ; +} +static bool do_compress_ram_page (QEMUFile *f,z_stream *stream,RAMBlock *block,ram_addr_t offset,uint8_t *source_buf) {
@@ -2450,6 +2485,16 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,return res; } +/* +* If memory encryption is enabled then use memory encryption APIs +* to write the outgoing buffer to the wire. The encryption APIs +* will take care of accessing the guest memory and re-encrypt it +* for the transport purposes. +*/ +if (kvm_memcrypt_enabled ()) { +return ram_save_encrypted_page (rs, pss, last_stage); + } +if (save_compress_page (rs, block, offset)) {return 1 ; }
@@ -4271,7 +4316,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) }if (flags & (RAM_SAVE_FLAG_ZERO |RAM_SAVE_FLAG_PAGE | -RAM_SAVE_FLAG_COMPRESS_PAGE |RAM_SAVE_FLAG_XBZRLE )) { +RAM_SAVE_FLAG_COMPRESS_PAGE |RAM_SAVE_FLAG_XBZRLE | +RAM_SAVE_FLAG_ENCRYPTED_PAGE )) {RAMBlock *block =ram_block_from_stream (f, flags);/*
@@ -4391,6 +4437,12 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)break ; }break ; +case RAM_SAVE_FLAG_ENCRYPTED_PAGE : +if (kvm_memcrypt_load_incoming_page (f, host)) { +error_report ("Failed to encrypted incoming data" ); + ret = -EINVAL ; + } +break ;case RAM_SAVE_FLAG_EOS :/* normal exit */ multifd_recv_sync_main ();
Signature · 1 line
2.17.1
RFC PATCH 4/12 kvm: add support to sync the page encryption state bitmap
In reply to Singh, BrijeshThe SEV VMs have concept of private and shared memory. The private memory is encrypted with guest-specific key, while shared memory may be encrypted with hyperivosr key. The KVM_GET_PAGE_ENC_BITMAP can be used to get a bitmap indicating whether the guest page is private or shared. A private page must be transmitted using the SEV migration commands.
--- accel/kvm/kvm-all.c | 1 + include/exec/ram_addr.h | 2 ++ migration/ram.c | 28 +++++++++++++++++++++++++++- target/i386/sev.c | 27 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +1 -0
@@ -1783,6 +1783,7 @@ static int kvm_init(MachineState *ms) } kvm_state->memcrypt_encrypt_data = sev_encrypt_data; + kvm_state->memcrypt_sync_page_enc_bitmap = sev_sync_page_enc_bitmap; } ret =kvm_arch_init (ms, s);
include/exec/ram_addr.h +2 -0
@@ -51,6 +51,8 @@ struct RAMBlock {unsigned long *unsentmap;/* bitmap of already received pages in postcopy */ unsigned long *receivedmap; +/* bitmap of page encryption state for an encrypted guest */ +unsigned long *encbmap; };static inline bool offset_in_ramblock (RAMBlock *b,ram_addr_t offset)
migration/ram.c +27 -1
@@ -1680,6 +1680,9 @@ static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb, rs->migration_dirty_pages +=cpu_physical_memory_sync_dirty_bitmap (rb,0 , length, &rs->num_dirty_pages_period); +if (kvm_memcrypt_enabled ()) { +kvm_memcrypt_sync_page_enc_bitmap (rb->host, length, rb->encbmap); + } }/**
@@ -2465,6 +2468,22 @@ static bool save_compress_page(RAMState *rs, RAMBlock *block, ram_addr_t offset)return false; } +/** +* encrypted_test_bitmap: check if the page is encrypted +* +* Returns a bool indicating whether the page is encrypted. +*/ +static bool encrypted_test_bitmap (RAMState *rs,RAMBlock *block, +unsigned long page) +{ +/* ROM devices contains the unencrypted data */ +if (memory_region_is_rom (block->mr)) { +return false; + } + +return test_bit (page, block->encbmap); +} +/** * ram_save_target_page: save one target page *
@@ -2491,7 +2510,8 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,* will take care of accessing the guest memory and re-encrypt it * for the transport purposes. */ -if (kvm_memcrypt_enabled ()) { +if (kvm_memcrypt_enabled () && +encrypted_test_bitmap (rs, pss->block, pss->page)) {return ram_save_encrypted_page (rs, pss, last_stage); }
@@ -2724,6 +2744,8 @@ static void ram_save_cleanup(void *opaque) block->bmap =NULL ;g_free (block->unsentmap); block->unsentmap =NULL ; +g_free (block->encbmap); + block->encbmap =NULL ; }xbzrle_cleanup ();
@@ -3251,6 +3273,10 @@ static void ram_list_init_bitmaps(void) block->unsentmap =bitmap_new (pages);bitmap_set (block->unsentmap,0 , pages); } +if (kvm_memcrypt_enabled ()) { + block->encbmap =bitmap_new (pages); +bitmap_set (block->encbmap,0 , pages); + } } } }
target/i386/sev.c +27 -0
@@ -819,6 +819,33 @@ sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len)return 0 ; } +int sev_sync_page_enc_bitmap (void *handle,uint8_t *host,uint64_t size, +unsigned long *bitmap) +{ +int r; +unsigned long base_gpa; +KVMState *s = kvm_state; +struct kvm_page_enc_bitmap e = {}; +unsigned long pages = size >>TARGET_PAGE_BITS ; + + r =kvm_physical_memory_addr_from_host (kvm_state, host, &base_gpa); +if (!r) { +return 1 ; + } + + e.enc_bitmap = bitmap; + e.start = base_gpa >>TARGET_PAGE_BITS ; + e.num_pages = pages; + +if (kvm_vm_ioctl (s,KVM_GET_PAGE_ENC_BITMAP , &e) ==-1 ) { +error_report ("%s: get page_enc bitmap start 0x%llx pages 0x%llx" , + __func__, e.start, e.num_pages); +return 1 ; + } + +return 0 ; +} +static void sev_register_types (void ) {
Signature · 1 line
2.17.1
RFC PATCH 5/12 doc: update AMD SEV API spec web link
In reply to Singh, Brijesh--- docs/amd-memory-encryption.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
docs/amd-memory-encryption.txt +1 -1
@@ -98,7 +98,7 @@ AMD Memory Encryption whitepaper: http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf Secure Encrypted Virtualization Key Management: -[1] http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf +[1] https://developer.amd.com/sev/ (Secure Encrypted Virtualization API) KVM Forum slides: http://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf
Signature · 1 line
2.17.1
RFC PATCH 6/12 doc: update AMD SEV to include Live migration flow
In reply to Singh, Brijesh--- docs/amd-memory-encryption.txt | 44 +++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
docs/amd-memory-encryption.txt +43 -1
@@ -89,7 +89,49 @@ TODO Live Migration ---------------- -TODO +AMD SEV encrypts the memory of VMs and because of this encryption is done +using an address tweak, the hypervisor will not be able to simply copy the +ciphertext between machines to migrate a VM. Instead the AMD SEV Key +Management API provides a set of function which the hypervisor can use +to package a guest page for migration, while maintaining the confidentiality +provided by the AMD SEV. + +SEV guest VMs have the concept of private and shared memory. The private +memory is encrypted with the guest-specific key, while shared memory may +be encrypted with the hypervisor key. The migration APIs provided by the +SEV API spec should be used migrating the private pages. The +KVM_GET_PAGE_ENC_BITMAP ioctl can be used to get the guest page state +bitmap. The bitmap can be used to check if the given guest page is +private or shared. + +Before initiating the migration, we need to know the targets public +Diffie-Hellman key (PDH) and certificate chain. It can retrieved +with 'query-sev-capabilities' QMP or using the sev-tool. The +migrate-set-sev-info object can be used to pass the targets PDH and +certificate chain. + +e.g +(QMP) migrate-sev-set-info pdh=<target_pdh> plat-cert=<target_cert_chain> \ + amd-cert=<amd_cert> +(QMP) migrate tcp:0:4444 + +Note: AMD cert contain be obtained from developer.amd.com/sev. + +During the migration flow, on source hypervisor SEND_START is called first +to create outgoing encryption context. Based on the SEV guest policy, the +certificated passed through the migrate-sev-set-info will be validated +before creating the encryption context. The SEND_UPDATE_DATA is called +to encrypt the guest private pages. After the migration is completed the +SEND_FINISH is called to destroy the encryption context and make the VM +non runnable to protect it against the cloning. + +On target hypevisor, the RECEIVE_START is called first to create an +incoming encryption context. The RECEIVE_UPDATE_DATA is called to copy +the received encrypted page into guest memory. After migration of +pages is completed, RECEIVE_FINISH is called to make the VM runnable. + +For more information about the migration see SEV API Appendix A +Usage flow (Live migration section). References -----------------
Signature · 1 line
2.17.1
RFC PATCH 7/12 target/i386: sev: do not create launch context for an incoming guest
In reply to Singh, BrijeshThe LAUNCH_START is used for creating an encryption context to encrypt newly created guest, for an incoming guest the RECEIVE_START should be used.
--- target/i386/sev.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
target/i386/sev.c +10 -4
@@ -789,10 +789,16 @@ 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; +/* +* The LAUNCH context is used for new guest, if its an incoming guest +* then RECEIVE context will be created after the connection is established. +*/ +if (!runstate_check (RUN_STATE_INMIGRATE )) { + 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);
Signature · 1 line
2.17.1
RFC PATCH 8/12 target.json: add migrate-set-sev-info command
In reply to Singh, BrijeshThe command can be used by the hypervisor to specify the target Platform Diffie-Hellman key (PDH) and certificate chain before starting the SEV guest migration. The values passed through the command will be used while creating the outgoing encryption context.
--- qapi/target.json | 18 ++++++++++++++++++ target/i386/monitor.c | 10 ++++++++++ target/i386/sev-stub.c | 5 +++++ target/i386/sev.c | 11 +++++++++++ target/i386/sev_i386.h | 9 ++++++++- 5 files changed, 52 insertions(+), 1 deletion(-)
qapi/target.json +18 -0
@@ -512,3 +512,21 @@ ## { 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'], 'if': 'defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_I386) || defined(TARGET_S390X) || defined(TARGET_MIPS)' } + +## +# @migrate-set-sev-info: +# +# The command is used to provide the target host information used during the +# SEV guest. +# +# @pdh the target host platform diffie-hellman key encoded in base64 +# +# @plat-cert the target host platform certificate chain encoded in base64 +# +# @amd-cert AMD certificate chain which include ASK and OCA encoded in base64 +# +# Since 4.3 +# +## +{ 'command': 'migrate-set-sev-info', + 'data': { 'pdh': 'str', 'plat-cert': 'str', 'amd-cert' : 'str' }}
target/i386/monitor.c +10 -0
@@ -736,3 +736,13 @@ SevCapability *qmp_query_sev_capabilities(Error **errp)return data; } + +void qmp_migrate_set_sev_info (const char *pdh,const char *plat_cert, +const char *amd_cert,Error **errp) +{ +if (sev_enabled ()) { +sev_set_migrate_info (pdh, plat_cert, amd_cert); + }else { +error_setg (errp,"SEV is not enabled" ); + } +}
target/i386/sev-stub.c +5 -0
@@ -48,3 +48,8 @@ SevCapability *sev_get_capabilities(void) {return NULL ; } + +void sev_set_migrate_info (const char *pdh,const char *plat_cert, +const char *amd_cert) +{ +}
target/i386/sev.c +11 -0
@@ -852,6 +852,17 @@ int sev_sync_page_enc_bitmap(void *handle, uint8_t *host, uint64_t size,return 0 ; } +void sev_set_migrate_info (const char *pdh,const char *plat_cert, +const char *amd_cert) +{ +SEVState *s = sev_state; + + s->remote_pdh =g_base64_decode (pdh, &s->remote_pdh_len); + s->remote_plat_cert =g_base64_decode (plat_cert, + &s->remote_plat_cert_len); + s->amd_cert =g_base64_decode (amd_cert, &s->amd_cert_len); +} +static void sev_register_types (void ) {
target/i386/sev_i386.h +8 -1
@@ -39,7 +39,8 @@ 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 ); - +extern void sev_set_migrate_info (const char *pdh,const char *plat_cert, +const char *amd_cert);typedef struct QSevGuestInfo QSevGuestInfo ;typedef struct QSevGuestInfoClass QSevGuestInfoClass ;
@@ -81,6 +82,12 @@ struct SEVState {int sev_fd;SevState state;gchar *measurement; +guchar *remote_pdh; +size_t remote_pdh_len; +guchar *remote_plat_cert; +size_t remote_plat_cert_len; +guchar *amd_cert; +size_t amd_cert_len; };typedef struct SEVState SEVState ;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [RFC PATCH v1 08/12] target.json: add migrate-set-sev-info command
Quoted 24 lines · Brijesh
The command can be used by the hypervisor to specify the target Platform Diffie-Hellman key (PDH) and certificate chain before starting the SEV guest migration. The values passed through the command will be used while creating the outgoing encryption context.
Signed-off-by Brijesh Singh <[email protected]>--- qapi/target.json | 18 ++++++++++++++++++ target/i386/monitor.c | 10 ++++++++++ target/i386/sev-stub.c | 5 +++++ target/i386/sev.c | 11 +++++++++++ target/i386/sev_i386.h | 9 ++++++++- 5 files changed, 52 insertions(+), 1 deletion(-)+++
b/qapi/target.jsonqapi/target.json +4
-0@@ -512,3 +512,21 @@ ## { 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'], 'if': 'defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_I386) || defined(TARGET_S390X) || defined(TARGET_MIPS)' } + +## +# @migrate-set-sev-info: +#
qapi/target.json line 528
Quoted 10 lines
qapi/target.json +10
-0+# The command is used to provide the target host information used during the +# SEV guest. +# +# @pdh the target host platform diffie-hellman key encoded in base64 +# +# @plat-cert the target host platform certificate chain encoded in base64 +# +# @amd-cert AMD certificate chain which include ASK and OCA encoded in base64 +# +# Since 4.3
The next release is 4.1, then likely 4.2 near the end of the calendar year, then 5.0 in 2020. There is no planned 4.3 release. Are you trying to get this in 4.1?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [RFC PATCH v1 08/12] target.json: add migrate-set-sev-info command
Quoted 31 lines · Eric Blake
Quoted 30 lines · Brijesh
The command can be used by the hypervisor to specify the target Platform Diffie-Hellman key (PDH) and certificate chain before starting the SEV guest migration. The values passed through the command will be used while creating the outgoing encryption context.
Signed-off-by Brijesh Singh <[email protected]>--- qapi/target.json | 18 ++++++++++++++++++ target/i386/monitor.c | 10 ++++++++++ target/i386/sev-stub.c | 5 +++++ target/i386/sev.c | 11 +++++++++++ target/i386/sev_i386.h | 9 ++++++++- 5 files changed, 52 insertions(+), 1 deletion(-)+++
b/qapi/target.jsonqapi/target.json +10
-0@@ -512,3 +512,21 @@ ## { 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'], 'if': 'defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_I386) || defined(TARGET_S390X) || defined(TARGET_MIPS)' } + +## +# @migrate-set-sev-info: +# +# The command is used to provide the target host information used during the +# SEV guest. +# +# @pdh the target host platform diffie-hellman key encoded in base64 +# +# @plat-cert the target host platform certificate chain encoded in base64
Quoted 8 lines
qapi/target.json line 528
Quoted 4 lines
qapi/target.json +4
-0+# +# @amd-cert AMD certificate chain which include ASK and OCA encoded in base64 +# +# Since 4.3The next release is 4.1, then likely 4.2 near the end of the calendar year, then 5.0 in 2020. There is no planned 4.3 release. Are you trying to get this in 4.1?
Ah, I was meaning to type 4.2 and not 4.3. The series has dependency on kernel patches, my best effort it to get it ready for 4.2 merge window.
RFC PATCH 9/12 target/i386: sev: add support to encrypt the outgoing page
In reply to Singh, BrijeshThe sev_save_outgoing_page() provide the implementation to encrypt the guest private pages during the transit. The routines uses the SEND_START command to create the outgoing encryption context on the first call then uses the SEND_UPDATE_DATA command to encrypt the data before writing it to the socket. While encrypting the data SEND_UPDATE_DATA produces some metadata (e.g MAC, IV). The metadata is also sent to the target machine. After migration is completed, we issue the SEND_FINISH command to transition the SEV guest state from sending to unrunnable state.
--- accel/kvm/kvm-all.c | 1 + target/i386/sev.c | 229 +++++++++++++++++++++++++++++++++++++++ target/i386/sev_i386.h | 2 + target/i386/trace-events | 3 + 4 files changed, 235 insertions(+)
accel/kvm/kvm-all.c +1 -0
@@ -1784,6 +1784,7 @@ static int kvm_init(MachineState *ms) kvm_state->memcrypt_encrypt_data = sev_encrypt_data; kvm_state->memcrypt_sync_page_enc_bitmap = sev_sync_page_enc_bitmap; + kvm_state->memcrypt_save_outgoing_page = sev_save_outgoing_page; } ret =kvm_arch_init (ms, s);
target/i386/sev.c +229 -0
253 lines not on this page · raw message
target/i386/sev_i386.h +2 -0
@@ -88,6 +88,8 @@ struct SEVState {size_t remote_plat_cert_len;guchar *amd_cert;size_t amd_cert_len; +gchar *send_packet_hdr; +size_t send_packet_hdr_len; };typedef struct SEVState SEVState ;
target/i386/trace-events +3 -0
@@ -15,3 +15,6 @@ 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_send_start(uint64_t pdh, int l1, uint64_t plat, int l2, uint64_t amd, int l3) "pdh 0x%" PRIx64 " len %d plat 0x%" PRIx64 " len %d amd 0x%" PRIx64 " len %d" +kvm_sev_send_update_data(void *src, void *dst, int len) "guest %p trans %p len %d" +kvm_sev_send_finish(void) ""
Signature · 1 line
2.17.1
RFC PATCH 10/12 target/i386: sev: add support to load incoming encrypted page
In reply to Singh, BrijeshThe sev_load_incoming_page() provide the implementation to read the incoming guest private pages from the socket and load it into the guest memory. The routines uses the RECEIVE_START command to create the incoming encryption context on the first call then uses the RECEIEVE_UPDATE_DATA command to load the encrypted pages into the guest memory. After migration is completed, we issue the RECEIVE_FINISH command to transition the SEV guest to the runnable state so that it can be executed.
--- accel/kvm/kvm-all.c | 1 + target/i386/sev.c | 126 ++++++++++++++++++++++++++++++++++++++- target/i386/trace-events | 3 + 3 files changed, 129 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +1 -0
@@ -1785,6 +1785,7 @@ static int kvm_init(MachineState *ms) kvm_state->memcrypt_encrypt_data = sev_encrypt_data; kvm_state->memcrypt_sync_page_enc_bitmap = sev_sync_page_enc_bitmap; kvm_state->memcrypt_save_outgoing_page = sev_save_outgoing_page; + kvm_state->memcrypt_load_incoming_page = sev_load_incoming_page; } ret =kvm_arch_init (ms, s);
target/i386/sev.c +125 -1
144 lines not on this page · raw message
target/i386/trace-events +3 -0
@@ -18,3 +18,6 @@ kvm_sev_launch_finish(void) "" kvm_sev_send_start(uint64_t pdh, int l1, uint64_t plat, int l2, uint64_t amd, int l3) "pdh 0x%" PRIx64 " len %d plat 0x%" PRIx64 " len %d amd 0x%" PRIx64 " len %d" kvm_sev_send_update_data(void *src, void *dst, int len) "guest %p trans %p len %d" kvm_sev_send_finish(void) "" +kvm_sev_receive_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p" +kvm_sev_receive_update_data(void *src, void *dst, int len, void *hdr, int hdr_len) "guest %p trans %p len %d hdr %p hdr_len %d" +kvm_sev_receive_finish(void) ""
Signature · 1 line
2.17.1
RFC PATCH 11/12 migration: add support to migrate page encryption bitmap
In reply to Singh, BrijeshWhen memory encryption is enabled, the hypervisor maintains a page encryption bitmap which is referred by hypervisor during migratoin to check if page is private or shared. The bitmap is built during the VM bootup and must be migrated to the target host so that hypervisor on target host can use it for future migration.
--- accel/kvm/kvm-all.c | 4 +++ migration/ram.c | 43 +++++++++++++++++++++++++++++- target/i386/sev.c | 56 ++++++++++++++++++++++++++++++++++++++++ target/i386/trace-events | 3 +++ 4 files changed, 105 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +4 -0
@@ -1786,6 +1786,10 @@ static int kvm_init(MachineState *ms) kvm_state->memcrypt_sync_page_enc_bitmap = sev_sync_page_enc_bitmap; kvm_state->memcrypt_save_outgoing_page = sev_save_outgoing_page; kvm_state->memcrypt_load_incoming_page = sev_load_incoming_page; + kvm_state->memcrypt_load_incoming_page_enc_bitmap = + sev_load_incoming_page_enc_bitmap; + kvm_state->memcrypt_save_outgoing_page_enc_bitmap = + sev_save_outgoing_page_enc_bitmap; } ret =kvm_arch_init (ms, s);
migration/ram.c +42 -1
73 lines not on this page · raw message
target/i386/sev.c +56 -0
68 lines not on this page · raw message
target/i386/trace-events +3 -0
@@ -21,3 +21,6 @@ kvm_sev_send_finish(void) "" kvm_sev_receive_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p" kvm_sev_receive_update_data(void *src, void *dst, int len, void *hdr, int hdr_len) "guest %p trans %p len %d hdr %p hdr_len %d" kvm_sev_receive_finish(void) "" +kvm_sev_sync_page_enc_bitmap(uint64_t start, uint64_t len) "start 0x%" PRIx64 " len 0x%" PRIx64 +kvm_sev_save_page_enc_bitmap(uint64_t start, uint64_t len) "start 0x%" PRIx64 " len 0x%" PRIx64 +kvm_sev_load_page_enc_bitmap(uint64_t start, uint64_t len) "start 0x%" PRIx64 " len 0x%" PRIx64
Signature · 1 line
2.17.1
RFC PATCH 12/12 target/i386: sev: remove migration blocker
In reply to Singh, Brijesh--- target/i386/sev.c | 12 ------------ 1 file changed, 12 deletions(-)
target/i386/sev.c +0 -12
@@ -34,7 +34,6 @@#define DEFAULT_SEV_DEVICE "/dev/sev"static SEVState *sev_state; -static Error *sev_mig_blocker;static const char *const sev_fw_errlist[] = {"" ,
@@ -685,7 +684,6 @@ 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);
@@ -696,16 +694,6 @@ 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 int
Signature · 1 line
2.17.1