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