blob: ac7dfa2417342f524d0a5a0bf48e6ce0062e8074 [file] [log] [blame]
sewardj0c2cb622004-09-06 23:21:21 +00001
2/*---------------------------------------------------------------*/
3/*--- ---*/
4/*--- This file (libvex_guest_x86.h) is ---*/
5/*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/
6/*--- ---*/
7/*---------------------------------------------------------------*/
8
sewardjf8ed9d82004-11-12 17:40:23 +00009/*
10 This file is part of LibVEX, a library for dynamic binary
11 instrumentation and translation.
12
13 Copyright (C) 2004 OpenWorks, LLP.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; Version 2 dated June 1991 of the
18 license.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or liability
23 for damages. See the GNU General Public License for more details.
24
25 Neither the names of the U.S. Department of Energy nor the
26 University of California nor the names of its contributors may be
27 used to endorse or promote products derived from this software
28 without prior written permission.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 USA.
34*/
35
sewardj0c2cb622004-09-06 23:21:21 +000036#ifndef __LIBVEX_PUB_GUEST_X86_H
37#define __LIBVEX_PUB_GUEST_X86_H
38
39#include "libvex_basictypes.h"
sewardj893aada2004-11-29 19:57:54 +000040#include "libvex_emwarn.h"
41
sewardj0c2cb622004-09-06 23:21:21 +000042
43/*---------------------------------------------------------------*/
sewardjf6dc3ce2004-10-19 01:03:46 +000044/*--- Vex's representation of the x86 CPU state. ---*/
45/*---------------------------------------------------------------*/
46
47/* The integer parts should be pretty straightforward. */
48
49/* Hmm, subregisters. The simulated state is stored in memory in the
50 host's byte ordering, so we can't say here what the offsets of %ax,
51 %al, %ah etc are since that depends on the host's byte ordering,
52 which we don't know. */
53
54/* FPU. For now, just simulate 8 64-bit registers, their tags, and
55 the reg-stack top pointer, of which only the least significant
56 three bits are relevant.
57
58 The model is:
59 F0 .. F7 are the 8 registers. FTOP[2:0] contains the
60 index of the current 'stack top' -- pretty meaningless, but
61 still. FTOP is a 32-bit value. FTOP[31:3] can be anything
62 (not guaranteed to be zero).
63
64 When a value is pushed onto the stack, ftop is first replaced by
65 (ftop-1) & 7, and then F[ftop] is assigned the value.
66
67 When a value is popped off the stack, the value is read from
68 F[ftop], and then ftop is replaced by (ftop+1) & 7.
69
70 In general, a reference to a register ST(i) actually references
71 F[ (ftop+i) & 7 ].
72
73 FTAG0 .. FTAG0+7 are the tags. Each is a byte, zero means empty,
74 non-zero means non-empty.
75
76 The general rule appears to be that a read or modify of a register
77 gets a stack underflow fault if the register is empty. A write of
78 a register (only a write, not a modify) gets a stack overflow fault
79 if the register is full. Note that "over" vs "under" is pretty
80 meaningless since the FP stack pointer can move around arbitrarily,
81 so it's really just two different kinds of exceptions:
82 register-empty and register full.
83
84 Naturally Intel (in its infinite wisdom) has seen fit to throw in
85 some ad-hoc inconsistencies to the fault-generation rules of the
86 above para, just to complicate everything. Known inconsistencies:
87
88 * fxam can read a register in any state without taking an underflow
89 fault.
90
91 * fst from st(0) to st(i) does not take an overflow fault even if the
92 destination is already full.
93
sewardj893aada2004-11-29 19:57:54 +000094 FPRTZ[0] is the FPU's notional rounding mode -- 0 "to nearest" (the
95 default), 1 "to zero". FPRTZ[31:1] is unused. Round to
96 +infinity/-infinity is not supported.
sewardjf6dc3ce2004-10-19 01:03:46 +000097
98 FC3210 contains the C3, C2, C1 and C0 bits in the same place they
99 are in the FPU's status word. (bits 14, 10, 9, 8 respectively).
100 All other bits should be zero. The relevant mask to select just
101 those bits is 0x4700. To select C3, C2 and C0 only, the mask is
sewardj893aada2004-11-29 19:57:54 +0000102 0x4500. */
sewardjf6dc3ce2004-10-19 01:03:46 +0000103
104typedef
105 struct {
106 UInt guest_EAX;
107 UInt guest_ECX;
108 UInt guest_EDX;
109 UInt guest_EBX;
110 UInt guest_ESP;
111 UInt guest_EBP;
112 UInt guest_ESI;
113 UInt guest_EDI;
sewardj2a2ba8b2004-11-08 13:14:06 +0000114 /* 4-word thunk used to calculate O S Z A C P flags. */
sewardjf6dc3ce2004-10-19 01:03:46 +0000115 UInt guest_CC_OP;
sewardj2a2ba8b2004-11-08 13:14:06 +0000116 UInt guest_CC_DEP1;
117 UInt guest_CC_DEP2;
118 UInt guest_CC_NDEP;
119 /* The D flag is stored here, encoded as either -1 or +1 */
sewardjf6dc3ce2004-10-19 01:03:46 +0000120 UInt guest_DFLAG;
sewardj006a6a22004-10-26 00:50:52 +0000121 /* Bit 21 (ID) of eflags stored here, as either 0 or 1. */
122 UInt guest_IDFLAG;
sewardjf6dc3ce2004-10-19 01:03:46 +0000123 /* EIP */
124 UInt guest_EIP;
125 /* FPU */
126 UInt guest_FTOP;
127 ULong guest_FPREG[8];
128 UChar guest_FPTAG[8];
sewardj893aada2004-11-29 19:57:54 +0000129 UInt guest_FPRTZ;
sewardjf6dc3ce2004-10-19 01:03:46 +0000130 UInt guest_FC3210;
sewardj063f02f2004-10-20 12:36:12 +0000131 /* Segment registers. */
132 UShort guest_CS;
133 UShort guest_DS;
134 UShort guest_ES;
135 UShort guest_FS;
136 UShort guest_GS;
137 UShort guest_SS;
sewardj893aada2004-11-29 19:57:54 +0000138 /* Emulation warnings */
139 UInt guest_EMWARN;
sewardj81ec4182004-10-25 23:15:52 +0000140 /* Padding to make it have an 8-aligned size */
sewardj893aada2004-11-29 19:57:54 +0000141 /* UInt padding; */
sewardjf6dc3ce2004-10-19 01:03:46 +0000142 }
143 VexGuestX86State;
144
145
sewardj8d2291c2004-10-25 14:50:21 +0000146
sewardjf6dc3ce2004-10-19 01:03:46 +0000147/*---------------------------------------------------------------*/
sewardj0c2cb622004-09-06 23:21:21 +0000148/*--- Utility functions for x86 guest stuff. ---*/
149/*---------------------------------------------------------------*/
150
sewardj8d2291c2004-10-25 14:50:21 +0000151
sewardj8d2291c2004-10-25 14:50:21 +0000152/* ALL THE FOLLOWING ARE VISIBLE TO LIBRARY CLIENT */
153
sewardj76bdc802004-10-25 15:33:26 +0000154
155/* Initialise all guest x86 state. The FPU is put in default mode. */
156extern
157void LibVEX_GuestX86_initialise ( /*OUT*/VexGuestX86State* vex_state );
158
159
sewardjf6dc3ce2004-10-19 01:03:46 +0000160/* Convert a saved x87 FPU image (as created by fsave) and write it
161 into the supplied VexGuestX86State structure. The non-FP parts of
sewardj893aada2004-11-29 19:57:54 +0000162 said structure are left unchanged. May return an emulation warning
163 value.
sewardj0c2cb622004-09-06 23:21:21 +0000164*/
sewardjf6dc3ce2004-10-19 01:03:46 +0000165extern
sewardj893aada2004-11-29 19:57:54 +0000166VexEmWarn
167 LibVEX_GuestX86_put_x87 ( /*IN*/UChar* x87_state,
sewardj76bdc802004-10-25 15:33:26 +0000168 /*OUT*/VexGuestX86State* vex_state );
sewardj0c2cb622004-09-06 23:21:21 +0000169
sewardjf6dc3ce2004-10-19 01:03:46 +0000170/* Extract from the supplied VexGuestX86State structure, an x87 FPU
171 image. */
172extern
sewardj76bdc802004-10-25 15:33:26 +0000173void LibVEX_GuestX86_get_x87 ( /*IN*/VexGuestX86State* vex_state,
174 /*OUT*/UChar* x87_state );
sewardjf6dc3ce2004-10-19 01:03:46 +0000175
176
177/* Given a 32-bit word containing native x86 %eflags values, set the
178 eflag-related fields in the supplied VexGuestX86State accordingly.
179 All other fields are left unchanged. */
180
181extern
sewardj76bdc802004-10-25 15:33:26 +0000182void LibVEX_GuestX86_put_eflags ( UInt eflags_native,
183 /*OUT*/VexGuestX86State* vex_state );
sewardjf6dc3ce2004-10-19 01:03:46 +0000184
185/* Extract from the supplied VexGuestX86State structure the
186 corresponding native %eflags value. */
187
188extern
sewardj76bdc802004-10-25 15:33:26 +0000189UInt LibVEX_GuestX86_get_eflags ( /*IN*/VexGuestX86State* vex_state );
sewardjf6dc3ce2004-10-19 01:03:46 +0000190
sewardj0c2cb622004-09-06 23:21:21 +0000191
192#endif /* ndef __LIBVEX_PUB_GUEST_X86_H */
193
194/*---------------------------------------------------------------*/
195/*--- libvex_guest_x86.h ---*/
196/*---------------------------------------------------------------*/