• 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 void *tst_heap_alloc_aligned(size_t size, size_t align);
68 void *tst_heap_alloc(size_t size);
69 
70 /* Arch dependent: */
71 
72 struct kvm_interrupt_frame;
73 
74 typedef int (*tst_interrupt_callback)(void *userdata,
75 	struct kvm_interrupt_frame *ifrm, unsigned long errcode);
76 
77 extern const char *tst_interrupt_names[INTERRUPT_COUNT];
78 
79 void tst_set_interrupt_callback(unsigned int vector,
80 	tst_interrupt_callback func, void *userdata);
81 
82 /* Get the instruction pointer from interrupt frame */
83 uintptr_t kvm_get_interrupt_ip(const struct kvm_interrupt_frame *ifrm);
84 
85 #endif /* KVM_GUEST_H_ */
86