]> git.proxmox.com Git - mirror_qemu.git/blame - target-i386/helper.c
fpu fixes (Jocelyn Mayer) - soft float support
[mirror_qemu.git] / target-i386 / helper.c
CommitLineData
2c0262af
FB
1/*
2 * i386 helpers
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "exec.h"
21
f3f2d9be
FB
22//#define DEBUG_PCALL
23
8145122b
FB
24#if 0
25#define raise_exception_err(a, b)\
26do {\
2ee73ac3 27 fprintf(logfile, "raise_exception line=%d\n", __LINE__);\
8145122b
FB
28 (raise_exception_err)(a, b);\
29} while (0)
30#endif
31
2c0262af
FB
32const uint8_t parity_table[256] = {
33 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
34 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
35 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
36 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
37 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
38 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
39 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
40 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
41 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
42 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
43 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
44 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
45 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
46 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
47 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
48 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
49 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
50 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
51 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
52 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
53 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
54 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
55 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
56 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
57 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
58 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
59 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
60 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
61 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
62 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
63 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
64 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
65};
66
67/* modulo 17 table */
68const uint8_t rclw_table[32] = {
69 0, 1, 2, 3, 4, 5, 6, 7,
70 8, 9,10,11,12,13,14,15,
71 16, 0, 1, 2, 3, 4, 5, 6,
72 7, 8, 9,10,11,12,13,14,
73};
74
75/* modulo 9 table */
76const uint8_t rclb_table[32] = {
77 0, 1, 2, 3, 4, 5, 6, 7,
78 8, 0, 1, 2, 3, 4, 5, 6,
79 7, 8, 0, 1, 2, 3, 4, 5,
80 6, 7, 8, 0, 1, 2, 3, 4,
81};
82
83const CPU86_LDouble f15rk[7] =
84{
85 0.00000000000000000000L,
86 1.00000000000000000000L,
87 3.14159265358979323851L, /*pi*/
88 0.30102999566398119523L, /*lg2*/
89 0.69314718055994530943L, /*ln2*/
90 1.44269504088896340739L, /*l2e*/
91 3.32192809488736234781L, /*l2t*/
92};
93
94/* thread support */
95
96spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
97
98void cpu_lock(void)
99{
100 spin_lock(&global_cpu_lock);
101}
102
103void cpu_unlock(void)
104{
105 spin_unlock(&global_cpu_lock);
106}
107
108void cpu_loop_exit(void)
109{
110 /* NOTE: the register at this point must be saved by hand because
111 longjmp restore them */
0d1a29f9 112 regs_to_env();
2c0262af
FB
113 longjmp(env->jmp_env, 1);
114}
115
7e84c249
FB
116/* return non zero if error */
117static inline int load_segment(uint32_t *e1_ptr, uint32_t *e2_ptr,
118 int selector)
119{
120 SegmentCache *dt;
121 int index;
14ce26e7 122 target_ulong ptr;
7e84c249
FB
123
124 if (selector & 0x4)
125 dt = &env->ldt;
126 else
127 dt = &env->gdt;
128 index = selector & ~7;
129 if ((index + 7) > dt->limit)
130 return -1;
131 ptr = dt->base + index;
132 *e1_ptr = ldl_kernel(ptr);
133 *e2_ptr = ldl_kernel(ptr + 4);
134 return 0;
135}
136
137static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
138{
139 unsigned int limit;
140 limit = (e1 & 0xffff) | (e2 & 0x000f0000);
141 if (e2 & DESC_G_MASK)
142 limit = (limit << 12) | 0xfff;
143 return limit;
144}
145
14ce26e7 146static inline uint32_t get_seg_base(uint32_t e1, uint32_t e2)
7e84c249 147{
14ce26e7 148 return ((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
7e84c249
FB
149}
150
151static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, uint32_t e2)
152{
153 sc->base = get_seg_base(e1, e2);
154 sc->limit = get_seg_limit(e1, e2);
155 sc->flags = e2;
156}
157
158/* init the segment cache in vm86 mode. */
159static inline void load_seg_vm(int seg, int selector)
160{
161 selector &= 0xffff;
162 cpu_x86_load_seg_cache(env, seg, selector,
14ce26e7 163 (selector << 4), 0xffff, 0);
7e84c249
FB
164}
165
2c0262af
FB
166static inline void get_ss_esp_from_tss(uint32_t *ss_ptr,
167 uint32_t *esp_ptr, int dpl)
168{
169 int type, index, shift;
170
171#if 0
172 {
173 int i;
174 printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
175 for(i=0;i<env->tr.limit;i++) {
176 printf("%02x ", env->tr.base[i]);
177 if ((i & 7) == 7) printf("\n");
178 }
179 printf("\n");
180 }
181#endif
182
183 if (!(env->tr.flags & DESC_P_MASK))
184 cpu_abort(env, "invalid tss");
185 type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
186 if ((type & 7) != 1)
187 cpu_abort(env, "invalid tss type");
188 shift = type >> 3;
189 index = (dpl * 4 + 2) << shift;
190 if (index + (4 << shift) - 1 > env->tr.limit)
191 raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
192 if (shift == 0) {
61382a50
FB
193 *esp_ptr = lduw_kernel(env->tr.base + index);
194 *ss_ptr = lduw_kernel(env->tr.base + index + 2);
2c0262af 195 } else {
61382a50
FB
196 *esp_ptr = ldl_kernel(env->tr.base + index);
197 *ss_ptr = lduw_kernel(env->tr.base + index + 4);
2c0262af
FB
198 }
199}
200
7e84c249
FB
201/* XXX: merge with load_seg() */
202static void tss_load_seg(int seg_reg, int selector)
203{
204 uint32_t e1, e2;
205 int rpl, dpl, cpl;
206
207 if ((selector & 0xfffc) != 0) {
208 if (load_segment(&e1, &e2, selector) != 0)
209 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
210 if (!(e2 & DESC_S_MASK))
211 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
212 rpl = selector & 3;
213 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
214 cpl = env->hflags & HF_CPL_MASK;
215 if (seg_reg == R_CS) {
216 if (!(e2 & DESC_CS_MASK))
217 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
218 if (dpl != rpl)
219 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
220 if ((e2 & DESC_C_MASK) && dpl > rpl)
221 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
222
223 } else if (seg_reg == R_SS) {
224 /* SS must be writable data */
225 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
226 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
227 if (dpl != cpl || dpl != rpl)
228 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
229 } else {
230 /* not readable code */
231 if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK))
232 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
233 /* if data or non conforming code, checks the rights */
234 if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
235 if (dpl < cpl || dpl < rpl)
236 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
237 }
238 }
239 if (!(e2 & DESC_P_MASK))
240 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
241 cpu_x86_load_seg_cache(env, seg_reg, selector,
242 get_seg_base(e1, e2),
243 get_seg_limit(e1, e2),
244 e2);
245 } else {
246 if (seg_reg == R_SS || seg_reg == R_CS)
247 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
248 }
249}
250
251#define SWITCH_TSS_JMP 0
252#define SWITCH_TSS_IRET 1
253#define SWITCH_TSS_CALL 2
254
255/* XXX: restore CPU state in registers (PowerPC case) */
256static void switch_tss(int tss_selector,
883da8e2
FB
257 uint32_t e1, uint32_t e2, int source,
258 uint32_t next_eip)
2c0262af 259{
7e84c249 260 int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
14ce26e7 261 target_ulong tss_base;
7e84c249
FB
262 uint32_t new_regs[8], new_segs[6];
263 uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
264 uint32_t old_eflags, eflags_mask;
2c0262af
FB
265 SegmentCache *dt;
266 int index;
14ce26e7 267 target_ulong ptr;
2c0262af 268
7e84c249 269 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
dc6f57fd 270#ifdef DEBUG_PCALL
e19e89a5 271 if (loglevel & CPU_LOG_PCALL)
dc6f57fd
FB
272 fprintf(logfile, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, source);
273#endif
7e84c249
FB
274
275 /* if task gate, we read the TSS segment and we load it */
276 if (type == 5) {
277 if (!(e2 & DESC_P_MASK))
278 raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
279 tss_selector = e1 >> 16;
280 if (tss_selector & 4)
281 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
282 if (load_segment(&e1, &e2, tss_selector) != 0)
283 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
284 if (e2 & DESC_S_MASK)
285 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
286 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
287 if ((type & 7) != 1)
288 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
289 }
290
291 if (!(e2 & DESC_P_MASK))
292 raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
293
294 if (type & 8)
295 tss_limit_max = 103;
2c0262af 296 else
7e84c249
FB
297 tss_limit_max = 43;
298 tss_limit = get_seg_limit(e1, e2);
299 tss_base = get_seg_base(e1, e2);
300 if ((tss_selector & 4) != 0 ||
301 tss_limit < tss_limit_max)
302 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
303 old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
304 if (old_type & 8)
305 old_tss_limit_max = 103;
306 else
307 old_tss_limit_max = 43;
308
309 /* read all the registers from the new TSS */
310 if (type & 8) {
311 /* 32 bit */
312 new_cr3 = ldl_kernel(tss_base + 0x1c);
313 new_eip = ldl_kernel(tss_base + 0x20);
314 new_eflags = ldl_kernel(tss_base + 0x24);
315 for(i = 0; i < 8; i++)
316 new_regs[i] = ldl_kernel(tss_base + (0x28 + i * 4));
317 for(i = 0; i < 6; i++)
318 new_segs[i] = lduw_kernel(tss_base + (0x48 + i * 4));
319 new_ldt = lduw_kernel(tss_base + 0x60);
320 new_trap = ldl_kernel(tss_base + 0x64);
321 } else {
322 /* 16 bit */
323 new_cr3 = 0;
324 new_eip = lduw_kernel(tss_base + 0x0e);
325 new_eflags = lduw_kernel(tss_base + 0x10);
326 for(i = 0; i < 8; i++)
327 new_regs[i] = lduw_kernel(tss_base + (0x12 + i * 2)) | 0xffff0000;
328 for(i = 0; i < 4; i++)
329 new_segs[i] = lduw_kernel(tss_base + (0x22 + i * 4));
330 new_ldt = lduw_kernel(tss_base + 0x2a);
331 new_segs[R_FS] = 0;
332 new_segs[R_GS] = 0;
333 new_trap = 0;
334 }
335
336 /* NOTE: we must avoid memory exceptions during the task switch,
337 so we make dummy accesses before */
338 /* XXX: it can still fail in some cases, so a bigger hack is
339 necessary to valid the TLB after having done the accesses */
340
341 v1 = ldub_kernel(env->tr.base);
342 v2 = ldub(env->tr.base + old_tss_limit_max);
343 stb_kernel(env->tr.base, v1);
344 stb_kernel(env->tr.base + old_tss_limit_max, v2);
345
346 /* clear busy bit (it is restartable) */
347 if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
14ce26e7 348 target_ulong ptr;
7e84c249 349 uint32_t e2;
883da8e2 350 ptr = env->gdt.base + (env->tr.selector & ~7);
7e84c249
FB
351 e2 = ldl_kernel(ptr + 4);
352 e2 &= ~DESC_TSS_BUSY_MASK;
353 stl_kernel(ptr + 4, e2);
354 }
355 old_eflags = compute_eflags();
356 if (source == SWITCH_TSS_IRET)
357 old_eflags &= ~NT_MASK;
358
359 /* save the current state in the old TSS */
360 if (type & 8) {
361 /* 32 bit */
883da8e2 362 stl_kernel(env->tr.base + 0x20, next_eip);
7e84c249 363 stl_kernel(env->tr.base + 0x24, old_eflags);
0d1a29f9
FB
364 stl_kernel(env->tr.base + (0x28 + 0 * 4), EAX);
365 stl_kernel(env->tr.base + (0x28 + 1 * 4), ECX);
366 stl_kernel(env->tr.base + (0x28 + 2 * 4), EDX);
367 stl_kernel(env->tr.base + (0x28 + 3 * 4), EBX);
368 stl_kernel(env->tr.base + (0x28 + 4 * 4), ESP);
369 stl_kernel(env->tr.base + (0x28 + 5 * 4), EBP);
370 stl_kernel(env->tr.base + (0x28 + 6 * 4), ESI);
371 stl_kernel(env->tr.base + (0x28 + 7 * 4), EDI);
7e84c249
FB
372 for(i = 0; i < 6; i++)
373 stw_kernel(env->tr.base + (0x48 + i * 4), env->segs[i].selector);
374 } else {
375 /* 16 bit */
883da8e2 376 stw_kernel(env->tr.base + 0x0e, next_eip);
7e84c249 377 stw_kernel(env->tr.base + 0x10, old_eflags);
0d1a29f9
FB
378 stw_kernel(env->tr.base + (0x12 + 0 * 2), EAX);
379 stw_kernel(env->tr.base + (0x12 + 1 * 2), ECX);
380 stw_kernel(env->tr.base + (0x12 + 2 * 2), EDX);
381 stw_kernel(env->tr.base + (0x12 + 3 * 2), EBX);
382 stw_kernel(env->tr.base + (0x12 + 4 * 2), ESP);
383 stw_kernel(env->tr.base + (0x12 + 5 * 2), EBP);
384 stw_kernel(env->tr.base + (0x12 + 6 * 2), ESI);
385 stw_kernel(env->tr.base + (0x12 + 7 * 2), EDI);
7e84c249
FB
386 for(i = 0; i < 4; i++)
387 stw_kernel(env->tr.base + (0x22 + i * 4), env->segs[i].selector);
388 }
389
390 /* now if an exception occurs, it will occurs in the next task
391 context */
392
393 if (source == SWITCH_TSS_CALL) {
394 stw_kernel(tss_base, env->tr.selector);
395 new_eflags |= NT_MASK;
396 }
397
398 /* set busy bit */
399 if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
14ce26e7 400 target_ulong ptr;
7e84c249 401 uint32_t e2;
883da8e2 402 ptr = env->gdt.base + (tss_selector & ~7);
7e84c249
FB
403 e2 = ldl_kernel(ptr + 4);
404 e2 |= DESC_TSS_BUSY_MASK;
405 stl_kernel(ptr + 4, e2);
406 }
407
408 /* set the new CPU state */
409 /* from this point, any exception which occurs can give problems */
410 env->cr[0] |= CR0_TS_MASK;
883da8e2 411 env->hflags |= HF_TS_MASK;
7e84c249
FB
412 env->tr.selector = tss_selector;
413 env->tr.base = tss_base;
414 env->tr.limit = tss_limit;
415 env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
416
417 if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
1ac157da 418 cpu_x86_update_cr3(env, new_cr3);
7e84c249
FB
419 }
420
421 /* load all registers without an exception, then reload them with
422 possible exception */
423 env->eip = new_eip;
4136f33c 424 eflags_mask = TF_MASK | AC_MASK | ID_MASK |
8145122b 425 IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
7e84c249
FB
426 if (!(type & 8))
427 eflags_mask &= 0xffff;
428 load_eflags(new_eflags, eflags_mask);
0d1a29f9
FB
429 /* XXX: what to do in 16 bit case ? */
430 EAX = new_regs[0];
431 ECX = new_regs[1];
432 EDX = new_regs[2];
433 EBX = new_regs[3];
434 ESP = new_regs[4];
435 EBP = new_regs[5];
436 ESI = new_regs[6];
437 EDI = new_regs[7];
7e84c249
FB
438 if (new_eflags & VM_MASK) {
439 for(i = 0; i < 6; i++)
440 load_seg_vm(i, new_segs[i]);
441 /* in vm86, CPL is always 3 */
442 cpu_x86_set_cpl(env, 3);
443 } else {
444 /* CPL is set the RPL of CS */
445 cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
446 /* first just selectors as the rest may trigger exceptions */
447 for(i = 0; i < 6; i++)
14ce26e7 448 cpu_x86_load_seg_cache(env, i, new_segs[i], 0, 0, 0);
7e84c249
FB
449 }
450
451 env->ldt.selector = new_ldt & ~4;
14ce26e7 452 env->ldt.base = 0;
7e84c249
FB
453 env->ldt.limit = 0;
454 env->ldt.flags = 0;
455
456 /* load the LDT */
457 if (new_ldt & 4)
458 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
459
8145122b
FB
460 if ((new_ldt & 0xfffc) != 0) {
461 dt = &env->gdt;
462 index = new_ldt & ~7;
463 if ((index + 7) > dt->limit)
464 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
465 ptr = dt->base + index;
466 e1 = ldl_kernel(ptr);
467 e2 = ldl_kernel(ptr + 4);
468 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
469 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
470 if (!(e2 & DESC_P_MASK))
471 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
472 load_seg_cache_raw_dt(&env->ldt, e1, e2);
473 }
7e84c249
FB
474
475 /* load the segments */
476 if (!(new_eflags & VM_MASK)) {
477 tss_load_seg(R_CS, new_segs[R_CS]);
478 tss_load_seg(R_SS, new_segs[R_SS]);
479 tss_load_seg(R_ES, new_segs[R_ES]);
480 tss_load_seg(R_DS, new_segs[R_DS]);
481 tss_load_seg(R_FS, new_segs[R_FS]);
482 tss_load_seg(R_GS, new_segs[R_GS]);
483 }
484
485 /* check that EIP is in the CS segment limits */
486 if (new_eip > env->segs[R_CS].limit) {
883da8e2 487 /* XXX: different exception if CALL ? */
7e84c249
FB
488 raise_exception_err(EXCP0D_GPF, 0);
489 }
2c0262af 490}
7e84c249
FB
491
492/* check if Port I/O is allowed in TSS */
493static inline void check_io(int addr, int size)
2c0262af 494{
7e84c249
FB
495 int io_offset, val, mask;
496
497 /* TSS must be a valid 32 bit one */
498 if (!(env->tr.flags & DESC_P_MASK) ||
499 ((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) != 9 ||
500 env->tr.limit < 103)
501 goto fail;
502 io_offset = lduw_kernel(env->tr.base + 0x66);
503 io_offset += (addr >> 3);
504 /* Note: the check needs two bytes */
505 if ((io_offset + 1) > env->tr.limit)
506 goto fail;
507 val = lduw_kernel(env->tr.base + io_offset);
508 val >>= (addr & 7);
509 mask = (1 << size) - 1;
510 /* all bits must be zero to allow the I/O */
511 if ((val & mask) != 0) {
512 fail:
513 raise_exception_err(EXCP0D_GPF, 0);
514 }
2c0262af
FB
515}
516
7e84c249 517void check_iob_T0(void)
2c0262af 518{
7e84c249 519 check_io(T0, 1);
2c0262af
FB
520}
521
7e84c249 522void check_iow_T0(void)
2c0262af 523{
7e84c249 524 check_io(T0, 2);
2c0262af
FB
525}
526
7e84c249 527void check_iol_T0(void)
2c0262af 528{
7e84c249
FB
529 check_io(T0, 4);
530}
531
532void check_iob_DX(void)
533{
534 check_io(EDX & 0xffff, 1);
535}
536
537void check_iow_DX(void)
538{
539 check_io(EDX & 0xffff, 2);
540}
541
542void check_iol_DX(void)
543{
544 check_io(EDX & 0xffff, 4);
2c0262af
FB
545}
546
891b38e4
FB
547static inline unsigned int get_sp_mask(unsigned int e2)
548{
549 if (e2 & DESC_B_MASK)
550 return 0xffffffff;
551 else
552 return 0xffff;
553}
554
555/* XXX: add a is_user flag to have proper security support */
556#define PUSHW(ssp, sp, sp_mask, val)\
557{\
558 sp -= 2;\
559 stw_kernel((ssp) + (sp & (sp_mask)), (val));\
560}
561
562#define PUSHL(ssp, sp, sp_mask, val)\
563{\
564 sp -= 4;\
565 stl_kernel((ssp) + (sp & (sp_mask)), (val));\
566}
567
568#define POPW(ssp, sp, sp_mask, val)\
569{\
570 val = lduw_kernel((ssp) + (sp & (sp_mask)));\
571 sp += 2;\
572}
573
574#define POPL(ssp, sp, sp_mask, val)\
575{\
14ce26e7 576 val = (uint32_t)ldl_kernel((ssp) + (sp & (sp_mask)));\
891b38e4
FB
577 sp += 4;\
578}
579
2c0262af
FB
580/* protected mode interrupt */
581static void do_interrupt_protected(int intno, int is_int, int error_code,
582 unsigned int next_eip, int is_hw)
583{
584 SegmentCache *dt;
14ce26e7 585 target_ulong ptr, ssp;
891b38e4 586 int type, dpl, selector, ss_dpl, cpl, sp_mask;
2c0262af 587 int has_error_code, new_stack, shift;
891b38e4
FB
588 uint32_t e1, e2, offset, ss, esp, ss_e1, ss_e2;
589 uint32_t old_eip;
2c0262af 590
7e84c249
FB
591 has_error_code = 0;
592 if (!is_int && !is_hw) {
593 switch(intno) {
594 case 8:
595 case 10:
596 case 11:
597 case 12:
598 case 13:
599 case 14:
600 case 17:
601 has_error_code = 1;
602 break;
603 }
604 }
883da8e2
FB
605 if (is_int)
606 old_eip = next_eip;
607 else
608 old_eip = env->eip;
7e84c249 609
2c0262af
FB
610 dt = &env->idt;
611 if (intno * 8 + 7 > dt->limit)
612 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
613 ptr = dt->base + intno * 8;
61382a50
FB
614 e1 = ldl_kernel(ptr);
615 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
616 /* check gate type */
617 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
618 switch(type) {
619 case 5: /* task gate */
7e84c249
FB
620 /* must do that check here to return the correct error code */
621 if (!(e2 & DESC_P_MASK))
622 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
883da8e2 623 switch_tss(intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip);
7e84c249
FB
624 if (has_error_code) {
625 int mask;
626 /* push the error code */
627 shift = (env->segs[R_CS].flags >> DESC_B_SHIFT) & 1;
628 if (env->segs[R_SS].flags & DESC_B_MASK)
629 mask = 0xffffffff;
630 else
631 mask = 0xffff;
0d1a29f9 632 esp = (ESP - (2 << shift)) & mask;
7e84c249
FB
633 ssp = env->segs[R_SS].base + esp;
634 if (shift)
635 stl_kernel(ssp, error_code);
636 else
637 stw_kernel(ssp, error_code);
0d1a29f9 638 ESP = (esp & mask) | (ESP & ~mask);
7e84c249
FB
639 }
640 return;
2c0262af
FB
641 case 6: /* 286 interrupt gate */
642 case 7: /* 286 trap gate */
643 case 14: /* 386 interrupt gate */
644 case 15: /* 386 trap gate */
645 break;
646 default:
647 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
648 break;
649 }
650 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
651 cpl = env->hflags & HF_CPL_MASK;
652 /* check privledge if software int */
653 if (is_int && dpl < cpl)
654 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
655 /* check valid bit */
656 if (!(e2 & DESC_P_MASK))
657 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
658 selector = e1 >> 16;
659 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
660 if ((selector & 0xfffc) == 0)
661 raise_exception_err(EXCP0D_GPF, 0);
662
663 if (load_segment(&e1, &e2, selector) != 0)
664 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
665 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
666 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
667 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
668 if (dpl > cpl)
669 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
670 if (!(e2 & DESC_P_MASK))
671 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
672 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
673 /* to inner priviledge */
674 get_ss_esp_from_tss(&ss, &esp, dpl);
675 if ((ss & 0xfffc) == 0)
676 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
677 if ((ss & 3) != dpl)
678 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
679 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
680 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
681 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
682 if (ss_dpl != dpl)
683 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
684 if (!(ss_e2 & DESC_S_MASK) ||
685 (ss_e2 & DESC_CS_MASK) ||
686 !(ss_e2 & DESC_W_MASK))
687 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
688 if (!(ss_e2 & DESC_P_MASK))
689 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
690 new_stack = 1;
891b38e4
FB
691 sp_mask = get_sp_mask(ss_e2);
692 ssp = get_seg_base(ss_e1, ss_e2);
2c0262af
FB
693 } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
694 /* to same priviledge */
8e682019
FB
695 if (env->eflags & VM_MASK)
696 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2c0262af 697 new_stack = 0;
891b38e4
FB
698 sp_mask = get_sp_mask(env->segs[R_SS].flags);
699 ssp = env->segs[R_SS].base;
700 esp = ESP;
4796f5e9 701 dpl = cpl;
2c0262af
FB
702 } else {
703 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
704 new_stack = 0; /* avoid warning */
891b38e4 705 sp_mask = 0; /* avoid warning */
14ce26e7 706 ssp = 0; /* avoid warning */
891b38e4 707 esp = 0; /* avoid warning */
2c0262af
FB
708 }
709
710 shift = type >> 3;
891b38e4
FB
711
712#if 0
713 /* XXX: check that enough room is available */
2c0262af
FB
714 push_size = 6 + (new_stack << 2) + (has_error_code << 1);
715 if (env->eflags & VM_MASK)
716 push_size += 8;
717 push_size <<= shift;
891b38e4 718#endif
2c0262af 719 if (shift == 1) {
2c0262af 720 if (new_stack) {
8e682019
FB
721 if (env->eflags & VM_MASK) {
722 PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
723 PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
724 PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
725 PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
726 }
891b38e4
FB
727 PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
728 PUSHL(ssp, esp, sp_mask, ESP);
2c0262af 729 }
891b38e4
FB
730 PUSHL(ssp, esp, sp_mask, compute_eflags());
731 PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
732 PUSHL(ssp, esp, sp_mask, old_eip);
2c0262af 733 if (has_error_code) {
891b38e4 734 PUSHL(ssp, esp, sp_mask, error_code);
2c0262af
FB
735 }
736 } else {
737 if (new_stack) {
8e682019
FB
738 if (env->eflags & VM_MASK) {
739 PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
740 PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
741 PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
742 PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
743 }
891b38e4
FB
744 PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
745 PUSHW(ssp, esp, sp_mask, ESP);
2c0262af 746 }
891b38e4
FB
747 PUSHW(ssp, esp, sp_mask, compute_eflags());
748 PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
749 PUSHW(ssp, esp, sp_mask, old_eip);
2c0262af 750 if (has_error_code) {
891b38e4 751 PUSHW(ssp, esp, sp_mask, error_code);
2c0262af
FB
752 }
753 }
754
891b38e4 755 if (new_stack) {
8e682019 756 if (env->eflags & VM_MASK) {
14ce26e7
FB
757 cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0, 0);
758 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0, 0);
759 cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0, 0);
760 cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0, 0);
8e682019 761 }
891b38e4
FB
762 ss = (ss & ~3) | dpl;
763 cpu_x86_load_seg_cache(env, R_SS, ss,
764 ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
765 }
766 ESP = (ESP & ~sp_mask) | (esp & sp_mask);
767
768 selector = (selector & ~3) | dpl;
769 cpu_x86_load_seg_cache(env, R_CS, selector,
770 get_seg_base(e1, e2),
771 get_seg_limit(e1, e2),
772 e2);
773 cpu_x86_set_cpl(env, dpl);
774 env->eip = offset;
775
2c0262af
FB
776 /* interrupt gate clear IF mask */
777 if ((type & 1) == 0) {
778 env->eflags &= ~IF_MASK;
779 }
780 env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
781}
782
14ce26e7
FB
783#ifdef TARGET_X86_64
784
785#define PUSHQ(sp, val)\
786{\
787 sp -= 8;\
788 stq_kernel(sp, (val));\
789}
790
791#define POPQ(sp, val)\
792{\
793 val = ldq_kernel(sp);\
794 sp += 8;\
795}
796
797static inline target_ulong get_rsp_from_tss(int level)
798{
799 int index;
800
801#if 0
802 printf("TR: base=" TARGET_FMT_lx " limit=%x\n",
803 env->tr.base, env->tr.limit);
804#endif
805
806 if (!(env->tr.flags & DESC_P_MASK))
807 cpu_abort(env, "invalid tss");
808 index = 8 * level + 4;
809 if ((index + 7) > env->tr.limit)
810 raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
811 return ldq_kernel(env->tr.base + index);
812}
813
814/* 64 bit interrupt */
815static void do_interrupt64(int intno, int is_int, int error_code,
816 target_ulong next_eip, int is_hw)
817{
818 SegmentCache *dt;
819 target_ulong ptr;
820 int type, dpl, selector, cpl, ist;
821 int has_error_code, new_stack;
822 uint32_t e1, e2, e3, ss;
823 target_ulong old_eip, esp, offset;
824
825 has_error_code = 0;
826 if (!is_int && !is_hw) {
827 switch(intno) {
828 case 8:
829 case 10:
830 case 11:
831 case 12:
832 case 13:
833 case 14:
834 case 17:
835 has_error_code = 1;
836 break;
837 }
838 }
839 if (is_int)
840 old_eip = next_eip;
841 else
842 old_eip = env->eip;
843
844 dt = &env->idt;
845 if (intno * 16 + 15 > dt->limit)
846 raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
847 ptr = dt->base + intno * 16;
848 e1 = ldl_kernel(ptr);
849 e2 = ldl_kernel(ptr + 4);
850 e3 = ldl_kernel(ptr + 8);
851 /* check gate type */
852 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
853 switch(type) {
854 case 14: /* 386 interrupt gate */
855 case 15: /* 386 trap gate */
856 break;
857 default:
858 raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
859 break;
860 }
861 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
862 cpl = env->hflags & HF_CPL_MASK;
863 /* check privledge if software int */
864 if (is_int && dpl < cpl)
865 raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
866 /* check valid bit */
867 if (!(e2 & DESC_P_MASK))
868 raise_exception_err(EXCP0B_NOSEG, intno * 16 + 2);
869 selector = e1 >> 16;
870 offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff);
871 ist = e2 & 7;
872 if ((selector & 0xfffc) == 0)
873 raise_exception_err(EXCP0D_GPF, 0);
874
875 if (load_segment(&e1, &e2, selector) != 0)
876 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
877 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
878 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
879 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
880 if (dpl > cpl)
881 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
882 if (!(e2 & DESC_P_MASK))
883 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
884 if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK))
885 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
886 if ((!(e2 & DESC_C_MASK) && dpl < cpl) || ist != 0) {
887 /* to inner priviledge */
888 if (ist != 0)
889 esp = get_rsp_from_tss(ist + 3);
890 else
891 esp = get_rsp_from_tss(dpl);
892 ss = 0;
893 new_stack = 1;
894 } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
895 /* to same priviledge */
896 if (env->eflags & VM_MASK)
897 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
898 new_stack = 0;
899 esp = ESP & ~0xf; /* align stack */
900 dpl = cpl;
901 } else {
902 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
903 new_stack = 0; /* avoid warning */
904 esp = 0; /* avoid warning */
905 }
906
907 PUSHQ(esp, env->segs[R_SS].selector);
908 PUSHQ(esp, ESP);
909 PUSHQ(esp, compute_eflags());
910 PUSHQ(esp, env->segs[R_CS].selector);
911 PUSHQ(esp, old_eip);
912 if (has_error_code) {
913 PUSHQ(esp, error_code);
914 }
915
916 if (new_stack) {
917 ss = 0 | dpl;
918 cpu_x86_load_seg_cache(env, R_SS, ss, 0, 0, 0);
919 }
920 ESP = esp;
921
922 selector = (selector & ~3) | dpl;
923 cpu_x86_load_seg_cache(env, R_CS, selector,
924 get_seg_base(e1, e2),
925 get_seg_limit(e1, e2),
926 e2);
927 cpu_x86_set_cpl(env, dpl);
928 env->eip = offset;
929
930 /* interrupt gate clear IF mask */
931 if ((type & 1) == 0) {
932 env->eflags &= ~IF_MASK;
933 }
934 env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
935}
936
06c2f506 937void helper_syscall(int next_eip_addend)
14ce26e7
FB
938{
939 int selector;
940
941 if (!(env->efer & MSR_EFER_SCE)) {
942 raise_exception_err(EXCP06_ILLOP, 0);
943 }
944 selector = (env->star >> 32) & 0xffff;
945 if (env->hflags & HF_LMA_MASK) {
06c2f506 946 ECX = env->eip + next_eip_addend;
14ce26e7
FB
947 env->regs[11] = compute_eflags();
948
949 cpu_x86_set_cpl(env, 0);
950 cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
951 0, 0xffffffff,
952 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
953 DESC_S_MASK |
954 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | DESC_L_MASK);
955 cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
956 0, 0xffffffff,
957 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
958 DESC_S_MASK |
959 DESC_W_MASK | DESC_A_MASK);
960 env->eflags &= ~env->fmask;
961 if (env->hflags & HF_CS64_MASK)
962 env->eip = env->lstar;
963 else
964 env->eip = env->cstar;
965 } else {
06c2f506 966 ECX = (uint32_t)(env->eip + next_eip_addend);
14ce26e7
FB
967
968 cpu_x86_set_cpl(env, 0);
969 cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
970 0, 0xffffffff,
971 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
972 DESC_S_MASK |
973 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
974 cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
975 0, 0xffffffff,
976 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
977 DESC_S_MASK |
978 DESC_W_MASK | DESC_A_MASK);
979 env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK);
980 env->eip = (uint32_t)env->star;
981 }
982}
983
984void helper_sysret(int dflag)
985{
986 int cpl, selector;
987
988 cpl = env->hflags & HF_CPL_MASK;
989 if (!(env->cr[0] & CR0_PE_MASK) || cpl != 0) {
990 raise_exception_err(EXCP0D_GPF, 0);
991 }
992 selector = (env->star >> 48) & 0xffff;
993 if (env->hflags & HF_LMA_MASK) {
994 if (dflag == 2) {
995 cpu_x86_load_seg_cache(env, R_CS, (selector + 16) | 3,
996 0, 0xffffffff,
997 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
998 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
999 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
1000 DESC_L_MASK);
1001 env->eip = ECX;
1002 } else {
1003 cpu_x86_load_seg_cache(env, R_CS, selector | 3,
1004 0, 0xffffffff,
1005 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1006 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1007 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1008 env->eip = (uint32_t)ECX;
1009 }
1010 cpu_x86_load_seg_cache(env, R_SS, selector + 8,
1011 0, 0xffffffff,
1012 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1013 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1014 DESC_W_MASK | DESC_A_MASK);
31313213
FB
1015 load_eflags((uint32_t)(env->regs[11]), TF_MASK | AC_MASK | ID_MASK |
1016 IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK);
14ce26e7
FB
1017 cpu_x86_set_cpl(env, 3);
1018 } else {
1019 cpu_x86_load_seg_cache(env, R_CS, selector | 3,
1020 0, 0xffffffff,
1021 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1022 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1023 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1024 env->eip = (uint32_t)ECX;
1025 cpu_x86_load_seg_cache(env, R_SS, selector + 8,
1026 0, 0xffffffff,
1027 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1028 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1029 DESC_W_MASK | DESC_A_MASK);
1030 env->eflags |= IF_MASK;
1031 cpu_x86_set_cpl(env, 3);
1032 }
1033}
1034#endif
1035
2c0262af
FB
1036/* real mode interrupt */
1037static void do_interrupt_real(int intno, int is_int, int error_code,
4136f33c 1038 unsigned int next_eip)
2c0262af
FB
1039{
1040 SegmentCache *dt;
14ce26e7 1041 target_ulong ptr, ssp;
2c0262af
FB
1042 int selector;
1043 uint32_t offset, esp;
1044 uint32_t old_cs, old_eip;
1045
1046 /* real mode (simpler !) */
1047 dt = &env->idt;
1048 if (intno * 4 + 3 > dt->limit)
1049 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
1050 ptr = dt->base + intno * 4;
61382a50
FB
1051 offset = lduw_kernel(ptr);
1052 selector = lduw_kernel(ptr + 2);
2c0262af
FB
1053 esp = ESP;
1054 ssp = env->segs[R_SS].base;
1055 if (is_int)
1056 old_eip = next_eip;
1057 else
1058 old_eip = env->eip;
1059 old_cs = env->segs[R_CS].selector;
891b38e4
FB
1060 /* XXX: use SS segment size ? */
1061 PUSHW(ssp, esp, 0xffff, compute_eflags());
1062 PUSHW(ssp, esp, 0xffff, old_cs);
1063 PUSHW(ssp, esp, 0xffff, old_eip);
2c0262af
FB
1064
1065 /* update processor state */
1066 ESP = (ESP & ~0xffff) | (esp & 0xffff);
1067 env->eip = offset;
1068 env->segs[R_CS].selector = selector;
14ce26e7 1069 env->segs[R_CS].base = (selector << 4);
2c0262af
FB
1070 env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
1071}
1072
1073/* fake user mode interrupt */
1074void do_interrupt_user(int intno, int is_int, int error_code,
14ce26e7 1075 target_ulong next_eip)
2c0262af
FB
1076{
1077 SegmentCache *dt;
14ce26e7 1078 target_ulong ptr;
2c0262af
FB
1079 int dpl, cpl;
1080 uint32_t e2;
1081
1082 dt = &env->idt;
1083 ptr = dt->base + (intno * 8);
61382a50 1084 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
1085
1086 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1087 cpl = env->hflags & HF_CPL_MASK;
1088 /* check privledge if software int */
1089 if (is_int && dpl < cpl)
1090 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
1091
1092 /* Since we emulate only user space, we cannot do more than
1093 exiting the emulation with the suitable exception and error
1094 code */
1095 if (is_int)
1096 EIP = next_eip;
1097}
1098
1099/*
e19e89a5 1100 * Begin execution of an interruption. is_int is TRUE if coming from
2c0262af
FB
1101 * the int instruction. next_eip is the EIP value AFTER the interrupt
1102 * instruction. It is only relevant if is_int is TRUE.
1103 */
1104void do_interrupt(int intno, int is_int, int error_code,
14ce26e7 1105 target_ulong next_eip, int is_hw)
2c0262af 1106{
e19e89a5
FB
1107#ifdef DEBUG_PCALL
1108 if (loglevel & (CPU_LOG_PCALL | CPU_LOG_INT)) {
1109 if ((env->cr[0] & CR0_PE_MASK)) {
1110 static int count;
14ce26e7 1111 fprintf(logfile, "%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx " pc=" TARGET_FMT_lx " SP=%04x:" TARGET_FMT_lx,
dc6f57fd
FB
1112 count, intno, error_code, is_int,
1113 env->hflags & HF_CPL_MASK,
1114 env->segs[R_CS].selector, EIP,
2ee73ac3 1115 (int)env->segs[R_CS].base + EIP,
8145122b
FB
1116 env->segs[R_SS].selector, ESP);
1117 if (intno == 0x0e) {
14ce26e7 1118 fprintf(logfile, " CR2=" TARGET_FMT_lx, env->cr[2]);
8145122b 1119 } else {
14ce26e7 1120 fprintf(logfile, " EAX=" TARGET_FMT_lx, EAX);
8145122b 1121 }
e19e89a5 1122 fprintf(logfile, "\n");
14ce26e7 1123#if 0
06c2f506 1124 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
e19e89a5
FB
1125 {
1126 int i;
1127 uint8_t *ptr;
1128 fprintf(logfile, " code=");
1129 ptr = env->segs[R_CS].base + env->eip;
1130 for(i = 0; i < 16; i++) {
1131 fprintf(logfile, " %02x", ldub(ptr + i));
dc6f57fd 1132 }
e19e89a5 1133 fprintf(logfile, "\n");
dc6f57fd 1134 }
8e682019 1135#endif
e19e89a5 1136 count++;
4136f33c 1137 }
4136f33c
FB
1138 }
1139#endif
2c0262af 1140 if (env->cr[0] & CR0_PE_MASK) {
14ce26e7
FB
1141#if TARGET_X86_64
1142 if (env->hflags & HF_LMA_MASK) {
1143 do_interrupt64(intno, is_int, error_code, next_eip, is_hw);
1144 } else
1145#endif
1146 {
1147 do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
1148 }
2c0262af
FB
1149 } else {
1150 do_interrupt_real(intno, is_int, error_code, next_eip);
1151 }
1152}
1153
1154/*
1155 * Signal an interruption. It is executed in the main CPU loop.
1156 * is_int is TRUE if coming from the int instruction. next_eip is the
1157 * EIP value AFTER the interrupt instruction. It is only relevant if
1158 * is_int is TRUE.
1159 */
1160void raise_interrupt(int intno, int is_int, int error_code,
a8ede8ba 1161 int next_eip_addend)
2c0262af
FB
1162{
1163 env->exception_index = intno;
1164 env->error_code = error_code;
1165 env->exception_is_int = is_int;
a8ede8ba 1166 env->exception_next_eip = env->eip + next_eip_addend;
2c0262af
FB
1167 cpu_loop_exit();
1168}
1169
0d1a29f9
FB
1170/* same as raise_exception_err, but do not restore global registers */
1171static void raise_exception_err_norestore(int exception_index, int error_code)
1172{
1173 env->exception_index = exception_index;
1174 env->error_code = error_code;
1175 env->exception_is_int = 0;
1176 env->exception_next_eip = 0;
1177 longjmp(env->jmp_env, 1);
1178}
1179
2c0262af 1180/* shortcuts to generate exceptions */
8145122b
FB
1181
1182void (raise_exception_err)(int exception_index, int error_code)
2c0262af
FB
1183{
1184 raise_interrupt(exception_index, 0, error_code, 0);
1185}
1186
1187void raise_exception(int exception_index)
1188{
1189 raise_interrupt(exception_index, 0, 0, 0);
1190}
1191
1192#ifdef BUGGY_GCC_DIV64
1193/* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
1194 call it from another function */
14ce26e7 1195uint32_t div32(uint32_t *q_ptr, uint64_t num, uint32_t den)
2c0262af
FB
1196{
1197 *q_ptr = num / den;
1198 return num % den;
1199}
1200
14ce26e7 1201int32_t idiv32(int32_t *q_ptr, int64_t num, int32_t den)
2c0262af
FB
1202{
1203 *q_ptr = num / den;
1204 return num % den;
1205}
1206#endif
1207
14ce26e7 1208void helper_divl_EAX_T0(void)
2c0262af
FB
1209{
1210 unsigned int den, q, r;
1211 uint64_t num;
1212
31313213 1213 num = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
2c0262af
FB
1214 den = T0;
1215 if (den == 0) {
2c0262af
FB
1216 raise_exception(EXCP00_DIVZ);
1217 }
1218#ifdef BUGGY_GCC_DIV64
14ce26e7 1219 r = div32(&q, num, den);
2c0262af
FB
1220#else
1221 q = (num / den);
1222 r = (num % den);
1223#endif
14ce26e7
FB
1224 EAX = (uint32_t)q;
1225 EDX = (uint32_t)r;
2c0262af
FB
1226}
1227
14ce26e7 1228void helper_idivl_EAX_T0(void)
2c0262af
FB
1229{
1230 int den, q, r;
1231 int64_t num;
1232
31313213 1233 num = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
2c0262af
FB
1234 den = T0;
1235 if (den == 0) {
2c0262af
FB
1236 raise_exception(EXCP00_DIVZ);
1237 }
1238#ifdef BUGGY_GCC_DIV64
14ce26e7 1239 r = idiv32(&q, num, den);
2c0262af
FB
1240#else
1241 q = (num / den);
1242 r = (num % den);
1243#endif
14ce26e7
FB
1244 EAX = (uint32_t)q;
1245 EDX = (uint32_t)r;
2c0262af
FB
1246}
1247
1248void helper_cmpxchg8b(void)
1249{
1250 uint64_t d;
1251 int eflags;
1252
1253 eflags = cc_table[CC_OP].compute_all();
14ce26e7 1254 d = ldq(A0);
2c0262af 1255 if (d == (((uint64_t)EDX << 32) | EAX)) {
14ce26e7 1256 stq(A0, ((uint64_t)ECX << 32) | EBX);
2c0262af
FB
1257 eflags |= CC_Z;
1258 } else {
1259 EDX = d >> 32;
1260 EAX = d;
1261 eflags &= ~CC_Z;
1262 }
1263 CC_SRC = eflags;
1264}
1265
2c0262af
FB
1266void helper_cpuid(void)
1267{
14ce26e7 1268 switch((uint32_t)EAX) {
8e682019
FB
1269 case 0:
1270 EAX = 2; /* max EAX index supported */
14ce26e7
FB
1271 EBX = env->cpuid_vendor1;
1272 EDX = env->cpuid_vendor2;
1273 ECX = env->cpuid_vendor3;
8e682019
FB
1274 break;
1275 case 1:
14ce26e7
FB
1276 EAX = env->cpuid_version;
1277 EBX = 0;
9df217a3 1278 ECX = env->cpuid_ext_features;
14ce26e7 1279 EDX = env->cpuid_features;
8e682019
FB
1280 break;
1281 default:
1282 /* cache info: needed for Pentium Pro compatibility */
1283 EAX = 0x410601;
2c0262af
FB
1284 EBX = 0;
1285 ECX = 0;
8e682019
FB
1286 EDX = 0;
1287 break;
14ce26e7
FB
1288#ifdef TARGET_X86_64
1289 case 0x80000000:
1290 EAX = 0x80000008;
1291 EBX = env->cpuid_vendor1;
1292 EDX = env->cpuid_vendor2;
1293 ECX = env->cpuid_vendor3;
1294 break;
1295 case 0x80000001:
1296 EAX = env->cpuid_features;
1297 EBX = 0;
1298 ECX = 0;
1299 /* long mode + syscall/sysret features */
1300 EDX = (env->cpuid_features & 0x0183F3FF) | (1 << 29) | (1 << 11);
1301 break;
1302 case 0x80000008:
1303 /* virtual & phys address size in low 2 bytes. */
1304 EAX = 0x00003028;
1305 EBX = 0;
1306 ECX = 0;
1307 EDX = 0;
1308 break;
1309#endif
2c0262af
FB
1310 }
1311}
1312
61a8c4ec
FB
1313void helper_enter_level(int level, int data32)
1314{
14ce26e7 1315 target_ulong ssp;
61a8c4ec
FB
1316 uint32_t esp_mask, esp, ebp;
1317
1318 esp_mask = get_sp_mask(env->segs[R_SS].flags);
1319 ssp = env->segs[R_SS].base;
1320 ebp = EBP;
1321 esp = ESP;
1322 if (data32) {
1323 /* 32 bit */
1324 esp -= 4;
1325 while (--level) {
1326 esp -= 4;
1327 ebp -= 4;
1328 stl(ssp + (esp & esp_mask), ldl(ssp + (ebp & esp_mask)));
1329 }
1330 esp -= 4;
1331 stl(ssp + (esp & esp_mask), T1);
1332 } else {
1333 /* 16 bit */
1334 esp -= 2;
1335 while (--level) {
1336 esp -= 2;
1337 ebp -= 2;
1338 stw(ssp + (esp & esp_mask), lduw(ssp + (ebp & esp_mask)));
1339 }
1340 esp -= 2;
1341 stw(ssp + (esp & esp_mask), T1);
1342 }
1343}
1344
2c0262af
FB
1345void helper_lldt_T0(void)
1346{
1347 int selector;
1348 SegmentCache *dt;
1349 uint32_t e1, e2;
14ce26e7
FB
1350 int index, entry_limit;
1351 target_ulong ptr;
2c0262af
FB
1352
1353 selector = T0 & 0xffff;
1354 if ((selector & 0xfffc) == 0) {
1355 /* XXX: NULL selector case: invalid LDT */
14ce26e7 1356 env->ldt.base = 0;
2c0262af
FB
1357 env->ldt.limit = 0;
1358 } else {
1359 if (selector & 0x4)
1360 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1361 dt = &env->gdt;
1362 index = selector & ~7;
14ce26e7
FB
1363#ifdef TARGET_X86_64
1364 if (env->hflags & HF_LMA_MASK)
1365 entry_limit = 15;
1366 else
1367#endif
1368 entry_limit = 7;
1369 if ((index + entry_limit) > dt->limit)
2c0262af
FB
1370 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1371 ptr = dt->base + index;
61382a50
FB
1372 e1 = ldl_kernel(ptr);
1373 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
1374 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
1375 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1376 if (!(e2 & DESC_P_MASK))
1377 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
14ce26e7
FB
1378#ifdef TARGET_X86_64
1379 if (env->hflags & HF_LMA_MASK) {
1380 uint32_t e3;
1381 e3 = ldl_kernel(ptr + 8);
1382 load_seg_cache_raw_dt(&env->ldt, e1, e2);
1383 env->ldt.base |= (target_ulong)e3 << 32;
1384 } else
1385#endif
1386 {
1387 load_seg_cache_raw_dt(&env->ldt, e1, e2);
1388 }
2c0262af
FB
1389 }
1390 env->ldt.selector = selector;
1391}
1392
1393void helper_ltr_T0(void)
1394{
1395 int selector;
1396 SegmentCache *dt;
1397 uint32_t e1, e2;
14ce26e7
FB
1398 int index, type, entry_limit;
1399 target_ulong ptr;
2c0262af
FB
1400
1401 selector = T0 & 0xffff;
1402 if ((selector & 0xfffc) == 0) {
14ce26e7
FB
1403 /* NULL selector case: invalid TR */
1404 env->tr.base = 0;
2c0262af
FB
1405 env->tr.limit = 0;
1406 env->tr.flags = 0;
1407 } else {
1408 if (selector & 0x4)
1409 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1410 dt = &env->gdt;
1411 index = selector & ~7;
14ce26e7
FB
1412#ifdef TARGET_X86_64
1413 if (env->hflags & HF_LMA_MASK)
1414 entry_limit = 15;
1415 else
1416#endif
1417 entry_limit = 7;
1418 if ((index + entry_limit) > dt->limit)
2c0262af
FB
1419 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1420 ptr = dt->base + index;
61382a50
FB
1421 e1 = ldl_kernel(ptr);
1422 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
1423 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1424 if ((e2 & DESC_S_MASK) ||
7e84c249 1425 (type != 1 && type != 9))
2c0262af
FB
1426 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1427 if (!(e2 & DESC_P_MASK))
1428 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
14ce26e7
FB
1429#ifdef TARGET_X86_64
1430 if (env->hflags & HF_LMA_MASK) {
1431 uint32_t e3;
1432 e3 = ldl_kernel(ptr + 8);
1433 load_seg_cache_raw_dt(&env->tr, e1, e2);
1434 env->tr.base |= (target_ulong)e3 << 32;
1435 } else
1436#endif
1437 {
1438 load_seg_cache_raw_dt(&env->tr, e1, e2);
1439 }
8e682019 1440 e2 |= DESC_TSS_BUSY_MASK;
61382a50 1441 stl_kernel(ptr + 4, e2);
2c0262af
FB
1442 }
1443 env->tr.selector = selector;
1444}
1445
3ab493de 1446/* only works if protected mode and not VM86. seg_reg must be != R_CS */
8e682019 1447void load_seg(int seg_reg, int selector)
2c0262af
FB
1448{
1449 uint32_t e1, e2;
3ab493de
FB
1450 int cpl, dpl, rpl;
1451 SegmentCache *dt;
1452 int index;
14ce26e7 1453 target_ulong ptr;
3ab493de 1454
8e682019 1455 selector &= 0xffff;
2c0262af
FB
1456 if ((selector & 0xfffc) == 0) {
1457 /* null selector case */
4d6b6c0a
FB
1458 if (seg_reg == R_SS
1459#ifdef TARGET_X86_64
1460 && !(env->hflags & HF_CS64_MASK)
1461#endif
1462 )
2c0262af 1463 raise_exception_err(EXCP0D_GPF, 0);
14ce26e7 1464 cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0);
2c0262af 1465 } else {
3ab493de
FB
1466
1467 if (selector & 0x4)
1468 dt = &env->ldt;
1469 else
1470 dt = &env->gdt;
1471 index = selector & ~7;
8e682019 1472 if ((index + 7) > dt->limit)
2c0262af 1473 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de
FB
1474 ptr = dt->base + index;
1475 e1 = ldl_kernel(ptr);
1476 e2 = ldl_kernel(ptr + 4);
14ce26e7 1477
8e682019 1478 if (!(e2 & DESC_S_MASK))
2c0262af 1479 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de
FB
1480 rpl = selector & 3;
1481 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1482 cpl = env->hflags & HF_CPL_MASK;
2c0262af 1483 if (seg_reg == R_SS) {
3ab493de 1484 /* must be writable segment */
8e682019 1485 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
2c0262af 1486 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
8e682019 1487 if (rpl != cpl || dpl != cpl)
3ab493de 1488 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2c0262af 1489 } else {
3ab493de 1490 /* must be readable segment */
8e682019 1491 if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
2c0262af 1492 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de
FB
1493
1494 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1495 /* if not conforming code, test rights */
8e682019 1496 if (dpl < cpl || dpl < rpl)
3ab493de 1497 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de 1498 }
2c0262af
FB
1499 }
1500
1501 if (!(e2 & DESC_P_MASK)) {
2c0262af
FB
1502 if (seg_reg == R_SS)
1503 raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
1504 else
1505 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1506 }
3ab493de
FB
1507
1508 /* set the access bit if not already set */
1509 if (!(e2 & DESC_A_MASK)) {
1510 e2 |= DESC_A_MASK;
1511 stl_kernel(ptr + 4, e2);
1512 }
1513
2c0262af
FB
1514 cpu_x86_load_seg_cache(env, seg_reg, selector,
1515 get_seg_base(e1, e2),
1516 get_seg_limit(e1, e2),
1517 e2);
1518#if 0
1519 fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1520 selector, (unsigned long)sc->base, sc->limit, sc->flags);
1521#endif
1522 }
1523}
1524
1525/* protected mode jump */
08cea4ee 1526void helper_ljmp_protected_T0_T1(int next_eip)
2c0262af 1527{
14ce26e7 1528 int new_cs, gate_cs, type;
2c0262af 1529 uint32_t e1, e2, cpl, dpl, rpl, limit;
14ce26e7
FB
1530 target_ulong new_eip;
1531
2c0262af
FB
1532 new_cs = T0;
1533 new_eip = T1;
1534 if ((new_cs & 0xfffc) == 0)
1535 raise_exception_err(EXCP0D_GPF, 0);
1536 if (load_segment(&e1, &e2, new_cs) != 0)
1537 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1538 cpl = env->hflags & HF_CPL_MASK;
1539 if (e2 & DESC_S_MASK) {
1540 if (!(e2 & DESC_CS_MASK))
1541 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1542 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
7e84c249 1543 if (e2 & DESC_C_MASK) {
2c0262af
FB
1544 /* conforming code segment */
1545 if (dpl > cpl)
1546 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1547 } else {
1548 /* non conforming code segment */
1549 rpl = new_cs & 3;
1550 if (rpl > cpl)
1551 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1552 if (dpl != cpl)
1553 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1554 }
1555 if (!(e2 & DESC_P_MASK))
1556 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1557 limit = get_seg_limit(e1, e2);
ca954f6d
FB
1558 if (new_eip > limit &&
1559 !(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK))
2c0262af
FB
1560 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1561 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1562 get_seg_base(e1, e2), limit, e2);
1563 EIP = new_eip;
1564 } else {
7e84c249
FB
1565 /* jump to call or task gate */
1566 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1567 rpl = new_cs & 3;
1568 cpl = env->hflags & HF_CPL_MASK;
1569 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1570 switch(type) {
1571 case 1: /* 286 TSS */
1572 case 9: /* 386 TSS */
1573 case 5: /* task gate */
1574 if (dpl < cpl || dpl < rpl)
1575 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
08cea4ee 1576 switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
7e84c249
FB
1577 break;
1578 case 4: /* 286 call gate */
1579 case 12: /* 386 call gate */
1580 if ((dpl < cpl) || (dpl < rpl))
1581 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1582 if (!(e2 & DESC_P_MASK))
1583 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1584 gate_cs = e1 >> 16;
516633dc
FB
1585 new_eip = (e1 & 0xffff);
1586 if (type == 12)
1587 new_eip |= (e2 & 0xffff0000);
7e84c249
FB
1588 if (load_segment(&e1, &e2, gate_cs) != 0)
1589 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1590 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1591 /* must be code segment */
1592 if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
1593 (DESC_S_MASK | DESC_CS_MASK)))
1594 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
14ce26e7 1595 if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
7e84c249
FB
1596 (!(e2 & DESC_C_MASK) && (dpl != cpl)))
1597 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1598 if (!(e2 & DESC_P_MASK))
1599 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
7e84c249
FB
1600 limit = get_seg_limit(e1, e2);
1601 if (new_eip > limit)
1602 raise_exception_err(EXCP0D_GPF, 0);
1603 cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1604 get_seg_base(e1, e2), limit, e2);
1605 EIP = new_eip;
1606 break;
1607 default:
1608 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1609 break;
1610 }
2c0262af
FB
1611 }
1612}
1613
1614/* real mode call */
1615void helper_lcall_real_T0_T1(int shift, int next_eip)
1616{
1617 int new_cs, new_eip;
1618 uint32_t esp, esp_mask;
14ce26e7 1619 target_ulong ssp;
2c0262af
FB
1620
1621 new_cs = T0;
1622 new_eip = T1;
1623 esp = ESP;
891b38e4 1624 esp_mask = get_sp_mask(env->segs[R_SS].flags);
2c0262af
FB
1625 ssp = env->segs[R_SS].base;
1626 if (shift) {
891b38e4
FB
1627 PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1628 PUSHL(ssp, esp, esp_mask, next_eip);
2c0262af 1629 } else {
891b38e4
FB
1630 PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1631 PUSHW(ssp, esp, esp_mask, next_eip);
2c0262af
FB
1632 }
1633
891b38e4 1634 ESP = (ESP & ~esp_mask) | (esp & esp_mask);
2c0262af
FB
1635 env->eip = new_eip;
1636 env->segs[R_CS].selector = new_cs;
14ce26e7 1637 env->segs[R_CS].base = (new_cs << 4);
2c0262af
FB
1638}
1639
1640/* protected mode call */
1641void helper_lcall_protected_T0_T1(int shift, int next_eip)
1642{
891b38e4 1643 int new_cs, new_eip, new_stack, i;
2c0262af 1644 uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
891b38e4
FB
1645 uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
1646 uint32_t val, limit, old_sp_mask;
14ce26e7 1647 target_ulong ssp, old_ssp;
2c0262af
FB
1648
1649 new_cs = T0;
1650 new_eip = T1;
f3f2d9be 1651#ifdef DEBUG_PCALL
e19e89a5
FB
1652 if (loglevel & CPU_LOG_PCALL) {
1653 fprintf(logfile, "lcall %04x:%08x s=%d\n",
1654 new_cs, new_eip, shift);
7fe48483 1655 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
f3f2d9be
FB
1656 }
1657#endif
2c0262af
FB
1658 if ((new_cs & 0xfffc) == 0)
1659 raise_exception_err(EXCP0D_GPF, 0);
1660 if (load_segment(&e1, &e2, new_cs) != 0)
1661 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1662 cpl = env->hflags & HF_CPL_MASK;
f3f2d9be 1663#ifdef DEBUG_PCALL
e19e89a5 1664 if (loglevel & CPU_LOG_PCALL) {
f3f2d9be
FB
1665 fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
1666 }
1667#endif
2c0262af
FB
1668 if (e2 & DESC_S_MASK) {
1669 if (!(e2 & DESC_CS_MASK))
1670 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1671 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
7e84c249 1672 if (e2 & DESC_C_MASK) {
2c0262af
FB
1673 /* conforming code segment */
1674 if (dpl > cpl)
1675 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1676 } else {
1677 /* non conforming code segment */
1678 rpl = new_cs & 3;
1679 if (rpl > cpl)
1680 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1681 if (dpl != cpl)
1682 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1683 }
1684 if (!(e2 & DESC_P_MASK))
1685 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1686
1687 sp = ESP;
891b38e4
FB
1688 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1689 ssp = env->segs[R_SS].base;
2c0262af 1690 if (shift) {
891b38e4
FB
1691 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1692 PUSHL(ssp, sp, sp_mask, next_eip);
2c0262af 1693 } else {
891b38e4
FB
1694 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1695 PUSHW(ssp, sp, sp_mask, next_eip);
2c0262af 1696 }
2c0262af
FB
1697
1698 limit = get_seg_limit(e1, e2);
1699 if (new_eip > limit)
1700 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1701 /* from this point, not restartable */
891b38e4 1702 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
1703 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1704 get_seg_base(e1, e2), limit, e2);
1705 EIP = new_eip;
1706 } else {
1707 /* check gate type */
1708 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
7e84c249
FB
1709 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1710 rpl = new_cs & 3;
2c0262af
FB
1711 switch(type) {
1712 case 1: /* available 286 TSS */
1713 case 9: /* available 386 TSS */
1714 case 5: /* task gate */
7e84c249
FB
1715 if (dpl < cpl || dpl < rpl)
1716 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
883da8e2 1717 switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
8145122b 1718 return;
2c0262af
FB
1719 case 4: /* 286 call gate */
1720 case 12: /* 386 call gate */
1721 break;
1722 default:
1723 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1724 break;
1725 }
1726 shift = type >> 3;
1727
2c0262af
FB
1728 if (dpl < cpl || dpl < rpl)
1729 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1730 /* check valid bit */
1731 if (!(e2 & DESC_P_MASK))
1732 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1733 selector = e1 >> 16;
1734 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
f3f2d9be 1735 param_count = e2 & 0x1f;
2c0262af
FB
1736 if ((selector & 0xfffc) == 0)
1737 raise_exception_err(EXCP0D_GPF, 0);
1738
1739 if (load_segment(&e1, &e2, selector) != 0)
1740 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1741 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
1742 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1743 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1744 if (dpl > cpl)
1745 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1746 if (!(e2 & DESC_P_MASK))
1747 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1748
1749 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1750 /* to inner priviledge */
1751 get_ss_esp_from_tss(&ss, &sp, dpl);
f3f2d9be 1752#ifdef DEBUG_PCALL
e19e89a5 1753 if (loglevel & CPU_LOG_PCALL)
14ce26e7 1754 fprintf(logfile, "new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx "\n",
f3f2d9be
FB
1755 ss, sp, param_count, ESP);
1756#endif
2c0262af
FB
1757 if ((ss & 0xfffc) == 0)
1758 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1759 if ((ss & 3) != dpl)
1760 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1761 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
1762 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1763 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1764 if (ss_dpl != dpl)
1765 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1766 if (!(ss_e2 & DESC_S_MASK) ||
1767 (ss_e2 & DESC_CS_MASK) ||
1768 !(ss_e2 & DESC_W_MASK))
1769 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1770 if (!(ss_e2 & DESC_P_MASK))
1771 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1772
891b38e4 1773 // push_size = ((param_count * 2) + 8) << shift;
2c0262af 1774
891b38e4
FB
1775 old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1776 old_ssp = env->segs[R_SS].base;
2c0262af 1777
891b38e4
FB
1778 sp_mask = get_sp_mask(ss_e2);
1779 ssp = get_seg_base(ss_e1, ss_e2);
2c0262af 1780 if (shift) {
891b38e4
FB
1781 PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1782 PUSHL(ssp, sp, sp_mask, ESP);
1783 for(i = param_count - 1; i >= 0; i--) {
1784 val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
1785 PUSHL(ssp, sp, sp_mask, val);
2c0262af
FB
1786 }
1787 } else {
891b38e4
FB
1788 PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1789 PUSHW(ssp, sp, sp_mask, ESP);
1790 for(i = param_count - 1; i >= 0; i--) {
1791 val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
1792 PUSHW(ssp, sp, sp_mask, val);
2c0262af
FB
1793 }
1794 }
891b38e4 1795 new_stack = 1;
2c0262af
FB
1796 } else {
1797 /* to same priviledge */
891b38e4
FB
1798 sp = ESP;
1799 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1800 ssp = env->segs[R_SS].base;
1801 // push_size = (4 << shift);
1802 new_stack = 0;
2c0262af
FB
1803 }
1804
1805 if (shift) {
891b38e4
FB
1806 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1807 PUSHL(ssp, sp, sp_mask, next_eip);
2c0262af 1808 } else {
891b38e4
FB
1809 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1810 PUSHW(ssp, sp, sp_mask, next_eip);
1811 }
1812
1813 /* from this point, not restartable */
1814
1815 if (new_stack) {
1816 ss = (ss & ~3) | dpl;
1817 cpu_x86_load_seg_cache(env, R_SS, ss,
1818 ssp,
1819 get_seg_limit(ss_e1, ss_e2),
1820 ss_e2);
2c0262af
FB
1821 }
1822
2c0262af
FB
1823 selector = (selector & ~3) | dpl;
1824 cpu_x86_load_seg_cache(env, R_CS, selector,
1825 get_seg_base(e1, e2),
1826 get_seg_limit(e1, e2),
1827 e2);
1828 cpu_x86_set_cpl(env, dpl);
891b38e4 1829 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
1830 EIP = offset;
1831 }
9df217a3
FB
1832#ifdef USE_KQEMU
1833 if (kqemu_is_ok(env)) {
1834 env->exception_index = -1;
1835 cpu_loop_exit();
1836 }
1837#endif
2c0262af
FB
1838}
1839
7e84c249 1840/* real and vm86 mode iret */
2c0262af
FB
1841void helper_iret_real(int shift)
1842{
891b38e4 1843 uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
14ce26e7 1844 target_ulong ssp;
2c0262af 1845 int eflags_mask;
7e84c249 1846
891b38e4
FB
1847 sp_mask = 0xffff; /* XXXX: use SS segment size ? */
1848 sp = ESP;
1849 ssp = env->segs[R_SS].base;
2c0262af
FB
1850 if (shift == 1) {
1851 /* 32 bits */
891b38e4
FB
1852 POPL(ssp, sp, sp_mask, new_eip);
1853 POPL(ssp, sp, sp_mask, new_cs);
1854 new_cs &= 0xffff;
1855 POPL(ssp, sp, sp_mask, new_eflags);
2c0262af
FB
1856 } else {
1857 /* 16 bits */
891b38e4
FB
1858 POPW(ssp, sp, sp_mask, new_eip);
1859 POPW(ssp, sp, sp_mask, new_cs);
1860 POPW(ssp, sp, sp_mask, new_eflags);
2c0262af 1861 }
4136f33c 1862 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
1863 load_seg_vm(R_CS, new_cs);
1864 env->eip = new_eip;
7e84c249 1865 if (env->eflags & VM_MASK)
8145122b 1866 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
7e84c249 1867 else
8145122b 1868 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
2c0262af
FB
1869 if (shift == 0)
1870 eflags_mask &= 0xffff;
1871 load_eflags(new_eflags, eflags_mask);
1872}
1873
8e682019
FB
1874static inline void validate_seg(int seg_reg, int cpl)
1875{
1876 int dpl;
1877 uint32_t e2;
1878
1879 e2 = env->segs[seg_reg].flags;
1880 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1881 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1882 /* data or non conforming code segment */
1883 if (dpl < cpl) {
14ce26e7 1884 cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
8e682019
FB
1885 }
1886 }
1887}
1888
2c0262af
FB
1889/* protected mode iret */
1890static inline void helper_ret_protected(int shift, int is_iret, int addend)
1891{
14ce26e7 1892 uint32_t new_cs, new_eflags, new_ss;
2c0262af
FB
1893 uint32_t new_es, new_ds, new_fs, new_gs;
1894 uint32_t e1, e2, ss_e1, ss_e2;
4136f33c 1895 int cpl, dpl, rpl, eflags_mask, iopl;
14ce26e7 1896 target_ulong ssp, sp, new_eip, new_esp, sp_mask;
2c0262af 1897
14ce26e7
FB
1898#ifdef TARGET_X86_64
1899 if (shift == 2)
1900 sp_mask = -1;
1901 else
1902#endif
1903 sp_mask = get_sp_mask(env->segs[R_SS].flags);
2c0262af 1904 sp = ESP;
891b38e4 1905 ssp = env->segs[R_SS].base;
354ff226 1906 new_eflags = 0; /* avoid warning */
14ce26e7
FB
1907#ifdef TARGET_X86_64
1908 if (shift == 2) {
1909 POPQ(sp, new_eip);
1910 POPQ(sp, new_cs);
1911 new_cs &= 0xffff;
1912 if (is_iret) {
1913 POPQ(sp, new_eflags);
1914 }
1915 } else
1916#endif
2c0262af
FB
1917 if (shift == 1) {
1918 /* 32 bits */
891b38e4
FB
1919 POPL(ssp, sp, sp_mask, new_eip);
1920 POPL(ssp, sp, sp_mask, new_cs);
1921 new_cs &= 0xffff;
1922 if (is_iret) {
1923 POPL(ssp, sp, sp_mask, new_eflags);
1924 if (new_eflags & VM_MASK)
1925 goto return_to_vm86;
1926 }
2c0262af
FB
1927 } else {
1928 /* 16 bits */
891b38e4
FB
1929 POPW(ssp, sp, sp_mask, new_eip);
1930 POPW(ssp, sp, sp_mask, new_cs);
2c0262af 1931 if (is_iret)
891b38e4 1932 POPW(ssp, sp, sp_mask, new_eflags);
2c0262af 1933 }
891b38e4 1934#ifdef DEBUG_PCALL
e19e89a5 1935 if (loglevel & CPU_LOG_PCALL) {
14ce26e7 1936 fprintf(logfile, "lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n",
e19e89a5 1937 new_cs, new_eip, shift, addend);
7fe48483 1938 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
891b38e4
FB
1939 }
1940#endif
2c0262af
FB
1941 if ((new_cs & 0xfffc) == 0)
1942 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1943 if (load_segment(&e1, &e2, new_cs) != 0)
1944 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1945 if (!(e2 & DESC_S_MASK) ||
1946 !(e2 & DESC_CS_MASK))
1947 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1948 cpl = env->hflags & HF_CPL_MASK;
1949 rpl = new_cs & 3;
1950 if (rpl < cpl)
1951 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1952 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
7e84c249 1953 if (e2 & DESC_C_MASK) {
2c0262af
FB
1954 if (dpl > rpl)
1955 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1956 } else {
1957 if (dpl != rpl)
1958 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1959 }
1960 if (!(e2 & DESC_P_MASK))
1961 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1962
891b38e4 1963 sp += addend;
ca954f6d
FB
1964 if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) ||
1965 ((env->hflags & HF_CS64_MASK) && !is_iret))) {
2c0262af
FB
1966 /* return to same priledge level */
1967 cpu_x86_load_seg_cache(env, R_CS, new_cs,
1968 get_seg_base(e1, e2),
1969 get_seg_limit(e1, e2),
1970 e2);
2c0262af
FB
1971 } else {
1972 /* return to different priviledge level */
14ce26e7
FB
1973#ifdef TARGET_X86_64
1974 if (shift == 2) {
1975 POPQ(sp, new_esp);
1976 POPQ(sp, new_ss);
1977 new_ss &= 0xffff;
1978 } else
1979#endif
2c0262af
FB
1980 if (shift == 1) {
1981 /* 32 bits */
891b38e4
FB
1982 POPL(ssp, sp, sp_mask, new_esp);
1983 POPL(ssp, sp, sp_mask, new_ss);
1984 new_ss &= 0xffff;
2c0262af
FB
1985 } else {
1986 /* 16 bits */
891b38e4
FB
1987 POPW(ssp, sp, sp_mask, new_esp);
1988 POPW(ssp, sp, sp_mask, new_ss);
2c0262af 1989 }
e19e89a5
FB
1990#ifdef DEBUG_PCALL
1991 if (loglevel & CPU_LOG_PCALL) {
14ce26e7 1992 fprintf(logfile, "new ss:esp=%04x:" TARGET_FMT_lx "\n",
e19e89a5
FB
1993 new_ss, new_esp);
1994 }
1995#endif
14ce26e7
FB
1996 if ((env->hflags & HF_LMA_MASK) && (new_ss & 0xfffc) == 0) {
1997 /* NULL ss is allowed in long mode */
1998 cpu_x86_load_seg_cache(env, R_SS, new_ss,
1999 0, 0xffffffff,
2000 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2001 DESC_S_MASK | (rpl << DESC_DPL_SHIFT) |
2002 DESC_W_MASK | DESC_A_MASK);
2003 } else {
2004 if ((new_ss & 3) != rpl)
2005 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2006 if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
2007 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2008 if (!(ss_e2 & DESC_S_MASK) ||
2009 (ss_e2 & DESC_CS_MASK) ||
2010 !(ss_e2 & DESC_W_MASK))
2011 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2012 dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
2013 if (dpl != rpl)
2014 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2015 if (!(ss_e2 & DESC_P_MASK))
2016 raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
2017 cpu_x86_load_seg_cache(env, R_SS, new_ss,
2018 get_seg_base(ss_e1, ss_e2),
2019 get_seg_limit(ss_e1, ss_e2),
2020 ss_e2);
2021 }
2c0262af
FB
2022
2023 cpu_x86_load_seg_cache(env, R_CS, new_cs,
2024 get_seg_base(e1, e2),
2025 get_seg_limit(e1, e2),
2026 e2);
2c0262af 2027 cpu_x86_set_cpl(env, rpl);
891b38e4 2028 sp = new_esp;
14ce26e7
FB
2029#ifdef TARGET_X86_64
2030 if (shift == 2)
2031 sp_mask = -1;
2032 else
2033#endif
2034 sp_mask = get_sp_mask(ss_e2);
8e682019
FB
2035
2036 /* validate data segments */
2037 validate_seg(R_ES, cpl);
2038 validate_seg(R_DS, cpl);
2039 validate_seg(R_FS, cpl);
2040 validate_seg(R_GS, cpl);
4afa6482
FB
2041
2042 sp += addend;
2c0262af 2043 }
891b38e4 2044 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
2045 env->eip = new_eip;
2046 if (is_iret) {
4136f33c 2047 /* NOTE: 'cpl' is the _old_ CPL */
8145122b 2048 eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
2c0262af 2049 if (cpl == 0)
4136f33c
FB
2050 eflags_mask |= IOPL_MASK;
2051 iopl = (env->eflags >> IOPL_SHIFT) & 3;
2052 if (cpl <= iopl)
2053 eflags_mask |= IF_MASK;
2c0262af
FB
2054 if (shift == 0)
2055 eflags_mask &= 0xffff;
2056 load_eflags(new_eflags, eflags_mask);
2057 }
2058 return;
2059
2060 return_to_vm86:
891b38e4
FB
2061 POPL(ssp, sp, sp_mask, new_esp);
2062 POPL(ssp, sp, sp_mask, new_ss);
2063 POPL(ssp, sp, sp_mask, new_es);
2064 POPL(ssp, sp, sp_mask, new_ds);
2065 POPL(ssp, sp, sp_mask, new_fs);
2066 POPL(ssp, sp, sp_mask, new_gs);
2c0262af
FB
2067
2068 /* modify processor state */
4136f33c 2069 load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK |
8145122b 2070 IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
891b38e4 2071 load_seg_vm(R_CS, new_cs & 0xffff);
2c0262af 2072 cpu_x86_set_cpl(env, 3);
891b38e4
FB
2073 load_seg_vm(R_SS, new_ss & 0xffff);
2074 load_seg_vm(R_ES, new_es & 0xffff);
2075 load_seg_vm(R_DS, new_ds & 0xffff);
2076 load_seg_vm(R_FS, new_fs & 0xffff);
2077 load_seg_vm(R_GS, new_gs & 0xffff);
2c0262af 2078
fd836909 2079 env->eip = new_eip & 0xffff;
2c0262af
FB
2080 ESP = new_esp;
2081}
2082
08cea4ee 2083void helper_iret_protected(int shift, int next_eip)
2c0262af 2084{
7e84c249
FB
2085 int tss_selector, type;
2086 uint32_t e1, e2;
2087
2088 /* specific case for TSS */
2089 if (env->eflags & NT_MASK) {
14ce26e7
FB
2090#ifdef TARGET_X86_64
2091 if (env->hflags & HF_LMA_MASK)
2092 raise_exception_err(EXCP0D_GPF, 0);
2093#endif
7e84c249
FB
2094 tss_selector = lduw_kernel(env->tr.base + 0);
2095 if (tss_selector & 4)
2096 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2097 if (load_segment(&e1, &e2, tss_selector) != 0)
2098 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2099 type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
2100 /* NOTE: we check both segment and busy TSS */
2101 if (type != 3)
2102 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
08cea4ee 2103 switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
7e84c249
FB
2104 } else {
2105 helper_ret_protected(shift, 1, 0);
2106 }
9df217a3
FB
2107#ifdef USE_KQEMU
2108 if (kqemu_is_ok(env)) {
2109 CC_OP = CC_OP_EFLAGS;
2110 env->exception_index = -1;
2111 cpu_loop_exit();
2112 }
2113#endif
2c0262af
FB
2114}
2115
2116void helper_lret_protected(int shift, int addend)
2117{
2118 helper_ret_protected(shift, 0, addend);
9df217a3
FB
2119#ifdef USE_KQEMU
2120 if (kqemu_is_ok(env)) {
2121 CC_OP = CC_OP_EFLAGS;
2122 env->exception_index = -1;
2123 cpu_loop_exit();
2124 }
2125#endif
2c0262af
FB
2126}
2127
023fe10d
FB
2128void helper_sysenter(void)
2129{
2130 if (env->sysenter_cs == 0) {
2131 raise_exception_err(EXCP0D_GPF, 0);
2132 }
2133 env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK);
2134 cpu_x86_set_cpl(env, 0);
2135 cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
14ce26e7 2136 0, 0xffffffff,
023fe10d
FB
2137 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2138 DESC_S_MASK |
2139 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2140 cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc,
14ce26e7 2141 0, 0xffffffff,
023fe10d
FB
2142 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2143 DESC_S_MASK |
2144 DESC_W_MASK | DESC_A_MASK);
2145 ESP = env->sysenter_esp;
2146 EIP = env->sysenter_eip;
2147}
2148
2149void helper_sysexit(void)
2150{
2151 int cpl;
2152
2153 cpl = env->hflags & HF_CPL_MASK;
2154 if (env->sysenter_cs == 0 || cpl != 0) {
2155 raise_exception_err(EXCP0D_GPF, 0);
2156 }
2157 cpu_x86_set_cpl(env, 3);
2158 cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) | 3,
14ce26e7 2159 0, 0xffffffff,
023fe10d
FB
2160 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2161 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2162 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2163 cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) | 3,
14ce26e7 2164 0, 0xffffffff,
023fe10d
FB
2165 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2166 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2167 DESC_W_MASK | DESC_A_MASK);
2168 ESP = ECX;
2169 EIP = EDX;
9df217a3
FB
2170#ifdef USE_KQEMU
2171 if (kqemu_is_ok(env)) {
2172 env->exception_index = -1;
2173 cpu_loop_exit();
2174 }
2175#endif
023fe10d
FB
2176}
2177
2c0262af
FB
2178void helper_movl_crN_T0(int reg)
2179{
4d6b6c0a 2180#if !defined(CONFIG_USER_ONLY)
2c0262af
FB
2181 switch(reg) {
2182 case 0:
1ac157da 2183 cpu_x86_update_cr0(env, T0);
2c0262af
FB
2184 break;
2185 case 3:
1ac157da
FB
2186 cpu_x86_update_cr3(env, T0);
2187 break;
2188 case 4:
2189 cpu_x86_update_cr4(env, T0);
2190 break;
4d6b6c0a
FB
2191 case 8:
2192 cpu_set_apic_tpr(env, T0);
2193 break;
1ac157da
FB
2194 default:
2195 env->cr[reg] = T0;
2c0262af
FB
2196 break;
2197 }
4d6b6c0a 2198#endif
2c0262af
FB
2199}
2200
2201/* XXX: do more */
2202void helper_movl_drN_T0(int reg)
2203{
2204 env->dr[reg] = T0;
2205}
2206
2207void helper_invlpg(unsigned int addr)
2208{
2209 cpu_x86_flush_tlb(env, addr);
2210}
2211
2c0262af
FB
2212void helper_rdtsc(void)
2213{
2214 uint64_t val;
28ab0e2e
FB
2215
2216 val = cpu_get_tsc(env);
14ce26e7
FB
2217 EAX = (uint32_t)(val);
2218 EDX = (uint32_t)(val >> 32);
2219}
2220
2221#if defined(CONFIG_USER_ONLY)
2222void helper_wrmsr(void)
2223{
2c0262af
FB
2224}
2225
14ce26e7
FB
2226void helper_rdmsr(void)
2227{
2228}
2229#else
2c0262af
FB
2230void helper_wrmsr(void)
2231{
14ce26e7
FB
2232 uint64_t val;
2233
2234 val = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
2235
2236 switch((uint32_t)ECX) {
2c0262af 2237 case MSR_IA32_SYSENTER_CS:
14ce26e7 2238 env->sysenter_cs = val & 0xffff;
2c0262af
FB
2239 break;
2240 case MSR_IA32_SYSENTER_ESP:
14ce26e7 2241 env->sysenter_esp = val;
2c0262af
FB
2242 break;
2243 case MSR_IA32_SYSENTER_EIP:
14ce26e7
FB
2244 env->sysenter_eip = val;
2245 break;
2246 case MSR_IA32_APICBASE:
2247 cpu_set_apic_base(env, val);
2248 break;
2249#ifdef TARGET_X86_64
2250 case MSR_EFER:
2251#define MSR_EFER_UPDATE_MASK (MSR_EFER_SCE | MSR_EFER_LME | \
2252 MSR_EFER_NXE | MSR_EFER_FFXSR)
2253 env->efer = (env->efer & ~MSR_EFER_UPDATE_MASK) |
2254 (val & MSR_EFER_UPDATE_MASK);
2c0262af 2255 break;
14ce26e7
FB
2256 case MSR_STAR:
2257 env->star = val;
2258 break;
2259 case MSR_LSTAR:
2260 env->lstar = val;
2261 break;
2262 case MSR_CSTAR:
2263 env->cstar = val;
2264 break;
2265 case MSR_FMASK:
2266 env->fmask = val;
2267 break;
2268 case MSR_FSBASE:
2269 env->segs[R_FS].base = val;
2270 break;
2271 case MSR_GSBASE:
2272 env->segs[R_GS].base = val;
2273 break;
2274 case MSR_KERNELGSBASE:
2275 env->kernelgsbase = val;
2276 break;
2277#endif
2c0262af
FB
2278 default:
2279 /* XXX: exception ? */
2280 break;
2281 }
2282}
2283
2284void helper_rdmsr(void)
2285{
14ce26e7
FB
2286 uint64_t val;
2287 switch((uint32_t)ECX) {
2c0262af 2288 case MSR_IA32_SYSENTER_CS:
14ce26e7 2289 val = env->sysenter_cs;
2c0262af
FB
2290 break;
2291 case MSR_IA32_SYSENTER_ESP:
14ce26e7 2292 val = env->sysenter_esp;
2c0262af
FB
2293 break;
2294 case MSR_IA32_SYSENTER_EIP:
14ce26e7
FB
2295 val = env->sysenter_eip;
2296 break;
2297 case MSR_IA32_APICBASE:
2298 val = cpu_get_apic_base(env);
2299 break;
2300#ifdef TARGET_X86_64
2301 case MSR_EFER:
2302 val = env->efer;
2303 break;
2304 case MSR_STAR:
2305 val = env->star;
2306 break;
2307 case MSR_LSTAR:
2308 val = env->lstar;
2309 break;
2310 case MSR_CSTAR:
2311 val = env->cstar;
2312 break;
2313 case MSR_FMASK:
2314 val = env->fmask;
2315 break;
2316 case MSR_FSBASE:
2317 val = env->segs[R_FS].base;
2318 break;
2319 case MSR_GSBASE:
2320 val = env->segs[R_GS].base;
2c0262af 2321 break;
14ce26e7
FB
2322 case MSR_KERNELGSBASE:
2323 val = env->kernelgsbase;
2324 break;
2325#endif
2c0262af
FB
2326 default:
2327 /* XXX: exception ? */
14ce26e7 2328 val = 0;
2c0262af
FB
2329 break;
2330 }
14ce26e7
FB
2331 EAX = (uint32_t)(val);
2332 EDX = (uint32_t)(val >> 32);
2c0262af 2333}
14ce26e7 2334#endif
2c0262af
FB
2335
2336void helper_lsl(void)
2337{
2338 unsigned int selector, limit;
2339 uint32_t e1, e2;
3ab493de 2340 int rpl, dpl, cpl, type;
2c0262af
FB
2341
2342 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2343 selector = T0 & 0xffff;
2344 if (load_segment(&e1, &e2, selector) != 0)
2345 return;
3ab493de
FB
2346 rpl = selector & 3;
2347 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2348 cpl = env->hflags & HF_CPL_MASK;
2349 if (e2 & DESC_S_MASK) {
2350 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2351 /* conforming */
2352 } else {
2353 if (dpl < cpl || dpl < rpl)
2354 return;
2355 }
2356 } else {
2357 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2358 switch(type) {
2359 case 1:
2360 case 2:
2361 case 3:
2362 case 9:
2363 case 11:
2364 break;
2365 default:
2366 return;
2367 }
2368 if (dpl < cpl || dpl < rpl)
2369 return;
2370 }
2371 limit = get_seg_limit(e1, e2);
2c0262af
FB
2372 T1 = limit;
2373 CC_SRC |= CC_Z;
2374}
2375
2376void helper_lar(void)
2377{
2378 unsigned int selector;
2379 uint32_t e1, e2;
3ab493de 2380 int rpl, dpl, cpl, type;
2c0262af
FB
2381
2382 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2383 selector = T0 & 0xffff;
3ab493de
FB
2384 if ((selector & 0xfffc) == 0)
2385 return;
2c0262af
FB
2386 if (load_segment(&e1, &e2, selector) != 0)
2387 return;
3ab493de
FB
2388 rpl = selector & 3;
2389 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2390 cpl = env->hflags & HF_CPL_MASK;
2391 if (e2 & DESC_S_MASK) {
2392 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2393 /* conforming */
2394 } else {
2395 if (dpl < cpl || dpl < rpl)
2396 return;
2397 }
2398 } else {
2399 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2400 switch(type) {
2401 case 1:
2402 case 2:
2403 case 3:
2404 case 4:
2405 case 5:
2406 case 9:
2407 case 11:
2408 case 12:
2409 break;
2410 default:
2411 return;
2412 }
2413 if (dpl < cpl || dpl < rpl)
2414 return;
2415 }
2c0262af
FB
2416 T1 = e2 & 0x00f0ff00;
2417 CC_SRC |= CC_Z;
2418}
2419
3ab493de
FB
2420void helper_verr(void)
2421{
2422 unsigned int selector;
2423 uint32_t e1, e2;
2424 int rpl, dpl, cpl;
2425
2426 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2427 selector = T0 & 0xffff;
2428 if ((selector & 0xfffc) == 0)
2429 return;
2430 if (load_segment(&e1, &e2, selector) != 0)
2431 return;
2432 if (!(e2 & DESC_S_MASK))
2433 return;
2434 rpl = selector & 3;
2435 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2436 cpl = env->hflags & HF_CPL_MASK;
2437 if (e2 & DESC_CS_MASK) {
2438 if (!(e2 & DESC_R_MASK))
2439 return;
2440 if (!(e2 & DESC_C_MASK)) {
2441 if (dpl < cpl || dpl < rpl)
2442 return;
2443 }
2444 } else {
2445 if (dpl < cpl || dpl < rpl)
2446 return;
2447 }
f3f2d9be 2448 CC_SRC |= CC_Z;
3ab493de
FB
2449}
2450
2451void helper_verw(void)
2452{
2453 unsigned int selector;
2454 uint32_t e1, e2;
2455 int rpl, dpl, cpl;
2456
2457 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2458 selector = T0 & 0xffff;
2459 if ((selector & 0xfffc) == 0)
2460 return;
2461 if (load_segment(&e1, &e2, selector) != 0)
2462 return;
2463 if (!(e2 & DESC_S_MASK))
2464 return;
2465 rpl = selector & 3;
2466 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2467 cpl = env->hflags & HF_CPL_MASK;
2468 if (e2 & DESC_CS_MASK) {
2469 return;
2470 } else {
2471 if (dpl < cpl || dpl < rpl)
2472 return;
2473 if (!(e2 & DESC_W_MASK))
2474 return;
2475 }
f3f2d9be 2476 CC_SRC |= CC_Z;
3ab493de
FB
2477}
2478
2c0262af
FB
2479/* FPU helpers */
2480
2c0262af
FB
2481void helper_fldt_ST0_A0(void)
2482{
2483 int new_fpstt;
2484 new_fpstt = (env->fpstt - 1) & 7;
664e0f19 2485 env->fpregs[new_fpstt].d = helper_fldt(A0);
2c0262af
FB
2486 env->fpstt = new_fpstt;
2487 env->fptags[new_fpstt] = 0; /* validate stack entry */
2488}
2489
2490void helper_fstt_ST0_A0(void)
2491{
14ce26e7 2492 helper_fstt(ST0, A0);
2c0262af 2493}
2c0262af 2494
2ee73ac3
FB
2495void fpu_set_exception(int mask)
2496{
2497 env->fpus |= mask;
2498 if (env->fpus & (~env->fpuc & FPUC_EM))
2499 env->fpus |= FPUS_SE | FPUS_B;
2500}
2501
2502CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b)
2503{
2504 if (b == 0.0)
2505 fpu_set_exception(FPUS_ZE);
2506 return a / b;
2507}
2508
2509void fpu_raise_exception(void)
2510{
2511 if (env->cr[0] & CR0_NE_MASK) {
2512 raise_exception(EXCP10_COPR);
2513 }
2514#if !defined(CONFIG_USER_ONLY)
2515 else {
2516 cpu_set_ferr(env);
2517 }
2518#endif
2519}
2520
2c0262af
FB
2521/* BCD ops */
2522
2c0262af
FB
2523void helper_fbld_ST0_A0(void)
2524{
2525 CPU86_LDouble tmp;
2526 uint64_t val;
2527 unsigned int v;
2528 int i;
2529
2530 val = 0;
2531 for(i = 8; i >= 0; i--) {
14ce26e7 2532 v = ldub(A0 + i);
2c0262af
FB
2533 val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
2534 }
2535 tmp = val;
14ce26e7 2536 if (ldub(A0 + 9) & 0x80)
2c0262af
FB
2537 tmp = -tmp;
2538 fpush();
2539 ST0 = tmp;
2540}
2541
2542void helper_fbst_ST0_A0(void)
2543{
2544 CPU86_LDouble tmp;
2545 int v;
14ce26e7 2546 target_ulong mem_ref, mem_end;
2c0262af
FB
2547 int64_t val;
2548
2549 tmp = rint(ST0);
2550 val = (int64_t)tmp;
14ce26e7 2551 mem_ref = A0;
2c0262af
FB
2552 mem_end = mem_ref + 9;
2553 if (val < 0) {
2554 stb(mem_end, 0x80);
2555 val = -val;
2556 } else {
2557 stb(mem_end, 0x00);
2558 }
2559 while (mem_ref < mem_end) {
2560 if (val == 0)
2561 break;
2562 v = val % 100;
2563 val = val / 100;
2564 v = ((v / 10) << 4) | (v % 10);
2565 stb(mem_ref++, v);
2566 }
2567 while (mem_ref < mem_end) {
2568 stb(mem_ref++, 0);
2569 }
2570}
2571
2572void helper_f2xm1(void)
2573{
2574 ST0 = pow(2.0,ST0) - 1.0;
2575}
2576
2577void helper_fyl2x(void)
2578{
2579 CPU86_LDouble fptemp;
2580
2581 fptemp = ST0;
2582 if (fptemp>0.0){
2583 fptemp = log(fptemp)/log(2.0); /* log2(ST) */
2584 ST1 *= fptemp;
2585 fpop();
2586 } else {
2587 env->fpus &= (~0x4700);
2588 env->fpus |= 0x400;
2589 }
2590}
2591
2592void helper_fptan(void)
2593{
2594 CPU86_LDouble fptemp;
2595
2596 fptemp = ST0;
2597 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2598 env->fpus |= 0x400;
2599 } else {
2600 ST0 = tan(fptemp);
2601 fpush();
2602 ST0 = 1.0;
2603 env->fpus &= (~0x400); /* C2 <-- 0 */
2604 /* the above code is for |arg| < 2**52 only */
2605 }
2606}
2607
2608void helper_fpatan(void)
2609{
2610 CPU86_LDouble fptemp, fpsrcop;
2611
2612 fpsrcop = ST1;
2613 fptemp = ST0;
2614 ST1 = atan2(fpsrcop,fptemp);
2615 fpop();
2616}
2617
2618void helper_fxtract(void)
2619{
2620 CPU86_LDoubleU temp;
2621 unsigned int expdif;
2622
2623 temp.d = ST0;
2624 expdif = EXPD(temp) - EXPBIAS;
2625 /*DP exponent bias*/
2626 ST0 = expdif;
2627 fpush();
2628 BIASEXPONENT(temp);
2629 ST0 = temp.d;
2630}
2631
2632void helper_fprem1(void)
2633{
2634 CPU86_LDouble dblq, fpsrcop, fptemp;
2635 CPU86_LDoubleU fpsrcop1, fptemp1;
2636 int expdif;
2637 int q;
2638
2639 fpsrcop = ST0;
2640 fptemp = ST1;
2641 fpsrcop1.d = fpsrcop;
2642 fptemp1.d = fptemp;
2643 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2644 if (expdif < 53) {
2645 dblq = fpsrcop / fptemp;
2646 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2647 ST0 = fpsrcop - fptemp*dblq;
2648 q = (int)dblq; /* cutting off top bits is assumed here */
2649 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2650 /* (C0,C1,C3) <-- (q2,q1,q0) */
2651 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2652 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2653 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2654 } else {
2655 env->fpus |= 0x400; /* C2 <-- 1 */
2656 fptemp = pow(2.0, expdif-50);
2657 fpsrcop = (ST0 / ST1) / fptemp;
2658 /* fpsrcop = integer obtained by rounding to the nearest */
2659 fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2660 floor(fpsrcop): ceil(fpsrcop);
2661 ST0 -= (ST1 * fpsrcop * fptemp);
2662 }
2663}
2664
2665void helper_fprem(void)
2666{
2667 CPU86_LDouble dblq, fpsrcop, fptemp;
2668 CPU86_LDoubleU fpsrcop1, fptemp1;
2669 int expdif;
2670 int q;
2671
2672 fpsrcop = ST0;
2673 fptemp = ST1;
2674 fpsrcop1.d = fpsrcop;
2675 fptemp1.d = fptemp;
2676 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2677 if ( expdif < 53 ) {
2678 dblq = fpsrcop / fptemp;
2679 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2680 ST0 = fpsrcop - fptemp*dblq;
2681 q = (int)dblq; /* cutting off top bits is assumed here */
2682 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2683 /* (C0,C1,C3) <-- (q2,q1,q0) */
2684 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2685 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2686 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2687 } else {
2688 env->fpus |= 0x400; /* C2 <-- 1 */
2689 fptemp = pow(2.0, expdif-50);
2690 fpsrcop = (ST0 / ST1) / fptemp;
2691 /* fpsrcop = integer obtained by chopping */
2692 fpsrcop = (fpsrcop < 0.0)?
2693 -(floor(fabs(fpsrcop))): floor(fpsrcop);
2694 ST0 -= (ST1 * fpsrcop * fptemp);
2695 }
2696}
2697
2698void helper_fyl2xp1(void)
2699{
2700 CPU86_LDouble fptemp;
2701
2702 fptemp = ST0;
2703 if ((fptemp+1.0)>0.0) {
2704 fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2705 ST1 *= fptemp;
2706 fpop();
2707 } else {
2708 env->fpus &= (~0x4700);
2709 env->fpus |= 0x400;
2710 }
2711}
2712
2713void helper_fsqrt(void)
2714{
2715 CPU86_LDouble fptemp;
2716
2717 fptemp = ST0;
2718 if (fptemp<0.0) {
2719 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2720 env->fpus |= 0x400;
2721 }
2722 ST0 = sqrt(fptemp);
2723}
2724
2725void helper_fsincos(void)
2726{
2727 CPU86_LDouble fptemp;
2728
2729 fptemp = ST0;
2730 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2731 env->fpus |= 0x400;
2732 } else {
2733 ST0 = sin(fptemp);
2734 fpush();
2735 ST0 = cos(fptemp);
2736 env->fpus &= (~0x400); /* C2 <-- 0 */
2737 /* the above code is for |arg| < 2**63 only */
2738 }
2739}
2740
2741void helper_frndint(void)
2742{
2743 CPU86_LDouble a;
2744
2745 a = ST0;
2746#ifdef __arm__
2747 switch(env->fpuc & RC_MASK) {
2748 default:
2749 case RC_NEAR:
2750 asm("rndd %0, %1" : "=f" (a) : "f"(a));
2751 break;
2752 case RC_DOWN:
2753 asm("rnddm %0, %1" : "=f" (a) : "f"(a));
2754 break;
2755 case RC_UP:
2756 asm("rnddp %0, %1" : "=f" (a) : "f"(a));
2757 break;
2758 case RC_CHOP:
2759 asm("rnddz %0, %1" : "=f" (a) : "f"(a));
2760 break;
2761 }
2762#else
2763 a = rint(a);
2764#endif
2765 ST0 = a;
2766}
2767
2768void helper_fscale(void)
2769{
2770 CPU86_LDouble fpsrcop, fptemp;
2771
2772 fpsrcop = 2.0;
2773 fptemp = pow(fpsrcop,ST1);
2774 ST0 *= fptemp;
2775}
2776
2777void helper_fsin(void)
2778{
2779 CPU86_LDouble fptemp;
2780
2781 fptemp = ST0;
2782 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2783 env->fpus |= 0x400;
2784 } else {
2785 ST0 = sin(fptemp);
2786 env->fpus &= (~0x400); /* C2 <-- 0 */
2787 /* the above code is for |arg| < 2**53 only */
2788 }
2789}
2790
2791void helper_fcos(void)
2792{
2793 CPU86_LDouble fptemp;
2794
2795 fptemp = ST0;
2796 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2797 env->fpus |= 0x400;
2798 } else {
2799 ST0 = cos(fptemp);
2800 env->fpus &= (~0x400); /* C2 <-- 0 */
2801 /* the above code is for |arg5 < 2**63 only */
2802 }
2803}
2804
2805void helper_fxam_ST0(void)
2806{
2807 CPU86_LDoubleU temp;
2808 int expdif;
2809
2810 temp.d = ST0;
2811
2812 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2813 if (SIGND(temp))
2814 env->fpus |= 0x200; /* C1 <-- 1 */
2815
2816 expdif = EXPD(temp);
2817 if (expdif == MAXEXPD) {
2818 if (MANTD(temp) == 0)
2819 env->fpus |= 0x500 /*Infinity*/;
2820 else
2821 env->fpus |= 0x100 /*NaN*/;
2822 } else if (expdif == 0) {
2823 if (MANTD(temp) == 0)
2824 env->fpus |= 0x4000 /*Zero*/;
2825 else
2826 env->fpus |= 0x4400 /*Denormal*/;
2827 } else {
2828 env->fpus |= 0x400;
2829 }
2830}
2831
14ce26e7 2832void helper_fstenv(target_ulong ptr, int data32)
2c0262af
FB
2833{
2834 int fpus, fptag, exp, i;
2835 uint64_t mant;
2836 CPU86_LDoubleU tmp;
2837
2838 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2839 fptag = 0;
2840 for (i=7; i>=0; i--) {
2841 fptag <<= 2;
2842 if (env->fptags[i]) {
2843 fptag |= 3;
2844 } else {
664e0f19 2845 tmp.d = env->fpregs[i].d;
2c0262af
FB
2846 exp = EXPD(tmp);
2847 mant = MANTD(tmp);
2848 if (exp == 0 && mant == 0) {
2849 /* zero */
2850 fptag |= 1;
2851 } else if (exp == 0 || exp == MAXEXPD
2852#ifdef USE_X86LDOUBLE
2853 || (mant & (1LL << 63)) == 0
2854#endif
2855 ) {
2856 /* NaNs, infinity, denormal */
2857 fptag |= 2;
2858 }
2859 }
2860 }
2861 if (data32) {
2862 /* 32 bit */
2863 stl(ptr, env->fpuc);
2864 stl(ptr + 4, fpus);
2865 stl(ptr + 8, fptag);
2edcdce3
FB
2866 stl(ptr + 12, 0); /* fpip */
2867 stl(ptr + 16, 0); /* fpcs */
2868 stl(ptr + 20, 0); /* fpoo */
2869 stl(ptr + 24, 0); /* fpos */
2c0262af
FB
2870 } else {
2871 /* 16 bit */
2872 stw(ptr, env->fpuc);
2873 stw(ptr + 2, fpus);
2874 stw(ptr + 4, fptag);
2875 stw(ptr + 6, 0);
2876 stw(ptr + 8, 0);
2877 stw(ptr + 10, 0);
2878 stw(ptr + 12, 0);
2879 }
2880}
2881
14ce26e7 2882void helper_fldenv(target_ulong ptr, int data32)
2c0262af
FB
2883{
2884 int i, fpus, fptag;
2885
2886 if (data32) {
2887 env->fpuc = lduw(ptr);
2888 fpus = lduw(ptr + 4);
2889 fptag = lduw(ptr + 8);
2890 }
2891 else {
2892 env->fpuc = lduw(ptr);
2893 fpus = lduw(ptr + 2);
2894 fptag = lduw(ptr + 4);
2895 }
2896 env->fpstt = (fpus >> 11) & 7;
2897 env->fpus = fpus & ~0x3800;
2edcdce3 2898 for(i = 0;i < 8; i++) {
2c0262af
FB
2899 env->fptags[i] = ((fptag & 3) == 3);
2900 fptag >>= 2;
2901 }
2902}
2903
14ce26e7 2904void helper_fsave(target_ulong ptr, int data32)
2c0262af
FB
2905{
2906 CPU86_LDouble tmp;
2907 int i;
2908
2909 helper_fstenv(ptr, data32);
2910
2911 ptr += (14 << data32);
2912 for(i = 0;i < 8; i++) {
2913 tmp = ST(i);
2c0262af 2914 helper_fstt(tmp, ptr);
2c0262af
FB
2915 ptr += 10;
2916 }
2917
2918 /* fninit */
2919 env->fpus = 0;
2920 env->fpstt = 0;
2921 env->fpuc = 0x37f;
2922 env->fptags[0] = 1;
2923 env->fptags[1] = 1;
2924 env->fptags[2] = 1;
2925 env->fptags[3] = 1;
2926 env->fptags[4] = 1;
2927 env->fptags[5] = 1;
2928 env->fptags[6] = 1;
2929 env->fptags[7] = 1;
2930}
2931
14ce26e7 2932void helper_frstor(target_ulong ptr, int data32)
2c0262af
FB
2933{
2934 CPU86_LDouble tmp;
2935 int i;
2936
2937 helper_fldenv(ptr, data32);
2938 ptr += (14 << data32);
2939
2940 for(i = 0;i < 8; i++) {
2c0262af 2941 tmp = helper_fldt(ptr);
2c0262af
FB
2942 ST(i) = tmp;
2943 ptr += 10;
2944 }
2945}
2946
14ce26e7
FB
2947void helper_fxsave(target_ulong ptr, int data64)
2948{
2949 int fpus, fptag, i, nb_xmm_regs;
2950 CPU86_LDouble tmp;
2951 target_ulong addr;
2952
2953 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2954 fptag = 0;
2955 for(i = 0; i < 8; i++) {
d3c61721 2956 fptag |= (env->fptags[i] << i);
14ce26e7
FB
2957 }
2958 stw(ptr, env->fpuc);
2959 stw(ptr + 2, fpus);
d3c61721 2960 stw(ptr + 4, fptag ^ 0xff);
14ce26e7
FB
2961
2962 addr = ptr + 0x20;
2963 for(i = 0;i < 8; i++) {
2964 tmp = ST(i);
2965 helper_fstt(tmp, addr);
2966 addr += 16;
2967 }
2968
2969 if (env->cr[4] & CR4_OSFXSR_MASK) {
a8ede8ba 2970 /* XXX: finish it */
664e0f19 2971 stl(ptr + 0x18, env->mxcsr); /* mxcsr */
d3c61721 2972 stl(ptr + 0x1c, 0x0000ffff); /* mxcsr_mask */
14ce26e7
FB
2973 nb_xmm_regs = 8 << data64;
2974 addr = ptr + 0xa0;
2975 for(i = 0; i < nb_xmm_regs; i++) {
a8ede8ba
FB
2976 stq(addr, env->xmm_regs[i].XMM_Q(0));
2977 stq(addr + 8, env->xmm_regs[i].XMM_Q(1));
14ce26e7
FB
2978 addr += 16;
2979 }
2980 }
2981}
2982
2983void helper_fxrstor(target_ulong ptr, int data64)
2984{
2985 int i, fpus, fptag, nb_xmm_regs;
2986 CPU86_LDouble tmp;
2987 target_ulong addr;
2988
2989 env->fpuc = lduw(ptr);
2990 fpus = lduw(ptr + 2);
d3c61721 2991 fptag = lduw(ptr + 4);
14ce26e7
FB
2992 env->fpstt = (fpus >> 11) & 7;
2993 env->fpus = fpus & ~0x3800;
2994 fptag ^= 0xff;
2995 for(i = 0;i < 8; i++) {
d3c61721 2996 env->fptags[i] = ((fptag >> i) & 1);
14ce26e7
FB
2997 }
2998
2999 addr = ptr + 0x20;
3000 for(i = 0;i < 8; i++) {
3001 tmp = helper_fldt(addr);
3002 ST(i) = tmp;
3003 addr += 16;
3004 }
3005
3006 if (env->cr[4] & CR4_OSFXSR_MASK) {
31313213 3007 /* XXX: finish it */
664e0f19 3008 env->mxcsr = ldl(ptr + 0x18);
14ce26e7
FB
3009 //ldl(ptr + 0x1c);
3010 nb_xmm_regs = 8 << data64;
3011 addr = ptr + 0xa0;
3012 for(i = 0; i < nb_xmm_regs; i++) {
a8ede8ba
FB
3013 env->xmm_regs[i].XMM_Q(0) = ldq(addr);
3014 env->xmm_regs[i].XMM_Q(1) = ldq(addr + 8);
14ce26e7
FB
3015 addr += 16;
3016 }
3017 }
3018}
1f1af9fd
FB
3019
3020#ifndef USE_X86LDOUBLE
3021
3022void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
3023{
3024 CPU86_LDoubleU temp;
3025 int e;
3026
3027 temp.d = f;
3028 /* mantissa */
3029 *pmant = (MANTD(temp) << 11) | (1LL << 63);
3030 /* exponent + sign */
3031 e = EXPD(temp) - EXPBIAS + 16383;
3032 e |= SIGND(temp) >> 16;
3033 *pexp = e;
3034}
3035
3036CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
3037{
3038 CPU86_LDoubleU temp;
3039 int e;
3040 uint64_t ll;
3041
3042 /* XXX: handle overflow ? */
3043 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
3044 e |= (upper >> 4) & 0x800; /* sign */
3045 ll = (mant >> 11) & ((1LL << 52) - 1);
3046#ifdef __arm__
3047 temp.l.upper = (e << 20) | (ll >> 32);
3048 temp.l.lower = ll;
3049#else
3050 temp.ll = ll | ((uint64_t)e << 52);
3051#endif
3052 return temp.d;
3053}
3054
3055#else
3056
3057void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
3058{
3059 CPU86_LDoubleU temp;
3060
3061 temp.d = f;
3062 *pmant = temp.l.lower;
3063 *pexp = temp.l.upper;
3064}
3065
3066CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
3067{
3068 CPU86_LDoubleU temp;
3069
3070 temp.l.upper = upper;
3071 temp.l.lower = mant;
3072 return temp.d;
3073}
3074#endif
3075
14ce26e7
FB
3076#ifdef TARGET_X86_64
3077
3078//#define DEBUG_MULDIV
3079
3080static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
3081{
3082 *plow += a;
3083 /* carry test */
3084 if (*plow < a)
3085 (*phigh)++;
3086 *phigh += b;
3087}
3088
3089static void neg128(uint64_t *plow, uint64_t *phigh)
3090{
3091 *plow = ~ *plow;
3092 *phigh = ~ *phigh;
3093 add128(plow, phigh, 1, 0);
3094}
3095
3096static void mul64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
3097{
3098 uint32_t a0, a1, b0, b1;
3099 uint64_t v;
3100
3101 a0 = a;
3102 a1 = a >> 32;
3103
3104 b0 = b;
3105 b1 = b >> 32;
3106
3107 v = (uint64_t)a0 * (uint64_t)b0;
3108 *plow = v;
3109 *phigh = 0;
3110
3111 v = (uint64_t)a0 * (uint64_t)b1;
3112 add128(plow, phigh, v << 32, v >> 32);
3113
3114 v = (uint64_t)a1 * (uint64_t)b0;
3115 add128(plow, phigh, v << 32, v >> 32);
3116
3117 v = (uint64_t)a1 * (uint64_t)b1;
3118 *phigh += v;
3119#ifdef DEBUG_MULDIV
3120 printf("mul: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
3121 a, b, *phigh, *plow);
3122#endif
3123}
3124
3125static void imul64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
3126{
3127 int sa, sb;
3128 sa = (a < 0);
3129 if (sa)
3130 a = -a;
3131 sb = (b < 0);
3132 if (sb)
3133 b = -b;
3134 mul64(plow, phigh, a, b);
3135 if (sa ^ sb) {
3136 neg128(plow, phigh);
3137 }
3138}
3139
a8ede8ba 3140/* XXX: overflow support */
14ce26e7
FB
3141static void div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
3142{
3143 uint64_t q, r, a1, a0;
3144 int i, qb;
3145
3146 a0 = *plow;
3147 a1 = *phigh;
3148 if (a1 == 0) {
3149 q = a0 / b;
3150 r = a0 % b;
3151 *plow = q;
3152 *phigh = r;
3153 } else {
3154 /* XXX: use a better algorithm */
3155 for(i = 0; i < 64; i++) {
a8ede8ba 3156 a1 = (a1 << 1) | (a0 >> 63);
14ce26e7
FB
3157 if (a1 >= b) {
3158 a1 -= b;
3159 qb = 1;
3160 } else {
3161 qb = 0;
3162 }
14ce26e7
FB
3163 a0 = (a0 << 1) | qb;
3164 }
a8ede8ba 3165#if defined(DEBUG_MULDIV)
14ce26e7
FB
3166 printf("div: 0x%016llx%016llx / 0x%016llx: q=0x%016llx r=0x%016llx\n",
3167 *phigh, *plow, b, a0, a1);
3168#endif
3169 *plow = a0;
3170 *phigh = a1;
3171 }
3172}
3173
31313213 3174static void idiv64(uint64_t *plow, uint64_t *phigh, int64_t b)
14ce26e7
FB
3175{
3176 int sa, sb;
3177 sa = ((int64_t)*phigh < 0);
3178 if (sa)
3179 neg128(plow, phigh);
3180 sb = (b < 0);
3181 if (sb)
3182 b = -b;
3183 div64(plow, phigh, b);
3184 if (sa ^ sb)
3185 *plow = - *plow;
31313213 3186 if (sa)
14ce26e7
FB
3187 *phigh = - *phigh;
3188}
3189
3190void helper_mulq_EAX_T0(void)
3191{
3192 uint64_t r0, r1;
3193
3194 mul64(&r0, &r1, EAX, T0);
3195 EAX = r0;
3196 EDX = r1;
3197 CC_DST = r0;
3198 CC_SRC = r1;
3199}
3200
3201void helper_imulq_EAX_T0(void)
3202{
3203 uint64_t r0, r1;
3204
3205 imul64(&r0, &r1, EAX, T0);
3206 EAX = r0;
3207 EDX = r1;
3208 CC_DST = r0;
a8ede8ba 3209 CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
14ce26e7
FB
3210}
3211
3212void helper_imulq_T0_T1(void)
3213{
3214 uint64_t r0, r1;
3215
3216 imul64(&r0, &r1, T0, T1);
3217 T0 = r0;
3218 CC_DST = r0;
3219 CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
3220}
3221
3222void helper_divq_EAX_T0(void)
3223{
3224 uint64_t r0, r1;
3225 if (T0 == 0) {
3226 raise_exception(EXCP00_DIVZ);
3227 }
3228 r0 = EAX;
3229 r1 = EDX;
3230 div64(&r0, &r1, T0);
3231 EAX = r0;
3232 EDX = r1;
3233}
3234
3235void helper_idivq_EAX_T0(void)
3236{
3237 uint64_t r0, r1;
3238 if (T0 == 0) {
3239 raise_exception(EXCP00_DIVZ);
3240 }
3241 r0 = EAX;
3242 r1 = EDX;
3243 idiv64(&r0, &r1, T0);
3244 EAX = r0;
3245 EDX = r1;
3246}
3247
3248#endif
3249
664e0f19
FB
3250/* XXX: do it */
3251int fpu_isnan(double a)
3252{
3253 return 0;
3254}
3255
3256float approx_rsqrt(float a)
3257{
3258 return 1.0 / sqrt(a);
3259}
3260
3261float approx_rcp(float a)
3262{
3263 return 1.0 / a;
3264}
3265
4d6b6c0a
FB
3266/* XXX: find a better solution */
3267double helper_sqrt(double a)
3268{
3269 return sqrt(a);
3270}
3271
3272/* XXX: move that to another file */
3273#if defined(__powerpc__)
3274/* better to call an helper on ppc */
3275float int32_to_float32(int32_t a)
3276{
3277 return (float)a;
3278}
3279
3280double int32_to_float64(int32_t a)
3281{
3282 return (double)a;
3283}
3284#endif
664e0f19 3285
61382a50
FB
3286#if !defined(CONFIG_USER_ONLY)
3287
3288#define MMUSUFFIX _mmu
3289#define GETPC() (__builtin_return_address(0))
3290
2c0262af
FB
3291#define SHIFT 0
3292#include "softmmu_template.h"
3293
3294#define SHIFT 1
3295#include "softmmu_template.h"
3296
3297#define SHIFT 2
3298#include "softmmu_template.h"
3299
3300#define SHIFT 3
3301#include "softmmu_template.h"
3302
61382a50
FB
3303#endif
3304
3305/* try to fill the TLB and return an exception if error. If retaddr is
3306 NULL, it means that the function was called in C code (i.e. not
3307 from generated code or from helper.c) */
3308/* XXX: fix it to restore all registers */
14ce26e7 3309void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
2c0262af
FB
3310{
3311 TranslationBlock *tb;
3312 int ret;
3313 unsigned long pc;
61382a50
FB
3314 CPUX86State *saved_env;
3315
3316 /* XXX: hack to restore env in all cases, even if not called from
3317 generated code */
3318 saved_env = env;
3319 env = cpu_single_env;
61382a50
FB
3320
3321 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, is_user, 1);
2c0262af 3322 if (ret) {
61382a50
FB
3323 if (retaddr) {
3324 /* now we have a real cpu fault */
3325 pc = (unsigned long)retaddr;
3326 tb = tb_find_pc(pc);
3327 if (tb) {
3328 /* the PC is inside the translated code. It means that we have
3329 a virtual CPU fault */
58fe2f10 3330 cpu_restore_state(tb, env, pc, NULL);
61382a50 3331 }
2c0262af 3332 }
0d1a29f9
FB
3333 if (retaddr)
3334 raise_exception_err(EXCP0E_PAGE, env->error_code);
3335 else
3336 raise_exception_err_norestore(EXCP0E_PAGE, env->error_code);
2c0262af 3337 }
61382a50 3338 env = saved_env;
2c0262af 3339}