gdb: xtensa-linux: add call0 support
[external/binutils.git] / gdb / xtensa-linux-nat.c
1 /* Xtensa GNU/Linux native support.
2
3    Copyright (C) 2007-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 "frame.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "linux-nat.h"
27 #include <sys/types.h>
28 #include <signal.h>
29 #include <sys/user.h>
30 #include <sys/ioctl.h>
31 #include "gdb_wait.h"
32 #include <fcntl.h>
33 #include <sys/procfs.h>
34 #include "nat/gdb_ptrace.h"
35 #include <asm/ptrace.h>
36
37 #include "gregset.h"
38 #include "xtensa-tdep.h"
39
40 /* Defines ps_err_e, struct ps_prochandle.  */
41 #include "gdb_proc_service.h"
42
43 /* Extended register set depends on hardware configs.
44    Keeping these definitions separately allows to introduce
45    hardware-specific overlays.  */
46 #include "xtensa-xtregs.c"
47
48 void
49 fill_gregset (const struct regcache *regcache,
50               gdb_gregset_t *gregsetp, int regnum)
51 {
52   int i;
53   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
54   struct gdbarch *gdbarch = get_regcache_arch (regcache);
55
56   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
57     regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
58   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
59     regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
60
61   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
62     regcache_raw_collect (regcache,
63                           gdbarch_tdep (gdbarch)->wb_regnum,
64                           &regs->windowbase);
65   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
66     regcache_raw_collect (regcache,
67                           gdbarch_tdep (gdbarch)->ws_regnum,
68                           &regs->windowstart);
69   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
70     regcache_raw_collect (regcache,
71                           gdbarch_tdep (gdbarch)->lbeg_regnum,
72                           &regs->lbeg);
73   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
74     regcache_raw_collect (regcache,
75                           gdbarch_tdep (gdbarch)->lend_regnum,
76                           &regs->lend);
77   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
78     regcache_raw_collect (regcache,
79                           gdbarch_tdep (gdbarch)->lcount_regnum,
80                           &regs->lcount);
81   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
82     regcache_raw_collect (regcache,
83                           gdbarch_tdep (gdbarch)->sar_regnum,
84                           &regs->sar);
85   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
86       && regnum < gdbarch_tdep (gdbarch)->ar_base
87                     + gdbarch_tdep (gdbarch)->num_aregs)
88     regcache_raw_collect (regcache,regnum,
89                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
90   else if (regnum == -1)
91     {
92       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
93         regcache_raw_collect (regcache,
94                               gdbarch_tdep (gdbarch)->ar_base + i,
95                               &regs->ar[i]);
96     }
97   if (regnum >= gdbarch_tdep (gdbarch)->a0_base
98       && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
99     regcache_raw_collect (regcache, regnum,
100                           &regs->ar[(4 * regs->windowbase + regnum
101                                      - gdbarch_tdep (gdbarch)->a0_base)
102                           % gdbarch_tdep (gdbarch)->num_aregs]);
103   else if (regnum == -1)
104     {
105       for (i = 0; i < C0_NREGS; ++i)
106         regcache_raw_collect (regcache,
107                               gdbarch_tdep (gdbarch)->a0_base + i,
108                               &regs->ar[(4 * regs->windowbase + i)
109                               % gdbarch_tdep (gdbarch)->num_aregs]);
110     }
111 }
112
113 static void
114 supply_gregset_reg (struct regcache *regcache,
115                     const gdb_gregset_t *gregsetp, int regnum)
116 {
117   int i;
118   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
119
120   struct gdbarch *gdbarch = get_regcache_arch (regcache);
121
122   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
123     regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
124   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
125     regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
126
127   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
128     regcache_raw_supply (regcache,
129                           gdbarch_tdep (gdbarch)->wb_regnum,
130                           &regs->windowbase);
131   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
132     regcache_raw_supply (regcache,
133                           gdbarch_tdep (gdbarch)->ws_regnum,
134                           &regs->windowstart);
135   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
136     regcache_raw_supply (regcache,
137                           gdbarch_tdep (gdbarch)->lbeg_regnum,
138                           &regs->lbeg);
139   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
140     regcache_raw_supply (regcache,
141                           gdbarch_tdep (gdbarch)->lend_regnum,
142                           &regs->lend);
143   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
144     regcache_raw_supply (regcache,
145                           gdbarch_tdep (gdbarch)->lcount_regnum,
146                           &regs->lcount);
147   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
148     regcache_raw_supply (regcache,
149                           gdbarch_tdep (gdbarch)->sar_regnum,
150                           &regs->sar);
151   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
152       && regnum < gdbarch_tdep (gdbarch)->ar_base
153                     + gdbarch_tdep (gdbarch)->num_aregs)
154     regcache_raw_supply (regcache,regnum,
155                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
156   else if (regnum == -1)
157     {
158       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
159         regcache_raw_supply (regcache,
160                               gdbarch_tdep (gdbarch)->ar_base + i,
161                               &regs->ar[i]);
162     }
163   if (regnum >= gdbarch_tdep (gdbarch)->a0_base
164       && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
165     regcache_raw_supply (regcache, regnum,
166                          &regs->ar[(4 * regs->windowbase + regnum
167                                     - gdbarch_tdep (gdbarch)->a0_base)
168                          % gdbarch_tdep (gdbarch)->num_aregs]);
169   else if (regnum == -1)
170     {
171       for (i = 0; i < C0_NREGS; ++i)
172         regcache_raw_supply (regcache,
173                              gdbarch_tdep (gdbarch)->a0_base + i,
174                              &regs->ar[(4 * regs->windowbase + i)
175                              % gdbarch_tdep (gdbarch)->num_aregs]);
176     }
177 }
178
179 void
180 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
181 {
182   supply_gregset_reg (regcache, gregsetp, -1);
183 }
184
185 void
186 fill_fpregset (const struct regcache *regcache,
187                gdb_fpregset_t *fpregsetp, int regnum)
188 {
189   return;
190 }
191
192 void 
193 supply_fpregset (struct regcache *regcache,
194                  const gdb_fpregset_t *fpregsetp)
195 {
196   return;
197 }
198
199 /* Fetch greg-register(s) from process/thread TID
200    and store value(s) in GDB's register array.  */
201
202 static void
203 fetch_gregs (struct regcache *regcache, int regnum)
204 {
205   int tid = ptid_get_lwp (regcache_get_ptid (regcache));
206   gdb_gregset_t regs;
207   int areg;
208   
209   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
210     {
211       perror_with_name (_("Couldn't get registers"));
212       return;
213     }
214  
215   supply_gregset_reg (regcache, &regs, regnum);
216 }
217
218 /* Store greg-register(s) in GDB's register 
219    array into the process/thread specified by TID.  */
220
221 static void
222 store_gregs (struct regcache *regcache, int regnum)
223 {
224   int tid = ptid_get_lwp (regcache_get_ptid (regcache));
225   gdb_gregset_t regs;
226   int areg;
227
228   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
229     {
230       perror_with_name (_("Couldn't get registers"));
231       return;
232     }
233
234   fill_gregset (regcache, &regs, regnum);
235
236   if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
237     {
238       perror_with_name (_("Couldn't write registers"));
239       return;
240     }
241 }
242
243 static int xtreg_lo;
244 static int xtreg_high;
245
246 /* Fetch/Store Xtensa TIE registers.  Xtensa GNU/Linux PTRACE
247    interface provides special requests for this.  */
248
249 static void
250 fetch_xtregs (struct regcache *regcache, int regnum)
251 {
252   int tid = ptid_get_lwp (regcache_get_ptid (regcache));
253   const xtensa_regtable_t *ptr;
254   char xtregs [XTENSA_ELF_XTREG_SIZE];
255
256   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
257     perror_with_name (_("Couldn't get extended registers"));
258
259   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
260     if (regnum == ptr->gdb_regnum || regnum == -1)
261       regcache_raw_supply (regcache, ptr->gdb_regnum,
262                            xtregs + ptr->ptrace_offset);
263 }
264
265 static void
266 store_xtregs (struct regcache *regcache, int regnum)
267 {
268   int tid = ptid_get_lwp (regcache_get_ptid (regcache));
269   const xtensa_regtable_t *ptr;
270   char xtregs [XTENSA_ELF_XTREG_SIZE];
271
272   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
273     perror_with_name (_("Couldn't get extended registers"));
274
275   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
276     if (regnum == ptr->gdb_regnum || regnum == -1)
277       regcache_raw_collect (regcache, ptr->gdb_regnum,
278                             xtregs + ptr->ptrace_offset);
279
280   if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
281     perror_with_name (_("Couldn't write extended registers"));
282 }
283
284 static void
285 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
286                                        struct regcache *regcache, int regnum)
287 {
288   if (regnum == -1)
289     {
290       fetch_gregs (regcache, regnum);
291       fetch_xtregs (regcache, regnum);
292     }
293   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
294     fetch_gregs (regcache, regnum);
295   else
296     fetch_xtregs (regcache, regnum);
297 }
298
299 static void
300 xtensa_linux_store_inferior_registers (struct target_ops *ops,
301                                        struct regcache *regcache, int regnum)
302 {
303   if (regnum == -1)
304     {
305       store_gregs (regcache, regnum);
306       store_xtregs (regcache, regnum);
307     }
308   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
309     store_gregs (regcache, regnum);
310   else
311     store_xtregs (regcache, regnum);
312 }
313
314 /* Called by libthread_db.  */
315
316 ps_err_e
317 ps_get_thread_area (struct ps_prochandle *ph,
318                     lwpid_t lwpid, int idx, void **base)
319 {
320   xtensa_elf_gregset_t regs;
321
322   if (ptrace (PTRACE_GETREGS, lwpid, NULL, &regs) != 0)
323     return PS_ERR;
324
325   /* IDX is the bias from the thread pointer to the beginning of the
326      thread descriptor.  It has to be subtracted due to implementation
327      quirks in libthread_db.  */
328   *base = (void *) ((char *) regs.threadptr - idx);
329
330   return PS_OK;
331 }
332
333 void _initialize_xtensa_linux_nat (void);
334
335 void
336 _initialize_xtensa_linux_nat (void)
337 {
338   struct target_ops *t;
339   const xtensa_regtable_t *ptr;
340
341   /* Calculate the number range for extended registers.  */
342   xtreg_lo = 1000000000;
343   xtreg_high = -1;
344   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
345     {
346       if (ptr->gdb_regnum < xtreg_lo)
347         xtreg_lo = ptr->gdb_regnum;
348       if (ptr->gdb_regnum > xtreg_high)
349         xtreg_high = ptr->gdb_regnum;
350     }
351
352   /* Fill in the generic GNU/Linux methods.  */
353   t = linux_target ();
354
355   /* Add our register access methods.  */
356   t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
357   t->to_store_registers = xtensa_linux_store_inferior_registers;
358
359   linux_nat_add_target (t);
360 }