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