From: "Jan Beulich" Subject: [PATCH v5 10/17] x86emul: add tables for 0f38 and 0f3a extension space Date: Fri, 03 Mar 2017 08:03:25 -0700 Message-ID: <58B993CD020000780013FE76@prv-mh.provo.novell.com> References: <58B9902A020000780013FDD0@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartA1984DAD.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 1cjojz-0003Dn-BV for xen-devel@lists.xenproject.org; Fri, 03 Mar 2017 15:03:31 +0000 In-Reply-To: <58B9902A020000780013FDD0@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. --=__PartA1984DAD.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Convert the few existing opcodes so far supported. Signed-off-by: Jan Beulich --- v5: Add comment explaining the precise meaning of "two_op" and "four_op". to_memory -> to_mem. v3: New. --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -43,6 +43,8 @@ #define SrcMask (7<<3) /* Generic ModRM decode. */ #define ModRM (1<<6) +/* vSIB addressing mode (0f38 extension opcodes only), aliasing ModRM. */ +#define vSIB (1<<6) /* Destination is only written; never read. */ #define Mov (1<<7) /* VEX/EVEX (SIMD only): 2nd source operand unused (must be all ones) */ @@ -340,6 +342,33 @@ static const struct { [0xff] =3D { ModRM } }; =20 +/* + * "two_op" and "four_op" below refer to the number of register operands + * (one of which possibly also allowing to be a memory one). The named + * operand counts do not include any immediate operands. + */ +static const struct { + uint8_t simd_size:5; + uint8_t to_mem:1; + uint8_t two_op:1; + uint8_t vsib:1; +} ext0f38_table[256] =3D { + [0x2a] =3D { .simd_size =3D simd_packed_int, .two_op =3D 1 }, + [0xf0] =3D { .two_op =3D 1 }, + [0xf1] =3D { .to_mem =3D 1, .two_op =3D 1 }, + [0xf2 ... 0xf3] =3D {}, + [0xf5 ... 0xf7] =3D {}, +}; + +static const struct { + uint8_t simd_size:5; + uint8_t to_mem:1; + uint8_t two_op:1; + uint8_t four_op:1; +} ext0f3a_table[256] =3D { + [0xf0] =3D {}, +}; + static const opcode_desc_t xop_table[] =3D { DstReg|SrcImmByte|ModRM, DstReg|SrcMem|ModRM, @@ -2134,7 +2163,7 @@ x86_decode_onebyte( /* fall through */ case 3: /* call (far, absolute indirect) */ case 5: /* jmp (far, absolute indirect) */ - state->desc =3D DstNone | SrcMem | ModRM | Mov; + state->desc =3D DstNone | SrcMem | Mov; break; } break; @@ -2204,7 +2233,7 @@ x86_decode_twobyte( if ( vex.pfx =3D=3D vex_f3 ) /* movq xmm/m64,xmm */ { case X86EMUL_OPC_VEX_F3(0, 0x7e): /* vmovq xmm/m64,xmm */ - state->desc =3D DstImplicit | SrcMem | ModRM | Mov; + state->desc =3D DstImplicit | SrcMem | Mov; state->simd_size =3D simd_other; /* Avoid the state->desc adjustment below. */ return X86EMUL_OKAY; @@ -2218,12 +2247,12 @@ x86_decode_twobyte( switch ( modrm_reg & 7 ) { case 2: /* {,v}ldmxcsr */ - state->desc =3D DstImplicit | SrcMem | ModRM | Mov; + state->desc =3D DstImplicit | SrcMem | Mov; op_bytes =3D 4; break; =20 case 3: /* {,v}stmxcsr */ - state->desc =3D DstMem | SrcImplicit | ModRM | Mov; + state->desc =3D DstMem | SrcImplicit | Mov; op_bytes =3D 4; break; } @@ -2244,7 +2273,7 @@ x86_decode_twobyte( ctxt->opcode |=3D MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK); /* fall through */ case X86EMUL_OPC_VEX_66(0, 0xc4): /* vpinsrw */ - state->desc =3D DstReg | SrcMem16 | ModRM; + state->desc =3D DstReg | SrcMem16; break; } =20 @@ -2280,8 +2309,8 @@ x86_decode_0f38( break; =20 case 0xf1: /* movbe / crc32 */ - if ( !repne_prefix() ) - state->desc =3D (state->desc & ~(DstMask | SrcMask)) | DstMem = | SrcReg | Mov; + if ( repne_prefix() ) + state->desc =3D DstReg | SrcMem; if ( rep_prefix() ) ctxt->opcode |=3D MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK); break; @@ -2532,10 +2561,7 @@ x86_decode( opcode |=3D b | MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK); =20 if ( !(d & ModRM) ) - { - modrm_reg =3D modrm_rm =3D modrm_mod =3D modrm =3D 0; break; - } =20 modrm =3D insn_fetch_type(uint8_t); modrm_mod =3D (modrm & 0xc0) >> 6; @@ -2546,6 +2572,8 @@ x86_decode( =20 if ( d & ModRM ) { + d &=3D ~ModRM; +#undef ModRM /* Only its aliases are valid to use from here on. */ modrm_reg =3D ((rex_prefix & 4) << 1) | ((modrm & 0x38) >> 3); modrm_rm =3D modrm & 0x07; =20 @@ -2555,8 +2583,9 @@ x86_decode( * normally be only addition/removal of SrcImm/SrcImm16, so their * fetching can be taken care of by the common code below. */ - if ( ext =3D=3D ext_none ) + switch ( ext ) { + case ext_none: switch ( b ) { case 0xf6 ... 0xf7: /* Grp3 */ @@ -2582,6 +2611,25 @@ x86_decode( } break; } + break; + + case vex_0f38: + d =3D ext0f38_table[b].to_mem ? DstMem | SrcReg + : DstReg | SrcMem; + if ( ext0f38_table[b].two_op ) + d |=3D TwoOp; + if ( ext0f38_table[b].vsib ) + d |=3D vSIB; + state->simd_size =3D ext0f38_table[b].simd_size; + break; + + case vex_0f3a: + /* + * Cannot update d here yet, as the immediate operand still + * needs fetching. + */ + default: + break; } =20 if ( modrm_mod =3D=3D 3 ) @@ -2592,6 +2640,7 @@ x86_decode( else if ( ad_bytes =3D=3D 2 ) { /* 16-bit ModR/M decode. */ + generate_exception_if(d & vSIB, EXC_UD); ea.type =3D OP_MEM; switch ( modrm_rm ) { @@ -2648,7 +2697,7 @@ x86_decode( sib =3D insn_fetch_type(uint8_t); sib_index =3D ((sib >> 3) & 7) | ((rex_prefix << 2) & 8); sib_base =3D (sib & 7) | ((rex_prefix << 3) & 8); - if ( sib_index !=3D 4 ) + if ( sib_index !=3D 4 && !(d & vSIB) ) ea.mem.off =3D *(long *)decode_register(sib_index, state->regs, = 0); ea.mem.off <<=3D (sib >> 6) & 3; @@ -2674,6 +2723,7 @@ x86_decode( } else { + generate_exception_if(d & vSIB, EXC_UD); modrm_rm |=3D (rex_prefix & 1) << 3; ea.mem.off =3D *(long *)decode_register(modrm_rm, state->regs, 0); @@ -2697,6 +2747,11 @@ x86_decode( } } } + else + { + modrm_mod =3D 0xff; + modrm_reg =3D modrm_rm =3D modrm =3D 0; + } =20 if ( override_seg !=3D x86_seg_none ) ea.mem.seg =3D override_seg; @@ -2745,6 +2800,13 @@ x86_decode( break; =20 case ext_0f3a: + d =3D ext0f3a_table[b].to_mem ? DstMem | SrcReg : DstReg | = SrcMem; + if ( ext0f3a_table[b].two_op ) + d |=3D TwoOp; + else if ( ext0f3a_table[b].four_op && !mode_64bit() && vex.opcx ) + imm1 &=3D 0x7f; + state->desc =3D d; + state->simd_size =3D ext0f3a_table[b].simd_size; if ( !vex.opcx ) ctxt->opcode |=3D MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK); break; @@ -6853,7 +6915,6 @@ x86_emulate( if ( vex.l ) vcpu_must_have(avx2); } - state->simd_size =3D simd_packed_int; goto movdqa; =20 case X86EMUL_OPC(0x0f38, 0xf0): /* movbe m,r */ @@ -7402,7 +7463,7 @@ x86_insn_modrm(const struct x86_emulate_ { check_state(state); =20 - if ( !(state->desc & ModRM) ) + if ( state->modrm_mod > 3 ) return -EINVAL; =20 if ( rm ) --=__PartA1984DAD.1__= Content-Type: text/plain; name="x86emul-0f3x-tables.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86emul-0f3x-tables.patch" x86emul: add tables for 0f38 and 0f3a extension space=0A=0AConvert the few = existing opcodes so far supported.=0A=0ASigned-off-by: Jan Beulich = =0A---=0Av5: Add comment explaining the precise meaning = of "two_op" and=0A "four_op". to_memory -> to_mem.=0Av3: New.=0A=0A--- = a/xen/arch/x86/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/x86_emulate/x= 86_emulate.c=0A@@ -43,6 +43,8 @@=0A #define SrcMask (7<<3)=0A /* = Generic ModRM decode. */=0A #define ModRM (1<<6)=0A+/* vSIB = addressing mode (0f38 extension opcodes only), aliasing ModRM. */=0A+#defin= e vSIB (1<<6)=0A /* Destination is only written; never read. */=0A = #define Mov (1<<7)=0A /* VEX/EVEX (SIMD only): 2nd source operand = unused (must be all ones) */=0A@@ -340,6 +342,33 @@ static const struct = {=0A [0xff] =3D { ModRM }=0A };=0A =0A+/*=0A+ * "two_op" and "four_op" = below refer to the number of register operands=0A+ * (one of which = possibly also allowing to be a memory one). The named=0A+ * operand counts = do not include any immediate operands.=0A+ */=0A+static const struct {=0A+ = uint8_t simd_size:5;=0A+ uint8_t to_mem:1;=0A+ uint8_t = two_op:1;=0A+ uint8_t vsib:1;=0A+} ext0f38_table[256] =3D {=0A+ = [0x2a] =3D { .simd_size =3D simd_packed_int, .two_op =3D 1 },=0A+ = [0xf0] =3D { .two_op =3D 1 },=0A+ [0xf1] =3D { .to_mem =3D 1, .two_op = =3D 1 },=0A+ [0xf2 ... 0xf3] =3D {},=0A+ [0xf5 ... 0xf7] =3D = {},=0A+};=0A+=0A+static const struct {=0A+ uint8_t simd_size:5;=0A+ = uint8_t to_mem:1;=0A+ uint8_t two_op:1;=0A+ uint8_t four_op:1;=0A+} = ext0f3a_table[256] =3D {=0A+ [0xf0] =3D {},=0A+};=0A+=0A static const = opcode_desc_t xop_table[] =3D {=0A DstReg|SrcImmByte|ModRM,=0A = DstReg|SrcMem|ModRM,=0A@@ -2134,7 +2163,7 @@ x86_decode_onebyte(=0A = /* fall through */=0A case 3: /* call (far, absolute = indirect) */=0A case 5: /* jmp (far, absolute indirect) */=0A- = state->desc =3D DstNone | SrcMem | ModRM | Mov;=0A+ = state->desc =3D DstNone | SrcMem | Mov;=0A break;=0A = }=0A break;=0A@@ -2204,7 +2233,7 @@ x86_decode_twobyte(=0A = if ( vex.pfx =3D=3D vex_f3 ) /* movq xmm/m64,xmm */=0A {=0A = case X86EMUL_OPC_VEX_F3(0, 0x7e): /* vmovq xmm/m64,xmm */=0A- = state->desc =3D DstImplicit | SrcMem | ModRM | Mov;=0A+ = state->desc =3D DstImplicit | SrcMem | Mov;=0A state->simd_size= =3D simd_other;=0A /* Avoid the state->desc adjustment below. = */=0A return X86EMUL_OKAY;=0A@@ -2218,12 +2247,12 @@ x86_decode= _twobyte(=0A switch ( modrm_reg & 7 )=0A {=0A case = 2: /* {,v}ldmxcsr */=0A- state->desc =3D DstImplicit | SrcMem | = ModRM | Mov;=0A+ state->desc =3D DstImplicit | SrcMem | Mov;=0A = op_bytes =3D 4;=0A break;=0A =0A case 3: = /* {,v}stmxcsr */=0A- state->desc =3D DstMem | SrcImplicit | = ModRM | Mov;=0A+ state->desc =3D DstMem | SrcImplicit | Mov;=0A = op_bytes =3D 4;=0A break;=0A }=0A@@ = -2244,7 +2273,7 @@ x86_decode_twobyte(=0A ctxt->opcode |=3D = MASK_INSR(vex.pfx, X86EMUL_OPC_PFX_MASK);=0A /* fall through */=0A = case X86EMUL_OPC_VEX_66(0, 0xc4): /* vpinsrw */=0A- state->desc = =3D DstReg | SrcMem16 | ModRM;=0A+ state->desc =3D DstReg | = SrcMem16;=0A break;=0A }=0A =0A@@ -2280,8 +2309,8 @@ x86_decode= _0f38(=0A break;=0A =0A case 0xf1: /* movbe / crc32 */=0A- = if ( !repne_prefix() )=0A- state->desc =3D (state->desc & = ~(DstMask | SrcMask)) | DstMem | SrcReg | Mov;=0A+ if ( repne_prefix= () )=0A+ state->desc =3D DstReg | SrcMem;=0A if ( = rep_prefix() )=0A ctxt->opcode |=3D MASK_INSR(vex.pfx, = X86EMUL_OPC_PFX_MASK);=0A break;=0A@@ -2532,10 +2561,7 @@ = x86_decode(=0A opcode |=3D b | MASK_INSR(vex.pfx, = X86EMUL_OPC_PFX_MASK);=0A =0A if ( !(d & ModRM) )=0A- = {=0A- modrm_reg =3D modrm_rm =3D modrm_mod = =3D modrm =3D 0;=0A break;=0A- }=0A =0A = modrm =3D insn_fetch_type(uint8_t);=0A = modrm_mod =3D (modrm & 0xc0) >> 6;=0A@@ -2546,6 +2572,8 @@ x86_decode(=0A = =0A if ( d & ModRM )=0A {=0A+ d &=3D ~ModRM;=0A+#undef = ModRM /* Only its aliases are valid to use from here on. */=0A = modrm_reg =3D ((rex_prefix & 4) << 1) | ((modrm & 0x38) >> 3);=0A = modrm_rm =3D modrm & 0x07;=0A =0A@@ -2555,8 +2583,9 @@ x86_decode(=0A = * normally be only addition/removal of SrcImm/SrcImm16, so their=0A = * fetching can be taken care of by the common code below.=0A = */=0A- if ( ext =3D=3D ext_none )=0A+ switch ( ext )=0A = {=0A+ case ext_none:=0A switch ( b )=0A = {=0A case 0xf6 ... 0xf7: /* Grp3 */=0A@@ -2582,6 +2611,25 @@ = x86_decode(=0A }=0A break;=0A = }=0A+ break;=0A+=0A+ case vex_0f38:=0A+ d =3D = ext0f38_table[b].to_mem ? DstMem | SrcReg=0A+ = : DstReg | SrcMem;=0A+ if ( ext0f38_table[b].two_op = )=0A+ d |=3D TwoOp;=0A+ if ( ext0f38_table[b].vsi= b )=0A+ d |=3D vSIB;=0A+ state->simd_size =3D = ext0f38_table[b].simd_size;=0A+ break;=0A+=0A+ case = vex_0f3a:=0A+ /*=0A+ * Cannot update d here yet, as = the immediate operand still=0A+ * needs fetching.=0A+ = */=0A+ default:=0A+ break;=0A }=0A =0A = if ( modrm_mod =3D=3D 3 )=0A@@ -2592,6 +2640,7 @@ x86_decode(=0A = else if ( ad_bytes =3D=3D 2 )=0A {=0A /* 16-bit ModR/M = decode. */=0A+ generate_exception_if(d & vSIB, EXC_UD);=0A = ea.type =3D OP_MEM;=0A switch ( modrm_rm )=0A = {=0A@@ -2648,7 +2697,7 @@ x86_decode(=0A sib =3D = insn_fetch_type(uint8_t);=0A sib_index =3D ((sib >> 3) & = 7) | ((rex_prefix << 2) & 8);=0A sib_base =3D (sib & 7) | = ((rex_prefix << 3) & 8);=0A- if ( sib_index !=3D 4 )=0A+ = if ( sib_index !=3D 4 && !(d & vSIB) )=0A = ea.mem.off =3D *(long *)decode_register(sib_index,=0A = state->regs, 0);=0A = ea.mem.off <<=3D (sib >> 6) & 3;=0A@@ -2674,6 +2723,7 @@ x86_decode(=0A = }=0A else=0A {=0A+ = generate_exception_if(d & vSIB, EXC_UD);=0A modrm_rm |=3D = (rex_prefix & 1) << 3;=0A ea.mem.off =3D *(long *)decode_re= gister(modrm_rm,=0A = state->regs, 0);=0A@@ -2697,6 +2747,11 @@ x86_decode(=0A }=0A = }=0A }=0A+ else=0A+ {=0A+ modrm_mod =3D 0xff;=0A+ = modrm_reg =3D modrm_rm =3D modrm =3D 0;=0A+ }=0A =0A if ( = override_seg !=3D x86_seg_none )=0A ea.mem.seg =3D override_seg;=0A= @@ -2745,6 +2800,13 @@ x86_decode(=0A break;=0A =0A case = ext_0f3a:=0A+ d =3D ext0f3a_table[b].to_mem ? DstMem | SrcReg : = DstReg | SrcMem;=0A+ if ( ext0f3a_table[b].two_op )=0A+ = d |=3D TwoOp;=0A+ else if ( ext0f3a_table[b].four_op && !mode_64bit(= ) && vex.opcx )=0A+ imm1 &=3D 0x7f;=0A+ state->desc =3D = d;=0A+ state->simd_size =3D ext0f3a_table[b].simd_size;=0A = if ( !vex.opcx )=0A ctxt->opcode |=3D MASK_INSR(vex.pfx, = X86EMUL_OPC_PFX_MASK);=0A break;=0A@@ -6853,7 +6915,6 @@ x86_emulat= e(=0A if ( vex.l )=0A vcpu_must_have(avx2);=0A = }=0A- state->simd_size =3D simd_packed_int;=0A goto = movdqa;=0A =0A case X86EMUL_OPC(0x0f38, 0xf0): /* movbe m,r */=0A@@ = -7402,7 +7463,7 @@ x86_insn_modrm(const struct x86_emulate_=0A {=0A = check_state(state);=0A =0A- if ( !(state->desc & ModRM) )=0A+ if ( = state->modrm_mod > 3 )=0A return -EINVAL;=0A =0A if ( rm )=0A --=__PartA1984DAD.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartA1984DAD.1__=--