Debian Codegraph @

libgcc/config/aarch64/linux-unwind.h

raw
1 /* Copyright (C) 2009-2025 Free Software Foundation, Inc.
2 Contributed by ARM Ltd.
3
4 This file is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
8
9 This file is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 Under Section 7 of GPL version 3, you are granted additional
15 permissions described in the GCC Runtime Library Exception, version
16 3.1, as published by the Free Software Foundation.
17
18 You should have received a copy of the GNU General Public License and
19 a copy of the GCC Runtime Library Exception along with this program;
20 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 <http://www.gnu.org/licenses/>. */
22
23 /* Always include AArch64 unwinder header file. */
24 #include "config/aarch64/aarch64-unwind.h"
25
26 #ifndef inhibit_libc
27
28 #include <signal.h>
29 #include <sys/ucontext.h>
30 #include <stdint.h>
31
32 /* Since insns are always stored LE, on a BE system the opcodes will
33 be loaded byte-reversed. Therefore, define two sets of opcodes,
34 one for LE and one for BE. */
35
36 #if __AARCH64EB__
37 #define MOVZ_X8_8B 0x681180d2
38 #define SVC_0 0x010000d4
39 #else
40 #define MOVZ_X8_8B 0xd2801168
41 #define SVC_0 0xd4000001
42 #endif
43
44 #define MD_FALLBACK_FRAME_STATE_FOR aarch64_fallback_frame_state
45
46 #ifndef FPSIMD_MAGIC
47 #define FPSIMD_MAGIC 0x46508001
48 #endif
49
50 #ifndef TPIDR2_MAGIC
51 #define TPIDR2_MAGIC 0x54504902
52 #endif
53
54 #ifndef ZA_MAGIC
55 #define ZA_MAGIC 0x54366345
56 #endif
57
58 #ifndef EXTRA_MAGIC
59 #define EXTRA_MAGIC 0x45585401
60 #endif
61
62 static _Unwind_Reason_Code
63 aarch64_fallback_frame_state (struct _Unwind_Context *context,
64 _Unwind_FrameState * fs)
65 {
66 /* The kernel creates an rt_sigframe on the stack immediately prior
67 to delivering a signal.
68
69 This structure must have the same shape as the linux kernel
70 equivalent. */
71 struct rt_sigframe
72 {
73 siginfo_t info;
74 ucontext_t uc;
75 };
76
77 struct tpidr2_block
78 {
79 uint64_t za_save_buffer;
80 uint16_t num_za_save_slices;
81 uint8_t reserved[6];
82 };
83
84 struct za_block
85 {
86 struct _aarch64_ctx head;
87 uint16_t vl;
88 uint16_t reserved[3];
89 uint64_t data;
90 };
91
92 struct rt_sigframe *rt_;
93 _Unwind_Ptr new_cfa;
94 unsigned *pc = context->ra;
95 struct sigcontext *sc;
96 struct _aarch64_ctx *extension_marker;
97 int i;
98
99 /* A signal frame will have a return address pointing to
100 __default_sa_restorer. This code is hardwired as:
101
102 0xd2801168 movz x8, #0x8b
103 0xd4000001 svc 0x0
104 */
105 if (pc[0] != MOVZ_X8_8B || pc[1] != SVC_0)
106 {
107 return _URC_END_OF_STACK;
108 }
109
110 rt_ = context->cfa;
111 /* Historically, the uc_mcontext member was of type struct sigcontext, but
112 glibc uses a different type now with member names in the implementation
113 namespace. */
114 sc = (struct sigcontext *) &rt_->uc.uc_mcontext;
115
116 /* This define duplicates the definition in aarch64.md */
117 #define SP_REGNUM 31
118
119 new_cfa = (_Unwind_Ptr) sc;
120 fs->regs.cfa_how = CFA_REG_OFFSET;
121 fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
122 fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
123
124 for (i = 0; i < AARCH64_DWARF_NUMBER_R; i++)
125 {
126 fs->regs.how[AARCH64_DWARF_R0 + i] = REG_SAVED_OFFSET;
127 fs->regs.reg[AARCH64_DWARF_R0 + i].loc.offset =
128 (_Unwind_Ptr) & (sc->regs[i]) - new_cfa;
129 }
130
131 /* The core context may be extended with an arbitrary set of
132 additional contexts appended sequentially. Each additional
133 context contains a magic identifier and size in bytes. The size
134 field can be used to skip over unrecognized context extensions.
135 The end of the context sequence is marked by a context with magic
136 0 or size 0. */
137 struct tpidr2_block *tpidr2 = 0;
138 struct za_block *za_ctx = 0;
139
140 for (extension_marker = (struct _aarch64_ctx *) &sc->__reserved;
141 extension_marker->magic;
142 extension_marker = (struct _aarch64_ctx *)
143 ((unsigned char *) extension_marker + extension_marker->size))
144 {
145 restart:
146 if (extension_marker->magic == FPSIMD_MAGIC)
147 {
148 struct fpsimd_context *ctx =
149 (struct fpsimd_context *) extension_marker;
150 int i;
151
152 for (i = 0; i < AARCH64_DWARF_NUMBER_V; i++)
153 {
154 _Unwind_Sword offset;
155
156 fs->regs.how[AARCH64_DWARF_V0 + i] = REG_SAVED_OFFSET;
157
158 /* sigcontext contains 32 128bit registers for V0 to
159 V31. The kernel will have saved the contents of the
160 V registers. We want to unwind the callee save D
161 registers. Each D register comprises the least
162 significant half of the corresponding V register. We
163 need to offset into the saved V register dependent on
164 our endianness to find the saved D register. */
165
166 offset = (_Unwind_Ptr) & (ctx->vregs[i]) - new_cfa;
167
168 /* The endianness adjustment code below expects that a
169 saved V register is 16 bytes. */
170 gcc_assert (sizeof (ctx->vregs[0]) == 16);
171 #if defined (__AARCH64EB__)
172 offset = offset + 8;
173 #endif
174 fs->regs.reg[AARCH64_DWARF_V0 + i].loc.offset = offset;
175 }
176 }
177 else if (extension_marker->magic == TPIDR2_MAGIC)
178 {
179 /* A TPIDR2 context.
180
181 All the casting is to support big-endian ILP32. We could read
182 directly into TPIDR2 otherwise. */
183 struct { struct _aarch64_ctx h; uint64_t tpidr2; } *ctx
184 = (void *)extension_marker;
185 #if defined (__ILP32__)
186 tpidr2 = (struct tpidr2_block *) (uintptr_t) ctx->tpidr2;
187 #else
188 tpidr2 = (struct tpidr2_block *) ctx->tpidr2;
189 #endif
190 }
191 else if (extension_marker->magic == ZA_MAGIC)
192 /* A ZA context. We interpret this later. */
193 za_ctx = (void *)extension_marker;
194 else if (extension_marker->magic == EXTRA_MAGIC)
195 {
196 /* Extra context. The ABI guarantees that the next _aarch64_ctx
197 in the current list will be the zero terminator, so we can simply
198 switch to the new list and continue from there. The new list is
199 also zero-terminated.
200
201 As above, the casting is to support big-endian ILP32. */
202 struct { struct _aarch64_ctx h; uint64_t next; } *ctx
203 = (void *)extension_marker;
204 #if defined (__ILP32__)
205 extension_marker = (struct _aarch64_ctx *) (uintptr_t) ctx->next;
206 #else
207 extension_marker = (struct _aarch64_ctx *) ctx->next;
208 #endif
209 goto restart;
210 }
211 else
212 {
213 /* There is context provided that we do not recognize! */
214 }
215 }
216
217 /* Signal handlers are entered with ZA in the off state (TPIDR2_ELO==0 and
218 PSTATE.ZA==0). The normal process when transitioning from ZA being
219 dormant to ZA being off is to commit the lazy save; see the AAPCS64
220 for details. However, this is not done when entering a signal handler.
221 Instead, linux saves the old contents of ZA and TPIDR2_EL0 to the
222 sigcontext without interpreting them further.
223
224 Therefore, if a signal handler throws an exception to code outside the
225 signal handler, the unwinder must commit the lazy save after the fact.
226 Committing a lazy save means:
227
228 (1) Storing the contents of ZA into the buffer provided by TPIDR2_EL0.
229 (2) Setting TPIDR2_EL0 to zero.
230 (3) Turning ZA off.
231
232 (2) and (3) have already been done by the call to __libgcc_arm_za_disable.
233 (1) involves copying data from the ZA sigcontext entry to the
234 corresponding lazy save buffer. */
235 if (tpidr2 && za_ctx && tpidr2->za_save_buffer)
236 {
237 /* There is a 16-bit vector length (measured in bytes) at ZA_CTX + 8.
238 The data itself starts at ZA_CTX + 16.
239 As above, the casting is to support big-endian ILP32. */
240 uint16_t vl = za_ctx->vl;
241 #if defined (__ILP32__)
242 void *save_buffer = (void *) (uintptr_t) tpidr2->za_save_buffer;
243 const void *za_buffer = (void *) (uintptr_t) &za_ctx->data;
244 #else
245 void *save_buffer = (void *) tpidr2->za_save_buffer;
246 const void *za_buffer = (void *) &za_ctx->data;
247 #endif
248 uint64_t num_slices = tpidr2->num_za_save_slices;
249 if (num_slices > vl)
250 num_slices = vl;
251 memcpy (save_buffer, za_buffer, num_slices * vl);
252 }
253
254 fs->regs.how[31] = REG_SAVED_OFFSET;
255 fs->regs.reg[31].loc.offset = (_Unwind_Ptr) & (sc->sp) - new_cfa;
256
257 fs->signal_frame = 1;
258
259 fs->regs.how[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__] =
260 REG_SAVED_VAL_OFFSET;
261 fs->regs.reg[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__].loc.offset =
262 (_Unwind_Ptr) (sc->pc) - new_cfa;
263
264 fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__;
265
266 return _URC_NO_REASON;
267 }
268
269 #endif