From: "Jan Beulich" Subject: [PATCH 2/2] x86/HVM: re-order operations in hvm_ud_intercept() Date: Wed, 08 Jun 2016 07:43:39 -0600 Message-ID: <57583D2B02000078000F31CE@prv-mh.provo.novell.com> References: <57583B4E02000078000F3198@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part5D6B5F1B.1__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bAdlp-0005cq-GW for xen-devel@lists.xenproject.org; Wed, 08 Jun 2016 13:43:45 +0000 In-Reply-To: <57583B4E02000078000F3198@prv-mh.provo.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Andrew Cooper 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. --=__Part5D6B5F1B.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Don't fetch CS explicitly, leverage the fact that hvm_emulate_prepare() already does (and that hvm_virtual_to_linear_addr() doesn't alter it). At once increase the length passed to hvm_virtual_to_linear_addr() by one: There definitely needs to be at least one more opcode byte, and we can avoid missing a wraparound case this way. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3834,19 +3834,20 @@ void hvm_ud_intercept(struct cpu_user_re { struct hvm_emulate_ctxt ctxt; =20 + hvm_emulate_prepare(&ctxt, regs); + if ( opt_hvm_fep ) { struct vcpu *cur =3D current; - struct segment_register cs; + const struct segment_register *cs =3D &ctxt.seg_reg[x86_seg_cs]; unsigned long addr; char sig[5]; /* ud2; .ascii "xen" */ =20 - hvm_get_segment_register(cur, x86_seg_cs, &cs); - if ( hvm_virtual_to_linear_addr(x86_seg_cs, &cs, regs->eip, - sizeof(sig), hvm_access_insn_fetch= , + if ( hvm_virtual_to_linear_addr(x86_seg_cs, cs, regs->eip, + sizeof(sig) + 1, hvm_access_insn_f= etch, (hvm_long_mode_enabled(cur) && - cs.attr.fields.l) ? 64 : - cs.attr.fields.db ? 32 : 16, = &addr) && + cs->attr.fields.l) ? 64 : + cs->attr.fields.db ? 32 : 16, = &addr) && (hvm_fetch_from_guest_virt_nofault(sig, addr, sizeof(sig), 0) =3D=3D HVMCOPY_okay) = && (memcmp(sig, "\xf\xbxen", sizeof(sig)) =3D=3D 0) ) @@ -3856,8 +3857,6 @@ void hvm_ud_intercept(struct cpu_user_re } } =20 - hvm_emulate_prepare(&ctxt, regs); - switch ( hvm_emulate_one(&ctxt) ) { case X86EMUL_UNHANDLEABLE: --=__Part5D6B5F1B.1__= Content-Type: text/plain; name="x86-HVM-fep-reorder.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-fep-reorder.patch" x86/HVM: re-order operations in hvm_ud_intercept()=0A=0ADon't fetch CS = explicitly, leverage the fact that hvm_emulate_prepare()=0Aalready does = (and that hvm_virtual_to_linear_addr() doesn't alter it).=0A=0AAt once = increase the length passed to hvm_virtual_to_linear_addr() by=0Aone: There = definitely needs to be at least one more opcode byte, and we=0Acan avoid = missing a wraparound case this way.=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/= hvm/hvm.c=0A@@ -3834,19 +3834,20 @@ void hvm_ud_intercept(struct cpu_user_r= e=0A {=0A struct hvm_emulate_ctxt ctxt;=0A =0A+ hvm_emulate_prepare(= &ctxt, regs);=0A+=0A if ( opt_hvm_fep )=0A {=0A struct = vcpu *cur =3D current;=0A- struct segment_register cs;=0A+ = const struct segment_register *cs =3D &ctxt.seg_reg[x86_seg_cs];=0A = unsigned long addr;=0A char sig[5]; /* ud2; .ascii "xen" */=0A = =0A- hvm_get_segment_register(cur, x86_seg_cs, &cs);=0A- if = ( hvm_virtual_to_linear_addr(x86_seg_cs, &cs, regs->eip,=0A- = sizeof(sig), hvm_access_insn_fetch,=0A+ if = ( hvm_virtual_to_linear_addr(x86_seg_cs, cs, regs->eip,=0A+ = sizeof(sig) + 1, hvm_access_insn_fetch,=0A = (hvm_long_mode_enabled(cur) &&=0A- = cs.attr.fields.l) ? 64 :=0A- = cs.attr.fields.db ? 32 : 16, &addr) &&=0A+ = cs->attr.fields.l) ? 64 :=0A+ = cs->attr.fields.db ? 32 : 16, &addr) &&=0A = (hvm_fetch_from_guest_virt_nofault(sig, addr, sizeof(sig),=0A = 0) =3D=3D HVMCOPY_okay) &&=0A = (memcmp(sig, "\xf\xbxen", sizeof(sig)) =3D=3D 0) )=0A@@ -3856,8 = +3857,6 @@ void hvm_ud_intercept(struct cpu_user_re=0A }=0A = }=0A =0A- hvm_emulate_prepare(&ctxt, regs);=0A-=0A switch ( = hvm_emulate_one(&ctxt) )=0A {=0A case X86EMUL_UNHANDLEABLE:=0A --=__Part5D6B5F1B.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwOi8vbGlzdHMueGVuLm9y Zy94ZW4tZGV2ZWwK --=__Part5D6B5F1B.1__=--