2 /*--------------------------------------------------------------------*/
3 /*--- The core dispatch loop, for jumping to a code address. ---*/
4 /*--- dispatch-ppc32-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_ppc32_linux)
35 #include "pub_core_dispatch_asm.h"
36 #include "pub_core_transtab_asm.h"
37 #include "libvex_guest_offsets.h" /* for OFFSET_ppc32_CIA */
40 /*------------------------------------------------------------*/
42 /*--- The dispatch loop. VG_(disp_run_translations) is ---*/
43 /*--- used to run all translations, ---*/
44 /*--- including no-redir ones. ---*/
46 /*------------------------------------------------------------*/
48 /*----------------------------------------------------*/
49 /*--- Entry and preamble (set everything up) ---*/
50 /*----------------------------------------------------*/
53 void VG_(disp_run_translations)( UWord* two_words,
58 .globl VG_(disp_run_translations)
59 .type VG_(disp_run_translations), @function
60 VG_(disp_run_translations):
61 /* r3 holds two_words */
62 /* r4 holds guest_state */
63 /* r5 holds host_addr */
65 /* ----- entry point to VG_(disp_run_translations) ----- */
66 /* For Linux/ppc32 we need the SysV ABI, which uses
67 LR->4(parent_sp), CR->anywhere.
68 (The AIX ABI, used on Darwin,
69 uses LR->8(prt_sp), CR->4(prt_sp))
77 stwu 1,-496(1) /* sp should maintain 16-byte alignment */
79 /* Save callee-saved registers... */
80 /* r3, r4, r5 are live here, so use r6 */
81 lis 6,VG_(machine_ppc32_has_FP)@ha
82 lwz 6,VG_(machine_ppc32_has_FP)@l(6)
86 /* Floating-point reg save area : 144 bytes */
107 /* General reg save area : 76 bytes */
127 stw 3,272(1) /* save two_words for later */
129 /* It's necessary to save/restore VRSAVE in the AIX / Darwin ABI.
130 The Linux kernel might not actually use VRSAVE for its intended
131 purpose, but it should be harmless to preserve anyway. */
132 /* r3, r4, r5 are live here, so use r6 */
133 lis 6,VG_(machine_ppc32_has_VMX)@ha
134 lwz 6,VG_(machine_ppc32_has_VMX)@l(6)
139 /* VRSAVE save word : 32 bytes */
140 mfspr 6,256 /* vrsave reg is spr number 256 */
143 /* Alignment padding : 4 bytes */
145 /* Vector reg save area (quadword aligned) : 192 bytes */
178 /* Local variable space... */
180 /* 32(sp) used later to check FPSCR[RM] */
182 /* r3 holds two_words */
183 /* r4 holds guest_state */
184 /* r5 holds host_addr */
186 /* 24(sp) used later to stop ctr reg being clobbered */
187 /* 20(sp) used later to load fpscr with zero */
190 /* Linkage Area (reserved)
195 /* set host FPU control word to the default mode expected
196 by VEX-generated code. See comments in libvex.h for
198 lis 6,VG_(machine_ppc32_has_FP)@ha
199 lwz 6,VG_(machine_ppc32_has_FP)@l(6)
203 /* get zero into f3 (tedious) */
204 /* note: fsub 3,3,3 is not a reliable way to do this,
205 since if f3 holds a NaN or similar then we don't necessarily
206 wind up with zero. */
210 mtfsf 0xFF,3 /* fpscr = f3 */
213 /* set host AltiVec control word to the default mode expected
214 by VEX-generated code. */
215 lis 6,VG_(machine_ppc32_has_VMX)@ha
216 lwz 6,VG_(machine_ppc32_has_VMX)@l(6)
221 vspltisw 3,0x0 /* generate zero */
227 /* make a stack frame for the code we are calling */
230 /* Set up the guest state ptr */
231 mr 31,4 /* r31 (generated code gsp) = r4 */
233 /* and jump into the code cache. Chained translations in
234 the code cache run, until for whatever reason, they can't
235 continue. When that happens, the translation in question
236 will jump (or call) to one of the continuation points
237 VG_(cp_...) below. */
242 /*----------------------------------------------------*/
243 /*--- Postamble and exit. ---*/
244 /*----------------------------------------------------*/
247 /* At this point, r6 and r7 contain two
248 words to be returned to the caller. r6
249 holds a TRC value, and r7 optionally may
250 hold another word (for CHAIN_ME exits, the
251 address of the place to patch.) */
253 /* We're leaving. Check that nobody messed with
254 VSCR or FPSCR in ways we don't expect. */
255 /* Using r10 - value used again further on, so don't trash! */
256 lis 10,VG_(machine_ppc32_has_FP)@ha
257 lwz 10,VG_(machine_ppc32_has_FP)@l(10)
259 /* Using r11 - value used again further on, so don't trash! */
260 lis 11,VG_(machine_ppc32_has_VMX)@ha
261 lwz 11,VG_(machine_ppc32_has_VMX)@l(11)
263 cmplwi 10,0 /* Do we have FP ? */
266 /* Set fpscr back to a known state, since vex-generated code
267 may have messed with fpscr[rm]. */
273 mtfsf 0xFF,3 /* fpscr = f3 */
276 cmplwi 11,0 /* Do we have altivec? */
280 /* Check VSCR[NJ] == 1 */
281 /* first generate 4x 0x00010000 */
282 vspltisw 4,0x1 /* 4x 0x00000001 */
283 vspltisw 5,0x0 /* zero */
284 vsldoi 6,4,5,0x2 /* <<2*8 => 4x 0x00010000 */
285 /* retrieve VSCR and mask wanted bits */
287 vand 7,7,6 /* gives NJ flag */
288 vspltw 7,7,0x3 /* flags-word to all lanes */
289 vcmpequw. 8,6,7 /* CR[24] = 1 if v6 == v7 */
290 bt 24,invariant_violation /* branch if all_equal */
294 /* otherwise we're OK */
298 li 6,VG_TRC_INVARIANT_FAILED
303 /* Restore FP regs */
304 /* r10 already holds VG_(machine_ppc32_has_FP) value */
308 /* Floating-point regs */
329 /* r11 already holds VG_(machine_ppc32_has_VMX) value */
333 /* Restore Altivec regs */
337 mfspr 4,256 /* VRSAVE reg is spr number 256 */
367 /* restore int regs, including importantly r3 (two_words) */
389 /* Stash return values */
393 /* restore lr & sp, and leave */
394 lwz 0,500(1) /* stack_size + 4 */
396 addi 1,1,496 /* stack_size */
400 /*----------------------------------------------------*/
401 /*--- Continuation points ---*/
402 /*----------------------------------------------------*/
404 /* ------ Chain me to slow entry point ------ */
405 .global VG_(disp_cp_chain_me_to_slowEP)
406 VG_(disp_cp_chain_me_to_slowEP):
407 /* We got called. The return address indicates
408 where the patching needs to happen. Collect
409 the return address and, exit back to C land,
410 handing the caller the pair (Chain_me_S, RA) */
411 li 6, VG_TRC_CHAIN_ME_TO_SLOW_EP
413 /* 8 = imm32-fixed2 r30, disp_cp_chain_me_to_slowEP
420 /* ------ Chain me to fast entry point ------ */
421 .global VG_(disp_cp_chain_me_to_fastEP)
422 VG_(disp_cp_chain_me_to_fastEP):
423 /* We got called. The return address indicates
424 where the patching needs to happen. Collect
425 the return address and, exit back to C land,
426 handing the caller the pair (Chain_me_S, RA) */
427 li 6, VG_TRC_CHAIN_ME_TO_FAST_EP
429 /* 8 = imm32-fixed2 r30, disp_cp_chain_me_to_fastEP
436 /* ------ Indirect but boring jump ------ */
437 .global VG_(disp_cp_xindir)
439 /* Where are we going? */
440 lwz 3,OFFSET_ppc32_CIA(31)
443 lis 5,VG_(stats__n_xindirs_32)@ha
444 addi 5,5,VG_(stats__n_xindirs_32)@l
449 /* r5 = &VG_(tt_fast) */
450 lis 5,VG_(tt_fast)@ha
451 addi 5,5,VG_(tt_fast)@l /* & VG_(tt_fast) */
453 /* try a fast lookup in the translation cache */
454 /* r4 = VG_TT_FAST_HASH(addr) * sizeof(FastCacheEntry)
455 = ((r3 >>u 2) & VG_TT_FAST_MASK) << 3 */
456 rlwinm 4,3,1, 29-VG_TT_FAST_BITS, 28 /* entry# * 8 */
457 add 5,5,4 /* & VG_(tt_fast)[entry#] */
458 lwz 6,0(5) /* .guest */
459 lwz 7,4(5) /* .host */
461 bne fast_lookup_failed
463 /* Found a match. Jump to .host. */
469 lis 5,VG_(stats__n_xindir_misses_32)@ha
470 addi 5,5,VG_(stats__n_xindir_misses_32)@l
475 li 6,VG_TRC_INNER_FASTMISS
480 /* ------ Assisted jump ------ */
481 .global VG_(disp_cp_xassisted)
482 VG_(disp_cp_xassisted):
483 /* r31 contains the TRC */
488 /* ------ Event check failed ------ */
489 .global VG_(disp_cp_evcheck_fail)
490 VG_(disp_cp_evcheck_fail):
491 li 6,VG_TRC_INNER_COUNTERZERO
496 .size VG_(disp_run_translations), .-VG_(disp_run_translations)
498 #endif // defined(VGP_ppc32_linux)
500 /* Let the linker know we don't need an executable stack */
503 /*--------------------------------------------------------------------*/
505 /*--------------------------------------------------------------------*/