xref: /kvmtool/include/kvm/util.h (revision 2cc4929cc6b9a382a0c866cf5bbe849e3c0ec271)
1 #include <linux/stringify.h>
2 
3 #ifndef KVM__UTIL_H
4 #define KVM__UTIL_H
5 
6 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
7 
8 /*
9  * Some bits are stolen from perf tool :)
10  */
11 
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <stdbool.h>
19 #include <signal.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <sys/param.h>
23 #include <sys/types.h>
24 #include <linux/types.h>
25 
26 #ifdef __GNUC__
27 #define NORETURN __attribute__((__noreturn__))
28 #else
29 #define NORETURN
30 #ifndef __attribute__
31 #define __attribute__(x)
32 #endif
33 #endif
34 
35 extern bool do_debug_print;
36 
37 #define PROT_RW (PROT_READ|PROT_WRITE)
38 #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
39 
40 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
41 extern void die_perror(const char *s) NORETURN;
42 extern void pr_err(const char *err, ...) __attribute__((format (printf, 1, 2)));
43 extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
44 extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2)));
45 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
46 
47 #define pr_debug(fmt, ...)						\
48 	do {								\
49 		if (do_debug_print)					\
50 			pr_info("(%s) %s:%d: " fmt, __FILE__,		\
51 				__func__, __LINE__, ##__VA_ARGS__);	\
52 	} while (0)
53 
54 
55 #define BUILD_BUG_ON(condition)	((void)sizeof(char[1 - 2*!!(condition)]))
56 
57 #ifndef BUG_ON_HANDLER
58 # define BUG_ON_HANDLER(condition)					\
59 	do {								\
60 		if ((condition)) {					\
61 			pr_err("BUG at %s:%d", __FILE__, __LINE__);	\
62 			raise(SIGABRT);					\
63 		}							\
64 	} while (0)
65 #endif
66 
67 #define BUG_ON(condition)	BUG_ON_HANDLER((condition))
68 
69 #define DIE_IF(cnd)						\
70 do {								\
71 	if (cnd)						\
72 	die(" at (" __FILE__ ":" __stringify(__LINE__) "): "	\
73 		__stringify(cnd) "\n");				\
74 } while (0)
75 
76 #define WARN_ON(condition) ({					\
77 	int __ret_warn_on = !!(condition);			\
78 	if (__ret_warn_on)					\
79 		pr_warning("(%s) %s:%d: failed condition: %s",	\
80 				__FILE__, __func__, __LINE__,	\
81 				__stringify(condition));	\
82 	__ret_warn_on;						\
83 })
84 
85 #define WARN_ONCE(condition, format, args...) ({	\
86 	static int __warned;							\
87 	int __ret_warn_on = !!(condition);				\
88 	if (!__warned && __ret_warn_on) {				\
89 		__warned = 1;								\
90 		pr_warning(format, args);					\
91 	}												\
92 	__ret_warn_on;									\
93 })
94 
95 #define MSECS_TO_USECS(s) ((s) * 1000)
96 
97 /* Millisecond sleep */
98 static inline void msleep(unsigned int msecs)
99 {
100 	usleep(MSECS_TO_USECS(msecs));
101 }
102 
103 /*
104  * Find last (most significant) bit set. Same implementation as Linux:
105  * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64
106  */
107 static inline int fls_long(unsigned long x)
108 {
109 	return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
110 }
111 
112 static inline unsigned long roundup_pow_of_two(unsigned long x)
113 {
114 	return x ? 1UL << fls_long(x - 1) : 0;
115 }
116 
117 #define is_power_of_two(x)	((x) > 0 ? ((x) & ((x) - 1)) == 0 : 0)
118 
119 /**
120  * pow2_size: return the number of bits needed to store values
121  * @x: number of distinct values to store (or number of bytes)
122  *
123  * Determines the number of bits needed to store @x different values.
124  * Could be used to determine the number of address bits needed to
125  * store @x bytes.
126  *
127  * Example:
128  * pow2_size(255) => 8
129  * pow2_size(256) => 8
130  * pow2_size(257) => 9
131  *
132  * Return: number of bits
133  */
134 static inline int pow2_size(unsigned long x)
135 {
136 	if (x <= 1)
137 		return x;
138 
139 	return sizeof(x) * 8 - __builtin_clzl(x - 1);
140 }
141 
142 struct kvm;
143 void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
144 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
145 
146 #endif /* KVM__UTIL_H */
147