sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- The core dispatch loop, for jumping to a code address. ---*/ |
| 4 | /*--- dispatch-s390x-linux.S ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Valgrind, a dynamic binary instrumentation |
| 9 | framework. |
| 10 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 11 | Copyright IBM Corp. 2010-2017 |
| 12 | Copyright (C) 2011-2017, Florian Krohm (britzel@acm.org) |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 13 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 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 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
| 32 | /* Contributed by Florian Krohm and Christian Borntraeger */ |
| 33 | |
| 34 | #include "pub_core_basics_asm.h" |
| 35 | #include "pub_core_dispatch_asm.h" |
| 36 | #include "pub_core_transtab_asm.h" |
| 37 | #include "libvex_guest_offsets.h" |
| 38 | #include "libvex_s390x_common.h" |
| 39 | |
| 40 | #if defined(VGA_s390x) |
| 41 | |
| 42 | /*------------------------------------------------------------*/ |
| 43 | /*--- ---*/ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 44 | /*--- The dispatch loop. VG_(disp_run_translations) is ---*/ |
| 45 | /*--- used to run all translations, ---*/ |
| 46 | /*--- including no-redir ones. ---*/ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 47 | /*--- ---*/ |
| 48 | /*------------------------------------------------------------*/ |
| 49 | |
| 50 | /* Convenience definitions for readability */ |
| 51 | #undef SP |
| 52 | #define SP S390_REGNO_STACK_POINTER |
| 53 | |
| 54 | #undef LR |
| 55 | #define LR S390_REGNO_LINK_REGISTER |
| 56 | |
| 57 | /* Location of valgrind's saved FPC register */ |
| 58 | #define S390_LOC_SAVED_FPC_V S390_OFFSET_SAVED_FPC_V(SP) |
| 59 | |
florian | ae24058 | 2011-09-25 00:15:54 +0000 | [diff] [blame] | 60 | /* Location of saved R2 register */ |
| 61 | #define S390_LOC_SAVED_R2 S390_OFFSET_SAVED_R2(SP) |
| 62 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 63 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 64 | /*----------------------------------------------------*/ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 65 | /*--- Entry and preamble (set everything up) ---*/ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 66 | /*----------------------------------------------------*/ |
| 67 | |
| 68 | /* signature: |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 69 | void VG_(disp_run_translations)( UWord* two_words, |
| 70 | void* guest_state, |
| 71 | Addr host_addr ); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 72 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 73 | Return results are placed in two_words: |
| 74 | |
| 75 | two_words[0] is set to the TRC |
| 76 | two_words[1] is set to the address to patch (in case two_words[0] is |
| 77 | VG_TRC_CHAIN_ME_TO_{SLOW,FAST}_EP). Otherwise, it is 0. |
| 78 | */ |
| 79 | .text |
| 80 | .align 4 |
| 81 | .globl VG_(disp_run_translations) |
| 82 | .type VG_(disp_run_translations), @function |
| 83 | VG_(disp_run_translations): |
| 84 | |
| 85 | /* r2 holds two_words */ |
| 86 | /* r3 holds pointer to guest_state */ |
| 87 | /* r4 holds host_addr, i.e. the address of the translation to run */ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 88 | |
| 89 | /* Save gprs ABI: r6...r13 and r15 */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 90 | stmg %r6,%r15,48(SP) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 91 | |
| 92 | /* New stack frame */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 93 | aghi SP,-S390_INNERLOOP_FRAME_SIZE |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 94 | |
| 95 | /* Save fprs: ABI: f8...f15 */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 96 | std %f8,160+0(SP) |
| 97 | std %f9,160+8(SP) |
| 98 | std %f10,160+16(SP) |
| 99 | std %f11,160+24(SP) |
| 100 | std %f12,160+32(SP) |
| 101 | std %f13,160+40(SP) |
| 102 | std %f14,160+48(SP) |
| 103 | std %f15,160+56(SP) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 104 | |
| 105 | /* Load address of guest state into guest state register (r13) */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 106 | lgr %r13,%r3 |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 107 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 108 | /* Save R2 on stack. In postamble it will be restored such that the |
| 109 | return values can be written */ |
| 110 | stg %r2,S390_LOC_SAVED_R2 |
| 111 | |
| 112 | /* Save valgrind's FPC on stack so postamble can restore |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 113 | it later . */ |
| 114 | stfpc S390_LOC_SAVED_FPC_V |
| 115 | |
| 116 | /* Load the FPC the way the client code wants it. I.e. pull the |
florian | 43acc07 | 2012-01-02 16:12:30 +0000 | [diff] [blame] | 117 | value from the guest state. */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 118 | lfpc OFFSET_s390x_fpc(%r13) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 119 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 120 | /* Jump into the code cache. Chained translations in |
| 121 | the code cache run, until for whatever reason, they can't |
| 122 | continue. When that happens, the translation in question |
| 123 | will jump (or call) to one of the continuation points |
| 124 | VG_(cp_...) below. */ |
| 125 | br %r4 |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 126 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 127 | |
| 128 | /*----------------------------------------------------*/ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 129 | /*--- Postamble and return to C code. ---*/ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 130 | /*----------------------------------------------------*/ |
| 131 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 132 | postamble: |
| 133 | /* At this point, %r0 and %r1 contain two |
| 134 | words to be returned to the caller. %r0 |
| 135 | holds a TRC value, and %r1 optionally may |
| 136 | hold another word (for CHAIN_ME exits, the |
| 137 | address of the place to patch.) */ |
florian | ae24058 | 2011-09-25 00:15:54 +0000 | [diff] [blame] | 138 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 139 | /* We're leaving. AMD has some code here to check invariants. |
| 140 | We don't have (need) that, as we save and restore the FPC register |
| 141 | whenever we switch between valgrind proper to client code. */ |
| 142 | |
| 143 | /* Restore valgrind's FPC, as client code may have changed it. */ |
| 144 | lfpc S390_LOC_SAVED_FPC_V |
| 145 | |
| 146 | /* Restore %r2 from stack; holds address of two_words */ |
| 147 | lg %r2,S390_LOC_SAVED_R2 |
| 148 | |
| 149 | stg %r0,0(%r2) /* Store %r0 to two_words[0] */ |
| 150 | stg %r1,8(%r2) /* Store %r1 to two_words[1] */ |
florian | ae24058 | 2011-09-25 00:15:54 +0000 | [diff] [blame] | 151 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 152 | /* Restore callee-saved registers... */ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 153 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 154 | /* Floating-point regs */ |
| 155 | ld %f8,160+0(SP) |
| 156 | ld %f9,160+8(SP) |
| 157 | ld %f10,160+16(SP) |
| 158 | ld %f11,160+24(SP) |
| 159 | ld %f12,160+32(SP) |
| 160 | ld %f13,160+40(SP) |
| 161 | ld %f14,160+48(SP) |
| 162 | ld %f15,160+56(SP) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 163 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 164 | /* Remove stack frame */ |
| 165 | aghi SP,S390_INNERLOOP_FRAME_SIZE |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 166 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 167 | /* General-purpose regs. This also restores the original link |
| 168 | register (r14) and stack pointer (r15). */ |
| 169 | lmg %r6,%r15,48(SP) |
| 170 | |
| 171 | /* Return */ |
| 172 | br LR |
| 173 | |
| 174 | |
| 175 | /*----------------------------------------------------*/ |
| 176 | /*--- Continuation points ---*/ |
| 177 | /*----------------------------------------------------*/ |
| 178 | |
| 179 | /* ------ Chain me to slow entry point ------ */ |
| 180 | .global VG_(disp_cp_chain_me_to_slowEP) |
| 181 | VG_(disp_cp_chain_me_to_slowEP): |
florian | 4b50415 | 2012-04-22 17:39:37 +0000 | [diff] [blame] | 182 | /* When we come here %r1 contains the address of the place to patch. |
| 183 | The return values (TRC, address-to-patch) are stored here in |
| 184 | %r0 and %r1, respectively */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 185 | lghi %r0,VG_TRC_CHAIN_ME_TO_SLOW_EP |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 186 | j postamble |
| 187 | |
| 188 | |
| 189 | /* ------ Chain me to fast entry point ------ */ |
| 190 | .global VG_(disp_cp_chain_me_to_fastEP) |
| 191 | VG_(disp_cp_chain_me_to_fastEP): |
| 192 | /* Identical to VG_(disp_cp_chain_me_to_slowEP), except value of %r0. */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 193 | lghi %r0,VG_TRC_CHAIN_ME_TO_FAST_EP |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 194 | j postamble |
| 195 | |
| 196 | |
| 197 | /* ------ Indirect but boring jump ------ */ |
| 198 | .global VG_(disp_cp_xindir) |
| 199 | VG_(disp_cp_xindir): |
| 200 | /* Where are we going? */ |
| 201 | lg %r2, OFFSET_s390x_IA(%r13) |
| 202 | |
florian | 6e0015a | 2012-04-22 15:37:15 +0000 | [diff] [blame] | 203 | /* Increment VG_(stats__n_xindirs_32) */ |
| 204 | larl %r8, VG_(stats__n_xindirs_32) |
| 205 | l %r10,0(%r8) |
| 206 | ahi %r10,1 |
| 207 | st %r10,0(%r8) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 208 | |
| 209 | /* Try a fast lookup in the translation cache: |
| 210 | Compute offset (not index) into VT_(tt_fast): |
| 211 | |
| 212 | offset = VG_TT_FAST_HASH(addr) * sizeof(FastCacheEntry) |
| 213 | |
| 214 | with VG_TT_FAST_HASH(addr) == (addr >> 1) & VG_TT_FAST_MASK |
| 215 | and sizeof(FastCacheEntry) == 16 |
| 216 | |
| 217 | offset = ((addr >> 1) & VG_TT_FAST_MASK) << 4 |
florian | 21f8bcf | 2011-09-17 15:46:59 +0000 | [diff] [blame] | 218 | which is |
| 219 | offset = ((addr & (VG_TT_FAST_MASK << 1) ) << 3 |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 220 | */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 221 | larl %r8, VG_(tt_fast) |
| 222 | llill %r5,(VG_TT_FAST_MASK << 1) & 0xffff |
florian | 21f8bcf | 2011-09-17 15:46:59 +0000 | [diff] [blame] | 223 | #if ((( VG_TT_FAST_MASK << 1) & 0xffff0000) >> 16 != 0) |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 224 | iilh %r5,((VG_TT_FAST_MASK << 1) & 0xffff0000) >> 16 |
florian | 21f8bcf | 2011-09-17 15:46:59 +0000 | [diff] [blame] | 225 | #endif |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 226 | ngr %r5,%r2 |
| 227 | sllg %r7,%r5,3 |
| 228 | lg %r11, 8(%r8,%r7) /* .host */ |
| 229 | cg %r2, 0(%r8,%r7) /* next guest address == .guest ? */ |
| 230 | jne fast_lookup_failed |
| 231 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 232 | /* Found a match. Call .host. |
| 233 | r11 is an address. There we will find the instrumented client code. |
florian | 6e0015a | 2012-04-22 15:37:15 +0000 | [diff] [blame] | 234 | That code may modify the guest state register r13. */ |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 235 | br %r11 |
| 236 | .long 0x0 /* persuade insn decoders not to speculate past here */ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 237 | |
| 238 | fast_lookup_failed: |
florian | 6e0015a | 2012-04-22 15:37:15 +0000 | [diff] [blame] | 239 | /* Increment VG_(stats__n_xindir_misses_32) */ |
| 240 | larl %r8, VG_(stats__n_xindir_misses_32) |
| 241 | l %r10,0(%r8) |
| 242 | ahi %r10,1 |
| 243 | st %r10,0(%r8) |
| 244 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 245 | lghi %r0,VG_TRC_INNER_FASTMISS |
| 246 | lghi %r1,0 |
| 247 | j postamble |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 248 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 249 | |
| 250 | /* ------ Assisted jump ------ */ |
| 251 | .global VG_(disp_cp_xassisted) |
| 252 | VG_(disp_cp_xassisted): |
| 253 | /* guest-state-pointer contains the TRC. Put the value into the |
| 254 | return register */ |
| 255 | lgr %r0,%r13 |
| 256 | lghi %r1,0 |
| 257 | j postamble |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 258 | |
| 259 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 260 | /* ------ Event check failed ------ */ |
| 261 | .global VG_(disp_cp_evcheck_fail) |
| 262 | VG_(disp_cp_evcheck_fail): |
| 263 | lghi %r0,VG_TRC_INNER_COUNTERZERO |
| 264 | lghi %r1,0 |
| 265 | j postamble |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 266 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 267 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 268 | .size VG_(disp_run_translations), .-VG_(disp_run_translations) |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 269 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 270 | #endif /* VGA_s390x */ |
| 271 | |
florian | 3f1d613 | 2015-09-30 20:30:48 +0000 | [diff] [blame] | 272 | /* Let the linker know we don't need an executable stack */ |
| 273 | MARK_STACK_NO_EXEC |
| 274 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 275 | /*--------------------------------------------------------------------*/ |
| 276 | /*--- end dispatch-s390x-linux.S ---*/ |
| 277 | /*--------------------------------------------------------------------*/ |