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"
24 //#define DEBUG_SIGNAL
26 /* main execution loop */
30 spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
34 spin_lock(&global_cpu_lock);
39 spin_unlock(&global_cpu_lock);
42 void cpu_loop_exit(void)
44 /* NOTE: the register at this point must be saved by hand because
45 longjmp restore them */
47 /* We have to stay in the same register window as our caller,
50 __asm__ __volatile__("restore\n\t"
54 env->regs[R_EAX] = EAX;
57 env->regs[R_ECX] = ECX;
60 env->regs[R_EDX] = EDX;
63 env->regs[R_EBX] = EBX;
66 env->regs[R_ESP] = ESP;
69 env->regs[R_EBP] = EBP;
72 env->regs[R_ESI] = ESI;
75 env->regs[R_EDI] = EDI;
77 longjmp(env->jmp_env, 1);
80 int cpu_x86_exec(CPUX86State *env1)
82 int saved_T0, saved_T1, saved_A0;
83 CPUX86State *saved_env;
108 int code_gen_size, ret;
109 void (*gen_func)(void);
110 TranslationBlock *tb, **ptb;
111 uint8_t *tc_ptr, *cs_base, *pc;
114 /* first we save global registers */
122 EAX = env->regs[R_EAX];
126 ECX = env->regs[R_ECX];
130 EDX = env->regs[R_EDX];
134 EBX = env->regs[R_EBX];
138 ESP = env->regs[R_ESP];
142 EBP = env->regs[R_EBP];
146 ESI = env->regs[R_ESI];
150 EDI = env->regs[R_EDI];
153 /* put eflags in CPU temporary format */
154 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
155 DF = 1 - (2 * ((env->eflags >> 10) & 1));
156 CC_OP = CC_OP_EFLAGS;
157 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
158 env->interrupt_request = 0;
160 /* prepare setjmp context for exception handling */
161 if (setjmp(env->jmp_env) == 0) {
162 T0 = 0; /* force lookup of first TB */
164 if (env->interrupt_request) {
165 env->exception_index = EXCP_INTERRUPT;
170 /* XXX: save all volatile state in cpu state */
171 /* restore flags in standard format */
172 env->regs[R_EAX] = EAX;
173 env->regs[R_EBX] = EBX;
174 env->regs[R_ECX] = ECX;
175 env->regs[R_EDX] = EDX;
176 env->regs[R_ESI] = ESI;
177 env->regs[R_EDI] = EDI;
178 env->regs[R_EBP] = EBP;
179 env->regs[R_ESP] = ESP;
180 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
181 cpu_x86_dump_state(env, logfile, 0);
182 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
185 /* we compute the CPU state. We assume it will not
186 change during the whole generated block. */
187 flags = env->seg_cache[R_CS].seg_32bit << GEN_FLAG_CODE32_SHIFT;
188 flags |= env->seg_cache[R_SS].seg_32bit << GEN_FLAG_SS32_SHIFT;
189 flags |= (((unsigned long)env->seg_cache[R_DS].base |
190 (unsigned long)env->seg_cache[R_ES].base |
191 (unsigned long)env->seg_cache[R_SS].base) != 0) <<
192 GEN_FLAG_ADDSEG_SHIFT;
193 if (!(env->eflags & VM_MASK)) {
194 flags |= (env->segs[R_CS] & 3) << GEN_FLAG_CPL_SHIFT;
196 /* NOTE: a dummy CPL is kept */
197 flags |= (1 << GEN_FLAG_VM_SHIFT);
198 flags |= (3 << GEN_FLAG_CPL_SHIFT);
200 flags |= (env->eflags & (IOPL_MASK | TF_MASK));
201 cs_base = env->seg_cache[R_CS].base;
202 pc = cs_base + env->eip;
203 tb = tb_find(&ptb, (unsigned long)pc, (unsigned long)cs_base,
207 /* if no translated code available, then translate it now */
208 tb = tb_alloc((unsigned long)pc);
210 /* flush must be done */
212 /* cannot fail at this point */
213 tb = tb_alloc((unsigned long)pc);
214 /* don't forget to invalidate previous TB info */
215 ptb = &tb_hash[tb_hash_func((unsigned long)pc)];
218 tc_ptr = code_gen_ptr;
220 tb->cs_base = (unsigned long)cs_base;
222 ret = cpu_x86_gen_code(tb, CODE_GEN_MAX_SIZE, &code_gen_size);
223 /* if invalid instruction, signal it */
225 /* NOTE: the tb is allocated but not linked, so we
227 spin_unlock(&tb_lock);
228 raise_exception(EXCP06_ILLOP);
231 tb->hash_next = NULL;
233 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
234 spin_unlock(&tb_lock);
238 fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n",
239 (long)tb->tc_ptr, (long)tb->pc,
240 lookup_symbol((void *)tb->pc));
243 /* see if we can patch the calling TB */
244 if (T0 != 0 && !(env->eflags & TF_MASK)) {
246 tb_add_jump((TranslationBlock *)(T0 & ~3), T0 & 3, tb);
247 spin_unlock(&tb_lock);
252 /* execute the generated code */
253 gen_func = (void *)tc_ptr;
255 __asm__ __volatile__("call %0\n\t"
259 : "i0", "i1", "i2", "i3", "i4", "i5");
265 ret = env->exception_index;
267 /* restore flags in standard format */
268 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
270 /* restore global registers */
302 void cpu_x86_interrupt(CPUX86State *s)
304 s->interrupt_request = 1;
308 void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
310 CPUX86State *saved_env;
314 if (env->eflags & VM_MASK) {
317 sc = &env->seg_cache[seg_reg];
318 /* NOTE: in VM86 mode, limit and seg_32bit are never reloaded,
319 so we must load them here */
320 sc->base = (void *)(selector << 4);
323 env->segs[seg_reg] = selector;
325 load_seg(seg_reg, selector, 0);
330 void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32)
332 CPUX86State *saved_env;
337 helper_fsave(ptr, data32);
342 void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32)
344 CPUX86State *saved_env;
349 helper_frstor(ptr, data32);
364 #include <sys/ucontext.h>
366 /* 'pc' is the host PC at which the exception was raised. 'address' is
367 the effective address of the memory exception. 'is_write' is 1 if a
368 write caused the exception and otherwise 0'. 'old_set' is the
369 signal set which should be restored */
370 static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
371 int is_write, sigset_t *old_set)
373 TranslationBlock *tb;
377 #if defined(DEBUG_SIGNAL)
378 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx wr=%d oldset=0x%08lx\n",
379 pc, address, is_write, *(unsigned long *)old_set);
381 /* XXX: locking issue */
382 if (is_write && page_unprotect(address)) {
387 /* the PC is inside the translated code. It means that we have
388 a virtual CPU fault */
389 ret = cpu_x86_search_pc(tb, &found_pc, pc);
392 env->eip = found_pc - tb->cs_base;
394 /* we restore the process signal mask as the sigreturn should
395 do it (XXX: use sigsetjmp) */
396 sigprocmask(SIG_SETMASK, old_set, NULL);
397 raise_exception_err(EXCP0E_PAGE, 4 | (is_write << 1));
398 /* never comes here */
405 #if defined(__i386__)
407 int cpu_x86_signal_handler(int host_signum, struct siginfo *info,
410 struct ucontext *uc = puc;
417 #define REG_TRAPNO TRAPNO
419 pc = uc->uc_mcontext.gregs[REG_EIP];
420 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
421 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
422 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
426 #elif defined(__powerpc)
428 int cpu_x86_signal_handler(int host_signum, struct siginfo *info,
431 struct ucontext *uc = puc;
432 struct pt_regs *regs = uc->uc_mcontext.regs;
440 if (regs->dsisr & 0x00800000)
443 if (regs->trap != 0x400 && (regs->dsisr & 0x02000000))
446 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
447 is_write, &uc->uc_sigmask);
450 #elif defined(__alpha__)
452 int cpu_x86_signal_handler(int host_signum, struct siginfo *info,
455 struct ucontext *uc = puc;
456 uint32_t *pc = uc->uc_mcontext.sc_pc;
460 switch (insn >> 26) {
475 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
476 is_write, &uc->uc_sigmask);
480 #error CPU specific signal handler needed