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