Automatic date update in version.in
[binutils-gdb.git] / gdb / aarch64-fbsd-tdep.c
1 /* Target-dependent code for FreeBSD/aarch64.
2
3 Copyright (C) 2017-2022 Free Software Foundation, Inc.
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
22 #include "gdbarch.h"
23 #include "fbsd-tdep.h"
24 #include "aarch64-tdep.h"
25 #include "aarch64-fbsd-tdep.h"
26 #include "inferior.h"
27 #include "osabi.h"
28 #include "solib-svr4.h"
29 #include "target.h"
30 #include "tramp-frame.h"
31 #include "trad-frame.h"
32
33 /* Register maps. */
34
35 static 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
45 static 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
53 /* In a signal frame, sp points to a 'struct sigframe' which is
54 defined as:
55
56 struct sigframe {
57 siginfo_t sf_si;
58 ucontext_t sf_uc;
59 };
60
61 ucontext_t is defined as:
62
63 struct __ucontext {
64 sigset_t uc_sigmask;
65 mcontext_t uc_mcontext;
66 ...
67 };
68
69 The mcontext_t contains the general purpose register set followed
70 by the floating point register set. The floating point register
71 set is only valid if the _MC_FP_VALID flag is set in mc_flags. */
72
73 #define AARCH64_SIGFRAME_UCONTEXT_OFFSET 80
74 #define AARCH64_UCONTEXT_MCONTEXT_OFFSET 16
75 #define AARCH64_MCONTEXT_FPREGS_OFFSET 272
76 #define AARCH64_MCONTEXT_FLAGS_OFFSET 800
77 #define AARCH64_MCONTEXT_FLAG_FP_VALID 0x1
78
79 /* Implement the "init" method of struct tramp_frame. */
80
81 static void
82 aarch64_fbsd_sigframe_init (const struct tramp_frame *self,
83 struct frame_info *this_frame,
84 struct trad_frame_cache *this_cache,
85 CORE_ADDR func)
86 {
87 struct gdbarch *gdbarch = get_frame_arch (this_frame);
88 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
89 CORE_ADDR sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
90 CORE_ADDR mcontext_addr
91 = (sp
92 + AARCH64_SIGFRAME_UCONTEXT_OFFSET
93 + AARCH64_UCONTEXT_MCONTEXT_OFFSET);
94 gdb_byte buf[4];
95
96 trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_gregmap, mcontext_addr,
97 regcache_map_entry_size (aarch64_fbsd_gregmap));
98
99 if (target_read_memory (mcontext_addr + AARCH64_MCONTEXT_FLAGS_OFFSET, buf,
100 4) == 0
101 && (extract_unsigned_integer (buf, 4, byte_order)
102 & AARCH64_MCONTEXT_FLAG_FP_VALID))
103 trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_fpregmap,
104 mcontext_addr + AARCH64_MCONTEXT_FPREGS_OFFSET,
105 regcache_map_entry_size (aarch64_fbsd_fpregmap));
106
107 trad_frame_set_id (this_cache, frame_id_build (sp, func));
108 }
109
110 static const struct tramp_frame aarch64_fbsd_sigframe =
111 {
112 SIGTRAMP_FRAME,
113 4,
114 {
115 {0x910003e0, ULONGEST_MAX}, /* mov x0, sp */
116 {0x91014000, ULONGEST_MAX}, /* add x0, x0, #SF_UC */
117 {0xd2803428, ULONGEST_MAX}, /* mov x8, #SYS_sigreturn */
118 {0xd4000001, ULONGEST_MAX}, /* svc 0x0 */
119 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
120 },
121 aarch64_fbsd_sigframe_init
122 };
123
124 /* Register set definitions. */
125
126 const struct regset aarch64_fbsd_gregset =
127 {
128 aarch64_fbsd_gregmap,
129 regcache_supply_regset, regcache_collect_regset
130 };
131
132 const struct regset aarch64_fbsd_fpregset =
133 {
134 aarch64_fbsd_fpregmap,
135 regcache_supply_regset, regcache_collect_regset
136 };
137
138 /* Implement the "iterate_over_regset_sections" gdbarch method. */
139
140 static void
141 aarch64_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
142 iterate_over_regset_sections_cb *cb,
143 void *cb_data,
144 const struct regcache *regcache)
145 {
146 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
147
148 cb (".reg", AARCH64_FBSD_SIZEOF_GREGSET, AARCH64_FBSD_SIZEOF_GREGSET,
149 &aarch64_fbsd_gregset, NULL, cb_data);
150 cb (".reg2", AARCH64_FBSD_SIZEOF_FPREGSET, AARCH64_FBSD_SIZEOF_FPREGSET,
151 &aarch64_fbsd_fpregset, NULL, cb_data);
152
153 if (tdep->has_tls ())
154 {
155 const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
156 {
157 { 1, tdep->tls_regnum, 8 },
158 { 0 }
159 };
160
161 const struct regset aarch64_fbsd_tls_regset =
162 {
163 aarch64_fbsd_tls_regmap,
164 regcache_supply_regset, regcache_collect_regset
165 };
166
167 cb (".reg-aarch-tls", AARCH64_FBSD_SIZEOF_TLSREGSET,
168 AARCH64_FBSD_SIZEOF_TLSREGSET, &aarch64_fbsd_tls_regset,
169 "TLS register", cb_data);
170 }
171 }
172
173 /* Implement the "core_read_description" gdbarch method. */
174
175 static const struct target_desc *
176 aarch64_fbsd_core_read_description (struct gdbarch *gdbarch,
177 struct target_ops *target, bfd *abfd)
178 {
179 asection *tls = bfd_get_section_by_name (abfd, ".reg-aarch-tls");
180
181 aarch64_features features;
182 features.tls = tls != nullptr;
183
184 return aarch64_read_description (features);
185 }
186
187 /* Implement the get_thread_local_address gdbarch method. */
188
189 static CORE_ADDR
190 aarch64_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
191 CORE_ADDR lm_addr, CORE_ADDR offset)
192 {
193 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
194 struct regcache *regcache;
195
196 regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
197 ptid, gdbarch);
198
199 target_fetch_registers (regcache, tdep->tls_regnum);
200
201 ULONGEST tpidr;
202 if (regcache->cooked_read (tdep->tls_regnum, &tpidr) != REG_VALID)
203 error (_("Unable to fetch %%tpidr"));
204
205 /* %tpidr points to the TCB whose first member is the dtv
206 pointer. */
207 CORE_ADDR dtv_addr = tpidr;
208 return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
209 }
210
211 /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
212
213 static void
214 aarch64_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
215 {
216 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
217
218 /* Generic FreeBSD support. */
219 fbsd_init_abi (info, gdbarch);
220
221 set_solib_svr4_fetch_link_map_offsets (gdbarch,
222 svr4_lp64_fetch_link_map_offsets);
223
224 tramp_frame_prepend_unwinder (gdbarch, &aarch64_fbsd_sigframe);
225
226 /* Enable longjmp. */
227 tdep->jb_pc = 13;
228
229 set_gdbarch_iterate_over_regset_sections
230 (gdbarch, aarch64_fbsd_iterate_over_regset_sections);
231 set_gdbarch_core_read_description (gdbarch,
232 aarch64_fbsd_core_read_description);
233
234 if (tdep->has_tls ())
235 {
236 set_gdbarch_fetch_tls_load_module_address (gdbarch,
237 svr4_fetch_objfile_link_map);
238 set_gdbarch_get_thread_local_address
239 (gdbarch, aarch64_fbsd_get_thread_local_address);
240 }
241 }
242
243 void _initialize_aarch64_fbsd_tdep ();
244 void
245 _initialize_aarch64_fbsd_tdep ()
246 {
247 gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_FREEBSD,
248 aarch64_fbsd_init_abi);
249 }