]>
git.proxmox.com Git - qemu.git/blob - exec-i386.c
2 * i386 emulator main execution loop
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
20 #include "exec-i386.h"
25 //#define DEBUG_SIGNAL
27 /* main execution loop */
29 /* maximum total translate dcode allocated */
30 #define CODE_GEN_BUFFER_SIZE (2048 * 1024)
31 //#define CODE_GEN_BUFFER_SIZE (128 * 1024)
32 #define CODE_GEN_MAX_SIZE 65536
33 #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
35 /* threshold to flush the translated code buffer */
36 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
38 #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64)
39 #define CODE_GEN_HASH_BITS 15
40 #define CODE_GEN_HASH_SIZE (1 << CODE_GEN_HASH_BITS)
42 typedef struct TranslationBlock
{
43 unsigned long pc
; /* simulated PC corresponding to this block (EIP + CS base) */
44 unsigned long cs_base
; /* CS base for this block */
45 unsigned int flags
; /* flags defining in which context the code was generated */
46 uint8_t *tc_ptr
; /* pointer to the translated code */
47 struct TranslationBlock
*hash_next
; /* next matching block */
50 TranslationBlock tbs
[CODE_GEN_MAX_BLOCKS
];
51 TranslationBlock
*tb_hash
[CODE_GEN_HASH_SIZE
];
54 uint8_t code_gen_buffer
[CODE_GEN_BUFFER_SIZE
];
55 uint8_t *code_gen_ptr
;
60 static inline int testandset (int *p
)
63 __asm__
__volatile__ (
71 : "r" (p
), "r" (1), "r" (0)
78 static inline int testandset (int *p
)
83 __asm__
__volatile__ ("lock; cmpxchgl %3, %1; sete %0"
84 : "=q" (ret
), "=m" (*p
), "=a" (readval
)
85 : "r" (1), "m" (*p
), "a" (0)
92 static inline int testandset (int *p
)
96 __asm__
__volatile__ ("0: cs %0,%1,0(%2)\n"
99 : "r" (1), "a" (p
), "0" (*p
)
105 int global_cpu_lock
= 0;
109 while (testandset(&global_cpu_lock
));
112 void cpu_unlock(void)
117 /* exception support */
118 /* NOTE: not static to force relocation generation by GCC */
119 void raise_exception(int exception_index
)
121 /* NOTE: the register at this point must be saved by hand because
122 longjmp restore them */
124 env
->regs
[R_EAX
] = EAX
;
127 env
->regs
[R_ECX
] = ECX
;
130 env
->regs
[R_EDX
] = EDX
;
133 env
->regs
[R_EBX
] = EBX
;
136 env
->regs
[R_ESP
] = ESP
;
139 env
->regs
[R_EBP
] = EBP
;
142 env
->regs
[R_ESI
] = ESI
;
145 env
->regs
[R_EDI
] = EDI
;
147 env
->exception_index
= exception_index
;
148 longjmp(env
->jmp_env
, 1);
151 #if defined(DEBUG_EXEC)
152 static const char *cc_op_str
[] = {
185 static void cpu_x86_dump_state(FILE *f
)
188 eflags
= cc_table
[CC_OP
].compute_all();
189 eflags
|= (DF
& DF_MASK
);
191 "EAX=%08x EBX=%08X ECX=%08x EDX=%08x\n"
192 "ESI=%08x EDI=%08X EBP=%08x ESP=%08x\n"
193 "CCS=%08x CCD=%08x CCO=%-8s EFL=%c%c%c%c%c%c%c\n"
195 env
->regs
[R_EAX
], env
->regs
[R_EBX
], env
->regs
[R_ECX
], env
->regs
[R_EDX
],
196 env
->regs
[R_ESI
], env
->regs
[R_EDI
], env
->regs
[R_EBP
], env
->regs
[R_ESP
],
197 env
->cc_src
, env
->cc_dst
, cc_op_str
[env
->cc_op
],
198 eflags
& DF_MASK
? 'D' : '-',
199 eflags
& CC_O
? 'O' : '-',
200 eflags
& CC_S
? 'S' : '-',
201 eflags
& CC_Z
? 'Z' : '-',
202 eflags
& CC_A
? 'A' : '-',
203 eflags
& CC_P
? 'P' : '-',
204 eflags
& CC_C
? 'C' : '-',
207 fprintf(f
, "ST0=%f ST1=%f ST2=%f ST3=%f\n",
208 (double)ST0
, (double)ST1
, (double)ST(2), (double)ST(3));
214 void cpu_x86_tblocks_init(void)
217 code_gen_ptr
= code_gen_buffer
;
221 /* flush all the translation blocks */
222 static void tb_flush(void)
226 printf("gemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
227 code_gen_ptr
- code_gen_buffer
,
229 (code_gen_ptr
- code_gen_buffer
) / nb_tbs
);
232 for(i
= 0;i
< CODE_GEN_HASH_SIZE
; i
++)
234 code_gen_ptr
= code_gen_buffer
;
235 /* XXX: flush processor icache at this point */
238 /* find a translation block in the translation cache. If not found,
239 return NULL and the pointer to the last element of the list in pptb */
240 static inline TranslationBlock
*tb_find(TranslationBlock
***pptb
,
242 unsigned long cs_base
,
245 TranslationBlock
**ptb
, *tb
;
248 h
= pc
& (CODE_GEN_HASH_SIZE
- 1);
254 if (tb
->pc
== pc
&& tb
->cs_base
== cs_base
&& tb
->flags
== flags
)
256 ptb
= &tb
->hash_next
;
262 /* allocate a new translation block. flush the translation buffer if
263 too many translation blocks or too much generated code */
264 static inline TranslationBlock
*tb_alloc(void)
266 TranslationBlock
*tb
;
267 if (nb_tbs
>= CODE_GEN_MAX_BLOCKS
||
268 (code_gen_ptr
- code_gen_buffer
) >= CODE_GEN_BUFFER_MAX_SIZE
)
274 int cpu_x86_exec(CPUX86State
*env1
)
276 int saved_T0
, saved_T1
, saved_A0
;
277 CPUX86State
*saved_env
;
302 int code_gen_size
, ret
;
303 void (*gen_func
)(void);
304 TranslationBlock
*tb
, **ptb
;
305 uint8_t *tc_ptr
, *cs_base
, *pc
;
308 /* first we save global registers */
316 EAX
= env
->regs
[R_EAX
];
320 ECX
= env
->regs
[R_ECX
];
324 EDX
= env
->regs
[R_EDX
];
328 EBX
= env
->regs
[R_EBX
];
332 ESP
= env
->regs
[R_ESP
];
336 EBP
= env
->regs
[R_EBP
];
340 ESI
= env
->regs
[R_ESI
];
344 EDI
= env
->regs
[R_EDI
];
347 /* put eflags in CPU temporary format */
348 CC_SRC
= env
->eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
349 DF
= 1 - (2 * ((env
->eflags
>> 10) & 1));
350 CC_OP
= CC_OP_EFLAGS
;
351 env
->eflags
&= ~(DF_MASK
| CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
352 env
->interrupt_request
= 0;
354 /* prepare setjmp context for exception handling */
355 if (setjmp(env
->jmp_env
) == 0) {
357 if (env
->interrupt_request
) {
358 raise_exception(EXCP_INTERRUPT
);
362 cpu_x86_dump_state(logfile
);
365 /* we compute the CPU state. We assume it will not
366 change during the whole generated block. */
367 flags
= env
->seg_cache
[R_CS
].seg_32bit
<< GEN_FLAG_CODE32_SHIFT
;
368 flags
|= env
->seg_cache
[R_SS
].seg_32bit
<< GEN_FLAG_SS32_SHIFT
;
369 flags
|= (((unsigned long)env
->seg_cache
[R_DS
].base
|
370 (unsigned long)env
->seg_cache
[R_ES
].base
|
371 (unsigned long)env
->seg_cache
[R_SS
].base
) != 0) <<
372 GEN_FLAG_ADDSEG_SHIFT
;
373 flags
|= (env
->eflags
& VM_MASK
) >> (17 - GEN_FLAG_VM_SHIFT
);
374 cs_base
= env
->seg_cache
[R_CS
].base
;
375 pc
= cs_base
+ env
->eip
;
376 tb
= tb_find(&ptb
, (unsigned long)pc
, (unsigned long)cs_base
,
379 /* if no translated code available, then translate it now */
380 /* XXX: very inefficient: we lock all the cpus when
383 tc_ptr
= code_gen_ptr
;
384 ret
= cpu_x86_gen_code(code_gen_ptr
, CODE_GEN_MAX_SIZE
,
385 &code_gen_size
, pc
, cs_base
, flags
);
386 /* if invalid instruction, signal it */
389 raise_exception(EXCP06_ILLOP
);
393 tb
->pc
= (unsigned long)pc
;
394 tb
->cs_base
= (unsigned long)cs_base
;
397 tb
->hash_next
= NULL
;
398 code_gen_ptr
= (void *)(((unsigned long)code_gen_ptr
+ code_gen_size
+ CODE_GEN_ALIGN
- 1) & ~(CODE_GEN_ALIGN
- 1));
402 fprintf(logfile
, "Trace 0x%08lx [0x%08lx] %s\n",
403 (long)tb
->tc_ptr
, (long)tb
->pc
,
404 lookup_symbol((void *)tb
->pc
));
407 /* execute the generated code */
409 gen_func
= (void *)tc_ptr
;
413 ret
= env
->exception_index
;
415 /* restore flags in standard format */
416 env
->eflags
= env
->eflags
| cc_table
[CC_OP
].compute_all() | (DF
& DF_MASK
);
418 /* restore global registers */
450 void cpu_x86_interrupt(CPUX86State
*s
)
452 s
->interrupt_request
= 1;
456 void cpu_x86_load_seg(CPUX86State
*s
, int seg_reg
, int selector
)
458 CPUX86State
*saved_env
;
462 load_seg(seg_reg
, selector
);
476 #include <sys/ucontext.h>
478 static inline int handle_cpu_signal(unsigned long pc
,
482 printf("gemu: SIGSEGV pc=0x%08lx oldset=0x%08lx\n",
483 pc
, *(unsigned long *)old_set
);
485 if (pc
>= (unsigned long)code_gen_buffer
&&
486 pc
< (unsigned long)code_gen_buffer
+ CODE_GEN_BUFFER_SIZE
) {
487 /* the PC is inside the translated code. It means that we have
488 a virtual CPU fault */
489 /* we restore the process signal mask as the sigreturn should
491 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
492 /* XXX: need to compute virtual pc position by retranslating
493 code. The rest of the CPU state should be correct. */
494 raise_exception(EXCP0D_GPF
);
495 /* never comes here */
502 int cpu_x86_signal_handler(int host_signum
, struct siginfo
*info
,
505 #if defined(__i386__)
506 struct ucontext
*uc
= puc
;
514 pc
= uc
->uc_mcontext
.gregs
[REG_EIP
];
515 pold_set
= &uc
->uc_sigmask
;
516 return handle_cpu_signal(pc
, pold_set
);
518 #warning No CPU specific signal handler: cannot handle target SIGSEGV events