linux-arc-low.cc revision 1.1 1 1.1 christos /* Target dependent code for the remote server for GNU/Linux ARC.
2 1.1 christos
3 1.1 christos Copyright 2020-2023 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "server.h"
21 1.1 christos #include "regdef.h"
22 1.1 christos #include "linux-low.h"
23 1.1 christos #include "tdesc.h"
24 1.1 christos #include "arch/arc.h"
25 1.1 christos
26 1.1 christos #include <linux/elf.h>
27 1.1 christos #include <arpa/inet.h>
28 1.1 christos
29 1.1 christos /* Linux starting with 4.12 supports NT_ARC_V2 note type, which adds R30,
30 1.1 christos R58 and R59 registers. */
31 1.1 christos #ifdef NT_ARC_V2
32 1.1 christos #define ARC_HAS_V2_REGSET
33 1.1 christos #endif
34 1.1 christos
35 1.1 christos /* The encoding of the instruction "TRAP_S 1" (endianness agnostic). */
36 1.1 christos #define TRAP_S_1_OPCODE 0x783e
37 1.1 christos #define TRAP_S_1_SIZE 2
38 1.1 christos
39 1.1 christos /* Using a mere "uint16_t arc_linux_traps_s = TRAP_S_1_OPCODE" would
40 1.1 christos work as well, because the endianness will end up correctly when
41 1.1 christos the code is compiled for the same endianness as the target (see
42 1.1 christos the notes for "low_breakpoint_at" in this file). However, this
43 1.1 christos illustrates how the __BIG_ENDIAN__ macro can be used to make
44 1.1 christos easy-to-understand codes. */
45 1.1 christos #if defined(__BIG_ENDIAN__)
46 1.1 christos /* 0x78, 0x3e. */
47 1.1 christos static gdb_byte arc_linux_trap_s[TRAP_S_1_SIZE]
48 1.1 christos = {TRAP_S_1_OPCODE >> 8, TRAP_S_1_OPCODE & 0xFF};
49 1.1 christos #else
50 1.1 christos /* 0x3e, 0x78. */
51 1.1 christos static gdb_byte arc_linux_trap_s[TRAP_S_1_SIZE]
52 1.1 christos = {TRAP_S_1_OPCODE && 0xFF, TRAP_S_1_OPCODE >> 8};
53 1.1 christos #endif
54 1.1 christos
55 1.1 christos /* Linux target op definitions for the ARC architecture.
56 1.1 christos Note for future: in case of adding the protected method low_get_next_pcs(),
57 1.1 christos the public method supports_software_single_step() should be added to return
58 1.1 christos "true". */
59 1.1 christos
60 1.1 christos class arc_target : public linux_process_target
61 1.1 christos {
62 1.1 christos public:
63 1.1 christos
64 1.1 christos const regs_info *get_regs_info () override;
65 1.1 christos
66 1.1 christos const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
67 1.1 christos
68 1.1 christos protected:
69 1.1 christos
70 1.1 christos void low_arch_setup () override;
71 1.1 christos
72 1.1 christos bool low_cannot_fetch_register (int regno) override;
73 1.1 christos
74 1.1 christos bool low_cannot_store_register (int regno) override;
75 1.1 christos
76 1.1 christos bool low_supports_breakpoints () override;
77 1.1 christos
78 1.1 christos CORE_ADDR low_get_pc (regcache *regcache) override;
79 1.1 christos
80 1.1 christos void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
81 1.1 christos
82 1.1 christos bool low_breakpoint_at (CORE_ADDR where) override;
83 1.1 christos };
84 1.1 christos
85 1.1 christos /* The singleton target ops object. */
86 1.1 christos
87 1.1 christos static arc_target the_arc_target;
88 1.1 christos
89 1.1 christos bool
90 1.1 christos arc_target::low_supports_breakpoints ()
91 1.1 christos {
92 1.1 christos return true;
93 1.1 christos }
94 1.1 christos
95 1.1 christos CORE_ADDR
96 1.1 christos arc_target::low_get_pc (regcache *regcache)
97 1.1 christos {
98 1.1 christos return linux_get_pc_32bit (regcache);
99 1.1 christos }
100 1.1 christos
101 1.1 christos void
102 1.1 christos arc_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
103 1.1 christos {
104 1.1 christos linux_set_pc_32bit (regcache, pc);
105 1.1 christos }
106 1.1 christos
107 1.1 christos static const struct target_desc *
108 1.1 christos arc_linux_read_description (void)
109 1.1 christos {
110 1.1 christos #ifdef __ARC700__
111 1.1 christos arc_arch_features features (4, ARC_ISA_ARCV1);
112 1.1 christos #else
113 1.1 christos arc_arch_features features (4, ARC_ISA_ARCV2);
114 1.1 christos #endif
115 1.1 christos target_desc_up tdesc = arc_create_target_description (features);
116 1.1 christos
117 1.1 christos static const char *expedite_regs[] = { "sp", "status32", nullptr };
118 1.1 christos init_target_desc (tdesc.get (), expedite_regs);
119 1.1 christos
120 1.1 christos return tdesc.release ();
121 1.1 christos }
122 1.1 christos
123 1.1 christos void
124 1.1 christos arc_target::low_arch_setup ()
125 1.1 christos {
126 1.1 christos current_process ()->tdesc = arc_linux_read_description ();
127 1.1 christos }
128 1.1 christos
129 1.1 christos bool
130 1.1 christos arc_target::low_cannot_fetch_register (int regno)
131 1.1 christos {
132 1.1 christos return (regno >= current_process ()->tdesc->reg_defs.size ());
133 1.1 christos }
134 1.1 christos
135 1.1 christos bool
136 1.1 christos arc_target::low_cannot_store_register (int regno)
137 1.1 christos {
138 1.1 christos return (regno >= current_process ()->tdesc->reg_defs.size ());
139 1.1 christos }
140 1.1 christos
141 1.1 christos /* This works for both endianness. Below you see an illustration of how
142 1.1 christos the "trap_s 1" instruction encoded for both endianness in the memory
143 1.1 christos will end up as the TRAP_S_1_OPCODE constant:
144 1.1 christos
145 1.1 christos BE: 0x78 0x3e --> at INSN addr: 0x78 0x3e --> INSN = 0x783e
146 1.1 christos LE: 0x3e 0x78 --> at INSN addr: 0x3e 0x78 --> INSN = 0x783e
147 1.1 christos
148 1.1 christos One can employ "memcmp()" for comparing the arrays too. */
149 1.1 christos
150 1.1 christos bool
151 1.1 christos arc_target::low_breakpoint_at (CORE_ADDR where)
152 1.1 christos {
153 1.1 christos uint16_t insn;
154 1.1 christos
155 1.1 christos /* "the_target" global variable is the current object at hand. */
156 1.1 christos this->read_memory (where, (gdb_byte *) &insn, TRAP_S_1_SIZE);
157 1.1 christos return (insn == TRAP_S_1_OPCODE);
158 1.1 christos }
159 1.1 christos
160 1.1 christos /* PTRACE_GETREGSET/NT_PRSTATUS and PTRACE_SETREGSET/NT_PRSTATUS work with
161 1.1 christos regsets in a struct, "user_regs_struct", defined in the
162 1.1 christos linux/arch/arc/include/uapi/asm/ptrace.h header. This code supports
163 1.1 christos ARC Linux ABI v3 and v4. */
164 1.1 christos
165 1.1 christos /* Populate a ptrace NT_PRSTATUS regset from a regcache.
166 1.1 christos
167 1.1 christos This appears to be a unique approach to populating the buffer, but
168 1.1 christos being name, rather than offset based, it is robust to future API
169 1.1 christos changes, as there is no need to create a regmap of registers in the
170 1.1 christos user_regs_struct. */
171 1.1 christos
172 1.1 christos static void
173 1.1 christos arc_fill_gregset (struct regcache *regcache, void *buf)
174 1.1 christos {
175 1.1 christos struct user_regs_struct *regbuf = (struct user_regs_struct *) buf;
176 1.1 christos
177 1.1 christos /* Core registers. */
178 1.1 christos collect_register_by_name (regcache, "r0", &(regbuf->scratch.r0));
179 1.1 christos collect_register_by_name (regcache, "r1", &(regbuf->scratch.r1));
180 1.1 christos collect_register_by_name (regcache, "r2", &(regbuf->scratch.r2));
181 1.1 christos collect_register_by_name (regcache, "r3", &(regbuf->scratch.r3));
182 1.1 christos collect_register_by_name (regcache, "r4", &(regbuf->scratch.r4));
183 1.1 christos collect_register_by_name (regcache, "r5", &(regbuf->scratch.r5));
184 1.1 christos collect_register_by_name (regcache, "r6", &(regbuf->scratch.r6));
185 1.1 christos collect_register_by_name (regcache, "r7", &(regbuf->scratch.r7));
186 1.1 christos collect_register_by_name (regcache, "r8", &(regbuf->scratch.r8));
187 1.1 christos collect_register_by_name (regcache, "r9", &(regbuf->scratch.r9));
188 1.1 christos collect_register_by_name (regcache, "r10", &(regbuf->scratch.r10));
189 1.1 christos collect_register_by_name (regcache, "r11", &(regbuf->scratch.r11));
190 1.1 christos collect_register_by_name (regcache, "r12", &(regbuf->scratch.r12));
191 1.1 christos collect_register_by_name (regcache, "r13", &(regbuf->callee.r13));
192 1.1 christos collect_register_by_name (regcache, "r14", &(regbuf->callee.r14));
193 1.1 christos collect_register_by_name (regcache, "r15", &(regbuf->callee.r15));
194 1.1 christos collect_register_by_name (regcache, "r16", &(regbuf->callee.r16));
195 1.1 christos collect_register_by_name (regcache, "r17", &(regbuf->callee.r17));
196 1.1 christos collect_register_by_name (regcache, "r18", &(regbuf->callee.r18));
197 1.1 christos collect_register_by_name (regcache, "r19", &(regbuf->callee.r19));
198 1.1 christos collect_register_by_name (regcache, "r20", &(regbuf->callee.r20));
199 1.1 christos collect_register_by_name (regcache, "r21", &(regbuf->callee.r21));
200 1.1 christos collect_register_by_name (regcache, "r22", &(regbuf->callee.r22));
201 1.1 christos collect_register_by_name (regcache, "r23", &(regbuf->callee.r23));
202 1.1 christos collect_register_by_name (regcache, "r24", &(regbuf->callee.r24));
203 1.1 christos collect_register_by_name (regcache, "r25", &(regbuf->callee.r25));
204 1.1 christos collect_register_by_name (regcache, "gp", &(regbuf->scratch.gp));
205 1.1 christos collect_register_by_name (regcache, "fp", &(regbuf->scratch.fp));
206 1.1 christos collect_register_by_name (regcache, "sp", &(regbuf->scratch.sp));
207 1.1 christos collect_register_by_name (regcache, "blink", &(regbuf->scratch.blink));
208 1.1 christos
209 1.1 christos /* Loop registers. */
210 1.1 christos collect_register_by_name (regcache, "lp_count", &(regbuf->scratch.lp_count));
211 1.1 christos collect_register_by_name (regcache, "lp_start", &(regbuf->scratch.lp_start));
212 1.1 christos collect_register_by_name (regcache, "lp_end", &(regbuf->scratch.lp_end));
213 1.1 christos
214 1.1 christos /* The current "pc" value must be written to "eret" (exception return
215 1.1 christos address) register, because that is the address that the kernel code
216 1.1 christos will jump back to after a breakpoint exception has been raised.
217 1.1 christos The "pc_stop" value is ignored by the genregs_set() in
218 1.1 christos linux/arch/arc/kernel/ptrace.c. */
219 1.1 christos collect_register_by_name (regcache, "pc", &(regbuf->scratch.ret));
220 1.1 christos
221 1.1 christos /* Currently ARC Linux ptrace doesn't allow writes to status32 because
222 1.1 christos some of its bits are kernel mode-only and shoudn't be writable from
223 1.1 christos user-space. Writing status32 from debugger could be useful, though,
224 1.1 christos so ability to write non-priviliged bits will be added to kernel
225 1.1 christos sooner or later. */
226 1.1 christos
227 1.1 christos /* BTA. */
228 1.1 christos collect_register_by_name (regcache, "bta", &(regbuf->scratch.bta));
229 1.1 christos }
230 1.1 christos
231 1.1 christos /* Populate a regcache from a ptrace NT_PRSTATUS regset. */
232 1.1 christos
233 1.1 christos static void
234 1.1 christos arc_store_gregset (struct regcache *regcache, const void *buf)
235 1.1 christos {
236 1.1 christos const struct user_regs_struct *regbuf = (const struct user_regs_struct *) buf;
237 1.1 christos
238 1.1 christos /* Core registers. */
239 1.1 christos supply_register_by_name (regcache, "r0", &(regbuf->scratch.r0));
240 1.1 christos supply_register_by_name (regcache, "r1", &(regbuf->scratch.r1));
241 1.1 christos supply_register_by_name (regcache, "r2", &(regbuf->scratch.r2));
242 1.1 christos supply_register_by_name (regcache, "r3", &(regbuf->scratch.r3));
243 1.1 christos supply_register_by_name (regcache, "r4", &(regbuf->scratch.r4));
244 1.1 christos supply_register_by_name (regcache, "r5", &(regbuf->scratch.r5));
245 1.1 christos supply_register_by_name (regcache, "r6", &(regbuf->scratch.r6));
246 1.1 christos supply_register_by_name (regcache, "r7", &(regbuf->scratch.r7));
247 1.1 christos supply_register_by_name (regcache, "r8", &(regbuf->scratch.r8));
248 1.1 christos supply_register_by_name (regcache, "r9", &(regbuf->scratch.r9));
249 1.1 christos supply_register_by_name (regcache, "r10", &(regbuf->scratch.r10));
250 1.1 christos supply_register_by_name (regcache, "r11", &(regbuf->scratch.r11));
251 1.1 christos supply_register_by_name (regcache, "r12", &(regbuf->scratch.r12));
252 1.1 christos supply_register_by_name (regcache, "r13", &(regbuf->callee.r13));
253 1.1 christos supply_register_by_name (regcache, "r14", &(regbuf->callee.r14));
254 1.1 christos supply_register_by_name (regcache, "r15", &(regbuf->callee.r15));
255 1.1 christos supply_register_by_name (regcache, "r16", &(regbuf->callee.r16));
256 1.1 christos supply_register_by_name (regcache, "r17", &(regbuf->callee.r17));
257 1.1 christos supply_register_by_name (regcache, "r18", &(regbuf->callee.r18));
258 1.1 christos supply_register_by_name (regcache, "r19", &(regbuf->callee.r19));
259 1.1 christos supply_register_by_name (regcache, "r20", &(regbuf->callee.r20));
260 1.1 christos supply_register_by_name (regcache, "r21", &(regbuf->callee.r21));
261 1.1 christos supply_register_by_name (regcache, "r22", &(regbuf->callee.r22));
262 1.1 christos supply_register_by_name (regcache, "r23", &(regbuf->callee.r23));
263 1.1 christos supply_register_by_name (regcache, "r24", &(regbuf->callee.r24));
264 1.1 christos supply_register_by_name (regcache, "r25", &(regbuf->callee.r25));
265 1.1 christos supply_register_by_name (regcache, "gp", &(regbuf->scratch.gp));
266 1.1 christos supply_register_by_name (regcache, "fp", &(regbuf->scratch.fp));
267 1.1 christos supply_register_by_name (regcache, "sp", &(regbuf->scratch.sp));
268 1.1 christos supply_register_by_name (regcache, "blink", &(regbuf->scratch.blink));
269 1.1 christos
270 1.1 christos /* Loop registers. */
271 1.1 christos supply_register_by_name (regcache, "lp_count", &(regbuf->scratch.lp_count));
272 1.1 christos supply_register_by_name (regcache, "lp_start", &(regbuf->scratch.lp_start));
273 1.1 christos supply_register_by_name (regcache, "lp_end", &(regbuf->scratch.lp_end));
274 1.1 christos
275 1.1 christos /* The genregs_get() in linux/arch/arc/kernel/ptrace.c populates the
276 1.1 christos pseudo register "stop_pc" with the "efa" (exception fault address)
277 1.1 christos register. This was deemed necessary, because the breakpoint
278 1.1 christos instruction, "trap_s 1", is a committing one; i.e. the "eret"
279 1.1 christos (exception return address) register will be pointing to the next
280 1.1 christos instruction, while "efa" points to the address that raised the
281 1.1 christos breakpoint. */
282 1.1 christos supply_register_by_name (regcache, "pc", &(regbuf->stop_pc));
283 1.1 christos unsigned long pcl = regbuf->stop_pc & ~3L;
284 1.1 christos supply_register_by_name (regcache, "pcl", &pcl);
285 1.1 christos
286 1.1 christos /* Other auxilliary registers. */
287 1.1 christos supply_register_by_name (regcache, "status32", &(regbuf->scratch.status32));
288 1.1 christos
289 1.1 christos /* BTA. */
290 1.1 christos supply_register_by_name (regcache, "bta", &(regbuf->scratch.bta));
291 1.1 christos }
292 1.1 christos
293 1.1 christos #ifdef ARC_HAS_V2_REGSET
294 1.1 christos
295 1.1 christos /* Look through a regcache's TDESC for a register named NAME.
296 1.1 christos If found, return true; false, otherwise. */
297 1.1 christos
298 1.1 christos static bool
299 1.1 christos is_reg_name_available_p (const struct target_desc *tdesc,
300 1.1 christos const char *name)
301 1.1 christos {
302 1.1 christos for (const gdb::reg ® : tdesc->reg_defs)
303 1.1 christos if (strcmp (name, reg.name) == 0)
304 1.1 christos return true;
305 1.1 christos return false;
306 1.1 christos }
307 1.1 christos
308 1.1 christos /* Copy registers from regcache to user_regs_arcv2. */
309 1.1 christos
310 1.1 christos static void
311 1.1 christos arc_fill_v2_regset (struct regcache *regcache, void *buf)
312 1.1 christos {
313 1.1 christos struct user_regs_arcv2 *regbuf = (struct user_regs_arcv2 *) buf;
314 1.1 christos
315 1.1 christos if (is_reg_name_available_p (regcache->tdesc, "r30"))
316 1.1 christos collect_register_by_name (regcache, "r30", &(regbuf->r30));
317 1.1 christos
318 1.1 christos if (is_reg_name_available_p (regcache->tdesc, "r58"))
319 1.1 christos collect_register_by_name (regcache, "r58", &(regbuf->r58));
320 1.1 christos
321 1.1 christos if (is_reg_name_available_p (regcache->tdesc, "r59"))
322 1.1 christos collect_register_by_name (regcache, "r59", &(regbuf->r59));
323 1.1 christos }
324 1.1 christos
325 1.1 christos /* Copy registers from user_regs_arcv2 to regcache. */
326 1.1 christos
327 1.1 christos static void
328 1.1 christos arc_store_v2_regset (struct regcache *regcache, const void *buf)
329 1.1 christos {
330 1.1 christos struct user_regs_arcv2 *regbuf = (struct user_regs_arcv2 *) buf;
331 1.1 christos
332 1.1 christos if (is_reg_name_available_p (regcache->tdesc, "r30"))
333 1.1 christos supply_register_by_name (regcache, "r30", &(regbuf->r30));
334 1.1 christos
335 1.1 christos if (is_reg_name_available_p (regcache->tdesc, "r58"))
336 1.1 christos supply_register_by_name (regcache, "r58", &(regbuf->r58));
337 1.1 christos
338 1.1 christos if (is_reg_name_available_p (regcache->tdesc, "r59"))
339 1.1 christos supply_register_by_name (regcache, "r59", &(regbuf->r59));
340 1.1 christos }
341 1.1 christos
342 1.1 christos #endif
343 1.1 christos
344 1.1 christos /* Fetch the thread-local storage pointer for libthread_db. Note that
345 1.1 christos this function is not called from GDB, but is called from libthread_db.
346 1.1 christos
347 1.1 christos This is the same function as for other architectures, for example in
348 1.1 christos linux-arm-low.c. */
349 1.1 christos
350 1.1 christos ps_err_e
351 1.1 christos ps_get_thread_area (struct ps_prochandle *ph, lwpid_t lwpid,
352 1.1 christos int idx, void **base)
353 1.1 christos {
354 1.1 christos if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, nullptr, base) != 0)
355 1.1 christos return PS_ERR;
356 1.1 christos
357 1.1 christos /* IDX is the bias from the thread pointer to the beginning of the
358 1.1 christos thread descriptor. It has to be subtracted due to implementation
359 1.1 christos quirks in libthread_db. */
360 1.1 christos *base = (void *) ((char *) *base - idx);
361 1.1 christos
362 1.1 christos return PS_OK;
363 1.1 christos }
364 1.1 christos
365 1.1 christos static struct regset_info arc_regsets[] =
366 1.1 christos {
367 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
368 1.1 christos sizeof (struct user_regs_struct), GENERAL_REGS,
369 1.1 christos arc_fill_gregset, arc_store_gregset
370 1.1 christos },
371 1.1 christos #ifdef ARC_HAS_V2_REGSET
372 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_ARC_V2,
373 1.1 christos sizeof (struct user_regs_arcv2), GENERAL_REGS,
374 1.1 christos arc_fill_v2_regset, arc_store_v2_regset
375 1.1 christos },
376 1.1 christos #endif
377 1.1 christos NULL_REGSET
378 1.1 christos };
379 1.1 christos
380 1.1 christos static struct regsets_info arc_regsets_info =
381 1.1 christos {
382 1.1 christos arc_regsets, /* regsets */
383 1.1 christos 0, /* num_regsets */
384 1.1 christos nullptr, /* disabled regsets */
385 1.1 christos };
386 1.1 christos
387 1.1 christos static struct regs_info arc_regs_info =
388 1.1 christos {
389 1.1 christos nullptr, /* regset_bitmap */
390 1.1 christos nullptr, /* usrregs */
391 1.1 christos &arc_regsets_info
392 1.1 christos };
393 1.1 christos
394 1.1 christos const regs_info *
395 1.1 christos arc_target::get_regs_info ()
396 1.1 christos {
397 1.1 christos return &arc_regs_info;
398 1.1 christos }
399 1.1 christos
400 1.1 christos /* One of the methods necessary for Z0 packet support. */
401 1.1 christos
402 1.1 christos const gdb_byte *
403 1.1 christos arc_target::sw_breakpoint_from_kind (int kind, int *size)
404 1.1 christos {
405 1.1 christos gdb_assert (kind == TRAP_S_1_SIZE);
406 1.1 christos *size = kind;
407 1.1 christos return arc_linux_trap_s;
408 1.1 christos }
409 1.1 christos
410 1.1 christos /* The linux target ops object. */
411 1.1 christos
412 1.1 christos linux_process_target *the_linux_target = &the_arc_target;
413 1.1 christos
414 1.1 christos void
415 1.1 christos initialize_low_arch (void)
416 1.1 christos {
417 1.1 christos initialize_regsets_info (&arc_regsets_info);
418 1.1 christos }
419