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