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 #define LOGLEVEL_ERROR 0
36 #define LOGLEVEL_WARNING 1
37 #define LOGLEVEL_INFO 2
38 #define LOGLEVEL_DEBUG 3
39
40 #define PROT_RW (PROT_READ|PROT_WRITE)
41 #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE)
42
43 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
44 extern void die_perror(const char *s) NORETURN;
45 extern void pr_err(const char *err, ...) __attribute__((format (printf, 1, 2)));
46 extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
47 extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2)));
48 extern void __pr_debug(const char *debug, ...) __attribute__((format (printf, 1, 2)));
49 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
50
51 extern int loglevel;
52
53 #define pr_debug(fmt, ...) \
54 do { \
55 if (loglevel >= LOGLEVEL_DEBUG) \
56 __pr_debug("(%s) %s:%d: " fmt, __FILE__, \
57 __func__, __LINE__, ##__VA_ARGS__); \
58 } while (0)
59
60
61 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
62
63 #ifndef BUG_ON_HANDLER
64 # define BUG_ON_HANDLER(condition) \
65 do { \
66 if ((condition)) { \
67 pr_err("BUG at %s:%d", __FILE__, __LINE__); \
68 raise(SIGABRT); \
69 } \
70 } while (0)
71 #endif
72
73 #define BUG_ON(condition) BUG_ON_HANDLER((condition))
74
75 #define DIE_IF(cnd) \
76 do { \
77 if (cnd) \
78 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
79 __stringify(cnd) "\n"); \
80 } while (0)
81
82 #define WARN_ON(condition) ({ \
83 int __ret_warn_on = !!(condition); \
84 if (__ret_warn_on) \
85 pr_warning("(%s) %s:%d: failed condition: %s", \
86 __FILE__, __func__, __LINE__, \
87 __stringify(condition)); \
88 __ret_warn_on; \
89 })
90
91 #define WARN_ONCE(condition, format, args...) ({ \
92 static int __warned; \
93 int __ret_warn_on = !!(condition); \
94 if (!__warned && __ret_warn_on) { \
95 __warned = 1; \
96 pr_warning(format, args); \
97 } \
98 __ret_warn_on; \
99 })
100
101 #define MSECS_TO_USECS(s) ((s) * 1000)
102
103 /* Millisecond sleep */
msleep(unsigned int msecs)104 static inline void msleep(unsigned int msecs)
105 {
106 usleep(MSECS_TO_USECS(msecs));
107 }
108
109 /*
110 * Find last (most significant) bit set. Same implementation as Linux:
111 * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64
112 */
fls_long(unsigned long x)113 static inline int fls_long(unsigned long x)
114 {
115 return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
116 }
117
roundup_pow_of_two(unsigned long x)118 static inline unsigned long roundup_pow_of_two(unsigned long x)
119 {
120 return x ? 1UL << fls_long(x - 1) : 0;
121 }
122
123 #define is_power_of_two(x) ((x) > 0 ? ((x) & ((x) - 1)) == 0 : 0)
124
125 /**
126 * pow2_size: return the number of bits needed to store values
127 * @x: number of distinct values to store (or number of bytes)
128 *
129 * Determines the number of bits needed to store @x different values.
130 * Could be used to determine the number of address bits needed to
131 * store @x bytes.
132 *
133 * Example:
134 * pow2_size(255) => 8
135 * pow2_size(256) => 8
136 * pow2_size(257) => 9
137 *
138 * Return: number of bits
139 */
pow2_size(unsigned long x)140 static inline int pow2_size(unsigned long x)
141 {
142 if (x <= 1)
143 return x;
144
145 return sizeof(x) * 8 - __builtin_clzl(x - 1);
146 }
147
148 struct kvm;
149 void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
150 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
151
152 #endif /* KVM__UTIL_H */
153