[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20211210164620.11636-10-michael.roth@amd.com>
Date: Fri, 10 Dec 2021 10:46:19 -0600
From: Michael Roth <michael.roth@....com>
To: <linux-kselftest@...r.kernel.org>
CC: <kvm@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
<x86@...nel.org>, Nathan Tempelman <natet@...gle.com>,
Marc Orr <marcorr@...gle.com>,
"Steve Rutherford" <srutherford@...gle.com>,
Sean Christopherson <seanjc@...gle.com>,
Mingwei Zhang <mizhang@...gle.com>,
Brijesh Singh <brijesh.singh@....com>,
Tom Lendacky <thomas.lendacky@....com>,
Varad Gautam <varad.gautam@...e.com>,
Shuah Khan <shuah@...nel.org>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
"David Woodhouse" <dwmw@...zon.co.uk>,
Ricardo Koller <ricarkol@...gle.com>,
"Jim Mattson" <jmattson@...gle.com>,
Joerg Roedel <joro@...tes.org>,
"Thomas Gleixner" <tglx@...utronix.de>,
Ingo Molnar <mingo@...hat.com>,
"Borislav Petkov" <bp@...en8.de>,
"H . Peter Anvin" <hpa@...or.com>,
"Christian Borntraeger" <borntraeger@...ux.ibm.com>,
Janosch Frank <frankja@...ux.ibm.com>,
David Hildenbrand <david@...hat.com>,
"Claudio Imbrenda" <imbrenda@...ux.ibm.com>,
Marc Zyngier <maz@...nel.org>,
James Morse <james.morse@....com>,
Alexandru Elisei <alexandru.elisei@....com>,
"Suzuki K Poulose" <suzuki.poulose@....com>,
<kvmarm@...ts.cs.columbia.edu>
Subject: [PATCH RFC 09/10] kvm: selftests: add GUEST_SHARED_* macros for shared ucall implementations
Introduce GUEST_SHARED_* macros, which are mostly analogous to existing
GUEST_SYNC/GUEST_ASSERT/etc macros used to simplify guest code that
uses ucall for host/guest synchronization.
There are also some new CHECK_GUEST_SHARED_* macros intended to provide
similar helpers in the host code that can pair directly with the guest
versions.
Signed-off-by: Michael Roth <michael.roth@....com>
---
.../selftests/kvm/include/ucall_common.h | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h
index ae0e8eec9734..b9b220dbd6c3 100644
--- a/tools/testing/selftests/kvm/include/ucall_common.h
+++ b/tools/testing/selftests/kvm/include/ucall_common.h
@@ -48,6 +48,7 @@ vm_vaddr_t ucall_shared_alloc(struct kvm_vm *vm, int count);
void ucall_shared(struct ucall *uc, uint64_t cmd, int nargs, ...);
uint64_t get_ucall_shared(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
+/* Helpers for host/guest synchronization using ucall_shared */
#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \
ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
@@ -76,4 +77,71 @@ uint64_t get_ucall_shared(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
#define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b)
+/* Helper macros for ucall synchronization via shared memory/ucall struct. */
+#define GUEST_SHARED_SYNC_ARGS(uc, stage, arg1, arg2, arg3, arg4) \
+ ucall_shared(uc, UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
+#define GUEST_SHARED_SYNC(uc, stage) \
+ ucall_shared(uc, UCALL_SYNC, 2, "hello", stage)
+#define GUEST_SHARED_DONE(uc) \
+ ucall_shared(uc, UCALL_DONE, 0)
+#define __GUEST_SHARED_ASSERT(uc, _condition, _condstr, _nargs, _args...) do { \
+ if (!(_condition)) \
+ ucall_shared(uc, UCALL_ABORT, 2 + _nargs, \
+ "Failed guest assert: " \
+ _condstr, __LINE__, _args); \
+} while (0)
+
+#define GUEST_SHARED_ASSERT(uc, _condition) \
+ __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 0, 0)
+
+#define GUEST_SHARED_ASSERT_1(uc, _condition, arg1) \
+ __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 1, (arg1))
+
+#define GUEST_SHARED_ASSERT_2(uc, _condition, arg1, arg2) \
+ __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 2, (arg1), (arg2))
+
+#define GUEST_SHARED_ASSERT_3(uc, _condition, arg1, arg2, arg3) \
+ __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 3, (arg1), (arg2), (arg3))
+
+#define GUEST_SHARED_ASSERT_4(uc, _condition, arg1, arg2, arg3, arg4) \
+ __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4))
+
+#define GUEST_SHARED_ASSERT_EQ(uc, a, b) \
+ __GUEST_SHARED_ASSERT(uc, (a) == (b), #a " == " #b, 2, a, b)
+
+#define __CHECK_SHARED_STATE(uc, uc_cmd, uc_cmd_expected) do { \
+ if (uc_cmd != uc_cmd_expected) { \
+ if (uc_cmd == UCALL_ABORT) \
+ TEST_FAIL("Unexpected guest abort: \"%s\" at %s:%ld", \
+ (const char *)uc->args[0], __FILE__, \
+ uc->args[1]); \
+ else \
+ TEST_FAIL("Unexpected ucall command/state: %" PRIu64, \
+ uc_cmd); \
+ } \
+} while (0)
+
+#define CHECK_SHARED_SYNC(vm, vcpu_id, uc, stage) do { \
+ uint64_t uc_cmd = get_ucall_shared(vm, vcpu_id, uc); \
+ TEST_ASSERT(uc_cmd == UCALL_SYNC, \
+ "Unexpected ucall command/state: %" PRIu64, uc_cmd); \
+ TEST_ASSERT(!strcmp((char *)uc->args[0], "hello"), \
+ "Invalid ucall signature argument."); \
+ TEST_ASSERT(uc->args[1] == stage, \
+ "Invalid ucall sync stage: %" PRIu64, uc->args[1]); \
+} while (0)
+
+#define CHECK_SHARED_DONE(vm, vcpu_id, uc) do { \
+ uint64_t uc_cmd = get_ucall_shared(vm, vcpu_id, uc); \
+ __CHECK_SHARED_STATE(uc, uc_cmd, UCALL_DONE); \
+ TEST_ASSERT(uc_cmd == UCALL_DONE, \
+ "Unexpected ucall command/state: %" PRIu64, uc_cmd); \
+} while (0)
+
+#define CHECK_SHARED_ABORT(vm, vcpu_id, uc) do { \
+ uint64_t uc_cmd = get_ucall_shared(vm, vcpu_id, uc); \
+ TEST_ASSERT(uc_cmd == UCALL_ABORT, \
+ "Unexpected ucall command/state: %" PRIu64, uc_cmd); \
+} while (0)
+
#endif /* SELFTEST_KVM_UCALL_COMMON_H */
--
2.25.1
Powered by blists - more mailing lists