2 /*--------------------------------------------------------------------*/
3 /*--- The core dispatch loop, for jumping to a code address. ---*/
4 /*--- dispatch-x86-freebsd.S ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2012 Julian Seward
13 Copyright (C) 2018-2021 Paul Floyd
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation; either version 2 of the
19 License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, see <http://www.gnu.org/licenses/>.
29 The GNU General Public License is contained in the file COPYING.
32 #include "pub_core_basics_asm.h"
34 #if defined(VGP_x86_freebsd)
36 #include "pub_core_dispatch_asm.h"
37 #include "pub_core_transtab_asm.h"
38 #include "libvex_guest_offsets.h" /* for OFFSET_x86_EIP */
41 /*------------------------------------------------------------*/
43 /*--- The dispatch loop. VG_(disp_run_translations) is ---*/
44 /*--- used to run all translations, ---*/
45 /*--- including no-redir ones. ---*/
47 /*------------------------------------------------------------*/
49 /*----------------------------------------------------*/
50 /*--- Entry and preamble (set everything up) ---*/
51 /*----------------------------------------------------*/
54 void VG_(disp_run_translations)( UWord* two_words,
59 .globl VG_(disp_run_translations)
60 .type VG_(disp_run_translations), @function
61 VG_(disp_run_translations):
62 /* 0(%esp) holds our return address. */
63 /* 4(%esp) holds two_words */
64 /* 8(%esp) holds guest_state */
65 /* 12(%esp) holds host_addr */
69 /* Save integer registers, since this is a pseudo-function. */
78 /* 28+4(%esp) holds two_words */
79 /* 28+8(%esp) holds guest_state */
80 /* 28+12(%esp) holds host_addr */
82 /* Get the host CPU in the state expected by generated code. */
84 /* set host FPU control word to the default mode expected
85 by VEX-generated code. See comments in libvex.h for
92 /* set host SSE control word to the default mode expected
93 by VEX-generated code. */
94 cmpl $0, VG_(machine_x86_have_mxcsr)
100 /* set dir flag to known value */
103 /* Set up the guest state pointer */
104 movl 28+8(%esp), %ebp
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, %eax and %edx contain two
120 words to be returned to the caller. %eax
121 holds a TRC value, and %edx 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 %eax or %edx here as they
127 holds the tentative return value, but any others are OK. */
128 #if !defined(ENABLE_INNER)
129 /* This check fails for self-hosting, so skip in that case */
133 popl %esi /* get rid of the word without trashing %eflags */
134 jnz invariant_violation
136 # cmpl $0, VG_(machine_x86_have_mxcsr)
140 andl $0xFFFFFFC0, (%esp) /* mask out status flags */
143 jnz invariant_violation
144 L2: /* otherwise we're OK */
147 movl $VG_TRC_INVARIANT_FAILED, %eax
151 /* Stash return values */
152 movl 28+4(%esp), %edi /* two_words */
155 /* Restore int regs and return. */
165 /*----------------------------------------------------*/
166 /*--- Continuation points ---*/
167 /*----------------------------------------------------*/
169 /* ------ Chain me to slow entry point ------ */
170 .global VG_(disp_cp_chain_me_to_slowEP)
171 VG_(disp_cp_chain_me_to_slowEP):
172 /* We got called. The return address indicates
173 where the patching needs to happen. Collect
174 the return address and, exit back to C land,
175 handing the caller the pair (Chain_me_S, RA) */
176 movl $VG_TRC_CHAIN_ME_TO_SLOW_EP, %eax
178 /* 5 = movl $VG_(disp_chain_me_to_slowEP), %edx;
183 /* ------ Chain me to fast entry point ------ */
184 .global VG_(disp_cp_chain_me_to_fastEP)
185 VG_(disp_cp_chain_me_to_fastEP):
186 /* We got called. The return address indicates
187 where the patching needs to happen. Collect
188 the return address and, exit back to C land,
189 handing the caller the pair (Chain_me_F, RA) */
190 movl $VG_TRC_CHAIN_ME_TO_FAST_EP, %eax
192 /* 5 = movl $VG_(disp_chain_me_to_fastEP), %edx;
197 /* ------ Indirect but boring jump ------ */
198 .global VG_(disp_cp_xindir)
200 /* Where are we going? */
201 movl OFFSET_x86_EIP(%ebp), %eax
204 addl $1, VG_(stats__n_xIndirs_32)
206 /* try a fast lookup in the translation cache */
207 movl %eax, %ebx /* next guest addr */
208 andl $VG_TT_FAST_MASK, %ebx /* entry# */
209 movl 0+VG_(tt_fast)(,%ebx,8), %esi /* .guest */
210 movl 4+VG_(tt_fast)(,%ebx,8), %edi /* .host */
212 jnz fast_lookup_failed
214 /* Found a match. Jump to .host. */
216 ud2 /* persuade insn decoders not to speculate past here */
220 addl $1, VG_(stats__n_xIndir_misses_32)
222 movl $VG_TRC_INNER_FASTMISS, %eax
226 /* ------ Assisted jump ------ */
227 .global VG_(disp_cp_xassisted)
228 VG_(disp_cp_xassisted):
229 /* %ebp contains the TRC */
234 /* ------ Event check failed ------ */
235 .global VG_(disp_cp_evcheck_fail)
236 VG_(disp_cp_evcheck_fail):
237 movl $VG_TRC_INNER_COUNTERZERO, %eax
242 .size VG_(disp_run_translations), .-VG_(disp_run_translations)
244 #endif // defined(VGP_x86_freebsd)
246 /* Let the linker know we don't need an executable stack */
249 /*--------------------------------------------------------------------*/
251 /*--------------------------------------------------------------------*/