]> Git Repo - qemu.git/blame - target-i386/helper.c
cmov fix
[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);
1015 load_eflags((uint32_t)(env->regs[11]), 0xffffffff);
1016 cpu_x86_set_cpl(env, 3);
1017 } else {
1018 cpu_x86_load_seg_cache(env, R_CS, selector | 3,
1019 0, 0xffffffff,
1020 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1021 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1022 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1023 env->eip = (uint32_t)ECX;
1024 cpu_x86_load_seg_cache(env, R_SS, selector + 8,
1025 0, 0xffffffff,
1026 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1027 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1028 DESC_W_MASK | DESC_A_MASK);
1029 env->eflags |= IF_MASK;
1030 cpu_x86_set_cpl(env, 3);
1031 }
1032}
1033#endif
1034
2c0262af
FB
1035/* real mode interrupt */
1036static void do_interrupt_real(int intno, int is_int, int error_code,
4136f33c 1037 unsigned int next_eip)
2c0262af
FB
1038{
1039 SegmentCache *dt;
14ce26e7 1040 target_ulong ptr, ssp;
2c0262af
FB
1041 int selector;
1042 uint32_t offset, esp;
1043 uint32_t old_cs, old_eip;
1044
1045 /* real mode (simpler !) */
1046 dt = &env->idt;
1047 if (intno * 4 + 3 > dt->limit)
1048 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
1049 ptr = dt->base + intno * 4;
61382a50
FB
1050 offset = lduw_kernel(ptr);
1051 selector = lduw_kernel(ptr + 2);
2c0262af
FB
1052 esp = ESP;
1053 ssp = env->segs[R_SS].base;
1054 if (is_int)
1055 old_eip = next_eip;
1056 else
1057 old_eip = env->eip;
1058 old_cs = env->segs[R_CS].selector;
891b38e4
FB
1059 /* XXX: use SS segment size ? */
1060 PUSHW(ssp, esp, 0xffff, compute_eflags());
1061 PUSHW(ssp, esp, 0xffff, old_cs);
1062 PUSHW(ssp, esp, 0xffff, old_eip);
2c0262af
FB
1063
1064 /* update processor state */
1065 ESP = (ESP & ~0xffff) | (esp & 0xffff);
1066 env->eip = offset;
1067 env->segs[R_CS].selector = selector;
14ce26e7 1068 env->segs[R_CS].base = (selector << 4);
2c0262af
FB
1069 env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
1070}
1071
1072/* fake user mode interrupt */
1073void do_interrupt_user(int intno, int is_int, int error_code,
14ce26e7 1074 target_ulong next_eip)
2c0262af
FB
1075{
1076 SegmentCache *dt;
14ce26e7 1077 target_ulong ptr;
2c0262af
FB
1078 int dpl, cpl;
1079 uint32_t e2;
1080
1081 dt = &env->idt;
1082 ptr = dt->base + (intno * 8);
61382a50 1083 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
1084
1085 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1086 cpl = env->hflags & HF_CPL_MASK;
1087 /* check privledge if software int */
1088 if (is_int && dpl < cpl)
1089 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
1090
1091 /* Since we emulate only user space, we cannot do more than
1092 exiting the emulation with the suitable exception and error
1093 code */
1094 if (is_int)
1095 EIP = next_eip;
1096}
1097
1098/*
e19e89a5 1099 * Begin execution of an interruption. is_int is TRUE if coming from
2c0262af
FB
1100 * the int instruction. next_eip is the EIP value AFTER the interrupt
1101 * instruction. It is only relevant if is_int is TRUE.
1102 */
1103void do_interrupt(int intno, int is_int, int error_code,
14ce26e7 1104 target_ulong next_eip, int is_hw)
2c0262af 1105{
e19e89a5
FB
1106#ifdef DEBUG_PCALL
1107 if (loglevel & (CPU_LOG_PCALL | CPU_LOG_INT)) {
1108 if ((env->cr[0] & CR0_PE_MASK)) {
1109 static int count;
14ce26e7 1110 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
1111 count, intno, error_code, is_int,
1112 env->hflags & HF_CPL_MASK,
1113 env->segs[R_CS].selector, EIP,
2ee73ac3 1114 (int)env->segs[R_CS].base + EIP,
8145122b
FB
1115 env->segs[R_SS].selector, ESP);
1116 if (intno == 0x0e) {
14ce26e7 1117 fprintf(logfile, " CR2=" TARGET_FMT_lx, env->cr[2]);
8145122b 1118 } else {
14ce26e7 1119 fprintf(logfile, " EAX=" TARGET_FMT_lx, EAX);
8145122b 1120 }
e19e89a5 1121 fprintf(logfile, "\n");
14ce26e7 1122#if 0
06c2f506 1123 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
e19e89a5
FB
1124 {
1125 int i;
1126 uint8_t *ptr;
1127 fprintf(logfile, " code=");
1128 ptr = env->segs[R_CS].base + env->eip;
1129 for(i = 0; i < 16; i++) {
1130 fprintf(logfile, " %02x", ldub(ptr + i));
dc6f57fd 1131 }
e19e89a5 1132 fprintf(logfile, "\n");
dc6f57fd 1133 }
8e682019 1134#endif
e19e89a5 1135 count++;
4136f33c 1136 }
4136f33c
FB
1137 }
1138#endif
2c0262af 1139 if (env->cr[0] & CR0_PE_MASK) {
14ce26e7
FB
1140#if TARGET_X86_64
1141 if (env->hflags & HF_LMA_MASK) {
1142 do_interrupt64(intno, is_int, error_code, next_eip, is_hw);
1143 } else
1144#endif
1145 {
1146 do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
1147 }
2c0262af
FB
1148 } else {
1149 do_interrupt_real(intno, is_int, error_code, next_eip);
1150 }
1151}
1152
1153/*
1154 * Signal an interruption. It is executed in the main CPU loop.
1155 * is_int is TRUE if coming from the int instruction. next_eip is the
1156 * EIP value AFTER the interrupt instruction. It is only relevant if
1157 * is_int is TRUE.
1158 */
1159void raise_interrupt(int intno, int is_int, int error_code,
a8ede8ba 1160 int next_eip_addend)
2c0262af
FB
1161{
1162 env->exception_index = intno;
1163 env->error_code = error_code;
1164 env->exception_is_int = is_int;
a8ede8ba 1165 env->exception_next_eip = env->eip + next_eip_addend;
2c0262af
FB
1166 cpu_loop_exit();
1167}
1168
0d1a29f9
FB
1169/* same as raise_exception_err, but do not restore global registers */
1170static void raise_exception_err_norestore(int exception_index, int error_code)
1171{
1172 env->exception_index = exception_index;
1173 env->error_code = error_code;
1174 env->exception_is_int = 0;
1175 env->exception_next_eip = 0;
1176 longjmp(env->jmp_env, 1);
1177}
1178
2c0262af 1179/* shortcuts to generate exceptions */
8145122b
FB
1180
1181void (raise_exception_err)(int exception_index, int error_code)
2c0262af
FB
1182{
1183 raise_interrupt(exception_index, 0, error_code, 0);
1184}
1185
1186void raise_exception(int exception_index)
1187{
1188 raise_interrupt(exception_index, 0, 0, 0);
1189}
1190
1191#ifdef BUGGY_GCC_DIV64
1192/* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
1193 call it from another function */
14ce26e7 1194uint32_t div32(uint32_t *q_ptr, uint64_t num, uint32_t den)
2c0262af
FB
1195{
1196 *q_ptr = num / den;
1197 return num % den;
1198}
1199
14ce26e7 1200int32_t idiv32(int32_t *q_ptr, int64_t num, int32_t den)
2c0262af
FB
1201{
1202 *q_ptr = num / den;
1203 return num % den;
1204}
1205#endif
1206
14ce26e7 1207void helper_divl_EAX_T0(void)
2c0262af
FB
1208{
1209 unsigned int den, q, r;
1210 uint64_t num;
1211
1212 num = EAX | ((uint64_t)EDX << 32);
1213 den = T0;
1214 if (den == 0) {
2c0262af
FB
1215 raise_exception(EXCP00_DIVZ);
1216 }
1217#ifdef BUGGY_GCC_DIV64
14ce26e7 1218 r = div32(&q, num, den);
2c0262af
FB
1219#else
1220 q = (num / den);
1221 r = (num % den);
1222#endif
14ce26e7
FB
1223 EAX = (uint32_t)q;
1224 EDX = (uint32_t)r;
2c0262af
FB
1225}
1226
14ce26e7 1227void helper_idivl_EAX_T0(void)
2c0262af
FB
1228{
1229 int den, q, r;
1230 int64_t num;
1231
1232 num = EAX | ((uint64_t)EDX << 32);
1233 den = T0;
1234 if (den == 0) {
2c0262af
FB
1235 raise_exception(EXCP00_DIVZ);
1236 }
1237#ifdef BUGGY_GCC_DIV64
14ce26e7 1238 r = idiv32(&q, num, den);
2c0262af
FB
1239#else
1240 q = (num / den);
1241 r = (num % den);
1242#endif
14ce26e7
FB
1243 EAX = (uint32_t)q;
1244 EDX = (uint32_t)r;
2c0262af
FB
1245}
1246
1247void helper_cmpxchg8b(void)
1248{
1249 uint64_t d;
1250 int eflags;
1251
1252 eflags = cc_table[CC_OP].compute_all();
14ce26e7 1253 d = ldq(A0);
2c0262af 1254 if (d == (((uint64_t)EDX << 32) | EAX)) {
14ce26e7 1255 stq(A0, ((uint64_t)ECX << 32) | EBX);
2c0262af
FB
1256 eflags |= CC_Z;
1257 } else {
1258 EDX = d >> 32;
1259 EAX = d;
1260 eflags &= ~CC_Z;
1261 }
1262 CC_SRC = eflags;
1263}
1264
2c0262af
FB
1265void helper_cpuid(void)
1266{
14ce26e7 1267 switch((uint32_t)EAX) {
8e682019
FB
1268 case 0:
1269 EAX = 2; /* max EAX index supported */
14ce26e7
FB
1270 EBX = env->cpuid_vendor1;
1271 EDX = env->cpuid_vendor2;
1272 ECX = env->cpuid_vendor3;
8e682019
FB
1273 break;
1274 case 1:
14ce26e7
FB
1275 EAX = env->cpuid_version;
1276 EBX = 0;
9df217a3 1277 ECX = env->cpuid_ext_features;
14ce26e7 1278 EDX = env->cpuid_features;
8e682019
FB
1279 break;
1280 default:
1281 /* cache info: needed for Pentium Pro compatibility */
1282 EAX = 0x410601;
2c0262af
FB
1283 EBX = 0;
1284 ECX = 0;
8e682019
FB
1285 EDX = 0;
1286 break;
14ce26e7
FB
1287#ifdef TARGET_X86_64
1288 case 0x80000000:
1289 EAX = 0x80000008;
1290 EBX = env->cpuid_vendor1;
1291 EDX = env->cpuid_vendor2;
1292 ECX = env->cpuid_vendor3;
1293 break;
1294 case 0x80000001:
1295 EAX = env->cpuid_features;
1296 EBX = 0;
1297 ECX = 0;
1298 /* long mode + syscall/sysret features */
1299 EDX = (env->cpuid_features & 0x0183F3FF) | (1 << 29) | (1 << 11);
1300 break;
1301 case 0x80000008:
1302 /* virtual & phys address size in low 2 bytes. */
1303 EAX = 0x00003028;
1304 EBX = 0;
1305 ECX = 0;
1306 EDX = 0;
1307 break;
1308#endif
2c0262af
FB
1309 }
1310}
1311
61a8c4ec
FB
1312void helper_enter_level(int level, int data32)
1313{
14ce26e7 1314 target_ulong ssp;
61a8c4ec
FB
1315 uint32_t esp_mask, esp, ebp;
1316
1317 esp_mask = get_sp_mask(env->segs[R_SS].flags);
1318 ssp = env->segs[R_SS].base;
1319 ebp = EBP;
1320 esp = ESP;
1321 if (data32) {
1322 /* 32 bit */
1323 esp -= 4;
1324 while (--level) {
1325 esp -= 4;
1326 ebp -= 4;
1327 stl(ssp + (esp & esp_mask), ldl(ssp + (ebp & esp_mask)));
1328 }
1329 esp -= 4;
1330 stl(ssp + (esp & esp_mask), T1);
1331 } else {
1332 /* 16 bit */
1333 esp -= 2;
1334 while (--level) {
1335 esp -= 2;
1336 ebp -= 2;
1337 stw(ssp + (esp & esp_mask), lduw(ssp + (ebp & esp_mask)));
1338 }
1339 esp -= 2;
1340 stw(ssp + (esp & esp_mask), T1);
1341 }
1342}
1343
2c0262af
FB
1344void helper_lldt_T0(void)
1345{
1346 int selector;
1347 SegmentCache *dt;
1348 uint32_t e1, e2;
14ce26e7
FB
1349 int index, entry_limit;
1350 target_ulong ptr;
2c0262af
FB
1351
1352 selector = T0 & 0xffff;
1353 if ((selector & 0xfffc) == 0) {
1354 /* XXX: NULL selector case: invalid LDT */
14ce26e7 1355 env->ldt.base = 0;
2c0262af
FB
1356 env->ldt.limit = 0;
1357 } else {
1358 if (selector & 0x4)
1359 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1360 dt = &env->gdt;
1361 index = selector & ~7;
14ce26e7
FB
1362#ifdef TARGET_X86_64
1363 if (env->hflags & HF_LMA_MASK)
1364 entry_limit = 15;
1365 else
1366#endif
1367 entry_limit = 7;
1368 if ((index + entry_limit) > dt->limit)
2c0262af
FB
1369 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1370 ptr = dt->base + index;
61382a50
FB
1371 e1 = ldl_kernel(ptr);
1372 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
1373 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
1374 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1375 if (!(e2 & DESC_P_MASK))
1376 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
14ce26e7
FB
1377#ifdef TARGET_X86_64
1378 if (env->hflags & HF_LMA_MASK) {
1379 uint32_t e3;
1380 e3 = ldl_kernel(ptr + 8);
1381 load_seg_cache_raw_dt(&env->ldt, e1, e2);
1382 env->ldt.base |= (target_ulong)e3 << 32;
1383 } else
1384#endif
1385 {
1386 load_seg_cache_raw_dt(&env->ldt, e1, e2);
1387 }
2c0262af
FB
1388 }
1389 env->ldt.selector = selector;
1390}
1391
1392void helper_ltr_T0(void)
1393{
1394 int selector;
1395 SegmentCache *dt;
1396 uint32_t e1, e2;
14ce26e7
FB
1397 int index, type, entry_limit;
1398 target_ulong ptr;
2c0262af
FB
1399
1400 selector = T0 & 0xffff;
1401 if ((selector & 0xfffc) == 0) {
14ce26e7
FB
1402 /* NULL selector case: invalid TR */
1403 env->tr.base = 0;
2c0262af
FB
1404 env->tr.limit = 0;
1405 env->tr.flags = 0;
1406 } else {
1407 if (selector & 0x4)
1408 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1409 dt = &env->gdt;
1410 index = selector & ~7;
14ce26e7
FB
1411#ifdef TARGET_X86_64
1412 if (env->hflags & HF_LMA_MASK)
1413 entry_limit = 15;
1414 else
1415#endif
1416 entry_limit = 7;
1417 if ((index + entry_limit) > dt->limit)
2c0262af
FB
1418 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1419 ptr = dt->base + index;
61382a50
FB
1420 e1 = ldl_kernel(ptr);
1421 e2 = ldl_kernel(ptr + 4);
2c0262af
FB
1422 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1423 if ((e2 & DESC_S_MASK) ||
7e84c249 1424 (type != 1 && type != 9))
2c0262af
FB
1425 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1426 if (!(e2 & DESC_P_MASK))
1427 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
14ce26e7
FB
1428#ifdef TARGET_X86_64
1429 if (env->hflags & HF_LMA_MASK) {
1430 uint32_t e3;
1431 e3 = ldl_kernel(ptr + 8);
1432 load_seg_cache_raw_dt(&env->tr, e1, e2);
1433 env->tr.base |= (target_ulong)e3 << 32;
1434 } else
1435#endif
1436 {
1437 load_seg_cache_raw_dt(&env->tr, e1, e2);
1438 }
8e682019 1439 e2 |= DESC_TSS_BUSY_MASK;
61382a50 1440 stl_kernel(ptr + 4, e2);
2c0262af
FB
1441 }
1442 env->tr.selector = selector;
1443}
1444
3ab493de 1445/* only works if protected mode and not VM86. seg_reg must be != R_CS */
8e682019 1446void load_seg(int seg_reg, int selector)
2c0262af
FB
1447{
1448 uint32_t e1, e2;
3ab493de
FB
1449 int cpl, dpl, rpl;
1450 SegmentCache *dt;
1451 int index;
14ce26e7 1452 target_ulong ptr;
3ab493de 1453
8e682019 1454 selector &= 0xffff;
2c0262af
FB
1455 if ((selector & 0xfffc) == 0) {
1456 /* null selector case */
4d6b6c0a
FB
1457 if (seg_reg == R_SS
1458#ifdef TARGET_X86_64
1459 && !(env->hflags & HF_CS64_MASK)
1460#endif
1461 )
2c0262af 1462 raise_exception_err(EXCP0D_GPF, 0);
14ce26e7 1463 cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0);
2c0262af 1464 } else {
3ab493de
FB
1465
1466 if (selector & 0x4)
1467 dt = &env->ldt;
1468 else
1469 dt = &env->gdt;
1470 index = selector & ~7;
8e682019 1471 if ((index + 7) > dt->limit)
2c0262af 1472 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de
FB
1473 ptr = dt->base + index;
1474 e1 = ldl_kernel(ptr);
1475 e2 = ldl_kernel(ptr + 4);
14ce26e7 1476
8e682019 1477 if (!(e2 & DESC_S_MASK))
2c0262af 1478 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de
FB
1479 rpl = selector & 3;
1480 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1481 cpl = env->hflags & HF_CPL_MASK;
2c0262af 1482 if (seg_reg == R_SS) {
3ab493de 1483 /* must be writable segment */
8e682019 1484 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
2c0262af 1485 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
8e682019 1486 if (rpl != cpl || dpl != cpl)
3ab493de 1487 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2c0262af 1488 } else {
3ab493de 1489 /* must be readable segment */
8e682019 1490 if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
2c0262af 1491 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de
FB
1492
1493 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1494 /* if not conforming code, test rights */
8e682019 1495 if (dpl < cpl || dpl < rpl)
3ab493de 1496 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
3ab493de 1497 }
2c0262af
FB
1498 }
1499
1500 if (!(e2 & DESC_P_MASK)) {
2c0262af
FB
1501 if (seg_reg == R_SS)
1502 raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
1503 else
1504 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1505 }
3ab493de
FB
1506
1507 /* set the access bit if not already set */
1508 if (!(e2 & DESC_A_MASK)) {
1509 e2 |= DESC_A_MASK;
1510 stl_kernel(ptr + 4, e2);
1511 }
1512
2c0262af
FB
1513 cpu_x86_load_seg_cache(env, seg_reg, selector,
1514 get_seg_base(e1, e2),
1515 get_seg_limit(e1, e2),
1516 e2);
1517#if 0
1518 fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1519 selector, (unsigned long)sc->base, sc->limit, sc->flags);
1520#endif
1521 }
1522}
1523
1524/* protected mode jump */
08cea4ee 1525void helper_ljmp_protected_T0_T1(int next_eip)
2c0262af 1526{
14ce26e7 1527 int new_cs, gate_cs, type;
2c0262af 1528 uint32_t e1, e2, cpl, dpl, rpl, limit;
14ce26e7
FB
1529 target_ulong new_eip;
1530
2c0262af
FB
1531 new_cs = T0;
1532 new_eip = T1;
1533 if ((new_cs & 0xfffc) == 0)
1534 raise_exception_err(EXCP0D_GPF, 0);
1535 if (load_segment(&e1, &e2, new_cs) != 0)
1536 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1537 cpl = env->hflags & HF_CPL_MASK;
1538 if (e2 & DESC_S_MASK) {
1539 if (!(e2 & DESC_CS_MASK))
1540 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1541 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
7e84c249 1542 if (e2 & DESC_C_MASK) {
2c0262af
FB
1543 /* conforming code segment */
1544 if (dpl > cpl)
1545 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1546 } else {
1547 /* non conforming code segment */
1548 rpl = new_cs & 3;
1549 if (rpl > cpl)
1550 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1551 if (dpl != cpl)
1552 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1553 }
1554 if (!(e2 & DESC_P_MASK))
1555 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1556 limit = get_seg_limit(e1, e2);
ca954f6d
FB
1557 if (new_eip > limit &&
1558 !(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK))
2c0262af
FB
1559 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1560 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1561 get_seg_base(e1, e2), limit, e2);
1562 EIP = new_eip;
1563 } else {
7e84c249
FB
1564 /* jump to call or task gate */
1565 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1566 rpl = new_cs & 3;
1567 cpl = env->hflags & HF_CPL_MASK;
1568 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1569 switch(type) {
1570 case 1: /* 286 TSS */
1571 case 9: /* 386 TSS */
1572 case 5: /* task gate */
1573 if (dpl < cpl || dpl < rpl)
1574 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
08cea4ee 1575 switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
7e84c249
FB
1576 break;
1577 case 4: /* 286 call gate */
1578 case 12: /* 386 call gate */
1579 if ((dpl < cpl) || (dpl < rpl))
1580 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1581 if (!(e2 & DESC_P_MASK))
1582 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1583 gate_cs = e1 >> 16;
516633dc
FB
1584 new_eip = (e1 & 0xffff);
1585 if (type == 12)
1586 new_eip |= (e2 & 0xffff0000);
7e84c249
FB
1587 if (load_segment(&e1, &e2, gate_cs) != 0)
1588 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1589 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1590 /* must be code segment */
1591 if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
1592 (DESC_S_MASK | DESC_CS_MASK)))
1593 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
14ce26e7 1594 if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
7e84c249
FB
1595 (!(e2 & DESC_C_MASK) && (dpl != cpl)))
1596 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1597 if (!(e2 & DESC_P_MASK))
1598 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
7e84c249
FB
1599 limit = get_seg_limit(e1, e2);
1600 if (new_eip > limit)
1601 raise_exception_err(EXCP0D_GPF, 0);
1602 cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1603 get_seg_base(e1, e2), limit, e2);
1604 EIP = new_eip;
1605 break;
1606 default:
1607 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1608 break;
1609 }
2c0262af
FB
1610 }
1611}
1612
1613/* real mode call */
1614void helper_lcall_real_T0_T1(int shift, int next_eip)
1615{
1616 int new_cs, new_eip;
1617 uint32_t esp, esp_mask;
14ce26e7 1618 target_ulong ssp;
2c0262af
FB
1619
1620 new_cs = T0;
1621 new_eip = T1;
1622 esp = ESP;
891b38e4 1623 esp_mask = get_sp_mask(env->segs[R_SS].flags);
2c0262af
FB
1624 ssp = env->segs[R_SS].base;
1625 if (shift) {
891b38e4
FB
1626 PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1627 PUSHL(ssp, esp, esp_mask, next_eip);
2c0262af 1628 } else {
891b38e4
FB
1629 PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1630 PUSHW(ssp, esp, esp_mask, next_eip);
2c0262af
FB
1631 }
1632
891b38e4 1633 ESP = (ESP & ~esp_mask) | (esp & esp_mask);
2c0262af
FB
1634 env->eip = new_eip;
1635 env->segs[R_CS].selector = new_cs;
14ce26e7 1636 env->segs[R_CS].base = (new_cs << 4);
2c0262af
FB
1637}
1638
1639/* protected mode call */
1640void helper_lcall_protected_T0_T1(int shift, int next_eip)
1641{
891b38e4 1642 int new_cs, new_eip, new_stack, i;
2c0262af 1643 uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
891b38e4
FB
1644 uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
1645 uint32_t val, limit, old_sp_mask;
14ce26e7 1646 target_ulong ssp, old_ssp;
2c0262af
FB
1647
1648 new_cs = T0;
1649 new_eip = T1;
f3f2d9be 1650#ifdef DEBUG_PCALL
e19e89a5
FB
1651 if (loglevel & CPU_LOG_PCALL) {
1652 fprintf(logfile, "lcall %04x:%08x s=%d\n",
1653 new_cs, new_eip, shift);
7fe48483 1654 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
f3f2d9be
FB
1655 }
1656#endif
2c0262af
FB
1657 if ((new_cs & 0xfffc) == 0)
1658 raise_exception_err(EXCP0D_GPF, 0);
1659 if (load_segment(&e1, &e2, new_cs) != 0)
1660 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1661 cpl = env->hflags & HF_CPL_MASK;
f3f2d9be 1662#ifdef DEBUG_PCALL
e19e89a5 1663 if (loglevel & CPU_LOG_PCALL) {
f3f2d9be
FB
1664 fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
1665 }
1666#endif
2c0262af
FB
1667 if (e2 & DESC_S_MASK) {
1668 if (!(e2 & DESC_CS_MASK))
1669 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1670 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
7e84c249 1671 if (e2 & DESC_C_MASK) {
2c0262af
FB
1672 /* conforming code segment */
1673 if (dpl > cpl)
1674 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1675 } else {
1676 /* non conforming code segment */
1677 rpl = new_cs & 3;
1678 if (rpl > cpl)
1679 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1680 if (dpl != cpl)
1681 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1682 }
1683 if (!(e2 & DESC_P_MASK))
1684 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1685
1686 sp = ESP;
891b38e4
FB
1687 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1688 ssp = env->segs[R_SS].base;
2c0262af 1689 if (shift) {
891b38e4
FB
1690 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1691 PUSHL(ssp, sp, sp_mask, next_eip);
2c0262af 1692 } else {
891b38e4
FB
1693 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1694 PUSHW(ssp, sp, sp_mask, next_eip);
2c0262af 1695 }
2c0262af
FB
1696
1697 limit = get_seg_limit(e1, e2);
1698 if (new_eip > limit)
1699 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1700 /* from this point, not restartable */
891b38e4 1701 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
1702 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1703 get_seg_base(e1, e2), limit, e2);
1704 EIP = new_eip;
1705 } else {
1706 /* check gate type */
1707 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
7e84c249
FB
1708 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1709 rpl = new_cs & 3;
2c0262af
FB
1710 switch(type) {
1711 case 1: /* available 286 TSS */
1712 case 9: /* available 386 TSS */
1713 case 5: /* task gate */
7e84c249
FB
1714 if (dpl < cpl || dpl < rpl)
1715 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
883da8e2 1716 switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
8145122b 1717 return;
2c0262af
FB
1718 case 4: /* 286 call gate */
1719 case 12: /* 386 call gate */
1720 break;
1721 default:
1722 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1723 break;
1724 }
1725 shift = type >> 3;
1726
2c0262af
FB
1727 if (dpl < cpl || dpl < rpl)
1728 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1729 /* check valid bit */
1730 if (!(e2 & DESC_P_MASK))
1731 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1732 selector = e1 >> 16;
1733 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
f3f2d9be 1734 param_count = e2 & 0x1f;
2c0262af
FB
1735 if ((selector & 0xfffc) == 0)
1736 raise_exception_err(EXCP0D_GPF, 0);
1737
1738 if (load_segment(&e1, &e2, selector) != 0)
1739 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1740 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
1741 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1742 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1743 if (dpl > cpl)
1744 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1745 if (!(e2 & DESC_P_MASK))
1746 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1747
1748 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1749 /* to inner priviledge */
1750 get_ss_esp_from_tss(&ss, &sp, dpl);
f3f2d9be 1751#ifdef DEBUG_PCALL
e19e89a5 1752 if (loglevel & CPU_LOG_PCALL)
14ce26e7 1753 fprintf(logfile, "new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx "\n",
f3f2d9be
FB
1754 ss, sp, param_count, ESP);
1755#endif
2c0262af
FB
1756 if ((ss & 0xfffc) == 0)
1757 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1758 if ((ss & 3) != dpl)
1759 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1760 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
1761 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1762 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1763 if (ss_dpl != dpl)
1764 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1765 if (!(ss_e2 & DESC_S_MASK) ||
1766 (ss_e2 & DESC_CS_MASK) ||
1767 !(ss_e2 & DESC_W_MASK))
1768 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1769 if (!(ss_e2 & DESC_P_MASK))
1770 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1771
891b38e4 1772 // push_size = ((param_count * 2) + 8) << shift;
2c0262af 1773
891b38e4
FB
1774 old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1775 old_ssp = env->segs[R_SS].base;
2c0262af 1776
891b38e4
FB
1777 sp_mask = get_sp_mask(ss_e2);
1778 ssp = get_seg_base(ss_e1, ss_e2);
2c0262af 1779 if (shift) {
891b38e4
FB
1780 PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1781 PUSHL(ssp, sp, sp_mask, ESP);
1782 for(i = param_count - 1; i >= 0; i--) {
1783 val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
1784 PUSHL(ssp, sp, sp_mask, val);
2c0262af
FB
1785 }
1786 } else {
891b38e4
FB
1787 PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1788 PUSHW(ssp, sp, sp_mask, ESP);
1789 for(i = param_count - 1; i >= 0; i--) {
1790 val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
1791 PUSHW(ssp, sp, sp_mask, val);
2c0262af
FB
1792 }
1793 }
891b38e4 1794 new_stack = 1;
2c0262af
FB
1795 } else {
1796 /* to same priviledge */
891b38e4
FB
1797 sp = ESP;
1798 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1799 ssp = env->segs[R_SS].base;
1800 // push_size = (4 << shift);
1801 new_stack = 0;
2c0262af
FB
1802 }
1803
1804 if (shift) {
891b38e4
FB
1805 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1806 PUSHL(ssp, sp, sp_mask, next_eip);
2c0262af 1807 } else {
891b38e4
FB
1808 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1809 PUSHW(ssp, sp, sp_mask, next_eip);
1810 }
1811
1812 /* from this point, not restartable */
1813
1814 if (new_stack) {
1815 ss = (ss & ~3) | dpl;
1816 cpu_x86_load_seg_cache(env, R_SS, ss,
1817 ssp,
1818 get_seg_limit(ss_e1, ss_e2),
1819 ss_e2);
2c0262af
FB
1820 }
1821
2c0262af
FB
1822 selector = (selector & ~3) | dpl;
1823 cpu_x86_load_seg_cache(env, R_CS, selector,
1824 get_seg_base(e1, e2),
1825 get_seg_limit(e1, e2),
1826 e2);
1827 cpu_x86_set_cpl(env, dpl);
891b38e4 1828 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
1829 EIP = offset;
1830 }
9df217a3
FB
1831#ifdef USE_KQEMU
1832 if (kqemu_is_ok(env)) {
1833 env->exception_index = -1;
1834 cpu_loop_exit();
1835 }
1836#endif
2c0262af
FB
1837}
1838
7e84c249 1839/* real and vm86 mode iret */
2c0262af
FB
1840void helper_iret_real(int shift)
1841{
891b38e4 1842 uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
14ce26e7 1843 target_ulong ssp;
2c0262af 1844 int eflags_mask;
7e84c249 1845
891b38e4
FB
1846 sp_mask = 0xffff; /* XXXX: use SS segment size ? */
1847 sp = ESP;
1848 ssp = env->segs[R_SS].base;
2c0262af
FB
1849 if (shift == 1) {
1850 /* 32 bits */
891b38e4
FB
1851 POPL(ssp, sp, sp_mask, new_eip);
1852 POPL(ssp, sp, sp_mask, new_cs);
1853 new_cs &= 0xffff;
1854 POPL(ssp, sp, sp_mask, new_eflags);
2c0262af
FB
1855 } else {
1856 /* 16 bits */
891b38e4
FB
1857 POPW(ssp, sp, sp_mask, new_eip);
1858 POPW(ssp, sp, sp_mask, new_cs);
1859 POPW(ssp, sp, sp_mask, new_eflags);
2c0262af 1860 }
4136f33c 1861 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
1862 load_seg_vm(R_CS, new_cs);
1863 env->eip = new_eip;
7e84c249 1864 if (env->eflags & VM_MASK)
8145122b 1865 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
7e84c249 1866 else
8145122b 1867 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
2c0262af
FB
1868 if (shift == 0)
1869 eflags_mask &= 0xffff;
1870 load_eflags(new_eflags, eflags_mask);
1871}
1872
8e682019
FB
1873static inline void validate_seg(int seg_reg, int cpl)
1874{
1875 int dpl;
1876 uint32_t e2;
1877
1878 e2 = env->segs[seg_reg].flags;
1879 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1880 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1881 /* data or non conforming code segment */
1882 if (dpl < cpl) {
14ce26e7 1883 cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
8e682019
FB
1884 }
1885 }
1886}
1887
2c0262af
FB
1888/* protected mode iret */
1889static inline void helper_ret_protected(int shift, int is_iret, int addend)
1890{
14ce26e7 1891 uint32_t new_cs, new_eflags, new_ss;
2c0262af
FB
1892 uint32_t new_es, new_ds, new_fs, new_gs;
1893 uint32_t e1, e2, ss_e1, ss_e2;
4136f33c 1894 int cpl, dpl, rpl, eflags_mask, iopl;
14ce26e7 1895 target_ulong ssp, sp, new_eip, new_esp, sp_mask;
2c0262af 1896
14ce26e7
FB
1897#ifdef TARGET_X86_64
1898 if (shift == 2)
1899 sp_mask = -1;
1900 else
1901#endif
1902 sp_mask = get_sp_mask(env->segs[R_SS].flags);
2c0262af 1903 sp = ESP;
891b38e4 1904 ssp = env->segs[R_SS].base;
354ff226 1905 new_eflags = 0; /* avoid warning */
14ce26e7
FB
1906#ifdef TARGET_X86_64
1907 if (shift == 2) {
1908 POPQ(sp, new_eip);
1909 POPQ(sp, new_cs);
1910 new_cs &= 0xffff;
1911 if (is_iret) {
1912 POPQ(sp, new_eflags);
1913 }
1914 } else
1915#endif
2c0262af
FB
1916 if (shift == 1) {
1917 /* 32 bits */
891b38e4
FB
1918 POPL(ssp, sp, sp_mask, new_eip);
1919 POPL(ssp, sp, sp_mask, new_cs);
1920 new_cs &= 0xffff;
1921 if (is_iret) {
1922 POPL(ssp, sp, sp_mask, new_eflags);
1923 if (new_eflags & VM_MASK)
1924 goto return_to_vm86;
1925 }
2c0262af
FB
1926 } else {
1927 /* 16 bits */
891b38e4
FB
1928 POPW(ssp, sp, sp_mask, new_eip);
1929 POPW(ssp, sp, sp_mask, new_cs);
2c0262af 1930 if (is_iret)
891b38e4 1931 POPW(ssp, sp, sp_mask, new_eflags);
2c0262af 1932 }
891b38e4 1933#ifdef DEBUG_PCALL
e19e89a5 1934 if (loglevel & CPU_LOG_PCALL) {
14ce26e7 1935 fprintf(logfile, "lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n",
e19e89a5 1936 new_cs, new_eip, shift, addend);
7fe48483 1937 cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
891b38e4
FB
1938 }
1939#endif
2c0262af
FB
1940 if ((new_cs & 0xfffc) == 0)
1941 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1942 if (load_segment(&e1, &e2, new_cs) != 0)
1943 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1944 if (!(e2 & DESC_S_MASK) ||
1945 !(e2 & DESC_CS_MASK))
1946 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1947 cpl = env->hflags & HF_CPL_MASK;
1948 rpl = new_cs & 3;
1949 if (rpl < cpl)
1950 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1951 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
7e84c249 1952 if (e2 & DESC_C_MASK) {
2c0262af
FB
1953 if (dpl > rpl)
1954 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1955 } else {
1956 if (dpl != rpl)
1957 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1958 }
1959 if (!(e2 & DESC_P_MASK))
1960 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1961
891b38e4 1962 sp += addend;
ca954f6d
FB
1963 if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) ||
1964 ((env->hflags & HF_CS64_MASK) && !is_iret))) {
2c0262af
FB
1965 /* return to same priledge level */
1966 cpu_x86_load_seg_cache(env, R_CS, new_cs,
1967 get_seg_base(e1, e2),
1968 get_seg_limit(e1, e2),
1969 e2);
2c0262af
FB
1970 } else {
1971 /* return to different priviledge level */
14ce26e7
FB
1972#ifdef TARGET_X86_64
1973 if (shift == 2) {
1974 POPQ(sp, new_esp);
1975 POPQ(sp, new_ss);
1976 new_ss &= 0xffff;
1977 } else
1978#endif
2c0262af
FB
1979 if (shift == 1) {
1980 /* 32 bits */
891b38e4
FB
1981 POPL(ssp, sp, sp_mask, new_esp);
1982 POPL(ssp, sp, sp_mask, new_ss);
1983 new_ss &= 0xffff;
2c0262af
FB
1984 } else {
1985 /* 16 bits */
891b38e4
FB
1986 POPW(ssp, sp, sp_mask, new_esp);
1987 POPW(ssp, sp, sp_mask, new_ss);
2c0262af 1988 }
e19e89a5
FB
1989#ifdef DEBUG_PCALL
1990 if (loglevel & CPU_LOG_PCALL) {
14ce26e7 1991 fprintf(logfile, "new ss:esp=%04x:" TARGET_FMT_lx "\n",
e19e89a5
FB
1992 new_ss, new_esp);
1993 }
1994#endif
14ce26e7
FB
1995 if ((env->hflags & HF_LMA_MASK) && (new_ss & 0xfffc) == 0) {
1996 /* NULL ss is allowed in long mode */
1997 cpu_x86_load_seg_cache(env, R_SS, new_ss,
1998 0, 0xffffffff,
1999 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2000 DESC_S_MASK | (rpl << DESC_DPL_SHIFT) |
2001 DESC_W_MASK | DESC_A_MASK);
2002 } else {
2003 if ((new_ss & 3) != rpl)
2004 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2005 if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
2006 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2007 if (!(ss_e2 & DESC_S_MASK) ||
2008 (ss_e2 & DESC_CS_MASK) ||
2009 !(ss_e2 & DESC_W_MASK))
2010 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2011 dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
2012 if (dpl != rpl)
2013 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2014 if (!(ss_e2 & DESC_P_MASK))
2015 raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
2016 cpu_x86_load_seg_cache(env, R_SS, new_ss,
2017 get_seg_base(ss_e1, ss_e2),
2018 get_seg_limit(ss_e1, ss_e2),
2019 ss_e2);
2020 }
2c0262af
FB
2021
2022 cpu_x86_load_seg_cache(env, R_CS, new_cs,
2023 get_seg_base(e1, e2),
2024 get_seg_limit(e1, e2),
2025 e2);
2c0262af 2026 cpu_x86_set_cpl(env, rpl);
891b38e4 2027 sp = new_esp;
14ce26e7
FB
2028#ifdef TARGET_X86_64
2029 if (shift == 2)
2030 sp_mask = -1;
2031 else
2032#endif
2033 sp_mask = get_sp_mask(ss_e2);
8e682019
FB
2034
2035 /* validate data segments */
2036 validate_seg(R_ES, cpl);
2037 validate_seg(R_DS, cpl);
2038 validate_seg(R_FS, cpl);
2039 validate_seg(R_GS, cpl);
4afa6482
FB
2040
2041 sp += addend;
2c0262af 2042 }
891b38e4 2043 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2c0262af
FB
2044 env->eip = new_eip;
2045 if (is_iret) {
4136f33c 2046 /* NOTE: 'cpl' is the _old_ CPL */
8145122b 2047 eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
2c0262af 2048 if (cpl == 0)
4136f33c
FB
2049 eflags_mask |= IOPL_MASK;
2050 iopl = (env->eflags >> IOPL_SHIFT) & 3;
2051 if (cpl <= iopl)
2052 eflags_mask |= IF_MASK;
2c0262af
FB
2053 if (shift == 0)
2054 eflags_mask &= 0xffff;
2055 load_eflags(new_eflags, eflags_mask);
2056 }
2057 return;
2058
2059 return_to_vm86:
891b38e4
FB
2060 POPL(ssp, sp, sp_mask, new_esp);
2061 POPL(ssp, sp, sp_mask, new_ss);
2062 POPL(ssp, sp, sp_mask, new_es);
2063 POPL(ssp, sp, sp_mask, new_ds);
2064 POPL(ssp, sp, sp_mask, new_fs);
2065 POPL(ssp, sp, sp_mask, new_gs);
2c0262af
FB
2066
2067 /* modify processor state */
4136f33c 2068 load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK |
8145122b 2069 IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
891b38e4 2070 load_seg_vm(R_CS, new_cs & 0xffff);
2c0262af 2071 cpu_x86_set_cpl(env, 3);
891b38e4
FB
2072 load_seg_vm(R_SS, new_ss & 0xffff);
2073 load_seg_vm(R_ES, new_es & 0xffff);
2074 load_seg_vm(R_DS, new_ds & 0xffff);
2075 load_seg_vm(R_FS, new_fs & 0xffff);
2076 load_seg_vm(R_GS, new_gs & 0xffff);
2c0262af 2077
fd836909 2078 env->eip = new_eip & 0xffff;
2c0262af
FB
2079 ESP = new_esp;
2080}
2081
08cea4ee 2082void helper_iret_protected(int shift, int next_eip)
2c0262af 2083{
7e84c249
FB
2084 int tss_selector, type;
2085 uint32_t e1, e2;
2086
2087 /* specific case for TSS */
2088 if (env->eflags & NT_MASK) {
14ce26e7
FB
2089#ifdef TARGET_X86_64
2090 if (env->hflags & HF_LMA_MASK)
2091 raise_exception_err(EXCP0D_GPF, 0);
2092#endif
7e84c249
FB
2093 tss_selector = lduw_kernel(env->tr.base + 0);
2094 if (tss_selector & 4)
2095 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2096 if (load_segment(&e1, &e2, tss_selector) != 0)
2097 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2098 type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
2099 /* NOTE: we check both segment and busy TSS */
2100 if (type != 3)
2101 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
08cea4ee 2102 switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
7e84c249
FB
2103 } else {
2104 helper_ret_protected(shift, 1, 0);
2105 }
9df217a3
FB
2106#ifdef USE_KQEMU
2107 if (kqemu_is_ok(env)) {
2108 CC_OP = CC_OP_EFLAGS;
2109 env->exception_index = -1;
2110 cpu_loop_exit();
2111 }
2112#endif
2c0262af
FB
2113}
2114
2115void helper_lret_protected(int shift, int addend)
2116{
2117 helper_ret_protected(shift, 0, addend);
9df217a3
FB
2118#ifdef USE_KQEMU
2119 if (kqemu_is_ok(env)) {
2120 CC_OP = CC_OP_EFLAGS;
2121 env->exception_index = -1;
2122 cpu_loop_exit();
2123 }
2124#endif
2c0262af
FB
2125}
2126
023fe10d
FB
2127void helper_sysenter(void)
2128{
2129 if (env->sysenter_cs == 0) {
2130 raise_exception_err(EXCP0D_GPF, 0);
2131 }
2132 env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK);
2133 cpu_x86_set_cpl(env, 0);
2134 cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
14ce26e7 2135 0, 0xffffffff,
023fe10d
FB
2136 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2137 DESC_S_MASK |
2138 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2139 cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc,
14ce26e7 2140 0, 0xffffffff,
023fe10d
FB
2141 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2142 DESC_S_MASK |
2143 DESC_W_MASK | DESC_A_MASK);
2144 ESP = env->sysenter_esp;
2145 EIP = env->sysenter_eip;
2146}
2147
2148void helper_sysexit(void)
2149{
2150 int cpl;
2151
2152 cpl = env->hflags & HF_CPL_MASK;
2153 if (env->sysenter_cs == 0 || cpl != 0) {
2154 raise_exception_err(EXCP0D_GPF, 0);
2155 }
2156 cpu_x86_set_cpl(env, 3);
2157 cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) | 3,
14ce26e7 2158 0, 0xffffffff,
023fe10d
FB
2159 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2160 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2161 DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2162 cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) | 3,
14ce26e7 2163 0, 0xffffffff,
023fe10d
FB
2164 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2165 DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2166 DESC_W_MASK | DESC_A_MASK);
2167 ESP = ECX;
2168 EIP = EDX;
9df217a3
FB
2169#ifdef USE_KQEMU
2170 if (kqemu_is_ok(env)) {
2171 env->exception_index = -1;
2172 cpu_loop_exit();
2173 }
2174#endif
023fe10d
FB
2175}
2176
2c0262af
FB
2177void helper_movl_crN_T0(int reg)
2178{
4d6b6c0a 2179#if !defined(CONFIG_USER_ONLY)
2c0262af
FB
2180 switch(reg) {
2181 case 0:
1ac157da 2182 cpu_x86_update_cr0(env, T0);
2c0262af
FB
2183 break;
2184 case 3:
1ac157da
FB
2185 cpu_x86_update_cr3(env, T0);
2186 break;
2187 case 4:
2188 cpu_x86_update_cr4(env, T0);
2189 break;
4d6b6c0a
FB
2190 case 8:
2191 cpu_set_apic_tpr(env, T0);
2192 break;
1ac157da
FB
2193 default:
2194 env->cr[reg] = T0;
2c0262af
FB
2195 break;
2196 }
4d6b6c0a 2197#endif
2c0262af
FB
2198}
2199
2200/* XXX: do more */
2201void helper_movl_drN_T0(int reg)
2202{
2203 env->dr[reg] = T0;
2204}
2205
2206void helper_invlpg(unsigned int addr)
2207{
2208 cpu_x86_flush_tlb(env, addr);
2209}
2210
2c0262af
FB
2211void helper_rdtsc(void)
2212{
2213 uint64_t val;
28ab0e2e
FB
2214
2215 val = cpu_get_tsc(env);
14ce26e7
FB
2216 EAX = (uint32_t)(val);
2217 EDX = (uint32_t)(val >> 32);
2218}
2219
2220#if defined(CONFIG_USER_ONLY)
2221void helper_wrmsr(void)
2222{
2c0262af
FB
2223}
2224
14ce26e7
FB
2225void helper_rdmsr(void)
2226{
2227}
2228#else
2c0262af
FB
2229void helper_wrmsr(void)
2230{
14ce26e7
FB
2231 uint64_t val;
2232
2233 val = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
2234
2235 switch((uint32_t)ECX) {
2c0262af 2236 case MSR_IA32_SYSENTER_CS:
14ce26e7 2237 env->sysenter_cs = val & 0xffff;
2c0262af
FB
2238 break;
2239 case MSR_IA32_SYSENTER_ESP:
14ce26e7 2240 env->sysenter_esp = val;
2c0262af
FB
2241 break;
2242 case MSR_IA32_SYSENTER_EIP:
14ce26e7
FB
2243 env->sysenter_eip = val;
2244 break;
2245 case MSR_IA32_APICBASE:
2246 cpu_set_apic_base(env, val);
2247 break;
2248#ifdef TARGET_X86_64
2249 case MSR_EFER:
2250#define MSR_EFER_UPDATE_MASK (MSR_EFER_SCE | MSR_EFER_LME | \
2251 MSR_EFER_NXE | MSR_EFER_FFXSR)
2252 env->efer = (env->efer & ~MSR_EFER_UPDATE_MASK) |
2253 (val & MSR_EFER_UPDATE_MASK);
2c0262af 2254 break;
14ce26e7
FB
2255 case MSR_STAR:
2256 env->star = val;
2257 break;
2258 case MSR_LSTAR:
2259 env->lstar = val;
2260 break;
2261 case MSR_CSTAR:
2262 env->cstar = val;
2263 break;
2264 case MSR_FMASK:
2265 env->fmask = val;
2266 break;
2267 case MSR_FSBASE:
2268 env->segs[R_FS].base = val;
2269 break;
2270 case MSR_GSBASE:
2271 env->segs[R_GS].base = val;
2272 break;
2273 case MSR_KERNELGSBASE:
2274 env->kernelgsbase = val;
2275 break;
2276#endif
2c0262af
FB
2277 default:
2278 /* XXX: exception ? */
2279 break;
2280 }
2281}
2282
2283void helper_rdmsr(void)
2284{
14ce26e7
FB
2285 uint64_t val;
2286 switch((uint32_t)ECX) {
2c0262af 2287 case MSR_IA32_SYSENTER_CS:
14ce26e7 2288 val = env->sysenter_cs;
2c0262af
FB
2289 break;
2290 case MSR_IA32_SYSENTER_ESP:
14ce26e7 2291 val = env->sysenter_esp;
2c0262af
FB
2292 break;
2293 case MSR_IA32_SYSENTER_EIP:
14ce26e7
FB
2294 val = env->sysenter_eip;
2295 break;
2296 case MSR_IA32_APICBASE:
2297 val = cpu_get_apic_base(env);
2298 break;
2299#ifdef TARGET_X86_64
2300 case MSR_EFER:
2301 val = env->efer;
2302 break;
2303 case MSR_STAR:
2304 val = env->star;
2305 break;
2306 case MSR_LSTAR:
2307 val = env->lstar;
2308 break;
2309 case MSR_CSTAR:
2310 val = env->cstar;
2311 break;
2312 case MSR_FMASK:
2313 val = env->fmask;
2314 break;
2315 case MSR_FSBASE:
2316 val = env->segs[R_FS].base;
2317 break;
2318 case MSR_GSBASE:
2319 val = env->segs[R_GS].base;
2c0262af 2320 break;
14ce26e7
FB
2321 case MSR_KERNELGSBASE:
2322 val = env->kernelgsbase;
2323 break;
2324#endif
2c0262af
FB
2325 default:
2326 /* XXX: exception ? */
14ce26e7 2327 val = 0;
2c0262af
FB
2328 break;
2329 }
14ce26e7
FB
2330 EAX = (uint32_t)(val);
2331 EDX = (uint32_t)(val >> 32);
2c0262af 2332}
14ce26e7 2333#endif
2c0262af
FB
2334
2335void helper_lsl(void)
2336{
2337 unsigned int selector, limit;
2338 uint32_t e1, e2;
3ab493de 2339 int rpl, dpl, cpl, type;
2c0262af
FB
2340
2341 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2342 selector = T0 & 0xffff;
2343 if (load_segment(&e1, &e2, selector) != 0)
2344 return;
3ab493de
FB
2345 rpl = selector & 3;
2346 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2347 cpl = env->hflags & HF_CPL_MASK;
2348 if (e2 & DESC_S_MASK) {
2349 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2350 /* conforming */
2351 } else {
2352 if (dpl < cpl || dpl < rpl)
2353 return;
2354 }
2355 } else {
2356 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2357 switch(type) {
2358 case 1:
2359 case 2:
2360 case 3:
2361 case 9:
2362 case 11:
2363 break;
2364 default:
2365 return;
2366 }
2367 if (dpl < cpl || dpl < rpl)
2368 return;
2369 }
2370 limit = get_seg_limit(e1, e2);
2c0262af
FB
2371 T1 = limit;
2372 CC_SRC |= CC_Z;
2373}
2374
2375void helper_lar(void)
2376{
2377 unsigned int selector;
2378 uint32_t e1, e2;
3ab493de 2379 int rpl, dpl, cpl, type;
2c0262af
FB
2380
2381 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2382 selector = T0 & 0xffff;
3ab493de
FB
2383 if ((selector & 0xfffc) == 0)
2384 return;
2c0262af
FB
2385 if (load_segment(&e1, &e2, selector) != 0)
2386 return;
3ab493de
FB
2387 rpl = selector & 3;
2388 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2389 cpl = env->hflags & HF_CPL_MASK;
2390 if (e2 & DESC_S_MASK) {
2391 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2392 /* conforming */
2393 } else {
2394 if (dpl < cpl || dpl < rpl)
2395 return;
2396 }
2397 } else {
2398 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2399 switch(type) {
2400 case 1:
2401 case 2:
2402 case 3:
2403 case 4:
2404 case 5:
2405 case 9:
2406 case 11:
2407 case 12:
2408 break;
2409 default:
2410 return;
2411 }
2412 if (dpl < cpl || dpl < rpl)
2413 return;
2414 }
2c0262af
FB
2415 T1 = e2 & 0x00f0ff00;
2416 CC_SRC |= CC_Z;
2417}
2418
3ab493de
FB
2419void helper_verr(void)
2420{
2421 unsigned int selector;
2422 uint32_t e1, e2;
2423 int rpl, dpl, cpl;
2424
2425 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2426 selector = T0 & 0xffff;
2427 if ((selector & 0xfffc) == 0)
2428 return;
2429 if (load_segment(&e1, &e2, selector) != 0)
2430 return;
2431 if (!(e2 & DESC_S_MASK))
2432 return;
2433 rpl = selector & 3;
2434 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2435 cpl = env->hflags & HF_CPL_MASK;
2436 if (e2 & DESC_CS_MASK) {
2437 if (!(e2 & DESC_R_MASK))
2438 return;
2439 if (!(e2 & DESC_C_MASK)) {
2440 if (dpl < cpl || dpl < rpl)
2441 return;
2442 }
2443 } else {
2444 if (dpl < cpl || dpl < rpl)
2445 return;
2446 }
f3f2d9be 2447 CC_SRC |= CC_Z;
3ab493de
FB
2448}
2449
2450void helper_verw(void)
2451{
2452 unsigned int selector;
2453 uint32_t e1, e2;
2454 int rpl, dpl, cpl;
2455
2456 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2457 selector = T0 & 0xffff;
2458 if ((selector & 0xfffc) == 0)
2459 return;
2460 if (load_segment(&e1, &e2, selector) != 0)
2461 return;
2462 if (!(e2 & DESC_S_MASK))
2463 return;
2464 rpl = selector & 3;
2465 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2466 cpl = env->hflags & HF_CPL_MASK;
2467 if (e2 & DESC_CS_MASK) {
2468 return;
2469 } else {
2470 if (dpl < cpl || dpl < rpl)
2471 return;
2472 if (!(e2 & DESC_W_MASK))
2473 return;
2474 }
f3f2d9be 2475 CC_SRC |= CC_Z;
3ab493de
FB
2476}
2477
2c0262af
FB
2478/* FPU helpers */
2479
2c0262af
FB
2480void helper_fldt_ST0_A0(void)
2481{
2482 int new_fpstt;
2483 new_fpstt = (env->fpstt - 1) & 7;
664e0f19 2484 env->fpregs[new_fpstt].d = helper_fldt(A0);
2c0262af
FB
2485 env->fpstt = new_fpstt;
2486 env->fptags[new_fpstt] = 0; /* validate stack entry */
2487}
2488
2489void helper_fstt_ST0_A0(void)
2490{
14ce26e7 2491 helper_fstt(ST0, A0);
2c0262af 2492}
2c0262af 2493
2ee73ac3
FB
2494void fpu_set_exception(int mask)
2495{
2496 env->fpus |= mask;
2497 if (env->fpus & (~env->fpuc & FPUC_EM))
2498 env->fpus |= FPUS_SE | FPUS_B;
2499}
2500
2501CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b)
2502{
2503 if (b == 0.0)
2504 fpu_set_exception(FPUS_ZE);
2505 return a / b;
2506}
2507
2508void fpu_raise_exception(void)
2509{
2510 if (env->cr[0] & CR0_NE_MASK) {
2511 raise_exception(EXCP10_COPR);
2512 }
2513#if !defined(CONFIG_USER_ONLY)
2514 else {
2515 cpu_set_ferr(env);
2516 }
2517#endif
2518}
2519
2c0262af
FB
2520/* BCD ops */
2521
2c0262af
FB
2522void helper_fbld_ST0_A0(void)
2523{
2524 CPU86_LDouble tmp;
2525 uint64_t val;
2526 unsigned int v;
2527 int i;
2528
2529 val = 0;
2530 for(i = 8; i >= 0; i--) {
14ce26e7 2531 v = ldub(A0 + i);
2c0262af
FB
2532 val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
2533 }
2534 tmp = val;
14ce26e7 2535 if (ldub(A0 + 9) & 0x80)
2c0262af
FB
2536 tmp = -tmp;
2537 fpush();
2538 ST0 = tmp;
2539}
2540
2541void helper_fbst_ST0_A0(void)
2542{
2543 CPU86_LDouble tmp;
2544 int v;
14ce26e7 2545 target_ulong mem_ref, mem_end;
2c0262af
FB
2546 int64_t val;
2547
2548 tmp = rint(ST0);
2549 val = (int64_t)tmp;
14ce26e7 2550 mem_ref = A0;
2c0262af
FB
2551 mem_end = mem_ref + 9;
2552 if (val < 0) {
2553 stb(mem_end, 0x80);
2554 val = -val;
2555 } else {
2556 stb(mem_end, 0x00);
2557 }
2558 while (mem_ref < mem_end) {
2559 if (val == 0)
2560 break;
2561 v = val % 100;
2562 val = val / 100;
2563 v = ((v / 10) << 4) | (v % 10);
2564 stb(mem_ref++, v);
2565 }
2566 while (mem_ref < mem_end) {
2567 stb(mem_ref++, 0);
2568 }
2569}
2570
2571void helper_f2xm1(void)
2572{
2573 ST0 = pow(2.0,ST0) - 1.0;
2574}
2575
2576void helper_fyl2x(void)
2577{
2578 CPU86_LDouble fptemp;
2579
2580 fptemp = ST0;
2581 if (fptemp>0.0){
2582 fptemp = log(fptemp)/log(2.0); /* log2(ST) */
2583 ST1 *= fptemp;
2584 fpop();
2585 } else {
2586 env->fpus &= (~0x4700);
2587 env->fpus |= 0x400;
2588 }
2589}
2590
2591void helper_fptan(void)
2592{
2593 CPU86_LDouble fptemp;
2594
2595 fptemp = ST0;
2596 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2597 env->fpus |= 0x400;
2598 } else {
2599 ST0 = tan(fptemp);
2600 fpush();
2601 ST0 = 1.0;
2602 env->fpus &= (~0x400); /* C2 <-- 0 */
2603 /* the above code is for |arg| < 2**52 only */
2604 }
2605}
2606
2607void helper_fpatan(void)
2608{
2609 CPU86_LDouble fptemp, fpsrcop;
2610
2611 fpsrcop = ST1;
2612 fptemp = ST0;
2613 ST1 = atan2(fpsrcop,fptemp);
2614 fpop();
2615}
2616
2617void helper_fxtract(void)
2618{
2619 CPU86_LDoubleU temp;
2620 unsigned int expdif;
2621
2622 temp.d = ST0;
2623 expdif = EXPD(temp) - EXPBIAS;
2624 /*DP exponent bias*/
2625 ST0 = expdif;
2626 fpush();
2627 BIASEXPONENT(temp);
2628 ST0 = temp.d;
2629}
2630
2631void helper_fprem1(void)
2632{
2633 CPU86_LDouble dblq, fpsrcop, fptemp;
2634 CPU86_LDoubleU fpsrcop1, fptemp1;
2635 int expdif;
2636 int q;
2637
2638 fpsrcop = ST0;
2639 fptemp = ST1;
2640 fpsrcop1.d = fpsrcop;
2641 fptemp1.d = fptemp;
2642 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2643 if (expdif < 53) {
2644 dblq = fpsrcop / fptemp;
2645 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2646 ST0 = fpsrcop - fptemp*dblq;
2647 q = (int)dblq; /* cutting off top bits is assumed here */
2648 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2649 /* (C0,C1,C3) <-- (q2,q1,q0) */
2650 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2651 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2652 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2653 } else {
2654 env->fpus |= 0x400; /* C2 <-- 1 */
2655 fptemp = pow(2.0, expdif-50);
2656 fpsrcop = (ST0 / ST1) / fptemp;
2657 /* fpsrcop = integer obtained by rounding to the nearest */
2658 fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2659 floor(fpsrcop): ceil(fpsrcop);
2660 ST0 -= (ST1 * fpsrcop * fptemp);
2661 }
2662}
2663
2664void helper_fprem(void)
2665{
2666 CPU86_LDouble dblq, fpsrcop, fptemp;
2667 CPU86_LDoubleU fpsrcop1, fptemp1;
2668 int expdif;
2669 int q;
2670
2671 fpsrcop = ST0;
2672 fptemp = ST1;
2673 fpsrcop1.d = fpsrcop;
2674 fptemp1.d = fptemp;
2675 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2676 if ( expdif < 53 ) {
2677 dblq = fpsrcop / fptemp;
2678 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2679 ST0 = fpsrcop - fptemp*dblq;
2680 q = (int)dblq; /* cutting off top bits is assumed here */
2681 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2682 /* (C0,C1,C3) <-- (q2,q1,q0) */
2683 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2684 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2685 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2686 } else {
2687 env->fpus |= 0x400; /* C2 <-- 1 */
2688 fptemp = pow(2.0, expdif-50);
2689 fpsrcop = (ST0 / ST1) / fptemp;
2690 /* fpsrcop = integer obtained by chopping */
2691 fpsrcop = (fpsrcop < 0.0)?
2692 -(floor(fabs(fpsrcop))): floor(fpsrcop);
2693 ST0 -= (ST1 * fpsrcop * fptemp);
2694 }
2695}
2696
2697void helper_fyl2xp1(void)
2698{
2699 CPU86_LDouble fptemp;
2700
2701 fptemp = ST0;
2702 if ((fptemp+1.0)>0.0) {
2703 fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2704 ST1 *= fptemp;
2705 fpop();
2706 } else {
2707 env->fpus &= (~0x4700);
2708 env->fpus |= 0x400;
2709 }
2710}
2711
2712void helper_fsqrt(void)
2713{
2714 CPU86_LDouble fptemp;
2715
2716 fptemp = ST0;
2717 if (fptemp<0.0) {
2718 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2719 env->fpus |= 0x400;
2720 }
2721 ST0 = sqrt(fptemp);
2722}
2723
2724void helper_fsincos(void)
2725{
2726 CPU86_LDouble fptemp;
2727
2728 fptemp = ST0;
2729 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2730 env->fpus |= 0x400;
2731 } else {
2732 ST0 = sin(fptemp);
2733 fpush();
2734 ST0 = cos(fptemp);
2735 env->fpus &= (~0x400); /* C2 <-- 0 */
2736 /* the above code is for |arg| < 2**63 only */
2737 }
2738}
2739
2740void helper_frndint(void)
2741{
2742 CPU86_LDouble a;
2743
2744 a = ST0;
2745#ifdef __arm__
2746 switch(env->fpuc & RC_MASK) {
2747 default:
2748 case RC_NEAR:
2749 asm("rndd %0, %1" : "=f" (a) : "f"(a));
2750 break;
2751 case RC_DOWN:
2752 asm("rnddm %0, %1" : "=f" (a) : "f"(a));
2753 break;
2754 case RC_UP:
2755 asm("rnddp %0, %1" : "=f" (a) : "f"(a));
2756 break;
2757 case RC_CHOP:
2758 asm("rnddz %0, %1" : "=f" (a) : "f"(a));
2759 break;
2760 }
2761#else
2762 a = rint(a);
2763#endif
2764 ST0 = a;
2765}
2766
2767void helper_fscale(void)
2768{
2769 CPU86_LDouble fpsrcop, fptemp;
2770
2771 fpsrcop = 2.0;
2772 fptemp = pow(fpsrcop,ST1);
2773 ST0 *= fptemp;
2774}
2775
2776void helper_fsin(void)
2777{
2778 CPU86_LDouble fptemp;
2779
2780 fptemp = ST0;
2781 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2782 env->fpus |= 0x400;
2783 } else {
2784 ST0 = sin(fptemp);
2785 env->fpus &= (~0x400); /* C2 <-- 0 */
2786 /* the above code is for |arg| < 2**53 only */
2787 }
2788}
2789
2790void helper_fcos(void)
2791{
2792 CPU86_LDouble fptemp;
2793
2794 fptemp = ST0;
2795 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2796 env->fpus |= 0x400;
2797 } else {
2798 ST0 = cos(fptemp);
2799 env->fpus &= (~0x400); /* C2 <-- 0 */
2800 /* the above code is for |arg5 < 2**63 only */
2801 }
2802}
2803
2804void helper_fxam_ST0(void)
2805{
2806 CPU86_LDoubleU temp;
2807 int expdif;
2808
2809 temp.d = ST0;
2810
2811 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2812 if (SIGND(temp))
2813 env->fpus |= 0x200; /* C1 <-- 1 */
2814
2815 expdif = EXPD(temp);
2816 if (expdif == MAXEXPD) {
2817 if (MANTD(temp) == 0)
2818 env->fpus |= 0x500 /*Infinity*/;
2819 else
2820 env->fpus |= 0x100 /*NaN*/;
2821 } else if (expdif == 0) {
2822 if (MANTD(temp) == 0)
2823 env->fpus |= 0x4000 /*Zero*/;
2824 else
2825 env->fpus |= 0x4400 /*Denormal*/;
2826 } else {
2827 env->fpus |= 0x400;
2828 }
2829}
2830
14ce26e7 2831void helper_fstenv(target_ulong ptr, int data32)
2c0262af
FB
2832{
2833 int fpus, fptag, exp, i;
2834 uint64_t mant;
2835 CPU86_LDoubleU tmp;
2836
2837 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2838 fptag = 0;
2839 for (i=7; i>=0; i--) {
2840 fptag <<= 2;
2841 if (env->fptags[i]) {
2842 fptag |= 3;
2843 } else {
664e0f19 2844 tmp.d = env->fpregs[i].d;
2c0262af
FB
2845 exp = EXPD(tmp);
2846 mant = MANTD(tmp);
2847 if (exp == 0 && mant == 0) {
2848 /* zero */
2849 fptag |= 1;
2850 } else if (exp == 0 || exp == MAXEXPD
2851#ifdef USE_X86LDOUBLE
2852 || (mant & (1LL << 63)) == 0
2853#endif
2854 ) {
2855 /* NaNs, infinity, denormal */
2856 fptag |= 2;
2857 }
2858 }
2859 }
2860 if (data32) {
2861 /* 32 bit */
2862 stl(ptr, env->fpuc);
2863 stl(ptr + 4, fpus);
2864 stl(ptr + 8, fptag);
2edcdce3
FB
2865 stl(ptr + 12, 0); /* fpip */
2866 stl(ptr + 16, 0); /* fpcs */
2867 stl(ptr + 20, 0); /* fpoo */
2868 stl(ptr + 24, 0); /* fpos */
2c0262af
FB
2869 } else {
2870 /* 16 bit */
2871 stw(ptr, env->fpuc);
2872 stw(ptr + 2, fpus);
2873 stw(ptr + 4, fptag);
2874 stw(ptr + 6, 0);
2875 stw(ptr + 8, 0);
2876 stw(ptr + 10, 0);
2877 stw(ptr + 12, 0);
2878 }
2879}
2880
14ce26e7 2881void helper_fldenv(target_ulong ptr, int data32)
2c0262af
FB
2882{
2883 int i, fpus, fptag;
2884
2885 if (data32) {
2886 env->fpuc = lduw(ptr);
2887 fpus = lduw(ptr + 4);
2888 fptag = lduw(ptr + 8);
2889 }
2890 else {
2891 env->fpuc = lduw(ptr);
2892 fpus = lduw(ptr + 2);
2893 fptag = lduw(ptr + 4);
2894 }
2895 env->fpstt = (fpus >> 11) & 7;
2896 env->fpus = fpus & ~0x3800;
2edcdce3 2897 for(i = 0;i < 8; i++) {
2c0262af
FB
2898 env->fptags[i] = ((fptag & 3) == 3);
2899 fptag >>= 2;
2900 }
2901}
2902
14ce26e7 2903void helper_fsave(target_ulong ptr, int data32)
2c0262af
FB
2904{
2905 CPU86_LDouble tmp;
2906 int i;
2907
2908 helper_fstenv(ptr, data32);
2909
2910 ptr += (14 << data32);
2911 for(i = 0;i < 8; i++) {
2912 tmp = ST(i);
2c0262af 2913 helper_fstt(tmp, ptr);
2c0262af
FB
2914 ptr += 10;
2915 }
2916
2917 /* fninit */
2918 env->fpus = 0;
2919 env->fpstt = 0;
2920 env->fpuc = 0x37f;
2921 env->fptags[0] = 1;
2922 env->fptags[1] = 1;
2923 env->fptags[2] = 1;
2924 env->fptags[3] = 1;
2925 env->fptags[4] = 1;
2926 env->fptags[5] = 1;
2927 env->fptags[6] = 1;
2928 env->fptags[7] = 1;
2929}
2930
14ce26e7 2931void helper_frstor(target_ulong ptr, int data32)
2c0262af
FB
2932{
2933 CPU86_LDouble tmp;
2934 int i;
2935
2936 helper_fldenv(ptr, data32);
2937 ptr += (14 << data32);
2938
2939 for(i = 0;i < 8; i++) {
2c0262af 2940 tmp = helper_fldt(ptr);
2c0262af
FB
2941 ST(i) = tmp;
2942 ptr += 10;
2943 }
2944}
2945
14ce26e7
FB
2946void helper_fxsave(target_ulong ptr, int data64)
2947{
2948 int fpus, fptag, i, nb_xmm_regs;
2949 CPU86_LDouble tmp;
2950 target_ulong addr;
2951
2952 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2953 fptag = 0;
2954 for(i = 0; i < 8; i++) {
d3c61721 2955 fptag |= (env->fptags[i] << i);
14ce26e7
FB
2956 }
2957 stw(ptr, env->fpuc);
2958 stw(ptr + 2, fpus);
d3c61721 2959 stw(ptr + 4, fptag ^ 0xff);
14ce26e7
FB
2960
2961 addr = ptr + 0x20;
2962 for(i = 0;i < 8; i++) {
2963 tmp = ST(i);
2964 helper_fstt(tmp, addr);
2965 addr += 16;
2966 }
2967
2968 if (env->cr[4] & CR4_OSFXSR_MASK) {
a8ede8ba 2969 /* XXX: finish it */
664e0f19 2970 stl(ptr + 0x18, env->mxcsr); /* mxcsr */
d3c61721 2971 stl(ptr + 0x1c, 0x0000ffff); /* mxcsr_mask */
14ce26e7
FB
2972 nb_xmm_regs = 8 << data64;
2973 addr = ptr + 0xa0;
2974 for(i = 0; i < nb_xmm_regs; i++) {
a8ede8ba
FB
2975 stq(addr, env->xmm_regs[i].XMM_Q(0));
2976 stq(addr + 8, env->xmm_regs[i].XMM_Q(1));
14ce26e7
FB
2977 addr += 16;
2978 }
2979 }
2980}
2981
2982void helper_fxrstor(target_ulong ptr, int data64)
2983{
2984 int i, fpus, fptag, nb_xmm_regs;
2985 CPU86_LDouble tmp;
2986 target_ulong addr;
2987
2988 env->fpuc = lduw(ptr);
2989 fpus = lduw(ptr + 2);
d3c61721 2990 fptag = lduw(ptr + 4);
14ce26e7
FB
2991 env->fpstt = (fpus >> 11) & 7;
2992 env->fpus = fpus & ~0x3800;
2993 fptag ^= 0xff;
2994 for(i = 0;i < 8; i++) {
d3c61721 2995 env->fptags[i] = ((fptag >> i) & 1);
14ce26e7
FB
2996 }
2997
2998 addr = ptr + 0x20;
2999 for(i = 0;i < 8; i++) {
3000 tmp = helper_fldt(addr);
3001 ST(i) = tmp;
3002 addr += 16;
3003 }
3004
3005 if (env->cr[4] & CR4_OSFXSR_MASK) {
3006 /* XXX: finish it, endianness */
664e0f19 3007 env->mxcsr = ldl(ptr + 0x18);
14ce26e7
FB
3008 //ldl(ptr + 0x1c);
3009 nb_xmm_regs = 8 << data64;
3010 addr = ptr + 0xa0;
3011 for(i = 0; i < nb_xmm_regs; i++) {
a8ede8ba
FB
3012 env->xmm_regs[i].XMM_Q(0) = ldq(addr);
3013 env->xmm_regs[i].XMM_Q(1) = ldq(addr + 8);
14ce26e7
FB
3014 addr += 16;
3015 }
3016 }
3017}
1f1af9fd
FB
3018
3019#ifndef USE_X86LDOUBLE
3020
3021void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
3022{
3023 CPU86_LDoubleU temp;
3024 int e;
3025
3026 temp.d = f;
3027 /* mantissa */
3028 *pmant = (MANTD(temp) << 11) | (1LL << 63);
3029 /* exponent + sign */
3030 e = EXPD(temp) - EXPBIAS + 16383;
3031 e |= SIGND(temp) >> 16;
3032 *pexp = e;
3033}
3034
3035CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
3036{
3037 CPU86_LDoubleU temp;
3038 int e;
3039 uint64_t ll;
3040
3041 /* XXX: handle overflow ? */
3042 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
3043 e |= (upper >> 4) & 0x800; /* sign */
3044 ll = (mant >> 11) & ((1LL << 52) - 1);
3045#ifdef __arm__
3046 temp.l.upper = (e << 20) | (ll >> 32);
3047 temp.l.lower = ll;
3048#else
3049 temp.ll = ll | ((uint64_t)e << 52);
3050#endif
3051 return temp.d;
3052}
3053
3054#else
3055
3056void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
3057{
3058 CPU86_LDoubleU temp;
3059
3060 temp.d = f;
3061 *pmant = temp.l.lower;
3062 *pexp = temp.l.upper;
3063}
3064
3065CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
3066{
3067 CPU86_LDoubleU temp;
3068
3069 temp.l.upper = upper;
3070 temp.l.lower = mant;
3071 return temp.d;
3072}
3073#endif
3074
14ce26e7
FB
3075#ifdef TARGET_X86_64
3076
3077//#define DEBUG_MULDIV
3078
3079static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
3080{
3081 *plow += a;
3082 /* carry test */
3083 if (*plow < a)
3084 (*phigh)++;
3085 *phigh += b;
3086}
3087
3088static void neg128(uint64_t *plow, uint64_t *phigh)
3089{
3090 *plow = ~ *plow;
3091 *phigh = ~ *phigh;
3092 add128(plow, phigh, 1, 0);
3093}
3094
3095static void mul64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
3096{
3097 uint32_t a0, a1, b0, b1;
3098 uint64_t v;
3099
3100 a0 = a;
3101 a1 = a >> 32;
3102
3103 b0 = b;
3104 b1 = b >> 32;
3105
3106 v = (uint64_t)a0 * (uint64_t)b0;
3107 *plow = v;
3108 *phigh = 0;
3109
3110 v = (uint64_t)a0 * (uint64_t)b1;
3111 add128(plow, phigh, v << 32, v >> 32);
3112
3113 v = (uint64_t)a1 * (uint64_t)b0;
3114 add128(plow, phigh, v << 32, v >> 32);
3115
3116 v = (uint64_t)a1 * (uint64_t)b1;
3117 *phigh += v;
3118#ifdef DEBUG_MULDIV
3119 printf("mul: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
3120 a, b, *phigh, *plow);
3121#endif
3122}
3123
3124static void imul64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
3125{
3126 int sa, sb;
3127 sa = (a < 0);
3128 if (sa)
3129 a = -a;
3130 sb = (b < 0);
3131 if (sb)
3132 b = -b;
3133 mul64(plow, phigh, a, b);
3134 if (sa ^ sb) {
3135 neg128(plow, phigh);
3136 }
3137}
3138
a8ede8ba 3139/* XXX: overflow support */
14ce26e7
FB
3140static void div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
3141{
3142 uint64_t q, r, a1, a0;
3143 int i, qb;
3144
3145 a0 = *plow;
3146 a1 = *phigh;
3147 if (a1 == 0) {
3148 q = a0 / b;
3149 r = a0 % b;
3150 *plow = q;
3151 *phigh = r;
3152 } else {
3153 /* XXX: use a better algorithm */
3154 for(i = 0; i < 64; i++) {
a8ede8ba 3155 a1 = (a1 << 1) | (a0 >> 63);
14ce26e7
FB
3156 if (a1 >= b) {
3157 a1 -= b;
3158 qb = 1;
3159 } else {
3160 qb = 0;
3161 }
14ce26e7
FB
3162 a0 = (a0 << 1) | qb;
3163 }
a8ede8ba 3164#if defined(DEBUG_MULDIV)
14ce26e7
FB
3165 printf("div: 0x%016llx%016llx / 0x%016llx: q=0x%016llx r=0x%016llx\n",
3166 *phigh, *plow, b, a0, a1);
3167#endif
3168 *plow = a0;
3169 *phigh = a1;
3170 }
3171}
3172
3173static void idiv64(uint64_t *plow, uint64_t *phigh, uint64_t b)
3174{
3175 int sa, sb;
3176 sa = ((int64_t)*phigh < 0);
3177 if (sa)
3178 neg128(plow, phigh);
3179 sb = (b < 0);
3180 if (sb)
3181 b = -b;
3182 div64(plow, phigh, b);
3183 if (sa ^ sb)
3184 *plow = - *plow;
3185 if (sb)
3186 *phigh = - *phigh;
3187}
3188
3189void helper_mulq_EAX_T0(void)
3190{
3191 uint64_t r0, r1;
3192
3193 mul64(&r0, &r1, EAX, T0);
3194 EAX = r0;
3195 EDX = r1;
3196 CC_DST = r0;
3197 CC_SRC = r1;
3198}
3199
3200void helper_imulq_EAX_T0(void)
3201{
3202 uint64_t r0, r1;
3203
3204 imul64(&r0, &r1, EAX, T0);
3205 EAX = r0;
3206 EDX = r1;
3207 CC_DST = r0;
a8ede8ba 3208 CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
14ce26e7
FB
3209}
3210
3211void helper_imulq_T0_T1(void)
3212{
3213 uint64_t r0, r1;
3214
3215 imul64(&r0, &r1, T0, T1);
3216 T0 = r0;
3217 CC_DST = r0;
3218 CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
3219}
3220
3221void helper_divq_EAX_T0(void)
3222{
3223 uint64_t r0, r1;
3224 if (T0 == 0) {
3225 raise_exception(EXCP00_DIVZ);
3226 }
3227 r0 = EAX;
3228 r1 = EDX;
3229 div64(&r0, &r1, T0);
3230 EAX = r0;
3231 EDX = r1;
3232}
3233
3234void helper_idivq_EAX_T0(void)
3235{
3236 uint64_t r0, r1;
3237 if (T0 == 0) {
3238 raise_exception(EXCP00_DIVZ);
3239 }
3240 r0 = EAX;
3241 r1 = EDX;
3242 idiv64(&r0, &r1, T0);
3243 EAX = r0;
3244 EDX = r1;
3245}
3246
3247#endif
3248
664e0f19
FB
3249/* XXX: do it */
3250int fpu_isnan(double a)
3251{
3252 return 0;
3253}
3254
3255float approx_rsqrt(float a)
3256{
3257 return 1.0 / sqrt(a);
3258}
3259
3260float approx_rcp(float a)
3261{
3262 return 1.0 / a;
3263}
3264
4d6b6c0a
FB
3265/* XXX: find a better solution */
3266double helper_sqrt(double a)
3267{
3268 return sqrt(a);
3269}
3270
3271/* XXX: move that to another file */
3272#if defined(__powerpc__)
3273/* better to call an helper on ppc */
3274float int32_to_float32(int32_t a)
3275{
3276 return (float)a;
3277}
3278
3279double int32_to_float64(int32_t a)
3280{
3281 return (double)a;
3282}
3283#endif
664e0f19 3284
61382a50
FB
3285#if !defined(CONFIG_USER_ONLY)
3286
3287#define MMUSUFFIX _mmu
3288#define GETPC() (__builtin_return_address(0))
3289
2c0262af
FB
3290#define SHIFT 0
3291#include "softmmu_template.h"
3292
3293#define SHIFT 1
3294#include "softmmu_template.h"
3295
3296#define SHIFT 2
3297#include "softmmu_template.h"
3298
3299#define SHIFT 3
3300#include "softmmu_template.h"
3301
61382a50
FB
3302#endif
3303
3304/* try to fill the TLB and return an exception if error. If retaddr is
3305 NULL, it means that the function was called in C code (i.e. not
3306 from generated code or from helper.c) */
3307/* XXX: fix it to restore all registers */
14ce26e7 3308void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
2c0262af
FB
3309{
3310 TranslationBlock *tb;
3311 int ret;
3312 unsigned long pc;
61382a50
FB
3313 CPUX86State *saved_env;
3314
3315 /* XXX: hack to restore env in all cases, even if not called from
3316 generated code */
3317 saved_env = env;
3318 env = cpu_single_env;
61382a50
FB
3319
3320 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, is_user, 1);
2c0262af 3321 if (ret) {
61382a50
FB
3322 if (retaddr) {
3323 /* now we have a real cpu fault */
3324 pc = (unsigned long)retaddr;
3325 tb = tb_find_pc(pc);
3326 if (tb) {
3327 /* the PC is inside the translated code. It means that we have
3328 a virtual CPU fault */
58fe2f10 3329 cpu_restore_state(tb, env, pc, NULL);
61382a50 3330 }
2c0262af 3331 }
0d1a29f9
FB
3332 if (retaddr)
3333 raise_exception_err(EXCP0E_PAGE, env->error_code);
3334 else
3335 raise_exception_err_norestore(EXCP0E_PAGE, env->error_code);
2c0262af 3336 }
61382a50 3337 env = saved_env;
2c0262af 3338}
This page took 0.462411 seconds and 4 git commands to generate.