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. ---## |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 4 | ##--- dispatch-x86.S ---## |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 5 | ##--------------------------------------------------------------------## |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6 | |
| 7 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, a dynamic binary instrumentation |
| 9 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2005 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 32 | #include "pub_tool_basics_asm.h" |
njn | a733b5f | 2005-05-12 13:15:38 +0000 | [diff] [blame] | 33 | #include "pub_core_dispatch_asm.h" |
njn | 2e8f4ef | 2005-05-14 21:44:20 +0000 | [diff] [blame] | 34 | #include "pub_core_transtab_asm.h" |
sewardj | 0312f51 | 2005-03-30 19:04:29 +0000 | [diff] [blame] | 35 | #include "libvex_guest_offsets.h" /* for OFFSET_x86_EIP */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /*------------------------------------------------------------*/ |
njn | d6f157d | 2004-11-30 17:27:21 +0000 | [diff] [blame] | 39 | /*--- The dispatch loop. ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 40 | /*------------------------------------------------------------*/ |
sewardj | 0312f51 | 2005-03-30 19:04:29 +0000 | [diff] [blame] | 41 | |
| 42 | /* signature: UWord VG_(run_innerloop) ( void* guest_state ) */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 43 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 44 | .globl VG_(run_innerloop) |
| 45 | VG_(run_innerloop): |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 46 | /* 4(%esp) holds guest_state */ |
| 47 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 48 | /* ----- entry point to VG_(run_innerloop) ----- */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 49 | pushl %ebx |
| 50 | pushl %ecx |
| 51 | pushl %edx |
| 52 | pushl %esi |
| 53 | pushl %edi |
| 54 | pushl %ebp |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 55 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 56 | /* 28(%esp) holds guest_state */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 57 | |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 58 | /* Set up the guest state pointer */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 59 | movl 28(%esp), %ebp |
| 60 | |
njn | d6f157d | 2004-11-30 17:27:21 +0000 | [diff] [blame] | 61 | /* fetch %EIP into %eax */ |
sewardj | 0312f51 | 2005-03-30 19:04:29 +0000 | [diff] [blame] | 62 | movl OFFSET_x86_EIP(%ebp), %eax |
sewardj | ff99272 | 2004-12-04 01:45:56 +0000 | [diff] [blame] | 63 | |
sewardj | aa561fa | 2004-12-06 14:23:28 +0000 | [diff] [blame] | 64 | /* set host FPU control word to the default mode expected |
sewardj | 5390e66 | 2005-01-10 16:51:14 +0000 | [diff] [blame] | 65 | by VEX-generated code. See comments in libvex.h for |
| 66 | more info. */ |
sewardj | aa561fa | 2004-12-06 14:23:28 +0000 | [diff] [blame] | 67 | finit |
sewardj | 5390e66 | 2005-01-10 16:51:14 +0000 | [diff] [blame] | 68 | pushl $0x027F |
sewardj | aa561fa | 2004-12-06 14:23:28 +0000 | [diff] [blame] | 69 | fldcw (%esp) |
sewardj | ff99272 | 2004-12-04 01:45:56 +0000 | [diff] [blame] | 70 | addl $4, %esp |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 71 | |
sewardj | aa561fa | 2004-12-06 14:23:28 +0000 | [diff] [blame] | 72 | /* set host SSE control word to the default mode expected |
| 73 | by VEX-generated code. */ |
| 74 | pushl $0x1F80 |
| 75 | ldmxcsr (%esp) |
| 76 | addl $4, %esp |
| 77 | |
| 78 | /* set dir flag to known value */ |
| 79 | cld |
| 80 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 81 | /* fall into main loop */ |
| 82 | |
njn | d6f157d | 2004-11-30 17:27:21 +0000 | [diff] [blame] | 83 | /* Here, %eax is the only live (real) register. The entire |
| 84 | simulated state is saved in the ThreadState. */ |
| 85 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 86 | dispatch_boring: |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 87 | /* save the jump address in the guest state */ |
sewardj | 0312f51 | 2005-03-30 19:04:29 +0000 | [diff] [blame] | 88 | movl %eax, OFFSET_x86_EIP(%ebp) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 89 | |
| 90 | /* Are we out of timeslice? If yes, defer to scheduler. */ |
| 91 | subl $1, VG_(dispatch_ctr) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 92 | jz counter_is_zero |
sewardj | 45f02c4 | 2005-02-05 18:27:14 +0000 | [diff] [blame] | 93 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 94 | /* try a fast lookup in the translation cache */ |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 95 | movl %eax, %ebx |
| 96 | andl $VG_TT_FAST_MASK, %ebx |
| 97 | movl VG_(tt_fast)(,%ebx,4), %ecx |
| 98 | cmpl %eax, (%ecx) |
| 99 | jnz fast_lookup_failed |
sewardj | 45f02c4 | 2005-02-05 18:27:14 +0000 | [diff] [blame] | 100 | /* increment bb profile counter */ |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 101 | movl VG_(tt_fastN)(,%ebx,4), %edx |
| 102 | incl (%edx) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 103 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 104 | /* Found a match. Call tce[1], which is 8 bytes along, since |
| 105 | each tce element is a 64-bit int. */ |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 106 | addl $8, %ecx |
| 107 | call *%ecx |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 110 | %eax holds destination (original) address. |
| 111 | %ebp indicates further details of the control transfer |
| 112 | requested to the address in %eax. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 113 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 114 | If ebp is unchanged (== * 28(%esp)), just jump next to %eax. |
sewardj | 1ff7957 | 2004-11-30 11:18:57 +0000 | [diff] [blame] | 115 | |
| 116 | Otherwise fall out, back to the scheduler, and let it |
| 117 | figure out what to do next. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 118 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 119 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 120 | cmpl 28(%esp), %ebp |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 121 | jz dispatch_boring |
| 122 | |
| 123 | jmp dispatch_exceptional |
| 124 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 125 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 126 | |
sewardj | 5390e66 | 2005-01-10 16:51:14 +0000 | [diff] [blame] | 127 | /* All exits from the dispatcher go through here. %eax holds |
| 128 | the return value. |
| 129 | */ |
| 130 | run_innerloop_exit: |
| 131 | /* We're leaving. Check that nobody messed with |
| 132 | %mxcsr or %fpucw. We can't mess with %eax here as it |
| 133 | holds the tentative return value, but any other is OK. */ |
| 134 | pushl $0 |
| 135 | fstcw (%esp) |
| 136 | cmpl $0x027F, (%esp) |
| 137 | popl %esi /* get rid of the word without trashing %eflags */ |
| 138 | jnz invariant_violation |
| 139 | pushl $0 |
| 140 | stmxcsr (%esp) |
| 141 | andl $0xFFFFFFC0, (%esp) /* mask out status flags */ |
| 142 | cmpl $0x1F80, (%esp) |
| 143 | popl %esi |
| 144 | jnz invariant_violation |
| 145 | /* otherwise we're OK */ |
| 146 | jmp run_innerloop_exit_REALLY |
| 147 | |
| 148 | invariant_violation: |
| 149 | movl $VG_TRC_INVARIANT_FAILED, %eax |
| 150 | jmp run_innerloop_exit_REALLY |
| 151 | |
| 152 | run_innerloop_exit_REALLY: |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 153 | popl %ebp |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 154 | popl %edi |
| 155 | popl %esi |
| 156 | popl %edx |
| 157 | popl %ecx |
| 158 | popl %ebx |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 159 | ret |
| 160 | |
| 161 | |
| 162 | |
| 163 | /* Other ways of getting out of the inner loop. Placed out-of-line to |
| 164 | make it look cleaner. |
| 165 | */ |
| 166 | dispatch_exceptional: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 167 | /* this is jumped to only, not fallen-through from above */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 168 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 169 | /* save %eax in %EIP and defer to sched */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 170 | movl 28(%esp), %edi |
sewardj | 0312f51 | 2005-03-30 19:04:29 +0000 | [diff] [blame] | 171 | movl %eax, OFFSET_x86_EIP(%edi) |
fitzhardinge | a02f881 | 2003-12-18 09:06:09 +0000 | [diff] [blame] | 172 | movl %ebp, %eax |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 173 | jmp run_innerloop_exit |
| 174 | |
sewardj | 5390e66 | 2005-01-10 16:51:14 +0000 | [diff] [blame] | 175 | fast_lookup_failed: |
| 176 | /* %EIP is up to date here since dispatch_boring dominates */ |
| 177 | addl $1, VG_(dispatch_ctr) |
| 178 | movl $VG_TRC_INNER_FASTMISS, %eax |
| 179 | jmp run_innerloop_exit |
| 180 | |
| 181 | counter_is_zero: |
| 182 | /* %EIP is up to date here since dispatch_boring dominates */ |
| 183 | addl $1, VG_(dispatch_ctr) |
| 184 | movl $VG_TRC_INNER_COUNTERZERO, %eax |
| 185 | jmp run_innerloop_exit |
| 186 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 187 | |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 188 | /* Let the linker know we don't need an executable stack */ |
| 189 | .section .note.GNU-stack,"",@progbits |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 190 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 191 | ##--------------------------------------------------------------------## |
njn | bb12095 | 2004-11-23 18:19:29 +0000 | [diff] [blame] | 192 | ##--- end ---## |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 193 | ##--------------------------------------------------------------------## |