2 ; IPRT - Global YASM/NASM macros
6 ; Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 ; This file is part of VirtualBox base platform packages, as
9 ; available from https://www.virtualbox.org.
11 ; This program is free software; you can redistribute it and/or
12 ; modify it under the terms of the GNU General Public License
13 ; as published by the Free Software Foundation, in version 3 of the
16 ; This program is distributed in the hope that it will be useful, but
17 ; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ; General Public License for more details.
21 ; You should have received a copy of the GNU General Public License
22 ; along with this program; if not, see <https://www.gnu.org/licenses>.
24 ; The contents of this file may alternatively be used under the terms
25 ; of the Common Development and Distribution License Version 1.0
26 ; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 ; in the VirtualBox distribution, in which case the provisions of the
28 ; CDDL are applicable instead of those of the GPL.
30 ; You may elect to license modified versions of this file under the
31 ; terms and conditions of either the GPL or the CDDL or both.
33 ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
36 ; Special hack for bs3kit.
37 %ifdef RT_ASMDEFS_INC_FIRST_FILE
38 %include "asmdefs-first.mac" ; edk2-ignore-hack
41 %ifndef ___iprt_asmdefs_mac
42 %define ___iprt_asmdefs_mac
45 ;; @defgroup grp_rt_cdefs_size Size Constants
46 ; (Of course, these are binary computer terms, not SI.)
48 ;; 1 K (Kilo) (1 024).
49 %define _1K 000000400h
50 ;; 4 K (Kilo) (4 096).
51 %define _4K 000001000h
52 ;; 8 K (Kilo) (8 192).
53 %define _8K 000002000h
54 ;; 16 K (Kilo) (16 384).
55 %define _16K 000004000h
56 ;; 32 K (Kilo) (32 768).
57 %define _32K 000008000h
58 ;; 64 K (Kilo) (65 536).
59 %define _64K 000010000h
60 ;; 128 K (Kilo) (131 072).
61 %define _128K 000020000h
62 ;; 256 K (Kilo) (262 144).
63 %define _256K 000040000h
64 ;; 512 K (Kilo) (524 288).
65 %define _512K 000080000h
66 ;; 1 M (Mega) (1 048 576).
67 %define _1M 000100000h
68 ;; 2 M (Mega) (2 097 152).
69 %define _2M 000200000h
70 ;; 4 M (Mega) (4 194 304).
71 %define _4M 000400000h
72 ;; 1 G (Giga) (1 073 741 824).
73 %define _1G 040000000h
74 ;; 2 G (Giga) (2 147 483 648).
75 %define _2G 00000000080000000h
76 ;; 4 G (Giga) (4 294 967 296).
77 %define _4G 00000000100000000h
78 ;; 1 T (Tera) (1 099 511 627 776).
79 %define _1T 00000010000000000h
80 ;; 1 P (Peta) (1 125 899 906 842 624).
81 %define _1P 00004000000000000h
82 ;; 1 E (Exa) (1 152 921 504 606 846 976).
83 %define _1E 01000000000000000h
84 ;; 2 E (Exa) (2 305 843 009 213 693 952).
85 %define _2E 02000000000000000h
90 ; Define RT_STRICT for debug builds like iprt/cdefs.h does.
101 ; Make the mask for the given bit.
102 %define RT_BIT(bit) (1 << bit)
105 ; Make the mask for the given bit.
106 %define RT_BIT_32(bit) (1 << bit)
108 ; Make the mask for the given bit.
109 %define RT_BIT_64(bit) (1 << bit)
112 ; Makes a 32-bit unsigned (not type safe, but whatever) out of four byte values.
113 %define RT_MAKE_U32_FROM_U8(b0, b1, b2, b3) ( (b3 << 24) | (b2 << 16) | (b1 << 8) | b0 )
115 ;; Preprocessor concatenation macro.
116 %define RT_CONCAT(a_1,a_2) a_1 %+ a_2
118 ;; Preprocessor concatenation macro, three arguments.
119 %define RT_CONCAT3(a_1,a_2,a_3) a_1 %+ a_2 %+ a_3
121 ;; Preprocessor concatenation macro, four arguments.
122 %define RT_CONCAT4(a_1,a_2,a_3,a_4) a_1 %+ a_2 %+ a_3 %+ a_4
125 ; Trick for using RT_CONCAT and the like on %define names.
126 ; @param 1 The name (expression.
127 ; @param 2 The value.
128 %macro RT_DEFINE_EX 2
134 ; Trick for using RT_CONCAT and the like on %xdefine names.
135 ; @param 1 The name (expression.
136 ; @param 2 The value.
137 %macro RT_XDEFINE_EX 2
142 ; Trick for using RT_CONCAT and the like on %undef names.
143 ; @param 1 The name (expression.
153 ;; Define ASM_FORMAT_PE64 if applicable.
156 %define ASM_FORMAT_PE64 1
162 %ifdef RT_ASM_WITH_SEH64
163 %ifndef ASM_FORMAT_PE64
164 %undef RT_ASM_WITH_SEH64
168 %ifdef RT_ASM_WITH_SEH64_ALT
169 %ifdef ASM_FORMAT_PE64
170 ;; @name Register numbers. Used with RT_CONCAT to convert macro inputs to numbers.
172 %define SEH64_PE_GREG_rax 0
173 %define SEH64_PE_GREG_xAX 0
174 %define SEH64_PE_GREG_rcx 1
175 %define SEH64_PE_GREG_xCX 1
176 %define SEH64_PE_GREG_rdx 2
177 %define SEH64_PE_GREG_xDX 2
178 %define SEH64_PE_GREG_rbx 3
179 %define SEH64_PE_GREG_xBX 3
180 %define SEH64_PE_GREG_rsp 4
181 %define SEH64_PE_GREG_xSP 4
182 %define SEH64_PE_GREG_rbp 5
183 %define SEH64_PE_GREG_xBP 5
184 %define SEH64_PE_GREG_rsi 6
185 %define SEH64_PE_GREG_xSI 6
186 %define SEH64_PE_GREG_rdi 7
187 %define SEH64_PE_GREG_xDI 7
188 %define SEH64_PE_GREG_r8 8
189 %define SEH64_PE_GREG_r9 9
190 %define SEH64_PE_GREG_r10 10
191 %define SEH64_PE_GREG_r11 11
192 %define SEH64_PE_GREG_r12 12
193 %define SEH64_PE_GREG_r13 13
194 %define SEH64_PE_GREG_r14 14
195 %define SEH64_PE_GREG_r15 15
198 ;; @name PE unwind operations.
200 %define SEH64_PE_PUSH_NONVOL 0
201 %define SEH64_PE_ALLOC_LARGE 1
202 %define SEH64_PE_ALLOC_SMALL 2
203 %define SEH64_PE_SET_FPREG 3
204 %define SEH64_PE_SAVE_NONVOL 4
205 %define SEH64_PE_SAVE_NONVOL_FAR 5
206 %define SEH64_PE_SAVE_XMM128 8
207 %define SEH64_PE_SAVE_XMM128_FAR 9
211 ; Starts the unwind info for the manual SEH64 info generation.
212 ; @param 1 Function name.
213 %macro SEH64_ALT_START_UNWIND_INFO 1
214 %assign seh64_idxOps 0
215 %assign seh64_FrameReg SEH64_PE_GREG_rsp
216 %assign seh64_offFrame 0
217 %define asm_seh64_proc %1
218 %undef seh64_slot_bytes
221 ;; We keep the unwind bytes in the seh64_slot_bytes (x)define, in reverse order as per spec.
222 %macro SEH64_APPEND_SLOT_PAIR 2
223 %ifdef seh64_slot_bytes
224 %xdefine seh64_slot_bytes %1, %2, seh64_slot_bytes
226 %xdefine seh64_slot_bytes %1, %2
230 ;; For multi-slot unwind info.
231 %macro SEH64_APPEND_SLOT_BYTES 2+
234 %ifdef seh64_slot_bytes
235 %xdefine seh64_slot_bytes %1, seh64_slot_bytes
237 %xdefine seh64_slot_bytes %1
243 %undef RT_ASM_WITH_SEH64_ALT
248 ; Records a xBP push.
249 %macro SEH64_PUSH_xBP 0
250 %ifdef RT_ASM_WITH_SEH64
253 %elifdef RT_ASM_WITH_SEH64_ALT
254 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
255 %ifdef ASM_FORMAT_PE64
256 SEH64_APPEND_SLOT_PAIR RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
257 SEH64_PE_PUSH_NONVOL | (SEH64_PE_GREG_rbp << 4)
259 %assign seh64_idxOps seh64_idxOps + 1
264 ; Records a general register push.
265 ; @param 1 Register name.
266 %macro SEH64_PUSH_GREG 1
267 %ifdef RT_ASM_WITH_SEH64
270 %elifdef RT_ASM_WITH_SEH64_ALT
271 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
272 %ifdef ASM_FORMAT_PE64
273 SEH64_APPEND_SLOT_PAIR RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
274 SEH64_PE_PUSH_NONVOL | (RT_CONCAT(SEH64_PE_GREG_,%1) << 4)
276 %assign seh64_idxOps seh64_idxOps + 1
281 ; Sets xBP as frame pointer that's pointing to a stack position %1 relative to xBP.
282 %macro SEH64_SET_FRAME_xBP 1
283 %ifdef RT_ASM_WITH_SEH64
286 %elifdef RT_ASM_WITH_SEH64_ALT
287 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
288 %ifdef ASM_FORMAT_PE64
289 SEH64_APPEND_SLOT_PAIR RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
290 SEH64_PE_SET_FPREG | 0 ; vs2019 seems to put the offset in the info field
291 %assign seh64_FrameReg SEH64_PE_GREG_rbp
292 %assign seh64_offFrame %1
294 %assign seh64_idxOps seh64_idxOps + 1
299 ; Records an ADD xSP, %1.
300 %macro SEH64_ALLOCATE_STACK 1
301 %ifdef RT_ASM_WITH_SEH64
304 %elifdef RT_ASM_WITH_SEH64_ALT
305 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
306 %ifdef ASM_FORMAT_PE64
308 %error "SEH64_ALLOCATE_STACK must be a multiple of 8"
311 %error "SEH64_ALLOCATE_STACK must have an argument that's 8 or higher."
313 SEH64_APPEND_SLOT_PAIR RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
314 SEH64_PE_ALLOC_SMALL | ((((%1) / 8) - 1) << 4)
316 SEH64_APPEND_SLOT_BYTES RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
317 SEH64_PE_ALLOC_LARGE | 0, \
318 ((%1) / 8) & 0xff, ((%1) / 8) >> 8
320 SEH64_APPEND_SLOT_BYTES RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
321 SEH64_PE_ALLOC_LARGE | 1, \
322 (%1) & 0xff, ((%1) >> 8) & 0xff, ((%1) >> 16) & 0xff, ((%1) >> 24) & 0xff
325 %assign seh64_idxOps seh64_idxOps + 1
329 %macro SEH64_INFO_HELPER 1
337 %macro SEH64_END_PROLOGUE 0
339 %ifdef RT_ASM_WITH_SEH64
342 %elifdef RT_ASM_WITH_SEH64_ALT
344 ; Emit the unwind info now.
345 %ifndef ASM_DEFINED_XDATA_SECTION
346 %define ASM_DEFINED_XDATA_SECTION
347 section .xdata rdata align=4
353 db 1 ; version 1 (3 bit), no flags (5 bits)
354 db .end_of_prologue - .start_of_prologue
356 db (.unwind_info_array_end - .unwind_info_array) / 2
357 db seh64_FrameReg | (seh64_offFrame & 0xf0) ; framereg and offset/16.
359 %ifdef seh64_slot_bytes
361 %undef seh64_slot_bytes
363 .unwind_info_array_end:
372 ; Macro for generating the endbr32/64 instruction when
373 ; RT_WITH_IBT_BRANCH_PROTECTION is defined.
375 %ifdef RT_WITH_IBT_BRANCH_PROTECTION
377 db 0xf3, 0x0f, 0x1e, 0xfa ; yasm doesn't know about endbr64
379 db 0xf3, 0x0f, 0x1e, 0xfb ; yasm doesn't know about endbr32
387 ; Macro for generating the endbr32/64 instruction when
388 ; RT_WITH_IBT_BRANCH_PROTECTION_WITHOUT_NOTRACK is defined.
389 %macro IBT_ENDBRxx_WITHOUT_NOTRACK 0
390 %ifdef RT_WITH_IBT_BRANCH_PROTECTION_WITHOUT_NOTRACK
392 db 0xf3, 0x0f, 0x1e, 0xfa ; yasm doesn't know about endbr64
394 db 0xf3, 0x0f, 0x1e, 0xfb ; yasm doesn't know about endbr32
402 ; Macro for generating a NOTRACK prefix to an indirect jmp or call
403 ; instruction when RT_WITH_IBT_BRANCH_PROTECTION is defined.
405 ; @note Carful if mixing with other segment prefixes (should work, but needs
408 %ifdef RT_WITH_IBT_BRANCH_PROTECTION
409 %ifndef RT_WITH_IBT_BRANCH_PROTECTION_WITHOUT_NOTRACK
417 ; Align code, pad with INT3.
418 %define ALIGNCODE(alignment) align alignment, db 0cch
421 ; Align data, pad with ZEROs.
422 %define ALIGNDATA(alignment) align alignment, db 0
425 ; Align BSS, pad with ZEROs.
426 %define ALIGNBSS(alignment) align alignment, resb 1
429 ; NAME_OVERLOAD can be defined by a .asm module to modify all the
430 ; names created using the name macros in this files.
431 ; This is handy when you've got some kind of template code.
432 %ifndef NAME_OVERLOAD
433 %ifdef RT_MANGLER_PREFIX
434 %define NAME_OVERLOAD(name) RT_MANGLER_PREFIX %+ name
436 %define NAME_OVERLOAD(name) name
441 ; Mangles the given name so it can be referenced using DECLASM() in the
443 %ifndef ASM_FORMAT_BIN
446 %define NAME(name) _ %+ NAME_OVERLOAD(name)
449 %define NAME(name) _ %+ NAME_OVERLOAD(name)
453 %define NAME(name) _ %+ NAME_OVERLOAD(name)
457 %define NAME(name) NAME_OVERLOAD(name)
461 ; Mangles the given C name so it will _import_ the right symbol.
463 %define IMPNAME(name) __imp_ %+ NAME(name)
465 %define IMPNAME(name) NAME(name)
469 ; Gets the pointer to an imported object.
472 %define IMP(name) qword [IMPNAME(name) wrt rip]
474 %define IMP(name) dword [IMPNAME(name)]
477 %define IMP(name) IMPNAME(name)
481 ; Gets the pointer to an imported object.
484 %define IMP_SEG(SegOverride, name) qword [SegOverride:IMPNAME(name) wrt rip]
486 %define IMP_SEG(SegOverride, name) dword [SegOverride:IMPNAME(name)]
489 %define IMP_SEG(SegOverride, name) IMPNAME(name)
493 ; Declares an imported object for use with IMP2.
494 ; @note May change the current section!
498 %ifdef ASM_FORMAT_MACHO
499 g_Imp2_ %+ %1: RTCCPTR_DEF IMPNAME(%1)
504 ; Gets the pointer to an imported object, version 2.
507 %define IMP2(name) qword [IMPNAME(name) wrt rip]
509 %define IMP2(name) dword [IMPNAME(name)]
511 %elifdef ASM_FORMAT_ELF
514 %define IMP2(name) qword [rel IMPNAME(name) wrt ..got]
516 %define IMP2(name) IMPNAME(name) wrt ..plt
519 %elifdef ASM_FORMAT_MACHO
520 %define IMP2(name) RTCCPTR_PRE [g_Imp2_ %+ name xWrtRIP]
523 %define IMP2(name) IMPNAME(name)
528 ; Define a label as given, with a '$' prepended to permit using instruction
529 ; names like fdiv as labels.
535 ; Global marker which is DECLASM() compatible.
537 %ifndef ASM_FORMAT_BIN
544 ; Global exported marker which is DECLASM() compatible.
545 %macro EXPORTEDNAME 1
550 %ifdef ASM_FORMAT_OMF
551 export NAME(%1) NAME(%1)
558 ; Same as GLOBALNAME_EX, but without the name mangling.
560 ; @param %1 The symbol name - subjected to NAME().
561 ; @param %2 ELF and PE attributes: 'function', 'object', 'data', 'notype'.
562 ; PE ignores all but 'function' (yasm only). Other formats ignores
564 ; @param %3 Symbol visibility: 'hidden', 'protected', 'internal', and
565 ; RT_NOTHING (for 'default' visibility).
566 ; These are ELF attributes, but 'hidden' is translated to
567 ; 'private_extern' for the Macho-O format.
568 ; Ignored by other formats.
570 %macro GLOBALNAME_RAW 3
571 %ifdef ASM_FORMAT_ELF
574 %elifdef ASM_FORMAT_PE
576 %ifdef __YASM__ ; nasm does not support any attributes, it errors out. So, nasm is no good with control flow guard atm.
585 %elifdef ASM_FORMAT_MACHO
587 global %1:private_extern
592 %elifndef ASM_FORMAT_BIN
601 ; Global marker which is DECLASM() compatible.
603 ; @param %1 The symbol name - subjected to NAME().
604 ; @param %2 ELF and PE attributes: 'function', 'object', 'data', 'notype'.
605 ; PE ignores all but 'function' (yasm only). Other formats ignores
607 ; @param %3 Symbol visibility: 'hidden', 'protected', 'internal', and
608 ; RT_NOTHING (for 'default' visibility).
609 ; These are ELF attributes, but 'hidden' is translated to
610 ; 'private_extern' for the Macho-O format.
611 ; Ignored by other formats.
613 %macro GLOBALNAME_EX 3
614 GLOBALNAME_RAW NAME(%1), %2, %3
619 ; Global exported marker, raw version w/o automatic name mangling.
621 ; @param %1 The internal symbol name.
622 ; @param %2 The external symbol name.
623 ; @param %3 ELF and PE attributes: 'function', 'object', 'data', 'notype'.
624 ; PE ignores all but 'function' (yasm only). Other formats ignores
627 %macro EXPORTEDNAME_RAW 3
632 %ifdef ASM_FORMAT_OMF
636 GLOBALNAME_RAW %1, %3, RT_NOTHING
640 ; Global exported marker which is DECLASM() compatible.
642 ; @param %1 The symbol name - subjected to NAME().
643 ; @param %2 ELF and PE attributes: 'function', 'object', 'data', 'notype'.
644 ; PE ignores all but 'function' (yasm only). Other formats ignores
647 %macro EXPORTEDNAME_EX 2
648 EXPORTEDNAME_RAW NAME(%1), %1, %2
653 ; Begins a procedure, raw version w/o automatic name mangling.
654 ; @param 1 The (raw) name.
655 ; @param 2 Whether to manually apply IBT_ENDBRxx (1) or
656 ; not (0, default). Optional
657 %macro BEGINPROC_RAW 1-2 0
658 %ifdef RT_ASM_WITH_SEH64_ALT
659 SEH64_ALT_START_UNWIND_INFO %1
661 %ifdef RT_ASM_WITH_SEH64
665 GLOBALNAME_RAW %1, function, hidden
674 ; Begins a C callable (DECLASM) procedure.
675 %macro BEGINPROC 1-2 0
676 BEGINPROC_RAW NAME(%1), %2
681 ; Begins a exported procedure, raw version w/o automatic name mangling.
682 ; @param 1 Internal name.
683 ; @param 2 Exported name.
684 ; @param 3 Whether to manually apply IBT_ENDBRxx (1) or
685 ; not (0, default). Optional
686 %macro BEGINPROC_EXPORTED_RAW 2-3 0
687 %ifdef RT_ASM_WITH_SEH64_ALT
688 SEH64_ALT_START_UNWIND_INFO %1
690 %ifdef RT_ASM_WITH_SEH64
697 EXPORTEDNAME_RAW %1, %2, function
706 ; Begins a C callable (DECLASM) exported procedure.
707 %macro BEGINPROC_EXPORTED 1-2 0
708 BEGINPROC_EXPORTED_RAW NAME(%1), %1, %2
713 ; Ends a procedure, raw version w/o automatic name mangling.
715 %ifdef RT_ASM_WITH_SEH64
718 GLOBALNAME_RAW %1 %+ _EndProc, , hidden ; no function here, this isn't a valid code flow target.
719 %ifdef ASM_FORMAT_ELF
720 %ifndef __NASM__ ; nasm does this in the global directive.
721 size %1 %1 %+ _EndProc - %1
722 size %1 %+ _EndProc 4 ; make it non-zero to shut up warnigns from Linux's objtool.
725 db 0xCC, 0xCC, 0xCC, 0xCC
727 %ifdef RT_ASM_WITH_SEH64_ALT
729 ; Emit the RUNTIME_FUNCTION entry. The linker is picky here, no label.
730 %ifndef ASM_DEFINED_PDATA_SECTION
731 %define ASM_DEFINED_PDATA_SECTION
732 section .pdata rdata align=4
736 dd %1 wrt ..imagebase
737 dd %1 %+ _EndProc wrt ..imagebase
738 dd %1 %+ .unwind_info wrt ..imagebase
740 ; Restore code section.
747 ; Ends a C callable procedure.
754 ; Do OMF and Mach-O/Yasm segment definitions
756 ; Both format requires this to get the segment order right, in the Mach-O/Yasm case
757 ; it's only to make sure the .bss section ends up last (it's not declared here).
759 %ifdef ASM_FORMAT_OMF
760 %ifndef RT_NOINC_SEGMENTS
762 ; 16-bit segments first (OMF / OS/2 specific).
763 %ifdef RT_INCL_16BIT_SEGMENTS
764 segment DATA16 public CLASS=FAR_DATA align=16 use16
765 segment DATA16_INIT public CLASS=FAR_DATA align=16 use16
766 group DGROUP16 DATA16 DATA16_INIT
775 ; Begins 16-bit init data
776 %macro BEGINDATA16INIT 0
780 segment CODE16 public CLASS=FAR_CODE align=16 use16
781 segment CODE16_INIT public CLASS=FAR_CODE align=16 use16
782 group CGROUP16 CODE16 CODE16_INIT
791 ; Begins 16-bit init code
792 %macro BEGINCODE16INIT 0
799 segment TEXT32 public CLASS=CODE align=16 use32 flat
800 segment DATA32 public CLASS=DATA align=16 use32 flat
801 segment BSS32 public CLASS=BSS align=16 use32 flat
803 ; Make the TEXT32 segment default.
805 %endif ; RT_NOINC_SEGMENTS
808 %ifdef ASM_FORMAT_MACHO
818 %ifdef ASM_FORMAT_OMF
829 ; Begins constant (read-only) data
831 ; @remarks This is mapped to the CODE section/segment when there isn't
832 ; any dedicated const section/segment. (There is code that
833 ; assumes this, so don't try change it.)
834 %ifdef ASM_FORMAT_OMF
840 %ifdef ASM_FORMAT_MACHO ;; @todo check the other guys too.
849 ; Begins initialized data
850 %ifdef ASM_FORMAT_OMF
861 ; Begins uninitialized data
862 %ifdef ASM_FORMAT_OMF
875 ; Defines the bit count of the current context.
885 ; Defines the host architechture bit count.
888 %define HC_ARCH_BITS ARCH_BITS
890 %define HC_ARCH_BITS 32
895 ; Defines the host ring-3 architechture bit count.
898 %define R3_ARCH_BITS ARCH_BITS
900 %define R3_ARCH_BITS HC_ARCH_BITS
905 ; Defines the host ring-0 architechture bit count.
908 %define R0_ARCH_BITS ARCH_BITS
910 %define R0_ARCH_BITS HC_ARCH_BITS
915 ; Defines the guest architechture bit count.
918 %define GC_ARCH_BITS ARCH_BITS
920 %define GC_ARCH_BITS 32
927 ; The pesudo-instruction used to declare an initialized pointer variable in the host context.
928 %if HC_ARCH_BITS == 64
929 %define RTHCPTR_DEF dq
931 %define RTHCPTR_DEF dd
935 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
936 ; variable of the host context.
937 %if HC_ARCH_BITS == 64
938 %define RTHCPTR_RES resq
940 %define RTHCPTR_RES resd
944 ; The memory operand prefix used for a pointer in the host context.
945 %if HC_ARCH_BITS == 64
946 %define RTHCPTR_PRE qword
948 %define RTHCPTR_PRE dword
952 ; The size in bytes of a pointer in the host context.
953 %if HC_ARCH_BITS == 64
962 ; The pesudo-instruction used to declare an initialized pointer variable in the ring-0 host context.
963 %if R0_ARCH_BITS == 64
964 %define RTR0PTR_DEF dq
966 %define RTR0PTR_DEF dd
970 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
971 ; variable of the ring-0 host context.
972 %if R0_ARCH_BITS == 64
973 %define RTR0PTR_RES resq
975 %define RTR0PTR_RES resd
979 ; The memory operand prefix used for a pointer in the ring-0 host context.
980 %if R0_ARCH_BITS == 64
981 %define RTR0PTR_PRE qword
983 %define RTR0PTR_PRE dword
987 ; The size in bytes of a pointer in the ring-0 host context.
988 %if R0_ARCH_BITS == 64
997 ; The pesudo-instruction used to declare an initialized pointer variable in the ring-3 host context.
998 %if R3_ARCH_BITS == 64
999 %define RTR3PTR_DEF dq
1001 %define RTR3PTR_DEF dd
1005 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1006 ; variable of the ring-3 host context.
1007 %if R3_ARCH_BITS == 64
1008 %define RTR3PTR_RES resq
1010 %define RTR3PTR_RES resd
1014 ; The memory operand prefix used for a pointer in the ring-3 host context.
1015 %if R3_ARCH_BITS == 64
1016 %define RTR3PTR_PRE qword
1018 %define RTR3PTR_PRE dword
1022 ; The size in bytes of a pointer in the ring-3 host context.
1023 %if R3_ARCH_BITS == 64
1024 %define RTR3PTR_CB 8
1026 %define RTR3PTR_CB 4
1032 ; The pesudo-instruction used to declare an initialized pointer variable in the guest context.
1033 %if GC_ARCH_BITS == 64
1034 %define RTGCPTR_DEF dq
1036 %define RTGCPTR_DEF dd
1040 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1041 ; variable of the guest context.
1042 %if GC_ARCH_BITS == 64
1043 %define RTGCPTR_RES resq
1045 %define RTGCPTR_RES resd
1048 %define RTGCPTR32_RES resd
1049 %define RTGCPTR64_RES resq
1052 ; The memory operand prefix used for a pointer in the guest context.
1053 %if GC_ARCH_BITS == 64
1054 %define RTGCPTR_PRE qword
1056 %define RTGCPTR_PRE dword
1060 ; The size in bytes of a pointer in the guest context.
1061 %if GC_ARCH_BITS == 64
1062 %define RTGCPTR_CB 8
1064 %define RTGCPTR_CB 4
1069 ; The pesudo-instruction used to declare an initialized pointer variable in the raw mode context.
1070 %define RTRCPTR_DEF dd
1073 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1074 ; variable of the raw mode context.
1075 %define RTRCPTR_RES resd
1078 ; The memory operand prefix used for a pointer in the raw mode context.
1079 %define RTRCPTR_PRE dword
1082 ; The size in bytes of a pointer in the raw mode context.
1083 %define RTRCPTR_CB 4
1086 ;; @def RT_CCPTR_DEF
1087 ; The pesudo-instruction used to declare an initialized pointer variable in the current context.
1089 ;; @def RT_CCPTR_RES
1090 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1091 ; variable of the current context.
1093 ;; @def RT_CCPTR_PRE
1094 ; The memory operand prefix used for a pointer in the current context.
1097 ; The size in bytes of a pointer in the current context.
1100 %define RTCCPTR_DEF RTRCPTR_DEF
1101 %define RTCCPTR_RES RTRCPTR_RES
1102 %define RTCCPTR_PRE RTRCPTR_PRE
1103 %define RTCCPTR_CB RTRCPTR_CB
1106 %define RTCCPTR_DEF RTR0PTR_DEF
1107 %define RTCCPTR_RES RTR0PTR_RES
1108 %define RTCCPTR_PRE RTR0PTR_PRE
1109 %define RTCCPTR_CB RTR0PTR_CB
1111 %define RTCCPTR_DEF RTR3PTR_DEF
1112 %define RTCCPTR_RES RTR3PTR_RES
1113 %define RTCCPTR_PRE RTR3PTR_PRE
1114 %define RTCCPTR_CB RTR3PTR_CB
1120 ;; @def RTHCPHYS_DEF
1121 ; The pesudo-instruction used to declare an initialized host physical address.
1122 %define RTHCPHYS_DEF dq
1125 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized
1126 ; host physical address variable
1127 %define RTHCPHYS_RES resq
1130 ; The memory operand prefix used for a host physical address.
1131 %define RTHCPHYS_PRE qword
1134 ; The size in bytes of a host physical address.
1135 %define RTHCPHYS_CB 8
1139 ;; @def RTGCPHYS_DEF
1140 ; The pesudo-instruction used to declare an initialized guest physical address.
1141 %define RTGCPHYS_DEF dq
1143 ;; @def RTGCPHYS_RES
1144 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized
1145 ; guest physical address variable
1146 %define RTGCPHYS_RES resq
1149 ; The memory operand prefix used for a guest physical address.
1150 %define RTGCPHYS_PRE qword
1153 ; The size in bytes of a guest physical address.
1154 %define RTGCPHYS_CB 8
1159 ; The size of the long double C/C++ type.
1160 ; On 32-bit Darwin this is 16 bytes, on L4, Linux, OS/2 and Windows
1162 ; @todo figure out what 64-bit Windows does (I don't recall right now).
1173 ;; @name Floating point constants along the lines of the iprt/types.h types.
1174 ; @note YASM does support special the __Infinity__ and __NaN__ nasm tokens.
1176 %define RTFLOAT32U_QNAN_PLUS 0x7fc00000
1177 %define RTFLOAT32U_QNAN_MINUS 0xffc00000
1178 %define RTFLOAT32U_INF_PLUS 0x7f800000
1179 %define RTFLOAT32U_INF_MINUS 0xff800000
1181 %define RTFLOAT64U_QNAN_PLUS 0x7ff8000000000000
1182 %define RTFLOAT64U_QNAN_MINUS 0xfff8000000000000
1183 %define RTFLOAT64U_INF_PLUS 0x7ff0000000000000
1184 %define RTFLOAT64U_INF_MINUS 0xfff0000000000000
1189 ;; @def ASM_CALL64_GCC
1190 ; Indicates that we're using the GCC 64-bit calling convention.
1191 ; @see @ref sec_vboxrem_amd64_compare (in VBoxREMWrapper.cpp) for an ABI description.
1193 ;; @def ASM_CALL64_MSC
1194 ; Indicates that we're using the Microsoft 64-bit calling convention (fastcall on steroids).
1195 ; @see @ref sec_vboxrem_amd64_compare (in VBoxREMWrapper.cpp) for an ABI description.
1197 ; Note: On X86 we're using cdecl unconditionally. There is not yet any common
1198 ; calling convention on AMD64, that's why we need to support two different ones.)
1200 %ifdef RT_ARCH_AMD64
1201 %ifndef ASM_CALL64_GCC
1202 %ifndef ASM_CALL64_MSC
1203 ; define it based on the object format.
1204 %ifdef ASM_FORMAT_PE
1205 %define ASM_CALL64_MSC
1207 %define ASM_CALL64_GCC
1212 %ifdef ASM_CALL64_MSC
1213 %error "Only one of the ASM_CALL64_* defines should be defined!"
1217 ;later; %ifdef ASM_CALL64_GCC
1218 ;later; %error "ASM_CALL64_GCC is defined without RT_ARCH_AMD64!" ASM_CALL64_GCC
1220 ;later; %ifdef ASM_CALL64_MSC
1221 ;later; %error "ASM_CALL64_MSC is defined without RT_ARCH_AMD64!" ASM_CALL64_MSC
1226 ;; @def RT_BEGINPROC
1227 ; Starts an IPRT procedure that should be exported unless IN_RT_STATIC is defined.
1229 ; @param 1 The function name. Will apply NAME macro to it.
1230 %macro RT_BEGINPROC 1
1234 BEGINPROC_EXPORTED %1, 0
1236 %endmacro ; RT_BEGINPROC
1240 ; Symbol name wrapper for the No-CRT bits.
1242 ; In order to coexist in the same process as other CRTs, we need to
1243 ; decorate the symbols such that they don't conflict the ones in the
1244 ; other CRTs. The result of such conflicts / duplicate symbols can
1245 ; confuse the dynamic loader on unix like systems.
1247 ; @remark Always feed the name to this macro first and then pass the result
1248 ; on to the next *NAME* macro.
1250 %ifndef RT_WITHOUT_NOCRT_WRAPPERS
1251 %define RT_NOCRT(name) nocrt_ %+ name
1253 %define RT_NOCRT(name) name
1256 ;; @def RT_NOCRT_BEGINPROC
1257 ; Starts a NOCRT procedure, taking care of name wrapping and aliasing.
1259 ; Weak aliases for regular crt (%1) names to the nocrt_ prefixed ones will be
1260 ; added when RT_WITH_NOCRT_ALIASES is defined and the output is ELF. If
1261 ; RT_WITH_GENALIAS_NOCRT_ALIASES is undefined, strong aliases will be added for
1262 ; for non-ELF targets, otherwise it's assumed the genalias build tool will do
1263 ; the weak aliasing for us.
1265 %macro RT_NOCRT_BEGINPROC 1
1266 %ifdef RT_WITH_NOCRT_ALIASES
1268 BEGINPROC RT_NOCRT(%1), 1 ; Do our own IBT_ENDBRxx after aliasing/
1270 BEGINPROC_EXPORTED RT_NOCRT(%1), 1 ; Do our own IBT_ENDBRxx after aliasing/
1272 %ifdef ASM_FORMAT_ELF
1274 %ifdef RT_WITH_NOCRT_UNDERSCORE_ALIASES
1275 global NAME(_ %+ %1):function
1277 SAFE_LABEL NAME(_ %+ %1)
1279 global NAME(%1):function
1283 %elifndef RT_WITH_GENALIAS_NOCRT_ALIASES
1284 ; non-ELF when not using genalias.
1285 %ifdef RT_WITH_NOCRT_UNDERSCORE_ALIASES
1291 %else ; !RT_WITH_NOCRT_ALIASES
1293 BEGINPROC RT_NOCRT(%1), 0
1295 BEGINPROC_EXPORTED RT_NOCRT(%1), 0
1297 %endif ; !RT_WITH_NOCRT_ALIASES
1298 %endmacro ; RT_NOCRT_BEGINPROC
1303 ; The stack unit size / The register unit size.
1306 ; The stack pointer register (RSP or ESP).
1309 ; The base pointer register (RBP or ESP).
1312 ; RAX or EAX depending on context.
1315 ; RBX or EBX depending on context.
1318 ; RCX or ECX depending on context.
1321 ; RDX or EDX depending on context.
1324 ; RDI or EDI depending on context.
1327 ; RSI or ESI depending on context.
1330 ; 'wrt rip' for AMD64 targets, nothing for x86 ones.
1332 %ifdef RT_ARCH_AMD64
1342 %define xWrtRIP wrt rip
1358 ; NASM sets __PASS__ to 0 in preprocess-only mode, and to 3 when only generating dependencies.
1359 ; YASM has no such setting which is why we must rely on kBuild to tell us what we're doing.
1360 ; For simplicity, we'll set the kBuild macro when using NASM.
1362 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1364 %if __PASS__ == 0 || __PASS__ == 3
1365 %define KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1372 ; Some simple compile time assertions.
1374 ; Note! Requires new kBuild to work with YASM (see above).
1378 ; Structure size assertion macro.
1379 %define AssertCompileSize(a_Type, a_Size) AssertCompileSizeML a_Type, a_Size
1380 %macro AssertCompileSizeML 2
1381 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1382 %assign AssertVar_cbActual %1 %+ _size
1383 %assign AssertVar_cbExpected %2
1384 %if AssertVar_cbActual != AssertVar_cbExpected
1385 %error %1 is AssertVar_cbActual bytes instead of AssertVar_cbExpected
1391 ; Structure size alignment assertion macro.
1393 %define AssertCompileSizeAlignment(a_Type, a_Align) AssertCompileSizeAlignmentML a_Type, a_Align
1394 %macro AssertCompileSizeAlignmentML 2
1395 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1396 %assign AssertVar_cbActual %1 %+ _size
1397 %assign AssertVar_cbAlignment %2
1398 %if (AssertVar_cbActual & (AssertVar_cbAlignment - 1)) != 0
1399 %error %1 is AssertVar_cbActual bytes, expected size with AssertVar_cbAlignment bytes alignment.
1405 ; Structure member offset assertion macro.
1406 %define AssertCompileMemberOffset(a_Type, a_Member, a_off) AssertCompileMemberOffsetML a_Type, a_Member, a_off
1407 %macro AssertCompileMemberOffsetML 3
1408 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1409 %assign AssertVar_offActual %1 %+ . %+ %2
1410 %assign AssertVar_offExpected %3
1411 %if AssertVar_offActual != AssertVar_offExpected
1412 %error %1 %+ . %+ %2 is at AssertVar_offActual instead of AssertVar_offExpected
1418 ; Structure member alignment assertion macro.
1419 %define AssertCompileMemberAlignment(a_Type, a_Member, a_cbAlign) AssertCompileMemberAlignmentML a_Type, a_Member, a_cbAlign
1420 %macro AssertCompileMemberAlignmentML 3
1421 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1422 %assign AssertVar_offActual %1 %+ . %+ %2
1423 %assign AssertVar_cbAlign %3
1424 %if AssertVar_offActual & (AssertVar_cbAlign - 1)
1425 %error %1 %+ . %+ %2 is at AssertVar_offActual, expected AssertVar_cbAlign alignment
1431 ; Generic compile time expression assertion.
1432 %define AssertCompile(a_Expr) AssertCompileML { a_Expr }
1433 %macro AssertCompileML 1
1434 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1436 %assign AssertVar_uResult %1
1437 %error %1 => AssertVar_uResult