translate-copy.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/*
* i386 on i386 translation
*
* Copyright (c) 2003 Fabrice Bellard
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "cpu.h"
#include "exec-all.h"
#include "disas.h"
#ifdef USE_CODE_COPY
#include <signal.h>
#include <sys/ucontext.h>
extern char exec_loop;
/* operand size */
enum {
OT_BYTE = 0,
};
#define PREFIX_REPZ 0x01
#define PREFIX_REPNZ 0x02
#define PREFIX_LOCK 0x04
#define PREFIX_DATA 0x08
#define PREFIX_ADR 0x10
typedef struct DisasContext {
/* current insn context */
int override; /* -1 if no override */
int prefix;
int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
static state change (stop translation) */
/* code output */
/* current block context */
int pe; /* protected mode */
int code32; /* 32 bit code segment */
int f_st; /* currently unused */
int vm86; /* vm86 mode */
int cpl;
int iopl;
int flags;
struct TranslationBlock *tb;
} DisasContext;
{
*s->gen_code_ptr++ = val;
}
{
*s->gen_code_ptr++ = val;
}
{
*s->gen_code_ptr++ = val;
}
{
}
static inline void gen_movl_addr_im(DisasContext *s,
{
gb(s, 0xc7);
gb(s, 0x05);
}
static inline void gen_movw_addr_im(DisasContext *s,
{
gb(s, 0x66);
gb(s, 0xc7);
gb(s, 0x05);
}
{
gl(s, 0);
s->is_jmp = 1;
}
{
gl(s, 0);
gl(s, 0);
s->is_jmp = 1;
}
static void gen_eob(DisasContext *s)
{
s->is_jmp = 1;
}
{
int havesib;
int index;
int scale;
if (s->aflag) {
havesib = 0;
index = 0;
scale = 0;
if (base == 4) {
havesib = 1;
}
switch (mod) {
case 0:
if (base == 5) {
base = -1;
s->pc += 4;
} else {
disp = 0;
}
break;
case 1:
break;
default:
case 2:
s->pc += 4;
break;
}
} else {
switch (mod) {
case 0:
if (rm == 6) {
s->pc += 2;
} else {
disp = 0;
}
break;
case 1:
break;
default:
case 2:
s->pc += 2;
break;
}
}
}
{
gen_lea_modrm(s, modrm);
}
{
switch(ot) {
case OT_BYTE:
s->pc++;
break;
case OT_WORD:
s->pc += 2;
break;
default:
case OT_LONG:
s->pc += 4;
break;
}
return ret;
}
/* convert one instruction. s->is_jmp is set if the translation must
be stopped. */
static int disas_insn(DisasContext *s)
{
int ot;
prefixes = 0;
s->override = -1;
s->pc++;
/* check prefixes */
switch (b) {
case 0xf3:
prefixes |= PREFIX_REPZ;
goto next_byte;
case 0xf2:
prefixes |= PREFIX_REPNZ;
goto next_byte;
case 0xf0:
prefixes |= PREFIX_LOCK;
goto next_byte;
case 0x2e:
goto next_byte;
case 0x36:
goto next_byte;
case 0x3e:
goto next_byte;
case 0x26:
goto next_byte;
case 0x64:
goto next_byte;
case 0x65:
goto next_byte;
case 0x66:
prefixes |= PREFIX_DATA;
goto next_byte;
case 0x67:
prefixes |= PREFIX_ADR;
goto next_byte;
}
if (prefixes & PREFIX_DATA)
dflag ^= 1;
if (prefixes & PREFIX_ADR)
aflag ^= 1;
/* lock generation */
if (prefixes & PREFIX_LOCK)
goto unsupported_op;
goto unsupported_op;
/* now check op code */
switch(b) {
case 0x0f:
/**************************/
/* extended op code */
goto reswitch;
/**************************/
/* arith & logic */
case 0x00 ... 0x05:
case 0x08 ... 0x0d:
case 0x10 ... 0x15:
case 0x18 ... 0x1d:
case 0x20 ... 0x25:
case 0x28 ... 0x2d:
case 0x30 ... 0x35:
case 0x38 ... 0x3d:
{
int f;
f = (b >> 1) & 3;
if ((b & 1) == 0)
else
switch(f) {
case 0: /* OP Ev, Gv */
parse_modrm(s, modrm);
break;
case 1: /* OP Gv, Ev */
parse_modrm(s, modrm);
break;
case 2: /* OP A, Iv */
break;
}
}
break;
case 0x80: /* GRP1 */
case 0x81:
case 0x82:
case 0x83:
{
if ((b & 1) == 0)
else
parse_modrm(s, modrm);
switch(b) {
default:
case 0x80:
case 0x81:
case 0x82:
break;
case 0x83:
break;
}
}
break;
/**************************/
/* inc, dec, and other misc arith */
case 0x40 ... 0x47: /* inc Gv */
break;
case 0x48 ... 0x4f: /* dec Gv */
break;
case 0xf6: /* GRP3 */
case 0xf7:
if ((b & 1) == 0)
else
parse_modrm(s, modrm);
switch(op) {
case 0: /* test */
break;
case 2: /* not */
break;
case 3: /* neg */
break;
case 4: /* mul */
break;
case 5: /* imul */
break;
case 6: /* div */
break;
case 7: /* idiv */
break;
default:
goto illegal_op;
}
break;
case 0xfe: /* GRP4 */
case 0xff: /* GRP5 */
if ((b & 1) == 0)
else
goto illegal_op;
}
parse_modrm(s, modrm);
switch(op) {
case 0: /* inc Ev */
break;
case 1: /* dec Ev */
break;
case 2: /* call Ev */
/* XXX: optimize and handle MEM exceptions specifically
fs movl %eax, regs[0]
movl Ev, %eax
pushl next_eip
fs movl %eax, eip
*/
goto unsupported_op;
case 3: /* lcall Ev */
goto unsupported_op;
case 4: /* jmp Ev */
/* XXX: optimize and handle MEM exceptions specifically
fs movl %eax, regs[0]
movl Ev, %eax
fs movl %eax, eip
*/
goto unsupported_op;
case 5: /* ljmp Ev */
goto unsupported_op;
case 6: /* push Ev */
break;
default:
goto illegal_op;
}
break;
case 0xa8: /* test eAX, Iv */
case 0xa9:
if ((b & 1) == 0)
else
break;
break;
break;
case 0x1af: /* imul Gv, Ev */
case 0x69: /* imul Gv, Ev, I */
case 0x6b:
parse_modrm(s, modrm);
if (b == 0x69) {
} else if (b == 0x6b) {
} else {
}
break;
case 0x84: /* test Ev, Gv */
case 0x85:
case 0x1c0:
case 0x1c1: /* xadd Ev, Gv */
case 0x1b0:
case 0x1b1: /* cmpxchg Ev, Gv */
case 0x8f: /* pop Ev */
case 0x88:
case 0x89: /* mov Gv, Ev */
case 0x8a:
case 0x8b: /* mov Ev, Gv */
case 0x1b6: /* movzbS Gv, Eb */
case 0x1b7: /* movzwS Gv, Eb */
case 0x1be: /* movsbS Gv, Eb */
case 0x1bf: /* movswS Gv, Eb */
case 0x86:
case 0x87: /* xchg Ev, Gv */
case 0xd0:
case 0xd1: /* shift Ev,1 */
case 0xd2:
case 0xd3: /* shift Ev,cl */
case 0x1a5: /* shld cl */
case 0x1ad: /* shrd cl */
case 0x190 ... 0x19f: /* setcc Gv */
/* XXX: emulate cmov if not available ? */
case 0x140 ... 0x14f: /* cmov Gv, Ev */
case 0x1a3: /* bt Gv, Ev */
case 0x1ab: /* bts */
case 0x1b3: /* btr */
case 0x1bb: /* btc */
case 0x1bc: /* bsf */
case 0x1bd: /* bsr */
parse_modrm(s, modrm);
break;
case 0x1c7: /* cmpxchg8b */
if (mod == 3)
goto illegal_op;
parse_modrm(s, modrm);
break;
/**************************/
case 0x50 ... 0x57: /* push */
case 0x58 ... 0x5f: /* pop */
case 0x60: /* pusha */
case 0x61: /* popa */
break;
case 0x68: /* push Iv */
case 0x6a:
if (b == 0x68)
else
break;
case 0xc8: /* enter */
s->pc += 2;
break;
case 0xc9: /* leave */
break;
case 0x06: /* push es */
case 0x0e: /* push cs */
case 0x16: /* push ss */
case 0x1e: /* push ds */
/* XXX: optimize:
push segs[n].selector
*/
goto unsupported_op;
case 0x1a0: /* push fs */
case 0x1a8: /* push gs */
goto unsupported_op;
case 0x07: /* pop es */
case 0x17: /* pop ss */
case 0x1f: /* pop ds */
goto unsupported_op;
case 0x1a1: /* pop fs */
case 0x1a9: /* pop gs */
goto unsupported_op;
case 0x8e: /* mov seg, Gv */
/* XXX: optimize:
fs movl r, regs[]
movl segs[].selector, r
mov r, Gv
fs movl regs[], r
*/
goto unsupported_op;
case 0x8c: /* mov Gv, seg */
goto unsupported_op;
case 0xc4: /* les Gv */
goto do_lxx;
case 0xc5: /* lds Gv */
goto do_lxx;
case 0x1b2: /* lss Gv */
goto do_lxx;
case 0x1b4: /* lfs Gv */
goto do_lxx;
case 0x1b5: /* lgs Gv */
goto unsupported_op;
/************************/
/* floats */
case 0xd8 ... 0xdf:
#if 1
/* currently not stable enough */
goto unsupported_op;
#else
goto unsupported_op;
#endif
#if 0
/* for testing FPU context switch */
{
static int count;
if (count != 0)
goto unsupported_op;
}
#endif
if (mod != 3) {
/* memory op */
parse_modrm(s, modrm);
switch(op) {
case 0x00 ... 0x07: /* fxxxs */
case 0x10 ... 0x17: /* fixxxl */
case 0x20 ... 0x27: /* fxxxl */
case 0x30 ... 0x37: /* fixxx */
break;
case 0x08: /* flds */
case 0x0a: /* fsts */
case 0x0b: /* fstps */
case 0x18: /* fildl */
case 0x1a: /* fistl */
case 0x1b: /* fistpl */
case 0x28: /* fldl */
case 0x2a: /* fstl */
case 0x2b: /* fstpl */
case 0x38: /* filds */
case 0x3a: /* fists */
case 0x3b: /* fistps */
case 0x0c: /* fldenv mem */
case 0x0d: /* fldcw mem */
case 0x0e: /* fnstenv mem */
case 0x0f: /* fnstcw mem */
case 0x1d: /* fldt mem */
case 0x1f: /* fstpt mem */
case 0x2c: /* frstor mem */
case 0x2e: /* fnsave mem */
case 0x2f: /* fnstsw mem */
case 0x3c: /* fbld */
case 0x3e: /* fbstp */
case 0x3d: /* fildll */
case 0x3f: /* fistpll */
break;
default:
goto illegal_op;
}
} else {
/* register float ops */
switch(op) {
case 0x08: /* fld sti */
case 0x09: /* fxchg sti */
break;
case 0x0a: /* grp d9/2 */
switch(rm) {
case 0: /* fnop */
break;
default:
goto illegal_op;
}
break;
case 0x0c: /* grp d9/4 */
switch(rm) {
case 0: /* fchs */
case 1: /* fabs */
case 4: /* ftst */
case 5: /* fxam */
break;
default:
goto illegal_op;
}
break;
case 0x0d: /* grp d9/5 */
switch(rm) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
break;
default:
goto illegal_op;
}
break;
case 0x0e: /* grp d9/6 */
break;
case 0x0f: /* grp d9/7 */
break;
case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
break;
case 0x02: /* fcom */
break;
case 0x03: /* fcomp */
break;
case 0x15: /* da/5 */
switch(rm) {
case 1: /* fucompp */
break;
default:
goto illegal_op;
}
break;
case 0x1c:
switch(rm) {
case 0: /* feni (287 only, just do nop here) */
case 1: /* fdisi (287 only, just do nop here) */
goto unsupported_op;
case 2: /* fclex */
case 3: /* fninit */
case 4: /* fsetpm (287 only, just do nop here) */
break;
default:
goto illegal_op;
}
break;
case 0x1d: /* fucomi */
break;
case 0x1e: /* fcomi */
break;
case 0x28: /* ffree sti */
break;
case 0x2a: /* fst sti */
break;
case 0x2b: /* fstp sti */
break;
case 0x2c: /* fucom st(i) */
break;
case 0x2d: /* fucomp st(i) */
break;
case 0x33: /* de/3 */
switch(rm) {
case 1: /* fcompp */
break;
default:
goto illegal_op;
}
break;
case 0x3c: /* df/4 */
switch(rm) {
case 0:
break;
default:
goto illegal_op;
}
break;
case 0x3d: /* fucomip */
break;
case 0x3e: /* fcomip */
break;
case 0x10 ... 0x13: /* fcmovxx */
case 0x18 ... 0x1b:
break;
default:
goto illegal_op;
}
}
break;
/**************************/
/* mov */
case 0xc6:
case 0xc7: /* mov Ev, Iv */
if ((b & 1) == 0)
else
parse_modrm(s, modrm);
break;
case 0x8d: /* lea */
if (mod == 3)
goto illegal_op;
parse_modrm(s, modrm);
break;
case 0xa0: /* mov EAX, Ov */
case 0xa1:
case 0xa2: /* mov Ov, EAX */
case 0xa3:
if ((b & 1) == 0)
else
if (s->aflag)
else
break;
case 0xd7: /* xlat */
break;
case 0xb0 ... 0xb7: /* mov R, Ib */
break;
case 0xb8 ... 0xbf: /* mov R, Iv */
break;
case 0x91 ... 0x97: /* xchg R, EAX */
break;
/************************/
/* shifts */
case 0xc0:
case 0xc1: /* shift Ev,imm */
case 0x1a4: /* shld imm */
case 0x1ac: /* shrd imm */
parse_modrm(s, modrm);
break;
/************************/
/* string ops */
case 0xa4: /* movsS */
case 0xa5:
break;
case 0xaa: /* stosS */
case 0xab:
break;
case 0xac: /* lodsS */
case 0xad:
break;
case 0xae: /* scasS */
case 0xaf:
break;
case 0xa6: /* cmpsS */
case 0xa7:
break;
case 0x6c: /* insS */
case 0x6d:
goto unsupported_op;
case 0x6e: /* outsS */
case 0x6f:
goto unsupported_op;
/************************/
/* port I/O */
case 0xe4:
case 0xe5:
goto unsupported_op;
case 0xe6:
case 0xe7:
goto unsupported_op;
case 0xec:
case 0xed:
goto unsupported_op;
case 0xee:
case 0xef:
goto unsupported_op;
/************************/
/* control */
#if 0
case 0xc2: /* ret im */
s->pc += 2;
gen_pop_T0(s);
if (s->dflag == 0)
gen_eob(s);
break;
#endif
case 0xc3: /* ret */
if (!s->dflag)
gb(s, 0x05);
if (!s->dflag) {
/* reset high bits of EIP */
}
gen_eob(s);
goto no_copy;
case 0xca: /* lret im */
case 0xcb: /* lret */
case 0xcf: /* iret */
case 0x9a: /* lcall im */
case 0xea: /* ljmp im */
goto unsupported_op;
case 0xe8: /* call im */
if (s->dflag) {
} else {
gb(s, 0x68);
val &= 0xffff;
}
goto no_copy;
case 0xe9: /* jmp */
if (s->dflag == 0)
goto no_copy;
case 0xeb: /* jmp Jb */
if (s->dflag == 0)
goto no_copy;
case 0x70 ... 0x7f: /* jcc Jb */
goto do_jcc;
case 0x180 ... 0x18f: /* jcc Jv */
if (dflag) {
} else {
}
if (s->dflag == 0)
val &= 0xffff;
goto no_copy;
/************************/
/* flags */
case 0x9c: /* pushf */
/* XXX: put specific code ? */
goto unsupported_op;
case 0x9d: /* popf */
goto unsupported_op;
case 0x9e: /* sahf */
case 0x9f: /* lahf */
case 0xf5: /* cmc */
case 0xf8: /* clc */
case 0xf9: /* stc */
case 0xfc: /* cld */
case 0xfd: /* std */
break;
/************************/
/* bit operations */
parse_modrm(s, modrm);
/* load shift */
if (op < 4)
goto illegal_op;
break;
/************************/
/* bcd */
case 0x27: /* daa */
break;
case 0x2f: /* das */
break;
case 0x37: /* aaa */
break;
case 0x3f: /* aas */
break;
case 0xd4: /* aam */
break;
case 0xd5: /* aad */
break;
/************************/
/* misc */
case 0x90: /* nop */
break;
case 0x9b: /* fwait */
(HF_MP_MASK | HF_TS_MASK)) {
goto unsupported_op;
}
break;
case 0xcc: /* int3 */
goto unsupported_op;
case 0xcd: /* int N */
goto unsupported_op;
case 0xce: /* into */
goto unsupported_op;
case 0xf1: /* icebp (undocumented, exits to external debugger) */
goto unsupported_op;
case 0xfa: /* cli */
goto unsupported_op;
case 0xfb: /* sti */
goto unsupported_op;
case 0x62: /* bound */
if (mod == 3)
goto illegal_op;
parse_modrm(s, modrm);
break;
case 0x1c8 ... 0x1cf: /* bswap reg */
break;
case 0xd6: /* salc */
break;
case 0xe0: /* loopnz */
case 0xe1: /* loopz */
case 0xe2: /* loop */
case 0xe3: /* jecxz */
goto unsupported_op;
case 0x130: /* wrmsr */
case 0x132: /* rdmsr */
goto unsupported_op;
case 0x131: /* rdtsc */
goto unsupported_op;
case 0x1a2: /* cpuid */
goto unsupported_op;
case 0xf4: /* hlt */
goto unsupported_op;
case 0x100:
goto unsupported_op;
case 0x101:
goto unsupported_op;
case 0x108: /* invd */
case 0x109: /* wbinvd */
goto unsupported_op;
case 0x63: /* arpl */
goto unsupported_op;
case 0x102: /* lar */
case 0x103: /* lsl */
goto unsupported_op;
case 0x118:
goto unsupported_op;
case 0x120: /* mov reg, crN */
case 0x122: /* mov crN, reg */
goto unsupported_op;
case 0x121: /* mov reg, drN */
case 0x123: /* mov drN, reg */
goto unsupported_op;
case 0x106: /* clts */
goto unsupported_op;
default:
goto illegal_op;
}
/* just copy the code */
/* no override yet */
if (!s->dflag)
gb(s, 0x66);
if (!s->aflag)
gb(s, 0x67);
if (prefixes & PREFIX_REPZ)
gb(s, 0xf3);
else if (prefixes & PREFIX_REPNZ)
gb(s, 0xf2);
{
int len, i;
for(i = 0; i < len; i++) {
}
}
return 0;
/* fall back to slower code gen necessary */
return -1;
}
#define GEN_CODE_MAX_SIZE 8192
#define GEN_CODE_MAX_INSN_SIZE 512
int *gen_code_size_ptr,
int search_pc,
{
if (env->nb_breakpoints > 0 ||
return -1;
return -1;
if (!(flags & HF_SS32_MASK))
return -1;
return -1;
/* generate intermediate code */
for(;;) {
if (ret < 0) {
/* unsupported insn */
/* if first instruction, signal that no copying was done */
return -1;
} else {
}
}
if (search_pc) {
/* search pc mode */
return 0;
}
}
/* stop translation if indicated */
break;
/* if too long translation, stop generation */
break;
}
}
#ifdef DEBUG_DISAS
if (loglevel & CPU_LOG_TB_IN_ASM) {
}
#endif
if (!search_pc) {
return 0;
} else {
return -1;
}
}
/* generate code by just copying data. Return -1 if cannot generate
any code. Return 0 if code was generated */
int max_code_size, int *gen_code_size_ptr)
{
/* generate machine code */
#ifdef USE_DIRECT_JUMP
/* the following two entries are optional (only used for string ops) */
#endif
0, NULL);
}
void *puc)
{
/* find opc index corresponding to search_pc */
return -1;
if (ret < 0)
return ret;
/* restore all the CPU state from the CPU context from the
signal. The FPU context stays in the host CPU. */
return 0;
}
#endif /* USE_CODE_COPY */