sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 2 | ##--------------------------------------------------------------------## |
| 3 | ##--- The core dispatch loop, for jumping to a code address. ---## |
| 4 | ##--- vg_dispatch.S ---## |
| 5 | ##--------------------------------------------------------------------## |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6 | |
| 7 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, an extensible x86 protected-mode |
| 9 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2003 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
| 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 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 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 53 | #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 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 60 | /* 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 | */ |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 66 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 67 | .globl VG_(run_innerloop) |
| 68 | VG_(run_innerloop): |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 69 | /* OYNK(1000) */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 70 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 71 | /* ----- entry point to VG_(run_innerloop) ----- */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 72 | pushl %ebx |
| 73 | pushl %ecx |
| 74 | pushl %edx |
| 75 | pushl %esi |
| 76 | pushl %edi |
| 77 | pushl %ebp |
| 78 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 79 | /* check to see if we're doing pointer checking */ |
fitzhardinge | dba9d93 | 2003-12-19 00:23:00 +0000 | [diff] [blame^] | 80 | movb VG_(clo_pointercheck), %al |
| 81 | testb %al,%al |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 82 | jz 1f |
| 83 | |
| 84 | pushl %fs /* save %fs */ |
| 85 | mov $(VG_POINTERCHECK_SEGIDX << 3) + 7, %eax /* load new %fs */ |
| 86 | movw %ax,%fs |
| 87 | |
| 88 | 1: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 89 | /* Set up the baseBlock pointer */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 90 | movl $VG_(baseBlock), %ebp |
| 91 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 92 | /* fetch m_eip into %eax */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 93 | movl VGOFF_(m_eip), %esi |
| 94 | movl (%ebp, %esi, 4), %eax |
| 95 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 96 | dispatch_main: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 97 | /* 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. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 101 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 102 | 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 | */ |
| 112 | cmpl $VG_(baseBlock), %ebp |
| 113 | jnz dispatch_exceptional |
| 114 | /* fall into main loop */ |
| 115 | |
| 116 | |
| 117 | dispatch_boring: |
| 118 | /* save the jump address at VG_(baseBlock)[VGOFF_(m_eip)] */ |
| 119 | movl VGOFF_(m_eip), %esi |
| 120 | movl %eax, (%ebp, %esi, 4) |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 121 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 122 | /* Are we out of timeslice? If yes, defer to scheduler. */ |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 123 | cmpl $0, VG_(dispatch_ctr) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 124 | jz counter_is_zero |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 125 | /* try a fast lookup in the translation cache */ |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 126 | TT_LOOKUP(%ebx, fast_lookup_failed) |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 127 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 128 | /* 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 */ |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 131 | call *%ebx |
| 132 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 133 | cmpl $VG_(baseBlock), %ebp |
| 134 | jz dispatch_boring |
| 135 | |
| 136 | jmp dispatch_exceptional |
| 137 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 138 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 139 | fast_lookup_failed: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 140 | /* %EIP is up to date here since dispatch_boring dominates */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 141 | movl $VG_TRC_INNER_FASTMISS, %eax |
| 142 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 143 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 144 | counter_is_zero: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 145 | /* %EIP is up to date here since dispatch_boring dominates */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 146 | movl $VG_TRC_INNER_COUNTERZERO, %eax |
| 147 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 148 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 149 | run_innerloop_exit: |
fitzhardinge | dba9d93 | 2003-12-19 00:23:00 +0000 | [diff] [blame^] | 150 | movb VG_(clo_pointercheck), %bl |
| 151 | testb %bl,%bl |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 152 | jz 1f |
| 153 | |
| 154 | /* restore %fs */ |
| 155 | popl %fs |
| 156 | |
| 157 | 1: popl %ebp |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 158 | popl %edi |
| 159 | popl %esi |
| 160 | popl %edx |
| 161 | popl %ecx |
| 162 | popl %ebx |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 163 | 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 | */ |
| 170 | dispatch_exceptional: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 171 | /* this is jumped to only, not fallen-through from above */ |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 172 | cmpl $VG_TRC_INNER_COUNTERZERO, %ebp |
| 173 | jz counter_is_zero |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 174 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 175 | /* save %eax in %EIP and defer to sched */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 176 | movl VGOFF_(m_eip), %esi |
fitzhardinge | a02f881 | 2003-12-18 09:06:09 +0000 | [diff] [blame] | 177 | movl %eax, VG_(baseBlock)(,%esi, 4) |
| 178 | movl %ebp, %eax |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 179 | jmp run_innerloop_exit |
| 180 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 181 | |
sewardj | 83f1186 | 2002-12-01 02:07:08 +0000 | [diff] [blame] | 182 | /* |
| 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 |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 200 | */ |
| 201 | .globl VG_(patch_me) |
| 202 | VG_(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 */ |
| 217 | 1: addl $4, %esp /* remove our call address */ |
| 218 | ret /* return into main dispatch loop above */ |
| 219 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 220 | .data |
| 221 | panic_msg_ebp: |
| 222 | .ascii "vg_dispatch: %ebp has invalid value!" |
| 223 | .byte 0 |
| 224 | .text |
| 225 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 226 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 227 | ##--------------------------------------------------------------------## |
| 228 | ##--- end vg_dispatch.S ---## |
| 229 | ##--------------------------------------------------------------------## |