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 | #undef EAX
|
---|
47 | #define EAX (env->regs[R_EAX])
|
---|
48 | #undef ECX
|
---|
49 | #define ECX (env->regs[R_ECX])
|
---|
50 | #undef EDX
|
---|
51 | #define EDX (env->regs[R_EDX])
|
---|
52 | #undef EBX
|
---|
53 | #define EBX (env->regs[R_EBX])
|
---|
54 | #undef ESP
|
---|
55 | #define ESP (env->regs[R_ESP])
|
---|
56 | #undef EBP
|
---|
57 | #define EBP (env->regs[R_EBP])
|
---|
58 | #undef ESI
|
---|
59 | #define ESI (env->regs[R_ESI])
|
---|
60 | #undef EDI
|
---|
61 | #define EDI (env->regs[R_EDI])
|
---|
62 | #undef EIP
|
---|
63 | #define EIP (env->eip)
|
---|
64 | #define DF (env->df)
|
---|
65 |
|
---|
66 | #define CC_SRC (env->cc_src)
|
---|
67 | #define CC_DST (env->cc_dst)
|
---|
68 | #define CC_OP (env->cc_op)
|
---|
69 |
|
---|
70 | /* float macros */
|
---|
71 | #define FT0 (env->ft0)
|
---|
72 | #define ST0 (env->fpregs[env->fpstt].d)
|
---|
73 | #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7].d)
|
---|
74 | #define ST1 ST(1)
|
---|
75 |
|
---|
76 | #include "cpu.h"
|
---|
77 | #include "exec-all.h"
|
---|
78 |
|
---|
79 | /* op_helper.c */
|
---|
80 | void do_interrupt(int intno, int is_int, int error_code,
|
---|
81 | target_ulong next_eip, int is_hw);
|
---|
82 | void do_interrupt_user(int intno, int is_int, int error_code,
|
---|
83 | target_ulong next_eip);
|
---|
84 | void QEMU_NORETURN raise_exception_err(int exception_index, int error_code);
|
---|
85 | void QEMU_NORETURN raise_exception(int exception_index);
|
---|
86 | void do_smm_enter(void);
|
---|
87 |
|
---|
88 | /* n must be a constant to be efficient */
|
---|
89 | static inline target_long lshift(target_long x, int n)
|
---|
90 | {
|
---|
91 | if (n >= 0)
|
---|
92 | return x << n;
|
---|
93 | else
|
---|
94 | return x >> (-n);
|
---|
95 | }
|
---|
96 |
|
---|
97 | #include "helper.h"
|
---|
98 |
|
---|
99 | static inline void svm_check_intercept(uint32_t type)
|
---|
100 | {
|
---|
101 | helper_svm_check_intercept_param(type, 0);
|
---|
102 | }
|
---|
103 |
|
---|
104 | #if !defined(CONFIG_USER_ONLY)
|
---|
105 |
|
---|
106 | #include "softmmu_exec.h"
|
---|
107 |
|
---|
108 | #endif /* !defined(CONFIG_USER_ONLY) */
|
---|
109 |
|
---|
110 | #ifdef USE_X86LDOUBLE
|
---|
111 | /* use long double functions */
|
---|
112 | #define floatx_to_int32 floatx80_to_int32
|
---|
113 | #define floatx_to_int64 floatx80_to_int64
|
---|
114 | #define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
|
---|
115 | #define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
|
---|
116 | #define int32_to_floatx int32_to_floatx80
|
---|
117 | #define int64_to_floatx int64_to_floatx80
|
---|
118 | #define float32_to_floatx float32_to_floatx80
|
---|
119 | #define float64_to_floatx float64_to_floatx80
|
---|
120 | #define floatx_to_float32 floatx80_to_float32
|
---|
121 | #define floatx_to_float64 floatx80_to_float64
|
---|
122 | #define floatx_abs floatx80_abs
|
---|
123 | #define floatx_chs floatx80_chs
|
---|
124 | #define floatx_round_to_int floatx80_round_to_int
|
---|
125 | #define floatx_compare floatx80_compare
|
---|
126 | #define floatx_compare_quiet floatx80_compare_quiet
|
---|
127 | #else
|
---|
128 | #define floatx_to_int32 float64_to_int32
|
---|
129 | #define floatx_to_int64 float64_to_int64
|
---|
130 | #define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
|
---|
131 | #define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
|
---|
132 | #define int32_to_floatx int32_to_float64
|
---|
133 | #define int64_to_floatx int64_to_float64
|
---|
134 | #define float32_to_floatx float32_to_float64
|
---|
135 | #define float64_to_floatx(x, e) (x)
|
---|
136 | #define floatx_to_float32 float64_to_float32
|
---|
137 | #define floatx_to_float64(x, e) (x)
|
---|
138 | #define floatx_abs float64_abs
|
---|
139 | #define floatx_chs float64_chs
|
---|
140 | #define floatx_round_to_int float64_round_to_int
|
---|
141 | #define floatx_compare float64_compare
|
---|
142 | #define floatx_compare_quiet float64_compare_quiet
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | #define RC_MASK 0xc00
|
---|
146 | #define RC_NEAR 0x000
|
---|
147 | #define RC_DOWN 0x400
|
---|
148 | #define RC_UP 0x800
|
---|
149 | #define RC_CHOP 0xc00
|
---|
150 |
|
---|
151 | #define MAXTAN 9223372036854775808.0
|
---|
152 |
|
---|
153 | #ifdef USE_X86LDOUBLE
|
---|
154 |
|
---|
155 | /* only for x86 */
|
---|
156 | typedef union {
|
---|
157 | long double d;
|
---|
158 | struct {
|
---|
159 | unsigned long long lower;
|
---|
160 | unsigned short upper;
|
---|
161 | } l;
|
---|
162 | } CPU86_LDoubleU;
|
---|
163 |
|
---|
164 | /* the following deal with x86 long double-precision numbers */
|
---|
165 | #define MAXEXPD 0x7fff
|
---|
166 | #define EXPBIAS 16383
|
---|
167 | #define EXPD(fp) (fp.l.upper & 0x7fff)
|
---|
168 | #define SIGND(fp) ((fp.l.upper) & 0x8000)
|
---|
169 | #define MANTD(fp) (fp.l.lower)
|
---|
170 | #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
|
---|
171 |
|
---|
172 | #else
|
---|
173 |
|
---|
174 | /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
|
---|
175 | typedef union {
|
---|
176 | double d;
|
---|
177 | #if !defined(HOST_WORDS_BIGENDIAN) && !defined(__arm__)
|
---|
178 | struct {
|
---|
179 | uint32_t lower;
|
---|
180 | int32_t upper;
|
---|
181 | } l;
|
---|
182 | #else
|
---|
183 | struct {
|
---|
184 | int32_t upper;
|
---|
185 | uint32_t lower;
|
---|
186 | } l;
|
---|
187 | #endif
|
---|
188 | #ifndef __arm__
|
---|
189 | int64_t ll;
|
---|
190 | #endif
|
---|
191 | } CPU86_LDoubleU;
|
---|
192 |
|
---|
193 | /* the following deal with IEEE double-precision numbers */
|
---|
194 | #define MAXEXPD 0x7ff
|
---|
195 | #define EXPBIAS 1023
|
---|
196 | #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
|
---|
197 | #define SIGND(fp) ((fp.l.upper) & 0x80000000)
|
---|
198 | #ifdef __arm__
|
---|
199 | #define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
|
---|
200 | #else
|
---|
201 | #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
|
---|
202 | #endif
|
---|
203 | #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | static inline void fpush(void)
|
---|
207 | {
|
---|
208 | env->fpstt = (env->fpstt - 1) & 7;
|
---|
209 | env->fptags[env->fpstt] = 0; /* validate stack entry */
|
---|
210 | }
|
---|
211 |
|
---|
212 | static inline void fpop(void)
|
---|
213 | {
|
---|
214 | env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
|
---|
215 | env->fpstt = (env->fpstt + 1) & 7;
|
---|
216 | }
|
---|
217 |
|
---|
218 | #ifndef USE_X86LDOUBLE
|
---|
219 | static inline CPU86_LDouble helper_fldt(target_ulong ptr)
|
---|
220 | {
|
---|
221 | CPU86_LDoubleU temp;
|
---|
222 | int upper, e;
|
---|
223 | uint64_t ll;
|
---|
224 |
|
---|
225 | /* mantissa */
|
---|
226 | upper = lduw(ptr + 8);
|
---|
227 | /* XXX: handle overflow ? */
|
---|
228 | e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
|
---|
229 | e |= (upper >> 4) & 0x800; /* sign */
|
---|
230 | ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
|
---|
231 | #ifdef __arm__
|
---|
232 | temp.l.upper = (e << 20) | (ll >> 32);
|
---|
233 | temp.l.lower = ll;
|
---|
234 | #else
|
---|
235 | temp.ll = ll | ((uint64_t)e << 52);
|
---|
236 | #endif
|
---|
237 | return temp.d;
|
---|
238 | }
|
---|
239 |
|
---|
240 | static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
|
---|
241 | {
|
---|
242 | CPU86_LDoubleU temp;
|
---|
243 | int e;
|
---|
244 |
|
---|
245 | temp.d = f;
|
---|
246 | /* mantissa */
|
---|
247 | stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
|
---|
248 | /* exponent + sign */
|
---|
249 | e = EXPD(temp) - EXPBIAS + 16383;
|
---|
250 | e |= SIGND(temp) >> 16;
|
---|
251 | stw(ptr + 8, e);
|
---|
252 | }
|
---|
253 | #else
|
---|
254 |
|
---|
255 | /* we use memory access macros */
|
---|
256 |
|
---|
257 | static inline CPU86_LDouble helper_fldt(target_ulong ptr)
|
---|
258 | {
|
---|
259 | CPU86_LDoubleU temp;
|
---|
260 |
|
---|
261 | temp.l.lower = ldq(ptr);
|
---|
262 | temp.l.upper = lduw(ptr + 8);
|
---|
263 | return temp.d;
|
---|
264 | }
|
---|
265 |
|
---|
266 | static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
|
---|
267 | {
|
---|
268 | CPU86_LDoubleU temp;
|
---|
269 |
|
---|
270 | temp.d = f;
|
---|
271 | stq(ptr, temp.l.lower);
|
---|
272 | stw(ptr + 8, temp.l.upper);
|
---|
273 | }
|
---|
274 |
|
---|
275 | #endif /* USE_X86LDOUBLE */
|
---|
276 |
|
---|
277 | #define FPUS_IE (1 << 0)
|
---|
278 | #define FPUS_DE (1 << 1)
|
---|
279 | #define FPUS_ZE (1 << 2)
|
---|
280 | #define FPUS_OE (1 << 3)
|
---|
281 | #define FPUS_UE (1 << 4)
|
---|
282 | #define FPUS_PE (1 << 5)
|
---|
283 | #define FPUS_SF (1 << 6)
|
---|
284 | #define FPUS_SE (1 << 7)
|
---|
285 | #define FPUS_B (1 << 15)
|
---|
286 |
|
---|
287 | #define FPUC_EM 0x3f
|
---|
288 |
|
---|
289 | static inline uint32_t compute_eflags(void)
|
---|
290 | {
|
---|
291 | return env->eflags | helper_cc_compute_all(CC_OP) | (DF & DF_MASK);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
|
---|
295 | static inline void load_eflags(int eflags, int update_mask)
|
---|
296 | {
|
---|
297 | CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
|
---|
298 | DF = 1 - (2 * ((eflags >> 10) & 1));
|
---|
299 | env->eflags = (env->eflags & ~update_mask) |
|
---|
300 | (eflags & update_mask) | 0x2;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static inline void env_to_regs(void)
|
---|
304 | {
|
---|
305 | #ifdef reg_EAX
|
---|
306 | EAX = env->regs[R_EAX];
|
---|
307 | #endif
|
---|
308 | #ifdef reg_ECX
|
---|
309 | ECX = env->regs[R_ECX];
|
---|
310 | #endif
|
---|
311 | #ifdef reg_EDX
|
---|
312 | EDX = env->regs[R_EDX];
|
---|
313 | #endif
|
---|
314 | #ifdef reg_EBX
|
---|
315 | EBX = env->regs[R_EBX];
|
---|
316 | #endif
|
---|
317 | #ifdef reg_ESP
|
---|
318 | ESP = env->regs[R_ESP];
|
---|
319 | #endif
|
---|
320 | #ifdef reg_EBP
|
---|
321 | EBP = env->regs[R_EBP];
|
---|
322 | #endif
|
---|
323 | #ifdef reg_ESI
|
---|
324 | ESI = env->regs[R_ESI];
|
---|
325 | #endif
|
---|
326 | #ifdef reg_EDI
|
---|
327 | EDI = env->regs[R_EDI];
|
---|
328 | #endif
|
---|
329 | }
|
---|
330 |
|
---|
331 | static inline void regs_to_env(void)
|
---|
332 | {
|
---|
333 | #ifdef reg_EAX
|
---|
334 | env->regs[R_EAX] = EAX;
|
---|
335 | #endif
|
---|
336 | #ifdef reg_ECX
|
---|
337 | env->regs[R_ECX] = ECX;
|
---|
338 | #endif
|
---|
339 | #ifdef reg_EDX
|
---|
340 | env->regs[R_EDX] = EDX;
|
---|
341 | #endif
|
---|
342 | #ifdef reg_EBX
|
---|
343 | env->regs[R_EBX] = EBX;
|
---|
344 | #endif
|
---|
345 | #ifdef reg_ESP
|
---|
346 | env->regs[R_ESP] = ESP;
|
---|
347 | #endif
|
---|
348 | #ifdef reg_EBP
|
---|
349 | env->regs[R_EBP] = EBP;
|
---|
350 | #endif
|
---|
351 | #ifdef reg_ESI
|
---|
352 | env->regs[R_ESI] = ESI;
|
---|
353 | #endif
|
---|
354 | #ifdef reg_EDI
|
---|
355 | env->regs[R_EDI] = EDI;
|
---|
356 | #endif
|
---|
357 | }
|
---|
358 |
|
---|
359 | static inline int cpu_has_work(CPUState *env)
|
---|
360 | {
|
---|
361 | int work;
|
---|
362 |
|
---|
363 | work = (env->interrupt_request & CPU_INTERRUPT_HARD) &&
|
---|
364 | (env->eflags & IF_MASK);
|
---|
365 | work |= env->interrupt_request & CPU_INTERRUPT_NMI;
|
---|
366 | work |= env->interrupt_request & CPU_INTERRUPT_INIT;
|
---|
367 | work |= env->interrupt_request & CPU_INTERRUPT_SIPI;
|
---|
368 |
|
---|
369 | return work;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static inline int cpu_halted(CPUState *env) {
|
---|
373 | /* handle exit of HALTED state */
|
---|
374 | if (!env->halted)
|
---|
375 | return 0;
|
---|
376 | /* disable halt condition */
|
---|
377 | if (cpu_has_work(env)) {
|
---|
378 | env->halted = 0;
|
---|
379 | return 0;
|
---|
380 | }
|
---|
381 | return EXCP_HALTED;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* load efer and update the corresponding hflags. XXX: do consistency
|
---|
385 | checks with cpuid bits ? */
|
---|
386 | static inline void cpu_load_efer(CPUState *env, uint64_t val)
|
---|
387 | {
|
---|
388 | env->efer = val;
|
---|
389 | env->hflags &= ~(HF_LMA_MASK | HF_SVME_MASK);
|
---|
390 | if (env->efer & MSR_EFER_LMA)
|
---|
391 | env->hflags |= HF_LMA_MASK;
|
---|
392 | if (env->efer & MSR_EFER_SVME)
|
---|
393 | env->hflags |= HF_SVME_MASK;
|
---|
394 | }
|
---|