blob: 997bf17f3dc9be745087dc5cb90aa27993036b4d [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
njnc9539842002-10-02 13:26:35 +00002##--------------------------------------------------------------------##
3##--- The core dispatch loop, for jumping to a code address. ---##
4##--- vg_dispatch.S ---##
5##--------------------------------------------------------------------##
sewardjde4a1d02002-03-22 01:27:54 +00006
7/*
njnc9539842002-10-02 13:26:35 +00008 This file is part of Valgrind, an extensible x86 protected-mode
9 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000010
nethercotebb1c9912004-01-04 16:43:23 +000011 Copyright (C) 2000-2004 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000012 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000013
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.
18
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.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000030*/
31
32#include "vg_constants.h"
33
34
35/*------------------------------------------------------------*/
36/*--- The normal-case dispatch machinery. ---*/
37/*------------------------------------------------------------*/
38
39/* To transfer to an (original) code address, load it into %eax and
40 jump to vg_dispatch. This fragment of code tries to find the
41 address of the corresponding translation by searching the translation
42 table. If it fails, a new translation is made, added to the
43 translation table, and then jumped to. Almost all the hard
44 work is done by C routines; this code simply handles the
45 common case fast -- when the translation address is found in
46 the translation cache.
47
48 At entry, %eax is the only live (real-machine) register; the
49 entire simulated state is tidily saved in vg_m_state.
50*/
51
52
sewardj22854b92002-11-30 14:00:47 +000053#define TT_LOOKUP(reg, fail) \
54 movl %eax, reg; \
55 andl $VG_TT_FAST_MASK, reg; \
56 movl VG_(tt_fast)(,reg,4), reg; \
57 cmpl %eax, (reg); \
58 jnz fail
59
sewardjde4a1d02002-03-22 01:27:54 +000060/* The C world needs a way to get started simulating. So we provide
61 a function void vg_run_innerloop ( void ), which starts running
62 from vg_m_eip, and exits when the counter reaches zero. This loop
63 can also exit if vg_oursignalhandler() catches a non-resumable
64 signal, for example SIGSEGV. It then longjmp()s back past here.
65*/
sewardj22854b92002-11-30 14:00:47 +000066
sewardjde4a1d02002-03-22 01:27:54 +000067.globl VG_(run_innerloop)
68VG_(run_innerloop):
njn25e49d8e72002-09-23 09:36:25 +000069 /* OYNK(1000) */
sewardj2e93c502002-04-12 11:12:52 +000070
njn25e49d8e72002-09-23 09:36:25 +000071 /* ----- entry point to VG_(run_innerloop) ----- */
sewardj2e93c502002-04-12 11:12:52 +000072 pushl %ebx
73 pushl %ecx
74 pushl %edx
75 pushl %esi
76 pushl %edi
77 pushl %ebp
78
fitzhardinge98abfc72003-12-16 02:05:15 +000079 /* check to see if we're doing pointer checking */
fitzhardingedba9d932003-12-19 00:23:00 +000080 movb VG_(clo_pointercheck), %al
81 testb %al,%al
fitzhardinge98abfc72003-12-16 02:05:15 +000082 jz 1f
83
84 pushl %fs /* save %fs */
85 mov $(VG_POINTERCHECK_SEGIDX << 3) + 7, %eax /* load new %fs */
86 movw %ax,%fs
87
881:
njn25e49d8e72002-09-23 09:36:25 +000089 /* Set up the baseBlock pointer */
sewardjde4a1d02002-03-22 01:27:54 +000090 movl $VG_(baseBlock), %ebp
91
njn25e49d8e72002-09-23 09:36:25 +000092 /* fetch m_eip into %eax */
sewardjde4a1d02002-03-22 01:27:54 +000093 movl VGOFF_(m_eip), %esi
94 movl (%ebp, %esi, 4), %eax
95
sewardj2e93c502002-04-12 11:12:52 +000096dispatch_main:
njn25e49d8e72002-09-23 09:36:25 +000097 /* Jump here to do a new dispatch.
98 %eax holds destination (original) address.
99 %ebp indicates further details of the control transfer
100 requested to the address in %eax.
sewardj2e93c502002-04-12 11:12:52 +0000101
njn25e49d8e72002-09-23 09:36:25 +0000102 If ebp == & VG_(baseBlock), just jump next to %eax.
103
104 If ebp == VG_EBP_JMP_SYSCALL, do a system call before
105 continuing at eax.
106
107 If ebp == VG_EBP_JMP_CLIENTREQ, do a client request before
108 continuing at eax.
109
110 If %ebp has any other value, we panic.
111 */
nethercote71980f02004-01-24 18:18:54 +0000112 /*cmpl $VG_(baseBlock), %ebp*/
113 /*jnz dispatch_exceptional*/
njn25e49d8e72002-09-23 09:36:25 +0000114 /* fall into main loop */
115
116
117dispatch_boring:
118 /* save the jump address at VG_(baseBlock)[VGOFF_(m_eip)] */
119 movl VGOFF_(m_eip), %esi
120 movl %eax, (%ebp, %esi, 4)
sewardj6c3769f2002-11-29 01:02:45 +0000121
njn25e49d8e72002-09-23 09:36:25 +0000122 /* Are we out of timeslice? If yes, defer to scheduler. */
sewardj22854b92002-11-30 14:00:47 +0000123 cmpl $0, VG_(dispatch_ctr)
sewardjde4a1d02002-03-22 01:27:54 +0000124 jz counter_is_zero
njn25e49d8e72002-09-23 09:36:25 +0000125 /* try a fast lookup in the translation cache */
sewardj22854b92002-11-30 14:00:47 +0000126 TT_LOOKUP(%ebx, fast_lookup_failed)
sewardj6c3769f2002-11-29 01:02:45 +0000127
sewardj22854b92002-11-30 14:00:47 +0000128 /* Found a match. Call the tce.payload field (+VG_CODE_OFFSET) */
129 addl $VG_CODE_OFFSET, %ebx
130 incl VG_(unchained_jumps_done) /* update stats */
sewardj6c3769f2002-11-29 01:02:45 +0000131 call *%ebx
132
njn25e49d8e72002-09-23 09:36:25 +0000133 cmpl $VG_(baseBlock), %ebp
134 jz dispatch_boring
135
136 jmp dispatch_exceptional
137
sewardjde4a1d02002-03-22 01:27:54 +0000138
sewardj2e93c502002-04-12 11:12:52 +0000139fast_lookup_failed:
njn25e49d8e72002-09-23 09:36:25 +0000140 /* %EIP is up to date here since dispatch_boring dominates */
sewardj2e93c502002-04-12 11:12:52 +0000141 movl $VG_TRC_INNER_FASTMISS, %eax
142 jmp run_innerloop_exit
sewardjde4a1d02002-03-22 01:27:54 +0000143
sewardjde4a1d02002-03-22 01:27:54 +0000144counter_is_zero:
njn25e49d8e72002-09-23 09:36:25 +0000145 /* %EIP is up to date here since dispatch_boring dominates */
sewardj2e93c502002-04-12 11:12:52 +0000146 movl $VG_TRC_INNER_COUNTERZERO, %eax
147 jmp run_innerloop_exit
sewardjde4a1d02002-03-22 01:27:54 +0000148
sewardj2e93c502002-04-12 11:12:52 +0000149run_innerloop_exit:
fitzhardingedba9d932003-12-19 00:23:00 +0000150 movb VG_(clo_pointercheck), %bl
151 testb %bl,%bl
fitzhardinge98abfc72003-12-16 02:05:15 +0000152 jz 1f
153
154 /* restore %fs */
155 popl %fs
156
1571: popl %ebp
sewardjde4a1d02002-03-22 01:27:54 +0000158 popl %edi
159 popl %esi
160 popl %edx
161 popl %ecx
162 popl %ebx
sewardj2e93c502002-04-12 11:12:52 +0000163 ret
164
165
166
167/* Other ways of getting out of the inner loop. Placed out-of-line to
168 make it look cleaner.
169*/
170dispatch_exceptional:
njn25e49d8e72002-09-23 09:36:25 +0000171 /* this is jumped to only, not fallen-through from above */
sewardj22854b92002-11-30 14:00:47 +0000172 cmpl $VG_TRC_INNER_COUNTERZERO, %ebp
173 jz counter_is_zero
sewardj2e93c502002-04-12 11:12:52 +0000174
njn25e49d8e72002-09-23 09:36:25 +0000175 /* save %eax in %EIP and defer to sched */
sewardj2e93c502002-04-12 11:12:52 +0000176 movl VGOFF_(m_eip), %esi
fitzhardingea02f8812003-12-18 09:06:09 +0000177 movl %eax, VG_(baseBlock)(,%esi, 4)
178 movl %ebp, %eax
sewardj2e93c502002-04-12 11:12:52 +0000179 jmp run_innerloop_exit
180
sewardj22854b92002-11-30 14:00:47 +0000181
sewardj83f11862002-12-01 02:07:08 +0000182/*
183 This is the translation chainer, our run-time linker, if you like.
184
185 VG_(patch_me) patches the call instruction in the jump site
186 with a jump to the generated code for the branch target. %eax
187 contains the original program's EIP - if we get a hit in
188 tt_fast, then the call is patched into a jump; otherwise it
189 simply drops back into the dispatch loop for normal
190 processing.
191
192 The callsite is expected to look like:
193 call VG_(patch_me)
194 it will be transformed into
195 jmp $TARGETADDR
196
197 The environment we're expecting on entry is:
198 %eax = branch target address (original code EIP)
199 *(%esp) = just after call
sewardj22854b92002-11-30 14:00:47 +0000200*/
201.globl VG_(patch_me)
202VG_(patch_me):
203 /* try a fast lookup in the translation cache */
204 TT_LOOKUP(%ebx, 1f)
205
206 /* Patch call instruction at callsite into a chained jmp */
207 popl %eax /* eax = just after (VG_PATCHME_CALLSZ byte) call */
208 addl $VG_CODE_OFFSET, %ebx /* ebx = target eip */
209 subl %eax, %ebx /* ebx = delta */
210 movb $0xE9, -(VG_PATCHME_CALLSZ-0)(%eax) /* 0xe9 = jmp */
211 movl %ebx, -(VG_PATCHME_CALLSZ-1)(%eax) /* store delta */
212 addl %eax, %ebx
213 incl VG_(bb_enchain_count) /* update stats */
214 jmp *%ebx /* jmp to dest */
215
216 /* tt_fast miss: return into main dispatch loop */
2171: addl $4, %esp /* remove our call address */
218 ret /* return into main dispatch loop above */
219
sewardj2e93c502002-04-12 11:12:52 +0000220.data
221panic_msg_ebp:
222.ascii "vg_dispatch: %ebp has invalid value!"
223.byte 0
224.text
225
sewardjde4a1d02002-03-22 01:27:54 +0000226
njnc9539842002-10-02 13:26:35 +0000227##--------------------------------------------------------------------##
228##--- end vg_dispatch.S ---##
229##--------------------------------------------------------------------##