sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 3 | /*--- begin libvex_guest_amd64.h ---*/ |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 4 | /*---------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 9 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 10 | Copyright (C) 2004-2017 OpenWorks LLP |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 11 | info@open-works.net |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 12 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 17 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
sewardj | 7bd6ffe | 2005-08-03 16:07:36 +0000 | [diff] [blame] | 26 | 02110-1301, USA. |
| 27 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 29 | |
| 30 | Neither the names of the U.S. Department of Energy nor the |
| 31 | University of California nor the names of its contributors may be |
| 32 | used to endorse or promote products derived from this software |
| 33 | without prior written permission. |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #ifndef __LIBVEX_PUB_GUEST_AMD64_H |
| 37 | #define __LIBVEX_PUB_GUEST_AMD64_H |
| 38 | |
| 39 | #include "libvex_basictypes.h" |
sewardj | 3e5d82d | 2015-07-21 14:43:23 +0000 | [diff] [blame] | 40 | #include "libvex_emnote.h" |
njn | d93ee4c | 2004-11-30 11:37:48 +0000 | [diff] [blame] | 41 | |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 42 | |
| 43 | /*---------------------------------------------------------------*/ |
| 44 | /*--- Vex's representation of the AMD64 CPU state. ---*/ |
| 45 | /*---------------------------------------------------------------*/ |
| 46 | |
sewardj | cb6091d | 2005-02-21 08:23:39 +0000 | [diff] [blame] | 47 | /* See detailed comments at the top of libvex_guest_x86.h for |
sewardj | 8d96531 | 2005-02-25 02:48:47 +0000 | [diff] [blame] | 48 | further info. This representation closely follows the |
sewardj | cb6091d | 2005-02-21 08:23:39 +0000 | [diff] [blame] | 49 | x86 representation. |
sewardj | cb6091d | 2005-02-21 08:23:39 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 53 | typedef |
| 54 | struct { |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 55 | /* Event check fail addr, counter, and padding to make RAX 16 |
| 56 | aligned. */ |
| 57 | /* 0 */ ULong host_EvC_FAILADDR; |
| 58 | /* 8 */ UInt host_EvC_COUNTER; |
| 59 | /* 12 */ UInt pad0; |
| 60 | /* 16 */ ULong guest_RAX; |
| 61 | /* 24 */ ULong guest_RCX; |
| 62 | /* 32 */ ULong guest_RDX; |
| 63 | /* 40 */ ULong guest_RBX; |
| 64 | /* 48 */ ULong guest_RSP; |
| 65 | /* 56 */ ULong guest_RBP; |
| 66 | /* 64 */ ULong guest_RSI; |
| 67 | /* 72 */ ULong guest_RDI; |
| 68 | /* 80 */ ULong guest_R8; |
| 69 | /* 88 */ ULong guest_R9; |
| 70 | /* 96 */ ULong guest_R10; |
| 71 | /* 104 */ ULong guest_R11; |
| 72 | /* 112 */ ULong guest_R12; |
| 73 | /* 120 */ ULong guest_R13; |
| 74 | /* 128 */ ULong guest_R14; |
| 75 | /* 136 */ ULong guest_R15; |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 76 | /* 4-word thunk used to calculate O S Z A C P flags. */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 77 | /* 144 */ ULong guest_CC_OP; |
| 78 | /* 152 */ ULong guest_CC_DEP1; |
| 79 | /* 160 */ ULong guest_CC_DEP2; |
| 80 | /* 168 */ ULong guest_CC_NDEP; |
sewardj | d0a12df | 2005-02-10 02:07:43 +0000 | [diff] [blame] | 81 | /* The D flag is stored here, encoded as either -1 or +1 */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 82 | /* 176 */ ULong guest_DFLAG; |
| 83 | /* 184 */ ULong guest_RIP; |
sewardj | 5e120aa | 2010-09-28 15:59:04 +0000 | [diff] [blame] | 84 | /* Bit 18 (AC) of eflags stored here, as either 0 or 1. */ |
| 85 | /* ... */ ULong guest_ACFLAG; |
| 86 | /* Bit 21 (ID) of eflags stored here, as either 0 or 1. */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 87 | /* 192 */ ULong guest_IDFLAG; |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 88 | /* Probably a lot more stuff too. |
njn | 95f7386 | 2004-11-28 16:05:46 +0000 | [diff] [blame] | 89 | D,ID flags |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 90 | 16 128-bit SSE registers |
| 91 | all the old x87 FPU gunk |
sewardj | 5e120aa | 2010-09-28 15:59:04 +0000 | [diff] [blame] | 92 | segment registers */ |
sewardj | 85520e4 | 2005-02-19 15:22:38 +0000 | [diff] [blame] | 93 | |
sewardj | 3e5d82d | 2015-07-21 14:43:23 +0000 | [diff] [blame] | 94 | /* HACK to e.g. make tls on amd64-linux/solaris work. %fs only ever seems |
| 95 | to hold a constant value (zero on linux main thread, 0x63 in other |
philippe | e2cc4de | 2014-12-16 23:57:51 +0000 | [diff] [blame] | 96 | threads), and so guest_FS_CONST holds |
| 97 | the 64-bit offset associated with this constant %fs value. */ |
| 98 | /* 200 */ ULong guest_FS_CONST; |
sewardj | a6b93d1 | 2005-02-17 09:28:28 +0000 | [diff] [blame] | 99 | |
sewardj | c4530ae | 2012-05-21 10:18:49 +0000 | [diff] [blame] | 100 | /* YMM registers. Note that these must be allocated |
sewardj | 0b2d3fe | 2010-08-06 07:59:38 +0000 | [diff] [blame] | 101 | consecutively in order that the SSE4.2 PCMP{E,I}STR{I,M} |
sewardj | c4530ae | 2012-05-21 10:18:49 +0000 | [diff] [blame] | 102 | helpers can treat them as an array. YMM16 is a fake reg used |
sewardj | 0b2d3fe | 2010-08-06 07:59:38 +0000 | [diff] [blame] | 103 | as an intermediary in handling aforementioned insns. */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 104 | /* 208 */ULong guest_SSEROUND; |
sewardj | c4530ae | 2012-05-21 10:18:49 +0000 | [diff] [blame] | 105 | /* 216 */U256 guest_YMM0; |
| 106 | U256 guest_YMM1; |
| 107 | U256 guest_YMM2; |
| 108 | U256 guest_YMM3; |
| 109 | U256 guest_YMM4; |
| 110 | U256 guest_YMM5; |
| 111 | U256 guest_YMM6; |
| 112 | U256 guest_YMM7; |
| 113 | U256 guest_YMM8; |
| 114 | U256 guest_YMM9; |
| 115 | U256 guest_YMM10; |
| 116 | U256 guest_YMM11; |
| 117 | U256 guest_YMM12; |
| 118 | U256 guest_YMM13; |
| 119 | U256 guest_YMM14; |
| 120 | U256 guest_YMM15; |
| 121 | U256 guest_YMM16; |
sewardj | cb6091d | 2005-02-21 08:23:39 +0000 | [diff] [blame] | 122 | |
sewardj | 8d96531 | 2005-02-25 02:48:47 +0000 | [diff] [blame] | 123 | /* FPU */ |
| 124 | /* Note. Setting guest_FTOP to be ULong messes up the |
| 125 | delicately-balanced PutI/GetI optimisation machinery. |
| 126 | Therefore best to leave it as a UInt. */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 127 | UInt guest_FTOP; |
philippe | 7e5aa0d | 2015-04-11 14:32:53 +0000 | [diff] [blame] | 128 | UInt pad1; |
sewardj | 8d96531 | 2005-02-25 02:48:47 +0000 | [diff] [blame] | 129 | ULong guest_FPREG[8]; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 130 | UChar guest_FPTAG[8]; |
| 131 | ULong guest_FPROUND; |
| 132 | ULong guest_FC3210; |
sewardj | 8d96531 | 2005-02-25 02:48:47 +0000 | [diff] [blame] | 133 | |
florian | 6ef84be | 2012-08-26 03:20:07 +0000 | [diff] [blame] | 134 | /* Emulation notes */ |
| 135 | UInt guest_EMNOTE; |
philippe | 7e5aa0d | 2015-04-11 14:32:53 +0000 | [diff] [blame] | 136 | UInt pad2; |
sewardj | 1f126c5 | 2005-03-16 13:57:58 +0000 | [diff] [blame] | 137 | |
| 138 | /* Translation-invalidation area description. Not used on amd64 |
| 139 | (there is no invalidate-icache insn), but needed so as to |
| 140 | allow users of the library to uniformly assume that the guest |
| 141 | state contains these two fields -- otherwise there is |
| 142 | compilation breakage. On amd64, these two fields are set to |
| 143 | zero by LibVEX_GuestAMD64_initialise and then should be |
| 144 | ignored forever thereafter. */ |
sewardj | 05f5e01 | 2014-05-04 10:52:11 +0000 | [diff] [blame] | 145 | ULong guest_CMSTART; |
| 146 | ULong guest_CMLEN; |
sewardj | 1f126c5 | 2005-03-16 13:57:58 +0000 | [diff] [blame] | 147 | |
sewardj | ce02aa7 | 2006-01-12 12:27:58 +0000 | [diff] [blame] | 148 | /* Used to record the unredirected guest address at the start of |
| 149 | a translation whose start has been redirected. By reading |
| 150 | this pseudo-register shortly afterwards, the translation can |
| 151 | find out what the corresponding no-redirection address was. |
| 152 | Note, this is only set for wrap-style redirects, not for |
| 153 | replace-style ones. */ |
| 154 | ULong guest_NRADDR; |
| 155 | |
sewardj | d660d41 | 2008-12-03 21:29:59 +0000 | [diff] [blame] | 156 | /* Used for Darwin syscall dispatching. */ |
| 157 | ULong guest_SC_CLASS; |
| 158 | |
philippe | e2cc4de | 2014-12-16 23:57:51 +0000 | [diff] [blame] | 159 | /* HACK to make e.g. tls on darwin work, wine on linux work, ... |
| 160 | %gs only ever seems to hold a constant value (e.g. 0x60 on darwin, |
| 161 | 0x6b on linux), and so guest_GS_CONST holds the 64-bit offset |
| 162 | associated with this constant %gs value. (A direct analogue |
sewardj | 3e5d82d | 2015-07-21 14:43:23 +0000 | [diff] [blame] | 163 | of the %fs-const hack for amd64-linux/solaris). */ |
philippe | e2cc4de | 2014-12-16 23:57:51 +0000 | [diff] [blame] | 164 | ULong guest_GS_CONST; |
sewardj | d660d41 | 2008-12-03 21:29:59 +0000 | [diff] [blame] | 165 | |
sewardj | e86310f | 2009-03-19 22:21:40 +0000 | [diff] [blame] | 166 | /* Needed for Darwin (but mandated for all guest architectures): |
| 167 | RIP at the last syscall insn (int 0x80/81/82, sysenter, |
| 168 | syscall). Used when backing up to restart a syscall that has |
| 169 | been interrupted by a signal. */ |
| 170 | ULong guest_IP_AT_SYSCALL; |
| 171 | |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 172 | /* Padding to make it have an 16-aligned size */ |
philippe | 7e5aa0d | 2015-04-11 14:32:53 +0000 | [diff] [blame] | 173 | ULong pad3; |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 174 | } |
| 175 | VexGuestAMD64State; |
| 176 | |
| 177 | |
| 178 | |
| 179 | /*---------------------------------------------------------------*/ |
| 180 | /*--- Utility functions for amd64 guest stuff. ---*/ |
| 181 | /*---------------------------------------------------------------*/ |
| 182 | |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 183 | /* ALL THE FOLLOWING ARE VISIBLE TO LIBRARY CLIENT */ |
| 184 | |
sewardj | 65526c2 | 2005-01-20 19:43:56 +0000 | [diff] [blame] | 185 | /* Initialise all guest amd64 state. The FPU is put in default |
| 186 | mode. */ |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 187 | extern |
njn | 9c6acb0 | 2004-11-30 15:56:47 +0000 | [diff] [blame] | 188 | void LibVEX_GuestAMD64_initialise ( /*OUT*/VexGuestAMD64State* vex_state ); |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 189 | |
| 190 | |
sewardj | 7de0d3c | 2005-02-13 02:26:41 +0000 | [diff] [blame] | 191 | /* Extract from the supplied VexGuestAMD64State structure the |
| 192 | corresponding native %rflags value. */ |
| 193 | extern |
florian | efa834a | 2012-11-24 21:07:14 +0000 | [diff] [blame] | 194 | ULong LibVEX_GuestAMD64_get_rflags ( /*IN*/const VexGuestAMD64State* vex_state ); |
sewardj | 7de0d3c | 2005-02-13 02:26:41 +0000 | [diff] [blame] | 195 | |
sewardj | 3e5d82d | 2015-07-21 14:43:23 +0000 | [diff] [blame] | 196 | /* Put rflags into the given state. */ |
| 197 | extern |
| 198 | void LibVEX_GuestAMD64_put_rflags ( ULong rflags, |
| 199 | /*MOD*/VexGuestAMD64State* vex_state ); |
| 200 | |
sewardj | d660d41 | 2008-12-03 21:29:59 +0000 | [diff] [blame] | 201 | /* Set the carry flag in the given state to 'new_carry_flag', which |
| 202 | should be zero or one. */ |
| 203 | extern |
| 204 | void |
| 205 | LibVEX_GuestAMD64_put_rflag_c ( ULong new_carry_flag, |
| 206 | /*MOD*/VexGuestAMD64State* vex_state ); |
| 207 | |
sewardj | 3e5d82d | 2015-07-21 14:43:23 +0000 | [diff] [blame] | 208 | /* Do FXSAVE from the supplied VexGuestAMD64tate structure and store the |
| 209 | result at the given address which represents a buffer of at least 416 |
| 210 | bytes. */ |
| 211 | extern |
| 212 | void LibVEX_GuestAMD64_fxsave ( /*IN*/VexGuestAMD64State* gst, |
| 213 | /*OUT*/HWord fp_state ); |
| 214 | |
| 215 | /* Do FXRSTOR from the supplied address and store read values to the given |
| 216 | VexGuestAMD64State structure. */ |
| 217 | extern |
| 218 | VexEmNote LibVEX_GuestAMD64_fxrstor ( /*IN*/HWord fp_state, |
| 219 | /*MOD*/VexGuestAMD64State* gst ); |
sewardj | 7de0d3c | 2005-02-13 02:26:41 +0000 | [diff] [blame] | 220 | |
sewardj | 9a7b75d | 2004-11-23 14:07:46 +0000 | [diff] [blame] | 221 | #endif /* ndef __LIBVEX_PUB_GUEST_AMD64_H */ |
| 222 | |
| 223 | /*---------------------------------------------------------------*/ |
| 224 | /*--- libvex_guest_amd64.h ---*/ |
| 225 | /*---------------------------------------------------------------*/ |