1 /* Native-dependent code for GNU/Linux ARC.
3 Copyright 2020-2021 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/>. */
25 #include "gdbsupport/gdb_assert.h"
27 #include "linux-nat.h"
28 #include "nat/gdb_ptrace.h"
31 #include <sys/types.h>
32 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include "gdbsupport/gdb_wait.h"
38 #include <sys/procfs.h>
39 #include <linux/elf.h>
43 #include "arc-linux-tdep.h"
46 /* Defines ps_err_e, struct ps_prochandle. */
47 #include "gdb_proc_service.h"
49 /* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30,
50 R58 and R59 registers, which are specific to ARC HS and aren't
51 available in ARC 700. */
52 #if defined (NT_ARC_V2) && defined (__ARCHS__)
53 #define ARC_HAS_V2_REGSET
56 class arc_linux_nat_target final
: public linux_nat_target
59 /* Add ARC register access methods. */
60 void fetch_registers (struct regcache
*, int) override
;
61 void store_registers (struct regcache
*, int) override
;
63 const struct target_desc
*read_description () override
;
66 void low_prepare_to_resume (struct lwp_info
*lp
) override
;
69 static arc_linux_nat_target the_arc_linux_nat_target
;
71 /* Read general registers from target process/thread (via ptrace)
75 fetch_gregs (struct regcache
*regcache
, int regnum
)
77 const int tid
= get_ptrace_pid (regcache
->ptid ());
82 iov
.iov_len
= sizeof (gdb_gregset_t
);
84 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, (void *) &iov
) < 0)
85 perror_with_name (_("Couldn't get general registers"));
87 arc_linux_supply_gregset (NULL
, regcache
, regnum
, ®s
, 0);
90 #ifdef ARC_HAS_V2_REGSET
91 /* Read ARC v2 registers from target process/thread (via ptrace)
95 fetch_v2_regs (struct regcache
*regcache
, int regnum
)
97 const int tid
= get_ptrace_pid (regcache
->ptid ());
99 bfd_byte v2_buffer
[ARC_LINUX_SIZEOF_V2_REGSET
];
101 iov
.iov_base
= &v2_buffer
;
102 iov
.iov_len
= ARC_LINUX_SIZEOF_V2_REGSET
;
104 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARC_V2
, (void *) &iov
) < 0)
105 perror_with_name (_("Couldn't get ARC HS registers"));
107 arc_linux_supply_v2_regset (NULL
, regcache
, regnum
, v2_buffer
, 0);
111 /* Store general registers from REGCACHE into the target process/thread. */
114 store_gregs (const struct regcache
*regcache
, int regnum
)
116 const int tid
= get_ptrace_pid (regcache
->ptid ());
120 iov
.iov_base
= ®s
;
121 iov
.iov_len
= sizeof (gdb_gregset_t
);
123 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, (void *) &iov
) < 0)
124 perror_with_name (_("Couldn't get general registers"));
127 arc_linux_collect_gregset (NULL
, regcache
, regnum
, regs
, 0);
129 if (ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, (void *) &iov
) < 0)
130 perror_with_name (_("Couldn't write general registers"));
134 #ifdef ARC_HAS_V2_REGSET
135 /* Store ARC v2 registers from REGCACHE into the target process/thread. */
138 store_v2_regs (const struct regcache
*regcache
, int regnum
)
140 const int tid
= get_ptrace_pid (regcache
->ptid ());
142 bfd_byte v2_buffer
[ARC_LINUX_SIZEOF_V2_REGSET
];
144 iov
.iov_base
= &v2_buffer
;
145 iov
.iov_len
= ARC_LINUX_SIZEOF_V2_REGSET
;
147 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARC_V2
, (void *) &iov
) < 0)
148 perror_with_name (_("Couldn't get ARC HS registers"));
151 arc_linux_collect_v2_regset (NULL
, regcache
, regnum
, v2_buffer
, 0);
153 if (ptrace (PTRACE_SETREGSET
, tid
, NT_ARC_V2
, (void *) &iov
) < 0)
154 perror_with_name (_("Couldn't write ARC HS registers"));
159 /* Target operation: Read REGNUM register (all registers if REGNUM == -1)
160 from target process into REGCACHE. */
163 arc_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regnum
)
166 if (regnum
== -1 || regnum
<= ARC_LAST_REGNUM
)
167 fetch_gregs (regcache
, regnum
);
169 #ifdef ARC_HAS_V2_REGSET
171 || regnum
== ARC_R30_REGNUM
172 || regnum
== ARC_R58_REGNUM
173 || regnum
== ARC_R59_REGNUM
)
174 fetch_v2_regs (regcache
, regnum
);
178 /* Target operation: Store REGNUM register (all registers if REGNUM == -1)
179 to the target process from REGCACHE. */
182 arc_linux_nat_target::store_registers (struct regcache
*regcache
, int regnum
)
184 if (regnum
== -1 || regnum
<= ARC_LAST_REGNUM
)
185 store_gregs (regcache
, regnum
);
187 #ifdef ARC_HAS_V2_REGSET
189 || regnum
== ARC_R30_REGNUM
190 || regnum
== ARC_R58_REGNUM
191 || regnum
== ARC_R59_REGNUM
)
192 store_v2_regs (regcache
, regnum
);
196 /* Copy general purpose register(s) from REGCACHE into regset GREGS.
197 This function is exported to proc-service.c */
200 fill_gregset (const struct regcache
*regcache
,
201 gdb_gregset_t
*gregs
, int regnum
)
203 arc_linux_collect_gregset (NULL
, regcache
, regnum
, gregs
, 0);
206 /* Copy all the general purpose registers from regset GREGS into REGCACHE.
207 This function is exported to proc-service.c. */
210 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregs
)
212 arc_linux_supply_gregset (NULL
, regcache
, -1, gregs
, 0);
215 /* ARC doesn't have separate FP registers. This function is exported
216 to proc-service.c. */
219 fill_fpregset (const struct regcache
*regcache
,
220 gdb_fpregset_t
*fpregsetp
, int regnum
)
223 debug_printf ("arc-linux-nat: fill_fpregset called.");
227 /* ARC doesn't have separate FP registers. This function is exported
228 to proc-service.c. */
231 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
234 debug_printf ("arc-linux-nat: supply_fpregset called.");
238 /* Implement the "read_description" method of linux_nat_target. */
240 const struct target_desc
*
241 arc_linux_nat_target::read_description ()
243 /* This is a native target, hence description is hardcoded. */
245 arc_arch_features
features (4, ARC_ISA_ARCV2
);
247 arc_arch_features
features (4, ARC_ISA_ARCV1
);
249 return arc_lookup_target_description (features
);
252 /* As described in arc_linux_collect_gregset(), we need to write resume-PC
253 to ERET. However by default GDB for native targets doesn't write
254 registers if they haven't been changed. This is a callback called by
255 generic GDB, and in this callback we have to rewrite PC value so it
256 would force rewrite of register on target. It seems that the only
257 other arch that utilizes this hook is x86/x86-64 for HW breakpoint
258 support. But then, AFAIK no other arch has this stop_pc/eret
261 No better way was found, other than this fake write of register value,
262 to force GDB into writing register to target. Is there any? */
265 arc_linux_nat_target::low_prepare_to_resume (struct lwp_info
*lwp
)
267 /* When new processes and threads are created we do not have the address
268 space for them and calling get_thread_regcache will cause an internal
269 error in GDB. It looks like that checking for last_resume_kind is the
270 sensible way to determine processes for which we cannot get regcache.
271 Ultimately, a better way would be removing the need for
272 low_prepare_to_resume in the first place. */
273 if (lwp
->last_resume_kind
== resume_stop
)
276 struct regcache
*regcache
= get_thread_regcache (this, lwp
->ptid
);
277 struct gdbarch
*gdbarch
= regcache
->arch ();
279 /* Read current PC value, then write it back. It is required to call
280 invalidate(), otherwise GDB will note that new value is equal to old
281 value and will skip write. */
283 regcache_cooked_read_unsigned (regcache
, gdbarch_pc_regnum (gdbarch
),
285 regcache
->invalidate (gdbarch_pc_regnum (gdbarch
));
286 regcache_cooked_write_unsigned (regcache
, gdbarch_pc_regnum (gdbarch
),
290 /* Fetch the thread-local storage pointer for libthread_db. Note that
291 this function is not called from GDB, but is called from libthread_db.
292 This is required to debug multithreaded applications with NPTL. */
295 ps_get_thread_area (struct ps_prochandle
*ph
, lwpid_t lwpid
, int idx
,
299 debug_printf ("arc-linux-nat: ps_get_thread_area called");
301 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
304 /* IDX is the bias from the thread pointer to the beginning of the
305 thread descriptor. It has to be subtracted due to implementation
306 quirks in libthread_db. */
307 *base
= (void *) ((char *) *base
- idx
);
312 /* Suppress warning from -Wmissing-prototypes. */
313 void _initialize_arc_linux_nat ();
315 _initialize_arc_linux_nat ()
317 /* Register the target. */
318 linux_target
= &the_arc_linux_nat_target
;
319 add_inf_child_target (&the_arc_linux_nat_target
);