Mercurial > pub > dyncall > dyncall
annotate doc/manual/callconvs/callconv_x64.tex @ 328:276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
author | Tassilo Philipp |
---|---|
date | Fri, 22 Nov 2019 23:11:56 +0100 |
parents | 277fe1ff3e14 |
children | 74c056b597b7 |
rev | line source |
---|---|
0 | 1 %////////////////////////////////////////////////////////////////////////////// |
2 % | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
3 % Copyright (c) 2007-2019 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 4 % Tassilo Philipp <tphilipp@potion-studios.com> |
5 % | |
6 % Permission to use, copy, modify, and distribute this software for any | |
7 % purpose with or without fee is hereby granted, provided that the above | |
8 % copyright notice and this permission notice appear in all copies. | |
9 % | |
10 % THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 % WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 % MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 % ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 % WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 % | |
18 %////////////////////////////////////////////////////////////////////////////// | |
19 | |
20 % ================================================== | |
21 % x64 | |
22 % ================================================== | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
23 \subsection{x64 Calling Conventions} |
0 | 24 |
25 | |
26 \paragraph{Overview} | |
27 | |
28 The x64 (64bit) architecture designed by AMD is based on Intel's x86 (32bit) | |
29 architecture, supporting it natively. It is sometimes referred to as x86-64, | |
30 AMD64, or, cloned by Intel, EM64T or Intel64.\\ | |
31 On this processor, a word is defined to be 16 bits in size, a dword 32 bits | |
32 and a qword 64 bits. Note that this is due to historical reasons (terminology | |
33 didn't change with the introduction of 32 and 64 bit processors).\\ | |
34 The x64 calling convention for MS Windows \cite{x64Win} differs from the | |
35 SystemV x64 calling convention \cite{x64SysV} used by Linux/*BSD/... | |
36 Note that this is not the only difference between these operating systems. The | |
37 64 bit programming model in use by 64 bit windows is LLP64, meaning that the C | |
38 types int and long remain 32 bits in size, whereas long long becomes 64 bits. | |
39 Under Linux/*BSD/... it's LP64.\\ | |
40 \\ | |
41 Compared to the x86 architecture, the 64 bit versions of the registers are | |
42 called rax, rbx, etc.. Furthermore, there are eight new general purpose | |
95 | 43 registers r8-r15.\\ |
0 | 44 |
45 | |
46 | |
47 \paragraph{\product{dyncall} support} | |
48 | |
49 \product{dyncall} supports the MS Windows and System V calling convention.\\ | |
50 \\ | |
51 | |
52 | |
53 | |
54 \subsubsection{MS Windows} | |
55 | |
56 \paragraph{Registers and register usage} | |
57 | |
58 \begin{table}[h] | |
77 | 59 \begin{tabular*}{0.95\textwidth}{3 B} |
0 | 60 Name & Brief description\\ |
61 \hline | |
62 {\bf rax} & scratch, return value\\ | |
63 {\bf rbx} & permanent\\ | |
64 {\bf rcx} & scratch, parameter 0 if integer or pointer\\ | |
65 {\bf rdx} & scratch, parameter 1 if integer or pointer\\ | |
66 {\bf rdi} & permanent\\ | |
67 {\bf rsi} & permanent\\ | |
276 | 68 {\bf rbp} & permanent, may be used as frame pointer\\ |
0 | 69 {\bf rsp} & stack pointer\\ |
70 {\bf r8-r9} & scratch, parameter 2 and 3 if integer or pointer\\ | |
71 {\bf r10-r11} & scratch, permanent if required by caller (used for syscall/sysret)\\ | |
72 {\bf r12-r15} & permanent\\ | |
73 {\bf xmm0} & scratch, floating point parameter 0, floating point return value\\ | |
74 {\bf xmm1-xmm3} & scratch, floating point parameters 1-3\\ | |
75 {\bf xmm4-xmm5} & scratch, permanent if required by caller\\ | |
76 {\bf xmm6-xmm15} & permanent\\ | |
76 | 77 \end{tabular*} |
0 | 78 \caption{Register usage on x64 MS Windows platform} |
79 \end{table} | |
80 | |
81 \paragraph{Parameter passing} | |
82 | |
83 \begin{itemize} | |
84 \item stack parameter order: right-to-left | |
85 \item caller cleans up the stack | |
86 \item first 4 integer/pointer parameters are passed via rcx, rdx, r8, r9 (from left to right), others are pushed on stack (there is a | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
87 spill area for the first 4) |
0 | 88 \item float and double parameters are passed via xmm0l-xmm3l |
89 \item first 4 parameters are passed via the correct register depending on the parameter type - with mixed float and int parameters, | |
90 some registers are left out (e.g. first parameter ends up in rcx or xmm0, second in rdx or xmm1, etc.) | |
91 \item parameters in registers are right justified | |
92 \item parameters \textless\ 64bits are not zero extended - zero the upper bits contiaining garbage if needed (but they are always | |
93 passed as a qword) | |
94 \item parameters \textgreater\ 64 bit are passed by reference | |
95 \item if callee takes address of a parameter, first 4 parameters must be dumped (to the reserved space on the stack) - for | |
96 floating point parameters, value must be stored in integer AND floating point register | |
97 \item caller cleans up the stack, not the callee (like cdecl) | |
98 \item stack is always 16byte aligned - since return address is 64 bits in size, stacks with an odd number of parameters are | |
99 already aligned | |
100 \item ellipsis calls take floating point values in int and float registers (single precision floats are promoted to double precision | |
101 as defined for ellipsis calls) | |
102 \item if size of parameters \textgreater\ 1 page of memory (usually between 4k and 64k), chkstk must be called | |
103 \end{itemize} | |
104 | |
105 | |
106 \paragraph{Return values} | |
107 | |
108 \begin{itemize} | |
109 \item return values of pointer or integral type (\textless=\ 64 bits) are returned via the rax register | |
110 \item floating point types are returned via the xmm0 register | |
111 \item for types \textgreater\ 64 bits, a secret first parameter with an address to the return value is passed | |
112 \end{itemize} | |
113 | |
114 | |
115 \paragraph{Stack layout} | |
116 | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
117 Stack frame is always 16-byte aligned. |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
118 % verified/amended: TP nov 2019 (@@@ no doc/disas_examples/x64.win.disas, yet...@@@) |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
119 Stack directly after function prolog:\\ |
0 | 120 |
121 \begin{figure}[h] | |
122 \begin{tabular}{5|3|1 1} | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
123 & \vdots & & \\ |
0 | 124 \hhline{~=~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
125 register save area & \hspace{4cm} & & \mrrbrace{10}{caller's frame} \\ |
0 | 126 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
127 local data & & & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
128 \hhline{~-~~} |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
129 \mrlbrace{7}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
130 & \ldots & & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
131 & arg 4 & & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
132 & r9 or xmm3 & \mrrbrace{4}{spill area} & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
133 & r8 or xmm2 & & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
134 & rdx or xmm1 & & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
135 & rcx or xmm0 & & \\ |
0 | 136 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
137 & return address & & \\ |
0 | 138 \hhline{~=~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
139 register save area & & & \mrrbrace{4}{current frame} \\ |
0 | 140 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
141 local data & & & \\ |
0 | 142 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
143 parameter area & & & \\ |
0 | 144 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
145 & \vdots & & \\ |
0 | 146 \end{tabular} |
147 \caption{Stack layout on x64 Microsoft platform} | |
148 \end{figure} | |
149 | |
150 | |
151 | |
152 \newpage | |
153 | |
154 \subsubsection{System V (Linux / *BSD / MacOS X)} | |
155 | |
156 \paragraph{Registers and register usage} | |
157 | |
158 \begin{table}[h] | |
77 | 159 \begin{tabular*}{0.95\textwidth}{3 B} |
0 | 160 Name & Brief description\\ |
161 \hline | |
162 {\bf rax} & scratch, return value\\ | |
163 {\bf rbx} & permanent\\ | |
164 {\bf rcx} & scratch, parameter 3 if integer or pointer\\ | |
165 {\bf rdx} & scratch, parameter 2 if integer or pointer, return value\\ | |
166 {\bf rdi} & scratch, parameter 0 if integer or pointer\\ | |
167 {\bf rsi} & scratch, parameter 1 if integer or pointer\\ | |
276 | 168 {\bf rbp} & permanent, may be used as frame pointer\\ |
0 | 169 {\bf rsp} & stack pointer\\ |
170 {\bf r8-r9} & scratch, parameter 4 and 5 if integer or pointer\\ | |
171 {\bf r10-r11} & scratch\\ | |
172 {\bf r12-r15} & permanent\\ | |
173 {\bf xmm0} & scratch, floating point parameters 0, floating point return value\\ | |
174 {\bf xmm1-xmm7} & scratch, floating point parameters 1-7\\ | |
175 {\bf xmm8-xmm15} & scratch\\ | |
176 {\bf st0-st1} & scratch, 16 byte floating point return value\\ | |
177 {\bf st2-st7} & scratch\\ | |
76 | 178 \end{tabular*} |
0 | 179 \caption{Register usage on x64 System V (Linux/*BSD)} |
180 \end{table} | |
181 | |
182 \paragraph{Parameter passing} | |
183 | |
184 \begin{itemize} | |
185 \item stack parameter order: right-to-left | |
186 \item caller cleans up the stack | |
187 \item first 6 integer/pointer parameters are passed via rdi, rsi, rdx, rcx, r8, r9 | |
188 \item first 8 floating point parameters \textless=\ 64 bits are passed via xmm0l-xmm7l | |
189 \item parameters in registers are right justified | |
190 \item parameters that are not passed via registers are pushed onto the stack | |
191 \item parameters \textless\ 64bits are not zero extended - zero the upper bits contiaining garbage if needed (but they are always | |
192 passed as a qword) | |
193 \item integer/pointer parameters \textgreater\ 64 bit are passed via 2 registers | |
194 \item if callee takes address of a parameter, number of used xmm registers is passed silently in al (passed number mustn't be | |
195 exact but an upper bound on the number of used xmm registers) | |
196 \item stack is always 16byte aligned - since return address is 64 bits in size, stacks with an odd number of parameters are | |
197 already aligned | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
198 \item no spill area is used on stack, iterating over varargs requires a specific va\_list implementation |
0 | 199 \end{itemize} |
200 | |
201 | |
202 \paragraph{Return values} | |
203 | |
204 \begin{itemize} | |
205 \item return values of pointer or integral type (\textless=\ 64 bits) are returned via the rax register | |
206 \item floating point types are returned via the xmm0 register | |
207 \item for types \textgreater\ 64 bits, a secret first parameter with an address to the return value is passed - the passed in address | |
208 will be returned in rax | |
209 \item floating point values \textgreater\ 64 bits are returned via st0 and st1 | |
210 \end{itemize} | |
211 | |
212 | |
213 \paragraph{Stack layout} | |
214 | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
215 Stack frame is always 16-byte aligned. |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
216 % verified/amended: TP nov 2019 (see also doc/disas_examples/x64.sysv.disas) |
0 | 217 Stack directly after function prolog:\\ |
218 | |
219 \begin{figure}[h] | |
220 \begin{tabular}{5|3|1 1} | |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
221 & \vdots & & \\ |
0 | 222 \hhline{~=~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
223 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
224 \hhline{~-~~} |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
225 local data (with padding) & & & \\ |
0 | 226 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
227 \mrlbrace{3}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
228 & \ldots & & \\ |
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
229 & arg 6 & & \\ |
0 | 230 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
231 & return address & & \\ |
0 | 232 \hhline{~=~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
233 register save area & & & \mrrbrace{4}{current frame} \\ |
0 | 234 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
235 local data & & & \\ |
0 | 236 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
237 parameter area & & & \\ |
0 | 238 \hhline{~-~~} |
328
276eb8c87aa0
- review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents:
276
diff
changeset
|
239 & \vdots & & \\ |
0 | 240 \end{tabular} |
241 \caption{Stack layout on x64 System V (Linux/*BSD)} | |
242 \end{figure} | |
243 |