sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
sewardj | bb68627 | 2006-01-22 20:12:45 +0000 | [diff] [blame] | 3 | /*--- Interface to LibVEX_Translate, and the SP-update pass ---*/ |
njn | 3cbfbc1 | 2005-05-13 23:11:40 +0000 | [diff] [blame] | 4 | /*--- m_translate.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 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 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2010 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_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 33 | #include "pub_core_vki.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 34 | #include "pub_core_aspacemgr.h" |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 35 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 36 | #include "pub_core_machine.h" // VG_(fnptr_to_fnentry) |
| 37 | // VG_(get_SP) |
| 38 | // VG_(machine_get_VexArchInfo) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 39 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 40 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 41 | #include "pub_core_libcprint.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 42 | #include "pub_core_options.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 43 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 44 | #include "pub_core_debuginfo.h" // VG_(get_fnname_w_offset) |
| 45 | #include "pub_core_redir.h" // VG_(redir_do_lookup) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 46 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 47 | #include "pub_core_signals.h" // VG_(synth_fault_{perms,mapping} |
| 48 | #include "pub_core_stacks.h" // VG_(unknown_SP_update)() |
| 49 | #include "pub_core_tooliface.h" // VG_(tdict) |
| 50 | |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 51 | #include "pub_core_translate.h" |
| 52 | #include "pub_core_transtab.h" |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 53 | #include "pub_core_dispatch.h" // VG_(run_innerloop__dispatch_{un}profiled) |
| 54 | // VG_(run_a_noredir_translation__return_point) |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 55 | |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 56 | #include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 57 | #include "pub_core_threadstate.h" // VexGuestArchState |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 58 | #include "pub_core_trampoline.h" // VG_(ppctoc_magic_redirect_return_stub) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 59 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 60 | #include "pub_core_execontext.h" // VG_(make_depth_1_ExeContext_from_Addr) |
| 61 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 62 | #include "pub_core_gdbserver.h" // VG_(tool_instrument_then_gdbserver_if_needed) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 63 | |
njn | a1f783f | 2005-06-18 03:44:34 +0000 | [diff] [blame] | 64 | /*------------------------------------------------------------*/ |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 65 | /*--- Stats ---*/ |
| 66 | /*------------------------------------------------------------*/ |
| 67 | |
| 68 | static UInt n_SP_updates_fast = 0; |
| 69 | static UInt n_SP_updates_generic_known = 0; |
| 70 | static UInt n_SP_updates_generic_unknown = 0; |
| 71 | |
| 72 | void VG_(print_translation_stats) ( void ) |
| 73 | { |
| 74 | Char buf[6]; |
| 75 | UInt n_SP_updates = n_SP_updates_fast + n_SP_updates_generic_known |
| 76 | + n_SP_updates_generic_unknown; |
| 77 | VG_(percentify)(n_SP_updates_fast, n_SP_updates, 1, 6, buf); |
| 78 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 79 | "translate: fast SP updates identified: %'u (%s)\n", |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 80 | n_SP_updates_fast, buf ); |
| 81 | |
| 82 | VG_(percentify)(n_SP_updates_generic_known, n_SP_updates, 1, 6, buf); |
| 83 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 84 | "translate: generic_known SP updates identified: %'u (%s)\n", |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 85 | n_SP_updates_generic_known, buf ); |
| 86 | |
| 87 | VG_(percentify)(n_SP_updates_generic_unknown, n_SP_updates, 1, 6, buf); |
| 88 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 89 | "translate: generic_unknown SP updates identified: %'u (%s)\n", |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 90 | n_SP_updates_generic_unknown, buf ); |
| 91 | } |
| 92 | |
| 93 | /*------------------------------------------------------------*/ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 94 | /*--- %SP-update pass ---*/ |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 95 | /*------------------------------------------------------------*/ |
| 96 | |
njn | a1f783f | 2005-06-18 03:44:34 +0000 | [diff] [blame] | 97 | static Bool need_to_handle_SP_assignment(void) |
| 98 | { |
sewardj | f5c8e37 | 2006-02-12 15:42:20 +0000 | [diff] [blame] | 99 | return ( VG_(tdict).track_new_mem_stack_4 || |
| 100 | VG_(tdict).track_die_mem_stack_4 || |
| 101 | VG_(tdict).track_new_mem_stack_8 || |
| 102 | VG_(tdict).track_die_mem_stack_8 || |
| 103 | VG_(tdict).track_new_mem_stack_12 || |
| 104 | VG_(tdict).track_die_mem_stack_12 || |
| 105 | VG_(tdict).track_new_mem_stack_16 || |
| 106 | VG_(tdict).track_die_mem_stack_16 || |
| 107 | VG_(tdict).track_new_mem_stack_32 || |
| 108 | VG_(tdict).track_die_mem_stack_32 || |
| 109 | VG_(tdict).track_new_mem_stack_112 || |
| 110 | VG_(tdict).track_die_mem_stack_112 || |
| 111 | VG_(tdict).track_new_mem_stack_128 || |
| 112 | VG_(tdict).track_die_mem_stack_128 || |
| 113 | VG_(tdict).track_new_mem_stack_144 || |
| 114 | VG_(tdict).track_die_mem_stack_144 || |
| 115 | VG_(tdict).track_new_mem_stack_160 || |
| 116 | VG_(tdict).track_die_mem_stack_160 || |
| 117 | VG_(tdict).track_new_mem_stack || |
| 118 | VG_(tdict).track_die_mem_stack ); |
njn | a1f783f | 2005-06-18 03:44:34 +0000 | [diff] [blame] | 119 | } |
| 120 | |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 121 | // - The SP aliases are held in an array which is used as a circular buffer. |
| 122 | // This misses very few constant updates of SP (ie. < 0.1%) while using a |
| 123 | // small, constant structure that will also never fill up and cause |
| 124 | // execution to abort. |
| 125 | // - Unused slots have a .temp value of 'IRTemp_INVALID'. |
| 126 | // - 'next_SP_alias_slot' is the index where the next alias will be stored. |
| 127 | // - If the buffer fills, we circle around and start over-writing |
| 128 | // non-IRTemp_INVALID values. This is rare, and the overwriting of a |
| 129 | // value that would have subsequently be used is even rarer. |
| 130 | // - Every slot below next_SP_alias_slot holds a non-IRTemp_INVALID value. |
| 131 | // The rest either all won't (if we haven't yet circled around) or all |
| 132 | // will (if we have circled around). |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 133 | |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 134 | typedef |
| 135 | struct { |
| 136 | IRTemp temp; |
| 137 | Long delta; |
| 138 | } |
| 139 | SP_Alias; |
| 140 | |
| 141 | // With 32 slots the buffer fills very rarely -- eg. once in a run of GCC. |
| 142 | // And I've tested with smaller values and the wrap-around case works ok. |
| 143 | #define N_ALIASES 32 |
| 144 | static SP_Alias SP_aliases[N_ALIASES]; |
| 145 | static Int next_SP_alias_slot = 0; |
| 146 | |
| 147 | static void clear_SP_aliases(void) |
| 148 | { |
| 149 | Int i; |
| 150 | for (i = 0; i < N_ALIASES; i++) { |
| 151 | SP_aliases[i].temp = IRTemp_INVALID; |
| 152 | SP_aliases[i].delta = 0; |
| 153 | } |
| 154 | next_SP_alias_slot = 0; |
| 155 | } |
| 156 | |
| 157 | static void add_SP_alias(IRTemp temp, Long delta) |
| 158 | { |
| 159 | vg_assert(temp != IRTemp_INVALID); |
| 160 | SP_aliases[ next_SP_alias_slot ].temp = temp; |
| 161 | SP_aliases[ next_SP_alias_slot ].delta = delta; |
| 162 | next_SP_alias_slot++; |
| 163 | if (N_ALIASES == next_SP_alias_slot) next_SP_alias_slot = 0; |
| 164 | } |
| 165 | |
| 166 | static Bool get_SP_delta(IRTemp temp, ULong* delta) |
| 167 | { |
| 168 | Int i; // i must be signed! |
| 169 | vg_assert(IRTemp_INVALID != temp); |
| 170 | // Search backwards between current buffer position and the start. |
| 171 | for (i = next_SP_alias_slot-1; i >= 0; i--) { |
| 172 | if (temp == SP_aliases[i].temp) { |
| 173 | *delta = SP_aliases[i].delta; |
| 174 | return True; |
| 175 | } |
| 176 | } |
| 177 | // Search backwards between the end and the current buffer position. |
| 178 | for (i = N_ALIASES-1; i >= next_SP_alias_slot; i--) { |
| 179 | if (temp == SP_aliases[i].temp) { |
| 180 | *delta = SP_aliases[i].delta; |
| 181 | return True; |
| 182 | } |
| 183 | } |
| 184 | return False; |
| 185 | } |
| 186 | |
| 187 | static void update_SP_aliases(Long delta) |
| 188 | { |
| 189 | Int i; |
| 190 | for (i = 0; i < N_ALIASES; i++) { |
| 191 | if (SP_aliases[i].temp == IRTemp_INVALID) { |
| 192 | return; |
| 193 | } |
| 194 | SP_aliases[i].delta += delta; |
| 195 | } |
| 196 | } |
| 197 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 198 | /* Given a guest IP, get an origin tag for a 1-element stack trace, |
| 199 | and wrap it up in an IR atom that can be passed as the origin-tag |
| 200 | value for a stack-adjustment helper function. */ |
| 201 | static IRExpr* mk_ecu_Expr ( Addr64 guest_IP ) |
| 202 | { |
| 203 | UInt ecu; |
| 204 | ExeContext* ec |
| 205 | = VG_(make_depth_1_ExeContext_from_Addr)( (Addr)guest_IP ); |
| 206 | vg_assert(ec); |
| 207 | ecu = VG_(get_ECU_from_ExeContext)( ec ); |
| 208 | vg_assert(VG_(is_plausible_ECU)(ecu)); |
| 209 | /* This is always safe to do, since ecu is only 32 bits, and |
| 210 | HWord is 32 or 64. */ |
| 211 | return mkIRExpr_HWord( (HWord)ecu ); |
| 212 | } |
| 213 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 214 | /* When gdbserver is activated, the translation of a block must |
| 215 | first be done by the tool function, then followed by a pass |
| 216 | which (if needed) instruments the code for gdbserver. |
| 217 | */ |
| 218 | static |
| 219 | IRSB* tool_instrument_then_gdbserver_if_needed ( VgCallbackClosure* closureV, |
| 220 | IRSB* sb_in, |
| 221 | VexGuestLayout* layout, |
| 222 | VexGuestExtents* vge, |
| 223 | IRType gWordTy, |
| 224 | IRType hWordTy ) |
| 225 | { |
| 226 | return VG_(instrument_for_gdbserver_if_needed) |
| 227 | (VG_(tdict).tool_instrument (closureV, |
| 228 | sb_in, |
| 229 | layout, |
| 230 | vge, |
| 231 | gWordTy, |
| 232 | hWordTy), |
| 233 | layout, |
| 234 | vge, |
| 235 | gWordTy, |
| 236 | hWordTy); |
| 237 | } |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 238 | |
| 239 | /* For tools that want to know about SP changes, this pass adds |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 240 | in the appropriate hooks. We have to do it after the tool's |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 241 | instrumentation, so the tool doesn't have to worry about the C calls |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 242 | it adds in, and we must do it before register allocation because |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 243 | spilled temps make it much harder to work out the SP deltas. |
| 244 | This it is done with Vex's "second instrumentation" pass. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 245 | |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 246 | Basically, we look for GET(SP)/PUT(SP) pairs and track constant |
| 247 | increments/decrements of SP between them. (This requires tracking one or |
| 248 | more "aliases", which are not exact aliases but instead are tempregs |
| 249 | whose value is equal to the SP's plus or minus a known constant.) |
| 250 | If all the changes to SP leading up to a PUT(SP) are by known, small |
| 251 | constants, we can do a specific call to eg. new_mem_stack_4, otherwise |
| 252 | we fall back to the case that handles an unknown SP change. |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 253 | |
| 254 | There is some extra complexity to deal correctly with updates to |
| 255 | only parts of SP. Bizarre, but it has been known to happen. |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 256 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 257 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 258 | IRSB* vg_SP_update_pass ( void* closureV, |
| 259 | IRSB* sb_in, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 260 | VexGuestLayout* layout, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 261 | VexGuestExtents* vge, |
| 262 | IRType gWordTy, |
| 263 | IRType hWordTy ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 264 | { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 265 | Int i, j, minoff_ST, maxoff_ST, sizeof_SP, offset_SP; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 266 | Int first_SP, last_SP, first_Put, last_Put; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 267 | IRDirty *dcall, *d; |
| 268 | IRStmt* st; |
| 269 | IRExpr* e; |
| 270 | IRRegArray* descr; |
| 271 | IRType typeof_SP; |
| 272 | Long delta, con; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 273 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 274 | /* Set up stuff for tracking the guest IP */ |
| 275 | Bool curr_IP_known = False; |
| 276 | Addr64 curr_IP = 0; |
| 277 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 278 | /* Set up BB */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 279 | IRSB* bb = emptyIRSB(); |
| 280 | bb->tyenv = deepCopyIRTypeEnv(sb_in->tyenv); |
| 281 | bb->next = deepCopyIRExpr(sb_in->next); |
| 282 | bb->jumpkind = sb_in->jumpkind; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 283 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 284 | delta = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 285 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 286 | sizeof_SP = layout->sizeof_SP; |
| 287 | offset_SP = layout->offset_SP; |
| 288 | typeof_SP = sizeof_SP==4 ? Ity_I32 : Ity_I64; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 289 | vg_assert(sizeof_SP == 4 || sizeof_SP == 8); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 290 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 291 | /* --- Start of #defines --- */ |
| 292 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 293 | # define IS_ADD(op) (sizeof_SP==4 ? ((op)==Iop_Add32) : ((op)==Iop_Add64)) |
| 294 | # define IS_SUB(op) (sizeof_SP==4 ? ((op)==Iop_Sub32) : ((op)==Iop_Sub64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 295 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 296 | # define IS_ADD_OR_SUB(op) (IS_ADD(op) || IS_SUB(op)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 297 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 298 | # define GET_CONST(con) \ |
| 299 | (sizeof_SP==4 ? (Long)(Int)(con->Ico.U32) \ |
| 300 | : (Long)(con->Ico.U64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 301 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 302 | # define DO_NEW(syze, tmpp) \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 303 | do { \ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 304 | Bool vanilla, w_ecu; \ |
| 305 | vg_assert(curr_IP_known); \ |
| 306 | vanilla = NULL != VG_(tdict).track_new_mem_stack_##syze; \ |
| 307 | w_ecu = NULL != VG_(tdict).track_new_mem_stack_##syze##_w_ECU; \ |
| 308 | vg_assert(!(vanilla && w_ecu)); /* can't have both */ \ |
| 309 | if (!(vanilla || w_ecu)) \ |
| 310 | goto generic; \ |
| 311 | \ |
| 312 | /* I don't know if it's really necessary to say that the */ \ |
| 313 | /* call reads the stack pointer. But anyway, we do. */ \ |
| 314 | if (w_ecu) { \ |
| 315 | dcall = unsafeIRDirty_0_N( \ |
| 316 | 2/*regparms*/, \ |
| 317 | "track_new_mem_stack_" #syze "_w_ECU", \ |
| 318 | VG_(fnptr_to_fnentry)( \ |
| 319 | VG_(tdict).track_new_mem_stack_##syze##_w_ECU ), \ |
| 320 | mkIRExprVec_2(IRExpr_RdTmp(tmpp), \ |
| 321 | mk_ecu_Expr(curr_IP)) \ |
| 322 | ); \ |
| 323 | } else { \ |
| 324 | dcall = unsafeIRDirty_0_N( \ |
| 325 | 1/*regparms*/, \ |
| 326 | "track_new_mem_stack_" #syze , \ |
| 327 | VG_(fnptr_to_fnentry)( \ |
| 328 | VG_(tdict).track_new_mem_stack_##syze ), \ |
| 329 | mkIRExprVec_1(IRExpr_RdTmp(tmpp)) \ |
| 330 | ); \ |
| 331 | } \ |
| 332 | dcall->nFxState = 1; \ |
| 333 | dcall->fxState[0].fx = Ifx_Read; \ |
| 334 | dcall->fxState[0].offset = layout->offset_SP; \ |
| 335 | dcall->fxState[0].size = layout->sizeof_SP; \ |
| 336 | \ |
| 337 | addStmtToIRSB( bb, IRStmt_Dirty(dcall) ); \ |
| 338 | \ |
| 339 | tl_assert(syze > 0); \ |
| 340 | update_SP_aliases(syze); \ |
| 341 | \ |
| 342 | n_SP_updates_fast++; \ |
| 343 | \ |
| 344 | } while (0) |
| 345 | |
| 346 | # define DO_DIE(syze, tmpp) \ |
| 347 | do { \ |
| 348 | if (!VG_(tdict).track_die_mem_stack_##syze) \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 349 | goto generic; \ |
| 350 | \ |
| 351 | /* I don't know if it's really necessary to say that the */ \ |
| 352 | /* call reads the stack pointer. But anyway, we do. */ \ |
| 353 | dcall = unsafeIRDirty_0_N( \ |
| 354 | 1/*regparms*/, \ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 355 | "track_die_mem_stack_" #syze, \ |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 356 | VG_(fnptr_to_fnentry)( \ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 357 | VG_(tdict).track_die_mem_stack_##syze ), \ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 358 | mkIRExprVec_1(IRExpr_RdTmp(tmpp)) \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 359 | ); \ |
| 360 | dcall->nFxState = 1; \ |
| 361 | dcall->fxState[0].fx = Ifx_Read; \ |
| 362 | dcall->fxState[0].offset = layout->offset_SP; \ |
| 363 | dcall->fxState[0].size = layout->sizeof_SP; \ |
| 364 | \ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 365 | addStmtToIRSB( bb, IRStmt_Dirty(dcall) ); \ |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 366 | \ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 367 | tl_assert(syze > 0); \ |
| 368 | update_SP_aliases(-(syze)); \ |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 369 | \ |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 370 | n_SP_updates_fast++; \ |
| 371 | \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 372 | } while (0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 373 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 374 | /* --- End of #defines --- */ |
| 375 | |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 376 | clear_SP_aliases(); |
| 377 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 378 | for (i = 0; i < sb_in->stmts_used; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 379 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 380 | st = sb_in->stmts[i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 381 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 382 | if (st->tag == Ist_IMark) { |
| 383 | curr_IP_known = True; |
| 384 | curr_IP = st->Ist.IMark.addr; |
| 385 | } |
| 386 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 387 | /* t = Get(sp): curr = t, delta = 0 */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 388 | if (st->tag != Ist_WrTmp) goto case2; |
| 389 | e = st->Ist.WrTmp.data; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 390 | if (e->tag != Iex_Get) goto case2; |
| 391 | if (e->Iex.Get.offset != offset_SP) goto case2; |
| 392 | if (e->Iex.Get.ty != typeof_SP) goto case2; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 393 | vg_assert( typeOfIRTemp(bb->tyenv, st->Ist.WrTmp.tmp) == typeof_SP ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 394 | add_SP_alias(st->Ist.WrTmp.tmp, 0); |
| 395 | addStmtToIRSB( bb, st ); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 396 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 397 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 398 | case2: |
| 399 | /* t' = curr +/- const: curr = t', delta +=/-= const */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 400 | if (st->tag != Ist_WrTmp) goto case3; |
| 401 | e = st->Ist.WrTmp.data; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 402 | if (e->tag != Iex_Binop) goto case3; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 403 | if (e->Iex.Binop.arg1->tag != Iex_RdTmp) goto case3; |
| 404 | if (!get_SP_delta(e->Iex.Binop.arg1->Iex.RdTmp.tmp, &delta)) goto case3; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 405 | if (e->Iex.Binop.arg2->tag != Iex_Const) goto case3; |
| 406 | if (!IS_ADD_OR_SUB(e->Iex.Binop.op)) goto case3; |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 407 | con = GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 408 | vg_assert( typeOfIRTemp(bb->tyenv, st->Ist.WrTmp.tmp) == typeof_SP ); |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 409 | if (IS_ADD(e->Iex.Binop.op)) { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 410 | add_SP_alias(st->Ist.WrTmp.tmp, delta + con); |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 411 | } else { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 412 | add_SP_alias(st->Ist.WrTmp.tmp, delta - con); |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 413 | } |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 414 | addStmtToIRSB( bb, st ); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 415 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 416 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 417 | case3: |
| 418 | /* t' = curr: curr = t' */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 419 | if (st->tag != Ist_WrTmp) goto case4; |
| 420 | e = st->Ist.WrTmp.data; |
| 421 | if (e->tag != Iex_RdTmp) goto case4; |
| 422 | if (!get_SP_delta(e->Iex.RdTmp.tmp, &delta)) goto case4; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 423 | vg_assert( typeOfIRTemp(bb->tyenv, st->Ist.WrTmp.tmp) == typeof_SP ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 424 | add_SP_alias(st->Ist.WrTmp.tmp, delta); |
| 425 | addStmtToIRSB( bb, st ); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 426 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 427 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 428 | case4: |
| 429 | /* Put(sp) = curr */ |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 430 | /* More generally, we must correctly handle a Put which writes |
| 431 | any part of SP, not just the case where all of SP is |
| 432 | written. */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 433 | if (st->tag != Ist_Put) goto case5; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 434 | first_SP = offset_SP; |
| 435 | last_SP = first_SP + sizeof_SP - 1; |
| 436 | first_Put = st->Ist.Put.offset; |
| 437 | last_Put = first_Put |
| 438 | + sizeofIRType( typeOfIRExpr( bb->tyenv, st->Ist.Put.data )) |
| 439 | - 1; |
| 440 | vg_assert(first_SP <= last_SP); |
| 441 | vg_assert(first_Put <= last_Put); |
| 442 | |
| 443 | if (last_Put < first_SP || last_SP < first_Put) |
| 444 | goto case5; /* no overlap */ |
| 445 | |
| 446 | if (st->Ist.Put.data->tag == Iex_RdTmp |
| 447 | && get_SP_delta(st->Ist.Put.data->Iex.RdTmp.tmp, &delta)) { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 448 | IRTemp tttmp = st->Ist.Put.data->Iex.RdTmp.tmp; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 449 | /* Why should the following assertion hold? Because any |
| 450 | alias added by put_SP_alias must be of a temporary which |
| 451 | has the same type as typeof_SP, and whose value is a Get |
| 452 | at exactly offset_SP of size typeof_SP. Each call to |
| 453 | put_SP_alias is immediately preceded by an assertion that |
| 454 | we are putting in a binding for a correctly-typed |
| 455 | temporary. */ |
| 456 | vg_assert( typeOfIRTemp(bb->tyenv, tttmp) == typeof_SP ); |
| 457 | /* From the same type-and-offset-correctness argument, if |
| 458 | we found a useable alias, it must for an "exact" write of SP. */ |
| 459 | vg_assert(first_SP == first_Put); |
| 460 | vg_assert(last_SP == last_Put); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 461 | switch (delta) { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 462 | case 0: addStmtToIRSB(bb,st); continue; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 463 | case 4: DO_DIE( 4, tttmp); addStmtToIRSB(bb,st); continue; |
| 464 | case -4: DO_NEW( 4, tttmp); addStmtToIRSB(bb,st); continue; |
| 465 | case 8: DO_DIE( 8, tttmp); addStmtToIRSB(bb,st); continue; |
| 466 | case -8: DO_NEW( 8, tttmp); addStmtToIRSB(bb,st); continue; |
| 467 | case 12: DO_DIE( 12, tttmp); addStmtToIRSB(bb,st); continue; |
| 468 | case -12: DO_NEW( 12, tttmp); addStmtToIRSB(bb,st); continue; |
| 469 | case 16: DO_DIE( 16, tttmp); addStmtToIRSB(bb,st); continue; |
| 470 | case -16: DO_NEW( 16, tttmp); addStmtToIRSB(bb,st); continue; |
| 471 | case 32: DO_DIE( 32, tttmp); addStmtToIRSB(bb,st); continue; |
| 472 | case -32: DO_NEW( 32, tttmp); addStmtToIRSB(bb,st); continue; |
| 473 | case 112: DO_DIE( 112, tttmp); addStmtToIRSB(bb,st); continue; |
| 474 | case -112: DO_NEW( 112, tttmp); addStmtToIRSB(bb,st); continue; |
| 475 | case 128: DO_DIE( 128, tttmp); addStmtToIRSB(bb,st); continue; |
| 476 | case -128: DO_NEW( 128, tttmp); addStmtToIRSB(bb,st); continue; |
| 477 | case 144: DO_DIE( 144, tttmp); addStmtToIRSB(bb,st); continue; |
| 478 | case -144: DO_NEW( 144, tttmp); addStmtToIRSB(bb,st); continue; |
| 479 | case 160: DO_DIE( 160, tttmp); addStmtToIRSB(bb,st); continue; |
| 480 | case -160: DO_NEW( 160, tttmp); addStmtToIRSB(bb,st); continue; |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 481 | default: |
sewardj | bb68627 | 2006-01-22 20:12:45 +0000 | [diff] [blame] | 482 | /* common values for ppc64: 144 128 160 112 176 */ |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 483 | n_SP_updates_generic_known++; |
| 484 | goto generic; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 485 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 486 | } else { |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 487 | /* Deal with an unknown update to SP. We're here because |
| 488 | either: |
| 489 | (1) the Put does not exactly cover SP; it is a partial update. |
| 490 | Highly unlikely, but has been known to happen for 16-bit |
| 491 | Windows apps running on Wine, doing 16-bit adjustments to |
| 492 | %sp. |
| 493 | (2) the Put does exactly cover SP, but we are unable to |
| 494 | determine how the value relates to the old SP. In any |
| 495 | case, we cannot assume that the Put.data value is a tmp; |
| 496 | we must assume it can be anything allowed in flat IR (tmp |
| 497 | or const). |
| 498 | */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 499 | IRTemp old_SP; |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 500 | n_SP_updates_generic_unknown++; |
| 501 | |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 502 | // Nb: if all is well, this generic case will typically be |
| 503 | // called something like every 1000th SP update. If it's more than |
| 504 | // that, the above code may be missing some cases. |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 505 | generic: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 506 | /* Pass both the old and new SP values to this helper. Also, |
| 507 | pass an origin tag, even if it isn't needed. */ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 508 | old_SP = newIRTemp(bb->tyenv, typeof_SP); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 509 | addStmtToIRSB( |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 510 | bb, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 511 | IRStmt_WrTmp( old_SP, IRExpr_Get(offset_SP, typeof_SP) ) |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 512 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 513 | |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 514 | /* Now we know what the old value of SP is. But knowing the new |
| 515 | value is a bit tricky if there is a partial write. */ |
| 516 | if (first_Put == first_SP && last_Put == last_SP) { |
| 517 | /* The common case, an exact write to SP. So st->Ist.Put.data |
| 518 | does hold the new value; simple. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 519 | vg_assert(curr_IP_known); |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 520 | dcall = unsafeIRDirty_0_N( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 521 | 3/*regparms*/, |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 522 | "VG_(unknown_SP_update)", |
| 523 | VG_(fnptr_to_fnentry)( &VG_(unknown_SP_update) ), |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 524 | mkIRExprVec_3( IRExpr_RdTmp(old_SP), st->Ist.Put.data, |
| 525 | mk_ecu_Expr(curr_IP) ) |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 526 | ); |
| 527 | addStmtToIRSB( bb, IRStmt_Dirty(dcall) ); |
| 528 | /* don't forget the original assignment */ |
| 529 | addStmtToIRSB( bb, st ); |
| 530 | } else { |
| 531 | /* We have a partial update to SP. We need to know what |
| 532 | the new SP will be, and hand that to the helper call, |
| 533 | but when the helper call happens, SP must hold the |
| 534 | value it had before the update. Tricky. |
| 535 | Therefore use the following kludge: |
| 536 | 1. do the partial SP update (Put) |
| 537 | 2. Get the new SP value into a tmp, new_SP |
| 538 | 3. Put old_SP |
| 539 | 4. Call the helper |
| 540 | 5. Put new_SP |
| 541 | */ |
| 542 | IRTemp new_SP; |
| 543 | /* 1 */ |
| 544 | addStmtToIRSB( bb, st ); |
| 545 | /* 2 */ |
| 546 | new_SP = newIRTemp(bb->tyenv, typeof_SP); |
| 547 | addStmtToIRSB( |
| 548 | bb, |
| 549 | IRStmt_WrTmp( new_SP, IRExpr_Get(offset_SP, typeof_SP) ) |
| 550 | ); |
| 551 | /* 3 */ |
| 552 | addStmtToIRSB( bb, IRStmt_Put(offset_SP, IRExpr_RdTmp(old_SP) )); |
| 553 | /* 4 */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 554 | vg_assert(curr_IP_known); |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 555 | dcall = unsafeIRDirty_0_N( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 556 | 3/*regparms*/, |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 557 | "VG_(unknown_SP_update)", |
| 558 | VG_(fnptr_to_fnentry)( &VG_(unknown_SP_update) ), |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 559 | mkIRExprVec_3( IRExpr_RdTmp(old_SP), |
| 560 | IRExpr_RdTmp(new_SP), |
| 561 | mk_ecu_Expr(curr_IP) ) |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 562 | ); |
| 563 | addStmtToIRSB( bb, IRStmt_Dirty(dcall) ); |
| 564 | /* 5 */ |
| 565 | addStmtToIRSB( bb, IRStmt_Put(offset_SP, IRExpr_RdTmp(new_SP) )); |
| 566 | } |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 567 | |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 568 | /* Forget what we already know. */ |
njn | 5096a39 | 2005-12-13 20:05:00 +0000 | [diff] [blame] | 569 | clear_SP_aliases(); |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 570 | |
| 571 | /* If this is a Put of a tmp that exactly updates SP, |
| 572 | start tracking aliases against this tmp. */ |
| 573 | |
| 574 | if (first_Put == first_SP && last_Put == last_SP |
| 575 | && st->Ist.Put.data->tag == Iex_RdTmp) { |
| 576 | vg_assert( typeOfIRTemp(bb->tyenv, st->Ist.Put.data->Iex.RdTmp.tmp) |
| 577 | == typeof_SP ); |
| 578 | add_SP_alias(st->Ist.Put.data->Iex.RdTmp.tmp, 0); |
| 579 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 580 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 581 | } |
| 582 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 583 | case5: |
| 584 | /* PutI or Dirty call which overlaps SP: complain. We can't |
| 585 | deal with SP changing in weird ways (well, we can, but not at |
| 586 | this time of night). */ |
| 587 | if (st->tag == Ist_PutI) { |
| 588 | descr = st->Ist.PutI.descr; |
| 589 | minoff_ST = descr->base; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 590 | maxoff_ST = descr->base |
| 591 | + descr->nElems * sizeofIRType(descr->elemTy) - 1; |
| 592 | if (!(offset_SP > maxoff_ST |
| 593 | || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 594 | goto complain; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 595 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 596 | if (st->tag == Ist_Dirty) { |
| 597 | d = st->Ist.Dirty.details; |
| 598 | for (j = 0; j < d->nFxState; j++) { |
| 599 | minoff_ST = d->fxState[j].offset; |
| 600 | maxoff_ST = d->fxState[j].offset + d->fxState[j].size - 1; |
| 601 | if (d->fxState[j].fx == Ifx_Read || d->fxState[j].fx == Ifx_None) |
| 602 | continue; |
sewardj | 2831b51 | 2007-11-11 18:56:39 +0000 | [diff] [blame] | 603 | if (!(offset_SP > maxoff_ST |
| 604 | || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 605 | goto complain; |
| 606 | } |
| 607 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 608 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 609 | /* well, not interesting. Just copy and keep going. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 610 | addStmtToIRSB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 611 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 612 | } /* for (i = 0; i < sb_in->stmts_used; i++) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 613 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 614 | return bb; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 615 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 616 | complain: |
| 617 | VG_(core_panic)("vg_SP_update_pass: PutI or Dirty which overlaps SP"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 618 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 619 | #undef IS_ADD |
| 620 | #undef IS_SUB |
| 621 | #undef IS_ADD_OR_SUB |
| 622 | #undef GET_CONST |
| 623 | #undef DO_NEW |
| 624 | #undef DO_DIE |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 625 | } |
sewardj | 7c4b604 | 2003-06-14 15:47:15 +0000 | [diff] [blame] | 626 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 627 | /*------------------------------------------------------------*/ |
| 628 | /*--- Main entry point for the JITter. ---*/ |
| 629 | /*------------------------------------------------------------*/ |
| 630 | |
sewardj | d2b70dc | 2005-10-14 17:22:31 +0000 | [diff] [blame] | 631 | /* Extra comments re self-checking translations and self-modifying |
| 632 | code. (JRS 14 Oct 05). |
| 633 | |
| 634 | There are 3 modes: |
| 635 | (1) no checking: all code assumed to be not self-modifying |
| 636 | (2) partial: known-problematic situations get a self-check |
| 637 | (3) full checking: all translations get a self-check |
| 638 | |
| 639 | As currently implemented, the default is (2). (3) is always safe, |
| 640 | but very slow. (1) works mostly, but fails for gcc nested-function |
| 641 | code which uses trampolines on the stack; this situation is |
| 642 | detected and handled by (2). |
| 643 | |
| 644 | ---------- |
| 645 | |
| 646 | A more robust and transparent solution, which is not currently |
| 647 | implemented, is a variant of (2): if a translation is made from an |
| 648 | area which aspacem says does not have 'w' permission, then it can |
| 649 | be non-self-checking. Otherwise, it needs a self-check. |
| 650 | |
| 651 | This is complicated by Vex's basic-block chasing. If a self-check |
| 652 | is requested, then Vex will not chase over basic block boundaries |
| 653 | (it's too complex). However there is still a problem if it chases |
| 654 | from a non-'w' area into a 'w' area. |
| 655 | |
| 656 | I think the right thing to do is: |
| 657 | |
| 658 | - if a translation request starts in a 'w' area, ask for a |
| 659 | self-checking translation, and do not allow any chasing (make |
| 660 | chase_into_ok return False). Note that the latter is redundant |
| 661 | in the sense that Vex won't chase anyway in this situation. |
| 662 | |
| 663 | - if a translation request starts in a non-'w' area, do not ask for |
| 664 | a self-checking translation. However, do not allow chasing (as |
| 665 | determined by chase_into_ok) to go into a 'w' area. |
| 666 | |
| 667 | The result of this is that all code inside 'w' areas is self |
| 668 | checking. |
| 669 | |
| 670 | To complete the trick, there is a caveat: we must watch the |
| 671 | client's mprotect calls. If pages are changed from non-'w' to 'w' |
| 672 | then we should throw away all translations which intersect the |
| 673 | affected area, so as to force them to be redone with self-checks. |
| 674 | |
| 675 | ---------- |
| 676 | |
| 677 | The above outlines the conditions under which bb chasing is allowed |
| 678 | from a self-modifying-code point of view. There are other |
| 679 | situations pertaining to function redirection in which it is |
| 680 | necessary to disallow chasing, but those fall outside the scope of |
| 681 | this comment. |
| 682 | */ |
| 683 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 684 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 685 | /* Vex dumps the final code in here. Then we can copy it off |
| 686 | wherever we like. */ |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 687 | /* 60000: should agree with assertion in VG_(add_to_transtab) in |
| 688 | m_transtab.c. */ |
| 689 | #define N_TMPBUF 60000 |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 690 | static UChar tmpbuf[N_TMPBUF]; |
| 691 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 692 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 693 | /* Function pointers we must supply to LibVEX in order that it |
| 694 | can bomb out and emit messages under Valgrind's control. */ |
| 695 | __attribute__ ((noreturn)) |
| 696 | static |
| 697 | void failure_exit ( void ) |
| 698 | { |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 699 | LibVEX_ShowAllocStats(); |
sewardj | c621f76 | 2004-11-26 13:49:59 +0000 | [diff] [blame] | 700 | VG_(core_panic)("LibVEX called failure_exit()."); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 704 | void log_bytes ( HChar* bytes, Int nbytes ) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 705 | { |
| 706 | Int i; |
| 707 | for (i = 0; i < nbytes-3; i += 4) |
| 708 | VG_(printf)("%c%c%c%c", bytes[i], bytes[i+1], bytes[i+2], bytes[i+3]); |
| 709 | for (; i < nbytes; i++) |
| 710 | VG_(printf)("%c", bytes[i]); |
| 711 | } |
| 712 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 713 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 714 | /* --------- Various helper functions for translation --------- */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 715 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 716 | /* Look for reasons to disallow making translations from the given |
| 717 | segment. */ |
| 718 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 719 | static Bool translations_allowable_from_seg ( NSegment const* seg ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 720 | { |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 721 | # if defined(VGA_x86) || defined(VGA_s390x) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 722 | Bool allowR = True; |
| 723 | # else |
| 724 | Bool allowR = False; |
| 725 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 726 | return seg != NULL |
tom | 1da86fe | 2009-10-12 13:53:12 +0000 | [diff] [blame] | 727 | && (seg->kind == SkAnonC || seg->kind == SkFileC || seg->kind == SkShmC) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 728 | && (seg->hasX || (seg->hasR && allowR)); |
| 729 | } |
| 730 | |
| 731 | |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 732 | /* Produce a bitmask stating which of the supplied extents needs a |
| 733 | self-check. See documentation of |
| 734 | VexTranslateArgs::needs_self_check for more details about the |
| 735 | return convention. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 736 | |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 737 | static UInt needs_self_check ( void* closureV, |
| 738 | VexGuestExtents* vge ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 739 | { |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 740 | VgCallbackClosure* closure = (VgCallbackClosure*)closureV; |
| 741 | UInt i, bitset; |
| 742 | |
| 743 | vg_assert(vge->n_used >= 1 && vge->n_used <= 3); |
| 744 | bitset = 0; |
| 745 | |
| 746 | for (i = 0; i < vge->n_used; i++) { |
| 747 | Bool check = False; |
| 748 | Addr addr = (Addr)vge->base[i]; |
| 749 | SizeT len = (SizeT)vge->len[i]; |
| 750 | NSegment const* segA = NULL; |
| 751 | |
| 752 | # if defined(VGO_darwin) |
| 753 | // GrP fixme hack - dyld i386 IMPORT gets rewritten. |
| 754 | // To really do this correctly, we'd need to flush the |
| 755 | // translation cache whenever a segment became +WX. |
| 756 | segA = VG_(am_find_nsegment)(addr); |
| 757 | if (segA && segA->hasX && segA->hasW) |
| 758 | check = True; |
| 759 | # endif |
| 760 | |
| 761 | if (!check) { |
| 762 | switch (VG_(clo_smc_check)) { |
| 763 | case Vg_SmcNone: |
| 764 | /* never check (except as per Darwin hack above) */ |
| 765 | break; |
| 766 | case Vg_SmcAll: |
| 767 | /* always check */ |
| 768 | check = True; |
| 769 | break; |
| 770 | case Vg_SmcStack: { |
| 771 | /* check if the address is in the same segment as this |
| 772 | thread's stack pointer */ |
| 773 | Addr sp = VG_(get_SP)(closure->tid); |
| 774 | if (!segA) { |
| 775 | segA = VG_(am_find_nsegment)(addr); |
| 776 | } |
| 777 | NSegment const* segSP = VG_(am_find_nsegment)(sp); |
| 778 | if (segA && segSP && segA == segSP) |
| 779 | check = True; |
| 780 | break; |
| 781 | } |
| 782 | case Vg_SmcAllNonFile: { |
| 783 | /* check if any part of the extent is not in a |
| 784 | file-mapped segment */ |
| 785 | if (!segA) { |
| 786 | segA = VG_(am_find_nsegment)(addr); |
| 787 | } |
| 788 | if (segA && segA->kind == SkFileC && segA->start <= addr |
| 789 | && (len == 0 || addr + len <= segA->end + 1)) { |
| 790 | /* in a file-mapped segment; skip the check */ |
| 791 | } else { |
| 792 | check = True; |
| 793 | } |
| 794 | break; |
| 795 | } |
| 796 | default: |
| 797 | vg_assert(0); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | if (check) |
| 802 | bitset |= (1 << i); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 803 | } |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 804 | |
| 805 | return bitset; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
| 809 | /* This is a callback passed to LibVEX_Translate. It stops Vex from |
| 810 | chasing into function entry points that we wish to redirect. |
| 811 | Chasing across them obviously defeats the redirect mechanism, with |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 812 | bad effects for Memcheck, Helgrind, DRD, Massif, and possibly others. |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 813 | */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 814 | static Bool chase_into_ok ( void* closureV, Addr64 addr64 ) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 815 | { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 816 | Addr addr = (Addr)addr64; |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 817 | NSegment const* seg = VG_(am_find_nsegment)(addr); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 818 | |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 819 | /* Work through a list of possibilities why we might not want to |
| 820 | allow a chase. */ |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 821 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 822 | /* Destination not in a plausible segment? */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 823 | if (!translations_allowable_from_seg(seg)) |
| 824 | goto dontchase; |
| 825 | |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 826 | /* Destination is redirected? */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 827 | if (addr != VG_(redir_do_lookup)(addr, NULL)) |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 828 | goto dontchase; |
| 829 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 830 | # if defined(VG_PLAT_USES_PPCTOC) |
| 831 | /* This needs to be at the start of its own block. Don't chase. Re |
| 832 | ULong_to_Ptr, be careful to ensure we only compare 32 bits on a |
| 833 | 32-bit target.*/ |
| 834 | if (ULong_to_Ptr(addr64) |
| 835 | == (void*)&VG_(ppctoc_magic_redirect_return_stub)) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 836 | goto dontchase; |
| 837 | # endif |
| 838 | |
sewardj | 5f76de0 | 2007-02-11 05:08:06 +0000 | [diff] [blame] | 839 | /* overly conservative, but .. don't chase into the distinguished |
| 840 | address that m_transtab uses as an empty-slot marker for |
| 841 | VG_(tt_fast). */ |
| 842 | if (addr == TRANSTAB_BOGUS_GUEST_ADDR) |
| 843 | goto dontchase; |
| 844 | |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 845 | /* well, ok then. go on and chase. */ |
| 846 | return True; |
| 847 | |
| 848 | vg_assert(0); |
| 849 | /*NOTREACHED*/ |
| 850 | |
| 851 | dontchase: |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 852 | if (0) VG_(printf)("not chasing into 0x%lx\n", addr); |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 853 | return False; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 854 | } |
| 855 | |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 856 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 857 | /* --------------- helpers for with-TOC platforms --------------- */ |
| 858 | |
| 859 | /* NOTE: with-TOC platforms are: ppc64-linux, ppc32-aix5, ppc64-aix5. */ |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 860 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 861 | static IRExpr* mkU64 ( ULong n ) { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 862 | return IRExpr_Const(IRConst_U64(n)); |
| 863 | } |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 864 | static IRExpr* mkU32 ( UInt n ) { |
| 865 | return IRExpr_Const(IRConst_U32(n)); |
| 866 | } |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 867 | |
| 868 | #if defined(VG_PLAT_USES_PPCTOC) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 869 | static IRExpr* mkU8 ( UChar n ) { |
| 870 | return IRExpr_Const(IRConst_U8(n)); |
| 871 | } |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 872 | static IRExpr* narrowTo32 ( IRTypeEnv* tyenv, IRExpr* e ) { |
| 873 | if (typeOfIRExpr(tyenv, e) == Ity_I32) { |
| 874 | return e; |
| 875 | } else { |
| 876 | vg_assert(typeOfIRExpr(tyenv, e) == Ity_I64); |
| 877 | return IRExpr_Unop(Iop_64to32, e); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /* Generate code to push word-typed expression 'e' onto this thread's |
| 882 | redir stack, checking for stack overflow and generating code to |
| 883 | bomb out if so. */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 884 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 885 | static void gen_PUSH ( IRSB* bb, IRExpr* e ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 886 | { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 887 | IRRegArray* descr; |
| 888 | IRTemp t1; |
| 889 | IRExpr* one; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 890 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 891 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc64_aix5) |
| 892 | Int stack_size = VEX_GUEST_PPC64_REDIR_STACK_SIZE; |
| 893 | Int offB_REDIR_SP = offsetof(VexGuestPPC64State,guest_REDIR_SP); |
| 894 | Int offB_REDIR_STACK = offsetof(VexGuestPPC64State,guest_REDIR_STACK); |
| 895 | Int offB_EMWARN = offsetof(VexGuestPPC64State,guest_EMWARN); |
| 896 | Bool is64 = True; |
| 897 | IRType ty_Word = Ity_I64; |
| 898 | IROp op_CmpNE = Iop_CmpNE64; |
| 899 | IROp op_Sar = Iop_Sar64; |
| 900 | IROp op_Sub = Iop_Sub64; |
| 901 | IROp op_Add = Iop_Add64; |
| 902 | IRExpr*(*mkU)(ULong) = mkU64; |
| 903 | vg_assert(VG_WORDSIZE == 8); |
| 904 | # else |
| 905 | Int stack_size = VEX_GUEST_PPC32_REDIR_STACK_SIZE; |
| 906 | Int offB_REDIR_SP = offsetof(VexGuestPPC32State,guest_REDIR_SP); |
| 907 | Int offB_REDIR_STACK = offsetof(VexGuestPPC32State,guest_REDIR_STACK); |
| 908 | Int offB_EMWARN = offsetof(VexGuestPPC32State,guest_EMWARN); |
| 909 | Bool is64 = False; |
| 910 | IRType ty_Word = Ity_I32; |
| 911 | IROp op_CmpNE = Iop_CmpNE32; |
| 912 | IROp op_Sar = Iop_Sar32; |
| 913 | IROp op_Sub = Iop_Sub32; |
| 914 | IROp op_Add = Iop_Add32; |
| 915 | IRExpr*(*mkU)(UInt) = mkU32; |
| 916 | vg_assert(VG_WORDSIZE == 4); |
| 917 | # endif |
| 918 | |
| 919 | vg_assert(sizeof(void*) == VG_WORDSIZE); |
| 920 | vg_assert(sizeof(Word) == VG_WORDSIZE); |
| 921 | vg_assert(sizeof(Addr) == VG_WORDSIZE); |
| 922 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 923 | descr = mkIRRegArray( offB_REDIR_STACK, ty_Word, stack_size ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 924 | t1 = newIRTemp( bb->tyenv, ty_Word ); |
| 925 | one = mkU(1); |
| 926 | |
| 927 | vg_assert(typeOfIRExpr(bb->tyenv, e) == ty_Word); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 928 | |
| 929 | /* t1 = guest_REDIR_SP + 1 */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 930 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 931 | bb, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 932 | IRStmt_WrTmp( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 933 | t1, |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 934 | IRExpr_Binop(op_Add, IRExpr_Get( offB_REDIR_SP, ty_Word ), one) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 935 | ) |
| 936 | ); |
| 937 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 938 | /* Bomb out if t1 >=s stack_size, that is, (stack_size-1)-t1 <s 0. |
| 939 | The destination (0) is a bit bogus but it doesn't matter since |
| 940 | this is an unrecoverable error and will lead to Valgrind |
| 941 | shutting down. _EMWARN is set regardless - that's harmless |
| 942 | since is only has a meaning if the exit is taken. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 943 | addStmtToIRSB( |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 944 | bb, |
| 945 | IRStmt_Put(offB_EMWARN, mkU32(EmWarn_PPC64_redir_overflow)) |
| 946 | ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 947 | addStmtToIRSB( |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 948 | bb, |
| 949 | IRStmt_Exit( |
| 950 | IRExpr_Binop( |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 951 | op_CmpNE, |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 952 | IRExpr_Binop( |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 953 | op_Sar, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 954 | IRExpr_Binop(op_Sub,mkU(stack_size-1),IRExpr_RdTmp(t1)), |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 955 | mkU8(8 * VG_WORDSIZE - 1) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 956 | ), |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 957 | mkU(0) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 958 | ), |
| 959 | Ijk_EmFail, |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 960 | is64 ? IRConst_U64(0) : IRConst_U32(0) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 961 | ) |
| 962 | ); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 963 | |
| 964 | /* guest_REDIR_SP = t1 */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 965 | addStmtToIRSB(bb, IRStmt_Put(offB_REDIR_SP, IRExpr_RdTmp(t1))); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 966 | |
| 967 | /* guest_REDIR_STACK[t1+0] = e */ |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 968 | /* PutI/GetI have I32-typed indexes regardless of guest word size */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 969 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 970 | bb, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 971 | IRStmt_PutI(descr, narrowTo32(bb->tyenv,IRExpr_RdTmp(t1)), 0, e) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 972 | ); |
| 973 | } |
| 974 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 975 | |
| 976 | /* Generate code to pop a word-sized value from this thread's redir |
| 977 | stack, binding it to a new temporary, which is returned. As with |
| 978 | gen_PUSH, an overflow check is also performed. */ |
| 979 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 980 | static IRTemp gen_POP ( IRSB* bb ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 981 | { |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 982 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc64_aix5) |
| 983 | Int stack_size = VEX_GUEST_PPC64_REDIR_STACK_SIZE; |
| 984 | Int offB_REDIR_SP = offsetof(VexGuestPPC64State,guest_REDIR_SP); |
| 985 | Int offB_REDIR_STACK = offsetof(VexGuestPPC64State,guest_REDIR_STACK); |
| 986 | Int offB_EMWARN = offsetof(VexGuestPPC64State,guest_EMWARN); |
| 987 | Bool is64 = True; |
| 988 | IRType ty_Word = Ity_I64; |
| 989 | IROp op_CmpNE = Iop_CmpNE64; |
| 990 | IROp op_Sar = Iop_Sar64; |
| 991 | IROp op_Sub = Iop_Sub64; |
| 992 | IRExpr*(*mkU)(ULong) = mkU64; |
| 993 | # else |
| 994 | Int stack_size = VEX_GUEST_PPC32_REDIR_STACK_SIZE; |
| 995 | Int offB_REDIR_SP = offsetof(VexGuestPPC32State,guest_REDIR_SP); |
| 996 | Int offB_REDIR_STACK = offsetof(VexGuestPPC32State,guest_REDIR_STACK); |
| 997 | Int offB_EMWARN = offsetof(VexGuestPPC32State,guest_EMWARN); |
| 998 | Bool is64 = False; |
| 999 | IRType ty_Word = Ity_I32; |
| 1000 | IROp op_CmpNE = Iop_CmpNE32; |
| 1001 | IROp op_Sar = Iop_Sar32; |
| 1002 | IROp op_Sub = Iop_Sub32; |
| 1003 | IRExpr*(*mkU)(UInt) = mkU32; |
| 1004 | # endif |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1005 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1006 | IRRegArray* descr = mkIRRegArray( offB_REDIR_STACK, ty_Word, stack_size ); |
| 1007 | IRTemp t1 = newIRTemp( bb->tyenv, ty_Word ); |
| 1008 | IRTemp res = newIRTemp( bb->tyenv, ty_Word ); |
| 1009 | IRExpr* one = mkU(1); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1010 | |
| 1011 | vg_assert(sizeof(void*) == VG_WORDSIZE); |
| 1012 | vg_assert(sizeof(Word) == VG_WORDSIZE); |
| 1013 | vg_assert(sizeof(Addr) == VG_WORDSIZE); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* t1 = guest_REDIR_SP */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1016 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1017 | bb, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1018 | IRStmt_WrTmp( t1, IRExpr_Get( offB_REDIR_SP, ty_Word ) ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1019 | ); |
| 1020 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1021 | /* Bomb out if t1 < 0. Same comments as gen_PUSH apply. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1022 | addStmtToIRSB( |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1023 | bb, |
| 1024 | IRStmt_Put(offB_EMWARN, mkU32(EmWarn_PPC64_redir_underflow)) |
| 1025 | ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1026 | addStmtToIRSB( |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1027 | bb, |
| 1028 | IRStmt_Exit( |
| 1029 | IRExpr_Binop( |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1030 | op_CmpNE, |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1031 | IRExpr_Binop( |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1032 | op_Sar, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1033 | IRExpr_RdTmp(t1), |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1034 | mkU8(8 * VG_WORDSIZE - 1) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1035 | ), |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1036 | mkU(0) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1037 | ), |
| 1038 | Ijk_EmFail, |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1039 | is64 ? IRConst_U64(0) : IRConst_U32(0) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1040 | ) |
| 1041 | ); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* res = guest_REDIR_STACK[t1+0] */ |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1044 | /* PutI/GetI have I32-typed indexes regardless of guest word size */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1045 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1046 | bb, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1047 | IRStmt_WrTmp( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1048 | res, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1049 | IRExpr_GetI(descr, narrowTo32(bb->tyenv,IRExpr_RdTmp(t1)), 0) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1050 | ) |
| 1051 | ); |
| 1052 | |
| 1053 | /* guest_REDIR_SP = t1-1 */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1054 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1055 | bb, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1056 | IRStmt_Put(offB_REDIR_SP, IRExpr_Binop(op_Sub, IRExpr_RdTmp(t1), one)) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1057 | ); |
| 1058 | |
| 1059 | return res; |
| 1060 | } |
| 1061 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1062 | /* Generate code to push LR and R2 onto this thread's redir stack, |
| 1063 | then set R2 to the new value (which is the TOC pointer to be used |
| 1064 | for the duration of the replacement function, as determined by |
| 1065 | m_debuginfo), and set LR to the magic return stub, so we get to |
| 1066 | intercept the return and restore R2 and L2 to the values saved |
| 1067 | here. */ |
| 1068 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1069 | static void gen_push_and_set_LR_R2 ( IRSB* bb, Addr64 new_R2_value ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1070 | { |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1071 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc64_aix5) |
| 1072 | Addr64 bogus_RA = (Addr64)&VG_(ppctoc_magic_redirect_return_stub); |
| 1073 | Int offB_GPR2 = offsetof(VexGuestPPC64State,guest_GPR2); |
| 1074 | Int offB_LR = offsetof(VexGuestPPC64State,guest_LR); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1075 | gen_PUSH( bb, IRExpr_Get(offB_LR, Ity_I64) ); |
| 1076 | gen_PUSH( bb, IRExpr_Get(offB_GPR2, Ity_I64) ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1077 | addStmtToIRSB( bb, IRStmt_Put( offB_LR, mkU64( bogus_RA )) ); |
| 1078 | addStmtToIRSB( bb, IRStmt_Put( offB_GPR2, mkU64( new_R2_value )) ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1079 | |
| 1080 | # elif defined(VGP_ppc32_aix5) |
| 1081 | Addr32 bogus_RA = (Addr32)&VG_(ppctoc_magic_redirect_return_stub); |
| 1082 | Int offB_GPR2 = offsetof(VexGuestPPC32State,guest_GPR2); |
| 1083 | Int offB_LR = offsetof(VexGuestPPC32State,guest_LR); |
| 1084 | gen_PUSH( bb, IRExpr_Get(offB_LR, Ity_I32) ); |
| 1085 | gen_PUSH( bb, IRExpr_Get(offB_GPR2, Ity_I32) ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1086 | addStmtToIRSB( bb, IRStmt_Put( offB_LR, mkU32( bogus_RA )) ); |
| 1087 | addStmtToIRSB( bb, IRStmt_Put( offB_GPR2, mkU32( new_R2_value )) ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1088 | |
| 1089 | # else |
| 1090 | # error Platform is not TOC-afflicted, fortunately |
| 1091 | # endif |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1094 | static void gen_pop_R2_LR_then_bLR ( IRSB* bb ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1095 | { |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1096 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc64_aix5) |
| 1097 | Int offB_GPR2 = offsetof(VexGuestPPC64State,guest_GPR2); |
| 1098 | Int offB_LR = offsetof(VexGuestPPC64State,guest_LR); |
| 1099 | IRTemp old_R2 = newIRTemp( bb->tyenv, Ity_I64 ); |
| 1100 | IRTemp old_LR = newIRTemp( bb->tyenv, Ity_I64 ); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1101 | /* Restore R2 */ |
| 1102 | old_R2 = gen_POP( bb ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1103 | addStmtToIRSB( bb, IRStmt_Put( offB_GPR2, IRExpr_RdTmp(old_R2)) ); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1104 | /* Restore LR */ |
| 1105 | old_LR = gen_POP( bb ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1106 | addStmtToIRSB( bb, IRStmt_Put( offB_LR, IRExpr_RdTmp(old_LR)) ); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1107 | /* Branch to LR */ |
| 1108 | /* re boring, we arrived here precisely because a wrapped fn did a |
| 1109 | blr (hence Ijk_Ret); so we should just mark this jump as Boring, |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1110 | else one _Call will have resulted in two _Rets. */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1111 | bb->jumpkind = Ijk_Boring; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1112 | bb->next = IRExpr_Binop(Iop_And64, IRExpr_RdTmp(old_LR), mkU64(~(3ULL))); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1113 | |
| 1114 | # elif defined(VGP_ppc32_aix5) |
| 1115 | Int offB_GPR2 = offsetof(VexGuestPPC32State,guest_GPR2); |
| 1116 | Int offB_LR = offsetof(VexGuestPPC32State,guest_LR); |
| 1117 | IRTemp old_R2 = newIRTemp( bb->tyenv, Ity_I32 ); |
| 1118 | IRTemp old_LR = newIRTemp( bb->tyenv, Ity_I32 ); |
| 1119 | /* Restore R2 */ |
| 1120 | old_R2 = gen_POP( bb ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1121 | addStmtToIRSB( bb, IRStmt_Put( offB_GPR2, IRExpr_RdTmp(old_R2)) ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1122 | /* Restore LR */ |
| 1123 | old_LR = gen_POP( bb ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1124 | addStmtToIRSB( bb, IRStmt_Put( offB_LR, IRExpr_RdTmp(old_LR)) ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1125 | |
| 1126 | /* Branch to LR */ |
| 1127 | /* re boring, we arrived here precisely because a wrapped fn did a |
| 1128 | blr (hence Ijk_Ret); so we should just mark this jump as Boring, |
| 1129 | else one _Call will have resulted in two _Rets. */ |
| 1130 | bb->jumpkind = Ijk_Boring; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1131 | bb->next = IRExpr_Binop(Iop_And32, IRExpr_RdTmp(old_LR), mkU32(~3)); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1132 | |
| 1133 | # else |
| 1134 | # error Platform is not TOC-afflicted, fortunately |
| 1135 | # endif |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1139 | Bool mk_preamble__ppctoc_magic_return_stub ( void* closureV, IRSB* bb ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1140 | { |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1141 | VgCallbackClosure* closure = (VgCallbackClosure*)closureV; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1142 | /* Since we're creating the entire IRSB right here, give it a |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1143 | proper IMark, as it won't get one any other way, and cachegrind |
| 1144 | will barf if it doesn't have one (fair enough really). */ |
sewardj | 80e88b0 | 2011-05-28 15:34:17 +0000 | [diff] [blame] | 1145 | addStmtToIRSB( bb, IRStmt_IMark( closure->readdr, 4, 0 ) ); |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1146 | /* Generate the magic sequence: |
| 1147 | pop R2 from hidden stack |
| 1148 | pop LR from hidden stack |
| 1149 | goto LR |
| 1150 | */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1151 | gen_pop_R2_LR_then_bLR(bb); |
| 1152 | return True; /* True == this is the entire BB; don't disassemble any |
| 1153 | real insns into it - just hand it directly to |
| 1154 | optimiser/instrumenter/backend. */ |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1158 | /* --------------- END helpers for with-TOC platforms --------------- */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1159 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* This is the IR preamble generator used for replacement |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1162 | functions. It adds code to set the guest_NRADDR{_GPR2} to zero |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1163 | (technically not necessary, but facilitates detecting mixups in |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1164 | which a replacement function has been erroneously declared using |
| 1165 | VG_REPLACE_FUNCTION_Z{U,Z} when instead it should have been written |
| 1166 | using VG_WRAP_FUNCTION_Z{U,Z}). |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1167 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1168 | On with-TOC platforms the follow hacks are also done: LR and R2 are |
| 1169 | pushed onto a hidden stack, R2 is set to the correct value for the |
| 1170 | replacement function, and LR is set to point at the magic |
| 1171 | return-stub address. Setting LR causes the return of the |
| 1172 | wrapped/redirected function to lead to our magic return stub, which |
| 1173 | restores LR and R2 from said stack and returns for real. |
sewardj | dfbaa22 | 2006-01-18 04:25:20 +0000 | [diff] [blame] | 1174 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 1175 | VG_(get_StackTrace_wrk) understands that the LR value may point to |
| 1176 | the return stub address, and that in that case it can get the real |
| 1177 | LR value from the hidden stack instead. */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1178 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1179 | Bool mk_preamble__set_NRADDR_to_zero ( void* closureV, IRSB* bb ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1180 | { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1181 | Int nraddr_szB |
| 1182 | = sizeof(((VexGuestArchState*)0)->guest_NRADDR); |
| 1183 | vg_assert(nraddr_szB == 4 || nraddr_szB == 8); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1184 | vg_assert(nraddr_szB == VG_WORDSIZE); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1185 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1186 | bb, |
| 1187 | IRStmt_Put( |
| 1188 | offsetof(VexGuestArchState,guest_NRADDR), |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1189 | nraddr_szB == 8 ? mkU64(0) : mkU32(0) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1190 | ) |
| 1191 | ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1192 | # if defined(VG_PLAT_USES_PPCTOC) |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1193 | { VgCallbackClosure* closure = (VgCallbackClosure*)closureV; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1194 | addStmtToIRSB( |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1195 | bb, |
| 1196 | IRStmt_Put( |
| 1197 | offsetof(VexGuestArchState,guest_NRADDR_GPR2), |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1198 | VG_WORDSIZE==8 ? mkU64(0) : mkU32(0) |
sewardj | 0283bae | 2006-01-22 01:15:36 +0000 | [diff] [blame] | 1199 | ) |
| 1200 | ); |
| 1201 | gen_push_and_set_LR_R2 ( bb, VG_(get_tocptr)( closure->readdr ) ); |
| 1202 | } |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1203 | # endif |
| 1204 | return False; |
| 1205 | } |
| 1206 | |
| 1207 | /* Ditto, except set guest_NRADDR to nraddr (the un-redirected guest |
| 1208 | address). This is needed for function wrapping - so the wrapper |
| 1209 | can read _NRADDR and find the address of the function being |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1210 | wrapped. On toc-afflicted platforms we must also snarf r2. */ |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1211 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1212 | Bool mk_preamble__set_NRADDR_to_nraddr ( void* closureV, IRSB* bb ) |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1213 | { |
| 1214 | VgCallbackClosure* closure = (VgCallbackClosure*)closureV; |
| 1215 | Int nraddr_szB |
| 1216 | = sizeof(((VexGuestArchState*)0)->guest_NRADDR); |
| 1217 | vg_assert(nraddr_szB == 4 || nraddr_szB == 8); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1218 | vg_assert(nraddr_szB == VG_WORDSIZE); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1219 | addStmtToIRSB( |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1220 | bb, |
| 1221 | IRStmt_Put( |
| 1222 | offsetof(VexGuestArchState,guest_NRADDR), |
| 1223 | nraddr_szB == 8 |
| 1224 | ? IRExpr_Const(IRConst_U64( closure->nraddr )) |
| 1225 | : IRExpr_Const(IRConst_U32( (UInt)closure->nraddr )) |
| 1226 | ) |
| 1227 | ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1228 | # if defined(VGP_ppc64_linux) || defined(VGP_ppc32_aix5) \ |
| 1229 | || defined(VGP_ppc64_aix5) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1230 | addStmtToIRSB( |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1231 | bb, |
| 1232 | IRStmt_Put( |
| 1233 | offsetof(VexGuestArchState,guest_NRADDR_GPR2), |
| 1234 | IRExpr_Get(offsetof(VexGuestArchState,guest_GPR2), |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1235 | VG_WORDSIZE==8 ? Ity_I64 : Ity_I32) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1236 | ) |
| 1237 | ); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1238 | gen_push_and_set_LR_R2 ( bb, VG_(get_tocptr)( closure->readdr ) ); |
| 1239 | # endif |
| 1240 | return False; |
| 1241 | } |
| 1242 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1243 | /* --- Helpers to do with PPC related stack redzones. --- */ |
| 1244 | |
| 1245 | __attribute__((unused)) |
| 1246 | static Bool const_True ( Addr64 guest_addr ) |
| 1247 | { |
| 1248 | return True; |
| 1249 | } |
| 1250 | |
| 1251 | __attribute__((unused)) |
| 1252 | static Bool bl_RZ_zap_ok_for_AIX ( Addr64 bl_target ) |
| 1253 | { |
| 1254 | /* paranoia */ |
| 1255 | if (sizeof(void*) == 4) |
| 1256 | bl_target &= 0xFFFFFFFFULL; |
| 1257 | |
| 1258 | /* don't zap the redzone for calls to millicode. */ |
| 1259 | if (bl_target < 0x10000ULL) |
| 1260 | return False; |
| 1261 | |
| 1262 | /* don't zap the redzone for calls to .$SAVEF14 .. .$SAVEF31. |
| 1263 | First we need to be reasonably sure we won't segfault by looking |
| 1264 | at the branch target. */ |
| 1265 | { NSegment const*const seg = VG_(am_find_nsegment)( (Addr)bl_target ); |
| 1266 | if (seg && seg->hasR) { |
| 1267 | switch ( *(UInt*)(Addr)bl_target ) { |
| 1268 | case 0xd9c1ff70: /* stfd f14,-144(r1) */ |
| 1269 | case 0xd9e1ff78: /* stfd f15,-136(r1) */ |
| 1270 | case 0xda01ff80: /* stfd f16,-128(r1) */ |
| 1271 | case 0xda21ff88: /* stfd f17,-120(r1) */ |
| 1272 | case 0xda41ff90: /* stfd f18,-112(r1) */ |
| 1273 | case 0xda61ff98: /* stfd f19,-104(r1) */ |
| 1274 | case 0xda81ffa0: /* stfd f20,-96(r1) */ |
| 1275 | case 0xdaa1ffa8: /* stfd f21,-88(r1) */ |
| 1276 | case 0xdac1ffb0: /* stfd f22,-80(r1) */ |
| 1277 | case 0xdae1ffb8: /* stfd f23,-72(r1) */ |
| 1278 | case 0xdb01ffc0: /* stfd f24,-64(r1) */ |
| 1279 | case 0xdb21ffc8: /* stfd f25,-56(r1) */ |
| 1280 | case 0xdb41ffd0: /* stfd f26,-48(r1) */ |
| 1281 | case 0xdb61ffd8: /* stfd f27,-40(r1) */ |
| 1282 | case 0xdb81ffe0: /* stfd f28,-32(r1) */ |
| 1283 | case 0xdba1ffe8: /* stfd f29,-24(r1) */ |
| 1284 | case 0xdbc1fff0: /* stfd f30,-16(r1) */ |
| 1285 | case 0xdbe1fff8: /* stfd f31,-8(r1) */ |
| 1286 | return False; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | return True; |
| 1291 | } |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1292 | |
| 1293 | /* --------------- main translation function --------------- */ |
| 1294 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1295 | /* Note: see comments at top of m_redir.c for the Big Picture on how |
| 1296 | redirections are managed. */ |
| 1297 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1298 | typedef |
| 1299 | enum { |
| 1300 | /* normal translation, redir neither requested nor inhibited */ |
| 1301 | T_Normal, |
| 1302 | /* redir translation, function-wrap (set _NRADDR) style */ |
| 1303 | T_Redir_Wrap, |
| 1304 | /* redir translation, replacement (don't set _NRADDR) style */ |
| 1305 | T_Redir_Replace, |
| 1306 | /* a translation in which redir is specifically disallowed */ |
| 1307 | T_NoRedir |
| 1308 | } |
| 1309 | T_Kind; |
| 1310 | |
| 1311 | /* Translate the basic block beginning at NRADDR, and add it to the |
| 1312 | translation cache & translation table. Unless |
| 1313 | DEBUGGING_TRANSLATION is true, in which case the call is being done |
| 1314 | for debugging purposes, so (a) throw away the translation once it |
| 1315 | is made, and (b) produce a load of debugging output. If |
| 1316 | ALLOW_REDIRECTION is False, do not attempt redirection of NRADDR, |
| 1317 | and also, put the resulting translation into the no-redirect tt/tc |
| 1318 | instead of the normal one. |
| 1319 | |
| 1320 | TID is the identity of the thread requesting this translation. |
| 1321 | */ |
| 1322 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1323 | Bool VG_(translate) ( ThreadId tid, |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1324 | Addr64 nraddr, |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1325 | Bool debugging_translation, |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 1326 | Int debugging_verbosity, |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1327 | ULong bbs_done, |
| 1328 | Bool allow_redirection ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1329 | { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1330 | Addr64 addr; |
| 1331 | T_Kind kind; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1332 | Int tmpbuf_used, verbosity, i; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1333 | Bool (*preamble_fn)(void*,IRSB*); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1334 | VexArch vex_arch; |
| 1335 | VexArchInfo vex_archinfo; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1336 | VexAbiInfo vex_abiinfo; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1337 | VexGuestExtents vge; |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1338 | VexTranslateArgs vta; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1339 | VexTranslateResult tres; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1340 | VgCallbackClosure closure; |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 1341 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1342 | /* Make sure Vex is initialised right. */ |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1343 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1344 | static Bool vex_init_done = False; |
| 1345 | |
| 1346 | if (!vex_init_done) { |
| 1347 | LibVEX_Init ( &failure_exit, &log_bytes, |
| 1348 | 1, /* debug_paranoia */ |
| 1349 | False, /* valgrind support */ |
| 1350 | &VG_(clo_vex_control) ); |
| 1351 | vex_init_done = True; |
| 1352 | } |
| 1353 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1354 | /* Establish the translation kind and actual guest address to |
| 1355 | start from. Sets (addr,kind). */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1356 | if (allow_redirection) { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1357 | Bool isWrap; |
| 1358 | Addr64 tmp = VG_(redir_do_lookup)( nraddr, &isWrap ); |
| 1359 | if (tmp == nraddr) { |
| 1360 | /* no redirection found */ |
| 1361 | addr = nraddr; |
| 1362 | kind = T_Normal; |
| 1363 | } else { |
| 1364 | /* found a redirect */ |
| 1365 | addr = tmp; |
| 1366 | kind = isWrap ? T_Redir_Wrap : T_Redir_Replace; |
| 1367 | } |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1368 | } else { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1369 | addr = nraddr; |
| 1370 | kind = T_NoRedir; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1371 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1372 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1373 | /* Established: (nraddr, addr, kind) */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1374 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1375 | /* Printing redirection info. */ |
| 1376 | |
| 1377 | if ((kind == T_Redir_Wrap || kind == T_Redir_Replace) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1378 | && (VG_(clo_verbosity) >= 2 || VG_(clo_trace_redir))) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1379 | Bool ok; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1380 | Char name1[64] = ""; |
| 1381 | Char name2[64] = ""; |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1382 | name1[0] = name2[0] = 0; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1383 | ok = VG_(get_fnname_w_offset)(nraddr, name1, 64); |
njn | be73f43 | 2005-03-26 21:34:45 +0000 | [diff] [blame] | 1384 | if (!ok) VG_(strcpy)(name1, "???"); |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1385 | ok = VG_(get_fnname_w_offset)(addr, name2, 64); |
njn | be73f43 | 2005-03-26 21:34:45 +0000 | [diff] [blame] | 1386 | if (!ok) VG_(strcpy)(name2, "???"); |
njn | 3f04d24 | 2005-03-20 18:21:14 +0000 | [diff] [blame] | 1387 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1388 | "REDIR: 0x%llx (%s) redirected to 0x%llx (%s)\n", |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1389 | nraddr, name1, |
| 1390 | addr, name2 ); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 1391 | } |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 1392 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1393 | if (!debugging_translation) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1394 | VG_TRACK( pre_mem_read, Vg_CoreTranslate, |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1395 | tid, "(translator)", addr, 1 ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1396 | |
sewardj | 85ac6d4 | 2005-02-23 11:36:56 +0000 | [diff] [blame] | 1397 | /* If doing any code printing, print a basic block start marker */ |
| 1398 | if (VG_(clo_trace_flags) || debugging_translation) { |
| 1399 | Char fnname[64] = ""; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1400 | VG_(get_fnname_w_offset)(addr, fnname, 64); |
sewardj | 85ac6d4 | 2005-02-23 11:36:56 +0000 | [diff] [blame] | 1401 | VG_(printf)( |
sewardj | e7d4607 | 2010-03-03 22:55:42 +0000 | [diff] [blame] | 1402 | "==== SB %d [tid %d] %s(0x%llx) SBs exec'd %lld ====\n", |
| 1403 | VG_(get_bbs_translated)(), (Int)tid, fnname, addr, |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 1404 | bbs_done); |
sewardj | 85ac6d4 | 2005-02-23 11:36:56 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1407 | /* Are we allowed to translate here? */ |
| 1408 | |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1409 | { /* BEGIN new scope specially for 'seg' */ |
| 1410 | NSegment const* seg = VG_(am_find_nsegment)(addr); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1411 | |
sewardj | 5f76de0 | 2007-02-11 05:08:06 +0000 | [diff] [blame] | 1412 | if ( (!translations_allowable_from_seg(seg)) |
| 1413 | || addr == TRANSTAB_BOGUS_GUEST_ADDR ) { |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1414 | if (VG_(clo_trace_signals)) |
sewardj | a594026 | 2007-09-10 16:28:38 +0000 | [diff] [blame] | 1415 | VG_(message)(Vg_DebugMsg, "translations not allowed here (0x%llx)" |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1416 | " - throwing SEGV\n", addr); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1417 | /* U R busted, sonny. Place your hands on your head and step |
| 1418 | away from the orig_addr. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1419 | /* Code address is bad - deliver a signal instead */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1420 | if (seg != NULL) { |
| 1421 | /* There's some kind of segment at the requested place, but we |
| 1422 | aren't allowed to execute code here. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1423 | if (debugging_translation) |
| 1424 | VG_(printf)("translations not allowed here (segment not executable)" |
| 1425 | "(0x%llx)\n", addr); |
| 1426 | else |
| 1427 | VG_(synth_fault_perms)(tid, addr); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1428 | } else { |
| 1429 | /* There is no segment at all; we are attempting to execute in |
| 1430 | the middle of nowhere. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1431 | if (debugging_translation) |
| 1432 | VG_(printf)("translations not allowed here (no segment)" |
| 1433 | "(0x%llx)\n", addr); |
| 1434 | else |
| 1435 | VG_(synth_fault_mapping)(tid, addr); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1436 | } |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 1437 | return False; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1438 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1439 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1440 | /* True if a debug trans., or if bit N set in VG_(clo_trace_codegen). */ |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 1441 | verbosity = 0; |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 1442 | if (debugging_translation) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1443 | verbosity = debugging_verbosity; |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 1444 | } |
| 1445 | else |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1446 | if ( (VG_(clo_trace_flags) > 0 |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 1447 | && VG_(get_bbs_translated)() >= VG_(clo_trace_notbelow) )) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1448 | verbosity = VG_(clo_trace_flags); |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 1449 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1450 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1451 | /* Figure out which preamble-mangling callback to send. */ |
| 1452 | preamble_fn = NULL; |
| 1453 | if (kind == T_Redir_Replace) |
| 1454 | preamble_fn = mk_preamble__set_NRADDR_to_zero; |
| 1455 | else |
| 1456 | if (kind == T_Redir_Wrap) |
| 1457 | preamble_fn = mk_preamble__set_NRADDR_to_nraddr; |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1458 | |
| 1459 | # if defined(VG_PLAT_USES_PPCTOC) |
| 1460 | if (ULong_to_Ptr(nraddr) |
| 1461 | == (void*)&VG_(ppctoc_magic_redirect_return_stub)) { |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1462 | /* If entering the special return stub, this means a wrapped or |
| 1463 | redirected function is returning. Make this translation one |
| 1464 | which restores R2 and LR from the thread's hidden redir |
| 1465 | stack, and branch to the (restored) link register, thereby |
| 1466 | really causing the function to return. */ |
| 1467 | vg_assert(kind == T_Normal); |
| 1468 | vg_assert(nraddr == addr); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1469 | preamble_fn = mk_preamble__ppctoc_magic_return_stub; |
| 1470 | } |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1471 | # endif |
| 1472 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1473 | /* ------ Actually do the translation. ------ */ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1474 | tl_assert2(VG_(tdict).tool_instrument, |
| 1475 | "you forgot to set VgToolInterface function 'tool_instrument'"); |
sewardj | 7be5509 | 2005-08-01 23:25:55 +0000 | [diff] [blame] | 1476 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1477 | /* Get the CPU info established at startup. */ |
| 1478 | VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo ); |
| 1479 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1480 | /* Set up 'abiinfo' structure with stuff Vex needs to know about |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1481 | the guest and host ABIs. */ |
| 1482 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1483 | LibVEX_default_VexAbiInfo( &vex_abiinfo ); |
| 1484 | vex_abiinfo.guest_stack_redzone_size = VG_STACK_REDZONE_SZB; |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1485 | |
sewardj | c0b2039 | 2008-12-04 00:07:30 +0000 | [diff] [blame] | 1486 | # if defined(VGP_amd64_linux) |
| 1487 | vex_abiinfo.guest_amd64_assume_fs_is_zero = True; |
| 1488 | # endif |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1489 | # if defined(VGP_amd64_darwin) |
| 1490 | vex_abiinfo.guest_amd64_assume_gs_is_0x60 = True; |
| 1491 | # endif |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1492 | # if defined(VGP_ppc32_linux) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1493 | vex_abiinfo.guest_ppc_zap_RZ_at_blr = False; |
| 1494 | vex_abiinfo.guest_ppc_zap_RZ_at_bl = NULL; |
| 1495 | vex_abiinfo.host_ppc32_regalign_int64_args = True; |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1496 | # endif |
| 1497 | # if defined(VGP_ppc64_linux) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1498 | vex_abiinfo.guest_ppc_zap_RZ_at_blr = True; |
| 1499 | vex_abiinfo.guest_ppc_zap_RZ_at_bl = const_True; |
| 1500 | vex_abiinfo.host_ppc_calls_use_fndescrs = True; |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1501 | # endif |
| 1502 | # if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1503 | vex_abiinfo.guest_ppc_zap_RZ_at_blr = False; |
| 1504 | vex_abiinfo.guest_ppc_zap_RZ_at_bl = bl_RZ_zap_ok_for_AIX; |
| 1505 | vex_abiinfo.guest_ppc_sc_continues_at_LR = True; |
| 1506 | vex_abiinfo.host_ppc_calls_use_fndescrs = True; |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1507 | # endif |
| 1508 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1509 | /* Set up closure args. */ |
| 1510 | closure.tid = tid; |
| 1511 | closure.nraddr = nraddr; |
| 1512 | closure.readdr = addr; |
sewardj | 7be5509 | 2005-08-01 23:25:55 +0000 | [diff] [blame] | 1513 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1514 | /* Set up args for LibVEX_Translate. */ |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1515 | vta.arch_guest = vex_arch; |
| 1516 | vta.archinfo_guest = vex_archinfo; |
| 1517 | vta.arch_host = vex_arch; |
| 1518 | vta.archinfo_host = vex_archinfo; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1519 | vta.abiinfo_both = vex_abiinfo; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1520 | vta.guest_bytes = (UChar*)ULong_to_Ptr(addr); |
| 1521 | vta.guest_bytes_addr = (Addr64)addr; |
| 1522 | vta.callback_opaque = (void*)&closure; |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1523 | vta.chase_into_ok = chase_into_ok; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1524 | vta.preamble_function = preamble_fn; |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1525 | vta.guest_extents = &vge; |
| 1526 | vta.host_bytes = tmpbuf; |
| 1527 | vta.host_bytes_size = N_TMPBUF; |
| 1528 | vta.host_bytes_used = &tmpbuf_used; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1529 | { /* At this point we have to reconcile Vex's view of the |
| 1530 | instrumentation callback - which takes a void* first argument |
| 1531 | - with Valgrind's view, in which the first arg is a |
| 1532 | VgCallbackClosure*. Hence the following longwinded casts. |
| 1533 | They are entirely legal but longwinded so as to maximise the |
| 1534 | chance of the C typechecker picking up any type snafus. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1535 | IRSB*(*f)(VgCallbackClosure*, |
| 1536 | IRSB*,VexGuestLayout*,VexGuestExtents*, |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1537 | IRType,IRType) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1538 | = VG_(clo_vgdb) != Vg_VgdbNo |
| 1539 | ? tool_instrument_then_gdbserver_if_needed |
| 1540 | : VG_(tdict).tool_instrument; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1541 | IRSB*(*g)(void*, |
| 1542 | IRSB*,VexGuestLayout*,VexGuestExtents*, |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1543 | IRType,IRType) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1544 | = (IRSB*(*)(void*,IRSB*,VexGuestLayout*,VexGuestExtents*,IRType,IRType))f; |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1545 | vta.instrument1 = g; |
| 1546 | } |
| 1547 | /* No need for type kludgery here. */ |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1548 | vta.instrument2 = need_to_handle_SP_assignment() |
| 1549 | ? vg_SP_update_pass |
| 1550 | : NULL; |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 1551 | vta.finaltidy = VG_(needs).final_IR_tidy_pass |
| 1552 | ? VG_(tdict).tool_final_IR_tidy_pass |
| 1553 | : NULL; |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 1554 | vta.needs_self_check = needs_self_check; |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1555 | vta.traceflags = verbosity; |
| 1556 | |
| 1557 | /* Set up the dispatch-return info. For archs without a link |
| 1558 | register, vex generates a jump back to the specified dispatch |
| 1559 | address. Else, it just generates a branch-to-LR. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1560 | |
sewardj | 1274027 | 2011-05-29 09:34:30 +0000 | [diff] [blame] | 1561 | # if defined(VGA_x86) || defined(VGA_amd64) |
| 1562 | if (!allow_redirection) { |
| 1563 | /* It's a no-redir translation. Will be run with the |
| 1564 | nonstandard dispatcher VG_(run_a_noredir_translation) and so |
| 1565 | needs a nonstandard return point. */ |
| 1566 | vta.dispatch_assisted |
| 1567 | = (void*) &VG_(run_a_noredir_translation__return_point); |
| 1568 | vta.dispatch_unassisted |
| 1569 | = vta.dispatch_assisted; |
| 1570 | } |
| 1571 | else |
| 1572 | if (VG_(clo_profile_flags) > 0) { |
| 1573 | /* normal translation; although we're profiling. */ |
| 1574 | vta.dispatch_assisted |
| 1575 | = (void*) &VG_(run_innerloop__dispatch_assisted_profiled); |
| 1576 | vta.dispatch_unassisted |
| 1577 | = (void*) &VG_(run_innerloop__dispatch_unassisted_profiled); |
| 1578 | } |
| 1579 | else { |
| 1580 | /* normal translation and we're not profiling (the normal case) */ |
| 1581 | vta.dispatch_assisted |
| 1582 | = (void*) &VG_(run_innerloop__dispatch_assisted_unprofiled); |
| 1583 | vta.dispatch_unassisted |
| 1584 | = (void*) &VG_(run_innerloop__dispatch_unassisted_unprofiled); |
| 1585 | } |
| 1586 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1587 | # elif defined(VGA_ppc32) || defined(VGA_ppc64) \ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1588 | || defined(VGA_arm) || defined(VGA_s390x) |
sewardj | 52805ff | 2011-05-30 06:29:39 +0000 | [diff] [blame] | 1589 | /* See comment in libvex.h. This target uses a |
| 1590 | return-to-link-register scheme to get back to the dispatcher, so |
| 1591 | both fields are NULL. */ |
| 1592 | vta.dispatch_assisted = NULL; |
| 1593 | vta.dispatch_unassisted = NULL; |
sewardj | 1274027 | 2011-05-29 09:34:30 +0000 | [diff] [blame] | 1594 | |
sewardj | 274807d | 2005-12-15 14:07:07 +0000 | [diff] [blame] | 1595 | # else |
| 1596 | # error "Unknown arch" |
| 1597 | # endif |
| 1598 | |
| 1599 | /* Sheesh. Finally, actually _do_ the translation! */ |
| 1600 | tres = LibVEX_Translate ( &vta ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1601 | |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 1602 | vg_assert(tres.status == VexTransOK); |
| 1603 | vg_assert(tres.n_sc_extents >= 0 && tres.n_sc_extents <= 3); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1604 | vg_assert(tmpbuf_used <= N_TMPBUF); |
| 1605 | vg_assert(tmpbuf_used > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1606 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1607 | /* Tell aspacem of all segments that have had translations taken |
| 1608 | from them. Optimisation: don't re-look up vge.base[0] since seg |
| 1609 | should already point to it. */ |
| 1610 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1611 | vg_assert( vge.base[0] == (Addr64)addr ); |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1612 | /* set 'translations taken from this segment' flag */ |
| 1613 | VG_(am_set_segment_hasT_if_SkFileC_or_SkAnonC)( (NSegment*)seg ); |
| 1614 | } /* END new scope specially for 'seg' */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1615 | |
| 1616 | for (i = 1; i < vge.n_used; i++) { |
sewardj | 4a0009c | 2006-10-17 01:52:05 +0000 | [diff] [blame] | 1617 | NSegment const* seg |
| 1618 | = VG_(am_find_nsegment)( vge.base[i] ); |
| 1619 | /* set 'translations taken from this segment' flag */ |
| 1620 | VG_(am_set_segment_hasT_if_SkFileC_or_SkAnonC)( (NSegment*)seg ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 1623 | /* Copy data at trans_addr into the translation cache. */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1624 | vg_assert(tmpbuf_used > 0 && tmpbuf_used < 65536); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 1625 | |
| 1626 | // If debugging, don't do anything with the translated block; we |
| 1627 | // only did this for the debugging output produced along the way. |
| 1628 | if (!debugging_translation) { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1629 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1630 | if (kind != T_NoRedir) { |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1631 | // Put it into the normal TT/TC structures. This is the |
| 1632 | // normal case. |
| 1633 | |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1634 | // Note that we use nraddr (the non-redirected address), not |
| 1635 | // addr, which might have been changed by the redirection |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1636 | VG_(add_to_transtab)( &vge, |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1637 | nraddr, |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1638 | (Addr)(&tmpbuf[0]), |
| 1639 | tmpbuf_used, |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 1640 | tres.n_sc_extents > 0 ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1641 | } else { |
| 1642 | VG_(add_to_unredir_transtab)( &vge, |
sewardj | 34593e5 | 2006-01-17 01:57:33 +0000 | [diff] [blame] | 1643 | nraddr, |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1644 | (Addr)(&tmpbuf[0]), |
njn | 1dcee09 | 2009-02-24 03:07:37 +0000 | [diff] [blame] | 1645 | tmpbuf_used ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1646 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1647 | } |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 1648 | |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 1649 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | /*--------------------------------------------------------------------*/ |
njn | 3cbfbc1 | 2005-05-13 23:11:40 +0000 | [diff] [blame] | 1653 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1654 | /*--------------------------------------------------------------------*/ |