1 /* Target-dependent code for FreeBSD/mips.
3 Copyright (C) 2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "trad-frame.h"
24 #include "tramp-frame.h"
26 #include "fbsd-tdep.h"
27 #include "mips-tdep.h"
28 #include "mips-fbsd-tdep.h"
30 #include "solib-svr4.h"
32 /* Core file support. */
34 /* Number of registers in `struct reg' from <machine/reg.h>. The
35 first 38 follow the standard MIPS layout. The 39th holds
36 IC_INT_REG on RM7K and RM9K processors. The 40th is a dummy for
38 #define MIPS_FBSD_NUM_GREGS 40
40 /* Number of registers in `struct fpreg' from <machine/reg.h>. The
41 first 32 hold floating point registers. 33 holds the FSR. The
42 34th is a dummy for padding. */
43 #define MIPS_FBSD_NUM_FPREGS 34
45 /* Supply a single register. The register size might not match, so use
46 regcache->raw_supply_integer (). */
49 mips_fbsd_supply_reg (struct regcache *regcache, int regnum, const void *addr,
52 regcache->raw_supply_integer (regnum, (const gdb_byte *) addr, len, true);
55 /* Collect a single register. The register size might not match, so use
56 regcache->raw_collect_integer (). */
59 mips_fbsd_collect_reg (const struct regcache *regcache, int regnum, void *addr,
62 regcache->raw_collect_integer (regnum, (gdb_byte *) addr, len, true);
65 /* Supply the floating-point registers stored in FPREGS to REGCACHE.
66 Each floating-point register in FPREGS is REGSIZE bytes in
70 mips_fbsd_supply_fpregs (struct regcache *regcache, int regnum,
71 const void *fpregs, size_t regsize)
73 struct gdbarch *gdbarch = get_regcache_arch (regcache);
74 const gdb_byte *regs = (const gdb_byte *) fpregs;
75 int i, fp0num, fsrnum;
77 fp0num = mips_regnum (gdbarch)->fp0;
78 fsrnum = mips_regnum (gdbarch)->fp_control_status;
79 for (i = fp0num; i <= fsrnum; i++)
80 if (regnum == i || regnum == -1)
81 mips_fbsd_supply_reg (regcache, i,
82 regs + (i - fp0num) * regsize, regsize);
85 /* Supply the general-purpose registers stored in GREGS to REGCACHE.
86 Each general-purpose register in GREGS is REGSIZE bytes in
90 mips_fbsd_supply_gregs (struct regcache *regcache, int regnum,
91 const void *gregs, size_t regsize)
93 struct gdbarch *gdbarch = get_regcache_arch (regcache);
94 const gdb_byte *regs = (const gdb_byte *) gregs;
97 for (i = 0; i <= mips_regnum (gdbarch)->pc; i++)
98 if (regnum == i || regnum == -1)
99 mips_fbsd_supply_reg (regcache, i, regs + i * regsize, regsize);
102 /* Collect the floating-point registers from REGCACHE and store them
103 in FPREGS. Each floating-point register in FPREGS is REGSIZE bytes
107 mips_fbsd_collect_fpregs (const struct regcache *regcache, int regnum,
108 void *fpregs, size_t regsize)
110 struct gdbarch *gdbarch = get_regcache_arch (regcache);
111 gdb_byte *regs = (gdb_byte *) fpregs;
112 int i, fp0num, fsrnum;
114 fp0num = mips_regnum (gdbarch)->fp0;
115 fsrnum = mips_regnum (gdbarch)->fp_control_status;
116 for (i = fp0num; i <= fsrnum; i++)
117 if (regnum == i || regnum == -1)
118 mips_fbsd_collect_reg (regcache, i,
119 regs + (i - fp0num) * regsize, regsize);
122 /* Collect the general-purpose registers from REGCACHE and store them
123 in GREGS. Each general-purpose register in GREGS is REGSIZE bytes
127 mips_fbsd_collect_gregs (const struct regcache *regcache, int regnum,
128 void *gregs, size_t regsize)
130 struct gdbarch *gdbarch = get_regcache_arch (regcache);
131 gdb_byte *regs = (gdb_byte *) gregs;
134 for (i = 0; i <= mips_regnum (gdbarch)->pc; i++)
135 if (regnum == i || regnum == -1)
136 mips_fbsd_collect_reg (regcache, i, regs + i * regsize, regsize);
139 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
140 in the floating-point register set REGSET to register cache
141 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
144 mips_fbsd_supply_fpregset (const struct regset *regset,
145 struct regcache *regcache,
146 int regnum, const void *fpregs, size_t len)
148 size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
150 gdb_assert (len >= MIPS_FBSD_NUM_FPREGS * regsize);
152 mips_fbsd_supply_fpregs (regcache, regnum, fpregs, regsize);
155 /* Collect register REGNUM from the register cache REGCACHE and store
156 it in the buffer specified by FPREGS and LEN in the floating-point
157 register set REGSET. If REGNUM is -1, do this for all registers in
161 mips_fbsd_collect_fpregset (const struct regset *regset,
162 const struct regcache *regcache,
163 int regnum, void *fpregs, size_t len)
165 size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
167 gdb_assert (len >= MIPS_FBSD_NUM_FPREGS * regsize);
169 mips_fbsd_collect_fpregs (regcache, regnum, fpregs, regsize);
172 /* Supply register REGNUM from the buffer specified by GREGS and LEN
173 in the general-purpose register set REGSET to register cache
174 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
177 mips_fbsd_supply_gregset (const struct regset *regset,
178 struct regcache *regcache, int regnum,
179 const void *gregs, size_t len)
181 size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
183 gdb_assert (len >= MIPS_FBSD_NUM_GREGS * regsize);
185 mips_fbsd_supply_gregs (regcache, regnum, gregs, regsize);
188 /* Collect register REGNUM from the register cache REGCACHE and store
189 it in the buffer specified by GREGS and LEN in the general-purpose
190 register set REGSET. If REGNUM is -1, do this for all registers in
194 mips_fbsd_collect_gregset (const struct regset *regset,
195 const struct regcache *regcache,
196 int regnum, void *gregs, size_t len)
198 size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
200 gdb_assert (len >= MIPS_FBSD_NUM_GREGS * regsize);
202 mips_fbsd_collect_gregs (regcache, regnum, gregs, regsize);
205 /* FreeBSD/mips register sets. */
207 static const struct regset mips_fbsd_gregset =
210 mips_fbsd_supply_gregset,
211 mips_fbsd_collect_gregset,
214 static const struct regset mips_fbsd_fpregset =
217 mips_fbsd_supply_fpregset,
218 mips_fbsd_collect_fpregset,
221 /* Iterate over core file register note sections. */
224 mips_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
225 iterate_over_regset_sections_cb *cb,
227 const struct regcache *regcache)
229 size_t regsize = mips_abi_regsize (gdbarch);
231 cb (".reg", MIPS_FBSD_NUM_GREGS * regsize, &mips_fbsd_gregset,
233 cb (".reg2", MIPS_FBSD_NUM_FPREGS * regsize, &mips_fbsd_fpregset,
237 /* Signal trampoline support. */
239 #define FBSD_SYS_sigreturn 417
241 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + FBSD_SYS_sigreturn
242 #define MIPS_INST_SYSCALL 0x0000000c
243 #define MIPS_INST_BREAK 0x0000000d
245 #define O32_SIGFRAME_UCONTEXT_OFFSET (16)
246 #define O32_SIGSET_T_SIZE (16)
248 #define O32_UCONTEXT_ONSTACK (O32_SIGSET_T_SIZE)
249 #define O32_UCONTEXT_PC (O32_UCONTEXT_ONSTACK + 4)
250 #define O32_UCONTEXT_REGS (O32_UCONTEXT_PC + 4)
251 #define O32_UCONTEXT_SR (O32_UCONTEXT_REGS + 4 * 32)
252 #define O32_UCONTEXT_LO (O32_UCONTEXT_SR + 4)
253 #define O32_UCONTEXT_HI (O32_UCONTEXT_LO + 4)
254 #define O32_UCONTEXT_FPUSED (O32_UCONTEXT_HI + 4)
255 #define O32_UCONTEXT_FPREGS (O32_UCONTEXT_FPUSED + 4)
257 #define O32_UCONTEXT_REG_SIZE 4
260 mips_fbsd_sigframe_init (const struct tramp_frame *self,
261 struct frame_info *this_frame,
262 struct trad_frame_cache *cache,
265 struct gdbarch *gdbarch = get_frame_arch (this_frame);
266 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
267 CORE_ADDR sp, ucontext_addr, addr;
271 /* We find the appropriate instance of `ucontext_t' at a
272 fixed offset in the signal frame. */
273 sp = get_frame_register_signed (this_frame,
274 MIPS_SP_REGNUM + gdbarch_num_regs (gdbarch));
275 ucontext_addr = sp + O32_SIGFRAME_UCONTEXT_OFFSET;
278 regnum = mips_regnum (gdbarch)->pc;
279 trad_frame_set_reg_addr (cache,
280 regnum + gdbarch_num_regs (gdbarch),
281 ucontext_addr + O32_UCONTEXT_PC);
284 for (regnum = MIPS_ZERO_REGNUM, addr = ucontext_addr + O32_UCONTEXT_REGS;
285 regnum <= MIPS_RA_REGNUM; regnum++, addr += O32_UCONTEXT_REG_SIZE)
286 trad_frame_set_reg_addr (cache,
287 regnum + gdbarch_num_regs (gdbarch),
290 regnum = MIPS_PS_REGNUM;
291 trad_frame_set_reg_addr (cache,
292 regnum + gdbarch_num_regs (gdbarch),
293 ucontext_addr + O32_UCONTEXT_SR);
296 regnum = mips_regnum (gdbarch)->lo;
297 trad_frame_set_reg_addr (cache,
298 regnum + gdbarch_num_regs (gdbarch),
299 ucontext_addr + O32_UCONTEXT_LO);
300 regnum = mips_regnum (gdbarch)->hi;
301 trad_frame_set_reg_addr (cache,
302 regnum + gdbarch_num_regs (gdbarch),
303 ucontext_addr + O32_UCONTEXT_HI);
305 if (target_read_memory (ucontext_addr + O32_UCONTEXT_FPUSED, buf, 4) == 0
306 && extract_unsigned_integer (buf, 4, byte_order) != 0)
308 for (regnum = 0, addr = ucontext_addr + O32_UCONTEXT_FPREGS;
309 regnum < 32; regnum++, addr += O32_UCONTEXT_REG_SIZE)
310 trad_frame_set_reg_addr (cache,
311 regnum + gdbarch_fp0_regnum (gdbarch),
313 trad_frame_set_reg_addr (cache, mips_regnum (gdbarch)->fp_control_status,
317 trad_frame_set_id (cache, frame_id_build (sp, func));
320 #define MIPS_INST_ADDIU_A0_SP_O32 (0x27a40000 \
321 + O32_SIGFRAME_UCONTEXT_OFFSET)
323 static const struct tramp_frame mips_fbsd_sigframe =
328 { MIPS_INST_ADDIU_A0_SP_O32, -1 }, /* addiu a0, sp, SIGF_UC */
329 { MIPS_INST_LI_V0_SIGRETURN, -1 }, /* li v0, SYS_sigreturn */
330 { MIPS_INST_SYSCALL, -1 }, /* syscall */
331 { MIPS_INST_BREAK, -1 }, /* break */
332 { TRAMP_SENTINEL_INSN, -1 }
334 mips_fbsd_sigframe_init
337 #define N64_SIGFRAME_UCONTEXT_OFFSET (32)
338 #define N64_SIGSET_T_SIZE (16)
340 #define N64_UCONTEXT_ONSTACK (N64_SIGSET_T_SIZE)
341 #define N64_UCONTEXT_PC (N64_UCONTEXT_ONSTACK + 8)
342 #define N64_UCONTEXT_REGS (N64_UCONTEXT_PC + 8)
343 #define N64_UCONTEXT_SR (N64_UCONTEXT_REGS + 8 * 32)
344 #define N64_UCONTEXT_LO (N64_UCONTEXT_SR + 8)
345 #define N64_UCONTEXT_HI (N64_UCONTEXT_LO + 8)
346 #define N64_UCONTEXT_FPUSED (N64_UCONTEXT_HI + 8)
347 #define N64_UCONTEXT_FPREGS (N64_UCONTEXT_FPUSED + 8)
349 #define N64_UCONTEXT_REG_SIZE 8
352 mips64_fbsd_sigframe_init (const struct tramp_frame *self,
353 struct frame_info *this_frame,
354 struct trad_frame_cache *cache,
357 struct gdbarch *gdbarch = get_frame_arch (this_frame);
358 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
359 CORE_ADDR sp, ucontext_addr, addr;
363 /* We find the appropriate instance of `ucontext_t' at a
364 fixed offset in the signal frame. */
365 sp = get_frame_register_signed (this_frame,
366 MIPS_SP_REGNUM + gdbarch_num_regs (gdbarch));
367 ucontext_addr = sp + N64_SIGFRAME_UCONTEXT_OFFSET;
370 regnum = mips_regnum (gdbarch)->pc;
371 trad_frame_set_reg_addr (cache,
372 regnum + gdbarch_num_regs (gdbarch),
373 ucontext_addr + N64_UCONTEXT_PC);
376 for (regnum = MIPS_ZERO_REGNUM, addr = ucontext_addr + N64_UCONTEXT_REGS;
377 regnum <= MIPS_RA_REGNUM; regnum++, addr += N64_UCONTEXT_REG_SIZE)
378 trad_frame_set_reg_addr (cache,
379 regnum + gdbarch_num_regs (gdbarch),
382 regnum = MIPS_PS_REGNUM;
383 trad_frame_set_reg_addr (cache,
384 regnum + gdbarch_num_regs (gdbarch),
385 ucontext_addr + N64_UCONTEXT_SR);
388 regnum = mips_regnum (gdbarch)->lo;
389 trad_frame_set_reg_addr (cache,
390 regnum + gdbarch_num_regs (gdbarch),
391 ucontext_addr + N64_UCONTEXT_LO);
392 regnum = mips_regnum (gdbarch)->hi;
393 trad_frame_set_reg_addr (cache,
394 regnum + gdbarch_num_regs (gdbarch),
395 ucontext_addr + N64_UCONTEXT_HI);
397 if (target_read_memory (ucontext_addr + N64_UCONTEXT_FPUSED, buf, 4) == 0
398 && extract_unsigned_integer (buf, 4, byte_order) != 0)
400 for (regnum = 0, addr = ucontext_addr + N64_UCONTEXT_FPREGS;
401 regnum < 32; regnum++, addr += N64_UCONTEXT_REG_SIZE)
402 trad_frame_set_reg_addr (cache,
403 regnum + gdbarch_fp0_regnum (gdbarch),
405 trad_frame_set_reg_addr (cache, mips_regnum (gdbarch)->fp_control_status,
409 trad_frame_set_id (cache, frame_id_build (sp, func));
412 #define MIPS_INST_DADDIU_A0_SP_N64 (0x67a40000 \
413 + N64_SIGFRAME_UCONTEXT_OFFSET)
415 static const struct tramp_frame mips64_fbsd_sigframe =
420 { MIPS_INST_DADDIU_A0_SP_N64, -1 }, /* daddiu a0, sp, SIGF_UC */
421 { MIPS_INST_LI_V0_SIGRETURN, -1 }, /* li v0, SYS_sigreturn */
422 { MIPS_INST_SYSCALL, -1 }, /* syscall */
423 { MIPS_INST_BREAK, -1 }, /* break */
424 { TRAMP_SENTINEL_INSN, -1 }
426 mips64_fbsd_sigframe_init
429 /* Shared library support. */
431 /* FreeBSD/mips uses a slightly different `struct link_map' than the
432 other FreeBSD platforms as it includes an additional `l_off'
435 static struct link_map_offsets *
436 mips_fbsd_ilp32_fetch_link_map_offsets (void)
438 static struct link_map_offsets lmo;
439 static struct link_map_offsets *lmp = NULL;
445 lmo.r_version_offset = 0;
446 lmo.r_version_size = 4;
447 lmo.r_map_offset = 4;
448 lmo.r_brk_offset = 8;
449 lmo.r_ldsomap_offset = -1;
451 lmo.link_map_size = 24;
452 lmo.l_addr_offset = 0;
453 lmo.l_name_offset = 8;
454 lmo.l_ld_offset = 12;
455 lmo.l_next_offset = 16;
456 lmo.l_prev_offset = 20;
462 static struct link_map_offsets *
463 mips_fbsd_lp64_fetch_link_map_offsets (void)
465 static struct link_map_offsets lmo;
466 static struct link_map_offsets *lmp = NULL;
472 lmo.r_version_offset = 0;
473 lmo.r_version_size = 4;
474 lmo.r_map_offset = 8;
475 lmo.r_brk_offset = 16;
476 lmo.r_ldsomap_offset = -1;
478 lmo.link_map_size = 48;
479 lmo.l_addr_offset = 0;
480 lmo.l_name_offset = 16;
481 lmo.l_ld_offset = 24;
482 lmo.l_next_offset = 32;
483 lmo.l_prev_offset = 40;
490 mips_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
492 enum mips_abi abi = mips_abi (gdbarch);
494 /* Generic FreeBSD support. */
495 fbsd_init_abi (info, gdbarch);
497 set_gdbarch_software_single_step (gdbarch, mips_software_single_step);
502 tramp_frame_prepend_unwinder (gdbarch, &mips_fbsd_sigframe);
507 tramp_frame_prepend_unwinder (gdbarch, &mips64_fbsd_sigframe);
511 set_gdbarch_iterate_over_regset_sections
512 (gdbarch, mips_fbsd_iterate_over_regset_sections);
514 /* FreeBSD/mips has SVR4-style shared libraries. */
515 set_solib_svr4_fetch_link_map_offsets
516 (gdbarch, (gdbarch_ptr_bit (gdbarch) == 32 ?
517 mips_fbsd_ilp32_fetch_link_map_offsets :
518 mips_fbsd_lp64_fetch_link_map_offsets));
522 /* Provide a prototype to silence -Wmissing-prototypes. */
523 void _initialize_mips_fbsd_tdep (void);
526 _initialize_mips_fbsd_tdep (void)
528 gdbarch_register_osabi (bfd_arch_mips, 0, GDB_OSABI_FREEBSD,