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