1 /* Xtensa GNU/Linux native support.
3 Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include "linux-nat.h"
27 #include <sys/types.h>
30 #include <sys/ioctl.h>
33 #include <sys/procfs.h>
34 #include "nat/gdb_ptrace.h"
35 #include <asm/ptrace.h>
38 #include "xtensa-tdep.h"
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"
46 get_thread_id (ptid_t ptid
)
48 int tid
= ptid_get_lwp (ptid
);
50 tid
= ptid_get_pid (ptid
);
53 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
56 fill_gregset (const struct regcache
*regcache
,
57 gdb_gregset_t
*gregsetp
, int regnum
)
60 xtensa_elf_gregset_t
*regs
= (xtensa_elf_gregset_t
*) gregsetp
;
61 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
63 if (regnum
== gdbarch_pc_regnum (gdbarch
) || regnum
== -1)
64 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), ®s
->pc
);
65 if (regnum
== gdbarch_ps_regnum (gdbarch
) || regnum
== -1)
66 regcache_raw_collect (regcache
, gdbarch_ps_regnum (gdbarch
), ®s
->ps
);
68 if (regnum
== gdbarch_tdep (gdbarch
)->wb_regnum
|| regnum
== -1)
69 regcache_raw_collect (regcache
,
70 gdbarch_tdep (gdbarch
)->wb_regnum
,
72 if (regnum
== gdbarch_tdep (gdbarch
)->ws_regnum
|| regnum
== -1)
73 regcache_raw_collect (regcache
,
74 gdbarch_tdep (gdbarch
)->ws_regnum
,
76 if (regnum
== gdbarch_tdep (gdbarch
)->lbeg_regnum
|| regnum
== -1)
77 regcache_raw_collect (regcache
,
78 gdbarch_tdep (gdbarch
)->lbeg_regnum
,
80 if (regnum
== gdbarch_tdep (gdbarch
)->lend_regnum
|| regnum
== -1)
81 regcache_raw_collect (regcache
,
82 gdbarch_tdep (gdbarch
)->lend_regnum
,
84 if (regnum
== gdbarch_tdep (gdbarch
)->lcount_regnum
|| regnum
== -1)
85 regcache_raw_collect (regcache
,
86 gdbarch_tdep (gdbarch
)->lcount_regnum
,
88 if (regnum
== gdbarch_tdep (gdbarch
)->sar_regnum
|| regnum
== -1)
89 regcache_raw_collect (regcache
,
90 gdbarch_tdep (gdbarch
)->sar_regnum
,
92 if (regnum
>=gdbarch_tdep (gdbarch
)->ar_base
93 && regnum
< gdbarch_tdep (gdbarch
)->ar_base
94 + gdbarch_tdep (gdbarch
)->num_aregs
)
95 regcache_raw_collect (regcache
,regnum
,
96 ®s
->ar
[regnum
- gdbarch_tdep (gdbarch
)->ar_base
]);
97 else if (regnum
== -1)
99 for (i
= 0; i
< gdbarch_tdep (gdbarch
)->num_aregs
; ++i
)
100 regcache_raw_collect (regcache
,
101 gdbarch_tdep (gdbarch
)->ar_base
+ i
,
107 supply_gregset_reg (struct regcache
*regcache
,
108 const gdb_gregset_t
*gregsetp
, int regnum
)
111 xtensa_elf_gregset_t
*regs
= (xtensa_elf_gregset_t
*) gregsetp
;
113 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
115 if (regnum
== gdbarch_pc_regnum (gdbarch
) || regnum
== -1)
116 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
), ®s
->pc
);
117 if (regnum
== gdbarch_ps_regnum (gdbarch
) || regnum
== -1)
118 regcache_raw_supply (regcache
, gdbarch_ps_regnum (gdbarch
), ®s
->ps
);
120 if (regnum
== gdbarch_tdep (gdbarch
)->wb_regnum
|| regnum
== -1)
121 regcache_raw_supply (regcache
,
122 gdbarch_tdep (gdbarch
)->wb_regnum
,
124 if (regnum
== gdbarch_tdep (gdbarch
)->ws_regnum
|| regnum
== -1)
125 regcache_raw_supply (regcache
,
126 gdbarch_tdep (gdbarch
)->ws_regnum
,
128 if (regnum
== gdbarch_tdep (gdbarch
)->lbeg_regnum
|| regnum
== -1)
129 regcache_raw_supply (regcache
,
130 gdbarch_tdep (gdbarch
)->lbeg_regnum
,
132 if (regnum
== gdbarch_tdep (gdbarch
)->lend_regnum
|| regnum
== -1)
133 regcache_raw_supply (regcache
,
134 gdbarch_tdep (gdbarch
)->lend_regnum
,
136 if (regnum
== gdbarch_tdep (gdbarch
)->lcount_regnum
|| regnum
== -1)
137 regcache_raw_supply (regcache
,
138 gdbarch_tdep (gdbarch
)->lcount_regnum
,
140 if (regnum
== gdbarch_tdep (gdbarch
)->sar_regnum
|| regnum
== -1)
141 regcache_raw_supply (regcache
,
142 gdbarch_tdep (gdbarch
)->sar_regnum
,
144 if (regnum
>=gdbarch_tdep (gdbarch
)->ar_base
145 && regnum
< gdbarch_tdep (gdbarch
)->ar_base
146 + gdbarch_tdep (gdbarch
)->num_aregs
)
147 regcache_raw_supply (regcache
,regnum
,
148 ®s
->ar
[regnum
- gdbarch_tdep (gdbarch
)->ar_base
]);
149 else if (regnum
== -1)
151 for (i
= 0; i
< gdbarch_tdep (gdbarch
)->num_aregs
; ++i
)
152 regcache_raw_supply (regcache
,
153 gdbarch_tdep (gdbarch
)->ar_base
+ i
,
159 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
161 supply_gregset_reg (regcache
, gregsetp
, -1);
165 fill_fpregset (const struct regcache
*regcache
,
166 gdb_fpregset_t
*fpregsetp
, int regnum
)
172 supply_fpregset (struct regcache
*regcache
,
173 const gdb_fpregset_t
*fpregsetp
)
178 /* Fetch greg-register(s) from process/thread TID
179 and store value(s) in GDB's register array. */
182 fetch_gregs (struct regcache
*regcache
, int regnum
)
184 int tid
= GET_THREAD_ID (inferior_ptid
);
185 const gdb_gregset_t regs
;
188 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
190 perror_with_name (_("Couldn't get registers"));
194 supply_gregset_reg (regcache
, ®s
, regnum
);
197 /* Store greg-register(s) in GDB's register
198 array into the process/thread specified by TID. */
201 store_gregs (struct regcache
*regcache
, int regnum
)
203 int tid
= GET_THREAD_ID (inferior_ptid
);
207 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
209 perror_with_name (_("Couldn't get registers"));
213 fill_gregset (regcache
, ®s
, regnum
);
215 if (ptrace (PTRACE_SETREGS
, tid
, 0, (long) ®s
) < 0)
217 perror_with_name (_("Couldn't write registers"));
223 static int xtreg_high
;
225 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
226 interface provides special requests for this. */
229 fetch_xtregs (struct regcache
*regcache
, int regnum
)
231 int tid
= GET_THREAD_ID (inferior_ptid
);
232 const xtensa_regtable_t
*ptr
;
233 char xtregs
[XTENSA_ELF_XTREG_SIZE
];
235 if (ptrace (PTRACE_GETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
236 perror_with_name (_("Couldn't get extended registers"));
238 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
239 if (regnum
== ptr
->gdb_regnum
|| regnum
== -1)
240 regcache_raw_supply (regcache
, ptr
->gdb_regnum
,
241 xtregs
+ ptr
->ptrace_offset
);
245 store_xtregs (struct regcache
*regcache
, int regnum
)
247 int tid
= GET_THREAD_ID (inferior_ptid
);
248 const xtensa_regtable_t
*ptr
;
249 char xtregs
[XTENSA_ELF_XTREG_SIZE
];
251 if (ptrace (PTRACE_GETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
252 perror_with_name (_("Couldn't get extended registers"));
254 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
255 if (regnum
== ptr
->gdb_regnum
|| regnum
== -1)
256 regcache_raw_collect (regcache
, ptr
->gdb_regnum
,
257 xtregs
+ ptr
->ptrace_offset
);
259 if (ptrace (PTRACE_SETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
260 perror_with_name (_("Couldn't write extended registers"));
264 xtensa_linux_fetch_inferior_registers (struct target_ops
*ops
,
265 struct regcache
*regcache
, int regnum
)
269 fetch_gregs (regcache
, regnum
);
270 fetch_xtregs (regcache
, regnum
);
272 else if ((regnum
< xtreg_lo
) || (regnum
> xtreg_high
))
273 fetch_gregs (regcache
, regnum
);
275 fetch_xtregs (regcache
, regnum
);
279 xtensa_linux_store_inferior_registers (struct target_ops
*ops
,
280 struct regcache
*regcache
, int regnum
)
284 store_gregs (regcache
, regnum
);
285 store_xtregs (regcache
, regnum
);
287 else if ((regnum
< xtreg_lo
) || (regnum
> xtreg_high
))
288 store_gregs (regcache
, regnum
);
290 store_xtregs (regcache
, regnum
);
293 void _initialize_xtensa_linux_nat (void);
296 _initialize_xtensa_linux_nat (void)
298 struct target_ops
*t
;
299 const xtensa_regtable_t
*ptr
;
301 /* Calculate the number range for extended registers. */
302 xtreg_lo
= 1000000000;
304 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
306 if (ptr
->gdb_regnum
< xtreg_lo
)
307 xtreg_lo
= ptr
->gdb_regnum
;
308 if (ptr
->gdb_regnum
> xtreg_high
)
309 xtreg_high
= ptr
->gdb_regnum
;
312 /* Fill in the generic GNU/Linux methods. */
315 /* Add our register access methods. */
316 t
->to_fetch_registers
= xtensa_linux_fetch_inferior_registers
;
317 t
->to_store_registers
= xtensa_linux_store_inferior_registers
;
319 linux_nat_add_target (t
);