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