annotate doc/manual/callconvs/callconv_x64.tex @ 340:6e33db95e724

- syscall infos in doc
author Tassilo Philipp
date Sat, 04 Jan 2020 22:48:51 +0100
parents 74c056b597b7
children c607d67cd6b8
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)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 \item float and double parameters are passed via xmm0l-xmm3l
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 \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
89 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
90 \item parameters in registers are right justified
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 \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
92 passed as a qword)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 \item parameters \textgreater\ 64 bit are passed by reference
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 \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
95 floating point parameters, value must be stored in integer AND floating point register
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 \item caller cleans up the stack, not the callee (like cdecl)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 \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
98 already aligned
331
74c056b597b7 - disassembly example annotations
Tassilo Philipp
parents: 328
diff changeset
99 \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
100 required by ellipsis calls)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 \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
102 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 \paragraph{Return values}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108 \item return values of pointer or integral type (\textless=\ 64 bits) are returned via the rax register
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 \item floating point types are returned via the xmm0 register
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 \item for types \textgreater\ 64 bits, a secret first parameter with an address to the return value is passed
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 \paragraph{Stack layout}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
116 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
117 % 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
118 Stack directly after function prolog:\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 \begin{figure}[h]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 \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
122 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
124 register save area & \hspace{4cm} & & \mrrbrace{10}{caller's frame} \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
126 local data & & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
127 \hhline{~-~~}
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
128 \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
129 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
130 & arg 4 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
131 & 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
132 & r8 or xmm2 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
133 & rdx or xmm1 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
134 & rcx or xmm0 & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
136 & return address & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
138 register save area & & & \mrrbrace{4}{current frame} \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
140 local data & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
142 parameter area & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
144 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 \end{tabular}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 \caption{Stack layout on x64 Microsoft platform}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 \end{figure}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 \newpage
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153 \subsubsection{System V (Linux / *BSD / MacOS X)}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 \paragraph{Registers and register usage}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 \begin{table}[h]
77
e441ef3ec782 - manual layout tweaks
cslag
parents: 76
diff changeset
158 \begin{tabular*}{0.95\textwidth}{3 B}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 Name & Brief description\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 \hline
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 {\bf rax} & scratch, return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 {\bf rbx} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 {\bf rcx} & scratch, parameter 3 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164 {\bf rdx} & scratch, parameter 2 if integer or pointer, return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
165 {\bf rdi} & scratch, parameter 0 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 {\bf rsi} & scratch, parameter 1 if integer or pointer\\
276
Tassilo Philipp
parents: 95
diff changeset
167 {\bf rbp} & permanent, may be used as frame pointer\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 {\bf rsp} & stack pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169 {\bf r8-r9} & scratch, parameter 4 and 5 if integer or pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 {\bf r10-r11} & scratch\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 {\bf r12-r15} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172 {\bf xmm0} & scratch, floating point parameters 0, floating point return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173 {\bf xmm1-xmm7} & scratch, floating point parameters 1-7\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 {\bf xmm8-xmm15} & scratch\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
175 {\bf st0-st1} & scratch, 16 byte floating point return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 {\bf st2-st7} & scratch\\
76
7ca46969e0ad - tweaks in manual, mainly for html generation
cslag
parents: 0
diff changeset
177 \end{tabular*}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 \caption{Register usage on x64 System V (Linux/*BSD)}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 \end{table}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
181 \paragraph{Parameter passing}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184 \item stack parameter order: right-to-left
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185 \item caller cleans up the stack
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 \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
187 \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
188 \item parameters in registers are right justified
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 \item parameters that are not passed via registers are pushed onto the stack
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190 \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
191 passed as a qword)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 \item integer/pointer parameters \textgreater\ 64 bit are passed via 2 registers
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 \item if callee takes address of a parameter, number of used xmm registers is passed silently in al (passed number mustn't be
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 exact but an upper bound on the number of used xmm registers)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 \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
196 already aligned
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
197 \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
198 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
199
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
200
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201 \paragraph{Return values}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
202
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
203 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
204 \item return values of pointer or integral type (\textless=\ 64 bits) are returned via the rax register
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
205 \item floating point types are returned via the xmm0 register
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
206 \item for types \textgreater\ 64 bits, a secret first parameter with an address to the return value is passed - the passed in address
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
207 will be returned in rax
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
208 \item floating point values \textgreater\ 64 bits are returned via st0 and st1
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
209 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
210
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
211
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
212 \paragraph{Stack layout}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
213
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
214 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
215 % 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
216 Stack directly after function prolog:\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
218 \begin{figure}[h]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219 \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
220 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
222 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
223 \hhline{~-~~}
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
224 local data (with padding) & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
225 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
226 \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
227 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
228 & arg 6 & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
229 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
230 & return address & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
231 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
232 register save area & & & \mrrbrace{4}{current frame} \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
233 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
234 local data & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
235 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
236 parameter area & & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
237 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 276
diff changeset
238 & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
239 \end{tabular}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
240 \caption{Stack layout on x64 System V (Linux/*BSD)}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
241 \end{figure}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
242
340
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
243
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
244 \newpage
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
245
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
246 \subsubsection{System V syscalls}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
247
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
248 \paragraph{Parameter passing}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
249
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
250 \begin{itemize}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
251 \item syscall is issued via the {\em syscall} instruction
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
252 \item kernel destroys registers rcx and r11
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
253 \item syscall number is set in rax
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
254 \item up to 6 params are passed in the following registers in this order: rdi, rsi, rdx, rcx, r8, r9
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
255 \item no stack in use, meaning syscalls are limited to six arguments
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
256 \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
257 \end{itemize}
6e33db95e724 - syscall infos in doc
Tassilo Philipp
parents: 331
diff changeset
258