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