2 /*--------------------------------------------------------------------*/
3 /*--- The core dispatch loop, for jumping to a code address. ---*/
4 /*--- dispatch-amd64-solaris.S ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2017 Julian Seward
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #include "pub_core_basics_asm.h"
32 #if defined(VGP_amd64_solaris)
34 #include "pub_core_dispatch_asm.h"
35 #include "pub_core_transtab_asm.h"
36 #include "libvex_guest_offsets.h" /* for OFFSET_amd64_RIP */
39 /*------------------------------------------------------------*/
41 /*--- The dispatch loop. VG_(disp_run_translations) is ---*/
42 /*--- used to run all translations, ---*/
43 /*--- including no-redir ones. ---*/
45 /*------------------------------------------------------------*/
47 /*----------------------------------------------------*/
48 /*--- Entry and preamble (set everything up) ---*/
49 /*----------------------------------------------------*/
52 void VG_(disp_run_translations)( UWord* two_words,
57 .globl VG_(disp_run_translations)
58 .type VG_(disp_run_translations), @function
59 VG_(disp_run_translations):
60 /* %rdi holds two_words */
61 /* %rsi holds guest_state */
62 /* %rdx holds host_addr */
66 /* Save integer registers, since this is a pseudo-function. */
81 /* %rdi must be saved last */
84 /* Get the host CPU in the state expected by generated code. */
86 /* set host FPU control word to the default mode expected
87 by VEX-generated code. See comments in libvex.h for
94 /* set host SSE control word to the default mode expected
95 by VEX-generated code. */
100 /* set dir flag to known value */
103 /* Set up the guest state pointer */
106 /* and jump into the code cache. Chained translations in
107 the code cache run, until for whatever reason, they can't
108 continue. When that happens, the translation in question
109 will jump (or call) to one of the continuation points
110 VG_(cp_...) below. */
114 /*----------------------------------------------------*/
115 /*--- Postamble and exit. ---*/
116 /*----------------------------------------------------*/
119 /* At this point, %rax and %rdx contain two
120 words to be returned to the caller. %rax
121 holds a TRC value, and %rdx optionally may
122 hold another word (for CHAIN_ME exits, the
123 address of the place to patch.) */
125 /* We're leaving. Check that nobody messed with %mxcsr
126 or %fpucw. We can't mess with %rax or %rdx here as they
127 hold the tentative return values, but any others are OK. */
128 #if !defined(ENABLE_INNER)
129 /* This check fails for self-hosting, so skip in that case */
133 popq %r15 /* get rid of the word without trashing %rflags */
134 jnz invariant_violation
138 andl $0xFFFFFFC0, (%rsp) /* mask out status flags */
141 jnz invariant_violation
142 /* otherwise we're OK */
145 movq $VG_TRC_INVARIANT_FAILED, %rax
149 /* Pop %rdi, stash return values */
153 /* Now pop everything else */
170 /*----------------------------------------------------*/
171 /*--- Continuation points ---*/
172 /*----------------------------------------------------*/
174 /* ------ Chain me to slow entry point ------ */
175 .global VG_(disp_cp_chain_me_to_slowEP)
176 VG_(disp_cp_chain_me_to_slowEP):
177 /* We got called. The return address indicates
178 where the patching needs to happen. Collect
179 the return address and, exit back to C land,
180 handing the caller the pair (Chain_me_S, RA) */
181 movq $VG_TRC_CHAIN_ME_TO_SLOW_EP, %rax
183 /* 10 = movabsq $VG_(disp_chain_me_to_slowEP), %r11;
188 /* ------ Chain me to fast entry point ------ */
189 .global VG_(disp_cp_chain_me_to_fastEP)
190 VG_(disp_cp_chain_me_to_fastEP):
191 /* We got called. The return address indicates
192 where the patching needs to happen. Collect
193 the return address and, exit back to C land,
194 handing the caller the pair (Chain_me_F, RA) */
195 movq $VG_TRC_CHAIN_ME_TO_FAST_EP, %rax
197 /* 10 = movabsq $VG_(disp_chain_me_to_fastEP), %r11;
202 /* ------ Indirect but boring jump ------ */
203 .global VG_(disp_cp_xindir)
205 /* Where are we going? */
206 movq OFFSET_amd64_RIP(%rbp), %rax // "guest"
209 addl $1, VG_(stats__n_xIndirs_32)
211 // LIVE: %rbp (guest state ptr), %rax (guest address to go to).
212 // We use 4 temporaries:
213 // %r9 (to point at the relevant FastCacheSet),
214 // %r10, %r11 and %r12 (scratch).
216 /* Try a fast lookup in the translation cache. This is pretty much
217 a handcoded version of VG_(lookupInFastCache). */
219 // Compute %r9 = VG_TT_FAST_HASH(guest)
220 movq %rax, %r9 // guest
221 shrq $VG_TT_FAST_BITS, %r9 // (guest >> VG_TT_FAST_BITS)
222 xorq %rax, %r9 // (guest >> VG_TT_FAST_BITS) ^ guest
223 andq $VG_TT_FAST_MASK, %r9 // setNo
225 // Compute %r9 = &VG_(tt_fast)[%r9]
226 shlq $VG_FAST_CACHE_SET_BITS, %r9 // setNo * sizeof(FastCacheSet)
227 movabsq $VG_(tt_fast), %r10 // &VG_(tt_fast)[0]
228 leaq (%r10, %r9), %r9 // &VG_(tt_fast)[setNo]
230 // LIVE: %rbp (guest state ptr), %rax (guest addr), %r9 (cache set)
232 cmpq %rax, FCS_g0(%r9) // cmp against .guest0
235 jmp *FCS_h0(%r9) // goto .host0
239 cmpq %rax, FCS_g1(%r9) // cmp against .guest1
241 // hit at way 1; swap upwards
243 addl $1, VG_(stats__n_xIndir_hits1_32)
244 movq FCS_g0(%r9), %r10 // r10 = old .guest0
245 movq FCS_h0(%r9), %r11 // r11 = old .host0
246 movq FCS_h1(%r9), %r12 // r12 = old .host1
247 movq %rax, FCS_g0(%r9) // new .guest0 = guest
248 movq %r12, FCS_h0(%r9) // new .host0 = old .host1
249 movq %r10, FCS_g1(%r9) // new .guest1 = old .guest0
250 movq %r11, FCS_h1(%r9) // new .host1 = old .host0
251 jmp *%r12 // goto old .host1 a.k.a. new .host0
255 cmpq %rax, FCS_g2(%r9) // cmp against .guest2
257 // hit at way 2; swap upwards
259 addl $1, VG_(stats__n_xIndir_hits2_32)
260 movq FCS_g1(%r9), %r10
261 movq FCS_h1(%r9), %r11
262 movq FCS_h2(%r9), %r12
263 movq %rax, FCS_g1(%r9)
264 movq %r12, FCS_h1(%r9)
265 movq %r10, FCS_g2(%r9)
266 movq %r11, FCS_h2(%r9)
271 cmpq %rax, FCS_g3(%r9) // cmp against .guest3
273 // hit at way 3; swap upwards
275 addl $1, VG_(stats__n_xIndir_hits3_32)
276 movq FCS_g2(%r9), %r10
277 movq FCS_h2(%r9), %r11
278 movq FCS_h3(%r9), %r12
279 movq %rax, FCS_g2(%r9)
280 movq %r12, FCS_h2(%r9)
281 movq %r10, FCS_g3(%r9)
282 movq %r11, FCS_h3(%r9)
286 4: // fast lookup failed
288 addl $1, VG_(stats__n_xIndir_misses_32)
290 movq $VG_TRC_INNER_FASTMISS, %rax
294 /* ------ Assisted jump ------ */
295 .global VG_(disp_cp_xassisted)
296 VG_(disp_cp_xassisted):
297 /* %rbp contains the TRC */
302 /* ------ Event check failed ------ */
303 .global VG_(disp_cp_evcheck_fail)
304 VG_(disp_cp_evcheck_fail):
305 movq $VG_TRC_INNER_COUNTERZERO, %rax
310 .size VG_(disp_run_translations), .-VG_(disp_run_translations)
312 #endif // defined(VGP_amd64_solaris)
314 /* Let the linker know we don't need an executable stack */
317 /*--------------------------------------------------------------------*/
319 /*--------------------------------------------------------------------*/