Mercurial > pub > dyncall > dyncall
annotate dyncall/dyncall_call_x64.S @ 533:71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
* integration of aggregate-by-value (struct, union) support patch for x64 (win and sysv)
* windows/x64 asm additions to specify how stack unwinds (help for debuggers, exception handling, etc.)
* see Changelog for details
- new calling convention modes for thiscalls (platform agnostic, was specific before)
* new signature character for platform agnostic thiscalls ('*' / DC_SIGCHAR_CC_THISCALL)
- dcCallF(), dcVCallF(), dcArgF() and dcVArgF():
* added support for aggregates-by-value (wasn't part of patch)
* change that those functions don't implicitly call dcReset() anymore, which was unflexible (breaking change)
- added macros to feature test implementation for aggregate-by-value and syscall support
- changed libdyncall_s.lib and libdyncallback_s.lib order in callback test makefiles, as some toolchains are picky about order
- doc:
* man page updates to describe aggregate interface
* manual overview changes to highlight platforms with aggregate-by-value support
- test/plain: replaced tests w/ old/stale sctruct interface with new aggregate one
author | Tassilo Philipp |
---|---|
date | Thu, 21 Apr 2022 13:35:47 +0200 |
parents | ab2d78e48ca2 |
children |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dyncall | |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
5 File: dyncall/dyncall_call_x64.S |
0 | 6 Description: All x64 abi call kernel implementation |
7 License: | |
8 | |
339 | 9 Copyright (c) 2007-2020 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 10 Tassilo Philipp <tphilipp@potion-studios.com> |
11 | |
12 Permission to use, copy, modify, and distribute this software for any | |
13 purpose with or without fee is hereby granted, provided that the above | |
14 copyright notice and this permission notice appear in all copies. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
23 | |
24 */ | |
25 | |
26 | |
27 | |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
28 #include "../portasm/portasm-x64.S" |
0 | 29 |
30 BEGIN_ASM | |
31 | |
32 /*--------------------------------------------------------------------------- | |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
33 |
0 | 34 Call Kernel for x64 System V |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
35 |
0 | 36 Input: |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
37 RDI : size of arguments to be passed via stack |
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
38 RSI : pointer to arguments to be passed via the stack |
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
39 RDX : pointer to arguments of integral/pointer type to be passed via registers |
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
40 RCX : pointer to arguments of floating point type to be passed via registers |
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
41 R8 : target function pointer |
0 | 42 Notes: |
43 RSP+8: is always 16-byte aligned (32-byte align if __m256 is used) | |
44 */ | |
45 | |
46 GLOBAL(dcCall_x64_sysv) | |
47 BEGIN_PROC(dcCall_x64_sysv) | |
48 PUSH(RBP) /* Pseudo-prolog - preserve RBP. */ | |
49 PUSH(RBX) /* Preserve RBX and store pointer to function in it. */ | |
50 MOV(RSP,RBP) /* Store stack pointer in RBP. */ | |
51 MOV(R8 ,RBX) | |
52 MOVSD(QWORD(RCX,0) ,XMM0) /* Copy first 8 floats to XMM0-XMM7. */ | |
53 MOVSD(QWORD(RCX,8) ,XMM1) | |
54 MOVSD(QWORD(RCX,16),XMM2) | |
55 MOVSD(QWORD(RCX,24),XMM3) | |
56 MOVSD(QWORD(RCX,32),XMM4) | |
57 MOVSD(QWORD(RCX,40),XMM5) | |
58 MOVSD(QWORD(RCX,48),XMM6) | |
59 MOVSD(QWORD(RCX,56),XMM7) | |
60 | |
61 ADD(LIT(31),RDI) /* Align stack to 32-byte. */ | |
62 AND(LIT(-32),RDI) | |
339 | 63 ADD(LIT(8),RDI) /* Adjust by 8-byte for the return-address. */ |
0 | 64 SUB(RDI,RSP) /* Setup stack frame by subtracting the size of arguments. */ |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
65 |
0 | 66 MOV(RDI,RCX) /* Store number of bytes to copy to stack in RCX (for rep movsb). */ |
67 MOV(RSP,RDI) /* Store pointer to beginning of stack arguments in RDI (for rep movsb). */ | |
68 | |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
69 REP(MOVSB) /* copy bytes (@@@ should be optimized, movq?). */ |
0 | 70 |
71 MOV(QWORD(RDX,0),RDI) /* copy first six int/pointer arguments to RDI, RSI, RDX, RCX, R8, R9. */ | |
72 MOV(QWORD(RDX,8),RSI) | |
73 MOV(QWORD(RDX,24),RCX) | |
74 MOV(QWORD(RDX,32),R8) | |
75 MOV(QWORD(RDX,40),R9) | |
76 MOV(QWORD(RDX,16),RDX) /* Set RDX last to not overwrite it to soon. */ | |
77 | |
78 MOVB(LIT(8),AL) /* Put upper bound of number of used xmm registers in AL. */ | |
79 CALL_REG(RBX) /* Call function. */ | |
80 | |
81 MOV(RBP,RSP) /* Restore stack pointer. */ | |
82 POP(RBX) /* Restore RBX. */ | |
83 POP(RBP) /* Pseudo-epilog. */ | |
84 RET() | |
183
e38d33230b09
- typo fix in x64 asm end_proc use (only affected masm)
Tassilo Philipp
parents:
166
diff
changeset
|
85 END_PROC(dcCall_x64_sysv) |
0 | 86 |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
87 /* wrapper for dcCall_x64_sysv to grab 4 regs used to return (small) aggregate by value */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
88 GLOBAL(dcCall_x64_sysv_aggr) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
89 BEGIN_PROC(dcCall_x64_sysv_aggr) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
90 PUSH(R9) /* preserve ptr to copy retval regs to (also realigns stack) */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
91 CALL(CSYM(dcCall_x64_sysv)) /* params (in regs) passed-through to next call, as-is */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
92 POP(R9) /* get ptr to retval regs back */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
93 |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
94 /* copy regs holding aggregate data to provided space (pointed to by r12) */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
95 MOV(RAX, QWORD(R9,0)) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
96 MOV(RDX, QWORD(R9,8)) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
97 MOVSD(XMM0, QWORD(R9,16)) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
98 MOVSD(XMM1, QWORD(R9,24)) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
99 |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
100 RET() |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
101 END_PROC(dcCall_x64_sysv_aggr) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
102 |
0 | 103 /*--------------------------------------------------------------------------- |
104 | |
105 Call Kernel for x64 Win64 | |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
106 |
0 | 107 Input: |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
108 RCX : size of arguments to be passed via stack |
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
109 RDX : pointer to arguments to be passed via the stack |
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
110 R8 : pointer to arguments of integral/pointer type to be passed via registers |
0 | 111 R9 : target function pointer |
112 | |
113 */ | |
114 | |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
115 GLOBAL_FRAME(dcCall_x64_win64) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
116 FRAME_BEGIN_PROC(dcCall_x64_win64) |
0 | 117 |
118 PUSH(RBP) /* Pseudo-prolog - preserve RBP. */ | |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
119 FRAME_PUSH_REG(RBP) |
0 | 120 PUSH(RSI) /* Preserve RSI and RDI. */ |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
121 FRAME_PUSH_REG(RSI) |
0 | 122 PUSH(RDI) |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
123 FRAME_PUSH_REG(RDI) |
165
572aff021627
- file name/layout cleanup, removed "-att" from x64 .S filenames, as unnecessary and also misleading
cslag
parents:
0
diff
changeset
|
124 |
0 | 125 MOV(RSP,RBP) /* Store stack pointer in RBP. */ |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
126 FRAME_SET(0, RBP) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
127 FRAME_ENDPROLOG() |
0 | 128 |
129 ADD(LIT(15),RCX) /* Align stack size to 16 bytes. */ | |
130 AND(LIT(-16),RCX) | |
131 SUB(RCX,RSP) /* Setup stack frame by subtracting the size of the arguments. */ | |
132 | |
133 | |
134 MOV(RDX, RSI) /* Let RSI point to the arguments. */ | |
135 MOV(RSP, RDI) /* Store pointer to beginning of stack arguments in RDI (for rep movsb). */ | |
136 MOV(R9, RAX) /* Put function address in RAX. */ | |
137 | |
138 REP(MOVSB) /* @@@ should be optimized (e.g. movq) */ | |
139 | |
140 MOV(QWORD(R8,0),RCX) /* Copy first four arguments to RCX, RDX, R9, R8 ( and XMM0-XMM3. ) */ | |
141 MOV(QWORD(R8,8),RDX) | |
142 MOV(QWORD(R8,24),R9) | |
143 MOV(QWORD(R8,16),R8) | |
144 | |
145 MOVD(RCX, XMM0) | |
146 MOVD(RDX, XMM1) | |
147 MOVD(R8, XMM2) | |
148 MOVD(R9, XMM3) | |
149 | |
150 PUSH(R9) /* Push first four arguments onto the stack preserve area. */ | |
151 PUSH(R8) | |
152 PUSH(RDX) | |
153 PUSH(RCX) | |
154 | |
155 CALL_REG(RAX) /* Invoke function. */ | |
156 | |
157 MOV(RBP, RSP) /* Restore stack pointer (such that we can pop the preserved vALues). */ | |
158 | |
159 POP(RDI) /* Restore RSI and RDI. */ | |
160 POP(RSI) | |
161 POP(RBP) /* Pseudo-epilog. */ | |
162 | |
163 RET() | |
164 | |
165 END_PROC(dcCall_x64_win64) | |
166 | |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
167 GLOBAL(dcCall_x64_win64_aggr) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
168 BEGIN_PROC(dcCall_x64_win64_aggr) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
169 SUB(LIT(8), RSP) /* Re-align the stack */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
170 CALL(CSYM(dcCall_x64_win64)) /* params (in regs) passed-through to next call, as-is */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
171 ADD(LIT(8), RSP) /* Restore the stack pointer */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
172 |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
173 MOV(QWORD(RSP, 40), R8) /* ptr to aggregate mem -> R8 (passed as only stack arg, 0x40 to skip ret addr and spill area */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
174 MOV(RAX, QWORD(R8, 0)) /* Copy aggregate value to memory */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
175 |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
176 RET() |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
177 END_PROC(dcCall_x64_win64_aggr) |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
178 |
339 | 179 /*--------------------------------------------------------------------------- |
180 | |
181 Call Kernel for x64 System V syscalls | |
182 | |
183 Input: | |
184 RDI : pointer to arguments | |
185 RSI : syscall id | |
186 | |
187 */ | |
188 | |
341 | 189 GLOBAL(dcCall_x64_syscall_sysv) |
190 BEGIN_PROC(dcCall_x64_syscall_sysv) | |
339 | 191 |
192 MOV(RSI,RAX) /* load system call id. */ | |
193 MOV(QWORD(RDI,40),R9) /* copy first six int/pointer arguments to RDI, RSI, RDX, R10, R8, R9. */ | |
194 MOV(QWORD(RDI,32),R8) | |
195 MOV(QWORD(RDI,24),R10) | |
196 MOV(QWORD(RDI,16),RDX) | |
197 MOV(QWORD(RDI,8),RSI) | |
198 MOV(QWORD(RDI,0),RDI) /* Set RDI last to not overwrite it to soon. */ | |
199 SYSCALL | |
200 RET() | |
201 | |
341 | 202 END_PROC(dcCall_x64_syscall_sysv) |
339 | 203 |
0 | 204 END_ASM |
205 | |
533
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
206 /* vim: set ts=8: */ |
71c884e610f0
- integration of patches from Raphael Luba, Thekla, Inc.:
Tassilo Philipp
parents:
341
diff
changeset
|
207 |