sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +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 | /* |
| 8 | This file is part of Valgrind, an x86 protected-mode emulator |
| 9 | designed for debugging and profiling binaries on x86-Unixes. |
| 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 | |
| 53 | /* The C world needs a way to get started simulating. So we provide |
| 54 | a function void vg_run_innerloop ( void ), which starts running |
| 55 | from vg_m_eip, and exits when the counter reaches zero. This loop |
| 56 | can also exit if vg_oursignalhandler() catches a non-resumable |
| 57 | signal, for example SIGSEGV. It then longjmp()s back past here. |
| 58 | */ |
| 59 | |
| 60 | .globl VG_(run_innerloop) |
| 61 | VG_(run_innerloop): |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 62 | /* OYNK(1000) */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 63 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 64 | /* ----- entry point to VG_(run_innerloop) ----- */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 65 | pushl %ebx |
| 66 | pushl %ecx |
| 67 | pushl %edx |
| 68 | pushl %esi |
| 69 | pushl %edi |
| 70 | pushl %ebp |
| 71 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 72 | /* Set up the baseBlock pointer */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 73 | movl $VG_(baseBlock), %ebp |
| 74 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 75 | /* fetch m_eip into %eax */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 76 | movl VGOFF_(m_eip), %esi |
| 77 | movl (%ebp, %esi, 4), %eax |
| 78 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 79 | dispatch_main: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 80 | /* Jump here to do a new dispatch. |
| 81 | %eax holds destination (original) address. |
| 82 | %ebp indicates further details of the control transfer |
| 83 | requested to the address in %eax. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 84 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 85 | If ebp == & VG_(baseBlock), just jump next to %eax. |
| 86 | |
| 87 | If ebp == VG_EBP_JMP_SYSCALL, do a system call before |
| 88 | continuing at eax. |
| 89 | |
| 90 | If ebp == VG_EBP_JMP_CLIENTREQ, do a client request before |
| 91 | continuing at eax. |
| 92 | |
| 93 | If %ebp has any other value, we panic. |
| 94 | */ |
| 95 | cmpl $VG_(baseBlock), %ebp |
| 96 | jnz dispatch_exceptional |
| 97 | /* fall into main loop */ |
| 98 | |
| 99 | |
| 100 | dispatch_boring: |
| 101 | /* save the jump address at VG_(baseBlock)[VGOFF_(m_eip)] */ |
| 102 | movl VGOFF_(m_eip), %esi |
| 103 | movl %eax, (%ebp, %esi, 4) |
| 104 | /* Are we out of timeslice? If yes, defer to scheduler. */ |
| 105 | decl VG_(dispatch_ctr) |
| 106 | jz counter_is_zero |
| 107 | /* try a fast lookup in the translation cache */ |
| 108 | movl %eax, %ebx |
| 109 | andl $VG_TT_FAST_MASK, %ebx |
| 110 | /* ebx = tt_fast index */ |
| 111 | movl VG_(tt_fast)(,%ebx,4), %ebx |
| 112 | /* ebx points at a tt entry |
| 113 | now compare target with the tte.orig_addr field (+0) */ |
| 114 | cmpl %eax, (%ebx) |
| 115 | jnz fast_lookup_failed |
| 116 | #if 1 |
| 117 | /* Found a match. Set the tte.mru_epoch field (+8) |
| 118 | and call the tte.trans_addr field (+4) */ |
| 119 | movl VG_(current_epoch), %ecx |
| 120 | movl %ecx, 8(%ebx) |
| 121 | #endif |
| 122 | call *4(%ebx) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 123 | cmpl $VG_(baseBlock), %ebp |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 124 | jnz dispatch_exceptional |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 125 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 126 | dispatch_boring_unroll2: |
| 127 | /* save the jump address at VG_(baseBlock)[VGOFF_(m_eip)] */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | movl VGOFF_(m_eip), %esi |
| 129 | movl %eax, (%ebp, %esi, 4) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 130 | #if 1 |
| 131 | /* Are we out of timeslice? If yes, defer to scheduler. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 132 | decl VG_(dispatch_ctr) |
| 133 | jz counter_is_zero |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 134 | #endif |
| 135 | /* try a fast lookup in the translation cache */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 136 | movl %eax, %ebx |
| 137 | andl $VG_TT_FAST_MASK, %ebx |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 138 | /* ebx = tt_fast index */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 139 | movl VG_(tt_fast)(,%ebx,4), %ebx |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 140 | /* ebx points at a tt entry |
| 141 | now compare target with the tte.orig_addr field (+0) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 142 | cmpl %eax, (%ebx) |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 143 | jnz fast_lookup_failed |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 144 | #if 1 |
| 145 | /* Found a match. Set the tte.mru_epoch field (+8) |
| 146 | and call the tte.trans_addr field (+4) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 147 | movl VG_(current_epoch), %ecx |
| 148 | movl %ecx, 8(%ebx) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 149 | #endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 150 | call *4(%ebx) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 151 | cmpl $VG_(baseBlock), %ebp |
| 152 | jz dispatch_boring |
| 153 | |
| 154 | jmp dispatch_exceptional |
| 155 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 156 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 157 | fast_lookup_failed: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 158 | /* %EIP is up to date here since dispatch_boring dominates */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 159 | movl $VG_TRC_INNER_FASTMISS, %eax |
| 160 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 161 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 162 | counter_is_zero: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 163 | /* %EIP is up to date here since dispatch_boring dominates */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 164 | movl $VG_TRC_INNER_COUNTERZERO, %eax |
| 165 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 166 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 167 | run_innerloop_exit: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 168 | popl %ebp |
| 169 | popl %edi |
| 170 | popl %esi |
| 171 | popl %edx |
| 172 | popl %ecx |
| 173 | popl %ebx |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 174 | ret |
| 175 | |
| 176 | |
| 177 | |
| 178 | /* Other ways of getting out of the inner loop. Placed out-of-line to |
| 179 | make it look cleaner. |
| 180 | */ |
| 181 | dispatch_exceptional: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 182 | /* this is jumped to only, not fallen-through from above */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 183 | cmpl $VG_TRC_EBP_JMP_SYSCALL, %ebp |
| 184 | jz dispatch_syscall |
| 185 | cmpl $VG_TRC_EBP_JMP_CLIENTREQ, %ebp |
| 186 | jz dispatch_clientreq |
| 187 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 188 | /* ebp has an invalid value ... crap out. */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 189 | pushl $panic_msg_ebp |
| 190 | call VG_(panic) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 191 | /* (never returns) */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 192 | |
| 193 | dispatch_syscall: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 194 | /* save %eax in %EIP and defer to sched */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 195 | movl $VG_(baseBlock), %ebp |
| 196 | movl VGOFF_(m_eip), %esi |
| 197 | movl %eax, (%ebp, %esi, 4) |
| 198 | movl $VG_TRC_EBP_JMP_SYSCALL, %eax |
| 199 | jmp run_innerloop_exit |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 200 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 201 | dispatch_clientreq: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 202 | /* save %eax in %EIP and defer to sched */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 203 | movl $VG_(baseBlock), %ebp |
| 204 | movl VGOFF_(m_eip), %esi |
| 205 | movl %eax, (%ebp, %esi, 4) |
| 206 | movl $VG_TRC_EBP_JMP_CLIENTREQ, %eax |
| 207 | jmp run_innerloop_exit |
| 208 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 209 | .data |
| 210 | panic_msg_ebp: |
| 211 | .ascii "vg_dispatch: %ebp has invalid value!" |
| 212 | .byte 0 |
| 213 | .text |
| 214 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 215 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 216 | /*--------------------------------------------------------------------*/ |
| 217 | /*--- end vg_dispatch.S ---*/ |
| 218 | /*--------------------------------------------------------------------*/ |