From patchwork Thu Aug 5 06:07:21 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yongjun X-Patchwork-Id: 117214 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o7569SmV016501 for ; Thu, 5 Aug 2010 06:09:28 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757993Ab0HEGJT (ORCPT ); Thu, 5 Aug 2010 02:09:19 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:64950 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1757624Ab0HEGJT (ORCPT ); Thu, 5 Aug 2010 02:09:19 -0400 Received: from tang.cn.fujitsu.com (tang.cn.fujitsu.com [10.167.250.3]) by song.cn.fujitsu.com (Postfix) with ESMTP id BF6EA170119; Thu, 5 Aug 2010 14:09:15 +0800 (CST) Received: from fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id o7566Ck9012082; Thu, 5 Aug 2010 14:06:13 +0800 Received: from [10.167.141.76] (unknown [10.167.141.76]) by fnst.cn.fujitsu.com (Postfix) with ESMTPA id 72A0C14C00E; Thu, 5 Aug 2010 14:10:00 +0800 (CST) Message-ID: <4C5A5519.9090400@cn.fujitsu.com> Date: Thu, 05 Aug 2010 14:07:21 +0800 From: Wei Yongjun User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.10) Gecko/20100528 Thunderbird/3.0.5 MIME-Version: 1.0 To: Paolo Bonzini , Avi Kivity CC: kvm@vger.kernel.org Subject: [PATCHv3] KVM: x86 emulator: fix BitOp instruction emulation References: <4C592470.1010608@cn.fujitsu.com> <4C5935CD.7010005@redhat.com> In-Reply-To: <4C5935CD.7010005@redhat.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Thu, 05 Aug 2010 06:09:29 +0000 (UTC) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index d197b46..8763708 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -724,6 +724,33 @@ done: return rc; } +static void fetch_bit_operand(struct decode_cache *c) +{ + unsigned long mask, byte_offset; + + if (c->dst.type != OP_MEM || c->src.type != OP_REG) { + /* Only subword offset */ + c->src.val &= (c->dst.bytes << 3) - 1; + return; + } + + if (c->src.bytes == 2) + c->src.val = (s16)c->src.val; + else if (c->src.bytes == 4) + c->src.val = (s32)c->src.val; + + mask = ~(c->dst.bytes * 8 - 1); + + if ((long)c->src.val > 0) { + c->dst.addr.mem = c->dst.addr.mem + (c->src.val & mask) / 8; + c->src.val &= (c->dst.bytes << 3) - 1; + } else { + byte_offset = c->dst.bytes + ((-c->src.val - 1) & mask) / 8; + c->dst.addr.mem -= byte_offset; + c->src.val += byte_offset << 3; + } +} + static int read_emulated(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, unsigned long addr, void *dest, unsigned size) @@ -2261,7 +2288,7 @@ static struct opcode twobyte_table[256] = { D(DstReg | SrcMem16 | ModRM | Mov), /* 0xB8 - 0xBF */ N, N, - G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock), + G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock), N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), /* 0xC0 - 0xCF */ @@ -2587,12 +2614,8 @@ done_prefixes: c->dst.bytes = 8; else c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; - if (c->dst.type == OP_MEM && (c->d & BitOp)) { - unsigned long mask = ~(c->dst.bytes * 8 - 1); - - c->dst.addr.mem = c->dst.addr.mem + - (c->src.val & mask) / 8; - } + if (c->d & BitOp) + fetch_bit_operand(c); c->dst.orig_val = c->dst.val; break; case DstAcc: @@ -3303,8 +3326,6 @@ twobyte_insn: case 0xa3: bt: /* bt */ c->dst.type = OP_NONE; - /* only subword offset */ - c->src.val &= (c->dst.bytes << 3) - 1; emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); break; case 0xa4: /* shld imm8, r, r/m */ @@ -3321,8 +3342,6 @@ twobyte_insn: break; case 0xab: bts: /* bts */ - /* only subword offset */ - c->src.val &= (c->dst.bytes << 3) - 1; emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); break; case 0xac: /* shrd imm8, r, r/m */ @@ -3350,8 +3369,6 @@ twobyte_insn: break; case 0xb3: btr: /* btr */ - /* only subword offset */ - c->src.val &= (c->dst.bytes << 3) - 1; emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); break; case 0xb6 ... 0xb7: /* movzx */ @@ -3373,8 +3390,6 @@ twobyte_insn: break; case 0xbb: btc: /* btc */ - /* only subword offset */ - c->src.val &= (c->dst.bytes << 3) - 1; emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); break; case 0xbe ... 0xbf: /* movsx */