1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2021 SUSE LLC <mdoucha@suse.cz>
4 *
5 * Minimal test library for KVM tests
6 */
7
8#ifndef KVM_GUEST_H_
9#define KVM_GUEST_H_
10
11/* The main LTP include dir is intentionally excluded during payload build */
12#include "../../../../include/tst_res_flags.h"
13#undef TERRNO
14#undef TTERRNO
15#undef TRERRNO
16
17#define TST_TEST_TCONF(message) \
18	void main(void) { tst_brk(TCONF, message); }
19
20#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
21
22/* Round x up to the next multiple of a.
23 * a must be a power of 2.
24 */
25#define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK((x), (typeof(x))(a) - 1)
26#define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
27
28#define INTERRUPT_COUNT 32
29
30typedef unsigned long size_t;
31typedef long ssize_t;
32
33typedef signed char int8_t;
34typedef unsigned char uint8_t;
35typedef short int16_t;
36typedef unsigned short uint16_t;
37typedef int int32_t;
38typedef unsigned int uint32_t;
39typedef long long int64_t;
40typedef unsigned long long uint64_t;
41typedef unsigned long uintptr_t;
42
43#define NULL ((void *)0)
44
45void *memset(void *dest, int val, size_t size);
46void *memzero(void *dest, size_t size);
47void *memcpy(void *dest, const void *src, size_t size);
48
49char *strcpy(char *dest, const char *src);
50char *strcat(char *dest, const char *src);
51size_t strlen(const char *str);
52
53/* Exit the VM by looping on a HLT instruction forever */
54void kvm_exit(void) __attribute__((noreturn));
55
56/* Exit the VM using the HLT instruction but allow resume */
57void kvm_yield(void);
58
59void tst_res_(const char *file, const int lineno, int result,
60	const char *message);
61#define tst_res(result, msg) tst_res_(__FILE__, __LINE__, (result), (msg))
62
63void tst_brk_(const char *file, const int lineno, int result,
64	const char *message) __attribute__((noreturn));
65#define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))
66
67/*
68 * Send asynchronous notification to host without stopping VM execution and
69 * return immediately. The notification must be handled by another host thread.
70 * The data argument will be passed to host in test_result->file_addr and
71 * can be used to send additional data both ways.
72 */
73void tst_signal_host(void *data);
74
75/*
76 * Call tst_signal_host(data) and wait for host to call
77 * tst_kvm_clear_guest_signal().
78 */
79void tst_wait_host(void *data);
80
81void *tst_heap_alloc_aligned(size_t size, size_t align);
82void *tst_heap_alloc(size_t size);
83
84/* Arch dependent: */
85
86struct kvm_interrupt_frame;
87
88typedef int (*tst_interrupt_callback)(void *userdata,
89	struct kvm_interrupt_frame *ifrm, unsigned long errcode);
90
91extern const char *tst_interrupt_names[INTERRUPT_COUNT];
92
93void tst_set_interrupt_callback(unsigned int vector,
94	tst_interrupt_callback func, void *userdata);
95
96/* Get the instruction pointer from interrupt frame */
97uintptr_t kvm_get_interrupt_ip(const struct kvm_interrupt_frame *ifrm);
98
99#endif /* KVM_GUEST_H_ */
100