1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2018, Google LLC.
4 */
5#ifndef SELFTEST_KVM_UCALL_COMMON_H
6#define SELFTEST_KVM_UCALL_COMMON_H
7#include "test_util.h"
8#include "ucall.h"
9
10/* Common ucalls */
11enum {
12	UCALL_NONE,
13	UCALL_SYNC,
14	UCALL_ABORT,
15	UCALL_PRINTF,
16	UCALL_DONE,
17	UCALL_UNHANDLED,
18};
19
20#define UCALL_MAX_ARGS 7
21#define UCALL_BUFFER_LEN 1024
22
23struct ucall {
24	uint64_t cmd;
25	uint64_t args[UCALL_MAX_ARGS];
26	char buffer[UCALL_BUFFER_LEN];
27
28	/* Host virtual address of this struct. */
29	struct ucall *hva;
30};
31
32void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
33void ucall_arch_do_ucall(vm_vaddr_t uc);
34void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
35
36void ucall(uint64_t cmd, int nargs, ...);
37__printf(2, 3) void ucall_fmt(uint64_t cmd, const char *fmt, ...);
38__printf(5, 6) void ucall_assert(uint64_t cmd, const char *exp,
39				 const char *file, unsigned int line,
40				 const char *fmt, ...);
41uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
42void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
43int ucall_nr_pages_required(uint64_t page_size);
44
45/*
46 * Perform userspace call without any associated data.  This bare call avoids
47 * allocating a ucall struct, which can be useful if the atomic operations in
48 * the full ucall() are problematic and/or unwanted.  Note, this will come out
49 * as UCALL_NONE on the backend.
50 */
51#define GUEST_UCALL_NONE()	ucall_arch_do_ucall((vm_vaddr_t)NULL)
52
53#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
54				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
55#define GUEST_SYNC(stage)	ucall(UCALL_SYNC, 2, "hello", stage)
56#define GUEST_SYNC1(arg0)	ucall(UCALL_SYNC, 1, arg0)
57#define GUEST_SYNC2(arg0, arg1)	ucall(UCALL_SYNC, 2, arg0, arg1)
58#define GUEST_SYNC3(arg0, arg1, arg2) \
59				ucall(UCALL_SYNC, 3, arg0, arg1, arg2)
60#define GUEST_SYNC4(arg0, arg1, arg2, arg3) \
61				ucall(UCALL_SYNC, 4, arg0, arg1, arg2, arg3)
62#define GUEST_SYNC5(arg0, arg1, arg2, arg3, arg4) \
63				ucall(UCALL_SYNC, 5, arg0, arg1, arg2, arg3, arg4)
64#define GUEST_SYNC6(arg0, arg1, arg2, arg3, arg4, arg5) \
65				ucall(UCALL_SYNC, 6, arg0, arg1, arg2, arg3, arg4, arg5)
66
67#define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)
68#define GUEST_DONE()		ucall(UCALL_DONE, 0)
69
70#define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)
71
72enum guest_assert_builtin_args {
73	GUEST_ERROR_STRING,
74	GUEST_FILE,
75	GUEST_LINE,
76	GUEST_ASSERT_BUILTIN_NARGS
77};
78
79#define ____GUEST_ASSERT(_condition, _exp, _fmt, _args...)				\
80do {											\
81	if (!(_condition))								\
82		ucall_assert(UCALL_ABORT, _exp, __FILE__, __LINE__, _fmt, ##_args);	\
83} while (0)
84
85#define __GUEST_ASSERT(_condition, _fmt, _args...)				\
86	____GUEST_ASSERT(_condition, #_condition, _fmt, ##_args)
87
88#define GUEST_ASSERT(_condition)						\
89	__GUEST_ASSERT(_condition, #_condition)
90
91#define GUEST_FAIL(_fmt, _args...)						\
92	ucall_assert(UCALL_ABORT, "Unconditional guest failure",		\
93		     __FILE__, __LINE__, _fmt, ##_args)
94
95#define GUEST_ASSERT_EQ(a, b)							\
96do {										\
97	typeof(a) __a = (a);							\
98	typeof(b) __b = (b);							\
99	____GUEST_ASSERT(__a == __b, #a " == " #b, "%#lx != %#lx (%s != %s)",	\
100			 (unsigned long)(__a), (unsigned long)(__b), #a, #b);	\
101} while (0)
102
103#define GUEST_ASSERT_NE(a, b)							\
104do {										\
105	typeof(a) __a = (a);							\
106	typeof(b) __b = (b);							\
107	____GUEST_ASSERT(__a != __b, #a " != " #b, "%#lx == %#lx (%s == %s)",	\
108			 (unsigned long)(__a), (unsigned long)(__b), #a, #b);	\
109} while (0)
110
111#define REPORT_GUEST_ASSERT(ucall)						\
112	test_assert(false, (const char *)(ucall).args[GUEST_ERROR_STRING],	\
113		    (const char *)(ucall).args[GUEST_FILE],			\
114		    (ucall).args[GUEST_LINE], "%s", (ucall).buffer)
115
116#endif /* SELFTEST_KVM_UCALL_COMMON_H */
117