From: "Jan Beulich" Subject: [PATCH] x86_emulate: fix wrap around handling for repeated string instructions Date: Fri, 20 Sep 2013 10:19:56 +0100 Message-ID: <523C2F5C02000078000F4DAB@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part390A5A2C.0__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VMwsD-00026H-Eo for xen-devel@lists.xenproject.org; Fri, 20 Sep 2013 09:19:37 +0000 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. --=__Part390A5A2C.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline For one, repeat count clipping for MOVS must be done taking into consideration both source and destination addresses. And then we should allow a wrap on the final iteration only if either the wrap is a precise one (i.e. the access itself doesn't wrap, just the resulting index register value would) or if there is just one iteration. In all other cases we should do a bulk operation first without hitting the wrap, and then issue an individual iteration. If we don't do it that way, - the last iteration not completing successfully will cause the whole operation to fail (i.e. registers not get updated to the failure point) - hvmemul_virtual_to_linear() may needlessly enforce non-repeated operation Additionally with the prior implementation there was a case (df=3D1, ea=3D~0, reps=3D~0, bytes_per_rep=3D1) where we'd end up passing = zero reps back to the caller, yet various places assume that there's at least on iteration. Signed-off-by: Jan Beulich --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -773,13 +773,20 @@ static void __put_rep_prefix( __put_rep_prefix(&_regs, ctxt->regs, ad_bytes, reps_completed); \ }) =20 -/* Clip maximum repetitions so that the index register only just wraps. = */ +/* Clip maximum repetitions so that the index register at most just = wraps. */ #define truncate_ea_and_reps(ea, reps, bytes_per_rep) ({ = \ - unsigned long __todo =3D (ctxt->regs->eflags & EFLG_DF) ? (ea) : = ~(ea); \ - __todo =3D truncate_word(__todo, ad_bytes); = \ - __todo =3D (__todo / (bytes_per_rep)) + 1; = \ - (reps) =3D (__todo < (reps)) ? __todo : (reps); = \ - truncate_word((ea), ad_bytes); = \ + unsigned long todo__, ea__ =3D truncate_word(ea, ad_bytes); = \ + if ( !(ctxt->regs->eflags & EFLG_DF) ) = \ + todo__ =3D truncate_word(-(ea), ad_bytes) / (bytes_per_rep); = \ + else if ( truncate_word((ea) + (bytes_per_rep) - 1, ad_bytes) < ea__ = )\ + todo__ =3D 1; = \ + else = \ + todo__ =3D ea__ / (bytes_per_rep) + 1; = \ + if ( !todo__ ) = \ + (reps) =3D 1; = \ + else if ( todo__ < (reps) ) = \ + (reps) =3D todo__; = \ + ea__; = \ }) =20 /* Compatibility function: read guest memory, zero-extend result to a = ulong. */ @@ -2385,8 +2392,9 @@ x86_emulate( dst.bytes =3D (d & ByteOp) ? 1 : op_bytes; dst.mem.seg =3D x86_seg_es; dst.mem.off =3D truncate_ea_and_reps(_regs.edi, nr_reps, = dst.bytes); + src.mem.off =3D truncate_ea_and_reps(_regs.esi, nr_reps, = dst.bytes); if ( (nr_reps > 1) && (ops->rep_movs !=3D NULL) && - ((rc =3D ops->rep_movs(ea.mem.seg, truncate_ea(_regs.esi), + ((rc =3D ops->rep_movs(ea.mem.seg, src.mem.off, dst.mem.seg, dst.mem.off, dst.bytes, &nr_reps, ctxt)) !=3D X86EMUL_UNHANDLEAB= LE) ) { @@ -2395,7 +2403,7 @@ x86_emulate( } else { - if ( (rc =3D read_ulong(ea.mem.seg, truncate_ea(_regs.esi), + if ( (rc =3D read_ulong(ea.mem.seg, src.mem.off, &dst.val, dst.bytes, ctxt, ops)) !=3D 0 = ) goto done; dst.type =3D OP_MEM; --=__Part390A5A2C.0__= Content-Type: text/plain; name="x86-emul-rep-wrap.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-emul-rep-wrap.patch" x86_emulate: fix wrap around handling for repeated string instructions=0A= =0AFor one, repeat count clipping for MOVS must be done taking into=0Aconsi= deration both source and destination addresses.=0A=0AAnd then we should = allow a wrap on the final iteration only if either=0Athe wrap is a precise = one (i.e. the access itself doesn't wrap, just=0Athe resulting index = register value would) or if there is just one=0Aiteration. In all other = cases we should do a bulk operation first=0Awithout hitting the wrap, and = then issue an individual iteration. If=0Awe don't do it that way,=0A- the = last iteration not completing successfully will cause the whole=0A = operation to fail (i.e. registers not get updated to the failure=0A = point)=0A- hvmemul_virtual_to_linear() may needlessly enforce non-repeated= =0A operation=0A=0AAdditionally with the prior implementation there was a = case=0A(df=3D1, ea=3D~0, reps=3D~0, bytes_per_rep=3D1) where we'd end up = passing zero=0Areps back to the caller, yet various places assume that = there's at=0Aleast on iteration.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/x86_emulate/x86_emulate.c=0A+++ = b/xen/arch/x86/x86_emulate/x86_emulate.c=0A@@ -773,13 +773,20 @@ static = void __put_rep_prefix(=0A __put_rep_prefix(&_regs, ctxt->regs, = ad_bytes, reps_completed); \=0A })=0A =0A-/* Clip maximum repetitions so = that the index register only just wraps. */=0A+/* Clip maximum repetitions = so that the index register at most just wraps. */=0A #define truncate_ea_an= d_reps(ea, reps, bytes_per_rep) ({ \=0A- unsigned long = __todo =3D (ctxt->regs->eflags & EFLG_DF) ? (ea) : ~(ea); \=0A- __todo = =3D truncate_word(__todo, ad_bytes); \=0A- = __todo =3D (__todo / (bytes_per_rep)) + 1; = \=0A- (reps) =3D (__todo < (reps)) ? __todo : (reps); = \=0A- truncate_word((ea), ad_bytes); = \=0A+ unsigned long todo__, ea__ =3D truncate_word(ea, = ad_bytes); \=0A+ if ( !(ctxt->regs->eflags & EFLG_DF) ) = \=0A+ todo__ =3D truncate_word(-(ea), = ad_bytes) / (bytes_per_rep); \=0A+ else if ( truncate_word((ea) = + (bytes_per_rep) - 1, ad_bytes) < ea__ )\=0A+ todo__ =3D 1; = \=0A+ else = \=0A+ todo__ =3D = ea__ / (bytes_per_rep) + 1; \=0A+ if ( = !todo__ ) \=0A+ = (reps) =3D 1; = \=0A+ else if ( todo__ < (reps) ) = \=0A+ (reps) =3D todo__; = \=0A+ ea__; = \=0A })=0A =0A /* Compatibility function: read guest memory, = zero-extend result to a ulong. */=0A@@ -2385,8 +2392,9 @@ x86_emulate(=0A = dst.bytes =3D (d & ByteOp) ? 1 : op_bytes;=0A dst.mem.seg = =3D x86_seg_es;=0A dst.mem.off =3D truncate_ea_and_reps(_regs.edi, = nr_reps, dst.bytes);=0A+ src.mem.off =3D truncate_ea_and_reps(_regs.= esi, nr_reps, dst.bytes);=0A if ( (nr_reps > 1) && (ops->rep_movs = !=3D NULL) &&=0A- ((rc =3D ops->rep_movs(ea.mem.seg, truncate_e= a(_regs.esi),=0A+ ((rc =3D ops->rep_movs(ea.mem.seg, src.mem.of= f,=0A dst.mem.seg, dst.mem.off, = dst.bytes,=0A &nr_reps, ctxt)) !=3D = X86EMUL_UNHANDLEABLE) )=0A {=0A@@ -2395,7 +2403,7 @@ x86_emulate(= =0A }=0A else=0A {=0A- if ( (rc =3D = read_ulong(ea.mem.seg, truncate_ea(_regs.esi),=0A+ if ( (rc =3D = read_ulong(ea.mem.seg, src.mem.off,=0A = &dst.val, dst.bytes, ctxt, ops)) !=3D 0 )=0A goto done;=0A = dst.type =3D OP_MEM;=0A --=__Part390A5A2C.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 --=__Part390A5A2C.0__=--