]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/aarch64-fbsd-tdep.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / aarch64-fbsd-tdep.c
CommitLineData
c0f84956
JB
1/* Target-dependent code for FreeBSD/aarch64.
2
1d506c26 3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
c0f84956
JB
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21
d55e5aa6 22#include "gdbarch.h"
4de283e4
TT
23#include "fbsd-tdep.h"
24#include "aarch64-tdep.h"
25#include "aarch64-fbsd-tdep.h"
f9fbb763 26#include "inferior.h"
c0f84956
JB
27#include "osabi.h"
28#include "solib-svr4.h"
29#include "target.h"
d55e5aa6 30#include "tramp-frame.h"
4de283e4 31#include "trad-frame.h"
c0f84956 32
7054e2ff
JB
33/* Register maps. */
34
35static const struct regcache_map_entry aarch64_fbsd_gregmap[] =
36 {
37 { 30, AARCH64_X0_REGNUM, 8 }, /* x0 ... x29 */
38 { 1, AARCH64_LR_REGNUM, 8 },
39 { 1, AARCH64_SP_REGNUM, 8 },
40 { 1, AARCH64_PC_REGNUM, 8 },
41 { 1, AARCH64_CPSR_REGNUM, 4 },
42 { 0 }
43 };
44
45static const struct regcache_map_entry aarch64_fbsd_fpregmap[] =
46 {
47 { 32, AARCH64_V0_REGNUM, 16 }, /* v0 ... v31 */
48 { 1, AARCH64_FPSR_REGNUM, 4 },
49 { 1, AARCH64_FPCR_REGNUM, 4 },
50 { 0 }
51 };
52
ba60b963 53/* Register numbers are relative to tdep->tls_regnum_base. */
d818c7ad
JB
54
55static const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
56 {
57 { 1, 0, 8 }, /* tpidr */
58 { 0 }
59 };
60
c0f84956
JB
61/* In a signal frame, sp points to a 'struct sigframe' which is
62 defined as:
63
64 struct sigframe {
65 siginfo_t sf_si;
66 ucontext_t sf_uc;
67 };
68
69 ucontext_t is defined as:
70
71 struct __ucontext {
72 sigset_t uc_sigmask;
73 mcontext_t uc_mcontext;
74 ...
75 };
76
77 The mcontext_t contains the general purpose register set followed
78 by the floating point register set. The floating point register
79 set is only valid if the _MC_FP_VALID flag is set in mc_flags. */
80
c0f84956
JB
81#define AARCH64_SIGFRAME_UCONTEXT_OFFSET 80
82#define AARCH64_UCONTEXT_MCONTEXT_OFFSET 16
83#define AARCH64_MCONTEXT_FPREGS_OFFSET 272
84#define AARCH64_MCONTEXT_FLAGS_OFFSET 800
85#define AARCH64_MCONTEXT_FLAG_FP_VALID 0x1
86
87/* Implement the "init" method of struct tramp_frame. */
88
89static void
90aarch64_fbsd_sigframe_init (const struct tramp_frame *self,
bd2b40ac 91 frame_info_ptr this_frame,
c0f84956
JB
92 struct trad_frame_cache *this_cache,
93 CORE_ADDR func)
94{
95 struct gdbarch *gdbarch = get_frame_arch (this_frame);
96 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
97 CORE_ADDR sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
7054e2ff
JB
98 CORE_ADDR mcontext_addr
99 = (sp
100 + AARCH64_SIGFRAME_UCONTEXT_OFFSET
101 + AARCH64_UCONTEXT_MCONTEXT_OFFSET);
c0f84956 102 gdb_byte buf[4];
7054e2ff
JB
103
104 trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_gregmap, mcontext_addr,
105 regcache_map_entry_size (aarch64_fbsd_gregmap));
c0f84956
JB
106
107 if (target_read_memory (mcontext_addr + AARCH64_MCONTEXT_FLAGS_OFFSET, buf,
108 4) == 0
109 && (extract_unsigned_integer (buf, 4, byte_order)
110 & AARCH64_MCONTEXT_FLAG_FP_VALID))
7054e2ff
JB
111 trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_fpregmap,
112 mcontext_addr + AARCH64_MCONTEXT_FPREGS_OFFSET,
113 regcache_map_entry_size (aarch64_fbsd_fpregmap));
c0f84956
JB
114
115 trad_frame_set_id (this_cache, frame_id_build (sp, func));
116}
117
118static const struct tramp_frame aarch64_fbsd_sigframe =
119{
120 SIGTRAMP_FRAME,
121 4,
122 {
7bc02706
TT
123 {0x910003e0, ULONGEST_MAX}, /* mov x0, sp */
124 {0x91014000, ULONGEST_MAX}, /* add x0, x0, #SF_UC */
125 {0xd2803428, ULONGEST_MAX}, /* mov x8, #SYS_sigreturn */
126 {0xd4000001, ULONGEST_MAX}, /* svc 0x0 */
127 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
c0f84956
JB
128 },
129 aarch64_fbsd_sigframe_init
130};
131
c0f84956
JB
132/* Register set definitions. */
133
134const struct regset aarch64_fbsd_gregset =
135 {
136 aarch64_fbsd_gregmap,
137 regcache_supply_regset, regcache_collect_regset
138 };
139
140const struct regset aarch64_fbsd_fpregset =
141 {
142 aarch64_fbsd_fpregmap,
143 regcache_supply_regset, regcache_collect_regset
144 };
145
d818c7ad
JB
146static void
147aarch64_fbsd_supply_tls_regset (const struct regset *regset,
148 struct regcache *regcache,
149 int regnum, const void *buf, size_t size)
150{
151 struct gdbarch *gdbarch = regcache->arch ();
152 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
153
ba60b963 154 regcache->supply_regset (regset, tdep->tls_regnum_base, regnum, buf, size);
d818c7ad
JB
155}
156
157static void
158aarch64_fbsd_collect_tls_regset (const struct regset *regset,
159 const struct regcache *regcache,
160 int regnum, void *buf, size_t size)
161{
162 struct gdbarch *gdbarch = regcache->arch ();
163 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
164
ba60b963 165 regcache->collect_regset (regset, tdep->tls_regnum_base, regnum, buf, size);
d818c7ad
JB
166}
167
168const struct regset aarch64_fbsd_tls_regset =
169 {
170 aarch64_fbsd_tls_regmap,
171 aarch64_fbsd_supply_tls_regset, aarch64_fbsd_collect_tls_regset
172 };
173
89203d40 174/* Implement the "iterate_over_regset_sections" gdbarch method. */
c0f84956
JB
175
176static void
177aarch64_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
178 iterate_over_regset_sections_cb *cb,
179 void *cb_data,
180 const struct regcache *regcache)
181{
08106042 182 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
0a765c1a 183
a616bb94
AH
184 cb (".reg", AARCH64_FBSD_SIZEOF_GREGSET, AARCH64_FBSD_SIZEOF_GREGSET,
185 &aarch64_fbsd_gregset, NULL, cb_data);
186 cb (".reg2", AARCH64_FBSD_SIZEOF_FPREGSET, AARCH64_FBSD_SIZEOF_FPREGSET,
187 &aarch64_fbsd_fpregset, NULL, cb_data);
0a765c1a
JB
188
189 if (tdep->has_tls ())
d818c7ad
JB
190 cb (".reg-aarch-tls", AARCH64_FBSD_SIZEOF_TLSREGSET,
191 AARCH64_FBSD_SIZEOF_TLSREGSET, &aarch64_fbsd_tls_regset,
192 "TLS register", cb_data);
0a765c1a
JB
193}
194
195/* Implement the "core_read_description" gdbarch method. */
196
197static const struct target_desc *
198aarch64_fbsd_core_read_description (struct gdbarch *gdbarch,
199 struct target_ops *target, bfd *abfd)
200{
201 asection *tls = bfd_get_section_by_name (abfd, ".reg-aarch-tls");
202
0ee6b1c5 203 aarch64_features features;
ba60b963 204 features.tls = tls != nullptr? 1 : 0;
0ee6b1c5
JB
205
206 return aarch64_read_description (features);
c0f84956
JB
207}
208
f9fbb763
JB
209/* Implement the get_thread_local_address gdbarch method. */
210
211static CORE_ADDR
212aarch64_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
213 CORE_ADDR lm_addr, CORE_ADDR offset)
214{
08106042 215 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
f9fbb763
JB
216 struct regcache *regcache;
217
74387712 218 regcache = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
f9fbb763 219
ba60b963 220 target_fetch_registers (regcache, tdep->tls_regnum_base);
f9fbb763
JB
221
222 ULONGEST tpidr;
ba60b963 223 if (regcache->cooked_read (tdep->tls_regnum_base, &tpidr) != REG_VALID)
f9fbb763
JB
224 error (_("Unable to fetch %%tpidr"));
225
226 /* %tpidr points to the TCB whose first member is the dtv
227 pointer. */
228 CORE_ADDR dtv_addr = tpidr;
229 return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
230}
231
c0f84956
JB
232/* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
233
234static void
235aarch64_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
236{
08106042 237 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
c0f84956
JB
238
239 /* Generic FreeBSD support. */
240 fbsd_init_abi (info, gdbarch);
241
242 set_solib_svr4_fetch_link_map_offsets (gdbarch,
243 svr4_lp64_fetch_link_map_offsets);
244
245 tramp_frame_prepend_unwinder (gdbarch, &aarch64_fbsd_sigframe);
246
247 /* Enable longjmp. */
248 tdep->jb_pc = 13;
249
250 set_gdbarch_iterate_over_regset_sections
251 (gdbarch, aarch64_fbsd_iterate_over_regset_sections);
0a765c1a
JB
252 set_gdbarch_core_read_description (gdbarch,
253 aarch64_fbsd_core_read_description);
f9fbb763
JB
254
255 if (tdep->has_tls ())
256 {
257 set_gdbarch_fetch_tls_load_module_address (gdbarch,
258 svr4_fetch_objfile_link_map);
259 set_gdbarch_get_thread_local_address
260 (gdbarch, aarch64_fbsd_get_thread_local_address);
261 }
c0f84956
JB
262}
263
6c265988 264void _initialize_aarch64_fbsd_tdep ();
c0f84956 265void
6c265988 266_initialize_aarch64_fbsd_tdep ()
c0f84956
JB
267{
268 gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_FREEBSD,
269 aarch64_fbsd_init_abi);
270}