Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: x86: Add emulator support for decoding VEX prefixes

After all the changes done in the previous patches, the only thing
left to support AVX MOV instructions is to expand the VEX prefix into
the appropriate REX, 66/F3/F2 and map prefixes. Three-operand
instructions are not supported.

The Avx bit in this case is not cleared, in fact it is used as the
sign that the instruction does support VEX encoding. Until it is
added to any instruction, however, the only functional change is
to change some not-implemented instructions to #UD if they correspond
to a VEX prefix with an invalid map.

Co-developed-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Link: https://patch.msgid.link/20251114003633.60689-10-pbonzini@redhat.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

authored by

Paolo Bonzini and committed by
Sean Christopherson
f0585a71 825f0aec

+112 -10
+112 -10
arch/x86/kvm/emulate.c
··· 3963 3963 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ 3964 3964 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) 3965 3965 3966 + static const struct opcode ud = I(SrcNone, emulate_ud); 3967 + 3966 3968 static const struct opcode group7_rm0[] = { 3967 3969 N, 3968 3970 I(SrcNone | Priv | EmulateOnUD, em_hypercall), ··· 4764 4762 return rc; 4765 4763 } 4766 4764 4765 + static int x86_decode_avx(struct x86_emulate_ctxt *ctxt, 4766 + u8 vex_1st, u8 vex_2nd, struct opcode *opcode) 4767 + { 4768 + u8 vex_3rd, map, pp, l, v; 4769 + int rc = X86EMUL_CONTINUE; 4770 + 4771 + if (ctxt->rep_prefix || ctxt->op_prefix || ctxt->rex_prefix) 4772 + goto ud; 4773 + 4774 + if (vex_1st == 0xc5) { 4775 + /* Expand RVVVVlpp to VEX3 format */ 4776 + vex_3rd = vex_2nd & ~0x80; /* VVVVlpp from VEX2, w=0 */ 4777 + vex_2nd = (vex_2nd & 0x80) | 0x61; /* R from VEX2, X=1 B=1 mmmmm=00001 */ 4778 + } else { 4779 + vex_3rd = insn_fetch(u8, ctxt); 4780 + } 4781 + 4782 + /* vex_2nd = RXBmmmmm, vex_3rd = wVVVVlpp. Fix polarity */ 4783 + vex_2nd ^= 0xE0; /* binary 11100000 */ 4784 + vex_3rd ^= 0x78; /* binary 01111000 */ 4785 + 4786 + ctxt->rex_prefix = REX_PREFIX; 4787 + ctxt->rex_bits = (vex_2nd & 0xE0) >> 5; /* RXB */ 4788 + ctxt->rex_bits |= (vex_3rd & 0x80) >> 4; /* w */ 4789 + if (ctxt->rex_bits && ctxt->mode != X86EMUL_MODE_PROT64) 4790 + goto ud; 4791 + 4792 + map = vex_2nd & 0x1f; 4793 + v = (vex_3rd >> 3) & 0xf; 4794 + l = vex_3rd & 0x4; 4795 + pp = vex_3rd & 0x3; 4796 + 4797 + ctxt->b = insn_fetch(u8, ctxt); 4798 + switch (map) { 4799 + case 1: 4800 + ctxt->opcode_len = 2; 4801 + *opcode = twobyte_table[ctxt->b]; 4802 + break; 4803 + case 2: 4804 + ctxt->opcode_len = 3; 4805 + *opcode = opcode_map_0f_38[ctxt->b]; 4806 + break; 4807 + case 3: 4808 + /* no 0f 3a instructions are supported yet */ 4809 + return X86EMUL_UNHANDLEABLE; 4810 + default: 4811 + goto ud; 4812 + } 4813 + 4814 + /* 4815 + * No three operand instructions are supported yet; those that 4816 + * *are* marked with the Avx flag reserve the VVVV flag. 4817 + */ 4818 + if (v) 4819 + goto ud; 4820 + 4821 + if (l) 4822 + ctxt->op_bytes = 32; 4823 + else 4824 + ctxt->op_bytes = 16; 4825 + 4826 + switch (pp) { 4827 + case 0: break; 4828 + case 1: ctxt->op_prefix = true; break; 4829 + case 2: ctxt->rep_prefix = 0xf3; break; 4830 + case 3: ctxt->rep_prefix = 0xf2; break; 4831 + } 4832 + 4833 + done: 4834 + return rc; 4835 + ud: 4836 + *opcode = ud; 4837 + return rc; 4838 + } 4839 + 4767 4840 int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type) 4768 4841 { 4769 4842 int rc = X86EMUL_CONTINUE; 4770 4843 int mode = ctxt->mode; 4771 4844 int def_op_bytes, def_ad_bytes, goffset, simd_prefix; 4845 + bool vex_prefix = false; 4772 4846 bool has_seg_override = false; 4773 4847 struct opcode opcode; 4774 4848 u16 dummy; ··· 4961 4883 ctxt->op_bytes = 8; 4962 4884 4963 4885 /* Opcode byte(s). */ 4964 - if (ctxt->b == 0x0f) { 4886 + if (ctxt->b == 0xc4 || ctxt->b == 0xc5) { 4887 + /* VEX or LDS/LES */ 4888 + u8 vex_2nd = insn_fetch(u8, ctxt); 4889 + if (mode != X86EMUL_MODE_PROT64 && (vex_2nd & 0xc0) != 0xc0) { 4890 + opcode = opcode_table[ctxt->b]; 4891 + ctxt->modrm = vex_2nd; 4892 + /* the Mod/RM byte has been fetched already! */ 4893 + goto done_modrm; 4894 + } 4895 + 4896 + vex_prefix = true; 4897 + rc = x86_decode_avx(ctxt, ctxt->b, vex_2nd, &opcode); 4898 + if (rc != X86EMUL_CONTINUE) 4899 + goto done; 4900 + } else if (ctxt->b == 0x0f) { 4965 4901 /* Two- or three-byte opcode */ 4966 4902 ctxt->opcode_len = 2; 4967 4903 ctxt->b = insn_fetch(u8, ctxt); ··· 4991 4899 /* Opcode byte(s). */ 4992 4900 opcode = opcode_table[ctxt->b]; 4993 4901 } 4994 - ctxt->d = opcode.flags; 4995 4902 4996 - if (ctxt->d & ModRM) 4903 + if (opcode.flags & ModRM) 4997 4904 ctxt->modrm = insn_fetch(u8, ctxt); 4998 4905 4999 - /* vex-prefix instructions are not implemented */ 5000 - if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && 5001 - (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) { 5002 - ctxt->d = NotImpl; 5003 - } 5004 - 4906 + done_modrm: 4907 + ctxt->d = opcode.flags; 5005 4908 while (ctxt->d & GroupMask) { 5006 4909 switch (ctxt->d & GroupMask) { 5007 4910 case Group: ··· 5061 4974 /* Unrecognised? */ 5062 4975 if (ctxt->d == 0) 5063 4976 return EMULATION_FAILED; 4977 + 4978 + if (unlikely(vex_prefix)) { 4979 + /* 4980 + * Only specifically marked instructions support VEX. Since many 4981 + * instructions support it but are not annotated, return not implemented 4982 + * rather than #UD. 4983 + */ 4984 + if (!(ctxt->d & Avx)) 4985 + return EMULATION_FAILED; 4986 + 4987 + if (!(ctxt->d & AlignMask)) 4988 + ctxt->d |= Unaligned; 4989 + } 5064 4990 5065 4991 ctxt->execute = opcode.u.execute; 5066 4992 ··· 5145 5045 if ((ctxt->d & No16) && ctxt->op_bytes == 2) 5146 5046 ctxt->op_bytes = 4; 5147 5047 5148 - if (ctxt->d & Sse) 5048 + if (vex_prefix) 5049 + ; 5050 + else if (ctxt->d & Sse) 5149 5051 ctxt->op_bytes = 16, ctxt->d &= ~Avx; 5150 5052 else if (ctxt->d & Mmx) 5151 5053 ctxt->op_bytes = 8;