From mboxrd@z Thu Jan 1 00:00:00 1970 From: Laurent Vivier Subject: [PATCH 3/5] extract prefix decoding part from x86_emulate_memop() to x86_decode_prefix() Date: Wed, 01 Aug 2007 11:16:06 +0200 Message-ID: <46B04F56.60607@bull.net> References: <46B04CCA.2010503@bull.net> <46B04DD6.7010702@bull.net> <46B04EB9.5010103@bull.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0667982394==" To: kvm-devel Return-path: In-Reply-To: <46B04EB9.5010103-6ktuUTfB/bM@public.gmane.org> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: kvm-devel-bounces-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org Errors-To: kvm-devel-bounces-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org List-Id: kvm.vger.kernel.org This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --===============0667982394== Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig037D7BAE7A5BBCF1074A8961" This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig037D7BAE7A5BBCF1074A8961 Content-Type: multipart/mixed; boundary="------------070707000908080704050401" This is a multi-part message in MIME format. --------------070707000908080704050401 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Extract prefix decoding part from x86_emulate_memop() to x86_decode_prefi= x(). Signed-off-by: Laurent Vivier --=20 ------------- Laurent.Vivier-6ktuUTfB/bM@public.gmane.org -------------- "Software is hard" - Donald Knuth --------------070707000908080704050401 Content-Type: text/plain; name="x86_decode_prefix" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline; filename="x86_decode_prefix" Index: kvm/drivers/kvm/x86_emulate.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/x86_emulate.c 2007-07-31 17:44:35.000000000 +020= 0 +++ kvm/drivers/kvm/x86_emulate.c 2007-07-31 18:23:39.000000000 +0200 @@ -480,43 +480,24 @@ } =20 int -x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops = *ops) +x86_decode_prefix(int mode, u8 *inst, struct x86_prefix *prefix) { - unsigned d; - u8 b, sib, twobyte =3D 0; - u8 modrm, modrm_mod =3D 0; - unsigned int i; - int rc =3D 0; - struct operand src, dst; - unsigned long cr2 =3D ctxt->cr2; - int mode =3D ctxt->mode; - unsigned long modrm_ea; - int use_modrm_ea, scale, rip_relative =3D 0; - int no_wb =3D 0; - u64 msr_data; - struct x86_prefix prefix; - - /* Shadow copy of register state. Committed on successful emulation. */= - unsigned long _regs[NR_VCPU_REGS]; - unsigned long _eip =3D ctxt->vcpu->rip, _eflags =3D ctxt->eflags; - unsigned long modrm_val =3D 0; - - memcpy(_regs, ctxt->vcpu->regs, sizeof _regs); - memset(&prefix, 0, sizeof(prefix)); - prefix.override_base =3D -1; + unsigned int op_bytes, ad_bytes; + int i; + u8 b; =20 switch (mode) { case X86EMUL_MODE_REAL: case X86EMUL_MODE_PROT16: - prefix.op_bytes =3D prefix.ad_bytes =3D 2; + op_bytes =3D ad_bytes =3D 2; break; case X86EMUL_MODE_PROT32: - prefix.op_bytes =3D prefix.ad_bytes =3D 4; + op_bytes =3D ad_bytes =3D 4; break; #ifdef CONFIG_X86_64 case X86EMUL_MODE_PROT64: - prefix.op_bytes =3D 4; - prefix.ad_bytes =3D 8; + op_bytes =3D 4; + ad_bytes =3D 8; break; #endif default: @@ -525,39 +506,39 @@ =20 /* Legacy prefixes. */ for (i =3D 0; i < 8; i++) { - switch (b =3D insn_fetch(u8, 1, _eip)) { + switch (b =3D inst[i]) { case 0x66: /* operand-size override */ - prefix.op_bytes ^=3D 6; /* switch between 2/4 bytes */ + op_bytes ^=3D 6; /* switch between 2/4 bytes */ break; case 0x67: /* address-size override */ if (mode =3D=3D X86EMUL_MODE_PROT64) - prefix.ad_bytes ^=3D 12; /* switch between 4/8 bytes */ + ad_bytes ^=3D 12; /* switch between 4/8 bytes */ else - prefix.ad_bytes ^=3D 6; /* switch between 2/4 bytes */ + ad_bytes ^=3D 6; /* switch between 2/4 bytes */ break; case 0x2e: /* CS override */ - prefix.override_base =3D X86EMUL_BASE_CS; + prefix->override_base =3D X86EMUL_BASE_CS; break; case 0x3e: /* DS override */ - prefix.override_base =3D X86EMUL_BASE_DS; + prefix->override_base =3D X86EMUL_BASE_DS; break; case 0x26: /* ES override */ - prefix.override_base =3D X86EMUL_BASE_ES; + prefix->override_base =3D X86EMUL_BASE_ES; break; case 0x64: /* FS override */ - prefix.override_base =3D X86EMUL_BASE_FS; + prefix->override_base =3D X86EMUL_BASE_FS; break; case 0x65: /* GS override */ - prefix.override_base =3D X86EMUL_BASE_GS; + prefix->override_base =3D X86EMUL_BASE_GS; break; case 0x36: /* SS override */ - prefix.override_base =3D X86EMUL_BASE_SS; + prefix->override_base =3D X86EMUL_BASE_SS; break; case 0xf0: /* LOCK */ - prefix.lock =3D 1; + prefix->lock =3D 1; break; case 0xf3: /* REP/REPE/REPZ */ - prefix.rep =3D 1; + prefix->rep =3D 1; break; case 0xf2: /* REPNE/REPNZ */ break; @@ -570,15 +551,62 @@ =20 /* REX prefix. */ if ((mode =3D=3D X86EMUL_MODE_PROT64) && ((b & 0xf0) =3D=3D 0x40)) { - prefix.rex =3D b; + prefix->rex =3D b; if (b & 8) - prefix.op_bytes =3D 8; /* REX.W */ - prefix.modrm_reg =3D (b & 4) << 1; /* REX.R */ - prefix.index_reg =3D (b & 2) << 2; /* REX.X */ - prefix.modrm_rm =3D prefix.base_reg =3D (b & 1) << 3; /* REG.B */ - b =3D insn_fetch(u8, 1, _eip); + op_bytes =3D 8; /* REX.W */ + prefix->modrm_reg =3D (b & 4) << 1; /* REX.R */ + prefix->index_reg =3D (b & 2) << 2; /* REX.X */ + prefix->modrm_rm =3D prefix->base_reg =3D (b & 1) << 3; /* REG.B */ + i++; } =20 + prefix->op_bytes =3D op_bytes; + prefix->ad_bytes =3D ad_bytes; + + return i; +} +EXPORT_SYMBOL_GPL(x86_decode_prefix); + +int +x86_emulate_memop(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops = *ops) +{ + unsigned d; + u8 b, sib, twobyte =3D 0; + u8 modrm, modrm_mod =3D 0; + int rc =3D 0; + struct operand src, dst; + unsigned long cr2 =3D ctxt->cr2; + int mode =3D ctxt->mode; + unsigned long modrm_ea; + int use_modrm_ea, scale, rip_relative =3D 0; + int no_wb =3D 0; + u64 msr_data, inst; + struct x86_prefix prefix; + int count; + + /* Shadow copy of register state. Committed on successful emulation. */= + unsigned long _regs[NR_VCPU_REGS]; + unsigned long _eip =3D ctxt->vcpu->rip, _eflags =3D ctxt->eflags; + unsigned long modrm_val =3D 0; + + memcpy(_regs, ctxt->vcpu->regs, sizeof _regs); + + /* decode prefixes */ + + rc =3D ops->read_std(_eip + ctxt->base[X86EMUL_BASE_CS], + &inst, sizeof(inst), ctxt->vcpu); + if ( rc !=3D 0 ) + goto done; + + memset(&prefix, 0, sizeof(prefix)); + prefix.override_base =3D -1; + + count =3D x86_decode_prefix(mode, (u8*)&inst, &prefix); + if (count =3D=3D -1) + return -1; + _eip +=3D count; + b =3D insn_fetch(u8, 1, _eip); + /* Opcode byte(s). */ d =3D opcode_table[b]; if (d =3D=3D 0) { Index: kvm/drivers/kvm/x86_emulate.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/x86_emulate.h 2007-07-31 18:00:51.000000000 +020= 0 +++ kvm/drivers/kvm/x86_emulate.h 2007-07-31 18:01:53.000000000 +0200 @@ -164,6 +164,13 @@ #endif =20 /* + * x86_decode_prefix: Decode instruction prefixes + * Returns -1 on failure, 0 on success. + */ +int +x86_decode_prefix(int mode, u8 *inst, struct x86_prefix *prefix); + +/* * x86_emulate_memop: Emulate an instruction that faulted attempting to * read/write a 'special' memory area. * Returns -1 on failure, 0 on success. --------------070707000908080704050401-- --------------enig037D7BAE7A5BBCF1074A8961 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.7 (GNU/Linux) iD8DBQFGsE9W9Kffa9pFVzwRArzGAKDHDo/uNaz50rpFUoYm+MYPSoowFwCgq/m+ BM2Z+ZN+FrG+bZ58p6oSjGA= =5lN0 -----END PGP SIGNATURE----- --------------enig037D7BAE7A5BBCF1074A8961-- --===============0667982394== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ --===============0667982394== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ kvm-devel mailing list kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/kvm-devel --===============0667982394==--