xref: /kvmtool/include/kvm/util.h (revision 143ffa2221d38b9405ce89552363b76cf3f6915c)
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 
85*143ffa22SMartin Radev #define WARN_ONCE(condition, format, args...) ({	\
86*143ffa22SMartin Radev 	static int __warned;							\
87*143ffa22SMartin Radev 	int __ret_warn_on = !!(condition);				\
88*143ffa22SMartin Radev 	if (!__warned && __ret_warn_on) {				\
89*143ffa22SMartin Radev 		__warned = 1;								\
90*143ffa22SMartin Radev 		pr_warning(format, args);					\
91*143ffa22SMartin Radev 	}												\
92*143ffa22SMartin Radev 	__ret_warn_on;									\
93*143ffa22SMartin Radev })
94*143ffa22SMartin Radev 
95aa400b00SPrasad Joshi #define MSECS_TO_USECS(s) ((s) * 1000)
96aa400b00SPrasad Joshi 
97aa400b00SPrasad Joshi /* Millisecond sleep */
98aa400b00SPrasad Joshi static inline void msleep(unsigned int msecs)
99aa400b00SPrasad Joshi {
100aa400b00SPrasad Joshi 	usleep(MSECS_TO_USECS(msecs));
101aa400b00SPrasad Joshi }
10261061257SMatt Evans 
103ac70b5aaSJean-Philippe Brucker /*
104ac70b5aaSJean-Philippe Brucker  * Find last (most significant) bit set. Same implementation as Linux:
105ac70b5aaSJean-Philippe Brucker  * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64
106ac70b5aaSJean-Philippe Brucker  */
107ac70b5aaSJean-Philippe Brucker static inline int fls_long(unsigned long x)
108ac70b5aaSJean-Philippe Brucker {
109ac70b5aaSJean-Philippe Brucker 	return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
110ac70b5aaSJean-Philippe Brucker }
111ac70b5aaSJean-Philippe Brucker 
112ac70b5aaSJean-Philippe Brucker static inline unsigned long roundup_pow_of_two(unsigned long x)
113ac70b5aaSJean-Philippe Brucker {
114ac70b5aaSJean-Philippe Brucker 	return x ? 1UL << fls_long(x - 1) : 0;
115ac70b5aaSJean-Philippe Brucker }
116ac70b5aaSJean-Philippe Brucker 
117ce2fc8f5SAlexandru Elisei #define is_power_of_two(x)	((x) > 0 ? ((x) & ((x) - 1)) == 0 : 0)
118ce2fc8f5SAlexandru Elisei 
11925cf3198SRaphael Gault /**
12025cf3198SRaphael Gault  * pow2_size: return the number of bits needed to store values
12125cf3198SRaphael Gault  * @x: number of distinct values to store (or number of bytes)
12225cf3198SRaphael Gault  *
12325cf3198SRaphael Gault  * Determines the number of bits needed to store @x different values.
12425cf3198SRaphael Gault  * Could be used to determine the number of address bits needed to
12525cf3198SRaphael Gault  * store @x bytes.
12625cf3198SRaphael Gault  *
12725cf3198SRaphael Gault  * Example:
12825cf3198SRaphael Gault  * pow2_size(255) => 8
12925cf3198SRaphael Gault  * pow2_size(256) => 8
13025cf3198SRaphael Gault  * pow2_size(257) => 9
13125cf3198SRaphael Gault  *
13225cf3198SRaphael Gault  * Return: number of bits
13325cf3198SRaphael Gault  */
13425cf3198SRaphael Gault static inline int pow2_size(unsigned long x)
13525cf3198SRaphael Gault {
13625cf3198SRaphael Gault 	if (x <= 1)
13725cf3198SRaphael Gault 		return x;
13825cf3198SRaphael Gault 
13925cf3198SRaphael Gault 	return sizeof(x) * 8 - __builtin_clzl(x - 1);
14025cf3198SRaphael Gault }
14125cf3198SRaphael Gault 
1423ebd8e0bSMichael Ellerman struct kvm;
1433ebd8e0bSMichael Ellerman void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
1443ebd8e0bSMichael Ellerman void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
14561061257SMatt Evans 
146f3150089SPekka Enberg #endif /* KVM__UTIL_H */
147