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