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