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