Bitdefender Hypervisor Memory Introspection
introdefs.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _INTRODEFS_H_
6 #define _INTRODEFS_H_
7 
8 // Add the Doxygen groups here as it seems the best place to do so:
9 
14 
20 
25 
26 #include "introtypes.h"
27 #include "intrinsics.h"
28 
29 #define UNREFERENCED_PARAMETER(P) ((void)(P))
30 #define UNREFERENCED_LOCAL_VARIABLE(V) ((void)(V))
31 
32 #ifndef __FILENAME__
33 # define __FILENAME__ __FILE__
34 #endif
35 
36 
37 #ifndef _MM_HINT_T0
38 # define _MM_HINT_T0 1
39 # define _MM_HINT_T1 2
40 # define _MM_HINT_T2 3
41 # define _MM_HINT_NTA 0
42 #endif
43 
44 
45 #if defined(INT_COMPILER_MSVC)
46 # define STATIC_ASSERT static_assert
47 #elif defined(INT_COMPILER_GNUC)
48 # define STATIC_ASSERT _Static_assert
49 #else
50 # define STATIC_ASSERT(Cond, Msg)
51 #endif
52 
53 #ifdef DEBUG
54 # define ASSERT(x) \
55  do { \
56  if (!(x)) { \
57  LOG("[ASSERT] Assertion failure!\n"); \
58  IntBugCheck(); \
59  } \
60  } while (0)
61 #else
62 # define ASSERT(x)
63 #endif
64 
65 #if defined(INT_COMPILER_GNUC) || defined(INT_COMPILER_CLANG)
66 # define PRAGMA(x) _Pragma(#x)
67 
68 # define WARNING_DISABLE(W) PRAGMA(GCC diagnostic push); PRAGMA(GCC diagnostic ignored W)
69 
70 # define WARNING_RESTORE() PRAGMA(GCC diagnostic pop)
71 
72 #elif defined(INT_COMPILER_MSVC)
73 
74 # define WARNING_DISABLE(W) \
75  __pragma(warning(push)); \
76  __pragma(warning(disable: ##W))
77 
78 # define WARNING_RESTORE() __pragma(warning(pop))
79 #endif
80 
81 
82 // Annotation for functions that can be used on the timer (basically anything that doesn't pause the VCPUs)
83 #define TIMER_FRIENDLY
84 
85 #ifndef __FILE_TAG__
86 # define __FILE_TAG__ 0xffffeeee
87 #endif
88 
89 #define ONE_KILOBYTE 1024ULL
90 #define ONE_MEGABYTE (ONE_KILOBYTE * ONE_KILOBYTE)
91 #define ONE_GIGABYTE (ONE_KILOBYTE * ONE_MEGABYTE)
92 
93 #define NSEC_PER_SEC (1000ULL * 1000ULL * 1000ULL)
94 #define USEC_PER_SEC (1000ULL * 1000ULL)
95 #define NSEC_PER_USEC 1000ULL
96 
97 #define NSEC_TO_SEC(nsec) ((nsec) / NSEC_PER_SEC)
98 #define NSEC_TO_USEC(nsec) ((nsec) / NSEC_PER_USEC)
99 
100 #ifndef ARRAYSIZE
101 # define ARRAYSIZE(A) (sizeof(A) / sizeof((A)[0]))
102 #endif
103 
104 #define CWSTRLEN(Wstring) ((sizeof(Wstring) - sizeof(WCHAR)) / sizeof(WCHAR))
105 #define CSTRLEN(String) (sizeof(String) - sizeof(char))
106 
107 // Helps to concatenate stuff
108 #define _PASTE(a,b) a##b
109 #define PASTE(a,b) _PASTE(a,b)
110 
111 
112 #ifdef INT_COMPILER_MSVC
113 # define MIN(a, b) ((a) < (b) ? (a) : (b))
114 # define MAX(a, b) ((a) > (b) ? (a) : (b))
115 
116 # define __ROUND_MASK(what, to) (((to)-1))
117 
118 # define ROUND_UP(what, to) ((((what) - 1) | __ROUND_MASK(what, to)) + 1)
119 # define ROUND_DOWN(what, to) ((what) & ~__ROUND_MASK(what, to))
120 
121 # define ALIGN_UP(a, b) (((a) % (b) == 0) ? (a) : (((a) + ((b) - 1)) & (-(INT64)(b))))
122 # define ALIGN_DOWN(a, b) (((a) % (b) == 0) ? (a) : ((a) - ((a) % (b))))
123 # define IN_RANGE(x, start, end) (((x) >= (start)) && ((x) < (end)))
124 # define IN_RANGE_INCLUSIVE(x, start, end) (((x) >= (start)) && ((x) <= (end)))
125 # define IN_RANGE_LEN(x, start, len) (((x) >= (start)) && ((x) < ((start) + (len))))
126 # define IN_RANGE_LEN_INCLUSIVE(x, start, len) (((x) >= (start)) && ((x) <= ((start) + (len))))
127 
128 
129 # define RE32(x) ((((x) & 0xFF) << 24) | \
130  (((x) & 0xFF00) << 8) | \
131  (((x) & 0xFF0000) >> 8) | \
132  (((x) & 0xFF000000) >> 24))
133 
134 
135 # define SIGN_EX_8(x) ((x) & 0x00000080 ? 0xFFFFFFFFFFFFFF00 | (x) : (x))
136 # define SIGN_EX_16(x) ((x) & 0x00008000 ? 0xFFFFFFFFFFFF0000 | (x) : (x))
137 # define SIGN_EX_32(x) ((x) & 0x80000000 ? 0xFFFFFFFF00000000 | (x) : (x))
138 # define SIGN_EX(sz, x) ((sz) == 1 ? SIGN_EX_8(x) : (sz) == 2 ? SIGN_EX_16(x) : (sz) == 4 ? SIGN_EX_32(x) : (x))
139 
140 # define __unreachable
141 # define __likely(x) (x)
142 # define __unlikely(x) (x)
143 
144 #else
145 
146 # define MIN(a, b) \
147  ({ __auto_type a_min_ = (a); \
148  __auto_type b_min_ = (b); \
149  (a_min_ < b_min_) ? a_min_ : b_min_; })
150 
151 # define MAX(a, b) \
152  ({ __auto_type a_max_ = (a); \
153  __auto_type b_max_ = (b); \
154  (a_max_ > b_max_) ? a_max_ : b_max_; })
155 
156 # define __ROUND_MASK(what, to) ((typeof(what))((to)-1))
157 
158 # define ROUND_UP(what, to) ((((what) - 1) | __ROUND_MASK(what, to)) + 1)
159 # define ROUND_DOWN(what, to) ((what) & ~__ROUND_MASK(what, to))
160 
161 # define __ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
162 # define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
163 
164 # define ALIGN_UP(x, a) __ALIGN((x), (a))
165 # define ALIGN_DOWN(x, a) __ALIGN((x) - ((a) - 1), (a))
166 
167 # define IN_RANGE(x, start, end) \
168  ({ __auto_type x_ir_ = (x); \
169  ((x_ir_ >= (start)) && (x_ir_ < (end))); })
170 
171 # define IN_RANGE_INCLUSIVE(x, start, end) \
172  ({ __auto_type x_ir_ = (x); \
173  ((x_ir_ >= (start)) && (x_ir_ <= (end))); })
174 
175 # define IN_RANGE_LEN(x, start, len) \
176  ({ __auto_type x_ir_ = (x); \
177  __auto_type start_ir_ = (start); \
178  __auto_type len_ir_ = (len); \
179  ((x_ir_ >= start_ir_) && (x_ir_ < start_ir_ + len_ir_)); })
180 
181 # define IN_RANGE_LEN_INCLUSIVE(x, start, len) \
182  ({ __auto_type x_ir_ = (x); \
183  __auto_type start_ir_ = (start); \
184  __auto_type len_ir_ = (len); \
185  ((x_ir_ >= start_ir_) && (x_ir_ <= start_ir_ + len_ir_)); })
186 
187 # define RE32(x) \
188  ({ __auto_type x_re32_ = (x); \
189  (((x_re32_ & 0xFF) << 24) | \
190  ((x_re32_ & 0xFF00) << 8) | \
191  ((x_re32_ & 0xFF0000) >> 8) | \
192  ((x_re32_ & 0xFF000000) >> 24)) })
193 
194 # define SIGN_EX_8(x) \
195  ({ __auto_type x_se8_ = (x); \
196  ((x_se8_ & 0x00000080) ? (0xFFFFFFFFFFFFFF00 | x_se8_) : x_se8_); })
197 
198 # define SIGN_EX_16(x) \
199  ({ __auto_type x_se16_ = (x); \
200  ((x_se16_ & 0x00008000) ? (0xFFFFFFFFFFFF0000 | x_se16_) : x_se16_); })
201 
202 # define SIGN_EX_32(x) \
203  ({ __auto_type x_se32_ = (x); \
204  ((x_se32_ & 0x80000000) ? (0xFFFFFFFF00000000 | x_se32_) : x_se32_); })
205 
206 # define SIGN_EX(sz, x) \
207  ({ __auto_type x_se_ = (x); \
208  ((sz) == 1 ? SIGN_EX_8(x_se_) : (sz) == 2 ? SIGN_EX_16(x_se_) : (sz) == 4 ? SIGN_EX_32(x_se_) : x_se_); })
209 
210 # define __unreachable __builtin_unreachable()
211 # define __likely(x) __builtin_expect(!!(x), 1)
212 # define __unlikely(x) __builtin_expect(!!(x), 0)
213 
214 #endif // INT_COMPILER_MSVC
215 
216 #ifndef BIT
217 # define BIT(x) (1ULL << (x))
218 #endif // BIT
219 
220 
221 #define INITIAL_CRC_VALUE 0xFFFFFFFF
222 
223 // Will bug check if the condition is met
224 #define BUG_ON_ALWAYS(cond) \
225  do { \
226  if (__unlikely((cond))) { \
227  IntBugCheck(); \
228  } \
229  } while (0)
230 
231 #ifdef DEBUG
232 // Will bug check if the condition is met
233 #define BUG_ON(cond) BUG_ON_ALWAYS(cond)
234 #else
235 // Does nothing on release, use BUG_ON_ALWAYS if you want to bug check on release as well
236 #define BUG_ON(cond)
237 #endif // DEBUG
238 
239 #define FIELD_OFFSET(type, field) ((SIZE_T)(&((type *)0)->field))
240 
241 #endif // _INTRODEFS_H_