a41c76
From 962f3c23ce6f477cfb24f5fa66a9f296ce953759 Mon Sep 17 00:00:00 2001
a41c76
Message-Id: <962f3c23ce6f477cfb24f5fa66a9f296ce953759@dist-git>
a41c76
From: Boris Fiuczynski <fiuczy@linux.ibm.com>
a41c76
Date: Wed, 24 Jun 2020 13:16:21 +0200
a41c76
Subject: [PATCH] tools: Secure guest check for AMD in virt-host-validate
a41c76
MIME-Version: 1.0
a41c76
Content-Type: text/plain; charset=UTF-8
a41c76
Content-Transfer-Encoding: 8bit
a41c76
a41c76
Add checking in virt-host-validate for secure guest support
a41c76
on x86 for AMD Secure Encrypted Virtualization.
a41c76
a41c76
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
a41c76
Reviewed-by: Paulo de Rezende Pinatti <ppinatti@linux.ibm.com>
a41c76
Reviewed-by: Bjoern Walk <bwalk@linux.ibm.com>
a41c76
Reviewed-by: Erik Skultety <eskultet@redhat.com>
a41c76
(cherry picked from commit 4b561d49ad5d6cc45766714253adb798bb99b6e8)
a41c76
a41c76
https://bugzilla.redhat.com/show_bug.cgi?id=1848997
a41c76
https://bugzilla.redhat.com/show_bug.cgi?id=1850351
a41c76
a41c76
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
a41c76
Message-Id: <638121e5c2692557b1b2d716371d324d61273282.1592996194.git.jdenemar@redhat.com>
a41c76
Reviewed-by: Ján Tomko <jtomko@redhat.com>
a41c76
---
a41c76
 tools/virt-host-validate-common.c | 30 +++++++++++++++++++++++++++++-
a41c76
 tools/virt-host-validate-common.h |  1 +
a41c76
 2 files changed, 30 insertions(+), 1 deletion(-)
a41c76
a41c76
diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-common.c
a41c76
index e59469a49b..82e6092c01 100644
a41c76
--- a/tools/virt-host-validate-common.c
a41c76
+++ b/tools/virt-host-validate-common.c
a41c76
@@ -40,7 +40,8 @@ VIR_ENUM_IMPL(virHostValidateCPUFlag,
a41c76
               "vmx",
a41c76
               "svm",
a41c76
               "sie",
a41c76
-              "158");
a41c76
+              "158",
a41c76
+              "sev");
a41c76
 
a41c76
 static bool quiet;
a41c76
 
a41c76
@@ -420,14 +421,18 @@ int virHostValidateSecureGuests(const char *hvname,
a41c76
 {
a41c76
     virBitmapPtr flags;
a41c76
     bool hasFac158 = false;
a41c76
+    bool hasAMDSev = false;
a41c76
     virArch arch = virArchFromHost();
a41c76
     g_autofree char *cmdline = NULL;
a41c76
     static const char *kIBMValues[] = {"y", "Y", "on", "ON", "oN", "On", "1"};
a41c76
+    g_autofree char *mod_value = NULL;
a41c76
 
a41c76
     flags = virHostValidateGetCPUFlags();
a41c76
 
a41c76
     if (flags && virBitmapIsBitSet(flags, VIR_HOST_VALIDATE_CPU_FLAG_FACILITY_158))
a41c76
         hasFac158 = true;
a41c76
+    else if (flags && virBitmapIsBitSet(flags, VIR_HOST_VALIDATE_CPU_FLAG_SEV))
a41c76
+        hasAMDSev = true;
a41c76
 
a41c76
     virBitmapFree(flags);
a41c76
 
a41c76
@@ -463,6 +468,29 @@ int virHostValidateSecureGuests(const char *hvname,
a41c76
             virHostMsgFail(level, "Hardware or firmware does not provide "
a41c76
                                   "support for IBM Secure Execution");
a41c76
         }
a41c76
+    } else if (hasAMDSev) {
a41c76
+        if (virFileReadValueString(&mod_value, "/sys/module/kvm_amd/parameters/sev") < 0) {
a41c76
+            virHostMsgFail(level, "AMD Secure Encrypted Virtualization not "
a41c76
+                                  "supported by the currently used kernel");
a41c76
+            return 0;
a41c76
+        }
a41c76
+
a41c76
+        if (mod_value[0] != '1') {
a41c76
+            virHostMsgFail(level,
a41c76
+                           "AMD Secure Encrypted Virtualization appears to be "
a41c76
+                           "disabled in kernel. Add kvm_amd.sev=1 "
a41c76
+                           "to the kernel cmdline arguments");
a41c76
+            return 0;
a41c76
+        }
a41c76
+
a41c76
+        if (virFileExists("/dev/sev")) {
a41c76
+            virHostMsgPass();
a41c76
+            return 1;
a41c76
+        } else {
a41c76
+            virHostMsgFail(level,
a41c76
+                           "AMD Secure Encrypted Virtualization appears to be "
a41c76
+                           "disabled in firemare.");
a41c76
+        }
a41c76
     } else {
a41c76
         virHostMsgFail(level,
a41c76
                        "Unknown if this platform has Secure Guest support");
a41c76
diff --git a/tools/virt-host-validate-common.h b/tools/virt-host-validate-common.h
a41c76
index 1653c9c95b..014331c0e0 100644
a41c76
--- a/tools/virt-host-validate-common.h
a41c76
+++ b/tools/virt-host-validate-common.h
a41c76
@@ -39,6 +39,7 @@ typedef enum {
a41c76
     VIR_HOST_VALIDATE_CPU_FLAG_SVM,
a41c76
     VIR_HOST_VALIDATE_CPU_FLAG_SIE,
a41c76
     VIR_HOST_VALIDATE_CPU_FLAG_FACILITY_158,
a41c76
+    VIR_HOST_VALIDATE_CPU_FLAG_SEV,
a41c76
 
a41c76
     VIR_HOST_VALIDATE_CPU_FLAG_LAST,
a41c76
 } virHostValidateCPUFlag;
a41c76
-- 
a41c76
2.27.0
a41c76