From: "Jan Beulich" Subject: [PATCH 17/17] x86emul: don't assume a memory operand Date: Thu, 08 Sep 2016 07:21:00 -0600 Message-ID: <57D181DC020000780010D1EA@prv-mh.provo.novell.com> References: <57D17C78020000780010D127@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2711ACAC.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bhzGK-0002dy-5E for xen-devel@lists.xenproject.org; Thu, 08 Sep 2016 13:21:04 +0000 In-Reply-To: <57D17C78020000780010D127@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. --=__Part2711ACAC.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Especially for x86_insn_operand_ea() to return dependable segment information even when the caller didn't consider applicability we shouldn't have ea.type start out as OP_MEM. Make it OP_NONE instead, and set it to OP_MEM when we actually encounter memory like operands. This requires to eliminate the XSA-123 fix, which has been no longer necessary since the elimination of the union in commit dd766684e7. That in turn allows restricting the scope of override_seg to x86_decode(). At this occasion also make it have a proper type, instead of plain int. Signed-off-by: Jan Beulich --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -1632,7 +1632,6 @@ struct x86_emulate_state { opcode_desc_t desc; union vex vex; union evex evex; - int override_seg; =20 /* * Data operand effective address (usually computed from ModRM). @@ -1664,7 +1663,6 @@ struct x86_emulate_state { #define lock_prefix (state->lock_prefix) #define vex (state->vex) #define evex (state->evex) -#define override_seg (state->override_seg) #define ea (state->ea) =20 static int @@ -1693,6 +1691,7 @@ x86_decode_base( case 0xa0: case 0xa1: /* mov mem.offs,{%al,%ax,%eax,%rax} */ case 0xa2: case 0xa3: /* mov {%al,%ax,%eax,%rax},mem.offs */ /* Source EA is not encoded via ModRM. */ + ea.type =3D OP_MEM; ea.mem.off =3D insn_fetch_bytes(ad_bytes); break; =20 @@ -1783,11 +1782,11 @@ x86_decode( { uint8_t b, d, sib, sib_index, sib_base; unsigned int def_op_bytes, def_ad_bytes, opcode; + enum x86_segment override_seg =3D x86_seg_none; int rc =3D X86EMUL_OKAY; =20 memset(state, 0, sizeof(*state)); - override_seg =3D -1; - ea.type =3D OP_MEM; + ea.type =3D OP_NONE; ea.mem.seg =3D x86_seg_ds; ea.reg =3D REG_POISON; state->regs =3D ctxt->regs; @@ -2085,6 +2084,7 @@ x86_decode( else if ( ad_bytes =3D=3D 2 ) { /* 16-bit ModR/M decode. */ + ea.type =3D OP_MEM; switch ( modrm_rm ) { case 0: @@ -2135,6 +2135,7 @@ x86_decode( else { /* 32/64-bit ModR/M decode. */ + ea.type =3D OP_MEM; if ( modrm_rm =3D=3D 4 ) { sib =3D insn_fetch_type(uint8_t); @@ -2199,7 +2200,7 @@ x86_decode( } } =20 - if ( override_seg !=3D -1 && ea.type =3D=3D OP_MEM ) + if ( override_seg !=3D x86_seg_none ) ea.mem.seg =3D override_seg; =20 /* Fetch the immediate operand, if present. */ @@ -4250,13 +4251,11 @@ x86_emulate( generate_exception_if(limit < sizeof(long) || (limit & (limit - 1)), EXC_UD, -1); base &=3D ~(limit - 1); - if ( override_seg =3D=3D -1 ) - override_seg =3D x86_seg_ds; if ( ops->rep_stos ) { unsigned long nr_reps =3D limit / sizeof(zero); =20 - rc =3D ops->rep_stos(&zero, override_seg, base, sizeof(zer= o), + rc =3D ops->rep_stos(&zero, ea.mem.seg, base, sizeof(zero)= , &nr_reps, ctxt); if ( rc =3D=3D X86EMUL_OKAY ) { @@ -4268,7 +4267,7 @@ x86_emulate( } while ( limit ) { - rc =3D ops->write(override_seg, base, &zero, sizeof(zero),= ctxt); + rc =3D ops->write(ea.mem.seg, base, &zero, sizeof(zero), = ctxt); if ( rc !=3D X86EMUL_OKAY ) goto done; base +=3D sizeof(zero); @@ -5254,7 +5253,6 @@ x86_emulate( #undef rex_prefix #undef lock_prefix #undef vex -#undef override_seg #undef ea =20 #ifdef __XEN__ --=__Part2711ACAC.1__= Content-Type: text/plain; name="x86emul-ea-init-OP_NONE.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86emul-ea-init-OP_NONE.patch" x86emul: don't assume a memory operand=0A=0AEspecially for x86_insn_operand= _ea() to return dependable segment=0Ainformation even when the caller = didn't consider applicability we=0Ashouldn't have ea.type start out as = OP_MEM. Make it OP_NONE instead,=0Aand set it to OP_MEM when we actually = encounter memory like operands.=0A=0AThis requires to eliminate the = XSA-123 fix, which has been no longer=0Anecessary since the elimination of = the union in commit dd766684e7. That=0Ain turn allows restricting the = scope of override_seg to x86_decode().=0AAt this occasion also make it = have a proper type, instead of plain int.=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@@ -1632,7 +1632,6 @@ struct = x86_emulate_state {=0A opcode_desc_t desc;=0A union vex vex;=0A = union evex evex;=0A- int override_seg;=0A =0A /*=0A * Data = operand effective address (usually computed from ModRM).=0A@@ -1664,7 = +1663,6 @@ struct x86_emulate_state {=0A #define lock_prefix (state->lock_p= refix)=0A #define vex (state->vex)=0A #define evex (state->evex)=0A-#define= override_seg (state->override_seg)=0A #define ea (state->ea)=0A =0A = static int=0A@@ -1693,6 +1691,7 @@ x86_decode_base(=0A case 0xa0: case = 0xa1: /* mov mem.offs,{%al,%ax,%eax,%rax} */=0A case 0xa2: case 0xa3: = /* mov {%al,%ax,%eax,%rax},mem.offs */=0A /* Source EA is not = encoded via ModRM. */=0A+ ea.type =3D OP_MEM;=0A ea.mem.off = =3D insn_fetch_bytes(ad_bytes);=0A break;=0A =0A@@ -1783,11 = +1782,11 @@ x86_decode(=0A {=0A uint8_t b, d, sib, sib_index, = sib_base;=0A unsigned int def_op_bytes, def_ad_bytes, opcode;=0A+ = enum x86_segment override_seg =3D x86_seg_none;=0A int rc =3D = X86EMUL_OKAY;=0A =0A memset(state, 0, sizeof(*state));=0A- = override_seg =3D -1;=0A- ea.type =3D OP_MEM;=0A+ ea.type =3D = OP_NONE;=0A ea.mem.seg =3D x86_seg_ds;=0A ea.reg =3D REG_POISON;=0A= state->regs =3D ctxt->regs;=0A@@ -2085,6 +2084,7 @@ x86_decode(=0A = else if ( ad_bytes =3D=3D 2 )=0A {=0A /* 16-bit = ModR/M decode. */=0A+ ea.type =3D OP_MEM;=0A switch = ( modrm_rm )=0A {=0A case 0:=0A@@ -2135,6 +2135,7 = @@ x86_decode(=0A else=0A {=0A /* 32/64-bit = ModR/M decode. */=0A+ ea.type =3D OP_MEM;=0A if ( = modrm_rm =3D=3D 4 )=0A {=0A sib =3D insn_fetch_= type(uint8_t);=0A@@ -2199,7 +2200,7 @@ x86_decode(=0A }=0A = }=0A =0A- if ( override_seg !=3D -1 && ea.type =3D=3D OP_MEM )=0A+ = if ( override_seg !=3D x86_seg_none )=0A ea.mem.seg =3D override_se= g;=0A =0A /* Fetch the immediate operand, if present. */=0A@@ -4250,13 = +4251,11 @@ x86_emulate(=0A generate_exception_if(limit < = sizeof(long) ||=0A (limit & (limit - = 1)), EXC_UD, -1);=0A base &=3D ~(limit - 1);=0A- if = ( override_seg =3D=3D -1 )=0A- override_seg =3D x86_seg_ds;= =0A if ( ops->rep_stos )=0A {=0A = unsigned long nr_reps =3D limit / sizeof(zero);=0A =0A- rc = =3D ops->rep_stos(&zero, override_seg, base, sizeof(zero),=0A+ = rc =3D ops->rep_stos(&zero, ea.mem.seg, base, sizeof(zero),=0A = &nr_reps, ctxt);=0A if ( rc = =3D=3D X86EMUL_OKAY )=0A {=0A@@ -4268,7 +4267,7 @@ = x86_emulate(=0A }=0A while ( limit )=0A = {=0A- rc =3D ops->write(override_seg, base, &zero, = sizeof(zero), ctxt);=0A+ rc =3D ops->write(ea.mem.seg, = base, &zero, sizeof(zero), ctxt);=0A if ( rc !=3D = X86EMUL_OKAY )=0A goto done;=0A base = +=3D sizeof(zero);=0A@@ -5254,7 +5253,6 @@ x86_emulate(=0A #undef = rex_prefix=0A #undef lock_prefix=0A #undef vex=0A-#undef override_seg=0A = #undef ea=0A =0A #ifdef __XEN__=0A --=__Part2711ACAC.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__Part2711ACAC.1__=--