2013-07-24 Sergio Durigan Junior <sergiodj@redhat.com>
[external/binutils.git] / gdb / nios2-linux-tdep.c
1 /* Target-dependent code for GNU/Linux on Nios II.
2    Copyright (C) 2012-2013 Free Software Foundation, Inc.
3    Contributed by Mentor Graphics, 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 "frame.h"
22 #include "gdb_assert.h"
23 #include "gdb_string.h"
24 #include "osabi.h"
25 #include "solib-svr4.h"
26 #include "trad-frame.h"
27 #include "tramp-frame.h"
28 #include "symtab.h"
29 #include "regset.h"
30 #include "regcache.h"
31 #include "linux-tdep.h"
32 #include "glibc-tdep.h"
33 #include "nios2-tdep.h"
34
35 #include "features/nios2-linux.c"
36
37 /* Core file and register set support.  */
38
39 /* Map from the normal register enumeration order to the order that
40    registers appear in core files, which corresponds to the order
41    of the register slots in the kernel's struct pt_regs.  */
42
43 static const int reg_offsets[NIOS2_NUM_REGS] =
44 {
45   -1,  8,  9, 10, 11, 12, 13, 14,       /* r0 - r7 */
46   0,  1,  2,  3,  4,  5,  6,  7,        /* r8 - r15 */
47   23, 24, 25, 26, 27, 28, 29, 30,       /* r16 - r23 */
48   -1, -1, 19, 18, 17, 21, -1, 16,       /* et bt gp sp fp ea ba ra */
49   21,                                   /* pc */
50   -1, 20, -1, -1, -1, -1, -1, -1,       /* status estatus ...  */
51   -1, -1, -1, -1, -1, -1, -1, -1
52 };
53
54 /* Implement the supply_regset hook for core files.  */
55
56 static void
57 nios2_supply_gregset (const struct regset *regset,
58                       struct regcache *regcache,
59                       int regnum, const void *gregs_buf, size_t len)
60 {
61   const gdb_byte *gregs = gregs_buf;
62   int regno;
63   static const gdb_byte zero_buf[4] = {0, 0, 0, 0};
64
65   for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
66     if (regnum == -1 || regnum == regno)
67       {
68         if (reg_offsets[regno] != -1)
69           regcache_raw_supply (regcache, regno,
70                                gregs + 4 * reg_offsets[regno]);
71         else
72           regcache_raw_supply (regcache, regno, zero_buf);
73       }
74 }
75
76 static struct regset nios2_core_regset =
77 {
78   NULL,
79   nios2_supply_gregset,
80   NULL,
81   NULL
82 };
83
84 /* Implement the regset_from_core_section gdbarch method.  */
85
86 static const struct regset *
87 nios2_regset_from_core_section (struct gdbarch *gdbarch,
88                                 const char *sect_name, size_t sect_size)
89 {
90   if (strcmp (sect_name, ".reg") == 0)
91     return &nios2_core_regset;
92
93   return NULL;
94 }
95
96 /* Initialize a trad-frame cache corresponding to the tramp-frame.
97    FUNC is the address of the instruction TRAMP[0] in memory.  */
98
99 static void
100 nios2_linux_sigreturn_init (const struct tramp_frame *self,
101                             struct frame_info *next_frame,
102                             struct trad_frame_cache *this_cache,
103                             CORE_ADDR func)
104 {
105   CORE_ADDR base = func + 16;
106   int i;
107
108   for (i = 0; i < 8; i++)
109     trad_frame_set_reg_addr (this_cache, i + 8, base + i * 4);
110   for (i = 0; i < 7; i++)
111     trad_frame_set_reg_addr (this_cache, i + 1, base + (i + 8) * 4);
112   trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 16 * 4);
113   trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 17 * 4);
114   trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 18 * 4);
115   trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 19 * 4);
116   trad_frame_set_reg_addr (this_cache, NIOS2_ESTATUS_REGNUM, base + 20 * 4);
117   trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 21 * 4);
118
119   /* Save a frame ID.  */
120   trad_frame_set_id (this_cache, frame_id_build (base, func));
121 }
122
123 /* Initialize a trad-frame cache corresponding to the tramp-frame.
124    FUNC is the address of the instruction TRAMP[0] in memory.  */
125
126 static void
127 nios2_linux_rt_sigreturn_init (const struct tramp_frame *self,
128                                struct frame_info *next_frame,
129                                struct trad_frame_cache *this_cache,
130                                CORE_ADDR func)
131 {
132   CORE_ADDR base = func + 41 * 4;
133   int i;
134
135   for (i = 0; i < 23; i++)
136     trad_frame_set_reg_addr (this_cache, i + 1, base + i * 4);
137   trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 23 * 4);
138   trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 24 * 4);
139   trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 25 * 4);
140   trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 27 * 4);
141   trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 28 * 4);
142
143   /* Save a frame ID.  */
144   trad_frame_set_id (this_cache, frame_id_build (base, func));
145 }
146
147 static struct tramp_frame nios2_linux_sigreturn_tramp_frame =
148 {
149   SIGTRAMP_FRAME,
150   4,
151   {
152     { 0x00800004 | (119 << 6), -1 },  /* movi r2,__NR_sigreturn */
153     { 0x003b683a, -1 },               /* trap */
154     { TRAMP_SENTINEL_INSN }
155   },
156   nios2_linux_sigreturn_init
157 };
158
159 static struct tramp_frame nios2_linux_rt_sigreturn_tramp_frame =
160 {
161   SIGTRAMP_FRAME,
162   4,
163   {
164     { 0x00800004 | (173 << 6), -1 },  /* movi r2,__NR_rt_sigreturn */
165     { 0x003b683a, -1 },               /* trap */
166     { TRAMP_SENTINEL_INSN }
167   },
168   nios2_linux_rt_sigreturn_init
169 };
170
171 /* When FRAME is at a syscall instruction, return the PC of the next
172    instruction to be executed.  */
173
174 static CORE_ADDR
175 nios2_linux_syscall_next_pc (struct frame_info *frame)
176 {
177   CORE_ADDR pc = get_frame_pc (frame);
178   ULONGEST syscall_nr = get_frame_register_unsigned (frame, NIOS2_R2_REGNUM);
179
180   /* If we are about to make a sigreturn syscall, use the unwinder to
181      decode the signal frame.  */
182   if (syscall_nr == 119 /* sigreturn */
183       || syscall_nr == 173 /* rt_sigreturn */)
184     return frame_unwind_caller_pc (frame);
185
186   return pc + NIOS2_OPCODE_SIZE;
187 }
188
189 /* Hook function for gdbarch_register_osabi.  */
190
191 static void
192 nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
193 {
194   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
195
196   linux_init_abi (info, gdbarch);
197
198   /* Shared library handling.  */
199   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
200   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
201
202   set_solib_svr4_fetch_link_map_offsets (gdbarch,
203                                          svr4_ilp32_fetch_link_map_offsets);
204   /* Enable TLS support.  */
205   set_gdbarch_fetch_tls_load_module_address (gdbarch,
206                                              svr4_fetch_objfile_link_map);
207   /* Core file support.  */
208   set_gdbarch_regset_from_core_section (gdbarch,
209                                         nios2_regset_from_core_section);
210   /* Linux signal frame unwinders.  */
211   tramp_frame_prepend_unwinder (gdbarch,
212                                 &nios2_linux_sigreturn_tramp_frame);
213   tramp_frame_prepend_unwinder (gdbarch,
214                                 &nios2_linux_rt_sigreturn_tramp_frame);
215
216   tdep->syscall_next_pc = nios2_linux_syscall_next_pc;
217
218   /* Index of target address word in glibc jmp_buf.  */
219   tdep->jb_pc = 10;
220 }
221
222 /* Provide a prototype to silence -Wmissing-prototypes.  */
223
224 extern initialize_file_ftype _initialize_nios2_linux_tdep;
225
226 void
227 _initialize_nios2_linux_tdep (void)
228 {
229   gdbarch_register_osabi (bfd_arch_nios2, 0, GDB_OSABI_LINUX,
230                           nios2_linux_init_abi);
231
232   initialize_tdesc_nios2_linux ();
233 }