diff mbox

[RFC,v1,12/22] sev: add SEV guest status command

Message ID 147377813040.11859.5268138025433994743.stgit@brijesh-build-machine (mailing list archive)
State New, archived
Headers show

Commit Message

Brijesh Singh Sept. 13, 2016, 2:48 p.m. UTC
The command is used for querying SEV status for this guest.

For more information see [1], section 6.10

[1] http://support.amd.com/TechDocs/55766_SEV-KM%20API_Spec.pdf

The following KVM RFC patches defines and implements this command

http://marc.info/?l=kvm&m=147190852423972&w=2
http://marc.info/?l=kvm&m=147190852423972&w=2

Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
---
 include/sysemu/sev.h |   18 ++++++++++++++++++
 sev.c                |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
diff mbox

Patch

diff --git a/include/sysemu/sev.h b/include/sysemu/sev.h
index a505d75..e8fa62f 100644
--- a/include/sysemu/sev.h
+++ b/include/sysemu/sev.h
@@ -16,6 +16,15 @@ 
 
 #include "sysemu/kvm.h"
 
+typedef enum SevState {
+    SEV_STATE_INVALID = 0x0,
+    SEV_STATE_LAUNCHING,
+    SEV_STATE_RECEIVING,
+    SEV_STATE_SENDING,
+    SEV_STATE_RUNNING,
+    SEV_STATE_MAX,
+} SevState;
+
 /**
  * sev_init - initialize Secure Encrypted Virtualization on this guest
  * @kvm_state - KVM handle
@@ -75,4 +84,13 @@  int kvm_sev_dbg_decrypt(uint8_t *dest, const uint8_t *src, uint32_t len);
  */
 int kvm_sev_dbg_encrypt(uint8_t *dest, const uint8_t *src, uint32_t len);
 
+/**
+ * kvm_sev_get_status - get the guest status in SEV mode.
+ * @state - guest SEV state
+ * @str - verbatim string literal (buffer must be allocated by caller)
+ *
+ * Returns: 0 on success and @state will be contain current guest state.
+ */
+int kvm_sev_get_status(SevState *state, char *str);
+
 #endif
diff --git a/sev.c b/sev.c
index 4e5da84..508eff2 100644
--- a/sev.c
+++ b/sev.c
@@ -59,6 +59,15 @@  struct SEVInfo {
     struct kvm_sev_launch_finish *launch_finish;
 };
 
+const char *sev_state_msg[] = {
+    "error",
+    "launching",
+    "receiving",
+    "sending",
+    "running",
+    "unknown",
+};
+
 typedef struct SEVInfo SEVInfo;
 static SEVInfo *sev_info;
 static const char *cfg_file;
@@ -478,3 +487,28 @@  int kvm_sev_dbg_encrypt(uint8_t *dst, const uint8_t *src, uint32_t len)
     DPRINTF("SEV: DBG_ENCRYPT dst %p src %p sz %d\n", dst, src, len);
     return 0;
 }
+
+int kvm_sev_get_status(SevState *state, char *msg)
+{
+    int ret;
+    struct kvm_sev_guest_status status;
+    struct kvm_sev_issue_cmd input;
+
+    input.cmd = KVM_SEV_GUEST_STATUS;
+    input.opaque = (unsigned long)&status;
+    ret = kvm_vm_ioctl(kvm_state, KVM_SEV_ISSUE_CMD, &input);
+    if (ret) {
+        fprintf(stderr, "SEV: guest_status failed ret=%d(%#010x)\n",
+                ret, input.ret_code);
+        return 1;
+    }
+
+    *state = status.state;
+    if (msg) {
+        strcpy(msg, sev_state_msg[*state]);
+    }
+
+    DPRINTF("SEV: GUEST_STATUS state %#x (%s)\n", *state,
+            sev_state_msg[*state]);
+    return 0;
+}