2 /*--------------------------------------------------------------------*/
3 /*--- The core dispatch loop, for jumping to a code address. ---*/
4 /*--- dispatch-s390x-linux.S ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright IBM Corp. 2010-2011
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 /* Contributed by Florian Krohm and Christian Borntraeger */
33 #include "pub_core_basics_asm.h"
34 #include "pub_core_dispatch_asm.h"
35 #include "pub_core_transtab_asm.h"
36 #include "libvex_guest_offsets.h"
37 #include "libvex_s390x_common.h"
39 #if defined(VGA_s390x)
41 /*------------------------------------------------------------*/
43 /*--- The dispatch loop. VG_(run_innerloop) is used to ---*/
44 /*--- run all translations except no-redir ones. ---*/
46 /*------------------------------------------------------------*/
48 /* Convenience definitions for readability */
50 #define SP S390_REGNO_STACK_POINTER
53 #define LR S390_REGNO_LINK_REGISTER
55 /* Location of valgrind's saved FPC register */
56 #define S390_LOC_SAVED_FPC_V S390_OFFSET_SAVED_FPC_V(SP)
58 /* Location of saved guest state pointer */
59 #define S390_LOC_SAVED_GSP S390_OFFSET_SAVED_GSP(SP)
61 /*----------------------------------------------------*/
62 /*--- Preamble (set everything up) ---*/
63 /*----------------------------------------------------*/
66 UWord VG_(run_innerloop) ( void* guest_state, UWord do_profiling );
71 .globl VG_(run_innerloop)
73 /* r2 holds address of guest_state */
74 /* r3 holds do_profiling (a flag) */
76 /* Save gprs ABI: r6...r13 and r15 */
80 aghi SP,-S390_INNERLOOP_FRAME_SIZE
82 /* Save fprs: ABI: f8...f15 */
92 /* Load address of guest state into guest state register (r13) */
95 /* Store address of guest state pointer on stack.
96 It will be needed later because upon return from a VEX translation
97 r13 may contain a special value. So the old value will be used to
98 determine whether r13 contains a special value. */
99 stg %r13,S390_LOC_SAVED_GSP
101 /* Save valgrind's FPC on stack so run_innerloop_exit can restore
103 stfpc S390_LOC_SAVED_FPC_V
105 /* Load the FPC the way the client code wants it. I.e. pull the
106 value from the guest state.
107 lfpc OFFSET_s390x_fpc(%r13)
109 /* Get the IA from the guest state */
110 lg %r2,OFFSET_s390x_IA(%r13)
112 /* Get VG_(dispatch_ctr) -- a 32-bit value -- and store it in a reg */
113 larl %r6,VG_(dispatch_ctr)
114 l S390_REGNO_DISPATCH_CTR,0(%r6)
116 /* Fall into main loop (the right one) */
118 /* r3 = 1 --> do_profiling. We may trash r3 later on. That's OK,
119 because it's a volatile register (does not need to be preserved). */
121 je run_innerloop__dispatch_unprofiled
122 j run_innerloop__dispatch_profiled
124 /*----------------------------------------------------*/
125 /*--- NO-PROFILING (standard) dispatcher ---*/
126 /*----------------------------------------------------*/
128 run_innerloop__dispatch_unprofiled:
129 /* This is the story:
131 r2 = IA = next guest address
132 r12 = VG_(dispatch_ctr)
133 r13 = guest state pointer or (upon return from guest code) some
135 r15 = stack pointer (as usual)
138 /* Has the guest state pointer been messed with? If yes, exit. */
139 cg %r13,S390_LOC_SAVED_GSP /* r13 = actual guest state pointer */
140 larl %r8, VG_(tt_fast)
143 /* Save the jump address in the guest state */
144 stg %r2,OFFSET_s390x_IA(%r13)
147 /* Try a fast lookup in the translation cache:
148 Compute offset (not index) into VT_(tt_fast):
150 offset = VG_TT_FAST_HASH(addr) * sizeof(FastCacheEntry)
152 with VG_TT_FAST_HASH(addr) == (addr >> 1) & VG_TT_FAST_MASK
153 and sizeof(FastCacheEntry) == 16
155 offset = ((addr >> 1) & VG_TT_FAST_MASK) << 4
157 lghi %r5,VG_TT_FAST_MASK
158 srlg %r7, %r2,1 /* next guest addr >> 1*/
162 /* Set the return address to the beginning of the loop here to
163 have some instruction between setting r7 and using it as an
165 larl LR,run_innerloop__dispatch_unprofiled
167 /* Are we out of timeslice? If yes, defer to scheduler. */
168 ahi S390_REGNO_DISPATCH_CTR,-1
172 lg %r10, 0(%r8,%r7) /* .guest */
173 lg %r11, 8(%r8,%r7) /* .host */
175 jne fast_lookup_failed
177 /* Found a match. Call .host.
178 r11 is an address. There we will find the instrumented client code.
179 That code may modify the guest state register r13. The client code
180 will return to the beginning of this loop start by issuing br LR.
181 We can simply branch to the host code */
185 /*----------------------------------------------------*/
186 /*--- PROFILING dispatcher (can be much slower) ---*/
187 /*----------------------------------------------------*/
189 run_innerloop__dispatch_profiled:
191 /* Has the guest state pointer been messed with? If yes, exit. */
192 cg %r13,S390_LOC_SAVED_GSP /* r13 = actual guest state pointer */
193 larl %r8, VG_(tt_fast)
196 /* Save the jump address in the guest state */
197 stg %r2,OFFSET_s390x_IA(%r13)
199 /* Try a fast lookup in the translation cache:
200 Compute offset (not index) into VT_(tt_fast):
202 offset = VG_TT_FAST_HASH(addr) * sizeof(FastCacheEntry)
204 with VG_TT_FAST_HASH(addr) == (addr >> 1) & VG_TT_FAST_MASK
205 and sizeof(FastCacheEntry) == 16
207 offset = ((addr >> 1) & VG_TT_FAST_MASK) << 4
209 lghi %r5,VG_TT_FAST_MASK
210 srlg %r7,%r2,1 /* next guest addr >> 1*/
214 /* Set the return address to the beginning of the loop here to
215 have some instruction between setting r7 and using it as an
217 larl LR,run_innerloop__dispatch_profiled
219 /* Are we out of timeslice? If yes, defer to scheduler. */
220 ahi S390_REGNO_DISPATCH_CTR,-1
223 lg %r10, 0(%r8,%r7) /* .guest */
224 lg %r11, 8(%r8,%r7) /* .host */
226 jne fast_lookup_failed
228 /* sizeof(FastCacheEntry) == 16, sizeof(*UInt)==8 */
231 /* we got a hit: VG_(tt_fastN) is guaranteed to point to count */
232 larl %r8, VG_(tt_fastN)
234 /* increment bb profile counter */
240 /* Found a match. Call .host.
241 r11 is an address. There we will find the instrumented client code.
242 That code may modify the guest state register r13. The client code
243 will return to the beginning of this loop start by issuing br LR.
244 We can simply branch to the host code */
247 /*----------------------------------------------------*/
248 /*--- exit points ---*/
249 /*----------------------------------------------------*/
252 /* Someone messed with the gsp (in r13). Have to
253 defer to scheduler to resolve this. The register
254 holding VG_(dispatch_ctr) is not yet decremented,
255 so no need to increment. */
257 /* Update the IA in the guest state */
258 lg %r6,S390_LOC_SAVED_GSP /* r6 = original guest state pointer */
259 stg %r2,OFFSET_s390x_IA(%r6)
261 /* Return the special guest state pointer value */
267 /* IA is up to date */
269 /* Back out decrement of the dispatch counter */
270 ahi S390_REGNO_DISPATCH_CTR,1
272 /* Set return value for the scheduler */
273 lghi %r2,VG_TRC_INNER_COUNTERZERO
278 /* IA is up to date */
280 /* Back out decrement of the dispatch counter */
281 ahi S390_REGNO_DISPATCH_CTR,1
283 /* Set return value for the scheduler */
284 lghi %r2,VG_TRC_INNER_FASTMISS
288 /* All exits from the dispatcher go through here.
289 When we come here r2 holds the return value. */
292 /* Restore valgrind's FPC, as client code may have changed it. */
293 lfpc S390_LOC_SAVED_FPC_V
295 /* Write ctr to VG_(dispatch_ctr) (=32bit value) */
296 larl %r6,VG_(dispatch_ctr)
297 st S390_REGNO_DISPATCH_CTR,0(%r6)
299 /* Restore callee-saved registers... */
301 /* Floating-point regs */
311 /* Remove atack frame */
312 aghi SP,S390_INNERLOOP_FRAME_SIZE
314 /* General-purpose regs. This also restores the original link
315 register (r14) and stack pointer (r15). */
321 /*------------------------------------------------------------*/
323 /*--- A special dispatcher, for running no-redir ---*/
324 /*--- translations. Just runs the given translation once. ---*/
326 /*------------------------------------------------------------*/
329 void VG_(run_a_noredir_translation) ( UWord* argblock );
332 /* Run a no-redir translation. argblock points to 4 UWords, 2 to carry args
333 and 2 to carry results:
334 0: input: ptr to translation
335 1: input: ptr to guest state
336 2: output: next guest PC
337 3: output: guest state pointer afterwards (== thread return code)
341 .globl VG_(run_a_noredir_translation)
342 VG_(run_a_noredir_translation):
344 aghi SP,-S390_INNERLOOP_FRAME_SIZE
354 /* Load address of guest state into guest state register (r13) */
360 /* save r2 (argblock) as it is clobbered */
363 /* the call itself */
366 /* restore argblock */
368 /* save the next guest PC */
371 /* save the guest state */
374 /* Restore Floating-point regs */
384 aghi SP,S390_INNERLOOP_FRAME_SIZE
390 /* Let the linker know we don't need an executable stack */
391 .section .note.GNU-stack,"",@progbits
393 #endif /* VGA_s390x */
395 /*--------------------------------------------------------------------*/
396 /*--- end dispatch-s390x-linux.S ---*/
397 /*--------------------------------------------------------------------*/