blob: 60d031069f7f42327cb1a899457ad210eadb7586 [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
11 Copyright (C) 2000-2002 Julian Seward
12 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
njn25e49d8e72002-09-23 09:36:25 +000079 /* Set up the baseBlock pointer */
sewardjde4a1d02002-03-22 01:27:54 +000080 movl $VG_(baseBlock), %ebp
81
njn25e49d8e72002-09-23 09:36:25 +000082 /* fetch m_eip into %eax */
sewardjde4a1d02002-03-22 01:27:54 +000083 movl VGOFF_(m_eip), %esi
84 movl (%ebp, %esi, 4), %eax
85
sewardj2e93c502002-04-12 11:12:52 +000086dispatch_main:
njn25e49d8e72002-09-23 09:36:25 +000087 /* Jump here to do a new dispatch.
88 %eax holds destination (original) address.
89 %ebp indicates further details of the control transfer
90 requested to the address in %eax.
sewardj2e93c502002-04-12 11:12:52 +000091
njn25e49d8e72002-09-23 09:36:25 +000092 If ebp == & VG_(baseBlock), just jump next to %eax.
93
94 If ebp == VG_EBP_JMP_SYSCALL, do a system call before
95 continuing at eax.
96
97 If ebp == VG_EBP_JMP_CLIENTREQ, do a client request before
98 continuing at eax.
99
100 If %ebp has any other value, we panic.
101 */
102 cmpl $VG_(baseBlock), %ebp
103 jnz dispatch_exceptional
104 /* fall into main loop */
105
106
107dispatch_boring:
108 /* save the jump address at VG_(baseBlock)[VGOFF_(m_eip)] */
109 movl VGOFF_(m_eip), %esi
110 movl %eax, (%ebp, %esi, 4)
sewardj6c3769f2002-11-29 01:02:45 +0000111
njn25e49d8e72002-09-23 09:36:25 +0000112 /* Are we out of timeslice? If yes, defer to scheduler. */
sewardj22854b92002-11-30 14:00:47 +0000113 cmpl $0, VG_(dispatch_ctr)
sewardjde4a1d02002-03-22 01:27:54 +0000114 jz counter_is_zero
njn25e49d8e72002-09-23 09:36:25 +0000115 /* try a fast lookup in the translation cache */
sewardj22854b92002-11-30 14:00:47 +0000116 TT_LOOKUP(%ebx, fast_lookup_failed)
sewardj6c3769f2002-11-29 01:02:45 +0000117
sewardj22854b92002-11-30 14:00:47 +0000118 /* Found a match. Call the tce.payload field (+VG_CODE_OFFSET) */
119 addl $VG_CODE_OFFSET, %ebx
120 incl VG_(unchained_jumps_done) /* update stats */
sewardj6c3769f2002-11-29 01:02:45 +0000121 call *%ebx
122
njn25e49d8e72002-09-23 09:36:25 +0000123 cmpl $VG_(baseBlock), %ebp
124 jz dispatch_boring
125
126 jmp dispatch_exceptional
127
sewardjde4a1d02002-03-22 01:27:54 +0000128
sewardj2e93c502002-04-12 11:12:52 +0000129fast_lookup_failed:
njn25e49d8e72002-09-23 09:36:25 +0000130 /* %EIP is up to date here since dispatch_boring dominates */
sewardj2e93c502002-04-12 11:12:52 +0000131 movl $VG_TRC_INNER_FASTMISS, %eax
132 jmp run_innerloop_exit
sewardjde4a1d02002-03-22 01:27:54 +0000133
sewardjde4a1d02002-03-22 01:27:54 +0000134counter_is_zero:
njn25e49d8e72002-09-23 09:36:25 +0000135 /* %EIP is up to date here since dispatch_boring dominates */
sewardj2e93c502002-04-12 11:12:52 +0000136 movl $VG_TRC_INNER_COUNTERZERO, %eax
137 jmp run_innerloop_exit
sewardjde4a1d02002-03-22 01:27:54 +0000138
sewardj2e93c502002-04-12 11:12:52 +0000139run_innerloop_exit:
sewardjde4a1d02002-03-22 01:27:54 +0000140 popl %ebp
141 popl %edi
142 popl %esi
143 popl %edx
144 popl %ecx
145 popl %ebx
sewardj2e93c502002-04-12 11:12:52 +0000146 ret
147
148
149
150/* Other ways of getting out of the inner loop. Placed out-of-line to
151 make it look cleaner.
152*/
153dispatch_exceptional:
njn25e49d8e72002-09-23 09:36:25 +0000154 /* this is jumped to only, not fallen-through from above */
sewardj2e93c502002-04-12 11:12:52 +0000155 cmpl $VG_TRC_EBP_JMP_SYSCALL, %ebp
156 jz dispatch_syscall
157 cmpl $VG_TRC_EBP_JMP_CLIENTREQ, %ebp
158 jz dispatch_clientreq
sewardj22854b92002-11-30 14:00:47 +0000159 cmpl $VG_TRC_INNER_COUNTERZERO, %ebp
160 jz counter_is_zero
161
njn25e49d8e72002-09-23 09:36:25 +0000162 /* ebp has an invalid value ... crap out. */
sewardj2e93c502002-04-12 11:12:52 +0000163 pushl $panic_msg_ebp
njne427a662002-10-02 11:08:25 +0000164 call VG_(core_panic)
njn25e49d8e72002-09-23 09:36:25 +0000165 /* (never returns) */
sewardj2e93c502002-04-12 11:12:52 +0000166
167dispatch_syscall:
njn25e49d8e72002-09-23 09:36:25 +0000168 /* save %eax in %EIP and defer to sched */
sewardj2e93c502002-04-12 11:12:52 +0000169 movl $VG_(baseBlock), %ebp
170 movl VGOFF_(m_eip), %esi
171 movl %eax, (%ebp, %esi, 4)
172 movl $VG_TRC_EBP_JMP_SYSCALL, %eax
173 jmp run_innerloop_exit
sewardjde4a1d02002-03-22 01:27:54 +0000174
sewardj2e93c502002-04-12 11:12:52 +0000175dispatch_clientreq:
njn25e49d8e72002-09-23 09:36:25 +0000176 /* save %eax in %EIP and defer to sched */
sewardj2e93c502002-04-12 11:12:52 +0000177 movl $VG_(baseBlock), %ebp
178 movl VGOFF_(m_eip), %esi
179 movl %eax, (%ebp, %esi, 4)
180 movl $VG_TRC_EBP_JMP_CLIENTREQ, %eax
181 jmp run_innerloop_exit
182
sewardj22854b92002-11-30 14:00:47 +0000183
sewardj83f11862002-12-01 02:07:08 +0000184/*
185 This is the translation chainer, our run-time linker, if you like.
186
187 VG_(patch_me) patches the call instruction in the jump site
188 with a jump to the generated code for the branch target. %eax
189 contains the original program's EIP - if we get a hit in
190 tt_fast, then the call is patched into a jump; otherwise it
191 simply drops back into the dispatch loop for normal
192 processing.
193
194 The callsite is expected to look like:
195 call VG_(patch_me)
196 it will be transformed into
197 jmp $TARGETADDR
198
199 The environment we're expecting on entry is:
200 %eax = branch target address (original code EIP)
201 *(%esp) = just after call
sewardj22854b92002-11-30 14:00:47 +0000202*/
203.globl VG_(patch_me)
204VG_(patch_me):
205 /* try a fast lookup in the translation cache */
206 TT_LOOKUP(%ebx, 1f)
207
208 /* Patch call instruction at callsite into a chained jmp */
209 popl %eax /* eax = just after (VG_PATCHME_CALLSZ byte) call */
210 addl $VG_CODE_OFFSET, %ebx /* ebx = target eip */
211 subl %eax, %ebx /* ebx = delta */
212 movb $0xE9, -(VG_PATCHME_CALLSZ-0)(%eax) /* 0xe9 = jmp */
213 movl %ebx, -(VG_PATCHME_CALLSZ-1)(%eax) /* store delta */
214 addl %eax, %ebx
215 incl VG_(bb_enchain_count) /* update stats */
216 jmp *%ebx /* jmp to dest */
217
218 /* tt_fast miss: return into main dispatch loop */
2191: addl $4, %esp /* remove our call address */
220 ret /* return into main dispatch loop above */
221
sewardj2e93c502002-04-12 11:12:52 +0000222.data
223panic_msg_ebp:
224.ascii "vg_dispatch: %ebp has invalid value!"
225.byte 0
226.text
227
sewardjde4a1d02002-03-22 01:27:54 +0000228
njnc9539842002-10-02 13:26:35 +0000229##--------------------------------------------------------------------##
230##--- end vg_dispatch.S ---##
231##--------------------------------------------------------------------##