1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * tools/testing/selftests/kvm/include/kvm_util.h
4  *
5  * Copyright (C) 2018, Google LLC.
6  */
7 #ifndef SELFTEST_KVM_UCALL_COMMON_H
8 #define SELFTEST_KVM_UCALL_COMMON_H
9 
10 /* Common ucalls */
11 enum {
12 	UCALL_NONE,
13 	UCALL_SYNC,
14 	UCALL_ABORT,
15 	UCALL_DONE,
16 	UCALL_UNHANDLED,
17 };
18 
19 #define UCALL_MAX_ARGS 6
20 
21 struct ucall {
22 	uint64_t cmd;
23 	uint64_t args[UCALL_MAX_ARGS];
24 };
25 
26 void ucall_init(struct kvm_vm *vm, void *arg);
27 void ucall_uninit(struct kvm_vm *vm);
28 void ucall(uint64_t cmd, int nargs, ...);
29 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
30 
31 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
32 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
33 #define GUEST_SYNC(stage)	ucall(UCALL_SYNC, 2, "hello", stage)
34 #define GUEST_DONE()		ucall(UCALL_DONE, 0)
35 #define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) do {    \
36 	if (!(_condition))                                              \
37 		ucall(UCALL_ABORT, 2 + _nargs,                          \
38 			"Failed guest assert: "                         \
39 			_condstr, __LINE__, _args);                     \
40 } while (0)
41 
42 #define GUEST_ASSERT(_condition) \
43 	__GUEST_ASSERT(_condition, #_condition, 0, 0)
44 
45 #define GUEST_ASSERT_1(_condition, arg1) \
46 	__GUEST_ASSERT(_condition, #_condition, 1, (arg1))
47 
48 #define GUEST_ASSERT_2(_condition, arg1, arg2) \
49 	__GUEST_ASSERT(_condition, #_condition, 2, (arg1), (arg2))
50 
51 #define GUEST_ASSERT_3(_condition, arg1, arg2, arg3) \
52 	__GUEST_ASSERT(_condition, #_condition, 3, (arg1), (arg2), (arg3))
53 
54 #define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \
55 	__GUEST_ASSERT(_condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4))
56 
57 #define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b)
58 
59 #endif /* SELFTEST_KVM_UCALL_COMMON_H */
60