1 | /*
|
---|
2 | * i386 execution defines
|
---|
3 | *
|
---|
4 | * Copyright (c) 2003 Fabrice Bellard
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*
|
---|
21 | * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
22 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
23 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
24 | * a choice of LGPL license versions is made available with the language indicating
|
---|
25 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
26 | * of the LGPL is applied is otherwise unspecified.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include "config.h"
|
---|
30 | #include "dyngen-exec.h"
|
---|
31 |
|
---|
32 | /* XXX: factorize this mess */
|
---|
33 | #ifdef TARGET_X86_64
|
---|
34 | #define TARGET_LONG_BITS 64
|
---|
35 | #else
|
---|
36 | #define TARGET_LONG_BITS 32
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include "cpu-defs.h"
|
---|
40 |
|
---|
41 | register struct CPUX86State *env asm(AREG0);
|
---|
42 |
|
---|
43 | #include "qemu-common.h"
|
---|
44 | #include "qemu-log.h"
|
---|
45 |
|
---|
46 | #define EAX (env->regs[R_EAX])
|
---|
47 | #define ECX (env->regs[R_ECX])
|
---|
48 | #define EDX (env->regs[R_EDX])
|
---|
49 | #define EBX (env->regs[R_EBX])
|
---|
50 | #define ESP (env->regs[R_ESP])
|
---|
51 | #define EBP (env->regs[R_EBP])
|
---|
52 | #define ESI (env->regs[R_ESI])
|
---|
53 | #define EDI (env->regs[R_EDI])
|
---|
54 | #define EIP (env->eip)
|
---|
55 | #define DF (env->df)
|
---|
56 |
|
---|
57 | #define CC_SRC (env->cc_src)
|
---|
58 | #define CC_DST (env->cc_dst)
|
---|
59 | #define CC_OP (env->cc_op)
|
---|
60 |
|
---|
61 | /* float macros */
|
---|
62 | #define FT0 (env->ft0)
|
---|
63 | #define ST0 (env->fpregs[env->fpstt].d)
|
---|
64 | #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7].d)
|
---|
65 | #define ST1 ST(1)
|
---|
66 |
|
---|
67 | #include "cpu.h"
|
---|
68 | #include "exec-all.h"
|
---|
69 |
|
---|
70 | /* op_helper.c */
|
---|
71 | void do_interrupt(int intno, int is_int, int error_code,
|
---|
72 | target_ulong next_eip, int is_hw);
|
---|
73 | void do_interrupt_user(int intno, int is_int, int error_code,
|
---|
74 | target_ulong next_eip);
|
---|
75 | void QEMU_NORETURN raise_exception_err(int exception_index, int error_code);
|
---|
76 | void QEMU_NORETURN raise_exception(int exception_index);
|
---|
77 | void do_smm_enter(void);
|
---|
78 |
|
---|
79 | /* n must be a constant to be efficient */
|
---|
80 | static inline target_long lshift(target_long x, int n)
|
---|
81 | {
|
---|
82 | if (n >= 0)
|
---|
83 | return x << n;
|
---|
84 | else
|
---|
85 | return x >> (-n);
|
---|
86 | }
|
---|
87 |
|
---|
88 | #include "helper.h"
|
---|
89 |
|
---|
90 | static inline void svm_check_intercept(uint32_t type)
|
---|
91 | {
|
---|
92 | helper_svm_check_intercept_param(type, 0);
|
---|
93 | }
|
---|
94 |
|
---|
95 | #if !defined(CONFIG_USER_ONLY)
|
---|
96 |
|
---|
97 | #include "softmmu_exec.h"
|
---|
98 |
|
---|
99 | #endif /* !defined(CONFIG_USER_ONLY) */
|
---|
100 |
|
---|
101 | #ifdef USE_X86LDOUBLE
|
---|
102 | /* use long double functions */
|
---|
103 | #define floatx_to_int32 floatx80_to_int32
|
---|
104 | #define floatx_to_int64 floatx80_to_int64
|
---|
105 | #define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
|
---|
106 | #define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
|
---|
107 | #define int32_to_floatx int32_to_floatx80
|
---|
108 | #define int64_to_floatx int64_to_floatx80
|
---|
109 | #define float32_to_floatx float32_to_floatx80
|
---|
110 | #define float64_to_floatx float64_to_floatx80
|
---|
111 | #define floatx_to_float32 floatx80_to_float32
|
---|
112 | #define floatx_to_float64 floatx80_to_float64
|
---|
113 | #define floatx_abs floatx80_abs
|
---|
114 | #define floatx_chs floatx80_chs
|
---|
115 | #define floatx_round_to_int floatx80_round_to_int
|
---|
116 | #define floatx_compare floatx80_compare
|
---|
117 | #define floatx_compare_quiet floatx80_compare_quiet
|
---|
118 | #ifdef VBOX
|
---|
119 | #undef sin
|
---|
120 | #undef cos
|
---|
121 | #undef sqrt
|
---|
122 | #undef pow
|
---|
123 | #undef log
|
---|
124 | #undef tan
|
---|
125 | #undef atan2
|
---|
126 | #undef floor
|
---|
127 | #undef ceil
|
---|
128 | #undef ldexp
|
---|
129 | #endif /* !VBOX */
|
---|
130 | #if !defined(VBOX) || !defined(_MSC_VER)
|
---|
131 | #define sin sinl
|
---|
132 | #define cos cosl
|
---|
133 | #define sqrt sqrtl
|
---|
134 | #define pow powl
|
---|
135 | #define log logl
|
---|
136 | #define tan tanl
|
---|
137 | #define atan2 atan2l
|
---|
138 | #define floor floorl
|
---|
139 | #define ceil ceill
|
---|
140 | #define ldexp ldexpl
|
---|
141 | #endif
|
---|
142 | #else
|
---|
143 | #define floatx_to_int32 float64_to_int32
|
---|
144 | #define floatx_to_int64 float64_to_int64
|
---|
145 | #define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
|
---|
146 | #define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
|
---|
147 | #define int32_to_floatx int32_to_float64
|
---|
148 | #define int64_to_floatx int64_to_float64
|
---|
149 | #define float32_to_floatx float32_to_float64
|
---|
150 | #define float64_to_floatx(x, e) (x)
|
---|
151 | #define floatx_to_float32 float64_to_float32
|
---|
152 | #define floatx_to_float64(x, e) (x)
|
---|
153 | #define floatx_abs float64_abs
|
---|
154 | #define floatx_chs float64_chs
|
---|
155 | #define floatx_round_to_int float64_round_to_int
|
---|
156 | #define floatx_compare float64_compare
|
---|
157 | #define floatx_compare_quiet float64_compare_quiet
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | #ifdef VBOX
|
---|
161 | extern CPU86_LDouble sin(CPU86_LDouble x);
|
---|
162 | extern CPU86_LDouble cos(CPU86_LDouble x);
|
---|
163 | extern CPU86_LDouble sqrt(CPU86_LDouble x);
|
---|
164 | extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
|
---|
165 | extern CPU86_LDouble log(CPU86_LDouble x);
|
---|
166 | extern CPU86_LDouble tan(CPU86_LDouble x);
|
---|
167 | extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
|
---|
168 | extern CPU86_LDouble floor(CPU86_LDouble x);
|
---|
169 | extern CPU86_LDouble ceil(CPU86_LDouble x);
|
---|
170 | #endif /* VBOX */
|
---|
171 |
|
---|
172 | #define RC_MASK 0xc00
|
---|
173 | #define RC_NEAR 0x000
|
---|
174 | #define RC_DOWN 0x400
|
---|
175 | #define RC_UP 0x800
|
---|
176 | #define RC_CHOP 0xc00
|
---|
177 |
|
---|
178 | #define MAXTAN 9223372036854775808.0
|
---|
179 |
|
---|
180 | #ifdef USE_X86LDOUBLE
|
---|
181 |
|
---|
182 | /* only for x86 */
|
---|
183 | typedef union {
|
---|
184 | long double d;
|
---|
185 | struct {
|
---|
186 | unsigned long long lower;
|
---|
187 | unsigned short upper;
|
---|
188 | } l;
|
---|
189 | } CPU86_LDoubleU;
|
---|
190 |
|
---|
191 | /* the following deal with x86 long double-precision numbers */
|
---|
192 | #define MAXEXPD 0x7fff
|
---|
193 | #define EXPBIAS 16383
|
---|
194 | #define EXPD(fp) (fp.l.upper & 0x7fff)
|
---|
195 | #define SIGND(fp) ((fp.l.upper) & 0x8000)
|
---|
196 | #define MANTD(fp) (fp.l.lower)
|
---|
197 | #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
|
---|
198 |
|
---|
199 | #else
|
---|
200 |
|
---|
201 | /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
|
---|
202 | typedef union {
|
---|
203 | double d;
|
---|
204 | #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
|
---|
205 | struct {
|
---|
206 | uint32_t lower;
|
---|
207 | int32_t upper;
|
---|
208 | } l;
|
---|
209 | #else
|
---|
210 | struct {
|
---|
211 | int32_t upper;
|
---|
212 | uint32_t lower;
|
---|
213 | } l;
|
---|
214 | #endif
|
---|
215 | #ifndef __arm__
|
---|
216 | int64_t ll;
|
---|
217 | #endif
|
---|
218 | } CPU86_LDoubleU;
|
---|
219 |
|
---|
220 | /* the following deal with IEEE double-precision numbers */
|
---|
221 | #define MAXEXPD 0x7ff
|
---|
222 | #define EXPBIAS 1023
|
---|
223 | #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
|
---|
224 | #define SIGND(fp) ((fp.l.upper) & 0x80000000)
|
---|
225 | #ifdef __arm__
|
---|
226 | #define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
|
---|
227 | #else
|
---|
228 | #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
|
---|
229 | #endif
|
---|
230 | #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
|
---|
231 | #endif
|
---|
232 |
|
---|
233 | static inline void fpush(void)
|
---|
234 | {
|
---|
235 | env->fpstt = (env->fpstt - 1) & 7;
|
---|
236 | env->fptags[env->fpstt] = 0; /* validate stack entry */
|
---|
237 | }
|
---|
238 |
|
---|
239 | static inline void fpop(void)
|
---|
240 | {
|
---|
241 | env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
|
---|
242 | env->fpstt = (env->fpstt + 1) & 7;
|
---|
243 | }
|
---|
244 |
|
---|
245 | #ifndef USE_X86LDOUBLE
|
---|
246 | static inline CPU86_LDouble helper_fldt(target_ulong ptr)
|
---|
247 | {
|
---|
248 | CPU86_LDoubleU temp;
|
---|
249 | int upper, e;
|
---|
250 | uint64_t ll;
|
---|
251 |
|
---|
252 | /* mantissa */
|
---|
253 | upper = lduw(ptr + 8);
|
---|
254 | /* XXX: handle overflow ? */
|
---|
255 | e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
|
---|
256 | e |= (upper >> 4) & 0x800; /* sign */
|
---|
257 | ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
|
---|
258 | #ifdef __arm__
|
---|
259 | temp.l.upper = (e << 20) | (ll >> 32);
|
---|
260 | temp.l.lower = ll;
|
---|
261 | #else
|
---|
262 | temp.ll = ll | ((uint64_t)e << 52);
|
---|
263 | #endif
|
---|
264 | return temp.d;
|
---|
265 | }
|
---|
266 |
|
---|
267 | static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
|
---|
268 | {
|
---|
269 | CPU86_LDoubleU temp;
|
---|
270 | int e;
|
---|
271 |
|
---|
272 | temp.d = f;
|
---|
273 | /* mantissa */
|
---|
274 | stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
|
---|
275 | /* exponent + sign */
|
---|
276 | e = EXPD(temp) - EXPBIAS + 16383;
|
---|
277 | e |= SIGND(temp) >> 16;
|
---|
278 | stw(ptr + 8, e);
|
---|
279 | }
|
---|
280 | #else
|
---|
281 |
|
---|
282 | /* we use memory access macros */
|
---|
283 |
|
---|
284 | static inline CPU86_LDouble helper_fldt(target_ulong ptr)
|
---|
285 | {
|
---|
286 | CPU86_LDoubleU temp;
|
---|
287 |
|
---|
288 | temp.l.lower = ldq(ptr);
|
---|
289 | temp.l.upper = lduw(ptr + 8);
|
---|
290 | return temp.d;
|
---|
291 | }
|
---|
292 |
|
---|
293 | static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
|
---|
294 | {
|
---|
295 | CPU86_LDoubleU temp;
|
---|
296 |
|
---|
297 | temp.d = f;
|
---|
298 | stq(ptr, temp.l.lower);
|
---|
299 | stw(ptr + 8, temp.l.upper);
|
---|
300 | }
|
---|
301 |
|
---|
302 | #endif /* USE_X86LDOUBLE */
|
---|
303 |
|
---|
304 | #define FPUS_IE (1 << 0)
|
---|
305 | #define FPUS_DE (1 << 1)
|
---|
306 | #define FPUS_ZE (1 << 2)
|
---|
307 | #define FPUS_OE (1 << 3)
|
---|
308 | #define FPUS_UE (1 << 4)
|
---|
309 | #define FPUS_PE (1 << 5)
|
---|
310 | #define FPUS_SF (1 << 6)
|
---|
311 | #define FPUS_SE (1 << 7)
|
---|
312 | #define FPUS_B (1 << 15)
|
---|
313 |
|
---|
314 | #define FPUC_EM 0x3f
|
---|
315 |
|
---|
316 | static inline uint32_t compute_eflags(void)
|
---|
317 | {
|
---|
318 | return env->eflags | helper_cc_compute_all(CC_OP) | (DF & DF_MASK);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
|
---|
322 | static inline void load_eflags(int eflags, int update_mask)
|
---|
323 | {
|
---|
324 | CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
|
---|
325 | DF = 1 - (2 * ((eflags >> 10) & 1));
|
---|
326 | env->eflags = (env->eflags & ~update_mask) |
|
---|
327 | (eflags & update_mask) | 0x2;
|
---|
328 | }
|
---|
329 |
|
---|
330 | static inline void env_to_regs(void)
|
---|
331 | {
|
---|
332 | #ifdef reg_EAX
|
---|
333 | EAX = env->regs[R_EAX];
|
---|
334 | #endif
|
---|
335 | #ifdef reg_ECX
|
---|
336 | ECX = env->regs[R_ECX];
|
---|
337 | #endif
|
---|
338 | #ifdef reg_EDX
|
---|
339 | EDX = env->regs[R_EDX];
|
---|
340 | #endif
|
---|
341 | #ifdef reg_EBX
|
---|
342 | EBX = env->regs[R_EBX];
|
---|
343 | #endif
|
---|
344 | #ifdef reg_ESP
|
---|
345 | ESP = env->regs[R_ESP];
|
---|
346 | #endif
|
---|
347 | #ifdef reg_EBP
|
---|
348 | EBP = env->regs[R_EBP];
|
---|
349 | #endif
|
---|
350 | #ifdef reg_ESI
|
---|
351 | ESI = env->regs[R_ESI];
|
---|
352 | #endif
|
---|
353 | #ifdef reg_EDI
|
---|
354 | EDI = env->regs[R_EDI];
|
---|
355 | #endif
|
---|
356 | }
|
---|
357 |
|
---|
358 | static inline void regs_to_env(void)
|
---|
359 | {
|
---|
360 | #ifdef reg_EAX
|
---|
361 | env->regs[R_EAX] = EAX;
|
---|
362 | #endif
|
---|
363 | #ifdef reg_ECX
|
---|
364 | env->regs[R_ECX] = ECX;
|
---|
365 | #endif
|
---|
366 | #ifdef reg_EDX
|
---|
367 | env->regs[R_EDX] = EDX;
|
---|
368 | #endif
|
---|
369 | #ifdef reg_EBX
|
---|
370 | env->regs[R_EBX] = EBX;
|
---|
371 | #endif
|
---|
372 | #ifdef reg_ESP
|
---|
373 | env->regs[R_ESP] = ESP;
|
---|
374 | #endif
|
---|
375 | #ifdef reg_EBP
|
---|
376 | env->regs[R_EBP] = EBP;
|
---|
377 | #endif
|
---|
378 | #ifdef reg_ESI
|
---|
379 | env->regs[R_ESI] = ESI;
|
---|
380 | #endif
|
---|
381 | #ifdef reg_EDI
|
---|
382 | env->regs[R_EDI] = EDI;
|
---|
383 | #endif
|
---|
384 | }
|
---|
385 |
|
---|
386 | static inline int cpu_has_work(CPUState *env)
|
---|
387 | {
|
---|
388 | int work;
|
---|
389 |
|
---|
390 | work = (env->interrupt_request & CPU_INTERRUPT_HARD) &&
|
---|
391 | (env->eflags & IF_MASK);
|
---|
392 | work |= env->interrupt_request & CPU_INTERRUPT_NMI;
|
---|
393 | work |= env->interrupt_request & CPU_INTERRUPT_INIT;
|
---|
394 | work |= env->interrupt_request & CPU_INTERRUPT_SIPI;
|
---|
395 |
|
---|
396 | return work;
|
---|
397 | }
|
---|
398 |
|
---|
399 | static inline int cpu_halted(CPUState *env) {
|
---|
400 | /* handle exit of HALTED state */
|
---|
401 | if (!env->halted)
|
---|
402 | return 0;
|
---|
403 | /* disable halt condition */
|
---|
404 | if (cpu_has_work(env)) {
|
---|
405 | env->halted = 0;
|
---|
406 | return 0;
|
---|
407 | }
|
---|
408 | return EXCP_HALTED;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* load efer and update the corresponding hflags. XXX: do consistency
|
---|
412 | checks with cpuid bits ? */
|
---|
413 | static inline void cpu_load_efer(CPUState *env, uint64_t val)
|
---|
414 | {
|
---|
415 | env->efer = val;
|
---|
416 | env->hflags &= ~(HF_LMA_MASK | HF_SVME_MASK);
|
---|
417 | if (env->efer & MSR_EFER_LMA)
|
---|
418 | env->hflags |= HF_LMA_MASK;
|
---|
419 | if (env->efer & MSR_EFER_SVME)
|
---|
420 | env->hflags |= HF_SVME_MASK;
|
---|
421 | }
|
---|