From: "Jan Beulich" Subject: [PATCH 5/5] x86/HVM: cache emulated instruction for retry processing Date: Mon, 30 Sep 2013 13:59:37 +0100 Message-ID: <524991D902000078000F8087@nat28.tlf.novell.com> References: <52498FFC02000078000F8068@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartAF9CB9A9.0__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VQd4h-0004N3-9c for xen-devel@lists.xenproject.org; Mon, 30 Sep 2013 12:59:43 +0000 In-Reply-To: <52498FFC02000078000F8068@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartAF9CB9A9.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Rather than re-reading the instruction bytes upon retry processing, stash away and re-use tha what we already read. That way we can be certain that the retry won't do something different from what requested the retry, getting once again closer to real hardware behavior (where what we use retries for is simply a bus operation, not involving redundant decoding of instructions). Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -569,9 +569,19 @@ static int hvmemul_insn_fetch( =20 /* Fall back if requested bytes are not in the prefetch cache. */ if ( unlikely((insn_off + bytes) > hvmemul_ctxt->insn_buf_bytes) ) - return __hvmemul_read( - seg, offset, p_data, bytes, - hvm_access_insn_fetch, hvmemul_ctxt); + { + int rc =3D __hvmemul_read(seg, offset, p_data, bytes, + hvm_access_insn_fetch, hvmemul_ctxt); + + if ( rc =3D=3D X86EMUL_OKAY ) + { + ASSERT(insn_off + bytes <=3D sizeof(hvmemul_ctxt->insn_buf)); + memcpy(&hvmemul_ctxt->insn_buf[insn_off], p_data, bytes); + hvmemul_ctxt->insn_buf_bytes =3D insn_off + bytes; + } + + return rc; + } =20 /* Hit the cache. Simple memcpy. */ memcpy(p_data, &hvmemul_ctxt->insn_buf[insn_off], bytes); @@ -1148,17 +1158,27 @@ int hvm_emulate_one( pfec |=3D PFEC_user_mode; =20 hvmemul_ctxt->insn_buf_eip =3D regs->eip; - hvmemul_ctxt->insn_buf_bytes =3D - hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) - ? : - (hvm_virtual_to_linear_addr( - x86_seg_cs, &hvmemul_ctxt->seg_reg[x86_seg_cs], - regs->eip, sizeof(hvmemul_ctxt->insn_buf), - hvm_access_insn_fetch, hvmemul_ctxt->ctxt.addr_size, &addr) = && - !hvm_fetch_from_guest_virt_nofault( - hvmemul_ctxt->insn_buf, addr, - sizeof(hvmemul_ctxt->insn_buf), pfec)) - ? sizeof(hvmemul_ctxt->insn_buf) : 0; + if ( !vio->mmio_insn_bytes ) + { + hvmemul_ctxt->insn_buf_bytes =3D + hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?: + (hvm_virtual_to_linear_addr(x86_seg_cs, + &hvmemul_ctxt->seg_reg[x86_seg_cs]= , + regs->eip, + sizeof(hvmemul_ctxt->insn_buf), + hvm_access_insn_fetch, + hvmemul_ctxt->ctxt.addr_size, + &addr) && + hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->insn_buf, = addr, + sizeof(hvmemul_ctxt->insn_b= uf), + pfec) =3D=3D HVMCOPY_okay) = ? + sizeof(hvmemul_ctxt->insn_buf) : 0; + } + else + { + hvmemul_ctxt->insn_buf_bytes =3D vio->mmio_insn_bytes; + memcpy(hvmemul_ctxt->insn_buf, vio->mmio_insn, vio->mmio_insn_byte= s); + } =20 hvmemul_ctxt->exn_pending =3D 0; vio->mmio_retrying =3D vio->mmio_retry; @@ -1169,7 +1189,16 @@ int hvm_emulate_one( if ( rc =3D=3D X86EMUL_OKAY && vio->mmio_retry ) rc =3D X86EMUL_RETRY; if ( rc !=3D X86EMUL_RETRY ) + { vio->mmio_large_read_bytes =3D vio->mmio_large_write_bytes =3D 0; + vio->mmio_insn_bytes =3D 0; + } + else + { + BUILD_BUG_ON(sizeof(vio->mmio_insn) < sizeof(hvmemul_ctxt->insn_bu= f)); + vio->mmio_insn_bytes =3D hvmemul_ctxt->insn_buf_bytes; + memcpy(vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_byte= s); + } =20 if ( rc !=3D X86EMUL_OKAY ) return rc; --- a/xen/include/asm-x86/hvm/vcpu.h +++ b/xen/include/asm-x86/hvm/vcpu.h @@ -66,6 +66,9 @@ struct hvm_vcpu_io { /* We may write up to m256 as a number of device-model transactions. = */ unsigned int mmio_large_write_bytes; paddr_t mmio_large_write_pa; + /* For retries we shouldn't re-fetch the instruction. */ + unsigned int mmio_insn_bytes; + unsigned char mmio_insn[16]; /* * For string instruction emulation we need to be able to signal a * necessary retry through other than function return codes. --=__PartAF9CB9A9.0__= Content-Type: text/plain; name="x86-HVM-cache-emulated-insn.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-cache-emulated-insn.patch" x86/HVM: cache emulated instruction for retry processing=0A=0ARather than = re-reading the instruction bytes upon retry processing,=0Astash away and = re-use tha what we already read. That way we can be=0Acertain that the = retry won't do something different from what requested=0Athe retry, = getting once again closer to real hardware behavior (where=0Awhat we use = retries for is simply a bus operation, not involving=0Aredundant decoding = of instructions).=0A=0ASigned-off-by: Jan Beulich =0A=0A= --- a/xen/arch/x86/hvm/emulate.c=0A+++ b/xen/arch/x86/hvm/emulate.c=0A@@ = -569,9 +569,19 @@ static int hvmemul_insn_fetch(=0A =0A /* Fall back = if requested bytes are not in the prefetch cache. */=0A if ( unlikely((= insn_off + bytes) > hvmemul_ctxt->insn_buf_bytes) )=0A- return = __hvmemul_read(=0A- seg, offset, p_data, bytes,=0A- = hvm_access_insn_fetch, hvmemul_ctxt);=0A+ {=0A+ int rc =3D = __hvmemul_read(seg, offset, p_data, bytes,=0A+ = hvm_access_insn_fetch, hvmemul_ctxt);=0A+=0A+ if ( rc =3D=3D = X86EMUL_OKAY )=0A+ {=0A+ ASSERT(insn_off + bytes <=3D = sizeof(hvmemul_ctxt->insn_buf));=0A+ memcpy(&hvmemul_ctxt->insn_= buf[insn_off], p_data, bytes);=0A+ hvmemul_ctxt->insn_buf_bytes = =3D insn_off + bytes;=0A+ }=0A+=0A+ return rc;=0A+ }=0A = =0A /* Hit the cache. Simple memcpy. */=0A memcpy(p_data, = &hvmemul_ctxt->insn_buf[insn_off], bytes);=0A@@ -1148,17 +1158,27 @@ int = hvm_emulate_one(=0A pfec |=3D PFEC_user_mode;=0A =0A hvmemul_ct= xt->insn_buf_eip =3D regs->eip;=0A- hvmemul_ctxt->insn_buf_bytes = =3D=0A- hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf)=0A- = ? :=0A- (hvm_virtual_to_linear_addr(=0A- x86_seg_cs, = &hvmemul_ctxt->seg_reg[x86_seg_cs],=0A- regs->eip, sizeof(hvmemu= l_ctxt->insn_buf),=0A- hvm_access_insn_fetch, hvmemul_ctxt->ctxt= .addr_size, &addr) &&=0A- !hvm_fetch_from_guest_virt_nofault(=0A- = hvmemul_ctxt->insn_buf, addr,=0A- sizeof(hvmemul_ctx= t->insn_buf), pfec))=0A- ? sizeof(hvmemul_ctxt->insn_buf) : 0;=0A+ = if ( !vio->mmio_insn_bytes )=0A+ {=0A+ hvmemul_ctxt->insn_buf_b= ytes =3D=0A+ hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) = ?:=0A+ (hvm_virtual_to_linear_addr(x86_seg_cs,=0A+ = &hvmemul_ctxt->seg_reg[x86_seg_cs],=0A+ = regs->eip,=0A+ = sizeof(hvmemul_ctxt->insn_buf),=0A+ = hvm_access_insn_fetch,=0A+ = hvmemul_ctxt->ctxt.addr_size,=0A+ = &addr) &&=0A+ hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->i= nsn_buf, addr,=0A+ sizeof(hvm= emul_ctxt->insn_buf),=0A+ = pfec) =3D=3D HVMCOPY_okay) ?=0A+ sizeof(hvmemul_ctxt->insn_buf) = : 0;=0A+ }=0A+ else=0A+ {=0A+ hvmemul_ctxt->insn_buf_bytes = =3D vio->mmio_insn_bytes;=0A+ memcpy(hvmemul_ctxt->insn_buf, = vio->mmio_insn, vio->mmio_insn_bytes);=0A+ }=0A =0A hvmemul_ctxt->ex= n_pending =3D 0;=0A vio->mmio_retrying =3D vio->mmio_retry;=0A@@ = -1169,7 +1189,16 @@ int hvm_emulate_one(=0A if ( rc =3D=3D X86EMUL_OKAY= && vio->mmio_retry )=0A rc =3D X86EMUL_RETRY;=0A if ( rc !=3D = X86EMUL_RETRY )=0A+ {=0A vio->mmio_large_read_bytes =3D = vio->mmio_large_write_bytes =3D 0;=0A+ vio->mmio_insn_bytes =3D = 0;=0A+ }=0A+ else=0A+ {=0A+ BUILD_BUG_ON(sizeof(vio->mmio_i= nsn) < sizeof(hvmemul_ctxt->insn_buf));=0A+ vio->mmio_insn_bytes = =3D hvmemul_ctxt->insn_buf_bytes;=0A+ memcpy(vio->mmio_insn, = hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);=0A+ }=0A =0A if ( rc = !=3D X86EMUL_OKAY )=0A return rc;=0A--- a/xen/include/asm-x86/hvm/v= cpu.h=0A+++ b/xen/include/asm-x86/hvm/vcpu.h=0A@@ -66,6 +66,9 @@ struct = hvm_vcpu_io {=0A /* We may write up to m256 as a number of device-model= transactions. */=0A unsigned int mmio_large_write_bytes;=0A = paddr_t mmio_large_write_pa;=0A+ /* For retries we shouldn't re-fetch = the instruction. */=0A+ unsigned int mmio_insn_bytes;=0A+ unsigned = char mmio_insn[16];=0A /*=0A * For string instruction emulation = we need to be able to signal a=0A * necessary retry through other = than function return codes.=0A --=__PartAF9CB9A9.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartAF9CB9A9.0__=--