sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- The JITter proper: register allocation & code improvement ---*/ |
| 4 | /*--- vg_translate.c ---*/ |
| 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 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2005 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 32 | #include "core.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 33 | #include "pub_core_aspacemgr.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 34 | #include "pub_core_tooliface.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 35 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 36 | |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 37 | /*------------------------------------------------------------*/ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 38 | /*--- %SP-update pass ---*/ |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 39 | /*------------------------------------------------------------*/ |
| 40 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 41 | /* NOTE: this comment is out of date */ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 42 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 43 | /* For tools that want to know about %ESP changes, this pass adds |
| 44 | in the appropriate hooks. We have to do it after the tool's |
| 45 | instrumentation, so the tool doesn't have to worry about the CCALLs |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 46 | it adds in, and we must do it before register allocation because |
| 47 | spilled temps make it much harder to work out the %esp deltas. |
njn | ed61971 | 2003-10-01 16:45:04 +0000 | [diff] [blame] | 48 | Thus we have it as an extra phase between the two. |
| 49 | |
| 50 | We look for "GETL %ESP, t_ESP", then track ADDs and SUBs of |
| 51 | literal values to t_ESP, and the total delta of the ADDs/SUBs. Then if |
| 52 | "PUTL t_ESP, %ESP" happens, we call the helper with the known delta. We |
| 53 | also cope with "MOVL t_ESP, tX", making tX the new t_ESP. If any other |
| 54 | instruction clobbers t_ESP, we don't track it anymore, and fall back to |
| 55 | the delta-is-unknown case. That case is also used when the delta is not |
| 56 | a nice small amount, or an unknown amount. |
| 57 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 58 | |
| 59 | static |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 60 | IRBB* vg_SP_update_pass ( IRBB* bb_in, VexGuestLayout* layout, |
njn | 8786e53 | 2005-03-24 04:35:11 +0000 | [diff] [blame] | 61 | IRType gWordTy, IRType hWordTy ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 62 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 63 | Int i, j, minoff_ST, maxoff_ST, sizeof_SP, offset_SP; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 64 | IRDirty *dcall, *d; |
| 65 | IRStmt* st; |
| 66 | IRExpr* e; |
| 67 | IRArray* descr; |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 68 | IRTemp curr; |
| 69 | IRType typeof_SP; |
| 70 | Long delta; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 71 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 72 | /* Set up BB */ |
| 73 | IRBB* bb = emptyIRBB(); |
| 74 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 75 | bb->next = dopyIRExpr(bb_in->next); |
| 76 | bb->jumpkind = bb_in->jumpkind; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 77 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 78 | curr = IRTemp_INVALID; |
| 79 | delta = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 80 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 81 | sizeof_SP = layout->sizeof_SP; |
| 82 | offset_SP = layout->offset_SP; |
| 83 | typeof_SP = sizeof_SP==4 ? Ity_I32 : Ity_I64; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 84 | vg_assert(sizeof_SP == 4 || sizeof_SP == 8); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 85 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 86 | # define IS_ADD(op) (sizeof_SP==4 ? ((op)==Iop_Add32) : ((op)==Iop_Add64)) |
| 87 | # define IS_SUB(op) (sizeof_SP==4 ? ((op)==Iop_Sub32) : ((op)==Iop_Sub64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 88 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 89 | # define IS_ADD_OR_SUB(op) (IS_ADD(op) || IS_SUB(op)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 90 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 91 | # define GET_CONST(con) \ |
| 92 | (sizeof_SP==4 ? (Long)(Int)(con->Ico.U32) \ |
| 93 | : (Long)(con->Ico.U64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 94 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 95 | # define DO(kind, syze) \ |
| 96 | do { \ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 97 | if (!VG_(tdict).track_##kind##_mem_stack_##syze) \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 98 | goto generic; \ |
| 99 | \ |
| 100 | /* I don't know if it's really necessary to say that the */ \ |
| 101 | /* call reads the stack pointer. But anyway, we do. */ \ |
| 102 | dcall = unsafeIRDirty_0_N( \ |
| 103 | 1/*regparms*/, \ |
| 104 | "track_" #kind "_mem_stack_" #syze, \ |
njn | cf81d55 | 2005-03-31 04:52:26 +0000 | [diff] [blame] | 105 | VG_(tdict).track_##kind##_mem_stack_##syze, \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 106 | mkIRExprVec_1(IRExpr_Tmp(curr)) \ |
| 107 | ); \ |
| 108 | dcall->nFxState = 1; \ |
| 109 | dcall->fxState[0].fx = Ifx_Read; \ |
| 110 | dcall->fxState[0].offset = layout->offset_SP; \ |
| 111 | dcall->fxState[0].size = layout->sizeof_SP; \ |
| 112 | \ |
| 113 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); \ |
| 114 | } while (0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 115 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 116 | for (i = 0; i < bb_in->stmts_used; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 117 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 118 | st = bb_in->stmts[i]; |
| 119 | if (!st) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 120 | continue; |
| 121 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 122 | /* t = Get(sp): curr = t, delta = 0 */ |
| 123 | if (st->tag != Ist_Tmp) goto case2; |
| 124 | e = st->Ist.Tmp.data; |
| 125 | if (e->tag != Iex_Get) goto case2; |
| 126 | if (e->Iex.Get.offset != offset_SP) goto case2; |
| 127 | if (e->Iex.Get.ty != typeof_SP) goto case2; |
| 128 | curr = st->Ist.Tmp.tmp; |
| 129 | delta = 0; |
| 130 | addStmtToIRBB( bb, st ); |
| 131 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 132 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 133 | case2: |
| 134 | /* t' = curr +/- const: curr = t', delta +=/-= const */ |
| 135 | if (st->tag != Ist_Tmp) goto case3; |
| 136 | e = st->Ist.Tmp.data; |
| 137 | if (e->tag != Iex_Binop) goto case3; |
| 138 | if (e->Iex.Binop.arg1->tag != Iex_Tmp) goto case3; |
| 139 | if (e->Iex.Binop.arg1->Iex.Tmp.tmp != curr) goto case3; |
| 140 | if (e->Iex.Binop.arg2->tag != Iex_Const) goto case3; |
| 141 | if (!IS_ADD_OR_SUB(e->Iex.Binop.op)) goto case3; |
| 142 | curr = st->Ist.Tmp.tmp; |
| 143 | if (IS_ADD(e->Iex.Binop.op)) |
| 144 | delta += GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
| 145 | else |
| 146 | delta -= GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
| 147 | addStmtToIRBB( bb, st ); |
| 148 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 149 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 150 | case3: |
| 151 | /* t' = curr: curr = t' */ |
| 152 | if (st->tag != Ist_Tmp) goto case4; |
| 153 | e = st->Ist.Tmp.data; |
| 154 | if (e->tag != Iex_Tmp) goto case4; |
| 155 | if (e->Iex.Tmp.tmp != curr) goto case4; |
| 156 | curr = st->Ist.Tmp.tmp; |
| 157 | addStmtToIRBB( bb, st ); |
| 158 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 159 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 160 | case4: |
| 161 | /* Put(sp) = curr */ |
| 162 | if (st->tag != Ist_Put) goto case5; |
| 163 | if (st->Ist.Put.offset != offset_SP) goto case5; |
| 164 | if (st->Ist.Put.data->tag != Iex_Tmp) goto case5; |
| 165 | if (st->Ist.Put.data->Iex.Tmp.tmp == curr) { |
| 166 | switch (delta) { |
| 167 | case 0: addStmtToIRBB(bb,st); delta = 0; continue; |
| 168 | case 4: DO(die, 4); addStmtToIRBB(bb,st); delta = 0; continue; |
| 169 | case -4: DO(new, 4); addStmtToIRBB(bb,st); delta = 0; continue; |
| 170 | case 8: DO(die, 8); addStmtToIRBB(bb,st); delta = 0; continue; |
| 171 | case -8: DO(new, 8); addStmtToIRBB(bb,st); delta = 0; continue; |
| 172 | case 12: DO(die, 12); addStmtToIRBB(bb,st); delta = 0; continue; |
| 173 | case -12: DO(new, 12); addStmtToIRBB(bb,st); delta = 0; continue; |
| 174 | case 16: DO(die, 16); addStmtToIRBB(bb,st); delta = 0; continue; |
| 175 | case -16: DO(new, 16); addStmtToIRBB(bb,st); delta = 0; continue; |
| 176 | case 32: DO(die, 32); addStmtToIRBB(bb,st); delta = 0; continue; |
| 177 | case -32: DO(new, 32); addStmtToIRBB(bb,st); delta = 0; continue; |
| 178 | default: goto generic; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 179 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 180 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 181 | IRTemp old_SP; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 182 | generic: |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 183 | /* Pass both the old and new SP values to this helper. */ |
| 184 | old_SP = newIRTemp(bb->tyenv, typeof_SP); |
| 185 | addStmtToIRBB( |
| 186 | bb, |
| 187 | IRStmt_Tmp( old_SP, IRExpr_Get(offset_SP, typeof_SP) ) |
| 188 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 189 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 190 | dcall = unsafeIRDirty_0_N( |
| 191 | 2/*regparms*/, |
| 192 | "VG_(unknown_SP_update)", &VG_(unknown_SP_update), |
| 193 | mkIRExprVec_2( IRExpr_Tmp(old_SP), st->Ist.Put.data ) |
| 194 | ); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 195 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 196 | |
| 197 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 198 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 199 | curr = st->Ist.Put.data->Iex.Tmp.tmp; |
| 200 | delta = 0; |
| 201 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 202 | } |
| 203 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 204 | case5: |
| 205 | /* PutI or Dirty call which overlaps SP: complain. We can't |
| 206 | deal with SP changing in weird ways (well, we can, but not at |
| 207 | this time of night). */ |
| 208 | if (st->tag == Ist_PutI) { |
| 209 | descr = st->Ist.PutI.descr; |
| 210 | minoff_ST = descr->base; |
| 211 | maxoff_ST = descr->base + descr->nElems * sizeofIRType(descr->elemTy) - 1; |
| 212 | if (!(offset_SP > maxoff_ST || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
| 213 | goto complain; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 214 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 215 | if (st->tag == Ist_Dirty) { |
| 216 | d = st->Ist.Dirty.details; |
| 217 | for (j = 0; j < d->nFxState; j++) { |
| 218 | minoff_ST = d->fxState[j].offset; |
| 219 | maxoff_ST = d->fxState[j].offset + d->fxState[j].size - 1; |
| 220 | if (d->fxState[j].fx == Ifx_Read || d->fxState[j].fx == Ifx_None) |
| 221 | continue; |
| 222 | if (!(offset_SP > maxoff_ST || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
| 223 | goto complain; |
| 224 | } |
| 225 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 226 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 227 | /* well, not interesting. Just copy and keep going. */ |
| 228 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 229 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 230 | } /* for (i = 0; i < bb_in->stmts_used; i++) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 231 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 232 | return bb; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 233 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 234 | complain: |
| 235 | VG_(core_panic)("vg_SP_update_pass: PutI or Dirty which overlaps SP"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 236 | |
| 237 | } |
sewardj | 7c4b604 | 2003-06-14 15:47:15 +0000 | [diff] [blame] | 238 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 239 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 240 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 241 | #if 0 |
| 242 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 243 | st = bb_in->stmts[i]; |
| 244 | if (!st) |
| 245 | continue; |
| 246 | if (st->tag != Ist_Put) |
| 247 | goto boring; |
| 248 | offP = st->Ist.Put.offset; |
| 249 | if (offP != layout->offset_SP) |
| 250 | goto boring; |
| 251 | szP = sizeofIRType(typeOfIRExpr(bb_in->tyenv, st->Ist.Put.data)); |
| 252 | if (szP != layout->sizeof_SP) |
| 253 | goto boring; |
| 254 | vg_assert(isAtom(st->Ist.Put.data)); |
sewardj | 97ced73 | 2002-03-25 00:07:36 +0000 | [diff] [blame] | 255 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 256 | /* I don't know if it's really necessary to say that the call reads |
| 257 | the stack pointer. But anyway, we do. */ |
| 258 | dcall = unsafeIRDirty_0_N( |
| 259 | mkIRCallee(1, "VG_(unknown_esp_update)", |
| 260 | (HWord)&VG_(unknown_esp_update)), |
| 261 | mkIRExprVec_1(st->Ist.Put.data) |
| 262 | ); |
| 263 | dcall->nFxState = 1; |
| 264 | dcall->fxState[0].fx = Ifx_Read; |
| 265 | dcall->fxState[0].offset = layout->offset_SP; |
| 266 | dcall->fxState[0].size = layout->sizeof_SP; |
sewardj | 97ced73 | 2002-03-25 00:07:36 +0000 | [diff] [blame] | 267 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 268 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); |
| 269 | |
| 270 | boring: |
| 271 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 272 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 273 | #endif |
| 274 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 275 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 276 | /*------------------------------------------------------------*/ |
| 277 | /*--- Main entry point for the JITter. ---*/ |
| 278 | /*------------------------------------------------------------*/ |
| 279 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 280 | /* Vex dumps the final code in here. Then we can copy it off |
| 281 | wherever we like. */ |
sewardj | fb154a5 | 2005-03-31 15:48:57 +0000 | [diff] [blame] | 282 | #define N_TMPBUF 20000 |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 283 | static UChar tmpbuf[N_TMPBUF]; |
| 284 | |
| 285 | /* Function pointers we must supply to LibVEX in order that it |
| 286 | can bomb out and emit messages under Valgrind's control. */ |
| 287 | __attribute__ ((noreturn)) |
| 288 | static |
| 289 | void failure_exit ( void ) |
| 290 | { |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 291 | LibVEX_ShowAllocStats(); |
sewardj | c621f76 | 2004-11-26 13:49:59 +0000 | [diff] [blame] | 292 | VG_(core_panic)("LibVEX called failure_exit()."); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 296 | void log_bytes ( HChar* bytes, Int nbytes ) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 297 | { |
| 298 | Int i; |
| 299 | for (i = 0; i < nbytes-3; i += 4) |
| 300 | VG_(printf)("%c%c%c%c", bytes[i], bytes[i+1], bytes[i+2], bytes[i+3]); |
| 301 | for (; i < nbytes; i++) |
| 302 | VG_(printf)("%c", bytes[i]); |
| 303 | } |
| 304 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 305 | /* Translate the basic block beginning at orig_addr, and add it to |
| 306 | the translation cache & translation table. Unless 'debugging' is true, |
| 307 | in which case the call is being done for debugging purposes, so |
| 308 | (a) throw away the translation once it is made, and (b) produce a |
| 309 | load of debugging output. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 310 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 311 | 'tid' is the identity of the thread needing this block. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 312 | */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 313 | |
| 314 | /* This stops Vex from chasing into function entry points that we wish |
| 315 | to redirect. Chasing across them obviously defeats the redirect |
| 316 | mechanism, with bad effects for Memcheck, Addrcheck, and possibly |
| 317 | others. */ |
| 318 | static Bool chase_into_ok ( Addr64 addr64 ) |
| 319 | { |
| 320 | Addr addr = (Addr)addr64; |
| 321 | if (addr != VG_(code_redirect)(addr)) { |
| 322 | if (0) VG_(printf)("not chasing into 0x%x\n", addr); |
| 323 | return False; |
| 324 | } else { |
| 325 | return True; /* ok to chase into 'addr' */ |
| 326 | } |
| 327 | } |
| 328 | |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 329 | static Bool need_to_handle_SP_assignment(void) |
| 330 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 331 | return ( VG_(tdict).track_new_mem_stack_4 || |
| 332 | VG_(tdict).track_die_mem_stack_4 || |
| 333 | VG_(tdict).track_new_mem_stack_8 || |
| 334 | VG_(tdict).track_die_mem_stack_8 || |
| 335 | VG_(tdict).track_new_mem_stack_12 || |
| 336 | VG_(tdict).track_die_mem_stack_12 || |
| 337 | VG_(tdict).track_new_mem_stack_16 || |
| 338 | VG_(tdict).track_die_mem_stack_16 || |
| 339 | VG_(tdict).track_new_mem_stack_32 || |
| 340 | VG_(tdict).track_die_mem_stack_32 || |
| 341 | VG_(tdict).track_new_mem_stack || |
| 342 | VG_(tdict).track_die_mem_stack ); |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 343 | } |
| 344 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 345 | Bool VG_(translate) ( ThreadId tid, |
| 346 | Addr64 orig_addr, |
| 347 | Bool debugging_translation, |
| 348 | Int debugging_verbosity ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 349 | { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 350 | Addr64 redir, orig_addr0 = orig_addr; |
| 351 | Int tmpbuf_used, verbosity; |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 352 | Bool notrace_until_done; |
| 353 | UInt notrace_until_limit = 0; |
| 354 | Segment* seg; |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 355 | VexGuestExtents vge; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 356 | |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 357 | /* Indicates what arch and subarch we are running on. */ |
| 358 | static VexArch vex_arch = VexArch_INVALID; |
| 359 | static VexSubArch vex_subarch = VexSubArch_INVALID; |
| 360 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 361 | /* Make sure Vex is initialised right. */ |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 362 | VexTranslateResult tres; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 363 | static Bool vex_init_done = False; |
| 364 | |
| 365 | if (!vex_init_done) { |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 366 | Bool ok = VGA_(getArchAndSubArch)( &vex_arch, &vex_subarch ); |
| 367 | if (!ok) { |
| 368 | VG_(printf)("\n"); |
| 369 | VG_(printf)("valgrind: fatal error: unsupported CPU.\n"); |
| 370 | VG_(printf)(" Supported CPUs are:\n"); |
| 371 | VG_(printf)(" * x86 with SSE state (Pentium II or above, " |
| 372 | "AMD Athlon or above)\n"); |
| 373 | VG_(printf)("\n"); |
| 374 | VG_(exit)(1); |
| 375 | } |
| 376 | if (VG_(clo_verbosity) > 2) { |
| 377 | VG_(message)(Vg_DebugMsg, |
| 378 | "Host CPU: arch = %s, subarch = %s", |
| 379 | LibVEX_ppVexArch ( vex_arch ), |
| 380 | LibVEX_ppVexSubArch( vex_subarch ) ); |
| 381 | } |
| 382 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 383 | LibVEX_Init ( &failure_exit, &log_bytes, |
| 384 | 1, /* debug_paranoia */ |
| 385 | False, /* valgrind support */ |
| 386 | &VG_(clo_vex_control) ); |
| 387 | vex_init_done = True; |
| 388 | } |
| 389 | |
| 390 | /* profiling ... */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 391 | VGP_PUSHCC(VgpTranslate); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 392 | |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 393 | /* Look in the code redirect table to see if we should |
| 394 | translate an alternative address for orig_addr. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 395 | redir = VG_(code_redirect)(orig_addr); |
| 396 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 397 | if (redir != orig_addr && VG_(clo_verbosity) >= 2) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 398 | Bool ok; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 399 | Char name1[64] = ""; |
| 400 | Char name2[64] = ""; |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 401 | name1[0] = name2[0] = 0; |
| 402 | ok = VG_(get_fnname_w_offset)(orig_addr, name1, 64); |
njn | be73f43 | 2005-03-26 21:34:45 +0000 | [diff] [blame] | 403 | if (!ok) VG_(strcpy)(name1, "???"); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 404 | ok = VG_(get_fnname_w_offset)(redir, name2, 64); |
njn | be73f43 | 2005-03-26 21:34:45 +0000 | [diff] [blame] | 405 | if (!ok) VG_(strcpy)(name2, "???"); |
njn | 3f04d24 | 2005-03-20 18:21:14 +0000 | [diff] [blame] | 406 | VG_(message)(Vg_DebugMsg, |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 407 | "TRANSLATE: 0x%llx (%s) redirected to 0x%llx (%s)", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 408 | orig_addr, name1, |
| 409 | redir, name2 ); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 410 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 411 | orig_addr = redir; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 412 | |
sewardj | a60be0e | 2003-05-26 08:47:27 +0000 | [diff] [blame] | 413 | /* If codegen tracing, don't start tracing until |
| 414 | notrace_until_limit blocks have gone by. This avoids printing |
| 415 | huge amounts of useless junk when all we want to see is the last |
| 416 | few blocks translated prior to a failure. Set |
| 417 | notrace_until_limit to be the number of translations to be made |
| 418 | before --trace-codegen= style printing takes effect. */ |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 419 | notrace_until_done |
| 420 | = VG_(get_bbs_translated)() >= notrace_until_limit; |
sewardj | a60be0e | 2003-05-26 08:47:27 +0000 | [diff] [blame] | 421 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 422 | seg = VG_(find_segment)(orig_addr); |
| 423 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 424 | if (!debugging_translation) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 425 | VG_TRACK( pre_mem_read, Vg_CoreTranslate, tid, "", orig_addr, 1 ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 426 | |
sewardj | 85ac6d4 | 2005-02-23 11:36:56 +0000 | [diff] [blame] | 427 | /* If doing any code printing, print a basic block start marker */ |
| 428 | if (VG_(clo_trace_flags) || debugging_translation) { |
| 429 | Char fnname[64] = ""; |
| 430 | VG_(get_fnname_w_offset)(orig_addr, fnname, 64); |
| 431 | VG_(printf)( |
| 432 | "==== BB %d %s(0x%llx) approx BBs exec'd %lld ====\n", |
| 433 | VG_(get_bbs_translated)(), fnname, orig_addr, |
| 434 | VG_(bbs_done)); |
| 435 | } |
| 436 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 437 | if (seg == NULL || |
| 438 | !VG_(seg_contains)(seg, orig_addr, 1) || |
| 439 | (seg->prot & (VKI_PROT_READ|VKI_PROT_EXEC)) == 0) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 440 | /* Code address is bad - deliver a signal instead */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 441 | vg_assert(!VG_(is_addressable)(orig_addr, 1, |
| 442 | VKI_PROT_READ|VKI_PROT_EXEC)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 443 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 444 | if (seg != NULL && VG_(seg_contains)(seg, orig_addr, 1)) { |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 445 | vg_assert((seg->prot & VKI_PROT_EXEC) == 0); |
| 446 | VG_(synth_fault_perms)(tid, orig_addr); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 447 | } else |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 448 | VG_(synth_fault_mapping)(tid, orig_addr); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 449 | |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 450 | return False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 451 | } else |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 452 | seg->flags |= SF_CODE; /* contains cached code */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 453 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 454 | /* 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] | 455 | verbosity = 0; |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 456 | if (debugging_translation) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 457 | verbosity = debugging_verbosity; |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 458 | } |
| 459 | else |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 460 | if ( (VG_(clo_trace_flags) > 0 |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 461 | && VG_(get_bbs_translated)() >= VG_(clo_trace_notbelow) )) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 462 | verbosity = VG_(clo_trace_flags); |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 463 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 464 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 465 | /* Actually do the translation. */ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 466 | tl_assert2(VG_(tdict).tool_instrument, |
| 467 | "you forgot to set VgToolInterface function 'tool_instrument'"); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 468 | tres = LibVEX_Translate ( |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 469 | vex_arch, vex_subarch, |
| 470 | vex_arch, vex_subarch, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 471 | (UChar*)ULong_to_Ptr(orig_addr), |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 472 | (Addr64)orig_addr, |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 473 | chase_into_ok, |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 474 | &vge, |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 475 | tmpbuf, N_TMPBUF, &tmpbuf_used, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 476 | VG_(tdict).tool_instrument, |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 477 | need_to_handle_SP_assignment() |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 478 | ? vg_SP_update_pass |
| 479 | : NULL, |
| 480 | True, /* cleanup after instrumentation */ |
| 481 | NULL, |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 482 | verbosity |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 483 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 484 | |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 485 | vg_assert(tres == VexTransOK); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 486 | vg_assert(tmpbuf_used <= N_TMPBUF); |
| 487 | vg_assert(tmpbuf_used > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 488 | |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 489 | #undef DECIDE_IF_PRINTING_CODEGEN |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 490 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 491 | /* Copy data at trans_addr into the translation cache. */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 492 | vg_assert(tmpbuf_used > 0 && tmpbuf_used < 65536); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 493 | |
| 494 | // If debugging, don't do anything with the translated block; we |
| 495 | // only did this for the debugging output produced along the way. |
| 496 | if (!debugging_translation) { |
| 497 | // Note that we use orig_addr0, not orig_addr, which might have been |
| 498 | // changed by the redirection |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 499 | VG_(add_to_trans_tab)( &vge, |
| 500 | orig_addr0, |
| 501 | (Addr)(&tmpbuf[0]), |
| 502 | tmpbuf_used ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 503 | } |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 504 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 505 | VGP_POPCC(VgpTranslate); |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 506 | |
| 507 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /*--------------------------------------------------------------------*/ |
| 511 | /*--- end vg_translate.c ---*/ |
| 512 | /*--------------------------------------------------------------------*/ |
njn | ed61971 | 2003-10-01 16:45:04 +0000 | [diff] [blame] | 513 | |