1 /* Native-dependent code for GNU/Linux ARC.
3 Copyright 2020-2024 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/>. */
24 #include "gdbsupport/gdb_assert.h"
26 #include "linux-nat.h"
27 #include "nat/gdb_ptrace.h"
30 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include "gdbsupport/gdb_wait.h"
37 #include <sys/procfs.h>
38 #include <linux/elf.h>
42 #include "arc-linux-tdep.h"
45 /* Defines ps_err_e, struct ps_prochandle. */
46 #include "gdb_proc_service.h"
48 /* Print an "arc-linux-nat" debug statement. */
50 #define arc_linux_nat_debug_printf(fmt, ...) \
51 debug_prefixed_printf_cond (arc_debug, "arc-linux-nat", fmt, ##__VA_ARGS__)
53 /* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30,
54 R58 and R59 registers, which are specific to ARC HS and aren't
55 available in ARC 700. */
56 #if defined (NT_ARC_V2) && defined (__ARCHS__)
57 #define ARC_HAS_V2_REGSET
60 class arc_linux_nat_target final
: public linux_nat_target
63 /* Add ARC register access methods. */
64 void fetch_registers (struct regcache
*, int) override
;
65 void store_registers (struct regcache
*, int) override
;
67 const struct target_desc
*read_description () override
;
70 void low_prepare_to_resume (struct lwp_info
*lp
) override
;
73 static arc_linux_nat_target the_arc_linux_nat_target
;
75 /* Read general registers from target process/thread (via ptrace)
79 fetch_gregs (struct regcache
*regcache
, int regnum
)
81 const int tid
= get_ptrace_pid (regcache
->ptid ());
86 iov
.iov_len
= sizeof (gdb_gregset_t
);
88 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, (void *) &iov
) < 0)
89 perror_with_name (_("Couldn't get general registers"));
91 arc_linux_supply_gregset (NULL
, regcache
, regnum
, ®s
, 0);
94 #ifdef ARC_HAS_V2_REGSET
95 /* Read ARC v2 registers from target process/thread (via ptrace)
99 fetch_v2_regs (struct regcache
*regcache
, int regnum
)
101 const int tid
= get_ptrace_pid (regcache
->ptid ());
103 bfd_byte v2_buffer
[ARC_LINUX_SIZEOF_V2_REGSET
];
105 iov
.iov_base
= &v2_buffer
;
106 iov
.iov_len
= ARC_LINUX_SIZEOF_V2_REGSET
;
108 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARC_V2
, (void *) &iov
) < 0)
109 perror_with_name (_("Couldn't get ARC HS registers"));
111 arc_linux_supply_v2_regset (NULL
, regcache
, regnum
, v2_buffer
, 0);
115 /* Store general registers from REGCACHE into the target process/thread. */
118 store_gregs (const struct regcache
*regcache
, int regnum
)
120 const int tid
= get_ptrace_pid (regcache
->ptid ());
124 iov
.iov_base
= ®s
;
125 iov
.iov_len
= sizeof (gdb_gregset_t
);
127 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
, (void *) &iov
) < 0)
128 perror_with_name (_("Couldn't get general registers"));
131 arc_linux_collect_gregset (NULL
, regcache
, regnum
, regs
, 0);
133 if (ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
, (void *) &iov
) < 0)
134 perror_with_name (_("Couldn't write general registers"));
138 #ifdef ARC_HAS_V2_REGSET
139 /* Store ARC v2 registers from REGCACHE into the target process/thread. */
142 store_v2_regs (const struct regcache
*regcache
, int regnum
)
144 const int tid
= get_ptrace_pid (regcache
->ptid ());
146 bfd_byte v2_buffer
[ARC_LINUX_SIZEOF_V2_REGSET
];
148 iov
.iov_base
= &v2_buffer
;
149 iov
.iov_len
= ARC_LINUX_SIZEOF_V2_REGSET
;
151 if (ptrace (PTRACE_GETREGSET
, tid
, NT_ARC_V2
, (void *) &iov
) < 0)
152 perror_with_name (_("Couldn't get ARC HS registers"));
155 arc_linux_collect_v2_regset (NULL
, regcache
, regnum
, v2_buffer
, 0);
157 if (ptrace (PTRACE_SETREGSET
, tid
, NT_ARC_V2
, (void *) &iov
) < 0)
158 perror_with_name (_("Couldn't write ARC HS registers"));
163 /* Target operation: Read REGNUM register (all registers if REGNUM == -1)
164 from target process into REGCACHE. */
167 arc_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regnum
)
170 if (regnum
== -1 || regnum
<= ARC_LAST_REGNUM
)
171 fetch_gregs (regcache
, regnum
);
173 #ifdef ARC_HAS_V2_REGSET
175 || regnum
== ARC_R30_REGNUM
176 || regnum
== ARC_R58_REGNUM
177 || regnum
== ARC_R59_REGNUM
)
178 fetch_v2_regs (regcache
, regnum
);
182 /* Target operation: Store REGNUM register (all registers if REGNUM == -1)
183 to the target process from REGCACHE. */
186 arc_linux_nat_target::store_registers (struct regcache
*regcache
, int regnum
)
188 if (regnum
== -1 || regnum
<= ARC_LAST_REGNUM
)
189 store_gregs (regcache
, regnum
);
191 #ifdef ARC_HAS_V2_REGSET
193 || regnum
== ARC_R30_REGNUM
194 || regnum
== ARC_R58_REGNUM
195 || regnum
== ARC_R59_REGNUM
)
196 store_v2_regs (regcache
, regnum
);
200 /* Copy general purpose register(s) from REGCACHE into regset GREGS.
201 This function is exported to proc-service.c */
204 fill_gregset (const struct regcache
*regcache
,
205 gdb_gregset_t
*gregs
, int regnum
)
207 arc_linux_collect_gregset (NULL
, regcache
, regnum
, gregs
, 0);
210 /* Copy all the general purpose registers from regset GREGS into REGCACHE.
211 This function is exported to proc-service.c. */
214 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregs
)
216 arc_linux_supply_gregset (NULL
, regcache
, -1, gregs
, 0);
219 /* ARC doesn't have separate FP registers. This function is exported
220 to proc-service.c. */
223 fill_fpregset (const struct regcache
*regcache
,
224 gdb_fpregset_t
*fpregsetp
, int regnum
)
226 arc_linux_nat_debug_printf ("called");
229 /* ARC doesn't have separate FP registers. This function is exported
230 to proc-service.c. */
233 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
235 arc_linux_nat_debug_printf ("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
,
298 arc_linux_nat_debug_printf ("called");
300 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
303 /* IDX is the bias from the thread pointer to the beginning of the
304 thread descriptor. It has to be subtracted due to implementation
305 quirks in libthread_db. */
306 *base
= (void *) ((char *) *base
- idx
);
311 /* Suppress warning from -Wmissing-prototypes. */
312 void _initialize_arc_linux_nat ();
314 _initialize_arc_linux_nat ()
316 /* Register the target. */
317 linux_target
= &the_arc_linux_nat_target
;
318 add_inf_child_target (&the_arc_linux_nat_target
);