]> git.proxmox.com Git - mirror_qemu.git/blob - target-i386/helper.c
suppressed no longer needed vm86 segment hack (Mike Nordell)
[mirror_qemu.git] / target-i386 / helper.c
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
22 //#define DEBUG_PCALL
23
24 #if 0
25 #define raise_exception_err(a, b)\
26 do {\
27 printf("raise_exception line=%d\n", __LINE__);\
28 (raise_exception_err)(a, b);\
29 } while (0)
30 #endif
31
32 const 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 */
68 const 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 */
76 const 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
83 const 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
96 spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
97
98 void cpu_lock(void)
99 {
100 spin_lock(&global_cpu_lock);
101 }
102
103 void cpu_unlock(void)
104 {
105 spin_unlock(&global_cpu_lock);
106 }
107
108 void cpu_loop_exit(void)
109 {
110 /* NOTE: the register at this point must be saved by hand because
111 longjmp restore them */
112 #ifdef reg_EAX
113 env->regs[R_EAX] = EAX;
114 #endif
115 #ifdef reg_ECX
116 env->regs[R_ECX] = ECX;
117 #endif
118 #ifdef reg_EDX
119 env->regs[R_EDX] = EDX;
120 #endif
121 #ifdef reg_EBX
122 env->regs[R_EBX] = EBX;
123 #endif
124 #ifdef reg_ESP
125 env->regs[R_ESP] = ESP;
126 #endif
127 #ifdef reg_EBP
128 env->regs[R_EBP] = EBP;
129 #endif
130 #ifdef reg_ESI
131 env->regs[R_ESI] = ESI;
132 #endif
133 #ifdef reg_EDI
134 env->regs[R_EDI] = EDI;
135 #endif
136 longjmp(env->jmp_env, 1);
137 }
138
139 /* return non zero if error */
140 static inline int load_segment(uint32_t *e1_ptr, uint32_t *e2_ptr,
141 int selector)
142 {
143 SegmentCache *dt;
144 int index;
145 uint8_t *ptr;
146
147 if (selector & 0x4)
148 dt = &env->ldt;
149 else
150 dt = &env->gdt;
151 index = selector & ~7;
152 if ((index + 7) > dt->limit)
153 return -1;
154 ptr = dt->base + index;
155 *e1_ptr = ldl_kernel(ptr);
156 *e2_ptr = ldl_kernel(ptr + 4);
157 return 0;
158 }
159
160 static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
161 {
162 unsigned int limit;
163 limit = (e1 & 0xffff) | (e2 & 0x000f0000);
164 if (e2 & DESC_G_MASK)
165 limit = (limit << 12) | 0xfff;
166 return limit;
167 }
168
169 static inline uint8_t *get_seg_base(uint32_t e1, uint32_t e2)
170 {
171 return (uint8_t *)((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
172 }
173
174 static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, uint32_t e2)
175 {
176 sc->base = get_seg_base(e1, e2);
177 sc->limit = get_seg_limit(e1, e2);
178 sc->flags = e2;
179 }
180
181 /* init the segment cache in vm86 mode. */
182 static inline void load_seg_vm(int seg, int selector)
183 {
184 selector &= 0xffff;
185 cpu_x86_load_seg_cache(env, seg, selector,
186 (uint8_t *)(selector << 4), 0xffff, 0);
187 }
188
189 static inline void get_ss_esp_from_tss(uint32_t *ss_ptr,
190 uint32_t *esp_ptr, int dpl)
191 {
192 int type, index, shift;
193
194 #if 0
195 {
196 int i;
197 printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
198 for(i=0;i<env->tr.limit;i++) {
199 printf("%02x ", env->tr.base[i]);
200 if ((i & 7) == 7) printf("\n");
201 }
202 printf("\n");
203 }
204 #endif
205
206 if (!(env->tr.flags & DESC_P_MASK))
207 cpu_abort(env, "invalid tss");
208 type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
209 if ((type & 7) != 1)
210 cpu_abort(env, "invalid tss type");
211 shift = type >> 3;
212 index = (dpl * 4 + 2) << shift;
213 if (index + (4 << shift) - 1 > env->tr.limit)
214 raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
215 if (shift == 0) {
216 *esp_ptr = lduw_kernel(env->tr.base + index);
217 *ss_ptr = lduw_kernel(env->tr.base + index + 2);
218 } else {
219 *esp_ptr = ldl_kernel(env->tr.base + index);
220 *ss_ptr = lduw_kernel(env->tr.base + index + 4);
221 }
222 }
223
224 /* XXX: merge with load_seg() */
225 static void tss_load_seg(int seg_reg, int selector)
226 {
227 uint32_t e1, e2;
228 int rpl, dpl, cpl;
229
230 if ((selector & 0xfffc) != 0) {
231 if (load_segment(&e1, &e2, selector) != 0)
232 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
233 if (!(e2 & DESC_S_MASK))
234 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
235 rpl = selector & 3;
236 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
237 cpl = env->hflags & HF_CPL_MASK;
238 if (seg_reg == R_CS) {
239 if (!(e2 & DESC_CS_MASK))
240 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
241 if (dpl != rpl)
242 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
243 if ((e2 & DESC_C_MASK) && dpl > rpl)
244 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
245
246 } else if (seg_reg == R_SS) {
247 /* SS must be writable data */
248 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
249 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
250 if (dpl != cpl || dpl != rpl)
251 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
252 } else {
253 /* not readable code */
254 if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK))
255 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
256 /* if data or non conforming code, checks the rights */
257 if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
258 if (dpl < cpl || dpl < rpl)
259 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
260 }
261 }
262 if (!(e2 & DESC_P_MASK))
263 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
264 cpu_x86_load_seg_cache(env, seg_reg, selector,
265 get_seg_base(e1, e2),
266 get_seg_limit(e1, e2),
267 e2);
268 } else {
269 if (seg_reg == R_SS || seg_reg == R_CS)
270 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
271 }
272 }
273
274 #define SWITCH_TSS_JMP 0
275 #define SWITCH_TSS_IRET 1
276 #define SWITCH_TSS_CALL 2
277
278 /* XXX: restore CPU state in registers (PowerPC case) */
279 static void switch_tss(int tss_selector,
280 uint32_t e1, uint32_t e2, int source,
281 uint32_t next_eip)
282 {
283 int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
284 uint8_t *tss_base;
285 uint32_t new_regs[8], new_segs[6];
286 uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
287 uint32_t old_eflags, eflags_mask;
288 SegmentCache *dt;
289 int index;
290 uint8_t *ptr;
291
292 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
293 #ifdef DEBUG_PCALL
294 if (loglevel & CPU_LOG_PCALL)
295 fprintf(logfile, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, source);
296 #endif
297
298 /* if task gate, we read the TSS segment and we load it */
299 if (type == 5) {
300 if (!(e2 & DESC_P_MASK))
301 raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
302 tss_selector = e1 >> 16;
303 if (tss_selector & 4)
304 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
305 if (load_segment(&e1, &e2, tss_selector) != 0)
306 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
307 if (e2 & DESC_S_MASK)
308 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
309 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
310 if ((type & 7) != 1)
311 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
312 }
313
314 if (!(e2 & DESC_P_MASK))
315 raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
316
317 if (type & 8)
318 tss_limit_max = 103;
319 else
320 tss_limit_max = 43;
321 tss_limit = get_seg_limit(e1, e2);
322 tss_base = get_seg_base(e1, e2);
323 if ((tss_selector & 4) != 0 ||
324 tss_limit < tss_limit_max)
325 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
326 old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
327 if (old_type & 8)
328 old_tss_limit_max = 103;
329 else
330 old_tss_limit_max = 43;
331
332 /* read all the registers from the new TSS */
333 if (type & 8) {
334 /* 32 bit */
335 new_cr3 = ldl_kernel(tss_base + 0x1c);
336 new_eip = ldl_kernel(tss_base + 0x20);
337 new_eflags = ldl_kernel(tss_base + 0x24);
338 for(i = 0; i < 8; i++)
339 new_regs[i] = ldl_kernel(tss_base + (0x28 + i * 4));
340 for(i = 0; i < 6; i++)
341 new_segs[i] = lduw_kernel(tss_base + (0x48 + i * 4));
342 new_ldt = lduw_kernel(tss_base + 0x60);
343 new_trap = ldl_kernel(tss_base + 0x64);
344 } else {
345 /* 16 bit */
346 new_cr3 = 0;
347 new_eip = lduw_kernel(tss_base + 0x0e);
348 new_eflags = lduw_kernel(tss_base + 0x10);
349 for(i = 0; i < 8; i++)
350 new_regs[i] = lduw_kernel(tss_base + (0x12 + i * 2)) | 0xffff0000;
351 for(i = 0; i < 4; i++)
352 new_segs[i] = lduw_kernel(tss_base + (0x22 + i * 4));
353 new_ldt = lduw_kernel(tss_base + 0x2a);
354 new_segs[R_FS] = 0;
355 new_segs[R_GS] = 0;
356 new_trap = 0;
357 }
358
359 /* NOTE: we must avoid memory exceptions during the task switch,
360 so we make dummy accesses before */
361 /* XXX: it can still fail in some cases, so a bigger hack is
362 necessary to valid the TLB after having done the accesses */
363
364 v1 = ldub_kernel(env->tr.base);
365 v2 = ldub(env->tr.base + old_tss_limit_max);
366 stb_kernel(env->tr.base, v1);
367 stb_kernel(env->tr.base + old_tss_limit_max, v2);
368
369 /* clear busy bit (it is restartable) */
370 if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
371 uint8_t *ptr;
372 uint32_t e2;
373 ptr = env->gdt.base + (env->tr.selector & ~7);
374 e2 = ldl_kernel(ptr + 4);
375 e2 &= ~DESC_TSS_BUSY_MASK;
376 stl_kernel(ptr + 4, e2);
377 }
378 old_eflags = compute_eflags();
379 if (source == SWITCH_TSS_IRET)
380 old_eflags &= ~NT_MASK;
381
382 /* save the current state in the old TSS */
383 if (type & 8) {
384 /* 32 bit */
385 stl_kernel(env->tr.base + 0x20, next_eip);
386 stl_kernel(env->tr.base + 0x24, old_eflags);
387 for(i = 0; i < 8; i++)
388 stl_kernel(env->tr.base + (0x28 + i * 4), env->regs[i]);
389 for(i = 0; i < 6; i++)
390 stw_kernel(env->tr.base + (0x48 + i * 4), env->segs[i].selector);
391 } else {
392 /* 16 bit */
393 stw_kernel(env->tr.base + 0x0e, next_eip);
394 stw_kernel(env->tr.base + 0x10, old_eflags);
395 for(i = 0; i < 8; i++)
396 stw_kernel(env->tr.base + (0x12 + i * 2), env->regs[i]);
397 for(i = 0; i < 4; i++)
398 stw_kernel(env->tr.base + (0x22 + i * 4), env->segs[i].selector);
399 }
400
401 /* now if an exception occurs, it will occurs in the next task
402 context */
403
404 if (source == SWITCH_TSS_CALL) {
405 stw_kernel(tss_base, env->tr.selector);
406 new_eflags |= NT_MASK;
407 }
408
409 /* set busy bit */
410 if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
411 uint8_t *ptr;
412 uint32_t e2;
413 ptr = env->gdt.base + (tss_selector & ~7);
414 e2 = ldl_kernel(ptr + 4);
415 e2 |= DESC_TSS_BUSY_MASK;
416 stl_kernel(ptr + 4, e2);
417 }
418
419 /* set the new CPU state */
420 /* from this point, any exception which occurs can give problems */
421 env->cr[0] |= CR0_TS_MASK;
422 env->hflags |= HF_TS_MASK;
423 env->tr.selector = tss_selector;
424 env->tr.base = tss_base;
425 env->tr.limit = tss_limit;
426 env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
427
428 if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
429 cpu_x86_update_cr3(env, new_cr3);
430 }
431
432 /* load all registers without an exception, then reload them with
433 possible exception */
434 env->eip = new_eip;
435 eflags_mask = TF_MASK | AC_MASK | ID_MASK |
436 IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
437 if (!(type & 8))
438 eflags_mask &= 0xffff;
439 load_eflags(new_eflags, eflags_mask);
440 for(i = 0; i < 8; i++)
441 env->regs[i] = new_regs[i];
442 if (new_eflags & VM_MASK) {
443 for(i = 0; i < 6; i++)
444 load_seg_vm(i, new_segs[i]);
445 /* in vm86, CPL is always 3 */
446 cpu_x86_set_cpl(env, 3);
447 } else {
448 /* CPL is set the RPL of CS */
449 cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
450 /* first just selectors as the rest may trigger exceptions */
451 for(i = 0; i < 6; i++)
452 cpu_x86_load_seg_cache(env, i, new_segs[i], NULL, 0, 0);
453 }
454
455 env->ldt.selector = new_ldt & ~4;
456 env->ldt.base = NULL;
457 env->ldt.limit = 0;
458 env->ldt.flags = 0;
459
460 /* load the LDT */
461 if (new_ldt & 4)
462 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
463
464 if ((new_ldt & 0xfffc) != 0) {
465 dt = &env->gdt;
466 index = new_ldt & ~7;
467 if ((index + 7) > dt->limit)
468 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
469 ptr = dt->base + index;
470 e1 = ldl_kernel(ptr);
471 e2 = ldl_kernel(ptr + 4);
472 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
473 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
474 if (!(e2 & DESC_P_MASK))
475 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
476 load_seg_cache_raw_dt(&env->ldt, e1, e2);
477 }
478
479 /* load the segments */
480 if (!(new_eflags & VM_MASK)) {
481 tss_load_seg(R_CS, new_segs[R_CS]);
482 tss_load_seg(R_SS, new_segs[R_SS]);
483 tss_load_seg(R_ES, new_segs[R_ES]);
484 tss_load_seg(R_DS, new_segs[R_DS]);
485 tss_load_seg(R_FS, new_segs[R_FS]);
486 tss_load_seg(R_GS, new_segs[R_GS]);
487 }
488
489 /* check that EIP is in the CS segment limits */
490 if (new_eip > env->segs[R_CS].limit) {
491 /* XXX: different exception if CALL ? */
492 raise_exception_err(EXCP0D_GPF, 0);
493 }
494 }
495
496 /* check if Port I/O is allowed in TSS */
497 static inline void check_io(int addr, int size)
498 {
499 int io_offset, val, mask;
500
501 /* TSS must be a valid 32 bit one */
502 if (!(env->tr.flags & DESC_P_MASK) ||
503 ((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) != 9 ||
504 env->tr.limit < 103)
505 goto fail;
506 io_offset = lduw_kernel(env->tr.base + 0x66);
507 io_offset += (addr >> 3);
508 /* Note: the check needs two bytes */
509 if ((io_offset + 1) > env->tr.limit)
510 goto fail;
511 val = lduw_kernel(env->tr.base + io_offset);
512 val >>= (addr & 7);
513 mask = (1 << size) - 1;
514 /* all bits must be zero to allow the I/O */
515 if ((val & mask) != 0) {
516 fail:
517 raise_exception_err(EXCP0D_GPF, 0);
518 }
519 }
520
521 void check_iob_T0(void)
522 {
523 check_io(T0, 1);
524 }
525
526 void check_iow_T0(void)
527 {
528 check_io(T0, 2);
529 }
530
531 void check_iol_T0(void)
532 {
533 check_io(T0, 4);
534 }
535
536 void check_iob_DX(void)
537 {
538 check_io(EDX & 0xffff, 1);
539 }
540
541 void check_iow_DX(void)
542 {
543 check_io(EDX & 0xffff, 2);
544 }
545
546 void check_iol_DX(void)
547 {
548 check_io(EDX & 0xffff, 4);
549 }
550
551 static inline unsigned int get_sp_mask(unsigned int e2)
552 {
553 if (e2 & DESC_B_MASK)
554 return 0xffffffff;
555 else
556 return 0xffff;
557 }
558
559 /* XXX: add a is_user flag to have proper security support */
560 #define PUSHW(ssp, sp, sp_mask, val)\
561 {\
562 sp -= 2;\
563 stw_kernel((ssp) + (sp & (sp_mask)), (val));\
564 }
565
566 #define PUSHL(ssp, sp, sp_mask, val)\
567 {\
568 sp -= 4;\
569 stl_kernel((ssp) + (sp & (sp_mask)), (val));\
570 }
571
572 #define POPW(ssp, sp, sp_mask, val)\
573 {\
574 val = lduw_kernel((ssp) + (sp & (sp_mask)));\
575 sp += 2;\
576 }
577
578 #define POPL(ssp, sp, sp_mask, val)\
579 {\
580 val = ldl_kernel((ssp) + (sp & (sp_mask)));\
581 sp += 4;\
582 }
583
584 /* protected mode interrupt */
585 static void do_interrupt_protected(int intno, int is_int, int error_code,
586 unsigned int next_eip, int is_hw)
587 {
588 SegmentCache *dt;
589 uint8_t *ptr, *ssp;
590 int type, dpl, selector, ss_dpl, cpl, sp_mask;
591 int has_error_code, new_stack, shift;
592 uint32_t e1, e2, offset, ss, esp, ss_e1, ss_e2;
593 uint32_t old_eip;
594
595 has_error_code = 0;
596 if (!is_int && !is_hw) {
597 switch(intno) {
598 case 8:
599 case 10:
600 case 11:
601 case 12:
602 case 13:
603 case 14:
604 case 17:
605 has_error_code = 1;
606 break;
607 }
608 }
609 if (is_int)
610 old_eip = next_eip;
611 else
612 old_eip = env->eip;
613
614 dt = &env->idt;
615 if (intno * 8 + 7 > dt->limit)
616 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
617 ptr = dt->base + intno * 8;
618 e1 = ldl_kernel(ptr);
619 e2 = ldl_kernel(ptr + 4);
620 /* check gate type */
621 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
622 switch(type) {
623 case 5: /* task gate */
624 /* must do that check here to return the correct error code */
625 if (!(e2 & DESC_P_MASK))
626 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
627 switch_tss(intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip);
628 if (has_error_code) {
629 int mask;
630 /* push the error code */
631 shift = (env->segs[R_CS].flags >> DESC_B_SHIFT) & 1;
632 if (env->segs[R_SS].flags & DESC_B_MASK)
633 mask = 0xffffffff;
634 else
635 mask = 0xffff;
636 esp = (env->regs[R_ESP] - (2 << shift)) & mask;
637 ssp = env->segs[R_SS].base + esp;
638 if (shift)
639 stl_kernel(ssp, error_code);
640 else
641 stw_kernel(ssp, error_code);
642 env->regs[R_ESP] = (esp & mask) | (env->regs[R_ESP] & ~mask);
643 }
644 return;
645 case 6: /* 286 interrupt gate */
646 case 7: /* 286 trap gate */
647 case 14: /* 386 interrupt gate */
648 case 15: /* 386 trap gate */
649 break;
650 default:
651 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
652 break;
653 }
654 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
655 cpl = env->hflags & HF_CPL_MASK;
656 /* check privledge if software int */
657 if (is_int && dpl < cpl)
658 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
659 /* check valid bit */
660 if (!(e2 & DESC_P_MASK))
661 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
662 selector = e1 >> 16;
663 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
664 if ((selector & 0xfffc) == 0)
665 raise_exception_err(EXCP0D_GPF, 0);
666
667 if (load_segment(&e1, &e2, selector) != 0)
668 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
669 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
670 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
671 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
672 if (dpl > cpl)
673 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
674 if (!(e2 & DESC_P_MASK))
675 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
676 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
677 /* to inner priviledge */
678 get_ss_esp_from_tss(&ss, &esp, dpl);
679 if ((ss & 0xfffc) == 0)
680 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
681 if ((ss & 3) != dpl)
682 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
683 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
684 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
685 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
686 if (ss_dpl != dpl)
687 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
688 if (!(ss_e2 & DESC_S_MASK) ||
689 (ss_e2 & DESC_CS_MASK) ||
690 !(ss_e2 & DESC_W_MASK))
691 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
692 if (!(ss_e2 & DESC_P_MASK))
693 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
694 new_stack = 1;
695 sp_mask = get_sp_mask(ss_e2);
696 ssp = get_seg_base(ss_e1, ss_e2);
697 } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
698 /* to same priviledge */
699 if (env->eflags & VM_MASK)
700 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
701 new_stack = 0;
702 sp_mask = get_sp_mask(env->segs[R_SS].flags);
703 ssp = env->segs[R_SS].base;
704 esp = ESP;
705 dpl = cpl;
706 } else {
707 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
708 new_stack = 0; /* avoid warning */
709 sp_mask = 0; /* avoid warning */
710 ssp = NULL; /* avoid warning */
711 esp = 0; /* avoid warning */
712 }
713
714 shift = type >> 3;
715
716 #if 0
717 /* XXX: check that enough room is available */
718 push_size = 6 + (new_stack << 2) + (has_error_code << 1);
719 if (env->eflags & VM_MASK)
720 push_size += 8;
721 push_size <<= shift;
722 #endif
723 if (shift == 1) {
724 if (new_stack) {
725 if (env->eflags & VM_MASK) {
726 PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
727 PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
728 PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
729 PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
730 }
731 PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
732 PUSHL(ssp, esp, sp_mask, ESP);
733 }
734 PUSHL(ssp, esp, sp_mask, compute_eflags());
735 PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
736 PUSHL(ssp, esp, sp_mask, old_eip);
737 if (has_error_code) {
738 PUSHL(ssp, esp, sp_mask, error_code);
739 }
740 } else {
741 if (new_stack) {
742 if (env->eflags & VM_MASK) {
743 PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
744 PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
745 PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
746 PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
747 }
748 PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
749 PUSHW(ssp, esp, sp_mask, ESP);
750 }
751 PUSHW(ssp, esp, sp_mask, compute_eflags());
752 PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
753 PUSHW(ssp, esp, sp_mask, old_eip);
754 if (has_error_code) {
755 PUSHW(ssp, esp, sp_mask, error_code);
756 }
757 }
758
759 if (new_stack) {
760 if (env->eflags & VM_MASK) {
761 cpu_x86_load_seg_cache(env, R_ES, 0, NULL, 0, 0);
762 cpu_x86_load_seg_cache(env, R_DS, 0, NULL, 0, 0);
763 cpu_x86_load_seg_cache(env, R_FS, 0, NULL, 0, 0);
764 cpu_x86_load_seg_cache(env, R_GS, 0, NULL, 0, 0);
765 }
766 ss = (ss & ~3) | dpl;
767 cpu_x86_load_seg_cache(env, R_SS, ss,
768 ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
769 }
770 ESP = (ESP & ~sp_mask) | (esp & sp_mask);
771
772 selector = (selector & ~3) | dpl;
773 cpu_x86_load_seg_cache(env, R_CS, selector,
774 get_seg_base(e1, e2),
775 get_seg_limit(e1, e2),
776 e2);
777 cpu_x86_set_cpl(env, dpl);
778 env->eip = offset;
779
780 /* interrupt gate clear IF mask */
781 if ((type & 1) == 0) {
782 env->eflags &= ~IF_MASK;
783 }
784 env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
785 }
786
787 /* real mode interrupt */
788 static void do_interrupt_real(int intno, int is_int, int error_code,
789 unsigned int next_eip)
790 {
791 SegmentCache *dt;
792 uint8_t *ptr, *ssp;
793 int selector;
794 uint32_t offset, esp;
795 uint32_t old_cs, old_eip;
796
797 /* real mode (simpler !) */
798 dt = &env->idt;
799 if (intno * 4 + 3 > dt->limit)
800 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
801 ptr = dt->base + intno * 4;
802 offset = lduw_kernel(ptr);
803 selector = lduw_kernel(ptr + 2);
804 esp = ESP;
805 ssp = env->segs[R_SS].base;
806 if (is_int)
807 old_eip = next_eip;
808 else
809 old_eip = env->eip;
810 old_cs = env->segs[R_CS].selector;
811 /* XXX: use SS segment size ? */
812 PUSHW(ssp, esp, 0xffff, compute_eflags());
813 PUSHW(ssp, esp, 0xffff, old_cs);
814 PUSHW(ssp, esp, 0xffff, old_eip);
815
816 /* update processor state */
817 ESP = (ESP & ~0xffff) | (esp & 0xffff);
818 env->eip = offset;
819 env->segs[R_CS].selector = selector;
820 env->segs[R_CS].base = (uint8_t *)(selector << 4);
821 env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
822 }
823
824 /* fake user mode interrupt */
825 void do_interrupt_user(int intno, int is_int, int error_code,
826 unsigned int next_eip)
827 {
828 SegmentCache *dt;
829 uint8_t *ptr;
830 int dpl, cpl;
831 uint32_t e2;
832
833 dt = &env->idt;
834 ptr = dt->base + (intno * 8);
835 e2 = ldl_kernel(ptr + 4);
836
837 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
838 cpl = env->hflags & HF_CPL_MASK;
839 /* check privledge if software int */
840 if (is_int && dpl < cpl)
841 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
842
843 /* Since we emulate only user space, we cannot do more than
844 exiting the emulation with the suitable exception and error
845 code */
846 if (is_int)
847 EIP = next_eip;
848 }
849
850 /*
851 * Begin execution of an interruption. is_int is TRUE if coming from
852 * the int instruction. next_eip is the EIP value AFTER the interrupt
853 * instruction. It is only relevant if is_int is TRUE.
854 */
855 void do_interrupt(int intno, int is_int, int error_code,
856 unsigned int next_eip, int is_hw)
857 {
858 #ifdef DEBUG_PCALL
859 if (loglevel & (CPU_LOG_PCALL | CPU_LOG_INT)) {
860 if ((env->cr[0] & CR0_PE_MASK)) {
861 static int count;
862 fprintf(logfile, "%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:%08x SP=%04x:%08x",
863 count, intno, error_code, is_int,
864 env->hflags & HF_CPL_MASK,
865 env->segs[R_CS].selector, EIP,
866 env->segs[R_SS].selector, ESP);
867 if (intno == 0x0e) {
868 fprintf(logfile, " CR2=%08x", env->cr[2]);
869 } else {
870 fprintf(logfile, " EAX=%08x", env->regs[R_EAX]);
871 }
872 fprintf(logfile, "\n");
873 #if 0
874 cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
875 {
876 int i;
877 uint8_t *ptr;
878 fprintf(logfile, " code=");
879 ptr = env->segs[R_CS].base + env->eip;
880 for(i = 0; i < 16; i++) {
881 fprintf(logfile, " %02x", ldub(ptr + i));
882 }
883 fprintf(logfile, "\n");
884 }
885 #endif
886 count++;
887 }
888 }
889 #endif
890 if (env->cr[0] & CR0_PE_MASK) {
891 do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
892 } else {
893 do_interrupt_real(intno, is_int, error_code, next_eip);
894 }
895 }
896
897 /*
898 * Signal an interruption. It is executed in the main CPU loop.
899 * is_int is TRUE if coming from the int instruction. next_eip is the
900 * EIP value AFTER the interrupt instruction. It is only relevant if
901 * is_int is TRUE.
902 */
903 void raise_interrupt(int intno, int is_int, int error_code,
904 unsigned int next_eip)
905 {
906 env->exception_index = intno;
907 env->error_code = error_code;
908 env->exception_is_int = is_int;
909 env->exception_next_eip = next_eip;
910 cpu_loop_exit();
911 }
912
913 /* shortcuts to generate exceptions */
914
915 void (raise_exception_err)(int exception_index, int error_code)
916 {
917 raise_interrupt(exception_index, 0, error_code, 0);
918 }
919
920 void raise_exception(int exception_index)
921 {
922 raise_interrupt(exception_index, 0, 0, 0);
923 }
924
925 #ifdef BUGGY_GCC_DIV64
926 /* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
927 call it from another function */
928 uint32_t div64(uint32_t *q_ptr, uint64_t num, uint32_t den)
929 {
930 *q_ptr = num / den;
931 return num % den;
932 }
933
934 int32_t idiv64(int32_t *q_ptr, int64_t num, int32_t den)
935 {
936 *q_ptr = num / den;
937 return num % den;
938 }
939 #endif
940
941 void helper_divl_EAX_T0(uint32_t eip)
942 {
943 unsigned int den, q, r;
944 uint64_t num;
945
946 num = EAX | ((uint64_t)EDX << 32);
947 den = T0;
948 if (den == 0) {
949 EIP = eip;
950 raise_exception(EXCP00_DIVZ);
951 }
952 #ifdef BUGGY_GCC_DIV64
953 r = div64(&q, num, den);
954 #else
955 q = (num / den);
956 r = (num % den);
957 #endif
958 EAX = q;
959 EDX = r;
960 }
961
962 void helper_idivl_EAX_T0(uint32_t eip)
963 {
964 int den, q, r;
965 int64_t num;
966
967 num = EAX | ((uint64_t)EDX << 32);
968 den = T0;
969 if (den == 0) {
970 EIP = eip;
971 raise_exception(EXCP00_DIVZ);
972 }
973 #ifdef BUGGY_GCC_DIV64
974 r = idiv64(&q, num, den);
975 #else
976 q = (num / den);
977 r = (num % den);
978 #endif
979 EAX = q;
980 EDX = r;
981 }
982
983 void helper_cmpxchg8b(void)
984 {
985 uint64_t d;
986 int eflags;
987
988 eflags = cc_table[CC_OP].compute_all();
989 d = ldq((uint8_t *)A0);
990 if (d == (((uint64_t)EDX << 32) | EAX)) {
991 stq((uint8_t *)A0, ((uint64_t)ECX << 32) | EBX);
992 eflags |= CC_Z;
993 } else {
994 EDX = d >> 32;
995 EAX = d;
996 eflags &= ~CC_Z;
997 }
998 CC_SRC = eflags;
999 }
1000
1001 #define CPUID_FP87 (1 << 0)
1002 #define CPUID_VME (1 << 1)
1003 #define CPUID_DE (1 << 2)
1004 #define CPUID_PSE (1 << 3)
1005 #define CPUID_TSC (1 << 4)
1006 #define CPUID_MSR (1 << 5)
1007 #define CPUID_PAE (1 << 6)
1008 #define CPUID_MCE (1 << 7)
1009 #define CPUID_CX8 (1 << 8)
1010 #define CPUID_APIC (1 << 9)
1011 #define CPUID_SEP (1 << 11) /* sysenter/sysexit */
1012 #define CPUID_MTRR (1 << 12)
1013 #define CPUID_PGE (1 << 13)
1014 #define CPUID_MCA (1 << 14)
1015 #define CPUID_CMOV (1 << 15)
1016 /* ... */
1017 #define CPUID_MMX (1 << 23)
1018 #define CPUID_FXSR (1 << 24)
1019 #define CPUID_SSE (1 << 25)
1020 #define CPUID_SSE2 (1 << 26)
1021
1022 void helper_cpuid(void)
1023 {
1024 switch(EAX) {
1025 case 0:
1026 EAX = 2; /* max EAX index supported */
1027 EBX = 0x756e6547;
1028 ECX = 0x6c65746e;
1029 EDX = 0x49656e69;
1030 break;
1031 case 1:
1032 {
1033 int family, model, stepping;
1034 /* EAX = 1 info */
1035 #if 0
1036 /* pentium 75-200 */
1037 family = 5;
1038 model = 2;
1039 stepping = 11;
1040 #else
1041 /* pentium pro */
1042 family = 6;
1043 model = 1;
1044 stepping = 3;
1045 #endif
1046 EAX = (family << 8) | (model << 4) | stepping;
1047 EBX = 0;
1048 ECX = 0;
1049 EDX = CPUID_FP87 | CPUID_DE | CPUID_PSE |
1050 CPUID_TSC | CPUID_MSR | CPUID_MCE |
1051 CPUID_CX8 | CPUID_PGE | CPUID_CMOV;
1052 }
1053 break;
1054 default:
1055 /* cache info: needed for Pentium Pro compatibility */
1056 EAX = 0x410601;
1057 EBX = 0;
1058 ECX = 0;
1059 EDX = 0;
1060 break;
1061 }
1062 }
1063
1064 void helper_lldt_T0(void)
1065 {
1066 int selector;
1067 SegmentCache *dt;
1068 uint32_t e1, e2;
1069 int index;
1070 uint8_t *ptr;
1071
1072 selector = T0 & 0xffff;
1073 if ((selector & 0xfffc) == 0) {
1074 /* XXX: NULL selector case: invalid LDT */
1075 env->ldt.base = NULL;
1076 env->ldt.limit = 0;
1077 } else {
1078 if (selector & 0x4)
1079 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1080 dt = &env->gdt;
1081 index = selector & ~7;
1082 if ((index + 7) > dt->limit)
1083 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1084 ptr = dt->base + index;
1085 e1 = ldl_kernel(ptr);
1086 e2 = ldl_kernel(ptr + 4);
1087 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
1088 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1089 if (!(e2 & DESC_P_MASK))
1090 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1091 load_seg_cache_raw_dt(&env->ldt, e1, e2);
1092 }
1093 env->ldt.selector = selector;
1094 }
1095
1096 void helper_ltr_T0(void)
1097 {
1098 int selector;
1099 SegmentCache *dt;
1100 uint32_t e1, e2;
1101 int index, type;
1102 uint8_t *ptr;
1103
1104 selector = T0 & 0xffff;
1105 if ((selector & 0xfffc) == 0) {
1106 /* NULL selector case: invalid LDT */
1107 env->tr.base = NULL;
1108 env->tr.limit = 0;
1109 env->tr.flags = 0;
1110 } else {
1111 if (selector & 0x4)
1112 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1113 dt = &env->gdt;
1114 index = selector & ~7;
1115 if ((index + 7) > dt->limit)
1116 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1117 ptr = dt->base + index;
1118 e1 = ldl_kernel(ptr);
1119 e2 = ldl_kernel(ptr + 4);
1120 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1121 if ((e2 & DESC_S_MASK) ||
1122 (type != 1 && type != 9))
1123 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1124 if (!(e2 & DESC_P_MASK))
1125 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1126 load_seg_cache_raw_dt(&env->tr, e1, e2);
1127 e2 |= DESC_TSS_BUSY_MASK;
1128 stl_kernel(ptr + 4, e2);
1129 }
1130 env->tr.selector = selector;
1131 }
1132
1133 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1134 void load_seg(int seg_reg, int selector)
1135 {
1136 uint32_t e1, e2;
1137 int cpl, dpl, rpl;
1138 SegmentCache *dt;
1139 int index;
1140 uint8_t *ptr;
1141
1142 selector &= 0xffff;
1143 if ((selector & 0xfffc) == 0) {
1144 /* null selector case */
1145 if (seg_reg == R_SS)
1146 raise_exception_err(EXCP0D_GPF, 0);
1147 cpu_x86_load_seg_cache(env, seg_reg, selector, NULL, 0, 0);
1148 } else {
1149
1150 if (selector & 0x4)
1151 dt = &env->ldt;
1152 else
1153 dt = &env->gdt;
1154 index = selector & ~7;
1155 if ((index + 7) > dt->limit)
1156 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1157 ptr = dt->base + index;
1158 e1 = ldl_kernel(ptr);
1159 e2 = ldl_kernel(ptr + 4);
1160
1161 if (!(e2 & DESC_S_MASK))
1162 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1163 rpl = selector & 3;
1164 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1165 cpl = env->hflags & HF_CPL_MASK;
1166 if (seg_reg == R_SS) {
1167 /* must be writable segment */
1168 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
1169 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1170 if (rpl != cpl || dpl != cpl)
1171 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1172 } else {
1173 /* must be readable segment */
1174 if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
1175 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1176
1177 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1178 /* if not conforming code, test rights */
1179 if (dpl < cpl || dpl < rpl)
1180 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1181 }
1182 }
1183
1184 if (!(e2 & DESC_P_MASK)) {
1185 if (seg_reg == R_SS)
1186 raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
1187 else
1188 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1189 }
1190
1191 /* set the access bit if not already set */
1192 if (!(e2 & DESC_A_MASK)) {
1193 e2 |= DESC_A_MASK;
1194 stl_kernel(ptr + 4, e2);
1195 }
1196
1197 cpu_x86_load_seg_cache(env, seg_reg, selector,
1198 get_seg_base(e1, e2),
1199 get_seg_limit(e1, e2),
1200 e2);
1201 #if 0
1202 fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1203 selector, (unsigned long)sc->base, sc->limit, sc->flags);
1204 #endif
1205 }
1206 }
1207
1208 /* protected mode jump */
1209 void helper_ljmp_protected_T0_T1(int next_eip)
1210 {
1211 int new_cs, new_eip, gate_cs, type;
1212 uint32_t e1, e2, cpl, dpl, rpl, limit;
1213
1214 new_cs = T0;
1215 new_eip = T1;
1216 if ((new_cs & 0xfffc) == 0)
1217 raise_exception_err(EXCP0D_GPF, 0);
1218 if (load_segment(&e1, &e2, new_cs) != 0)
1219 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1220 cpl = env->hflags & HF_CPL_MASK;
1221 if (e2 & DESC_S_MASK) {
1222 if (!(e2 & DESC_CS_MASK))
1223 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1224 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1225 if (e2 & DESC_C_MASK) {
1226 /* conforming code segment */
1227 if (dpl > cpl)
1228 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1229 } else {
1230 /* non conforming code segment */
1231 rpl = new_cs & 3;
1232 if (rpl > cpl)
1233 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1234 if (dpl != cpl)
1235 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1236 }
1237 if (!(e2 & DESC_P_MASK))
1238 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1239 limit = get_seg_limit(e1, e2);
1240 if (new_eip > limit)
1241 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1242 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1243 get_seg_base(e1, e2), limit, e2);
1244 EIP = new_eip;
1245 } else {
1246 /* jump to call or task gate */
1247 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1248 rpl = new_cs & 3;
1249 cpl = env->hflags & HF_CPL_MASK;
1250 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1251 switch(type) {
1252 case 1: /* 286 TSS */
1253 case 9: /* 386 TSS */
1254 case 5: /* task gate */
1255 if (dpl < cpl || dpl < rpl)
1256 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1257 switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
1258 break;
1259 case 4: /* 286 call gate */
1260 case 12: /* 386 call gate */
1261 if ((dpl < cpl) || (dpl < rpl))
1262 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1263 if (!(e2 & DESC_P_MASK))
1264 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1265 gate_cs = e1 >> 16;
1266 if (load_segment(&e1, &e2, gate_cs) != 0)
1267 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1268 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1269 /* must be code segment */
1270 if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
1271 (DESC_S_MASK | DESC_CS_MASK)))
1272 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1273 if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
1274 (!(e2 & DESC_C_MASK) && (dpl != cpl)))
1275 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1276 if (!(e2 & DESC_P_MASK))
1277 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1278 new_eip = (e1 & 0xffff);
1279 if (type == 12)
1280 new_eip |= (e2 & 0xffff0000);
1281 limit = get_seg_limit(e1, e2);
1282 if (new_eip > limit)
1283 raise_exception_err(EXCP0D_GPF, 0);
1284 cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1285 get_seg_base(e1, e2), limit, e2);
1286 EIP = new_eip;
1287 break;
1288 default:
1289 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1290 break;
1291 }
1292 }
1293 }
1294
1295 /* real mode call */
1296 void helper_lcall_real_T0_T1(int shift, int next_eip)
1297 {
1298 int new_cs, new_eip;
1299 uint32_t esp, esp_mask;
1300 uint8_t *ssp;
1301
1302 new_cs = T0;
1303 new_eip = T1;
1304 esp = ESP;
1305 esp_mask = get_sp_mask(env->segs[R_SS].flags);
1306 ssp = env->segs[R_SS].base;
1307 if (shift) {
1308 PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1309 PUSHL(ssp, esp, esp_mask, next_eip);
1310 } else {
1311 PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1312 PUSHW(ssp, esp, esp_mask, next_eip);
1313 }
1314
1315 ESP = (ESP & ~esp_mask) | (esp & esp_mask);
1316 env->eip = new_eip;
1317 env->segs[R_CS].selector = new_cs;
1318 env->segs[R_CS].base = (uint8_t *)(new_cs << 4);
1319 }
1320
1321 /* protected mode call */
1322 void helper_lcall_protected_T0_T1(int shift, int next_eip)
1323 {
1324 int new_cs, new_eip, new_stack, i;
1325 uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
1326 uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
1327 uint32_t val, limit, old_sp_mask;
1328 uint8_t *ssp, *old_ssp;
1329
1330 new_cs = T0;
1331 new_eip = T1;
1332 #ifdef DEBUG_PCALL
1333 if (loglevel & CPU_LOG_PCALL) {
1334 fprintf(logfile, "lcall %04x:%08x s=%d\n",
1335 new_cs, new_eip, shift);
1336 cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
1337 }
1338 #endif
1339 if ((new_cs & 0xfffc) == 0)
1340 raise_exception_err(EXCP0D_GPF, 0);
1341 if (load_segment(&e1, &e2, new_cs) != 0)
1342 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1343 cpl = env->hflags & HF_CPL_MASK;
1344 #ifdef DEBUG_PCALL
1345 if (loglevel & CPU_LOG_PCALL) {
1346 fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
1347 }
1348 #endif
1349 if (e2 & DESC_S_MASK) {
1350 if (!(e2 & DESC_CS_MASK))
1351 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1352 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1353 if (e2 & DESC_C_MASK) {
1354 /* conforming code segment */
1355 if (dpl > cpl)
1356 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1357 } else {
1358 /* non conforming code segment */
1359 rpl = new_cs & 3;
1360 if (rpl > cpl)
1361 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1362 if (dpl != cpl)
1363 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1364 }
1365 if (!(e2 & DESC_P_MASK))
1366 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1367
1368 sp = ESP;
1369 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1370 ssp = env->segs[R_SS].base;
1371 if (shift) {
1372 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1373 PUSHL(ssp, sp, sp_mask, next_eip);
1374 } else {
1375 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1376 PUSHW(ssp, sp, sp_mask, next_eip);
1377 }
1378
1379 limit = get_seg_limit(e1, e2);
1380 if (new_eip > limit)
1381 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1382 /* from this point, not restartable */
1383 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1384 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1385 get_seg_base(e1, e2), limit, e2);
1386 EIP = new_eip;
1387 } else {
1388 /* check gate type */
1389 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
1390 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1391 rpl = new_cs & 3;
1392 switch(type) {
1393 case 1: /* available 286 TSS */
1394 case 9: /* available 386 TSS */
1395 case 5: /* task gate */
1396 if (dpl < cpl || dpl < rpl)
1397 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1398 switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
1399 return;
1400 case 4: /* 286 call gate */
1401 case 12: /* 386 call gate */
1402 break;
1403 default:
1404 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1405 break;
1406 }
1407 shift = type >> 3;
1408
1409 if (dpl < cpl || dpl < rpl)
1410 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1411 /* check valid bit */
1412 if (!(e2 & DESC_P_MASK))
1413 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1414 selector = e1 >> 16;
1415 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
1416 param_count = e2 & 0x1f;
1417 if ((selector & 0xfffc) == 0)
1418 raise_exception_err(EXCP0D_GPF, 0);
1419
1420 if (load_segment(&e1, &e2, selector) != 0)
1421 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1422 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
1423 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1424 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1425 if (dpl > cpl)
1426 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1427 if (!(e2 & DESC_P_MASK))
1428 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1429
1430 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1431 /* to inner priviledge */
1432 get_ss_esp_from_tss(&ss, &sp, dpl);
1433 #ifdef DEBUG_PCALL
1434 if (loglevel & CPU_LOG_PCALL)
1435 fprintf(logfile, "new ss:esp=%04x:%08x param_count=%d ESP=%x\n",
1436 ss, sp, param_count, ESP);
1437 #endif
1438 if ((ss & 0xfffc) == 0)
1439 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1440 if ((ss & 3) != dpl)
1441 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1442 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
1443 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1444 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1445 if (ss_dpl != dpl)
1446 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1447 if (!(ss_e2 & DESC_S_MASK) ||
1448 (ss_e2 & DESC_CS_MASK) ||
1449 !(ss_e2 & DESC_W_MASK))
1450 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1451 if (!(ss_e2 & DESC_P_MASK))
1452 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1453
1454 // push_size = ((param_count * 2) + 8) << shift;
1455
1456 old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1457 old_ssp = env->segs[R_SS].base;
1458
1459 sp_mask = get_sp_mask(ss_e2);
1460 ssp = get_seg_base(ss_e1, ss_e2);
1461 if (shift) {
1462 PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1463 PUSHL(ssp, sp, sp_mask, ESP);
1464 for(i = param_count - 1; i >= 0; i--) {
1465 val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
1466 PUSHL(ssp, sp, sp_mask, val);
1467 }
1468 } else {
1469 PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1470 PUSHW(ssp, sp, sp_mask, ESP);
1471 for(i = param_count - 1; i >= 0; i--) {
1472 val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
1473 PUSHW(ssp, sp, sp_mask, val);
1474 }
1475 }
1476 new_stack = 1;
1477 } else {
1478 /* to same priviledge */
1479 sp = ESP;
1480 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1481 ssp = env->segs[R_SS].base;
1482 // push_size = (4 << shift);
1483 new_stack = 0;
1484 }
1485
1486 if (shift) {
1487 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1488 PUSHL(ssp, sp, sp_mask, next_eip);
1489 } else {
1490 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1491 PUSHW(ssp, sp, sp_mask, next_eip);
1492 }
1493
1494 /* from this point, not restartable */
1495
1496 if (new_stack) {
1497 ss = (ss & ~3) | dpl;
1498 cpu_x86_load_seg_cache(env, R_SS, ss,
1499 ssp,
1500 get_seg_limit(ss_e1, ss_e2),
1501 ss_e2);
1502 }
1503
1504 selector = (selector & ~3) | dpl;
1505 cpu_x86_load_seg_cache(env, R_CS, selector,
1506 get_seg_base(e1, e2),
1507 get_seg_limit(e1, e2),
1508 e2);
1509 cpu_x86_set_cpl(env, dpl);
1510 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1511 EIP = offset;
1512 }
1513 }
1514
1515 /* real and vm86 mode iret */
1516 void helper_iret_real(int shift)
1517 {
1518 uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
1519 uint8_t *ssp;
1520 int eflags_mask;
1521
1522 sp_mask = 0xffff; /* XXXX: use SS segment size ? */
1523 sp = ESP;
1524 ssp = env->segs[R_SS].base;
1525 if (shift == 1) {
1526 /* 32 bits */
1527 POPL(ssp, sp, sp_mask, new_eip);
1528 POPL(ssp, sp, sp_mask, new_cs);
1529 new_cs &= 0xffff;
1530 POPL(ssp, sp, sp_mask, new_eflags);
1531 } else {
1532 /* 16 bits */
1533 POPW(ssp, sp, sp_mask, new_eip);
1534 POPW(ssp, sp, sp_mask, new_cs);
1535 POPW(ssp, sp, sp_mask, new_eflags);
1536 }
1537 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1538 load_seg_vm(R_CS, new_cs);
1539 env->eip = new_eip;
1540 if (env->eflags & VM_MASK)
1541 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
1542 else
1543 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
1544 if (shift == 0)
1545 eflags_mask &= 0xffff;
1546 load_eflags(new_eflags, eflags_mask);
1547 }
1548
1549 static inline void validate_seg(int seg_reg, int cpl)
1550 {
1551 int dpl;
1552 uint32_t e2;
1553
1554 e2 = env->segs[seg_reg].flags;
1555 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1556 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1557 /* data or non conforming code segment */
1558 if (dpl < cpl) {
1559 cpu_x86_load_seg_cache(env, seg_reg, 0, NULL, 0, 0);
1560 }
1561 }
1562 }
1563
1564 /* protected mode iret */
1565 static inline void helper_ret_protected(int shift, int is_iret, int addend)
1566 {
1567 uint32_t sp, new_cs, new_eip, new_eflags, new_esp, new_ss, sp_mask;
1568 uint32_t new_es, new_ds, new_fs, new_gs;
1569 uint32_t e1, e2, ss_e1, ss_e2;
1570 int cpl, dpl, rpl, eflags_mask, iopl;
1571 uint8_t *ssp;
1572
1573 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1574 sp = ESP;
1575 ssp = env->segs[R_SS].base;
1576 if (shift == 1) {
1577 /* 32 bits */
1578 POPL(ssp, sp, sp_mask, new_eip);
1579 POPL(ssp, sp, sp_mask, new_cs);
1580 new_cs &= 0xffff;
1581 if (is_iret) {
1582 POPL(ssp, sp, sp_mask, new_eflags);
1583 if (new_eflags & VM_MASK)
1584 goto return_to_vm86;
1585 }
1586 } else {
1587 /* 16 bits */
1588 POPW(ssp, sp, sp_mask, new_eip);
1589 POPW(ssp, sp, sp_mask, new_cs);
1590 if (is_iret)
1591 POPW(ssp, sp, sp_mask, new_eflags);
1592 }
1593 #ifdef DEBUG_PCALL
1594 if (loglevel & CPU_LOG_PCALL) {
1595 fprintf(logfile, "lret new %04x:%08x s=%d addend=0x%x\n",
1596 new_cs, new_eip, shift, addend);
1597 cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
1598 }
1599 #endif
1600 if ((new_cs & 0xfffc) == 0)
1601 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1602 if (load_segment(&e1, &e2, new_cs) != 0)
1603 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1604 if (!(e2 & DESC_S_MASK) ||
1605 !(e2 & DESC_CS_MASK))
1606 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1607 cpl = env->hflags & HF_CPL_MASK;
1608 rpl = new_cs & 3;
1609 if (rpl < cpl)
1610 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1611 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1612 if (e2 & DESC_C_MASK) {
1613 if (dpl > rpl)
1614 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1615 } else {
1616 if (dpl != rpl)
1617 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1618 }
1619 if (!(e2 & DESC_P_MASK))
1620 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1621
1622 sp += addend;
1623 if (rpl == cpl) {
1624 /* return to same priledge level */
1625 cpu_x86_load_seg_cache(env, R_CS, new_cs,
1626 get_seg_base(e1, e2),
1627 get_seg_limit(e1, e2),
1628 e2);
1629 } else {
1630 /* return to different priviledge level */
1631 if (shift == 1) {
1632 /* 32 bits */
1633 POPL(ssp, sp, sp_mask, new_esp);
1634 POPL(ssp, sp, sp_mask, new_ss);
1635 new_ss &= 0xffff;
1636 } else {
1637 /* 16 bits */
1638 POPW(ssp, sp, sp_mask, new_esp);
1639 POPW(ssp, sp, sp_mask, new_ss);
1640 }
1641 #ifdef DEBUG_PCALL
1642 if (loglevel & CPU_LOG_PCALL) {
1643 fprintf(logfile, "new ss:esp=%04x:%08x\n",
1644 new_ss, new_esp);
1645 }
1646 #endif
1647
1648 if ((new_ss & 3) != rpl)
1649 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1650 if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
1651 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1652 if (!(ss_e2 & DESC_S_MASK) ||
1653 (ss_e2 & DESC_CS_MASK) ||
1654 !(ss_e2 & DESC_W_MASK))
1655 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1656 dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1657 if (dpl != rpl)
1658 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1659 if (!(ss_e2 & DESC_P_MASK))
1660 raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
1661
1662 cpu_x86_load_seg_cache(env, R_CS, new_cs,
1663 get_seg_base(e1, e2),
1664 get_seg_limit(e1, e2),
1665 e2);
1666 cpu_x86_load_seg_cache(env, R_SS, new_ss,
1667 get_seg_base(ss_e1, ss_e2),
1668 get_seg_limit(ss_e1, ss_e2),
1669 ss_e2);
1670 cpu_x86_set_cpl(env, rpl);
1671 sp = new_esp;
1672 sp_mask = get_sp_mask(ss_e2);
1673
1674 /* validate data segments */
1675 validate_seg(R_ES, cpl);
1676 validate_seg(R_DS, cpl);
1677 validate_seg(R_FS, cpl);
1678 validate_seg(R_GS, cpl);
1679
1680 sp += addend;
1681 }
1682 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1683 env->eip = new_eip;
1684 if (is_iret) {
1685 /* NOTE: 'cpl' is the _old_ CPL */
1686 eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
1687 if (cpl == 0)
1688 eflags_mask |= IOPL_MASK;
1689 iopl = (env->eflags >> IOPL_SHIFT) & 3;
1690 if (cpl <= iopl)
1691 eflags_mask |= IF_MASK;
1692 if (shift == 0)
1693 eflags_mask &= 0xffff;
1694 load_eflags(new_eflags, eflags_mask);
1695 }
1696 return;
1697
1698 return_to_vm86:
1699 POPL(ssp, sp, sp_mask, new_esp);
1700 POPL(ssp, sp, sp_mask, new_ss);
1701 POPL(ssp, sp, sp_mask, new_es);
1702 POPL(ssp, sp, sp_mask, new_ds);
1703 POPL(ssp, sp, sp_mask, new_fs);
1704 POPL(ssp, sp, sp_mask, new_gs);
1705
1706 /* modify processor state */
1707 load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK |
1708 IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
1709 load_seg_vm(R_CS, new_cs & 0xffff);
1710 cpu_x86_set_cpl(env, 3);
1711 load_seg_vm(R_SS, new_ss & 0xffff);
1712 load_seg_vm(R_ES, new_es & 0xffff);
1713 load_seg_vm(R_DS, new_ds & 0xffff);
1714 load_seg_vm(R_FS, new_fs & 0xffff);
1715 load_seg_vm(R_GS, new_gs & 0xffff);
1716
1717 env->eip = new_eip & 0xffff;
1718 ESP = new_esp;
1719 }
1720
1721 void helper_iret_protected(int shift, int next_eip)
1722 {
1723 int tss_selector, type;
1724 uint32_t e1, e2;
1725
1726 /* specific case for TSS */
1727 if (env->eflags & NT_MASK) {
1728 tss_selector = lduw_kernel(env->tr.base + 0);
1729 if (tss_selector & 4)
1730 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1731 if (load_segment(&e1, &e2, tss_selector) != 0)
1732 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1733 type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
1734 /* NOTE: we check both segment and busy TSS */
1735 if (type != 3)
1736 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1737 switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
1738 } else {
1739 helper_ret_protected(shift, 1, 0);
1740 }
1741 }
1742
1743 void helper_lret_protected(int shift, int addend)
1744 {
1745 helper_ret_protected(shift, 0, addend);
1746 }
1747
1748 void helper_movl_crN_T0(int reg)
1749 {
1750 switch(reg) {
1751 case 0:
1752 cpu_x86_update_cr0(env, T0);
1753 break;
1754 case 3:
1755 cpu_x86_update_cr3(env, T0);
1756 break;
1757 case 4:
1758 cpu_x86_update_cr4(env, T0);
1759 break;
1760 default:
1761 env->cr[reg] = T0;
1762 break;
1763 }
1764 }
1765
1766 /* XXX: do more */
1767 void helper_movl_drN_T0(int reg)
1768 {
1769 env->dr[reg] = T0;
1770 }
1771
1772 void helper_invlpg(unsigned int addr)
1773 {
1774 cpu_x86_flush_tlb(env, addr);
1775 }
1776
1777 /* rdtsc */
1778 #if !defined(__i386__) && !defined(__x86_64__)
1779 uint64_t emu_time;
1780 #endif
1781
1782 void helper_rdtsc(void)
1783 {
1784 uint64_t val;
1785 #if defined(__i386__) || defined(__x86_64__)
1786 asm volatile ("rdtsc" : "=A" (val));
1787 #else
1788 /* better than nothing: the time increases */
1789 val = emu_time++;
1790 #endif
1791 EAX = val;
1792 EDX = val >> 32;
1793 }
1794
1795 void helper_wrmsr(void)
1796 {
1797 switch(ECX) {
1798 case MSR_IA32_SYSENTER_CS:
1799 env->sysenter_cs = EAX & 0xffff;
1800 break;
1801 case MSR_IA32_SYSENTER_ESP:
1802 env->sysenter_esp = EAX;
1803 break;
1804 case MSR_IA32_SYSENTER_EIP:
1805 env->sysenter_eip = EAX;
1806 break;
1807 default:
1808 /* XXX: exception ? */
1809 break;
1810 }
1811 }
1812
1813 void helper_rdmsr(void)
1814 {
1815 switch(ECX) {
1816 case MSR_IA32_SYSENTER_CS:
1817 EAX = env->sysenter_cs;
1818 EDX = 0;
1819 break;
1820 case MSR_IA32_SYSENTER_ESP:
1821 EAX = env->sysenter_esp;
1822 EDX = 0;
1823 break;
1824 case MSR_IA32_SYSENTER_EIP:
1825 EAX = env->sysenter_eip;
1826 EDX = 0;
1827 break;
1828 default:
1829 /* XXX: exception ? */
1830 break;
1831 }
1832 }
1833
1834 void helper_lsl(void)
1835 {
1836 unsigned int selector, limit;
1837 uint32_t e1, e2;
1838 int rpl, dpl, cpl, type;
1839
1840 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1841 selector = T0 & 0xffff;
1842 if (load_segment(&e1, &e2, selector) != 0)
1843 return;
1844 rpl = selector & 3;
1845 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1846 cpl = env->hflags & HF_CPL_MASK;
1847 if (e2 & DESC_S_MASK) {
1848 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
1849 /* conforming */
1850 } else {
1851 if (dpl < cpl || dpl < rpl)
1852 return;
1853 }
1854 } else {
1855 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1856 switch(type) {
1857 case 1:
1858 case 2:
1859 case 3:
1860 case 9:
1861 case 11:
1862 break;
1863 default:
1864 return;
1865 }
1866 if (dpl < cpl || dpl < rpl)
1867 return;
1868 }
1869 limit = get_seg_limit(e1, e2);
1870 T1 = limit;
1871 CC_SRC |= CC_Z;
1872 }
1873
1874 void helper_lar(void)
1875 {
1876 unsigned int selector;
1877 uint32_t e1, e2;
1878 int rpl, dpl, cpl, type;
1879
1880 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1881 selector = T0 & 0xffff;
1882 if ((selector & 0xfffc) == 0)
1883 return;
1884 if (load_segment(&e1, &e2, selector) != 0)
1885 return;
1886 rpl = selector & 3;
1887 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1888 cpl = env->hflags & HF_CPL_MASK;
1889 if (e2 & DESC_S_MASK) {
1890 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
1891 /* conforming */
1892 } else {
1893 if (dpl < cpl || dpl < rpl)
1894 return;
1895 }
1896 } else {
1897 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1898 switch(type) {
1899 case 1:
1900 case 2:
1901 case 3:
1902 case 4:
1903 case 5:
1904 case 9:
1905 case 11:
1906 case 12:
1907 break;
1908 default:
1909 return;
1910 }
1911 if (dpl < cpl || dpl < rpl)
1912 return;
1913 }
1914 T1 = e2 & 0x00f0ff00;
1915 CC_SRC |= CC_Z;
1916 }
1917
1918 void helper_verr(void)
1919 {
1920 unsigned int selector;
1921 uint32_t e1, e2;
1922 int rpl, dpl, cpl;
1923
1924 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1925 selector = T0 & 0xffff;
1926 if ((selector & 0xfffc) == 0)
1927 return;
1928 if (load_segment(&e1, &e2, selector) != 0)
1929 return;
1930 if (!(e2 & DESC_S_MASK))
1931 return;
1932 rpl = selector & 3;
1933 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1934 cpl = env->hflags & HF_CPL_MASK;
1935 if (e2 & DESC_CS_MASK) {
1936 if (!(e2 & DESC_R_MASK))
1937 return;
1938 if (!(e2 & DESC_C_MASK)) {
1939 if (dpl < cpl || dpl < rpl)
1940 return;
1941 }
1942 } else {
1943 if (dpl < cpl || dpl < rpl)
1944 return;
1945 }
1946 CC_SRC |= CC_Z;
1947 }
1948
1949 void helper_verw(void)
1950 {
1951 unsigned int selector;
1952 uint32_t e1, e2;
1953 int rpl, dpl, cpl;
1954
1955 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1956 selector = T0 & 0xffff;
1957 if ((selector & 0xfffc) == 0)
1958 return;
1959 if (load_segment(&e1, &e2, selector) != 0)
1960 return;
1961 if (!(e2 & DESC_S_MASK))
1962 return;
1963 rpl = selector & 3;
1964 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1965 cpl = env->hflags & HF_CPL_MASK;
1966 if (e2 & DESC_CS_MASK) {
1967 return;
1968 } else {
1969 if (dpl < cpl || dpl < rpl)
1970 return;
1971 if (!(e2 & DESC_W_MASK))
1972 return;
1973 }
1974 CC_SRC |= CC_Z;
1975 }
1976
1977 /* FPU helpers */
1978
1979 void helper_fldt_ST0_A0(void)
1980 {
1981 int new_fpstt;
1982 new_fpstt = (env->fpstt - 1) & 7;
1983 env->fpregs[new_fpstt] = helper_fldt((uint8_t *)A0);
1984 env->fpstt = new_fpstt;
1985 env->fptags[new_fpstt] = 0; /* validate stack entry */
1986 }
1987
1988 void helper_fstt_ST0_A0(void)
1989 {
1990 helper_fstt(ST0, (uint8_t *)A0);
1991 }
1992
1993 /* BCD ops */
1994
1995 void helper_fbld_ST0_A0(void)
1996 {
1997 CPU86_LDouble tmp;
1998 uint64_t val;
1999 unsigned int v;
2000 int i;
2001
2002 val = 0;
2003 for(i = 8; i >= 0; i--) {
2004 v = ldub((uint8_t *)A0 + i);
2005 val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
2006 }
2007 tmp = val;
2008 if (ldub((uint8_t *)A0 + 9) & 0x80)
2009 tmp = -tmp;
2010 fpush();
2011 ST0 = tmp;
2012 }
2013
2014 void helper_fbst_ST0_A0(void)
2015 {
2016 CPU86_LDouble tmp;
2017 int v;
2018 uint8_t *mem_ref, *mem_end;
2019 int64_t val;
2020
2021 tmp = rint(ST0);
2022 val = (int64_t)tmp;
2023 mem_ref = (uint8_t *)A0;
2024 mem_end = mem_ref + 9;
2025 if (val < 0) {
2026 stb(mem_end, 0x80);
2027 val = -val;
2028 } else {
2029 stb(mem_end, 0x00);
2030 }
2031 while (mem_ref < mem_end) {
2032 if (val == 0)
2033 break;
2034 v = val % 100;
2035 val = val / 100;
2036 v = ((v / 10) << 4) | (v % 10);
2037 stb(mem_ref++, v);
2038 }
2039 while (mem_ref < mem_end) {
2040 stb(mem_ref++, 0);
2041 }
2042 }
2043
2044 void helper_f2xm1(void)
2045 {
2046 ST0 = pow(2.0,ST0) - 1.0;
2047 }
2048
2049 void helper_fyl2x(void)
2050 {
2051 CPU86_LDouble fptemp;
2052
2053 fptemp = ST0;
2054 if (fptemp>0.0){
2055 fptemp = log(fptemp)/log(2.0); /* log2(ST) */
2056 ST1 *= fptemp;
2057 fpop();
2058 } else {
2059 env->fpus &= (~0x4700);
2060 env->fpus |= 0x400;
2061 }
2062 }
2063
2064 void helper_fptan(void)
2065 {
2066 CPU86_LDouble fptemp;
2067
2068 fptemp = ST0;
2069 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2070 env->fpus |= 0x400;
2071 } else {
2072 ST0 = tan(fptemp);
2073 fpush();
2074 ST0 = 1.0;
2075 env->fpus &= (~0x400); /* C2 <-- 0 */
2076 /* the above code is for |arg| < 2**52 only */
2077 }
2078 }
2079
2080 void helper_fpatan(void)
2081 {
2082 CPU86_LDouble fptemp, fpsrcop;
2083
2084 fpsrcop = ST1;
2085 fptemp = ST0;
2086 ST1 = atan2(fpsrcop,fptemp);
2087 fpop();
2088 }
2089
2090 void helper_fxtract(void)
2091 {
2092 CPU86_LDoubleU temp;
2093 unsigned int expdif;
2094
2095 temp.d = ST0;
2096 expdif = EXPD(temp) - EXPBIAS;
2097 /*DP exponent bias*/
2098 ST0 = expdif;
2099 fpush();
2100 BIASEXPONENT(temp);
2101 ST0 = temp.d;
2102 }
2103
2104 void helper_fprem1(void)
2105 {
2106 CPU86_LDouble dblq, fpsrcop, fptemp;
2107 CPU86_LDoubleU fpsrcop1, fptemp1;
2108 int expdif;
2109 int q;
2110
2111 fpsrcop = ST0;
2112 fptemp = ST1;
2113 fpsrcop1.d = fpsrcop;
2114 fptemp1.d = fptemp;
2115 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2116 if (expdif < 53) {
2117 dblq = fpsrcop / fptemp;
2118 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2119 ST0 = fpsrcop - fptemp*dblq;
2120 q = (int)dblq; /* cutting off top bits is assumed here */
2121 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2122 /* (C0,C1,C3) <-- (q2,q1,q0) */
2123 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2124 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2125 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2126 } else {
2127 env->fpus |= 0x400; /* C2 <-- 1 */
2128 fptemp = pow(2.0, expdif-50);
2129 fpsrcop = (ST0 / ST1) / fptemp;
2130 /* fpsrcop = integer obtained by rounding to the nearest */
2131 fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2132 floor(fpsrcop): ceil(fpsrcop);
2133 ST0 -= (ST1 * fpsrcop * fptemp);
2134 }
2135 }
2136
2137 void helper_fprem(void)
2138 {
2139 CPU86_LDouble dblq, fpsrcop, fptemp;
2140 CPU86_LDoubleU fpsrcop1, fptemp1;
2141 int expdif;
2142 int q;
2143
2144 fpsrcop = ST0;
2145 fptemp = ST1;
2146 fpsrcop1.d = fpsrcop;
2147 fptemp1.d = fptemp;
2148 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2149 if ( expdif < 53 ) {
2150 dblq = fpsrcop / fptemp;
2151 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2152 ST0 = fpsrcop - fptemp*dblq;
2153 q = (int)dblq; /* cutting off top bits is assumed here */
2154 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2155 /* (C0,C1,C3) <-- (q2,q1,q0) */
2156 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2157 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2158 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2159 } else {
2160 env->fpus |= 0x400; /* C2 <-- 1 */
2161 fptemp = pow(2.0, expdif-50);
2162 fpsrcop = (ST0 / ST1) / fptemp;
2163 /* fpsrcop = integer obtained by chopping */
2164 fpsrcop = (fpsrcop < 0.0)?
2165 -(floor(fabs(fpsrcop))): floor(fpsrcop);
2166 ST0 -= (ST1 * fpsrcop * fptemp);
2167 }
2168 }
2169
2170 void helper_fyl2xp1(void)
2171 {
2172 CPU86_LDouble fptemp;
2173
2174 fptemp = ST0;
2175 if ((fptemp+1.0)>0.0) {
2176 fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2177 ST1 *= fptemp;
2178 fpop();
2179 } else {
2180 env->fpus &= (~0x4700);
2181 env->fpus |= 0x400;
2182 }
2183 }
2184
2185 void helper_fsqrt(void)
2186 {
2187 CPU86_LDouble fptemp;
2188
2189 fptemp = ST0;
2190 if (fptemp<0.0) {
2191 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2192 env->fpus |= 0x400;
2193 }
2194 ST0 = sqrt(fptemp);
2195 }
2196
2197 void helper_fsincos(void)
2198 {
2199 CPU86_LDouble fptemp;
2200
2201 fptemp = ST0;
2202 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2203 env->fpus |= 0x400;
2204 } else {
2205 ST0 = sin(fptemp);
2206 fpush();
2207 ST0 = cos(fptemp);
2208 env->fpus &= (~0x400); /* C2 <-- 0 */
2209 /* the above code is for |arg| < 2**63 only */
2210 }
2211 }
2212
2213 void helper_frndint(void)
2214 {
2215 CPU86_LDouble a;
2216
2217 a = ST0;
2218 #ifdef __arm__
2219 switch(env->fpuc & RC_MASK) {
2220 default:
2221 case RC_NEAR:
2222 asm("rndd %0, %1" : "=f" (a) : "f"(a));
2223 break;
2224 case RC_DOWN:
2225 asm("rnddm %0, %1" : "=f" (a) : "f"(a));
2226 break;
2227 case RC_UP:
2228 asm("rnddp %0, %1" : "=f" (a) : "f"(a));
2229 break;
2230 case RC_CHOP:
2231 asm("rnddz %0, %1" : "=f" (a) : "f"(a));
2232 break;
2233 }
2234 #else
2235 a = rint(a);
2236 #endif
2237 ST0 = a;
2238 }
2239
2240 void helper_fscale(void)
2241 {
2242 CPU86_LDouble fpsrcop, fptemp;
2243
2244 fpsrcop = 2.0;
2245 fptemp = pow(fpsrcop,ST1);
2246 ST0 *= fptemp;
2247 }
2248
2249 void helper_fsin(void)
2250 {
2251 CPU86_LDouble fptemp;
2252
2253 fptemp = ST0;
2254 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2255 env->fpus |= 0x400;
2256 } else {
2257 ST0 = sin(fptemp);
2258 env->fpus &= (~0x400); /* C2 <-- 0 */
2259 /* the above code is for |arg| < 2**53 only */
2260 }
2261 }
2262
2263 void helper_fcos(void)
2264 {
2265 CPU86_LDouble fptemp;
2266
2267 fptemp = ST0;
2268 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2269 env->fpus |= 0x400;
2270 } else {
2271 ST0 = cos(fptemp);
2272 env->fpus &= (~0x400); /* C2 <-- 0 */
2273 /* the above code is for |arg5 < 2**63 only */
2274 }
2275 }
2276
2277 void helper_fxam_ST0(void)
2278 {
2279 CPU86_LDoubleU temp;
2280 int expdif;
2281
2282 temp.d = ST0;
2283
2284 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2285 if (SIGND(temp))
2286 env->fpus |= 0x200; /* C1 <-- 1 */
2287
2288 expdif = EXPD(temp);
2289 if (expdif == MAXEXPD) {
2290 if (MANTD(temp) == 0)
2291 env->fpus |= 0x500 /*Infinity*/;
2292 else
2293 env->fpus |= 0x100 /*NaN*/;
2294 } else if (expdif == 0) {
2295 if (MANTD(temp) == 0)
2296 env->fpus |= 0x4000 /*Zero*/;
2297 else
2298 env->fpus |= 0x4400 /*Denormal*/;
2299 } else {
2300 env->fpus |= 0x400;
2301 }
2302 }
2303
2304 void helper_fstenv(uint8_t *ptr, int data32)
2305 {
2306 int fpus, fptag, exp, i;
2307 uint64_t mant;
2308 CPU86_LDoubleU tmp;
2309
2310 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2311 fptag = 0;
2312 for (i=7; i>=0; i--) {
2313 fptag <<= 2;
2314 if (env->fptags[i]) {
2315 fptag |= 3;
2316 } else {
2317 tmp.d = env->fpregs[i];
2318 exp = EXPD(tmp);
2319 mant = MANTD(tmp);
2320 if (exp == 0 && mant == 0) {
2321 /* zero */
2322 fptag |= 1;
2323 } else if (exp == 0 || exp == MAXEXPD
2324 #ifdef USE_X86LDOUBLE
2325 || (mant & (1LL << 63)) == 0
2326 #endif
2327 ) {
2328 /* NaNs, infinity, denormal */
2329 fptag |= 2;
2330 }
2331 }
2332 }
2333 if (data32) {
2334 /* 32 bit */
2335 stl(ptr, env->fpuc);
2336 stl(ptr + 4, fpus);
2337 stl(ptr + 8, fptag);
2338 stl(ptr + 12, 0); /* fpip */
2339 stl(ptr + 16, 0); /* fpcs */
2340 stl(ptr + 20, 0); /* fpoo */
2341 stl(ptr + 24, 0); /* fpos */
2342 } else {
2343 /* 16 bit */
2344 stw(ptr, env->fpuc);
2345 stw(ptr + 2, fpus);
2346 stw(ptr + 4, fptag);
2347 stw(ptr + 6, 0);
2348 stw(ptr + 8, 0);
2349 stw(ptr + 10, 0);
2350 stw(ptr + 12, 0);
2351 }
2352 }
2353
2354 void helper_fldenv(uint8_t *ptr, int data32)
2355 {
2356 int i, fpus, fptag;
2357
2358 if (data32) {
2359 env->fpuc = lduw(ptr);
2360 fpus = lduw(ptr + 4);
2361 fptag = lduw(ptr + 8);
2362 }
2363 else {
2364 env->fpuc = lduw(ptr);
2365 fpus = lduw(ptr + 2);
2366 fptag = lduw(ptr + 4);
2367 }
2368 env->fpstt = (fpus >> 11) & 7;
2369 env->fpus = fpus & ~0x3800;
2370 for(i = 0;i < 8; i++) {
2371 env->fptags[i] = ((fptag & 3) == 3);
2372 fptag >>= 2;
2373 }
2374 }
2375
2376 void helper_fsave(uint8_t *ptr, int data32)
2377 {
2378 CPU86_LDouble tmp;
2379 int i;
2380
2381 helper_fstenv(ptr, data32);
2382
2383 ptr += (14 << data32);
2384 for(i = 0;i < 8; i++) {
2385 tmp = ST(i);
2386 helper_fstt(tmp, ptr);
2387 ptr += 10;
2388 }
2389
2390 /* fninit */
2391 env->fpus = 0;
2392 env->fpstt = 0;
2393 env->fpuc = 0x37f;
2394 env->fptags[0] = 1;
2395 env->fptags[1] = 1;
2396 env->fptags[2] = 1;
2397 env->fptags[3] = 1;
2398 env->fptags[4] = 1;
2399 env->fptags[5] = 1;
2400 env->fptags[6] = 1;
2401 env->fptags[7] = 1;
2402 }
2403
2404 void helper_frstor(uint8_t *ptr, int data32)
2405 {
2406 CPU86_LDouble tmp;
2407 int i;
2408
2409 helper_fldenv(ptr, data32);
2410 ptr += (14 << data32);
2411
2412 for(i = 0;i < 8; i++) {
2413 tmp = helper_fldt(ptr);
2414 ST(i) = tmp;
2415 ptr += 10;
2416 }
2417 }
2418
2419 /* XXX: merge with helper_fstt ? */
2420
2421 #ifndef USE_X86LDOUBLE
2422
2423 void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
2424 {
2425 CPU86_LDoubleU temp;
2426 int e;
2427
2428 temp.d = f;
2429 /* mantissa */
2430 *pmant = (MANTD(temp) << 11) | (1LL << 63);
2431 /* exponent + sign */
2432 e = EXPD(temp) - EXPBIAS + 16383;
2433 e |= SIGND(temp) >> 16;
2434 *pexp = e;
2435 }
2436
2437 CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
2438 {
2439 CPU86_LDoubleU temp;
2440 int e;
2441 uint64_t ll;
2442
2443 /* XXX: handle overflow ? */
2444 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
2445 e |= (upper >> 4) & 0x800; /* sign */
2446 ll = (mant >> 11) & ((1LL << 52) - 1);
2447 #ifdef __arm__
2448 temp.l.upper = (e << 20) | (ll >> 32);
2449 temp.l.lower = ll;
2450 #else
2451 temp.ll = ll | ((uint64_t)e << 52);
2452 #endif
2453 return temp.d;
2454 }
2455
2456 #else
2457
2458 void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
2459 {
2460 CPU86_LDoubleU temp;
2461
2462 temp.d = f;
2463 *pmant = temp.l.lower;
2464 *pexp = temp.l.upper;
2465 }
2466
2467 CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
2468 {
2469 CPU86_LDoubleU temp;
2470
2471 temp.l.upper = upper;
2472 temp.l.lower = mant;
2473 return temp.d;
2474 }
2475 #endif
2476
2477 #if !defined(CONFIG_USER_ONLY)
2478
2479 #define MMUSUFFIX _mmu
2480 #define GETPC() (__builtin_return_address(0))
2481
2482 #define SHIFT 0
2483 #include "softmmu_template.h"
2484
2485 #define SHIFT 1
2486 #include "softmmu_template.h"
2487
2488 #define SHIFT 2
2489 #include "softmmu_template.h"
2490
2491 #define SHIFT 3
2492 #include "softmmu_template.h"
2493
2494 #endif
2495
2496 /* try to fill the TLB and return an exception if error. If retaddr is
2497 NULL, it means that the function was called in C code (i.e. not
2498 from generated code or from helper.c) */
2499 /* XXX: fix it to restore all registers */
2500 void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
2501 {
2502 TranslationBlock *tb;
2503 int ret;
2504 unsigned long pc;
2505 CPUX86State *saved_env;
2506
2507 /* XXX: hack to restore env in all cases, even if not called from
2508 generated code */
2509 saved_env = env;
2510 env = cpu_single_env;
2511
2512 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, is_user, 1);
2513 if (ret) {
2514 if (retaddr) {
2515 /* now we have a real cpu fault */
2516 pc = (unsigned long)retaddr;
2517 tb = tb_find_pc(pc);
2518 if (tb) {
2519 /* the PC is inside the translated code. It means that we have
2520 a virtual CPU fault */
2521 cpu_restore_state(tb, env, pc, NULL);
2522 }
2523 }
2524 raise_exception_err(EXCP0E_PAGE, env->error_code);
2525 }
2526 env = saved_env;
2527 }