annotate doc/manual/callconvs/callconv_arm64.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 4a64b733dc76
children bac52ab8869f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
1 %//////////////////////////////////////////////////////////////////////////////
0
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: 117
diff changeset
3 % Copyright (c) 2014-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 %
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
18 %//////////////////////////////////////////////////////////////////////////////
0
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 % ARM64
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: 117
diff changeset
23 \subsection{ARM64 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 \paragraph{Overview}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
117
4a64b733dc76 - doc, added first version of mips64 callconv page
cslag
parents: 95
diff changeset
27 ARMv8 introduced the AArch64 calling convention. ARM64 chips can be run in 64 or 32bit mode, but not by the same process. Interworking is only intra-process.\\
4a64b733dc76 - doc, added first version of mips64 callconv page
cslag
parents: 95
diff changeset
28 The word size is defined to be 32 bits, a dword 64 bits. Note that this is due to historical reasons (terminology didn't change from ARM32).\\
95
9e99918065e6 - doc fixes (mainly for html output)
cslag
parents: 92
diff changeset
29 For more details, take a look at the Procedure Call Standard for the ARM 64-bit Architecture \cite{AAPCS64}.\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 \paragraph{\product{dyncall} support}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
33 The \product{dyncall} library supports the ARM 64-bit AArch64 PCS ABI, as well as Apple's convention derived from it, for calls and callbacks.
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 \subsubsection{AAPCS64 Calling Convention}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 \paragraph{Registers and register usage}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
39 ARM64 features thirty-one 64 bit general purpose registers, namely {\bf r0-r30},
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
40 which are referred to as either {\bf x0-x30} for 64bit access, or {\bf w0-w30}
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
41 for 32bit access (with upper bits either cleared or sign extended on load).\\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
42 Also, there is {\bf sp/xzr/wzr}, a register with restricted use, used for the
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
43 stack pointer in instructions dealing with the stack ({\bf sp}) or a hardware
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
44 zero register for all other instructions {\bf xzr/wzr}, and {\bf pc}, the
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
45 program counter. Additionally, there are thirty-two 128 bit registers {\bf v0-v31},
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
46 to be used as SIMD and floating point registers, referred to as {\bf q0-q31}, {\bf d0-d31}
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
47 and {\bf s0-s31}, respectively, depending on their use:\\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 \begin{table}[h]
77
e441ef3ec782 - manual layout tweaks
cslag
parents: 76
diff changeset
50 \begin{tabular*}{0.95\textwidth}{3 B}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 Name & Brief description\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 \hline
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 {\bf x0-x7} & parameters, scratch, return value\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 {\bf x8} & indirect result location pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 {\bf x9-x15} & scratch\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 {\bf x16} & permanent in some cases, can have special function (IP0), see doc\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 {\bf x17} & permanent in some cases, can have special function (IP1), see doc\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 {\bf x18} & reserved as platform register, advised not to be used for handwritten, portable asm, see doc \\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 {\bf x19-x28} & permanent\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 {\bf x29} & permanent, frame pointer\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 {\bf x30} & permanent, link register\\
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
62 {\bf sp} & permanent, stack pointer\\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
63 {\bf pc} & program counter\\
76
7ca46969e0ad - tweaks in manual, mainly for html generation
cslag
parents: 68
diff changeset
64 \end{tabular*}
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 \caption{Register usage on arm64}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 \end{table}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 \paragraph{Parameter passing}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 \item stack parameter order: right-to-left
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 \item caller cleans up the stack
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 \item first 8 integer arguments are passed using x0-x7
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 \item first 8 floating point arguments are passed using d0-d7
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 \item subsequent parameters are pushed onto the stack
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
76 \item if the callee takes the address of one of the parameters and uses it to address other parameters (e.g. varargs) it has to copy - in its prolog - the first 8 integer and 8 floating-point registers to a reserved stack area adjacent to the other parameters on the stack (only the unnamed integer parameters require saving, though)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 \item structures and unions are passed by value, with the first four words of the parameters in r0-r3
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 \item if return value is a structure, a pointer pointing to the return value's space is passed in r0, the first parameter in r1, etc... (see {\bf return values})
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 \item stack is required to be throughout eight-byte aligned
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 \paragraph{Return values}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 \item integer return values use x0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 \item floating-point return values use d0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 \item otherwise, the caller allocates space, passes pointer to it to the callee through x8, and callee writes return value to this space
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 \paragraph{Stack layout}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
91 % verified/amended: TP nov 2019 (see also doc/disas_examples/arm64.aapcs.disas)
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 Stack directly after function prolog:\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 \begin{figure}[h]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 \begin{tabular}{5|3|1 1}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
96 & \vdots & & \\
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
97 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
98 register save area & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
99 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
100 local data & & & \\
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
101 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
102 \mrlbrace{9}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
103 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
104 & arg 8 & & \\
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
105 \hhline{~=~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
106 & x7 & \mrrbrace{6}{spill area (if needed)} & \mrrbrace{9}{current frame} \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
107 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
108 & x? (first unnamed reg) & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
109 & q7 & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
110 & \ldots & & \\
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
111 & q0 & & \\
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
112 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
113 register save area (with return address) & & & \\ % fp will point here (to 1st arg) @@@ verify
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
114 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
115 local data & & & \\
92
5c3fa8897e0e - manual work
cslag
parents: 90
diff changeset
116 \hhline{~-~~}
328
276eb8c87aa0 - review and fixes, cleanup, amendments to calling convention appendix of manual
Tassilo Philipp
parents: 117
diff changeset
117 parameter area & \vdots & & \\
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 \end{tabular}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 \caption{Stack layout on arm64}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 \end{figure}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 \newpage
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 \subsubsection{Apple's ARM64 Function Calling Conventions}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 \paragraph{Overview}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 Apple's ARM64 calling convention is based on the AAPCS64 standard, however, diverges in some ways.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 Only the differences are listed here, for more details, take a look at Apple's official documentation \cite{AppleARM64}.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 \begin{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 \item arguments passed via stack use only the space they need, but are subject to the type alignment requirements (which is 1 byte for char and bool, 2 for short, 4 for int and 8 for every other type)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 \item caller is required to sign and zero-extend arguments smaller than 32bits
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 \end{itemize}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136