[RFC 10/16] KVM: x86: Enabled protected memory extension

Kirill A. Shutemov
2020-05-22T12:52:52Z

Wire up hypercalls for the feature and define VM_KVM_PROTECTED.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
arch/x86/Kconfig | 1 +
arch/x86/kvm/cpuid.c | 3 +++
arch/x86/kvm/x86.c | 9 +++++++++
include/linux/mm.h | 4 ++++
4 files changed, 17 insertions(+)

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 58dd44a1b92f..420e3947f0c6 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -801,6 +801,7 @@ config KVM_GUEST
select ARCH_CPUIDLE_HALTPOLL
select X86_MEM_ENCRYPT_COMMON
select SWIOTLB
+ select ARCH_USES_HIGH_VMA_FLAGS
default y
---help---
This option enables various optimizations for running under the KVM
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 901cd1fdecd9..94cc5e45467e 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -714,6 +714,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
(1 << KVM_FEATURE_POLL_CONTROL) |
(1 << KVM_FEATURE_PV_SCHED_YIELD);

+ if (VM_KVM_PROTECTED)
+ entry->eax |=(1 << KVM_FEATURE_MEM_PROTECTED);
+
if (sched_info_on())
entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c17e6eb9ad43..acba0ac07f61 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7598,6 +7598,15 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
kvm_sched_yield(vcpu->kvm, a0);
ret = 0;
break;
+ case KVM_HC_ENABLE_MEM_PROTECTED:
+ ret = kvm_protect_all_memory(vcpu->kvm);
+ break;
+ case KVM_HC_MEM_SHARE:
+ ret = kvm_protect_memory(vcpu->kvm, a0, a1, false);
+ break;
+ case KVM_HC_MEM_UNSHARE:
+ ret = kvm_protect_memory(vcpu->kvm, a0, a1, true);
+ break;
default:
ret = -KVM_ENOSYS;
break;
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 4f7195365cc0..6eb771c14968 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -329,7 +329,11 @@ extern unsigned int kobjsize(const void *objp);
# define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
#endif

+#if defined(CONFIG_X86_64) && defined(CONFIG_KVM)
+#define VM_KVM_PROTECTED VM_HIGH_ARCH_4
+#else
#define VM_KVM_PROTECTED 0
+#endif

#ifndef VM_GROWSUP
# define VM_GROWSUP VM_NONE
--
2.26.2