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