Refactor svr4_create_solib_event_breakpoints
[external/binutils.git] / gdb / aarch64-fbsd-tdep.c
1 /* Target-dependent code for FreeBSD/aarch64.
2
3    Copyright (C) 2017-2019 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 "osabi.h"
27 #include "solib-svr4.h"
28 #include "target.h"
29 #include "tramp-frame.h"
30 #include "trad-frame.h"
31
32 /* Register maps.  */
33
34 static const struct regcache_map_entry aarch64_fbsd_gregmap[] =
35   {
36     { 30, AARCH64_X0_REGNUM, 8 }, /* x0 ... x29 */
37     { 1, AARCH64_LR_REGNUM, 8 },
38     { 1, AARCH64_SP_REGNUM, 8 },
39     { 1, AARCH64_PC_REGNUM, 8 },
40     { 1, AARCH64_CPSR_REGNUM, 4 },
41     { 0 }
42   };
43
44 static const struct regcache_map_entry aarch64_fbsd_fpregmap[] =
45   {
46     { 32, AARCH64_V0_REGNUM, 16 }, /* v0 ... v31 */
47     { 1, AARCH64_FPSR_REGNUM, 4 },
48     { 1, AARCH64_FPCR_REGNUM, 4 },
49     { 0 }
50   };
51
52 /* In a signal frame, sp points to a 'struct sigframe' which is
53    defined as:
54
55    struct sigframe {
56            siginfo_t    sf_si;
57            ucontext_t   sf_uc;
58    };
59
60    ucontext_t is defined as:
61
62    struct __ucontext {
63            sigset_t     uc_sigmask;
64            mcontext_t   uc_mcontext;
65            ...
66    };
67
68    The mcontext_t contains the general purpose register set followed
69    by the floating point register set.  The floating point register
70    set is only valid if the _MC_FP_VALID flag is set in mc_flags.  */
71
72 #define AARCH64_SIGFRAME_UCONTEXT_OFFSET        80
73 #define AARCH64_UCONTEXT_MCONTEXT_OFFSET        16
74 #define AARCH64_MCONTEXT_FPREGS_OFFSET          272
75 #define AARCH64_MCONTEXT_FLAGS_OFFSET           800
76 #define AARCH64_MCONTEXT_FLAG_FP_VALID          0x1
77
78 /* Implement the "init" method of struct tramp_frame.  */
79
80 static void
81 aarch64_fbsd_sigframe_init (const struct tramp_frame *self,
82                              struct frame_info *this_frame,
83                              struct trad_frame_cache *this_cache,
84                              CORE_ADDR func)
85 {
86   struct gdbarch *gdbarch = get_frame_arch (this_frame);
87   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
88   CORE_ADDR sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
89   CORE_ADDR mcontext_addr
90     = (sp
91        + AARCH64_SIGFRAME_UCONTEXT_OFFSET
92        + AARCH64_UCONTEXT_MCONTEXT_OFFSET);
93   gdb_byte buf[4];
94
95   trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_gregmap, mcontext_addr,
96                              regcache_map_entry_size (aarch64_fbsd_gregmap));
97
98   if (target_read_memory (mcontext_addr + AARCH64_MCONTEXT_FLAGS_OFFSET, buf,
99                           4) == 0
100       && (extract_unsigned_integer (buf, 4, byte_order)
101           & AARCH64_MCONTEXT_FLAG_FP_VALID))
102     trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_fpregmap,
103                                mcontext_addr + AARCH64_MCONTEXT_FPREGS_OFFSET,
104                                regcache_map_entry_size (aarch64_fbsd_fpregmap));
105
106   trad_frame_set_id (this_cache, frame_id_build (sp, func));
107 }
108
109 static const struct tramp_frame aarch64_fbsd_sigframe =
110 {
111   SIGTRAMP_FRAME,
112   4,
113   {
114     {0x910003e0, ULONGEST_MAX},         /* mov  x0, sp  */
115     {0x91014000, ULONGEST_MAX},         /* add  x0, x0, #SF_UC  */
116     {0xd2803428, ULONGEST_MAX},         /* mov  x8, #SYS_sigreturn  */
117     {0xd4000001, ULONGEST_MAX},         /* svc  0x0  */
118     {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
119   },
120   aarch64_fbsd_sigframe_init
121 };
122
123 /* Register set definitions.  */
124
125 const struct regset aarch64_fbsd_gregset =
126   {
127     aarch64_fbsd_gregmap,
128     regcache_supply_regset, regcache_collect_regset
129   };
130
131 const struct regset aarch64_fbsd_fpregset =
132   {
133     aarch64_fbsd_fpregmap,
134     regcache_supply_regset, regcache_collect_regset
135   };
136
137 /* Implement the "regset_from_core_section" gdbarch method.  */
138
139 static void
140 aarch64_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
141                                            iterate_over_regset_sections_cb *cb,
142                                            void *cb_data,
143                                            const struct regcache *regcache)
144 {
145   cb (".reg", AARCH64_FBSD_SIZEOF_GREGSET, AARCH64_FBSD_SIZEOF_GREGSET,
146       &aarch64_fbsd_gregset, NULL, cb_data);
147   cb (".reg2", AARCH64_FBSD_SIZEOF_FPREGSET, AARCH64_FBSD_SIZEOF_FPREGSET,
148       &aarch64_fbsd_fpregset, NULL, cb_data);
149 }
150
151 /* Implement the 'init_osabi' method of struct gdb_osabi_handler.  */
152
153 static void
154 aarch64_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
155 {
156   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
157
158   /* Generic FreeBSD support.  */
159   fbsd_init_abi (info, gdbarch);
160
161   set_solib_svr4_fetch_link_map_offsets (gdbarch,
162                                          svr4_lp64_fetch_link_map_offsets);
163
164   tramp_frame_prepend_unwinder (gdbarch, &aarch64_fbsd_sigframe);
165
166   /* Enable longjmp.  */
167   tdep->jb_pc = 13;
168
169   set_gdbarch_iterate_over_regset_sections
170     (gdbarch, aarch64_fbsd_iterate_over_regset_sections);
171 }
172
173 void
174 _initialize_aarch64_fbsd_tdep (void)
175 {
176   gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_FREEBSD,
177                           aarch64_fbsd_init_abi);
178 }