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