From: "Jan Beulich" Subject: [PATCH 09/17] SVM: use generic instruction decoding Date: Thu, 08 Sep 2016 07:14:55 -0600 Message-ID: <57D1806F020000780010D18F@prv-mh.provo.novell.com> References: <57D17C78020000780010D127@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartD5E35E5F.2__=" 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 1bhzAS-00017J-NB for xen-devel@lists.xenproject.org; Thu, 08 Sep 2016 13:15:01 +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 , Boris Ostrovsky , Suravee Suthikulpanit 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. --=__PartD5E35E5F.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... instead of custom handling. To facilitate this break out init code from _hvm_emulate_one() into the new hvm_emulate_init(), and make hvmemul_insn_fetch( globally available. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -835,7 +835,7 @@ static int hvmemul_read( container_of(ctxt, struct hvm_emulate_ctxt, ctxt)); } =20 -static int hvmemul_insn_fetch( +int hvmemul_insn_fetch( enum x86_segment seg, unsigned long offset, void *p_data, @@ -1765,15 +1765,14 @@ static const struct x86_emulate_ops hvm_ .vmfunc =3D hvmemul_vmfunc, }; =20 -static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt, - const struct x86_emulate_ops *ops) +void hvm_emulate_init( + struct hvm_emulate_ctxt *hvmemul_ctxt, + const unsigned char *insn_buf, + unsigned int insn_bytes) { - struct cpu_user_regs *regs =3D hvmemul_ctxt->ctxt.regs; struct vcpu *curr =3D current; - uint32_t new_intr_shadow, pfec =3D PFEC_page_present; - struct hvm_vcpu_io *vio =3D &curr->arch.hvm_vcpu.hvm_io; + unsigned int pfec =3D PFEC_page_present; unsigned long addr; - int rc; =20 if ( hvm_long_mode_enabled(curr) && hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l ) @@ -1791,14 +1790,14 @@ static int _hvm_emulate_one(struct hvm_e if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl =3D=3D 3 ) pfec |=3D PFEC_user_mode; =20 - hvmemul_ctxt->insn_buf_eip =3D regs->eip; - if ( !vio->mmio_insn_bytes ) + hvmemul_ctxt->insn_buf_eip =3D hvmemul_ctxt->ctxt.regs->eip; + if ( !insn_bytes ) { hvmemul_ctxt->insn_buf_bytes =3D hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?: (hvm_virtual_to_linear_addr(x86_seg_cs, &hvmemul_ctxt->seg_reg[x86_seg_cs]= , - regs->eip, + hvmemul_ctxt->insn_buf_eip, sizeof(hvmemul_ctxt->insn_buf), hvm_access_insn_fetch, hvmemul_ctxt->ctxt.addr_size, @@ -1810,11 +1809,24 @@ static int _hvm_emulate_one(struct hvm_e } else { - hvmemul_ctxt->insn_buf_bytes =3D vio->mmio_insn_bytes; - memcpy(hvmemul_ctxt->insn_buf, vio->mmio_insn, vio->mmio_insn_byte= s); + hvmemul_ctxt->insn_buf_bytes =3D insn_bytes; + memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes); } =20 hvmemul_ctxt->exn_pending =3D 0; +} + +static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt, + const struct x86_emulate_ops *ops) +{ + const struct cpu_user_regs *regs =3D hvmemul_ctxt->ctxt.regs; + struct vcpu *curr =3D current; + uint32_t new_intr_shadow; + struct hvm_vcpu_io *vio =3D &curr->arch.hvm_vcpu.hvm_io; + int rc; + + hvm_emulate_init(hvmemul_ctxt, vio->mmio_insn, vio->mmio_insn_bytes); + vio->mmio_retry =3D 0; =20 if ( cpu_has_vmx ) --- a/xen/arch/x86/hvm/svm/emulate.c +++ b/xen/arch/x86/hvm/svm/emulate.c @@ -15,7 +15,7 @@ * this program; If not, see . */ =20 -#include +#include #include #include #include @@ -26,41 +26,6 @@ #include #include =20 -static unsigned int is_prefix(u8 opc) -{ - switch ( opc ) - { - case 0x66: - case 0x67: - case 0x2E: - case 0x3E: - case 0x26: - case 0x64: - case 0x65: - case 0x36: - case 0xF0: - case 0xF3: - case 0xF2: - case 0x40 ... 0x4f: - return 1; - } - return 0; -} - -static unsigned long svm_rip2pointer(struct vcpu *v, unsigned long = *limit) -{ - struct vmcb_struct *vmcb =3D v->arch.hvm_svm.vmcb; - unsigned long p =3D vmcb->cs.base + vmcb->rip; - - if ( !(vmcb->cs.attr.fields.l && hvm_long_mode_enabled(v)) ) - { - *limit =3D vmcb->cs.limit; - return (u32)p; /* mask to 32 bits */ - } - *limit =3D ~0UL; - return p; -} - static unsigned long svm_nextrip_insn_length(struct vcpu *v) { struct vmcb_struct *vmcb =3D v->arch.hvm_svm.vmcb; @@ -89,141 +54,96 @@ static unsigned long svm_nextrip_insn_le return vmcb->nextrip - vmcb->rip; } =20 -/* First byte: Length. Following bytes: Opcode bytes. */ -#define MAKE_INSTR(nm, ...) static const u8 OPCODE_##nm[] =3D { __VA_ARGS_= _ } -MAKE_INSTR(INVD, 2, 0x0f, 0x08); -MAKE_INSTR(WBINVD, 2, 0x0f, 0x09); -MAKE_INSTR(CPUID, 2, 0x0f, 0xa2); -MAKE_INSTR(RDMSR, 2, 0x0f, 0x32); -MAKE_INSTR(WRMSR, 2, 0x0f, 0x30); -MAKE_INSTR(VMCALL, 3, 0x0f, 0x01, 0xd9); -MAKE_INSTR(HLT, 1, 0xf4); -MAKE_INSTR(INT3, 1, 0xcc); -MAKE_INSTR(RDTSC, 2, 0x0f, 0x31); -MAKE_INSTR(PAUSE, 1, 0x90); -MAKE_INSTR(XSETBV, 3, 0x0f, 0x01, 0xd1); -MAKE_INSTR(VMRUN, 3, 0x0f, 0x01, 0xd8); -MAKE_INSTR(VMLOAD, 3, 0x0f, 0x01, 0xda); -MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, 0xdb); -MAKE_INSTR(STGI, 3, 0x0f, 0x01, 0xdc); -MAKE_INSTR(CLGI, 3, 0x0f, 0x01, 0xdd); -MAKE_INSTR(INVLPGA,3, 0x0f, 0x01, 0xdf); - -static const u8 *const opc_bytes[INSTR_MAX_COUNT] =3D -{ - [INSTR_INVD] =3D OPCODE_INVD, - [INSTR_WBINVD] =3D OPCODE_WBINVD, - [INSTR_CPUID] =3D OPCODE_CPUID, - [INSTR_RDMSR] =3D OPCODE_RDMSR, - [INSTR_WRMSR] =3D OPCODE_WRMSR, - [INSTR_VMCALL] =3D OPCODE_VMCALL, - [INSTR_HLT] =3D OPCODE_HLT, - [INSTR_INT3] =3D OPCODE_INT3, - [INSTR_RDTSC] =3D OPCODE_RDTSC, - [INSTR_PAUSE] =3D OPCODE_PAUSE, - [INSTR_XSETBV] =3D OPCODE_XSETBV, - [INSTR_VMRUN] =3D OPCODE_VMRUN, - [INSTR_VMLOAD] =3D OPCODE_VMLOAD, - [INSTR_VMSAVE] =3D OPCODE_VMSAVE, - [INSTR_STGI] =3D OPCODE_STGI, - [INSTR_CLGI] =3D OPCODE_CLGI, - [INSTR_INVLPGA] =3D OPCODE_INVLPGA, +static const struct { + unsigned int opcode; + struct { + unsigned int rm:3; + unsigned int reg:3; + unsigned int mod:2; +#define MODRM(mod, reg, rm) { rm, reg, mod } + } modrm; +} const opc_tab[INSTR_MAX_COUNT] =3D { + [INSTR_PAUSE] =3D { X86EMUL_OPC_F3(0, 0x90) }, + [INSTR_INT3] =3D { X86EMUL_OPC( 0, 0xcc) }, + [INSTR_HLT] =3D { X86EMUL_OPC( 0, 0xf4) }, + [INSTR_XSETBV] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 2, 1) }, + [INSTR_VMRUN] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 0) }, + [INSTR_VMCALL] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 1) }, + [INSTR_VMLOAD] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 2) }, + [INSTR_VMSAVE] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 3) }, + [INSTR_STGI] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 4) }, + [INSTR_CLGI] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 5) }, + [INSTR_INVLPGA] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 7) }, + [INSTR_INVD] =3D { X86EMUL_OPC(0x0f, 0x08) }, + [INSTR_WBINVD] =3D { X86EMUL_OPC(0x0f, 0x09) }, + [INSTR_WRMSR] =3D { X86EMUL_OPC(0x0f, 0x30) }, + [INSTR_RDTSC] =3D { X86EMUL_OPC(0x0f, 0x31) }, + [INSTR_RDMSR] =3D { X86EMUL_OPC(0x0f, 0x32) }, + [INSTR_CPUID] =3D { X86EMUL_OPC(0x0f, 0xa2) }, }; =20 -static bool_t fetch(const struct vmcb_struct *vmcb, u8 *buf, - unsigned long addr, unsigned int len) -{ - uint32_t pfec =3D (vmcb_get_cpl(vmcb) =3D=3D 3) ? PFEC_user_mode : 0; - - switch ( hvm_fetch_from_guest_virt(buf, addr, len, pfec) ) - { - case HVMCOPY_okay: - break; - case HVMCOPY_bad_gva_to_gfn: - /* OK just to give up; we'll have injected #PF already */ - return 0; - default: - /* Not OK: fetches from non-RAM pages are not supportable. */ - gdprintk(XENLOG_WARNING, "Bad instruction fetch at %#lx (%#lx)\n",= - vmcb->rip, addr); - hvm_inject_hw_exception(TRAP_gp_fault, 0); - return 0; - } - return 1; -} - int __get_instruction_length_from_list(struct vcpu *v, const enum instruction_index *list, unsigned int list_count) { struct vmcb_struct *vmcb =3D v->arch.hvm_svm.vmcb; - unsigned int i, j, inst_len =3D 0; - enum instruction_index instr =3D 0; - u8 buf[MAX_INST_LEN]; - const u8 *opcode =3D NULL; - unsigned long fetch_addr, fetch_limit; - unsigned int fetch_len, max_len; + struct hvm_emulate_ctxt ctxt; + struct x86_emulate_state *state; + unsigned int inst_len, j, modrm_rm, modrm_reg; + int modrm_mod; =20 +#ifdef NDEBUG if ( (inst_len =3D svm_nextrip_insn_length(v)) !=3D 0 ) return inst_len; =20 if ( vmcb->exitcode =3D=3D VMEXIT_IOIO ) return vmcb->exitinfo2 - vmcb->rip; +#endif =20 - /* Fetch up to the next page break; we'll fetch from the next page - * later if we have to. */ - fetch_addr =3D svm_rip2pointer(v, &fetch_limit); - if ( vmcb->rip > fetch_limit ) - return 0; - max_len =3D min(fetch_limit - vmcb->rip + 1, MAX_INST_LEN + 0UL); - fetch_len =3D min_t(unsigned int, max_len, - PAGE_SIZE - (fetch_addr & ~PAGE_MASK)); - if ( !fetch(vmcb, buf, fetch_addr, fetch_len) ) + ASSERT(v =3D=3D current); + hvm_emulate_prepare(&ctxt, guest_cpu_user_regs()); + hvm_emulate_init(&ctxt, NULL, 0); + state =3D x86_decode_insn(&ctxt.ctxt, hvmemul_insn_fetch); + if ( IS_ERR_OR_NULL(state) ) return 0; =20 - while ( (inst_len < max_len) && is_prefix(buf[inst_len]) ) - { - inst_len++; - if ( inst_len >=3D fetch_len ) - { - if ( !fetch(vmcb, buf + fetch_len, fetch_addr + fetch_len, - max_len - fetch_len) ) - return 0; - fetch_len =3D max_len; - } + inst_len =3D x86_insn_length(state, &ctxt.ctxt); + modrm_mod =3D x86_insn_modrm(state, &modrm_rm, &modrm_reg); + x86_emulate_free_state(state); +#ifndef NDEBUG + if ( vmcb->exitcode =3D=3D VMEXIT_IOIO ) + j =3D vmcb->exitinfo2 - vmcb->rip; + else + j =3D svm_nextrip_insn_length(v); + if ( j && j !=3D inst_len ) + { + gprintk(XENLOG_WARNING, "insn-len[%02x]=3D%u (exp %u)\n", + ctxt.ctxt.opcode, inst_len, j); + return j; } +#endif =20 for ( j =3D 0; j < list_count; j++ ) { - instr =3D list[j]; - opcode =3D opc_bytes[instr]; + enum instruction_index instr =3D list[j]; =20 - for ( i =3D 0; (i < opcode[0]) && ((inst_len + i) < max_len); i++ = ) + ASSERT(instr >=3D 0 && instr < ARRAY_SIZE(opc_tab)); + if ( opc_tab[instr].opcode =3D=3D ctxt.ctxt.opcode ) { - if ( (inst_len + i) >=3D fetch_len )=20 - { - if ( !fetch(vmcb, buf + fetch_len, fetch_addr + fetch_len,= - max_len - fetch_len) ) - return 0; - fetch_len =3D max_len; - } + if ( !opc_tab[instr].modrm.mod ) + return inst_len; =20 - if ( buf[inst_len+i] !=3D opcode[i+1] ) - goto mismatch; + if ( modrm_mod =3D=3D opc_tab[instr].modrm.mod && + (modrm_rm & 7) =3D=3D opc_tab[instr].modrm.rm && + (modrm_reg & 7) =3D=3D opc_tab[instr].modrm.reg ) + return inst_len; } - goto done; - mismatch: ; } =20 gdprintk(XENLOG_WARNING, - "%s: Mismatch between expected and actual instruction bytes: = " + "%s: Mismatch between expected and actual instruction: " "eip =3D %lx\n", __func__, (unsigned long)vmcb->rip); hvm_inject_hw_exception(TRAP_gp_fault, 0); return 0; - - done: - inst_len +=3D opcode[0]; - ASSERT(inst_len <=3D max_len); - return inst_len; } =20 /* --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -382,7 +382,7 @@ struct operand { } mem; }; #ifdef __x86_64__ -#define REG_POISON ((unsigned long *) 0x8086000000008086UL) /* non-canonic= al */ +#define REG_POISON ((void *)0x8086000000008086UL) /* non-canonical */ #else #define REG_POISON NULL /* 32-bit builds are for user-space, so NULL is = OK. */ #endif @@ -1631,6 +1631,10 @@ struct x86_emulate_state { =20 unsigned long eip; struct cpu_user_regs *regs; + +#ifndef NDEBUG + void *caller; +#endif }; =20 /* Helper definitions. */ @@ -1658,6 +1662,11 @@ x86_decode_base( =20 switch ( ctxt->opcode ) { + case 0x90: /* nop / pause */ + if ( repe_prefix() ) + ctxt->opcode |=3D X86EMUL_OPC_F3(0, 0); + break; + case 0x9a: /* call (far, absolute) */ case 0xea: /* jmp (far, absolute) */ generate_exception_if(mode_64bit(), EXC_UD, -1); @@ -2852,8 +2861,9 @@ x86_emulate( break; =20 case 0x90: /* nop / xchg %%r8,%%rax */ + case X86EMUL_OPC_F3(0, 0x90): /* pause / xchg %%r8,%%rax */ if ( !(rex_prefix & 1) ) - break; /* nop */ + break; /* nop / pause */ /* fall through */ =20 case 0x91 ... 0x97: /* xchg reg,%%rax */ @@ -5200,3 +5210,89 @@ x86_emulate( #undef vex #undef override_seg #undef ea + +#ifdef __XEN__ + +#include + +struct x86_emulate_state * +x86_decode_insn( + struct x86_emulate_ctxt *ctxt, + int (*insn_fetch)( + enum x86_segment seg, unsigned long offset, + void *p_data, unsigned int bytes, + struct x86_emulate_ctxt *ctxt)) +{ + static DEFINE_PER_CPU(struct x86_emulate_state, state); + struct x86_emulate_state *state =3D &this_cpu(state); + const struct x86_emulate_ops ops =3D { + .insn_fetch =3D insn_fetch, + .read =3D x86emul_unhandleable_rw, + .write =3D REG_POISON, + .cmpxchg =3D REG_POISON, + }; + int rc =3D x86_decode(state, ctxt, &ops); + + if ( unlikely(rc !=3D X86EMUL_OKAY) ) + return ERR_PTR(-rc); + +#ifndef NDEBUG + /* + * While we avoid memory allocation (by use of per-CPU data) above, + * nevertheless make sure callers properly release the state = structure + * for forward compatibility. + */ + if ( state->caller ) + { + printk(XENLOG_ERR "Unreleased emulation state acquired by %ps\n", + state->caller); + dump_execution_state(); + } + state->caller =3D __builtin_return_address(0); +#endif + + return state; +} + +static inline void check_state(const struct x86_emulate_state *state) +{ +#ifndef NDEBUG + ASSERT(state->caller); +#endif +} + +#ifndef NDEBUG +void x86_emulate_free_state(struct x86_emulate_state *state) +{ + check_state(state); + state->caller =3D NULL; +} +#endif + +int +x86_insn_modrm(const struct x86_emulate_state *state, + unsigned int *rm, unsigned int *reg) +{ + check_state(state); + + if ( !(state->desc & ModRM) ) + return -EINVAL; + + if ( rm ) + *rm =3D state->modrm_rm; + if ( reg ) + *reg =3D state->modrm_reg; + + return state->modrm_mod; +} + +unsigned int +x86_insn_length(const struct x86_emulate_state *state, + const struct x86_emulate_ctxt *ctxt) +{ + check_state(state); + + return state->eip - ctxt->regs->eip; +} + +#endif --- a/xen/arch/x86/x86_emulate/x86_emulate.h +++ b/xen/arch/x86/x86_emulate/x86_emulate.h @@ -523,4 +523,29 @@ x86emul_unhandleable_rw( unsigned int bytes, struct x86_emulate_ctxt *ctxt); =20 +#ifdef __XEN__ + +struct x86_emulate_state * +x86_decode_insn( + struct x86_emulate_ctxt *ctxt, + int (*insn_fetch)( + enum x86_segment seg, unsigned long offset, + void *p_data, unsigned int bytes, + struct x86_emulate_ctxt *ctxt)); + +int +x86_insn_modrm(const struct x86_emulate_state *state, + unsigned int *rm, unsigned int *reg); +unsigned int +x86_insn_length(const struct x86_emulate_state *state, + const struct x86_emulate_ctxt *ctxt); + +#ifdef NDEBUG +static inline void x86_emulate_free_state(struct x86_emulate_state = *state) {} +#else +void x86_emulate_free_state(struct x86_emulate_state *state); +#endif + +#endif + #endif /* __X86_EMULATE_H__ */ --- a/xen/include/asm-x86/hvm/emulate.h +++ b/xen/include/asm-x86/hvm/emulate.h @@ -53,6 +53,10 @@ void hvm_mem_access_emulate_one(enum emu void hvm_emulate_prepare( struct hvm_emulate_ctxt *hvmemul_ctxt, struct cpu_user_regs *regs); +void hvm_emulate_init( + struct hvm_emulate_ctxt *hvmemul_ctxt, + const unsigned char *insn_buf, + unsigned int insn_bytes); void hvm_emulate_writeback( struct hvm_emulate_ctxt *hvmemul_ctxt); struct segment_register *hvmemul_get_seg_reg( @@ -60,6 +64,11 @@ struct segment_register *hvmemul_get_seg struct hvm_emulate_ctxt *hvmemul_ctxt); int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla); =20 +int hvmemul_insn_fetch(enum x86_segment seg, + unsigned long offset, + void *p_data, + unsigned int bytes, + struct x86_emulate_ctxt *ctxt); int hvmemul_do_pio_buffer(uint16_t port, unsigned int size, uint8_t dir, --=__PartD5E35E5F.2__= Content-Type: text/plain; name="SVM-use-generic-decode.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="SVM-use-generic-decode.patch" SVM: use generic instruction decoding=0A=0A... instead of custom handling. = To facilitate this break out init code=0Afrom _hvm_emulate_one() into the = new hvm_emulate_init(), and make=0Ahvmemul_insn_fetch( globally available.= =0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x8= 6/hvm/emulate.c=0A+++ b/xen/arch/x86/hvm/emulate.c=0A@@ -835,7 +835,7 @@ = static int hvmemul_read(=0A container_of(ctxt, struct hvm_emulate_c= txt, ctxt));=0A }=0A =0A-static int hvmemul_insn_fetch(=0A+int hvmemul_insn= _fetch(=0A enum x86_segment seg,=0A unsigned long offset,=0A = void *p_data,=0A@@ -1765,15 +1765,14 @@ static const struct x86_emulate_ops= hvm_=0A .vmfunc =3D hvmemul_vmfunc,=0A };=0A =0A-static int = _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,=0A- const = struct x86_emulate_ops *ops)=0A+void hvm_emulate_init(=0A+ struct = hvm_emulate_ctxt *hvmemul_ctxt,=0A+ const unsigned char *insn_buf,=0A+ = unsigned int insn_bytes)=0A {=0A- struct cpu_user_regs *regs =3D = hvmemul_ctxt->ctxt.regs;=0A struct vcpu *curr =3D current;=0A- = uint32_t new_intr_shadow, pfec =3D PFEC_page_present;=0A- struct = hvm_vcpu_io *vio =3D &curr->arch.hvm_vcpu.hvm_io;=0A+ unsigned int pfec = =3D PFEC_page_present;=0A unsigned long addr;=0A- int rc;=0A =0A = if ( hvm_long_mode_enabled(curr) &&=0A hvmemul_ctxt->seg_reg[x86_= seg_cs].attr.fields.l )=0A@@ -1791,14 +1790,14 @@ static int _hvm_emulate_o= ne(struct hvm_e=0A if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.d= pl =3D=3D 3 )=0A pfec |=3D PFEC_user_mode;=0A =0A- hvmemul_ctxt-= >insn_buf_eip =3D regs->eip;=0A- if ( !vio->mmio_insn_bytes )=0A+ = hvmemul_ctxt->insn_buf_eip =3D hvmemul_ctxt->ctxt.regs->eip;=0A+ if ( = !insn_bytes )=0A {=0A hvmemul_ctxt->insn_buf_bytes =3D=0A = hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?:=0A = (hvm_virtual_to_linear_addr(x86_seg_cs,=0A = &hvmemul_ctxt->seg_reg[x86_seg_cs],=0A- = regs->eip,=0A+ = hvmemul_ctxt->insn_buf_eip,=0A = sizeof(hvmemul_ctxt->insn_buf),=0A = hvm_access_insn_fetch,=0A = hvmemul_ctxt->ctxt.addr_size,=0A@@ -1810,11 +1809,24 @@ static int = _hvm_emulate_one(struct hvm_e=0A }=0A else=0A {=0A- = hvmemul_ctxt->insn_buf_bytes =3D vio->mmio_insn_bytes;=0A- = memcpy(hvmemul_ctxt->insn_buf, vio->mmio_insn, vio->mmio_insn_bytes);=0A+ = hvmemul_ctxt->insn_buf_bytes =3D insn_bytes;=0A+ memcpy(hvmemu= l_ctxt->insn_buf, insn_buf, insn_bytes);=0A }=0A =0A hvmemul_ctxt->= exn_pending =3D 0;=0A+}=0A+=0A+static int _hvm_emulate_one(struct = hvm_emulate_ctxt *hvmemul_ctxt,=0A+ const struct x86_emulate_ops = *ops)=0A+{=0A+ const struct cpu_user_regs *regs =3D hvmemul_ctxt->ctxt.r= egs;=0A+ struct vcpu *curr =3D current;=0A+ uint32_t new_intr_shadow;= =0A+ struct hvm_vcpu_io *vio =3D &curr->arch.hvm_vcpu.hvm_io;=0A+ = int rc;=0A+=0A+ hvm_emulate_init(hvmemul_ctxt, vio->mmio_insn, = vio->mmio_insn_bytes);=0A+=0A vio->mmio_retry =3D 0;=0A =0A if ( = cpu_has_vmx )=0A--- a/xen/arch/x86/hvm/svm/emulate.c=0A+++ b/xen/arch/x86/h= vm/svm/emulate.c=0A@@ -15,7 +15,7 @@=0A * this program; If not, see = .=0A */=0A =0A-#include =0A+#i= nclude =0A #include =0A #include =0A = #include =0A@@ -26,41 +26,6 @@=0A #include =0A #include =0A =0A-static unsigned int is_prefix(= u8 opc)=0A-{=0A- switch ( opc )=0A- {=0A- case 0x66:=0A- case = 0x67:=0A- case 0x2E:=0A- case 0x3E:=0A- case 0x26:=0A- case = 0x64:=0A- case 0x65:=0A- case 0x36:=0A- case 0xF0:=0A- case = 0xF3:=0A- case 0xF2:=0A- case 0x40 ... 0x4f:=0A- return = 1;=0A- }=0A- return 0;=0A-}=0A-=0A-static unsigned long svm_rip2point= er(struct vcpu *v, unsigned long *limit)=0A-{=0A- struct vmcb_struct = *vmcb =3D v->arch.hvm_svm.vmcb;=0A- unsigned long p =3D vmcb->cs.base + = vmcb->rip;=0A-=0A- if ( !(vmcb->cs.attr.fields.l && hvm_long_mode_enable= d(v)) )=0A- {=0A- *limit =3D vmcb->cs.limit;=0A- return = (u32)p; /* mask to 32 bits */=0A- }=0A- *limit =3D ~0UL;=0A- = return p;=0A-}=0A-=0A static unsigned long svm_nextrip_insn_length(struct = vcpu *v)=0A {=0A struct vmcb_struct *vmcb =3D v->arch.hvm_svm.vmcb;=0A@= @ -89,141 +54,96 @@ static unsigned long svm_nextrip_insn_le=0A return = vmcb->nextrip - vmcb->rip;=0A }=0A =0A-/* First byte: Length. Following = bytes: Opcode bytes. */=0A-#define MAKE_INSTR(nm, ...) static const u8 = OPCODE_##nm[] =3D { __VA_ARGS__ }=0A-MAKE_INSTR(INVD, 2, 0x0f, 0x08);=0A-= MAKE_INSTR(WBINVD, 2, 0x0f, 0x09);=0A-MAKE_INSTR(CPUID, 2, 0x0f, = 0xa2);=0A-MAKE_INSTR(RDMSR, 2, 0x0f, 0x32);=0A-MAKE_INSTR(WRMSR, 2, = 0x0f, 0x30);=0A-MAKE_INSTR(VMCALL, 3, 0x0f, 0x01, 0xd9);=0A-MAKE_INSTR(HLT,= 1, 0xf4);=0A-MAKE_INSTR(INT3, 1, 0xcc);=0A-MAKE_INSTR(RDTSC, 2, = 0x0f, 0x31);=0A-MAKE_INSTR(PAUSE, 1, 0x90);=0A-MAKE_INSTR(XSETBV, 3, = 0x0f, 0x01, 0xd1);=0A-MAKE_INSTR(VMRUN, 3, 0x0f, 0x01, 0xd8);=0A-MAKE_INST= R(VMLOAD, 3, 0x0f, 0x01, 0xda);=0A-MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, = 0xdb);=0A-MAKE_INSTR(STGI, 3, 0x0f, 0x01, 0xdc);=0A-MAKE_INSTR(CLGI, = 3, 0x0f, 0x01, 0xdd);=0A-MAKE_INSTR(INVLPGA,3, 0x0f, 0x01, 0xdf);=0A-=0A-st= atic const u8 *const opc_bytes[INSTR_MAX_COUNT] =3D=0A-{=0A- [INSTR_INVD= ] =3D OPCODE_INVD,=0A- [INSTR_WBINVD] =3D OPCODE_WBINVD,=0A- = [INSTR_CPUID] =3D OPCODE_CPUID,=0A- [INSTR_RDMSR] =3D OPCODE_RDMSR,=0A= - [INSTR_WRMSR] =3D OPCODE_WRMSR,=0A- [INSTR_VMCALL] =3D OPCODE_VMCA= LL,=0A- [INSTR_HLT] =3D OPCODE_HLT,=0A- [INSTR_INT3] =3D = OPCODE_INT3,=0A- [INSTR_RDTSC] =3D OPCODE_RDTSC,=0A- [INSTR_PAUSE] = =3D OPCODE_PAUSE,=0A- [INSTR_XSETBV] =3D OPCODE_XSETBV,=0A- = [INSTR_VMRUN] =3D OPCODE_VMRUN,=0A- [INSTR_VMLOAD] =3D OPCODE_VMLOAD,= =0A- [INSTR_VMSAVE] =3D OPCODE_VMSAVE,=0A- [INSTR_STGI] =3D = OPCODE_STGI,=0A- [INSTR_CLGI] =3D OPCODE_CLGI,=0A- [INSTR_INVLPGA] = =3D OPCODE_INVLPGA,=0A+static const struct {=0A+ unsigned int opcode;=0A= + struct {=0A+ unsigned int rm:3;=0A+ unsigned int = reg:3;=0A+ unsigned int mod:2;=0A+#define MODRM(mod, reg, rm) { rm, = reg, mod }=0A+ } modrm;=0A+} const opc_tab[INSTR_MAX_COUNT] =3D {=0A+ = [INSTR_PAUSE] =3D { X86EMUL_OPC_F3(0, 0x90) },=0A+ [INSTR_INT3] =3D = { X86EMUL_OPC( 0, 0xcc) },=0A+ [INSTR_HLT] =3D { X86EMUL_OPC( 0, = 0xf4) },=0A+ [INSTR_XSETBV] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 2, = 1) },=0A+ [INSTR_VMRUN] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 0) = },=0A+ [INSTR_VMCALL] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 1) = },=0A+ [INSTR_VMLOAD] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 2) = },=0A+ [INSTR_VMSAVE] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 3) = },=0A+ [INSTR_STGI] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 4) = },=0A+ [INSTR_CLGI] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 5) = },=0A+ [INSTR_INVLPGA] =3D { X86EMUL_OPC(0x0f, 0x01), MODRM(3, 3, 7) = },=0A+ [INSTR_INVD] =3D { X86EMUL_OPC(0x0f, 0x08) },=0A+ [INSTR_WBI= NVD] =3D { X86EMUL_OPC(0x0f, 0x09) },=0A+ [INSTR_WRMSR] =3D { = X86EMUL_OPC(0x0f, 0x30) },=0A+ [INSTR_RDTSC] =3D { X86EMUL_OPC(0x0f, = 0x31) },=0A+ [INSTR_RDMSR] =3D { X86EMUL_OPC(0x0f, 0x32) },=0A+ = [INSTR_CPUID] =3D { X86EMUL_OPC(0x0f, 0xa2) },=0A };=0A =0A-static bool_t = fetch(const struct vmcb_struct *vmcb, u8 *buf,=0A- = unsigned long addr, unsigned int len)=0A-{=0A- uint32_t pfec =3D = (vmcb_get_cpl(vmcb) =3D=3D 3) ? PFEC_user_mode : 0;=0A-=0A- switch ( = hvm_fetch_from_guest_virt(buf, addr, len, pfec) )=0A- {=0A- case = HVMCOPY_okay:=0A- break;=0A- case HVMCOPY_bad_gva_to_gfn:=0A- = /* OK just to give up; we'll have injected #PF already */=0A- = return 0;=0A- default:=0A- /* Not OK: fetches from non-RAM pages = are not supportable. */=0A- gdprintk(XENLOG_WARNING, "Bad instructio= n fetch at %#lx (%#lx)\n",=0A- vmcb->rip, addr);=0A- = hvm_inject_hw_exception(TRAP_gp_fault, 0);=0A- return 0;=0A- = }=0A- return 1;=0A-}=0A-=0A int __get_instruction_length_from_list(struc= t vcpu *v,=0A const enum instruction_index *list, unsigned int = list_count)=0A {=0A struct vmcb_struct *vmcb =3D v->arch.hvm_svm.vmcb;= =0A- unsigned int i, j, inst_len =3D 0;=0A- enum instruction_index = instr =3D 0;=0A- u8 buf[MAX_INST_LEN];=0A- const u8 *opcode =3D = NULL;=0A- unsigned long fetch_addr, fetch_limit;=0A- unsigned int = fetch_len, max_len;=0A+ struct hvm_emulate_ctxt ctxt;=0A+ struct = x86_emulate_state *state;=0A+ unsigned int inst_len, j, modrm_rm, = modrm_reg;=0A+ int modrm_mod;=0A =0A+#ifdef NDEBUG=0A if ( = (inst_len =3D svm_nextrip_insn_length(v)) !=3D 0 )=0A return = inst_len;=0A =0A if ( vmcb->exitcode =3D=3D VMEXIT_IOIO )=0A = return vmcb->exitinfo2 - vmcb->rip;=0A+#endif=0A =0A- /* Fetch up to = the next page break; we'll fetch from the next page=0A- * later if we = have to. */=0A- fetch_addr =3D svm_rip2pointer(v, &fetch_limit);=0A- = if ( vmcb->rip > fetch_limit )=0A- return 0;=0A- max_len =3D = min(fetch_limit - vmcb->rip + 1, MAX_INST_LEN + 0UL);=0A- fetch_len =3D = min_t(unsigned int, max_len,=0A- PAGE_SIZE - = (fetch_addr & ~PAGE_MASK));=0A- if ( !fetch(vmcb, buf, fetch_addr, = fetch_len) )=0A+ ASSERT(v =3D=3D current);=0A+ hvm_emulate_prepare(&c= txt, guest_cpu_user_regs());=0A+ hvm_emulate_init(&ctxt, NULL, 0);=0A+ = state =3D x86_decode_insn(&ctxt.ctxt, hvmemul_insn_fetch);=0A+ if ( = IS_ERR_OR_NULL(state) )=0A return 0;=0A =0A- while ( (inst_len = < max_len) && is_prefix(buf[inst_len]) )=0A- {=0A- inst_len++;=0A= - if ( inst_len >=3D fetch_len )=0A- {=0A- if ( = !fetch(vmcb, buf + fetch_len, fetch_addr + fetch_len,=0A- = max_len - fetch_len) )=0A- return 0;=0A- = fetch_len =3D max_len;=0A- }=0A+ inst_len =3D x86_insn_length(sta= te, &ctxt.ctxt);=0A+ modrm_mod =3D x86_insn_modrm(state, &modrm_rm, = &modrm_reg);=0A+ x86_emulate_free_state(state);=0A+#ifndef NDEBUG=0A+ = if ( vmcb->exitcode =3D=3D VMEXIT_IOIO )=0A+ j =3D vmcb->exitinfo2 = - vmcb->rip;=0A+ else=0A+ j =3D svm_nextrip_insn_length(v);=0A+ = if ( j && j !=3D inst_len )=0A+ {=0A+ gprintk(XENLOG_WARNING, = "insn-len[%02x]=3D%u (exp %u)\n",=0A+ ctxt.ctxt.opcode, = inst_len, j);=0A+ return j;=0A }=0A+#endif=0A =0A for ( j = =3D 0; j < list_count; j++ )=0A {=0A- instr =3D list[j];=0A- = opcode =3D opc_bytes[instr];=0A+ enum instruction_index instr = =3D list[j];=0A =0A- for ( i =3D 0; (i < opcode[0]) && ((inst_len + = i) < max_len); i++ )=0A+ ASSERT(instr >=3D 0 && instr < ARRAY_SIZE(o= pc_tab));=0A+ if ( opc_tab[instr].opcode =3D=3D ctxt.ctxt.opcode = )=0A {=0A- if ( (inst_len + i) >=3D fetch_len ) =0A- = {=0A- if ( !fetch(vmcb, buf + fetch_len, fetch_addr = + fetch_len,=0A- max_len - fetch_len) )=0A- = return 0;=0A- fetch_len =3D max_len;=0A- = }=0A+ if ( !opc_tab[instr].modrm.mod )=0A+ = return inst_len;=0A =0A- if ( buf[inst_len+i] !=3D opcode[i+1] = )=0A- goto mismatch;=0A+ if ( modrm_mod =3D=3D = opc_tab[instr].modrm.mod &&=0A+ (modrm_rm & 7) =3D=3D = opc_tab[instr].modrm.rm &&=0A+ (modrm_reg & 7) =3D=3D = opc_tab[instr].modrm.reg )=0A+ return inst_len;=0A = }=0A- goto done;=0A- mismatch: ;=0A }=0A =0A gdprintk(XEN= LOG_WARNING,=0A- "%s: Mismatch between expected and actual = instruction bytes: "=0A+ "%s: Mismatch between expected and = actual instruction: "=0A "eip =3D %lx\n", __func__, = (unsigned long)vmcb->rip);=0A hvm_inject_hw_exception(TRAP_gp_fault, = 0);=0A return 0;=0A-=0A- done:=0A- inst_len +=3D opcode[0];=0A- = ASSERT(inst_len <=3D max_len);=0A- return inst_len;=0A }=0A =0A = /*=0A--- a/xen/arch/x86/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/x86_= emulate/x86_emulate.c=0A@@ -382,7 +382,7 @@ struct operand {=0A } = mem;=0A };=0A #ifdef __x86_64__=0A-#define REG_POISON ((unsigned long *) = 0x8086000000008086UL) /* non-canonical */=0A+#define REG_POISON ((void = *)0x8086000000008086UL) /* non-canonical */=0A #else=0A #define REG_POISON = NULL /* 32-bit builds are for user-space, so NULL is OK. */=0A #endif=0A@@ = -1631,6 +1631,10 @@ struct x86_emulate_state {=0A =0A unsigned long = eip;=0A struct cpu_user_regs *regs;=0A+=0A+#ifndef NDEBUG=0A+ void = *caller;=0A+#endif=0A };=0A =0A /* Helper definitions. */=0A@@ -1658,6 = +1662,11 @@ x86_decode_base(=0A =0A switch ( ctxt->opcode )=0A = {=0A+ case 0x90: /* nop / pause */=0A+ if ( repe_prefix() )=0A+ = ctxt->opcode |=3D X86EMUL_OPC_F3(0, 0);=0A+ break;=0A+=0A = case 0x9a: /* call (far, absolute) */=0A case 0xea: /* jmp (far, = absolute) */=0A generate_exception_if(mode_64bit(), EXC_UD, = -1);=0A@@ -2852,8 +2861,9 @@ x86_emulate(=0A break;=0A =0A = case 0x90: /* nop / xchg %%r8,%%rax */=0A+ case X86EMUL_OPC_F3(0, = 0x90): /* pause / xchg %%r8,%%rax */=0A if ( !(rex_prefix & 1) = )=0A- break; /* nop */=0A+ break; /* nop / pause = */=0A /* fall through */=0A =0A case 0x91 ... 0x97: /* xchg = reg,%%rax */=0A@@ -5200,3 +5210,89 @@ x86_emulate(=0A #undef vex=0A #undef = override_seg=0A #undef ea=0A+=0A+#ifdef __XEN__=0A+=0A+#include = =0A+=0A+struct x86_emulate_state *=0A+x86_decode_insn(=0A+ struct = x86_emulate_ctxt *ctxt,=0A+ int (*insn_fetch)(=0A+ enum = x86_segment seg, unsigned long offset,=0A+ void *p_data, unsigned = int bytes,=0A+ struct x86_emulate_ctxt *ctxt))=0A+{=0A+ static = DEFINE_PER_CPU(struct x86_emulate_state, state);=0A+ struct x86_emulate_= state *state =3D &this_cpu(state);=0A+ const struct x86_emulate_ops ops = =3D {=0A+ .insn_fetch =3D insn_fetch,=0A+ .read =3D = x86emul_unhandleable_rw,=0A+ .write =3D REG_POISON,=0A+ = .cmpxchg =3D REG_POISON,=0A+ };=0A+ int rc =3D x86_decode(state, = ctxt, &ops);=0A+=0A+ if ( unlikely(rc !=3D X86EMUL_OKAY) )=0A+ = return ERR_PTR(-rc);=0A+=0A+#ifndef NDEBUG=0A+ /*=0A+ * While we = avoid memory allocation (by use of per-CPU data) above,=0A+ * = nevertheless make sure callers properly release the state structure=0A+ = * for forward compatibility.=0A+ */=0A+ if ( state->caller )=0A+ = {=0A+ printk(XENLOG_ERR "Unreleased emulation state acquired by = %ps\n",=0A+ state->caller);=0A+ dump_execution_state()= ;=0A+ }=0A+ state->caller =3D __builtin_return_address(0);=0A+#endif= =0A+=0A+ return state;=0A+}=0A+=0A+static inline void check_state(const = struct x86_emulate_state *state)=0A+{=0A+#ifndef NDEBUG=0A+ ASSERT(state= ->caller);=0A+#endif=0A+}=0A+=0A+#ifndef NDEBUG=0A+void x86_emulate_free_st= ate(struct x86_emulate_state *state)=0A+{=0A+ check_state(state);=0A+ = state->caller =3D NULL;=0A+}=0A+#endif=0A+=0A+int=0A+x86_insn_modrm(const = struct x86_emulate_state *state,=0A+ unsigned int *rm, = unsigned int *reg)=0A+{=0A+ check_state(state);=0A+=0A+ if ( = !(state->desc & ModRM) )=0A+ return -EINVAL;=0A+=0A+ if ( rm = )=0A+ *rm =3D state->modrm_rm;=0A+ if ( reg )=0A+ *reg = =3D state->modrm_reg;=0A+=0A+ return state->modrm_mod;=0A+}=0A+=0A+unsig= ned int=0A+x86_insn_length(const struct x86_emulate_state *state,=0A+ = const struct x86_emulate_ctxt *ctxt)=0A+{=0A+ check_state(stat= e);=0A+=0A+ return state->eip - ctxt->regs->eip;=0A+}=0A+=0A+#endif=0A--= - a/xen/arch/x86/x86_emulate/x86_emulate.h=0A+++ b/xen/arch/x86/x86_emulate= /x86_emulate.h=0A@@ -523,4 +523,29 @@ x86emul_unhandleable_rw(=0A = unsigned int bytes,=0A struct x86_emulate_ctxt *ctxt);=0A =0A+#ifdef = __XEN__=0A+=0A+struct x86_emulate_state *=0A+x86_decode_insn(=0A+ = struct x86_emulate_ctxt *ctxt,=0A+ int (*insn_fetch)(=0A+ enum = x86_segment seg, unsigned long offset,=0A+ void *p_data, unsigned = int bytes,=0A+ struct x86_emulate_ctxt *ctxt));=0A+=0A+int=0A+x86_in= sn_modrm(const struct x86_emulate_state *state,=0A+ unsigned = int *rm, unsigned int *reg);=0A+unsigned int=0A+x86_insn_length(const = struct x86_emulate_state *state,=0A+ const struct x86_emulat= e_ctxt *ctxt);=0A+=0A+#ifdef NDEBUG=0A+static inline void x86_emulate_free_= state(struct x86_emulate_state *state) {}=0A+#else=0A+void x86_emulate_free= _state(struct x86_emulate_state *state);=0A+#endif=0A+=0A+#endif=0A+=0A = #endif /* __X86_EMULATE_H__ */=0A--- a/xen/include/asm-x86/hvm/emulate.h=0A= +++ b/xen/include/asm-x86/hvm/emulate.h=0A@@ -53,6 +53,10 @@ void = hvm_mem_access_emulate_one(enum emu=0A void hvm_emulate_prepare(=0A = struct hvm_emulate_ctxt *hvmemul_ctxt,=0A struct cpu_user_regs = *regs);=0A+void hvm_emulate_init(=0A+ struct hvm_emulate_ctxt *hvmemul_c= txt,=0A+ const unsigned char *insn_buf,=0A+ unsigned int insn_bytes);= =0A void hvm_emulate_writeback(=0A struct hvm_emulate_ctxt *hvmemul_ctx= t);=0A struct segment_register *hvmemul_get_seg_reg(=0A@@ -60,6 +64,11 @@ = struct segment_register *hvmemul_get_seg=0A struct hvm_emulate_ctxt = *hvmemul_ctxt);=0A int hvm_emulate_one_mmio(unsigned long mfn, unsigned = long gla);=0A =0A+int hvmemul_insn_fetch(enum x86_segment seg,=0A+ = unsigned long offset,=0A+ void = *p_data,=0A+ unsigned int bytes,=0A+ = struct x86_emulate_ctxt *ctxt);=0A int hvmemul_do_pio_buffer(uint16_t= port,=0A unsigned int size,=0A = uint8_t dir,=0A --=__PartD5E35E5F.2__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__PartD5E35E5F.2__=--