1 /*++ 2 3 Copyright (c) 1998 Intel Corporation 4 5 Module Name: 6 7 efefind.h 8 9 Abstract: 10 11 EFI to compile bindings 12 13 14 15 16 Revision History 17 18 --*/ 19 #ifndef X86_64_EFI_BIND 20 #define X86_64_EFI_BIND 21 #ifndef __GNUC__ 22 #pragma pack() 23 #endif 24 25 #if defined(GNU_EFI_USE_MS_ABI) 26 #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))||(defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2))) 27 #define HAVE_USE_MS_ABI 1 28 #elif defined(__clang__) 29 #define HAVE_USE_MS_ABI 1 30 #else 31 #error Compiler is too old for GNU_EFI_USE_MS_ABI 32 #endif 33 #endif 34 35 // 36 // Basic int types of various widths 37 // 38 39 #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) 40 41 // No ANSI C 1999/2000 stdint.h integer width declarations 42 43 #if defined(_MSC_EXTENSIONS) 44 45 // Use Microsoft C compiler integer width declarations 46 47 typedef unsigned __int64 uint64_t; 48 typedef __int64 int64_t; 49 typedef unsigned __int32 uint32_t; 50 typedef __int32 int32_t; 51 typedef unsigned short uint16_t; 52 typedef short int16_t; 53 typedef unsigned char uint8_t; 54 typedef char int8_t; 55 #elif defined(__GNUC__) 56 typedef int __attribute__((__mode__(__DI__))) int64_t; 57 typedef unsigned int __attribute__((__mode__(__DI__))) uint64_t; 58 typedef unsigned int uint32_t; 59 typedef int int32_t; 60 typedef unsigned short uint16_t; 61 typedef short int16_t; 62 typedef unsigned char uint8_t; 63 typedef signed char int8_t; 64 #elif defined(UNIX_LP64) 65 66 /* Use LP64 programming model from C_FLAGS for integer width declarations */ 67 68 typedef unsigned long uint64_t; 69 typedef long int64_t; 70 typedef unsigned int uint32_t; 71 typedef int int32_t; 72 typedef unsigned short uint16_t; 73 typedef short int16_t; 74 typedef unsigned char uint8_t; 75 typedef char int8_t; 76 #else 77 78 /* Assume P64 programming model from C_FLAGS for integer width declarations */ 79 80 typedef unsigned long long uint64_t __attribute__((aligned (8))); 81 typedef long long int64_t __attribute__((aligned (8))); 82 typedef unsigned int uint32_t; 83 typedef int int32_t; 84 typedef unsigned short uint16_t; 85 typedef short int16_t; 86 typedef unsigned char uint8_t; 87 typedef char int8_t; 88 #endif 89 #elif defined(__GNUC__) 90 #include <stdint.h> 91 #endif 92 93 // 94 // Basic EFI types of various widths 95 // 96 97 #ifndef __WCHAR_TYPE__ 98 # define __WCHAR_TYPE__ short 99 #endif 100 101 typedef uint64_t UINT64; 102 typedef int64_t INT64; 103 104 #ifndef _BASETSD_H_ 105 typedef uint32_t UINT32; 106 typedef int32_t INT32; 107 #endif 108 109 typedef uint16_t UINT16; 110 typedef int16_t INT16; 111 typedef uint8_t UINT8; 112 typedef int8_t INT8; 113 typedef __WCHAR_TYPE__ WCHAR; 114 115 #undef VOID 116 #define VOID void 117 118 119 typedef int64_t INTN; 120 typedef uint64_t UINTN; 121 122 #ifdef EFI_NT_EMULATOR 123 #define POST_CODE(_Data) 124 #else 125 #ifdef EFI_DEBUG 126 #define POST_CODE(_Data) __asm mov eax,(_Data) __asm out 0x80,al 127 #else 128 #define POST_CODE(_Data) 129 #endif 130 #endif 131 132 #define EFIERR(a) (0x8000000000000000 | a) 133 #define EFI_ERROR_MASK 0x8000000000000000 134 #define EFIERR_OEM(a) (0xc000000000000000 | a) 135 136 137 #define BAD_POINTER 0xFBFBFBFBFBFBFBFB 138 #define MAX_ADDRESS 0xFFFFFFFFFFFFFFFF 139 140 #ifdef EFI_NT_EMULATOR 141 #define BREAKPOINT() __asm { int 3 } 142 #else 143 #define BREAKPOINT() while (TRUE); // Make it hang on Bios[Dbg]32 144 #endif 145 146 // 147 // Pointers must be aligned to these address to function 148 // 149 150 #define MIN_ALIGNMENT_SIZE 4 151 152 #define ALIGN_VARIABLE(Value ,Adjustment) \ 153 (UINTN)Adjustment = 0; \ 154 if((UINTN)Value % MIN_ALIGNMENT_SIZE) \ 155 (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \ 156 Value = (UINTN)Value + (UINTN)Adjustment 157 158 159 // 160 // Define macros to build data structure signatures from characters. 161 // 162 163 #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8)) 164 #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16)) 165 #define EFI_SIGNATURE_64(A,B,C,D,E,F,G,H) (EFI_SIGNATURE_32(A,B,C,D) | ((UINT64)(EFI_SIGNATURE_32(E,F,G,H)) << 32)) 166 // 167 // To export & import functions in the EFI emulator environment 168 // 169 170 #ifdef EFI_NT_EMULATOR 171 #define EXPORTAPI __declspec( dllexport ) 172 #else 173 #define EXPORTAPI 174 #endif 175 176 177 // 178 // EFIAPI - prototype calling convention for EFI function pointers 179 // BOOTSERVICE - prototype for implementation of a boot service interface 180 // RUNTIMESERVICE - prototype for implementation of a runtime service interface 181 // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service 182 // RUNTIME_CODE - pragma macro for declaring runtime code 183 // 184 185 #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options 186 #ifdef _MSC_EXTENSIONS 187 #define EFIAPI __cdecl // Force C calling convention for Microsoft C compiler 188 #elif defined(HAVE_USE_MS_ABI) 189 // Force amd64/ms calling conventions. 190 #define EFIAPI __attribute__((ms_abi)) 191 #else 192 #define EFIAPI // Substitute expresion to force C calling convention 193 #endif 194 #endif 195 196 #define BOOTSERVICE 197 //#define RUNTIMESERVICE(proto,a) alloc_text("rtcode",a); proto a 198 //#define RUNTIMEFUNCTION(proto,a) alloc_text("rtcode",a); proto a 199 #define RUNTIMESERVICE 200 #define RUNTIMEFUNCTION 201 202 203 #define RUNTIME_CODE(a) alloc_text("rtcode", a) 204 #define BEGIN_RUNTIME_DATA() data_seg("rtdata") 205 #define END_RUNTIME_DATA() data_seg("") 206 207 #define VOLATILE volatile 208 209 #define MEMORY_FENCE() 210 211 #ifdef EFI_NT_EMULATOR 212 213 // 214 // To help ensure proper coding of integrated drivers, they are 215 // compiled as DLLs. In NT they require a dll init entry pointer. 216 // The macro puts a stub entry point into the DLL so it will load. 217 // 218 219 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 220 UINTN \ 221 __stdcall \ 222 _DllMainCRTStartup ( \ 223 UINTN Inst, \ 224 UINTN reason_for_call, \ 225 VOID *rserved \ 226 ) \ 227 { \ 228 return 1; \ 229 } \ 230 \ 231 int \ 232 EXPORTAPI \ 233 __cdecl \ 234 InitializeDriver ( \ 235 void *ImageHandle, \ 236 void *SystemTable \ 237 ) \ 238 { \ 239 return InitFunction(ImageHandle, SystemTable); \ 240 } 241 242 243 #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \ 244 (_if)->LoadInternal(type, name, NULL) 245 246 #else // EFI_NT_EMULATOR 247 248 // 249 // When build similiar to FW, then link everything together as 250 // one big module. For the MSVC toolchain, we simply tell the 251 // linker what our driver init function is using /ENTRY. 252 // 253 #if defined(_MSC_EXTENSIONS) 254 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 255 __pragma(comment(linker, "/ENTRY:" # InitFunction)) 256 #else 257 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 258 UINTN \ 259 InitializeDriver ( \ 260 VOID *ImageHandle, \ 261 VOID *SystemTable \ 262 ) \ 263 { \ 264 return InitFunction(ImageHandle, \ 265 SystemTable); \ 266 } \ 267 \ 268 EFI_STATUS efi_main( \ 269 EFI_HANDLE image, \ 270 EFI_SYSTEM_TABLE *systab \ 271 ) __attribute__((weak, \ 272 alias ("InitializeDriver"))); 273 #endif 274 275 #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \ 276 (_if)->LoadInternal(type, name, entry) 277 278 #endif // EFI_NT_EMULATOR 279 280 // 281 // Some compilers don't support the forward reference construct: 282 // typedef struct XXXXX 283 // 284 // The following macro provide a workaround for such cases. 285 // 286 #ifdef NO_INTERFACE_DECL 287 #define INTERFACE_DECL(x) 288 #else 289 #if defined(__GNUC__) || defined(_MSC_EXTENSIONS) 290 #define INTERFACE_DECL(x) struct x 291 #else 292 #define INTERFACE_DECL(x) typedef struct x 293 #endif 294 #endif 295 296 /* for x86_64, EFI_FUNCTION_WRAPPER must be defined */ 297 #if defined(HAVE_USE_MS_ABI) 298 #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__) 299 #else 300 /* 301 Credits for macro-magic: 302 https://groups.google.com/forum/?fromgroups#!topic/comp.std.c/d-6Mj5Lko_s 303 http://efesx.com/2010/08/31/overloading-macros/ 304 */ 305 #define __VA_NARG__(...) \ 306 __VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) 307 #define __VA_NARG_(...) \ 308 __VA_ARG_N(__VA_ARGS__) 309 #define __VA_ARG_N( \ 310 _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,N,...) N 311 #define __RSEQ_N() \ 312 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 313 314 #define __VA_ARG_NSUFFIX__(prefix,...) \ 315 __VA_ARG_NSUFFIX_N(prefix, __VA_NARG__(__VA_ARGS__)) 316 #define __VA_ARG_NSUFFIX_N(prefix,nargs) \ 317 __VA_ARG_NSUFFIX_N_(prefix, nargs) 318 #define __VA_ARG_NSUFFIX_N_(prefix,nargs) \ 319 prefix ## nargs 320 321 /* Prototypes of EFI cdecl -> stdcall trampolines */ 322 UINT64 efi_call0(void *func); 323 UINT64 efi_call1(void *func, UINT64 arg1); 324 UINT64 efi_call2(void *func, UINT64 arg1, UINT64 arg2); 325 UINT64 efi_call3(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3); 326 UINT64 efi_call4(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 327 UINT64 arg4); 328 UINT64 efi_call5(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 329 UINT64 arg4, UINT64 arg5); 330 UINT64 efi_call6(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 331 UINT64 arg4, UINT64 arg5, UINT64 arg6); 332 UINT64 efi_call7(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 333 UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7); 334 UINT64 efi_call8(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 335 UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7, 336 UINT64 arg8); 337 UINT64 efi_call9(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 338 UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7, 339 UINT64 arg8, UINT64 arg9); 340 UINT64 efi_call10(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3, 341 UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7, 342 UINT64 arg8, UINT64 arg9, UINT64 arg10); 343 344 /* Front-ends to efi_callX to avoid compiler warnings */ 345 #define _cast64_efi_call0(f) \ 346 efi_call0(f) 347 #define _cast64_efi_call1(f,a1) \ 348 efi_call1(f, (UINT64)(a1)) 349 #define _cast64_efi_call2(f,a1,a2) \ 350 efi_call2(f, (UINT64)(a1), (UINT64)(a2)) 351 #define _cast64_efi_call3(f,a1,a2,a3) \ 352 efi_call3(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3)) 353 #define _cast64_efi_call4(f,a1,a2,a3,a4) \ 354 efi_call4(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4)) 355 #define _cast64_efi_call5(f,a1,a2,a3,a4,a5) \ 356 efi_call5(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ 357 (UINT64)(a5)) 358 #define _cast64_efi_call6(f,a1,a2,a3,a4,a5,a6) \ 359 efi_call6(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ 360 (UINT64)(a5), (UINT64)(a6)) 361 #define _cast64_efi_call7(f,a1,a2,a3,a4,a5,a6,a7) \ 362 efi_call7(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ 363 (UINT64)(a5), (UINT64)(a6), (UINT64)(a7)) 364 #define _cast64_efi_call8(f,a1,a2,a3,a4,a5,a6,a7,a8) \ 365 efi_call8(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ 366 (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8)) 367 #define _cast64_efi_call9(f,a1,a2,a3,a4,a5,a6,a7,a8,a9) \ 368 efi_call9(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ 369 (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8), \ 370 (UINT64)(a9)) 371 #define _cast64_efi_call10(f,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) \ 372 efi_call10(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \ 373 (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8), \ 374 (UINT64)(a9), (UINT64)(a10)) 375 376 /* main wrapper (va_num ignored) */ 377 #define uefi_call_wrapper(func,va_num,...) \ 378 __VA_ARG_NSUFFIX__(_cast64_efi_call, __VA_ARGS__) (func , ##__VA_ARGS__) 379 380 #endif 381 382 #if defined(HAVE_USE_MS_ABI) && !defined(_MSC_EXTENSIONS) 383 #define EFI_FUNCTION __attribute__((ms_abi)) 384 #else 385 #define EFI_FUNCTION 386 #endif 387 388 #ifdef _MSC_EXTENSIONS 389 #pragma warning ( disable : 4731 ) // Suppress warnings about modification of EBP 390 #endif 391 392 #endif 393