blob: 7db7cbc35c28e304d17a85c54f011e9ea268742a [file] [log] [blame]
sewardj9a7b75d2004-11-23 14:07:46 +00001
2/*---------------------------------------------------------------*/
sewardj752f9062010-05-03 21:38:49 +00003/*--- begin libvex_guest_amd64.h ---*/
sewardj9a7b75d2004-11-23 14:07:46 +00004/*---------------------------------------------------------------*/
5
6/*
sewardj752f9062010-05-03 21:38:49 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardj9a7b75d2004-11-23 14:07:46 +00009
Elliott Hughesed398002017-06-21 14:41:24 -070010 Copyright (C) 2004-2017 OpenWorks LLP
sewardj752f9062010-05-03 21:38:49 +000011 info@open-works.net
sewardj9a7b75d2004-11-23 14:07:46 +000012
sewardj752f9062010-05-03 21:38:49 +000013 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.
sewardj9a7b75d2004-11-23 14:07:46 +000017
sewardj752f9062010-05-03 21:38:49 +000018 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
sewardj7bd6ffe2005-08-03 16:07:36 +000026 02110-1301, USA.
27
sewardj752f9062010-05-03 21:38:49 +000028 The GNU General Public License is contained in the file COPYING.
sewardj9a7b75d2004-11-23 14:07:46 +000029
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.
sewardj9a7b75d2004-11-23 14:07:46 +000034*/
35
36#ifndef __LIBVEX_PUB_GUEST_AMD64_H
37#define __LIBVEX_PUB_GUEST_AMD64_H
38
39#include "libvex_basictypes.h"
sewardj3e5d82d2015-07-21 14:43:23 +000040#include "libvex_emnote.h"
njnd93ee4c2004-11-30 11:37:48 +000041
sewardj9a7b75d2004-11-23 14:07:46 +000042
43/*---------------------------------------------------------------*/
44/*--- Vex's representation of the AMD64 CPU state. ---*/
45/*---------------------------------------------------------------*/
46
sewardjcb6091d2005-02-21 08:23:39 +000047/* See detailed comments at the top of libvex_guest_x86.h for
sewardj8d965312005-02-25 02:48:47 +000048 further info. This representation closely follows the
sewardjcb6091d2005-02-21 08:23:39 +000049 x86 representation.
sewardjcb6091d2005-02-21 08:23:39 +000050*/
51
52
sewardj9a7b75d2004-11-23 14:07:46 +000053typedef
54 struct {
sewardjc6f970f2012-04-02 21:54:49 +000055 /* 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;
sewardj9a7b75d2004-11-23 14:07:46 +000076 /* 4-word thunk used to calculate O S Z A C P flags. */
sewardjc6f970f2012-04-02 21:54:49 +000077 /* 144 */ ULong guest_CC_OP;
78 /* 152 */ ULong guest_CC_DEP1;
79 /* 160 */ ULong guest_CC_DEP2;
80 /* 168 */ ULong guest_CC_NDEP;
sewardjd0a12df2005-02-10 02:07:43 +000081 /* The D flag is stored here, encoded as either -1 or +1 */
sewardjc6f970f2012-04-02 21:54:49 +000082 /* 176 */ ULong guest_DFLAG;
83 /* 184 */ ULong guest_RIP;
sewardj5e120aa2010-09-28 15:59:04 +000084 /* 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. */
sewardjc6f970f2012-04-02 21:54:49 +000087 /* 192 */ ULong guest_IDFLAG;
sewardj9a7b75d2004-11-23 14:07:46 +000088 /* Probably a lot more stuff too.
njn95f73862004-11-28 16:05:46 +000089 D,ID flags
sewardj9a7b75d2004-11-23 14:07:46 +000090 16 128-bit SSE registers
91 all the old x87 FPU gunk
sewardj5e120aa2010-09-28 15:59:04 +000092 segment registers */
sewardj85520e42005-02-19 15:22:38 +000093
sewardj3e5d82d2015-07-21 14:43:23 +000094 /* 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
philippee2cc4de2014-12-16 23:57:51 +000096 threads), and so guest_FS_CONST holds
97 the 64-bit offset associated with this constant %fs value. */
98 /* 200 */ ULong guest_FS_CONST;
sewardja6b93d12005-02-17 09:28:28 +000099
sewardjc4530ae2012-05-21 10:18:49 +0000100 /* YMM registers. Note that these must be allocated
sewardj0b2d3fe2010-08-06 07:59:38 +0000101 consecutively in order that the SSE4.2 PCMP{E,I}STR{I,M}
sewardjc4530ae2012-05-21 10:18:49 +0000102 helpers can treat them as an array. YMM16 is a fake reg used
sewardj0b2d3fe2010-08-06 07:59:38 +0000103 as an intermediary in handling aforementioned insns. */
sewardjc6f970f2012-04-02 21:54:49 +0000104 /* 208 */ULong guest_SSEROUND;
sewardjc4530ae2012-05-21 10:18:49 +0000105 /* 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;
sewardjcb6091d2005-02-21 08:23:39 +0000122
sewardj8d965312005-02-25 02:48:47 +0000123 /* 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. */
sewardjc6f970f2012-04-02 21:54:49 +0000127 UInt guest_FTOP;
philippe7e5aa0d2015-04-11 14:32:53 +0000128 UInt pad1;
sewardj8d965312005-02-25 02:48:47 +0000129 ULong guest_FPREG[8];
sewardjc6f970f2012-04-02 21:54:49 +0000130 UChar guest_FPTAG[8];
131 ULong guest_FPROUND;
132 ULong guest_FC3210;
sewardj8d965312005-02-25 02:48:47 +0000133
florian6ef84be2012-08-26 03:20:07 +0000134 /* Emulation notes */
135 UInt guest_EMNOTE;
philippe7e5aa0d2015-04-11 14:32:53 +0000136 UInt pad2;
sewardj1f126c52005-03-16 13:57:58 +0000137
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. */
sewardj05f5e012014-05-04 10:52:11 +0000145 ULong guest_CMSTART;
146 ULong guest_CMLEN;
sewardj1f126c52005-03-16 13:57:58 +0000147
sewardjce02aa72006-01-12 12:27:58 +0000148 /* 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
sewardjd660d412008-12-03 21:29:59 +0000156 /* Used for Darwin syscall dispatching. */
157 ULong guest_SC_CLASS;
158
philippee2cc4de2014-12-16 23:57:51 +0000159 /* 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
sewardj3e5d82d2015-07-21 14:43:23 +0000163 of the %fs-const hack for amd64-linux/solaris). */
philippee2cc4de2014-12-16 23:57:51 +0000164 ULong guest_GS_CONST;
sewardjd660d412008-12-03 21:29:59 +0000165
sewardje86310f2009-03-19 22:21:40 +0000166 /* 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
sewardj478646f2008-05-01 20:13:04 +0000172 /* Padding to make it have an 16-aligned size */
philippe7e5aa0d2015-04-11 14:32:53 +0000173 ULong pad3;
sewardj9a7b75d2004-11-23 14:07:46 +0000174 }
175 VexGuestAMD64State;
176
177
178
179/*---------------------------------------------------------------*/
180/*--- Utility functions for amd64 guest stuff. ---*/
181/*---------------------------------------------------------------*/
182
sewardj9a7b75d2004-11-23 14:07:46 +0000183/* ALL THE FOLLOWING ARE VISIBLE TO LIBRARY CLIENT */
184
sewardj65526c22005-01-20 19:43:56 +0000185/* Initialise all guest amd64 state. The FPU is put in default
186 mode. */
sewardj9a7b75d2004-11-23 14:07:46 +0000187extern
njn9c6acb02004-11-30 15:56:47 +0000188void LibVEX_GuestAMD64_initialise ( /*OUT*/VexGuestAMD64State* vex_state );
sewardj9a7b75d2004-11-23 14:07:46 +0000189
190
sewardj7de0d3c2005-02-13 02:26:41 +0000191/* Extract from the supplied VexGuestAMD64State structure the
192 corresponding native %rflags value. */
193extern
florianefa834a2012-11-24 21:07:14 +0000194ULong LibVEX_GuestAMD64_get_rflags ( /*IN*/const VexGuestAMD64State* vex_state );
sewardj7de0d3c2005-02-13 02:26:41 +0000195
sewardj3e5d82d2015-07-21 14:43:23 +0000196/* Put rflags into the given state. */
197extern
198void LibVEX_GuestAMD64_put_rflags ( ULong rflags,
199 /*MOD*/VexGuestAMD64State* vex_state );
200
sewardjd660d412008-12-03 21:29:59 +0000201/* Set the carry flag in the given state to 'new_carry_flag', which
202 should be zero or one. */
203extern
204void
205LibVEX_GuestAMD64_put_rflag_c ( ULong new_carry_flag,
206 /*MOD*/VexGuestAMD64State* vex_state );
207
sewardj3e5d82d2015-07-21 14:43:23 +0000208/* 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. */
211extern
212void 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. */
217extern
218VexEmNote LibVEX_GuestAMD64_fxrstor ( /*IN*/HWord fp_state,
219 /*MOD*/VexGuestAMD64State* gst );
sewardj7de0d3c2005-02-13 02:26:41 +0000220
sewardj9a7b75d2004-11-23 14:07:46 +0000221#endif /* ndef __LIBVEX_PUB_GUEST_AMD64_H */
222
223/*---------------------------------------------------------------*/
224/*--- libvex_guest_amd64.h ---*/
225/*---------------------------------------------------------------*/