From: "Jan Beulich" Subject: [PATCH] x86emul: improve LOCK handling Date: Tue, 16 Aug 2016 07:51:23 -0600 Message-ID: <57B3367B0200007800106590@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartDDEB344B.1__=" Return-path: Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bZem9-0001iP-0e for xen-devel@lists.xenproject.org; Tue, 16 Aug 2016 13:51:29 +0000 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. --=__PartDDEB344B.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Certain opcodes would so far not have got #UD when a LOCK prefix was present. Adjust this by - moving the too early generic check into destination operand decoding, where DstNone and DstReg already have respective handling - switching source and destination of TEST r,r/m, for it to be taken care of by aforementioned generic checks - explicitly dealing with all forms of CMP, SHLD, SHRD, as well as TEST $imm,r/m To make the handling of opcodes F6 and F7 more obvious, reduce the amount of state set in the table, and adjust the respective switch() statement accordingly. Also eliminate the latent bug of the check in DstNone handling not considering the opcode extension set. Signed-off-by: Jan Beulich --- This will only apply cleanly on top of https://lists.xenproject.org/archives/html/xen-devel/2016-08/msg01975.html.= --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -110,7 +110,7 @@ static uint8_t opcode_table[256] =3D { /* 0x80 - 0x87 */ ByteOp|DstMem|SrcImm|ModRM, DstMem|SrcImm|ModRM, ByteOp|DstMem|SrcImm|ModRM, DstMem|SrcImmByte|ModRM, - ByteOp|DstMem|SrcReg|ModRM, DstMem|SrcReg|ModRM, + ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstMem|SrcReg|ModRM, DstMem|SrcReg|ModRM, /* 0x88 - 0x8F */ ByteOp|DstMem|SrcReg|ModRM|Mov, DstMem|SrcReg|ModRM|Mov, @@ -169,8 +169,7 @@ static uint8_t opcode_table[256] =3D { DstEax|SrcImplicit, DstEax|SrcImplicit, ImplicitOps, ImplicitOps, /* 0xF0 - 0xF7 */ 0, ImplicitOps, 0, 0, - ImplicitOps, ImplicitOps, - ByteOp|DstMem|SrcNone|ModRM, DstMem|SrcNone|ModRM, + ImplicitOps, ImplicitOps, ByteOp|ModRM, ModRM, /* 0xF8 - 0xFF */ ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, ByteOp|DstMem|SrcNone|ModRM, DstMem|SrcNone|= ModRM @@ -1651,9 +1650,6 @@ x86_emulate( } } =20 - /* Lock prefix is allowed only on RMW instructions. */ - generate_exception_if((d & Mov) && lock_prefix, EXC_UD, -1); - /* ModRM and SIB bytes. */ if ( d & ModRM ) { @@ -1729,13 +1725,17 @@ x86_emulate( switch ( modrm_reg & 7 ) { case 0 ... 1: /* test */ - d =3D (d & ~SrcMask) | SrcImm; + d |=3D DstMem | SrcImm; + break; + case 2: /* not */ + case 3: /* neg */ + d |=3D DstMem; break; case 4: /* mul */ case 5: /* imul */ case 6: /* div */ case 7: /* idiv */ - d =3D (d & (ByteOp | ModRM)) | DstEax | SrcMem; + d |=3D DstEax | SrcMem; break; } break; @@ -1983,8 +1983,9 @@ x86_emulate( */ generate_exception_if( lock_prefix && - ((b < 0x20) || (b > 0x23)) && /* MOV CRn/DRn */ - (b !=3D 0xc7), /* CMPXCHG{8,16}B */ + (ext !=3D ext_0f || + (((b < 0x20) || (b > 0x23)) && /* MOV CRn/DRn */ + (b !=3D 0xc7))), /* CMPXCHG{8,16}B */ EXC_UD, -1); dst.type =3D OP_NONE; break; @@ -2062,6 +2063,8 @@ x86_emulate( goto done; dst.orig_val =3D dst.val; } + else /* Lock prefix is allowed only on RMW instructions. */ + generate_exception_if(lock_prefix, EXC_UD, -1); break; } =20 @@ -2111,6 +2114,7 @@ x86_emulate( break; =20 case 0x38 ... 0x3d: cmp: /* cmp */ + generate_exception_if(lock_prefix, EXC_UD, -1); emulate_2op_SrcV("cmp", src, dst, _regs.eflags); dst.type =3D OP_NONE; break; @@ -3545,6 +3549,7 @@ x86_emulate( unsigned long u[2], v; =20 case 0 ... 1: /* test */ + generate_exception_if(lock_prefix, EXC_UD, -1); goto test; case 2: /* not */ dst.val =3D ~dst.val; @@ -4507,6 +4512,7 @@ x86_emulate( case 0xad: /* shrd %%cl,r,r/m */ { uint8_t shift, width =3D dst.bytes << 3; =20 + generate_exception_if(lock_prefix, EXC_UD, -1); if ( b & 1 ) shift =3D _regs.ecx; else --=__PartDDEB344B.1__= Content-Type: text/plain; name="x86emul-lock-handling.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86emul-lock-handling.patch" x86emul: improve LOCK handling=0A=0ACertain opcodes would so far not have = got #UD when a LOCK prefix was=0Apresent. Adjust this by=0A- moving the = too early generic check into destination operand decoding,=0A where = DstNone and DstReg already have respective handling=0A- switching source = and destination of TEST r,r/m, for it to be taken=0A care of by aforementi= oned generic checks=0A- explicitly dealing with all forms of CMP, SHLD, = SHRD, as well as=0A TEST $imm,r/m=0A=0ATo make the handling of opcodes F6 = and F7 more obvious, reduce the=0Aamount of state set in the table, and = adjust the respective switch()=0Astatement accordingly.=0A=0AAlso = eliminate the latent bug of the check in DstNone handling not=0Aconsidering= the opcode extension set.=0A=0ASigned-off-by: Jan Beulich =0A---=0AThis will only apply cleanly on top of=0Ahttps://lists.xenproje= ct.org/archives/html/xen-devel/2016-08/msg01975.html.=0A=0A--- a/xen/arch/x= 86/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/x86_emulate/x86_emulate.c= =0A@@ -110,7 +110,7 @@ static uint8_t opcode_table[256] =3D {=0A /* = 0x80 - 0x87 */=0A ByteOp|DstMem|SrcImm|ModRM, DstMem|SrcImm|ModRM,=0A = ByteOp|DstMem|SrcImm|ModRM, DstMem|SrcImmByte|ModRM,=0A- ByteOp|DstMe= m|SrcReg|ModRM, DstMem|SrcReg|ModRM,=0A+ ByteOp|DstReg|SrcMem|ModRM, = DstReg|SrcMem|ModRM,=0A ByteOp|DstMem|SrcReg|ModRM, DstMem|SrcReg|ModRM= ,=0A /* 0x88 - 0x8F */=0A ByteOp|DstMem|SrcReg|ModRM|Mov, = DstMem|SrcReg|ModRM|Mov,=0A@@ -169,8 +169,7 @@ static uint8_t opcode_table[= 256] =3D {=0A DstEax|SrcImplicit, DstEax|SrcImplicit, ImplicitOps, = ImplicitOps,=0A /* 0xF0 - 0xF7 */=0A 0, ImplicitOps, 0, 0,=0A- = ImplicitOps, ImplicitOps,=0A- ByteOp|DstMem|SrcNone|ModRM, DstMem|SrcNon= e|ModRM,=0A+ ImplicitOps, ImplicitOps, ByteOp|ModRM, ModRM,=0A /* = 0xF8 - 0xFF */=0A ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,= =0A ImplicitOps, ImplicitOps, ByteOp|DstMem|SrcNone|ModRM, DstMem|SrcNo= ne|ModRM=0A@@ -1651,9 +1650,6 @@ x86_emulate(=0A }=0A }=0A = =0A- /* Lock prefix is allowed only on RMW instructions. */=0A- = generate_exception_if((d & Mov) && lock_prefix, EXC_UD, -1);=0A-=0A /* = ModRM and SIB bytes. */=0A if ( d & ModRM )=0A {=0A@@ -1729,13 = +1725,17 @@ x86_emulate(=0A switch ( modrm_reg & 7 )=0A = {=0A case 0 ... 1: /* test */=0A- = d =3D (d & ~SrcMask) | SrcImm;=0A+ d |=3D DstMem = | SrcImm;=0A+ break;=0A+ case 2: /* not = */=0A+ case 3: /* neg */=0A+ d |=3D = DstMem;=0A break;=0A case 4: /* mul = */=0A case 5: /* imul */=0A case 6: /* div = */=0A case 7: /* idiv */=0A- d =3D (d & = (ByteOp | ModRM)) | DstEax | SrcMem;=0A+ d |=3D DstEax = | SrcMem;=0A break;=0A }=0A = break;=0A@@ -1983,8 +1983,9 @@ x86_emulate(=0A */=0A = generate_exception_if(=0A lock_prefix &&=0A- ((b < = 0x20) || (b > 0x23)) && /* MOV CRn/DRn */=0A- (b !=3D 0xc7), = /* CMPXCHG{8,16}B */=0A+ (ext !=3D ext_0f ||=0A+ = (((b < 0x20) || (b > 0x23)) && /* MOV CRn/DRn */=0A+ = (b !=3D 0xc7))), /* CMPXCHG{8,16}B */=0A = EXC_UD, -1);=0A dst.type =3D OP_NONE;=0A break;=0A@@ = -2062,6 +2063,8 @@ x86_emulate(=0A goto done;=0A = dst.orig_val =3D dst.val;=0A }=0A+ else /* Lock prefix is = allowed only on RMW instructions. */=0A+ generate_exception_if(l= ock_prefix, EXC_UD, -1);=0A break;=0A }=0A =0A@@ -2111,6 = +2114,7 @@ x86_emulate(=0A break;=0A =0A case 0x38 ... 0x3d: = cmp: /* cmp */=0A+ generate_exception_if(lock_prefix, EXC_UD, = -1);=0A emulate_2op_SrcV("cmp", src, dst, _regs.eflags);=0A = dst.type =3D OP_NONE;=0A break;=0A@@ -3545,6 +3549,7 @@ x86_emulat= e(=0A unsigned long u[2], v;=0A =0A case 0 ... 1: /* = test */=0A+ generate_exception_if(lock_prefix, EXC_UD, -1);=0A = goto test;=0A case 2: /* not */=0A dst.val = =3D ~dst.val;=0A@@ -4507,6 +4512,7 @@ x86_emulate(=0A case 0xad: /* = shrd %%cl,r,r/m */ {=0A uint8_t shift, width =3D dst.bytes << = 3;=0A =0A+ generate_exception_if(lock_prefix, EXC_UD, -1);=0A = if ( b & 1 )=0A shift =3D _regs.ecx;=0A else=0A --=__PartDDEB344B.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartDDEB344B.1__=--