From patchwork Fri Aug 6 07:17:57 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yongjun X-Patchwork-Id: 117717 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 o767Ju6K027992 for ; Fri, 6 Aug 2010 07:19:56 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933921Ab0HFHTs (ORCPT ); Fri, 6 Aug 2010 03:19:48 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:62772 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S933221Ab0HFHTr (ORCPT ); Fri, 6 Aug 2010 03:19:47 -0400 Received: from tang.cn.fujitsu.com (tang.cn.fujitsu.com [10.167.250.3]) by song.cn.fujitsu.com (Postfix) with ESMTP id 40FBC17014C; Fri, 6 Aug 2010 15:19:45 +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 o767GfE1017131; Fri, 6 Aug 2010 15:16:41 +0800 Received: from [10.167.141.76] (unknown [10.167.141.76]) by fnst.cn.fujitsu.com (Postfix) with ESMTPA id 04E4F14C012; Fri, 6 Aug 2010 15:20:34 +0800 (CST) Message-ID: <4C5BB725.3060609@cn.fujitsu.com> Date: Fri, 06 Aug 2010 15:17:57 +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: Avi Kivity CC: kvm@vger.kernel.org Subject: [PATCH 1/3] KVM: x86 emulator: fix negative bit offset BitOp instruction emulation 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]); Fri, 06 Aug 2010 07:19:56 +0000 (UTC) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 0e360c6..470c7eb 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -724,6 +724,30 @@ done: return rc; } +static void fetch_bit_operand(struct decode_cache *c) +{ + unsigned long mask, byte_offset; + + if (c->dst.type == OP_MEM) { + 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) { + /* negative bit offset */ + byte_offset = c->dst.bytes + + ((-c->src.val - 1) & mask) / 8; + c->dst.addr.mem -= byte_offset; + } else { + /* positive bit offset */ + c->dst.addr.mem += (c->src.val & mask) / 8; + } + } +} + static int read_emulated(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, unsigned long addr, void *dest, unsigned size) @@ -2646,12 +2670,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: