github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/common/include/vmm_defs.h (about) 1 /* 2 * Copyright (c) 2013 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 #ifndef _VMM_DEFS_H_ 16 #define _VMM_DEFS_H_ 17 18 #include "common_types.h" 19 20 #define VMM_MAX_CPU_SUPPORTED MAX_CPUS 21 #define VMM_MAX_GUESTS_SUPPORTED 4 22 23 /* Flag to enable VTLB add on. Enable it in makefile by flag ENABLE_VTLB */ 24 //#define VTLB_IS_SUPPORTED 25 26 #ifdef VTLB_UNITTESTING 27 #pragma message("Using Windows environment basic types") 28 #include <windows.h> 29 30 #else // VTLB_UNITTESTING 31 32 #ifndef _DOS 33 #ifndef _EFI_TYPES_H_ 34 #ifndef _NTDEF_ 35 36 typedef signed char INT8; 37 typedef signed short INT16; 38 typedef signed int INT32; 39 typedef long long INT64; 40 41 #ifndef FALSE 42 #define FALSE 0 43 #endif 44 45 #ifndef TRUE 46 #define TRUE 1 47 #endif 48 49 typedef void VOID; 50 typedef INT32 BOOLEAN; 51 52 #endif // ifndef _NTDEF_ 53 #endif // ifndef _EFI_TYPES_H_ 54 #endif // ifndef _DOS 55 #endif // VTLB_UNITTESTING 56 57 #ifndef _DOS 58 #ifndef _EFI_TYPES_H_ 59 60 typedef unsigned char UINT8; 61 typedef unsigned short UINT16; 62 typedef unsigned int UINT32; 63 typedef unsigned long long UINT64; 64 typedef UINT8 CHAR8; 65 typedef UINT16 CHAR16; 66 67 #endif // ifndef _EFI_TYPES_H_ 68 69 typedef struct _UINT128 { 70 UINT64 uint64[2]; 71 } UINT128; 72 73 typedef UINT32 BOOL32; 74 75 #endif // ifndef _DOS 76 77 typedef enum { 78 VMM_ERROR = -1, 79 VMM_OK 80 } VMM_STATUS; 81 82 typedef enum { 83 NO_ACCESS = 0, 84 WRITE_ACCESS = 1, 85 READ_ACCESS = 2, 86 READ_WRITE_ACCESS = WRITE_ACCESS | READ_ACCESS, 87 } RW_ACCESS; 88 89 #ifdef EFI32 90 #define ARCH_ADDRESS_WIDTH 4 91 #endif 92 93 #ifndef ARCH_ADDRESS_WIDTH 94 #define ARCH_ADDRESS_WIDTH 8 95 #endif 96 97 #if 8 == ARCH_ADDRESS_WIDTH 98 typedef UINT64 ADDRESS; 99 #else 100 typedef UINT32 ADDRESS; 101 #endif 102 103 #ifndef NULL 104 #define NULL ((void*)0) 105 #endif 106 107 #ifndef VTLB_UNITTESTING 108 #ifndef _DOS 109 #ifndef _NTDEF_ 110 typedef ADDRESS size_t; 111 #endif 112 #endif 113 #endif 114 115 // convert 32bit pointer to UINT64 116 #define PTR32_TO_UINT64( p32 ) ((UINT64)(UINT32)(p32)) 117 118 #define VMM_GUID_DATA4_SIZE 8 119 typedef struct _VMM_GUID { 120 UINT32 data1; 121 UINT16 data2; 122 UINT16 data3; 123 UINT8 data4[VMM_GUID_DATA4_SIZE]; 124 } VMM_GUID; 125 126 typedef struct { 127 UINT16 year; 128 UINT8 month; 129 UINT8 day; 130 UINT8 hour; 131 UINT8 minute; 132 UINT8 second; 133 UINT8 pad1; 134 UINT32 nanosecond; 135 INT16 time_zone; 136 UINT8 daylight; 137 UINT8 pad2; 138 } VMM_TIME; 139 140 typedef UINT16 CPU_ID; 141 typedef UINT16 GUEST_ID; 142 typedef UINT8 VECTOR_ID; 143 typedef ADDRESS HVA; 144 typedef ADDRESS HPA; 145 typedef ADDRESS GVA; 146 typedef ADDRESS GPA; 147 typedef UINT32 MSR_ID; 148 typedef UINT16 IO_PORT_ID; 149 150 151 // compile time alignment 152 #ifdef __GNUC__ 153 #define ALIGN_N(__type, __var, __alignment) __type __attribute__((aligned(__alignment))) __var 154 #else 155 #define ALIGN_N(__type, __var, __alignment) __declspec(align(__alignment)) __type __var 156 #endif 157 158 #define ALIGN16(__type, __var) ALIGN_N(__type, __var, 16) 159 #define ALIGN8(__type, __var) ALIGN_N(__type, __var, 8) 160 161 #define UINT64_ALL_ONES ((UINT64)-1) 162 #define UINT32_ALL_ONES ((UINT32)-1) 163 #define UINT16_ALL_ONES ((UINT16)-1) 164 #define UINT8_ALL_ONES ((UINT8)-1) 165 #define SIZE_T_ALL_ONES ((size_t)-1) 166 #define INVALID_PHYSICAL_ADDRESS ((UINT64)-1) 167 168 #define NELEMENTS(__array) (INT32) (sizeof(__array) / sizeof(__array[0])) 169 #define OFFSET_OF(__struct, __member) ((size_t)&(((__struct *) 0)->__member)) 170 #define ALIGN_BACKWARD(__address, __bytes) ((ADDRESS)(__address) & ~((__bytes) - 1)) 171 #define ALIGN_FORWARD(__address, __bytes) ALIGN_BACKWARD(__address + __bytes - 1, __bytes) 172 #define ADDRESSES_ON_THE_SAME_PAGE(__addr1, __addr2) \ 173 (ALIGN_BACKWARD(__addr1, PAGE_4KB_SIZE) == ALIGN_BACKWARD(__addr2, PAGE_4KB_SIZE)) 174 #define IS_NEGATIVE(__n) (((int) (__n)) < 0) 175 #define SIGN_EXTENSION(__n) (IS_NEGATIVE(__n) ? (((UINT64) 0xffffffff) << 32) | __n : (UINT64) __n) 176 #define IS_POW_OF_2(__n) (((__n) & ((__n) - 1)) == 0) 177 #define IS_ALIGN(__address, __bytes) (ALIGN_BACKWARD( __address, __bytes ) == __address) 178 179 extern UINT32 align_forward_to_power_of_2(UINT64 number); 180 #define ALIGN_FORWARD_TO_POW_OF_2(__n) (align_forward_to_power_of_2(__n)) 181 182 #define BITMAP_SET(__word, __mask) ((__word) |= (__mask)) 183 #define BITMAP_CLR(__word, __mask) ((__word) &= ~(__mask)) 184 #define BITMAP_GET(__word, __mask) ((__word) & (__mask)) 185 #define BITMAP_ASSIGN(__word, __mask, __value) { \ 186 BITMAP_CLR(__word, __mask); \ 187 __word |= BITMAP_GET(__value, __mask); \ 188 } 189 190 #define BITMAP_SET64(__word, __mask) ((__word) |= (UINT64)(__mask)) 191 #define BITMAP_CLR64(__word, __mask) ((__word) &= ~(UINT64)(__mask)) 192 #define BITMAP_GET64(__word, __mask) ((__word) & (UINT64)(__mask)) 193 #define BITMAP_ASSIGN64(__word, __mask, __value) { \ 194 BITMAP_CLR64(__word, __mask); \ 195 __word |= BITMAP_GET64(__value, __mask); \ 196 } 197 198 #define BIT_VALUE(__bitno) (1 << (__bitno)) 199 #define BIT_SET(__word, __bitno) BITMAP_SET(__word, 1 << (__bitno)) 200 #define BIT_CLR(__word, __bitno) BITMAP_CLR(__word, 1 << (__bitno)) 201 #define BIT_GET(__word, __bitno) (((__word) >> (__bitno)) & 1) 202 203 #define BIT_VALUE64(__bitno) ((UINT64)1 << (__bitno)) 204 #define BIT_SET64(__word, __bitno) BITMAP_SET(__word, (UINT64)1 << (__bitno)) 205 #define BIT_CLR64(__word, __bitno) BITMAP_CLR(__word, (UINT64)1 << (__bitno)) 206 #define BIT_GET64(__word, __bitno) BIT_GET(__word, __bitno) 207 208 #define BITARRAY_SIZE_IN_BYTES( __size_in_bits ) (((__size_in_bits) + 7) / 8) 209 #define BITARRAY(__name, __size_in_bits) UINT8 __name[BITARRAY_SIZE_IN_BYTES(__size_in_bits)] 210 #define BITARRAY_BYTE(__bitno) ((__bitno) >> 3) 211 #define BITARRAY_MASK(__bitno) (1 << ((__bitno) & 7)) 212 #define BITARRAY_SET(__bitarray, __bitno) __bitarray[BITARRAY_BYTE(__bitno)] |= BITARRAY_MASK(__bitno) 213 #define BITARRAY_CLR(__bitarray, __bitno) __bitarray[BITARRAY_BYTE(__bitno)] &= ~ BITARRAY_MASK(__bitno) 214 #define BITARRAY_GET(__bitarray, __bitno) __bitarray[BITARRAY_BYTE(__bitno)] & BITARRAY_MASK(__bitno) 215 216 #define BITMAP_ARRAY64_BYTE(__bitno) ((__bitno) >> 6) 217 #define BITMAP_ARRAY64_CLR(__bitarray64, __bitno) BITMAP_CLR64(__bitarray64[BITMAP_ARRAY64_BYTE(__bitno)], (UINT64)1 << (__bitno % 64)) 218 #define BITMAP_ARRAY64_SET(__bitarray64, __bitno) BITMAP_SET64(__bitarray64[BITMAP_ARRAY64_BYTE(__bitno)], (UINT64)1 << (__bitno % 64)) 219 #define BITMAP_ARRAY64_GET(__bitarray64, __bitno) BITMAP_GET64(__bitarray64[BITMAP_ARRAY64_BYTE(__bitno)], (UINT64)1 << (__bitno % 64)) 220 221 /* 222 * PURPOSE : Set bit vectors for all the bits which are 223 * lower than threshold. Clear all upper 224 * bits as ZERO. 225 * ARGUMENTS: array_64 - an UINT64 array, pointer to 226 * array_64[0],array_64[1],... 227 * len - the length of array_64 228 * threshold - if bit vector position is lower 229 * than threshold, then set it; 230 * if bit vector position is higher 231 * than threshold, then clear it; 232 */ 233 #define BITMAP_ARRAY_ASSIGN(array_64, len, threshold) \ 234 { \ 235 UINT32 idx; \ 236 UINT32 quot = (threshold) / 64, \ 237 rem = (threshold) % 64; \ 238 \ 239 for (idx = 0; idx < (len); idx ++){ \ 240 \ 241 if(idx < quot){ \ 242 (array_64)[idx] = UINT64_ALL_ONES; \ 243 } \ 244 else if( idx > quot){ \ 245 (array_64)[idx] = 0; \ 246 } \ 247 else{ \ 248 (array_64)[idx] = (UINT64)((UINT64)1 << rem) - 1; \ 249 } \ 250 } \ 251 } 252 253 /* 254 * PURPOSE : Check if all bits of bitmap array are reset 255 * ARGUMENTS: array_64 - an UINT64 array, pointer to 256 * array_64[0],array_64[1],... 257 * len - the length of array_64 258 * ret - 1 if all bits are cleared, 259 * 0 otherwise 260 */ 261 #define BITMAP_ARRAY64_CHECKBITS_ALLZERO(array_64, len, ret) \ 262 { \ 263 UINT32 idx; \ 264 \ 265 (ret) = 1; \ 266 \ 267 for (idx = 0; idx < (len); idx ++){ \ 268 if((array_64)[idx] != 0) { \ 269 (ret) = 0; \ 270 break; \ 271 } \ 272 } \ 273 } 274 275 /* 276 * PURPOSE : Check if all bits of bitmap array are set 277 * ARGUMENTS: array_64 - an UINT64 array, pointer to 278 * array_64[0],array_64[1],... 279 * len - the length of array_64 280 * ret - 1 if all bits are set, 281 * 0 otherwise 282 */ 283 #define BITMAP_ARRAY64_CHECKBITS_ALLONE(array_64, len, ret) \ 284 { \ 285 UINT32 idx; \ 286 \ 287 (ret) = 1; \ 288 \ 289 for (idx = 0; idx < (len); idx ++){ \ 290 if((array_64)[idx] != UINT64_ALL_ONES) { \ 291 (ret) = 0; \ 292 break; \ 293 } \ 294 } \ 295 } 296 297 /* 298 * PURPOSE : Returns index of the highest bit set in the 299 * bitmap array 300 * ARGUMENTS: array_64 - an UINT64 array, pointer to 301 * array_64[0],array_64[1],... 302 * len - the length of array_64 303 * bit_index - index of the highest bit set, 304 * set to -1 if no bit is set in 305 * the bitmap array 306 */ 307 #define BITMAP_ARRAY64_HIGHESTINDEX(array_64, len, bit_index) \ 308 { \ 309 INT32 i; \ 310 UINT32 index; \ 311 \ 312 (bit_index) = -1; \ 313 \ 314 for(i = ((len) - 1); i >= 0; i--) { \ 315 if(hw_scan_bit_backward64((UINT32 *)&index, (array_64)[i])) { \ 316 (bit_index) = (index + (i * sizeof(UINT64) * 8)); \ 317 break; \ 318 } \ 319 } \ 320 } 321 322 /* 323 * enumerate all bits in BITARRAY 324 * 325 * Call user-given function for each bit set. 326 * User-given function receives bit_number and arbitrary data, passed to enumerator 327 */ 328 typedef void (*BITARRAY_ENUM_FUNC)( UINT32 bit_number, void* data_for_this_func ); 329 extern void bitarray_enumerate_bits( UINT8* bitarray, UINT32 bitarray_size_in_bits, 330 BITARRAY_ENUM_FUNC cb, void* cb_data ); 331 332 #define BITARRAY_ENUMERATE(__name, __size_in_bits, __cb_func, __data_for_cb ) \ 333 bitarray_enumerate_bits( __name, __size_in_bits, __cb_func, __data_for_cb ) 334 335 336 #define GET_BYTE(__word, __no) (((__word) >> ((__no) * 8)) & 0xFF) 337 #define GET_2BYTE(__word, __no)(((__word) >> ((__no) * 16)) & 0xFFFF) 338 #define GET_4BYTE(__word, __no)(((__word) >> ((__no) * 32)) & 0xFFFFFFFF) 339 340 #ifndef IN 341 #define IN 342 #endif 343 #ifndef OUT 344 #define OUT 345 #endif 346 #ifndef OPTIONAL 347 #define OPTIONAL 348 #endif 349 350 #define KILOBYTE *1024 351 #define KILOBYTES KILOBYTE 352 353 #define MEGABYTE *1024 KILOBYTES 354 #define MEGABYTES MEGABYTE 355 356 #define GIGABYTE *1024 MEGABYTES 357 #define GIGABYTES GIGABYTE 358 359 #ifndef MAX 360 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 361 #endif 362 363 #ifndef MIN 364 #define MIN(a, b) ((b) > (a) ? (a) : (b)) 365 #endif 366 367 /* 368 * Pages 369 */ 370 371 // The following definitions and macros will be used to perform alignment 372 373 #define PAGE_4KB_SIZE (4 KILOBYTES) 374 #define PAGE_4KB_MASK (PAGE_4KB_SIZE - 1) 375 #define PAGE_ALIGN_4K(x) ALIGN_FORWARD( x, PAGE_4KB_SIZE ) 376 // returns TRUE if the structure occupies the single page 377 #define IS_ON_THE_SAME_4K_PAGE( address, size ) \ 378 ALIGN_BACKWARD( address, PAGE_4KB_SIZE ) == ALIGN_BACKWARD( address + PAGE_4KB_SIZE ) 379 380 381 // Returns number of pages (4KB) required to accomdate x bytes 382 #define PAGE_ROUNDUP(x) (PAGE_ALIGN_4K(x) / PAGE_4KB_SIZE) 383 384 #define PAGE_2MB_SIZE (2 MEGABYTES) 385 #define PAGE_2MB_MASK (PAGE_2MB_SIZE - 1) 386 #define PAGE_ALIGN_2M(x) ALIGN_FORWARD( x, PAGE_2MB_SIZE ) 387 388 // returns TRUE if the structure occupies the single page 389 #define IS_ON_THE_SAME_2M_PAGE( address, size ) \ 390 ALIGN_BACKWARD( address, PAGE_2MB_SIZE ) == ALIGN_BACKWARD( address + PAGE_2MB_SIZE ) 391 392 #define PAGE_4MB_SIZE (4 MEGABYTES) 393 #define PAGE_4MB_MASK (PAGE_4MB_SIZE - 1) 394 #define PAGE_ALIGN_4M(x) ALIGN_FORWARD( x, PAGE_4MB_SIZE ) 395 396 // returns TRUE if the structure occupies the single page 397 #define IS_ON_THE_SAME_4M_PAGE( address, size ) \ 398 ALIGN_BACKWARD( address, PAGE_4MB_SIZE ) == ALIGN_BACKWARD( address + PAGE_4MB_SIZE ) 399 400 #define PAGE_1GB_SIZE (1 GIGABYTES) 401 #define PAGE_1GB_MASK (PAGE_1GB_SIZE - 1) 402 #define PAGE_ALIGN_1G(x) ALIGN_FORWARD( x, PAGE_1GB_SIZE ) 403 404 405 #define ADDRESS_TO_FN(addr) ((ADDRESS)(addr) >> 12) 406 #define FN_TO_ADDRESS(fn) ((ADDRESS)(fn) << 12) 407 408 #define MEMORY_REGIONS_NON_OVERLAPPED(__addr1, __size1, __addr2, __size2)\ 409 (((__addr1) + (__size1)) < (__addr2) || ((__addr2) + (__size2)) < (__addr1)) 410 411 #define MEMORY_REGIONS_OVERLAPPED(__addr1, __size1, __addr2, __size2)\ 412 (! MEMORY_REGIONS_NON_OVERLAPPED(__addr1, __size1, __addr2, __size2)) 413 414 415 /* 416 * Calling conventions 417 */ 418 419 #ifdef __GNUC__ 420 421 #define API_FUNCTION 422 #define ASM_FUNCTION 423 #define CDECL 424 #define STDCALL 425 426 #define PACKED __attribute ((packed)) 427 #define PACK_ON 428 #define PACK_OFF 429 #define UNUSED __attribute__((unused)) 430 431 #else // MS Compiler 432 433 #define API_FUNCTION __stdcall 434 #define ASM_FUNCTION __stdcall 435 #define STDCALL __stdcall 436 437 #ifndef UNITTESTING 438 #define CDECL __cdecl 439 #endif 440 441 #define PACKED 442 #define PACK_ON pack(1) 443 #define PACK_OFF pack() 444 #define UNUSED 445 446 #endif // ! __GNUC__ 447 448 449 #ifdef DEBUG 450 #define USED_IN_DEBUG_ONLY 451 #else 452 #define USED_IN_DEBUG_ONLY UNUSED 453 #endif 454 455 456 #ifdef DEBUG 457 458 #define INLINE static 459 #define FORCE_INLINE(__x) static __x 460 461 #else 462 463 #ifdef __GNUC__ 464 #define INLINE static inline 465 #define FORCE_INLINE(__x) inline __x __attribute__((always_inline)) 466 #else 467 #define INLINE static __inline 468 #define FORCE_INLINE(__x) __forceinline __x 469 #endif 470 471 //#define FORCE_INLINE __forceinline 472 473 #endif // ! DEBUG 474 475 476 477 478 479 #ifndef ARRAY_SIZE 480 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) 481 #endif 482 483 /* 484 * varags 485 */ 486 487 // find size of parameter aligned on the native integer size 488 #define _VMM_INT_SIZE_OF(n) ( (sizeof(n) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1) ) 489 490 #ifndef va_start 491 492 #ifdef __GNUC__ 493 #define va_list __builtin_va_list 494 #define va_start(ap,v) __builtin_va_start((ap),v) 495 #define va_arg(ap,t) __builtin_va_arg(ap,t) 496 #define va_end __builtin_va_end 497 #else 498 typedef char* va_list; 499 #define va_start(ap,v) ( ap = (va_list)&(v) + _VMM_INT_SIZE_OF(v) ) 500 #define va_arg(ap,t) ( *(t *)((ap += _VMM_INT_SIZE_OF(t)) - _VMM_INT_SIZE_OF(t)) ) 501 #define va_end(ap) ( ap = (va_list)0 ) 502 #endif 503 504 #endif 505 506 #define VMM_UP_BREAKPOINT() \ 507 { \ 508 volatile int __never_change = 1; \ 509 while (__never_change); \ 510 } 511 512 513 #endif // _VMM_DEFS_H_ 514