1 /* GNU/Linux/PowerPC specific low level interface, for the remote server for
3 Copyright (C) 1995-2017 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/>. */
21 #include "linux-low.h"
24 #include <asm/ptrace.h>
26 #include "nat/ppc-linux.h"
27 #include "linux-ppc-tdesc.h"
29 #include "tracepoint.h"
31 #define PPC_FIELD(value, from, len) \
32 (((value) >> (32 - (from) - (len))) & ((1 << (len)) - 1))
33 #define PPC_SEXT(v, bs) \
34 ((((CORE_ADDR) (v) & (((CORE_ADDR) 1 << (bs)) - 1)) \
35 ^ ((CORE_ADDR) 1 << ((bs) - 1))) \
36 - ((CORE_ADDR) 1 << ((bs) - 1)))
37 #define PPC_OP6(insn) PPC_FIELD (insn, 0, 6)
38 #define PPC_BO(insn) PPC_FIELD (insn, 6, 5)
39 #define PPC_LI(insn) (PPC_SEXT (PPC_FIELD (insn, 6, 24), 24) << 2)
40 #define PPC_BD(insn) (PPC_SEXT (PPC_FIELD (insn, 16, 14), 14) << 2)
42 static unsigned long ppc_hwcap
;
45 #define ppc_num_regs 73
48 /* We use a constant for FPSCR instead of PT_FPSCR, because
49 many shipped PPC64 kernels had the wrong value in ptrace.h. */
50 static int ppc_regmap
[] =
51 {PT_R0
* 8, PT_R1
* 8, PT_R2
* 8, PT_R3
* 8,
52 PT_R4
* 8, PT_R5
* 8, PT_R6
* 8, PT_R7
* 8,
53 PT_R8
* 8, PT_R9
* 8, PT_R10
* 8, PT_R11
* 8,
54 PT_R12
* 8, PT_R13
* 8, PT_R14
* 8, PT_R15
* 8,
55 PT_R16
* 8, PT_R17
* 8, PT_R18
* 8, PT_R19
* 8,
56 PT_R20
* 8, PT_R21
* 8, PT_R22
* 8, PT_R23
* 8,
57 PT_R24
* 8, PT_R25
* 8, PT_R26
* 8, PT_R27
* 8,
58 PT_R28
* 8, PT_R29
* 8, PT_R30
* 8, PT_R31
* 8,
59 PT_FPR0
*8, PT_FPR0
*8 + 8, PT_FPR0
*8+16, PT_FPR0
*8+24,
60 PT_FPR0
*8+32, PT_FPR0
*8+40, PT_FPR0
*8+48, PT_FPR0
*8+56,
61 PT_FPR0
*8+64, PT_FPR0
*8+72, PT_FPR0
*8+80, PT_FPR0
*8+88,
62 PT_FPR0
*8+96, PT_FPR0
*8+104, PT_FPR0
*8+112, PT_FPR0
*8+120,
63 PT_FPR0
*8+128, PT_FPR0
*8+136, PT_FPR0
*8+144, PT_FPR0
*8+152,
64 PT_FPR0
*8+160, PT_FPR0
*8+168, PT_FPR0
*8+176, PT_FPR0
*8+184,
65 PT_FPR0
*8+192, PT_FPR0
*8+200, PT_FPR0
*8+208, PT_FPR0
*8+216,
66 PT_FPR0
*8+224, PT_FPR0
*8+232, PT_FPR0
*8+240, PT_FPR0
*8+248,
67 PT_NIP
* 8, PT_MSR
* 8, PT_CCR
* 8, PT_LNK
* 8,
68 PT_CTR
* 8, PT_XER
* 8, PT_FPR0
*8 + 256,
69 PT_ORIG_R3
* 8, PT_TRAP
* 8 };
71 /* Currently, don't check/send MQ. */
72 static int ppc_regmap
[] =
73 {PT_R0
* 4, PT_R1
* 4, PT_R2
* 4, PT_R3
* 4,
74 PT_R4
* 4, PT_R5
* 4, PT_R6
* 4, PT_R7
* 4,
75 PT_R8
* 4, PT_R9
* 4, PT_R10
* 4, PT_R11
* 4,
76 PT_R12
* 4, PT_R13
* 4, PT_R14
* 4, PT_R15
* 4,
77 PT_R16
* 4, PT_R17
* 4, PT_R18
* 4, PT_R19
* 4,
78 PT_R20
* 4, PT_R21
* 4, PT_R22
* 4, PT_R23
* 4,
79 PT_R24
* 4, PT_R25
* 4, PT_R26
* 4, PT_R27
* 4,
80 PT_R28
* 4, PT_R29
* 4, PT_R30
* 4, PT_R31
* 4,
81 PT_FPR0
*4, PT_FPR0
*4 + 8, PT_FPR0
*4+16, PT_FPR0
*4+24,
82 PT_FPR0
*4+32, PT_FPR0
*4+40, PT_FPR0
*4+48, PT_FPR0
*4+56,
83 PT_FPR0
*4+64, PT_FPR0
*4+72, PT_FPR0
*4+80, PT_FPR0
*4+88,
84 PT_FPR0
*4+96, PT_FPR0
*4+104, PT_FPR0
*4+112, PT_FPR0
*4+120,
85 PT_FPR0
*4+128, PT_FPR0
*4+136, PT_FPR0
*4+144, PT_FPR0
*4+152,
86 PT_FPR0
*4+160, PT_FPR0
*4+168, PT_FPR0
*4+176, PT_FPR0
*4+184,
87 PT_FPR0
*4+192, PT_FPR0
*4+200, PT_FPR0
*4+208, PT_FPR0
*4+216,
88 PT_FPR0
*4+224, PT_FPR0
*4+232, PT_FPR0
*4+240, PT_FPR0
*4+248,
89 PT_NIP
* 4, PT_MSR
* 4, PT_CCR
* 4, PT_LNK
* 4,
90 PT_CTR
* 4, PT_XER
* 4, PT_FPSCR
* 4,
91 PT_ORIG_R3
* 4, PT_TRAP
* 4
94 static int ppc_regmap_e500
[] =
95 {PT_R0
* 4, PT_R1
* 4, PT_R2
* 4, PT_R3
* 4,
96 PT_R4
* 4, PT_R5
* 4, PT_R6
* 4, PT_R7
* 4,
97 PT_R8
* 4, PT_R9
* 4, PT_R10
* 4, PT_R11
* 4,
98 PT_R12
* 4, PT_R13
* 4, PT_R14
* 4, PT_R15
* 4,
99 PT_R16
* 4, PT_R17
* 4, PT_R18
* 4, PT_R19
* 4,
100 PT_R20
* 4, PT_R21
* 4, PT_R22
* 4, PT_R23
* 4,
101 PT_R24
* 4, PT_R25
* 4, PT_R26
* 4, PT_R27
* 4,
102 PT_R28
* 4, PT_R29
* 4, PT_R30
* 4, PT_R31
* 4,
111 PT_NIP
* 4, PT_MSR
* 4, PT_CCR
* 4, PT_LNK
* 4,
112 PT_CTR
* 4, PT_XER
* 4, -1,
113 PT_ORIG_R3
* 4, PT_TRAP
* 4
118 ppc_cannot_store_register (int regno
)
120 const struct target_desc
*tdesc
= current_process ()->tdesc
;
122 #ifndef __powerpc64__
123 /* Some kernels do not allow us to store fpscr. */
124 if (!(ppc_hwcap
& PPC_FEATURE_HAS_SPE
)
125 && regno
== find_regno (tdesc
, "fpscr"))
129 /* Some kernels do not allow us to store orig_r3 or trap. */
130 if (regno
== find_regno (tdesc
, "orig_r3")
131 || regno
== find_regno (tdesc
, "trap"))
138 ppc_cannot_fetch_register (int regno
)
144 ppc_collect_ptrace_register (struct regcache
*regcache
, int regno
, char *buf
)
146 memset (buf
, 0, sizeof (long));
148 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
150 /* Little-endian values always sit at the left end of the buffer. */
151 collect_register (regcache
, regno
, buf
);
153 else if (__BYTE_ORDER
== __BIG_ENDIAN
)
155 /* Big-endian values sit at the right end of the buffer. In case of
156 registers whose sizes are smaller than sizeof (long), we must use a
157 padding to access them correctly. */
158 int size
= register_size (regcache
->tdesc
, regno
);
160 if (size
< sizeof (long))
161 collect_register (regcache
, regno
, buf
+ sizeof (long) - size
);
163 collect_register (regcache
, regno
, buf
);
166 perror_with_name ("Unexpected byte order");
170 ppc_supply_ptrace_register (struct regcache
*regcache
,
171 int regno
, const char *buf
)
173 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
175 /* Little-endian values always sit at the left end of the buffer. */
176 supply_register (regcache
, regno
, buf
);
178 else if (__BYTE_ORDER
== __BIG_ENDIAN
)
180 /* Big-endian values sit at the right end of the buffer. In case of
181 registers whose sizes are smaller than sizeof (long), we must use a
182 padding to access them correctly. */
183 int size
= register_size (regcache
->tdesc
, regno
);
185 if (size
< sizeof (long))
186 supply_register (regcache
, regno
, buf
+ sizeof (long) - size
);
188 supply_register (regcache
, regno
, buf
);
191 perror_with_name ("Unexpected byte order");
195 #define INSTR_SC 0x44000002
196 #define NR_spu_run 0x0116
198 /* If the PPU thread is currently stopped on a spu_run system call,
199 return to FD and ADDR the file handle and NPC parameter address
200 used with the system call. Return non-zero if successful. */
202 parse_spufs_run (struct regcache
*regcache
, int *fd
, CORE_ADDR
*addr
)
208 if (register_size (regcache
->tdesc
, 0) == 4)
210 unsigned int pc
, r0
, r3
, r4
;
211 collect_register_by_name (regcache
, "pc", &pc
);
212 collect_register_by_name (regcache
, "r0", &r0
);
213 collect_register_by_name (regcache
, "orig_r3", &r3
);
214 collect_register_by_name (regcache
, "r4", &r4
);
215 curr_pc
= (CORE_ADDR
) pc
;
218 *addr
= (CORE_ADDR
) r4
;
222 unsigned long pc
, r0
, r3
, r4
;
223 collect_register_by_name (regcache
, "pc", &pc
);
224 collect_register_by_name (regcache
, "r0", &r0
);
225 collect_register_by_name (regcache
, "orig_r3", &r3
);
226 collect_register_by_name (regcache
, "r4", &r4
);
227 curr_pc
= (CORE_ADDR
) pc
;
230 *addr
= (CORE_ADDR
) r4
;
233 /* Fetch instruction preceding current NIP. */
234 if ((*the_target
->read_memory
) (curr_pc
- 4,
235 (unsigned char *) &curr_insn
, 4) != 0)
237 /* It should be a "sc" instruction. */
238 if (curr_insn
!= INSTR_SC
)
240 /* System call number should be NR_spu_run. */
241 if (curr_r0
!= NR_spu_run
)
248 ppc_get_pc (struct regcache
*regcache
)
253 if (parse_spufs_run (regcache
, &fd
, &addr
))
256 (*the_target
->read_memory
) (addr
, (unsigned char *) &pc
, 4);
257 return ((CORE_ADDR
)1 << 63)
258 | ((CORE_ADDR
)fd
<< 32) | (CORE_ADDR
) (pc
- 4);
260 else if (register_size (regcache
->tdesc
, 0) == 4)
263 collect_register_by_name (regcache
, "pc", &pc
);
264 return (CORE_ADDR
) pc
;
269 collect_register_by_name (regcache
, "pc", &pc
);
270 return (CORE_ADDR
) pc
;
275 ppc_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
280 if (parse_spufs_run (regcache
, &fd
, &addr
))
282 unsigned int newpc
= pc
;
283 (*the_target
->write_memory
) (addr
, (unsigned char *) &newpc
, 4);
285 else if (register_size (regcache
->tdesc
, 0) == 4)
287 unsigned int newpc
= pc
;
288 supply_register_by_name (regcache
, "pc", &newpc
);
292 unsigned long newpc
= pc
;
293 supply_register_by_name (regcache
, "pc", &newpc
);
299 ppc_get_auxv (unsigned long type
, unsigned long *valp
)
301 const struct target_desc
*tdesc
= current_process ()->tdesc
;
302 int wordsize
= register_size (tdesc
, 0);
303 unsigned char *data
= (unsigned char *) alloca (2 * wordsize
);
306 while ((*the_target
->read_auxv
) (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
310 unsigned int *data_p
= (unsigned int *)data
;
311 if (data_p
[0] == type
)
319 unsigned long *data_p
= (unsigned long *)data
;
320 if (data_p
[0] == type
)
327 offset
+= 2 * wordsize
;
334 #ifndef __powerpc64__
335 static int ppc_regmap_adjusted
;
339 /* Correct in either endianness.
340 This instruction is "twge r2, r2", which GDB uses as a software
342 static const unsigned int ppc_breakpoint
= 0x7d821008;
343 #define ppc_breakpoint_len 4
345 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
347 static const gdb_byte
*
348 ppc_sw_breakpoint_from_kind (int kind
, int *size
)
350 *size
= ppc_breakpoint_len
;
351 return (const gdb_byte
*) &ppc_breakpoint
;
355 ppc_breakpoint_at (CORE_ADDR where
)
359 if (where
& ((CORE_ADDR
)1 << 63))
362 sprintf (mem_annex
, "%d/mem", (int)((where
>> 32) & 0x7fffffff));
363 (*the_target
->qxfer_spu
) (mem_annex
, (unsigned char *) &insn
,
364 NULL
, where
& 0xffffffff, 4);
370 (*the_target
->read_memory
) (where
, (unsigned char *) &insn
, 4);
371 if (insn
== ppc_breakpoint
)
373 /* If necessary, recognize more trap instructions here. GDB only uses
380 /* Implement supports_z_point_type target-ops.
381 Returns true if type Z_TYPE breakpoint is supported.
383 Handling software breakpoint at server side, so tracepoints
384 and breakpoints can be inserted at the same location. */
387 ppc_supports_z_point_type (char z_type
)
394 case Z_PACKET_WRITE_WP
:
395 case Z_PACKET_ACCESS_WP
:
401 /* Implement insert_point target-ops.
402 Returns 0 on success, -1 on failure and 1 on unsupported. */
405 ppc_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
406 int size
, struct raw_breakpoint
*bp
)
410 case raw_bkpt_type_sw
:
411 return insert_memory_breakpoint (bp
);
413 case raw_bkpt_type_hw
:
414 case raw_bkpt_type_write_wp
:
415 case raw_bkpt_type_access_wp
:
422 /* Implement remove_point target-ops.
423 Returns 0 on success, -1 on failure and 1 on unsupported. */
426 ppc_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
427 int size
, struct raw_breakpoint
*bp
)
431 case raw_bkpt_type_sw
:
432 return remove_memory_breakpoint (bp
);
434 case raw_bkpt_type_hw
:
435 case raw_bkpt_type_write_wp
:
436 case raw_bkpt_type_access_wp
:
443 /* Provide only a fill function for the general register set. ps_lgetregs
444 will use this for NPTL support. */
446 static void ppc_fill_gregset (struct regcache
*regcache
, void *buf
)
450 for (i
= 0; i
< 32; i
++)
451 ppc_collect_ptrace_register (regcache
, i
, (char *) buf
+ ppc_regmap
[i
]);
453 for (i
= 64; i
< 70; i
++)
454 ppc_collect_ptrace_register (regcache
, i
, (char *) buf
+ ppc_regmap
[i
]);
456 for (i
= 71; i
< 73; i
++)
457 ppc_collect_ptrace_register (regcache
, i
, (char *) buf
+ ppc_regmap
[i
]);
460 #define SIZEOF_VSXREGS 32*8
463 ppc_fill_vsxregset (struct regcache
*regcache
, void *buf
)
466 char *regset
= (char *) buf
;
468 if (!(ppc_hwcap
& PPC_FEATURE_HAS_VSX
))
471 base
= find_regno (regcache
->tdesc
, "vs0h");
472 for (i
= 0; i
< 32; i
++)
473 collect_register (regcache
, base
+ i
, ®set
[i
* 8]);
477 ppc_store_vsxregset (struct regcache
*regcache
, const void *buf
)
480 const char *regset
= (const char *) buf
;
482 if (!(ppc_hwcap
& PPC_FEATURE_HAS_VSX
))
485 base
= find_regno (regcache
->tdesc
, "vs0h");
486 for (i
= 0; i
< 32; i
++)
487 supply_register (regcache
, base
+ i
, ®set
[i
* 8]);
490 #define SIZEOF_VRREGS 33*16+4
493 ppc_fill_vrregset (struct regcache
*regcache
, void *buf
)
496 char *regset
= (char *) buf
;
498 if (!(ppc_hwcap
& PPC_FEATURE_HAS_ALTIVEC
))
501 base
= find_regno (regcache
->tdesc
, "vr0");
502 for (i
= 0; i
< 32; i
++)
503 collect_register (regcache
, base
+ i
, ®set
[i
* 16]);
505 collect_register_by_name (regcache
, "vscr", ®set
[32 * 16 + 12]);
506 collect_register_by_name (regcache
, "vrsave", ®set
[33 * 16]);
510 ppc_store_vrregset (struct regcache
*regcache
, const void *buf
)
513 const char *regset
= (const char *) buf
;
515 if (!(ppc_hwcap
& PPC_FEATURE_HAS_ALTIVEC
))
518 base
= find_regno (regcache
->tdesc
, "vr0");
519 for (i
= 0; i
< 32; i
++)
520 supply_register (regcache
, base
+ i
, ®set
[i
* 16]);
522 supply_register_by_name (regcache
, "vscr", ®set
[32 * 16 + 12]);
523 supply_register_by_name (regcache
, "vrsave", ®set
[33 * 16]);
526 struct gdb_evrregset_t
528 unsigned long evr
[32];
529 unsigned long long acc
;
530 unsigned long spefscr
;
534 ppc_fill_evrregset (struct regcache
*regcache
, void *buf
)
537 struct gdb_evrregset_t
*regset
= (struct gdb_evrregset_t
*) buf
;
539 if (!(ppc_hwcap
& PPC_FEATURE_HAS_SPE
))
542 ev0
= find_regno (regcache
->tdesc
, "ev0h");
543 for (i
= 0; i
< 32; i
++)
544 collect_register (regcache
, ev0
+ i
, ®set
->evr
[i
]);
546 collect_register_by_name (regcache
, "acc", ®set
->acc
);
547 collect_register_by_name (regcache
, "spefscr", ®set
->spefscr
);
551 ppc_store_evrregset (struct regcache
*regcache
, const void *buf
)
554 const struct gdb_evrregset_t
*regset
= (const struct gdb_evrregset_t
*) buf
;
556 if (!(ppc_hwcap
& PPC_FEATURE_HAS_SPE
))
559 ev0
= find_regno (regcache
->tdesc
, "ev0h");
560 for (i
= 0; i
< 32; i
++)
561 supply_register (regcache
, ev0
+ i
, ®set
->evr
[i
]);
563 supply_register_by_name (regcache
, "acc", ®set
->acc
);
564 supply_register_by_name (regcache
, "spefscr", ®set
->spefscr
);
567 /* Support for hardware single step. */
570 ppc_supports_hardware_single_step (void)
575 static struct regset_info ppc_regsets
[] = {
576 /* List the extra register sets before GENERAL_REGS. That way we will
577 fetch them every time, but still fall back to PTRACE_PEEKUSER for the
578 general registers. Some kernels support these, but not the newer
579 PPC_PTRACE_GETREGS. */
580 { PTRACE_GETVSXREGS
, PTRACE_SETVSXREGS
, 0, SIZEOF_VSXREGS
, EXTENDED_REGS
,
581 ppc_fill_vsxregset
, ppc_store_vsxregset
},
582 { PTRACE_GETVRREGS
, PTRACE_SETVRREGS
, 0, SIZEOF_VRREGS
, EXTENDED_REGS
,
583 ppc_fill_vrregset
, ppc_store_vrregset
},
584 { PTRACE_GETEVRREGS
, PTRACE_SETEVRREGS
, 0, 32 * 4 + 8 + 4, EXTENDED_REGS
,
585 ppc_fill_evrregset
, ppc_store_evrregset
},
586 { 0, 0, 0, 0, GENERAL_REGS
, ppc_fill_gregset
, NULL
},
590 static struct usrregs_info ppc_usrregs_info
=
596 static struct regsets_info ppc_regsets_info
=
598 ppc_regsets
, /* regsets */
600 NULL
, /* disabled_regsets */
603 static struct regs_info regs_info
=
605 NULL
, /* regset_bitmap */
610 static const struct regs_info
*
617 ppc_arch_setup (void)
619 const struct target_desc
*tdesc
;
622 struct regcache
*regcache
;
624 /* On a 64-bit host, assume 64-bit inferior process with no
625 AltiVec registers. Reset ppc_hwcap to ensure that the
626 collect_register call below does not fail. */
627 tdesc
= tdesc_powerpc_64l
;
628 current_process ()->tdesc
= tdesc
;
631 regcache
= new_register_cache (tdesc
);
632 fetch_inferior_registers (regcache
, find_regno (tdesc
, "msr"));
633 collect_register_by_name (regcache
, "msr", &msr
);
634 free_register_cache (regcache
);
635 if (ppc64_64bit_inferior_p (msr
))
637 ppc_get_auxv (AT_HWCAP
, &ppc_hwcap
);
638 if (ppc_hwcap
& PPC_FEATURE_CELL
)
639 tdesc
= tdesc_powerpc_cell64l
;
640 else if (ppc_hwcap
& PPC_FEATURE_HAS_VSX
)
642 /* Power ISA 2.05 (implemented by Power 6 and newer processors)
643 increases the FPSCR from 32 bits to 64 bits. Even though Power 7
644 supports this ISA version, it doesn't have PPC_FEATURE_ARCH_2_05
645 set, only PPC_FEATURE_ARCH_2_06. Since for now the only bits
646 used in the higher half of the register are for Decimal Floating
647 Point, we check if that feature is available to decide the size
649 if (ppc_hwcap
& PPC_FEATURE_HAS_DFP
)
650 tdesc
= tdesc_powerpc_isa205_vsx64l
;
652 tdesc
= tdesc_powerpc_vsx64l
;
654 else if (ppc_hwcap
& PPC_FEATURE_HAS_ALTIVEC
)
656 if (ppc_hwcap
& PPC_FEATURE_HAS_DFP
)
657 tdesc
= tdesc_powerpc_isa205_altivec64l
;
659 tdesc
= tdesc_powerpc_altivec64l
;
662 current_process ()->tdesc
= tdesc
;
667 /* OK, we have a 32-bit inferior. */
668 tdesc
= tdesc_powerpc_32l
;
669 current_process ()->tdesc
= tdesc
;
671 ppc_get_auxv (AT_HWCAP
, &ppc_hwcap
);
672 if (ppc_hwcap
& PPC_FEATURE_CELL
)
673 tdesc
= tdesc_powerpc_cell32l
;
674 else if (ppc_hwcap
& PPC_FEATURE_HAS_VSX
)
676 if (ppc_hwcap
& PPC_FEATURE_HAS_DFP
)
677 tdesc
= tdesc_powerpc_isa205_vsx32l
;
679 tdesc
= tdesc_powerpc_vsx32l
;
681 else if (ppc_hwcap
& PPC_FEATURE_HAS_ALTIVEC
)
683 if (ppc_hwcap
& PPC_FEATURE_HAS_DFP
)
684 tdesc
= tdesc_powerpc_isa205_altivec32l
;
686 tdesc
= tdesc_powerpc_altivec32l
;
689 /* On 32-bit machines, check for SPE registers.
690 Set the low target's regmap field as appropriately. */
691 #ifndef __powerpc64__
692 if (ppc_hwcap
& PPC_FEATURE_HAS_SPE
)
693 tdesc
= tdesc_powerpc_e500l
;
695 if (!ppc_regmap_adjusted
)
697 if (ppc_hwcap
& PPC_FEATURE_HAS_SPE
)
698 ppc_usrregs_info
.regmap
= ppc_regmap_e500
;
700 /* If the FPSCR is 64-bit wide, we need to fetch the whole
701 64-bit slot and not just its second word. The PT_FPSCR
702 supplied in a 32-bit GDB compilation doesn't reflect
704 if (register_size (tdesc
, 70) == 8)
705 ppc_regmap
[70] = (48 + 2*32) * sizeof (long);
707 ppc_regmap_adjusted
= 1;
710 current_process ()->tdesc
= tdesc
;
713 /* Implementation of linux_target_ops method "supports_tracepoints". */
716 ppc_supports_tracepoints (void)
721 /* Get the thread area address. This is used to recognize which
722 thread is which when tracing with the in-process agent library. We
723 don't read anything from the address, and treat it as opaque; it's
724 the address itself that we assume is unique per-thread. */
727 ppc_get_thread_area (int lwpid
, CORE_ADDR
*addr
)
729 struct lwp_info
*lwp
= find_lwp_pid (pid_to_ptid (lwpid
));
730 struct thread_info
*thr
= get_lwp_thread (lwp
);
731 struct regcache
*regcache
= get_thread_regcache (thr
, 1);
735 if (register_size (regcache
->tdesc
, 0) == 8)
736 collect_register_by_name (regcache
, "r13", &tp
);
739 collect_register_by_name (regcache
, "r2", &tp
);
748 /* Older glibc doesn't provide this. */
751 #define EF_PPC64_ABI 3
754 /* Returns 1 if inferior is using ELFv2 ABI. Undefined for 32-bit
758 is_elfv2_inferior (void)
760 /* To be used as fallback if we're unable to determine the right result -
761 assume inferior uses the same ABI as gdbserver. */
763 const int def_res
= 1;
765 const int def_res
= 0;
770 if (!ppc_get_auxv (AT_PHDR
, &phdr
))
773 /* Assume ELF header is at the beginning of the page where program headers
774 are located. If it doesn't look like one, bail. */
776 read_inferior_memory (phdr
& ~0xfff, (unsigned char *) &ehdr
, sizeof ehdr
);
777 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
))
780 return (ehdr
.e_flags
& EF_PPC64_ABI
) == 2;
785 /* Generate a ds-form instruction in BUF and return the number of bytes written
788 | OPCD | RST | RA | DS |XO| */
790 __attribute__((unused
)) /* Maybe unused due to conditional compilation. */
792 gen_ds_form (uint32_t *buf
, int opcd
, int rst
, int ra
, int ds
, int xo
)
796 gdb_assert ((opcd
& ~0x3f) == 0);
797 gdb_assert ((rst
& ~0x1f) == 0);
798 gdb_assert ((ra
& ~0x1f) == 0);
799 gdb_assert ((xo
& ~0x3) == 0);
801 insn
= (rst
<< 21) | (ra
<< 16) | (ds
& 0xfffc) | (xo
& 0x3);
802 *buf
= (opcd
<< 26) | insn
;
806 /* Followings are frequently used ds-form instructions. */
808 #define GEN_STD(buf, rs, ra, offset) gen_ds_form (buf, 62, rs, ra, offset, 0)
809 #define GEN_STDU(buf, rs, ra, offset) gen_ds_form (buf, 62, rs, ra, offset, 1)
810 #define GEN_LD(buf, rt, ra, offset) gen_ds_form (buf, 58, rt, ra, offset, 0)
811 #define GEN_LDU(buf, rt, ra, offset) gen_ds_form (buf, 58, rt, ra, offset, 1)
813 /* Generate a d-form instruction in BUF.
816 | OPCD | RST | RA | D | */
819 gen_d_form (uint32_t *buf
, int opcd
, int rst
, int ra
, int si
)
823 gdb_assert ((opcd
& ~0x3f) == 0);
824 gdb_assert ((rst
& ~0x1f) == 0);
825 gdb_assert ((ra
& ~0x1f) == 0);
827 insn
= (rst
<< 21) | (ra
<< 16) | (si
& 0xffff);
828 *buf
= (opcd
<< 26) | insn
;
832 /* Followings are frequently used d-form instructions. */
834 #define GEN_ADDI(buf, rt, ra, si) gen_d_form (buf, 14, rt, ra, si)
835 #define GEN_ADDIS(buf, rt, ra, si) gen_d_form (buf, 15, rt, ra, si)
836 #define GEN_LI(buf, rt, si) GEN_ADDI (buf, rt, 0, si)
837 #define GEN_LIS(buf, rt, si) GEN_ADDIS (buf, rt, 0, si)
838 #define GEN_ORI(buf, rt, ra, si) gen_d_form (buf, 24, rt, ra, si)
839 #define GEN_ORIS(buf, rt, ra, si) gen_d_form (buf, 25, rt, ra, si)
840 #define GEN_LWZ(buf, rt, ra, si) gen_d_form (buf, 32, rt, ra, si)
841 #define GEN_STW(buf, rt, ra, si) gen_d_form (buf, 36, rt, ra, si)
842 #define GEN_STWU(buf, rt, ra, si) gen_d_form (buf, 37, rt, ra, si)
844 /* Generate a xfx-form instruction in BUF and return the number of bytes
848 | OPCD | RST | RI | XO |/| */
851 gen_xfx_form (uint32_t *buf
, int opcd
, int rst
, int ri
, int xo
)
854 unsigned int n
= ((ri
& 0x1f) << 5) | ((ri
>> 5) & 0x1f);
856 gdb_assert ((opcd
& ~0x3f) == 0);
857 gdb_assert ((rst
& ~0x1f) == 0);
858 gdb_assert ((xo
& ~0x3ff) == 0);
860 insn
= (rst
<< 21) | (n
<< 11) | (xo
<< 1);
861 *buf
= (opcd
<< 26) | insn
;
865 /* Followings are frequently used xfx-form instructions. */
867 #define GEN_MFSPR(buf, rt, spr) gen_xfx_form (buf, 31, rt, spr, 339)
868 #define GEN_MTSPR(buf, rt, spr) gen_xfx_form (buf, 31, rt, spr, 467)
869 #define GEN_MFCR(buf, rt) gen_xfx_form (buf, 31, rt, 0, 19)
870 #define GEN_MTCR(buf, rt) gen_xfx_form (buf, 31, rt, 0x3cf, 144)
871 #define GEN_SYNC(buf, L, E) gen_xfx_form (buf, 31, L & 0x3, \
873 #define GEN_LWSYNC(buf) GEN_SYNC (buf, 1, 0)
876 /* Generate a x-form instruction in BUF and return the number of bytes written.
879 | OPCD | RST | RA | RB | XO |RC| */
882 gen_x_form (uint32_t *buf
, int opcd
, int rst
, int ra
, int rb
, int xo
, int rc
)
886 gdb_assert ((opcd
& ~0x3f) == 0);
887 gdb_assert ((rst
& ~0x1f) == 0);
888 gdb_assert ((ra
& ~0x1f) == 0);
889 gdb_assert ((rb
& ~0x1f) == 0);
890 gdb_assert ((xo
& ~0x3ff) == 0);
891 gdb_assert ((rc
& ~1) == 0);
893 insn
= (rst
<< 21) | (ra
<< 16) | (rb
<< 11) | (xo
<< 1) | rc
;
894 *buf
= (opcd
<< 26) | insn
;
898 /* Followings are frequently used x-form instructions. */
900 #define GEN_OR(buf, ra, rs, rb) gen_x_form (buf, 31, rs, ra, rb, 444, 0)
901 #define GEN_MR(buf, ra, rs) GEN_OR (buf, ra, rs, rs)
902 #define GEN_LWARX(buf, rt, ra, rb) gen_x_form (buf, 31, rt, ra, rb, 20, 0)
903 #define GEN_STWCX(buf, rs, ra, rb) gen_x_form (buf, 31, rs, ra, rb, 150, 1)
904 /* Assume bf = cr7. */
905 #define GEN_CMPW(buf, ra, rb) gen_x_form (buf, 31, 28, ra, rb, 0, 0)
908 /* Generate a md-form instruction in BUF and return the number of bytes written.
910 0 6 11 16 21 27 30 31 32
911 | OPCD | RS | RA | sh | mb | XO |sh|Rc| */
914 gen_md_form (uint32_t *buf
, int opcd
, int rs
, int ra
, int sh
, int mb
,
918 unsigned int n
= ((mb
& 0x1f) << 1) | ((mb
>> 5) & 0x1);
919 unsigned int sh0_4
= sh
& 0x1f;
920 unsigned int sh5
= (sh
>> 5) & 1;
922 gdb_assert ((opcd
& ~0x3f) == 0);
923 gdb_assert ((rs
& ~0x1f) == 0);
924 gdb_assert ((ra
& ~0x1f) == 0);
925 gdb_assert ((sh
& ~0x3f) == 0);
926 gdb_assert ((mb
& ~0x3f) == 0);
927 gdb_assert ((xo
& ~0x7) == 0);
928 gdb_assert ((rc
& ~0x1) == 0);
930 insn
= (rs
<< 21) | (ra
<< 16) | (sh0_4
<< 11) | (n
<< 5)
931 | (sh5
<< 1) | (xo
<< 2) | (rc
& 1);
932 *buf
= (opcd
<< 26) | insn
;
936 /* The following are frequently used md-form instructions. */
938 #define GEN_RLDICL(buf, ra, rs ,sh, mb) \
939 gen_md_form (buf, 30, rs, ra, sh, mb, 0, 0)
940 #define GEN_RLDICR(buf, ra, rs ,sh, mb) \
941 gen_md_form (buf, 30, rs, ra, sh, mb, 1, 0)
943 /* Generate a i-form instruction in BUF and return the number of bytes written.
946 | OPCD | LI |AA|LK| */
949 gen_i_form (uint32_t *buf
, int opcd
, int li
, int aa
, int lk
)
953 gdb_assert ((opcd
& ~0x3f) == 0);
955 insn
= (li
& 0x3fffffc) | (aa
& 1) | (lk
& 1);
956 *buf
= (opcd
<< 26) | insn
;
960 /* The following are frequently used i-form instructions. */
962 #define GEN_B(buf, li) gen_i_form (buf, 18, li, 0, 0)
963 #define GEN_BL(buf, li) gen_i_form (buf, 18, li, 0, 1)
965 /* Generate a b-form instruction in BUF and return the number of bytes written.
968 | OPCD | BO | BI | BD |AA|LK| */
971 gen_b_form (uint32_t *buf
, int opcd
, int bo
, int bi
, int bd
,
976 gdb_assert ((opcd
& ~0x3f) == 0);
977 gdb_assert ((bo
& ~0x1f) == 0);
978 gdb_assert ((bi
& ~0x1f) == 0);
980 insn
= (bo
<< 21) | (bi
<< 16) | (bd
& 0xfffc) | (aa
& 1) | (lk
& 1);
981 *buf
= (opcd
<< 26) | insn
;
985 /* The following are frequently used b-form instructions. */
986 /* Assume bi = cr7. */
987 #define GEN_BNE(buf, bd) gen_b_form (buf, 16, 0x4, (7 << 2) | 2, bd, 0 ,0)
989 /* GEN_LOAD and GEN_STORE generate 64- or 32-bit load/store for ppc64 or ppc32
990 respectively. They are primary used for save/restore GPRs in jump-pad,
991 not used for bytecode compiling. */
994 #define GEN_LOAD(buf, rt, ra, si, is_64) (is_64 ? \
995 GEN_LD (buf, rt, ra, si) : \
996 GEN_LWZ (buf, rt, ra, si))
997 #define GEN_STORE(buf, rt, ra, si, is_64) (is_64 ? \
998 GEN_STD (buf, rt, ra, si) : \
999 GEN_STW (buf, rt, ra, si))
1001 #define GEN_LOAD(buf, rt, ra, si, is_64) GEN_LWZ (buf, rt, ra, si)
1002 #define GEN_STORE(buf, rt, ra, si, is_64) GEN_STW (buf, rt, ra, si)
1005 /* Generate a sequence of instructions to load IMM in the register REG.
1006 Write the instructions in BUF and return the number of bytes written. */
1009 gen_limm (uint32_t *buf
, int reg
, uint64_t imm
, int is_64
)
1013 if ((imm
+ 32768) < 65536)
1015 /* li reg, imm[15:0] */
1016 p
+= GEN_LI (p
, reg
, imm
);
1018 else if ((imm
>> 32) == 0)
1020 /* lis reg, imm[31:16]
1021 ori reg, reg, imm[15:0]
1022 rldicl reg, reg, 0, 32 */
1023 p
+= GEN_LIS (p
, reg
, (imm
>> 16) & 0xffff);
1024 if ((imm
& 0xffff) != 0)
1025 p
+= GEN_ORI (p
, reg
, reg
, imm
& 0xffff);
1026 /* Clear upper 32-bit if sign-bit is set. */
1027 if (imm
& (1u << 31) && is_64
)
1028 p
+= GEN_RLDICL (p
, reg
, reg
, 0, 32);
1033 /* lis reg, <imm[63:48]>
1034 ori reg, reg, <imm[48:32]>
1035 rldicr reg, reg, 32, 31
1036 oris reg, reg, <imm[31:16]>
1037 ori reg, reg, <imm[15:0]> */
1038 p
+= GEN_LIS (p
, reg
, ((imm
>> 48) & 0xffff));
1039 if (((imm
>> 32) & 0xffff) != 0)
1040 p
+= GEN_ORI (p
, reg
, reg
, ((imm
>> 32) & 0xffff));
1041 p
+= GEN_RLDICR (p
, reg
, reg
, 32, 31);
1042 if (((imm
>> 16) & 0xffff) != 0)
1043 p
+= GEN_ORIS (p
, reg
, reg
, ((imm
>> 16) & 0xffff));
1044 if ((imm
& 0xffff) != 0)
1045 p
+= GEN_ORI (p
, reg
, reg
, (imm
& 0xffff));
1051 /* Generate a sequence for atomically exchange at location LOCK.
1052 This code sequence clobbers r6, r7, r8. LOCK is the location for
1053 the atomic-xchg, OLD_VALUE is expected old value stored in the
1054 location, and R_NEW is a register for the new value. */
1057 gen_atomic_xchg (uint32_t *buf
, CORE_ADDR lock
, int old_value
, int r_new
,
1060 const int r_lock
= 6;
1061 const int r_old
= 7;
1062 const int r_tmp
= 8;
1066 1: lwarx TMP, 0, LOCK
1072 p
+= gen_limm (p
, r_lock
, lock
, is_64
);
1073 p
+= gen_limm (p
, r_old
, old_value
, is_64
);
1075 p
+= GEN_LWARX (p
, r_tmp
, 0, r_lock
);
1076 p
+= GEN_CMPW (p
, r_tmp
, r_old
);
1077 p
+= GEN_BNE (p
, -8);
1078 p
+= GEN_STWCX (p
, r_new
, 0, r_lock
);
1079 p
+= GEN_BNE (p
, -16);
1084 /* Generate a sequence of instructions for calling a function
1085 at address of FN. Return the number of bytes are written in BUF. */
1088 gen_call (uint32_t *buf
, CORE_ADDR fn
, int is_64
, int is_opd
)
1092 /* Must be called by r12 for caller to calculate TOC address. */
1093 p
+= gen_limm (p
, 12, fn
, is_64
);
1096 p
+= GEN_LOAD (p
, 11, 12, 16, is_64
);
1097 p
+= GEN_LOAD (p
, 2, 12, 8, is_64
);
1098 p
+= GEN_LOAD (p
, 12, 12, 0, is_64
);
1100 p
+= GEN_MTSPR (p
, 12, 9); /* mtctr r12 */
1101 *p
++ = 0x4e800421; /* bctrl */
1106 /* Copy the instruction from OLDLOC to *TO, and update *TO to *TO + size
1107 of instruction. This function is used to adjust pc-relative instructions
1111 ppc_relocate_instruction (CORE_ADDR
*to
, CORE_ADDR oldloc
)
1116 read_inferior_memory (oldloc
, (unsigned char *) &insn
, 4);
1117 op6
= PPC_OP6 (insn
);
1119 if (op6
== 18 && (insn
& 2) == 0)
1121 /* branch && AA = 0 */
1122 rel
= PPC_LI (insn
);
1123 newrel
= (oldloc
- *to
) + rel
;
1125 /* Out of range. Cannot relocate instruction. */
1126 if (newrel
>= (1 << 25) || newrel
< -(1 << 25))
1129 insn
= (insn
& ~0x3fffffc) | (newrel
& 0x3fffffc);
1131 else if (op6
== 16 && (insn
& 2) == 0)
1133 /* conditional branch && AA = 0 */
1135 /* If the new relocation is too big for even a 26-bit unconditional
1136 branch, there is nothing we can do. Just abort.
1138 Otherwise, if it can be fit in 16-bit conditional branch, just
1139 copy the instruction and relocate the address.
1141 If the it's big for conditional-branch (16-bit), try to invert the
1142 condition and jump with 26-bit branch. For example,
1153 After this transform, we are actually jump from *TO+4 instead of *TO,
1154 so check the relocation again because it will be 1-insn farther then
1155 before if *TO is after OLDLOC.
1158 For BDNZT (or so) is transformed from
1170 See also "BO field encodings". */
1172 rel
= PPC_BD (insn
);
1173 newrel
= (oldloc
- *to
) + rel
;
1175 if (newrel
< (1 << 15) && newrel
>= -(1 << 15))
1176 insn
= (insn
& ~0xfffc) | (newrel
& 0xfffc);
1177 else if ((PPC_BO (insn
) & 0x14) == 0x4 || (PPC_BO (insn
) & 0x14) == 0x10)
1181 /* Out of range. Cannot relocate instruction. */
1182 if (newrel
>= (1 << 25) || newrel
< -(1 << 25))
1185 if ((PPC_BO (insn
) & 0x14) == 0x4)
1187 else if ((PPC_BO (insn
) & 0x14) == 0x10)
1190 /* Jump over the unconditional branch. */
1191 insn
= (insn
& ~0xfffc) | 0x8;
1192 write_inferior_memory (*to
, (unsigned char *) &insn
, 4);
1195 /* Build a unconditional branch and copy LK bit. */
1196 insn
= (18 << 26) | (0x3fffffc & newrel
) | (insn
& 0x3);
1197 write_inferior_memory (*to
, (unsigned char *) &insn
, 4);
1202 else if ((PPC_BO (insn
) & 0x14) == 0)
1204 uint32_t bdnz_insn
= (16 << 26) | (0x10 << 21) | 12;
1205 uint32_t bf_insn
= (16 << 26) | (0x4 << 21) | 8;
1209 /* Out of range. Cannot relocate instruction. */
1210 if (newrel
>= (1 << 25) || newrel
< -(1 << 25))
1213 /* Copy BI field. */
1214 bf_insn
|= (insn
& 0x1f0000);
1216 /* Invert condition. */
1217 bdnz_insn
|= (insn
^ (1 << 22)) & (1 << 22);
1218 bf_insn
|= (insn
^ (1 << 24)) & (1 << 24);
1220 write_inferior_memory (*to
, (unsigned char *) &bdnz_insn
, 4);
1222 write_inferior_memory (*to
, (unsigned char *) &bf_insn
, 4);
1225 /* Build a unconditional branch and copy LK bit. */
1226 insn
= (18 << 26) | (0x3fffffc & newrel
) | (insn
& 0x3);
1227 write_inferior_memory (*to
, (unsigned char *) &insn
, 4);
1232 else /* (BO & 0x14) == 0x14, branch always. */
1234 /* Out of range. Cannot relocate instruction. */
1235 if (newrel
>= (1 << 25) || newrel
< -(1 << 25))
1238 /* Build a unconditional branch and copy LK bit. */
1239 insn
= (18 << 26) | (0x3fffffc & newrel
) | (insn
& 0x3);
1240 write_inferior_memory (*to
, (unsigned char *) &insn
, 4);
1247 write_inferior_memory (*to
, (unsigned char *) &insn
, 4);
1251 /* Implement install_fast_tracepoint_jump_pad of target_ops.
1252 See target.h for details. */
1255 ppc_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
1256 CORE_ADDR collector
,
1259 CORE_ADDR
*jump_entry
,
1260 CORE_ADDR
*trampoline
,
1261 ULONGEST
*trampoline_size
,
1262 unsigned char *jjump_pad_insn
,
1263 ULONGEST
*jjump_pad_insn_size
,
1264 CORE_ADDR
*adjusted_insn_addr
,
1265 CORE_ADDR
*adjusted_insn_addr_end
,
1271 CORE_ADDR buildaddr
= *jump_entry
;
1272 const CORE_ADDR entryaddr
= *jump_entry
;
1273 int rsz
, min_frame
, frame_size
, tp_reg
;
1274 #ifdef __powerpc64__
1275 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
1276 int is_64
= register_size (regcache
->tdesc
, 0) == 8;
1277 int is_opd
= is_64
&& !is_elfv2_inferior ();
1279 int is_64
= 0, is_opd
= 0;
1282 #ifdef __powerpc64__
1285 /* Minimum frame size is 32 bytes for ELFv2, and 112 bytes for ELFv1. */
1288 frame_size
= (40 * rsz
) + min_frame
;
1296 frame_size
= (40 * rsz
) + min_frame
;
1298 #ifdef __powerpc64__
1302 /* Stack frame layout for this jump pad,
1304 High thread_area (r13/r2) |
1305 tpoint - collecting_t obj
1315 R0 - collected registers
1321 The code flow of this jump pad,
1326 4. Call gdb_collector
1327 5. Restore GPR and SPR
1329 7. Build a jump for back to the program
1330 8. Copy/relocate original instruction
1331 9. Build a jump for replacing orignal instruction. */
1333 /* Adjust stack pointer. */
1335 p
+= GEN_STDU (p
, 1, 1, -frame_size
); /* stdu r1,-frame_size(r1) */
1337 p
+= GEN_STWU (p
, 1, 1, -frame_size
); /* stwu r1,-frame_size(r1) */
1339 /* Store GPRs. Save R1 later, because it had just been modified, but
1340 we want the original value. */
1341 for (j
= 2; j
< 32; j
++)
1342 p
+= GEN_STORE (p
, j
, 1, min_frame
+ j
* rsz
, is_64
);
1343 p
+= GEN_STORE (p
, 0, 1, min_frame
+ 0 * rsz
, is_64
);
1344 /* Set r0 to the original value of r1 before adjusting stack frame,
1345 and then save it. */
1346 p
+= GEN_ADDI (p
, 0, 1, frame_size
);
1347 p
+= GEN_STORE (p
, 0, 1, min_frame
+ 1 * rsz
, is_64
);
1349 /* Save CR, XER, LR, and CTR. */
1350 p
+= GEN_MFCR (p
, 3); /* mfcr r3 */
1351 p
+= GEN_MFSPR (p
, 4, 1); /* mfxer r4 */
1352 p
+= GEN_MFSPR (p
, 5, 8); /* mflr r5 */
1353 p
+= GEN_MFSPR (p
, 6, 9); /* mfctr r6 */
1354 p
+= GEN_STORE (p
, 3, 1, min_frame
+ 32 * rsz
, is_64
);/* std r3, 32(r1) */
1355 p
+= GEN_STORE (p
, 4, 1, min_frame
+ 33 * rsz
, is_64
);/* std r4, 33(r1) */
1356 p
+= GEN_STORE (p
, 5, 1, min_frame
+ 34 * rsz
, is_64
);/* std r5, 34(r1) */
1357 p
+= GEN_STORE (p
, 6, 1, min_frame
+ 35 * rsz
, is_64
);/* std r6, 35(r1) */
1359 /* Save PC<tpaddr> */
1360 p
+= gen_limm (p
, 3, tpaddr
, is_64
);
1361 p
+= GEN_STORE (p
, 3, 1, min_frame
+ 36 * rsz
, is_64
);
1364 /* Setup arguments to collector. */
1365 /* Set r4 to collected registers. */
1366 p
+= GEN_ADDI (p
, 4, 1, min_frame
);
1367 /* Set r3 to TPOINT. */
1368 p
+= gen_limm (p
, 3, tpoint
, is_64
);
1370 /* Prepare collecting_t object for lock. */
1371 p
+= GEN_STORE (p
, 3, 1, min_frame
+ 37 * rsz
, is_64
);
1372 p
+= GEN_STORE (p
, tp_reg
, 1, min_frame
+ 38 * rsz
, is_64
);
1373 /* Set R5 to collecting object. */
1374 p
+= GEN_ADDI (p
, 5, 1, 37 * rsz
);
1376 p
+= GEN_LWSYNC (p
);
1377 p
+= gen_atomic_xchg (p
, lockaddr
, 0, 5, is_64
);
1378 p
+= GEN_LWSYNC (p
);
1380 /* Call to collector. */
1381 p
+= gen_call (p
, collector
, is_64
, is_opd
);
1383 /* Simply write 0 to release the lock. */
1384 p
+= gen_limm (p
, 3, lockaddr
, is_64
);
1385 p
+= gen_limm (p
, 4, 0, is_64
);
1386 p
+= GEN_LWSYNC (p
);
1387 p
+= GEN_STORE (p
, 4, 3, 0, is_64
);
1389 /* Restore stack and registers. */
1390 p
+= GEN_LOAD (p
, 3, 1, min_frame
+ 32 * rsz
, is_64
); /* ld r3, 32(r1) */
1391 p
+= GEN_LOAD (p
, 4, 1, min_frame
+ 33 * rsz
, is_64
); /* ld r4, 33(r1) */
1392 p
+= GEN_LOAD (p
, 5, 1, min_frame
+ 34 * rsz
, is_64
); /* ld r5, 34(r1) */
1393 p
+= GEN_LOAD (p
, 6, 1, min_frame
+ 35 * rsz
, is_64
); /* ld r6, 35(r1) */
1394 p
+= GEN_MTCR (p
, 3); /* mtcr r3 */
1395 p
+= GEN_MTSPR (p
, 4, 1); /* mtxer r4 */
1396 p
+= GEN_MTSPR (p
, 5, 8); /* mtlr r5 */
1397 p
+= GEN_MTSPR (p
, 6, 9); /* mtctr r6 */
1400 for (j
= 2; j
< 32; j
++)
1401 p
+= GEN_LOAD (p
, j
, 1, min_frame
+ j
* rsz
, is_64
);
1402 p
+= GEN_LOAD (p
, 0, 1, min_frame
+ 0 * rsz
, is_64
);
1404 p
+= GEN_ADDI (p
, 1, 1, frame_size
);
1406 /* Flush instructions to inferior memory. */
1407 write_inferior_memory (buildaddr
, (unsigned char *) buf
, (p
- buf
) * 4);
1409 /* Now, insert the original instruction to execute in the jump pad. */
1410 *adjusted_insn_addr
= buildaddr
+ (p
- buf
) * 4;
1411 *adjusted_insn_addr_end
= *adjusted_insn_addr
;
1412 ppc_relocate_instruction (adjusted_insn_addr_end
, tpaddr
);
1414 /* Verify the relocation size. If should be 4 for normal copy,
1415 8 or 12 for some conditional branch. */
1416 if ((*adjusted_insn_addr_end
- *adjusted_insn_addr
== 0)
1417 || (*adjusted_insn_addr_end
- *adjusted_insn_addr
> 12))
1419 sprintf (err
, "E.Unexpected instruction length = %d"
1420 "when relocate instruction.",
1421 (int) (*adjusted_insn_addr_end
- *adjusted_insn_addr
));
1425 buildaddr
= *adjusted_insn_addr_end
;
1427 /* Finally, write a jump back to the program. */
1428 offset
= (tpaddr
+ 4) - buildaddr
;
1429 if (offset
>= (1 << 25) || offset
< -(1 << 25))
1431 sprintf (err
, "E.Jump back from jump pad too far from tracepoint "
1432 "(offset 0x%x > 26-bit).", offset
);
1436 p
+= GEN_B (p
, offset
);
1437 write_inferior_memory (buildaddr
, (unsigned char *) buf
, (p
- buf
) * 4);
1438 *jump_entry
= buildaddr
+ (p
- buf
) * 4;
1440 /* The jump pad is now built. Wire in a jump to our jump pad. This
1441 is always done last (by our caller actually), so that we can
1442 install fast tracepoints with threads running. This relies on
1443 the agent's atomic write support. */
1444 offset
= entryaddr
- tpaddr
;
1445 if (offset
>= (1 << 25) || offset
< -(1 << 25))
1447 sprintf (err
, "E.Jump back from jump pad too far from tracepoint "
1448 "(offset 0x%x > 26-bit).", offset
);
1452 GEN_B ((uint32_t *) jjump_pad_insn
, offset
);
1453 *jjump_pad_insn_size
= 4;
1458 /* Returns the minimum instruction length for installing a tracepoint. */
1461 ppc_get_min_fast_tracepoint_insn_len (void)
1466 /* Emits a given buffer into the target at current_insn_ptr. Length
1467 is in units of 32-bit words. */
1470 emit_insns (uint32_t *buf
, int n
)
1472 n
= n
* sizeof (uint32_t);
1473 write_inferior_memory (current_insn_ptr
, (unsigned char *) buf
, n
);
1474 current_insn_ptr
+= n
;
1477 #define __EMIT_ASM(NAME, INSNS) \
1480 extern uint32_t start_bcax_ ## NAME []; \
1481 extern uint32_t end_bcax_ ## NAME []; \
1482 emit_insns (start_bcax_ ## NAME, \
1483 end_bcax_ ## NAME - start_bcax_ ## NAME); \
1484 __asm__ (".section .text.__ppcbcax\n\t" \
1485 "start_bcax_" #NAME ":\n\t" \
1487 "end_bcax_" #NAME ":\n\t" \
1491 #define _EMIT_ASM(NAME, INSNS) __EMIT_ASM (NAME, INSNS)
1492 #define EMIT_ASM(INSNS) _EMIT_ASM (__LINE__, INSNS)
1496 Bytecode execution stack frame - 32-bit
1498 | LR save area (SP + 4)
1499 SP' -> +- Back chain (SP + 0)
1500 | Save r31 for access saved arguments
1501 | Save r30 for bytecode stack pointer
1502 | Save r4 for incoming argument *value
1503 | Save r3 for incoming argument regs
1504 r30 -> +- Bytecode execution stack
1506 | 64-byte (8 doublewords) at initial.
1507 | Expand stack as needed.
1510 | Some padding for minimum stack frame and 16-byte alignment.
1512 SP +- Back-chain (SP')
1518 r30 is the stack-pointer for bytecode machine.
1519 It should point to next-empty, so we can use LDU for pop.
1520 r3 is used for cache of the high part of TOP value.
1521 It was the first argument, pointer to regs.
1522 r4 is used for cache of the low part of TOP value.
1523 It was the second argument, pointer to the result.
1524 We should set *result = TOP after leaving this function.
1527 * To restore stack at epilogue
1529 * To check stack is big enough for bytecode execution.
1531 * To return execution result.
1536 /* Regardless of endian, register 3 is always high part, 4 is low part.
1537 These defines are used when the register pair is stored/loaded.
1538 Likewise, to simplify code, have a similiar define for 5:6. */
1540 #if __BYTE_ORDER == __LITTLE_ENDIAN
1541 #define TOP_FIRST "4"
1542 #define TOP_SECOND "3"
1543 #define TMP_FIRST "6"
1544 #define TMP_SECOND "5"
1546 #define TOP_FIRST "3"
1547 #define TOP_SECOND "4"
1548 #define TMP_FIRST "5"
1549 #define TMP_SECOND "6"
1552 /* Emit prologue in inferior memory. See above comments. */
1555 ppc_emit_prologue (void)
1557 EMIT_ASM (/* Save return address. */
1560 /* Adjust SP. 96 is the initial frame size. */
1562 /* Save r30 and incoming arguments. */
1563 "stw 31, 96-4(1) \n"
1564 "stw 30, 96-8(1) \n"
1565 "stw 4, 96-12(1) \n"
1566 "stw 3, 96-16(1) \n"
1567 /* Point r31 to original r1 for access arguments. */
1569 /* Set r30 to pointing stack-top. */
1571 /* Initial r3/TOP to 0. */
1576 /* Emit epilogue in inferior memory. See above comments. */
1579 ppc_emit_epilogue (void)
1581 EMIT_ASM (/* *result = TOP */
1583 "stw " TOP_FIRST
", 0(5) \n"
1584 "stw " TOP_SECOND
", 4(5) \n"
1585 /* Restore registers. */
1592 /* Return 0 for no-error. */
1598 /* TOP = stack[--sp] + TOP */
1603 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1604 "lwz " TMP_SECOND
", 4(30)\n"
1609 /* TOP = stack[--sp] - TOP */
1614 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1615 "lwz " TMP_SECOND
", 4(30) \n"
1617 "subfe 3, 3, 5 \n");
1620 /* TOP = stack[--sp] * TOP */
1625 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1626 "lwz " TMP_SECOND
", 4(30) \n"
1635 /* TOP = stack[--sp] << TOP */
1640 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1641 "lwz " TMP_SECOND
", 4(30) \n"
1642 "subfic 3, 4, 32\n" /* r3 = 32 - TOP */
1643 "addi 7, 4, -32\n" /* r7 = TOP - 32 */
1644 "slw 5, 5, 4\n" /* Shift high part left */
1645 "slw 4, 6, 4\n" /* Shift low part left */
1646 "srw 3, 6, 3\n" /* Shift low to high if shift < 32 */
1647 "slw 7, 6, 7\n" /* Shift low to high if shift >= 32 */
1649 "or 3, 7, 3\n"); /* Assemble high part */
1652 /* Top = stack[--sp] >> TOP
1653 (Arithmetic shift right) */
1656 ppc_emit_rsh_signed (void)
1658 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1659 "lwz " TMP_SECOND
", 4(30) \n"
1660 "addi 7, 4, -32\n" /* r7 = TOP - 32 */
1661 "sraw 3, 5, 4\n" /* Shift high part right */
1663 "blt 0, 1f\n" /* If shift <= 32, goto 1: */
1664 "sraw 4, 5, 7\n" /* Shift high to low */
1667 "subfic 7, 4, 32\n" /* r7 = 32 - TOP */
1668 "srw 4, 6, 4\n" /* Shift low part right */
1669 "slw 5, 5, 7\n" /* Shift high to low */
1670 "or 4, 4, 5\n" /* Assemble low part */
1674 /* Top = stack[--sp] >> TOP
1675 (Logical shift right) */
1678 ppc_emit_rsh_unsigned (void)
1680 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1681 "lwz " TMP_SECOND
", 4(30) \n"
1682 "subfic 3, 4, 32\n" /* r3 = 32 - TOP */
1683 "addi 7, 4, -32\n" /* r7 = TOP - 32 */
1684 "srw 6, 6, 4\n" /* Shift low part right */
1685 "slw 3, 5, 3\n" /* Shift high to low if shift < 32 */
1686 "srw 7, 5, 7\n" /* Shift high to low if shift >= 32 */
1688 "srw 3, 5, 4\n" /* Shift high part right */
1689 "or 4, 6, 7\n"); /* Assemble low part */
1692 /* Emit code for signed-extension specified by ARG. */
1695 ppc_emit_ext (int arg
)
1700 EMIT_ASM ("extsb 4, 4\n"
1704 EMIT_ASM ("extsh 4, 4\n"
1708 EMIT_ASM ("srawi 3, 4, 31");
1715 /* Emit code for zero-extension specified by ARG. */
1718 ppc_emit_zero_ext (int arg
)
1723 EMIT_ASM ("clrlwi 4,4,24\n"
1727 EMIT_ASM ("clrlwi 4,4,16\n"
1731 EMIT_ASM ("li 3, 0");
1739 i.e., TOP = (TOP == 0) ? 1 : 0; */
1742 ppc_emit_log_not (void)
1744 EMIT_ASM ("or 4, 3, 4 \n"
1750 /* TOP = stack[--sp] & TOP */
1753 ppc_emit_bit_and (void)
1755 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1756 "lwz " TMP_SECOND
", 4(30) \n"
1761 /* TOP = stack[--sp] | TOP */
1764 ppc_emit_bit_or (void)
1766 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1767 "lwz " TMP_SECOND
", 4(30) \n"
1772 /* TOP = stack[--sp] ^ TOP */
1775 ppc_emit_bit_xor (void)
1777 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1778 "lwz " TMP_SECOND
", 4(30) \n"
1784 i.e., TOP = ~(TOP | TOP) */
1787 ppc_emit_bit_not (void)
1789 EMIT_ASM ("nor 3, 3, 3 \n"
1793 /* TOP = stack[--sp] == TOP */
1796 ppc_emit_equal (void)
1798 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1799 "lwz " TMP_SECOND
", 4(30) \n"
1808 /* TOP = stack[--sp] < TOP
1809 (Signed comparison) */
1812 ppc_emit_less_signed (void)
1814 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1815 "lwz " TMP_SECOND
", 4(30) \n"
1818 /* CR6 bit 0 = low less and high equal */
1819 "crand 6*4+0, 6*4+0, 7*4+2\n"
1820 /* CR7 bit 0 = (low less and high equal) or high less */
1821 "cror 7*4+0, 7*4+0, 6*4+0\n"
1823 "rlwinm 4, 4, 29, 31, 31 \n"
1827 /* TOP = stack[--sp] < TOP
1828 (Unsigned comparison) */
1831 ppc_emit_less_unsigned (void)
1833 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
1834 "lwz " TMP_SECOND
", 4(30) \n"
1837 /* CR6 bit 0 = low less and high equal */
1838 "crand 6*4+0, 6*4+0, 7*4+2\n"
1839 /* CR7 bit 0 = (low less and high equal) or high less */
1840 "cror 7*4+0, 7*4+0, 6*4+0\n"
1842 "rlwinm 4, 4, 29, 31, 31 \n"
1846 /* Access the memory address in TOP in size of SIZE.
1847 Zero-extend the read value. */
1850 ppc_emit_ref (int size
)
1855 EMIT_ASM ("lbz 4, 0(4)\n"
1859 EMIT_ASM ("lhz 4, 0(4)\n"
1863 EMIT_ASM ("lwz 4, 0(4)\n"
1867 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
1868 EMIT_ASM ("lwz 3, 4(4)\n"
1871 EMIT_ASM ("lwz 3, 0(4)\n"
1880 ppc_emit_const (LONGEST num
)
1885 p
+= gen_limm (p
, 3, num
>> 32 & 0xffffffff, 0);
1886 p
+= gen_limm (p
, 4, num
& 0xffffffff, 0);
1888 emit_insns (buf
, p
- buf
);
1889 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
1892 /* Set TOP to the value of register REG by calling get_raw_reg function
1893 with two argument, collected buffer and register number. */
1896 ppc_emit_reg (int reg
)
1901 /* fctx->regs is passed in r3 and then saved in -16(31). */
1902 p
+= GEN_LWZ (p
, 3, 31, -16);
1903 p
+= GEN_LI (p
, 4, reg
); /* li r4, reg */
1904 p
+= gen_call (p
, get_raw_reg_func_addr (), 0, 0);
1906 emit_insns (buf
, p
- buf
);
1907 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
1909 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
1911 EMIT_ASM ("mr 5, 4\n"
1917 /* TOP = stack[--sp] */
1922 EMIT_ASM ("lwzu " TOP_FIRST
", 8(30) \n"
1923 "lwz " TOP_SECOND
", 4(30) \n");
1926 /* stack[sp++] = TOP
1928 Because we may use up bytecode stack, expand 8 doublewords more
1932 ppc_emit_stack_flush (void)
1934 /* Make sure bytecode stack is big enough before push.
1935 Otherwise, expand 64-byte more. */
1937 EMIT_ASM (" stw " TOP_FIRST
", 0(30) \n"
1938 " stw " TOP_SECOND
", 4(30)\n"
1939 " addi 5, 30, -(8 + 8) \n"
1942 " stwu 31, -64(1) \n"
1943 "1:addi 30, 30, -8 \n");
1946 /* Swap TOP and stack[sp-1] */
1949 ppc_emit_swap (void)
1951 EMIT_ASM ("lwz " TMP_FIRST
", 8(30) \n"
1952 "lwz " TMP_SECOND
", 12(30) \n"
1953 "stw " TOP_FIRST
", 8(30) \n"
1954 "stw " TOP_SECOND
", 12(30) \n"
1959 /* Discard N elements in the stack. Also used for ppc64. */
1962 ppc_emit_stack_adjust (int n
)
1974 p
+= GEN_ADDI (p
, 30, 30, n
);
1976 emit_insns (buf
, p
- buf
);
1977 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
1980 /* Call function FN. */
1983 ppc_emit_call (CORE_ADDR fn
)
1988 p
+= gen_call (p
, fn
, 0, 0);
1990 emit_insns (buf
, p
- buf
);
1991 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
1994 /* FN's prototype is `LONGEST(*fn)(int)'.
1999 ppc_emit_int_call_1 (CORE_ADDR fn
, int arg1
)
2004 /* Setup argument. arg1 is a 16-bit value. */
2005 p
+= gen_limm (p
, 3, (uint32_t) arg1
, 0);
2006 p
+= gen_call (p
, fn
, 0, 0);
2008 emit_insns (buf
, p
- buf
);
2009 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2011 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
2013 EMIT_ASM ("mr 5, 4\n"
2019 /* FN's prototype is `void(*fn)(int,LONGEST)'.
2022 TOP should be preserved/restored before/after the call. */
2025 ppc_emit_void_call_2 (CORE_ADDR fn
, int arg1
)
2030 /* Save TOP. 0(30) is next-empty. */
2031 p
+= GEN_STW (p
, 3, 30, 0);
2032 p
+= GEN_STW (p
, 4, 30, 4);
2034 /* Setup argument. arg1 is a 16-bit value. */
2035 if (__BYTE_ORDER
== __LITTLE_ENDIAN
)
2037 p
+= GEN_MR (p
, 5, 4);
2038 p
+= GEN_MR (p
, 6, 3);
2042 p
+= GEN_MR (p
, 5, 3);
2043 p
+= GEN_MR (p
, 6, 4);
2045 p
+= gen_limm (p
, 3, (uint32_t) arg1
, 0);
2046 p
+= gen_call (p
, fn
, 0, 0);
2049 p
+= GEN_LWZ (p
, 3, 30, 0);
2050 p
+= GEN_LWZ (p
, 4, 30, 4);
2052 emit_insns (buf
, p
- buf
);
2053 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2056 /* Note in the following goto ops:
2058 When emitting goto, the target address is later relocated by
2059 write_goto_address. OFFSET_P is the offset of the branch instruction
2060 in the code sequence, and SIZE_P is how to relocate the instruction,
2061 recognized by ppc_write_goto_address. In current implementation,
2062 SIZE can be either 24 or 14 for branch of conditional-branch instruction.
2065 /* If TOP is true, goto somewhere. Otherwise, just fall-through. */
2068 ppc_emit_if_goto (int *offset_p
, int *size_p
)
2070 EMIT_ASM ("or. 3, 3, 4 \n"
2071 "lwzu " TOP_FIRST
", 8(30) \n"
2072 "lwz " TOP_SECOND
", 4(30) \n"
2081 /* Unconditional goto. Also used for ppc64. */
2084 ppc_emit_goto (int *offset_p
, int *size_p
)
2086 EMIT_ASM ("1:b 1b");
2094 /* Goto if stack[--sp] == TOP */
2097 ppc_emit_eq_goto (int *offset_p
, int *size_p
)
2099 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
2100 "lwz " TMP_SECOND
", 4(30) \n"
2104 "lwzu " TOP_FIRST
", 8(30) \n"
2105 "lwz " TOP_SECOND
", 4(30) \n"
2114 /* Goto if stack[--sp] != TOP */
2117 ppc_emit_ne_goto (int *offset_p
, int *size_p
)
2119 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
2120 "lwz " TMP_SECOND
", 4(30) \n"
2124 "lwzu " TOP_FIRST
", 8(30) \n"
2125 "lwz " TOP_SECOND
", 4(30) \n"
2134 /* Goto if stack[--sp] < TOP */
2137 ppc_emit_lt_goto (int *offset_p
, int *size_p
)
2139 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
2140 "lwz " TMP_SECOND
", 4(30) \n"
2143 /* CR6 bit 0 = low less and high equal */
2144 "crand 6*4+0, 6*4+0, 7*4+2\n"
2145 /* CR7 bit 0 = (low less and high equal) or high less */
2146 "cror 7*4+0, 7*4+0, 6*4+0\n"
2147 "lwzu " TOP_FIRST
", 8(30) \n"
2148 "lwz " TOP_SECOND
", 4(30)\n"
2157 /* Goto if stack[--sp] <= TOP */
2160 ppc_emit_le_goto (int *offset_p
, int *size_p
)
2162 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
2163 "lwz " TMP_SECOND
", 4(30) \n"
2166 /* CR6 bit 0 = low less/equal and high equal */
2167 "crandc 6*4+0, 7*4+2, 6*4+1\n"
2168 /* CR7 bit 0 = (low less/eq and high equal) or high less */
2169 "cror 7*4+0, 7*4+0, 6*4+0\n"
2170 "lwzu " TOP_FIRST
", 8(30) \n"
2171 "lwz " TOP_SECOND
", 4(30)\n"
2180 /* Goto if stack[--sp] > TOP */
2183 ppc_emit_gt_goto (int *offset_p
, int *size_p
)
2185 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
2186 "lwz " TMP_SECOND
", 4(30) \n"
2189 /* CR6 bit 0 = low greater and high equal */
2190 "crand 6*4+0, 6*4+1, 7*4+2\n"
2191 /* CR7 bit 0 = (low greater and high equal) or high greater */
2192 "cror 7*4+0, 7*4+1, 6*4+0\n"
2193 "lwzu " TOP_FIRST
", 8(30) \n"
2194 "lwz " TOP_SECOND
", 4(30)\n"
2203 /* Goto if stack[--sp] >= TOP */
2206 ppc_emit_ge_goto (int *offset_p
, int *size_p
)
2208 EMIT_ASM ("lwzu " TMP_FIRST
", 8(30) \n"
2209 "lwz " TMP_SECOND
", 4(30) \n"
2212 /* CR6 bit 0 = low ge and high equal */
2213 "crandc 6*4+0, 7*4+2, 6*4+0\n"
2214 /* CR7 bit 0 = (low ge and high equal) or high greater */
2215 "cror 7*4+0, 7*4+1, 6*4+0\n"
2216 "lwzu " TOP_FIRST
", 8(30)\n"
2217 "lwz " TOP_SECOND
", 4(30)\n"
2226 /* Relocate previous emitted branch instruction. FROM is the address
2227 of the branch instruction, TO is the goto target address, and SIZE
2228 if the value we set by *SIZE_P before. Currently, it is either
2229 24 or 14 of branch and conditional-branch instruction.
2230 Also used for ppc64. */
2233 ppc_write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
2235 long rel
= to
- from
;
2239 read_inferior_memory (from
, (unsigned char *) &insn
, 4);
2240 opcd
= (insn
>> 26) & 0x3f;
2246 || (rel
>= (1 << 15) || rel
< -(1 << 15)))
2248 insn
= (insn
& ~0xfffc) | (rel
& 0xfffc);
2252 || (rel
>= (1 << 25) || rel
< -(1 << 25)))
2254 insn
= (insn
& ~0x3fffffc) | (rel
& 0x3fffffc);
2261 write_inferior_memory (from
, (unsigned char *) &insn
, 4);
2264 /* Table of emit ops for 32-bit. */
2266 static struct emit_ops ppc_emit_ops_impl
=
2274 ppc_emit_rsh_signed
,
2275 ppc_emit_rsh_unsigned
,
2283 ppc_emit_less_signed
,
2284 ppc_emit_less_unsigned
,
2288 ppc_write_goto_address
,
2293 ppc_emit_stack_flush
,
2296 ppc_emit_stack_adjust
,
2297 ppc_emit_int_call_1
,
2298 ppc_emit_void_call_2
,
2307 #ifdef __powerpc64__
2311 Bytecode execution stack frame - 64-bit
2313 | LR save area (SP + 16)
2314 | CR save area (SP + 8)
2315 SP' -> +- Back chain (SP + 0)
2316 | Save r31 for access saved arguments
2317 | Save r30 for bytecode stack pointer
2318 | Save r4 for incoming argument *value
2319 | Save r3 for incoming argument regs
2320 r30 -> +- Bytecode execution stack
2322 | 64-byte (8 doublewords) at initial.
2323 | Expand stack as needed.
2326 | Some padding for minimum stack frame.
2328 SP +- Back-chain (SP')
2331 = 112 + (4 * 8) + 64
2334 r30 is the stack-pointer for bytecode machine.
2335 It should point to next-empty, so we can use LDU for pop.
2336 r3 is used for cache of TOP value.
2337 It was the first argument, pointer to regs.
2338 r4 is the second argument, pointer to the result.
2339 We should set *result = TOP after leaving this function.
2342 * To restore stack at epilogue
2344 * To check stack is big enough for bytecode execution.
2345 => r30 - 8 > SP + 112
2346 * To return execution result.
2351 /* Emit prologue in inferior memory. See above comments. */
2354 ppc64v1_emit_prologue (void)
2356 /* On ELFv1, function pointers really point to function descriptor,
2357 so emit one here. We don't care about contents of words 1 and 2,
2358 so let them just overlap out code. */
2359 uint64_t opd
= current_insn_ptr
+ 8;
2362 /* Mind the strict aliasing rules. */
2363 memcpy (buf
, &opd
, sizeof buf
);
2365 EMIT_ASM (/* Save return address. */
2368 /* Save r30 and incoming arguments. */
2373 /* Point r31 to current r1 for access arguments. */
2375 /* Adjust SP. 208 is the initial frame size. */
2376 "stdu 1, -208(1) \n"
2377 /* Set r30 to pointing stack-top. */
2378 "addi 30, 1, 168 \n"
2379 /* Initial r3/TOP to 0. */
2383 /* Emit prologue in inferior memory. See above comments. */
2386 ppc64v2_emit_prologue (void)
2388 EMIT_ASM (/* Save return address. */
2391 /* Save r30 and incoming arguments. */
2396 /* Point r31 to current r1 for access arguments. */
2398 /* Adjust SP. 208 is the initial frame size. */
2399 "stdu 1, -208(1) \n"
2400 /* Set r30 to pointing stack-top. */
2401 "addi 30, 1, 168 \n"
2402 /* Initial r3/TOP to 0. */
2406 /* Emit epilogue in inferior memory. See above comments. */
2409 ppc64_emit_epilogue (void)
2411 EMIT_ASM (/* Restore SP. */
2416 /* Restore registers. */
2421 /* Return 0 for no-error. */
2427 /* TOP = stack[--sp] + TOP */
2430 ppc64_emit_add (void)
2432 EMIT_ASM ("ldu 4, 8(30) \n"
2436 /* TOP = stack[--sp] - TOP */
2439 ppc64_emit_sub (void)
2441 EMIT_ASM ("ldu 4, 8(30) \n"
2445 /* TOP = stack[--sp] * TOP */
2448 ppc64_emit_mul (void)
2450 EMIT_ASM ("ldu 4, 8(30) \n"
2451 "mulld 3, 4, 3 \n");
2454 /* TOP = stack[--sp] << TOP */
2457 ppc64_emit_lsh (void)
2459 EMIT_ASM ("ldu 4, 8(30) \n"
2463 /* Top = stack[--sp] >> TOP
2464 (Arithmetic shift right) */
2467 ppc64_emit_rsh_signed (void)
2469 EMIT_ASM ("ldu 4, 8(30) \n"
2473 /* Top = stack[--sp] >> TOP
2474 (Logical shift right) */
2477 ppc64_emit_rsh_unsigned (void)
2479 EMIT_ASM ("ldu 4, 8(30) \n"
2483 /* Emit code for signed-extension specified by ARG. */
2486 ppc64_emit_ext (int arg
)
2491 EMIT_ASM ("extsb 3, 3");
2494 EMIT_ASM ("extsh 3, 3");
2497 EMIT_ASM ("extsw 3, 3");
2504 /* Emit code for zero-extension specified by ARG. */
2507 ppc64_emit_zero_ext (int arg
)
2512 EMIT_ASM ("rldicl 3,3,0,56");
2515 EMIT_ASM ("rldicl 3,3,0,48");
2518 EMIT_ASM ("rldicl 3,3,0,32");
2526 i.e., TOP = (TOP == 0) ? 1 : 0; */
2529 ppc64_emit_log_not (void)
2531 EMIT_ASM ("cntlzd 3, 3 \n"
2535 /* TOP = stack[--sp] & TOP */
2538 ppc64_emit_bit_and (void)
2540 EMIT_ASM ("ldu 4, 8(30) \n"
2544 /* TOP = stack[--sp] | TOP */
2547 ppc64_emit_bit_or (void)
2549 EMIT_ASM ("ldu 4, 8(30) \n"
2553 /* TOP = stack[--sp] ^ TOP */
2556 ppc64_emit_bit_xor (void)
2558 EMIT_ASM ("ldu 4, 8(30) \n"
2563 i.e., TOP = ~(TOP | TOP) */
2566 ppc64_emit_bit_not (void)
2568 EMIT_ASM ("nor 3, 3, 3 \n");
2571 /* TOP = stack[--sp] == TOP */
2574 ppc64_emit_equal (void)
2576 EMIT_ASM ("ldu 4, 8(30) \n"
2582 /* TOP = stack[--sp] < TOP
2583 (Signed comparison) */
2586 ppc64_emit_less_signed (void)
2588 EMIT_ASM ("ldu 4, 8(30) \n"
2591 "rlwinm 3, 3, 29, 31, 31 \n");
2594 /* TOP = stack[--sp] < TOP
2595 (Unsigned comparison) */
2598 ppc64_emit_less_unsigned (void)
2600 EMIT_ASM ("ldu 4, 8(30) \n"
2603 "rlwinm 3, 3, 29, 31, 31 \n");
2606 /* Access the memory address in TOP in size of SIZE.
2607 Zero-extend the read value. */
2610 ppc64_emit_ref (int size
)
2615 EMIT_ASM ("lbz 3, 0(3)");
2618 EMIT_ASM ("lhz 3, 0(3)");
2621 EMIT_ASM ("lwz 3, 0(3)");
2624 EMIT_ASM ("ld 3, 0(3)");
2632 ppc64_emit_const (LONGEST num
)
2637 p
+= gen_limm (p
, 3, num
, 1);
2639 emit_insns (buf
, p
- buf
);
2640 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2643 /* Set TOP to the value of register REG by calling get_raw_reg function
2644 with two argument, collected buffer and register number. */
2647 ppc64v1_emit_reg (int reg
)
2652 /* fctx->regs is passed in r3 and then saved in 176(1). */
2653 p
+= GEN_LD (p
, 3, 31, -32);
2654 p
+= GEN_LI (p
, 4, reg
);
2655 p
+= GEN_STD (p
, 2, 1, 40); /* Save TOC. */
2656 p
+= gen_call (p
, get_raw_reg_func_addr (), 1, 1);
2657 p
+= GEN_LD (p
, 2, 1, 40); /* Restore TOC. */
2659 emit_insns (buf
, p
- buf
);
2660 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2663 /* Likewise, for ELFv2. */
2666 ppc64v2_emit_reg (int reg
)
2671 /* fctx->regs is passed in r3 and then saved in 176(1). */
2672 p
+= GEN_LD (p
, 3, 31, -32);
2673 p
+= GEN_LI (p
, 4, reg
);
2674 p
+= GEN_STD (p
, 2, 1, 24); /* Save TOC. */
2675 p
+= gen_call (p
, get_raw_reg_func_addr (), 1, 0);
2676 p
+= GEN_LD (p
, 2, 1, 24); /* Restore TOC. */
2678 emit_insns (buf
, p
- buf
);
2679 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2682 /* TOP = stack[--sp] */
2685 ppc64_emit_pop (void)
2687 EMIT_ASM ("ldu 3, 8(30)");
2690 /* stack[sp++] = TOP
2692 Because we may use up bytecode stack, expand 8 doublewords more
2696 ppc64_emit_stack_flush (void)
2698 /* Make sure bytecode stack is big enough before push.
2699 Otherwise, expand 64-byte more. */
2701 EMIT_ASM (" std 3, 0(30) \n"
2702 " addi 4, 30, -(112 + 8) \n"
2705 " stdu 31, -64(1) \n"
2706 "1:addi 30, 30, -8 \n");
2709 /* Swap TOP and stack[sp-1] */
2712 ppc64_emit_swap (void)
2714 EMIT_ASM ("ld 4, 8(30) \n"
2719 /* Call function FN - ELFv1. */
2722 ppc64v1_emit_call (CORE_ADDR fn
)
2727 p
+= GEN_STD (p
, 2, 1, 40); /* Save TOC. */
2728 p
+= gen_call (p
, fn
, 1, 1);
2729 p
+= GEN_LD (p
, 2, 1, 40); /* Restore TOC. */
2731 emit_insns (buf
, p
- buf
);
2732 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2735 /* Call function FN - ELFv2. */
2738 ppc64v2_emit_call (CORE_ADDR fn
)
2743 p
+= GEN_STD (p
, 2, 1, 24); /* Save TOC. */
2744 p
+= gen_call (p
, fn
, 1, 0);
2745 p
+= GEN_LD (p
, 2, 1, 24); /* Restore TOC. */
2747 emit_insns (buf
, p
- buf
);
2748 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2751 /* FN's prototype is `LONGEST(*fn)(int)'.
2756 ppc64v1_emit_int_call_1 (CORE_ADDR fn
, int arg1
)
2761 /* Setup argument. arg1 is a 16-bit value. */
2762 p
+= gen_limm (p
, 3, arg1
, 1);
2763 p
+= GEN_STD (p
, 2, 1, 40); /* Save TOC. */
2764 p
+= gen_call (p
, fn
, 1, 1);
2765 p
+= GEN_LD (p
, 2, 1, 40); /* Restore TOC. */
2767 emit_insns (buf
, p
- buf
);
2768 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2771 /* Likewise for ELFv2. */
2774 ppc64v2_emit_int_call_1 (CORE_ADDR fn
, int arg1
)
2779 /* Setup argument. arg1 is a 16-bit value. */
2780 p
+= gen_limm (p
, 3, arg1
, 1);
2781 p
+= GEN_STD (p
, 2, 1, 24); /* Save TOC. */
2782 p
+= gen_call (p
, fn
, 1, 0);
2783 p
+= GEN_LD (p
, 2, 1, 24); /* Restore TOC. */
2785 emit_insns (buf
, p
- buf
);
2786 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2789 /* FN's prototype is `void(*fn)(int,LONGEST)'.
2792 TOP should be preserved/restored before/after the call. */
2795 ppc64v1_emit_void_call_2 (CORE_ADDR fn
, int arg1
)
2800 /* Save TOP. 0(30) is next-empty. */
2801 p
+= GEN_STD (p
, 3, 30, 0);
2803 /* Setup argument. arg1 is a 16-bit value. */
2804 p
+= GEN_MR (p
, 4, 3); /* mr r4, r3 */
2805 p
+= gen_limm (p
, 3, arg1
, 1);
2806 p
+= GEN_STD (p
, 2, 1, 40); /* Save TOC. */
2807 p
+= gen_call (p
, fn
, 1, 1);
2808 p
+= GEN_LD (p
, 2, 1, 40); /* Restore TOC. */
2811 p
+= GEN_LD (p
, 3, 30, 0);
2813 emit_insns (buf
, p
- buf
);
2814 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2817 /* Likewise for ELFv2. */
2820 ppc64v2_emit_void_call_2 (CORE_ADDR fn
, int arg1
)
2825 /* Save TOP. 0(30) is next-empty. */
2826 p
+= GEN_STD (p
, 3, 30, 0);
2828 /* Setup argument. arg1 is a 16-bit value. */
2829 p
+= GEN_MR (p
, 4, 3); /* mr r4, r3 */
2830 p
+= gen_limm (p
, 3, arg1
, 1);
2831 p
+= GEN_STD (p
, 2, 1, 24); /* Save TOC. */
2832 p
+= gen_call (p
, fn
, 1, 0);
2833 p
+= GEN_LD (p
, 2, 1, 24); /* Restore TOC. */
2836 p
+= GEN_LD (p
, 3, 30, 0);
2838 emit_insns (buf
, p
- buf
);
2839 gdb_assert ((p
- buf
) <= (sizeof (buf
) / sizeof (*buf
)));
2842 /* If TOP is true, goto somewhere. Otherwise, just fall-through. */
2845 ppc64_emit_if_goto (int *offset_p
, int *size_p
)
2847 EMIT_ASM ("cmpdi 7, 3, 0 \n"
2857 /* Goto if stack[--sp] == TOP */
2860 ppc64_emit_eq_goto (int *offset_p
, int *size_p
)
2862 EMIT_ASM ("ldu 4, 8(30) \n"
2873 /* Goto if stack[--sp] != TOP */
2876 ppc64_emit_ne_goto (int *offset_p
, int *size_p
)
2878 EMIT_ASM ("ldu 4, 8(30) \n"
2889 /* Goto if stack[--sp] < TOP */
2892 ppc64_emit_lt_goto (int *offset_p
, int *size_p
)
2894 EMIT_ASM ("ldu 4, 8(30) \n"
2905 /* Goto if stack[--sp] <= TOP */
2908 ppc64_emit_le_goto (int *offset_p
, int *size_p
)
2910 EMIT_ASM ("ldu 4, 8(30) \n"
2921 /* Goto if stack[--sp] > TOP */
2924 ppc64_emit_gt_goto (int *offset_p
, int *size_p
)
2926 EMIT_ASM ("ldu 4, 8(30) \n"
2937 /* Goto if stack[--sp] >= TOP */
2940 ppc64_emit_ge_goto (int *offset_p
, int *size_p
)
2942 EMIT_ASM ("ldu 4, 8(30) \n"
2953 /* Table of emit ops for 64-bit ELFv1. */
2955 static struct emit_ops ppc64v1_emit_ops_impl
=
2957 ppc64v1_emit_prologue
,
2958 ppc64_emit_epilogue
,
2963 ppc64_emit_rsh_signed
,
2964 ppc64_emit_rsh_unsigned
,
2972 ppc64_emit_less_signed
,
2973 ppc64_emit_less_unsigned
,
2977 ppc_write_goto_address
,
2982 ppc64_emit_stack_flush
,
2983 ppc64_emit_zero_ext
,
2985 ppc_emit_stack_adjust
,
2986 ppc64v1_emit_int_call_1
,
2987 ppc64v1_emit_void_call_2
,
2996 /* Table of emit ops for 64-bit ELFv2. */
2998 static struct emit_ops ppc64v2_emit_ops_impl
=
3000 ppc64v2_emit_prologue
,
3001 ppc64_emit_epilogue
,
3006 ppc64_emit_rsh_signed
,
3007 ppc64_emit_rsh_unsigned
,
3015 ppc64_emit_less_signed
,
3016 ppc64_emit_less_unsigned
,
3020 ppc_write_goto_address
,
3025 ppc64_emit_stack_flush
,
3026 ppc64_emit_zero_ext
,
3028 ppc_emit_stack_adjust
,
3029 ppc64v2_emit_int_call_1
,
3030 ppc64v2_emit_void_call_2
,
3041 /* Implementation of linux_target_ops method "emit_ops". */
3043 static struct emit_ops
*
3046 #ifdef __powerpc64__
3047 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
3049 if (register_size (regcache
->tdesc
, 0) == 8)
3051 if (is_elfv2_inferior ())
3052 return &ppc64v2_emit_ops_impl
;
3054 return &ppc64v1_emit_ops_impl
;
3057 return &ppc_emit_ops_impl
;
3060 /* Implementation of linux_target_ops method "get_ipa_tdesc_idx". */
3063 ppc_get_ipa_tdesc_idx (void)
3065 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
3066 const struct target_desc
*tdesc
= regcache
->tdesc
;
3068 #ifdef __powerpc64__
3069 if (tdesc
== tdesc_powerpc_64l
)
3070 return PPC_TDESC_BASE
;
3071 if (tdesc
== tdesc_powerpc_altivec64l
)
3072 return PPC_TDESC_ALTIVEC
;
3073 if (tdesc
== tdesc_powerpc_cell64l
)
3074 return PPC_TDESC_CELL
;
3075 if (tdesc
== tdesc_powerpc_vsx64l
)
3076 return PPC_TDESC_VSX
;
3077 if (tdesc
== tdesc_powerpc_isa205_64l
)
3078 return PPC_TDESC_ISA205
;
3079 if (tdesc
== tdesc_powerpc_isa205_altivec64l
)
3080 return PPC_TDESC_ISA205_ALTIVEC
;
3081 if (tdesc
== tdesc_powerpc_isa205_vsx64l
)
3082 return PPC_TDESC_ISA205_VSX
;
3085 if (tdesc
== tdesc_powerpc_32l
)
3086 return PPC_TDESC_BASE
;
3087 if (tdesc
== tdesc_powerpc_altivec32l
)
3088 return PPC_TDESC_ALTIVEC
;
3089 if (tdesc
== tdesc_powerpc_cell32l
)
3090 return PPC_TDESC_CELL
;
3091 if (tdesc
== tdesc_powerpc_vsx32l
)
3092 return PPC_TDESC_VSX
;
3093 if (tdesc
== tdesc_powerpc_isa205_32l
)
3094 return PPC_TDESC_ISA205
;
3095 if (tdesc
== tdesc_powerpc_isa205_altivec32l
)
3096 return PPC_TDESC_ISA205_ALTIVEC
;
3097 if (tdesc
== tdesc_powerpc_isa205_vsx32l
)
3098 return PPC_TDESC_ISA205_VSX
;
3099 if (tdesc
== tdesc_powerpc_e500l
)
3100 return PPC_TDESC_E500
;
3105 struct linux_target_ops the_low_target
= {
3108 ppc_cannot_fetch_register
,
3109 ppc_cannot_store_register
,
3110 NULL
, /* fetch_register */
3113 NULL
, /* breakpoint_kind_from_pc */
3114 ppc_sw_breakpoint_from_kind
,
3118 ppc_supports_z_point_type
,
3123 ppc_collect_ptrace_register
,
3124 ppc_supply_ptrace_register
,
3125 NULL
, /* siginfo_fixup */
3126 NULL
, /* new_process */
3127 NULL
, /* new_thread */
3128 NULL
, /* new_fork */
3129 NULL
, /* prepare_to_resume */
3130 NULL
, /* process_qsupported */
3131 ppc_supports_tracepoints
,
3132 ppc_get_thread_area
,
3133 ppc_install_fast_tracepoint_jump_pad
,
3135 ppc_get_min_fast_tracepoint_insn_len
,
3136 NULL
, /* supports_range_stepping */
3137 NULL
, /* breakpoint_kind_from_current_state */
3138 ppc_supports_hardware_single_step
,
3139 NULL
, /* get_syscall_trapinfo */
3140 ppc_get_ipa_tdesc_idx
,
3144 initialize_low_arch (void)
3146 /* Initialize the Linux target descriptions. */
3148 init_registers_powerpc_32l ();
3149 init_registers_powerpc_altivec32l ();
3150 init_registers_powerpc_cell32l ();
3151 init_registers_powerpc_vsx32l ();
3152 init_registers_powerpc_isa205_32l ();
3153 init_registers_powerpc_isa205_altivec32l ();
3154 init_registers_powerpc_isa205_vsx32l ();
3155 init_registers_powerpc_e500l ();
3157 init_registers_powerpc_64l ();
3158 init_registers_powerpc_altivec64l ();
3159 init_registers_powerpc_cell64l ();
3160 init_registers_powerpc_vsx64l ();
3161 init_registers_powerpc_isa205_64l ();
3162 init_registers_powerpc_isa205_altivec64l ();
3163 init_registers_powerpc_isa205_vsx64l ();
3166 initialize_regsets_info (&ppc_regsets_info
);