1 1.1 christos /* GNU/Linux/PowerPC specific low level interface, for the remote server for 2 1.1 christos GDB. 3 1.1.1.3 christos Copyright (C) 1995-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "linux-low.h" 21 1.1 christos 22 1.1 christos #include "elf/common.h" 23 1.1 christos #include <sys/uio.h> 24 1.1 christos #include <elf.h> 25 1.1 christos #include <asm/ptrace.h> 26 1.1 christos 27 1.1 christos #include "arch/ppc-linux-common.h" 28 1.1 christos #include "arch/ppc-linux-tdesc.h" 29 1.1 christos #include "nat/ppc-linux.h" 30 1.1 christos #include "nat/linux-ptrace.h" 31 1.1 christos #include "linux-ppc-tdesc-init.h" 32 1.1 christos #include "ax.h" 33 1.1 christos #include "tracepoint.h" 34 1.1 christos 35 1.1 christos #define PPC_FIELD(value, from, len) \ 36 1.1 christos (((value) >> (32 - (from) - (len))) & ((1 << (len)) - 1)) 37 1.1 christos #define PPC_SEXT(v, bs) \ 38 1.1 christos ((((CORE_ADDR) (v) & (((CORE_ADDR) 1 << (bs)) - 1)) \ 39 1.1 christos ^ ((CORE_ADDR) 1 << ((bs) - 1))) \ 40 1.1 christos - ((CORE_ADDR) 1 << ((bs) - 1))) 41 1.1 christos #define PPC_OP6(insn) PPC_FIELD (insn, 0, 6) 42 1.1 christos #define PPC_BO(insn) PPC_FIELD (insn, 6, 5) 43 1.1 christos #define PPC_LI(insn) (PPC_SEXT (PPC_FIELD (insn, 6, 24), 24) << 2) 44 1.1 christos #define PPC_BD(insn) (PPC_SEXT (PPC_FIELD (insn, 16, 14), 14) << 2) 45 1.1 christos 46 1.1 christos /* Linux target op definitions for the PowerPC architecture. */ 47 1.1 christos 48 1.1 christos class ppc_target : public linux_process_target 49 1.1 christos { 50 1.1 christos public: 51 1.1 christos 52 1.1 christos const regs_info *get_regs_info () override; 53 1.1 christos 54 1.1 christos const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override; 55 1.1 christos 56 1.1 christos bool supports_z_point_type (char z_type) override; 57 1.1 christos 58 1.1 christos 59 1.1 christos void low_collect_ptrace_register (regcache *regcache, int regno, 60 1.1 christos char *buf) override; 61 1.1 christos 62 1.1 christos void low_supply_ptrace_register (regcache *regcache, int regno, 63 1.1 christos const char *buf) override; 64 1.1 christos 65 1.1 christos bool supports_tracepoints () override; 66 1.1 christos 67 1.1 christos bool supports_fast_tracepoints () override; 68 1.1 christos 69 1.1 christos int install_fast_tracepoint_jump_pad 70 1.1 christos (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector, 71 1.1 christos CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry, 72 1.1 christos CORE_ADDR *trampoline, ULONGEST *trampoline_size, 73 1.1 christos unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size, 74 1.1 christos CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end, 75 1.1 christos char *err) override; 76 1.1 christos 77 1.1 christos int get_min_fast_tracepoint_insn_len () override; 78 1.1 christos 79 1.1 christos struct emit_ops *emit_ops () override; 80 1.1 christos 81 1.1 christos int get_ipa_tdesc_idx () override; 82 1.1 christos 83 1.1 christos protected: 84 1.1 christos 85 1.1 christos void low_arch_setup () override; 86 1.1 christos 87 1.1 christos bool low_cannot_fetch_register (int regno) override; 88 1.1 christos 89 1.1 christos bool low_cannot_store_register (int regno) override; 90 1.1 christos 91 1.1 christos bool low_supports_breakpoints () override; 92 1.1 christos 93 1.1 christos CORE_ADDR low_get_pc (regcache *regcache) override; 94 1.1 christos 95 1.1 christos void low_set_pc (regcache *regcache, CORE_ADDR newpc) override; 96 1.1 christos 97 1.1 christos bool low_breakpoint_at (CORE_ADDR pc) override; 98 1.1 christos 99 1.1 christos int low_insert_point (raw_bkpt_type type, CORE_ADDR addr, 100 1.1 christos int size, raw_breakpoint *bp) override; 101 1.1 christos 102 1.1 christos int low_remove_point (raw_bkpt_type type, CORE_ADDR addr, 103 1.1 christos int size, raw_breakpoint *bp) override; 104 1.1 christos 105 1.1 christos int low_get_thread_area (int lwpid, CORE_ADDR *addrp) override; 106 1.1 christos }; 107 1.1 christos 108 1.1 christos /* The singleton target ops object. */ 109 1.1 christos 110 1.1 christos static ppc_target the_ppc_target; 111 1.1 christos 112 1.1 christos /* Holds the AT_HWCAP auxv entry. */ 113 1.1 christos 114 1.1 christos static unsigned long ppc_hwcap; 115 1.1 christos 116 1.1 christos /* Holds the AT_HWCAP2 auxv entry. */ 117 1.1 christos 118 1.1 christos static unsigned long ppc_hwcap2; 119 1.1 christos 120 1.1 christos 121 1.1 christos #define ppc_num_regs 73 122 1.1 christos 123 1.1 christos #ifdef __powerpc64__ 124 1.1 christos /* We use a constant for FPSCR instead of PT_FPSCR, because 125 1.1 christos many shipped PPC64 kernels had the wrong value in ptrace.h. */ 126 1.1 christos static int ppc_regmap[] = 127 1.1 christos {PT_R0 * 8, PT_R1 * 8, PT_R2 * 8, PT_R3 * 8, 128 1.1 christos PT_R4 * 8, PT_R5 * 8, PT_R6 * 8, PT_R7 * 8, 129 1.1 christos PT_R8 * 8, PT_R9 * 8, PT_R10 * 8, PT_R11 * 8, 130 1.1 christos PT_R12 * 8, PT_R13 * 8, PT_R14 * 8, PT_R15 * 8, 131 1.1 christos PT_R16 * 8, PT_R17 * 8, PT_R18 * 8, PT_R19 * 8, 132 1.1 christos PT_R20 * 8, PT_R21 * 8, PT_R22 * 8, PT_R23 * 8, 133 1.1 christos PT_R24 * 8, PT_R25 * 8, PT_R26 * 8, PT_R27 * 8, 134 1.1 christos PT_R28 * 8, PT_R29 * 8, PT_R30 * 8, PT_R31 * 8, 135 1.1 christos PT_FPR0*8, PT_FPR0*8 + 8, PT_FPR0*8+16, PT_FPR0*8+24, 136 1.1 christos PT_FPR0*8+32, PT_FPR0*8+40, PT_FPR0*8+48, PT_FPR0*8+56, 137 1.1 christos PT_FPR0*8+64, PT_FPR0*8+72, PT_FPR0*8+80, PT_FPR0*8+88, 138 1.1 christos PT_FPR0*8+96, PT_FPR0*8+104, PT_FPR0*8+112, PT_FPR0*8+120, 139 1.1 christos PT_FPR0*8+128, PT_FPR0*8+136, PT_FPR0*8+144, PT_FPR0*8+152, 140 1.1 christos PT_FPR0*8+160, PT_FPR0*8+168, PT_FPR0*8+176, PT_FPR0*8+184, 141 1.1 christos PT_FPR0*8+192, PT_FPR0*8+200, PT_FPR0*8+208, PT_FPR0*8+216, 142 1.1 christos PT_FPR0*8+224, PT_FPR0*8+232, PT_FPR0*8+240, PT_FPR0*8+248, 143 1.1 christos PT_NIP * 8, PT_MSR * 8, PT_CCR * 8, PT_LNK * 8, 144 1.1 christos PT_CTR * 8, PT_XER * 8, PT_FPR0*8 + 256, 145 1.1 christos PT_ORIG_R3 * 8, PT_TRAP * 8 }; 146 1.1 christos #else 147 1.1 christos /* Currently, don't check/send MQ. */ 148 1.1 christos static int ppc_regmap[] = 149 1.1 christos {PT_R0 * 4, PT_R1 * 4, PT_R2 * 4, PT_R3 * 4, 150 1.1 christos PT_R4 * 4, PT_R5 * 4, PT_R6 * 4, PT_R7 * 4, 151 1.1 christos PT_R8 * 4, PT_R9 * 4, PT_R10 * 4, PT_R11 * 4, 152 1.1 christos PT_R12 * 4, PT_R13 * 4, PT_R14 * 4, PT_R15 * 4, 153 1.1 christos PT_R16 * 4, PT_R17 * 4, PT_R18 * 4, PT_R19 * 4, 154 1.1 christos PT_R20 * 4, PT_R21 * 4, PT_R22 * 4, PT_R23 * 4, 155 1.1 christos PT_R24 * 4, PT_R25 * 4, PT_R26 * 4, PT_R27 * 4, 156 1.1 christos PT_R28 * 4, PT_R29 * 4, PT_R30 * 4, PT_R31 * 4, 157 1.1 christos PT_FPR0*4, PT_FPR0*4 + 8, PT_FPR0*4+16, PT_FPR0*4+24, 158 1.1 christos PT_FPR0*4+32, PT_FPR0*4+40, PT_FPR0*4+48, PT_FPR0*4+56, 159 1.1 christos PT_FPR0*4+64, PT_FPR0*4+72, PT_FPR0*4+80, PT_FPR0*4+88, 160 1.1 christos PT_FPR0*4+96, PT_FPR0*4+104, PT_FPR0*4+112, PT_FPR0*4+120, 161 1.1 christos PT_FPR0*4+128, PT_FPR0*4+136, PT_FPR0*4+144, PT_FPR0*4+152, 162 1.1 christos PT_FPR0*4+160, PT_FPR0*4+168, PT_FPR0*4+176, PT_FPR0*4+184, 163 1.1 christos PT_FPR0*4+192, PT_FPR0*4+200, PT_FPR0*4+208, PT_FPR0*4+216, 164 1.1 christos PT_FPR0*4+224, PT_FPR0*4+232, PT_FPR0*4+240, PT_FPR0*4+248, 165 1.1 christos PT_NIP * 4, PT_MSR * 4, PT_CCR * 4, PT_LNK * 4, 166 1.1 christos PT_CTR * 4, PT_XER * 4, PT_FPSCR * 4, 167 1.1 christos PT_ORIG_R3 * 4, PT_TRAP * 4 168 1.1 christos }; 169 1.1 christos 170 1.1 christos static int ppc_regmap_e500[] = 171 1.1 christos {PT_R0 * 4, PT_R1 * 4, PT_R2 * 4, PT_R3 * 4, 172 1.1 christos PT_R4 * 4, PT_R5 * 4, PT_R6 * 4, PT_R7 * 4, 173 1.1 christos PT_R8 * 4, PT_R9 * 4, PT_R10 * 4, PT_R11 * 4, 174 1.1 christos PT_R12 * 4, PT_R13 * 4, PT_R14 * 4, PT_R15 * 4, 175 1.1 christos PT_R16 * 4, PT_R17 * 4, PT_R18 * 4, PT_R19 * 4, 176 1.1 christos PT_R20 * 4, PT_R21 * 4, PT_R22 * 4, PT_R23 * 4, 177 1.1 christos PT_R24 * 4, PT_R25 * 4, PT_R26 * 4, PT_R27 * 4, 178 1.1 christos PT_R28 * 4, PT_R29 * 4, PT_R30 * 4, PT_R31 * 4, 179 1.1 christos -1, -1, -1, -1, 180 1.1 christos -1, -1, -1, -1, 181 1.1 christos -1, -1, -1, -1, 182 1.1 christos -1, -1, -1, -1, 183 1.1 christos -1, -1, -1, -1, 184 1.1 christos -1, -1, -1, -1, 185 1.1 christos -1, -1, -1, -1, 186 1.1 christos -1, -1, -1, -1, 187 1.1 christos PT_NIP * 4, PT_MSR * 4, PT_CCR * 4, PT_LNK * 4, 188 1.1 christos PT_CTR * 4, PT_XER * 4, -1, 189 1.1 christos PT_ORIG_R3 * 4, PT_TRAP * 4 190 1.1 christos }; 191 1.1 christos #endif 192 1.1 christos 193 1.1 christos /* Check whether the kernel provides a register set with number 194 1.1 christos REGSET_ID of size REGSETSIZE for process/thread TID. */ 195 1.1 christos 196 1.1 christos static int 197 1.1 christos ppc_check_regset (int tid, int regset_id, int regsetsize) 198 1.1 christos { 199 1.1 christos void *buf = alloca (regsetsize); 200 1.1 christos struct iovec iov; 201 1.1 christos 202 1.1 christos iov.iov_base = buf; 203 1.1 christos iov.iov_len = regsetsize; 204 1.1 christos 205 1.1 christos if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) >= 0 206 1.1 christos || errno == ENODATA) 207 1.1 christos return 1; 208 1.1 christos return 0; 209 1.1 christos } 210 1.1 christos 211 1.1 christos bool 212 1.1 christos ppc_target::low_cannot_store_register (int regno) 213 1.1 christos { 214 1.1 christos const struct target_desc *tdesc = current_process ()->tdesc; 215 1.1 christos 216 1.1 christos #ifndef __powerpc64__ 217 1.1 christos /* Some kernels do not allow us to store fpscr. */ 218 1.1 christos if (!(ppc_hwcap & PPC_FEATURE_HAS_SPE) 219 1.1 christos && regno == find_regno (tdesc, "fpscr")) 220 1.1 christos return true; 221 1.1 christos #endif 222 1.1 christos 223 1.1 christos /* Some kernels do not allow us to store orig_r3 or trap. */ 224 1.1 christos if (regno == find_regno (tdesc, "orig_r3") 225 1.1 christos || regno == find_regno (tdesc, "trap")) 226 1.1 christos return true; 227 1.1 christos 228 1.1 christos return false; 229 1.1 christos } 230 1.1 christos 231 1.1 christos bool 232 1.1 christos ppc_target::low_cannot_fetch_register (int regno) 233 1.1 christos { 234 1.1 christos return false; 235 1.1 christos } 236 1.1 christos 237 1.1 christos void 238 1.1 christos ppc_target::low_collect_ptrace_register (regcache *regcache, int regno, 239 1.1 christos char *buf) 240 1.1 christos { 241 1.1 christos memset (buf, 0, sizeof (long)); 242 1.1 christos 243 1.1 christos if (__BYTE_ORDER == __LITTLE_ENDIAN) 244 1.1 christos { 245 1.1 christos /* Little-endian values always sit at the left end of the buffer. */ 246 1.1 christos collect_register (regcache, regno, buf); 247 1.1 christos } 248 1.1 christos else if (__BYTE_ORDER == __BIG_ENDIAN) 249 1.1 christos { 250 1.1 christos /* Big-endian values sit at the right end of the buffer. In case of 251 1.1.1.2 christos registers whose sizes are smaller than sizeof (long), we must use a 252 1.1.1.2 christos padding to access them correctly. */ 253 1.1 christos int size = register_size (regcache->tdesc, regno); 254 1.1 christos 255 1.1 christos if (size < sizeof (long)) 256 1.1 christos collect_register (regcache, regno, buf + sizeof (long) - size); 257 1.1 christos else 258 1.1 christos collect_register (regcache, regno, buf); 259 1.1 christos } 260 1.1 christos else 261 1.1 christos perror_with_name ("Unexpected byte order"); 262 1.1 christos } 263 1.1 christos 264 1.1 christos void 265 1.1 christos ppc_target::low_supply_ptrace_register (regcache *regcache, int regno, 266 1.1 christos const char *buf) 267 1.1 christos { 268 1.1 christos if (__BYTE_ORDER == __LITTLE_ENDIAN) 269 1.1 christos { 270 1.1 christos /* Little-endian values always sit at the left end of the buffer. */ 271 1.1 christos supply_register (regcache, regno, buf); 272 1.1 christos } 273 1.1 christos else if (__BYTE_ORDER == __BIG_ENDIAN) 274 1.1 christos { 275 1.1 christos /* Big-endian values sit at the right end of the buffer. In case of 276 1.1.1.2 christos registers whose sizes are smaller than sizeof (long), we must use a 277 1.1.1.2 christos padding to access them correctly. */ 278 1.1 christos int size = register_size (regcache->tdesc, regno); 279 1.1 christos 280 1.1 christos if (size < sizeof (long)) 281 1.1 christos supply_register (regcache, regno, buf + sizeof (long) - size); 282 1.1 christos else 283 1.1 christos supply_register (regcache, regno, buf); 284 1.1 christos } 285 1.1 christos else 286 1.1 christos perror_with_name ("Unexpected byte order"); 287 1.1 christos } 288 1.1 christos 289 1.1 christos bool 290 1.1 christos ppc_target::low_supports_breakpoints () 291 1.1 christos { 292 1.1 christos return true; 293 1.1 christos } 294 1.1 christos 295 1.1 christos CORE_ADDR 296 1.1 christos ppc_target::low_get_pc (regcache *regcache) 297 1.1 christos { 298 1.1 christos if (register_size (regcache->tdesc, 0) == 4) 299 1.1 christos { 300 1.1 christos unsigned int pc; 301 1.1 christos collect_register_by_name (regcache, "pc", &pc); 302 1.1 christos return (CORE_ADDR) pc; 303 1.1 christos } 304 1.1 christos else 305 1.1 christos { 306 1.1 christos unsigned long pc; 307 1.1 christos collect_register_by_name (regcache, "pc", &pc); 308 1.1 christos return (CORE_ADDR) pc; 309 1.1 christos } 310 1.1 christos } 311 1.1 christos 312 1.1 christos void 313 1.1 christos ppc_target::low_set_pc (regcache *regcache, CORE_ADDR pc) 314 1.1 christos { 315 1.1 christos if (register_size (regcache->tdesc, 0) == 4) 316 1.1 christos { 317 1.1 christos unsigned int newpc = pc; 318 1.1 christos supply_register_by_name (regcache, "pc", &newpc); 319 1.1 christos } 320 1.1 christos else 321 1.1 christos { 322 1.1 christos unsigned long newpc = pc; 323 1.1 christos supply_register_by_name (regcache, "pc", &newpc); 324 1.1 christos } 325 1.1 christos } 326 1.1 christos 327 1.1 christos #ifndef __powerpc64__ 328 1.1 christos static int ppc_regmap_adjusted; 329 1.1 christos #endif 330 1.1 christos 331 1.1 christos 332 1.1 christos /* Correct in either endianness. 333 1.1 christos This instruction is "twge r2, r2", which GDB uses as a software 334 1.1 christos breakpoint. */ 335 1.1 christos static const unsigned int ppc_breakpoint = 0x7d821008; 336 1.1 christos #define ppc_breakpoint_len 4 337 1.1 christos 338 1.1 christos /* Implementation of target ops method "sw_breakpoint_from_kind". */ 339 1.1 christos 340 1.1 christos const gdb_byte * 341 1.1 christos ppc_target::sw_breakpoint_from_kind (int kind, int *size) 342 1.1 christos { 343 1.1 christos *size = ppc_breakpoint_len; 344 1.1 christos return (const gdb_byte *) &ppc_breakpoint; 345 1.1 christos } 346 1.1 christos 347 1.1 christos bool 348 1.1 christos ppc_target::low_breakpoint_at (CORE_ADDR where) 349 1.1 christos { 350 1.1 christos unsigned int insn; 351 1.1 christos 352 1.1 christos read_memory (where, (unsigned char *) &insn, 4); 353 1.1 christos if (insn == ppc_breakpoint) 354 1.1 christos return true; 355 1.1 christos /* If necessary, recognize more trap instructions here. GDB only uses 356 1.1 christos the one. */ 357 1.1 christos 358 1.1 christos return false; 359 1.1 christos } 360 1.1 christos 361 1.1 christos /* Implement supports_z_point_type target-ops. 362 1.1 christos Returns true if type Z_TYPE breakpoint is supported. 363 1.1 christos 364 1.1 christos Handling software breakpoint at server side, so tracepoints 365 1.1 christos and breakpoints can be inserted at the same location. */ 366 1.1 christos 367 1.1 christos bool 368 1.1 christos ppc_target::supports_z_point_type (char z_type) 369 1.1 christos { 370 1.1 christos switch (z_type) 371 1.1 christos { 372 1.1 christos case Z_PACKET_SW_BP: 373 1.1 christos return true; 374 1.1 christos case Z_PACKET_HW_BP: 375 1.1 christos case Z_PACKET_WRITE_WP: 376 1.1 christos case Z_PACKET_ACCESS_WP: 377 1.1 christos default: 378 1.1 christos return false; 379 1.1 christos } 380 1.1 christos } 381 1.1 christos 382 1.1 christos /* Implement the low_insert_point linux target op. 383 1.1 christos Returns 0 on success, -1 on failure and 1 on unsupported. */ 384 1.1 christos 385 1.1 christos int 386 1.1 christos ppc_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr, 387 1.1 christos int size, raw_breakpoint *bp) 388 1.1 christos { 389 1.1 christos switch (type) 390 1.1 christos { 391 1.1 christos case raw_bkpt_type_sw: 392 1.1 christos return insert_memory_breakpoint (bp); 393 1.1 christos 394 1.1 christos case raw_bkpt_type_hw: 395 1.1 christos case raw_bkpt_type_write_wp: 396 1.1 christos case raw_bkpt_type_access_wp: 397 1.1 christos default: 398 1.1 christos /* Unsupported. */ 399 1.1 christos return 1; 400 1.1 christos } 401 1.1 christos } 402 1.1 christos 403 1.1 christos /* Implement the low_remove_point linux target op. 404 1.1 christos Returns 0 on success, -1 on failure and 1 on unsupported. */ 405 1.1 christos 406 1.1 christos int 407 1.1 christos ppc_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr, 408 1.1 christos int size, raw_breakpoint *bp) 409 1.1 christos { 410 1.1 christos switch (type) 411 1.1 christos { 412 1.1 christos case raw_bkpt_type_sw: 413 1.1 christos return remove_memory_breakpoint (bp); 414 1.1 christos 415 1.1 christos case raw_bkpt_type_hw: 416 1.1 christos case raw_bkpt_type_write_wp: 417 1.1 christos case raw_bkpt_type_access_wp: 418 1.1 christos default: 419 1.1 christos /* Unsupported. */ 420 1.1 christos return 1; 421 1.1 christos } 422 1.1 christos } 423 1.1 christos 424 1.1 christos /* Provide only a fill function for the general register set. ps_lgetregs 425 1.1 christos will use this for NPTL support. */ 426 1.1 christos 427 1.1 christos static void ppc_fill_gregset (struct regcache *regcache, void *buf) 428 1.1 christos { 429 1.1 christos int i; 430 1.1 christos 431 1.1 christos ppc_target *my_ppc_target = (ppc_target *) the_linux_target; 432 1.1 christos 433 1.1 christos for (i = 0; i < 32; i++) 434 1.1 christos my_ppc_target->low_collect_ptrace_register (regcache, i, 435 1.1 christos (char *) buf + ppc_regmap[i]); 436 1.1 christos 437 1.1 christos for (i = 64; i < 70; i++) 438 1.1 christos my_ppc_target->low_collect_ptrace_register (regcache, i, 439 1.1 christos (char *) buf + ppc_regmap[i]); 440 1.1 christos 441 1.1 christos for (i = 71; i < 73; i++) 442 1.1 christos my_ppc_target->low_collect_ptrace_register (regcache, i, 443 1.1 christos (char *) buf + ppc_regmap[i]); 444 1.1 christos } 445 1.1 christos 446 1.1 christos /* Program Priority Register regset fill function. */ 447 1.1 christos 448 1.1 christos static void 449 1.1 christos ppc_fill_pprregset (struct regcache *regcache, void *buf) 450 1.1 christos { 451 1.1 christos char *ppr = (char *) buf; 452 1.1 christos 453 1.1 christos collect_register_by_name (regcache, "ppr", ppr); 454 1.1 christos } 455 1.1 christos 456 1.1 christos /* Program Priority Register regset store function. */ 457 1.1 christos 458 1.1 christos static void 459 1.1 christos ppc_store_pprregset (struct regcache *regcache, const void *buf) 460 1.1 christos { 461 1.1 christos const char *ppr = (const char *) buf; 462 1.1 christos 463 1.1 christos supply_register_by_name (regcache, "ppr", ppr); 464 1.1 christos } 465 1.1 christos 466 1.1 christos /* Data Stream Control Register regset fill function. */ 467 1.1 christos 468 1.1 christos static void 469 1.1 christos ppc_fill_dscrregset (struct regcache *regcache, void *buf) 470 1.1 christos { 471 1.1 christos char *dscr = (char *) buf; 472 1.1 christos 473 1.1 christos collect_register_by_name (regcache, "dscr", dscr); 474 1.1 christos } 475 1.1 christos 476 1.1 christos /* Data Stream Control Register regset store function. */ 477 1.1 christos 478 1.1 christos static void 479 1.1 christos ppc_store_dscrregset (struct regcache *regcache, const void *buf) 480 1.1 christos { 481 1.1 christos const char *dscr = (const char *) buf; 482 1.1 christos 483 1.1 christos supply_register_by_name (regcache, "dscr", dscr); 484 1.1 christos } 485 1.1 christos 486 1.1 christos /* Target Address Register regset fill function. */ 487 1.1 christos 488 1.1 christos static void 489 1.1 christos ppc_fill_tarregset (struct regcache *regcache, void *buf) 490 1.1 christos { 491 1.1 christos char *tar = (char *) buf; 492 1.1 christos 493 1.1 christos collect_register_by_name (regcache, "tar", tar); 494 1.1 christos } 495 1.1 christos 496 1.1 christos /* Target Address Register regset store function. */ 497 1.1 christos 498 1.1 christos static void 499 1.1 christos ppc_store_tarregset (struct regcache *regcache, const void *buf) 500 1.1 christos { 501 1.1 christos const char *tar = (const char *) buf; 502 1.1 christos 503 1.1 christos supply_register_by_name (regcache, "tar", tar); 504 1.1 christos } 505 1.1 christos 506 1.1 christos /* Event-Based Branching regset store function. Unless the inferior 507 1.1 christos has a perf event open, ptrace can return in error when reading and 508 1.1 christos writing to the regset, with ENODATA. For reading, the registers 509 1.1 christos will correctly show as unavailable. For writing, gdbserver 510 1.1 christos currently only caches any register writes from P and G packets and 511 1.1 christos the stub always tries to write all the regsets when resuming the 512 1.1 christos inferior, which would result in frequent warnings. For this 513 1.1 christos reason, we don't define a fill function. This also means that the 514 1.1 christos client-side regcache will be dirty if the user tries to write to 515 1.1 christos the EBB registers. G packets that the client sends to write to 516 1.1 christos unrelated registers will also include data for EBB registers, even 517 1.1 christos if they are unavailable. */ 518 1.1 christos 519 1.1 christos static void 520 1.1 christos ppc_store_ebbregset (struct regcache *regcache, const void *buf) 521 1.1 christos { 522 1.1 christos const char *regset = (const char *) buf; 523 1.1 christos 524 1.1 christos /* The order in the kernel regset is: EBBRR, EBBHR, BESCR. In the 525 1.1 christos .dat file is BESCR, EBBHR, EBBRR. */ 526 1.1 christos supply_register_by_name (regcache, "ebbrr", ®set[0]); 527 1.1 christos supply_register_by_name (regcache, "ebbhr", ®set[8]); 528 1.1 christos supply_register_by_name (regcache, "bescr", ®set[16]); 529 1.1 christos } 530 1.1 christos 531 1.1 christos /* Performance Monitoring Unit regset fill function. */ 532 1.1 christos 533 1.1 christos static void 534 1.1 christos ppc_fill_pmuregset (struct regcache *regcache, void *buf) 535 1.1 christos { 536 1.1 christos char *regset = (char *) buf; 537 1.1 christos 538 1.1 christos /* The order in the kernel regset is SIAR, SDAR, SIER, MMCR2, MMCR0. 539 1.1 christos In the .dat file is MMCR0, MMCR2, SIAR, SDAR, SIER. */ 540 1.1 christos collect_register_by_name (regcache, "siar", ®set[0]); 541 1.1 christos collect_register_by_name (regcache, "sdar", ®set[8]); 542 1.1 christos collect_register_by_name (regcache, "sier", ®set[16]); 543 1.1 christos collect_register_by_name (regcache, "mmcr2", ®set[24]); 544 1.1 christos collect_register_by_name (regcache, "mmcr0", ®set[32]); 545 1.1 christos } 546 1.1 christos 547 1.1 christos /* Performance Monitoring Unit regset store function. */ 548 1.1 christos 549 1.1 christos static void 550 1.1 christos ppc_store_pmuregset (struct regcache *regcache, const void *buf) 551 1.1 christos { 552 1.1 christos const char *regset = (const char *) buf; 553 1.1 christos 554 1.1 christos supply_register_by_name (regcache, "siar", ®set[0]); 555 1.1 christos supply_register_by_name (regcache, "sdar", ®set[8]); 556 1.1 christos supply_register_by_name (regcache, "sier", ®set[16]); 557 1.1 christos supply_register_by_name (regcache, "mmcr2", ®set[24]); 558 1.1 christos supply_register_by_name (regcache, "mmcr0", ®set[32]); 559 1.1 christos } 560 1.1 christos 561 1.1 christos /* Hardware Transactional Memory special-purpose register regset fill 562 1.1 christos function. */ 563 1.1 christos 564 1.1 christos static void 565 1.1 christos ppc_fill_tm_sprregset (struct regcache *regcache, void *buf) 566 1.1 christos { 567 1.1 christos int i, base; 568 1.1 christos char *regset = (char *) buf; 569 1.1 christos 570 1.1 christos base = find_regno (regcache->tdesc, "tfhar"); 571 1.1 christos for (i = 0; i < 3; i++) 572 1.1 christos collect_register (regcache, base + i, ®set[i * 8]); 573 1.1 christos } 574 1.1 christos 575 1.1 christos /* Hardware Transactional Memory special-purpose register regset store 576 1.1 christos function. */ 577 1.1 christos 578 1.1 christos static void 579 1.1 christos ppc_store_tm_sprregset (struct regcache *regcache, const void *buf) 580 1.1 christos { 581 1.1 christos int i, base; 582 1.1 christos const char *regset = (const char *) buf; 583 1.1 christos 584 1.1 christos base = find_regno (regcache->tdesc, "tfhar"); 585 1.1 christos for (i = 0; i < 3; i++) 586 1.1 christos supply_register (regcache, base + i, ®set[i * 8]); 587 1.1 christos } 588 1.1 christos 589 1.1 christos /* For the same reasons as the EBB regset, none of the HTM 590 1.1 christos checkpointed regsets have a fill function. These registers are 591 1.1 christos only available if the inferior is in a transaction. */ 592 1.1 christos 593 1.1 christos /* Hardware Transactional Memory checkpointed general-purpose regset 594 1.1 christos store function. */ 595 1.1 christos 596 1.1 christos static void 597 1.1 christos ppc_store_tm_cgprregset (struct regcache *regcache, const void *buf) 598 1.1 christos { 599 1.1 christos int i, base, size, endian_offset; 600 1.1 christos const char *regset = (const char *) buf; 601 1.1 christos 602 1.1 christos base = find_regno (regcache->tdesc, "cr0"); 603 1.1 christos size = register_size (regcache->tdesc, base); 604 1.1 christos 605 1.1 christos gdb_assert (size == 4 || size == 8); 606 1.1 christos 607 1.1 christos for (i = 0; i < 32; i++) 608 1.1 christos supply_register (regcache, base + i, ®set[i * size]); 609 1.1 christos 610 1.1 christos endian_offset = 0; 611 1.1 christos 612 1.1 christos if ((size == 8) && (__BYTE_ORDER == __BIG_ENDIAN)) 613 1.1 christos endian_offset = 4; 614 1.1 christos 615 1.1 christos supply_register_by_name (regcache, "ccr", 616 1.1 christos ®set[PT_CCR * size + endian_offset]); 617 1.1 christos 618 1.1 christos supply_register_by_name (regcache, "cxer", 619 1.1 christos ®set[PT_XER * size + endian_offset]); 620 1.1 christos 621 1.1 christos supply_register_by_name (regcache, "clr", ®set[PT_LNK * size]); 622 1.1 christos supply_register_by_name (regcache, "cctr", ®set[PT_CTR * size]); 623 1.1 christos } 624 1.1 christos 625 1.1 christos /* Hardware Transactional Memory checkpointed floating-point regset 626 1.1 christos store function. */ 627 1.1 christos 628 1.1 christos static void 629 1.1 christos ppc_store_tm_cfprregset (struct regcache *regcache, const void *buf) 630 1.1 christos { 631 1.1 christos int i, base; 632 1.1 christos const char *regset = (const char *) buf; 633 1.1 christos 634 1.1 christos base = find_regno (regcache->tdesc, "cf0"); 635 1.1 christos 636 1.1 christos for (i = 0; i < 32; i++) 637 1.1 christos supply_register (regcache, base + i, ®set[i * 8]); 638 1.1 christos 639 1.1 christos supply_register_by_name (regcache, "cfpscr", ®set[32 * 8]); 640 1.1 christos } 641 1.1 christos 642 1.1 christos /* Hardware Transactional Memory checkpointed vector regset store 643 1.1 christos function. */ 644 1.1 christos 645 1.1 christos static void 646 1.1 christos ppc_store_tm_cvrregset (struct regcache *regcache, const void *buf) 647 1.1 christos { 648 1.1 christos int i, base; 649 1.1 christos const char *regset = (const char *) buf; 650 1.1 christos int vscr_offset = 0; 651 1.1 christos 652 1.1 christos base = find_regno (regcache->tdesc, "cvr0"); 653 1.1 christos 654 1.1 christos for (i = 0; i < 32; i++) 655 1.1 christos supply_register (regcache, base + i, ®set[i * 16]); 656 1.1 christos 657 1.1 christos if (__BYTE_ORDER == __BIG_ENDIAN) 658 1.1 christos vscr_offset = 12; 659 1.1 christos 660 1.1 christos supply_register_by_name (regcache, "cvscr", 661 1.1 christos ®set[32 * 16 + vscr_offset]); 662 1.1 christos 663 1.1 christos supply_register_by_name (regcache, "cvrsave", ®set[33 * 16]); 664 1.1 christos } 665 1.1 christos 666 1.1 christos /* Hardware Transactional Memory checkpointed vector-scalar regset 667 1.1 christos store function. */ 668 1.1 christos 669 1.1 christos static void 670 1.1 christos ppc_store_tm_cvsxregset (struct regcache *regcache, const void *buf) 671 1.1 christos { 672 1.1 christos int i, base; 673 1.1 christos const char *regset = (const char *) buf; 674 1.1 christos 675 1.1 christos base = find_regno (regcache->tdesc, "cvs0h"); 676 1.1 christos for (i = 0; i < 32; i++) 677 1.1 christos supply_register (regcache, base + i, ®set[i * 8]); 678 1.1 christos } 679 1.1 christos 680 1.1 christos /* Hardware Transactional Memory checkpointed Program Priority 681 1.1 christos Register regset store function. */ 682 1.1 christos 683 1.1 christos static void 684 1.1 christos ppc_store_tm_cpprregset (struct regcache *regcache, const void *buf) 685 1.1 christos { 686 1.1 christos const char *cppr = (const char *) buf; 687 1.1 christos 688 1.1 christos supply_register_by_name (regcache, "cppr", cppr); 689 1.1 christos } 690 1.1 christos 691 1.1 christos /* Hardware Transactional Memory checkpointed Data Stream Control 692 1.1 christos Register regset store function. */ 693 1.1 christos 694 1.1 christos static void 695 1.1 christos ppc_store_tm_cdscrregset (struct regcache *regcache, const void *buf) 696 1.1 christos { 697 1.1 christos const char *cdscr = (const char *) buf; 698 1.1 christos 699 1.1 christos supply_register_by_name (regcache, "cdscr", cdscr); 700 1.1 christos } 701 1.1 christos 702 1.1 christos /* Hardware Transactional Memory checkpointed Target Address Register 703 1.1 christos regset store function. */ 704 1.1 christos 705 1.1 christos static void 706 1.1 christos ppc_store_tm_ctarregset (struct regcache *regcache, const void *buf) 707 1.1 christos { 708 1.1 christos const char *ctar = (const char *) buf; 709 1.1 christos 710 1.1 christos supply_register_by_name (regcache, "ctar", ctar); 711 1.1 christos } 712 1.1 christos 713 1.1 christos static void 714 1.1 christos ppc_fill_vsxregset (struct regcache *regcache, void *buf) 715 1.1 christos { 716 1.1 christos int i, base; 717 1.1 christos char *regset = (char *) buf; 718 1.1 christos 719 1.1 christos base = find_regno (regcache->tdesc, "vs0h"); 720 1.1 christos for (i = 0; i < 32; i++) 721 1.1 christos collect_register (regcache, base + i, ®set[i * 8]); 722 1.1 christos } 723 1.1 christos 724 1.1 christos static void 725 1.1 christos ppc_store_vsxregset (struct regcache *regcache, const void *buf) 726 1.1 christos { 727 1.1 christos int i, base; 728 1.1 christos const char *regset = (const char *) buf; 729 1.1 christos 730 1.1 christos base = find_regno (regcache->tdesc, "vs0h"); 731 1.1 christos for (i = 0; i < 32; i++) 732 1.1 christos supply_register (regcache, base + i, ®set[i * 8]); 733 1.1 christos } 734 1.1 christos 735 1.1 christos static void 736 1.1 christos ppc_fill_vrregset (struct regcache *regcache, void *buf) 737 1.1 christos { 738 1.1 christos int i, base; 739 1.1 christos char *regset = (char *) buf; 740 1.1 christos int vscr_offset = 0; 741 1.1 christos 742 1.1 christos base = find_regno (regcache->tdesc, "vr0"); 743 1.1 christos for (i = 0; i < 32; i++) 744 1.1 christos collect_register (regcache, base + i, ®set[i * 16]); 745 1.1 christos 746 1.1 christos if (__BYTE_ORDER == __BIG_ENDIAN) 747 1.1 christos vscr_offset = 12; 748 1.1 christos 749 1.1 christos collect_register_by_name (regcache, "vscr", 750 1.1 christos ®set[32 * 16 + vscr_offset]); 751 1.1 christos 752 1.1 christos collect_register_by_name (regcache, "vrsave", ®set[33 * 16]); 753 1.1 christos } 754 1.1 christos 755 1.1 christos static void 756 1.1 christos ppc_store_vrregset (struct regcache *regcache, const void *buf) 757 1.1 christos { 758 1.1 christos int i, base; 759 1.1 christos const char *regset = (const char *) buf; 760 1.1 christos int vscr_offset = 0; 761 1.1 christos 762 1.1 christos base = find_regno (regcache->tdesc, "vr0"); 763 1.1 christos for (i = 0; i < 32; i++) 764 1.1 christos supply_register (regcache, base + i, ®set[i * 16]); 765 1.1 christos 766 1.1 christos if (__BYTE_ORDER == __BIG_ENDIAN) 767 1.1 christos vscr_offset = 12; 768 1.1 christos 769 1.1 christos supply_register_by_name (regcache, "vscr", 770 1.1 christos ®set[32 * 16 + vscr_offset]); 771 1.1 christos supply_register_by_name (regcache, "vrsave", ®set[33 * 16]); 772 1.1 christos } 773 1.1 christos 774 1.1 christos struct gdb_evrregset_t 775 1.1 christos { 776 1.1 christos unsigned long evr[32]; 777 1.1 christos unsigned long long acc; 778 1.1 christos unsigned long spefscr; 779 1.1 christos }; 780 1.1 christos 781 1.1 christos static void 782 1.1 christos ppc_fill_evrregset (struct regcache *regcache, void *buf) 783 1.1 christos { 784 1.1 christos int i, ev0; 785 1.1 christos struct gdb_evrregset_t *regset = (struct gdb_evrregset_t *) buf; 786 1.1 christos 787 1.1 christos ev0 = find_regno (regcache->tdesc, "ev0h"); 788 1.1 christos for (i = 0; i < 32; i++) 789 1.1 christos collect_register (regcache, ev0 + i, ®set->evr[i]); 790 1.1 christos 791 1.1 christos collect_register_by_name (regcache, "acc", ®set->acc); 792 1.1 christos collect_register_by_name (regcache, "spefscr", ®set->spefscr); 793 1.1 christos } 794 1.1 christos 795 1.1 christos static void 796 1.1 christos ppc_store_evrregset (struct regcache *regcache, const void *buf) 797 1.1 christos { 798 1.1 christos int i, ev0; 799 1.1 christos const struct gdb_evrregset_t *regset = (const struct gdb_evrregset_t *) buf; 800 1.1 christos 801 1.1 christos ev0 = find_regno (regcache->tdesc, "ev0h"); 802 1.1 christos for (i = 0; i < 32; i++) 803 1.1 christos supply_register (regcache, ev0 + i, ®set->evr[i]); 804 1.1 christos 805 1.1 christos supply_register_by_name (regcache, "acc", ®set->acc); 806 1.1 christos supply_register_by_name (regcache, "spefscr", ®set->spefscr); 807 1.1 christos } 808 1.1 christos 809 1.1 christos static struct regset_info ppc_regsets[] = { 810 1.1 christos /* List the extra register sets before GENERAL_REGS. That way we will 811 1.1 christos fetch them every time, but still fall back to PTRACE_PEEKUSER for the 812 1.1 christos general registers. Some kernels support these, but not the newer 813 1.1 christos PPC_PTRACE_GETREGS. */ 814 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CTAR, 0, EXTENDED_REGS, 815 1.1 christos NULL, ppc_store_tm_ctarregset }, 816 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CDSCR, 0, EXTENDED_REGS, 817 1.1 christos NULL, ppc_store_tm_cdscrregset }, 818 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CPPR, 0, EXTENDED_REGS, 819 1.1 christos NULL, ppc_store_tm_cpprregset }, 820 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CVSX, 0, EXTENDED_REGS, 821 1.1 christos NULL, ppc_store_tm_cvsxregset }, 822 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CVMX, 0, EXTENDED_REGS, 823 1.1 christos NULL, ppc_store_tm_cvrregset }, 824 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CFPR, 0, EXTENDED_REGS, 825 1.1 christos NULL, ppc_store_tm_cfprregset }, 826 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CGPR, 0, EXTENDED_REGS, 827 1.1 christos NULL, ppc_store_tm_cgprregset }, 828 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_SPR, 0, EXTENDED_REGS, 829 1.1 christos ppc_fill_tm_sprregset, ppc_store_tm_sprregset }, 830 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_EBB, 0, EXTENDED_REGS, 831 1.1 christos NULL, ppc_store_ebbregset }, 832 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_PMU, 0, EXTENDED_REGS, 833 1.1 christos ppc_fill_pmuregset, ppc_store_pmuregset }, 834 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TAR, 0, EXTENDED_REGS, 835 1.1 christos ppc_fill_tarregset, ppc_store_tarregset }, 836 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_PPR, 0, EXTENDED_REGS, 837 1.1 christos ppc_fill_pprregset, ppc_store_pprregset }, 838 1.1 christos { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_DSCR, 0, EXTENDED_REGS, 839 1.1 christos ppc_fill_dscrregset, ppc_store_dscrregset }, 840 1.1 christos { PTRACE_GETVSXREGS, PTRACE_SETVSXREGS, 0, 0, EXTENDED_REGS, 841 1.1 christos ppc_fill_vsxregset, ppc_store_vsxregset }, 842 1.1 christos { PTRACE_GETVRREGS, PTRACE_SETVRREGS, 0, 0, EXTENDED_REGS, 843 1.1 christos ppc_fill_vrregset, ppc_store_vrregset }, 844 1.1 christos { PTRACE_GETEVRREGS, PTRACE_SETEVRREGS, 0, 0, EXTENDED_REGS, 845 1.1 christos ppc_fill_evrregset, ppc_store_evrregset }, 846 1.1 christos { 0, 0, 0, 0, GENERAL_REGS, ppc_fill_gregset, NULL }, 847 1.1 christos NULL_REGSET 848 1.1 christos }; 849 1.1 christos 850 1.1 christos static struct usrregs_info ppc_usrregs_info = 851 1.1 christos { 852 1.1 christos ppc_num_regs, 853 1.1 christos ppc_regmap, 854 1.1 christos }; 855 1.1 christos 856 1.1 christos static struct regsets_info ppc_regsets_info = 857 1.1 christos { 858 1.1 christos ppc_regsets, /* regsets */ 859 1.1 christos 0, /* num_regsets */ 860 1.1 christos NULL, /* disabled_regsets */ 861 1.1 christos }; 862 1.1 christos 863 1.1 christos static struct regs_info myregs_info = 864 1.1 christos { 865 1.1 christos NULL, /* regset_bitmap */ 866 1.1 christos &ppc_usrregs_info, 867 1.1 christos &ppc_regsets_info 868 1.1 christos }; 869 1.1 christos 870 1.1 christos const regs_info * 871 1.1 christos ppc_target::get_regs_info () 872 1.1 christos { 873 1.1 christos return &myregs_info; 874 1.1 christos } 875 1.1 christos 876 1.1 christos void 877 1.1 christos ppc_target::low_arch_setup () 878 1.1 christos { 879 1.1 christos const struct target_desc *tdesc; 880 1.1 christos struct regset_info *regset; 881 1.1 christos struct ppc_linux_features features = ppc_linux_no_features; 882 1.1.1.4 christos int tid = current_thread->id.lwp (); 883 1.1 christos 884 1.1 christos features.wordsize = ppc_linux_target_wordsize (tid); 885 1.1 christos 886 1.1 christos if (features.wordsize == 4) 887 1.1 christos tdesc = tdesc_powerpc_32l; 888 1.1 christos else 889 1.1 christos tdesc = tdesc_powerpc_64l; 890 1.1 christos 891 1.1 christos current_process ()->tdesc = tdesc; 892 1.1 christos 893 1.1 christos /* The value of current_process ()->tdesc needs to be set for this 894 1.1 christos call. */ 895 1.1.1.3 christos ppc_hwcap = linux_get_hwcap (current_thread->id.pid (), features.wordsize); 896 1.1.1.3 christos ppc_hwcap2 = linux_get_hwcap2 (current_thread->id.pid (), features.wordsize); 897 1.1 christos 898 1.1 christos features.isa205 = ppc_linux_has_isa205 (ppc_hwcap); 899 1.1 christos 900 1.1 christos if (ppc_hwcap & PPC_FEATURE_HAS_VSX) 901 1.1 christos features.vsx = true; 902 1.1 christos 903 1.1 christos if (ppc_hwcap & PPC_FEATURE_HAS_ALTIVEC) 904 1.1 christos features.altivec = true; 905 1.1 christos 906 1.1 christos if ((ppc_hwcap2 & PPC_FEATURE2_DSCR) 907 1.1 christos && ppc_check_regset (tid, NT_PPC_DSCR, PPC_LINUX_SIZEOF_DSCRREGSET) 908 1.1 christos && ppc_check_regset (tid, NT_PPC_PPR, PPC_LINUX_SIZEOF_PPRREGSET)) 909 1.1 christos { 910 1.1 christos features.ppr_dscr = true; 911 1.1 christos if ((ppc_hwcap2 & PPC_FEATURE2_ARCH_2_07) 912 1.1 christos && (ppc_hwcap2 & PPC_FEATURE2_TAR) 913 1.1 christos && (ppc_hwcap2 & PPC_FEATURE2_EBB) 914 1.1 christos && ppc_check_regset (tid, NT_PPC_TAR, 915 1.1 christos PPC_LINUX_SIZEOF_TARREGSET) 916 1.1 christos && ppc_check_regset (tid, NT_PPC_EBB, 917 1.1 christos PPC_LINUX_SIZEOF_EBBREGSET) 918 1.1 christos && ppc_check_regset (tid, NT_PPC_PMU, 919 1.1 christos PPC_LINUX_SIZEOF_PMUREGSET)) 920 1.1 christos { 921 1.1 christos features.isa207 = true; 922 1.1 christos if ((ppc_hwcap2 & PPC_FEATURE2_HTM) 923 1.1 christos && ppc_check_regset (tid, NT_PPC_TM_SPR, 924 1.1 christos PPC_LINUX_SIZEOF_TM_SPRREGSET)) 925 1.1 christos features.htm = true; 926 1.1 christos } 927 1.1 christos } 928 1.1 christos 929 1.1 christos tdesc = ppc_linux_match_description (features); 930 1.1 christos 931 1.1 christos /* On 32-bit machines, check for SPE registers. 932 1.1 christos Set the low target's regmap field as appropriately. */ 933 1.1 christos #ifndef __powerpc64__ 934 1.1 christos if (ppc_hwcap & PPC_FEATURE_HAS_SPE) 935 1.1 christos tdesc = tdesc_powerpc_e500l; 936 1.1 christos 937 1.1 christos if (!ppc_regmap_adjusted) 938 1.1 christos { 939 1.1 christos if (ppc_hwcap & PPC_FEATURE_HAS_SPE) 940 1.1 christos ppc_usrregs_info.regmap = ppc_regmap_e500; 941 1.1 christos 942 1.1 christos /* If the FPSCR is 64-bit wide, we need to fetch the whole 943 1.1 christos 64-bit slot and not just its second word. The PT_FPSCR 944 1.1 christos supplied in a 32-bit GDB compilation doesn't reflect 945 1.1 christos this. */ 946 1.1 christos if (register_size (tdesc, 70) == 8) 947 1.1 christos ppc_regmap[70] = (48 + 2*32) * sizeof (long); 948 1.1 christos 949 1.1 christos ppc_regmap_adjusted = 1; 950 1.1 christos } 951 1.1 christos #endif 952 1.1 christos 953 1.1 christos current_process ()->tdesc = tdesc; 954 1.1 christos 955 1.1 christos for (regset = ppc_regsets; regset->size >= 0; regset++) 956 1.1 christos switch (regset->get_request) 957 1.1 christos { 958 1.1 christos case PTRACE_GETVRREGS: 959 1.1 christos regset->size = features.altivec ? PPC_LINUX_SIZEOF_VRREGSET : 0; 960 1.1 christos break; 961 1.1 christos case PTRACE_GETVSXREGS: 962 1.1 christos regset->size = features.vsx ? PPC_LINUX_SIZEOF_VSXREGSET : 0; 963 1.1 christos break; 964 1.1 christos case PTRACE_GETEVRREGS: 965 1.1 christos if (ppc_hwcap & PPC_FEATURE_HAS_SPE) 966 1.1 christos regset->size = 32 * 4 + 8 + 4; 967 1.1 christos else 968 1.1 christos regset->size = 0; 969 1.1 christos break; 970 1.1 christos case PTRACE_GETREGSET: 971 1.1 christos switch (regset->nt_type) 972 1.1 christos { 973 1.1 christos case NT_PPC_PPR: 974 1.1 christos regset->size = (features.ppr_dscr ? 975 1.1 christos PPC_LINUX_SIZEOF_PPRREGSET : 0); 976 1.1 christos break; 977 1.1 christos case NT_PPC_DSCR: 978 1.1 christos regset->size = (features.ppr_dscr ? 979 1.1 christos PPC_LINUX_SIZEOF_DSCRREGSET : 0); 980 1.1 christos break; 981 1.1 christos case NT_PPC_TAR: 982 1.1 christos regset->size = (features.isa207 ? 983 1.1 christos PPC_LINUX_SIZEOF_TARREGSET : 0); 984 1.1 christos break; 985 1.1 christos case NT_PPC_EBB: 986 1.1 christos regset->size = (features.isa207 ? 987 1.1 christos PPC_LINUX_SIZEOF_EBBREGSET : 0); 988 1.1 christos break; 989 1.1 christos case NT_PPC_PMU: 990 1.1 christos regset->size = (features.isa207 ? 991 1.1 christos PPC_LINUX_SIZEOF_PMUREGSET : 0); 992 1.1 christos break; 993 1.1 christos case NT_PPC_TM_SPR: 994 1.1 christos regset->size = (features.htm ? 995 1.1 christos PPC_LINUX_SIZEOF_TM_SPRREGSET : 0); 996 1.1 christos break; 997 1.1 christos case NT_PPC_TM_CGPR: 998 1.1 christos if (features.wordsize == 4) 999 1.1 christos regset->size = (features.htm ? 1000 1.1 christos PPC32_LINUX_SIZEOF_CGPRREGSET : 0); 1001 1.1 christos else 1002 1.1 christos regset->size = (features.htm ? 1003 1.1 christos PPC64_LINUX_SIZEOF_CGPRREGSET : 0); 1004 1.1 christos break; 1005 1.1 christos case NT_PPC_TM_CFPR: 1006 1.1 christos regset->size = (features.htm ? 1007 1.1 christos PPC_LINUX_SIZEOF_CFPRREGSET : 0); 1008 1.1 christos break; 1009 1.1 christos case NT_PPC_TM_CVMX: 1010 1.1 christos regset->size = (features.htm ? 1011 1.1 christos PPC_LINUX_SIZEOF_CVMXREGSET : 0); 1012 1.1 christos break; 1013 1.1 christos case NT_PPC_TM_CVSX: 1014 1.1 christos regset->size = (features.htm ? 1015 1.1 christos PPC_LINUX_SIZEOF_CVSXREGSET : 0); 1016 1.1 christos break; 1017 1.1 christos case NT_PPC_TM_CPPR: 1018 1.1 christos regset->size = (features.htm ? 1019 1.1 christos PPC_LINUX_SIZEOF_CPPRREGSET : 0); 1020 1.1 christos break; 1021 1.1 christos case NT_PPC_TM_CDSCR: 1022 1.1 christos regset->size = (features.htm ? 1023 1.1 christos PPC_LINUX_SIZEOF_CDSCRREGSET : 0); 1024 1.1 christos break; 1025 1.1 christos case NT_PPC_TM_CTAR: 1026 1.1 christos regset->size = (features.htm ? 1027 1.1 christos PPC_LINUX_SIZEOF_CTARREGSET : 0); 1028 1.1 christos break; 1029 1.1 christos default: 1030 1.1 christos break; 1031 1.1 christos } 1032 1.1 christos break; 1033 1.1 christos default: 1034 1.1 christos break; 1035 1.1 christos } 1036 1.1 christos } 1037 1.1 christos 1038 1.1 christos /* Implementation of target ops method "supports_tracepoints". */ 1039 1.1 christos 1040 1.1 christos bool 1041 1.1 christos ppc_target::supports_tracepoints () 1042 1.1 christos { 1043 1.1 christos return true; 1044 1.1 christos } 1045 1.1 christos 1046 1.1 christos /* Get the thread area address. This is used to recognize which 1047 1.1 christos thread is which when tracing with the in-process agent library. We 1048 1.1 christos don't read anything from the address, and treat it as opaque; it's 1049 1.1 christos the address itself that we assume is unique per-thread. */ 1050 1.1 christos 1051 1.1 christos int 1052 1.1 christos ppc_target::low_get_thread_area (int lwpid, CORE_ADDR *addr) 1053 1.1 christos { 1054 1.1 christos struct lwp_info *lwp = find_lwp_pid (ptid_t (lwpid)); 1055 1.1.1.4 christos thread_info *thr = lwp->thread; 1056 1.1.1.4 christos regcache *regcache = get_thread_regcache (thr); 1057 1.1 christos ULONGEST tp = 0; 1058 1.1 christos 1059 1.1 christos #ifdef __powerpc64__ 1060 1.1 christos if (register_size (regcache->tdesc, 0) == 8) 1061 1.1 christos collect_register_by_name (regcache, "r13", &tp); 1062 1.1 christos else 1063 1.1 christos #endif 1064 1.1 christos collect_register_by_name (regcache, "r2", &tp); 1065 1.1 christos 1066 1.1 christos *addr = tp; 1067 1.1 christos 1068 1.1 christos return 0; 1069 1.1 christos } 1070 1.1 christos 1071 1.1 christos #ifdef __powerpc64__ 1072 1.1 christos 1073 1.1 christos /* Older glibc doesn't provide this. */ 1074 1.1 christos 1075 1.1 christos #ifndef EF_PPC64_ABI 1076 1.1 christos #define EF_PPC64_ABI 3 1077 1.1 christos #endif 1078 1.1 christos 1079 1.1 christos /* Returns 1 if inferior is using ELFv2 ABI. Undefined for 32-bit 1080 1.1 christos inferiors. */ 1081 1.1 christos 1082 1.1 christos static int 1083 1.1 christos is_elfv2_inferior (void) 1084 1.1 christos { 1085 1.1 christos /* To be used as fallback if we're unable to determine the right result - 1086 1.1 christos assume inferior uses the same ABI as gdbserver. */ 1087 1.1 christos #if _CALL_ELF == 2 1088 1.1 christos const int def_res = 1; 1089 1.1 christos #else 1090 1.1 christos const int def_res = 0; 1091 1.1 christos #endif 1092 1.1 christos CORE_ADDR phdr; 1093 1.1 christos Elf64_Ehdr ehdr; 1094 1.1 christos 1095 1.1 christos const struct target_desc *tdesc = current_process ()->tdesc; 1096 1.1 christos int wordsize = register_size (tdesc, 0); 1097 1.1 christos 1098 1.1.1.3 christos if (!linux_get_auxv (current_thread->id.pid (), wordsize, AT_PHDR, &phdr)) 1099 1.1 christos return def_res; 1100 1.1 christos 1101 1.1 christos /* Assume ELF header is at the beginning of the page where program headers 1102 1.1 christos are located. If it doesn't look like one, bail. */ 1103 1.1 christos 1104 1.1 christos read_inferior_memory (phdr & ~0xfff, (unsigned char *) &ehdr, sizeof ehdr); 1105 1.1 christos if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG)) 1106 1.1 christos return def_res; 1107 1.1 christos 1108 1.1 christos return (ehdr.e_flags & EF_PPC64_ABI) == 2; 1109 1.1 christos } 1110 1.1 christos 1111 1.1 christos #endif 1112 1.1 christos 1113 1.1 christos /* Generate a ds-form instruction in BUF and return the number of bytes written 1114 1.1 christos 1115 1.1 christos 0 6 11 16 30 32 1116 1.1 christos | OPCD | RST | RA | DS |XO| */ 1117 1.1 christos 1118 1.1 christos __attribute__((unused)) /* Maybe unused due to conditional compilation. */ 1119 1.1 christos static int 1120 1.1 christos gen_ds_form (uint32_t *buf, int opcd, int rst, int ra, int ds, int xo) 1121 1.1 christos { 1122 1.1 christos uint32_t insn; 1123 1.1 christos 1124 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1125 1.1 christos gdb_assert ((rst & ~0x1f) == 0); 1126 1.1 christos gdb_assert ((ra & ~0x1f) == 0); 1127 1.1 christos gdb_assert ((xo & ~0x3) == 0); 1128 1.1 christos 1129 1.1 christos insn = (rst << 21) | (ra << 16) | (ds & 0xfffc) | (xo & 0x3); 1130 1.1 christos *buf = (opcd << 26) | insn; 1131 1.1 christos return 1; 1132 1.1 christos } 1133 1.1 christos 1134 1.1 christos /* Followings are frequently used ds-form instructions. */ 1135 1.1 christos 1136 1.1 christos #define GEN_STD(buf, rs, ra, offset) gen_ds_form (buf, 62, rs, ra, offset, 0) 1137 1.1 christos #define GEN_STDU(buf, rs, ra, offset) gen_ds_form (buf, 62, rs, ra, offset, 1) 1138 1.1 christos #define GEN_LD(buf, rt, ra, offset) gen_ds_form (buf, 58, rt, ra, offset, 0) 1139 1.1 christos #define GEN_LDU(buf, rt, ra, offset) gen_ds_form (buf, 58, rt, ra, offset, 1) 1140 1.1 christos 1141 1.1 christos /* Generate a d-form instruction in BUF. 1142 1.1 christos 1143 1.1 christos 0 6 11 16 32 1144 1.1 christos | OPCD | RST | RA | D | */ 1145 1.1 christos 1146 1.1 christos static int 1147 1.1 christos gen_d_form (uint32_t *buf, int opcd, int rst, int ra, int si) 1148 1.1 christos { 1149 1.1 christos uint32_t insn; 1150 1.1 christos 1151 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1152 1.1 christos gdb_assert ((rst & ~0x1f) == 0); 1153 1.1 christos gdb_assert ((ra & ~0x1f) == 0); 1154 1.1 christos 1155 1.1 christos insn = (rst << 21) | (ra << 16) | (si & 0xffff); 1156 1.1 christos *buf = (opcd << 26) | insn; 1157 1.1 christos return 1; 1158 1.1 christos } 1159 1.1 christos 1160 1.1 christos /* Followings are frequently used d-form instructions. */ 1161 1.1 christos 1162 1.1 christos #define GEN_ADDI(buf, rt, ra, si) gen_d_form (buf, 14, rt, ra, si) 1163 1.1 christos #define GEN_ADDIS(buf, rt, ra, si) gen_d_form (buf, 15, rt, ra, si) 1164 1.1 christos #define GEN_LI(buf, rt, si) GEN_ADDI (buf, rt, 0, si) 1165 1.1 christos #define GEN_LIS(buf, rt, si) GEN_ADDIS (buf, rt, 0, si) 1166 1.1 christos #define GEN_ORI(buf, rt, ra, si) gen_d_form (buf, 24, rt, ra, si) 1167 1.1 christos #define GEN_ORIS(buf, rt, ra, si) gen_d_form (buf, 25, rt, ra, si) 1168 1.1 christos #define GEN_LWZ(buf, rt, ra, si) gen_d_form (buf, 32, rt, ra, si) 1169 1.1 christos #define GEN_STW(buf, rt, ra, si) gen_d_form (buf, 36, rt, ra, si) 1170 1.1 christos #define GEN_STWU(buf, rt, ra, si) gen_d_form (buf, 37, rt, ra, si) 1171 1.1 christos 1172 1.1 christos /* Generate a xfx-form instruction in BUF and return the number of bytes 1173 1.1 christos written. 1174 1.1 christos 1175 1.1 christos 0 6 11 21 31 32 1176 1.1 christos | OPCD | RST | RI | XO |/| */ 1177 1.1 christos 1178 1.1 christos static int 1179 1.1 christos gen_xfx_form (uint32_t *buf, int opcd, int rst, int ri, int xo) 1180 1.1 christos { 1181 1.1 christos uint32_t insn; 1182 1.1 christos unsigned int n = ((ri & 0x1f) << 5) | ((ri >> 5) & 0x1f); 1183 1.1 christos 1184 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1185 1.1 christos gdb_assert ((rst & ~0x1f) == 0); 1186 1.1 christos gdb_assert ((xo & ~0x3ff) == 0); 1187 1.1 christos 1188 1.1 christos insn = (rst << 21) | (n << 11) | (xo << 1); 1189 1.1 christos *buf = (opcd << 26) | insn; 1190 1.1 christos return 1; 1191 1.1 christos } 1192 1.1 christos 1193 1.1 christos /* Followings are frequently used xfx-form instructions. */ 1194 1.1 christos 1195 1.1 christos #define GEN_MFSPR(buf, rt, spr) gen_xfx_form (buf, 31, rt, spr, 339) 1196 1.1 christos #define GEN_MTSPR(buf, rt, spr) gen_xfx_form (buf, 31, rt, spr, 467) 1197 1.1 christos #define GEN_MFCR(buf, rt) gen_xfx_form (buf, 31, rt, 0, 19) 1198 1.1 christos #define GEN_MTCR(buf, rt) gen_xfx_form (buf, 31, rt, 0x3cf, 144) 1199 1.1 christos #define GEN_SYNC(buf, L, E) gen_xfx_form (buf, 31, L & 0x3, \ 1200 1.1 christos E & 0xf, 598) 1201 1.1 christos #define GEN_LWSYNC(buf) GEN_SYNC (buf, 1, 0) 1202 1.1 christos 1203 1.1 christos 1204 1.1 christos /* Generate a x-form instruction in BUF and return the number of bytes written. 1205 1.1 christos 1206 1.1 christos 0 6 11 16 21 31 32 1207 1.1 christos | OPCD | RST | RA | RB | XO |RC| */ 1208 1.1 christos 1209 1.1 christos static int 1210 1.1 christos gen_x_form (uint32_t *buf, int opcd, int rst, int ra, int rb, int xo, int rc) 1211 1.1 christos { 1212 1.1 christos uint32_t insn; 1213 1.1 christos 1214 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1215 1.1 christos gdb_assert ((rst & ~0x1f) == 0); 1216 1.1 christos gdb_assert ((ra & ~0x1f) == 0); 1217 1.1 christos gdb_assert ((rb & ~0x1f) == 0); 1218 1.1 christos gdb_assert ((xo & ~0x3ff) == 0); 1219 1.1 christos gdb_assert ((rc & ~1) == 0); 1220 1.1 christos 1221 1.1 christos insn = (rst << 21) | (ra << 16) | (rb << 11) | (xo << 1) | rc; 1222 1.1 christos *buf = (opcd << 26) | insn; 1223 1.1 christos return 1; 1224 1.1 christos } 1225 1.1 christos 1226 1.1 christos /* Followings are frequently used x-form instructions. */ 1227 1.1 christos 1228 1.1 christos #define GEN_OR(buf, ra, rs, rb) gen_x_form (buf, 31, rs, ra, rb, 444, 0) 1229 1.1 christos #define GEN_MR(buf, ra, rs) GEN_OR (buf, ra, rs, rs) 1230 1.1 christos #define GEN_LWARX(buf, rt, ra, rb) gen_x_form (buf, 31, rt, ra, rb, 20, 0) 1231 1.1 christos #define GEN_STWCX(buf, rs, ra, rb) gen_x_form (buf, 31, rs, ra, rb, 150, 1) 1232 1.1 christos /* Assume bf = cr7. */ 1233 1.1 christos #define GEN_CMPW(buf, ra, rb) gen_x_form (buf, 31, 28, ra, rb, 0, 0) 1234 1.1 christos 1235 1.1 christos 1236 1.1 christos /* Generate a md-form instruction in BUF and return the number of bytes written. 1237 1.1 christos 1238 1.1 christos 0 6 11 16 21 27 30 31 32 1239 1.1 christos | OPCD | RS | RA | sh | mb | XO |sh|Rc| */ 1240 1.1 christos 1241 1.1 christos static int 1242 1.1 christos gen_md_form (uint32_t *buf, int opcd, int rs, int ra, int sh, int mb, 1243 1.1 christos int xo, int rc) 1244 1.1 christos { 1245 1.1 christos uint32_t insn; 1246 1.1 christos unsigned int n = ((mb & 0x1f) << 1) | ((mb >> 5) & 0x1); 1247 1.1 christos unsigned int sh0_4 = sh & 0x1f; 1248 1.1 christos unsigned int sh5 = (sh >> 5) & 1; 1249 1.1 christos 1250 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1251 1.1 christos gdb_assert ((rs & ~0x1f) == 0); 1252 1.1 christos gdb_assert ((ra & ~0x1f) == 0); 1253 1.1 christos gdb_assert ((sh & ~0x3f) == 0); 1254 1.1 christos gdb_assert ((mb & ~0x3f) == 0); 1255 1.1 christos gdb_assert ((xo & ~0x7) == 0); 1256 1.1 christos gdb_assert ((rc & ~0x1) == 0); 1257 1.1 christos 1258 1.1 christos insn = (rs << 21) | (ra << 16) | (sh0_4 << 11) | (n << 5) 1259 1.1 christos | (sh5 << 1) | (xo << 2) | (rc & 1); 1260 1.1 christos *buf = (opcd << 26) | insn; 1261 1.1 christos return 1; 1262 1.1 christos } 1263 1.1 christos 1264 1.1 christos /* The following are frequently used md-form instructions. */ 1265 1.1 christos 1266 1.1 christos #define GEN_RLDICL(buf, ra, rs ,sh, mb) \ 1267 1.1 christos gen_md_form (buf, 30, rs, ra, sh, mb, 0, 0) 1268 1.1 christos #define GEN_RLDICR(buf, ra, rs ,sh, mb) \ 1269 1.1 christos gen_md_form (buf, 30, rs, ra, sh, mb, 1, 0) 1270 1.1 christos 1271 1.1 christos /* Generate a i-form instruction in BUF and return the number of bytes written. 1272 1.1 christos 1273 1.1 christos 0 6 30 31 32 1274 1.1 christos | OPCD | LI |AA|LK| */ 1275 1.1 christos 1276 1.1 christos static int 1277 1.1 christos gen_i_form (uint32_t *buf, int opcd, int li, int aa, int lk) 1278 1.1 christos { 1279 1.1 christos uint32_t insn; 1280 1.1 christos 1281 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1282 1.1 christos 1283 1.1 christos insn = (li & 0x3fffffc) | (aa & 1) | (lk & 1); 1284 1.1 christos *buf = (opcd << 26) | insn; 1285 1.1 christos return 1; 1286 1.1 christos } 1287 1.1 christos 1288 1.1 christos /* The following are frequently used i-form instructions. */ 1289 1.1 christos 1290 1.1 christos #define GEN_B(buf, li) gen_i_form (buf, 18, li, 0, 0) 1291 1.1 christos #define GEN_BL(buf, li) gen_i_form (buf, 18, li, 0, 1) 1292 1.1 christos 1293 1.1 christos /* Generate a b-form instruction in BUF and return the number of bytes written. 1294 1.1 christos 1295 1.1 christos 0 6 11 16 30 31 32 1296 1.1 christos | OPCD | BO | BI | BD |AA|LK| */ 1297 1.1 christos 1298 1.1 christos static int 1299 1.1 christos gen_b_form (uint32_t *buf, int opcd, int bo, int bi, int bd, 1300 1.1 christos int aa, int lk) 1301 1.1 christos { 1302 1.1 christos uint32_t insn; 1303 1.1 christos 1304 1.1 christos gdb_assert ((opcd & ~0x3f) == 0); 1305 1.1 christos gdb_assert ((bo & ~0x1f) == 0); 1306 1.1 christos gdb_assert ((bi & ~0x1f) == 0); 1307 1.1 christos 1308 1.1 christos insn = (bo << 21) | (bi << 16) | (bd & 0xfffc) | (aa & 1) | (lk & 1); 1309 1.1 christos *buf = (opcd << 26) | insn; 1310 1.1 christos return 1; 1311 1.1 christos } 1312 1.1 christos 1313 1.1 christos /* The following are frequently used b-form instructions. */ 1314 1.1 christos /* Assume bi = cr7. */ 1315 1.1 christos #define GEN_BNE(buf, bd) gen_b_form (buf, 16, 0x4, (7 << 2) | 2, bd, 0 ,0) 1316 1.1 christos 1317 1.1 christos /* GEN_LOAD and GEN_STORE generate 64- or 32-bit load/store for ppc64 or ppc32 1318 1.1 christos respectively. They are primary used for save/restore GPRs in jump-pad, 1319 1.1 christos not used for bytecode compiling. */ 1320 1.1 christos 1321 1.1 christos #ifdef __powerpc64__ 1322 1.1 christos #define GEN_LOAD(buf, rt, ra, si, is_64) (is_64 ? \ 1323 1.1 christos GEN_LD (buf, rt, ra, si) : \ 1324 1.1 christos GEN_LWZ (buf, rt, ra, si)) 1325 1.1 christos #define GEN_STORE(buf, rt, ra, si, is_64) (is_64 ? \ 1326 1.1 christos GEN_STD (buf, rt, ra, si) : \ 1327 1.1 christos GEN_STW (buf, rt, ra, si)) 1328 1.1 christos #else 1329 1.1 christos #define GEN_LOAD(buf, rt, ra, si, is_64) GEN_LWZ (buf, rt, ra, si) 1330 1.1 christos #define GEN_STORE(buf, rt, ra, si, is_64) GEN_STW (buf, rt, ra, si) 1331 1.1 christos #endif 1332 1.1 christos 1333 1.1 christos /* Generate a sequence of instructions to load IMM in the register REG. 1334 1.1 christos Write the instructions in BUF and return the number of bytes written. */ 1335 1.1 christos 1336 1.1 christos static int 1337 1.1 christos gen_limm (uint32_t *buf, int reg, uint64_t imm, int is_64) 1338 1.1 christos { 1339 1.1 christos uint32_t *p = buf; 1340 1.1 christos 1341 1.1 christos if ((imm + 32768) < 65536) 1342 1.1 christos { 1343 1.1 christos /* li reg, imm[15:0] */ 1344 1.1 christos p += GEN_LI (p, reg, imm); 1345 1.1 christos } 1346 1.1 christos else if ((imm >> 32) == 0) 1347 1.1 christos { 1348 1.1 christos /* lis reg, imm[31:16] 1349 1.1 christos ori reg, reg, imm[15:0] 1350 1.1 christos rldicl reg, reg, 0, 32 */ 1351 1.1 christos p += GEN_LIS (p, reg, (imm >> 16) & 0xffff); 1352 1.1 christos if ((imm & 0xffff) != 0) 1353 1.1 christos p += GEN_ORI (p, reg, reg, imm & 0xffff); 1354 1.1 christos /* Clear upper 32-bit if sign-bit is set. */ 1355 1.1 christos if (imm & (1u << 31) && is_64) 1356 1.1 christos p += GEN_RLDICL (p, reg, reg, 0, 32); 1357 1.1 christos } 1358 1.1 christos else 1359 1.1 christos { 1360 1.1 christos gdb_assert (is_64); 1361 1.1 christos /* lis reg, <imm[63:48]> 1362 1.1 christos ori reg, reg, <imm[48:32]> 1363 1.1 christos rldicr reg, reg, 32, 31 1364 1.1 christos oris reg, reg, <imm[31:16]> 1365 1.1 christos ori reg, reg, <imm[15:0]> */ 1366 1.1 christos p += GEN_LIS (p, reg, ((imm >> 48) & 0xffff)); 1367 1.1 christos if (((imm >> 32) & 0xffff) != 0) 1368 1.1.1.2 christos p += GEN_ORI (p, reg, reg, ((imm >> 32) & 0xffff)); 1369 1.1 christos p += GEN_RLDICR (p, reg, reg, 32, 31); 1370 1.1 christos if (((imm >> 16) & 0xffff) != 0) 1371 1.1.1.2 christos p += GEN_ORIS (p, reg, reg, ((imm >> 16) & 0xffff)); 1372 1.1 christos if ((imm & 0xffff) != 0) 1373 1.1.1.2 christos p += GEN_ORI (p, reg, reg, (imm & 0xffff)); 1374 1.1 christos } 1375 1.1 christos 1376 1.1 christos return p - buf; 1377 1.1 christos } 1378 1.1 christos 1379 1.1 christos /* Generate a sequence for atomically exchange at location LOCK. 1380 1.1 christos This code sequence clobbers r6, r7, r8. LOCK is the location for 1381 1.1 christos the atomic-xchg, OLD_VALUE is expected old value stored in the 1382 1.1 christos location, and R_NEW is a register for the new value. */ 1383 1.1 christos 1384 1.1 christos static int 1385 1.1 christos gen_atomic_xchg (uint32_t *buf, CORE_ADDR lock, int old_value, int r_new, 1386 1.1 christos int is_64) 1387 1.1 christos { 1388 1.1 christos const int r_lock = 6; 1389 1.1 christos const int r_old = 7; 1390 1.1 christos const int r_tmp = 8; 1391 1.1 christos uint32_t *p = buf; 1392 1.1 christos 1393 1.1 christos /* 1394 1.1 christos 1: lwarx TMP, 0, LOCK 1395 1.1 christos cmpwi TMP, OLD 1396 1.1 christos bne 1b 1397 1.1 christos stwcx. NEW, 0, LOCK 1398 1.1 christos bne 1b */ 1399 1.1 christos 1400 1.1 christos p += gen_limm (p, r_lock, lock, is_64); 1401 1.1 christos p += gen_limm (p, r_old, old_value, is_64); 1402 1.1 christos 1403 1.1 christos p += GEN_LWARX (p, r_tmp, 0, r_lock); 1404 1.1 christos p += GEN_CMPW (p, r_tmp, r_old); 1405 1.1 christos p += GEN_BNE (p, -8); 1406 1.1 christos p += GEN_STWCX (p, r_new, 0, r_lock); 1407 1.1 christos p += GEN_BNE (p, -16); 1408 1.1 christos 1409 1.1 christos return p - buf; 1410 1.1 christos } 1411 1.1 christos 1412 1.1 christos /* Generate a sequence of instructions for calling a function 1413 1.1 christos at address of FN. Return the number of bytes are written in BUF. */ 1414 1.1 christos 1415 1.1 christos static int 1416 1.1 christos gen_call (uint32_t *buf, CORE_ADDR fn, int is_64, int is_opd) 1417 1.1 christos { 1418 1.1 christos uint32_t *p = buf; 1419 1.1 christos 1420 1.1 christos /* Must be called by r12 for caller to calculate TOC address. */ 1421 1.1 christos p += gen_limm (p, 12, fn, is_64); 1422 1.1 christos if (is_opd) 1423 1.1 christos { 1424 1.1 christos p += GEN_LOAD (p, 11, 12, 16, is_64); 1425 1.1 christos p += GEN_LOAD (p, 2, 12, 8, is_64); 1426 1.1 christos p += GEN_LOAD (p, 12, 12, 0, is_64); 1427 1.1 christos } 1428 1.1 christos p += GEN_MTSPR (p, 12, 9); /* mtctr r12 */ 1429 1.1 christos *p++ = 0x4e800421; /* bctrl */ 1430 1.1 christos 1431 1.1 christos return p - buf; 1432 1.1 christos } 1433 1.1 christos 1434 1.1 christos /* Copy the instruction from OLDLOC to *TO, and update *TO to *TO + size 1435 1.1 christos of instruction. This function is used to adjust pc-relative instructions 1436 1.1 christos when copying. */ 1437 1.1 christos 1438 1.1 christos static void 1439 1.1 christos ppc_relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc) 1440 1.1 christos { 1441 1.1 christos uint32_t insn, op6; 1442 1.1 christos long rel, newrel; 1443 1.1 christos 1444 1.1 christos read_inferior_memory (oldloc, (unsigned char *) &insn, 4); 1445 1.1 christos op6 = PPC_OP6 (insn); 1446 1.1 christos 1447 1.1 christos if (op6 == 18 && (insn & 2) == 0) 1448 1.1 christos { 1449 1.1 christos /* branch && AA = 0 */ 1450 1.1 christos rel = PPC_LI (insn); 1451 1.1 christos newrel = (oldloc - *to) + rel; 1452 1.1 christos 1453 1.1 christos /* Out of range. Cannot relocate instruction. */ 1454 1.1 christos if (newrel >= (1 << 25) || newrel < -(1 << 25)) 1455 1.1 christos return; 1456 1.1 christos 1457 1.1 christos insn = (insn & ~0x3fffffc) | (newrel & 0x3fffffc); 1458 1.1 christos } 1459 1.1 christos else if (op6 == 16 && (insn & 2) == 0) 1460 1.1 christos { 1461 1.1 christos /* conditional branch && AA = 0 */ 1462 1.1 christos 1463 1.1 christos /* If the new relocation is too big for even a 26-bit unconditional 1464 1.1 christos branch, there is nothing we can do. Just abort. 1465 1.1 christos 1466 1.1 christos Otherwise, if it can be fit in 16-bit conditional branch, just 1467 1.1 christos copy the instruction and relocate the address. 1468 1.1 christos 1469 1.1 christos If the it's big for conditional-branch (16-bit), try to invert the 1470 1.1 christos condition and jump with 26-bit branch. For example, 1471 1.1 christos 1472 1.1 christos beq .Lgoto 1473 1.1 christos INSN1 1474 1.1 christos 1475 1.1 christos => 1476 1.1 christos 1477 1.1 christos bne 1f (+8) 1478 1.1 christos b .Lgoto 1479 1.1 christos 1:INSN1 1480 1.1 christos 1481 1.1 christos After this transform, we are actually jump from *TO+4 instead of *TO, 1482 1.1 christos so check the relocation again because it will be 1-insn farther then 1483 1.1 christos before if *TO is after OLDLOC. 1484 1.1 christos 1485 1.1 christos 1486 1.1 christos For BDNZT (or so) is transformed from 1487 1.1 christos 1488 1.1 christos bdnzt eq, .Lgoto 1489 1.1 christos INSN1 1490 1.1 christos 1491 1.1 christos => 1492 1.1 christos 1493 1.1 christos bdz 1f (+12) 1494 1.1 christos bf eq, 1f (+8) 1495 1.1 christos b .Lgoto 1496 1.1 christos 1:INSN1 1497 1.1 christos 1498 1.1 christos See also "BO field encodings". */ 1499 1.1 christos 1500 1.1 christos rel = PPC_BD (insn); 1501 1.1 christos newrel = (oldloc - *to) + rel; 1502 1.1 christos 1503 1.1 christos if (newrel < (1 << 15) && newrel >= -(1 << 15)) 1504 1.1 christos insn = (insn & ~0xfffc) | (newrel & 0xfffc); 1505 1.1 christos else if ((PPC_BO (insn) & 0x14) == 0x4 || (PPC_BO (insn) & 0x14) == 0x10) 1506 1.1 christos { 1507 1.1 christos newrel -= 4; 1508 1.1 christos 1509 1.1 christos /* Out of range. Cannot relocate instruction. */ 1510 1.1 christos if (newrel >= (1 << 25) || newrel < -(1 << 25)) 1511 1.1 christos return; 1512 1.1 christos 1513 1.1 christos if ((PPC_BO (insn) & 0x14) == 0x4) 1514 1.1 christos insn ^= (1 << 24); 1515 1.1 christos else if ((PPC_BO (insn) & 0x14) == 0x10) 1516 1.1 christos insn ^= (1 << 22); 1517 1.1 christos 1518 1.1 christos /* Jump over the unconditional branch. */ 1519 1.1 christos insn = (insn & ~0xfffc) | 0x8; 1520 1.1 christos target_write_memory (*to, (unsigned char *) &insn, 4); 1521 1.1 christos *to += 4; 1522 1.1 christos 1523 1.1 christos /* Build a unconditional branch and copy LK bit. */ 1524 1.1 christos insn = (18 << 26) | (0x3fffffc & newrel) | (insn & 0x3); 1525 1.1 christos target_write_memory (*to, (unsigned char *) &insn, 4); 1526 1.1 christos *to += 4; 1527 1.1 christos 1528 1.1 christos return; 1529 1.1 christos } 1530 1.1 christos else if ((PPC_BO (insn) & 0x14) == 0) 1531 1.1 christos { 1532 1.1 christos uint32_t bdnz_insn = (16 << 26) | (0x10 << 21) | 12; 1533 1.1 christos uint32_t bf_insn = (16 << 26) | (0x4 << 21) | 8; 1534 1.1 christos 1535 1.1 christos newrel -= 8; 1536 1.1 christos 1537 1.1 christos /* Out of range. Cannot relocate instruction. */ 1538 1.1 christos if (newrel >= (1 << 25) || newrel < -(1 << 25)) 1539 1.1 christos return; 1540 1.1 christos 1541 1.1 christos /* Copy BI field. */ 1542 1.1 christos bf_insn |= (insn & 0x1f0000); 1543 1.1 christos 1544 1.1 christos /* Invert condition. */ 1545 1.1 christos bdnz_insn |= (insn ^ (1 << 22)) & (1 << 22); 1546 1.1 christos bf_insn |= (insn ^ (1 << 24)) & (1 << 24); 1547 1.1 christos 1548 1.1 christos target_write_memory (*to, (unsigned char *) &bdnz_insn, 4); 1549 1.1 christos *to += 4; 1550 1.1 christos target_write_memory (*to, (unsigned char *) &bf_insn, 4); 1551 1.1 christos *to += 4; 1552 1.1 christos 1553 1.1 christos /* Build a unconditional branch and copy LK bit. */ 1554 1.1 christos insn = (18 << 26) | (0x3fffffc & newrel) | (insn & 0x3); 1555 1.1 christos target_write_memory (*to, (unsigned char *) &insn, 4); 1556 1.1 christos *to += 4; 1557 1.1 christos 1558 1.1 christos return; 1559 1.1 christos } 1560 1.1 christos else /* (BO & 0x14) == 0x14, branch always. */ 1561 1.1 christos { 1562 1.1 christos /* Out of range. Cannot relocate instruction. */ 1563 1.1 christos if (newrel >= (1 << 25) || newrel < -(1 << 25)) 1564 1.1 christos return; 1565 1.1 christos 1566 1.1 christos /* Build a unconditional branch and copy LK bit. */ 1567 1.1 christos insn = (18 << 26) | (0x3fffffc & newrel) | (insn & 0x3); 1568 1.1 christos target_write_memory (*to, (unsigned char *) &insn, 4); 1569 1.1 christos *to += 4; 1570 1.1 christos 1571 1.1 christos return; 1572 1.1 christos } 1573 1.1 christos } 1574 1.1 christos 1575 1.1 christos target_write_memory (*to, (unsigned char *) &insn, 4); 1576 1.1 christos *to += 4; 1577 1.1 christos } 1578 1.1 christos 1579 1.1 christos bool 1580 1.1 christos ppc_target::supports_fast_tracepoints () 1581 1.1 christos { 1582 1.1 christos return true; 1583 1.1 christos } 1584 1.1 christos 1585 1.1 christos /* Implement install_fast_tracepoint_jump_pad of target_ops. 1586 1.1 christos See target.h for details. */ 1587 1.1 christos 1588 1.1 christos int 1589 1.1 christos ppc_target::install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, 1590 1.1 christos CORE_ADDR tpaddr, 1591 1.1 christos CORE_ADDR collector, 1592 1.1 christos CORE_ADDR lockaddr, 1593 1.1 christos ULONGEST orig_size, 1594 1.1 christos CORE_ADDR *jump_entry, 1595 1.1 christos CORE_ADDR *trampoline, 1596 1.1 christos ULONGEST *trampoline_size, 1597 1.1 christos unsigned char *jjump_pad_insn, 1598 1.1 christos ULONGEST *jjump_pad_insn_size, 1599 1.1 christos CORE_ADDR *adjusted_insn_addr, 1600 1.1 christos CORE_ADDR *adjusted_insn_addr_end, 1601 1.1 christos char *err) 1602 1.1 christos { 1603 1.1 christos uint32_t buf[256]; 1604 1.1 christos uint32_t *p = buf; 1605 1.1 christos int j, offset; 1606 1.1 christos CORE_ADDR buildaddr = *jump_entry; 1607 1.1 christos const CORE_ADDR entryaddr = *jump_entry; 1608 1.1 christos int rsz, min_frame, frame_size, tp_reg; 1609 1.1 christos #ifdef __powerpc64__ 1610 1.1.1.4 christos int is_64 = register_size (current_process ()->tdesc, 0) == 8; 1611 1.1 christos int is_opd = is_64 && !is_elfv2_inferior (); 1612 1.1 christos #else 1613 1.1 christos int is_64 = 0, is_opd = 0; 1614 1.1 christos #endif 1615 1.1 christos 1616 1.1 christos #ifdef __powerpc64__ 1617 1.1 christos if (is_64) 1618 1.1 christos { 1619 1.1 christos /* Minimum frame size is 32 bytes for ELFv2, and 112 bytes for ELFv1. */ 1620 1.1 christos rsz = 8; 1621 1.1 christos min_frame = 112; 1622 1.1 christos frame_size = (40 * rsz) + min_frame; 1623 1.1 christos tp_reg = 13; 1624 1.1 christos } 1625 1.1 christos else 1626 1.1 christos { 1627 1.1 christos #endif 1628 1.1 christos rsz = 4; 1629 1.1 christos min_frame = 16; 1630 1.1 christos frame_size = (40 * rsz) + min_frame; 1631 1.1 christos tp_reg = 2; 1632 1.1 christos #ifdef __powerpc64__ 1633 1.1 christos } 1634 1.1 christos #endif 1635 1.1 christos 1636 1.1 christos /* Stack frame layout for this jump pad, 1637 1.1 christos 1638 1.1 christos High thread_area (r13/r2) | 1639 1.1 christos tpoint - collecting_t obj 1640 1.1 christos PC/<tpaddr> | +36 1641 1.1 christos CTR | +35 1642 1.1 christos LR | +34 1643 1.1 christos XER | +33 1644 1.1 christos CR | +32 1645 1.1 christos R31 | 1646 1.1 christos R29 | 1647 1.1 christos ... | 1648 1.1 christos R1 | +1 1649 1.1 christos R0 - collected registers 1650 1.1 christos ... | 1651 1.1 christos ... | 1652 1.1 christos Low Back-chain - 1653 1.1 christos 1654 1.1 christos 1655 1.1 christos The code flow of this jump pad, 1656 1.1 christos 1657 1.1 christos 1. Adjust SP 1658 1.1 christos 2. Save GPR and SPR 1659 1.1 christos 3. Prepare argument 1660 1.1 christos 4. Call gdb_collector 1661 1.1 christos 5. Restore GPR and SPR 1662 1.1 christos 6. Restore SP 1663 1.1 christos 7. Build a jump for back to the program 1664 1.1 christos 8. Copy/relocate original instruction 1665 1.1 christos 9. Build a jump for replacing original instruction. */ 1666 1.1 christos 1667 1.1 christos /* Adjust stack pointer. */ 1668 1.1 christos if (is_64) 1669 1.1 christos p += GEN_STDU (p, 1, 1, -frame_size); /* stdu r1,-frame_size(r1) */ 1670 1.1 christos else 1671 1.1 christos p += GEN_STWU (p, 1, 1, -frame_size); /* stwu r1,-frame_size(r1) */ 1672 1.1 christos 1673 1.1 christos /* Store GPRs. Save R1 later, because it had just been modified, but 1674 1.1 christos we want the original value. */ 1675 1.1 christos for (j = 2; j < 32; j++) 1676 1.1 christos p += GEN_STORE (p, j, 1, min_frame + j * rsz, is_64); 1677 1.1 christos p += GEN_STORE (p, 0, 1, min_frame + 0 * rsz, is_64); 1678 1.1 christos /* Set r0 to the original value of r1 before adjusting stack frame, 1679 1.1 christos and then save it. */ 1680 1.1 christos p += GEN_ADDI (p, 0, 1, frame_size); 1681 1.1 christos p += GEN_STORE (p, 0, 1, min_frame + 1 * rsz, is_64); 1682 1.1 christos 1683 1.1 christos /* Save CR, XER, LR, and CTR. */ 1684 1.1 christos p += GEN_MFCR (p, 3); /* mfcr r3 */ 1685 1.1 christos p += GEN_MFSPR (p, 4, 1); /* mfxer r4 */ 1686 1.1 christos p += GEN_MFSPR (p, 5, 8); /* mflr r5 */ 1687 1.1 christos p += GEN_MFSPR (p, 6, 9); /* mfctr r6 */ 1688 1.1 christos p += GEN_STORE (p, 3, 1, min_frame + 32 * rsz, is_64);/* std r3, 32(r1) */ 1689 1.1 christos p += GEN_STORE (p, 4, 1, min_frame + 33 * rsz, is_64);/* std r4, 33(r1) */ 1690 1.1 christos p += GEN_STORE (p, 5, 1, min_frame + 34 * rsz, is_64);/* std r5, 34(r1) */ 1691 1.1 christos p += GEN_STORE (p, 6, 1, min_frame + 35 * rsz, is_64);/* std r6, 35(r1) */ 1692 1.1 christos 1693 1.1 christos /* Save PC<tpaddr> */ 1694 1.1 christos p += gen_limm (p, 3, tpaddr, is_64); 1695 1.1 christos p += GEN_STORE (p, 3, 1, min_frame + 36 * rsz, is_64); 1696 1.1 christos 1697 1.1 christos 1698 1.1 christos /* Setup arguments to collector. */ 1699 1.1 christos /* Set r4 to collected registers. */ 1700 1.1 christos p += GEN_ADDI (p, 4, 1, min_frame); 1701 1.1 christos /* Set r3 to TPOINT. */ 1702 1.1 christos p += gen_limm (p, 3, tpoint, is_64); 1703 1.1 christos 1704 1.1 christos /* Prepare collecting_t object for lock. */ 1705 1.1 christos p += GEN_STORE (p, 3, 1, min_frame + 37 * rsz, is_64); 1706 1.1 christos p += GEN_STORE (p, tp_reg, 1, min_frame + 38 * rsz, is_64); 1707 1.1 christos /* Set R5 to collecting object. */ 1708 1.1 christos p += GEN_ADDI (p, 5, 1, 37 * rsz); 1709 1.1 christos 1710 1.1 christos p += GEN_LWSYNC (p); 1711 1.1 christos p += gen_atomic_xchg (p, lockaddr, 0, 5, is_64); 1712 1.1 christos p += GEN_LWSYNC (p); 1713 1.1 christos 1714 1.1 christos /* Call to collector. */ 1715 1.1 christos p += gen_call (p, collector, is_64, is_opd); 1716 1.1 christos 1717 1.1 christos /* Simply write 0 to release the lock. */ 1718 1.1 christos p += gen_limm (p, 3, lockaddr, is_64); 1719 1.1 christos p += gen_limm (p, 4, 0, is_64); 1720 1.1 christos p += GEN_LWSYNC (p); 1721 1.1 christos p += GEN_STORE (p, 4, 3, 0, is_64); 1722 1.1 christos 1723 1.1 christos /* Restore stack and registers. */ 1724 1.1 christos p += GEN_LOAD (p, 3, 1, min_frame + 32 * rsz, is_64); /* ld r3, 32(r1) */ 1725 1.1 christos p += GEN_LOAD (p, 4, 1, min_frame + 33 * rsz, is_64); /* ld r4, 33(r1) */ 1726 1.1 christos p += GEN_LOAD (p, 5, 1, min_frame + 34 * rsz, is_64); /* ld r5, 34(r1) */ 1727 1.1 christos p += GEN_LOAD (p, 6, 1, min_frame + 35 * rsz, is_64); /* ld r6, 35(r1) */ 1728 1.1 christos p += GEN_MTCR (p, 3); /* mtcr r3 */ 1729 1.1 christos p += GEN_MTSPR (p, 4, 1); /* mtxer r4 */ 1730 1.1 christos p += GEN_MTSPR (p, 5, 8); /* mtlr r5 */ 1731 1.1 christos p += GEN_MTSPR (p, 6, 9); /* mtctr r6 */ 1732 1.1 christos 1733 1.1 christos /* Restore GPRs. */ 1734 1.1 christos for (j = 2; j < 32; j++) 1735 1.1 christos p += GEN_LOAD (p, j, 1, min_frame + j * rsz, is_64); 1736 1.1 christos p += GEN_LOAD (p, 0, 1, min_frame + 0 * rsz, is_64); 1737 1.1 christos /* Restore SP. */ 1738 1.1 christos p += GEN_ADDI (p, 1, 1, frame_size); 1739 1.1 christos 1740 1.1 christos /* Flush instructions to inferior memory. */ 1741 1.1 christos target_write_memory (buildaddr, (unsigned char *) buf, (p - buf) * 4); 1742 1.1 christos 1743 1.1 christos /* Now, insert the original instruction to execute in the jump pad. */ 1744 1.1 christos *adjusted_insn_addr = buildaddr + (p - buf) * 4; 1745 1.1 christos *adjusted_insn_addr_end = *adjusted_insn_addr; 1746 1.1 christos ppc_relocate_instruction (adjusted_insn_addr_end, tpaddr); 1747 1.1 christos 1748 1.1 christos /* Verify the relocation size. If should be 4 for normal copy, 1749 1.1 christos 8 or 12 for some conditional branch. */ 1750 1.1 christos if ((*adjusted_insn_addr_end - *adjusted_insn_addr == 0) 1751 1.1 christos || (*adjusted_insn_addr_end - *adjusted_insn_addr > 12)) 1752 1.1 christos { 1753 1.1 christos sprintf (err, "E.Unexpected instruction length = %d" 1754 1.1 christos "when relocate instruction.", 1755 1.1 christos (int) (*adjusted_insn_addr_end - *adjusted_insn_addr)); 1756 1.1 christos return 1; 1757 1.1 christos } 1758 1.1 christos 1759 1.1 christos buildaddr = *adjusted_insn_addr_end; 1760 1.1 christos p = buf; 1761 1.1 christos /* Finally, write a jump back to the program. */ 1762 1.1 christos offset = (tpaddr + 4) - buildaddr; 1763 1.1 christos if (offset >= (1 << 25) || offset < -(1 << 25)) 1764 1.1 christos { 1765 1.1 christos sprintf (err, "E.Jump back from jump pad too far from tracepoint " 1766 1.1 christos "(offset 0x%x > 26-bit).", offset); 1767 1.1 christos return 1; 1768 1.1 christos } 1769 1.1 christos /* b <tpaddr+4> */ 1770 1.1 christos p += GEN_B (p, offset); 1771 1.1 christos target_write_memory (buildaddr, (unsigned char *) buf, (p - buf) * 4); 1772 1.1 christos *jump_entry = buildaddr + (p - buf) * 4; 1773 1.1 christos 1774 1.1 christos /* The jump pad is now built. Wire in a jump to our jump pad. This 1775 1.1 christos is always done last (by our caller actually), so that we can 1776 1.1 christos install fast tracepoints with threads running. This relies on 1777 1.1 christos the agent's atomic write support. */ 1778 1.1 christos offset = entryaddr - tpaddr; 1779 1.1 christos if (offset >= (1 << 25) || offset < -(1 << 25)) 1780 1.1 christos { 1781 1.1 christos sprintf (err, "E.Jump back from jump pad too far from tracepoint " 1782 1.1 christos "(offset 0x%x > 26-bit).", offset); 1783 1.1 christos return 1; 1784 1.1 christos } 1785 1.1 christos /* b <jentry> */ 1786 1.1 christos GEN_B ((uint32_t *) jjump_pad_insn, offset); 1787 1.1 christos *jjump_pad_insn_size = 4; 1788 1.1 christos 1789 1.1 christos return 0; 1790 1.1 christos } 1791 1.1 christos 1792 1.1 christos /* Returns the minimum instruction length for installing a tracepoint. */ 1793 1.1 christos 1794 1.1 christos int 1795 1.1 christos ppc_target::get_min_fast_tracepoint_insn_len () 1796 1.1 christos { 1797 1.1 christos return 4; 1798 1.1 christos } 1799 1.1 christos 1800 1.1 christos /* Emits a given buffer into the target at current_insn_ptr. Length 1801 1.1 christos is in units of 32-bit words. */ 1802 1.1 christos 1803 1.1 christos static void 1804 1.1 christos emit_insns (uint32_t *buf, int n) 1805 1.1 christos { 1806 1.1 christos n = n * sizeof (uint32_t); 1807 1.1 christos target_write_memory (current_insn_ptr, (unsigned char *) buf, n); 1808 1.1 christos current_insn_ptr += n; 1809 1.1 christos } 1810 1.1 christos 1811 1.1 christos #define __EMIT_ASM(NAME, INSNS) \ 1812 1.1 christos do \ 1813 1.1 christos { \ 1814 1.1 christos extern uint32_t start_bcax_ ## NAME []; \ 1815 1.1 christos extern uint32_t end_bcax_ ## NAME []; \ 1816 1.1 christos emit_insns (start_bcax_ ## NAME, \ 1817 1.1 christos end_bcax_ ## NAME - start_bcax_ ## NAME); \ 1818 1.1 christos __asm__ (".section .text.__ppcbcax\n\t" \ 1819 1.1 christos "start_bcax_" #NAME ":\n\t" \ 1820 1.1 christos INSNS "\n\t" \ 1821 1.1 christos "end_bcax_" #NAME ":\n\t" \ 1822 1.1 christos ".previous\n\t"); \ 1823 1.1 christos } while (0) 1824 1.1 christos 1825 1.1 christos #define _EMIT_ASM(NAME, INSNS) __EMIT_ASM (NAME, INSNS) 1826 1.1 christos #define EMIT_ASM(INSNS) _EMIT_ASM (__LINE__, INSNS) 1827 1.1 christos 1828 1.1 christos /* 1829 1.1 christos 1830 1.1 christos Bytecode execution stack frame - 32-bit 1831 1.1 christos 1832 1.1 christos | LR save area (SP + 4) 1833 1.1 christos SP' -> +- Back chain (SP + 0) 1834 1.1 christos | Save r31 for access saved arguments 1835 1.1 christos | Save r30 for bytecode stack pointer 1836 1.1 christos | Save r4 for incoming argument *value 1837 1.1 christos | Save r3 for incoming argument regs 1838 1.1 christos r30 -> +- Bytecode execution stack 1839 1.1 christos | 1840 1.1 christos | 64-byte (8 doublewords) at initial. 1841 1.1 christos | Expand stack as needed. 1842 1.1 christos | 1843 1.1 christos +- 1844 1.1.1.2 christos | Some padding for minimum stack frame and 16-byte alignment. 1845 1.1.1.2 christos | 16 bytes. 1846 1.1 christos SP +- Back-chain (SP') 1847 1.1 christos 1848 1.1 christos initial frame size 1849 1.1 christos = 16 + (4 * 4) + 64 1850 1.1 christos = 96 1851 1.1 christos 1852 1.1 christos r30 is the stack-pointer for bytecode machine. 1853 1.1 christos It should point to next-empty, so we can use LDU for pop. 1854 1.1 christos r3 is used for cache of the high part of TOP value. 1855 1.1 christos It was the first argument, pointer to regs. 1856 1.1 christos r4 is used for cache of the low part of TOP value. 1857 1.1 christos It was the second argument, pointer to the result. 1858 1.1 christos We should set *result = TOP after leaving this function. 1859 1.1 christos 1860 1.1 christos Note: 1861 1.1 christos * To restore stack at epilogue 1862 1.1 christos => sp = r31 1863 1.1 christos * To check stack is big enough for bytecode execution. 1864 1.1 christos => r30 - 8 > SP + 8 1865 1.1 christos * To return execution result. 1866 1.1 christos => 0(r4) = TOP 1867 1.1 christos 1868 1.1 christos */ 1869 1.1 christos 1870 1.1 christos /* Regardless of endian, register 3 is always high part, 4 is low part. 1871 1.1 christos These defines are used when the register pair is stored/loaded. 1872 1.1.1.3 christos Likewise, to simplify code, have a similar define for 5:6. */ 1873 1.1 christos 1874 1.1 christos #if __BYTE_ORDER == __LITTLE_ENDIAN 1875 1.1 christos #define TOP_FIRST "4" 1876 1.1 christos #define TOP_SECOND "3" 1877 1.1 christos #define TMP_FIRST "6" 1878 1.1 christos #define TMP_SECOND "5" 1879 1.1 christos #else 1880 1.1 christos #define TOP_FIRST "3" 1881 1.1 christos #define TOP_SECOND "4" 1882 1.1 christos #define TMP_FIRST "5" 1883 1.1 christos #define TMP_SECOND "6" 1884 1.1 christos #endif 1885 1.1 christos 1886 1.1 christos /* Emit prologue in inferior memory. See above comments. */ 1887 1.1 christos 1888 1.1 christos static void 1889 1.1 christos ppc_emit_prologue (void) 1890 1.1 christos { 1891 1.1 christos EMIT_ASM (/* Save return address. */ 1892 1.1 christos "mflr 0 \n" 1893 1.1 christos "stw 0, 4(1) \n" 1894 1.1 christos /* Adjust SP. 96 is the initial frame size. */ 1895 1.1 christos "stwu 1, -96(1) \n" 1896 1.1 christos /* Save r30 and incoming arguments. */ 1897 1.1 christos "stw 31, 96-4(1) \n" 1898 1.1 christos "stw 30, 96-8(1) \n" 1899 1.1 christos "stw 4, 96-12(1) \n" 1900 1.1 christos "stw 3, 96-16(1) \n" 1901 1.1 christos /* Point r31 to original r1 for access arguments. */ 1902 1.1 christos "addi 31, 1, 96 \n" 1903 1.1 christos /* Set r30 to pointing stack-top. */ 1904 1.1 christos "addi 30, 1, 64 \n" 1905 1.1 christos /* Initial r3/TOP to 0. */ 1906 1.1 christos "li 3, 0 \n" 1907 1.1 christos "li 4, 0 \n"); 1908 1.1 christos } 1909 1.1 christos 1910 1.1 christos /* Emit epilogue in inferior memory. See above comments. */ 1911 1.1 christos 1912 1.1 christos static void 1913 1.1 christos ppc_emit_epilogue (void) 1914 1.1 christos { 1915 1.1 christos EMIT_ASM (/* *result = TOP */ 1916 1.1 christos "lwz 5, -12(31) \n" 1917 1.1 christos "stw " TOP_FIRST ", 0(5) \n" 1918 1.1 christos "stw " TOP_SECOND ", 4(5) \n" 1919 1.1 christos /* Restore registers. */ 1920 1.1 christos "lwz 31, -4(31) \n" 1921 1.1 christos "lwz 30, -8(31) \n" 1922 1.1 christos /* Restore SP. */ 1923 1.1 christos "lwz 1, 0(1) \n" 1924 1.1 christos /* Restore LR. */ 1925 1.1 christos "lwz 0, 4(1) \n" 1926 1.1 christos /* Return 0 for no-error. */ 1927 1.1 christos "li 3, 0 \n" 1928 1.1 christos "mtlr 0 \n" 1929 1.1 christos "blr \n"); 1930 1.1 christos } 1931 1.1 christos 1932 1.1 christos /* TOP = stack[--sp] + TOP */ 1933 1.1 christos 1934 1.1 christos static void 1935 1.1 christos ppc_emit_add (void) 1936 1.1 christos { 1937 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 1938 1.1 christos "lwz " TMP_SECOND ", 4(30)\n" 1939 1.1 christos "addc 4, 6, 4 \n" 1940 1.1 christos "adde 3, 5, 3 \n"); 1941 1.1 christos } 1942 1.1 christos 1943 1.1 christos /* TOP = stack[--sp] - TOP */ 1944 1.1 christos 1945 1.1 christos static void 1946 1.1 christos ppc_emit_sub (void) 1947 1.1 christos { 1948 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 1949 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 1950 1.1 christos "subfc 4, 4, 6 \n" 1951 1.1 christos "subfe 3, 3, 5 \n"); 1952 1.1 christos } 1953 1.1 christos 1954 1.1 christos /* TOP = stack[--sp] * TOP */ 1955 1.1 christos 1956 1.1 christos static void 1957 1.1 christos ppc_emit_mul (void) 1958 1.1 christos { 1959 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 1960 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 1961 1.1 christos "mulhwu 7, 6, 4 \n" 1962 1.1 christos "mullw 3, 6, 3 \n" 1963 1.1 christos "mullw 5, 4, 5 \n" 1964 1.1 christos "mullw 4, 6, 4 \n" 1965 1.1 christos "add 3, 5, 3 \n" 1966 1.1 christos "add 3, 7, 3 \n"); 1967 1.1 christos } 1968 1.1 christos 1969 1.1 christos /* TOP = stack[--sp] << TOP */ 1970 1.1 christos 1971 1.1 christos static void 1972 1.1 christos ppc_emit_lsh (void) 1973 1.1 christos { 1974 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 1975 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 1976 1.1 christos "subfic 3, 4, 32\n" /* r3 = 32 - TOP */ 1977 1.1 christos "addi 7, 4, -32\n" /* r7 = TOP - 32 */ 1978 1.1 christos "slw 5, 5, 4\n" /* Shift high part left */ 1979 1.1 christos "slw 4, 6, 4\n" /* Shift low part left */ 1980 1.1 christos "srw 3, 6, 3\n" /* Shift low to high if shift < 32 */ 1981 1.1 christos "slw 7, 6, 7\n" /* Shift low to high if shift >= 32 */ 1982 1.1 christos "or 3, 5, 3\n" 1983 1.1 christos "or 3, 7, 3\n"); /* Assemble high part */ 1984 1.1 christos } 1985 1.1 christos 1986 1.1 christos /* Top = stack[--sp] >> TOP 1987 1.1 christos (Arithmetic shift right) */ 1988 1.1 christos 1989 1.1 christos static void 1990 1.1 christos ppc_emit_rsh_signed (void) 1991 1.1 christos { 1992 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 1993 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 1994 1.1 christos "addi 7, 4, -32\n" /* r7 = TOP - 32 */ 1995 1.1 christos "sraw 3, 5, 4\n" /* Shift high part right */ 1996 1.1 christos "cmpwi 7, 1\n" 1997 1.1 christos "blt 0, 1f\n" /* If shift <= 32, goto 1: */ 1998 1.1 christos "sraw 4, 5, 7\n" /* Shift high to low */ 1999 1.1 christos "b 2f\n" 2000 1.1 christos "1:\n" 2001 1.1 christos "subfic 7, 4, 32\n" /* r7 = 32 - TOP */ 2002 1.1 christos "srw 4, 6, 4\n" /* Shift low part right */ 2003 1.1 christos "slw 5, 5, 7\n" /* Shift high to low */ 2004 1.1 christos "or 4, 4, 5\n" /* Assemble low part */ 2005 1.1 christos "2:\n"); 2006 1.1 christos } 2007 1.1 christos 2008 1.1 christos /* Top = stack[--sp] >> TOP 2009 1.1 christos (Logical shift right) */ 2010 1.1 christos 2011 1.1 christos static void 2012 1.1 christos ppc_emit_rsh_unsigned (void) 2013 1.1 christos { 2014 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2015 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2016 1.1 christos "subfic 3, 4, 32\n" /* r3 = 32 - TOP */ 2017 1.1 christos "addi 7, 4, -32\n" /* r7 = TOP - 32 */ 2018 1.1 christos "srw 6, 6, 4\n" /* Shift low part right */ 2019 1.1 christos "slw 3, 5, 3\n" /* Shift high to low if shift < 32 */ 2020 1.1 christos "srw 7, 5, 7\n" /* Shift high to low if shift >= 32 */ 2021 1.1 christos "or 6, 6, 3\n" 2022 1.1 christos "srw 3, 5, 4\n" /* Shift high part right */ 2023 1.1 christos "or 4, 6, 7\n"); /* Assemble low part */ 2024 1.1 christos } 2025 1.1 christos 2026 1.1 christos /* Emit code for signed-extension specified by ARG. */ 2027 1.1 christos 2028 1.1 christos static void 2029 1.1 christos ppc_emit_ext (int arg) 2030 1.1 christos { 2031 1.1 christos switch (arg) 2032 1.1 christos { 2033 1.1 christos case 8: 2034 1.1 christos EMIT_ASM ("extsb 4, 4\n" 2035 1.1 christos "srawi 3, 4, 31"); 2036 1.1 christos break; 2037 1.1 christos case 16: 2038 1.1 christos EMIT_ASM ("extsh 4, 4\n" 2039 1.1 christos "srawi 3, 4, 31"); 2040 1.1 christos break; 2041 1.1 christos case 32: 2042 1.1 christos EMIT_ASM ("srawi 3, 4, 31"); 2043 1.1 christos break; 2044 1.1 christos default: 2045 1.1 christos emit_error = 1; 2046 1.1 christos } 2047 1.1 christos } 2048 1.1 christos 2049 1.1 christos /* Emit code for zero-extension specified by ARG. */ 2050 1.1 christos 2051 1.1 christos static void 2052 1.1 christos ppc_emit_zero_ext (int arg) 2053 1.1 christos { 2054 1.1 christos switch (arg) 2055 1.1 christos { 2056 1.1 christos case 8: 2057 1.1 christos EMIT_ASM ("clrlwi 4,4,24\n" 2058 1.1 christos "li 3, 0\n"); 2059 1.1 christos break; 2060 1.1 christos case 16: 2061 1.1 christos EMIT_ASM ("clrlwi 4,4,16\n" 2062 1.1 christos "li 3, 0\n"); 2063 1.1 christos break; 2064 1.1 christos case 32: 2065 1.1 christos EMIT_ASM ("li 3, 0"); 2066 1.1 christos break; 2067 1.1 christos default: 2068 1.1 christos emit_error = 1; 2069 1.1 christos } 2070 1.1 christos } 2071 1.1 christos 2072 1.1 christos /* TOP = !TOP 2073 1.1 christos i.e., TOP = (TOP == 0) ? 1 : 0; */ 2074 1.1 christos 2075 1.1 christos static void 2076 1.1 christos ppc_emit_log_not (void) 2077 1.1 christos { 2078 1.1 christos EMIT_ASM ("or 4, 3, 4 \n" 2079 1.1 christos "cntlzw 4, 4 \n" 2080 1.1 christos "srwi 4, 4, 5 \n" 2081 1.1 christos "li 3, 0 \n"); 2082 1.1 christos } 2083 1.1 christos 2084 1.1 christos /* TOP = stack[--sp] & TOP */ 2085 1.1 christos 2086 1.1 christos static void 2087 1.1 christos ppc_emit_bit_and (void) 2088 1.1 christos { 2089 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2090 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2091 1.1 christos "and 4, 6, 4 \n" 2092 1.1 christos "and 3, 5, 3 \n"); 2093 1.1 christos } 2094 1.1 christos 2095 1.1 christos /* TOP = stack[--sp] | TOP */ 2096 1.1 christos 2097 1.1 christos static void 2098 1.1 christos ppc_emit_bit_or (void) 2099 1.1 christos { 2100 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2101 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2102 1.1 christos "or 4, 6, 4 \n" 2103 1.1 christos "or 3, 5, 3 \n"); 2104 1.1 christos } 2105 1.1 christos 2106 1.1 christos /* TOP = stack[--sp] ^ TOP */ 2107 1.1 christos 2108 1.1 christos static void 2109 1.1 christos ppc_emit_bit_xor (void) 2110 1.1 christos { 2111 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2112 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2113 1.1 christos "xor 4, 6, 4 \n" 2114 1.1 christos "xor 3, 5, 3 \n"); 2115 1.1 christos } 2116 1.1 christos 2117 1.1 christos /* TOP = ~TOP 2118 1.1 christos i.e., TOP = ~(TOP | TOP) */ 2119 1.1 christos 2120 1.1 christos static void 2121 1.1 christos ppc_emit_bit_not (void) 2122 1.1 christos { 2123 1.1 christos EMIT_ASM ("nor 3, 3, 3 \n" 2124 1.1 christos "nor 4, 4, 4 \n"); 2125 1.1 christos } 2126 1.1 christos 2127 1.1 christos /* TOP = stack[--sp] == TOP */ 2128 1.1 christos 2129 1.1 christos static void 2130 1.1 christos ppc_emit_equal (void) 2131 1.1 christos { 2132 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2133 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2134 1.1 christos "xor 4, 6, 4 \n" 2135 1.1 christos "xor 3, 5, 3 \n" 2136 1.1 christos "or 4, 3, 4 \n" 2137 1.1 christos "cntlzw 4, 4 \n" 2138 1.1 christos "srwi 4, 4, 5 \n" 2139 1.1 christos "li 3, 0 \n"); 2140 1.1 christos } 2141 1.1 christos 2142 1.1 christos /* TOP = stack[--sp] < TOP 2143 1.1 christos (Signed comparison) */ 2144 1.1 christos 2145 1.1 christos static void 2146 1.1 christos ppc_emit_less_signed (void) 2147 1.1 christos { 2148 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2149 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2150 1.1 christos "cmplw 6, 6, 4 \n" 2151 1.1 christos "cmpw 7, 5, 3 \n" 2152 1.1 christos /* CR6 bit 0 = low less and high equal */ 2153 1.1 christos "crand 6*4+0, 6*4+0, 7*4+2\n" 2154 1.1 christos /* CR7 bit 0 = (low less and high equal) or high less */ 2155 1.1 christos "cror 7*4+0, 7*4+0, 6*4+0\n" 2156 1.1 christos "mfcr 4 \n" 2157 1.1 christos "rlwinm 4, 4, 29, 31, 31 \n" 2158 1.1 christos "li 3, 0 \n"); 2159 1.1 christos } 2160 1.1 christos 2161 1.1 christos /* TOP = stack[--sp] < TOP 2162 1.1 christos (Unsigned comparison) */ 2163 1.1 christos 2164 1.1 christos static void 2165 1.1 christos ppc_emit_less_unsigned (void) 2166 1.1 christos { 2167 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2168 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2169 1.1 christos "cmplw 6, 6, 4 \n" 2170 1.1 christos "cmplw 7, 5, 3 \n" 2171 1.1 christos /* CR6 bit 0 = low less and high equal */ 2172 1.1 christos "crand 6*4+0, 6*4+0, 7*4+2\n" 2173 1.1 christos /* CR7 bit 0 = (low less and high equal) or high less */ 2174 1.1 christos "cror 7*4+0, 7*4+0, 6*4+0\n" 2175 1.1 christos "mfcr 4 \n" 2176 1.1 christos "rlwinm 4, 4, 29, 31, 31 \n" 2177 1.1 christos "li 3, 0 \n"); 2178 1.1 christos } 2179 1.1 christos 2180 1.1 christos /* Access the memory address in TOP in size of SIZE. 2181 1.1 christos Zero-extend the read value. */ 2182 1.1 christos 2183 1.1 christos static void 2184 1.1 christos ppc_emit_ref (int size) 2185 1.1 christos { 2186 1.1 christos switch (size) 2187 1.1 christos { 2188 1.1 christos case 1: 2189 1.1 christos EMIT_ASM ("lbz 4, 0(4)\n" 2190 1.1 christos "li 3, 0"); 2191 1.1 christos break; 2192 1.1 christos case 2: 2193 1.1 christos EMIT_ASM ("lhz 4, 0(4)\n" 2194 1.1 christos "li 3, 0"); 2195 1.1 christos break; 2196 1.1 christos case 4: 2197 1.1 christos EMIT_ASM ("lwz 4, 0(4)\n" 2198 1.1 christos "li 3, 0"); 2199 1.1 christos break; 2200 1.1 christos case 8: 2201 1.1 christos if (__BYTE_ORDER == __LITTLE_ENDIAN) 2202 1.1 christos EMIT_ASM ("lwz 3, 4(4)\n" 2203 1.1 christos "lwz 4, 0(4)"); 2204 1.1 christos else 2205 1.1 christos EMIT_ASM ("lwz 3, 0(4)\n" 2206 1.1 christos "lwz 4, 4(4)"); 2207 1.1 christos break; 2208 1.1 christos } 2209 1.1 christos } 2210 1.1 christos 2211 1.1 christos /* TOP = NUM */ 2212 1.1 christos 2213 1.1 christos static void 2214 1.1 christos ppc_emit_const (LONGEST num) 2215 1.1 christos { 2216 1.1 christos uint32_t buf[10]; 2217 1.1 christos uint32_t *p = buf; 2218 1.1 christos 2219 1.1 christos p += gen_limm (p, 3, num >> 32 & 0xffffffff, 0); 2220 1.1 christos p += gen_limm (p, 4, num & 0xffffffff, 0); 2221 1.1 christos 2222 1.1 christos emit_insns (buf, p - buf); 2223 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2224 1.1 christos } 2225 1.1 christos 2226 1.1 christos /* Set TOP to the value of register REG by calling get_raw_reg function 2227 1.1 christos with two argument, collected buffer and register number. */ 2228 1.1 christos 2229 1.1 christos static void 2230 1.1 christos ppc_emit_reg (int reg) 2231 1.1 christos { 2232 1.1 christos uint32_t buf[13]; 2233 1.1 christos uint32_t *p = buf; 2234 1.1 christos 2235 1.1 christos /* fctx->regs is passed in r3 and then saved in -16(31). */ 2236 1.1 christos p += GEN_LWZ (p, 3, 31, -16); 2237 1.1 christos p += GEN_LI (p, 4, reg); /* li r4, reg */ 2238 1.1 christos p += gen_call (p, get_raw_reg_func_addr (), 0, 0); 2239 1.1 christos 2240 1.1 christos emit_insns (buf, p - buf); 2241 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2242 1.1 christos 2243 1.1 christos if (__BYTE_ORDER == __LITTLE_ENDIAN) 2244 1.1 christos { 2245 1.1 christos EMIT_ASM ("mr 5, 4\n" 2246 1.1 christos "mr 4, 3\n" 2247 1.1 christos "mr 3, 5\n"); 2248 1.1 christos } 2249 1.1 christos } 2250 1.1 christos 2251 1.1 christos /* TOP = stack[--sp] */ 2252 1.1 christos 2253 1.1 christos static void 2254 1.1 christos ppc_emit_pop (void) 2255 1.1 christos { 2256 1.1 christos EMIT_ASM ("lwzu " TOP_FIRST ", 8(30) \n" 2257 1.1 christos "lwz " TOP_SECOND ", 4(30) \n"); 2258 1.1 christos } 2259 1.1 christos 2260 1.1 christos /* stack[sp++] = TOP 2261 1.1 christos 2262 1.1 christos Because we may use up bytecode stack, expand 8 doublewords more 2263 1.1 christos if needed. */ 2264 1.1 christos 2265 1.1 christos static void 2266 1.1 christos ppc_emit_stack_flush (void) 2267 1.1 christos { 2268 1.1 christos /* Make sure bytecode stack is big enough before push. 2269 1.1 christos Otherwise, expand 64-byte more. */ 2270 1.1 christos 2271 1.1 christos EMIT_ASM (" stw " TOP_FIRST ", 0(30) \n" 2272 1.1 christos " stw " TOP_SECOND ", 4(30)\n" 2273 1.1 christos " addi 5, 30, -(8 + 8) \n" 2274 1.1 christos " cmpw 7, 5, 1 \n" 2275 1.1 christos " bgt 7, 1f \n" 2276 1.1 christos " stwu 31, -64(1) \n" 2277 1.1 christos "1:addi 30, 30, -8 \n"); 2278 1.1 christos } 2279 1.1 christos 2280 1.1 christos /* Swap TOP and stack[sp-1] */ 2281 1.1 christos 2282 1.1 christos static void 2283 1.1 christos ppc_emit_swap (void) 2284 1.1 christos { 2285 1.1 christos EMIT_ASM ("lwz " TMP_FIRST ", 8(30) \n" 2286 1.1 christos "lwz " TMP_SECOND ", 12(30) \n" 2287 1.1 christos "stw " TOP_FIRST ", 8(30) \n" 2288 1.1 christos "stw " TOP_SECOND ", 12(30) \n" 2289 1.1 christos "mr 3, 5 \n" 2290 1.1 christos "mr 4, 6 \n"); 2291 1.1 christos } 2292 1.1 christos 2293 1.1 christos /* Discard N elements in the stack. Also used for ppc64. */ 2294 1.1 christos 2295 1.1 christos static void 2296 1.1 christos ppc_emit_stack_adjust (int n) 2297 1.1 christos { 2298 1.1 christos uint32_t buf[6]; 2299 1.1 christos uint32_t *p = buf; 2300 1.1 christos 2301 1.1 christos n = n << 3; 2302 1.1 christos if ((n >> 15) != 0) 2303 1.1 christos { 2304 1.1 christos emit_error = 1; 2305 1.1 christos return; 2306 1.1 christos } 2307 1.1 christos 2308 1.1 christos p += GEN_ADDI (p, 30, 30, n); 2309 1.1 christos 2310 1.1 christos emit_insns (buf, p - buf); 2311 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2312 1.1 christos } 2313 1.1 christos 2314 1.1 christos /* Call function FN. */ 2315 1.1 christos 2316 1.1 christos static void 2317 1.1 christos ppc_emit_call (CORE_ADDR fn) 2318 1.1 christos { 2319 1.1 christos uint32_t buf[11]; 2320 1.1 christos uint32_t *p = buf; 2321 1.1 christos 2322 1.1 christos p += gen_call (p, fn, 0, 0); 2323 1.1 christos 2324 1.1 christos emit_insns (buf, p - buf); 2325 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2326 1.1 christos } 2327 1.1 christos 2328 1.1 christos /* FN's prototype is `LONGEST(*fn)(int)'. 2329 1.1 christos TOP = fn (arg1) 2330 1.1 christos */ 2331 1.1 christos 2332 1.1 christos static void 2333 1.1 christos ppc_emit_int_call_1 (CORE_ADDR fn, int arg1) 2334 1.1 christos { 2335 1.1 christos uint32_t buf[15]; 2336 1.1 christos uint32_t *p = buf; 2337 1.1 christos 2338 1.1 christos /* Setup argument. arg1 is a 16-bit value. */ 2339 1.1 christos p += gen_limm (p, 3, (uint32_t) arg1, 0); 2340 1.1 christos p += gen_call (p, fn, 0, 0); 2341 1.1 christos 2342 1.1 christos emit_insns (buf, p - buf); 2343 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2344 1.1 christos 2345 1.1 christos if (__BYTE_ORDER == __LITTLE_ENDIAN) 2346 1.1 christos { 2347 1.1 christos EMIT_ASM ("mr 5, 4\n" 2348 1.1 christos "mr 4, 3\n" 2349 1.1 christos "mr 3, 5\n"); 2350 1.1 christos } 2351 1.1 christos } 2352 1.1 christos 2353 1.1 christos /* FN's prototype is `void(*fn)(int,LONGEST)'. 2354 1.1 christos fn (arg1, TOP) 2355 1.1 christos 2356 1.1 christos TOP should be preserved/restored before/after the call. */ 2357 1.1 christos 2358 1.1 christos static void 2359 1.1 christos ppc_emit_void_call_2 (CORE_ADDR fn, int arg1) 2360 1.1 christos { 2361 1.1 christos uint32_t buf[21]; 2362 1.1 christos uint32_t *p = buf; 2363 1.1 christos 2364 1.1 christos /* Save TOP. 0(30) is next-empty. */ 2365 1.1 christos p += GEN_STW (p, 3, 30, 0); 2366 1.1 christos p += GEN_STW (p, 4, 30, 4); 2367 1.1 christos 2368 1.1 christos /* Setup argument. arg1 is a 16-bit value. */ 2369 1.1 christos if (__BYTE_ORDER == __LITTLE_ENDIAN) 2370 1.1 christos { 2371 1.1 christos p += GEN_MR (p, 5, 4); 2372 1.1 christos p += GEN_MR (p, 6, 3); 2373 1.1 christos } 2374 1.1 christos else 2375 1.1 christos { 2376 1.1 christos p += GEN_MR (p, 5, 3); 2377 1.1 christos p += GEN_MR (p, 6, 4); 2378 1.1 christos } 2379 1.1 christos p += gen_limm (p, 3, (uint32_t) arg1, 0); 2380 1.1 christos p += gen_call (p, fn, 0, 0); 2381 1.1 christos 2382 1.1 christos /* Restore TOP */ 2383 1.1 christos p += GEN_LWZ (p, 3, 30, 0); 2384 1.1 christos p += GEN_LWZ (p, 4, 30, 4); 2385 1.1 christos 2386 1.1 christos emit_insns (buf, p - buf); 2387 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2388 1.1 christos } 2389 1.1 christos 2390 1.1 christos /* Note in the following goto ops: 2391 1.1 christos 2392 1.1 christos When emitting goto, the target address is later relocated by 2393 1.1 christos write_goto_address. OFFSET_P is the offset of the branch instruction 2394 1.1 christos in the code sequence, and SIZE_P is how to relocate the instruction, 2395 1.1 christos recognized by ppc_write_goto_address. In current implementation, 2396 1.1 christos SIZE can be either 24 or 14 for branch of conditional-branch instruction. 2397 1.1 christos */ 2398 1.1 christos 2399 1.1 christos /* If TOP is true, goto somewhere. Otherwise, just fall-through. */ 2400 1.1 christos 2401 1.1 christos static void 2402 1.1 christos ppc_emit_if_goto (int *offset_p, int *size_p) 2403 1.1 christos { 2404 1.1 christos EMIT_ASM ("or. 3, 3, 4 \n" 2405 1.1 christos "lwzu " TOP_FIRST ", 8(30) \n" 2406 1.1 christos "lwz " TOP_SECOND ", 4(30) \n" 2407 1.1 christos "1:bne 0, 1b \n"); 2408 1.1 christos 2409 1.1 christos if (offset_p) 2410 1.1 christos *offset_p = 12; 2411 1.1 christos if (size_p) 2412 1.1 christos *size_p = 14; 2413 1.1 christos } 2414 1.1 christos 2415 1.1 christos /* Unconditional goto. Also used for ppc64. */ 2416 1.1 christos 2417 1.1 christos static void 2418 1.1 christos ppc_emit_goto (int *offset_p, int *size_p) 2419 1.1 christos { 2420 1.1 christos EMIT_ASM ("1:b 1b"); 2421 1.1 christos 2422 1.1 christos if (offset_p) 2423 1.1 christos *offset_p = 0; 2424 1.1 christos if (size_p) 2425 1.1 christos *size_p = 24; 2426 1.1 christos } 2427 1.1 christos 2428 1.1 christos /* Goto if stack[--sp] == TOP */ 2429 1.1 christos 2430 1.1 christos static void 2431 1.1 christos ppc_emit_eq_goto (int *offset_p, int *size_p) 2432 1.1 christos { 2433 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2434 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2435 1.1 christos "xor 4, 6, 4 \n" 2436 1.1 christos "xor 3, 5, 3 \n" 2437 1.1 christos "or. 3, 3, 4 \n" 2438 1.1 christos "lwzu " TOP_FIRST ", 8(30) \n" 2439 1.1 christos "lwz " TOP_SECOND ", 4(30) \n" 2440 1.1 christos "1:beq 0, 1b \n"); 2441 1.1 christos 2442 1.1 christos if (offset_p) 2443 1.1 christos *offset_p = 28; 2444 1.1 christos if (size_p) 2445 1.1 christos *size_p = 14; 2446 1.1 christos } 2447 1.1 christos 2448 1.1 christos /* Goto if stack[--sp] != TOP */ 2449 1.1 christos 2450 1.1 christos static void 2451 1.1 christos ppc_emit_ne_goto (int *offset_p, int *size_p) 2452 1.1 christos { 2453 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2454 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2455 1.1 christos "xor 4, 6, 4 \n" 2456 1.1 christos "xor 3, 5, 3 \n" 2457 1.1 christos "or. 3, 3, 4 \n" 2458 1.1 christos "lwzu " TOP_FIRST ", 8(30) \n" 2459 1.1 christos "lwz " TOP_SECOND ", 4(30) \n" 2460 1.1 christos "1:bne 0, 1b \n"); 2461 1.1 christos 2462 1.1 christos if (offset_p) 2463 1.1 christos *offset_p = 28; 2464 1.1 christos if (size_p) 2465 1.1 christos *size_p = 14; 2466 1.1 christos } 2467 1.1 christos 2468 1.1 christos /* Goto if stack[--sp] < TOP */ 2469 1.1 christos 2470 1.1 christos static void 2471 1.1 christos ppc_emit_lt_goto (int *offset_p, int *size_p) 2472 1.1 christos { 2473 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2474 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2475 1.1 christos "cmplw 6, 6, 4 \n" 2476 1.1 christos "cmpw 7, 5, 3 \n" 2477 1.1 christos /* CR6 bit 0 = low less and high equal */ 2478 1.1 christos "crand 6*4+0, 6*4+0, 7*4+2\n" 2479 1.1 christos /* CR7 bit 0 = (low less and high equal) or high less */ 2480 1.1 christos "cror 7*4+0, 7*4+0, 6*4+0\n" 2481 1.1 christos "lwzu " TOP_FIRST ", 8(30) \n" 2482 1.1 christos "lwz " TOP_SECOND ", 4(30)\n" 2483 1.1 christos "1:blt 7, 1b \n"); 2484 1.1 christos 2485 1.1 christos if (offset_p) 2486 1.1 christos *offset_p = 32; 2487 1.1 christos if (size_p) 2488 1.1 christos *size_p = 14; 2489 1.1 christos } 2490 1.1 christos 2491 1.1 christos /* Goto if stack[--sp] <= TOP */ 2492 1.1 christos 2493 1.1 christos static void 2494 1.1 christos ppc_emit_le_goto (int *offset_p, int *size_p) 2495 1.1 christos { 2496 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2497 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2498 1.1 christos "cmplw 6, 6, 4 \n" 2499 1.1 christos "cmpw 7, 5, 3 \n" 2500 1.1 christos /* CR6 bit 0 = low less/equal and high equal */ 2501 1.1 christos "crandc 6*4+0, 7*4+2, 6*4+1\n" 2502 1.1 christos /* CR7 bit 0 = (low less/eq and high equal) or high less */ 2503 1.1 christos "cror 7*4+0, 7*4+0, 6*4+0\n" 2504 1.1 christos "lwzu " TOP_FIRST ", 8(30) \n" 2505 1.1 christos "lwz " TOP_SECOND ", 4(30)\n" 2506 1.1 christos "1:blt 7, 1b \n"); 2507 1.1 christos 2508 1.1 christos if (offset_p) 2509 1.1 christos *offset_p = 32; 2510 1.1 christos if (size_p) 2511 1.1 christos *size_p = 14; 2512 1.1 christos } 2513 1.1 christos 2514 1.1 christos /* Goto if stack[--sp] > TOP */ 2515 1.1 christos 2516 1.1 christos static void 2517 1.1 christos ppc_emit_gt_goto (int *offset_p, int *size_p) 2518 1.1 christos { 2519 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2520 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2521 1.1 christos "cmplw 6, 6, 4 \n" 2522 1.1 christos "cmpw 7, 5, 3 \n" 2523 1.1 christos /* CR6 bit 0 = low greater and high equal */ 2524 1.1 christos "crand 6*4+0, 6*4+1, 7*4+2\n" 2525 1.1 christos /* CR7 bit 0 = (low greater and high equal) or high greater */ 2526 1.1 christos "cror 7*4+0, 7*4+1, 6*4+0\n" 2527 1.1 christos "lwzu " TOP_FIRST ", 8(30) \n" 2528 1.1 christos "lwz " TOP_SECOND ", 4(30)\n" 2529 1.1 christos "1:blt 7, 1b \n"); 2530 1.1 christos 2531 1.1 christos if (offset_p) 2532 1.1 christos *offset_p = 32; 2533 1.1 christos if (size_p) 2534 1.1 christos *size_p = 14; 2535 1.1 christos } 2536 1.1 christos 2537 1.1 christos /* Goto if stack[--sp] >= TOP */ 2538 1.1 christos 2539 1.1 christos static void 2540 1.1 christos ppc_emit_ge_goto (int *offset_p, int *size_p) 2541 1.1 christos { 2542 1.1 christos EMIT_ASM ("lwzu " TMP_FIRST ", 8(30) \n" 2543 1.1 christos "lwz " TMP_SECOND ", 4(30) \n" 2544 1.1 christos "cmplw 6, 6, 4 \n" 2545 1.1 christos "cmpw 7, 5, 3 \n" 2546 1.1 christos /* CR6 bit 0 = low ge and high equal */ 2547 1.1 christos "crandc 6*4+0, 7*4+2, 6*4+0\n" 2548 1.1 christos /* CR7 bit 0 = (low ge and high equal) or high greater */ 2549 1.1 christos "cror 7*4+0, 7*4+1, 6*4+0\n" 2550 1.1 christos "lwzu " TOP_FIRST ", 8(30)\n" 2551 1.1 christos "lwz " TOP_SECOND ", 4(30)\n" 2552 1.1 christos "1:blt 7, 1b \n"); 2553 1.1 christos 2554 1.1 christos if (offset_p) 2555 1.1 christos *offset_p = 32; 2556 1.1 christos if (size_p) 2557 1.1 christos *size_p = 14; 2558 1.1 christos } 2559 1.1 christos 2560 1.1 christos /* Relocate previous emitted branch instruction. FROM is the address 2561 1.1 christos of the branch instruction, TO is the goto target address, and SIZE 2562 1.1 christos if the value we set by *SIZE_P before. Currently, it is either 2563 1.1 christos 24 or 14 of branch and conditional-branch instruction. 2564 1.1 christos Also used for ppc64. */ 2565 1.1 christos 2566 1.1 christos static void 2567 1.1 christos ppc_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size) 2568 1.1 christos { 2569 1.1 christos long rel = to - from; 2570 1.1 christos uint32_t insn; 2571 1.1 christos int opcd; 2572 1.1 christos 2573 1.1 christos read_inferior_memory (from, (unsigned char *) &insn, 4); 2574 1.1 christos opcd = (insn >> 26) & 0x3f; 2575 1.1 christos 2576 1.1 christos switch (size) 2577 1.1 christos { 2578 1.1 christos case 14: 2579 1.1 christos if (opcd != 16 2580 1.1 christos || (rel >= (1 << 15) || rel < -(1 << 15))) 2581 1.1 christos emit_error = 1; 2582 1.1 christos insn = (insn & ~0xfffc) | (rel & 0xfffc); 2583 1.1 christos break; 2584 1.1 christos case 24: 2585 1.1 christos if (opcd != 18 2586 1.1 christos || (rel >= (1 << 25) || rel < -(1 << 25))) 2587 1.1 christos emit_error = 1; 2588 1.1 christos insn = (insn & ~0x3fffffc) | (rel & 0x3fffffc); 2589 1.1 christos break; 2590 1.1 christos default: 2591 1.1 christos emit_error = 1; 2592 1.1 christos } 2593 1.1 christos 2594 1.1 christos if (!emit_error) 2595 1.1 christos target_write_memory (from, (unsigned char *) &insn, 4); 2596 1.1 christos } 2597 1.1 christos 2598 1.1 christos /* Table of emit ops for 32-bit. */ 2599 1.1 christos 2600 1.1 christos static struct emit_ops ppc_emit_ops_impl = 2601 1.1 christos { 2602 1.1 christos ppc_emit_prologue, 2603 1.1 christos ppc_emit_epilogue, 2604 1.1 christos ppc_emit_add, 2605 1.1 christos ppc_emit_sub, 2606 1.1 christos ppc_emit_mul, 2607 1.1 christos ppc_emit_lsh, 2608 1.1 christos ppc_emit_rsh_signed, 2609 1.1 christos ppc_emit_rsh_unsigned, 2610 1.1 christos ppc_emit_ext, 2611 1.1 christos ppc_emit_log_not, 2612 1.1 christos ppc_emit_bit_and, 2613 1.1 christos ppc_emit_bit_or, 2614 1.1 christos ppc_emit_bit_xor, 2615 1.1 christos ppc_emit_bit_not, 2616 1.1 christos ppc_emit_equal, 2617 1.1 christos ppc_emit_less_signed, 2618 1.1 christos ppc_emit_less_unsigned, 2619 1.1 christos ppc_emit_ref, 2620 1.1 christos ppc_emit_if_goto, 2621 1.1 christos ppc_emit_goto, 2622 1.1 christos ppc_write_goto_address, 2623 1.1 christos ppc_emit_const, 2624 1.1 christos ppc_emit_call, 2625 1.1 christos ppc_emit_reg, 2626 1.1 christos ppc_emit_pop, 2627 1.1 christos ppc_emit_stack_flush, 2628 1.1 christos ppc_emit_zero_ext, 2629 1.1 christos ppc_emit_swap, 2630 1.1 christos ppc_emit_stack_adjust, 2631 1.1 christos ppc_emit_int_call_1, 2632 1.1 christos ppc_emit_void_call_2, 2633 1.1 christos ppc_emit_eq_goto, 2634 1.1 christos ppc_emit_ne_goto, 2635 1.1 christos ppc_emit_lt_goto, 2636 1.1 christos ppc_emit_le_goto, 2637 1.1 christos ppc_emit_gt_goto, 2638 1.1 christos ppc_emit_ge_goto 2639 1.1 christos }; 2640 1.1 christos 2641 1.1 christos #ifdef __powerpc64__ 2642 1.1 christos 2643 1.1 christos /* 2644 1.1 christos 2645 1.1 christos Bytecode execution stack frame - 64-bit 2646 1.1 christos 2647 1.1 christos | LR save area (SP + 16) 2648 1.1 christos | CR save area (SP + 8) 2649 1.1 christos SP' -> +- Back chain (SP + 0) 2650 1.1 christos | Save r31 for access saved arguments 2651 1.1 christos | Save r30 for bytecode stack pointer 2652 1.1 christos | Save r4 for incoming argument *value 2653 1.1 christos | Save r3 for incoming argument regs 2654 1.1 christos r30 -> +- Bytecode execution stack 2655 1.1 christos | 2656 1.1 christos | 64-byte (8 doublewords) at initial. 2657 1.1 christos | Expand stack as needed. 2658 1.1 christos | 2659 1.1 christos +- 2660 1.1.1.2 christos | Some padding for minimum stack frame. 2661 1.1.1.2 christos | 112 for ELFv1. 2662 1.1 christos SP +- Back-chain (SP') 2663 1.1 christos 2664 1.1 christos initial frame size 2665 1.1 christos = 112 + (4 * 8) + 64 2666 1.1 christos = 208 2667 1.1 christos 2668 1.1 christos r30 is the stack-pointer for bytecode machine. 2669 1.1 christos It should point to next-empty, so we can use LDU for pop. 2670 1.1 christos r3 is used for cache of TOP value. 2671 1.1 christos It was the first argument, pointer to regs. 2672 1.1 christos r4 is the second argument, pointer to the result. 2673 1.1 christos We should set *result = TOP after leaving this function. 2674 1.1 christos 2675 1.1 christos Note: 2676 1.1 christos * To restore stack at epilogue 2677 1.1 christos => sp = r31 2678 1.1 christos * To check stack is big enough for bytecode execution. 2679 1.1 christos => r30 - 8 > SP + 112 2680 1.1 christos * To return execution result. 2681 1.1 christos => 0(r4) = TOP 2682 1.1 christos 2683 1.1 christos */ 2684 1.1 christos 2685 1.1 christos /* Emit prologue in inferior memory. See above comments. */ 2686 1.1 christos 2687 1.1 christos static void 2688 1.1 christos ppc64v1_emit_prologue (void) 2689 1.1 christos { 2690 1.1 christos /* On ELFv1, function pointers really point to function descriptor, 2691 1.1 christos so emit one here. We don't care about contents of words 1 and 2, 2692 1.1 christos so let them just overlap out code. */ 2693 1.1 christos uint64_t opd = current_insn_ptr + 8; 2694 1.1 christos uint32_t buf[2]; 2695 1.1 christos 2696 1.1 christos /* Mind the strict aliasing rules. */ 2697 1.1 christos memcpy (buf, &opd, sizeof buf); 2698 1.1 christos emit_insns(buf, 2); 2699 1.1 christos EMIT_ASM (/* Save return address. */ 2700 1.1 christos "mflr 0 \n" 2701 1.1 christos "std 0, 16(1) \n" 2702 1.1 christos /* Save r30 and incoming arguments. */ 2703 1.1 christos "std 31, -8(1) \n" 2704 1.1 christos "std 30, -16(1) \n" 2705 1.1 christos "std 4, -24(1) \n" 2706 1.1 christos "std 3, -32(1) \n" 2707 1.1 christos /* Point r31 to current r1 for access arguments. */ 2708 1.1 christos "mr 31, 1 \n" 2709 1.1 christos /* Adjust SP. 208 is the initial frame size. */ 2710 1.1 christos "stdu 1, -208(1) \n" 2711 1.1 christos /* Set r30 to pointing stack-top. */ 2712 1.1 christos "addi 30, 1, 168 \n" 2713 1.1 christos /* Initial r3/TOP to 0. */ 2714 1.1 christos "li 3, 0 \n"); 2715 1.1 christos } 2716 1.1 christos 2717 1.1 christos /* Emit prologue in inferior memory. See above comments. */ 2718 1.1 christos 2719 1.1 christos static void 2720 1.1 christos ppc64v2_emit_prologue (void) 2721 1.1 christos { 2722 1.1 christos EMIT_ASM (/* Save return address. */ 2723 1.1 christos "mflr 0 \n" 2724 1.1 christos "std 0, 16(1) \n" 2725 1.1 christos /* Save r30 and incoming arguments. */ 2726 1.1 christos "std 31, -8(1) \n" 2727 1.1 christos "std 30, -16(1) \n" 2728 1.1 christos "std 4, -24(1) \n" 2729 1.1 christos "std 3, -32(1) \n" 2730 1.1 christos /* Point r31 to current r1 for access arguments. */ 2731 1.1 christos "mr 31, 1 \n" 2732 1.1 christos /* Adjust SP. 208 is the initial frame size. */ 2733 1.1 christos "stdu 1, -208(1) \n" 2734 1.1 christos /* Set r30 to pointing stack-top. */ 2735 1.1 christos "addi 30, 1, 168 \n" 2736 1.1 christos /* Initial r3/TOP to 0. */ 2737 1.1 christos "li 3, 0 \n"); 2738 1.1 christos } 2739 1.1 christos 2740 1.1 christos /* Emit epilogue in inferior memory. See above comments. */ 2741 1.1 christos 2742 1.1 christos static void 2743 1.1 christos ppc64_emit_epilogue (void) 2744 1.1 christos { 2745 1.1 christos EMIT_ASM (/* Restore SP. */ 2746 1.1 christos "ld 1, 0(1) \n" 2747 1.1 christos /* *result = TOP */ 2748 1.1 christos "ld 4, -24(1) \n" 2749 1.1 christos "std 3, 0(4) \n" 2750 1.1 christos /* Restore registers. */ 2751 1.1 christos "ld 31, -8(1) \n" 2752 1.1 christos "ld 30, -16(1) \n" 2753 1.1.1.2 christos /* Restore LR. */ 2754 1.1 christos "ld 0, 16(1) \n" 2755 1.1 christos /* Return 0 for no-error. */ 2756 1.1 christos "li 3, 0 \n" 2757 1.1 christos "mtlr 0 \n" 2758 1.1 christos "blr \n"); 2759 1.1 christos } 2760 1.1 christos 2761 1.1 christos /* TOP = stack[--sp] + TOP */ 2762 1.1 christos 2763 1.1 christos static void 2764 1.1 christos ppc64_emit_add (void) 2765 1.1 christos { 2766 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2767 1.1 christos "add 3, 4, 3 \n"); 2768 1.1 christos } 2769 1.1 christos 2770 1.1 christos /* TOP = stack[--sp] - TOP */ 2771 1.1 christos 2772 1.1 christos static void 2773 1.1 christos ppc64_emit_sub (void) 2774 1.1 christos { 2775 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2776 1.1 christos "sub 3, 4, 3 \n"); 2777 1.1 christos } 2778 1.1 christos 2779 1.1 christos /* TOP = stack[--sp] * TOP */ 2780 1.1 christos 2781 1.1 christos static void 2782 1.1 christos ppc64_emit_mul (void) 2783 1.1 christos { 2784 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2785 1.1 christos "mulld 3, 4, 3 \n"); 2786 1.1 christos } 2787 1.1 christos 2788 1.1 christos /* TOP = stack[--sp] << TOP */ 2789 1.1 christos 2790 1.1 christos static void 2791 1.1 christos ppc64_emit_lsh (void) 2792 1.1 christos { 2793 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2794 1.1 christos "sld 3, 4, 3 \n"); 2795 1.1 christos } 2796 1.1 christos 2797 1.1 christos /* Top = stack[--sp] >> TOP 2798 1.1 christos (Arithmetic shift right) */ 2799 1.1 christos 2800 1.1 christos static void 2801 1.1 christos ppc64_emit_rsh_signed (void) 2802 1.1 christos { 2803 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2804 1.1 christos "srad 3, 4, 3 \n"); 2805 1.1 christos } 2806 1.1 christos 2807 1.1 christos /* Top = stack[--sp] >> TOP 2808 1.1 christos (Logical shift right) */ 2809 1.1 christos 2810 1.1 christos static void 2811 1.1 christos ppc64_emit_rsh_unsigned (void) 2812 1.1 christos { 2813 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2814 1.1 christos "srd 3, 4, 3 \n"); 2815 1.1 christos } 2816 1.1 christos 2817 1.1 christos /* Emit code for signed-extension specified by ARG. */ 2818 1.1 christos 2819 1.1 christos static void 2820 1.1 christos ppc64_emit_ext (int arg) 2821 1.1 christos { 2822 1.1 christos switch (arg) 2823 1.1 christos { 2824 1.1 christos case 8: 2825 1.1 christos EMIT_ASM ("extsb 3, 3"); 2826 1.1 christos break; 2827 1.1 christos case 16: 2828 1.1 christos EMIT_ASM ("extsh 3, 3"); 2829 1.1 christos break; 2830 1.1 christos case 32: 2831 1.1 christos EMIT_ASM ("extsw 3, 3"); 2832 1.1 christos break; 2833 1.1 christos default: 2834 1.1 christos emit_error = 1; 2835 1.1 christos } 2836 1.1 christos } 2837 1.1 christos 2838 1.1 christos /* Emit code for zero-extension specified by ARG. */ 2839 1.1 christos 2840 1.1 christos static void 2841 1.1 christos ppc64_emit_zero_ext (int arg) 2842 1.1 christos { 2843 1.1 christos switch (arg) 2844 1.1 christos { 2845 1.1 christos case 8: 2846 1.1 christos EMIT_ASM ("rldicl 3,3,0,56"); 2847 1.1 christos break; 2848 1.1 christos case 16: 2849 1.1 christos EMIT_ASM ("rldicl 3,3,0,48"); 2850 1.1 christos break; 2851 1.1 christos case 32: 2852 1.1 christos EMIT_ASM ("rldicl 3,3,0,32"); 2853 1.1 christos break; 2854 1.1 christos default: 2855 1.1 christos emit_error = 1; 2856 1.1 christos } 2857 1.1 christos } 2858 1.1 christos 2859 1.1 christos /* TOP = !TOP 2860 1.1 christos i.e., TOP = (TOP == 0) ? 1 : 0; */ 2861 1.1 christos 2862 1.1 christos static void 2863 1.1 christos ppc64_emit_log_not (void) 2864 1.1 christos { 2865 1.1 christos EMIT_ASM ("cntlzd 3, 3 \n" 2866 1.1 christos "srdi 3, 3, 6 \n"); 2867 1.1 christos } 2868 1.1 christos 2869 1.1 christos /* TOP = stack[--sp] & TOP */ 2870 1.1 christos 2871 1.1 christos static void 2872 1.1 christos ppc64_emit_bit_and (void) 2873 1.1 christos { 2874 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2875 1.1 christos "and 3, 4, 3 \n"); 2876 1.1 christos } 2877 1.1 christos 2878 1.1 christos /* TOP = stack[--sp] | TOP */ 2879 1.1 christos 2880 1.1 christos static void 2881 1.1 christos ppc64_emit_bit_or (void) 2882 1.1 christos { 2883 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2884 1.1 christos "or 3, 4, 3 \n"); 2885 1.1 christos } 2886 1.1 christos 2887 1.1 christos /* TOP = stack[--sp] ^ TOP */ 2888 1.1 christos 2889 1.1 christos static void 2890 1.1 christos ppc64_emit_bit_xor (void) 2891 1.1 christos { 2892 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2893 1.1 christos "xor 3, 4, 3 \n"); 2894 1.1 christos } 2895 1.1 christos 2896 1.1 christos /* TOP = ~TOP 2897 1.1 christos i.e., TOP = ~(TOP | TOP) */ 2898 1.1 christos 2899 1.1 christos static void 2900 1.1 christos ppc64_emit_bit_not (void) 2901 1.1 christos { 2902 1.1 christos EMIT_ASM ("nor 3, 3, 3 \n"); 2903 1.1 christos } 2904 1.1 christos 2905 1.1 christos /* TOP = stack[--sp] == TOP */ 2906 1.1 christos 2907 1.1 christos static void 2908 1.1 christos ppc64_emit_equal (void) 2909 1.1 christos { 2910 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2911 1.1 christos "xor 3, 3, 4 \n" 2912 1.1 christos "cntlzd 3, 3 \n" 2913 1.1 christos "srdi 3, 3, 6 \n"); 2914 1.1 christos } 2915 1.1 christos 2916 1.1 christos /* TOP = stack[--sp] < TOP 2917 1.1 christos (Signed comparison) */ 2918 1.1 christos 2919 1.1 christos static void 2920 1.1 christos ppc64_emit_less_signed (void) 2921 1.1 christos { 2922 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2923 1.1 christos "cmpd 7, 4, 3 \n" 2924 1.1 christos "mfcr 3 \n" 2925 1.1 christos "rlwinm 3, 3, 29, 31, 31 \n"); 2926 1.1 christos } 2927 1.1 christos 2928 1.1 christos /* TOP = stack[--sp] < TOP 2929 1.1 christos (Unsigned comparison) */ 2930 1.1 christos 2931 1.1 christos static void 2932 1.1 christos ppc64_emit_less_unsigned (void) 2933 1.1 christos { 2934 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 2935 1.1 christos "cmpld 7, 4, 3 \n" 2936 1.1 christos "mfcr 3 \n" 2937 1.1 christos "rlwinm 3, 3, 29, 31, 31 \n"); 2938 1.1 christos } 2939 1.1 christos 2940 1.1 christos /* Access the memory address in TOP in size of SIZE. 2941 1.1 christos Zero-extend the read value. */ 2942 1.1 christos 2943 1.1 christos static void 2944 1.1 christos ppc64_emit_ref (int size) 2945 1.1 christos { 2946 1.1 christos switch (size) 2947 1.1 christos { 2948 1.1 christos case 1: 2949 1.1 christos EMIT_ASM ("lbz 3, 0(3)"); 2950 1.1 christos break; 2951 1.1 christos case 2: 2952 1.1 christos EMIT_ASM ("lhz 3, 0(3)"); 2953 1.1 christos break; 2954 1.1 christos case 4: 2955 1.1 christos EMIT_ASM ("lwz 3, 0(3)"); 2956 1.1 christos break; 2957 1.1 christos case 8: 2958 1.1 christos EMIT_ASM ("ld 3, 0(3)"); 2959 1.1 christos break; 2960 1.1 christos } 2961 1.1 christos } 2962 1.1 christos 2963 1.1 christos /* TOP = NUM */ 2964 1.1 christos 2965 1.1 christos static void 2966 1.1 christos ppc64_emit_const (LONGEST num) 2967 1.1 christos { 2968 1.1 christos uint32_t buf[5]; 2969 1.1 christos uint32_t *p = buf; 2970 1.1 christos 2971 1.1 christos p += gen_limm (p, 3, num, 1); 2972 1.1 christos 2973 1.1 christos emit_insns (buf, p - buf); 2974 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2975 1.1 christos } 2976 1.1 christos 2977 1.1 christos /* Set TOP to the value of register REG by calling get_raw_reg function 2978 1.1 christos with two argument, collected buffer and register number. */ 2979 1.1 christos 2980 1.1 christos static void 2981 1.1 christos ppc64v1_emit_reg (int reg) 2982 1.1 christos { 2983 1.1 christos uint32_t buf[15]; 2984 1.1 christos uint32_t *p = buf; 2985 1.1 christos 2986 1.1 christos /* fctx->regs is passed in r3 and then saved in 176(1). */ 2987 1.1 christos p += GEN_LD (p, 3, 31, -32); 2988 1.1 christos p += GEN_LI (p, 4, reg); 2989 1.1 christos p += GEN_STD (p, 2, 1, 40); /* Save TOC. */ 2990 1.1 christos p += gen_call (p, get_raw_reg_func_addr (), 1, 1); 2991 1.1 christos p += GEN_LD (p, 2, 1, 40); /* Restore TOC. */ 2992 1.1 christos 2993 1.1 christos emit_insns (buf, p - buf); 2994 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 2995 1.1 christos } 2996 1.1 christos 2997 1.1 christos /* Likewise, for ELFv2. */ 2998 1.1 christos 2999 1.1 christos static void 3000 1.1 christos ppc64v2_emit_reg (int reg) 3001 1.1 christos { 3002 1.1 christos uint32_t buf[12]; 3003 1.1 christos uint32_t *p = buf; 3004 1.1 christos 3005 1.1 christos /* fctx->regs is passed in r3 and then saved in 176(1). */ 3006 1.1 christos p += GEN_LD (p, 3, 31, -32); 3007 1.1 christos p += GEN_LI (p, 4, reg); 3008 1.1 christos p += GEN_STD (p, 2, 1, 24); /* Save TOC. */ 3009 1.1 christos p += gen_call (p, get_raw_reg_func_addr (), 1, 0); 3010 1.1 christos p += GEN_LD (p, 2, 1, 24); /* Restore TOC. */ 3011 1.1 christos 3012 1.1 christos emit_insns (buf, p - buf); 3013 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3014 1.1 christos } 3015 1.1 christos 3016 1.1 christos /* TOP = stack[--sp] */ 3017 1.1 christos 3018 1.1 christos static void 3019 1.1 christos ppc64_emit_pop (void) 3020 1.1 christos { 3021 1.1 christos EMIT_ASM ("ldu 3, 8(30)"); 3022 1.1 christos } 3023 1.1 christos 3024 1.1 christos /* stack[sp++] = TOP 3025 1.1 christos 3026 1.1 christos Because we may use up bytecode stack, expand 8 doublewords more 3027 1.1 christos if needed. */ 3028 1.1 christos 3029 1.1 christos static void 3030 1.1 christos ppc64_emit_stack_flush (void) 3031 1.1 christos { 3032 1.1 christos /* Make sure bytecode stack is big enough before push. 3033 1.1 christos Otherwise, expand 64-byte more. */ 3034 1.1 christos 3035 1.1 christos EMIT_ASM (" std 3, 0(30) \n" 3036 1.1 christos " addi 4, 30, -(112 + 8) \n" 3037 1.1 christos " cmpd 7, 4, 1 \n" 3038 1.1 christos " bgt 7, 1f \n" 3039 1.1 christos " stdu 31, -64(1) \n" 3040 1.1 christos "1:addi 30, 30, -8 \n"); 3041 1.1 christos } 3042 1.1 christos 3043 1.1 christos /* Swap TOP and stack[sp-1] */ 3044 1.1 christos 3045 1.1 christos static void 3046 1.1 christos ppc64_emit_swap (void) 3047 1.1 christos { 3048 1.1 christos EMIT_ASM ("ld 4, 8(30) \n" 3049 1.1 christos "std 3, 8(30) \n" 3050 1.1 christos "mr 3, 4 \n"); 3051 1.1 christos } 3052 1.1 christos 3053 1.1 christos /* Call function FN - ELFv1. */ 3054 1.1 christos 3055 1.1 christos static void 3056 1.1 christos ppc64v1_emit_call (CORE_ADDR fn) 3057 1.1 christos { 3058 1.1 christos uint32_t buf[13]; 3059 1.1 christos uint32_t *p = buf; 3060 1.1 christos 3061 1.1 christos p += GEN_STD (p, 2, 1, 40); /* Save TOC. */ 3062 1.1 christos p += gen_call (p, fn, 1, 1); 3063 1.1 christos p += GEN_LD (p, 2, 1, 40); /* Restore TOC. */ 3064 1.1 christos 3065 1.1 christos emit_insns (buf, p - buf); 3066 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3067 1.1 christos } 3068 1.1 christos 3069 1.1 christos /* Call function FN - ELFv2. */ 3070 1.1 christos 3071 1.1 christos static void 3072 1.1 christos ppc64v2_emit_call (CORE_ADDR fn) 3073 1.1 christos { 3074 1.1 christos uint32_t buf[10]; 3075 1.1 christos uint32_t *p = buf; 3076 1.1 christos 3077 1.1 christos p += GEN_STD (p, 2, 1, 24); /* Save TOC. */ 3078 1.1 christos p += gen_call (p, fn, 1, 0); 3079 1.1 christos p += GEN_LD (p, 2, 1, 24); /* Restore TOC. */ 3080 1.1 christos 3081 1.1 christos emit_insns (buf, p - buf); 3082 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3083 1.1 christos } 3084 1.1 christos 3085 1.1 christos /* FN's prototype is `LONGEST(*fn)(int)'. 3086 1.1 christos TOP = fn (arg1) 3087 1.1 christos */ 3088 1.1 christos 3089 1.1 christos static void 3090 1.1 christos ppc64v1_emit_int_call_1 (CORE_ADDR fn, int arg1) 3091 1.1 christos { 3092 1.1 christos uint32_t buf[13]; 3093 1.1 christos uint32_t *p = buf; 3094 1.1 christos 3095 1.1 christos /* Setup argument. arg1 is a 16-bit value. */ 3096 1.1 christos p += gen_limm (p, 3, arg1, 1); 3097 1.1 christos p += GEN_STD (p, 2, 1, 40); /* Save TOC. */ 3098 1.1 christos p += gen_call (p, fn, 1, 1); 3099 1.1 christos p += GEN_LD (p, 2, 1, 40); /* Restore TOC. */ 3100 1.1 christos 3101 1.1 christos emit_insns (buf, p - buf); 3102 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3103 1.1 christos } 3104 1.1 christos 3105 1.1 christos /* Likewise for ELFv2. */ 3106 1.1 christos 3107 1.1 christos static void 3108 1.1 christos ppc64v2_emit_int_call_1 (CORE_ADDR fn, int arg1) 3109 1.1 christos { 3110 1.1 christos uint32_t buf[10]; 3111 1.1 christos uint32_t *p = buf; 3112 1.1 christos 3113 1.1 christos /* Setup argument. arg1 is a 16-bit value. */ 3114 1.1 christos p += gen_limm (p, 3, arg1, 1); 3115 1.1 christos p += GEN_STD (p, 2, 1, 24); /* Save TOC. */ 3116 1.1 christos p += gen_call (p, fn, 1, 0); 3117 1.1 christos p += GEN_LD (p, 2, 1, 24); /* Restore TOC. */ 3118 1.1 christos 3119 1.1 christos emit_insns (buf, p - buf); 3120 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3121 1.1 christos } 3122 1.1 christos 3123 1.1 christos /* FN's prototype is `void(*fn)(int,LONGEST)'. 3124 1.1 christos fn (arg1, TOP) 3125 1.1 christos 3126 1.1 christos TOP should be preserved/restored before/after the call. */ 3127 1.1 christos 3128 1.1 christos static void 3129 1.1 christos ppc64v1_emit_void_call_2 (CORE_ADDR fn, int arg1) 3130 1.1 christos { 3131 1.1 christos uint32_t buf[17]; 3132 1.1 christos uint32_t *p = buf; 3133 1.1 christos 3134 1.1 christos /* Save TOP. 0(30) is next-empty. */ 3135 1.1 christos p += GEN_STD (p, 3, 30, 0); 3136 1.1 christos 3137 1.1 christos /* Setup argument. arg1 is a 16-bit value. */ 3138 1.1 christos p += GEN_MR (p, 4, 3); /* mr r4, r3 */ 3139 1.1 christos p += gen_limm (p, 3, arg1, 1); 3140 1.1 christos p += GEN_STD (p, 2, 1, 40); /* Save TOC. */ 3141 1.1 christos p += gen_call (p, fn, 1, 1); 3142 1.1 christos p += GEN_LD (p, 2, 1, 40); /* Restore TOC. */ 3143 1.1 christos 3144 1.1 christos /* Restore TOP */ 3145 1.1 christos p += GEN_LD (p, 3, 30, 0); 3146 1.1 christos 3147 1.1 christos emit_insns (buf, p - buf); 3148 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3149 1.1 christos } 3150 1.1 christos 3151 1.1 christos /* Likewise for ELFv2. */ 3152 1.1 christos 3153 1.1 christos static void 3154 1.1 christos ppc64v2_emit_void_call_2 (CORE_ADDR fn, int arg1) 3155 1.1 christos { 3156 1.1 christos uint32_t buf[14]; 3157 1.1 christos uint32_t *p = buf; 3158 1.1 christos 3159 1.1 christos /* Save TOP. 0(30) is next-empty. */ 3160 1.1 christos p += GEN_STD (p, 3, 30, 0); 3161 1.1 christos 3162 1.1 christos /* Setup argument. arg1 is a 16-bit value. */ 3163 1.1 christos p += GEN_MR (p, 4, 3); /* mr r4, r3 */ 3164 1.1 christos p += gen_limm (p, 3, arg1, 1); 3165 1.1 christos p += GEN_STD (p, 2, 1, 24); /* Save TOC. */ 3166 1.1 christos p += gen_call (p, fn, 1, 0); 3167 1.1 christos p += GEN_LD (p, 2, 1, 24); /* Restore TOC. */ 3168 1.1 christos 3169 1.1 christos /* Restore TOP */ 3170 1.1 christos p += GEN_LD (p, 3, 30, 0); 3171 1.1 christos 3172 1.1 christos emit_insns (buf, p - buf); 3173 1.1 christos gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf))); 3174 1.1 christos } 3175 1.1 christos 3176 1.1 christos /* If TOP is true, goto somewhere. Otherwise, just fall-through. */ 3177 1.1 christos 3178 1.1 christos static void 3179 1.1 christos ppc64_emit_if_goto (int *offset_p, int *size_p) 3180 1.1 christos { 3181 1.1 christos EMIT_ASM ("cmpdi 7, 3, 0 \n" 3182 1.1 christos "ldu 3, 8(30) \n" 3183 1.1 christos "1:bne 7, 1b \n"); 3184 1.1 christos 3185 1.1 christos if (offset_p) 3186 1.1 christos *offset_p = 8; 3187 1.1 christos if (size_p) 3188 1.1 christos *size_p = 14; 3189 1.1 christos } 3190 1.1 christos 3191 1.1 christos /* Goto if stack[--sp] == TOP */ 3192 1.1 christos 3193 1.1 christos static void 3194 1.1 christos ppc64_emit_eq_goto (int *offset_p, int *size_p) 3195 1.1 christos { 3196 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 3197 1.1 christos "cmpd 7, 4, 3 \n" 3198 1.1 christos "ldu 3, 8(30) \n" 3199 1.1 christos "1:beq 7, 1b \n"); 3200 1.1 christos 3201 1.1 christos if (offset_p) 3202 1.1 christos *offset_p = 12; 3203 1.1 christos if (size_p) 3204 1.1 christos *size_p = 14; 3205 1.1 christos } 3206 1.1 christos 3207 1.1 christos /* Goto if stack[--sp] != TOP */ 3208 1.1 christos 3209 1.1 christos static void 3210 1.1 christos ppc64_emit_ne_goto (int *offset_p, int *size_p) 3211 1.1 christos { 3212 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 3213 1.1 christos "cmpd 7, 4, 3 \n" 3214 1.1 christos "ldu 3, 8(30) \n" 3215 1.1 christos "1:bne 7, 1b \n"); 3216 1.1 christos 3217 1.1 christos if (offset_p) 3218 1.1 christos *offset_p = 12; 3219 1.1 christos if (size_p) 3220 1.1 christos *size_p = 14; 3221 1.1 christos } 3222 1.1 christos 3223 1.1 christos /* Goto if stack[--sp] < TOP */ 3224 1.1 christos 3225 1.1 christos static void 3226 1.1 christos ppc64_emit_lt_goto (int *offset_p, int *size_p) 3227 1.1 christos { 3228 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 3229 1.1 christos "cmpd 7, 4, 3 \n" 3230 1.1 christos "ldu 3, 8(30) \n" 3231 1.1 christos "1:blt 7, 1b \n"); 3232 1.1 christos 3233 1.1 christos if (offset_p) 3234 1.1 christos *offset_p = 12; 3235 1.1 christos if (size_p) 3236 1.1 christos *size_p = 14; 3237 1.1 christos } 3238 1.1 christos 3239 1.1 christos /* Goto if stack[--sp] <= TOP */ 3240 1.1 christos 3241 1.1 christos static void 3242 1.1 christos ppc64_emit_le_goto (int *offset_p, int *size_p) 3243 1.1 christos { 3244 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 3245 1.1 christos "cmpd 7, 4, 3 \n" 3246 1.1 christos "ldu 3, 8(30) \n" 3247 1.1 christos "1:ble 7, 1b \n"); 3248 1.1 christos 3249 1.1 christos if (offset_p) 3250 1.1 christos *offset_p = 12; 3251 1.1 christos if (size_p) 3252 1.1 christos *size_p = 14; 3253 1.1 christos } 3254 1.1 christos 3255 1.1 christos /* Goto if stack[--sp] > TOP */ 3256 1.1 christos 3257 1.1 christos static void 3258 1.1 christos ppc64_emit_gt_goto (int *offset_p, int *size_p) 3259 1.1 christos { 3260 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 3261 1.1 christos "cmpd 7, 4, 3 \n" 3262 1.1 christos "ldu 3, 8(30) \n" 3263 1.1 christos "1:bgt 7, 1b \n"); 3264 1.1 christos 3265 1.1 christos if (offset_p) 3266 1.1 christos *offset_p = 12; 3267 1.1 christos if (size_p) 3268 1.1 christos *size_p = 14; 3269 1.1 christos } 3270 1.1 christos 3271 1.1 christos /* Goto if stack[--sp] >= TOP */ 3272 1.1 christos 3273 1.1 christos static void 3274 1.1 christos ppc64_emit_ge_goto (int *offset_p, int *size_p) 3275 1.1 christos { 3276 1.1 christos EMIT_ASM ("ldu 4, 8(30) \n" 3277 1.1 christos "cmpd 7, 4, 3 \n" 3278 1.1 christos "ldu 3, 8(30) \n" 3279 1.1 christos "1:bge 7, 1b \n"); 3280 1.1 christos 3281 1.1 christos if (offset_p) 3282 1.1 christos *offset_p = 12; 3283 1.1 christos if (size_p) 3284 1.1 christos *size_p = 14; 3285 1.1 christos } 3286 1.1 christos 3287 1.1 christos /* Table of emit ops for 64-bit ELFv1. */ 3288 1.1 christos 3289 1.1 christos static struct emit_ops ppc64v1_emit_ops_impl = 3290 1.1 christos { 3291 1.1 christos ppc64v1_emit_prologue, 3292 1.1 christos ppc64_emit_epilogue, 3293 1.1 christos ppc64_emit_add, 3294 1.1 christos ppc64_emit_sub, 3295 1.1 christos ppc64_emit_mul, 3296 1.1 christos ppc64_emit_lsh, 3297 1.1 christos ppc64_emit_rsh_signed, 3298 1.1 christos ppc64_emit_rsh_unsigned, 3299 1.1 christos ppc64_emit_ext, 3300 1.1 christos ppc64_emit_log_not, 3301 1.1 christos ppc64_emit_bit_and, 3302 1.1 christos ppc64_emit_bit_or, 3303 1.1 christos ppc64_emit_bit_xor, 3304 1.1 christos ppc64_emit_bit_not, 3305 1.1 christos ppc64_emit_equal, 3306 1.1 christos ppc64_emit_less_signed, 3307 1.1 christos ppc64_emit_less_unsigned, 3308 1.1 christos ppc64_emit_ref, 3309 1.1 christos ppc64_emit_if_goto, 3310 1.1 christos ppc_emit_goto, 3311 1.1 christos ppc_write_goto_address, 3312 1.1 christos ppc64_emit_const, 3313 1.1 christos ppc64v1_emit_call, 3314 1.1 christos ppc64v1_emit_reg, 3315 1.1 christos ppc64_emit_pop, 3316 1.1 christos ppc64_emit_stack_flush, 3317 1.1 christos ppc64_emit_zero_ext, 3318 1.1 christos ppc64_emit_swap, 3319 1.1 christos ppc_emit_stack_adjust, 3320 1.1 christos ppc64v1_emit_int_call_1, 3321 1.1 christos ppc64v1_emit_void_call_2, 3322 1.1 christos ppc64_emit_eq_goto, 3323 1.1 christos ppc64_emit_ne_goto, 3324 1.1 christos ppc64_emit_lt_goto, 3325 1.1 christos ppc64_emit_le_goto, 3326 1.1 christos ppc64_emit_gt_goto, 3327 1.1 christos ppc64_emit_ge_goto 3328 1.1 christos }; 3329 1.1 christos 3330 1.1 christos /* Table of emit ops for 64-bit ELFv2. */ 3331 1.1 christos 3332 1.1 christos static struct emit_ops ppc64v2_emit_ops_impl = 3333 1.1 christos { 3334 1.1 christos ppc64v2_emit_prologue, 3335 1.1 christos ppc64_emit_epilogue, 3336 1.1 christos ppc64_emit_add, 3337 1.1 christos ppc64_emit_sub, 3338 1.1 christos ppc64_emit_mul, 3339 1.1 christos ppc64_emit_lsh, 3340 1.1 christos ppc64_emit_rsh_signed, 3341 1.1 christos ppc64_emit_rsh_unsigned, 3342 1.1 christos ppc64_emit_ext, 3343 1.1 christos ppc64_emit_log_not, 3344 1.1 christos ppc64_emit_bit_and, 3345 1.1 christos ppc64_emit_bit_or, 3346 1.1 christos ppc64_emit_bit_xor, 3347 1.1 christos ppc64_emit_bit_not, 3348 1.1 christos ppc64_emit_equal, 3349 1.1 christos ppc64_emit_less_signed, 3350 1.1 christos ppc64_emit_less_unsigned, 3351 1.1 christos ppc64_emit_ref, 3352 1.1 christos ppc64_emit_if_goto, 3353 1.1 christos ppc_emit_goto, 3354 1.1 christos ppc_write_goto_address, 3355 1.1 christos ppc64_emit_const, 3356 1.1 christos ppc64v2_emit_call, 3357 1.1 christos ppc64v2_emit_reg, 3358 1.1 christos ppc64_emit_pop, 3359 1.1 christos ppc64_emit_stack_flush, 3360 1.1 christos ppc64_emit_zero_ext, 3361 1.1 christos ppc64_emit_swap, 3362 1.1 christos ppc_emit_stack_adjust, 3363 1.1 christos ppc64v2_emit_int_call_1, 3364 1.1 christos ppc64v2_emit_void_call_2, 3365 1.1 christos ppc64_emit_eq_goto, 3366 1.1 christos ppc64_emit_ne_goto, 3367 1.1 christos ppc64_emit_lt_goto, 3368 1.1 christos ppc64_emit_le_goto, 3369 1.1 christos ppc64_emit_gt_goto, 3370 1.1 christos ppc64_emit_ge_goto 3371 1.1 christos }; 3372 1.1 christos 3373 1.1 christos #endif 3374 1.1 christos 3375 1.1 christos /* Implementation of target ops method "emit_ops". */ 3376 1.1 christos 3377 1.1 christos emit_ops * 3378 1.1 christos ppc_target::emit_ops () 3379 1.1 christos { 3380 1.1 christos #ifdef __powerpc64__ 3381 1.1.1.4 christos if (register_size (current_process ()->tdesc, 0) == 8) 3382 1.1 christos { 3383 1.1 christos if (is_elfv2_inferior ()) 3384 1.1.1.2 christos return &ppc64v2_emit_ops_impl; 3385 1.1 christos else 3386 1.1.1.2 christos return &ppc64v1_emit_ops_impl; 3387 1.1 christos } 3388 1.1 christos #endif 3389 1.1 christos return &ppc_emit_ops_impl; 3390 1.1 christos } 3391 1.1 christos 3392 1.1 christos /* Implementation of target ops method "get_ipa_tdesc_idx". */ 3393 1.1 christos 3394 1.1 christos int 3395 1.1 christos ppc_target::get_ipa_tdesc_idx () 3396 1.1 christos { 3397 1.1.1.4 christos const target_desc *tdesc = current_process ()->tdesc; 3398 1.1 christos 3399 1.1 christos #ifdef __powerpc64__ 3400 1.1 christos if (tdesc == tdesc_powerpc_64l) 3401 1.1 christos return PPC_TDESC_BASE; 3402 1.1 christos if (tdesc == tdesc_powerpc_altivec64l) 3403 1.1 christos return PPC_TDESC_ALTIVEC; 3404 1.1 christos if (tdesc == tdesc_powerpc_vsx64l) 3405 1.1 christos return PPC_TDESC_VSX; 3406 1.1 christos if (tdesc == tdesc_powerpc_isa205_64l) 3407 1.1 christos return PPC_TDESC_ISA205; 3408 1.1 christos if (tdesc == tdesc_powerpc_isa205_altivec64l) 3409 1.1 christos return PPC_TDESC_ISA205_ALTIVEC; 3410 1.1 christos if (tdesc == tdesc_powerpc_isa205_vsx64l) 3411 1.1 christos return PPC_TDESC_ISA205_VSX; 3412 1.1 christos if (tdesc == tdesc_powerpc_isa205_ppr_dscr_vsx64l) 3413 1.1 christos return PPC_TDESC_ISA205_PPR_DSCR_VSX; 3414 1.1 christos if (tdesc == tdesc_powerpc_isa207_vsx64l) 3415 1.1 christos return PPC_TDESC_ISA207_VSX; 3416 1.1 christos if (tdesc == tdesc_powerpc_isa207_htm_vsx64l) 3417 1.1 christos return PPC_TDESC_ISA207_HTM_VSX; 3418 1.1 christos #endif 3419 1.1 christos 3420 1.1 christos if (tdesc == tdesc_powerpc_32l) 3421 1.1 christos return PPC_TDESC_BASE; 3422 1.1 christos if (tdesc == tdesc_powerpc_altivec32l) 3423 1.1 christos return PPC_TDESC_ALTIVEC; 3424 1.1 christos if (tdesc == tdesc_powerpc_vsx32l) 3425 1.1 christos return PPC_TDESC_VSX; 3426 1.1 christos if (tdesc == tdesc_powerpc_isa205_32l) 3427 1.1 christos return PPC_TDESC_ISA205; 3428 1.1 christos if (tdesc == tdesc_powerpc_isa205_altivec32l) 3429 1.1 christos return PPC_TDESC_ISA205_ALTIVEC; 3430 1.1 christos if (tdesc == tdesc_powerpc_isa205_vsx32l) 3431 1.1 christos return PPC_TDESC_ISA205_VSX; 3432 1.1 christos if (tdesc == tdesc_powerpc_isa205_ppr_dscr_vsx32l) 3433 1.1 christos return PPC_TDESC_ISA205_PPR_DSCR_VSX; 3434 1.1 christos if (tdesc == tdesc_powerpc_isa207_vsx32l) 3435 1.1 christos return PPC_TDESC_ISA207_VSX; 3436 1.1 christos if (tdesc == tdesc_powerpc_isa207_htm_vsx32l) 3437 1.1 christos return PPC_TDESC_ISA207_HTM_VSX; 3438 1.1 christos if (tdesc == tdesc_powerpc_e500l) 3439 1.1 christos return PPC_TDESC_E500; 3440 1.1 christos 3441 1.1 christos return 0; 3442 1.1 christos } 3443 1.1 christos 3444 1.1 christos /* The linux target ops object. */ 3445 1.1 christos 3446 1.1 christos linux_process_target *the_linux_target = &the_ppc_target; 3447 1.1 christos 3448 1.1 christos void 3449 1.1 christos initialize_low_arch (void) 3450 1.1 christos { 3451 1.1 christos /* Initialize the Linux target descriptions. */ 3452 1.1 christos 3453 1.1 christos init_registers_powerpc_32l (); 3454 1.1 christos init_registers_powerpc_altivec32l (); 3455 1.1 christos init_registers_powerpc_vsx32l (); 3456 1.1 christos init_registers_powerpc_isa205_32l (); 3457 1.1 christos init_registers_powerpc_isa205_altivec32l (); 3458 1.1 christos init_registers_powerpc_isa205_vsx32l (); 3459 1.1 christos init_registers_powerpc_isa205_ppr_dscr_vsx32l (); 3460 1.1 christos init_registers_powerpc_isa207_vsx32l (); 3461 1.1 christos init_registers_powerpc_isa207_htm_vsx32l (); 3462 1.1 christos init_registers_powerpc_e500l (); 3463 1.1 christos #if __powerpc64__ 3464 1.1 christos init_registers_powerpc_64l (); 3465 1.1 christos init_registers_powerpc_altivec64l (); 3466 1.1 christos init_registers_powerpc_vsx64l (); 3467 1.1 christos init_registers_powerpc_isa205_64l (); 3468 1.1 christos init_registers_powerpc_isa205_altivec64l (); 3469 1.1 christos init_registers_powerpc_isa205_vsx64l (); 3470 1.1 christos init_registers_powerpc_isa205_ppr_dscr_vsx64l (); 3471 1.1 christos init_registers_powerpc_isa207_vsx64l (); 3472 1.1 christos init_registers_powerpc_isa207_htm_vsx64l (); 3473 1.1 christos #endif 3474 1.1 christos 3475 1.1 christos initialize_regsets_info (&ppc_regsets_info); 3476 1.1 christos } 3477