Include gdb_assert.h in common-defs.h
[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 <string.h>
22 #include "frame.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "target.h"
27 #include "linux-nat.h"
28
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <signal.h>
32 #include <sys/user.h>
33 #include <sys/ioctl.h>
34 #include "gdb_wait.h"
35 #include <fcntl.h>
36 #include <sys/procfs.h>
37 #include <sys/ptrace.h>
38 #include <asm/ptrace.h>
39
40 #include "gregset.h"
41 #include "xtensa-tdep.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 static int
49 get_thread_id (ptid_t ptid)
50 {
51   int tid = ptid_get_lwp (ptid);
52   if (0 == tid)
53     tid = ptid_get_pid (ptid);
54   return tid;
55 }
56 #define GET_THREAD_ID(PTID)     get_thread_id (PTID)
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 = get_regcache_arch (regcache);
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)->ar_base
96       && regnum < gdbarch_tdep (gdbarch)->ar_base
97                     + gdbarch_tdep (gdbarch)->num_aregs)
98     regcache_raw_collect (regcache,regnum,
99                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
100   else if (regnum == -1)
101     {
102       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
103         regcache_raw_collect (regcache,
104                               gdbarch_tdep (gdbarch)->ar_base + i,
105                               &regs->ar[i]);
106     }
107 }
108
109 void
110 supply_gregset_reg (struct regcache *regcache,
111                     const gdb_gregset_t *gregsetp, int regnum)
112 {
113   int i;
114   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
115
116   struct gdbarch *gdbarch = get_regcache_arch (regcache);
117
118   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
119     regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
120   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
121     regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
122
123   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
124     regcache_raw_supply (regcache,
125                           gdbarch_tdep (gdbarch)->wb_regnum,
126                           &regs->windowbase);
127   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
128     regcache_raw_supply (regcache,
129                           gdbarch_tdep (gdbarch)->ws_regnum,
130                           &regs->windowstart);
131   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
132     regcache_raw_supply (regcache,
133                           gdbarch_tdep (gdbarch)->lbeg_regnum,
134                           &regs->lbeg);
135   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
136     regcache_raw_supply (regcache,
137                           gdbarch_tdep (gdbarch)->lend_regnum,
138                           &regs->lend);
139   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
140     regcache_raw_supply (regcache,
141                           gdbarch_tdep (gdbarch)->lcount_regnum,
142                           &regs->lcount);
143   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
144     regcache_raw_supply (regcache,
145                           gdbarch_tdep (gdbarch)->sar_regnum,
146                           &regs->sar);
147   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
148       && regnum < gdbarch_tdep (gdbarch)->ar_base
149                     + gdbarch_tdep (gdbarch)->num_aregs)
150     regcache_raw_supply (regcache,regnum,
151                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
152   else if (regnum == -1)
153     {
154       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
155         regcache_raw_supply (regcache,
156                               gdbarch_tdep (gdbarch)->ar_base + i,
157                               &regs->ar[i]);
158     }
159 }
160
161 void
162 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
163 {
164   supply_gregset_reg (regcache, gregsetp, -1);
165 }
166
167 void
168 fill_fpregset (const struct regcache *regcache,
169                gdb_fpregset_t *fpregsetp, int regnum)
170 {
171   return;
172 }
173
174 void 
175 supply_fpregset (struct regcache *regcache,
176                  const gdb_fpregset_t *fpregsetp)
177 {
178   return;
179 }
180
181 /* Fetch greg-register(s) from process/thread TID
182    and store value(s) in GDB's register array.  */
183
184 static void
185 fetch_gregs (struct regcache *regcache, int regnum)
186 {
187   int tid = GET_THREAD_ID (inferior_ptid);
188   const gdb_gregset_t regs;
189   int areg;
190   
191   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
192     {
193       perror_with_name (_("Couldn't get registers"));
194       return;
195     }
196  
197   supply_gregset_reg (regcache, &regs, regnum);
198 }
199
200 /* Store greg-register(s) in GDB's register 
201    array into the process/thread specified by TID.  */
202
203 static void
204 store_gregs (struct regcache *regcache, int regnum)
205 {
206   int tid = GET_THREAD_ID (inferior_ptid);
207   gdb_gregset_t regs;
208   int areg;
209
210   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
211     {
212       perror_with_name (_("Couldn't get registers"));
213       return;
214     }
215
216   fill_gregset (regcache, &regs, regnum);
217
218   if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
219     {
220       perror_with_name (_("Couldn't write registers"));
221       return;
222     }
223 }
224
225 static int xtreg_lo;
226 static int xtreg_high;
227
228 /* Fetch/Store Xtensa TIE registers.  Xtensa GNU/Linux PTRACE
229    interface provides special requests for this.  */
230
231 static void
232 fetch_xtregs (struct regcache *regcache, int regnum)
233 {
234   int tid = GET_THREAD_ID (inferior_ptid);
235   const xtensa_regtable_t *ptr;
236   char xtregs [XTENSA_ELF_XTREG_SIZE];
237
238   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
239     perror_with_name (_("Couldn't get extended registers"));
240
241   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
242     if (regnum == ptr->gdb_regnum || regnum == -1)
243       regcache_raw_supply (regcache, ptr->gdb_regnum,
244                            xtregs + ptr->ptrace_offset);
245 }
246
247 static void
248 store_xtregs (struct regcache *regcache, int regnum)
249 {
250   int tid = GET_THREAD_ID (inferior_ptid);
251   const xtensa_regtable_t *ptr;
252   char xtregs [XTENSA_ELF_XTREG_SIZE];
253
254   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
255     perror_with_name (_("Couldn't get extended registers"));
256
257   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
258     if (regnum == ptr->gdb_regnum || regnum == -1)
259       regcache_raw_collect (regcache, ptr->gdb_regnum,
260                             xtregs + ptr->ptrace_offset);
261
262   if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
263     perror_with_name (_("Couldn't write extended registers"));
264 }
265
266 void
267 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
268                                        struct regcache *regcache, int regnum)
269 {
270   if (regnum == -1)
271     {
272       fetch_gregs (regcache, regnum);
273       fetch_xtregs (regcache, regnum);
274     }
275   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
276     fetch_gregs (regcache, regnum);
277   else
278     fetch_xtregs (regcache, regnum);
279 }
280
281 void
282 xtensa_linux_store_inferior_registers (struct target_ops *ops,
283                                        struct regcache *regcache, int regnum)
284 {
285   if (regnum == -1)
286     {
287       store_gregs (regcache, regnum);
288       store_xtregs (regcache, regnum);
289     }
290   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
291     store_gregs (regcache, regnum);
292   else
293     store_xtregs (regcache, regnum);
294 }
295
296 void _initialize_xtensa_linux_nat (void);
297
298 void
299 _initialize_xtensa_linux_nat (void)
300 {
301   struct target_ops *t;
302   const xtensa_regtable_t *ptr;
303
304   /* Calculate the number range for extended registers.  */
305   xtreg_lo = 1000000000;
306   xtreg_high = -1;
307   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
308     {
309       if (ptr->gdb_regnum < xtreg_lo)
310         xtreg_lo = ptr->gdb_regnum;
311       if (ptr->gdb_regnum > xtreg_high)
312         xtreg_high = ptr->gdb_regnum;
313     }
314
315   /* Fill in the generic GNU/Linux methods.  */
316   t = linux_target ();
317
318   /* Add our register access methods.  */
319   t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
320   t->to_store_registers = xtensa_linux_store_inferior_registers;
321
322   linux_nat_add_target (t);
323 }