[PATCH RFC 08/10] kvm: selftests: introduce ucall implementation based on halt instructions

From: Michael Roth
Date: Fri Dec 10 2021 - 11:50:45 EST


With the shared ucall interfaces the only thing the ucall
implementation really needs to do is generate an exit to host userspace
so that the shared ucall struct can be examined. This implementation
uses a 'halt' instruction to generate such an exit, and is suitable for
use with SEV guests, and potentially other confidential guest
implementations.

Signed-off-by: Michael Roth <michael.roth@xxxxxxx>
---
.../selftests/kvm/include/x86_64/ucall.h | 1 +
.../testing/selftests/kvm/lib/x86_64/ucall.c | 23 +++++++++++++++++++
2 files changed, 24 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/x86_64/ucall.h b/tools/testing/selftests/kvm/include/x86_64/ucall.h
index 8366bdc9c04e..457fc1406746 100644
--- a/tools/testing/selftests/kvm/include/x86_64/ucall.h
+++ b/tools/testing/selftests/kvm/include/x86_64/ucall.h
@@ -12,6 +12,7 @@
#include "ucall_common.h"

extern const struct ucall_ops ucall_ops_pio;
+extern const struct ucall_ops ucall_ops_halt;

extern const struct ucall_ops ucall_ops_default;

diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing/selftests/kvm/lib/x86_64/ucall.c
index 157d2a102547..4dfb12881434 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c
@@ -57,4 +57,27 @@ const struct ucall_ops ucall_ops_pio = {
.recv_cmd_shared = ucall_ops_pio_recv_cmd_shared,
};

+static void ucall_ops_halt_send_cmd_shared(struct ucall *uc)
+{
+ asm volatile("hlt" : : : "memory");
+}
+
+static uint64_t ucall_ops_halt_recv_cmd_shared(struct kvm_vm *vm, uint32_t vcpu_id,
+ struct ucall *uc)
+{
+ struct kvm_run *run = vcpu_state(vm, vcpu_id);
+
+ TEST_ASSERT(run->exit_reason == KVM_EXIT_HLT,
+ "unexpected exit reason: %u (%s)",
+ run->exit_reason, exit_reason_str(run->exit_reason));
+
+ return uc->cmd;
+}
+
+const struct ucall_ops ucall_ops_halt = {
+ .name = "halt",
+ .send_cmd_shared = ucall_ops_halt_send_cmd_shared,
+ .recv_cmd_shared = ucall_ops_halt_recv_cmd_shared,
+};
+
const struct ucall_ops ucall_ops_default = ucall_ops_pio;
--
2.25.1