xref: /kvmtool/include/kvm/util.h (revision ce2fc8f56145d88480b9e0b4229793afb216da6c)
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>
194d95c112SCyrill Gorcunov #include <signal.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 
544d95c112SCyrill Gorcunov 
55b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition)	((void)sizeof(char[1 - 2*!!(condition)]))
564d95c112SCyrill Gorcunov 
574d95c112SCyrill Gorcunov #ifndef BUG_ON_HANDLER
584d95c112SCyrill Gorcunov # define BUG_ON_HANDLER(condition)					\
594d95c112SCyrill Gorcunov 	do {								\
604d95c112SCyrill Gorcunov 		if ((condition)) {					\
614d95c112SCyrill Gorcunov 			pr_err("BUG at %s:%d", __FILE__, __LINE__);	\
624d95c112SCyrill Gorcunov 			raise(SIGABRT);					\
634d95c112SCyrill Gorcunov 		}							\
644d95c112SCyrill Gorcunov 	} while (0)
654d95c112SCyrill Gorcunov #endif
664d95c112SCyrill Gorcunov 
674d95c112SCyrill Gorcunov #define BUG_ON(condition)	BUG_ON_HANDLER((condition))
68b3594ec7SCyrill Gorcunov 
69ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
70ad054a21SCyrill Gorcunov do {								\
71ad054a21SCyrill Gorcunov 	if (cnd)						\
72ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
73ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
74ad054a21SCyrill Gorcunov } while (0)
75ad054a21SCyrill Gorcunov 
76d642f038SLai Jiangshan #define WARN_ON(condition) ({					\
77d642f038SLai Jiangshan 	int __ret_warn_on = !!(condition);			\
78d642f038SLai Jiangshan 	if (__ret_warn_on)					\
79d642f038SLai Jiangshan 		pr_warning("(%s) %s:%d: failed condition: %s",	\
80d642f038SLai Jiangshan 				__FILE__, __func__, __LINE__,	\
81d642f038SLai Jiangshan 				__stringify(condition));	\
82d642f038SLai Jiangshan 	__ret_warn_on;						\
83d642f038SLai Jiangshan })
84d642f038SLai Jiangshan 
85aa400b00SPrasad Joshi #define MSECS_TO_USECS(s) ((s) * 1000)
86aa400b00SPrasad Joshi 
87aa400b00SPrasad Joshi /* Millisecond sleep */
88aa400b00SPrasad Joshi static inline void msleep(unsigned int msecs)
89aa400b00SPrasad Joshi {
90aa400b00SPrasad Joshi 	usleep(MSECS_TO_USECS(msecs));
91aa400b00SPrasad Joshi }
9261061257SMatt Evans 
93ac70b5aaSJean-Philippe Brucker /*
94ac70b5aaSJean-Philippe Brucker  * Find last (most significant) bit set. Same implementation as Linux:
95ac70b5aaSJean-Philippe Brucker  * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64
96ac70b5aaSJean-Philippe Brucker  */
97ac70b5aaSJean-Philippe Brucker static inline int fls_long(unsigned long x)
98ac70b5aaSJean-Philippe Brucker {
99ac70b5aaSJean-Philippe Brucker 	return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
100ac70b5aaSJean-Philippe Brucker }
101ac70b5aaSJean-Philippe Brucker 
102ac70b5aaSJean-Philippe Brucker static inline unsigned long roundup_pow_of_two(unsigned long x)
103ac70b5aaSJean-Philippe Brucker {
104ac70b5aaSJean-Philippe Brucker 	return x ? 1UL << fls_long(x - 1) : 0;
105ac70b5aaSJean-Philippe Brucker }
106ac70b5aaSJean-Philippe Brucker 
107*ce2fc8f5SAlexandru Elisei #define is_power_of_two(x)	((x) > 0 ? ((x) & ((x) - 1)) == 0 : 0)
108*ce2fc8f5SAlexandru Elisei 
1093ebd8e0bSMichael Ellerman struct kvm;
1103ebd8e0bSMichael Ellerman void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
1113ebd8e0bSMichael Ellerman void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
11261061257SMatt Evans 
113f3150089SPekka Enberg #endif /* KVM__UTIL_H */
114