Remove get_thread_id
[external/binutils.git] / gdb / xtensa-linux-nat.c
1 /* Xtensa GNU/Linux native support.
2
3    Copyright (C) 2007-2015 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 /* Extended register set depends on hardware configs.
41    Keeping these definitions separately allows to introduce
42    hardware-specific overlays.  */
43 #include "xtensa-xtregs.c"
44
45 void
46 fill_gregset (const struct regcache *regcache,
47               gdb_gregset_t *gregsetp, int regnum)
48 {
49   int i;
50   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
51   struct gdbarch *gdbarch = get_regcache_arch (regcache);
52
53   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
54     regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
55   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
56     regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
57
58   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
59     regcache_raw_collect (regcache,
60                           gdbarch_tdep (gdbarch)->wb_regnum,
61                           &regs->windowbase);
62   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
63     regcache_raw_collect (regcache,
64                           gdbarch_tdep (gdbarch)->ws_regnum,
65                           &regs->windowstart);
66   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
67     regcache_raw_collect (regcache,
68                           gdbarch_tdep (gdbarch)->lbeg_regnum,
69                           &regs->lbeg);
70   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
71     regcache_raw_collect (regcache,
72                           gdbarch_tdep (gdbarch)->lend_regnum,
73                           &regs->lend);
74   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
75     regcache_raw_collect (regcache,
76                           gdbarch_tdep (gdbarch)->lcount_regnum,
77                           &regs->lcount);
78   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
79     regcache_raw_collect (regcache,
80                           gdbarch_tdep (gdbarch)->sar_regnum,
81                           &regs->sar);
82   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
83       && regnum < gdbarch_tdep (gdbarch)->ar_base
84                     + gdbarch_tdep (gdbarch)->num_aregs)
85     regcache_raw_collect (regcache,regnum,
86                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
87   else if (regnum == -1)
88     {
89       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
90         regcache_raw_collect (regcache,
91                               gdbarch_tdep (gdbarch)->ar_base + i,
92                               &regs->ar[i]);
93     }
94 }
95
96 void
97 supply_gregset_reg (struct regcache *regcache,
98                     const gdb_gregset_t *gregsetp, int regnum)
99 {
100   int i;
101   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
102
103   struct gdbarch *gdbarch = get_regcache_arch (regcache);
104
105   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
106     regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
107   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
108     regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
109
110   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
111     regcache_raw_supply (regcache,
112                           gdbarch_tdep (gdbarch)->wb_regnum,
113                           &regs->windowbase);
114   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
115     regcache_raw_supply (regcache,
116                           gdbarch_tdep (gdbarch)->ws_regnum,
117                           &regs->windowstart);
118   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
119     regcache_raw_supply (regcache,
120                           gdbarch_tdep (gdbarch)->lbeg_regnum,
121                           &regs->lbeg);
122   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
123     regcache_raw_supply (regcache,
124                           gdbarch_tdep (gdbarch)->lend_regnum,
125                           &regs->lend);
126   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
127     regcache_raw_supply (regcache,
128                           gdbarch_tdep (gdbarch)->lcount_regnum,
129                           &regs->lcount);
130   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
131     regcache_raw_supply (regcache,
132                           gdbarch_tdep (gdbarch)->sar_regnum,
133                           &regs->sar);
134   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
135       && regnum < gdbarch_tdep (gdbarch)->ar_base
136                     + gdbarch_tdep (gdbarch)->num_aregs)
137     regcache_raw_supply (regcache,regnum,
138                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
139   else if (regnum == -1)
140     {
141       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
142         regcache_raw_supply (regcache,
143                               gdbarch_tdep (gdbarch)->ar_base + i,
144                               &regs->ar[i]);
145     }
146 }
147
148 void
149 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
150 {
151   supply_gregset_reg (regcache, gregsetp, -1);
152 }
153
154 void
155 fill_fpregset (const struct regcache *regcache,
156                gdb_fpregset_t *fpregsetp, int regnum)
157 {
158   return;
159 }
160
161 void 
162 supply_fpregset (struct regcache *regcache,
163                  const gdb_fpregset_t *fpregsetp)
164 {
165   return;
166 }
167
168 /* Fetch greg-register(s) from process/thread TID
169    and store value(s) in GDB's register array.  */
170
171 static void
172 fetch_gregs (struct regcache *regcache, int regnum)
173 {
174   int tid = ptid_get_lwp (inferior_ptid);
175   const gdb_gregset_t regs;
176   int areg;
177   
178   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
179     {
180       perror_with_name (_("Couldn't get registers"));
181       return;
182     }
183  
184   supply_gregset_reg (regcache, &regs, regnum);
185 }
186
187 /* Store greg-register(s) in GDB's register 
188    array into the process/thread specified by TID.  */
189
190 static void
191 store_gregs (struct regcache *regcache, int regnum)
192 {
193   int tid = ptid_get_lwp (inferior_ptid);
194   gdb_gregset_t regs;
195   int areg;
196
197   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
198     {
199       perror_with_name (_("Couldn't get registers"));
200       return;
201     }
202
203   fill_gregset (regcache, &regs, regnum);
204
205   if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
206     {
207       perror_with_name (_("Couldn't write registers"));
208       return;
209     }
210 }
211
212 static int xtreg_lo;
213 static int xtreg_high;
214
215 /* Fetch/Store Xtensa TIE registers.  Xtensa GNU/Linux PTRACE
216    interface provides special requests for this.  */
217
218 static void
219 fetch_xtregs (struct regcache *regcache, int regnum)
220 {
221   int tid = ptid_get_lwp (inferior_ptid);
222   const xtensa_regtable_t *ptr;
223   char xtregs [XTENSA_ELF_XTREG_SIZE];
224
225   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
226     perror_with_name (_("Couldn't get extended registers"));
227
228   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
229     if (regnum == ptr->gdb_regnum || regnum == -1)
230       regcache_raw_supply (regcache, ptr->gdb_regnum,
231                            xtregs + ptr->ptrace_offset);
232 }
233
234 static void
235 store_xtregs (struct regcache *regcache, int regnum)
236 {
237   int tid = ptid_get_lwp (inferior_ptid);
238   const xtensa_regtable_t *ptr;
239   char xtregs [XTENSA_ELF_XTREG_SIZE];
240
241   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
242     perror_with_name (_("Couldn't get extended registers"));
243
244   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
245     if (regnum == ptr->gdb_regnum || regnum == -1)
246       regcache_raw_collect (regcache, ptr->gdb_regnum,
247                             xtregs + ptr->ptrace_offset);
248
249   if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
250     perror_with_name (_("Couldn't write extended registers"));
251 }
252
253 void
254 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
255                                        struct regcache *regcache, int regnum)
256 {
257   if (regnum == -1)
258     {
259       fetch_gregs (regcache, regnum);
260       fetch_xtregs (regcache, regnum);
261     }
262   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
263     fetch_gregs (regcache, regnum);
264   else
265     fetch_xtregs (regcache, regnum);
266 }
267
268 void
269 xtensa_linux_store_inferior_registers (struct target_ops *ops,
270                                        struct regcache *regcache, int regnum)
271 {
272   if (regnum == -1)
273     {
274       store_gregs (regcache, regnum);
275       store_xtregs (regcache, regnum);
276     }
277   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
278     store_gregs (regcache, regnum);
279   else
280     store_xtregs (regcache, regnum);
281 }
282
283 void _initialize_xtensa_linux_nat (void);
284
285 void
286 _initialize_xtensa_linux_nat (void)
287 {
288   struct target_ops *t;
289   const xtensa_regtable_t *ptr;
290
291   /* Calculate the number range for extended registers.  */
292   xtreg_lo = 1000000000;
293   xtreg_high = -1;
294   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
295     {
296       if (ptr->gdb_regnum < xtreg_lo)
297         xtreg_lo = ptr->gdb_regnum;
298       if (ptr->gdb_regnum > xtreg_high)
299         xtreg_high = ptr->gdb_regnum;
300     }
301
302   /* Fill in the generic GNU/Linux methods.  */
303   t = linux_target ();
304
305   /* Add our register access methods.  */
306   t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
307   t->to_store_registers = xtensa_linux_store_inferior_registers;
308
309   linux_nat_add_target (t);
310 }