diff options
Diffstat (limited to 'arch/x86/kvm/emulate.c')
-rw-r--r-- | arch/x86/kvm/emulate.c | 2262 |
1 files changed, 1345 insertions, 917 deletions
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 66ca98aafdd6..38b6e8dafaff 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c | |||
@@ -9,7 +9,7 @@ | |||
9 | * privileged instructions: | 9 | * privileged instructions: |
10 | * | 10 | * |
11 | * Copyright (C) 2006 Qumranet | 11 | * Copyright (C) 2006 Qumranet |
12 | * Copyright 2010 Red Hat, Inc. and/or its affilates. | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
13 | * | 13 | * |
14 | * Avi Kivity <avi@qumranet.com> | 14 | * Avi Kivity <avi@qumranet.com> |
15 | * Yaniv Kamay <yaniv@qumranet.com> | 15 | * Yaniv Kamay <yaniv@qumranet.com> |
@@ -51,13 +51,13 @@ | |||
51 | #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */ | 51 | #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */ |
52 | #define DstReg (2<<1) /* Register operand. */ | 52 | #define DstReg (2<<1) /* Register operand. */ |
53 | #define DstMem (3<<1) /* Memory operand. */ | 53 | #define DstMem (3<<1) /* Memory operand. */ |
54 | #define DstAcc (4<<1) /* Destination Accumulator */ | 54 | #define DstAcc (4<<1) /* Destination Accumulator */ |
55 | #define DstDI (5<<1) /* Destination is in ES:(E)DI */ | 55 | #define DstDI (5<<1) /* Destination is in ES:(E)DI */ |
56 | #define DstMem64 (6<<1) /* 64bit memory operand */ | 56 | #define DstMem64 (6<<1) /* 64bit memory operand */ |
57 | #define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */ | ||
57 | #define DstMask (7<<1) | 58 | #define DstMask (7<<1) |
58 | /* Source operand type. */ | 59 | /* Source operand type. */ |
59 | #define SrcNone (0<<4) /* No source operand. */ | 60 | #define SrcNone (0<<4) /* No source operand. */ |
60 | #define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */ | ||
61 | #define SrcReg (1<<4) /* Register operand. */ | 61 | #define SrcReg (1<<4) /* Register operand. */ |
62 | #define SrcMem (2<<4) /* Memory operand. */ | 62 | #define SrcMem (2<<4) /* Memory operand. */ |
63 | #define SrcMem16 (3<<4) /* Memory operand (16-bit). */ | 63 | #define SrcMem16 (3<<4) /* Memory operand (16-bit). */ |
@@ -71,6 +71,7 @@ | |||
71 | #define SrcImmFAddr (0xb<<4) /* Source is immediate far address */ | 71 | #define SrcImmFAddr (0xb<<4) /* Source is immediate far address */ |
72 | #define SrcMemFAddr (0xc<<4) /* Source is far address in memory */ | 72 | #define SrcMemFAddr (0xc<<4) /* Source is far address in memory */ |
73 | #define SrcAcc (0xd<<4) /* Source Accumulator */ | 73 | #define SrcAcc (0xd<<4) /* Source Accumulator */ |
74 | #define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */ | ||
74 | #define SrcMask (0xf<<4) | 75 | #define SrcMask (0xf<<4) |
75 | /* Generic ModRM decode. */ | 76 | /* Generic ModRM decode. */ |
76 | #define ModRM (1<<8) | 77 | #define ModRM (1<<8) |
@@ -82,8 +83,10 @@ | |||
82 | #define Stack (1<<13) /* Stack instruction (push/pop) */ | 83 | #define Stack (1<<13) /* Stack instruction (push/pop) */ |
83 | #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */ | 84 | #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */ |
84 | #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */ | 85 | #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */ |
85 | #define GroupMask 0xff /* Group number stored in bits 0:7 */ | ||
86 | /* Misc flags */ | 86 | /* Misc flags */ |
87 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ | ||
88 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ | ||
89 | #define Undefined (1<<25) /* No Such Instruction */ | ||
87 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ | 90 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
88 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ | 91 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
89 | #define No64 (1<<28) | 92 | #define No64 (1<<28) |
@@ -92,285 +95,30 @@ | |||
92 | #define Src2CL (1<<29) | 95 | #define Src2CL (1<<29) |
93 | #define Src2ImmByte (2<<29) | 96 | #define Src2ImmByte (2<<29) |
94 | #define Src2One (3<<29) | 97 | #define Src2One (3<<29) |
98 | #define Src2Imm (4<<29) | ||
95 | #define Src2Mask (7<<29) | 99 | #define Src2Mask (7<<29) |
96 | 100 | ||
97 | enum { | 101 | #define X2(x...) x, x |
98 | Group1_80, Group1_81, Group1_82, Group1_83, | 102 | #define X3(x...) X2(x), x |
99 | Group1A, Group3_Byte, Group3, Group4, Group5, Group7, | 103 | #define X4(x...) X2(x), X2(x) |
100 | Group8, Group9, | 104 | #define X5(x...) X4(x), x |
105 | #define X6(x...) X4(x), X2(x) | ||
106 | #define X7(x...) X4(x), X3(x) | ||
107 | #define X8(x...) X4(x), X4(x) | ||
108 | #define X16(x...) X8(x), X8(x) | ||
109 | |||
110 | struct opcode { | ||
111 | u32 flags; | ||
112 | union { | ||
113 | int (*execute)(struct x86_emulate_ctxt *ctxt); | ||
114 | struct opcode *group; | ||
115 | struct group_dual *gdual; | ||
116 | } u; | ||
101 | }; | 117 | }; |
102 | 118 | ||
103 | static u32 opcode_table[256] = { | 119 | struct group_dual { |
104 | /* 0x00 - 0x07 */ | 120 | struct opcode mod012[8]; |
105 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | 121 | struct opcode mod3[8]; |
106 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
107 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, | ||
108 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, | ||
109 | /* 0x08 - 0x0F */ | ||
110 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
111 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
112 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, | ||
113 | ImplicitOps | Stack | No64, 0, | ||
114 | /* 0x10 - 0x17 */ | ||
115 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
116 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
117 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, | ||
118 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, | ||
119 | /* 0x18 - 0x1F */ | ||
120 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
121 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
122 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, | ||
123 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, | ||
124 | /* 0x20 - 0x27 */ | ||
125 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
126 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
127 | ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0, | ||
128 | /* 0x28 - 0x2F */ | ||
129 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
130 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
131 | ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0, | ||
132 | /* 0x30 - 0x37 */ | ||
133 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
134 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
135 | ByteOp | DstAcc | SrcImmByte, DstAcc | SrcImm, 0, 0, | ||
136 | /* 0x38 - 0x3F */ | ||
137 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, | ||
138 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, | ||
139 | ByteOp | DstAcc | SrcImm, DstAcc | SrcImm, | ||
140 | 0, 0, | ||
141 | /* 0x40 - 0x47 */ | ||
142 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, | ||
143 | /* 0x48 - 0x4F */ | ||
144 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, | ||
145 | /* 0x50 - 0x57 */ | ||
146 | SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, | ||
147 | SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, SrcReg | Stack, | ||
148 | /* 0x58 - 0x5F */ | ||
149 | DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack, | ||
150 | DstReg | Stack, DstReg | Stack, DstReg | Stack, DstReg | Stack, | ||
151 | /* 0x60 - 0x67 */ | ||
152 | ImplicitOps | Stack | No64, ImplicitOps | Stack | No64, | ||
153 | 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ , | ||
154 | 0, 0, 0, 0, | ||
155 | /* 0x68 - 0x6F */ | ||
156 | SrcImm | Mov | Stack, 0, SrcImmByte | Mov | Stack, 0, | ||
157 | DstDI | ByteOp | Mov | String, DstDI | Mov | String, /* insb, insw/insd */ | ||
158 | SrcSI | ByteOp | ImplicitOps | String, SrcSI | ImplicitOps | String, /* outsb, outsw/outsd */ | ||
159 | /* 0x70 - 0x77 */ | ||
160 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, | ||
161 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, | ||
162 | /* 0x78 - 0x7F */ | ||
163 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, | ||
164 | SrcImmByte, SrcImmByte, SrcImmByte, SrcImmByte, | ||
165 | /* 0x80 - 0x87 */ | ||
166 | Group | Group1_80, Group | Group1_81, | ||
167 | Group | Group1_82, Group | Group1_83, | ||
168 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, | ||
169 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
170 | /* 0x88 - 0x8F */ | ||
171 | ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov, | ||
172 | ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
173 | DstMem | SrcNone | ModRM | Mov, ModRM | DstReg, | ||
174 | ImplicitOps | SrcMem16 | ModRM, Group | Group1A, | ||
175 | /* 0x90 - 0x97 */ | ||
176 | DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, DstReg, | ||
177 | /* 0x98 - 0x9F */ | ||
178 | 0, 0, SrcImmFAddr | No64, 0, | ||
179 | ImplicitOps | Stack, ImplicitOps | Stack, 0, 0, | ||
180 | /* 0xA0 - 0xA7 */ | ||
181 | ByteOp | DstAcc | SrcMem | Mov | MemAbs, DstAcc | SrcMem | Mov | MemAbs, | ||
182 | ByteOp | DstMem | SrcAcc | Mov | MemAbs, DstMem | SrcAcc | Mov | MemAbs, | ||
183 | ByteOp | SrcSI | DstDI | Mov | String, SrcSI | DstDI | Mov | String, | ||
184 | ByteOp | SrcSI | DstDI | String, SrcSI | DstDI | String, | ||
185 | /* 0xA8 - 0xAF */ | ||
186 | DstAcc | SrcImmByte | ByteOp, DstAcc | SrcImm, ByteOp | DstDI | Mov | String, DstDI | Mov | String, | ||
187 | ByteOp | SrcSI | DstAcc | Mov | String, SrcSI | DstAcc | Mov | String, | ||
188 | ByteOp | DstDI | String, DstDI | String, | ||
189 | /* 0xB0 - 0xB7 */ | ||
190 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, | ||
191 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, | ||
192 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, | ||
193 | ByteOp | DstReg | SrcImm | Mov, ByteOp | DstReg | SrcImm | Mov, | ||
194 | /* 0xB8 - 0xBF */ | ||
195 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, | ||
196 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, | ||
197 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, | ||
198 | DstReg | SrcImm | Mov, DstReg | SrcImm | Mov, | ||
199 | /* 0xC0 - 0xC7 */ | ||
200 | ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM, | ||
201 | 0, ImplicitOps | Stack, 0, 0, | ||
202 | ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov, | ||
203 | /* 0xC8 - 0xCF */ | ||
204 | 0, 0, 0, ImplicitOps | Stack, | ||
205 | ImplicitOps, SrcImmByte, ImplicitOps | No64, ImplicitOps, | ||
206 | /* 0xD0 - 0xD7 */ | ||
207 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, | ||
208 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, | ||
209 | 0, 0, 0, 0, | ||
210 | /* 0xD8 - 0xDF */ | ||
211 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
212 | /* 0xE0 - 0xE7 */ | ||
213 | 0, 0, 0, 0, | ||
214 | ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc, | ||
215 | ByteOp | SrcImmUByte | DstAcc, SrcImmUByte | DstAcc, | ||
216 | /* 0xE8 - 0xEF */ | ||
217 | SrcImm | Stack, SrcImm | ImplicitOps, | ||
218 | SrcImmFAddr | No64, SrcImmByte | ImplicitOps, | ||
219 | SrcNone | ByteOp | DstAcc, SrcNone | DstAcc, | ||
220 | SrcNone | ByteOp | DstAcc, SrcNone | DstAcc, | ||
221 | /* 0xF0 - 0xF7 */ | ||
222 | 0, 0, 0, 0, | ||
223 | ImplicitOps | Priv, ImplicitOps, Group | Group3_Byte, Group | Group3, | ||
224 | /* 0xF8 - 0xFF */ | ||
225 | ImplicitOps, 0, ImplicitOps, ImplicitOps, | ||
226 | ImplicitOps, ImplicitOps, Group | Group4, Group | Group5, | ||
227 | }; | ||
228 | |||
229 | static u32 twobyte_table[256] = { | ||
230 | /* 0x00 - 0x0F */ | ||
231 | 0, Group | GroupDual | Group7, 0, 0, | ||
232 | 0, ImplicitOps, ImplicitOps | Priv, 0, | ||
233 | ImplicitOps | Priv, ImplicitOps | Priv, 0, 0, | ||
234 | 0, ImplicitOps | ModRM, 0, 0, | ||
235 | /* 0x10 - 0x1F */ | ||
236 | 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, | ||
237 | /* 0x20 - 0x2F */ | ||
238 | ModRM | ImplicitOps | Priv, ModRM | Priv, | ||
239 | ModRM | ImplicitOps | Priv, ModRM | Priv, | ||
240 | 0, 0, 0, 0, | ||
241 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
242 | /* 0x30 - 0x3F */ | ||
243 | ImplicitOps | Priv, 0, ImplicitOps | Priv, 0, | ||
244 | ImplicitOps, ImplicitOps | Priv, 0, 0, | ||
245 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
246 | /* 0x40 - 0x47 */ | ||
247 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
248 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
249 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
250 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
251 | /* 0x48 - 0x4F */ | ||
252 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
253 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
254 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
255 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, | ||
256 | /* 0x50 - 0x5F */ | ||
257 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
258 | /* 0x60 - 0x6F */ | ||
259 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
260 | /* 0x70 - 0x7F */ | ||
261 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
262 | /* 0x80 - 0x8F */ | ||
263 | SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, | ||
264 | SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, SrcImm, | ||
265 | /* 0x90 - 0x9F */ | ||
266 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
267 | /* 0xA0 - 0xA7 */ | ||
268 | ImplicitOps | Stack, ImplicitOps | Stack, | ||
269 | 0, DstMem | SrcReg | ModRM | BitOp, | ||
270 | DstMem | SrcReg | Src2ImmByte | ModRM, | ||
271 | DstMem | SrcReg | Src2CL | ModRM, 0, 0, | ||
272 | /* 0xA8 - 0xAF */ | ||
273 | ImplicitOps | Stack, ImplicitOps | Stack, | ||
274 | 0, DstMem | SrcReg | ModRM | BitOp | Lock, | ||
275 | DstMem | SrcReg | Src2ImmByte | ModRM, | ||
276 | DstMem | SrcReg | Src2CL | ModRM, | ||
277 | ModRM, 0, | ||
278 | /* 0xB0 - 0xB7 */ | ||
279 | ByteOp | DstMem | SrcReg | ModRM | Lock, DstMem | SrcReg | ModRM | Lock, | ||
280 | 0, DstMem | SrcReg | ModRM | BitOp | Lock, | ||
281 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, | ||
282 | DstReg | SrcMem16 | ModRM | Mov, | ||
283 | /* 0xB8 - 0xBF */ | ||
284 | 0, 0, | ||
285 | Group | Group8, DstMem | SrcReg | ModRM | BitOp | Lock, | ||
286 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, | ||
287 | DstReg | SrcMem16 | ModRM | Mov, | ||
288 | /* 0xC0 - 0xCF */ | ||
289 | 0, 0, 0, DstMem | SrcReg | ModRM | Mov, | ||
290 | 0, 0, 0, Group | GroupDual | Group9, | ||
291 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
292 | /* 0xD0 - 0xDF */ | ||
293 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
294 | /* 0xE0 - 0xEF */ | ||
295 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
296 | /* 0xF0 - 0xFF */ | ||
297 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
298 | }; | ||
299 | |||
300 | static u32 group_table[] = { | ||
301 | [Group1_80*8] = | ||
302 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
303 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
304 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
305 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
306 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
307 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
308 | ByteOp | DstMem | SrcImm | ModRM | Lock, | ||
309 | ByteOp | DstMem | SrcImm | ModRM, | ||
310 | [Group1_81*8] = | ||
311 | DstMem | SrcImm | ModRM | Lock, | ||
312 | DstMem | SrcImm | ModRM | Lock, | ||
313 | DstMem | SrcImm | ModRM | Lock, | ||
314 | DstMem | SrcImm | ModRM | Lock, | ||
315 | DstMem | SrcImm | ModRM | Lock, | ||
316 | DstMem | SrcImm | ModRM | Lock, | ||
317 | DstMem | SrcImm | ModRM | Lock, | ||
318 | DstMem | SrcImm | ModRM, | ||
319 | [Group1_82*8] = | ||
320 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
321 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
322 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
323 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
324 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
325 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
326 | ByteOp | DstMem | SrcImm | ModRM | No64 | Lock, | ||
327 | ByteOp | DstMem | SrcImm | ModRM | No64, | ||
328 | [Group1_83*8] = | ||
329 | DstMem | SrcImmByte | ModRM | Lock, | ||
330 | DstMem | SrcImmByte | ModRM | Lock, | ||
331 | DstMem | SrcImmByte | ModRM | Lock, | ||
332 | DstMem | SrcImmByte | ModRM | Lock, | ||
333 | DstMem | SrcImmByte | ModRM | Lock, | ||
334 | DstMem | SrcImmByte | ModRM | Lock, | ||
335 | DstMem | SrcImmByte | ModRM | Lock, | ||
336 | DstMem | SrcImmByte | ModRM, | ||
337 | [Group1A*8] = | ||
338 | DstMem | SrcNone | ModRM | Mov | Stack, 0, 0, 0, 0, 0, 0, 0, | ||
339 | [Group3_Byte*8] = | ||
340 | ByteOp | SrcImm | DstMem | ModRM, ByteOp | SrcImm | DstMem | ModRM, | ||
341 | ByteOp | DstMem | SrcNone | ModRM, ByteOp | DstMem | SrcNone | ModRM, | ||
342 | 0, 0, 0, 0, | ||
343 | [Group3*8] = | ||
344 | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM, | ||
345 | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, | ||
346 | 0, 0, 0, 0, | ||
347 | [Group4*8] = | ||
348 | ByteOp | DstMem | SrcNone | ModRM | Lock, ByteOp | DstMem | SrcNone | ModRM | Lock, | ||
349 | 0, 0, 0, 0, 0, 0, | ||
350 | [Group5*8] = | ||
351 | DstMem | SrcNone | ModRM | Lock, DstMem | SrcNone | ModRM | Lock, | ||
352 | SrcMem | ModRM | Stack, 0, | ||
353 | SrcMem | ModRM | Stack, SrcMemFAddr | ModRM | ImplicitOps, | ||
354 | SrcMem | ModRM | Stack, 0, | ||
355 | [Group7*8] = | ||
356 | 0, 0, ModRM | SrcMem | Priv, ModRM | SrcMem | Priv, | ||
357 | SrcNone | ModRM | DstMem | Mov, 0, | ||
358 | SrcMem16 | ModRM | Mov | Priv, SrcMem | ModRM | ByteOp | Priv, | ||
359 | [Group8*8] = | ||
360 | 0, 0, 0, 0, | ||
361 | DstMem | SrcImmByte | ModRM, DstMem | SrcImmByte | ModRM | Lock, | ||
362 | DstMem | SrcImmByte | ModRM | Lock, DstMem | SrcImmByte | ModRM | Lock, | ||
363 | [Group9*8] = | ||
364 | 0, DstMem64 | ModRM | Lock, 0, 0, 0, 0, 0, 0, | ||
365 | }; | ||
366 | |||
367 | static u32 group2_table[] = { | ||
368 | [Group7*8] = | ||
369 | SrcNone | ModRM | Priv, 0, 0, SrcNone | ModRM | Priv, | ||
370 | SrcNone | ModRM | DstMem | Mov, 0, | ||
371 | SrcMem16 | ModRM | Mov | Priv, 0, | ||
372 | [Group9*8] = | ||
373 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
374 | }; | 122 | }; |
375 | 123 | ||
376 | /* EFLAGS bit definitions. */ | 124 | /* EFLAGS bit definitions. */ |
@@ -392,6 +140,9 @@ static u32 group2_table[] = { | |||
392 | #define EFLG_PF (1<<2) | 140 | #define EFLG_PF (1<<2) |
393 | #define EFLG_CF (1<<0) | 141 | #define EFLG_CF (1<<0) |
394 | 142 | ||
143 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a | ||
144 | #define EFLG_RESERVED_ONE_MASK 2 | ||
145 | |||
395 | /* | 146 | /* |
396 | * Instruction emulation: | 147 | * Instruction emulation: |
397 | * Most instructions are emulated directly via a fragment of inline assembly | 148 | * Most instructions are emulated directly via a fragment of inline assembly |
@@ -444,13 +195,13 @@ static u32 group2_table[] = { | |||
444 | #define ON64(x) | 195 | #define ON64(x) |
445 | #endif | 196 | #endif |
446 | 197 | ||
447 | #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \ | 198 | #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \ |
448 | do { \ | 199 | do { \ |
449 | __asm__ __volatile__ ( \ | 200 | __asm__ __volatile__ ( \ |
450 | _PRE_EFLAGS("0", "4", "2") \ | 201 | _PRE_EFLAGS("0", "4", "2") \ |
451 | _op _suffix " %"_x"3,%1; " \ | 202 | _op _suffix " %"_x"3,%1; " \ |
452 | _POST_EFLAGS("0", "4", "2") \ | 203 | _POST_EFLAGS("0", "4", "2") \ |
453 | : "=m" (_eflags), "=m" ((_dst).val), \ | 204 | : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\ |
454 | "=&r" (_tmp) \ | 205 | "=&r" (_tmp) \ |
455 | : _y ((_src).val), "i" (EFLAGS_MASK)); \ | 206 | : _y ((_src).val), "i" (EFLAGS_MASK)); \ |
456 | } while (0) | 207 | } while (0) |
@@ -463,13 +214,13 @@ static u32 group2_table[] = { | |||
463 | \ | 214 | \ |
464 | switch ((_dst).bytes) { \ | 215 | switch ((_dst).bytes) { \ |
465 | case 2: \ | 216 | case 2: \ |
466 | ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \ | 217 | ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\ |
467 | break; \ | 218 | break; \ |
468 | case 4: \ | 219 | case 4: \ |
469 | ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \ | 220 | ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\ |
470 | break; \ | 221 | break; \ |
471 | case 8: \ | 222 | case 8: \ |
472 | ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \ | 223 | ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \ |
473 | break; \ | 224 | break; \ |
474 | } \ | 225 | } \ |
475 | } while (0) | 226 | } while (0) |
@@ -479,7 +230,7 @@ static u32 group2_table[] = { | |||
479 | unsigned long _tmp; \ | 230 | unsigned long _tmp; \ |
480 | switch ((_dst).bytes) { \ | 231 | switch ((_dst).bytes) { \ |
481 | case 1: \ | 232 | case 1: \ |
482 | ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \ | 233 | ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \ |
483 | break; \ | 234 | break; \ |
484 | default: \ | 235 | default: \ |
485 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ | 236 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
@@ -566,6 +317,74 @@ static u32 group2_table[] = { | |||
566 | } \ | 317 | } \ |
567 | } while (0) | 318 | } while (0) |
568 | 319 | ||
320 | #define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \ | ||
321 | do { \ | ||
322 | unsigned long _tmp; \ | ||
323 | \ | ||
324 | __asm__ __volatile__ ( \ | ||
325 | _PRE_EFLAGS("0", "4", "1") \ | ||
326 | _op _suffix " %5; " \ | ||
327 | _POST_EFLAGS("0", "4", "1") \ | ||
328 | : "=m" (_eflags), "=&r" (_tmp), \ | ||
329 | "+a" (_rax), "+d" (_rdx) \ | ||
330 | : "i" (EFLAGS_MASK), "m" ((_src).val), \ | ||
331 | "a" (_rax), "d" (_rdx)); \ | ||
332 | } while (0) | ||
333 | |||
334 | #define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \ | ||
335 | do { \ | ||
336 | unsigned long _tmp; \ | ||
337 | \ | ||
338 | __asm__ __volatile__ ( \ | ||
339 | _PRE_EFLAGS("0", "5", "1") \ | ||
340 | "1: \n\t" \ | ||
341 | _op _suffix " %6; " \ | ||
342 | "2: \n\t" \ | ||
343 | _POST_EFLAGS("0", "5", "1") \ | ||
344 | ".pushsection .fixup,\"ax\" \n\t" \ | ||
345 | "3: movb $1, %4 \n\t" \ | ||
346 | "jmp 2b \n\t" \ | ||
347 | ".popsection \n\t" \ | ||
348 | _ASM_EXTABLE(1b, 3b) \ | ||
349 | : "=m" (_eflags), "=&r" (_tmp), \ | ||
350 | "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \ | ||
351 | : "i" (EFLAGS_MASK), "m" ((_src).val), \ | ||
352 | "a" (_rax), "d" (_rdx)); \ | ||
353 | } while (0) | ||
354 | |||
355 | /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */ | ||
356 | #define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \ | ||
357 | do { \ | ||
358 | switch((_src).bytes) { \ | ||
359 | case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \ | ||
360 | case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \ | ||
361 | case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \ | ||
362 | case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \ | ||
363 | } \ | ||
364 | } while (0) | ||
365 | |||
366 | #define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \ | ||
367 | do { \ | ||
368 | switch((_src).bytes) { \ | ||
369 | case 1: \ | ||
370 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ | ||
371 | _eflags, "b", _ex); \ | ||
372 | break; \ | ||
373 | case 2: \ | ||
374 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ | ||
375 | _eflags, "w", _ex); \ | ||
376 | break; \ | ||
377 | case 4: \ | ||
378 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ | ||
379 | _eflags, "l", _ex); \ | ||
380 | break; \ | ||
381 | case 8: ON64( \ | ||
382 | __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \ | ||
383 | _eflags, "q", _ex)); \ | ||
384 | break; \ | ||
385 | } \ | ||
386 | } while (0) | ||
387 | |||
569 | /* Fetch next part of the instruction being emulated. */ | 388 | /* Fetch next part of the instruction being emulated. */ |
570 | #define insn_fetch(_type, _size, _eip) \ | 389 | #define insn_fetch(_type, _size, _eip) \ |
571 | ({ unsigned long _x; \ | 390 | ({ unsigned long _x; \ |
@@ -661,7 +480,6 @@ static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, | |||
661 | ctxt->exception = vec; | 480 | ctxt->exception = vec; |
662 | ctxt->error_code = error; | 481 | ctxt->error_code = error; |
663 | ctxt->error_code_valid = valid; | 482 | ctxt->error_code_valid = valid; |
664 | ctxt->restart = false; | ||
665 | } | 483 | } |
666 | 484 | ||
667 | static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err) | 485 | static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
@@ -669,11 +487,9 @@ static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err) | |||
669 | emulate_exception(ctxt, GP_VECTOR, err, true); | 487 | emulate_exception(ctxt, GP_VECTOR, err, true); |
670 | } | 488 | } |
671 | 489 | ||
672 | static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr, | 490 | static void emulate_pf(struct x86_emulate_ctxt *ctxt) |
673 | int err) | ||
674 | { | 491 | { |
675 | ctxt->cr2 = addr; | 492 | emulate_exception(ctxt, PF_VECTOR, 0, true); |
676 | emulate_exception(ctxt, PF_VECTOR, err, true); | ||
677 | } | 493 | } |
678 | 494 | ||
679 | static void emulate_ud(struct x86_emulate_ctxt *ctxt) | 495 | static void emulate_ud(struct x86_emulate_ctxt *ctxt) |
@@ -686,6 +502,12 @@ static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err) | |||
686 | emulate_exception(ctxt, TS_VECTOR, err, true); | 502 | emulate_exception(ctxt, TS_VECTOR, err, true); |
687 | } | 503 | } |
688 | 504 | ||
505 | static int emulate_de(struct x86_emulate_ctxt *ctxt) | ||
506 | { | ||
507 | emulate_exception(ctxt, DE_VECTOR, 0, false); | ||
508 | return X86EMUL_PROPAGATE_FAULT; | ||
509 | } | ||
510 | |||
689 | static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt, | 511 | static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt, |
690 | struct x86_emulate_ops *ops, | 512 | struct x86_emulate_ops *ops, |
691 | unsigned long eip, u8 *dest) | 513 | unsigned long eip, u8 *dest) |
@@ -742,7 +564,7 @@ static void *decode_register(u8 modrm_reg, unsigned long *regs, | |||
742 | 564 | ||
743 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, | 565 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
744 | struct x86_emulate_ops *ops, | 566 | struct x86_emulate_ops *ops, |
745 | void *ptr, | 567 | ulong addr, |
746 | u16 *size, unsigned long *address, int op_bytes) | 568 | u16 *size, unsigned long *address, int op_bytes) |
747 | { | 569 | { |
748 | int rc; | 570 | int rc; |
@@ -750,12 +572,10 @@ static int read_descriptor(struct x86_emulate_ctxt *ctxt, | |||
750 | if (op_bytes == 2) | 572 | if (op_bytes == 2) |
751 | op_bytes = 3; | 573 | op_bytes = 3; |
752 | *address = 0; | 574 | *address = 0; |
753 | rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2, | 575 | rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL); |
754 | ctxt->vcpu, NULL); | ||
755 | if (rc != X86EMUL_CONTINUE) | 576 | if (rc != X86EMUL_CONTINUE) |
756 | return rc; | 577 | return rc; |
757 | rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes, | 578 | rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL); |
758 | ctxt->vcpu, NULL); | ||
759 | return rc; | 579 | return rc; |
760 | } | 580 | } |
761 | 581 | ||
@@ -794,6 +614,24 @@ static int test_cc(unsigned int condition, unsigned int flags) | |||
794 | return (!!rc ^ (condition & 1)); | 614 | return (!!rc ^ (condition & 1)); |
795 | } | 615 | } |
796 | 616 | ||
617 | static void fetch_register_operand(struct operand *op) | ||
618 | { | ||
619 | switch (op->bytes) { | ||
620 | case 1: | ||
621 | op->val = *(u8 *)op->addr.reg; | ||
622 | break; | ||
623 | case 2: | ||
624 | op->val = *(u16 *)op->addr.reg; | ||
625 | break; | ||
626 | case 4: | ||
627 | op->val = *(u32 *)op->addr.reg; | ||
628 | break; | ||
629 | case 8: | ||
630 | op->val = *(u64 *)op->addr.reg; | ||
631 | break; | ||
632 | } | ||
633 | } | ||
634 | |||
797 | static void decode_register_operand(struct operand *op, | 635 | static void decode_register_operand(struct operand *op, |
798 | struct decode_cache *c, | 636 | struct decode_cache *c, |
799 | int inhibit_bytereg) | 637 | int inhibit_bytereg) |
@@ -805,34 +643,25 @@ static void decode_register_operand(struct operand *op, | |||
805 | reg = (c->b & 7) | ((c->rex_prefix & 1) << 3); | 643 | reg = (c->b & 7) | ((c->rex_prefix & 1) << 3); |
806 | op->type = OP_REG; | 644 | op->type = OP_REG; |
807 | if ((c->d & ByteOp) && !inhibit_bytereg) { | 645 | if ((c->d & ByteOp) && !inhibit_bytereg) { |
808 | op->ptr = decode_register(reg, c->regs, highbyte_regs); | 646 | op->addr.reg = decode_register(reg, c->regs, highbyte_regs); |
809 | op->val = *(u8 *)op->ptr; | ||
810 | op->bytes = 1; | 647 | op->bytes = 1; |
811 | } else { | 648 | } else { |
812 | op->ptr = decode_register(reg, c->regs, 0); | 649 | op->addr.reg = decode_register(reg, c->regs, 0); |
813 | op->bytes = c->op_bytes; | 650 | op->bytes = c->op_bytes; |
814 | switch (op->bytes) { | ||
815 | case 2: | ||
816 | op->val = *(u16 *)op->ptr; | ||
817 | break; | ||
818 | case 4: | ||
819 | op->val = *(u32 *)op->ptr; | ||
820 | break; | ||
821 | case 8: | ||
822 | op->val = *(u64 *) op->ptr; | ||
823 | break; | ||
824 | } | ||
825 | } | 651 | } |
652 | fetch_register_operand(op); | ||
826 | op->orig_val = op->val; | 653 | op->orig_val = op->val; |
827 | } | 654 | } |
828 | 655 | ||
829 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, | 656 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
830 | struct x86_emulate_ops *ops) | 657 | struct x86_emulate_ops *ops, |
658 | struct operand *op) | ||
831 | { | 659 | { |
832 | struct decode_cache *c = &ctxt->decode; | 660 | struct decode_cache *c = &ctxt->decode; |
833 | u8 sib; | 661 | u8 sib; |
834 | int index_reg = 0, base_reg = 0, scale; | 662 | int index_reg = 0, base_reg = 0, scale; |
835 | int rc = X86EMUL_CONTINUE; | 663 | int rc = X86EMUL_CONTINUE; |
664 | ulong modrm_ea = 0; | ||
836 | 665 | ||
837 | if (c->rex_prefix) { | 666 | if (c->rex_prefix) { |
838 | c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */ | 667 | c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */ |
@@ -844,16 +673,19 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, | |||
844 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; | 673 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; |
845 | c->modrm_reg |= (c->modrm & 0x38) >> 3; | 674 | c->modrm_reg |= (c->modrm & 0x38) >> 3; |
846 | c->modrm_rm |= (c->modrm & 0x07); | 675 | c->modrm_rm |= (c->modrm & 0x07); |
847 | c->modrm_ea = 0; | 676 | c->modrm_seg = VCPU_SREG_DS; |
848 | c->use_modrm_ea = 1; | ||
849 | 677 | ||
850 | if (c->modrm_mod == 3) { | 678 | if (c->modrm_mod == 3) { |
851 | c->modrm_ptr = decode_register(c->modrm_rm, | 679 | op->type = OP_REG; |
680 | op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
681 | op->addr.reg = decode_register(c->modrm_rm, | ||
852 | c->regs, c->d & ByteOp); | 682 | c->regs, c->d & ByteOp); |
853 | c->modrm_val = *(unsigned long *)c->modrm_ptr; | 683 | fetch_register_operand(op); |
854 | return rc; | 684 | return rc; |
855 | } | 685 | } |
856 | 686 | ||
687 | op->type = OP_MEM; | ||
688 | |||
857 | if (c->ad_bytes == 2) { | 689 | if (c->ad_bytes == 2) { |
858 | unsigned bx = c->regs[VCPU_REGS_RBX]; | 690 | unsigned bx = c->regs[VCPU_REGS_RBX]; |
859 | unsigned bp = c->regs[VCPU_REGS_RBP]; | 691 | unsigned bp = c->regs[VCPU_REGS_RBP]; |
@@ -864,47 +696,46 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, | |||
864 | switch (c->modrm_mod) { | 696 | switch (c->modrm_mod) { |
865 | case 0: | 697 | case 0: |
866 | if (c->modrm_rm == 6) | 698 | if (c->modrm_rm == 6) |
867 | c->modrm_ea += insn_fetch(u16, 2, c->eip); | 699 | modrm_ea += insn_fetch(u16, 2, c->eip); |
868 | break; | 700 | break; |
869 | case 1: | 701 | case 1: |
870 | c->modrm_ea += insn_fetch(s8, 1, c->eip); | 702 | modrm_ea += insn_fetch(s8, 1, c->eip); |
871 | break; | 703 | break; |
872 | case 2: | 704 | case 2: |
873 | c->modrm_ea += insn_fetch(u16, 2, c->eip); | 705 | modrm_ea += insn_fetch(u16, 2, c->eip); |
874 | break; | 706 | break; |
875 | } | 707 | } |
876 | switch (c->modrm_rm) { | 708 | switch (c->modrm_rm) { |
877 | case 0: | 709 | case 0: |
878 | c->modrm_ea += bx + si; | 710 | modrm_ea += bx + si; |
879 | break; | 711 | break; |
880 | case 1: | 712 | case 1: |
881 | c->modrm_ea += bx + di; | 713 | modrm_ea += bx + di; |
882 | break; | 714 | break; |
883 | case 2: | 715 | case 2: |
884 | c->modrm_ea += bp + si; | 716 | modrm_ea += bp + si; |
885 | break; | 717 | break; |
886 | case 3: | 718 | case 3: |
887 | c->modrm_ea += bp + di; | 719 | modrm_ea += bp + di; |
888 | break; | 720 | break; |
889 | case 4: | 721 | case 4: |
890 | c->modrm_ea += si; | 722 | modrm_ea += si; |
891 | break; | 723 | break; |
892 | case 5: | 724 | case 5: |
893 | c->modrm_ea += di; | 725 | modrm_ea += di; |
894 | break; | 726 | break; |
895 | case 6: | 727 | case 6: |
896 | if (c->modrm_mod != 0) | 728 | if (c->modrm_mod != 0) |
897 | c->modrm_ea += bp; | 729 | modrm_ea += bp; |
898 | break; | 730 | break; |
899 | case 7: | 731 | case 7: |
900 | c->modrm_ea += bx; | 732 | modrm_ea += bx; |
901 | break; | 733 | break; |
902 | } | 734 | } |
903 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || | 735 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || |
904 | (c->modrm_rm == 6 && c->modrm_mod != 0)) | 736 | (c->modrm_rm == 6 && c->modrm_mod != 0)) |
905 | if (!c->has_seg_override) | 737 | c->modrm_seg = VCPU_SREG_SS; |
906 | set_seg_override(c, VCPU_SREG_SS); | 738 | modrm_ea = (u16)modrm_ea; |
907 | c->modrm_ea = (u16)c->modrm_ea; | ||
908 | } else { | 739 | } else { |
909 | /* 32/64-bit ModR/M decode. */ | 740 | /* 32/64-bit ModR/M decode. */ |
910 | if ((c->modrm_rm & 7) == 4) { | 741 | if ((c->modrm_rm & 7) == 4) { |
@@ -914,410 +745,74 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, | |||
914 | scale = sib >> 6; | 745 | scale = sib >> 6; |
915 | 746 | ||
916 | if ((base_reg & 7) == 5 && c->modrm_mod == 0) | 747 | if ((base_reg & 7) == 5 && c->modrm_mod == 0) |
917 | c->modrm_ea += insn_fetch(s32, 4, c->eip); | 748 | modrm_ea += insn_fetch(s32, 4, c->eip); |
918 | else | 749 | else |
919 | c->modrm_ea += c->regs[base_reg]; | 750 | modrm_ea += c->regs[base_reg]; |
920 | if (index_reg != 4) | 751 | if (index_reg != 4) |
921 | c->modrm_ea += c->regs[index_reg] << scale; | 752 | modrm_ea += c->regs[index_reg] << scale; |
922 | } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) { | 753 | } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) { |
923 | if (ctxt->mode == X86EMUL_MODE_PROT64) | 754 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
924 | c->rip_relative = 1; | 755 | c->rip_relative = 1; |
925 | } else | 756 | } else |
926 | c->modrm_ea += c->regs[c->modrm_rm]; | 757 | modrm_ea += c->regs[c->modrm_rm]; |
927 | switch (c->modrm_mod) { | 758 | switch (c->modrm_mod) { |
928 | case 0: | 759 | case 0: |
929 | if (c->modrm_rm == 5) | 760 | if (c->modrm_rm == 5) |
930 | c->modrm_ea += insn_fetch(s32, 4, c->eip); | 761 | modrm_ea += insn_fetch(s32, 4, c->eip); |
931 | break; | 762 | break; |
932 | case 1: | 763 | case 1: |
933 | c->modrm_ea += insn_fetch(s8, 1, c->eip); | 764 | modrm_ea += insn_fetch(s8, 1, c->eip); |
934 | break; | 765 | break; |
935 | case 2: | 766 | case 2: |
936 | c->modrm_ea += insn_fetch(s32, 4, c->eip); | 767 | modrm_ea += insn_fetch(s32, 4, c->eip); |
937 | break; | 768 | break; |
938 | } | 769 | } |
939 | } | 770 | } |
771 | op->addr.mem = modrm_ea; | ||
940 | done: | 772 | done: |
941 | return rc; | 773 | return rc; |
942 | } | 774 | } |
943 | 775 | ||
944 | static int decode_abs(struct x86_emulate_ctxt *ctxt, | 776 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
945 | struct x86_emulate_ops *ops) | 777 | struct x86_emulate_ops *ops, |
778 | struct operand *op) | ||
946 | { | 779 | { |
947 | struct decode_cache *c = &ctxt->decode; | 780 | struct decode_cache *c = &ctxt->decode; |
948 | int rc = X86EMUL_CONTINUE; | 781 | int rc = X86EMUL_CONTINUE; |
949 | 782 | ||
783 | op->type = OP_MEM; | ||
950 | switch (c->ad_bytes) { | 784 | switch (c->ad_bytes) { |
951 | case 2: | 785 | case 2: |
952 | c->modrm_ea = insn_fetch(u16, 2, c->eip); | 786 | op->addr.mem = insn_fetch(u16, 2, c->eip); |
953 | break; | 787 | break; |
954 | case 4: | 788 | case 4: |
955 | c->modrm_ea = insn_fetch(u32, 4, c->eip); | 789 | op->addr.mem = insn_fetch(u32, 4, c->eip); |
956 | break; | 790 | break; |
957 | case 8: | 791 | case 8: |
958 | c->modrm_ea = insn_fetch(u64, 8, c->eip); | 792 | op->addr.mem = insn_fetch(u64, 8, c->eip); |
959 | break; | 793 | break; |
960 | } | 794 | } |
961 | done: | 795 | done: |
962 | return rc; | 796 | return rc; |
963 | } | 797 | } |
964 | 798 | ||
965 | int | 799 | static void fetch_bit_operand(struct decode_cache *c) |
966 | x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | ||
967 | { | 800 | { |
968 | struct decode_cache *c = &ctxt->decode; | 801 | long sv = 0, mask; |
969 | int rc = X86EMUL_CONTINUE; | ||
970 | int mode = ctxt->mode; | ||
971 | int def_op_bytes, def_ad_bytes, group; | ||
972 | |||
973 | |||
974 | /* we cannot decode insn before we complete previous rep insn */ | ||
975 | WARN_ON(ctxt->restart); | ||
976 | |||
977 | c->eip = ctxt->eip; | ||
978 | c->fetch.start = c->fetch.end = c->eip; | ||
979 | ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); | ||
980 | |||
981 | switch (mode) { | ||
982 | case X86EMUL_MODE_REAL: | ||
983 | case X86EMUL_MODE_VM86: | ||
984 | case X86EMUL_MODE_PROT16: | ||
985 | def_op_bytes = def_ad_bytes = 2; | ||
986 | break; | ||
987 | case X86EMUL_MODE_PROT32: | ||
988 | def_op_bytes = def_ad_bytes = 4; | ||
989 | break; | ||
990 | #ifdef CONFIG_X86_64 | ||
991 | case X86EMUL_MODE_PROT64: | ||
992 | def_op_bytes = 4; | ||
993 | def_ad_bytes = 8; | ||
994 | break; | ||
995 | #endif | ||
996 | default: | ||
997 | return -1; | ||
998 | } | ||
999 | |||
1000 | c->op_bytes = def_op_bytes; | ||
1001 | c->ad_bytes = def_ad_bytes; | ||
1002 | |||
1003 | /* Legacy prefixes. */ | ||
1004 | for (;;) { | ||
1005 | switch (c->b = insn_fetch(u8, 1, c->eip)) { | ||
1006 | case 0x66: /* operand-size override */ | ||
1007 | /* switch between 2/4 bytes */ | ||
1008 | c->op_bytes = def_op_bytes ^ 6; | ||
1009 | break; | ||
1010 | case 0x67: /* address-size override */ | ||
1011 | if (mode == X86EMUL_MODE_PROT64) | ||
1012 | /* switch between 4/8 bytes */ | ||
1013 | c->ad_bytes = def_ad_bytes ^ 12; | ||
1014 | else | ||
1015 | /* switch between 2/4 bytes */ | ||
1016 | c->ad_bytes = def_ad_bytes ^ 6; | ||
1017 | break; | ||
1018 | case 0x26: /* ES override */ | ||
1019 | case 0x2e: /* CS override */ | ||
1020 | case 0x36: /* SS override */ | ||
1021 | case 0x3e: /* DS override */ | ||
1022 | set_seg_override(c, (c->b >> 3) & 3); | ||
1023 | break; | ||
1024 | case 0x64: /* FS override */ | ||
1025 | case 0x65: /* GS override */ | ||
1026 | set_seg_override(c, c->b & 7); | ||
1027 | break; | ||
1028 | case 0x40 ... 0x4f: /* REX */ | ||
1029 | if (mode != X86EMUL_MODE_PROT64) | ||
1030 | goto done_prefixes; | ||
1031 | c->rex_prefix = c->b; | ||
1032 | continue; | ||
1033 | case 0xf0: /* LOCK */ | ||
1034 | c->lock_prefix = 1; | ||
1035 | break; | ||
1036 | case 0xf2: /* REPNE/REPNZ */ | ||
1037 | c->rep_prefix = REPNE_PREFIX; | ||
1038 | break; | ||
1039 | case 0xf3: /* REP/REPE/REPZ */ | ||
1040 | c->rep_prefix = REPE_PREFIX; | ||
1041 | break; | ||
1042 | default: | ||
1043 | goto done_prefixes; | ||
1044 | } | ||
1045 | |||
1046 | /* Any legacy prefix after a REX prefix nullifies its effect. */ | ||
1047 | |||
1048 | c->rex_prefix = 0; | ||
1049 | } | ||
1050 | |||
1051 | done_prefixes: | ||
1052 | |||
1053 | /* REX prefix. */ | ||
1054 | if (c->rex_prefix) | ||
1055 | if (c->rex_prefix & 8) | ||
1056 | c->op_bytes = 8; /* REX.W */ | ||
1057 | |||
1058 | /* Opcode byte(s). */ | ||
1059 | c->d = opcode_table[c->b]; | ||
1060 | if (c->d == 0) { | ||
1061 | /* Two-byte opcode? */ | ||
1062 | if (c->b == 0x0f) { | ||
1063 | c->twobyte = 1; | ||
1064 | c->b = insn_fetch(u8, 1, c->eip); | ||
1065 | c->d = twobyte_table[c->b]; | ||
1066 | } | ||
1067 | } | ||
1068 | |||
1069 | if (c->d & Group) { | ||
1070 | group = c->d & GroupMask; | ||
1071 | c->modrm = insn_fetch(u8, 1, c->eip); | ||
1072 | --c->eip; | ||
1073 | |||
1074 | group = (group << 3) + ((c->modrm >> 3) & 7); | ||
1075 | if ((c->d & GroupDual) && (c->modrm >> 6) == 3) | ||
1076 | c->d = group2_table[group]; | ||
1077 | else | ||
1078 | c->d = group_table[group]; | ||
1079 | } | ||
1080 | |||
1081 | /* Unrecognised? */ | ||
1082 | if (c->d == 0) { | ||
1083 | DPRINTF("Cannot emulate %02x\n", c->b); | ||
1084 | return -1; | ||
1085 | } | ||
1086 | |||
1087 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) | ||
1088 | c->op_bytes = 8; | ||
1089 | |||
1090 | /* ModRM and SIB bytes. */ | ||
1091 | if (c->d & ModRM) | ||
1092 | rc = decode_modrm(ctxt, ops); | ||
1093 | else if (c->d & MemAbs) | ||
1094 | rc = decode_abs(ctxt, ops); | ||
1095 | if (rc != X86EMUL_CONTINUE) | ||
1096 | goto done; | ||
1097 | |||
1098 | if (!c->has_seg_override) | ||
1099 | set_seg_override(c, VCPU_SREG_DS); | ||
1100 | |||
1101 | if (!(!c->twobyte && c->b == 0x8d)) | ||
1102 | c->modrm_ea += seg_override_base(ctxt, ops, c); | ||
1103 | |||
1104 | if (c->ad_bytes != 8) | ||
1105 | c->modrm_ea = (u32)c->modrm_ea; | ||
1106 | |||
1107 | if (c->rip_relative) | ||
1108 | c->modrm_ea += c->eip; | ||
1109 | |||
1110 | /* | ||
1111 | * Decode and fetch the source operand: register, memory | ||
1112 | * or immediate. | ||
1113 | */ | ||
1114 | switch (c->d & SrcMask) { | ||
1115 | case SrcNone: | ||
1116 | break; | ||
1117 | case SrcReg: | ||
1118 | decode_register_operand(&c->src, c, 0); | ||
1119 | break; | ||
1120 | case SrcMem16: | ||
1121 | c->src.bytes = 2; | ||
1122 | goto srcmem_common; | ||
1123 | case SrcMem32: | ||
1124 | c->src.bytes = 4; | ||
1125 | goto srcmem_common; | ||
1126 | case SrcMem: | ||
1127 | c->src.bytes = (c->d & ByteOp) ? 1 : | ||
1128 | c->op_bytes; | ||
1129 | /* Don't fetch the address for invlpg: it could be unmapped. */ | ||
1130 | if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7) | ||
1131 | break; | ||
1132 | srcmem_common: | ||
1133 | /* | ||
1134 | * For instructions with a ModR/M byte, switch to register | ||
1135 | * access if Mod = 3. | ||
1136 | */ | ||
1137 | if ((c->d & ModRM) && c->modrm_mod == 3) { | ||
1138 | c->src.type = OP_REG; | ||
1139 | c->src.val = c->modrm_val; | ||
1140 | c->src.ptr = c->modrm_ptr; | ||
1141 | break; | ||
1142 | } | ||
1143 | c->src.type = OP_MEM; | ||
1144 | c->src.ptr = (unsigned long *)c->modrm_ea; | ||
1145 | c->src.val = 0; | ||
1146 | break; | ||
1147 | case SrcImm: | ||
1148 | case SrcImmU: | ||
1149 | c->src.type = OP_IMM; | ||
1150 | c->src.ptr = (unsigned long *)c->eip; | ||
1151 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1152 | if (c->src.bytes == 8) | ||
1153 | c->src.bytes = 4; | ||
1154 | /* NB. Immediates are sign-extended as necessary. */ | ||
1155 | switch (c->src.bytes) { | ||
1156 | case 1: | ||
1157 | c->src.val = insn_fetch(s8, 1, c->eip); | ||
1158 | break; | ||
1159 | case 2: | ||
1160 | c->src.val = insn_fetch(s16, 2, c->eip); | ||
1161 | break; | ||
1162 | case 4: | ||
1163 | c->src.val = insn_fetch(s32, 4, c->eip); | ||
1164 | break; | ||
1165 | } | ||
1166 | if ((c->d & SrcMask) == SrcImmU) { | ||
1167 | switch (c->src.bytes) { | ||
1168 | case 1: | ||
1169 | c->src.val &= 0xff; | ||
1170 | break; | ||
1171 | case 2: | ||
1172 | c->src.val &= 0xffff; | ||
1173 | break; | ||
1174 | case 4: | ||
1175 | c->src.val &= 0xffffffff; | ||
1176 | break; | ||
1177 | } | ||
1178 | } | ||
1179 | break; | ||
1180 | case SrcImmByte: | ||
1181 | case SrcImmUByte: | ||
1182 | c->src.type = OP_IMM; | ||
1183 | c->src.ptr = (unsigned long *)c->eip; | ||
1184 | c->src.bytes = 1; | ||
1185 | if ((c->d & SrcMask) == SrcImmByte) | ||
1186 | c->src.val = insn_fetch(s8, 1, c->eip); | ||
1187 | else | ||
1188 | c->src.val = insn_fetch(u8, 1, c->eip); | ||
1189 | break; | ||
1190 | case SrcAcc: | ||
1191 | c->src.type = OP_REG; | ||
1192 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1193 | c->src.ptr = &c->regs[VCPU_REGS_RAX]; | ||
1194 | switch (c->src.bytes) { | ||
1195 | case 1: | ||
1196 | c->src.val = *(u8 *)c->src.ptr; | ||
1197 | break; | ||
1198 | case 2: | ||
1199 | c->src.val = *(u16 *)c->src.ptr; | ||
1200 | break; | ||
1201 | case 4: | ||
1202 | c->src.val = *(u32 *)c->src.ptr; | ||
1203 | break; | ||
1204 | case 8: | ||
1205 | c->src.val = *(u64 *)c->src.ptr; | ||
1206 | break; | ||
1207 | } | ||
1208 | break; | ||
1209 | case SrcOne: | ||
1210 | c->src.bytes = 1; | ||
1211 | c->src.val = 1; | ||
1212 | break; | ||
1213 | case SrcSI: | ||
1214 | c->src.type = OP_MEM; | ||
1215 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1216 | c->src.ptr = (unsigned long *) | ||
1217 | register_address(c, seg_override_base(ctxt, ops, c), | ||
1218 | c->regs[VCPU_REGS_RSI]); | ||
1219 | c->src.val = 0; | ||
1220 | break; | ||
1221 | case SrcImmFAddr: | ||
1222 | c->src.type = OP_IMM; | ||
1223 | c->src.ptr = (unsigned long *)c->eip; | ||
1224 | c->src.bytes = c->op_bytes + 2; | ||
1225 | insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip); | ||
1226 | break; | ||
1227 | case SrcMemFAddr: | ||
1228 | c->src.type = OP_MEM; | ||
1229 | c->src.ptr = (unsigned long *)c->modrm_ea; | ||
1230 | c->src.bytes = c->op_bytes + 2; | ||
1231 | break; | ||
1232 | } | ||
1233 | 802 | ||
1234 | /* | 803 | if (c->dst.type == OP_MEM && c->src.type == OP_REG) { |
1235 | * Decode and fetch the second source operand: register, memory | 804 | mask = ~(c->dst.bytes * 8 - 1); |
1236 | * or immediate. | ||
1237 | */ | ||
1238 | switch (c->d & Src2Mask) { | ||
1239 | case Src2None: | ||
1240 | break; | ||
1241 | case Src2CL: | ||
1242 | c->src2.bytes = 1; | ||
1243 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; | ||
1244 | break; | ||
1245 | case Src2ImmByte: | ||
1246 | c->src2.type = OP_IMM; | ||
1247 | c->src2.ptr = (unsigned long *)c->eip; | ||
1248 | c->src2.bytes = 1; | ||
1249 | c->src2.val = insn_fetch(u8, 1, c->eip); | ||
1250 | break; | ||
1251 | case Src2One: | ||
1252 | c->src2.bytes = 1; | ||
1253 | c->src2.val = 1; | ||
1254 | break; | ||
1255 | } | ||
1256 | 805 | ||
1257 | /* Decode and fetch the destination operand: register or memory. */ | 806 | if (c->src.bytes == 2) |
1258 | switch (c->d & DstMask) { | 807 | sv = (s16)c->src.val & (s16)mask; |
1259 | case ImplicitOps: | 808 | else if (c->src.bytes == 4) |
1260 | /* Special instructions do their own operand decoding. */ | 809 | sv = (s32)c->src.val & (s32)mask; |
1261 | return 0; | ||
1262 | case DstReg: | ||
1263 | decode_register_operand(&c->dst, c, | ||
1264 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); | ||
1265 | break; | ||
1266 | case DstMem: | ||
1267 | case DstMem64: | ||
1268 | if ((c->d & ModRM) && c->modrm_mod == 3) { | ||
1269 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1270 | c->dst.type = OP_REG; | ||
1271 | c->dst.val = c->dst.orig_val = c->modrm_val; | ||
1272 | c->dst.ptr = c->modrm_ptr; | ||
1273 | break; | ||
1274 | } | ||
1275 | c->dst.type = OP_MEM; | ||
1276 | c->dst.ptr = (unsigned long *)c->modrm_ea; | ||
1277 | if ((c->d & DstMask) == DstMem64) | ||
1278 | c->dst.bytes = 8; | ||
1279 | else | ||
1280 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1281 | c->dst.val = 0; | ||
1282 | if (c->d & BitOp) { | ||
1283 | unsigned long mask = ~(c->dst.bytes * 8 - 1); | ||
1284 | 810 | ||
1285 | c->dst.ptr = (void *)c->dst.ptr + | 811 | c->dst.addr.mem += (sv >> 3); |
1286 | (c->src.val & mask) / 8; | ||
1287 | } | ||
1288 | break; | ||
1289 | case DstAcc: | ||
1290 | c->dst.type = OP_REG; | ||
1291 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1292 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; | ||
1293 | switch (c->dst.bytes) { | ||
1294 | case 1: | ||
1295 | c->dst.val = *(u8 *)c->dst.ptr; | ||
1296 | break; | ||
1297 | case 2: | ||
1298 | c->dst.val = *(u16 *)c->dst.ptr; | ||
1299 | break; | ||
1300 | case 4: | ||
1301 | c->dst.val = *(u32 *)c->dst.ptr; | ||
1302 | break; | ||
1303 | case 8: | ||
1304 | c->dst.val = *(u64 *)c->dst.ptr; | ||
1305 | break; | ||
1306 | } | ||
1307 | c->dst.orig_val = c->dst.val; | ||
1308 | break; | ||
1309 | case DstDI: | ||
1310 | c->dst.type = OP_MEM; | ||
1311 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
1312 | c->dst.ptr = (unsigned long *) | ||
1313 | register_address(c, es_base(ctxt, ops), | ||
1314 | c->regs[VCPU_REGS_RDI]); | ||
1315 | c->dst.val = 0; | ||
1316 | break; | ||
1317 | } | 812 | } |
1318 | 813 | ||
1319 | done: | 814 | /* only subword offset */ |
1320 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; | 815 | c->src.val &= (c->dst.bytes << 3) - 1; |
1321 | } | 816 | } |
1322 | 817 | ||
1323 | static int read_emulated(struct x86_emulate_ctxt *ctxt, | 818 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
@@ -1337,7 +832,7 @@ static int read_emulated(struct x86_emulate_ctxt *ctxt, | |||
1337 | rc = ops->read_emulated(addr, mc->data + mc->end, n, &err, | 832 | rc = ops->read_emulated(addr, mc->data + mc->end, n, &err, |
1338 | ctxt->vcpu); | 833 | ctxt->vcpu); |
1339 | if (rc == X86EMUL_PROPAGATE_FAULT) | 834 | if (rc == X86EMUL_PROPAGATE_FAULT) |
1340 | emulate_pf(ctxt, addr, err); | 835 | emulate_pf(ctxt); |
1341 | if (rc != X86EMUL_CONTINUE) | 836 | if (rc != X86EMUL_CONTINUE) |
1342 | return rc; | 837 | return rc; |
1343 | mc->end += n; | 838 | mc->end += n; |
@@ -1424,7 +919,7 @@ static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, | |||
1424 | addr = dt.address + index * 8; | 919 | addr = dt.address + index * 8; |
1425 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); | 920 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
1426 | if (ret == X86EMUL_PROPAGATE_FAULT) | 921 | if (ret == X86EMUL_PROPAGATE_FAULT) |
1427 | emulate_pf(ctxt, addr, err); | 922 | emulate_pf(ctxt); |
1428 | 923 | ||
1429 | return ret; | 924 | return ret; |
1430 | } | 925 | } |
@@ -1450,7 +945,7 @@ static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, | |||
1450 | addr = dt.address + index * 8; | 945 | addr = dt.address + index * 8; |
1451 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); | 946 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
1452 | if (ret == X86EMUL_PROPAGATE_FAULT) | 947 | if (ret == X86EMUL_PROPAGATE_FAULT) |
1453 | emulate_pf(ctxt, addr, err); | 948 | emulate_pf(ctxt); |
1454 | 949 | ||
1455 | return ret; | 950 | return ret; |
1456 | } | 951 | } |
@@ -1573,6 +1068,25 @@ exception: | |||
1573 | return X86EMUL_PROPAGATE_FAULT; | 1068 | return X86EMUL_PROPAGATE_FAULT; |
1574 | } | 1069 | } |
1575 | 1070 | ||
1071 | static void write_register_operand(struct operand *op) | ||
1072 | { | ||
1073 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ | ||
1074 | switch (op->bytes) { | ||
1075 | case 1: | ||
1076 | *(u8 *)op->addr.reg = (u8)op->val; | ||
1077 | break; | ||
1078 | case 2: | ||
1079 | *(u16 *)op->addr.reg = (u16)op->val; | ||
1080 | break; | ||
1081 | case 4: | ||
1082 | *op->addr.reg = (u32)op->val; | ||
1083 | break; /* 64b: zero-extend */ | ||
1084 | case 8: | ||
1085 | *op->addr.reg = op->val; | ||
1086 | break; | ||
1087 | } | ||
1088 | } | ||
1089 | |||
1576 | static inline int writeback(struct x86_emulate_ctxt *ctxt, | 1090 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
1577 | struct x86_emulate_ops *ops) | 1091 | struct x86_emulate_ops *ops) |
1578 | { | 1092 | { |
@@ -1582,28 +1096,12 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt, | |||
1582 | 1096 | ||
1583 | switch (c->dst.type) { | 1097 | switch (c->dst.type) { |
1584 | case OP_REG: | 1098 | case OP_REG: |
1585 | /* The 4-byte case *is* correct: | 1099 | write_register_operand(&c->dst); |
1586 | * in 64-bit mode we zero-extend. | ||
1587 | */ | ||
1588 | switch (c->dst.bytes) { | ||
1589 | case 1: | ||
1590 | *(u8 *)c->dst.ptr = (u8)c->dst.val; | ||
1591 | break; | ||
1592 | case 2: | ||
1593 | *(u16 *)c->dst.ptr = (u16)c->dst.val; | ||
1594 | break; | ||
1595 | case 4: | ||
1596 | *c->dst.ptr = (u32)c->dst.val; | ||
1597 | break; /* 64b: zero-ext */ | ||
1598 | case 8: | ||
1599 | *c->dst.ptr = c->dst.val; | ||
1600 | break; | ||
1601 | } | ||
1602 | break; | 1100 | break; |
1603 | case OP_MEM: | 1101 | case OP_MEM: |
1604 | if (c->lock_prefix) | 1102 | if (c->lock_prefix) |
1605 | rc = ops->cmpxchg_emulated( | 1103 | rc = ops->cmpxchg_emulated( |
1606 | (unsigned long)c->dst.ptr, | 1104 | c->dst.addr.mem, |
1607 | &c->dst.orig_val, | 1105 | &c->dst.orig_val, |
1608 | &c->dst.val, | 1106 | &c->dst.val, |
1609 | c->dst.bytes, | 1107 | c->dst.bytes, |
@@ -1611,14 +1109,13 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt, | |||
1611 | ctxt->vcpu); | 1109 | ctxt->vcpu); |
1612 | else | 1110 | else |
1613 | rc = ops->write_emulated( | 1111 | rc = ops->write_emulated( |
1614 | (unsigned long)c->dst.ptr, | 1112 | c->dst.addr.mem, |
1615 | &c->dst.val, | 1113 | &c->dst.val, |
1616 | c->dst.bytes, | 1114 | c->dst.bytes, |
1617 | &err, | 1115 | &err, |
1618 | ctxt->vcpu); | 1116 | ctxt->vcpu); |
1619 | if (rc == X86EMUL_PROPAGATE_FAULT) | 1117 | if (rc == X86EMUL_PROPAGATE_FAULT) |
1620 | emulate_pf(ctxt, | 1118 | emulate_pf(ctxt); |
1621 | (unsigned long)c->dst.ptr, err); | ||
1622 | if (rc != X86EMUL_CONTINUE) | 1119 | if (rc != X86EMUL_CONTINUE) |
1623 | return rc; | 1120 | return rc; |
1624 | break; | 1121 | break; |
@@ -1640,8 +1137,8 @@ static inline void emulate_push(struct x86_emulate_ctxt *ctxt, | |||
1640 | c->dst.bytes = c->op_bytes; | 1137 | c->dst.bytes = c->op_bytes; |
1641 | c->dst.val = c->src.val; | 1138 | c->dst.val = c->src.val; |
1642 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); | 1139 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
1643 | c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops), | 1140 | c->dst.addr.mem = register_address(c, ss_base(ctxt, ops), |
1644 | c->regs[VCPU_REGS_RSP]); | 1141 | c->regs[VCPU_REGS_RSP]); |
1645 | } | 1142 | } |
1646 | 1143 | ||
1647 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, | 1144 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
@@ -1701,6 +1198,9 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt, | |||
1701 | *(unsigned long *)dest = | 1198 | *(unsigned long *)dest = |
1702 | (ctxt->eflags & ~change_mask) | (val & change_mask); | 1199 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
1703 | 1200 | ||
1201 | if (rc == X86EMUL_PROPAGATE_FAULT) | ||
1202 | emulate_pf(ctxt); | ||
1203 | |||
1704 | return rc; | 1204 | return rc; |
1705 | } | 1205 | } |
1706 | 1206 | ||
@@ -1778,6 +1278,150 @@ static int emulate_popa(struct x86_emulate_ctxt *ctxt, | |||
1778 | return rc; | 1278 | return rc; |
1779 | } | 1279 | } |
1780 | 1280 | ||
1281 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, | ||
1282 | struct x86_emulate_ops *ops, int irq) | ||
1283 | { | ||
1284 | struct decode_cache *c = &ctxt->decode; | ||
1285 | int rc; | ||
1286 | struct desc_ptr dt; | ||
1287 | gva_t cs_addr; | ||
1288 | gva_t eip_addr; | ||
1289 | u16 cs, eip; | ||
1290 | u32 err; | ||
1291 | |||
1292 | /* TODO: Add limit checks */ | ||
1293 | c->src.val = ctxt->eflags; | ||
1294 | emulate_push(ctxt, ops); | ||
1295 | rc = writeback(ctxt, ops); | ||
1296 | if (rc != X86EMUL_CONTINUE) | ||
1297 | return rc; | ||
1298 | |||
1299 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); | ||
1300 | |||
1301 | c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); | ||
1302 | emulate_push(ctxt, ops); | ||
1303 | rc = writeback(ctxt, ops); | ||
1304 | if (rc != X86EMUL_CONTINUE) | ||
1305 | return rc; | ||
1306 | |||
1307 | c->src.val = c->eip; | ||
1308 | emulate_push(ctxt, ops); | ||
1309 | rc = writeback(ctxt, ops); | ||
1310 | if (rc != X86EMUL_CONTINUE) | ||
1311 | return rc; | ||
1312 | |||
1313 | c->dst.type = OP_NONE; | ||
1314 | |||
1315 | ops->get_idt(&dt, ctxt->vcpu); | ||
1316 | |||
1317 | eip_addr = dt.address + (irq << 2); | ||
1318 | cs_addr = dt.address + (irq << 2) + 2; | ||
1319 | |||
1320 | rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err); | ||
1321 | if (rc != X86EMUL_CONTINUE) | ||
1322 | return rc; | ||
1323 | |||
1324 | rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err); | ||
1325 | if (rc != X86EMUL_CONTINUE) | ||
1326 | return rc; | ||
1327 | |||
1328 | rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS); | ||
1329 | if (rc != X86EMUL_CONTINUE) | ||
1330 | return rc; | ||
1331 | |||
1332 | c->eip = eip; | ||
1333 | |||
1334 | return rc; | ||
1335 | } | ||
1336 | |||
1337 | static int emulate_int(struct x86_emulate_ctxt *ctxt, | ||
1338 | struct x86_emulate_ops *ops, int irq) | ||
1339 | { | ||
1340 | switch(ctxt->mode) { | ||
1341 | case X86EMUL_MODE_REAL: | ||
1342 | return emulate_int_real(ctxt, ops, irq); | ||
1343 | case X86EMUL_MODE_VM86: | ||
1344 | case X86EMUL_MODE_PROT16: | ||
1345 | case X86EMUL_MODE_PROT32: | ||
1346 | case X86EMUL_MODE_PROT64: | ||
1347 | default: | ||
1348 | /* Protected mode interrupts unimplemented yet */ | ||
1349 | return X86EMUL_UNHANDLEABLE; | ||
1350 | } | ||
1351 | } | ||
1352 | |||
1353 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt, | ||
1354 | struct x86_emulate_ops *ops) | ||
1355 | { | ||
1356 | struct decode_cache *c = &ctxt->decode; | ||
1357 | int rc = X86EMUL_CONTINUE; | ||
1358 | unsigned long temp_eip = 0; | ||
1359 | unsigned long temp_eflags = 0; | ||
1360 | unsigned long cs = 0; | ||
1361 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | | ||
1362 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | | ||
1363 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ | ||
1364 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; | ||
1365 | |||
1366 | /* TODO: Add stack limit check */ | ||
1367 | |||
1368 | rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes); | ||
1369 | |||
1370 | if (rc != X86EMUL_CONTINUE) | ||
1371 | return rc; | ||
1372 | |||
1373 | if (temp_eip & ~0xffff) { | ||
1374 | emulate_gp(ctxt, 0); | ||
1375 | return X86EMUL_PROPAGATE_FAULT; | ||
1376 | } | ||
1377 | |||
1378 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); | ||
1379 | |||
1380 | if (rc != X86EMUL_CONTINUE) | ||
1381 | return rc; | ||
1382 | |||
1383 | rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes); | ||
1384 | |||
1385 | if (rc != X86EMUL_CONTINUE) | ||
1386 | return rc; | ||
1387 | |||
1388 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); | ||
1389 | |||
1390 | if (rc != X86EMUL_CONTINUE) | ||
1391 | return rc; | ||
1392 | |||
1393 | c->eip = temp_eip; | ||
1394 | |||
1395 | |||
1396 | if (c->op_bytes == 4) | ||
1397 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); | ||
1398 | else if (c->op_bytes == 2) { | ||
1399 | ctxt->eflags &= ~0xffff; | ||
1400 | ctxt->eflags |= temp_eflags; | ||
1401 | } | ||
1402 | |||
1403 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ | ||
1404 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; | ||
1405 | |||
1406 | return rc; | ||
1407 | } | ||
1408 | |||
1409 | static inline int emulate_iret(struct x86_emulate_ctxt *ctxt, | ||
1410 | struct x86_emulate_ops* ops) | ||
1411 | { | ||
1412 | switch(ctxt->mode) { | ||
1413 | case X86EMUL_MODE_REAL: | ||
1414 | return emulate_iret_real(ctxt, ops); | ||
1415 | case X86EMUL_MODE_VM86: | ||
1416 | case X86EMUL_MODE_PROT16: | ||
1417 | case X86EMUL_MODE_PROT32: | ||
1418 | case X86EMUL_MODE_PROT64: | ||
1419 | default: | ||
1420 | /* iret from protected mode unimplemented yet */ | ||
1421 | return X86EMUL_UNHANDLEABLE; | ||
1422 | } | ||
1423 | } | ||
1424 | |||
1781 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, | 1425 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
1782 | struct x86_emulate_ops *ops) | 1426 | struct x86_emulate_ops *ops) |
1783 | { | 1427 | { |
@@ -1819,6 +1463,9 @@ static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, | |||
1819 | struct x86_emulate_ops *ops) | 1463 | struct x86_emulate_ops *ops) |
1820 | { | 1464 | { |
1821 | struct decode_cache *c = &ctxt->decode; | 1465 | struct decode_cache *c = &ctxt->decode; |
1466 | unsigned long *rax = &c->regs[VCPU_REGS_RAX]; | ||
1467 | unsigned long *rdx = &c->regs[VCPU_REGS_RDX]; | ||
1468 | u8 de = 0; | ||
1822 | 1469 | ||
1823 | switch (c->modrm_reg) { | 1470 | switch (c->modrm_reg) { |
1824 | case 0 ... 1: /* test */ | 1471 | case 0 ... 1: /* test */ |
@@ -1830,10 +1477,26 @@ static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, | |||
1830 | case 3: /* neg */ | 1477 | case 3: /* neg */ |
1831 | emulate_1op("neg", c->dst, ctxt->eflags); | 1478 | emulate_1op("neg", c->dst, ctxt->eflags); |
1832 | break; | 1479 | break; |
1480 | case 4: /* mul */ | ||
1481 | emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags); | ||
1482 | break; | ||
1483 | case 5: /* imul */ | ||
1484 | emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags); | ||
1485 | break; | ||
1486 | case 6: /* div */ | ||
1487 | emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx, | ||
1488 | ctxt->eflags, de); | ||
1489 | break; | ||
1490 | case 7: /* idiv */ | ||
1491 | emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx, | ||
1492 | ctxt->eflags, de); | ||
1493 | break; | ||
1833 | default: | 1494 | default: |
1834 | return 0; | 1495 | return X86EMUL_UNHANDLEABLE; |
1835 | } | 1496 | } |
1836 | return 1; | 1497 | if (de) |
1498 | return emulate_de(ctxt); | ||
1499 | return X86EMUL_CONTINUE; | ||
1837 | } | 1500 | } |
1838 | 1501 | ||
1839 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, | 1502 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
@@ -1905,6 +1568,23 @@ static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, | |||
1905 | return rc; | 1568 | return rc; |
1906 | } | 1569 | } |
1907 | 1570 | ||
1571 | static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, | ||
1572 | struct x86_emulate_ops *ops, int seg) | ||
1573 | { | ||
1574 | struct decode_cache *c = &ctxt->decode; | ||
1575 | unsigned short sel; | ||
1576 | int rc; | ||
1577 | |||
1578 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); | ||
1579 | |||
1580 | rc = load_segment_descriptor(ctxt, ops, sel, seg); | ||
1581 | if (rc != X86EMUL_CONTINUE) | ||
1582 | return rc; | ||
1583 | |||
1584 | c->dst.val = c->src.val; | ||
1585 | return rc; | ||
1586 | } | ||
1587 | |||
1908 | static inline void | 1588 | static inline void |
1909 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, | 1589 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
1910 | struct x86_emulate_ops *ops, struct desc_struct *cs, | 1590 | struct x86_emulate_ops *ops, struct desc_struct *cs, |
@@ -2160,9 +1840,15 @@ static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, | |||
2160 | struct x86_emulate_ops *ops, | 1840 | struct x86_emulate_ops *ops, |
2161 | u16 port, u16 len) | 1841 | u16 port, u16 len) |
2162 | { | 1842 | { |
1843 | if (ctxt->perm_ok) | ||
1844 | return true; | ||
1845 | |||
2163 | if (emulator_bad_iopl(ctxt, ops)) | 1846 | if (emulator_bad_iopl(ctxt, ops)) |
2164 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) | 1847 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
2165 | return false; | 1848 | return false; |
1849 | |||
1850 | ctxt->perm_ok = true; | ||
1851 | |||
2166 | return true; | 1852 | return true; |
2167 | } | 1853 | } |
2168 | 1854 | ||
@@ -2254,7 +1940,7 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, | |||
2254 | &err); | 1940 | &err); |
2255 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 1941 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2256 | /* FIXME: need to provide precise fault address */ | 1942 | /* FIXME: need to provide precise fault address */ |
2257 | emulate_pf(ctxt, old_tss_base, err); | 1943 | emulate_pf(ctxt); |
2258 | return ret; | 1944 | return ret; |
2259 | } | 1945 | } |
2260 | 1946 | ||
@@ -2264,7 +1950,7 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, | |||
2264 | &err); | 1950 | &err); |
2265 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 1951 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2266 | /* FIXME: need to provide precise fault address */ | 1952 | /* FIXME: need to provide precise fault address */ |
2267 | emulate_pf(ctxt, old_tss_base, err); | 1953 | emulate_pf(ctxt); |
2268 | return ret; | 1954 | return ret; |
2269 | } | 1955 | } |
2270 | 1956 | ||
@@ -2272,7 +1958,7 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, | |||
2272 | &err); | 1958 | &err); |
2273 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 1959 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2274 | /* FIXME: need to provide precise fault address */ | 1960 | /* FIXME: need to provide precise fault address */ |
2275 | emulate_pf(ctxt, new_tss_base, err); | 1961 | emulate_pf(ctxt); |
2276 | return ret; | 1962 | return ret; |
2277 | } | 1963 | } |
2278 | 1964 | ||
@@ -2285,7 +1971,7 @@ static int task_switch_16(struct x86_emulate_ctxt *ctxt, | |||
2285 | ctxt->vcpu, &err); | 1971 | ctxt->vcpu, &err); |
2286 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 1972 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2287 | /* FIXME: need to provide precise fault address */ | 1973 | /* FIXME: need to provide precise fault address */ |
2288 | emulate_pf(ctxt, new_tss_base, err); | 1974 | emulate_pf(ctxt); |
2289 | return ret; | 1975 | return ret; |
2290 | } | 1976 | } |
2291 | } | 1977 | } |
@@ -2396,7 +2082,7 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, | |||
2396 | &err); | 2082 | &err); |
2397 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 2083 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2398 | /* FIXME: need to provide precise fault address */ | 2084 | /* FIXME: need to provide precise fault address */ |
2399 | emulate_pf(ctxt, old_tss_base, err); | 2085 | emulate_pf(ctxt); |
2400 | return ret; | 2086 | return ret; |
2401 | } | 2087 | } |
2402 | 2088 | ||
@@ -2406,7 +2092,7 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, | |||
2406 | &err); | 2092 | &err); |
2407 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 2093 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2408 | /* FIXME: need to provide precise fault address */ | 2094 | /* FIXME: need to provide precise fault address */ |
2409 | emulate_pf(ctxt, old_tss_base, err); | 2095 | emulate_pf(ctxt); |
2410 | return ret; | 2096 | return ret; |
2411 | } | 2097 | } |
2412 | 2098 | ||
@@ -2414,7 +2100,7 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, | |||
2414 | &err); | 2100 | &err); |
2415 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 2101 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2416 | /* FIXME: need to provide precise fault address */ | 2102 | /* FIXME: need to provide precise fault address */ |
2417 | emulate_pf(ctxt, new_tss_base, err); | 2103 | emulate_pf(ctxt); |
2418 | return ret; | 2104 | return ret; |
2419 | } | 2105 | } |
2420 | 2106 | ||
@@ -2427,7 +2113,7 @@ static int task_switch_32(struct x86_emulate_ctxt *ctxt, | |||
2427 | ctxt->vcpu, &err); | 2113 | ctxt->vcpu, &err); |
2428 | if (ret == X86EMUL_PROPAGATE_FAULT) { | 2114 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2429 | /* FIXME: need to provide precise fault address */ | 2115 | /* FIXME: need to provide precise fault address */ |
2430 | emulate_pf(ctxt, new_tss_base, err); | 2116 | emulate_pf(ctxt); |
2431 | return ret; | 2117 | return ret; |
2432 | } | 2118 | } |
2433 | } | 2119 | } |
@@ -2523,10 +2209,10 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, | |||
2523 | } | 2209 | } |
2524 | 2210 | ||
2525 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, | 2211 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
2526 | struct x86_emulate_ops *ops, | ||
2527 | u16 tss_selector, int reason, | 2212 | u16 tss_selector, int reason, |
2528 | bool has_error_code, u32 error_code) | 2213 | bool has_error_code, u32 error_code) |
2529 | { | 2214 | { |
2215 | struct x86_emulate_ops *ops = ctxt->ops; | ||
2530 | struct decode_cache *c = &ctxt->decode; | 2216 | struct decode_cache *c = &ctxt->decode; |
2531 | int rc; | 2217 | int rc; |
2532 | 2218 | ||
@@ -2552,16 +2238,784 @@ static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base, | |||
2552 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; | 2238 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; |
2553 | 2239 | ||
2554 | register_address_increment(c, &c->regs[reg], df * op->bytes); | 2240 | register_address_increment(c, &c->regs[reg], df * op->bytes); |
2555 | op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]); | 2241 | op->addr.mem = register_address(c, base, c->regs[reg]); |
2242 | } | ||
2243 | |||
2244 | static int em_push(struct x86_emulate_ctxt *ctxt) | ||
2245 | { | ||
2246 | emulate_push(ctxt, ctxt->ops); | ||
2247 | return X86EMUL_CONTINUE; | ||
2248 | } | ||
2249 | |||
2250 | static int em_das(struct x86_emulate_ctxt *ctxt) | ||
2251 | { | ||
2252 | struct decode_cache *c = &ctxt->decode; | ||
2253 | u8 al, old_al; | ||
2254 | bool af, cf, old_cf; | ||
2255 | |||
2256 | cf = ctxt->eflags & X86_EFLAGS_CF; | ||
2257 | al = c->dst.val; | ||
2258 | |||
2259 | old_al = al; | ||
2260 | old_cf = cf; | ||
2261 | cf = false; | ||
2262 | af = ctxt->eflags & X86_EFLAGS_AF; | ||
2263 | if ((al & 0x0f) > 9 || af) { | ||
2264 | al -= 6; | ||
2265 | cf = old_cf | (al >= 250); | ||
2266 | af = true; | ||
2267 | } else { | ||
2268 | af = false; | ||
2269 | } | ||
2270 | if (old_al > 0x99 || old_cf) { | ||
2271 | al -= 0x60; | ||
2272 | cf = true; | ||
2273 | } | ||
2274 | |||
2275 | c->dst.val = al; | ||
2276 | /* Set PF, ZF, SF */ | ||
2277 | c->src.type = OP_IMM; | ||
2278 | c->src.val = 0; | ||
2279 | c->src.bytes = 1; | ||
2280 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); | ||
2281 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); | ||
2282 | if (cf) | ||
2283 | ctxt->eflags |= X86_EFLAGS_CF; | ||
2284 | if (af) | ||
2285 | ctxt->eflags |= X86_EFLAGS_AF; | ||
2286 | return X86EMUL_CONTINUE; | ||
2287 | } | ||
2288 | |||
2289 | static int em_call_far(struct x86_emulate_ctxt *ctxt) | ||
2290 | { | ||
2291 | struct decode_cache *c = &ctxt->decode; | ||
2292 | u16 sel, old_cs; | ||
2293 | ulong old_eip; | ||
2294 | int rc; | ||
2295 | |||
2296 | old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); | ||
2297 | old_eip = c->eip; | ||
2298 | |||
2299 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); | ||
2300 | if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS)) | ||
2301 | return X86EMUL_CONTINUE; | ||
2302 | |||
2303 | c->eip = 0; | ||
2304 | memcpy(&c->eip, c->src.valptr, c->op_bytes); | ||
2305 | |||
2306 | c->src.val = old_cs; | ||
2307 | emulate_push(ctxt, ctxt->ops); | ||
2308 | rc = writeback(ctxt, ctxt->ops); | ||
2309 | if (rc != X86EMUL_CONTINUE) | ||
2310 | return rc; | ||
2311 | |||
2312 | c->src.val = old_eip; | ||
2313 | emulate_push(ctxt, ctxt->ops); | ||
2314 | rc = writeback(ctxt, ctxt->ops); | ||
2315 | if (rc != X86EMUL_CONTINUE) | ||
2316 | return rc; | ||
2317 | |||
2318 | c->dst.type = OP_NONE; | ||
2319 | |||
2320 | return X86EMUL_CONTINUE; | ||
2321 | } | ||
2322 | |||
2323 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) | ||
2324 | { | ||
2325 | struct decode_cache *c = &ctxt->decode; | ||
2326 | int rc; | ||
2327 | |||
2328 | c->dst.type = OP_REG; | ||
2329 | c->dst.addr.reg = &c->eip; | ||
2330 | c->dst.bytes = c->op_bytes; | ||
2331 | rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes); | ||
2332 | if (rc != X86EMUL_CONTINUE) | ||
2333 | return rc; | ||
2334 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val); | ||
2335 | return X86EMUL_CONTINUE; | ||
2336 | } | ||
2337 | |||
2338 | static int em_imul(struct x86_emulate_ctxt *ctxt) | ||
2339 | { | ||
2340 | struct decode_cache *c = &ctxt->decode; | ||
2341 | |||
2342 | emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags); | ||
2343 | return X86EMUL_CONTINUE; | ||
2344 | } | ||
2345 | |||
2346 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) | ||
2347 | { | ||
2348 | struct decode_cache *c = &ctxt->decode; | ||
2349 | |||
2350 | c->dst.val = c->src2.val; | ||
2351 | return em_imul(ctxt); | ||
2352 | } | ||
2353 | |||
2354 | static int em_cwd(struct x86_emulate_ctxt *ctxt) | ||
2355 | { | ||
2356 | struct decode_cache *c = &ctxt->decode; | ||
2357 | |||
2358 | c->dst.type = OP_REG; | ||
2359 | c->dst.bytes = c->src.bytes; | ||
2360 | c->dst.addr.reg = &c->regs[VCPU_REGS_RDX]; | ||
2361 | c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1); | ||
2362 | |||
2363 | return X86EMUL_CONTINUE; | ||
2364 | } | ||
2365 | |||
2366 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) | ||
2367 | { | ||
2368 | unsigned cpl = ctxt->ops->cpl(ctxt->vcpu); | ||
2369 | struct decode_cache *c = &ctxt->decode; | ||
2370 | u64 tsc = 0; | ||
2371 | |||
2372 | if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) { | ||
2373 | emulate_gp(ctxt, 0); | ||
2374 | return X86EMUL_PROPAGATE_FAULT; | ||
2375 | } | ||
2376 | ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc); | ||
2377 | c->regs[VCPU_REGS_RAX] = (u32)tsc; | ||
2378 | c->regs[VCPU_REGS_RDX] = tsc >> 32; | ||
2379 | return X86EMUL_CONTINUE; | ||
2380 | } | ||
2381 | |||
2382 | static int em_mov(struct x86_emulate_ctxt *ctxt) | ||
2383 | { | ||
2384 | struct decode_cache *c = &ctxt->decode; | ||
2385 | c->dst.val = c->src.val; | ||
2386 | return X86EMUL_CONTINUE; | ||
2387 | } | ||
2388 | |||
2389 | #define D(_y) { .flags = (_y) } | ||
2390 | #define N D(0) | ||
2391 | #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) } | ||
2392 | #define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) } | ||
2393 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } | ||
2394 | |||
2395 | #define D2bv(_f) D((_f) | ByteOp), D(_f) | ||
2396 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) | ||
2397 | |||
2398 | #define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \ | ||
2399 | D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \ | ||
2400 | D2bv(((_f) & ~Lock) | DstAcc | SrcImm) | ||
2401 | |||
2402 | |||
2403 | static struct opcode group1[] = { | ||
2404 | X7(D(Lock)), N | ||
2405 | }; | ||
2406 | |||
2407 | static struct opcode group1A[] = { | ||
2408 | D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N, | ||
2409 | }; | ||
2410 | |||
2411 | static struct opcode group3[] = { | ||
2412 | D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM), | ||
2413 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), | ||
2414 | X4(D(SrcMem | ModRM)), | ||
2415 | }; | ||
2416 | |||
2417 | static struct opcode group4[] = { | ||
2418 | D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock), | ||
2419 | N, N, N, N, N, N, | ||
2420 | }; | ||
2421 | |||
2422 | static struct opcode group5[] = { | ||
2423 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), | ||
2424 | D(SrcMem | ModRM | Stack), | ||
2425 | I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far), | ||
2426 | D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps), | ||
2427 | D(SrcMem | ModRM | Stack), N, | ||
2428 | }; | ||
2429 | |||
2430 | static struct group_dual group7 = { { | ||
2431 | N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv), | ||
2432 | D(SrcNone | ModRM | DstMem | Mov), N, | ||
2433 | D(SrcMem16 | ModRM | Mov | Priv), | ||
2434 | D(SrcMem | ModRM | ByteOp | Priv | NoAccess), | ||
2435 | }, { | ||
2436 | D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv), | ||
2437 | D(SrcNone | ModRM | DstMem | Mov), N, | ||
2438 | D(SrcMem16 | ModRM | Mov | Priv), N, | ||
2439 | } }; | ||
2440 | |||
2441 | static struct opcode group8[] = { | ||
2442 | N, N, N, N, | ||
2443 | D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock), | ||
2444 | D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock), | ||
2445 | }; | ||
2446 | |||
2447 | static struct group_dual group9 = { { | ||
2448 | N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N, | ||
2449 | }, { | ||
2450 | N, N, N, N, N, N, N, N, | ||
2451 | } }; | ||
2452 | |||
2453 | static struct opcode group11[] = { | ||
2454 | I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)), | ||
2455 | }; | ||
2456 | |||
2457 | static struct opcode opcode_table[256] = { | ||
2458 | /* 0x00 - 0x07 */ | ||
2459 | D6ALU(Lock), | ||
2460 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), | ||
2461 | /* 0x08 - 0x0F */ | ||
2462 | D6ALU(Lock), | ||
2463 | D(ImplicitOps | Stack | No64), N, | ||
2464 | /* 0x10 - 0x17 */ | ||
2465 | D6ALU(Lock), | ||
2466 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), | ||
2467 | /* 0x18 - 0x1F */ | ||
2468 | D6ALU(Lock), | ||
2469 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), | ||
2470 | /* 0x20 - 0x27 */ | ||
2471 | D6ALU(Lock), N, N, | ||
2472 | /* 0x28 - 0x2F */ | ||
2473 | D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das), | ||
2474 | /* 0x30 - 0x37 */ | ||
2475 | D6ALU(Lock), N, N, | ||
2476 | /* 0x38 - 0x3F */ | ||
2477 | D6ALU(0), N, N, | ||
2478 | /* 0x40 - 0x4F */ | ||
2479 | X16(D(DstReg)), | ||
2480 | /* 0x50 - 0x57 */ | ||
2481 | X8(I(SrcReg | Stack, em_push)), | ||
2482 | /* 0x58 - 0x5F */ | ||
2483 | X8(D(DstReg | Stack)), | ||
2484 | /* 0x60 - 0x67 */ | ||
2485 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), | ||
2486 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , | ||
2487 | N, N, N, N, | ||
2488 | /* 0x68 - 0x6F */ | ||
2489 | I(SrcImm | Mov | Stack, em_push), | ||
2490 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), | ||
2491 | I(SrcImmByte | Mov | Stack, em_push), | ||
2492 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), | ||
2493 | D2bv(DstDI | Mov | String), /* insb, insw/insd */ | ||
2494 | D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */ | ||
2495 | /* 0x70 - 0x7F */ | ||
2496 | X16(D(SrcImmByte)), | ||
2497 | /* 0x80 - 0x87 */ | ||
2498 | G(ByteOp | DstMem | SrcImm | ModRM | Group, group1), | ||
2499 | G(DstMem | SrcImm | ModRM | Group, group1), | ||
2500 | G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1), | ||
2501 | G(DstMem | SrcImmByte | ModRM | Group, group1), | ||
2502 | D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock), | ||
2503 | /* 0x88 - 0x8F */ | ||
2504 | I2bv(DstMem | SrcReg | ModRM | Mov, em_mov), | ||
2505 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), | ||
2506 | D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg), | ||
2507 | D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A), | ||
2508 | /* 0x90 - 0x97 */ | ||
2509 | X8(D(SrcAcc | DstReg)), | ||
2510 | /* 0x98 - 0x9F */ | ||
2511 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), | ||
2512 | I(SrcImmFAddr | No64, em_call_far), N, | ||
2513 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N, | ||
2514 | /* 0xA0 - 0xA7 */ | ||
2515 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), | ||
2516 | I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov), | ||
2517 | I2bv(SrcSI | DstDI | Mov | String, em_mov), | ||
2518 | D2bv(SrcSI | DstDI | String), | ||
2519 | /* 0xA8 - 0xAF */ | ||
2520 | D2bv(DstAcc | SrcImm), | ||
2521 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), | ||
2522 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), | ||
2523 | D2bv(SrcAcc | DstDI | String), | ||
2524 | /* 0xB0 - 0xB7 */ | ||
2525 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), | ||
2526 | /* 0xB8 - 0xBF */ | ||
2527 | X8(I(DstReg | SrcImm | Mov, em_mov)), | ||
2528 | /* 0xC0 - 0xC7 */ | ||
2529 | D2bv(DstMem | SrcImmByte | ModRM), | ||
2530 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), | ||
2531 | D(ImplicitOps | Stack), | ||
2532 | D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64), | ||
2533 | G(ByteOp, group11), G(0, group11), | ||
2534 | /* 0xC8 - 0xCF */ | ||
2535 | N, N, N, D(ImplicitOps | Stack), | ||
2536 | D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps), | ||
2537 | /* 0xD0 - 0xD7 */ | ||
2538 | D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM), | ||
2539 | N, N, N, N, | ||
2540 | /* 0xD8 - 0xDF */ | ||
2541 | N, N, N, N, N, N, N, N, | ||
2542 | /* 0xE0 - 0xE7 */ | ||
2543 | X4(D(SrcImmByte)), | ||
2544 | D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte), | ||
2545 | /* 0xE8 - 0xEF */ | ||
2546 | D(SrcImm | Stack), D(SrcImm | ImplicitOps), | ||
2547 | D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps), | ||
2548 | D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps), | ||
2549 | /* 0xF0 - 0xF7 */ | ||
2550 | N, N, N, N, | ||
2551 | D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3), | ||
2552 | /* 0xF8 - 0xFF */ | ||
2553 | D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), | ||
2554 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), | ||
2555 | }; | ||
2556 | |||
2557 | static struct opcode twobyte_table[256] = { | ||
2558 | /* 0x00 - 0x0F */ | ||
2559 | N, GD(0, &group7), N, N, | ||
2560 | N, D(ImplicitOps), D(ImplicitOps | Priv), N, | ||
2561 | D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N, | ||
2562 | N, D(ImplicitOps | ModRM), N, N, | ||
2563 | /* 0x10 - 0x1F */ | ||
2564 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, | ||
2565 | /* 0x20 - 0x2F */ | ||
2566 | D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264), | ||
2567 | D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264), | ||
2568 | N, N, N, N, | ||
2569 | N, N, N, N, N, N, N, N, | ||
2570 | /* 0x30 - 0x3F */ | ||
2571 | D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc), | ||
2572 | D(ImplicitOps | Priv), N, | ||
2573 | D(ImplicitOps), D(ImplicitOps | Priv), N, N, | ||
2574 | N, N, N, N, N, N, N, N, | ||
2575 | /* 0x40 - 0x4F */ | ||
2576 | X16(D(DstReg | SrcMem | ModRM | Mov)), | ||
2577 | /* 0x50 - 0x5F */ | ||
2578 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | ||
2579 | /* 0x60 - 0x6F */ | ||
2580 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | ||
2581 | /* 0x70 - 0x7F */ | ||
2582 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | ||
2583 | /* 0x80 - 0x8F */ | ||
2584 | X16(D(SrcImm)), | ||
2585 | /* 0x90 - 0x9F */ | ||
2586 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), | ||
2587 | /* 0xA0 - 0xA7 */ | ||
2588 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), | ||
2589 | N, D(DstMem | SrcReg | ModRM | BitOp), | ||
2590 | D(DstMem | SrcReg | Src2ImmByte | ModRM), | ||
2591 | D(DstMem | SrcReg | Src2CL | ModRM), N, N, | ||
2592 | /* 0xA8 - 0xAF */ | ||
2593 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), | ||
2594 | N, D(DstMem | SrcReg | ModRM | BitOp | Lock), | ||
2595 | D(DstMem | SrcReg | Src2ImmByte | ModRM), | ||
2596 | D(DstMem | SrcReg | Src2CL | ModRM), | ||
2597 | D(ModRM), I(DstReg | SrcMem | ModRM, em_imul), | ||
2598 | /* 0xB0 - 0xB7 */ | ||
2599 | D2bv(DstMem | SrcReg | ModRM | Lock), | ||
2600 | D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock), | ||
2601 | D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM), | ||
2602 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), | ||
2603 | /* 0xB8 - 0xBF */ | ||
2604 | N, N, | ||
2605 | G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock), | ||
2606 | D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), | ||
2607 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), | ||
2608 | /* 0xC0 - 0xCF */ | ||
2609 | D2bv(DstMem | SrcReg | ModRM | Lock), | ||
2610 | N, D(DstMem | SrcReg | ModRM | Mov), | ||
2611 | N, N, N, GD(0, &group9), | ||
2612 | N, N, N, N, N, N, N, N, | ||
2613 | /* 0xD0 - 0xDF */ | ||
2614 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | ||
2615 | /* 0xE0 - 0xEF */ | ||
2616 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | ||
2617 | /* 0xF0 - 0xFF */ | ||
2618 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N | ||
2619 | }; | ||
2620 | |||
2621 | #undef D | ||
2622 | #undef N | ||
2623 | #undef G | ||
2624 | #undef GD | ||
2625 | #undef I | ||
2626 | |||
2627 | #undef D2bv | ||
2628 | #undef I2bv | ||
2629 | #undef D6ALU | ||
2630 | |||
2631 | static unsigned imm_size(struct decode_cache *c) | ||
2632 | { | ||
2633 | unsigned size; | ||
2634 | |||
2635 | size = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
2636 | if (size == 8) | ||
2637 | size = 4; | ||
2638 | return size; | ||
2639 | } | ||
2640 | |||
2641 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, | ||
2642 | unsigned size, bool sign_extension) | ||
2643 | { | ||
2644 | struct decode_cache *c = &ctxt->decode; | ||
2645 | struct x86_emulate_ops *ops = ctxt->ops; | ||
2646 | int rc = X86EMUL_CONTINUE; | ||
2647 | |||
2648 | op->type = OP_IMM; | ||
2649 | op->bytes = size; | ||
2650 | op->addr.mem = c->eip; | ||
2651 | /* NB. Immediates are sign-extended as necessary. */ | ||
2652 | switch (op->bytes) { | ||
2653 | case 1: | ||
2654 | op->val = insn_fetch(s8, 1, c->eip); | ||
2655 | break; | ||
2656 | case 2: | ||
2657 | op->val = insn_fetch(s16, 2, c->eip); | ||
2658 | break; | ||
2659 | case 4: | ||
2660 | op->val = insn_fetch(s32, 4, c->eip); | ||
2661 | break; | ||
2662 | } | ||
2663 | if (!sign_extension) { | ||
2664 | switch (op->bytes) { | ||
2665 | case 1: | ||
2666 | op->val &= 0xff; | ||
2667 | break; | ||
2668 | case 2: | ||
2669 | op->val &= 0xffff; | ||
2670 | break; | ||
2671 | case 4: | ||
2672 | op->val &= 0xffffffff; | ||
2673 | break; | ||
2674 | } | ||
2675 | } | ||
2676 | done: | ||
2677 | return rc; | ||
2556 | } | 2678 | } |
2557 | 2679 | ||
2558 | int | 2680 | int |
2559 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | 2681 | x86_decode_insn(struct x86_emulate_ctxt *ctxt) |
2560 | { | 2682 | { |
2683 | struct x86_emulate_ops *ops = ctxt->ops; | ||
2684 | struct decode_cache *c = &ctxt->decode; | ||
2685 | int rc = X86EMUL_CONTINUE; | ||
2686 | int mode = ctxt->mode; | ||
2687 | int def_op_bytes, def_ad_bytes, dual, goffset; | ||
2688 | struct opcode opcode, *g_mod012, *g_mod3; | ||
2689 | struct operand memop = { .type = OP_NONE }; | ||
2690 | |||
2691 | c->eip = ctxt->eip; | ||
2692 | c->fetch.start = c->fetch.end = c->eip; | ||
2693 | ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); | ||
2694 | |||
2695 | switch (mode) { | ||
2696 | case X86EMUL_MODE_REAL: | ||
2697 | case X86EMUL_MODE_VM86: | ||
2698 | case X86EMUL_MODE_PROT16: | ||
2699 | def_op_bytes = def_ad_bytes = 2; | ||
2700 | break; | ||
2701 | case X86EMUL_MODE_PROT32: | ||
2702 | def_op_bytes = def_ad_bytes = 4; | ||
2703 | break; | ||
2704 | #ifdef CONFIG_X86_64 | ||
2705 | case X86EMUL_MODE_PROT64: | ||
2706 | def_op_bytes = 4; | ||
2707 | def_ad_bytes = 8; | ||
2708 | break; | ||
2709 | #endif | ||
2710 | default: | ||
2711 | return -1; | ||
2712 | } | ||
2713 | |||
2714 | c->op_bytes = def_op_bytes; | ||
2715 | c->ad_bytes = def_ad_bytes; | ||
2716 | |||
2717 | /* Legacy prefixes. */ | ||
2718 | for (;;) { | ||
2719 | switch (c->b = insn_fetch(u8, 1, c->eip)) { | ||
2720 | case 0x66: /* operand-size override */ | ||
2721 | /* switch between 2/4 bytes */ | ||
2722 | c->op_bytes = def_op_bytes ^ 6; | ||
2723 | break; | ||
2724 | case 0x67: /* address-size override */ | ||
2725 | if (mode == X86EMUL_MODE_PROT64) | ||
2726 | /* switch between 4/8 bytes */ | ||
2727 | c->ad_bytes = def_ad_bytes ^ 12; | ||
2728 | else | ||
2729 | /* switch between 2/4 bytes */ | ||
2730 | c->ad_bytes = def_ad_bytes ^ 6; | ||
2731 | break; | ||
2732 | case 0x26: /* ES override */ | ||
2733 | case 0x2e: /* CS override */ | ||
2734 | case 0x36: /* SS override */ | ||
2735 | case 0x3e: /* DS override */ | ||
2736 | set_seg_override(c, (c->b >> 3) & 3); | ||
2737 | break; | ||
2738 | case 0x64: /* FS override */ | ||
2739 | case 0x65: /* GS override */ | ||
2740 | set_seg_override(c, c->b & 7); | ||
2741 | break; | ||
2742 | case 0x40 ... 0x4f: /* REX */ | ||
2743 | if (mode != X86EMUL_MODE_PROT64) | ||
2744 | goto done_prefixes; | ||
2745 | c->rex_prefix = c->b; | ||
2746 | continue; | ||
2747 | case 0xf0: /* LOCK */ | ||
2748 | c->lock_prefix = 1; | ||
2749 | break; | ||
2750 | case 0xf2: /* REPNE/REPNZ */ | ||
2751 | c->rep_prefix = REPNE_PREFIX; | ||
2752 | break; | ||
2753 | case 0xf3: /* REP/REPE/REPZ */ | ||
2754 | c->rep_prefix = REPE_PREFIX; | ||
2755 | break; | ||
2756 | default: | ||
2757 | goto done_prefixes; | ||
2758 | } | ||
2759 | |||
2760 | /* Any legacy prefix after a REX prefix nullifies its effect. */ | ||
2761 | |||
2762 | c->rex_prefix = 0; | ||
2763 | } | ||
2764 | |||
2765 | done_prefixes: | ||
2766 | |||
2767 | /* REX prefix. */ | ||
2768 | if (c->rex_prefix & 8) | ||
2769 | c->op_bytes = 8; /* REX.W */ | ||
2770 | |||
2771 | /* Opcode byte(s). */ | ||
2772 | opcode = opcode_table[c->b]; | ||
2773 | /* Two-byte opcode? */ | ||
2774 | if (c->b == 0x0f) { | ||
2775 | c->twobyte = 1; | ||
2776 | c->b = insn_fetch(u8, 1, c->eip); | ||
2777 | opcode = twobyte_table[c->b]; | ||
2778 | } | ||
2779 | c->d = opcode.flags; | ||
2780 | |||
2781 | if (c->d & Group) { | ||
2782 | dual = c->d & GroupDual; | ||
2783 | c->modrm = insn_fetch(u8, 1, c->eip); | ||
2784 | --c->eip; | ||
2785 | |||
2786 | if (c->d & GroupDual) { | ||
2787 | g_mod012 = opcode.u.gdual->mod012; | ||
2788 | g_mod3 = opcode.u.gdual->mod3; | ||
2789 | } else | ||
2790 | g_mod012 = g_mod3 = opcode.u.group; | ||
2791 | |||
2792 | c->d &= ~(Group | GroupDual); | ||
2793 | |||
2794 | goffset = (c->modrm >> 3) & 7; | ||
2795 | |||
2796 | if ((c->modrm >> 6) == 3) | ||
2797 | opcode = g_mod3[goffset]; | ||
2798 | else | ||
2799 | opcode = g_mod012[goffset]; | ||
2800 | c->d |= opcode.flags; | ||
2801 | } | ||
2802 | |||
2803 | c->execute = opcode.u.execute; | ||
2804 | |||
2805 | /* Unrecognised? */ | ||
2806 | if (c->d == 0 || (c->d & Undefined)) { | ||
2807 | DPRINTF("Cannot emulate %02x\n", c->b); | ||
2808 | return -1; | ||
2809 | } | ||
2810 | |||
2811 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) | ||
2812 | c->op_bytes = 8; | ||
2813 | |||
2814 | if (c->d & Op3264) { | ||
2815 | if (mode == X86EMUL_MODE_PROT64) | ||
2816 | c->op_bytes = 8; | ||
2817 | else | ||
2818 | c->op_bytes = 4; | ||
2819 | } | ||
2820 | |||
2821 | /* ModRM and SIB bytes. */ | ||
2822 | if (c->d & ModRM) { | ||
2823 | rc = decode_modrm(ctxt, ops, &memop); | ||
2824 | if (!c->has_seg_override) | ||
2825 | set_seg_override(c, c->modrm_seg); | ||
2826 | } else if (c->d & MemAbs) | ||
2827 | rc = decode_abs(ctxt, ops, &memop); | ||
2828 | if (rc != X86EMUL_CONTINUE) | ||
2829 | goto done; | ||
2830 | |||
2831 | if (!c->has_seg_override) | ||
2832 | set_seg_override(c, VCPU_SREG_DS); | ||
2833 | |||
2834 | if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d)) | ||
2835 | memop.addr.mem += seg_override_base(ctxt, ops, c); | ||
2836 | |||
2837 | if (memop.type == OP_MEM && c->ad_bytes != 8) | ||
2838 | memop.addr.mem = (u32)memop.addr.mem; | ||
2839 | |||
2840 | if (memop.type == OP_MEM && c->rip_relative) | ||
2841 | memop.addr.mem += c->eip; | ||
2842 | |||
2843 | /* | ||
2844 | * Decode and fetch the source operand: register, memory | ||
2845 | * or immediate. | ||
2846 | */ | ||
2847 | switch (c->d & SrcMask) { | ||
2848 | case SrcNone: | ||
2849 | break; | ||
2850 | case SrcReg: | ||
2851 | decode_register_operand(&c->src, c, 0); | ||
2852 | break; | ||
2853 | case SrcMem16: | ||
2854 | memop.bytes = 2; | ||
2855 | goto srcmem_common; | ||
2856 | case SrcMem32: | ||
2857 | memop.bytes = 4; | ||
2858 | goto srcmem_common; | ||
2859 | case SrcMem: | ||
2860 | memop.bytes = (c->d & ByteOp) ? 1 : | ||
2861 | c->op_bytes; | ||
2862 | srcmem_common: | ||
2863 | c->src = memop; | ||
2864 | break; | ||
2865 | case SrcImmU16: | ||
2866 | rc = decode_imm(ctxt, &c->src, 2, false); | ||
2867 | break; | ||
2868 | case SrcImm: | ||
2869 | rc = decode_imm(ctxt, &c->src, imm_size(c), true); | ||
2870 | break; | ||
2871 | case SrcImmU: | ||
2872 | rc = decode_imm(ctxt, &c->src, imm_size(c), false); | ||
2873 | break; | ||
2874 | case SrcImmByte: | ||
2875 | rc = decode_imm(ctxt, &c->src, 1, true); | ||
2876 | break; | ||
2877 | case SrcImmUByte: | ||
2878 | rc = decode_imm(ctxt, &c->src, 1, false); | ||
2879 | break; | ||
2880 | case SrcAcc: | ||
2881 | c->src.type = OP_REG; | ||
2882 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
2883 | c->src.addr.reg = &c->regs[VCPU_REGS_RAX]; | ||
2884 | fetch_register_operand(&c->src); | ||
2885 | break; | ||
2886 | case SrcOne: | ||
2887 | c->src.bytes = 1; | ||
2888 | c->src.val = 1; | ||
2889 | break; | ||
2890 | case SrcSI: | ||
2891 | c->src.type = OP_MEM; | ||
2892 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
2893 | c->src.addr.mem = | ||
2894 | register_address(c, seg_override_base(ctxt, ops, c), | ||
2895 | c->regs[VCPU_REGS_RSI]); | ||
2896 | c->src.val = 0; | ||
2897 | break; | ||
2898 | case SrcImmFAddr: | ||
2899 | c->src.type = OP_IMM; | ||
2900 | c->src.addr.mem = c->eip; | ||
2901 | c->src.bytes = c->op_bytes + 2; | ||
2902 | insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip); | ||
2903 | break; | ||
2904 | case SrcMemFAddr: | ||
2905 | memop.bytes = c->op_bytes + 2; | ||
2906 | goto srcmem_common; | ||
2907 | break; | ||
2908 | } | ||
2909 | |||
2910 | if (rc != X86EMUL_CONTINUE) | ||
2911 | goto done; | ||
2912 | |||
2913 | /* | ||
2914 | * Decode and fetch the second source operand: register, memory | ||
2915 | * or immediate. | ||
2916 | */ | ||
2917 | switch (c->d & Src2Mask) { | ||
2918 | case Src2None: | ||
2919 | break; | ||
2920 | case Src2CL: | ||
2921 | c->src2.bytes = 1; | ||
2922 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; | ||
2923 | break; | ||
2924 | case Src2ImmByte: | ||
2925 | rc = decode_imm(ctxt, &c->src2, 1, true); | ||
2926 | break; | ||
2927 | case Src2One: | ||
2928 | c->src2.bytes = 1; | ||
2929 | c->src2.val = 1; | ||
2930 | break; | ||
2931 | case Src2Imm: | ||
2932 | rc = decode_imm(ctxt, &c->src2, imm_size(c), true); | ||
2933 | break; | ||
2934 | } | ||
2935 | |||
2936 | if (rc != X86EMUL_CONTINUE) | ||
2937 | goto done; | ||
2938 | |||
2939 | /* Decode and fetch the destination operand: register or memory. */ | ||
2940 | switch (c->d & DstMask) { | ||
2941 | case DstReg: | ||
2942 | decode_register_operand(&c->dst, c, | ||
2943 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); | ||
2944 | break; | ||
2945 | case DstImmUByte: | ||
2946 | c->dst.type = OP_IMM; | ||
2947 | c->dst.addr.mem = c->eip; | ||
2948 | c->dst.bytes = 1; | ||
2949 | c->dst.val = insn_fetch(u8, 1, c->eip); | ||
2950 | break; | ||
2951 | case DstMem: | ||
2952 | case DstMem64: | ||
2953 | c->dst = memop; | ||
2954 | if ((c->d & DstMask) == DstMem64) | ||
2955 | c->dst.bytes = 8; | ||
2956 | else | ||
2957 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
2958 | if (c->d & BitOp) | ||
2959 | fetch_bit_operand(c); | ||
2960 | c->dst.orig_val = c->dst.val; | ||
2961 | break; | ||
2962 | case DstAcc: | ||
2963 | c->dst.type = OP_REG; | ||
2964 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
2965 | c->dst.addr.reg = &c->regs[VCPU_REGS_RAX]; | ||
2966 | fetch_register_operand(&c->dst); | ||
2967 | c->dst.orig_val = c->dst.val; | ||
2968 | break; | ||
2969 | case DstDI: | ||
2970 | c->dst.type = OP_MEM; | ||
2971 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; | ||
2972 | c->dst.addr.mem = | ||
2973 | register_address(c, es_base(ctxt, ops), | ||
2974 | c->regs[VCPU_REGS_RDI]); | ||
2975 | c->dst.val = 0; | ||
2976 | break; | ||
2977 | case ImplicitOps: | ||
2978 | /* Special instructions do their own operand decoding. */ | ||
2979 | default: | ||
2980 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
2981 | return 0; | ||
2982 | } | ||
2983 | |||
2984 | done: | ||
2985 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; | ||
2986 | } | ||
2987 | |||
2988 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) | ||
2989 | { | ||
2990 | struct decode_cache *c = &ctxt->decode; | ||
2991 | |||
2992 | /* The second termination condition only applies for REPE | ||
2993 | * and REPNE. Test if the repeat string operation prefix is | ||
2994 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the | ||
2995 | * corresponding termination condition according to: | ||
2996 | * - if REPE/REPZ and ZF = 0 then done | ||
2997 | * - if REPNE/REPNZ and ZF = 1 then done | ||
2998 | */ | ||
2999 | if (((c->b == 0xa6) || (c->b == 0xa7) || | ||
3000 | (c->b == 0xae) || (c->b == 0xaf)) | ||
3001 | && (((c->rep_prefix == REPE_PREFIX) && | ||
3002 | ((ctxt->eflags & EFLG_ZF) == 0)) | ||
3003 | || ((c->rep_prefix == REPNE_PREFIX) && | ||
3004 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) | ||
3005 | return true; | ||
3006 | |||
3007 | return false; | ||
3008 | } | ||
3009 | |||
3010 | int | ||
3011 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt) | ||
3012 | { | ||
3013 | struct x86_emulate_ops *ops = ctxt->ops; | ||
2561 | u64 msr_data; | 3014 | u64 msr_data; |
2562 | struct decode_cache *c = &ctxt->decode; | 3015 | struct decode_cache *c = &ctxt->decode; |
2563 | int rc = X86EMUL_CONTINUE; | 3016 | int rc = X86EMUL_CONTINUE; |
2564 | int saved_dst_type = c->dst.type; | 3017 | int saved_dst_type = c->dst.type; |
3018 | int irq; /* Used for int 3, int, and into */ | ||
2565 | 3019 | ||
2566 | ctxt->decode.mem_read.pos = 0; | 3020 | ctxt->decode.mem_read.pos = 0; |
2567 | 3021 | ||
@@ -2576,6 +3030,11 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | |||
2576 | goto done; | 3030 | goto done; |
2577 | } | 3031 | } |
2578 | 3032 | ||
3033 | if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) { | ||
3034 | emulate_ud(ctxt); | ||
3035 | goto done; | ||
3036 | } | ||
3037 | |||
2579 | /* Privileged instruction can be executed only in CPL=0 */ | 3038 | /* Privileged instruction can be executed only in CPL=0 */ |
2580 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { | 3039 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { |
2581 | emulate_gp(ctxt, 0); | 3040 | emulate_gp(ctxt, 0); |
@@ -2583,35 +3042,15 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | |||
2583 | } | 3042 | } |
2584 | 3043 | ||
2585 | if (c->rep_prefix && (c->d & String)) { | 3044 | if (c->rep_prefix && (c->d & String)) { |
2586 | ctxt->restart = true; | ||
2587 | /* All REP prefixes have the same first termination condition */ | 3045 | /* All REP prefixes have the same first termination condition */ |
2588 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { | 3046 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { |
2589 | string_done: | ||
2590 | ctxt->restart = false; | ||
2591 | ctxt->eip = c->eip; | 3047 | ctxt->eip = c->eip; |
2592 | goto done; | 3048 | goto done; |
2593 | } | 3049 | } |
2594 | /* The second termination condition only applies for REPE | ||
2595 | * and REPNE. Test if the repeat string operation prefix is | ||
2596 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the | ||
2597 | * corresponding termination condition according to: | ||
2598 | * - if REPE/REPZ and ZF = 0 then done | ||
2599 | * - if REPNE/REPNZ and ZF = 1 then done | ||
2600 | */ | ||
2601 | if ((c->b == 0xa6) || (c->b == 0xa7) || | ||
2602 | (c->b == 0xae) || (c->b == 0xaf)) { | ||
2603 | if ((c->rep_prefix == REPE_PREFIX) && | ||
2604 | ((ctxt->eflags & EFLG_ZF) == 0)) | ||
2605 | goto string_done; | ||
2606 | if ((c->rep_prefix == REPNE_PREFIX) && | ||
2607 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) | ||
2608 | goto string_done; | ||
2609 | } | ||
2610 | c->eip = ctxt->eip; | ||
2611 | } | 3050 | } |
2612 | 3051 | ||
2613 | if (c->src.type == OP_MEM) { | 3052 | if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) { |
2614 | rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr, | 3053 | rc = read_emulated(ctxt, ops, c->src.addr.mem, |
2615 | c->src.valptr, c->src.bytes); | 3054 | c->src.valptr, c->src.bytes); |
2616 | if (rc != X86EMUL_CONTINUE) | 3055 | if (rc != X86EMUL_CONTINUE) |
2617 | goto done; | 3056 | goto done; |
@@ -2619,7 +3058,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | |||
2619 | } | 3058 | } |
2620 | 3059 | ||
2621 | if (c->src2.type == OP_MEM) { | 3060 | if (c->src2.type == OP_MEM) { |
2622 | rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr, | 3061 | rc = read_emulated(ctxt, ops, c->src2.addr.mem, |
2623 | &c->src2.val, c->src2.bytes); | 3062 | &c->src2.val, c->src2.bytes); |
2624 | if (rc != X86EMUL_CONTINUE) | 3063 | if (rc != X86EMUL_CONTINUE) |
2625 | goto done; | 3064 | goto done; |
@@ -2631,7 +3070,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | |||
2631 | 3070 | ||
2632 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { | 3071 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { |
2633 | /* optimisation - avoid slow emulated read if Mov */ | 3072 | /* optimisation - avoid slow emulated read if Mov */ |
2634 | rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr, | 3073 | rc = read_emulated(ctxt, ops, c->dst.addr.mem, |
2635 | &c->dst.val, c->dst.bytes); | 3074 | &c->dst.val, c->dst.bytes); |
2636 | if (rc != X86EMUL_CONTINUE) | 3075 | if (rc != X86EMUL_CONTINUE) |
2637 | goto done; | 3076 | goto done; |
@@ -2640,6 +3079,13 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) | |||
2640 | 3079 | ||
2641 | special_insn: | 3080 | special_insn: |
2642 | 3081 | ||
3082 | if (c->execute) { | ||
3083 | rc = c->execute(ctxt); | ||
3084 | if (rc != X86EMUL_CONTINUE) | ||
3085 | goto done; | ||
3086 | goto writeback; | ||
3087 | } | ||
3088 | |||
2643 | if (c->twobyte) | 3089 | if (c->twobyte) |
2644 | goto twobyte_insn; | 3090 | goto twobyte_insn; |
2645 | 3091 | ||
@@ -2653,8 +3099,6 @@ special_insn: | |||
2653 | break; | 3099 | break; |
2654 | case 0x07: /* pop es */ | 3100 | case 0x07: /* pop es */ |
2655 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); | 3101 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
2656 | if (rc != X86EMUL_CONTINUE) | ||
2657 | goto done; | ||
2658 | break; | 3102 | break; |
2659 | case 0x08 ... 0x0d: | 3103 | case 0x08 ... 0x0d: |
2660 | or: /* or */ | 3104 | or: /* or */ |
@@ -2672,8 +3116,6 @@ special_insn: | |||
2672 | break; | 3116 | break; |
2673 | case 0x17: /* pop ss */ | 3117 | case 0x17: /* pop ss */ |
2674 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); | 3118 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
2675 | if (rc != X86EMUL_CONTINUE) | ||
2676 | goto done; | ||
2677 | break; | 3119 | break; |
2678 | case 0x18 ... 0x1d: | 3120 | case 0x18 ... 0x1d: |
2679 | sbb: /* sbb */ | 3121 | sbb: /* sbb */ |
@@ -2684,8 +3126,6 @@ special_insn: | |||
2684 | break; | 3126 | break; |
2685 | case 0x1f: /* pop ds */ | 3127 | case 0x1f: /* pop ds */ |
2686 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); | 3128 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
2687 | if (rc != X86EMUL_CONTINUE) | ||
2688 | goto done; | ||
2689 | break; | 3129 | break; |
2690 | case 0x20 ... 0x25: | 3130 | case 0x20 ... 0x25: |
2691 | and: /* and */ | 3131 | and: /* and */ |
@@ -2709,58 +3149,29 @@ special_insn: | |||
2709 | case 0x48 ... 0x4f: /* dec r16/r32 */ | 3149 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
2710 | emulate_1op("dec", c->dst, ctxt->eflags); | 3150 | emulate_1op("dec", c->dst, ctxt->eflags); |
2711 | break; | 3151 | break; |
2712 | case 0x50 ... 0x57: /* push reg */ | ||
2713 | emulate_push(ctxt, ops); | ||
2714 | break; | ||
2715 | case 0x58 ... 0x5f: /* pop reg */ | 3152 | case 0x58 ... 0x5f: /* pop reg */ |
2716 | pop_instruction: | 3153 | pop_instruction: |
2717 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); | 3154 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
2718 | if (rc != X86EMUL_CONTINUE) | ||
2719 | goto done; | ||
2720 | break; | 3155 | break; |
2721 | case 0x60: /* pusha */ | 3156 | case 0x60: /* pusha */ |
2722 | rc = emulate_pusha(ctxt, ops); | 3157 | rc = emulate_pusha(ctxt, ops); |
2723 | if (rc != X86EMUL_CONTINUE) | ||
2724 | goto done; | ||
2725 | break; | 3158 | break; |
2726 | case 0x61: /* popa */ | 3159 | case 0x61: /* popa */ |
2727 | rc = emulate_popa(ctxt, ops); | 3160 | rc = emulate_popa(ctxt, ops); |
2728 | if (rc != X86EMUL_CONTINUE) | ||
2729 | goto done; | ||
2730 | break; | 3161 | break; |
2731 | case 0x63: /* movsxd */ | 3162 | case 0x63: /* movsxd */ |
2732 | if (ctxt->mode != X86EMUL_MODE_PROT64) | 3163 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
2733 | goto cannot_emulate; | 3164 | goto cannot_emulate; |
2734 | c->dst.val = (s32) c->src.val; | 3165 | c->dst.val = (s32) c->src.val; |
2735 | break; | 3166 | break; |
2736 | case 0x68: /* push imm */ | ||
2737 | case 0x6a: /* push imm8 */ | ||
2738 | emulate_push(ctxt, ops); | ||
2739 | break; | ||
2740 | case 0x6c: /* insb */ | 3167 | case 0x6c: /* insb */ |
2741 | case 0x6d: /* insw/insd */ | 3168 | case 0x6d: /* insw/insd */ |
2742 | c->dst.bytes = min(c->dst.bytes, 4u); | 3169 | c->src.val = c->regs[VCPU_REGS_RDX]; |
2743 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], | 3170 | goto do_io_in; |
2744 | c->dst.bytes)) { | ||
2745 | emulate_gp(ctxt, 0); | ||
2746 | goto done; | ||
2747 | } | ||
2748 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, | ||
2749 | c->regs[VCPU_REGS_RDX], &c->dst.val)) | ||
2750 | goto done; /* IO is needed, skip writeback */ | ||
2751 | break; | ||
2752 | case 0x6e: /* outsb */ | 3171 | case 0x6e: /* outsb */ |
2753 | case 0x6f: /* outsw/outsd */ | 3172 | case 0x6f: /* outsw/outsd */ |
2754 | c->src.bytes = min(c->src.bytes, 4u); | 3173 | c->dst.val = c->regs[VCPU_REGS_RDX]; |
2755 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], | 3174 | goto do_io_out; |
2756 | c->src.bytes)) { | ||
2757 | emulate_gp(ctxt, 0); | ||
2758 | goto done; | ||
2759 | } | ||
2760 | ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX], | ||
2761 | &c->src.val, 1, ctxt->vcpu); | ||
2762 | |||
2763 | c->dst.type = OP_NONE; /* nothing to writeback */ | ||
2764 | break; | 3175 | break; |
2765 | case 0x70 ... 0x7f: /* jcc (short) */ | 3176 | case 0x70 ... 0x7f: /* jcc (short) */ |
2766 | if (test_cc(c->b, ctxt->eflags)) | 3177 | if (test_cc(c->b, ctxt->eflags)) |
@@ -2793,29 +3204,15 @@ special_insn: | |||
2793 | case 0x86 ... 0x87: /* xchg */ | 3204 | case 0x86 ... 0x87: /* xchg */ |
2794 | xchg: | 3205 | xchg: |
2795 | /* Write back the register source. */ | 3206 | /* Write back the register source. */ |
2796 | switch (c->dst.bytes) { | 3207 | c->src.val = c->dst.val; |
2797 | case 1: | 3208 | write_register_operand(&c->src); |
2798 | *(u8 *) c->src.ptr = (u8) c->dst.val; | ||
2799 | break; | ||
2800 | case 2: | ||
2801 | *(u16 *) c->src.ptr = (u16) c->dst.val; | ||
2802 | break; | ||
2803 | case 4: | ||
2804 | *c->src.ptr = (u32) c->dst.val; | ||
2805 | break; /* 64b reg: zero-extend */ | ||
2806 | case 8: | ||
2807 | *c->src.ptr = c->dst.val; | ||
2808 | break; | ||
2809 | } | ||
2810 | /* | 3209 | /* |
2811 | * Write back the memory destination with implicit LOCK | 3210 | * Write back the memory destination with implicit LOCK |
2812 | * prefix. | 3211 | * prefix. |
2813 | */ | 3212 | */ |
2814 | c->dst.val = c->src.val; | 3213 | c->dst.val = c->src.orig_val; |
2815 | c->lock_prefix = 1; | 3214 | c->lock_prefix = 1; |
2816 | break; | 3215 | break; |
2817 | case 0x88 ... 0x8b: /* mov */ | ||
2818 | goto mov; | ||
2819 | case 0x8c: /* mov r/m, sreg */ | 3216 | case 0x8c: /* mov r/m, sreg */ |
2820 | if (c->modrm_reg > VCPU_SREG_GS) { | 3217 | if (c->modrm_reg > VCPU_SREG_GS) { |
2821 | emulate_ud(ctxt); | 3218 | emulate_ud(ctxt); |
@@ -2824,7 +3221,7 @@ special_insn: | |||
2824 | c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu); | 3221 | c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu); |
2825 | break; | 3222 | break; |
2826 | case 0x8d: /* lea r16/r32, m */ | 3223 | case 0x8d: /* lea r16/r32, m */ |
2827 | c->dst.val = c->modrm_ea; | 3224 | c->dst.val = c->src.addr.mem; |
2828 | break; | 3225 | break; |
2829 | case 0x8e: { /* mov seg, r/m16 */ | 3226 | case 0x8e: { /* mov seg, r/m16 */ |
2830 | uint16_t sel; | 3227 | uint16_t sel; |
@@ -2847,76 +3244,87 @@ special_insn: | |||
2847 | } | 3244 | } |
2848 | case 0x8f: /* pop (sole member of Grp1a) */ | 3245 | case 0x8f: /* pop (sole member of Grp1a) */ |
2849 | rc = emulate_grp1a(ctxt, ops); | 3246 | rc = emulate_grp1a(ctxt, ops); |
2850 | if (rc != X86EMUL_CONTINUE) | ||
2851 | goto done; | ||
2852 | break; | 3247 | break; |
2853 | case 0x90: /* nop / xchg r8,rax */ | 3248 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
2854 | if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) { | 3249 | if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX]) |
2855 | c->dst.type = OP_NONE; /* nop */ | ||
2856 | break; | 3250 | break; |
2857 | } | ||
2858 | case 0x91 ... 0x97: /* xchg reg,rax */ | ||
2859 | c->src.type = OP_REG; | ||
2860 | c->src.bytes = c->op_bytes; | ||
2861 | c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX]; | ||
2862 | c->src.val = *(c->src.ptr); | ||
2863 | goto xchg; | 3251 | goto xchg; |
3252 | case 0x98: /* cbw/cwde/cdqe */ | ||
3253 | switch (c->op_bytes) { | ||
3254 | case 2: c->dst.val = (s8)c->dst.val; break; | ||
3255 | case 4: c->dst.val = (s16)c->dst.val; break; | ||
3256 | case 8: c->dst.val = (s32)c->dst.val; break; | ||
3257 | } | ||
3258 | break; | ||
2864 | case 0x9c: /* pushf */ | 3259 | case 0x9c: /* pushf */ |
2865 | c->src.val = (unsigned long) ctxt->eflags; | 3260 | c->src.val = (unsigned long) ctxt->eflags; |
2866 | emulate_push(ctxt, ops); | 3261 | emulate_push(ctxt, ops); |
2867 | break; | 3262 | break; |
2868 | case 0x9d: /* popf */ | 3263 | case 0x9d: /* popf */ |
2869 | c->dst.type = OP_REG; | 3264 | c->dst.type = OP_REG; |
2870 | c->dst.ptr = (unsigned long *) &ctxt->eflags; | 3265 | c->dst.addr.reg = &ctxt->eflags; |
2871 | c->dst.bytes = c->op_bytes; | 3266 | c->dst.bytes = c->op_bytes; |
2872 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); | 3267 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
2873 | if (rc != X86EMUL_CONTINUE) | ||
2874 | goto done; | ||
2875 | break; | 3268 | break; |
2876 | case 0xa0 ... 0xa3: /* mov */ | ||
2877 | case 0xa4 ... 0xa5: /* movs */ | ||
2878 | goto mov; | ||
2879 | case 0xa6 ... 0xa7: /* cmps */ | 3269 | case 0xa6 ... 0xa7: /* cmps */ |
2880 | c->dst.type = OP_NONE; /* Disable writeback. */ | 3270 | c->dst.type = OP_NONE; /* Disable writeback. */ |
2881 | DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr); | 3271 | DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem); |
2882 | goto cmp; | 3272 | goto cmp; |
2883 | case 0xa8 ... 0xa9: /* test ax, imm */ | 3273 | case 0xa8 ... 0xa9: /* test ax, imm */ |
2884 | goto test; | 3274 | goto test; |
2885 | case 0xaa ... 0xab: /* stos */ | ||
2886 | c->dst.val = c->regs[VCPU_REGS_RAX]; | ||
2887 | break; | ||
2888 | case 0xac ... 0xad: /* lods */ | ||
2889 | goto mov; | ||
2890 | case 0xae ... 0xaf: /* scas */ | 3275 | case 0xae ... 0xaf: /* scas */ |
2891 | DPRINTF("Urk! I don't handle SCAS.\n"); | 3276 | goto cmp; |
2892 | goto cannot_emulate; | ||
2893 | case 0xb0 ... 0xbf: /* mov r, imm */ | ||
2894 | goto mov; | ||
2895 | case 0xc0 ... 0xc1: | 3277 | case 0xc0 ... 0xc1: |
2896 | emulate_grp2(ctxt); | 3278 | emulate_grp2(ctxt); |
2897 | break; | 3279 | break; |
2898 | case 0xc3: /* ret */ | 3280 | case 0xc3: /* ret */ |
2899 | c->dst.type = OP_REG; | 3281 | c->dst.type = OP_REG; |
2900 | c->dst.ptr = &c->eip; | 3282 | c->dst.addr.reg = &c->eip; |
2901 | c->dst.bytes = c->op_bytes; | 3283 | c->dst.bytes = c->op_bytes; |
2902 | goto pop_instruction; | 3284 | goto pop_instruction; |
2903 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ | 3285 | case 0xc4: /* les */ |
2904 | mov: | 3286 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES); |
2905 | c->dst.val = c->src.val; | 3287 | break; |
3288 | case 0xc5: /* lds */ | ||
3289 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS); | ||
2906 | break; | 3290 | break; |
2907 | case 0xcb: /* ret far */ | 3291 | case 0xcb: /* ret far */ |
2908 | rc = emulate_ret_far(ctxt, ops); | 3292 | rc = emulate_ret_far(ctxt, ops); |
2909 | if (rc != X86EMUL_CONTINUE) | 3293 | break; |
2910 | goto done; | 3294 | case 0xcc: /* int3 */ |
3295 | irq = 3; | ||
3296 | goto do_interrupt; | ||
3297 | case 0xcd: /* int n */ | ||
3298 | irq = c->src.val; | ||
3299 | do_interrupt: | ||
3300 | rc = emulate_int(ctxt, ops, irq); | ||
3301 | break; | ||
3302 | case 0xce: /* into */ | ||
3303 | if (ctxt->eflags & EFLG_OF) { | ||
3304 | irq = 4; | ||
3305 | goto do_interrupt; | ||
3306 | } | ||
3307 | break; | ||
3308 | case 0xcf: /* iret */ | ||
3309 | rc = emulate_iret(ctxt, ops); | ||
2911 | break; | 3310 | break; |
2912 | case 0xd0 ... 0xd1: /* Grp2 */ | 3311 | case 0xd0 ... 0xd1: /* Grp2 */ |
2913 | c->src.val = 1; | ||
2914 | emulate_grp2(ctxt); | 3312 | emulate_grp2(ctxt); |
2915 | break; | 3313 | break; |
2916 | case 0xd2 ... 0xd3: /* Grp2 */ | 3314 | case 0xd2 ... 0xd3: /* Grp2 */ |
2917 | c->src.val = c->regs[VCPU_REGS_RCX]; | 3315 | c->src.val = c->regs[VCPU_REGS_RCX]; |
2918 | emulate_grp2(ctxt); | 3316 | emulate_grp2(ctxt); |
2919 | break; | 3317 | break; |
3318 | case 0xe0 ... 0xe2: /* loop/loopz/loopnz */ | ||
3319 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); | ||
3320 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 && | ||
3321 | (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags))) | ||
3322 | jmp_rel(c, c->src.val); | ||
3323 | break; | ||
3324 | case 0xe3: /* jcxz/jecxz/jrcxz */ | ||
3325 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) | ||
3326 | jmp_rel(c, c->src.val); | ||
3327 | break; | ||
2920 | case 0xe4: /* inb */ | 3328 | case 0xe4: /* inb */ |
2921 | case 0xe5: /* in */ | 3329 | case 0xe5: /* in */ |
2922 | goto do_io_in; | 3330 | goto do_io_in; |
@@ -2964,15 +3372,16 @@ special_insn: | |||
2964 | break; | 3372 | break; |
2965 | case 0xee: /* out dx,al */ | 3373 | case 0xee: /* out dx,al */ |
2966 | case 0xef: /* out dx,(e/r)ax */ | 3374 | case 0xef: /* out dx,(e/r)ax */ |
2967 | c->src.val = c->regs[VCPU_REGS_RDX]; | 3375 | c->dst.val = c->regs[VCPU_REGS_RDX]; |
2968 | do_io_out: | 3376 | do_io_out: |
2969 | c->dst.bytes = min(c->dst.bytes, 4u); | 3377 | c->src.bytes = min(c->src.bytes, 4u); |
2970 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { | 3378 | if (!emulator_io_permited(ctxt, ops, c->dst.val, |
3379 | c->src.bytes)) { | ||
2971 | emulate_gp(ctxt, 0); | 3380 | emulate_gp(ctxt, 0); |
2972 | goto done; | 3381 | goto done; |
2973 | } | 3382 | } |
2974 | ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1, | 3383 | ops->pio_out_emulated(c->src.bytes, c->dst.val, |
2975 | ctxt->vcpu); | 3384 | &c->src.val, 1, ctxt->vcpu); |
2976 | c->dst.type = OP_NONE; /* Disable writeback. */ | 3385 | c->dst.type = OP_NONE; /* Disable writeback. */ |
2977 | break; | 3386 | break; |
2978 | case 0xf4: /* hlt */ | 3387 | case 0xf4: /* hlt */ |
@@ -2981,24 +3390,22 @@ special_insn: | |||
2981 | case 0xf5: /* cmc */ | 3390 | case 0xf5: /* cmc */ |
2982 | /* complement carry flag from eflags reg */ | 3391 | /* complement carry flag from eflags reg */ |
2983 | ctxt->eflags ^= EFLG_CF; | 3392 | ctxt->eflags ^= EFLG_CF; |
2984 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
2985 | break; | 3393 | break; |
2986 | case 0xf6 ... 0xf7: /* Grp3 */ | 3394 | case 0xf6 ... 0xf7: /* Grp3 */ |
2987 | if (!emulate_grp3(ctxt, ops)) | 3395 | rc = emulate_grp3(ctxt, ops); |
2988 | goto cannot_emulate; | ||
2989 | break; | 3396 | break; |
2990 | case 0xf8: /* clc */ | 3397 | case 0xf8: /* clc */ |
2991 | ctxt->eflags &= ~EFLG_CF; | 3398 | ctxt->eflags &= ~EFLG_CF; |
2992 | c->dst.type = OP_NONE; /* Disable writeback. */ | 3399 | break; |
3400 | case 0xf9: /* stc */ | ||
3401 | ctxt->eflags |= EFLG_CF; | ||
2993 | break; | 3402 | break; |
2994 | case 0xfa: /* cli */ | 3403 | case 0xfa: /* cli */ |
2995 | if (emulator_bad_iopl(ctxt, ops)) { | 3404 | if (emulator_bad_iopl(ctxt, ops)) { |
2996 | emulate_gp(ctxt, 0); | 3405 | emulate_gp(ctxt, 0); |
2997 | goto done; | 3406 | goto done; |
2998 | } else { | 3407 | } else |
2999 | ctxt->eflags &= ~X86_EFLAGS_IF; | 3408 | ctxt->eflags &= ~X86_EFLAGS_IF; |
3000 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
3001 | } | ||
3002 | break; | 3409 | break; |
3003 | case 0xfb: /* sti */ | 3410 | case 0xfb: /* sti */ |
3004 | if (emulator_bad_iopl(ctxt, ops)) { | 3411 | if (emulator_bad_iopl(ctxt, ops)) { |
@@ -3007,29 +3414,29 @@ special_insn: | |||
3007 | } else { | 3414 | } else { |
3008 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; | 3415 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
3009 | ctxt->eflags |= X86_EFLAGS_IF; | 3416 | ctxt->eflags |= X86_EFLAGS_IF; |
3010 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
3011 | } | 3417 | } |
3012 | break; | 3418 | break; |
3013 | case 0xfc: /* cld */ | 3419 | case 0xfc: /* cld */ |
3014 | ctxt->eflags &= ~EFLG_DF; | 3420 | ctxt->eflags &= ~EFLG_DF; |
3015 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
3016 | break; | 3421 | break; |
3017 | case 0xfd: /* std */ | 3422 | case 0xfd: /* std */ |
3018 | ctxt->eflags |= EFLG_DF; | 3423 | ctxt->eflags |= EFLG_DF; |
3019 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
3020 | break; | 3424 | break; |
3021 | case 0xfe: /* Grp4 */ | 3425 | case 0xfe: /* Grp4 */ |
3022 | grp45: | 3426 | grp45: |
3023 | rc = emulate_grp45(ctxt, ops); | 3427 | rc = emulate_grp45(ctxt, ops); |
3024 | if (rc != X86EMUL_CONTINUE) | ||
3025 | goto done; | ||
3026 | break; | 3428 | break; |
3027 | case 0xff: /* Grp5 */ | 3429 | case 0xff: /* Grp5 */ |
3028 | if (c->modrm_reg == 5) | 3430 | if (c->modrm_reg == 5) |
3029 | goto jump_far; | 3431 | goto jump_far; |
3030 | goto grp45; | 3432 | goto grp45; |
3433 | default: | ||
3434 | goto cannot_emulate; | ||
3031 | } | 3435 | } |
3032 | 3436 | ||
3437 | if (rc != X86EMUL_CONTINUE) | ||
3438 | goto done; | ||
3439 | |||
3033 | writeback: | 3440 | writeback: |
3034 | rc = writeback(ctxt, ops); | 3441 | rc = writeback(ctxt, ops); |
3035 | if (rc != X86EMUL_CONTINUE) | 3442 | if (rc != X86EMUL_CONTINUE) |
@@ -3050,25 +3457,32 @@ writeback: | |||
3050 | &c->dst); | 3457 | &c->dst); |
3051 | 3458 | ||
3052 | if (c->rep_prefix && (c->d & String)) { | 3459 | if (c->rep_prefix && (c->d & String)) { |
3053 | struct read_cache *rc = &ctxt->decode.io_read; | 3460 | struct read_cache *r = &ctxt->decode.io_read; |
3054 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); | 3461 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
3055 | /* | 3462 | |
3056 | * Re-enter guest when pio read ahead buffer is empty or, | 3463 | if (!string_insn_completed(ctxt)) { |
3057 | * if it is not used, after each 1024 iteration. | 3464 | /* |
3058 | */ | 3465 | * Re-enter guest when pio read ahead buffer is empty |
3059 | if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) || | 3466 | * or, if it is not used, after each 1024 iteration. |
3060 | (rc->end != 0 && rc->end == rc->pos)) | 3467 | */ |
3061 | ctxt->restart = false; | 3468 | if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) && |
3469 | (r->end == 0 || r->end != r->pos)) { | ||
3470 | /* | ||
3471 | * Reset read cache. Usually happens before | ||
3472 | * decode, but since instruction is restarted | ||
3473 | * we have to do it here. | ||
3474 | */ | ||
3475 | ctxt->decode.mem_read.end = 0; | ||
3476 | return EMULATION_RESTART; | ||
3477 | } | ||
3478 | goto done; /* skip rip writeback */ | ||
3479 | } | ||
3062 | } | 3480 | } |
3063 | /* | 3481 | |
3064 | * reset read cache here in case string instruction is restared | ||
3065 | * without decoding | ||
3066 | */ | ||
3067 | ctxt->decode.mem_read.end = 0; | ||
3068 | ctxt->eip = c->eip; | 3482 | ctxt->eip = c->eip; |
3069 | 3483 | ||
3070 | done: | 3484 | done: |
3071 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; | 3485 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
3072 | 3486 | ||
3073 | twobyte_insn: | 3487 | twobyte_insn: |
3074 | switch (c->b) { | 3488 | switch (c->b) { |
@@ -3091,7 +3505,7 @@ twobyte_insn: | |||
3091 | c->dst.type = OP_NONE; | 3505 | c->dst.type = OP_NONE; |
3092 | break; | 3506 | break; |
3093 | case 2: /* lgdt */ | 3507 | case 2: /* lgdt */ |
3094 | rc = read_descriptor(ctxt, ops, c->src.ptr, | 3508 | rc = read_descriptor(ctxt, ops, c->src.addr.mem, |
3095 | &size, &address, c->op_bytes); | 3509 | &size, &address, c->op_bytes); |
3096 | if (rc != X86EMUL_CONTINUE) | 3510 | if (rc != X86EMUL_CONTINUE) |
3097 | goto done; | 3511 | goto done; |
@@ -3104,14 +3518,12 @@ twobyte_insn: | |||
3104 | switch (c->modrm_rm) { | 3518 | switch (c->modrm_rm) { |
3105 | case 1: | 3519 | case 1: |
3106 | rc = kvm_fix_hypercall(ctxt->vcpu); | 3520 | rc = kvm_fix_hypercall(ctxt->vcpu); |
3107 | if (rc != X86EMUL_CONTINUE) | ||
3108 | goto done; | ||
3109 | break; | 3521 | break; |
3110 | default: | 3522 | default: |
3111 | goto cannot_emulate; | 3523 | goto cannot_emulate; |
3112 | } | 3524 | } |
3113 | } else { | 3525 | } else { |
3114 | rc = read_descriptor(ctxt, ops, c->src.ptr, | 3526 | rc = read_descriptor(ctxt, ops, c->src.addr.mem, |
3115 | &size, &address, | 3527 | &size, &address, |
3116 | c->op_bytes); | 3528 | c->op_bytes); |
3117 | if (rc != X86EMUL_CONTINUE) | 3529 | if (rc != X86EMUL_CONTINUE) |
@@ -3126,7 +3538,7 @@ twobyte_insn: | |||
3126 | c->dst.val = ops->get_cr(0, ctxt->vcpu); | 3538 | c->dst.val = ops->get_cr(0, ctxt->vcpu); |
3127 | break; | 3539 | break; |
3128 | case 6: /* lmsw */ | 3540 | case 6: /* lmsw */ |
3129 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) | | 3541 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) | |
3130 | (c->src.val & 0x0f), ctxt->vcpu); | 3542 | (c->src.val & 0x0f), ctxt->vcpu); |
3131 | c->dst.type = OP_NONE; | 3543 | c->dst.type = OP_NONE; |
3132 | break; | 3544 | break; |
@@ -3134,7 +3546,7 @@ twobyte_insn: | |||
3134 | emulate_ud(ctxt); | 3546 | emulate_ud(ctxt); |
3135 | goto done; | 3547 | goto done; |
3136 | case 7: /* invlpg*/ | 3548 | case 7: /* invlpg*/ |
3137 | emulate_invlpg(ctxt->vcpu, c->modrm_ea); | 3549 | emulate_invlpg(ctxt->vcpu, c->src.addr.mem); |
3138 | /* Disable writeback. */ | 3550 | /* Disable writeback. */ |
3139 | c->dst.type = OP_NONE; | 3551 | c->dst.type = OP_NONE; |
3140 | break; | 3552 | break; |
@@ -3144,23 +3556,16 @@ twobyte_insn: | |||
3144 | break; | 3556 | break; |
3145 | case 0x05: /* syscall */ | 3557 | case 0x05: /* syscall */ |
3146 | rc = emulate_syscall(ctxt, ops); | 3558 | rc = emulate_syscall(ctxt, ops); |
3147 | if (rc != X86EMUL_CONTINUE) | ||
3148 | goto done; | ||
3149 | else | ||
3150 | goto writeback; | ||
3151 | break; | 3559 | break; |
3152 | case 0x06: | 3560 | case 0x06: |
3153 | emulate_clts(ctxt->vcpu); | 3561 | emulate_clts(ctxt->vcpu); |
3154 | c->dst.type = OP_NONE; | ||
3155 | break; | 3562 | break; |
3156 | case 0x09: /* wbinvd */ | 3563 | case 0x09: /* wbinvd */ |
3157 | kvm_emulate_wbinvd(ctxt->vcpu); | 3564 | kvm_emulate_wbinvd(ctxt->vcpu); |
3158 | c->dst.type = OP_NONE; | ||
3159 | break; | 3565 | break; |
3160 | case 0x08: /* invd */ | 3566 | case 0x08: /* invd */ |
3161 | case 0x0d: /* GrpP (prefetch) */ | 3567 | case 0x0d: /* GrpP (prefetch) */ |
3162 | case 0x18: /* Grp16 (prefetch/nop) */ | 3568 | case 0x18: /* Grp16 (prefetch/nop) */ |
3163 | c->dst.type = OP_NONE; | ||
3164 | break; | 3569 | break; |
3165 | case 0x20: /* mov cr, reg */ | 3570 | case 0x20: /* mov cr, reg */ |
3166 | switch (c->modrm_reg) { | 3571 | switch (c->modrm_reg) { |
@@ -3170,8 +3575,7 @@ twobyte_insn: | |||
3170 | emulate_ud(ctxt); | 3575 | emulate_ud(ctxt); |
3171 | goto done; | 3576 | goto done; |
3172 | } | 3577 | } |
3173 | c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu); | 3578 | c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu); |
3174 | c->dst.type = OP_NONE; /* no writeback */ | ||
3175 | break; | 3579 | break; |
3176 | case 0x21: /* mov from dr to reg */ | 3580 | case 0x21: /* mov from dr to reg */ |
3177 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && | 3581 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
@@ -3179,11 +3583,10 @@ twobyte_insn: | |||
3179 | emulate_ud(ctxt); | 3583 | emulate_ud(ctxt); |
3180 | goto done; | 3584 | goto done; |
3181 | } | 3585 | } |
3182 | ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu); | 3586 | ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu); |
3183 | c->dst.type = OP_NONE; /* no writeback */ | ||
3184 | break; | 3587 | break; |
3185 | case 0x22: /* mov reg, cr */ | 3588 | case 0x22: /* mov reg, cr */ |
3186 | if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) { | 3589 | if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) { |
3187 | emulate_gp(ctxt, 0); | 3590 | emulate_gp(ctxt, 0); |
3188 | goto done; | 3591 | goto done; |
3189 | } | 3592 | } |
@@ -3196,7 +3599,7 @@ twobyte_insn: | |||
3196 | goto done; | 3599 | goto done; |
3197 | } | 3600 | } |
3198 | 3601 | ||
3199 | if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] & | 3602 | if (ops->set_dr(c->modrm_reg, c->src.val & |
3200 | ((ctxt->mode == X86EMUL_MODE_PROT64) ? | 3603 | ((ctxt->mode == X86EMUL_MODE_PROT64) ? |
3201 | ~0ULL : ~0U), ctxt->vcpu) < 0) { | 3604 | ~0ULL : ~0U), ctxt->vcpu) < 0) { |
3202 | /* #UD condition is already handled by the code above */ | 3605 | /* #UD condition is already handled by the code above */ |
@@ -3215,7 +3618,6 @@ twobyte_insn: | |||
3215 | goto done; | 3618 | goto done; |
3216 | } | 3619 | } |
3217 | rc = X86EMUL_CONTINUE; | 3620 | rc = X86EMUL_CONTINUE; |
3218 | c->dst.type = OP_NONE; | ||
3219 | break; | 3621 | break; |
3220 | case 0x32: | 3622 | case 0x32: |
3221 | /* rdmsr */ | 3623 | /* rdmsr */ |
@@ -3227,21 +3629,12 @@ twobyte_insn: | |||
3227 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; | 3629 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
3228 | } | 3630 | } |
3229 | rc = X86EMUL_CONTINUE; | 3631 | rc = X86EMUL_CONTINUE; |
3230 | c->dst.type = OP_NONE; | ||
3231 | break; | 3632 | break; |
3232 | case 0x34: /* sysenter */ | 3633 | case 0x34: /* sysenter */ |
3233 | rc = emulate_sysenter(ctxt, ops); | 3634 | rc = emulate_sysenter(ctxt, ops); |
3234 | if (rc != X86EMUL_CONTINUE) | ||
3235 | goto done; | ||
3236 | else | ||
3237 | goto writeback; | ||
3238 | break; | 3635 | break; |
3239 | case 0x35: /* sysexit */ | 3636 | case 0x35: /* sysexit */ |
3240 | rc = emulate_sysexit(ctxt, ops); | 3637 | rc = emulate_sysexit(ctxt, ops); |
3241 | if (rc != X86EMUL_CONTINUE) | ||
3242 | goto done; | ||
3243 | else | ||
3244 | goto writeback; | ||
3245 | break; | 3638 | break; |
3246 | case 0x40 ... 0x4f: /* cmov */ | 3639 | case 0x40 ... 0x4f: /* cmov */ |
3247 | c->dst.val = c->dst.orig_val = c->src.val; | 3640 | c->dst.val = c->dst.orig_val = c->src.val; |
@@ -3251,15 +3644,15 @@ twobyte_insn: | |||
3251 | case 0x80 ... 0x8f: /* jnz rel, etc*/ | 3644 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
3252 | if (test_cc(c->b, ctxt->eflags)) | 3645 | if (test_cc(c->b, ctxt->eflags)) |
3253 | jmp_rel(c, c->src.val); | 3646 | jmp_rel(c, c->src.val); |
3254 | c->dst.type = OP_NONE; | 3647 | break; |
3648 | case 0x90 ... 0x9f: /* setcc r/m8 */ | ||
3649 | c->dst.val = test_cc(c->b, ctxt->eflags); | ||
3255 | break; | 3650 | break; |
3256 | case 0xa0: /* push fs */ | 3651 | case 0xa0: /* push fs */ |
3257 | emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); | 3652 | emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); |
3258 | break; | 3653 | break; |
3259 | case 0xa1: /* pop fs */ | 3654 | case 0xa1: /* pop fs */ |
3260 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); | 3655 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
3261 | if (rc != X86EMUL_CONTINUE) | ||
3262 | goto done; | ||
3263 | break; | 3656 | break; |
3264 | case 0xa3: | 3657 | case 0xa3: |
3265 | bt: /* bt */ | 3658 | bt: /* bt */ |
@@ -3277,13 +3670,9 @@ twobyte_insn: | |||
3277 | break; | 3670 | break; |
3278 | case 0xa9: /* pop gs */ | 3671 | case 0xa9: /* pop gs */ |
3279 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); | 3672 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
3280 | if (rc != X86EMUL_CONTINUE) | ||
3281 | goto done; | ||
3282 | break; | 3673 | break; |
3283 | case 0xab: | 3674 | case 0xab: |
3284 | bts: /* bts */ | 3675 | bts: /* bts */ |
3285 | /* only subword offset */ | ||
3286 | c->src.val &= (c->dst.bytes << 3) - 1; | ||
3287 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); | 3676 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
3288 | break; | 3677 | break; |
3289 | case 0xac: /* shrd imm8, r, r/m */ | 3678 | case 0xac: /* shrd imm8, r, r/m */ |
@@ -3306,15 +3695,22 @@ twobyte_insn: | |||
3306 | } else { | 3695 | } else { |
3307 | /* Failure: write the value we saw to EAX. */ | 3696 | /* Failure: write the value we saw to EAX. */ |
3308 | c->dst.type = OP_REG; | 3697 | c->dst.type = OP_REG; |
3309 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; | 3698 | c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
3310 | } | 3699 | } |
3311 | break; | 3700 | break; |
3701 | case 0xb2: /* lss */ | ||
3702 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS); | ||
3703 | break; | ||
3312 | case 0xb3: | 3704 | case 0xb3: |
3313 | btr: /* btr */ | 3705 | btr: /* btr */ |
3314 | /* only subword offset */ | ||
3315 | c->src.val &= (c->dst.bytes << 3) - 1; | ||
3316 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); | 3706 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
3317 | break; | 3707 | break; |
3708 | case 0xb4: /* lfs */ | ||
3709 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS); | ||
3710 | break; | ||
3711 | case 0xb5: /* lgs */ | ||
3712 | rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS); | ||
3713 | break; | ||
3318 | case 0xb6 ... 0xb7: /* movzx */ | 3714 | case 0xb6 ... 0xb7: /* movzx */ |
3319 | c->dst.bytes = c->op_bytes; | 3715 | c->dst.bytes = c->op_bytes; |
3320 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val | 3716 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
@@ -3334,15 +3730,43 @@ twobyte_insn: | |||
3334 | break; | 3730 | break; |
3335 | case 0xbb: | 3731 | case 0xbb: |
3336 | btc: /* btc */ | 3732 | btc: /* btc */ |
3337 | /* only subword offset */ | ||
3338 | c->src.val &= (c->dst.bytes << 3) - 1; | ||
3339 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); | 3733 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
3340 | break; | 3734 | break; |
3735 | case 0xbc: { /* bsf */ | ||
3736 | u8 zf; | ||
3737 | __asm__ ("bsf %2, %0; setz %1" | ||
3738 | : "=r"(c->dst.val), "=q"(zf) | ||
3739 | : "r"(c->src.val)); | ||
3740 | ctxt->eflags &= ~X86_EFLAGS_ZF; | ||
3741 | if (zf) { | ||
3742 | ctxt->eflags |= X86_EFLAGS_ZF; | ||
3743 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
3744 | } | ||
3745 | break; | ||
3746 | } | ||
3747 | case 0xbd: { /* bsr */ | ||
3748 | u8 zf; | ||
3749 | __asm__ ("bsr %2, %0; setz %1" | ||
3750 | : "=r"(c->dst.val), "=q"(zf) | ||
3751 | : "r"(c->src.val)); | ||
3752 | ctxt->eflags &= ~X86_EFLAGS_ZF; | ||
3753 | if (zf) { | ||
3754 | ctxt->eflags |= X86_EFLAGS_ZF; | ||
3755 | c->dst.type = OP_NONE; /* Disable writeback. */ | ||
3756 | } | ||
3757 | break; | ||
3758 | } | ||
3341 | case 0xbe ... 0xbf: /* movsx */ | 3759 | case 0xbe ... 0xbf: /* movsx */ |
3342 | c->dst.bytes = c->op_bytes; | 3760 | c->dst.bytes = c->op_bytes; |
3343 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : | 3761 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
3344 | (s16) c->src.val; | 3762 | (s16) c->src.val; |
3345 | break; | 3763 | break; |
3764 | case 0xc0 ... 0xc1: /* xadd */ | ||
3765 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); | ||
3766 | /* Write back the register source. */ | ||
3767 | c->src.val = c->dst.orig_val; | ||
3768 | write_register_operand(&c->src); | ||
3769 | break; | ||
3346 | case 0xc3: /* movnti */ | 3770 | case 0xc3: /* movnti */ |
3347 | c->dst.bytes = c->op_bytes; | 3771 | c->dst.bytes = c->op_bytes; |
3348 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : | 3772 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
@@ -3350,10 +3774,14 @@ twobyte_insn: | |||
3350 | break; | 3774 | break; |
3351 | case 0xc7: /* Grp9 (cmpxchg8b) */ | 3775 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
3352 | rc = emulate_grp9(ctxt, ops); | 3776 | rc = emulate_grp9(ctxt, ops); |
3353 | if (rc != X86EMUL_CONTINUE) | ||
3354 | goto done; | ||
3355 | break; | 3777 | break; |
3778 | default: | ||
3779 | goto cannot_emulate; | ||
3356 | } | 3780 | } |
3781 | |||
3782 | if (rc != X86EMUL_CONTINUE) | ||
3783 | goto done; | ||
3784 | |||
3357 | goto writeback; | 3785 | goto writeback; |
3358 | 3786 | ||
3359 | cannot_emulate: | 3787 | cannot_emulate: |