xref: /kvmtool/include/kvm/util.h (revision d642f038d7eadbfe4fd47da1fb60be0f9b13a9fe)
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 
12a2857479SCyrill Gorcunov #include <assert.h>
13ad054a21SCyrill Gorcunov #include <unistd.h>
14ad054a21SCyrill Gorcunov #include <stdio.h>
15ad054a21SCyrill Gorcunov #include <stddef.h>
16ad054a21SCyrill Gorcunov #include <stdlib.h>
17ad054a21SCyrill Gorcunov #include <stdarg.h>
18ad054a21SCyrill Gorcunov #include <string.h>
19ed036f03SCyrill Gorcunov #include <stdbool.h>
20ad054a21SCyrill Gorcunov #include <errno.h>
21ad054a21SCyrill Gorcunov #include <limits.h>
22ad054a21SCyrill Gorcunov #include <sys/param.h>
23ad054a21SCyrill Gorcunov #include <sys/types.h>
2461061257SMatt Evans #include <linux/types.h>
25ad054a21SCyrill Gorcunov 
26ad054a21SCyrill Gorcunov #ifdef __GNUC__
27ad054a21SCyrill Gorcunov #define NORETURN __attribute__((__noreturn__))
28ad054a21SCyrill Gorcunov #else
29ad054a21SCyrill Gorcunov #define NORETURN
30ad054a21SCyrill Gorcunov #ifndef __attribute__
31ad054a21SCyrill Gorcunov #define __attribute__(x)
32ad054a21SCyrill Gorcunov #endif
33ad054a21SCyrill Gorcunov #endif
34ad054a21SCyrill Gorcunov 
35ed036f03SCyrill Gorcunov extern bool do_debug_print;
36ed036f03SCyrill Gorcunov 
3737c34ca8SSasha Levin #define PROT_RW (PROT_READ|PROT_WRITE)
3837c34ca8SSasha Levin #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
3937c34ca8SSasha Levin 
40ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
41ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN;
42599ed2a8SCyrill Gorcunov extern int pr_err(const char *err, ...) __attribute__((format (printf, 1, 2)));
434542f276SCyrill Gorcunov extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
444542f276SCyrill Gorcunov extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2)));
45ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
46ad054a21SCyrill Gorcunov 
474542f276SCyrill Gorcunov #define pr_debug(fmt, ...)						\
48ed036f03SCyrill Gorcunov 	do {								\
49ed036f03SCyrill Gorcunov 		if (do_debug_print)					\
504542f276SCyrill Gorcunov 			pr_info("(%s) %s:%d: " fmt, __FILE__,		\
51ed036f03SCyrill Gorcunov 				__func__, __LINE__, ##__VA_ARGS__);	\
52ed036f03SCyrill Gorcunov 	} while (0)
53ed036f03SCyrill Gorcunov 
54a2857479SCyrill Gorcunov #
55b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition)	((void)sizeof(char[1 - 2*!!(condition)]))
56a2857479SCyrill Gorcunov #define BUG_ON(condition)	assert(!(condition))
57b3594ec7SCyrill Gorcunov 
58ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
59ad054a21SCyrill Gorcunov do {								\
60ad054a21SCyrill Gorcunov 	if (cnd)						\
61ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
62ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
63ad054a21SCyrill Gorcunov } while (0)
64ad054a21SCyrill Gorcunov 
65*d642f038SLai Jiangshan #define WARN_ON(condition) ({					\
66*d642f038SLai Jiangshan 	int __ret_warn_on = !!(condition);			\
67*d642f038SLai Jiangshan 	if (__ret_warn_on)					\
68*d642f038SLai Jiangshan 		pr_warning("(%s) %s:%d: failed condition: %s",	\
69*d642f038SLai Jiangshan 				__FILE__, __func__, __LINE__,	\
70*d642f038SLai Jiangshan 				__stringify(condition));	\
71*d642f038SLai Jiangshan 	__ret_warn_on;						\
72*d642f038SLai Jiangshan })
73*d642f038SLai Jiangshan 
74aa400b00SPrasad Joshi #define MSECS_TO_USECS(s) ((s) * 1000)
75aa400b00SPrasad Joshi 
76aa400b00SPrasad Joshi /* Millisecond sleep */
77aa400b00SPrasad Joshi static inline void msleep(unsigned int msecs)
78aa400b00SPrasad Joshi {
79aa400b00SPrasad Joshi 	usleep(MSECS_TO_USECS(msecs));
80aa400b00SPrasad Joshi }
8161061257SMatt Evans 
8261061257SMatt Evans void *mmap_hugetlbfs(const char *htlbfs_path, u64 size);
8361061257SMatt Evans 
84f3150089SPekka Enberg #endif /* KVM__UTIL_H */
85