Use XCNEW gdbarch_tdep
[external/binutils.git] / gdb / mips-fbsd-tdep.c
1 /* Target-dependent code for FreeBSD/mips.
2
3    Copyright (C) 2017 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 #include "osabi.h"
22 #include "regset.h"
23 #include "trad-frame.h"
24 #include "tramp-frame.h"
25
26 #include "fbsd-tdep.h"
27 #include "mips-tdep.h"
28 #include "mips-fbsd-tdep.h"
29
30 #include "solib-svr4.h"
31
32 /* Core file support. */
33
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
37    padding.  */
38 #define MIPS_FBSD_NUM_GREGS     40
39
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
44
45 /* Supply a single register.  If the source register size matches the
46    size the regcache expects, this can use regcache_raw_supply().  If
47    they are different, this copies the source register into a buffer
48    that can be passed to regcache_raw_supply().  */
49
50 static void
51 mips_fbsd_supply_reg (struct regcache *regcache, int regnum, const void *addr,
52                       size_t len)
53 {
54   struct gdbarch *gdbarch = get_regcache_arch (regcache);
55
56   if (register_size (gdbarch, regnum) == len)
57     regcache_raw_supply (regcache, regnum, addr);
58   else
59     {
60       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
61       gdb_byte buf[MAX_REGISTER_SIZE];
62       LONGEST val;
63
64       val = extract_signed_integer ((const gdb_byte *) addr, len, byte_order);
65       store_signed_integer (buf, register_size (gdbarch, regnum), byte_order,
66                             val);
67       regcache_raw_supply (regcache, regnum, buf);
68     }
69 }
70
71 /* Collect a single register.  If the destination register size
72    matches the size the regcache expects, this can use
73    regcache_raw_supply().  If they are different, this fetches the
74    register via regcache_raw_supply() into a buffer and then copies it
75    into the final destination.  */
76
77 static void
78 mips_fbsd_collect_reg (const struct regcache *regcache, int regnum, void *addr,
79                        size_t len)
80 {
81   struct gdbarch *gdbarch = get_regcache_arch (regcache);
82
83   if (register_size (gdbarch, regnum) == len)
84     regcache_raw_collect (regcache, regnum, addr);
85   else
86     {
87       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
88       gdb_byte buf[MAX_REGISTER_SIZE];
89       LONGEST val;
90
91       regcache_raw_collect (regcache, regnum, buf);
92       val = extract_signed_integer (buf, register_size (gdbarch, regnum),
93                                     byte_order);
94       store_signed_integer ((gdb_byte *) addr, len, byte_order, val);
95     }
96 }
97
98 /* Supply the floating-point registers stored in FPREGS to REGCACHE.
99    Each floating-point register in FPREGS is REGSIZE bytes in
100    length.  */
101
102 void
103 mips_fbsd_supply_fpregs (struct regcache *regcache, int regnum,
104                          const void *fpregs, size_t regsize)
105 {
106   struct gdbarch *gdbarch = get_regcache_arch (regcache);
107   const gdb_byte *regs = (const gdb_byte *) fpregs;
108   int i, fp0num, fsrnum;
109
110   fp0num = mips_regnum (gdbarch)->fp0;
111   fsrnum = mips_regnum (gdbarch)->fp_control_status;
112   for (i = fp0num; i <= fsrnum; i++)
113     if (regnum == i || regnum == -1)
114       mips_fbsd_supply_reg (regcache, i,
115                             regs + (i - fp0num) * regsize, regsize);
116 }
117
118 /* Supply the general-purpose registers stored in GREGS to REGCACHE.
119    Each general-purpose register in GREGS is REGSIZE bytes in
120    length.  */
121
122 void
123 mips_fbsd_supply_gregs (struct regcache *regcache, int regnum,
124                         const void *gregs, size_t regsize)
125 {
126   struct gdbarch *gdbarch = get_regcache_arch (regcache);
127   const gdb_byte *regs = (const gdb_byte *) gregs;
128   int i;
129
130   for (i = 0; i <= mips_regnum (gdbarch)->pc; i++)
131     if (regnum == i || regnum == -1)
132       mips_fbsd_supply_reg (regcache, i, regs + i * regsize, regsize);
133 }
134
135 /* Collect the floating-point registers from REGCACHE and store them
136    in FPREGS.  Each floating-point register in FPREGS is REGSIZE bytes
137    in length.  */
138
139 void
140 mips_fbsd_collect_fpregs (const struct regcache *regcache, int regnum,
141                           void *fpregs, size_t regsize)
142 {
143   struct gdbarch *gdbarch = get_regcache_arch (regcache);
144   gdb_byte *regs = (gdb_byte *) fpregs;
145   int i, fp0num, fsrnum;
146
147   fp0num = mips_regnum (gdbarch)->fp0;
148   fsrnum = mips_regnum (gdbarch)->fp_control_status;
149   for (i = fp0num; i <= fsrnum; i++)
150     if (regnum == i || regnum == -1)
151       mips_fbsd_collect_reg (regcache, i,
152                              regs + (i - fp0num) * regsize, regsize);
153 }
154
155 /* Collect the general-purpose registers from REGCACHE and store them
156    in GREGS.  Each general-purpose register in GREGS is REGSIZE bytes
157    in length.  */
158
159 void
160 mips_fbsd_collect_gregs (const struct regcache *regcache, int regnum,
161                          void *gregs, size_t regsize)
162 {
163   struct gdbarch *gdbarch = get_regcache_arch (regcache);
164   gdb_byte *regs = (gdb_byte *) gregs;
165   int i;
166
167   for (i = 0; i <= mips_regnum (gdbarch)->pc; i++)
168     if (regnum == i || regnum == -1)
169       mips_fbsd_collect_reg (regcache, i, regs + i * regsize, regsize);
170 }
171
172 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
173    in the floating-point register set REGSET to register cache
174    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
175
176 static void
177 mips_fbsd_supply_fpregset (const struct regset *regset,
178                            struct regcache *regcache,
179                            int regnum, const void *fpregs, size_t len)
180 {
181   size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
182
183   gdb_assert (len >= MIPS_FBSD_NUM_FPREGS * regsize);
184
185   mips_fbsd_supply_fpregs (regcache, regnum, fpregs, regsize);
186 }
187
188 /* Collect register REGNUM from the register cache REGCACHE and store
189    it in the buffer specified by FPREGS and LEN in the floating-point
190    register set REGSET.  If REGNUM is -1, do this for all registers in
191    REGSET.  */
192
193 static void
194 mips_fbsd_collect_fpregset (const struct regset *regset,
195                             const struct regcache *regcache,
196                             int regnum, void *fpregs, size_t len)
197 {
198   size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
199
200   gdb_assert (len >= MIPS_FBSD_NUM_FPREGS * regsize);
201
202   mips_fbsd_collect_fpregs (regcache, regnum, fpregs, regsize);
203 }
204
205 /* Supply register REGNUM from the buffer specified by GREGS and LEN
206    in the general-purpose register set REGSET to register cache
207    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
208
209 static void
210 mips_fbsd_supply_gregset (const struct regset *regset,
211                           struct regcache *regcache, int regnum,
212                           const void *gregs, size_t len)
213 {
214   size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
215
216   gdb_assert (len >= MIPS_FBSD_NUM_GREGS * regsize);
217
218   mips_fbsd_supply_gregs (regcache, regnum, gregs, regsize);
219 }
220
221 /* Collect register REGNUM from the register cache REGCACHE and store
222    it in the buffer specified by GREGS and LEN in the general-purpose
223    register set REGSET.  If REGNUM is -1, do this for all registers in
224    REGSET.  */
225
226 static void
227 mips_fbsd_collect_gregset (const struct regset *regset,
228                            const struct regcache *regcache,
229                            int regnum, void *gregs, size_t len)
230 {
231   size_t regsize = mips_abi_regsize (get_regcache_arch (regcache));
232
233   gdb_assert (len >= MIPS_FBSD_NUM_GREGS * regsize);
234
235   mips_fbsd_collect_gregs (regcache, regnum, gregs, regsize);
236 }
237
238 /* FreeBSD/mips register sets.  */
239
240 static const struct regset mips_fbsd_gregset =
241 {
242   NULL,
243   mips_fbsd_supply_gregset,
244   mips_fbsd_collect_gregset,
245 };
246
247 static const struct regset mips_fbsd_fpregset =
248 {
249   NULL,
250   mips_fbsd_supply_fpregset,
251   mips_fbsd_collect_fpregset,
252 };
253
254 /* Iterate over core file register note sections.  */
255
256 static void
257 mips_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
258                                         iterate_over_regset_sections_cb *cb,
259                                         void *cb_data,
260                                         const struct regcache *regcache)
261 {
262   size_t regsize = mips_abi_regsize (gdbarch);
263
264   cb (".reg", MIPS_FBSD_NUM_GREGS * regsize, &mips_fbsd_gregset,
265       NULL, cb_data);
266   cb (".reg2", MIPS_FBSD_NUM_FPREGS * regsize, &mips_fbsd_fpregset,
267       NULL, cb_data);
268 }
269
270 /* Signal trampoline support.  */
271
272 #define FBSD_SYS_sigreturn      417
273
274 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + FBSD_SYS_sigreturn
275 #define MIPS_INST_SYSCALL       0x0000000c
276 #define MIPS_INST_BREAK         0x0000000d
277
278 #define O32_SIGFRAME_UCONTEXT_OFFSET    (16)
279 #define O32_SIGSET_T_SIZE       (16)
280
281 #define O32_UCONTEXT_ONSTACK    (O32_SIGSET_T_SIZE)
282 #define O32_UCONTEXT_PC         (O32_UCONTEXT_ONSTACK + 4)
283 #define O32_UCONTEXT_REGS       (O32_UCONTEXT_PC + 4)
284 #define O32_UCONTEXT_SR         (O32_UCONTEXT_REGS + 4 * 32)
285 #define O32_UCONTEXT_LO         (O32_UCONTEXT_SR + 4)
286 #define O32_UCONTEXT_HI         (O32_UCONTEXT_LO + 4)
287 #define O32_UCONTEXT_FPUSED     (O32_UCONTEXT_HI + 4)
288 #define O32_UCONTEXT_FPREGS     (O32_UCONTEXT_FPUSED + 4)
289
290 #define O32_UCONTEXT_REG_SIZE   4
291
292 static void
293 mips_fbsd_sigframe_init (const struct tramp_frame *self,
294                          struct frame_info *this_frame,
295                          struct trad_frame_cache *cache,
296                          CORE_ADDR func)
297 {
298   struct gdbarch *gdbarch = get_frame_arch (this_frame);
299   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
300   CORE_ADDR sp, ucontext_addr, addr;
301   int regnum;
302   gdb_byte buf[4];
303
304   /* We find the appropriate instance of `ucontext_t' at a
305      fixed offset in the signal frame.  */
306   sp = get_frame_register_signed (this_frame,
307                                   MIPS_SP_REGNUM + gdbarch_num_regs (gdbarch));
308   ucontext_addr = sp + O32_SIGFRAME_UCONTEXT_OFFSET;
309
310   /* PC.  */
311   regnum = mips_regnum (gdbarch)->pc;
312   trad_frame_set_reg_addr (cache,
313                            regnum + gdbarch_num_regs (gdbarch),
314                            ucontext_addr + O32_UCONTEXT_PC);
315
316   /* GPRs.  */
317   for (regnum = MIPS_ZERO_REGNUM, addr = ucontext_addr + O32_UCONTEXT_REGS;
318        regnum <= MIPS_RA_REGNUM; regnum++, addr += O32_UCONTEXT_REG_SIZE)
319     trad_frame_set_reg_addr (cache,
320                              regnum + gdbarch_num_regs (gdbarch),
321                              addr);
322
323   regnum = MIPS_PS_REGNUM;
324   trad_frame_set_reg_addr (cache,
325                            regnum + gdbarch_num_regs (gdbarch),
326                            ucontext_addr + O32_UCONTEXT_SR);
327
328   /* HI and LO.  */
329   regnum = mips_regnum (gdbarch)->lo;
330   trad_frame_set_reg_addr (cache,
331                            regnum + gdbarch_num_regs (gdbarch),
332                            ucontext_addr + O32_UCONTEXT_LO);
333   regnum = mips_regnum (gdbarch)->hi;
334   trad_frame_set_reg_addr (cache,
335                            regnum + gdbarch_num_regs (gdbarch),
336                            ucontext_addr + O32_UCONTEXT_HI);
337
338   if (target_read_memory (ucontext_addr + O32_UCONTEXT_FPUSED, buf, 4) == 0
339       && extract_unsigned_integer (buf, 4, byte_order) != 0)
340     {
341       for (regnum = 0, addr = ucontext_addr + O32_UCONTEXT_FPREGS;
342            regnum < 32; regnum++, addr += O32_UCONTEXT_REG_SIZE)
343         trad_frame_set_reg_addr (cache,
344                                  regnum + gdbarch_fp0_regnum (gdbarch),
345                                  addr);
346       trad_frame_set_reg_addr (cache, mips_regnum (gdbarch)->fp_control_status,
347                                addr);
348     }
349
350   trad_frame_set_id (cache, frame_id_build (sp, func));
351 }
352
353 #define MIPS_INST_ADDIU_A0_SP_O32 (0x27a40000 \
354                                    + O32_SIGFRAME_UCONTEXT_OFFSET)
355
356 static const struct tramp_frame mips_fbsd_sigframe =
357 {
358   SIGTRAMP_FRAME,
359   MIPS_INSN32_SIZE,
360   {
361     { MIPS_INST_ADDIU_A0_SP_O32, -1 },  /* addiu   a0, sp, SIGF_UC */
362     { MIPS_INST_LI_V0_SIGRETURN, -1 },  /* li      v0, SYS_sigreturn */
363     { MIPS_INST_SYSCALL, -1 },          /* syscall */
364     { MIPS_INST_BREAK, -1 },            /* break */
365     { TRAMP_SENTINEL_INSN, -1 }
366   },
367   mips_fbsd_sigframe_init
368 };
369
370 #define N64_SIGFRAME_UCONTEXT_OFFSET    (32)
371 #define N64_SIGSET_T_SIZE       (16)
372
373 #define N64_UCONTEXT_ONSTACK    (N64_SIGSET_T_SIZE)
374 #define N64_UCONTEXT_PC         (N64_UCONTEXT_ONSTACK + 8)
375 #define N64_UCONTEXT_REGS       (N64_UCONTEXT_PC + 8)
376 #define N64_UCONTEXT_SR         (N64_UCONTEXT_REGS + 8 * 32)
377 #define N64_UCONTEXT_LO         (N64_UCONTEXT_SR + 8)
378 #define N64_UCONTEXT_HI         (N64_UCONTEXT_LO + 8)
379 #define N64_UCONTEXT_FPUSED     (N64_UCONTEXT_HI + 8)
380 #define N64_UCONTEXT_FPREGS     (N64_UCONTEXT_FPUSED + 8)
381
382 #define N64_UCONTEXT_REG_SIZE   8
383
384 static void
385 mips64_fbsd_sigframe_init (const struct tramp_frame *self,
386                            struct frame_info *this_frame,
387                            struct trad_frame_cache *cache,
388                            CORE_ADDR func)
389 {
390   struct gdbarch *gdbarch = get_frame_arch (this_frame);
391   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
392   CORE_ADDR sp, ucontext_addr, addr;
393   int regnum;
394   gdb_byte buf[4];
395
396   /* We find the appropriate instance of `ucontext_t' at a
397      fixed offset in the signal frame.  */
398   sp = get_frame_register_signed (this_frame,
399                                   MIPS_SP_REGNUM + gdbarch_num_regs (gdbarch));
400   ucontext_addr = sp + N64_SIGFRAME_UCONTEXT_OFFSET;
401
402   /* PC.  */
403   regnum = mips_regnum (gdbarch)->pc;
404   trad_frame_set_reg_addr (cache,
405                            regnum + gdbarch_num_regs (gdbarch),
406                            ucontext_addr + N64_UCONTEXT_PC);
407
408   /* GPRs.  */
409   for (regnum = MIPS_ZERO_REGNUM, addr = ucontext_addr + N64_UCONTEXT_REGS;
410        regnum <= MIPS_RA_REGNUM; regnum++, addr += N64_UCONTEXT_REG_SIZE)
411     trad_frame_set_reg_addr (cache,
412                              regnum + gdbarch_num_regs (gdbarch),
413                              addr);
414
415   regnum = MIPS_PS_REGNUM;
416   trad_frame_set_reg_addr (cache,
417                            regnum + gdbarch_num_regs (gdbarch),
418                            ucontext_addr + N64_UCONTEXT_SR);
419
420   /* HI and LO.  */
421   regnum = mips_regnum (gdbarch)->lo;
422   trad_frame_set_reg_addr (cache,
423                            regnum + gdbarch_num_regs (gdbarch),
424                            ucontext_addr + N64_UCONTEXT_LO);
425   regnum = mips_regnum (gdbarch)->hi;
426   trad_frame_set_reg_addr (cache,
427                            regnum + gdbarch_num_regs (gdbarch),
428                            ucontext_addr + N64_UCONTEXT_HI);
429
430   if (target_read_memory (ucontext_addr + N64_UCONTEXT_FPUSED, buf, 4) == 0
431       && extract_unsigned_integer (buf, 4, byte_order) != 0)
432     {
433       for (regnum = 0, addr = ucontext_addr + N64_UCONTEXT_FPREGS;
434            regnum < 32; regnum++, addr += N64_UCONTEXT_REG_SIZE)
435         trad_frame_set_reg_addr (cache,
436                                  regnum + gdbarch_fp0_regnum (gdbarch),
437                                  addr);
438       trad_frame_set_reg_addr (cache, mips_regnum (gdbarch)->fp_control_status,
439                                addr);
440     }
441
442   trad_frame_set_id (cache, frame_id_build (sp, func));
443 }
444
445 #define MIPS_INST_DADDIU_A0_SP_N64 (0x67a40000 \
446                                     + N64_SIGFRAME_UCONTEXT_OFFSET)
447
448 static const struct tramp_frame mips64_fbsd_sigframe =
449 {
450   SIGTRAMP_FRAME,
451   MIPS_INSN32_SIZE,
452   {
453     { MIPS_INST_DADDIU_A0_SP_N64, -1 }, /* daddiu  a0, sp, SIGF_UC */
454     { MIPS_INST_LI_V0_SIGRETURN, -1 },  /* li      v0, SYS_sigreturn */
455     { MIPS_INST_SYSCALL, -1 },          /* syscall */
456     { MIPS_INST_BREAK, -1 },            /* break */
457     { TRAMP_SENTINEL_INSN, -1 }
458   },
459   mips64_fbsd_sigframe_init
460 };
461
462 /* Shared library support.  */
463
464 /* FreeBSD/mips uses a slightly different `struct link_map' than the
465    other FreeBSD platforms as it includes an additional `l_off'
466    member.  */
467
468 static struct link_map_offsets *
469 mips_fbsd_ilp32_fetch_link_map_offsets (void)
470 {
471   static struct link_map_offsets lmo;
472   static struct link_map_offsets *lmp = NULL;
473
474   if (lmp == NULL)
475     {
476       lmp = &lmo;
477
478       lmo.r_version_offset = 0;
479       lmo.r_version_size = 4;
480       lmo.r_map_offset = 4;
481       lmo.r_brk_offset = 8;
482       lmo.r_ldsomap_offset = -1;
483
484       lmo.link_map_size = 24;
485       lmo.l_addr_offset = 0;
486       lmo.l_name_offset = 8;
487       lmo.l_ld_offset = 12;
488       lmo.l_next_offset = 16;
489       lmo.l_prev_offset = 20;
490     }
491
492   return lmp;
493 }
494
495 static struct link_map_offsets *
496 mips_fbsd_lp64_fetch_link_map_offsets (void)
497 {
498   static struct link_map_offsets lmo;
499   static struct link_map_offsets *lmp = NULL;
500
501   if (lmp == NULL)
502     {
503       lmp = &lmo;
504
505       lmo.r_version_offset = 0;
506       lmo.r_version_size = 4;
507       lmo.r_map_offset = 8;
508       lmo.r_brk_offset = 16;
509       lmo.r_ldsomap_offset = -1;
510
511       lmo.link_map_size = 48;
512       lmo.l_addr_offset = 0;
513       lmo.l_name_offset = 16;
514       lmo.l_ld_offset = 24;
515       lmo.l_next_offset = 32;
516       lmo.l_prev_offset = 40;
517     }
518
519   return lmp;
520 }
521
522 static void
523 mips_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
524 {
525   enum mips_abi abi = mips_abi (gdbarch);
526
527   /* Generic FreeBSD support.  */
528   fbsd_init_abi (info, gdbarch);
529
530   set_gdbarch_software_single_step (gdbarch, mips_software_single_step);
531
532   switch (abi)
533     {
534       case MIPS_ABI_O32:
535         tramp_frame_prepend_unwinder (gdbarch, &mips_fbsd_sigframe);
536         break;
537       case MIPS_ABI_N32:
538         break;
539       case MIPS_ABI_N64:
540         tramp_frame_prepend_unwinder (gdbarch, &mips64_fbsd_sigframe);
541         break;
542     }
543
544   set_gdbarch_iterate_over_regset_sections
545     (gdbarch, mips_fbsd_iterate_over_regset_sections);
546
547   /* FreeBSD/mips has SVR4-style shared libraries.  */
548   set_solib_svr4_fetch_link_map_offsets
549     (gdbarch, (gdbarch_ptr_bit (gdbarch) == 32 ?
550                mips_fbsd_ilp32_fetch_link_map_offsets :
551                mips_fbsd_lp64_fetch_link_map_offsets));
552 }
553 \f
554
555 /* Provide a prototype to silence -Wmissing-prototypes.  */
556 void _initialize_mips_fbsd_tdep (void);
557
558 void
559 _initialize_mips_fbsd_tdep (void)
560 {
561   gdbarch_register_osabi (bfd_arch_mips, 0, GDB_OSABI_FREEBSD,
562                           mips_fbsd_init_abi);
563 }