Sort includes for files gdb/[a-f]*.[chyl].
[external/binutils.git] / gdb / frv-linux-tdep.c
1 /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
2    for GDB.
3
4    Copyright (C) 2004-2019 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22
23 /* Local non-gdb includes.  */
24 #include "elf-bfd.h"
25 #include "elf/frv.h"
26 #include "frame-unwind.h"
27 #include "frame.h"
28 #include "frv-tdep.h"
29 #include "gdbcore.h"
30 #include "linux-tdep.h"
31 #include "osabi.h"
32 #include "regcache.h"
33 #include "regset.h"
34 #include "target.h"
35 #include "trad-frame.h"
36
37 /* Define the size (in bytes) of an FR-V instruction.  */
38 static const int frv_instr_size = 4;
39
40 enum {
41   NORMAL_SIGTRAMP = 1,
42   RT_SIGTRAMP = 2
43 };
44
45 static int
46 frv_linux_pc_in_sigtramp (struct gdbarch *gdbarch, CORE_ADDR pc,
47                           const char *name)
48 {
49   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
50   gdb_byte buf[frv_instr_size];
51   LONGEST instr;
52   int retval = 0;
53
54   if (target_read_memory (pc, buf, sizeof buf) != 0)
55     return 0;
56
57   instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
58
59   if (instr == 0x8efc0077)      /* setlos #__NR_sigreturn, gr7 */
60     retval = NORMAL_SIGTRAMP;
61   else if (instr == 0x8efc00ad) /* setlos #__NR_rt_sigreturn, gr7 */
62     retval = RT_SIGTRAMP;
63   else
64     return 0;
65
66   if (target_read_memory (pc + frv_instr_size, buf, sizeof buf) != 0)
67     return 0;
68   instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
69   if (instr != 0xc0700000)      /* tira gr0, 0 */
70     return 0;
71
72   /* If we get this far, we'll return a non-zero value, either
73      NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2).  */
74   return retval;
75 }
76
77 /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
78    wish to decode, and REGNO, one of the frv register numbers defined
79    in frv-tdep.h, return the address of the saved register (corresponding
80    to REGNO) in the sigtramp frame.  Return -1 if the register is not
81    found in the sigtramp frame.  The magic numbers in the code below
82    were computed by examining the following kernel structs:
83
84    From arch/frv/kernel/signal.c:
85
86       struct sigframe
87       {
88               void (*pretcode)(void);
89               int sig;
90               struct sigcontext sc;
91               unsigned long extramask[_NSIG_WORDS-1];
92               uint32_t retcode[2];
93       };
94
95       struct rt_sigframe
96       {
97               void (*pretcode)(void);
98               int sig;
99               struct siginfo *pinfo;
100               void *puc;
101               struct siginfo info;
102               struct ucontext uc;
103               uint32_t retcode[2];
104       };
105
106    From include/asm-frv/ucontext.h:
107
108       struct ucontext {
109               unsigned long             uc_flags;
110               struct ucontext           *uc_link;
111               stack_t                   uc_stack;
112               struct sigcontext uc_mcontext;
113               sigset_t          uc_sigmask;
114       };
115
116    From include/asm-frv/signal.h:
117
118       typedef struct sigaltstack {
119               void *ss_sp;
120               int ss_flags;
121               size_t ss_size;
122       } stack_t;
123
124    From include/asm-frv/sigcontext.h:
125
126       struct sigcontext {
127               struct user_context       sc_context;
128               unsigned long             sc_oldmask;
129       } __attribute__((aligned(8)));
130
131    From include/asm-frv/registers.h:
132       struct user_int_regs
133       {
134               unsigned long             psr;
135               unsigned long             isr;
136               unsigned long             ccr;
137               unsigned long             cccr;
138               unsigned long             lr;
139               unsigned long             lcr;
140               unsigned long             pc;
141               unsigned long             __status;
142               unsigned long             syscallno;
143               unsigned long             orig_gr8;
144               unsigned long             gner[2];
145               unsigned long long        iacc[1];
146
147               union {
148                       unsigned long     tbr;
149                       unsigned long     gr[64];
150               };
151       };
152
153       struct user_fpmedia_regs
154       {
155               unsigned long     fr[64];
156               unsigned long     fner[2];
157               unsigned long     msr[2];
158               unsigned long     acc[8];
159               unsigned char     accg[8];
160               unsigned long     fsr[1];
161       };
162
163       struct user_context
164       {
165               struct user_int_regs              i;
166               struct user_fpmedia_regs  f;
167
168               void *extension;
169       } __attribute__((aligned(8)));  */
170
171 static LONGEST
172 frv_linux_sigcontext_reg_addr (struct frame_info *this_frame, int regno,
173                                CORE_ADDR *sc_addr_cache_ptr)
174 {
175   struct gdbarch *gdbarch = get_frame_arch (this_frame);
176   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
177   CORE_ADDR sc_addr;
178
179   if (sc_addr_cache_ptr && *sc_addr_cache_ptr)
180     {
181       sc_addr = *sc_addr_cache_ptr;
182     }
183   else
184     {
185       CORE_ADDR pc, sp;
186       gdb_byte buf[4];
187       int tramp_type;
188
189       pc = get_frame_pc (this_frame);
190       tramp_type = frv_linux_pc_in_sigtramp (gdbarch, pc, 0);
191
192       get_frame_register (this_frame, sp_regnum, buf);
193       sp = extract_unsigned_integer (buf, sizeof buf, byte_order);
194
195       if (tramp_type == NORMAL_SIGTRAMP)
196         {
197           /* For a normal sigtramp frame, the sigcontext struct starts
198              at SP + 8.  */
199           sc_addr = sp + 8;
200         }
201       else if (tramp_type == RT_SIGTRAMP)
202         {
203           /* For a realtime sigtramp frame, SP + 12 contains a pointer
204              to a ucontext struct.  The ucontext struct contains a
205              sigcontext struct starting 24 bytes in.  (The offset of
206              uc_mcontext within struct ucontext is derived as follows: 
207              stack_t is a 12-byte struct and struct sigcontext is
208              8-byte aligned.  This gives an offset of 8 + 12 + 4 (for
209              padding) = 24.)  */
210           if (target_read_memory (sp + 12, buf, sizeof buf) != 0)
211             {
212               warning (_("Can't read realtime sigtramp frame."));
213               return 0;
214             }
215           sc_addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
216           sc_addr += 24;
217         }
218       else
219         internal_error (__FILE__, __LINE__, _("not a signal trampoline"));
220
221       if (sc_addr_cache_ptr)
222         *sc_addr_cache_ptr = sc_addr;
223     }
224
225   switch (regno)
226     {
227     case psr_regnum :
228       return sc_addr + 0;
229     /* sc_addr + 4 has "isr", the Integer Status Register.  */
230     case ccr_regnum :
231       return sc_addr + 8;
232     case cccr_regnum :
233       return sc_addr + 12;
234     case lr_regnum :
235       return sc_addr + 16;
236     case lcr_regnum :
237       return sc_addr + 20;
238     case pc_regnum :
239       return sc_addr + 24;
240     /* sc_addr + 28 is __status, the exception status.
241        sc_addr + 32 is syscallno, the syscall number or -1.
242        sc_addr + 36 is orig_gr8, the original syscall arg #1.
243        sc_addr + 40 is gner[0].
244        sc_addr + 44 is gner[1].  */
245     case iacc0h_regnum :
246       return sc_addr + 48;
247     case iacc0l_regnum :
248       return sc_addr + 52;
249     default : 
250       if (first_gpr_regnum <= regno && regno <= last_gpr_regnum)
251         return sc_addr + 56 + 4 * (regno - first_gpr_regnum);
252       else if (first_fpr_regnum <= regno && regno <= last_fpr_regnum)
253         return sc_addr + 312 + 4 * (regno - first_fpr_regnum);
254       else
255         return -1;  /* not saved.  */
256     }
257 }
258
259 /* Signal trampolines.  */
260
261 static struct trad_frame_cache *
262 frv_linux_sigtramp_frame_cache (struct frame_info *this_frame,
263                                 void **this_cache)
264 {
265   struct gdbarch *gdbarch = get_frame_arch (this_frame);
266   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
267   struct trad_frame_cache *cache;
268   CORE_ADDR addr;
269   gdb_byte buf[4];
270   int regnum;
271   CORE_ADDR sc_addr_cache_val = 0;
272   struct frame_id this_id;
273
274   if (*this_cache)
275     return (struct trad_frame_cache *) *this_cache;
276
277   cache = trad_frame_cache_zalloc (this_frame);
278
279   /* FIXME: cagney/2004-05-01: This is is long standing broken code.
280      The frame ID's code address should be the start-address of the
281      signal trampoline and not the current PC within that
282      trampoline.  */
283   get_frame_register (this_frame, sp_regnum, buf);
284   addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
285   this_id = frame_id_build (addr, get_frame_pc (this_frame));
286   trad_frame_set_id (cache, this_id);
287
288   for (regnum = 0; regnum < frv_num_regs; regnum++)
289     {
290       LONGEST reg_addr = frv_linux_sigcontext_reg_addr (this_frame, regnum,
291                                                         &sc_addr_cache_val);
292       if (reg_addr != -1)
293         trad_frame_set_reg_addr (cache, regnum, reg_addr);
294     }
295
296   *this_cache = cache;
297   return cache;
298 }
299
300 static void
301 frv_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
302                                   void **this_cache,
303                                   struct frame_id *this_id)
304 {
305   struct trad_frame_cache *cache
306     = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
307   trad_frame_get_id (cache, this_id);
308 }
309
310 static struct value *
311 frv_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
312                                         void **this_cache, int regnum)
313 {
314   /* Make sure we've initialized the cache.  */
315   struct trad_frame_cache *cache
316     = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
317   return trad_frame_get_register (cache, this_frame, regnum);
318 }
319
320 static int
321 frv_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
322                                   struct frame_info *this_frame,
323                                   void **this_cache)
324 {
325   struct gdbarch *gdbarch = get_frame_arch (this_frame);
326   CORE_ADDR pc = get_frame_pc (this_frame);
327   const char *name;
328
329   find_pc_partial_function (pc, &name, NULL, NULL);
330   if (frv_linux_pc_in_sigtramp (gdbarch, pc, name))
331     return 1;
332
333   return 0;
334 }
335
336 static const struct frame_unwind frv_linux_sigtramp_frame_unwind =
337 {
338   SIGTRAMP_FRAME,
339   default_frame_unwind_stop_reason,
340   frv_linux_sigtramp_frame_this_id,
341   frv_linux_sigtramp_frame_prev_register,
342   NULL,
343   frv_linux_sigtramp_frame_sniffer
344 };
345 \f
346 /* The FRV kernel defines ELF_NGREG as 46.  We add 2 in order to include
347    the loadmap addresses in the register set.  (See below for more info.)  */
348 #define FRV_ELF_NGREG (46 + 2)
349 typedef unsigned char frv_elf_greg_t[4];
350 typedef struct { frv_elf_greg_t reg[FRV_ELF_NGREG]; } frv_elf_gregset_t;
351
352 typedef unsigned char frv_elf_fpreg_t[4];
353 typedef struct
354 {
355   frv_elf_fpreg_t fr[64];
356   frv_elf_fpreg_t fner[2];
357   frv_elf_fpreg_t msr[2];
358   frv_elf_fpreg_t acc[8];
359   unsigned char accg[8];
360   frv_elf_fpreg_t fsr[1];
361 } frv_elf_fpregset_t;
362
363 /* Register maps.  */
364
365 static const struct regcache_map_entry frv_linux_gregmap[] =
366   {
367     { 1, psr_regnum, 4 },
368     { 1, REGCACHE_MAP_SKIP, 4 }, /* isr */
369     { 1, ccr_regnum, 4 },
370     { 1, cccr_regnum, 4 },
371     { 1, lr_regnum, 4 },
372     { 1, lcr_regnum, 4 },
373     { 1, pc_regnum, 4 },
374     { 1, REGCACHE_MAP_SKIP, 4 }, /* __status */
375     { 1, REGCACHE_MAP_SKIP, 4 }, /* syscallno */
376     { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_gr8 */
377     { 1, gner0_regnum, 4 },
378     { 1, gner1_regnum, 4 },
379     { 1, REGCACHE_MAP_SKIP, 8 }, /* iacc0 */
380     { 1, tbr_regnum, 4 },
381     { 31, first_gpr_regnum + 1, 4 }, /* gr1 ... gr31 */
382
383     /* Technically, the loadmap addresses are not part of `pr_reg' as
384        found in the elf_prstatus struct.  The fields which communicate
385        the loadmap address appear (by design) immediately after
386        `pr_reg' though, and the BFD function elf32_frv_grok_prstatus()
387        has been implemented to include these fields in the register
388        section that it extracts from the core file.  So, for our
389        purposes, they may be viewed as registers.  */
390
391     { 1, fdpic_loadmap_exec_regnum, 4 },
392     { 1, fdpic_loadmap_interp_regnum, 4 },
393     { 0 }
394   };
395
396 static const struct regcache_map_entry frv_linux_fpregmap[] =
397   {
398     { 64, first_fpr_regnum, 4 }, /* fr0 ... fr63 */
399     { 1, fner0_regnum, 4 },
400     { 1, fner1_regnum, 4 },
401     { 1, msr0_regnum, 4 },
402     { 1, msr1_regnum, 4 },
403     { 8, acc0_regnum, 4 },      /* acc0 ... acc7 */
404     { 1, accg0123_regnum, 4 },
405     { 1, accg4567_regnum, 4 },
406     { 1, fsr0_regnum, 4 },
407     { 0 }
408   };
409
410 /* Unpack an frv_elf_gregset_t into GDB's register cache.  */
411
412 static void 
413 frv_linux_supply_gregset (const struct regset *regset,
414                           struct regcache *regcache,
415                           int regnum, const void *gregs, size_t len)
416 {
417   int regi;
418
419   /* gr0 always contains 0.  Also, the kernel passes the TBR value in
420      this slot.  */
421   regcache->raw_supply_zeroed (first_gpr_regnum);
422
423   /* Fill gr32, ..., gr63 with zeros. */
424   for (regi = first_gpr_regnum + 32; regi <= last_gpr_regnum; regi++)
425     regcache->raw_supply_zeroed (regi);
426
427   regcache_supply_regset (regset, regcache, regnum, gregs, len);
428 }
429
430 /* FRV Linux kernel register sets.  */
431
432 static const struct regset frv_linux_gregset =
433 {
434   frv_linux_gregmap,
435   frv_linux_supply_gregset, regcache_collect_regset
436 };
437
438 static const struct regset frv_linux_fpregset =
439 {
440   frv_linux_fpregmap,
441   regcache_supply_regset, regcache_collect_regset
442 };
443
444 static void
445 frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
446                                         iterate_over_regset_sections_cb *cb,
447                                         void *cb_data,
448                                         const struct regcache *regcache)
449 {
450   cb (".reg", sizeof (frv_elf_gregset_t), sizeof (frv_elf_gregset_t),
451       &frv_linux_gregset, NULL, cb_data);
452   cb (".reg2", sizeof (frv_elf_fpregset_t), sizeof (frv_elf_fpregset_t),
453       &frv_linux_fpregset, NULL, cb_data);
454 }
455
456 \f
457 static void
458 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
459 {
460   linux_init_abi (info, gdbarch);
461
462   /* Set the sigtramp frame sniffer.  */
463   frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind); 
464
465   set_gdbarch_iterate_over_regset_sections
466     (gdbarch, frv_linux_iterate_over_regset_sections);
467 }
468
469 static enum gdb_osabi
470 frv_linux_elf_osabi_sniffer (bfd *abfd)
471 {
472   int elf_flags;
473
474   elf_flags = elf_elfheader (abfd)->e_flags;
475
476   /* Assume GNU/Linux if using the FDPIC ABI.  If/when another OS shows
477      up that uses this ABI, we'll need to start using .note sections
478      or some such.  */
479   if (elf_flags & EF_FRV_FDPIC)
480     return GDB_OSABI_LINUX;
481   else
482     return GDB_OSABI_UNKNOWN;
483 }
484
485 void
486 _initialize_frv_linux_tdep (void)
487 {
488   gdbarch_register_osabi (bfd_arch_frv, 0, GDB_OSABI_LINUX,
489                           frv_linux_init_abi);
490   gdbarch_register_osabi_sniffer (bfd_arch_frv,
491                                   bfd_target_elf_flavour,
492                                   frv_linux_elf_osabi_sniffer);
493 }