Path: blob/master/tools/testing/selftests/kvm/include/ucall_common.h
38237 views
/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (C) 2018, Google LLC.3*/4#ifndef SELFTEST_KVM_UCALL_COMMON_H5#define SELFTEST_KVM_UCALL_COMMON_H6#include "test_util.h"7#include "ucall.h"89/* Common ucalls */10enum {11UCALL_NONE,12UCALL_SYNC,13UCALL_ABORT,14UCALL_PRINTF,15UCALL_DONE,16UCALL_UNHANDLED,17};1819#define UCALL_MAX_ARGS 720#define UCALL_BUFFER_LEN 10242122struct ucall {23uint64_t cmd;24uint64_t args[UCALL_MAX_ARGS];25char buffer[UCALL_BUFFER_LEN];2627/* Host virtual address of this struct. */28struct ucall *hva;29};3031void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);32void ucall_arch_do_ucall(vm_vaddr_t uc);33void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);3435void ucall(uint64_t cmd, int nargs, ...);36__printf(2, 3) void ucall_fmt(uint64_t cmd, const char *fmt, ...);37__printf(5, 6) void ucall_assert(uint64_t cmd, const char *exp,38const char *file, unsigned int line,39const char *fmt, ...);40uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);41void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);42int ucall_nr_pages_required(uint64_t page_size);4344/*45* Perform userspace call without any associated data. This bare call avoids46* allocating a ucall struct, which can be useful if the atomic operations in47* the full ucall() are problematic and/or unwanted. Note, this will come out48* as UCALL_NONE on the backend.49*/50#define GUEST_UCALL_NONE() ucall_arch_do_ucall((vm_vaddr_t)NULL)5152#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \53ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)54#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)55#define GUEST_SYNC1(arg0) ucall(UCALL_SYNC, 1, arg0)56#define GUEST_SYNC2(arg0, arg1) ucall(UCALL_SYNC, 2, arg0, arg1)57#define GUEST_SYNC3(arg0, arg1, arg2) \58ucall(UCALL_SYNC, 3, arg0, arg1, arg2)59#define GUEST_SYNC4(arg0, arg1, arg2, arg3) \60ucall(UCALL_SYNC, 4, arg0, arg1, arg2, arg3)61#define GUEST_SYNC5(arg0, arg1, arg2, arg3, arg4) \62ucall(UCALL_SYNC, 5, arg0, arg1, arg2, arg3, arg4)63#define GUEST_SYNC6(arg0, arg1, arg2, arg3, arg4, arg5) \64ucall(UCALL_SYNC, 6, arg0, arg1, arg2, arg3, arg4, arg5)6566#define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)67#define GUEST_DONE() ucall(UCALL_DONE, 0)6869#define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)7071enum guest_assert_builtin_args {72GUEST_ERROR_STRING,73GUEST_FILE,74GUEST_LINE,75GUEST_ASSERT_BUILTIN_NARGS76};7778#define ____GUEST_ASSERT(_condition, _exp, _fmt, _args...) \79do { \80if (!(_condition)) \81ucall_assert(UCALL_ABORT, _exp, __FILE__, __LINE__, _fmt, ##_args); \82} while (0)8384#define __GUEST_ASSERT(_condition, _fmt, _args...) \85____GUEST_ASSERT(_condition, #_condition, _fmt, ##_args)8687#define GUEST_ASSERT(_condition) \88__GUEST_ASSERT(_condition, #_condition)8990#define GUEST_FAIL(_fmt, _args...) \91ucall_assert(UCALL_ABORT, "Unconditional guest failure", \92__FILE__, __LINE__, _fmt, ##_args)9394#define GUEST_ASSERT_EQ(a, b) \95do { \96typeof(a) __a = (a); \97typeof(b) __b = (b); \98____GUEST_ASSERT(__a == __b, #a " == " #b, "%#lx != %#lx (%s != %s)", \99(unsigned long)(__a), (unsigned long)(__b), #a, #b); \100} while (0)101102#define GUEST_ASSERT_NE(a, b) \103do { \104typeof(a) __a = (a); \105typeof(b) __b = (b); \106____GUEST_ASSERT(__a != __b, #a " != " #b, "%#lx == %#lx (%s == %s)", \107(unsigned long)(__a), (unsigned long)(__b), #a, #b); \108} while (0)109110#define REPORT_GUEST_ASSERT(ucall) \111test_assert(false, (const char *)(ucall).args[GUEST_ERROR_STRING], \112(const char *)(ucall).args[GUEST_FILE], \113(ucall).args[GUEST_LINE], "%s", (ucall).buffer)114115#endif /* SELFTEST_KVM_UCALL_COMMON_H */116117118