2 /*--------------------------------------------------------------------*/
3 /*--- The core dispatch loop, for jumping to a code address. ---*/
4 /*--- dispatch-ppc64-linux.S ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2005-2017 Cerion Armour-Brown <cerion@open-works.co.uk>
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics_asm.h"
33 #if defined(VGP_ppc64be_linux)
35 #include "pub_core_dispatch_asm.h"
36 #include "pub_core_transtab_asm.h"
37 #include "libvex_guest_offsets.h" /* for OFFSET_ppc64_CIA */
40 /* References to globals via the TOC */
43 .globl vgPlain_tt_fast
44 .lcomm vgPlain_tt_fast,4,4
45 .type vgPlain_tt_fast, @object
48 .tocent__vgPlain_tt_fast:
49 .tc vgPlain_tt_fast[TC],vgPlain_tt_fast
50 .tocent__vgPlain_stats__n_xindirs_32:
51 .tc vgPlain_stats__n_xindirs_32[TC],vgPlain_stats__n_xindirs_32
52 .tocent__vgPlain_stats__n_xindir_misses_32:
53 .tc vgPlain_stats__n_xindir_misses_32[TC],vgPlain_stats__n_xindir_misses_32
54 .tocent__vgPlain_machine_ppc64_has_VMX:
55 .tc vgPlain_machine_ppc64_has_VMX[TC],vgPlain_machine_ppc64_has_VMX
57 /*------------------------------------------------------------*/
59 /*--- The dispatch loop. VG_(disp_run_translations) is ---*/
60 /*--- used to run all translations, ---*/
61 /*--- including no-redir ones. ---*/
63 /*------------------------------------------------------------*/
65 /*----------------------------------------------------*/
66 /*--- Entry and preamble (set everything up) ---*/
67 /*----------------------------------------------------*/
70 void VG_(disp_run_translations)( UWord* two_words,
77 .globl VG_(disp_run_translations)
80 VG_(disp_run_translations):
81 .quad .VG_(disp_run_translations),.TOC.@tocbase,0
83 .type .VG_(disp_run_translations),@function
84 .globl .VG_(disp_run_translations)
85 .VG_(disp_run_translations):
86 /* r3 holds two_words */
87 /* r4 holds guest_state */
88 /* r5 holds host_addr */
90 /* ----- entry point to VG_(disp_run_translations) ----- */
91 /* PPC64 ABI saves LR->16(prt_sp), CR->8(prt_sp)) */
100 stdu 1,-624(1) /* sp should maintain 16-byte alignment */
102 /* General reg save area : 152 bytes */
122 std 3,104(1) /* save two_words for later */
124 /* Save callee-saved registers... */
125 /* Floating-point reg save area : 144 bytes */
145 /* It's necessary to save/restore VRSAVE in the AIX / Darwin ABI.
146 The Linux kernel might not actually use VRSAVE for its intended
147 purpose, but it should be harmless to preserve anyway. */
148 /* r3, r4, r5 are live here, so use r6 */
149 ld 6,.tocent__vgPlain_machine_ppc64_has_VMX@toc(2)
154 /* VRSAVE save word : 32 bytes */
155 mfspr 6,256 /* vrsave reg is spr number 256 */
158 /* Alignment padding : 4 bytes */
160 /* Vector reg save area (quadword aligned) : 192 bytes */
187 /* Local variable space... */
189 /* r3 holds two_words */
190 /* r4 holds guest_state */
191 /* r5 holds host_addr */
193 /* 96(sp) used later to check FPSCR[RM] */
194 /* 88(sp) used later to load fpscr with zero */
197 /* Linkage Area (reserved) BE ABI
199 32(sp) : link editor doubleword
200 24(sp) : compiler doubleword
206 /* set host FPU control word to the default mode expected
207 by VEX-generated code. See comments in libvex.h for
209 /* => get zero into f3 (tedious)
210 fsub 3,3,3 is not a reliable way to do this, since if
211 f3 holds a NaN or similar then we don't necessarily
212 wind up with zero. */
216 mtfsf 0xFF,3 /* fpscr = lo32 of f3 */
218 /* set host AltiVec control word to the default mode expected
219 by VEX-generated code. */
220 ld 6,.tocent__vgPlain_machine_ppc64_has_VMX@toc(2)
225 vspltisw 3,0x0 /* generate zero */
229 /* make a stack frame for the code we are calling */
232 /* Set up the guest state ptr */
233 mr 31,4 /* r31 (generated code gsp) = r4 */
235 /* and jump into the code cache. Chained translations in
236 the code cache run, until for whatever reason, they can't
237 continue. When that happens, the translation in question
238 will jump (or call) to one of the continuation points
239 VG_(cp_...) below. */
244 /*----------------------------------------------------*/
245 /*--- Postamble and exit. ---*/
246 /*----------------------------------------------------*/
249 /* At this point, r6 and r7 contain two
250 words to be returned to the caller. r6
251 holds a TRC value, and r7 optionally may
252 hold another word (for CHAIN_ME exits, the
253 address of the place to patch.) */
255 /* undo the "make a stack frame for the code we are calling" */
258 /* We're leaving. Check that nobody messed with
259 VSCR or FPSCR in ways we don't expect. */
260 /* Using r11 - value used again further on, so don't trash! */
261 ld 11,.tocent__vgPlain_machine_ppc64_has_VMX@toc(2)
264 /* Set fpscr back to a known state, since vex-generated code
265 may have messed with fpscr[rm]. */
271 mtfsf 0xFF,3 /* fpscr = f3 */
273 cmpldi 11,0 /* Do we have altivec? */
276 /* Check VSCR[NJ] == 1 */
277 /* first generate 4x 0x00010000 */
278 vspltisw 4,0x1 /* 4x 0x00000001 */
279 vspltisw 5,0x0 /* zero */
280 vsldoi 6,4,5,0x2 /* <<2*8 => 4x 0x00010000 */
281 /* retrieve VSCR and mask wanted bits */
283 vand 7,7,6 /* gives NJ flag */
284 vspltw 7,7,0x3 /* flags-word to all lanes */
285 vcmpequw. 8,6,7 /* CR[24] = 1 if v6 == v7 */
286 bt 24,.invariant_violation /* branch if all_equal */
289 /* otherwise we're OK */
292 .invariant_violation:
293 li 6,VG_TRC_INVARIANT_FAILED
298 /* r11 already holds VG_(machine_ppc32_has_VMX) value */
302 /* Restore Altivec regs.
303 Use r5 as scratch since r6/r7 are live. */
306 mfspr 5,256 /* VRSAVE reg is spr number 256 */
335 /* Restore FP regs */
336 /* Floating-point regs */
356 /* restore int regs, including importantly r3 (two_words) */
377 /* Stash return values */
381 /* restore lr & sp, and leave */
382 ld 0,632(1) /* stack_size + 8 */
384 ld 0,640(1) /* stack_size + 16 */
386 addi 1,1,624 /* stack_size */
390 /*----------------------------------------------------*/
391 /*--- Continuation points ---*/
392 /*----------------------------------------------------*/
394 /* ------ Chain me to slow entry point ------ */
397 .globl VG_(disp_cp_chain_me_to_slowEP)
400 VG_(disp_cp_chain_me_to_slowEP):
401 .quad .VG_(disp_cp_chain_me_to_slowEP),.TOC.@tocbase,0
403 .type .VG_(disp_cp_chain_me_to_slowEP),@function
404 .globl .VG_(disp_cp_chain_me_to_slowEP)
405 .VG_(disp_cp_chain_me_to_slowEP):
406 /* We got called. The return address indicates
407 where the patching needs to happen. Collect
408 the return address and, exit back to C land,
409 handing the caller the pair (Chain_me_S, RA) */
410 li 6, VG_TRC_CHAIN_ME_TO_SLOW_EP
412 /* 20 = imm64-fixed5 r30, disp_cp_chain_me_to_slowEP
419 /* ------ Chain me to fast entry point ------ */
422 .globl VG_(disp_cp_chain_me_to_fastEP)
425 VG_(disp_cp_chain_me_to_fastEP):
426 .quad .VG_(disp_cp_chain_me_to_fastEP),.TOC.@tocbase,0
428 .type .VG_(disp_cp_chain_me_to_fastEP),@function
429 .globl .VG_(disp_cp_chain_me_to_fastEP)
430 .VG_(disp_cp_chain_me_to_fastEP):
431 /* We got called. The return address indicates
432 where the patching needs to happen. Collect
433 the return address and, exit back to C land,
434 handing the caller the pair (Chain_me_S, RA) */
435 li 6, VG_TRC_CHAIN_ME_TO_FAST_EP
437 /* 20 = imm64-fixed5 r30, disp_cp_chain_me_to_fastEP
444 /* ------ Indirect but boring jump ------ */
447 .globl VG_(disp_cp_xindir)
451 .quad .VG_(disp_cp_xindir),.TOC.@tocbase,0
453 .type .VG_(disp_cp_xindir),@function
454 .globl .VG_(disp_cp_xindir)
455 .VG_(disp_cp_xindir):
456 /* Where are we going? */
457 ld 3,OFFSET_ppc64_CIA(31)
460 ld 5, .tocent__vgPlain_stats__n_xindirs_32@toc(2)
465 /* r5 = &VG_(tt_fast) */
466 ld 5, .tocent__vgPlain_tt_fast@toc(2) /* &VG_(tt_fast) */
468 /* try a fast lookup in the translation cache */
469 /* r4 = VG_TT_FAST_HASH(addr) * sizeof(FastCacheEntry)
470 = ((r3 >>u 2) & VG_TT_FAST_MASK) << 4 */
471 rldicl 4,3, 62, 64-VG_TT_FAST_BITS /* entry# */
472 sldi 4,4,4 /* entry# * sizeof(FastCacheEntry) */
473 add 5,5,4 /* & VG_(tt_fast)[entry#] */
474 ld 6,0(5) /* .guest */
475 ld 7,8(5) /* .host */
477 bne .fast_lookup_failed
479 /* Found a match. Jump to .host. */
485 ld 5, .tocent__vgPlain_stats__n_xindir_misses_32@toc(2)
490 li 6,VG_TRC_INNER_FASTMISS
495 /* ------ Assisted jump ------ */
498 .globl VG_(disp_cp_xassisted)
501 VG_(disp_cp_xassisted):
502 .quad .VG_(disp_cp_xassisted),.TOC.@tocbase,0
504 .type .VG_(disp_cp_xassisted),@function
505 .globl .VG_(disp_cp_xassisted)
506 .VG_(disp_cp_xassisted):
507 /* r31 contains the TRC */
512 /* ------ Event check failed ------ */
515 .globl VG_(disp_cp_evcheck_fail)
518 VG_(disp_cp_evcheck_fail):
519 .quad .VG_(disp_cp_evcheck_fail),.TOC.@tocbase,0
521 .type .VG_(disp_cp_evcheck_fail),@function
522 .globl .VG_(disp_cp_evcheck_fail)
523 .VG_(disp_cp_evcheck_fail):
524 li 6,VG_TRC_INNER_COUNTERZERO
529 .size .VG_(disp_run_translations), .-.VG_(disp_run_translations)
531 #endif // defined(VGP_ppc64be_linux)
533 /* Let the linker know we don't need an executable stack */
536 /*--------------------------------------------------------------------*/
538 /*--------------------------------------------------------------------*/