Skip to content

Commit

Permalink
kvm: introduce memory encryption APIs
Browse files Browse the repository at this point in the history
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.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
codomania authored and bonzini committed Mar 13, 2018
1 parent b20e378 commit 54e8953
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions accel/kvm/kvm-all.c
Expand Up @@ -107,6 +107,7 @@ struct KVMState

/* memory encryption */
void *memcrypt_handle;
int (*memcrypt_encrypt_data)(void *handle, uint8_t *ptr, uint64_t len);
};

KVMState *kvm_state;
Expand Down Expand Up @@ -151,6 +152,17 @@ bool kvm_memcrypt_enabled(void)
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;
}

static KVMSlot *kvm_get_free_slot(KVMMemoryListener *kml)
{
KVMState *s = kvm_state;
Expand Down Expand Up @@ -1659,6 +1671,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);
Expand Down
5 changes: 5 additions & 0 deletions accel/kvm/sev-stub.c
Expand Up @@ -15,6 +15,11 @@
#include "qemu-common.h"
#include "sysemu/sev.h"

int sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len)
{
abort();
}

void *sev_guest_init(const char *id)
{
return NULL;
Expand Down
5 changes: 5 additions & 0 deletions accel/stubs/kvm-stub.c
Expand Up @@ -110,6 +110,11 @@ bool kvm_memcrypt_enabled(void)
return false;
}

int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len)
{
return 1;
}

#ifndef CONFIG_USER_ONLY
int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev)
{
Expand Down
8 changes: 8 additions & 0 deletions include/sysemu/kvm.h
Expand Up @@ -239,6 +239,14 @@ bool kvm_arm_supports_user_irq(void);
*/
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);


#ifdef NEED_CPU_H
#include "cpu.h"
Expand Down
1 change: 1 addition & 0 deletions include/sysemu/sev.h
Expand Up @@ -17,4 +17,5 @@
#include "sysemu/kvm.h"

void *sev_guest_init(const char *id);
int sev_encrypt_data(void *handle, uint8_t *ptr, uint64_t len);
#endif

0 comments on commit 54e8953

Please sign in to comment.