annotate doc/manual/callconvs/callconv_x64.tex @ 663:127b569978cc default tip

- another tweak handling clang trying to be too smart (see last commit)
author Tassilo Philipp
date Sun, 24 Mar 2024 13:52:44 +0100
parents fc614cb865c6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 %//////////////////////////////////////////////////////////////////////////////
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 % Tassilo Philipp <tphilipp@potion-studios.com>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 % Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 % purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 % copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 % THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 % WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 % MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 % ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 % WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 %//////////////////////////////////////////////////////////////////////////////
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 % ==================================================
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 % x64
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 \paragraph{Overview}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 The x64 (64bit) architecture designed by AMD is based on Intel's x86 (32bit)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 architecture, supporting it natively. It is sometimes referred to as x86-64,
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 AMD64, or, cloned by Intel, EM64T or Intel64.\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 On this processor, a word is defined to be 16 bits in size, a dword 32 bits
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 and a qword 64 bits. Note that this is due to historical reasons (terminology
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 didn't change with the introduction of 32 and 64 bit processors).\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 The x64 calling convention for MS Windows \cite{x64Win} differs from the
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 SystemV x64 calling convention \cite{x64SysV} used by Linux/*BSD/...
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 Note that this is not the only difference between these operating systems. The
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 64 bit programming model in use by 64 bit windows is LLP64, meaning that the C
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 types int and long remain 32 bits in size, whereas long long becomes 64 bits.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 Under Linux/*BSD/... it's LP64.\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 \\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 Compared to the x86 architecture, the 64 bit versions of the registers are
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 called rax, rbx, etc.. Furthermore, there are eight new general purpose
95
9e99918065e6 - doc fixes (mainly for html output)
cslag
parents: 92
diff changeset
43 registers r8-r15.\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 \paragraph{\product{dyncall} support}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48
340
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
49 Currently, the MS Windows and System V calling conventions are supported.\\
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
50 \product{Dyncall} can also be used to issue syscalls on System V platforms by
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
51 using the syscall number as target parameter and selecting the correct mode.
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 \subsubsection{MS Windows}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 \paragraph{Registers and register usage}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 \begin{table}[h]
77
e441ef3ec782 - manual layout tweaks
cslag
parents: 76
diff changeset
58 \begin{tabular*}{0.95\textwidth}{3 B}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 Name & Brief description\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 \hline
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 {\bf rax} & scratch, return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 {\bf rbx} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 {\bf rcx} & scratch, parameter 0 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 {\bf rdx} & scratch, parameter 1 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 {\bf rdi} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 {\bf rsi} & permanent\\
276
Tassilo Philipp
parents: 95
diff changeset
67 {\bf rbp} & permanent, may be used as frame pointer\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 {\bf rsp} & stack pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 {\bf r8-r9} & scratch, parameter 2 and 3 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 {\bf r10-r11} & scratch, permanent if required by caller (used for syscall/sysret)\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 {\bf r12-r15} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 {\bf xmm0} & scratch, floating point parameter 0, floating point return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 {\bf xmm1-xmm3} & scratch, floating point parameters 1-3\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 {\bf xmm4-xmm5} & scratch, permanent if required by caller\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 {\bf xmm6-xmm15} & permanent\\
76
7ca46969e0ad - tweaks in manual, mainly for html generation
cslag
parents: 0
diff changeset
76 \end{tabular*}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 \caption{Register usage on x64 MS Windows platform}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 \end{table}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 \paragraph{Parameter passing}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 \item stack parameter order: right-to-left
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 \item caller cleans up the stack
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 \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
86 spill area for the first 4)
499
fc614cb865c6 - doc and disasexample additions specific to non-trivial C++ aggregates as return values (incl. fixes to doc and additional LSB specific PPC32 section)
Tassilo Philipp
parents: 493
diff changeset
87 \item {\it non-trivial} C++ aggregates (as defined by the language) of any size, are passed indirectly via a pointer to a copy of the aggregate
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
88 \item aggregates (structs and unions) \textless\ 64 bits are passed like equal-sized integers
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 \item float and double parameters are passed via xmm0l-xmm3l
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 \item first 4 parameters are passed via the correct register depending on the parameter type - with mixed float and int parameters,
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 some registers are left out (e.g. first parameter ends up in rcx or xmm0, second in rdx or xmm1, etc.)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 \item parameters in registers are right justified
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 \item parameters \textless\ 64bits are not zero extended - zero the upper bits contiaining garbage if needed (but they are always
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 passed as a qword)
493
75cb8f79d725 - doc and disas examples update about C++ non-trivial aggregates
Tassilo Philipp
parents: 486
diff changeset
95 \item parameters \textgreater\ 64 bits are passed by via a pointer to a copy (for aggregate types, that caller-allocated memory must be 16-byte aligned)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 \item if callee takes address of a parameter, first 4 parameters must be dumped (to the reserved space on the stack) - for
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 floating point parameters, value must be stored in integer AND floating point register
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 \item caller cleans up the stack, not the callee (like cdecl)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 \item stack is always 16byte aligned - since return address is 64 bits in size, stacks with an odd number of parameters are
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 already aligned
331
74c056b597b7 - disassembly example annotations
Tassilo Philipp
parents: 328
diff changeset
101 \item ellipsis calls take floating point values in int and float registers (single precision floats are promoted to double precision as
74c056b597b7 - disassembly example annotations
Tassilo Philipp
parents: 328
diff changeset
102 required by ellipsis calls)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 \item if size of parameters \textgreater\ 1 page of memory (usually between 4k and 64k), chkstk must be called
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 \paragraph{Return values}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 \begin{itemize}
486
d160046da104 doc cleanup: removed outdated/wrong info and fixed wrong value size specs
Tassilo Philipp
parents: 467
diff changeset
110 \item return values of pointer, integral or aggregate (structs and unions) type (\textless=\ 64 bits) are returned via the rax register
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 \item floating point types are returned via the xmm0 register
493
75cb8f79d725 - doc and disas examples update about C++ non-trivial aggregates
Tassilo Philipp
parents: 486
diff changeset
112 \item for any other type \textgreater\ 64 bits (or for {\it non-trivial} C++ aggregates of any size), a hidden first parameter, with an address to the
75cb8f79d725 - doc and disas examples update about C++ non-trivial aggregates
Tassilo Philipp
parents: 486
diff changeset
113 return value is passed (for C++ thiscalls it is passed as {\bf second} parameter, after the this pointer)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 \paragraph{Stack layout}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
119 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
120 % 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
121 Stack directly after function prolog:\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 \begin{figure}[h]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 \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
125 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
127 register save area & \hspace{4cm} & & \mrrbrace{10}{caller's frame} \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
129 local data & & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
130 \hhline{~-~~}
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
131 \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
132 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
133 & arg 4 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
134 & 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
135 & r8 or xmm2 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
136 & rdx or xmm1 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
137 & rcx or xmm0 & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
139 & return address & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
141 register save area & & & \mrrbrace{4}{current frame} \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
143 local data & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
145 parameter area & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
147 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 \end{tabular}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 \caption{Stack layout on x64 Microsoft platform}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150 \end{figure}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
154 \clearpage
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 \subsubsection{System V (Linux / *BSD / MacOS X)}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
158 \paragraph{Registers and register usage}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 \begin{table}[h]
77
e441ef3ec782 - manual layout tweaks
cslag
parents: 76
diff changeset
161 \begin{tabular*}{0.95\textwidth}{3 B}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 Name & Brief description\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 \hline
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
164 {\bf rax} & scratch, return value, special use for varargs (in al, see below)\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
165 {\bf rbx} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 {\bf rcx} & scratch, parameter 3 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 {\bf rdx} & scratch, parameter 2 if integer or pointer, return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 {\bf rdi} & scratch, parameter 0 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169 {\bf rsi} & scratch, parameter 1 if integer or pointer\\
276
Tassilo Philipp
parents: 95
diff changeset
170 {\bf rbp} & permanent, may be used as frame pointer\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 {\bf rsp} & stack pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172 {\bf r8-r9} & scratch, parameter 4 and 5 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173 {\bf r10-r11} & scratch\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 {\bf r12-r15} & permanent\\
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
175 {\bf xmm0-xmm1} & scratch, floating point parameters 0-1, floating point return value\\
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
176 {\bf xmm2-xmm7} & scratch, floating point parameters 2-7\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177 {\bf xmm8-xmm15} & scratch\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 {\bf st0-st1} & scratch, 16 byte floating point return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 {\bf st2-st7} & scratch\\
76
7ca46969e0ad - tweaks in manual, mainly for html generation
cslag
parents: 0
diff changeset
180 \end{tabular*}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
181 \caption{Register usage on x64 System V (Linux/*BSD)}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182 \end{table}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184 \paragraph{Parameter passing}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 \item stack parameter order: right-to-left
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 \item caller cleans up the stack
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 \item first 6 integer/pointer parameters are passed via rdi, rsi, rdx, rcx, r8, r9
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190 \item first 8 floating point parameters \textless=\ 64 bits are passed via xmm0l-xmm7l
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191 \item parameters in registers are right justified
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
192 \item parameters that are not passed via registers are pushed onto the stack (with their sizes rounded up to qwords)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 \item parameters \textless\ 64bits are not zero extended - zero the upper bits contiaining garbage if needed (but they are always
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 passed as a qword)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 \item integer/pointer parameters \textgreater\ 64 bit are passed via 2 registers
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
196 \item if callee takes address of a parameter, number of used xmm registers is passed silently in al (passed number doesn't need to be
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197 exact but an upper bound on the number of used xmm registers)
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
198 \item aggregates (structs, unions (and arrays within those)) follow a more complicated logic (the following {\bf only considers field types supported by dyncall}):
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
199 \begin{itemize}
499
fc614cb865c6 - doc and disasexample additions specific to non-trivial C++ aggregates as return values (incl. fixes to doc and additional LSB specific PPC32 section)
Tassilo Philipp
parents: 493
diff changeset
200 \item {\it non-trivial} C++ aggregates (as defined by the language) of any size, are passed indirectly via a pointer to a copy of the aggregate
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
201 \item aggregates \textgreater\ 16 bytes are always passed entirely via the stack
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
202 \item all other aggregates are classified per qword, by looking at all fields occupying all or part of that qword, recursively
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
203 \begin{itemize}
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
204 \item if any field would be passed via the stack, the entire qword will
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
205 \item otherwise, if any field would be passed like an integer/pointer value, the entire qword will
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
206 \item otherwise the qword is passed like a floating point value
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
207 \end{itemize}
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
208 \item after qword classification, the logic is:
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
209 \begin{itemize}
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
210 \item if any qword is classified to be passed via the stack, the entire aggregate will
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
211 \item if the size of the aggregate is \textgreater\ 2 qwords, it is passed via the stack (except for single floating point values \textgreater\ 128bits)
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
212 \item all others are passed qword by qword according to their classification, like individual arguments
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
213 \item however, an aggregate is never split between registers and the stack, if it doesn't fit into available registers it is entirely passed via the stack (freeing such registers for subsequent arguments)
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
214 \end{itemize}
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
215 \end{itemize}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
216 \item stack is always 16byte aligned - since return address is 64 bits in size, stacks with an odd number of parameters are
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217 already aligned
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
218 \item no spill area is used on stack, iterating over varargs requires a specific va\_list implementation
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
220
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
222 \paragraph{Return values}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
223
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
224 \begin{itemize}
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
225 \item return values of pointer or integral type are returned via the rax register (and rdx if needed)
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
226 \item floating point types are returned via the xmm0 register (and xmm1 if needed)
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
227 \item aggregates are first classified in the same way as when passing them by value, then:
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
228 \begin{itemize}
493
75cb8f79d725 - doc and disas examples update about C++ non-trivial aggregates
Tassilo Philipp
parents: 486
diff changeset
229 \item for aggregates that would be passed via the stack (or for {\it non-trivial} C++ aggregates of any size), a hidden pointer to a non-shared,
75cb8f79d725 - doc and disas examples update about C++ non-trivial aggregates
Tassilo Philipp
parents: 486
diff changeset
230 caller provided space is {\bf passed} as hidden, first argument; this pointer will be returned via rax
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
231 \item otherwise, qword by qword is passed, using rax and rdx for integer/pointer qwords, and xmm0 and xmm1 for floating point ones
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
232 \end{itemize}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
233 \item floating point values \textgreater\ 64 bits are returned via st0 and st1
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
234 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
235
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
236
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
237 \paragraph{Stack layout}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
238
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
239 Stack frame is always 16-byte aligned. A 128 byte large zone beyond the
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
240 location pointed to by the stack pointer is referred to as "red zone",
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
241 considered to be reserved and not be modified by signal or interrupt handlers
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
242 (useful for temporary data not needed to be preserved across calls, and for
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
243 optimizations for leaf functions).
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
244 % verified/amended: TP nov 2019 (see also doc/disas_examples/x64.sysv.disas)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
245 Stack directly after function prolog:\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
246
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
247 \begin{figure}[h]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
248 \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
249 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
250 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
251 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
252 \hhline{~-~~}
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
253 local data (with padding) & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
254 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
255 \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
256 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
257 & arg 6 & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
258 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
259 & return address & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
260 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
261 register save area & & & \mrrbrace{4}{current frame} \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
262 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
263 local data & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
264 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
265 parameter area & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
266 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
267 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
268 \end{tabular}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
269 \caption{Stack layout on x64 System V (Linux/*BSD)}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
270 \end{figure}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
271
340
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
272
467
b47168dacba6 manual:
Tassilo Philipp
parents: 345
diff changeset
273 \clearpage
340
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
274
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
275 \subsubsection{System V syscalls}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
276
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
277 \paragraph{Parameter passing}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
278
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
279 \begin{itemize}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
280 \item syscall is issued via the {\em syscall} instruction
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
281 \item kernel destroys registers rcx and r11
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
282 \item syscall number is set in rax
345
c607d67cd6b8 - doc: added syscall info to appendix, fixed broken literature link
Tassilo Philipp
parents: 340
diff changeset
283 \item params are passed in the following registers in this order: rdi, rsi, rdx, rcx, r8, r9
c607d67cd6b8 - doc: added syscall info to appendix, fixed broken literature link
Tassilo Philipp
parents: 340
diff changeset
284 \item no stack in use, meaning syscalls are in theory limited to six arguments
340
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
285 \item register rax holds the return value (values in between -4095 and -1 indicate errors)
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
286 \end{itemize}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
287