• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 typedef unsigned long size_t;
31 typedef long ssize_t;
32 
33 typedef signed char int8_t;
34 typedef unsigned char uint8_t;
35 typedef short int16_t;
36 typedef unsigned short uint16_t;
37 typedef int int32_t;
38 typedef unsigned int uint32_t;
39 typedef long long int64_t;
40 typedef unsigned long long uint64_t;
41 typedef unsigned long uintptr_t;
42 
43 #define NULL ((void *)0)
44 
45 void *memset(void *dest, int val, size_t size);
46 void *memzero(void *dest, size_t size);
47 void *memcpy(void *dest, const void *src, size_t size);
48 
49 char *strcpy(char *dest, const char *src);
50 char *strcat(char *dest, const char *src);
51 size_t strlen(const char *str);
52 
53 /* Exit the VM by looping on a HLT instruction forever */
54 void kvm_exit(void) __attribute__((noreturn));
55 
56 /* Exit the VM using the HLT instruction but allow resume */
57 void kvm_yield(void);
58 
59 void 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 
63 void 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  */
73 void 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  */
79 void tst_wait_host(void *data);
80 
81 void *tst_heap_alloc_aligned(size_t size, size_t align);
82 void *tst_heap_alloc(size_t size);
83 
84 /* Arch dependent: */
85 
86 struct kvm_interrupt_frame;
87 
88 typedef int (*tst_interrupt_callback)(void *userdata,
89 	struct kvm_interrupt_frame *ifrm, unsigned long errcode);
90 
91 extern const char *tst_interrupt_names[INTERRUPT_COUNT];
92 
93 void tst_set_interrupt_callback(unsigned int vector,
94 	tst_interrupt_callback func, void *userdata);
95 
96 /* Get the instruction pointer from interrupt frame */
97 uintptr_t kvm_get_interrupt_ip(const struct kvm_interrupt_frame *ifrm);
98 
99 #endif /* KVM_GUEST_H_ */
100