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 | |
| 11 | Copyright (C) 2000-2002 Julian Seward |
| 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 79 | /* Set up the baseBlock pointer */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 80 | movl $VG_(baseBlock), %ebp |
| 81 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 82 | /* fetch m_eip into %eax */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 83 | movl VGOFF_(m_eip), %esi |
| 84 | movl (%ebp, %esi, 4), %eax |
| 85 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 86 | dispatch_main: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 87 | /* 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. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 91 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 92 | 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 | |
| 107 | dispatch_boring: |
| 108 | /* save the jump address at VG_(baseBlock)[VGOFF_(m_eip)] */ |
| 109 | movl VGOFF_(m_eip), %esi |
| 110 | movl %eax, (%ebp, %esi, 4) |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 111 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 112 | /* Are we out of timeslice? If yes, defer to scheduler. */ |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame^] | 113 | cmpl $0, VG_(dispatch_ctr) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 114 | jz counter_is_zero |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 115 | /* try a fast lookup in the translation cache */ |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame^] | 116 | TT_LOOKUP(%ebx, fast_lookup_failed) |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 117 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame^] | 118 | /* 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 */ |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 121 | call *%ebx |
| 122 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 123 | cmpl $VG_(baseBlock), %ebp |
| 124 | jz dispatch_boring |
| 125 | |
| 126 | jmp dispatch_exceptional |
| 127 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 129 | fast_lookup_failed: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 130 | /* %EIP is up to date here since dispatch_boring dominates */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 131 | movl $VG_TRC_INNER_FASTMISS, %eax |
| 132 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 133 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 134 | counter_is_zero: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 135 | /* %EIP is up to date here since dispatch_boring dominates */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 136 | movl $VG_TRC_INNER_COUNTERZERO, %eax |
| 137 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 138 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 139 | run_innerloop_exit: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 140 | popl %ebp |
| 141 | popl %edi |
| 142 | popl %esi |
| 143 | popl %edx |
| 144 | popl %ecx |
| 145 | popl %ebx |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 146 | 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 | */ |
| 153 | dispatch_exceptional: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 154 | /* this is jumped to only, not fallen-through from above */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 155 | cmpl $VG_TRC_EBP_JMP_SYSCALL, %ebp |
| 156 | jz dispatch_syscall |
| 157 | cmpl $VG_TRC_EBP_JMP_CLIENTREQ, %ebp |
| 158 | jz dispatch_clientreq |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame^] | 159 | cmpl $VG_TRC_INNER_COUNTERZERO, %ebp |
| 160 | jz counter_is_zero |
| 161 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 162 | /* ebp has an invalid value ... crap out. */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 163 | pushl $panic_msg_ebp |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 164 | call VG_(core_panic) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 165 | /* (never returns) */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 166 | |
| 167 | dispatch_syscall: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 168 | /* save %eax in %EIP and defer to sched */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 169 | 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 |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 174 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 175 | dispatch_clientreq: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 176 | /* save %eax in %EIP and defer to sched */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 177 | 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 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame^] | 183 | |
| 184 | /* This is the translation chainer, our run-time linker, if you like. |
| 185 | |
| 186 | This enters with %eax pointing to next eip we want. If |
| 187 | we've already compiled that eip (ie, get a fast hit), we |
| 188 | backpatch the call instruction with a jump, and jump there. |
| 189 | Otherwise, we do a slow hit/compile through the normal path |
| 190 | (and get to do a backpatch next time through). |
| 191 | */ |
| 192 | .globl VG_(patch_me) |
| 193 | VG_(patch_me): |
| 194 | /* try a fast lookup in the translation cache */ |
| 195 | TT_LOOKUP(%ebx, 1f) |
| 196 | |
| 197 | /* Patch call instruction at callsite into a chained jmp */ |
| 198 | popl %eax /* eax = just after (VG_PATCHME_CALLSZ byte) call */ |
| 199 | addl $VG_CODE_OFFSET, %ebx /* ebx = target eip */ |
| 200 | subl %eax, %ebx /* ebx = delta */ |
| 201 | movb $0xE9, -(VG_PATCHME_CALLSZ-0)(%eax) /* 0xe9 = jmp */ |
| 202 | movl %ebx, -(VG_PATCHME_CALLSZ-1)(%eax) /* store delta */ |
| 203 | addl %eax, %ebx |
| 204 | incl VG_(bb_enchain_count) /* update stats */ |
| 205 | jmp *%ebx /* jmp to dest */ |
| 206 | |
| 207 | /* tt_fast miss: return into main dispatch loop */ |
| 208 | 1: addl $4, %esp /* remove our call address */ |
| 209 | ret /* return into main dispatch loop above */ |
| 210 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 211 | .data |
| 212 | panic_msg_ebp: |
| 213 | .ascii "vg_dispatch: %ebp has invalid value!" |
| 214 | .byte 0 |
| 215 | .text |
| 216 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 217 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 218 | ##--------------------------------------------------------------------## |
| 219 | ##--- end vg_dispatch.S ---## |
| 220 | ##--------------------------------------------------------------------## |