1 /* Xtensa GNU/Linux native support.
3 Copyright (C) 2007-2018 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 /* Defines ps_err_e, struct ps_prochandle. */
41 #include "gdb_proc_service.h"
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"
48 class xtensa_linux_nat_target final
: public linux_nat_target
51 /* Add our register access methods. */
52 void fetch_registers (struct regcache
*, int) override
;
53 void store_registers (struct regcache
*, int) override
;
56 static xtensa_linux_nat_target the_xtensa_linux_nat_target
;
59 fill_gregset (const struct regcache
*regcache
,
60 gdb_gregset_t
*gregsetp
, int regnum
)
63 xtensa_elf_gregset_t
*regs
= (xtensa_elf_gregset_t
*) gregsetp
;
64 struct gdbarch
*gdbarch
= regcache
->arch ();
66 if (regnum
== gdbarch_pc_regnum (gdbarch
) || regnum
== -1)
67 regcache_raw_collect (regcache
, gdbarch_pc_regnum (gdbarch
), ®s
->pc
);
68 if (regnum
== gdbarch_ps_regnum (gdbarch
) || regnum
== -1)
69 regcache_raw_collect (regcache
, gdbarch_ps_regnum (gdbarch
), ®s
->ps
);
71 if (regnum
== gdbarch_tdep (gdbarch
)->wb_regnum
|| regnum
== -1)
72 regcache_raw_collect (regcache
,
73 gdbarch_tdep (gdbarch
)->wb_regnum
,
75 if (regnum
== gdbarch_tdep (gdbarch
)->ws_regnum
|| regnum
== -1)
76 regcache_raw_collect (regcache
,
77 gdbarch_tdep (gdbarch
)->ws_regnum
,
79 if (regnum
== gdbarch_tdep (gdbarch
)->lbeg_regnum
|| regnum
== -1)
80 regcache_raw_collect (regcache
,
81 gdbarch_tdep (gdbarch
)->lbeg_regnum
,
83 if (regnum
== gdbarch_tdep (gdbarch
)->lend_regnum
|| regnum
== -1)
84 regcache_raw_collect (regcache
,
85 gdbarch_tdep (gdbarch
)->lend_regnum
,
87 if (regnum
== gdbarch_tdep (gdbarch
)->lcount_regnum
|| regnum
== -1)
88 regcache_raw_collect (regcache
,
89 gdbarch_tdep (gdbarch
)->lcount_regnum
,
91 if (regnum
== gdbarch_tdep (gdbarch
)->sar_regnum
|| regnum
== -1)
92 regcache_raw_collect (regcache
,
93 gdbarch_tdep (gdbarch
)->sar_regnum
,
95 if (regnum
== gdbarch_tdep (gdbarch
)->threadptr_regnum
|| regnum
== -1)
96 regcache_raw_collect (regcache
,
97 gdbarch_tdep (gdbarch
)->threadptr_regnum
,
99 if (regnum
>=gdbarch_tdep (gdbarch
)->ar_base
100 && regnum
< gdbarch_tdep (gdbarch
)->ar_base
101 + gdbarch_tdep (gdbarch
)->num_aregs
)
102 regcache_raw_collect (regcache
,regnum
,
103 ®s
->ar
[regnum
- gdbarch_tdep (gdbarch
)->ar_base
]);
104 else if (regnum
== -1)
106 for (i
= 0; i
< gdbarch_tdep (gdbarch
)->num_aregs
; ++i
)
107 regcache_raw_collect (regcache
,
108 gdbarch_tdep (gdbarch
)->ar_base
+ i
,
111 if (regnum
>= gdbarch_tdep (gdbarch
)->a0_base
112 && regnum
< gdbarch_tdep (gdbarch
)->a0_base
+ C0_NREGS
)
113 regcache_raw_collect (regcache
, regnum
,
114 ®s
->ar
[(4 * regs
->windowbase
+ regnum
115 - gdbarch_tdep (gdbarch
)->a0_base
)
116 % gdbarch_tdep (gdbarch
)->num_aregs
]);
117 else if (regnum
== -1)
119 for (i
= 0; i
< C0_NREGS
; ++i
)
120 regcache_raw_collect (regcache
,
121 gdbarch_tdep (gdbarch
)->a0_base
+ i
,
122 ®s
->ar
[(4 * regs
->windowbase
+ i
)
123 % gdbarch_tdep (gdbarch
)->num_aregs
]);
128 supply_gregset_reg (struct regcache
*regcache
,
129 const gdb_gregset_t
*gregsetp
, int regnum
)
132 xtensa_elf_gregset_t
*regs
= (xtensa_elf_gregset_t
*) gregsetp
;
134 struct gdbarch
*gdbarch
= regcache
->arch ();
136 if (regnum
== gdbarch_pc_regnum (gdbarch
) || regnum
== -1)
137 regcache_raw_supply (regcache
, gdbarch_pc_regnum (gdbarch
), ®s
->pc
);
138 if (regnum
== gdbarch_ps_regnum (gdbarch
) || regnum
== -1)
139 regcache_raw_supply (regcache
, gdbarch_ps_regnum (gdbarch
), ®s
->ps
);
141 if (regnum
== gdbarch_tdep (gdbarch
)->wb_regnum
|| regnum
== -1)
142 regcache_raw_supply (regcache
,
143 gdbarch_tdep (gdbarch
)->wb_regnum
,
145 if (regnum
== gdbarch_tdep (gdbarch
)->ws_regnum
|| regnum
== -1)
146 regcache_raw_supply (regcache
,
147 gdbarch_tdep (gdbarch
)->ws_regnum
,
149 if (regnum
== gdbarch_tdep (gdbarch
)->lbeg_regnum
|| regnum
== -1)
150 regcache_raw_supply (regcache
,
151 gdbarch_tdep (gdbarch
)->lbeg_regnum
,
153 if (regnum
== gdbarch_tdep (gdbarch
)->lend_regnum
|| regnum
== -1)
154 regcache_raw_supply (regcache
,
155 gdbarch_tdep (gdbarch
)->lend_regnum
,
157 if (regnum
== gdbarch_tdep (gdbarch
)->lcount_regnum
|| regnum
== -1)
158 regcache_raw_supply (regcache
,
159 gdbarch_tdep (gdbarch
)->lcount_regnum
,
161 if (regnum
== gdbarch_tdep (gdbarch
)->sar_regnum
|| regnum
== -1)
162 regcache_raw_supply (regcache
,
163 gdbarch_tdep (gdbarch
)->sar_regnum
,
165 if (regnum
== gdbarch_tdep (gdbarch
)->threadptr_regnum
|| regnum
== -1)
166 regcache_raw_supply (regcache
,
167 gdbarch_tdep (gdbarch
)->threadptr_regnum
,
169 if (regnum
>=gdbarch_tdep (gdbarch
)->ar_base
170 && regnum
< gdbarch_tdep (gdbarch
)->ar_base
171 + gdbarch_tdep (gdbarch
)->num_aregs
)
172 regcache_raw_supply (regcache
,regnum
,
173 ®s
->ar
[regnum
- gdbarch_tdep (gdbarch
)->ar_base
]);
174 else if (regnum
== -1)
176 for (i
= 0; i
< gdbarch_tdep (gdbarch
)->num_aregs
; ++i
)
177 regcache_raw_supply (regcache
,
178 gdbarch_tdep (gdbarch
)->ar_base
+ i
,
181 if (regnum
>= gdbarch_tdep (gdbarch
)->a0_base
182 && regnum
< gdbarch_tdep (gdbarch
)->a0_base
+ C0_NREGS
)
183 regcache_raw_supply (regcache
, regnum
,
184 ®s
->ar
[(4 * regs
->windowbase
+ regnum
185 - gdbarch_tdep (gdbarch
)->a0_base
)
186 % gdbarch_tdep (gdbarch
)->num_aregs
]);
187 else if (regnum
== -1)
189 for (i
= 0; i
< C0_NREGS
; ++i
)
190 regcache_raw_supply (regcache
,
191 gdbarch_tdep (gdbarch
)->a0_base
+ i
,
192 ®s
->ar
[(4 * regs
->windowbase
+ i
)
193 % gdbarch_tdep (gdbarch
)->num_aregs
]);
198 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
200 supply_gregset_reg (regcache
, gregsetp
, -1);
204 fill_fpregset (const struct regcache
*regcache
,
205 gdb_fpregset_t
*fpregsetp
, int regnum
)
211 supply_fpregset (struct regcache
*regcache
,
212 const gdb_fpregset_t
*fpregsetp
)
217 /* Fetch greg-register(s) from process/thread TID
218 and store value(s) in GDB's register array. */
221 fetch_gregs (struct regcache
*regcache
, int regnum
)
223 int tid
= ptid_get_lwp (regcache_get_ptid (regcache
));
227 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
229 perror_with_name (_("Couldn't get registers"));
233 supply_gregset_reg (regcache
, ®s
, regnum
);
236 /* Store greg-register(s) in GDB's register
237 array into the process/thread specified by TID. */
240 store_gregs (struct regcache
*regcache
, int regnum
)
242 int tid
= ptid_get_lwp (regcache_get_ptid (regcache
));
246 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
248 perror_with_name (_("Couldn't get registers"));
252 fill_gregset (regcache
, ®s
, regnum
);
254 if (ptrace (PTRACE_SETREGS
, tid
, 0, (long) ®s
) < 0)
256 perror_with_name (_("Couldn't write registers"));
262 static int xtreg_high
;
264 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
265 interface provides special requests for this. */
268 fetch_xtregs (struct regcache
*regcache
, int regnum
)
270 int tid
= ptid_get_lwp (regcache_get_ptid (regcache
));
271 const xtensa_regtable_t
*ptr
;
272 char xtregs
[XTENSA_ELF_XTREG_SIZE
];
274 if (ptrace (PTRACE_GETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
275 perror_with_name (_("Couldn't get extended registers"));
277 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
278 if (regnum
== ptr
->gdb_regnum
|| regnum
== -1)
279 regcache_raw_supply (regcache
, ptr
->gdb_regnum
,
280 xtregs
+ ptr
->ptrace_offset
);
284 store_xtregs (struct regcache
*regcache
, int regnum
)
286 int tid
= ptid_get_lwp (regcache_get_ptid (regcache
));
287 const xtensa_regtable_t
*ptr
;
288 char xtregs
[XTENSA_ELF_XTREG_SIZE
];
290 if (ptrace (PTRACE_GETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
291 perror_with_name (_("Couldn't get extended registers"));
293 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
294 if (regnum
== ptr
->gdb_regnum
|| regnum
== -1)
295 regcache_raw_collect (regcache
, ptr
->gdb_regnum
,
296 xtregs
+ ptr
->ptrace_offset
);
298 if (ptrace (PTRACE_SETXTREGS
, tid
, 0, (long)&xtregs
) < 0)
299 perror_with_name (_("Couldn't write extended registers"));
303 xtensa_linux_nat_target::fetch_registers (struct regcache
*regcache
,
308 fetch_gregs (regcache
, regnum
);
309 fetch_xtregs (regcache
, regnum
);
311 else if ((regnum
< xtreg_lo
) || (regnum
> xtreg_high
))
312 fetch_gregs (regcache
, regnum
);
314 fetch_xtregs (regcache
, regnum
);
318 xtensa_linux_nat_target::store_registers (struct regcache
*regcache
,
323 store_gregs (regcache
, regnum
);
324 store_xtregs (regcache
, regnum
);
326 else if ((regnum
< xtreg_lo
) || (regnum
> xtreg_high
))
327 store_gregs (regcache
, regnum
);
329 store_xtregs (regcache
, regnum
);
332 /* Called by libthread_db. */
335 ps_get_thread_area (struct ps_prochandle
*ph
,
336 lwpid_t lwpid
, int idx
, void **base
)
338 xtensa_elf_gregset_t regs
;
340 if (ptrace (PTRACE_GETREGS
, lwpid
, NULL
, ®s
) != 0)
343 /* IDX is the bias from the thread pointer to the beginning of the
344 thread descriptor. It has to be subtracted due to implementation
345 quirks in libthread_db. */
346 *base
= (void *) ((char *) regs
.threadptr
- idx
);
352 _initialize_xtensa_linux_nat (void)
354 const xtensa_regtable_t
*ptr
;
356 /* Calculate the number range for extended registers. */
357 xtreg_lo
= 1000000000;
359 for (ptr
= xtensa_regmap_table
; ptr
->name
; ptr
++)
361 if (ptr
->gdb_regnum
< xtreg_lo
)
362 xtreg_lo
= ptr
->gdb_regnum
;
363 if (ptr
->gdb_regnum
> xtreg_high
)
364 xtreg_high
= ptr
->gdb_regnum
;
367 linux_target
= &the_xtensa_linux_nat_target
;
368 add_inf_child_target (&the_xtensa_linux_nat_target
);