powerpc/mm: Avoid calling arch_enter/leave_lazy_mmu() in set_ptes
[platform/kernel/linux-starfive.git] / tools / testing / selftests / kvm / include / ucall_common.h
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 #include "ucall.h"
11
12 /* Common ucalls */
13 enum {
14         UCALL_NONE,
15         UCALL_SYNC,
16         UCALL_ABORT,
17         UCALL_PRINTF,
18         UCALL_DONE,
19         UCALL_UNHANDLED,
20 };
21
22 #define UCALL_MAX_ARGS 7
23 #define UCALL_BUFFER_LEN 1024
24
25 struct ucall {
26         uint64_t cmd;
27         uint64_t args[UCALL_MAX_ARGS];
28         char buffer[UCALL_BUFFER_LEN];
29
30         /* Host virtual address of this struct. */
31         struct ucall *hva;
32 };
33
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);
37
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);
45
46 /*
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.
51  */
52 #define GUEST_UCALL_NONE()      ucall_arch_do_ucall((vm_vaddr_t)NULL)
53
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)
59
60 #define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)
61
62 enum guest_assert_builtin_args {
63         GUEST_ERROR_STRING,
64         GUEST_FILE,
65         GUEST_LINE,
66         GUEST_ASSERT_BUILTIN_NARGS
67 };
68
69 #define ____GUEST_ASSERT(_condition, _exp, _fmt, _args...)                              \
70 do {                                                                                    \
71         if (!(_condition))                                                              \
72                 ucall_assert(UCALL_ABORT, _exp, __FILE__, __LINE__, _fmt, ##_args);     \
73 } while (0)
74
75 #define __GUEST_ASSERT(_condition, _fmt, _args...)                              \
76         ____GUEST_ASSERT(_condition, #_condition, _fmt, ##_args)
77
78 #define GUEST_ASSERT(_condition)                                                \
79         __GUEST_ASSERT(_condition, #_condition)
80
81 #define GUEST_FAIL(_fmt, _args...)                                              \
82         ucall_assert(UCALL_ABORT, "Unconditional guest failure",                \
83                      __FILE__, __LINE__, _fmt, ##_args)
84
85 #define GUEST_ASSERT_EQ(a, b)                                                   \
86 do {                                                                            \
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);   \
91 } while (0)
92
93 #define GUEST_ASSERT_NE(a, b)                                                   \
94 do {                                                                            \
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);   \
99 } while (0)
100
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)
105
106 #endif /* SELFTEST_KVM_UCALL_COMMON_H */