xref: /kvmtool/include/kvm/util.h (revision 610612576a8a68477e7e8289fca6df50caf43cd6)
1ac600533SPrasad Joshi #include <linux/stringify.h>
2ac600533SPrasad Joshi 
3f3150089SPekka Enberg #ifndef KVM__UTIL_H
4f3150089SPekka Enberg #define KVM__UTIL_H
5ad054a21SCyrill Gorcunov 
62a601aafSPekka Enberg #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
72a601aafSPekka Enberg 
8ad054a21SCyrill Gorcunov /*
9ad054a21SCyrill Gorcunov  * Some bits are stolen from perf tool :)
10ad054a21SCyrill Gorcunov  */
11ad054a21SCyrill Gorcunov 
12ad054a21SCyrill Gorcunov #include <unistd.h>
13ad054a21SCyrill Gorcunov #include <stdio.h>
14ad054a21SCyrill Gorcunov #include <stddef.h>
15ad054a21SCyrill Gorcunov #include <stdlib.h>
16ad054a21SCyrill Gorcunov #include <stdarg.h>
17ad054a21SCyrill Gorcunov #include <string.h>
18ed036f03SCyrill Gorcunov #include <stdbool.h>
19ad054a21SCyrill Gorcunov #include <errno.h>
20ad054a21SCyrill Gorcunov #include <limits.h>
21ad054a21SCyrill Gorcunov #include <sys/param.h>
22ad054a21SCyrill Gorcunov #include <sys/types.h>
23*61061257SMatt Evans #include <linux/types.h>
24ad054a21SCyrill Gorcunov 
25ad054a21SCyrill Gorcunov #ifdef __GNUC__
26ad054a21SCyrill Gorcunov #define NORETURN __attribute__((__noreturn__))
27ad054a21SCyrill Gorcunov #else
28ad054a21SCyrill Gorcunov #define NORETURN
29ad054a21SCyrill Gorcunov #ifndef __attribute__
30ad054a21SCyrill Gorcunov #define __attribute__(x)
31ad054a21SCyrill Gorcunov #endif
32ad054a21SCyrill Gorcunov #endif
33ad054a21SCyrill Gorcunov 
34ed036f03SCyrill Gorcunov extern bool do_debug_print;
35ed036f03SCyrill Gorcunov 
3637c34ca8SSasha Levin #define PROT_RW (PROT_READ|PROT_WRITE)
3737c34ca8SSasha Levin #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
3837c34ca8SSasha Levin 
39ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
40ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN;
414542f276SCyrill Gorcunov extern int pr_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
424542f276SCyrill Gorcunov extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
434542f276SCyrill Gorcunov extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2)));
44ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
45ad054a21SCyrill Gorcunov 
464542f276SCyrill Gorcunov #define pr_debug(fmt, ...)						\
47ed036f03SCyrill Gorcunov 	do {								\
48ed036f03SCyrill Gorcunov 		if (do_debug_print)					\
494542f276SCyrill Gorcunov 			pr_info("(%s) %s:%d: " fmt, __FILE__,		\
50ed036f03SCyrill Gorcunov 				__func__, __LINE__, ##__VA_ARGS__);	\
51ed036f03SCyrill Gorcunov 	} while (0)
52ed036f03SCyrill Gorcunov 
53b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
54b3594ec7SCyrill Gorcunov 
55ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
56ad054a21SCyrill Gorcunov do {								\
57ad054a21SCyrill Gorcunov 	if (cnd)						\
58ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
59ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
60ad054a21SCyrill Gorcunov } while (0)
61ad054a21SCyrill Gorcunov 
62aa400b00SPrasad Joshi #define MSECS_TO_USECS(s) ((s) * 1000)
63aa400b00SPrasad Joshi 
64aa400b00SPrasad Joshi /* Millisecond sleep */
65aa400b00SPrasad Joshi static inline void msleep(unsigned int msecs)
66aa400b00SPrasad Joshi {
67aa400b00SPrasad Joshi 	usleep(MSECS_TO_USECS(msecs));
68aa400b00SPrasad Joshi }
69*61061257SMatt Evans 
70*61061257SMatt Evans void *mmap_hugetlbfs(const char *htlbfs_path, u64 size);
71*61061257SMatt Evans 
72f3150089SPekka Enberg #endif /* KVM__UTIL_H */
73