Debian Codegraph @

gdbserver/linux-ppc-low.cc

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