]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgcc/config/aarch64/linux-unwind.h
Update copyright years.
[thirdparty/gcc.git] / libgcc / config / aarch64 / linux-unwind.h
1 /* Copyright (C) 2009-2024 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
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 static _Unwind_Reason_Code
47 aarch64_fallback_frame_state (struct _Unwind_Context *context,
48 _Unwind_FrameState * fs)
49 {
50 /* The kernel creates an rt_sigframe on the stack immediately prior
51 to delivering a signal.
52
53 This structure must have the same shape as the linux kernel
54 equivalent. */
55 struct rt_sigframe
56 {
57 siginfo_t info;
58 ucontext_t uc;
59 };
60
61 struct rt_sigframe *rt_;
62 _Unwind_Ptr new_cfa;
63 unsigned *pc = context->ra;
64 struct sigcontext *sc;
65 struct _aarch64_ctx *extension_marker;
66 int i;
67
68 /* A signal frame will have a return address pointing to
69 __default_sa_restorer. This code is hardwired as:
70
71 0xd2801168 movz x8, #0x8b
72 0xd4000001 svc 0x0
73 */
74 if (pc[0] != MOVZ_X8_8B || pc[1] != SVC_0)
75 {
76 return _URC_END_OF_STACK;
77 }
78
79 rt_ = context->cfa;
80 /* Historically, the uc_mcontext member was of type struct sigcontext, but
81 glibc uses a different type now with member names in the implementation
82 namespace. */
83 sc = (struct sigcontext *) &rt_->uc.uc_mcontext;
84
85 /* This define duplicates the definition in aarch64.md */
86 #define SP_REGNUM 31
87
88 new_cfa = (_Unwind_Ptr) sc;
89 fs->regs.cfa_how = CFA_REG_OFFSET;
90 fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
91 fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
92
93 for (i = 0; i < AARCH64_DWARF_NUMBER_R; i++)
94 {
95 fs->regs.how[AARCH64_DWARF_R0 + i] = REG_SAVED_OFFSET;
96 fs->regs.reg[AARCH64_DWARF_R0 + i].loc.offset =
97 (_Unwind_Ptr) & (sc->regs[i]) - new_cfa;
98 }
99
100 /* The core context may be extended with an arbitrary set of
101 additional contexts appended sequentially. Each additional
102 context contains a magic identifier and size in bytes. The size
103 field can be used to skip over unrecognized context extensions.
104 The end of the context sequence is marked by a context with magic
105 0 or size 0. */
106 for (extension_marker = (struct _aarch64_ctx *) &sc->__reserved;
107 extension_marker->magic;
108 extension_marker = (struct _aarch64_ctx *)
109 ((unsigned char *) extension_marker + extension_marker->size))
110 {
111 if (extension_marker->magic == FPSIMD_MAGIC)
112 {
113 struct fpsimd_context *ctx =
114 (struct fpsimd_context *) extension_marker;
115 int i;
116
117 for (i = 0; i < AARCH64_DWARF_NUMBER_V; i++)
118 {
119 _Unwind_Sword offset;
120
121 fs->regs.how[AARCH64_DWARF_V0 + i] = REG_SAVED_OFFSET;
122
123 /* sigcontext contains 32 128bit registers for V0 to
124 V31. The kernel will have saved the contents of the
125 V registers. We want to unwind the callee save D
126 registers. Each D register comprises the least
127 significant half of the corresponding V register. We
128 need to offset into the saved V register dependent on
129 our endianness to find the saved D register. */
130
131 offset = (_Unwind_Ptr) & (ctx->vregs[i]) - new_cfa;
132
133 /* The endianness adjustment code below expects that a
134 saved V register is 16 bytes. */
135 gcc_assert (sizeof (ctx->vregs[0]) == 16);
136 #if defined (__AARCH64EB__)
137 offset = offset + 8;
138 #endif
139 fs->regs.reg[AARCH64_DWARF_V0 + i].loc.offset = offset;
140 }
141 }
142 else
143 {
144 /* There is context provided that we do not recognize! */
145 }
146 }
147
148 fs->regs.how[31] = REG_SAVED_OFFSET;
149 fs->regs.reg[31].loc.offset = (_Unwind_Ptr) & (sc->sp) - new_cfa;
150
151 fs->signal_frame = 1;
152
153 fs->regs.how[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__] =
154 REG_SAVED_VAL_OFFSET;
155 fs->regs.reg[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__].loc.offset =
156 (_Unwind_Ptr) (sc->pc) - new_cfa;
157
158 fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__;
159
160 return _URC_NO_REASON;
161 }
162
163 #endif