xref: /kvmtool/include/kvm/util.h (revision bd4ba57156dad39349edfb2338bdc2f4ed3c0bae)
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 
35*bd4ba571SAlexandru Elisei #define LOGLEVEL_ERROR		0
36*bd4ba571SAlexandru Elisei #define LOGLEVEL_WARNING	1
37*bd4ba571SAlexandru Elisei #define LOGLEVEL_INFO		2
38*bd4ba571SAlexandru Elisei #define LOGLEVEL_DEBUG		3
39ed036f03SCyrill Gorcunov 
4037c34ca8SSasha Levin #define PROT_RW (PROT_READ|PROT_WRITE)
4137c34ca8SSasha Levin #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
4237c34ca8SSasha Levin 
43ad054a21SCyrill Gorcunov extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
44ad054a21SCyrill Gorcunov extern void die_perror(const char *s) NORETURN;
452cc4929cSAlexandru Elisei extern void pr_err(const char *err, ...) __attribute__((format (printf, 1, 2)));
464542f276SCyrill Gorcunov extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
474542f276SCyrill Gorcunov extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2)));
48fc184a68SAlexandru Elisei extern void __pr_debug(const char *debug, ...) __attribute__((format (printf, 1, 2)));
49ad054a21SCyrill Gorcunov extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
50ad054a21SCyrill Gorcunov 
51*bd4ba571SAlexandru Elisei extern int loglevel;
52*bd4ba571SAlexandru Elisei 
534542f276SCyrill Gorcunov #define pr_debug(fmt, ...)						\
54ed036f03SCyrill Gorcunov 	do {								\
55*bd4ba571SAlexandru Elisei 		if (loglevel >= LOGLEVEL_DEBUG)				\
56fc184a68SAlexandru Elisei 			__pr_debug("(%s) %s:%d: " fmt, __FILE__,	\
57ed036f03SCyrill Gorcunov 				__func__, __LINE__, ##__VA_ARGS__);	\
58ed036f03SCyrill Gorcunov 	} while (0)
59ed036f03SCyrill Gorcunov 
604d95c112SCyrill Gorcunov 
61b3594ec7SCyrill Gorcunov #define BUILD_BUG_ON(condition)	((void)sizeof(char[1 - 2*!!(condition)]))
624d95c112SCyrill Gorcunov 
634d95c112SCyrill Gorcunov #ifndef BUG_ON_HANDLER
644d95c112SCyrill Gorcunov # define BUG_ON_HANDLER(condition)					\
654d95c112SCyrill Gorcunov 	do {								\
664d95c112SCyrill Gorcunov 		if ((condition)) {					\
674d95c112SCyrill Gorcunov 			pr_err("BUG at %s:%d", __FILE__, __LINE__);	\
684d95c112SCyrill Gorcunov 			raise(SIGABRT);					\
694d95c112SCyrill Gorcunov 		}							\
704d95c112SCyrill Gorcunov 	} while (0)
714d95c112SCyrill Gorcunov #endif
724d95c112SCyrill Gorcunov 
734d95c112SCyrill Gorcunov #define BUG_ON(condition)	BUG_ON_HANDLER((condition))
74b3594ec7SCyrill Gorcunov 
75ad054a21SCyrill Gorcunov #define DIE_IF(cnd)						\
76ad054a21SCyrill Gorcunov do {								\
77ad054a21SCyrill Gorcunov 	if (cnd)						\
78ad054a21SCyrill Gorcunov 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
79ad054a21SCyrill Gorcunov 		__stringify(cnd) "\n");				\
80ad054a21SCyrill Gorcunov } while (0)
81ad054a21SCyrill Gorcunov 
82d642f038SLai Jiangshan #define WARN_ON(condition) ({					\
83d642f038SLai Jiangshan 	int __ret_warn_on = !!(condition);			\
84d642f038SLai Jiangshan 	if (__ret_warn_on)					\
85d642f038SLai Jiangshan 		pr_warning("(%s) %s:%d: failed condition: %s",	\
86d642f038SLai Jiangshan 				__FILE__, __func__, __LINE__,	\
87d642f038SLai Jiangshan 				__stringify(condition));	\
88d642f038SLai Jiangshan 	__ret_warn_on;						\
89d642f038SLai Jiangshan })
90d642f038SLai Jiangshan 
91143ffa22SMartin Radev #define WARN_ONCE(condition, format, args...) ({	\
92143ffa22SMartin Radev 	static int __warned;							\
93143ffa22SMartin Radev 	int __ret_warn_on = !!(condition);				\
94143ffa22SMartin Radev 	if (!__warned && __ret_warn_on) {				\
95143ffa22SMartin Radev 		__warned = 1;								\
96143ffa22SMartin Radev 		pr_warning(format, args);					\
97143ffa22SMartin Radev 	}												\
98143ffa22SMartin Radev 	__ret_warn_on;									\
99143ffa22SMartin Radev })
100143ffa22SMartin Radev 
101aa400b00SPrasad Joshi #define MSECS_TO_USECS(s) ((s) * 1000)
102aa400b00SPrasad Joshi 
103aa400b00SPrasad Joshi /* Millisecond sleep */
msleep(unsigned int msecs)104aa400b00SPrasad Joshi static inline void msleep(unsigned int msecs)
105aa400b00SPrasad Joshi {
106aa400b00SPrasad Joshi 	usleep(MSECS_TO_USECS(msecs));
107aa400b00SPrasad Joshi }
10861061257SMatt Evans 
109ac70b5aaSJean-Philippe Brucker /*
110ac70b5aaSJean-Philippe Brucker  * Find last (most significant) bit set. Same implementation as Linux:
111ac70b5aaSJean-Philippe Brucker  * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64
112ac70b5aaSJean-Philippe Brucker  */
fls_long(unsigned long x)113ac70b5aaSJean-Philippe Brucker static inline int fls_long(unsigned long x)
114ac70b5aaSJean-Philippe Brucker {
115ac70b5aaSJean-Philippe Brucker 	return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
116ac70b5aaSJean-Philippe Brucker }
117ac70b5aaSJean-Philippe Brucker 
roundup_pow_of_two(unsigned long x)118ac70b5aaSJean-Philippe Brucker static inline unsigned long roundup_pow_of_two(unsigned long x)
119ac70b5aaSJean-Philippe Brucker {
120ac70b5aaSJean-Philippe Brucker 	return x ? 1UL << fls_long(x - 1) : 0;
121ac70b5aaSJean-Philippe Brucker }
122ac70b5aaSJean-Philippe Brucker 
123ce2fc8f5SAlexandru Elisei #define is_power_of_two(x)	((x) > 0 ? ((x) & ((x) - 1)) == 0 : 0)
124ce2fc8f5SAlexandru Elisei 
12525cf3198SRaphael Gault /**
12625cf3198SRaphael Gault  * pow2_size: return the number of bits needed to store values
12725cf3198SRaphael Gault  * @x: number of distinct values to store (or number of bytes)
12825cf3198SRaphael Gault  *
12925cf3198SRaphael Gault  * Determines the number of bits needed to store @x different values.
13025cf3198SRaphael Gault  * Could be used to determine the number of address bits needed to
13125cf3198SRaphael Gault  * store @x bytes.
13225cf3198SRaphael Gault  *
13325cf3198SRaphael Gault  * Example:
13425cf3198SRaphael Gault  * pow2_size(255) => 8
13525cf3198SRaphael Gault  * pow2_size(256) => 8
13625cf3198SRaphael Gault  * pow2_size(257) => 9
13725cf3198SRaphael Gault  *
13825cf3198SRaphael Gault  * Return: number of bits
13925cf3198SRaphael Gault  */
pow2_size(unsigned long x)14025cf3198SRaphael Gault static inline int pow2_size(unsigned long x)
14125cf3198SRaphael Gault {
14225cf3198SRaphael Gault 	if (x <= 1)
14325cf3198SRaphael Gault 		return x;
14425cf3198SRaphael Gault 
14525cf3198SRaphael Gault 	return sizeof(x) * 8 - __builtin_clzl(x - 1);
14625cf3198SRaphael Gault }
14725cf3198SRaphael Gault 
1483ebd8e0bSMichael Ellerman struct kvm;
1493ebd8e0bSMichael Ellerman void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
1503ebd8e0bSMichael Ellerman void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
15161061257SMatt Evans 
152f3150089SPekka Enberg #endif /* KVM__UTIL_H */
153