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 #include "test_util.h" 10 11 /* Common ucalls */ 12 enum { 13 UCALL_NONE, 14 UCALL_SYNC, 15 UCALL_ABORT, 16 UCALL_DONE, 17 UCALL_UNHANDLED, 18 }; 19 20 #define UCALL_MAX_ARGS 7 21 22 struct ucall { 23 uint64_t cmd; 24 uint64_t args[UCALL_MAX_ARGS]; 25 }; 26 27 void ucall_init(struct kvm_vm *vm, void *arg); 28 void ucall_uninit(struct kvm_vm *vm); 29 void ucall(uint64_t cmd, int nargs, ...); 30 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc); 31 32 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ 33 ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) 34 #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) 35 #define GUEST_DONE() ucall(UCALL_DONE, 0) 36 37 enum guest_assert_builtin_args { 38 GUEST_ERROR_STRING, 39 GUEST_FILE, 40 GUEST_LINE, 41 GUEST_ASSERT_BUILTIN_NARGS 42 }; 43 44 #define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) \ 45 do { \ 46 if (!(_condition)) \ 47 ucall(UCALL_ABORT, GUEST_ASSERT_BUILTIN_NARGS + _nargs, \ 48 "Failed guest assert: " _condstr, \ 49 __FILE__, __LINE__, ##_args); \ 50 } while (0) 51 52 #define GUEST_ASSERT(_condition) \ 53 __GUEST_ASSERT(_condition, #_condition, 0, 0) 54 55 #define GUEST_ASSERT_1(_condition, arg1) \ 56 __GUEST_ASSERT(_condition, #_condition, 1, (arg1)) 57 58 #define GUEST_ASSERT_2(_condition, arg1, arg2) \ 59 __GUEST_ASSERT(_condition, #_condition, 2, (arg1), (arg2)) 60 61 #define GUEST_ASSERT_3(_condition, arg1, arg2, arg3) \ 62 __GUEST_ASSERT(_condition, #_condition, 3, (arg1), (arg2), (arg3)) 63 64 #define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \ 65 __GUEST_ASSERT(_condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4)) 66 67 #define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) 68 69 #define __REPORT_GUEST_ASSERT(_ucall, fmt, _args...) \ 70 TEST_FAIL("%s at %s:%ld\n" fmt, \ 71 (const char *)(_ucall).args[GUEST_ERROR_STRING], \ 72 (const char *)(_ucall).args[GUEST_FILE], \ 73 (_ucall).args[GUEST_LINE], \ 74 ##_args) 75 76 #define GUEST_ASSERT_ARG(ucall, i) ((ucall).args[GUEST_ASSERT_BUILTIN_NARGS + i]) 77 78 #define REPORT_GUEST_ASSERT(ucall) \ 79 __REPORT_GUEST_ASSERT((ucall), "") 80 81 #define REPORT_GUEST_ASSERT_1(ucall, fmt) \ 82 __REPORT_GUEST_ASSERT((ucall), \ 83 fmt, \ 84 GUEST_ASSERT_ARG((ucall), 0)) 85 86 #define REPORT_GUEST_ASSERT_2(ucall, fmt) \ 87 __REPORT_GUEST_ASSERT((ucall), \ 88 fmt, \ 89 GUEST_ASSERT_ARG((ucall), 0), \ 90 GUEST_ASSERT_ARG((ucall), 1)) 91 92 #define REPORT_GUEST_ASSERT_3(ucall, fmt) \ 93 __REPORT_GUEST_ASSERT((ucall), \ 94 fmt, \ 95 GUEST_ASSERT_ARG((ucall), 0), \ 96 GUEST_ASSERT_ARG((ucall), 1), \ 97 GUEST_ASSERT_ARG((ucall), 2)) 98 99 #define REPORT_GUEST_ASSERT_4(ucall, fmt) \ 100 __REPORT_GUEST_ASSERT((ucall), \ 101 fmt, \ 102 GUEST_ASSERT_ARG((ucall), 0), \ 103 GUEST_ASSERT_ARG((ucall), 1), \ 104 GUEST_ASSERT_ARG((ucall), 2), \ 105 GUEST_ASSERT_ARG((ucall), 3)) 106 107 #define REPORT_GUEST_ASSERT_N(ucall, fmt, args...) \ 108 __REPORT_GUEST_ASSERT((ucall), fmt, ##args) 109 110 #endif /* SELFTEST_KVM_UCALL_COMMON_H */ 111