1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * tools/testing/selftests/kvm/include/kvm_util.h
5 * Copyright (C) 2018, Google LLC.
7 #ifndef SELFTEST_KVM_UCALL_COMMON_H
8 #define SELFTEST_KVM_UCALL_COMMON_H
22 #define UCALL_MAX_ARGS 7
23 #define UCALL_BUFFER_LEN 1024
27 uint64_t args[UCALL_MAX_ARGS];
28 char buffer[UCALL_BUFFER_LEN];
30 /* Host virtual address of this struct. */
34 void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
35 void ucall_arch_do_ucall(vm_vaddr_t uc);
36 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
38 void ucall(uint64_t cmd, int nargs, ...);
39 void ucall_fmt(uint64_t cmd, const char *fmt, ...);
40 void ucall_assert(uint64_t cmd, const char *exp, const char *file,
41 unsigned int line, const char *fmt, ...);
42 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
43 void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
44 int ucall_nr_pages_required(uint64_t page_size);
47 * Perform userspace call without any associated data. This bare call avoids
48 * allocating a ucall struct, which can be useful if the atomic operations in
49 * the full ucall() are problematic and/or unwanted. Note, this will come out
50 * as UCALL_NONE on the backend.
52 #define GUEST_UCALL_NONE() ucall_arch_do_ucall((vm_vaddr_t)NULL)
54 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \
55 ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
56 #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
57 #define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)
58 #define GUEST_DONE() ucall(UCALL_DONE, 0)
60 #define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)
62 enum guest_assert_builtin_args {
66 GUEST_ASSERT_BUILTIN_NARGS
69 #define ____GUEST_ASSERT(_condition, _exp, _fmt, _args...) \
72 ucall_assert(UCALL_ABORT, _exp, __FILE__, __LINE__, _fmt, ##_args); \
75 #define __GUEST_ASSERT(_condition, _fmt, _args...) \
76 ____GUEST_ASSERT(_condition, #_condition, _fmt, ##_args)
78 #define GUEST_ASSERT(_condition) \
79 __GUEST_ASSERT(_condition, #_condition)
81 #define GUEST_FAIL(_fmt, _args...) \
82 ucall_assert(UCALL_ABORT, "Unconditional guest failure", \
83 __FILE__, __LINE__, _fmt, ##_args)
85 #define GUEST_ASSERT_EQ(a, b) \
87 typeof(a) __a = (a); \
88 typeof(b) __b = (b); \
89 ____GUEST_ASSERT(__a == __b, #a " == " #b, "%#lx != %#lx (%s != %s)", \
90 (unsigned long)(__a), (unsigned long)(__b), #a, #b); \
93 #define GUEST_ASSERT_NE(a, b) \
95 typeof(a) __a = (a); \
96 typeof(b) __b = (b); \
97 ____GUEST_ASSERT(__a != __b, #a " != " #b, "%#lx == %#lx (%s == %s)", \
98 (unsigned long)(__a), (unsigned long)(__b), #a, #b); \
101 #define REPORT_GUEST_ASSERT(ucall) \
102 test_assert(false, (const char *)(ucall).args[GUEST_ERROR_STRING], \
103 (const char *)(ucall).args[GUEST_FILE], \
104 (ucall).args[GUEST_LINE], "%s", (ucall).buffer)
106 #endif /* SELFTEST_KVM_UCALL_COMMON_H */