Update description of macro keyword argument assignment in assembler documentation.
[binutils-gdb.git] / gdb / ppc-linux-tdep.c
blob2da2d32eb2628a6330f437a645bb2fdef2e8e968
1 /* Target-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-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/>. */
20 #include "frame.h"
21 #include "inferior.h"
22 #include "symtab.h"
23 #include "target.h"
24 #include "gdbcore.h"
25 #include "gdbcmd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "regcache.h"
29 #include "value.h"
30 #include "osabi.h"
31 #include "regset.h"
32 #include "solib-svr4.h"
33 #include "solib.h"
34 #include "solist.h"
35 #include "ppc-tdep.h"
36 #include "ppc64-tdep.h"
37 #include "ppc-linux-tdep.h"
38 #include "arch/ppc-linux-common.h"
39 #include "arch/ppc-linux-tdesc.h"
40 #include "glibc-tdep.h"
41 #include "trad-frame.h"
42 #include "frame-unwind.h"
43 #include "tramp-frame.h"
44 #include "observable.h"
45 #include "auxv.h"
46 #include "elf/common.h"
47 #include "elf/ppc64.h"
48 #include "arch-utils.h"
49 #include "xml-syscall.h"
50 #include "linux-tdep.h"
51 #include "linux-record.h"
52 #include "record-full.h"
53 #include "infrun.h"
54 #include "expop.h"
56 #include "stap-probe.h"
57 #include "ax.h"
58 #include "ax-gdb.h"
59 #include "cli/cli-utils.h"
60 #include "parser-defs.h"
61 #include "user-regs.h"
62 #include <ctype.h>
63 #include "elf-bfd.h"
64 #include "producer.h"
65 #include "target-float.h"
67 #include "features/rs6000/powerpc-32l.c"
68 #include "features/rs6000/powerpc-altivec32l.c"
69 #include "features/rs6000/powerpc-vsx32l.c"
70 #include "features/rs6000/powerpc-isa205-32l.c"
71 #include "features/rs6000/powerpc-isa205-altivec32l.c"
72 #include "features/rs6000/powerpc-isa205-vsx32l.c"
73 #include "features/rs6000/powerpc-isa205-ppr-dscr-vsx32l.c"
74 #include "features/rs6000/powerpc-isa207-vsx32l.c"
75 #include "features/rs6000/powerpc-isa207-htm-vsx32l.c"
76 #include "features/rs6000/powerpc-64l.c"
77 #include "features/rs6000/powerpc-altivec64l.c"
78 #include "features/rs6000/powerpc-vsx64l.c"
79 #include "features/rs6000/powerpc-isa205-64l.c"
80 #include "features/rs6000/powerpc-isa205-altivec64l.c"
81 #include "features/rs6000/powerpc-isa205-vsx64l.c"
82 #include "features/rs6000/powerpc-isa205-ppr-dscr-vsx64l.c"
83 #include "features/rs6000/powerpc-isa207-vsx64l.c"
84 #include "features/rs6000/powerpc-isa207-htm-vsx64l.c"
85 #include "features/rs6000/powerpc-e500l.c"
86 #include "dwarf2/frame.h"
88 /* Shared library operations for PowerPC-Linux. */
89 static solib_ops powerpc_so_ops;
91 /* The syscall's XML filename for PPC and PPC64. */
92 #define XML_SYSCALL_FILENAME_PPC "syscalls/ppc-linux.xml"
93 #define XML_SYSCALL_FILENAME_PPC64 "syscalls/ppc64-linux.xml"
95 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
96 in much the same fashion as memory_remove_breakpoint in mem-break.c,
97 but is careful not to write back the previous contents if the code
98 in question has changed in between inserting the breakpoint and
99 removing it.
101 Here is the problem that we're trying to solve...
103 Once upon a time, before introducing this function to remove
104 breakpoints from the inferior, setting a breakpoint on a shared
105 library function prior to running the program would not work
106 properly. In order to understand the problem, it is first
107 necessary to understand a little bit about dynamic linking on
108 this platform.
110 A call to a shared library function is accomplished via a bl
111 (branch-and-link) instruction whose branch target is an entry
112 in the procedure linkage table (PLT). The PLT in the object
113 file is uninitialized. To gdb, prior to running the program, the
114 entries in the PLT are all zeros.
116 Once the program starts running, the shared libraries are loaded
117 and the procedure linkage table is initialized, but the entries in
118 the table are not (necessarily) resolved. Once a function is
119 actually called, the code in the PLT is hit and the function is
120 resolved. In order to better illustrate this, an example is in
121 order; the following example is from the gdb testsuite.
123 We start the program shmain.
125 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
126 [...]
128 We place two breakpoints, one on shr1 and the other on main.
130 (gdb) b shr1
131 Breakpoint 1 at 0x100409d4
132 (gdb) b main
133 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
135 Examine the instruction (and the immediatly following instruction)
136 upon which the breakpoint was placed. Note that the PLT entry
137 for shr1 contains zeros.
139 (gdb) x/2i 0x100409d4
140 0x100409d4 <shr1>: .long 0x0
141 0x100409d8 <shr1+4>: .long 0x0
143 Now run 'til main.
145 (gdb) r
146 Starting program: gdb.base/shmain
147 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
149 Breakpoint 2, main ()
150 at gdb.base/shmain.c:44
151 44 g = 1;
153 Examine the PLT again. Note that the loading of the shared
154 library has initialized the PLT to code which loads a constant
155 (which I think is an index into the GOT) into r11 and then
156 branches a short distance to the code which actually does the
157 resolving.
159 (gdb) x/2i 0x100409d4
160 0x100409d4 <shr1>: li r11,4
161 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
162 (gdb) c
163 Continuing.
165 Breakpoint 1, shr1 (x=1)
166 at gdb.base/shr1.c:19
167 19 l = 1;
169 Now we've hit the breakpoint at shr1. (The breakpoint was
170 reset from the PLT entry to the actual shr1 function after the
171 shared library was loaded.) Note that the PLT entry has been
172 resolved to contain a branch that takes us directly to shr1.
173 (The real one, not the PLT entry.)
175 (gdb) x/2i 0x100409d4
176 0x100409d4 <shr1>: b 0xffaf76c <shr1>
177 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
179 The thing to note here is that the PLT entry for shr1 has been
180 changed twice.
182 Now the problem should be obvious. GDB places a breakpoint (a
183 trap instruction) on the zero value of the PLT entry for shr1.
184 Later on, after the shared library had been loaded and the PLT
185 initialized, GDB gets a signal indicating this fact and attempts
186 (as it always does when it stops) to remove all the breakpoints.
188 The breakpoint removal was causing the former contents (a zero
189 word) to be written back to the now initialized PLT entry thus
190 destroying a portion of the initialization that had occurred only a
191 short time ago. When execution continued, the zero word would be
192 executed as an instruction an illegal instruction trap was
193 generated instead. (0 is not a legal instruction.)
195 The fix for this problem was fairly straightforward. The function
196 memory_remove_breakpoint from mem-break.c was copied to this file,
197 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
198 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
199 function.
201 The differences between ppc_linux_memory_remove_breakpoint () and
202 memory_remove_breakpoint () are minor. All that the former does
203 that the latter does not is check to make sure that the breakpoint
204 location actually contains a breakpoint (trap instruction) prior
205 to attempting to write back the old contents. If it does contain
206 a trap instruction, we allow the old contents to be written back.
207 Otherwise, we silently do nothing.
209 The big question is whether memory_remove_breakpoint () should be
210 changed to have the same functionality. The downside is that more
211 traffic is generated for remote targets since we'll have an extra
212 fetch of a memory word each time a breakpoint is removed.
214 For the time being, we'll leave this self-modifying-code-friendly
215 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
216 else in the event that some other platform has similar needs with
217 regard to removing breakpoints in some potentially self modifying
218 code. */
219 static int
220 ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
221 struct bp_target_info *bp_tgt)
223 CORE_ADDR addr = bp_tgt->reqstd_address;
224 const unsigned char *bp;
225 int val;
226 int bplen;
227 gdb_byte old_contents[BREAKPOINT_MAX];
229 /* Determine appropriate breakpoint contents and size for this address. */
230 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
232 /* Make sure we see the memory breakpoints. */
233 scoped_restore restore_memory
234 = make_scoped_restore_show_memory_breakpoints (1);
235 val = target_read_memory (addr, old_contents, bplen);
237 /* If our breakpoint is no longer at the address, this means that the
238 program modified the code on us, so it is wrong to put back the
239 old value. */
240 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
241 val = target_write_raw_memory (addr, bp_tgt->shadow_contents, bplen);
243 return val;
246 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
247 than the 32 bit SYSV R4 ABI structure return convention - all
248 structures, no matter their size, are put in memory. Vectors,
249 which were added later, do get returned in a register though. */
251 static enum return_value_convention
252 ppc_linux_return_value (struct gdbarch *gdbarch, struct value *function,
253 struct type *valtype, struct regcache *regcache,
254 struct value **read_value, const gdb_byte *writebuf)
256 gdb_byte *readbuf = nullptr;
257 if (read_value != nullptr)
259 *read_value = value::allocate (valtype);
260 readbuf = (*read_value)->contents_raw ().data ();
263 if ((valtype->code () == TYPE_CODE_STRUCT
264 || valtype->code () == TYPE_CODE_UNION)
265 && !((valtype->length () == 16 || valtype->length () == 8)
266 && valtype->is_vector ()))
267 return RETURN_VALUE_STRUCT_CONVENTION;
268 else
269 return ppc_sysv_abi_return_value (gdbarch, function, valtype, regcache,
270 readbuf, writebuf);
273 /* PLT stub in an executable. */
274 static const struct ppc_insn_pattern powerpc32_plt_stub[] =
276 { 0xffff0000, 0x3d600000, 0 }, /* lis r11, xxxx */
277 { 0xffff0000, 0x816b0000, 0 }, /* lwz r11, xxxx(r11) */
278 { 0xffffffff, 0x7d6903a6, 0 }, /* mtctr r11 */
279 { 0xffffffff, 0x4e800420, 0 }, /* bctr */
280 { 0, 0, 0 }
283 /* PLT stubs in a shared library or PIE.
284 The first variant is used when the PLT entry is within +/-32k of
285 the GOT pointer (r30). */
286 static const struct ppc_insn_pattern powerpc32_plt_stub_so_1[] =
288 { 0xffff0000, 0x817e0000, 0 }, /* lwz r11, xxxx(r30) */
289 { 0xffffffff, 0x7d6903a6, 0 }, /* mtctr r11 */
290 { 0xffffffff, 0x4e800420, 0 }, /* bctr */
291 { 0, 0, 0 }
294 /* The second variant is used when the PLT entry is more than +/-32k
295 from the GOT pointer (r30). */
296 static const struct ppc_insn_pattern powerpc32_plt_stub_so_2[] =
298 { 0xffff0000, 0x3d7e0000, 0 }, /* addis r11, r30, xxxx */
299 { 0xffff0000, 0x816b0000, 0 }, /* lwz r11, xxxx(r11) */
300 { 0xffffffff, 0x7d6903a6, 0 }, /* mtctr r11 */
301 { 0xffffffff, 0x4e800420, 0 }, /* bctr */
302 { 0, 0, 0 }
305 /* The max number of insns we check using ppc_insns_match_pattern. */
306 #define POWERPC32_PLT_CHECK_LEN (ARRAY_SIZE (powerpc32_plt_stub) - 1)
308 /* Check if PC is in PLT stub. For non-secure PLT, stub is in .plt
309 section. For secure PLT, stub is in .text and we need to check
310 instruction patterns. */
312 static int
313 powerpc_linux_in_dynsym_resolve_code (CORE_ADDR pc)
315 struct bound_minimal_symbol sym;
317 /* Check whether PC is in the dynamic linker. This also checks
318 whether it is in the .plt section, used by non-PIC executables. */
319 if (svr4_in_dynsym_resolve_code (pc))
320 return 1;
322 /* Check if we are in the resolver. */
323 sym = lookup_minimal_symbol_by_pc (pc);
324 if (sym.minsym != NULL
325 && (strcmp (sym.minsym->linkage_name (), "__glink") == 0
326 || strcmp (sym.minsym->linkage_name (), "__glink_PLTresolve") == 0))
327 return 1;
329 return 0;
332 /* Follow PLT stub to actual routine.
334 When the execution direction is EXEC_REVERSE, scan backward to
335 check whether we are in the middle of a PLT stub. Currently,
336 we only look-behind at most 4 instructions (the max length of a PLT
337 stub sequence. */
339 static CORE_ADDR
340 ppc_skip_trampoline_code (const frame_info_ptr &frame, CORE_ADDR pc)
342 unsigned int insnbuf[POWERPC32_PLT_CHECK_LEN];
343 struct gdbarch *gdbarch = get_frame_arch (frame);
344 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
345 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
346 CORE_ADDR target = 0;
347 int scan_limit, i;
349 scan_limit = 1;
350 /* When reverse-debugging, scan backward to check whether we are
351 in the middle of trampoline code. */
352 if (execution_direction == EXEC_REVERSE)
353 scan_limit = 4; /* At most 4 instructions. */
355 for (i = 0; i < scan_limit; i++)
357 if (ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub, insnbuf))
359 /* Calculate PLT entry address from
360 lis r11, xxxx
361 lwz r11, xxxx(r11). */
362 target = ((ppc_insn_d_field (insnbuf[0]) << 16)
363 + ppc_insn_d_field (insnbuf[1]));
365 else if (i < ARRAY_SIZE (powerpc32_plt_stub_so_1) - 1
366 && ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub_so_1,
367 insnbuf))
369 /* Calculate PLT entry address from
370 lwz r11, xxxx(r30). */
371 target = (ppc_insn_d_field (insnbuf[0])
372 + get_frame_register_unsigned (frame,
373 tdep->ppc_gp0_regnum + 30));
375 else if (ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub_so_2,
376 insnbuf))
378 /* Calculate PLT entry address from
379 addis r11, r30, xxxx
380 lwz r11, xxxx(r11). */
381 target = ((ppc_insn_d_field (insnbuf[0]) << 16)
382 + ppc_insn_d_field (insnbuf[1])
383 + get_frame_register_unsigned (frame,
384 tdep->ppc_gp0_regnum + 30));
386 else
388 /* Scan backward one more instruction if it doesn't match. */
389 pc -= 4;
390 continue;
393 target = read_memory_unsigned_integer (target, 4, byte_order);
394 return target;
397 return 0;
400 /* Wrappers to handle Linux-only registers. */
402 static void
403 ppc_linux_supply_gregset (const struct regset *regset,
404 struct regcache *regcache,
405 int regnum, const void *gregs, size_t len)
407 const struct ppc_reg_offsets *offsets
408 = (const struct ppc_reg_offsets *) regset->regmap;
410 ppc_supply_gregset (regset, regcache, regnum, gregs, len);
412 if (ppc_linux_trap_reg_p (regcache->arch ()))
414 /* "orig_r3" is stored 2 slots after "pc". */
415 if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
416 ppc_supply_reg (regcache, PPC_ORIG_R3_REGNUM, (const gdb_byte *) gregs,
417 offsets->pc_offset + 2 * offsets->gpr_size,
418 offsets->gpr_size);
420 /* "trap" is stored 8 slots after "pc". */
421 if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
422 ppc_supply_reg (regcache, PPC_TRAP_REGNUM, (const gdb_byte *) gregs,
423 offsets->pc_offset + 8 * offsets->gpr_size,
424 offsets->gpr_size);
428 static void
429 ppc_linux_collect_gregset (const struct regset *regset,
430 const struct regcache *regcache,
431 int regnum, void *gregs, size_t len)
433 const struct ppc_reg_offsets *offsets
434 = (const struct ppc_reg_offsets *) regset->regmap;
436 /* Clear areas in the linux gregset not written elsewhere. */
437 if (regnum == -1)
438 memset (gregs, 0, len);
440 ppc_collect_gregset (regset, regcache, regnum, gregs, len);
442 if (ppc_linux_trap_reg_p (regcache->arch ()))
444 /* "orig_r3" is stored 2 slots after "pc". */
445 if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
446 ppc_collect_reg (regcache, PPC_ORIG_R3_REGNUM, (gdb_byte *) gregs,
447 offsets->pc_offset + 2 * offsets->gpr_size,
448 offsets->gpr_size);
450 /* "trap" is stored 8 slots after "pc". */
451 if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
452 ppc_collect_reg (regcache, PPC_TRAP_REGNUM, (gdb_byte *) gregs,
453 offsets->pc_offset + 8 * offsets->gpr_size,
454 offsets->gpr_size);
458 /* Regset descriptions. */
459 static const struct ppc_reg_offsets ppc32_linux_reg_offsets =
461 /* General-purpose registers. */
462 /* .r0_offset = */ 0,
463 /* .gpr_size = */ 4,
464 /* .xr_size = */ 4,
465 /* .pc_offset = */ 128,
466 /* .ps_offset = */ 132,
467 /* .cr_offset = */ 152,
468 /* .lr_offset = */ 144,
469 /* .ctr_offset = */ 140,
470 /* .xer_offset = */ 148,
471 /* .mq_offset = */ 156,
473 /* Floating-point registers. */
474 /* .f0_offset = */ 0,
475 /* .fpscr_offset = */ 256,
476 /* .fpscr_size = */ 8
479 static const struct ppc_reg_offsets ppc64_linux_reg_offsets =
481 /* General-purpose registers. */
482 /* .r0_offset = */ 0,
483 /* .gpr_size = */ 8,
484 /* .xr_size = */ 8,
485 /* .pc_offset = */ 256,
486 /* .ps_offset = */ 264,
487 /* .cr_offset = */ 304,
488 /* .lr_offset = */ 288,
489 /* .ctr_offset = */ 280,
490 /* .xer_offset = */ 296,
491 /* .mq_offset = */ 312,
493 /* Floating-point registers. */
494 /* .f0_offset = */ 0,
495 /* .fpscr_offset = */ 256,
496 /* .fpscr_size = */ 8
499 static const struct regset ppc32_linux_gregset = {
500 &ppc32_linux_reg_offsets,
501 ppc_linux_supply_gregset,
502 ppc_linux_collect_gregset
505 static const struct regset ppc64_linux_gregset = {
506 &ppc64_linux_reg_offsets,
507 ppc_linux_supply_gregset,
508 ppc_linux_collect_gregset
511 static const struct regset ppc32_linux_fpregset = {
512 &ppc32_linux_reg_offsets,
513 ppc_supply_fpregset,
514 ppc_collect_fpregset
517 static const struct regcache_map_entry ppc32_le_linux_vrregmap[] =
519 { 32, PPC_VR0_REGNUM, 16 },
520 { 1, PPC_VSCR_REGNUM, 4 },
521 { 1, REGCACHE_MAP_SKIP, 12 },
522 { 1, PPC_VRSAVE_REGNUM, 4 },
523 { 1, REGCACHE_MAP_SKIP, 12 },
524 { 0 }
527 static const struct regcache_map_entry ppc32_be_linux_vrregmap[] =
529 { 32, PPC_VR0_REGNUM, 16 },
530 { 1, REGCACHE_MAP_SKIP, 12},
531 { 1, PPC_VSCR_REGNUM, 4 },
532 { 1, PPC_VRSAVE_REGNUM, 4 },
533 { 1, REGCACHE_MAP_SKIP, 12 },
534 { 0 }
537 static const struct regset ppc32_le_linux_vrregset = {
538 ppc32_le_linux_vrregmap,
539 regcache_supply_regset,
540 regcache_collect_regset
543 static const struct regset ppc32_be_linux_vrregset = {
544 ppc32_be_linux_vrregmap,
545 regcache_supply_regset,
546 regcache_collect_regset
549 static const struct regcache_map_entry ppc32_linux_vsxregmap[] =
551 { 32, PPC_VSR0_UPPER_REGNUM, 8 },
552 { 0 }
555 static const struct regset ppc32_linux_vsxregset = {
556 ppc32_linux_vsxregmap,
557 regcache_supply_regset,
558 regcache_collect_regset
561 /* Program Priorty Register regmap. */
563 static const struct regcache_map_entry ppc32_regmap_ppr[] =
565 { 1, PPC_PPR_REGNUM, 8 },
566 { 0 }
569 /* Program Priorty Register regset. */
571 const struct regset ppc32_linux_pprregset = {
572 ppc32_regmap_ppr,
573 regcache_supply_regset,
574 regcache_collect_regset
577 /* Data Stream Control Register regmap. */
579 static const struct regcache_map_entry ppc32_regmap_dscr[] =
581 { 1, PPC_DSCR_REGNUM, 8 },
582 { 0 }
585 /* Data Stream Control Register regset. */
587 const struct regset ppc32_linux_dscrregset = {
588 ppc32_regmap_dscr,
589 regcache_supply_regset,
590 regcache_collect_regset
593 /* Target Address Register regmap. */
595 static const struct regcache_map_entry ppc32_regmap_tar[] =
597 { 1, PPC_TAR_REGNUM, 8 },
598 { 0 }
601 /* Target Address Register regset. */
603 const struct regset ppc32_linux_tarregset = {
604 ppc32_regmap_tar,
605 regcache_supply_regset,
606 regcache_collect_regset
609 /* Event-Based Branching regmap. */
611 static const struct regcache_map_entry ppc32_regmap_ebb[] =
613 { 1, PPC_EBBRR_REGNUM, 8 },
614 { 1, PPC_EBBHR_REGNUM, 8 },
615 { 1, PPC_BESCR_REGNUM, 8 },
616 { 0 }
619 /* Event-Based Branching regset. */
621 const struct regset ppc32_linux_ebbregset = {
622 ppc32_regmap_ebb,
623 regcache_supply_regset,
624 regcache_collect_regset
627 /* Performance Monitoring Unit regmap. */
629 static const struct regcache_map_entry ppc32_regmap_pmu[] =
631 { 1, PPC_SIAR_REGNUM, 8 },
632 { 1, PPC_SDAR_REGNUM, 8 },
633 { 1, PPC_SIER_REGNUM, 8 },
634 { 1, PPC_MMCR2_REGNUM, 8 },
635 { 1, PPC_MMCR0_REGNUM, 8 },
636 { 0 }
639 /* Performance Monitoring Unit regset. */
641 const struct regset ppc32_linux_pmuregset = {
642 ppc32_regmap_pmu,
643 regcache_supply_regset,
644 regcache_collect_regset
647 /* Hardware Transactional Memory special-purpose register regmap. */
649 static const struct regcache_map_entry ppc32_regmap_tm_spr[] =
651 { 1, PPC_TFHAR_REGNUM, 8 },
652 { 1, PPC_TEXASR_REGNUM, 8 },
653 { 1, PPC_TFIAR_REGNUM, 8 },
654 { 0 }
657 /* Hardware Transactional Memory special-purpose register regset. */
659 const struct regset ppc32_linux_tm_sprregset = {
660 ppc32_regmap_tm_spr,
661 regcache_supply_regset,
662 regcache_collect_regset
665 /* Regmaps for the Hardware Transactional Memory checkpointed
666 general-purpose regsets for 32-bit, 64-bit big-endian, and 64-bit
667 little endian targets. The ptrace and core file buffers for 64-bit
668 targets use 8-byte fields for the 4-byte registers, and the
669 position of the register in the fields depends on the endianness.
670 The 32-bit regmap is the same for both endian types because the
671 fields are all 4-byte long.
673 The layout of checkpointed GPR regset is the same as a regular
674 struct pt_regs, but we skip all registers that are not actually
675 checkpointed by the processor (e.g. msr, nip), except when
676 generating a core file. The 64-bit regset is 48 * 8 bytes long.
677 In some 64-bit kernels, the regset for a 32-bit inferior has the
678 same length, but all the registers are squeezed in the first half
679 (48 * 4 bytes). The pt_regs struct calls the regular cr ccr, but
680 we use ccr for "checkpointed condition register". Note that CR
681 (condition register) field 0 is not checkpointed, but the kernel
682 returns all 4 bytes. The skipped registers should not be touched
683 when writing the regset to the inferior (with
684 PTRACE_SETREGSET). */
686 static const struct regcache_map_entry ppc32_regmap_cgpr[] =
688 { 32, PPC_CR0_REGNUM, 4 },
689 { 3, REGCACHE_MAP_SKIP, 4 }, /* nip, msr, orig_gpr3. */
690 { 1, PPC_CCTR_REGNUM, 4 },
691 { 1, PPC_CLR_REGNUM, 4 },
692 { 1, PPC_CXER_REGNUM, 4 },
693 { 1, PPC_CCR_REGNUM, 4 },
694 { 9, REGCACHE_MAP_SKIP, 4 }, /* All the rest. */
695 { 0 }
698 static const struct regcache_map_entry ppc64_le_regmap_cgpr[] =
700 { 32, PPC_CR0_REGNUM, 8 },
701 { 3, REGCACHE_MAP_SKIP, 8 },
702 { 1, PPC_CCTR_REGNUM, 8 },
703 { 1, PPC_CLR_REGNUM, 8 },
704 { 1, PPC_CXER_REGNUM, 4 },
705 { 1, REGCACHE_MAP_SKIP, 4 }, /* CXER padding. */
706 { 1, PPC_CCR_REGNUM, 4 },
707 { 1, REGCACHE_MAP_SKIP, 4}, /* CCR padding. */
708 { 9, REGCACHE_MAP_SKIP, 8},
709 { 0 }
712 static const struct regcache_map_entry ppc64_be_regmap_cgpr[] =
714 { 32, PPC_CR0_REGNUM, 8 },
715 { 3, REGCACHE_MAP_SKIP, 8 },
716 { 1, PPC_CCTR_REGNUM, 8 },
717 { 1, PPC_CLR_REGNUM, 8 },
718 { 1, REGCACHE_MAP_SKIP, 4}, /* CXER padding. */
719 { 1, PPC_CXER_REGNUM, 4 },
720 { 1, REGCACHE_MAP_SKIP, 4}, /* CCR padding. */
721 { 1, PPC_CCR_REGNUM, 4 },
722 { 9, REGCACHE_MAP_SKIP, 8},
723 { 0 }
726 /* Regsets for the Hardware Transactional Memory checkpointed
727 general-purpose registers for 32-bit, 64-bit big-endian, and 64-bit
728 little endian targets.
730 Some 64-bit kernels generate a checkpointed gpr note section with
731 48*8 bytes for a 32-bit thread, of which only 48*4 are actually
732 used, so we set the variable size flag in the corresponding regset
733 to accept this case. */
735 static const struct regset ppc32_linux_cgprregset = {
736 ppc32_regmap_cgpr,
737 regcache_supply_regset,
738 regcache_collect_regset,
739 REGSET_VARIABLE_SIZE
742 static const struct regset ppc64_be_linux_cgprregset = {
743 ppc64_be_regmap_cgpr,
744 regcache_supply_regset,
745 regcache_collect_regset
748 static const struct regset ppc64_le_linux_cgprregset = {
749 ppc64_le_regmap_cgpr,
750 regcache_supply_regset,
751 regcache_collect_regset
754 /* Hardware Transactional Memory checkpointed floating-point regmap. */
756 static const struct regcache_map_entry ppc32_regmap_cfpr[] =
758 { 32, PPC_CF0_REGNUM, 8 },
759 { 1, PPC_CFPSCR_REGNUM, 8 },
760 { 0 }
763 /* Hardware Transactional Memory checkpointed floating-point regset. */
765 const struct regset ppc32_linux_cfprregset = {
766 ppc32_regmap_cfpr,
767 regcache_supply_regset,
768 regcache_collect_regset
771 /* Regmaps for the Hardware Transactional Memory checkpointed vector
772 regsets, for big and little endian targets. The position of the
773 4-byte VSCR in its 16-byte field depends on the endianness. */
775 static const struct regcache_map_entry ppc32_le_regmap_cvmx[] =
777 { 32, PPC_CVR0_REGNUM, 16 },
778 { 1, PPC_CVSCR_REGNUM, 4 },
779 { 1, REGCACHE_MAP_SKIP, 12 },
780 { 1, PPC_CVRSAVE_REGNUM, 4 },
781 { 1, REGCACHE_MAP_SKIP, 12 },
782 { 0 }
785 static const struct regcache_map_entry ppc32_be_regmap_cvmx[] =
787 { 32, PPC_CVR0_REGNUM, 16 },
788 { 1, REGCACHE_MAP_SKIP, 12 },
789 { 1, PPC_CVSCR_REGNUM, 4 },
790 { 1, PPC_CVRSAVE_REGNUM, 4 },
791 { 1, REGCACHE_MAP_SKIP, 12},
792 { 0 }
795 /* Hardware Transactional Memory checkpointed vector regsets, for little
796 and big endian targets. */
798 static const struct regset ppc32_le_linux_cvmxregset = {
799 ppc32_le_regmap_cvmx,
800 regcache_supply_regset,
801 regcache_collect_regset
804 static const struct regset ppc32_be_linux_cvmxregset = {
805 ppc32_be_regmap_cvmx,
806 regcache_supply_regset,
807 regcache_collect_regset
810 /* Hardware Transactional Memory checkpointed vector-scalar regmap. */
812 static const struct regcache_map_entry ppc32_regmap_cvsx[] =
814 { 32, PPC_CVSR0_UPPER_REGNUM, 8 },
815 { 0 }
818 /* Hardware Transactional Memory checkpointed vector-scalar regset. */
820 const struct regset ppc32_linux_cvsxregset = {
821 ppc32_regmap_cvsx,
822 regcache_supply_regset,
823 regcache_collect_regset
826 /* Hardware Transactional Memory checkpointed Program Priority Register
827 regmap. */
829 static const struct regcache_map_entry ppc32_regmap_cppr[] =
831 { 1, PPC_CPPR_REGNUM, 8 },
832 { 0 }
835 /* Hardware Transactional Memory checkpointed Program Priority Register
836 regset. */
838 const struct regset ppc32_linux_cpprregset = {
839 ppc32_regmap_cppr,
840 regcache_supply_regset,
841 regcache_collect_regset
844 /* Hardware Transactional Memory checkpointed Data Stream Control
845 Register regmap. */
847 static const struct regcache_map_entry ppc32_regmap_cdscr[] =
849 { 1, PPC_CDSCR_REGNUM, 8 },
850 { 0 }
853 /* Hardware Transactional Memory checkpointed Data Stream Control
854 Register regset. */
856 const struct regset ppc32_linux_cdscrregset = {
857 ppc32_regmap_cdscr,
858 regcache_supply_regset,
859 regcache_collect_regset
862 /* Hardware Transactional Memory checkpointed Target Address Register
863 regmap. */
865 static const struct regcache_map_entry ppc32_regmap_ctar[] =
867 { 1, PPC_CTAR_REGNUM, 8 },
868 { 0 }
871 /* Hardware Transactional Memory checkpointed Target Address Register
872 regset. */
874 const struct regset ppc32_linux_ctarregset = {
875 ppc32_regmap_ctar,
876 regcache_supply_regset,
877 regcache_collect_regset
880 const struct regset *
881 ppc_linux_gregset (int wordsize)
883 return wordsize == 8 ? &ppc64_linux_gregset : &ppc32_linux_gregset;
886 const struct regset *
887 ppc_linux_fpregset (void)
889 return &ppc32_linux_fpregset;
892 const struct regset *
893 ppc_linux_vrregset (struct gdbarch *gdbarch)
895 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
896 return &ppc32_be_linux_vrregset;
897 else
898 return &ppc32_le_linux_vrregset;
901 const struct regset *
902 ppc_linux_vsxregset (void)
904 return &ppc32_linux_vsxregset;
907 const struct regset *
908 ppc_linux_cgprregset (struct gdbarch *gdbarch)
910 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
912 if (tdep->wordsize == 4)
914 return &ppc32_linux_cgprregset;
916 else
918 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
919 return &ppc64_be_linux_cgprregset;
920 else
921 return &ppc64_le_linux_cgprregset;
925 const struct regset *
926 ppc_linux_cvmxregset (struct gdbarch *gdbarch)
928 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
929 return &ppc32_be_linux_cvmxregset;
930 else
931 return &ppc32_le_linux_cvmxregset;
934 /* Collect function used to generate the core note for the
935 checkpointed GPR regset. Here, we don't want to skip the
936 "checkpointed" NIP and MSR, so that the note section we generate is
937 similar to the one generated by the kernel. To avoid having to
938 define additional registers in GDB which are not actually
939 checkpointed in the architecture, we copy TFHAR to the checkpointed
940 NIP slot, which is what the kernel does, and copy the regular MSR
941 to the checkpointed MSR slot, which will have a similar value in
942 most cases. */
944 static void
945 ppc_linux_collect_core_cpgrregset (const struct regset *regset,
946 const struct regcache *regcache,
947 int regnum, void *buf, size_t len)
949 struct gdbarch *gdbarch = regcache->arch ();
950 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
952 const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
954 /* We collect the checkpointed GPRs already defined in the regular
955 regmap, then overlay TFHAR/MSR on the checkpointed NIP/MSR
956 slots. */
957 cgprregset->collect_regset (cgprregset, regcache, regnum, buf, len);
959 /* Check that we are collecting all the registers, which should be
960 the case when generating a core file. */
961 if (regnum != -1)
962 return;
964 /* PT_NIP and PT_MSR are 32 and 33 for powerpc. Don't redefine
965 these symbols since this file can run on clients in other
966 architectures where they can already be defined to other
967 values. */
968 int pt_offset = 32;
970 /* Check that our buffer is long enough to hold two slots at
971 pt_offset * wordsize, one for NIP and one for MSR. */
972 gdb_assert ((pt_offset + 2) * tdep->wordsize <= len);
974 /* TFHAR is 8 bytes wide, but the NIP slot for a 32-bit thread is
975 4-bytes long. We use raw_collect_integer which handles
976 differences in the sizes for the source and destination buffers
977 for both endian modes. */
978 (regcache->raw_collect_integer
979 (PPC_TFHAR_REGNUM, ((gdb_byte *) buf) + pt_offset * tdep->wordsize,
980 tdep->wordsize, false));
982 pt_offset = 33;
984 (regcache->raw_collect_integer
985 (PPC_MSR_REGNUM, ((gdb_byte *) buf) + pt_offset * tdep->wordsize,
986 tdep->wordsize, false));
989 /* Iterate over supported core file register note sections. */
991 static void
992 ppc_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
993 iterate_over_regset_sections_cb *cb,
994 void *cb_data,
995 const struct regcache *regcache)
997 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
998 int have_altivec = tdep->ppc_vr0_regnum != -1;
999 int have_vsx = tdep->ppc_vsr0_upper_regnum != -1;
1000 int have_ppr = tdep->ppc_ppr_regnum != -1;
1001 int have_dscr = tdep->ppc_dscr_regnum != -1;
1002 int have_tar = tdep->ppc_tar_regnum != -1;
1004 if (tdep->wordsize == 4)
1005 cb (".reg", 48 * 4, 48 * 4, &ppc32_linux_gregset, NULL, cb_data);
1006 else
1007 cb (".reg", 48 * 8, 48 * 8, &ppc64_linux_gregset, NULL, cb_data);
1009 cb (".reg2", 264, 264, &ppc32_linux_fpregset, NULL, cb_data);
1011 if (have_altivec)
1013 const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
1014 cb (".reg-ppc-vmx", PPC_LINUX_SIZEOF_VRREGSET, PPC_LINUX_SIZEOF_VRREGSET,
1015 vrregset, "ppc Altivec", cb_data);
1018 if (have_vsx)
1019 cb (".reg-ppc-vsx", PPC_LINUX_SIZEOF_VSXREGSET, PPC_LINUX_SIZEOF_VSXREGSET,
1020 &ppc32_linux_vsxregset, "POWER7 VSX", cb_data);
1022 if (have_ppr)
1023 cb (".reg-ppc-ppr", PPC_LINUX_SIZEOF_PPRREGSET,
1024 PPC_LINUX_SIZEOF_PPRREGSET,
1025 &ppc32_linux_pprregset, "Priority Program Register", cb_data);
1027 if (have_dscr)
1028 cb (".reg-ppc-dscr", PPC_LINUX_SIZEOF_DSCRREGSET,
1029 PPC_LINUX_SIZEOF_DSCRREGSET,
1030 &ppc32_linux_dscrregset, "Data Stream Control Register",
1031 cb_data);
1033 if (have_tar)
1034 cb (".reg-ppc-tar", PPC_LINUX_SIZEOF_TARREGSET,
1035 PPC_LINUX_SIZEOF_TARREGSET,
1036 &ppc32_linux_tarregset, "Target Address Register", cb_data);
1038 /* EBB registers are unavailable when ptrace returns ENODATA. Check
1039 availability when generating a core file (regcache != NULL). */
1040 if (tdep->have_ebb)
1041 if (regcache == NULL
1042 || REG_VALID == regcache->get_register_status (PPC_BESCR_REGNUM))
1043 cb (".reg-ppc-ebb", PPC_LINUX_SIZEOF_EBBREGSET,
1044 PPC_LINUX_SIZEOF_EBBREGSET,
1045 &ppc32_linux_ebbregset, "Event-based Branching Registers",
1046 cb_data);
1048 if (tdep->ppc_mmcr0_regnum != -1)
1049 cb (".reg-ppc-pmu", PPC_LINUX_SIZEOF_PMUREGSET,
1050 PPC_LINUX_SIZEOF_PMUREGSET,
1051 &ppc32_linux_pmuregset, "Performance Monitor Registers",
1052 cb_data);
1054 if (tdep->have_htm_spr)
1055 cb (".reg-ppc-tm-spr", PPC_LINUX_SIZEOF_TM_SPRREGSET,
1056 PPC_LINUX_SIZEOF_TM_SPRREGSET,
1057 &ppc32_linux_tm_sprregset,
1058 "Hardware Transactional Memory Special Purpose Registers",
1059 cb_data);
1061 /* Checkpointed registers can be unavailable, don't call back if
1062 we are generating a core file. */
1064 if (tdep->have_htm_core)
1066 /* Only generate the checkpointed GPR core note if we also have
1067 access to the HTM SPRs, because we need TFHAR to fill the
1068 "checkpointed" NIP slot. We can read a core file without it
1069 since GDB is not aware of this NIP as a visible register. */
1070 if (regcache == NULL ||
1071 (REG_VALID == regcache->get_register_status (PPC_CR0_REGNUM)
1072 && tdep->have_htm_spr))
1074 int cgpr_size = (tdep->wordsize == 4?
1075 PPC32_LINUX_SIZEOF_CGPRREGSET
1076 : PPC64_LINUX_SIZEOF_CGPRREGSET);
1078 const struct regset *cgprregset =
1079 ppc_linux_cgprregset (gdbarch);
1081 if (regcache != NULL)
1083 struct regset core_cgprregset = *cgprregset;
1085 core_cgprregset.collect_regset
1086 = ppc_linux_collect_core_cpgrregset;
1088 cb (".reg-ppc-tm-cgpr",
1089 cgpr_size, cgpr_size,
1090 &core_cgprregset,
1091 "Checkpointed General Purpose Registers", cb_data);
1093 else
1095 cb (".reg-ppc-tm-cgpr",
1096 cgpr_size, cgpr_size,
1097 cgprregset,
1098 "Checkpointed General Purpose Registers", cb_data);
1103 if (tdep->have_htm_fpu)
1105 if (regcache == NULL ||
1106 REG_VALID == regcache->get_register_status (PPC_CF0_REGNUM))
1107 cb (".reg-ppc-tm-cfpr", PPC_LINUX_SIZEOF_CFPRREGSET,
1108 PPC_LINUX_SIZEOF_CFPRREGSET,
1109 &ppc32_linux_cfprregset,
1110 "Checkpointed Floating Point Registers", cb_data);
1113 if (tdep->have_htm_altivec)
1115 if (regcache == NULL ||
1116 REG_VALID == regcache->get_register_status (PPC_CVR0_REGNUM))
1118 const struct regset *cvmxregset =
1119 ppc_linux_cvmxregset (gdbarch);
1121 cb (".reg-ppc-tm-cvmx", PPC_LINUX_SIZEOF_CVMXREGSET,
1122 PPC_LINUX_SIZEOF_CVMXREGSET,
1123 cvmxregset,
1124 "Checkpointed Altivec (VMX) Registers", cb_data);
1128 if (tdep->have_htm_vsx)
1130 if (regcache == NULL ||
1131 (REG_VALID
1132 == regcache->get_register_status (PPC_CVSR0_UPPER_REGNUM)))
1133 cb (".reg-ppc-tm-cvsx", PPC_LINUX_SIZEOF_CVSXREGSET,
1134 PPC_LINUX_SIZEOF_CVSXREGSET,
1135 &ppc32_linux_cvsxregset,
1136 "Checkpointed VSX Registers", cb_data);
1139 if (tdep->ppc_cppr_regnum != -1)
1141 if (regcache == NULL ||
1142 REG_VALID == regcache->get_register_status (PPC_CPPR_REGNUM))
1143 cb (".reg-ppc-tm-cppr", PPC_LINUX_SIZEOF_CPPRREGSET,
1144 PPC_LINUX_SIZEOF_CPPRREGSET,
1145 &ppc32_linux_cpprregset,
1146 "Checkpointed Priority Program Register", cb_data);
1149 if (tdep->ppc_cdscr_regnum != -1)
1151 if (regcache == NULL ||
1152 REG_VALID == regcache->get_register_status (PPC_CDSCR_REGNUM))
1153 cb (".reg-ppc-tm-cdscr", PPC_LINUX_SIZEOF_CDSCRREGSET,
1154 PPC_LINUX_SIZEOF_CDSCRREGSET,
1155 &ppc32_linux_cdscrregset,
1156 "Checkpointed Data Stream Control Register", cb_data);
1159 if (tdep->ppc_ctar_regnum)
1161 if ( regcache == NULL ||
1162 REG_VALID == regcache->get_register_status (PPC_CTAR_REGNUM))
1163 cb (".reg-ppc-tm-ctar", PPC_LINUX_SIZEOF_CTARREGSET,
1164 PPC_LINUX_SIZEOF_CTARREGSET,
1165 &ppc32_linux_ctarregset,
1166 "Checkpointed Target Address Register", cb_data);
1170 static void
1171 ppc_linux_sigtramp_cache (const frame_info_ptr &this_frame,
1172 struct trad_frame_cache *this_cache,
1173 CORE_ADDR func, LONGEST offset,
1174 int bias)
1176 CORE_ADDR base;
1177 CORE_ADDR regs;
1178 CORE_ADDR gpregs;
1179 CORE_ADDR fpregs;
1180 int i;
1181 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1182 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1183 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1185 base = get_frame_register_unsigned (this_frame,
1186 gdbarch_sp_regnum (gdbarch));
1187 if (bias > 0 && get_frame_pc (this_frame) != func)
1188 /* See below, some signal trampolines increment the stack as their
1189 first instruction, need to compensate for that. */
1190 base -= bias;
1192 /* Find the address of the register buffer pointer. */
1193 regs = base + offset;
1194 /* Use that to find the address of the corresponding register
1195 buffers. */
1196 gpregs = read_memory_unsigned_integer (regs, tdep->wordsize, byte_order);
1197 fpregs = gpregs + 48 * tdep->wordsize;
1199 /* General purpose. */
1200 for (i = 0; i < 32; i++)
1202 int regnum = i + tdep->ppc_gp0_regnum;
1203 trad_frame_set_reg_addr (this_cache,
1204 regnum, gpregs + i * tdep->wordsize);
1206 trad_frame_set_reg_addr (this_cache,
1207 gdbarch_pc_regnum (gdbarch),
1208 gpregs + 32 * tdep->wordsize);
1209 trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
1210 gpregs + 35 * tdep->wordsize);
1211 trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
1212 gpregs + 36 * tdep->wordsize);
1213 trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
1214 gpregs + 37 * tdep->wordsize);
1215 trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
1216 gpregs + 38 * tdep->wordsize);
1218 if (ppc_linux_trap_reg_p (gdbarch))
1220 trad_frame_set_reg_addr (this_cache, PPC_ORIG_R3_REGNUM,
1221 gpregs + 34 * tdep->wordsize);
1222 trad_frame_set_reg_addr (this_cache, PPC_TRAP_REGNUM,
1223 gpregs + 40 * tdep->wordsize);
1226 if (ppc_floating_point_unit_p (gdbarch))
1228 /* Floating point registers. */
1229 for (i = 0; i < 32; i++)
1231 int regnum = i + gdbarch_fp0_regnum (gdbarch);
1232 trad_frame_set_reg_addr (this_cache, regnum,
1233 fpregs + i * tdep->wordsize);
1235 trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
1236 fpregs + 32 * tdep->wordsize);
1238 trad_frame_set_id (this_cache, frame_id_build (base, func));
1241 static void
1242 ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
1243 const frame_info_ptr &this_frame,
1244 struct trad_frame_cache *this_cache,
1245 CORE_ADDR func)
1247 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
1248 0xd0 /* Offset to ucontext_t. */
1249 + 0x30 /* Offset to .reg. */,
1253 static void
1254 ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
1255 const frame_info_ptr &this_frame,
1256 struct trad_frame_cache *this_cache,
1257 CORE_ADDR func)
1259 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
1260 0x80 /* Offset to ucontext_t. */
1261 + 0xe0 /* Offset to .reg. */,
1262 128);
1265 static void
1266 ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
1267 const frame_info_ptr &this_frame,
1268 struct trad_frame_cache *this_cache,
1269 CORE_ADDR func)
1271 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
1272 0x40 /* Offset to ucontext_t. */
1273 + 0x1c /* Offset to .reg. */,
1277 static void
1278 ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
1279 const frame_info_ptr &this_frame,
1280 struct trad_frame_cache *this_cache,
1281 CORE_ADDR func)
1283 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
1284 0x80 /* Offset to struct sigcontext. */
1285 + 0x38 /* Offset to .reg. */,
1286 128);
1289 static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
1290 SIGTRAMP_FRAME,
1293 { 0x380000ac, ULONGEST_MAX }, /* li r0, 172 */
1294 { 0x44000002, ULONGEST_MAX }, /* sc */
1295 { TRAMP_SENTINEL_INSN },
1297 ppc32_linux_sigaction_cache_init
1299 static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
1300 SIGTRAMP_FRAME,
1303 { 0x38210080, ULONGEST_MAX }, /* addi r1,r1,128 */
1304 { 0x380000ac, ULONGEST_MAX }, /* li r0, 172 */
1305 { 0x44000002, ULONGEST_MAX }, /* sc */
1306 { TRAMP_SENTINEL_INSN },
1308 ppc64_linux_sigaction_cache_init
1310 static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
1311 SIGTRAMP_FRAME,
1314 { 0x38000077, ULONGEST_MAX }, /* li r0,119 */
1315 { 0x44000002, ULONGEST_MAX }, /* sc */
1316 { TRAMP_SENTINEL_INSN },
1318 ppc32_linux_sighandler_cache_init
1320 static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
1321 SIGTRAMP_FRAME,
1324 { 0x38210080, ULONGEST_MAX }, /* addi r1,r1,128 */
1325 { 0x38000077, ULONGEST_MAX }, /* li r0,119 */
1326 { 0x44000002, ULONGEST_MAX }, /* sc */
1327 { TRAMP_SENTINEL_INSN },
1329 ppc64_linux_sighandler_cache_init
1332 /* Return 1 if PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM are usable. */
1334 ppc_linux_trap_reg_p (struct gdbarch *gdbarch)
1336 /* If we do not have a target description with registers, then
1337 the special registers will not be included in the register set. */
1338 if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
1339 return 0;
1341 /* If we do, then it is safe to check the size. */
1342 return register_size (gdbarch, PPC_ORIG_R3_REGNUM) > 0
1343 && register_size (gdbarch, PPC_TRAP_REGNUM) > 0;
1346 /* Return the current system call's number present in the
1347 r0 register. When the function fails, it returns -1. */
1348 static LONGEST
1349 ppc_linux_get_syscall_number (struct gdbarch *gdbarch,
1350 thread_info *thread)
1352 struct regcache *regcache = get_thread_regcache (thread);
1353 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1354 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1356 /* Make sure we're in a 32- or 64-bit machine */
1357 gdb_assert (tdep->wordsize == 4 || tdep->wordsize == 8);
1359 /* The content of a register */
1360 gdb::byte_vector buf (tdep->wordsize);
1362 /* Getting the system call number from the register.
1363 When dealing with PowerPC architecture, this information
1364 is stored at 0th register. */
1365 regcache->cooked_read (tdep->ppc_gp0_regnum, buf.data ());
1367 return extract_signed_integer (buf.data (), tdep->wordsize, byte_order);
1370 /* PPC process record-replay */
1372 static struct linux_record_tdep ppc_linux_record_tdep;
1373 static struct linux_record_tdep ppc64_linux_record_tdep;
1375 /* ppc_canonicalize_syscall maps from the native PowerPC Linux set of
1376 syscall ids into a canonical set of syscall ids used by process
1377 record. (See arch/powerpc/include/uapi/asm/unistd.h in kernel tree.)
1378 Return -1 if this system call is not supported by process record.
1379 Otherwise, return the syscall number for process record of given
1380 SYSCALL. */
1382 static enum gdb_syscall
1383 ppc_canonicalize_syscall (int syscall, int wordsize)
1385 int result = -1;
1387 if (syscall <= 165)
1388 result = syscall;
1389 else if (syscall >= 167 && syscall <= 190) /* Skip query_module 166 */
1390 result = syscall + 1;
1391 else if (syscall >= 192 && syscall <= 197) /* mmap2 */
1392 result = syscall;
1393 else if (syscall == 208) /* tkill */
1394 result = gdb_sys_tkill;
1395 else if (syscall >= 207 && syscall <= 220) /* gettid */
1396 result = syscall + 224 - 207;
1397 else if (syscall >= 234 && syscall <= 239) /* exit_group */
1398 result = syscall + 252 - 234;
1399 else if (syscall >= 240 && syscall <= 248) /* timer_create */
1400 result = syscall += 259 - 240;
1401 else if (syscall >= 250 && syscall <= 251) /* tgkill */
1402 result = syscall + 270 - 250;
1403 else if (syscall == 286)
1404 result = gdb_sys_openat;
1405 else if (syscall == 291)
1407 if (wordsize == 64)
1408 result = gdb_sys_newfstatat;
1409 else
1410 result = gdb_sys_fstatat64;
1412 else if (syscall == 317)
1413 result = gdb_sys_pipe2;
1414 else if (syscall == 336)
1415 result = gdb_sys_recv;
1416 else if (syscall == 337)
1417 result = gdb_sys_recvfrom;
1418 else if (syscall == 342)
1419 result = gdb_sys_recvmsg;
1420 else if (syscall == 359)
1421 result = gdb_sys_getrandom;
1423 return (enum gdb_syscall) result;
1426 /* Record registers which might be clobbered during system call.
1427 Return 0 if successful. */
1429 static int
1430 ppc_linux_syscall_record (struct regcache *regcache)
1432 struct gdbarch *gdbarch = regcache->arch ();
1433 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1434 ULONGEST scnum;
1435 enum gdb_syscall syscall_gdb;
1436 int ret;
1438 regcache_raw_read_unsigned (regcache, tdep->ppc_gp0_regnum, &scnum);
1439 syscall_gdb = ppc_canonicalize_syscall (scnum, tdep->wordsize);
1441 if (syscall_gdb < 0)
1443 gdb_printf (gdb_stderr,
1444 _("Process record and replay target doesn't "
1445 "support syscall number %d\n"), (int) scnum);
1446 return 0;
1449 if (syscall_gdb == gdb_sys_sigreturn
1450 || syscall_gdb == gdb_sys_rt_sigreturn)
1452 int i, j;
1453 int regsets[] = { tdep->ppc_gp0_regnum,
1454 tdep->ppc_fp0_regnum,
1455 tdep->ppc_vr0_regnum,
1456 tdep->ppc_vsr0_upper_regnum };
1458 for (j = 0; j < 4; j++)
1460 if (regsets[j] == -1)
1461 continue;
1462 for (i = 0; i < 32; i++)
1464 if (record_full_arch_list_add_reg (regcache, regsets[j] + i))
1465 return -1;
1469 if (record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum))
1470 return -1;
1471 if (record_full_arch_list_add_reg (regcache, tdep->ppc_ctr_regnum))
1472 return -1;
1473 if (record_full_arch_list_add_reg (regcache, tdep->ppc_lr_regnum))
1474 return -1;
1475 if (record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum))
1476 return -1;
1478 return 0;
1481 if (tdep->wordsize == 8)
1482 ret = record_linux_system_call (syscall_gdb, regcache,
1483 &ppc64_linux_record_tdep);
1484 else
1485 ret = record_linux_system_call (syscall_gdb, regcache,
1486 &ppc_linux_record_tdep);
1488 if (ret != 0)
1489 return ret;
1491 /* Record registers clobbered during syscall. */
1492 for (int i = 3; i <= 12; i++)
1494 if (record_full_arch_list_add_reg (regcache, tdep->ppc_gp0_regnum + i))
1495 return -1;
1497 if (record_full_arch_list_add_reg (regcache, tdep->ppc_gp0_regnum + 0))
1498 return -1;
1499 if (record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum))
1500 return -1;
1501 if (record_full_arch_list_add_reg (regcache, tdep->ppc_ctr_regnum))
1502 return -1;
1503 if (record_full_arch_list_add_reg (regcache, tdep->ppc_lr_regnum))
1504 return -1;
1506 return 0;
1509 /* Record registers which might be clobbered during signal handling.
1510 Return 0 if successful. */
1512 static int
1513 ppc_linux_record_signal (struct gdbarch *gdbarch, struct regcache *regcache,
1514 enum gdb_signal signal)
1516 /* See handle_rt_signal64 in arch/powerpc/kernel/signal_64.c
1517 handle_rt_signal32 in arch/powerpc/kernel/signal_32.c
1518 arch/powerpc/include/asm/ptrace.h
1519 for details. */
1520 const int SIGNAL_FRAMESIZE = 128;
1521 const int sizeof_rt_sigframe = 1440 * 2 + 8 * 2 + 4 * 6 + 8 + 8 + 128 + 512;
1522 ULONGEST sp;
1523 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1524 int i;
1526 for (i = 3; i <= 12; i++)
1528 if (record_full_arch_list_add_reg (regcache, tdep->ppc_gp0_regnum + i))
1529 return -1;
1532 if (record_full_arch_list_add_reg (regcache, tdep->ppc_lr_regnum))
1533 return -1;
1534 if (record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum))
1535 return -1;
1536 if (record_full_arch_list_add_reg (regcache, tdep->ppc_ctr_regnum))
1537 return -1;
1538 if (record_full_arch_list_add_reg (regcache, gdbarch_pc_regnum (gdbarch)))
1539 return -1;
1540 if (record_full_arch_list_add_reg (regcache, gdbarch_sp_regnum (gdbarch)))
1541 return -1;
1543 /* Record the change in the stack.
1544 frame-size = sizeof (struct rt_sigframe) + SIGNAL_FRAMESIZE */
1545 regcache_raw_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch), &sp);
1546 sp -= SIGNAL_FRAMESIZE;
1547 sp -= sizeof_rt_sigframe;
1549 if (record_full_arch_list_add_mem (sp, SIGNAL_FRAMESIZE + sizeof_rt_sigframe))
1550 return -1;
1552 if (record_full_arch_list_add_end ())
1553 return -1;
1555 return 0;
1558 static void
1559 ppc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
1561 struct gdbarch *gdbarch = regcache->arch ();
1563 regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch), pc);
1565 /* Set special TRAP register to -1 to prevent the kernel from
1566 messing with the PC we just installed, if we happen to be
1567 within an interrupted system call that the kernel wants to
1568 restart.
1570 Note that after we return from the dummy call, the TRAP and
1571 ORIG_R3 registers will be automatically restored, and the
1572 kernel continues to restart the system call at this point. */
1573 if (ppc_linux_trap_reg_p (gdbarch))
1574 regcache_cooked_write_unsigned (regcache, PPC_TRAP_REGNUM, -1);
1577 static const struct target_desc *
1578 ppc_linux_core_read_description (struct gdbarch *gdbarch,
1579 struct target_ops *target,
1580 bfd *abfd)
1582 struct ppc_linux_features features = ppc_linux_no_features;
1583 asection *altivec = bfd_get_section_by_name (abfd, ".reg-ppc-vmx");
1584 asection *vsx = bfd_get_section_by_name (abfd, ".reg-ppc-vsx");
1585 asection *section = bfd_get_section_by_name (abfd, ".reg");
1586 asection *ppr = bfd_get_section_by_name (abfd, ".reg-ppc-ppr");
1587 asection *dscr = bfd_get_section_by_name (abfd, ".reg-ppc-dscr");
1588 asection *tar = bfd_get_section_by_name (abfd, ".reg-ppc-tar");
1589 asection *pmu = bfd_get_section_by_name (abfd, ".reg-ppc-pmu");
1590 asection *htmspr = bfd_get_section_by_name (abfd, ".reg-ppc-tm-spr");
1592 if (! section)
1593 return NULL;
1595 switch (bfd_section_size (section))
1597 case 48 * 4:
1598 features.wordsize = 4;
1599 break;
1600 case 48 * 8:
1601 features.wordsize = 8;
1602 break;
1603 default:
1604 return NULL;
1607 if (altivec)
1608 features.altivec = true;
1610 if (vsx)
1611 features.vsx = true;
1613 std::optional<gdb::byte_vector> auxv = target_read_auxv_raw (target);
1614 CORE_ADDR hwcap = linux_get_hwcap (auxv, target, gdbarch);
1616 features.isa205 = ppc_linux_has_isa205 (hwcap);
1618 if (ppr && dscr)
1620 features.ppr_dscr = true;
1622 /* We don't require the EBB note section to be present in the
1623 core file to select isa207 because these registers could have
1624 been unavailable when the core file was created. They will
1625 be in the tdep but will show as unavailable. */
1626 if (tar && pmu)
1628 features.isa207 = true;
1629 if (htmspr)
1630 features.htm = true;
1634 return ppc_linux_match_description (features);
1638 /* Implementation of `gdbarch_elf_make_msymbol_special', as defined in
1639 gdbarch.h. This implementation is used for the ELFv2 ABI only. */
1641 static void
1642 ppc_elfv2_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
1644 if ((sym->flags & BSF_SYNTHETIC) != 0)
1645 /* ELFv2 synthetic symbols (the PLT stubs and the __glink_PLTresolve
1646 trampoline) do not have a local entry point. */
1647 return;
1649 elf_symbol_type *elf_sym = (elf_symbol_type *)sym;
1651 /* If the symbol is marked as having a local entry point, set a target
1652 flag in the msymbol. We currently only support local entry point
1653 offsets of 8 bytes, which is the only entry point offset ever used
1654 by current compilers. If/when other offsets are ever used, we will
1655 have to use additional target flag bits to store them. */
1656 switch (PPC64_LOCAL_ENTRY_OFFSET (elf_sym->internal_elf_sym.st_other))
1658 default:
1659 break;
1660 case 8:
1661 msym->set_target_flag_1 (true);
1662 break;
1666 /* Implementation of `gdbarch_skip_entrypoint', as defined in
1667 gdbarch.h. This implementation is used for the ELFv2 ABI only. */
1669 static CORE_ADDR
1670 ppc_elfv2_skip_entrypoint (struct gdbarch *gdbarch, CORE_ADDR pc)
1672 struct bound_minimal_symbol fun;
1673 int local_entry_offset = 0;
1675 fun = lookup_minimal_symbol_by_pc (pc);
1676 if (fun.minsym == NULL)
1677 return pc;
1679 /* See ppc_elfv2_elf_make_msymbol_special for how local entry point
1680 offset values are encoded. */
1681 if (fun.minsym->target_flag_1 ())
1682 local_entry_offset = 8;
1684 if (fun.value_address () <= pc
1685 && pc < fun.value_address () + local_entry_offset)
1686 return fun.value_address () + local_entry_offset;
1688 return pc;
1691 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
1692 gdbarch.h. */
1694 static int
1695 ppc_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
1697 return (*s == 'i' /* Literal number. */
1698 || (isdigit (*s) && s[1] == '('
1699 && isdigit (s[2])) /* Displacement. */
1700 || (*s == '(' && isdigit (s[1])) /* Register indirection. */
1701 || isdigit (*s)); /* Register value. */
1704 /* Implementation of `gdbarch_stap_parse_special_token', as defined in
1705 gdbarch.h. */
1707 static expr::operation_up
1708 ppc_stap_parse_special_token (struct gdbarch *gdbarch,
1709 struct stap_parse_info *p)
1711 if (isdigit (*p->arg))
1713 /* This temporary pointer is needed because we have to do a lookahead.
1714 We could be dealing with a register displacement, and in such case
1715 we would not need to do anything. */
1716 const char *s = p->arg;
1717 char *regname;
1718 int len;
1720 while (isdigit (*s))
1721 ++s;
1723 if (*s == '(')
1725 /* It is a register displacement indeed. Returning 0 means we are
1726 deferring the treatment of this case to the generic parser. */
1727 return {};
1730 len = s - p->arg;
1731 regname = (char *) alloca (len + 2);
1732 regname[0] = 'r';
1734 strncpy (regname + 1, p->arg, len);
1735 ++len;
1736 regname[len] = '\0';
1738 if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
1739 error (_("Invalid register name `%s' on expression `%s'."),
1740 regname, p->saved_arg);
1742 p->arg = s;
1744 return expr::make_operation<expr::register_operation> (regname);
1747 /* All the other tokens should be handled correctly by the generic
1748 parser. */
1749 return {};
1752 /* Initialize linux_record_tdep if not initialized yet.
1753 WORDSIZE is 4 or 8 for 32- or 64-bit PowerPC Linux respectively.
1754 Sizes of data structures are initialized accordingly. */
1756 static void
1757 ppc_init_linux_record_tdep (struct linux_record_tdep *record_tdep,
1758 int wordsize)
1760 /* The values for TCGETS, TCSETS, TCSETSW, TCSETSF are based on the
1761 size of struct termios in the kernel source.
1762 include/uapi/asm-generic/termbits.h */
1763 #define SIZE_OF_STRUCT_TERMIOS 0x2c
1765 /* Simply return if it had been initialized. */
1766 if (record_tdep->size_pointer != 0)
1767 return;
1769 /* These values are the size of the type that will be used in a system
1770 call. They are obtained from Linux Kernel source. */
1772 if (wordsize == 8)
1774 record_tdep->size_pointer = 8;
1775 record_tdep->size__old_kernel_stat = 32;
1776 record_tdep->size_tms = 32;
1777 record_tdep->size_loff_t = 8;
1778 record_tdep->size_flock = 32;
1779 record_tdep->size_oldold_utsname = 45;
1780 record_tdep->size_ustat = 32;
1781 record_tdep->size_old_sigaction = 32;
1782 record_tdep->size_old_sigset_t = 8;
1783 record_tdep->size_rlimit = 16;
1784 record_tdep->size_rusage = 144;
1785 record_tdep->size_timeval = 16;
1786 record_tdep->size_timezone = 8;
1787 record_tdep->size_old_gid_t = 4;
1788 record_tdep->size_old_uid_t = 4;
1789 record_tdep->size_fd_set = 128;
1790 record_tdep->size_old_dirent = 280;
1791 record_tdep->size_statfs = 120;
1792 record_tdep->size_statfs64 = 120;
1793 record_tdep->size_sockaddr = 16;
1794 record_tdep->size_int = 4;
1795 record_tdep->size_long = 8;
1796 record_tdep->size_ulong = 8;
1797 record_tdep->size_msghdr = 56;
1798 record_tdep->size_itimerval = 32;
1799 record_tdep->size_stat = 144;
1800 record_tdep->size_old_utsname = 325;
1801 record_tdep->size_sysinfo = 112;
1802 record_tdep->size_msqid_ds = 120;
1803 record_tdep->size_shmid_ds = 112;
1804 record_tdep->size_new_utsname = 390;
1805 record_tdep->size_timex = 208;
1806 record_tdep->size_mem_dqinfo = 24;
1807 record_tdep->size_if_dqblk = 72;
1808 record_tdep->size_fs_quota_stat = 80;
1809 record_tdep->size_timespec = 16;
1810 record_tdep->size_pollfd = 8;
1811 record_tdep->size_NFS_FHSIZE = 32;
1812 record_tdep->size_knfsd_fh = 132;
1813 record_tdep->size_TASK_COMM_LEN = 16;
1814 record_tdep->size_sigaction = 32;
1815 record_tdep->size_sigset_t = 8;
1816 record_tdep->size_siginfo_t = 128;
1817 record_tdep->size_cap_user_data_t = 8;
1818 record_tdep->size_stack_t = 24;
1819 record_tdep->size_off_t = 8;
1820 record_tdep->size_stat64 = 104;
1821 record_tdep->size_gid_t = 4;
1822 record_tdep->size_uid_t = 4;
1823 record_tdep->size_PAGE_SIZE = 0x10000; /* 64KB */
1824 record_tdep->size_flock64 = 32;
1825 record_tdep->size_io_event = 32;
1826 record_tdep->size_iocb = 64;
1827 record_tdep->size_epoll_event = 16;
1828 record_tdep->size_itimerspec = 32;
1829 record_tdep->size_mq_attr = 64;
1830 record_tdep->size_termios = 44;
1831 record_tdep->size_pid_t = 4;
1832 record_tdep->size_winsize = 8;
1833 record_tdep->size_serial_struct = 72;
1834 record_tdep->size_serial_icounter_struct = 80;
1835 record_tdep->size_size_t = 8;
1836 record_tdep->size_iovec = 16;
1837 record_tdep->size_time_t = 8;
1839 else if (wordsize == 4)
1841 record_tdep->size_pointer = 4;
1842 record_tdep->size__old_kernel_stat = 32;
1843 record_tdep->size_tms = 16;
1844 record_tdep->size_loff_t = 8;
1845 record_tdep->size_flock = 16;
1846 record_tdep->size_oldold_utsname = 45;
1847 record_tdep->size_ustat = 20;
1848 record_tdep->size_old_sigaction = 16;
1849 record_tdep->size_old_sigset_t = 4;
1850 record_tdep->size_rlimit = 8;
1851 record_tdep->size_rusage = 72;
1852 record_tdep->size_timeval = 8;
1853 record_tdep->size_timezone = 8;
1854 record_tdep->size_old_gid_t = 4;
1855 record_tdep->size_old_uid_t = 4;
1856 record_tdep->size_fd_set = 128;
1857 record_tdep->size_old_dirent = 268;
1858 record_tdep->size_statfs = 64;
1859 record_tdep->size_statfs64 = 88;
1860 record_tdep->size_sockaddr = 16;
1861 record_tdep->size_int = 4;
1862 record_tdep->size_long = 4;
1863 record_tdep->size_ulong = 4;
1864 record_tdep->size_msghdr = 28;
1865 record_tdep->size_itimerval = 16;
1866 record_tdep->size_stat = 88;
1867 record_tdep->size_old_utsname = 325;
1868 record_tdep->size_sysinfo = 64;
1869 record_tdep->size_msqid_ds = 68;
1870 record_tdep->size_shmid_ds = 60;
1871 record_tdep->size_new_utsname = 390;
1872 record_tdep->size_timex = 128;
1873 record_tdep->size_mem_dqinfo = 24;
1874 record_tdep->size_if_dqblk = 72;
1875 record_tdep->size_fs_quota_stat = 80;
1876 record_tdep->size_timespec = 8;
1877 record_tdep->size_pollfd = 8;
1878 record_tdep->size_NFS_FHSIZE = 32;
1879 record_tdep->size_knfsd_fh = 132;
1880 record_tdep->size_TASK_COMM_LEN = 16;
1881 record_tdep->size_sigaction = 20;
1882 record_tdep->size_sigset_t = 8;
1883 record_tdep->size_siginfo_t = 128;
1884 record_tdep->size_cap_user_data_t = 4;
1885 record_tdep->size_stack_t = 12;
1886 record_tdep->size_off_t = 4;
1887 record_tdep->size_stat64 = 104;
1888 record_tdep->size_gid_t = 4;
1889 record_tdep->size_uid_t = 4;
1890 record_tdep->size_PAGE_SIZE = 0x10000; /* 64KB */
1891 record_tdep->size_flock64 = 32;
1892 record_tdep->size_io_event = 32;
1893 record_tdep->size_iocb = 64;
1894 record_tdep->size_epoll_event = 16;
1895 record_tdep->size_itimerspec = 16;
1896 record_tdep->size_mq_attr = 32;
1897 record_tdep->size_termios = 44;
1898 record_tdep->size_pid_t = 4;
1899 record_tdep->size_winsize = 8;
1900 record_tdep->size_serial_struct = 60;
1901 record_tdep->size_serial_icounter_struct = 80;
1902 record_tdep->size_size_t = 4;
1903 record_tdep->size_iovec = 8;
1904 record_tdep->size_time_t = 4;
1906 else
1907 internal_error (_("unexpected wordsize"));
1909 /* These values are the second argument of system call "sys_fcntl"
1910 and "sys_fcntl64". They are obtained from Linux Kernel source. */
1911 record_tdep->fcntl_F_GETLK = 5;
1912 record_tdep->fcntl_F_GETLK64 = 12;
1913 record_tdep->fcntl_F_SETLK64 = 13;
1914 record_tdep->fcntl_F_SETLKW64 = 14;
1916 record_tdep->arg1 = PPC_R0_REGNUM + 3;
1917 record_tdep->arg2 = PPC_R0_REGNUM + 4;
1918 record_tdep->arg3 = PPC_R0_REGNUM + 5;
1919 record_tdep->arg4 = PPC_R0_REGNUM + 6;
1920 record_tdep->arg5 = PPC_R0_REGNUM + 7;
1921 record_tdep->arg6 = PPC_R0_REGNUM + 8;
1923 /* These values are the second argument of system call "sys_ioctl".
1924 They are obtained from Linux Kernel source.
1925 See arch/powerpc/include/uapi/asm/ioctls.h. */
1926 record_tdep->ioctl_TCGETA = 0x40147417;
1927 record_tdep->ioctl_TCSETA = 0x80147418;
1928 record_tdep->ioctl_TCSETAW = 0x80147419;
1929 record_tdep->ioctl_TCSETAF = 0x8014741c;
1930 record_tdep->ioctl_TCGETS = 0x40007413 | (SIZE_OF_STRUCT_TERMIOS << 16);
1931 record_tdep->ioctl_TCSETS = 0x80007414 | (SIZE_OF_STRUCT_TERMIOS << 16);
1932 record_tdep->ioctl_TCSETSW = 0x80007415 | (SIZE_OF_STRUCT_TERMIOS << 16);
1933 record_tdep->ioctl_TCSETSF = 0x80007416 | (SIZE_OF_STRUCT_TERMIOS << 16);
1935 record_tdep->ioctl_TCSBRK = 0x2000741d;
1936 record_tdep->ioctl_TCXONC = 0x2000741e;
1937 record_tdep->ioctl_TCFLSH = 0x2000741f;
1938 record_tdep->ioctl_TIOCEXCL = 0x540c;
1939 record_tdep->ioctl_TIOCNXCL = 0x540d;
1940 record_tdep->ioctl_TIOCSCTTY = 0x540e;
1941 record_tdep->ioctl_TIOCGPGRP = 0x40047477;
1942 record_tdep->ioctl_TIOCSPGRP = 0x80047476;
1943 record_tdep->ioctl_TIOCOUTQ = 0x40047473;
1944 record_tdep->ioctl_TIOCSTI = 0x5412;
1945 record_tdep->ioctl_TIOCGWINSZ = 0x40087468;
1946 record_tdep->ioctl_TIOCSWINSZ = 0x80087467;
1947 record_tdep->ioctl_TIOCMGET = 0x5415;
1948 record_tdep->ioctl_TIOCMBIS = 0x5416;
1949 record_tdep->ioctl_TIOCMBIC = 0x5417;
1950 record_tdep->ioctl_TIOCMSET = 0x5418;
1951 record_tdep->ioctl_TIOCGSOFTCAR = 0x5419;
1952 record_tdep->ioctl_TIOCSSOFTCAR = 0x541a;
1953 record_tdep->ioctl_FIONREAD = 0x4004667f;
1954 record_tdep->ioctl_TIOCINQ = 0x4004667f;
1955 record_tdep->ioctl_TIOCLINUX = 0x541c;
1956 record_tdep->ioctl_TIOCCONS = 0x541d;
1957 record_tdep->ioctl_TIOCGSERIAL = 0x541e;
1958 record_tdep->ioctl_TIOCSSERIAL = 0x541f;
1959 record_tdep->ioctl_TIOCPKT = 0x5420;
1960 record_tdep->ioctl_FIONBIO = 0x8004667e;
1961 record_tdep->ioctl_TIOCNOTTY = 0x5422;
1962 record_tdep->ioctl_TIOCSETD = 0x5423;
1963 record_tdep->ioctl_TIOCGETD = 0x5424;
1964 record_tdep->ioctl_TCSBRKP = 0x5425;
1965 record_tdep->ioctl_TIOCSBRK = 0x5427;
1966 record_tdep->ioctl_TIOCCBRK = 0x5428;
1967 record_tdep->ioctl_TIOCGSID = 0x5429;
1968 record_tdep->ioctl_TIOCGPTN = 0x40045430;
1969 record_tdep->ioctl_TIOCSPTLCK = 0x80045431;
1970 record_tdep->ioctl_FIONCLEX = 0x20006602;
1971 record_tdep->ioctl_FIOCLEX = 0x20006601;
1972 record_tdep->ioctl_FIOASYNC = 0x8004667d;
1973 record_tdep->ioctl_TIOCSERCONFIG = 0x5453;
1974 record_tdep->ioctl_TIOCSERGWILD = 0x5454;
1975 record_tdep->ioctl_TIOCSERSWILD = 0x5455;
1976 record_tdep->ioctl_TIOCGLCKTRMIOS = 0x5456;
1977 record_tdep->ioctl_TIOCSLCKTRMIOS = 0x5457;
1978 record_tdep->ioctl_TIOCSERGSTRUCT = 0x5458;
1979 record_tdep->ioctl_TIOCSERGETLSR = 0x5459;
1980 record_tdep->ioctl_TIOCSERGETMULTI = 0x545a;
1981 record_tdep->ioctl_TIOCSERSETMULTI = 0x545b;
1982 record_tdep->ioctl_TIOCMIWAIT = 0x545c;
1983 record_tdep->ioctl_TIOCGICOUNT = 0x545d;
1984 record_tdep->ioctl_FIOQSIZE = 0x40086680;
1987 /* Return a floating-point format for a floating-point variable of
1988 length LEN in bits. If non-NULL, NAME is the name of its type.
1989 If no suitable type is found, return NULL. */
1991 static const struct floatformat **
1992 ppc_floatformat_for_type (struct gdbarch *gdbarch,
1993 const char *name, int len)
1995 if (len == 128 && name)
1997 if (strcmp (name, "__float128") == 0
1998 || strcmp (name, "_Float128") == 0
1999 || strcmp (name, "_Float64x") == 0
2000 || strcmp (name, "complex _Float128") == 0
2001 || strcmp (name, "complex _Float64x") == 0)
2002 return floatformats_ieee_quad;
2004 if (strcmp (name, "__ibm128") == 0)
2005 return floatformats_ibm_long_double;
2008 return default_floatformat_for_type (gdbarch, name, len);
2011 static bool
2012 linux_dwarf2_omit_typedef_p (struct type *target_type,
2013 const char *producer, const char *name)
2015 int gcc_major, gcc_minor;
2017 if (producer_is_gcc (producer, &gcc_major, &gcc_minor))
2019 if ((target_type->code () == TYPE_CODE_FLT
2020 || target_type->code () == TYPE_CODE_COMPLEX)
2021 && (strcmp (name, "long double") == 0
2022 || strcmp (name, "complex long double") == 0))
2024 /* IEEE 128-bit floating point and IBM long double are two
2025 encodings for 128-bit values. The DWARF debug data can't
2026 distinguish between them. See bugzilla:
2027 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104194
2029 A GCC hack was introduced to still allow the debugger to identify
2030 the case where "long double" uses the IEEE 128-bit floating point
2031 format: GCC will emit a bogus DWARF type record pretending that
2032 "long double" is a typedef alias for the _Float128 type.
2034 This hack should not be visible to the GDB user, so we replace
2035 this bogus typedef by a normal floating-point type, copying the
2036 format information from the target type of the bogus typedef. */
2037 return true;
2040 return false;
2043 /* Specify the powerpc64le target triplet.
2044 This can be variations of
2045 ppc64le-{distro}-linux-gcc
2047 powerpc64le-{distro}-linux-gcc. */
2049 static const char *
2050 ppc64le_gnu_triplet_regexp (struct gdbarch *gdbarch)
2052 return "p(ower)?pc64le";
2055 /* Specify the powerpc64 target triplet.
2056 This can be variations of
2057 ppc64-{distro}-linux-gcc
2059 powerpc64-{distro}-linux-gcc. */
2061 static const char *
2062 ppc64_gnu_triplet_regexp (struct gdbarch *gdbarch)
2064 return "p(ower)?pc64";
2067 /* Implement the linux_gcc_target_options method. */
2069 static std::string
2070 ppc64_linux_gcc_target_options (struct gdbarch *gdbarch)
2072 return "";
2075 static displaced_step_prepare_status
2076 ppc_linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
2077 CORE_ADDR &displaced_pc)
2079 ppc_inferior_data *per_inferior = get_ppc_per_inferior (thread->inf);
2080 if (!per_inferior->disp_step_buf.has_value ())
2082 /* Figure out where the displaced step buffer is. */
2083 CORE_ADDR disp_step_buf_addr
2084 = linux_displaced_step_location (thread->inf->arch ());
2086 per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
2089 return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
2092 /* Convert a Dwarf 2 register number to a GDB register number for Linux. */
2094 static int
2095 rs6000_linux_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
2097 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep>(gdbarch);
2099 if (0 <= num && num <= 31)
2100 return tdep->ppc_gp0_regnum + num;
2101 else if (32 <= num && num <= 63)
2102 /* Map dwarf register numbers for floating point, double, IBM double and
2103 IEEE 128-bit floating point to the fpr range. Will have to fix the
2104 mapping for the IEEE 128-bit register numbers later. */
2105 return tdep->ppc_fp0_regnum + (num - 32);
2106 else if (77 <= num && num < 77 + 32)
2107 return tdep->ppc_vr0_regnum + (num - 77);
2108 else
2109 switch (num)
2111 case 65:
2112 return tdep->ppc_lr_regnum;
2113 case 66:
2114 return tdep->ppc_ctr_regnum;
2115 case 76:
2116 return tdep->ppc_xer_regnum;
2117 case 109:
2118 return tdep->ppc_vrsave_regnum;
2119 case 110:
2120 return tdep->ppc_vrsave_regnum - 1; /* vscr */
2123 /* Unknown DWARF register number. */
2124 return -1;
2127 /* Translate a .eh_frame register to DWARF register, or adjust a
2128 .debug_frame register. */
2130 static int
2131 rs6000_linux_adjust_frame_regnum (struct gdbarch *gdbarch, int num,
2132 int eh_frame_p)
2134 /* Linux uses the same numbering for .debug_frame numbering as .eh_frame. */
2135 return num;
2138 static void
2139 ppc_linux_init_abi (struct gdbarch_info info,
2140 struct gdbarch *gdbarch)
2142 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
2143 struct tdesc_arch_data *tdesc_data = info.tdesc_data;
2144 static const char *const stap_integer_prefixes[] = { "i", NULL };
2145 static const char *const stap_register_indirection_prefixes[] = { "(",
2146 NULL };
2147 static const char *const stap_register_indirection_suffixes[] = { ")",
2148 NULL };
2150 linux_init_abi (info, gdbarch, 0);
2152 /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
2153 128-bit, they can be either IBM long double or IEEE quad long double.
2154 The 64-bit long double case will be detected automatically using
2155 the size specified in debug info. We use a .gnu.attribute flag
2156 to distinguish between the IBM long double and IEEE quad cases. */
2157 set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
2158 if (tdep->long_double_abi == POWERPC_LONG_DOUBLE_IEEE128)
2159 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
2160 else
2161 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
2163 /* Support for floating-point data type variants. */
2164 set_gdbarch_floatformat_for_type (gdbarch, ppc_floatformat_for_type);
2166 /* Support for replacing typedef record. */
2167 set_gdbarch_dwarf2_omit_typedef_p (gdbarch, linux_dwarf2_omit_typedef_p);
2169 /* Handle inferior calls during interrupted system calls. */
2170 set_gdbarch_write_pc (gdbarch, ppc_linux_write_pc);
2172 /* Get the syscall number from the arch's register. */
2173 set_gdbarch_get_syscall_number (gdbarch, ppc_linux_get_syscall_number);
2175 /* SystemTap functions. */
2176 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
2177 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
2178 stap_register_indirection_prefixes);
2179 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
2180 stap_register_indirection_suffixes);
2181 set_gdbarch_stap_gdb_register_prefix (gdbarch, "r");
2182 set_gdbarch_stap_is_single_operand (gdbarch, ppc_stap_is_single_operand);
2183 set_gdbarch_stap_parse_special_token (gdbarch,
2184 ppc_stap_parse_special_token);
2185 /* Linux DWARF register mapping is different from the other OSes. */
2186 set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
2187 rs6000_linux_dwarf2_reg_to_regnum);
2188 /* Note on Linux the mapping for the DWARF registers and the stab registers
2189 use the same numbers. Install rs6000_linux_dwarf2_reg_to_regnum for the
2190 stab register mappings as well. */
2191 set_gdbarch_stab_reg_to_regnum (gdbarch,
2192 rs6000_linux_dwarf2_reg_to_regnum);
2193 dwarf2_frame_set_adjust_regnum (gdbarch, rs6000_linux_adjust_frame_regnum);
2195 if (tdep->wordsize == 4)
2197 /* Until November 2001, gcc did not comply with the 32 bit SysV
2198 R4 ABI requirement that structures less than or equal to 8
2199 bytes should be returned in registers. Instead GCC was using
2200 the AIX/PowerOpen ABI - everything returned in memory
2201 (well ignoring vectors that is). When this was corrected, it
2202 wasn't fixed for GNU/Linux native platform. Use the
2203 PowerOpen struct convention. */
2204 set_gdbarch_return_value_as_value (gdbarch, ppc_linux_return_value);
2205 set_gdbarch_return_value (gdbarch, nullptr);
2207 set_gdbarch_memory_remove_breakpoint (gdbarch,
2208 ppc_linux_memory_remove_breakpoint);
2210 /* Shared library handling. */
2211 set_gdbarch_skip_trampoline_code (gdbarch, ppc_skip_trampoline_code);
2212 set_solib_svr4_fetch_link_map_offsets
2213 (gdbarch, linux_ilp32_fetch_link_map_offsets);
2215 /* Setting the correct XML syscall filename. */
2216 set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_PPC);
2218 /* Trampolines. */
2219 tramp_frame_prepend_unwinder (gdbarch,
2220 &ppc32_linux_sigaction_tramp_frame);
2221 tramp_frame_prepend_unwinder (gdbarch,
2222 &ppc32_linux_sighandler_tramp_frame);
2224 /* BFD target for core files. */
2225 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
2226 set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpcle");
2227 else
2228 set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");
2230 if (powerpc_so_ops.in_dynsym_resolve_code == NULL)
2232 powerpc_so_ops = svr4_so_ops;
2233 /* Override dynamic resolve function. */
2234 powerpc_so_ops.in_dynsym_resolve_code =
2235 powerpc_linux_in_dynsym_resolve_code;
2237 set_gdbarch_so_ops (gdbarch, &powerpc_so_ops);
2239 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
2242 if (tdep->wordsize == 8)
2244 if (tdep->elf_abi == POWERPC_ELF_V1)
2246 /* Handle PPC GNU/Linux 64-bit function pointers (which are really
2247 function descriptors). */
2248 set_gdbarch_convert_from_func_ptr_addr
2249 (gdbarch, ppc64_convert_from_func_ptr_addr);
2251 set_gdbarch_elf_make_msymbol_special
2252 (gdbarch, ppc64_elf_make_msymbol_special);
2254 else
2256 set_gdbarch_elf_make_msymbol_special
2257 (gdbarch, ppc_elfv2_elf_make_msymbol_special);
2259 set_gdbarch_skip_entrypoint (gdbarch, ppc_elfv2_skip_entrypoint);
2262 /* Shared library handling. */
2263 set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
2264 set_solib_svr4_fetch_link_map_offsets
2265 (gdbarch, linux_lp64_fetch_link_map_offsets);
2267 /* Setting the correct XML syscall filename. */
2268 set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_PPC64);
2270 /* Trampolines. */
2271 tramp_frame_prepend_unwinder (gdbarch,
2272 &ppc64_linux_sigaction_tramp_frame);
2273 tramp_frame_prepend_unwinder (gdbarch,
2274 &ppc64_linux_sighandler_tramp_frame);
2276 /* BFD target for core files. */
2277 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
2278 set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpcle");
2279 else
2280 set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
2281 /* Set compiler triplet. */
2282 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
2283 set_gdbarch_gnu_triplet_regexp (gdbarch, ppc64le_gnu_triplet_regexp);
2284 else
2285 set_gdbarch_gnu_triplet_regexp (gdbarch, ppc64_gnu_triplet_regexp);
2286 /* Set GCC target options. */
2287 set_gdbarch_gcc_target_options (gdbarch, ppc64_linux_gcc_target_options);
2290 set_gdbarch_core_read_description (gdbarch, ppc_linux_core_read_description);
2291 set_gdbarch_iterate_over_regset_sections (gdbarch,
2292 ppc_linux_iterate_over_regset_sections);
2294 /* Enable TLS support. */
2295 set_gdbarch_fetch_tls_load_module_address (gdbarch,
2296 svr4_fetch_objfile_link_map);
2298 if (tdesc_data)
2300 const struct tdesc_feature *feature;
2302 /* If we have target-described registers, then we can safely
2303 reserve a number for PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM
2304 (whether they are described or not). */
2305 gdb_assert (gdbarch_num_regs (gdbarch) <= PPC_ORIG_R3_REGNUM);
2306 set_gdbarch_num_regs (gdbarch, PPC_TRAP_REGNUM + 1);
2308 /* If they are present, then assign them to the reserved number. */
2309 feature = tdesc_find_feature (info.target_desc,
2310 "org.gnu.gdb.power.linux");
2311 if (feature != NULL)
2313 tdesc_numbered_register (feature, tdesc_data,
2314 PPC_ORIG_R3_REGNUM, "orig_r3");
2315 tdesc_numbered_register (feature, tdesc_data,
2316 PPC_TRAP_REGNUM, "trap");
2320 /* Support reverse debugging. */
2321 set_gdbarch_process_record (gdbarch, ppc_process_record);
2322 set_gdbarch_process_record_signal (gdbarch, ppc_linux_record_signal);
2323 tdep->ppc_syscall_record = ppc_linux_syscall_record;
2325 ppc_init_linux_record_tdep (&ppc_linux_record_tdep, 4);
2326 ppc_init_linux_record_tdep (&ppc64_linux_record_tdep, 8);
2328 /* Setup displaced stepping. */
2329 set_gdbarch_displaced_step_prepare (gdbarch,
2330 ppc_linux_displaced_step_prepare);
2334 void _initialize_ppc_linux_tdep ();
2335 void
2336 _initialize_ppc_linux_tdep ()
2338 /* Register for all sub-families of the POWER/PowerPC: 32-bit and
2339 64-bit PowerPC, and the older rs6k. */
2340 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
2341 ppc_linux_init_abi);
2342 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
2343 ppc_linux_init_abi);
2344 gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
2345 ppc_linux_init_abi);
2347 /* Initialize the Linux target descriptions. */
2348 initialize_tdesc_powerpc_32l ();
2349 initialize_tdesc_powerpc_altivec32l ();
2350 initialize_tdesc_powerpc_vsx32l ();
2351 initialize_tdesc_powerpc_isa205_32l ();
2352 initialize_tdesc_powerpc_isa205_altivec32l ();
2353 initialize_tdesc_powerpc_isa205_vsx32l ();
2354 initialize_tdesc_powerpc_isa205_ppr_dscr_vsx32l ();
2355 initialize_tdesc_powerpc_isa207_vsx32l ();
2356 initialize_tdesc_powerpc_isa207_htm_vsx32l ();
2357 initialize_tdesc_powerpc_64l ();
2358 initialize_tdesc_powerpc_altivec64l ();
2359 initialize_tdesc_powerpc_vsx64l ();
2360 initialize_tdesc_powerpc_isa205_64l ();
2361 initialize_tdesc_powerpc_isa205_altivec64l ();
2362 initialize_tdesc_powerpc_isa205_vsx64l ();
2363 initialize_tdesc_powerpc_isa205_ppr_dscr_vsx64l ();
2364 initialize_tdesc_powerpc_isa207_vsx64l ();
2365 initialize_tdesc_powerpc_isa207_htm_vsx64l ();
2366 initialize_tdesc_powerpc_e500l ();