From: "Jan Beulich" Subject: [PATCH] EPT: utilize GLA->GPA translation known for certain faults Date: Thu, 28 Aug 2014 15:40:57 +0100 Message-ID: <53FF5B99020000780002EC6E@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part92A03369.0__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XN0so-0007pO-19 for xen-devel@lists.xenproject.org; Thu, 28 Aug 2014 14:41:02 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Tim Deegan , Keir Fraser 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. --=__Part92A03369.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Rather than doing the translation ourselves in __hvmemul_{read,write}() leverage that we know the association for faults other than such having occurred when translating addresses of page tables. There is one intentional but not necessarily obvious (and possibly subtle) adjustment to behavior: __hvmemul_read() no longer blindly bails on instruction fetches matching the MMIO GVA (the callers of handle_mmio_with_translation() now control the behavior via the struct npfec they pass, and it didn't seem right to bail here rather than just falling through to the unaccelerated path) Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -481,10 +481,11 @@ static int __hvmemul_read( while ( off & (chunk - 1) ) chunk >>=3D 1; =20 - if ( unlikely(vio->mmio_gva =3D=3D (addr & PAGE_MASK)) && vio->mmio_gv= a ) + if ( ((access_type !=3D hvm_access_insn_fetch + ? vio->mmio_access.read_access + : vio->mmio_access.insn_fetch)) && + (vio->mmio_gva =3D=3D (addr & PAGE_MASK)) ) { - if ( access_type =3D=3D hvm_access_insn_fetch ) - return X86EMUL_UNHANDLEABLE; gpa =3D (((paddr_t)vio->mmio_gpfn << PAGE_SHIFT) | off); while ( (off + chunk) <=3D PAGE_SIZE ) { @@ -624,7 +625,8 @@ static int hvmemul_write( while ( off & (chunk - 1) ) chunk >>=3D 1; =20 - if ( unlikely(vio->mmio_gva =3D=3D (addr & PAGE_MASK)) && vio->mmio_gv= a ) + if ( vio->mmio_access.write_access && + (vio->mmio_gva =3D=3D (addr & PAGE_MASK)) ) { gpa =3D (((paddr_t)vio->mmio_gpfn << PAGE_SHIFT) | off); while ( (off + chunk) <=3D PAGE_SIZE ) --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -2788,7 +2788,7 @@ int hvm_hap_nested_page_fault(paddr_t gp && is_hvm_vcpu(v) && hvm_mmio_internal(gpa) ) { - if ( !handle_mmio() ) + if ( !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, npfec) = ) hvm_inject_hw_exception(TRAP_gp_fault, 0); rc =3D 1; goto out; @@ -2862,7 +2862,7 @@ int hvm_hap_nested_page_fault(paddr_t gp if ( unlikely(is_pvh_vcpu(v)) ) goto out; =20 - if ( !handle_mmio() ) + if ( !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, npfec) = ) hvm_inject_hw_exception(TRAP_gp_fault, 0); rc =3D 1; goto out; --- a/xen/arch/x86/hvm/io.c +++ b/xen/arch/x86/hvm/io.c @@ -95,7 +95,7 @@ int handle_mmio(void) if ( vio->io_state =3D=3D HVMIO_awaiting_completion ) vio->io_state =3D HVMIO_handle_mmio_awaiting_completion; else - vio->mmio_gva =3D 0; + vio->mmio_access =3D (struct npfec){}; =20 switch ( rc ) { @@ -124,9 +124,14 @@ int handle_mmio(void) return 1; } =20 -int handle_mmio_with_translation(unsigned long gva, unsigned long gpfn) +int handle_mmio_with_translation(unsigned long gva, unsigned long gpfn, + struct npfec access) { struct hvm_vcpu_io *vio =3D ¤t->arch.hvm_vcpu.hvm_io; + + vio->mmio_access =3D access.gla_valid && + access.kind =3D=3D npfec_kind_with_gla + ? access : (struct npfec){}; vio->mmio_gva =3D gva & PAGE_MASK; vio->mmio_gpfn =3D gpfn; return handle_mmio(); --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -2824,6 +2824,11 @@ static int sh_page_fault(struct vcpu *v, p2m_type_t p2mt; uint32_t rc; int version; + struct npfec access =3D { + .read_access =3D 1, + .gla_valid =3D 1, + .kind =3D npfec_kind_with_gla + }; #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION int fast_emul =3D 0; #endif @@ -2834,6 +2839,9 @@ static int sh_page_fault(struct vcpu *v, =20 perfc_incr(shadow_fault); =20 + if ( regs->error_code & PFEC_write_access ) + access.write_access =3D 1; + #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION /* If faulting frame is successfully emulated in last shadow fault * it's highly likely to reach same emulation action for this frame. @@ -2935,7 +2943,7 @@ static int sh_page_fault(struct vcpu *v, SHADOW_PRINTK("fast path mmio %#"PRIpaddr"\n", gpa); reset_early_unshadow(v); trace_shadow_gen(TRC_SHADOW_FAST_MMIO, va); - return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT) + return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT, = access) ? EXCRET_fault_fixed : 0); } else @@ -3424,7 +3432,7 @@ static int sh_page_fault(struct vcpu *v, paging_unlock(d); put_gfn(d, gfn_x(gfn)); trace_shadow_gen(TRC_SHADOW_MMIO, va); - return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT) + return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT, access) ? EXCRET_fault_fixed : 0); =20 not_a_shadow_fault: --- a/xen/include/asm-x86/hvm/io.h +++ b/xen/include/asm-x86/hvm/io.h @@ -119,7 +119,8 @@ static inline void register_buffered_io_ void send_timeoffset_req(unsigned long timeoff); void send_invalidate_req(void); int handle_mmio(void); -int handle_mmio_with_translation(unsigned long gva, unsigned long gpfn); +int handle_mmio_with_translation(unsigned long gva, unsigned long gpfn, + struct npfec); int handle_pio(uint16_t port, unsigned int size, int dir); void hvm_interrupt_post(struct vcpu *v, int vector, int type); void hvm_io_assist(ioreq_t *p); --- a/xen/include/asm-x86/hvm/vcpu.h +++ b/xen/include/asm-x86/hvm/vcpu.h @@ -54,8 +54,9 @@ struct hvm_vcpu_io { * HVM emulation: * Virtual address @mmio_gva maps to MMIO physical frame @mmio_gpfn. * The latter is known to be an MMIO frame (not RAM). - * This translation is only valid if @mmio_gva is non-zero. + * This translation is only valid for accesses as per @mmio_access. */ + struct npfec mmio_access; unsigned long mmio_gva; unsigned long mmio_gpfn; =20 --=__Part92A03369.0__= Content-Type: text/plain; name="EPT-fault-GLA.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="EPT-fault-GLA.patch" EPT: utilize GLA->GPA translation known for certain faults=0A=0ARather = than doing the translation ourselves in __hvmemul_{read,write}()=0Aleverage= that we know the association for faults other than such having=0Aoccurred = when translating addresses of page tables.=0A=0AThere is one intentional = but not necessarily obvious (and possibly=0Asubtle) adjustment to = behavior: __hvmemul_read() no longer blindly=0Abails on instruction = fetches matching the MMIO GVA (the callers of=0Ahandle_mmio_with_translatio= n() now control the behavior via the struct=0Anpfec they pass, and it = didn't seem right to bail here rather than just=0Afalling through to the = unaccelerated path)=0A=0ASigned-off-by: Jan Beulich =0A= =0A--- a/xen/arch/x86/hvm/emulate.c=0A+++ b/xen/arch/x86/hvm/emulate.c=0A@@= -481,10 +481,11 @@ static int __hvmemul_read(=0A while ( off & = (chunk - 1) )=0A chunk >>=3D 1;=0A =0A- if ( unlikely(vio->m= mio_gva =3D=3D (addr & PAGE_MASK)) && vio->mmio_gva )=0A+ if ( = ((access_type !=3D hvm_access_insn_fetch=0A+ ? vio->mmio_access.r= ead_access=0A+ : vio->mmio_access.insn_fetch)) &&=0A+ = (vio->mmio_gva =3D=3D (addr & PAGE_MASK)) )=0A {=0A- if ( = access_type =3D=3D hvm_access_insn_fetch )=0A- return X86EMUL_UN= HANDLEABLE;=0A gpa =3D (((paddr_t)vio->mmio_gpfn << PAGE_SHIFT) | = off);=0A while ( (off + chunk) <=3D PAGE_SIZE )=0A {=0A@@ = -624,7 +625,8 @@ static int hvmemul_write(=0A while ( off & (chunk = - 1) )=0A chunk >>=3D 1;=0A =0A- if ( unlikely(vio->mmio_gva= =3D=3D (addr & PAGE_MASK)) && vio->mmio_gva )=0A+ if ( vio->mmio_access= .write_access &&=0A+ (vio->mmio_gva =3D=3D (addr & PAGE_MASK)) = )=0A {=0A gpa =3D (((paddr_t)vio->mmio_gpfn << PAGE_SHIFT) | = off);=0A while ( (off + chunk) <=3D PAGE_SIZE )=0A--- a/xen/arch/x8= 6/hvm/hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c=0A@@ -2788,7 +2788,7 @@ int = hvm_hap_nested_page_fault(paddr_t gp=0A && is_hvm_vcpu(v)=0A = && hvm_mmio_internal(gpa) )=0A {=0A- if ( !handle_mmio() = )=0A+ if ( !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, = npfec) )=0A hvm_inject_hw_exception(TRAP_gp_fault, 0);=0A = rc =3D 1;=0A goto out;=0A@@ -2862,7 +2862,7 @@ int hvm_hap_neste= d_page_fault(paddr_t gp=0A if ( unlikely(is_pvh_vcpu(v)) )=0A = goto out;=0A =0A- if ( !handle_mmio() )=0A+ if ( = !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, npfec) )=0A = hvm_inject_hw_exception(TRAP_gp_fault, 0);=0A rc =3D 1;=0A = goto out;=0A--- a/xen/arch/x86/hvm/io.c=0A+++ b/xen/arch/x86/hvm/io.c=0A= @@ -95,7 +95,7 @@ int handle_mmio(void)=0A if ( vio->io_state =3D=3D = HVMIO_awaiting_completion )=0A vio->io_state =3D HVMIO_handle_mmio_= awaiting_completion;=0A else=0A- vio->mmio_gva =3D 0;=0A+ = vio->mmio_access =3D (struct npfec){};=0A =0A switch ( rc )=0A = {=0A@@ -124,9 +124,14 @@ int handle_mmio(void)=0A return 1;=0A }=0A = =0A-int handle_mmio_with_translation(unsigned long gva, unsigned long = gpfn)=0A+int handle_mmio_with_translation(unsigned long gva, unsigned long = gpfn,=0A+ struct npfec access)=0A {=0A = struct hvm_vcpu_io *vio =3D ¤t->arch.hvm_vcpu.hvm_io;=0A+=0A+ = vio->mmio_access =3D access.gla_valid &&=0A+ = access.kind =3D=3D npfec_kind_with_gla=0A+ ? access = : (struct npfec){};=0A vio->mmio_gva =3D gva & PAGE_MASK;=0A = vio->mmio_gpfn =3D gpfn;=0A return handle_mmio();=0A--- a/xen/arch/x86/= mm/shadow/multi.c=0A+++ b/xen/arch/x86/mm/shadow/multi.c=0A@@ -2824,6 = +2824,11 @@ static int sh_page_fault(struct vcpu *v,=0A p2m_type_t = p2mt;=0A uint32_t rc;=0A int version;=0A+ struct npfec access = =3D {=0A+ .read_access =3D 1,=0A+ .gla_valid =3D 1,=0A+ = .kind =3D npfec_kind_with_gla=0A+ };=0A #if SHADOW_OPTIMIZATIONS & = SHOPT_FAST_EMULATION=0A int fast_emul =3D 0;=0A #endif=0A@@ -2834,6 = +2839,9 @@ static int sh_page_fault(struct vcpu *v,=0A =0A perfc_incr(s= hadow_fault);=0A =0A+ if ( regs->error_code & PFEC_write_access )=0A+ = access.write_access =3D 1;=0A+=0A #if SHADOW_OPTIMIZATIONS & = SHOPT_FAST_EMULATION=0A /* If faulting frame is successfully emulated = in last shadow fault=0A * it's highly likely to reach same emulation = action for this frame.=0A@@ -2935,7 +2943,7 @@ static int sh_page_fault(str= uct vcpu *v,=0A SHADOW_PRINTK("fast path mmio %#"PRIpaddr"\n", = gpa);=0A reset_early_unshadow(v);=0A trace_shadow_g= en(TRC_SHADOW_FAST_MMIO, va);=0A- return (handle_mmio_with_trans= lation(va, gpa >> PAGE_SHIFT)=0A+ return (handle_mmio_with_trans= lation(va, gpa >> PAGE_SHIFT, access)=0A ? EXCRET_fault= _fixed : 0);=0A }=0A else=0A@@ -3424,7 +3432,7 @@ static = int sh_page_fault(struct vcpu *v,=0A paging_unlock(d);=0A = put_gfn(d, gfn_x(gfn));=0A trace_shadow_gen(TRC_SHADOW_MMIO, va);=0A- = return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT)=0A+ = return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT, access)=0A = ? EXCRET_fault_fixed : 0);=0A =0A not_a_shadow_fault:=0A--- = a/xen/include/asm-x86/hvm/io.h=0A+++ b/xen/include/asm-x86/hvm/io.h=0A@@ = -119,7 +119,8 @@ static inline void register_buffered_io_=0A void = send_timeoffset_req(unsigned long timeoff);=0A void send_invalidate_req(voi= d);=0A int handle_mmio(void);=0A-int handle_mmio_with_translation(unsigned = long gva, unsigned long gpfn);=0A+int handle_mmio_with_translation(unsigned= long gva, unsigned long gpfn,=0A+ struct = npfec);=0A int handle_pio(uint16_t port, unsigned int size, int dir);=0A = void hvm_interrupt_post(struct vcpu *v, int vector, int type);=0A void = hvm_io_assist(ioreq_t *p);=0A--- a/xen/include/asm-x86/hvm/vcpu.h=0A+++ = b/xen/include/asm-x86/hvm/vcpu.h=0A@@ -54,8 +54,9 @@ struct hvm_vcpu_io = {=0A * HVM emulation:=0A * Virtual address @mmio_gva maps to = MMIO physical frame @mmio_gpfn.=0A * The latter is known to be an = MMIO frame (not RAM).=0A- * This translation is only valid if = @mmio_gva is non-zero.=0A+ * This translation is only valid for = accesses as per @mmio_access.=0A */=0A+ struct npfec = mmio_access;=0A unsigned long mmio_gva;=0A unsigned long = mmio_gpfn;=0A =0A --=__Part92A03369.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part92A03369.0__=--