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"
29 #include <sys/types.h>
32 #include <sys/ioctl.h>
35 #include <sys/procfs.h>
36 #include <sys/ptrace.h>
37 #include <asm/ptrace.h>
40 #include "xtensa-tdep.h"
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"
48 get_thread_id (ptid_t ptid
)
50 int tid
= ptid_get_lwp (ptid
);
52 tid
= ptid_get_pid (ptid
);
55 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
58 fill_gregset (const struct regcache
*regcache
,
59 gdb_gregset_t
*gregsetp
, int regnum
)
62 xtensa_elf_gregset_t
*regs
= (xtensa_elf_gregset_t
*) gregsetp
;
63 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
65 if (regnum
== gdbarch_pc_regnum (gdbarch
) || regnum
== -1)
66 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), ®s
->pc
);
67 if (regnum
== gdbarch_ps_regnum (gdbarch
) || regnum
== -1)
68 regcache_raw_collect (regcache
, gdbarch_ps_regnum (gdbarch
), ®s
->ps
);
70 if (regnum
== gdbarch_tdep (gdbarch
)->wb_regnum
|| regnum
== -1)
71 regcache_raw_collect (regcache
,
72 gdbarch_tdep (gdbarch
)->wb_regnum
,
74 if (regnum
== gdbarch_tdep (gdbarch
)->ws_regnum
|| regnum
== -1)
75 regcache_raw_collect (regcache
,
76 gdbarch_tdep (gdbarch
)->ws_regnum
,
78 if (regnum
== gdbarch_tdep (gdbarch
)->lbeg_regnum
|| regnum
== -1)
79 regcache_raw_collect (regcache
,
80 gdbarch_tdep (gdbarch
)->lbeg_regnum
,
82 if (regnum
== gdbarch_tdep (gdbarch
)->lend_regnum
|| regnum
== -1)
83 regcache_raw_collect (regcache
,
84 gdbarch_tdep (gdbarch
)->lend_regnum
,
86 if (regnum
== gdbarch_tdep (gdbarch
)->lcount_regnum
|| regnum
== -1)
87 regcache_raw_collect (regcache
,
88 gdbarch_tdep (gdbarch
)->lcount_regnum
,
90 if (regnum
== gdbarch_tdep (gdbarch
)->sar_regnum
|| regnum
== -1)
91 regcache_raw_collect (regcache
,
92 gdbarch_tdep (gdbarch
)->sar_regnum
,
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 ®s
->ar
[regnum
- gdbarch_tdep (gdbarch
)->ar_base
]);
99 else if (regnum
== -1)
101 for (i
= 0; i
< gdbarch_tdep (gdbarch
)->num_aregs
; ++i
)
102 regcache_raw_collect (regcache
,
103 gdbarch_tdep (gdbarch
)->ar_base
+ i
,
109 supply_gregset_reg (struct regcache
*regcache
,
110 const gdb_gregset_t
*gregsetp
, int regnum
)
113 xtensa_elf_gregset_t
*regs
= (xtensa_elf_gregset_t
*) gregsetp
;
115 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
117 if (regnum
== gdbarch_pc_regnum (gdbarch
) || regnum
== -1)
118 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
), ®s
->pc
);
119 if (regnum
== gdbarch_ps_regnum (gdbarch
) || regnum
== -1)
120 regcache_raw_supply (regcache
, gdbarch_ps_regnum (gdbarch
), ®s
->ps
);
122 if (regnum
== gdbarch_tdep (gdbarch
)->wb_regnum
|| regnum
== -1)
123 regcache_raw_supply (regcache
,
124 gdbarch_tdep (gdbarch
)->wb_regnum
,
126 if (regnum
== gdbarch_tdep (gdbarch
)->ws_regnum
|| regnum
== -1)
127 regcache_raw_supply (regcache
,
128 gdbarch_tdep (gdbarch
)->ws_regnum
,
130 if (regnum
== gdbarch_tdep (gdbarch
)->lbeg_regnum
|| regnum
== -1)
131 regcache_raw_supply (regcache
,
132 gdbarch_tdep (gdbarch
)->lbeg_regnum
,
134 if (regnum
== gdbarch_tdep (gdbarch
)->lend_regnum
|| regnum
== -1)
135 regcache_raw_supply (regcache
,
136 gdbarch_tdep (gdbarch
)->lend_regnum
,
138 if (regnum
== gdbarch_tdep (gdbarch
)->lcount_regnum
|| regnum
== -1)
139 regcache_raw_supply (regcache
,
140 gdbarch_tdep (gdbarch
)->lcount_regnum
,
142 if (regnum
== gdbarch_tdep (gdbarch
)->sar_regnum
|| regnum
== -1)
143 regcache_raw_supply (regcache
,
144 gdbarch_tdep (gdbarch
)->sar_regnum
,
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 ®s
->ar
[regnum
- gdbarch_tdep (gdbarch
)->ar_base
]);
151 else if (regnum
== -1)
153 for (i
= 0; i
< gdbarch_tdep (gdbarch
)->num_aregs
; ++i
)
154 regcache_raw_supply (regcache
,
155 gdbarch_tdep (gdbarch
)->ar_base
+ i
,
161 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
163 supply_gregset_reg (regcache
, gregsetp
, -1);
167 fill_fpregset (const struct regcache
*regcache
,
168 gdb_fpregset_t
*fpregsetp
, int regnum
)
174 supply_fpregset (struct regcache
*regcache
,
175 const gdb_fpregset_t
*fpregsetp
)
180 /* Fetch greg-register(s) from process/thread TID
181 and store value(s) in GDB's register array. */
184 fetch_gregs (struct regcache
*regcache
, int regnum
)
186 int tid
= GET_THREAD_ID (inferior_ptid
);
187 const gdb_gregset_t regs
;
190 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
192 perror_with_name (_("Couldn't get registers"));
196 supply_gregset_reg (regcache
, ®s
, regnum
);
199 /* Store greg-register(s) in GDB's register
200 array into the process/thread specified by TID. */
203 store_gregs (struct regcache
*regcache
, int regnum
)
205 int tid
= GET_THREAD_ID (inferior_ptid
);
209 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
211 perror_with_name (_("Couldn't get registers"));
215 fill_gregset (regcache
, ®s
, regnum
);
217 if (ptrace (PTRACE_SETREGS
, tid
, 0, (long) ®s
) < 0)
219 perror_with_name (_("Couldn't write registers"));
225 static int xtreg_high
;
227 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
228 interface provides special requests for this. */
231 fetch_xtregs (struct regcache
*regcache
, int regnum
)
233 int tid
= GET_THREAD_ID (inferior_ptid
);
234 const xtensa_regtable_t
*ptr
;
235 char xtregs
[XTENSA_ELF_XTREG_SIZE
];
237 if (ptrace (PTRACE_GETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
238 perror_with_name (_("Couldn't get extended registers"));
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
);
247 store_xtregs (struct regcache
*regcache
, int regnum
)
249 int tid
= GET_THREAD_ID (inferior_ptid
);
250 const xtensa_regtable_t
*ptr
;
251 char xtregs
[XTENSA_ELF_XTREG_SIZE
];
253 if (ptrace (PTRACE_GETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
254 perror_with_name (_("Couldn't get extended registers"));
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
);
261 if (ptrace (PTRACE_SETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
262 perror_with_name (_("Couldn't write extended registers"));
266 xtensa_linux_fetch_inferior_registers (struct target_ops
*ops
,
267 struct regcache
*regcache
, int regnum
)
271 fetch_gregs (regcache
, regnum
);
272 fetch_xtregs (regcache
, regnum
);
274 else if ((regnum
< xtreg_lo
) || (regnum
> xtreg_high
))
275 fetch_gregs (regcache
, regnum
);
277 fetch_xtregs (regcache
, regnum
);
281 xtensa_linux_store_inferior_registers (struct target_ops
*ops
,
282 struct regcache
*regcache
, int regnum
)
286 store_gregs (regcache
, regnum
);
287 store_xtregs (regcache
, regnum
);
289 else if ((regnum
< xtreg_lo
) || (regnum
> xtreg_high
))
290 store_gregs (regcache
, regnum
);
292 store_xtregs (regcache
, regnum
);
295 void _initialize_xtensa_linux_nat (void);
298 _initialize_xtensa_linux_nat (void)
300 struct target_ops
*t
;
301 const xtensa_regtable_t
*ptr
;
303 /* Calculate the number range for extended registers. */
304 xtreg_lo
= 1000000000;
306 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
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
;
314 /* Fill in the generic GNU/Linux methods. */
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
;
321 linux_nat_add_target (t
);