EPT: utilize GLA->GPA translation known for certain faults
PATCH 1/1 EPT: utilize GLA->GPA translation known for certain faults
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)
xen/arch/x86/hvm/emulate.c +6 -4
@@ -481,10 +481,11 @@ static int __hvmemul_read(while ( off & (chunk -1 ) ) chunk >>=1 ; -if (unlikely (vio->mmio_gva == (addr &PAGE_MASK )) && vio->mmio_gva ) +if ( ((access_type != hvm_access_insn_fetch + ? vio->mmio_access.read_access + : vio->mmio_access.insn_fetch)) && + (vio->mmio_gva == (addr &PAGE_MASK )) ) { -if ( access_type == hvm_access_insn_fetch ) -return X86EMUL_UNHANDLEABLE ; gpa = (((paddr_t )vio->mmio_gpfn <<PAGE_SHIFT ) | off);while ( (off + chunk) <=PAGE_SIZE ) {
@@ -624,7 +625,8 @@ static int hvmemul_write(while ( off & (chunk -1 ) ) chunk >>=1 ; -if (unlikely (vio->mmio_gva == (addr &PAGE_MASK )) && vio->mmio_gva ) +if ( vio->mmio_access.write_access && + (vio->mmio_gva == (addr &PAGE_MASK )) ) { gpa = (((paddr_t )vio->mmio_gpfn <<PAGE_SHIFT ) | off);while ( (off + chunk) <=PAGE_SIZE )
xen/arch/x86/hvm/hvm.c +2 -2
@@ -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 =1 ; goto out;
@@ -2862,7 +2862,7 @@ int hvm_hap_nested_page_fault(paddr_t gpif (unlikely (is_pvh_vcpu (v)) ) goto out; -if ( !handle_mmio () ) +if ( !handle_mmio_with_translation (gla, gpa >>PAGE_SHIFT , npfec) )hvm_inject_hw_exception (TRAP_gp_fault,0 ); rc =1 ; goto out;
xen/arch/x86/hvm/io.c +7 -2
@@ -95,7 +95,7 @@ int handle_mmio(void)if ( vio->io_state == HVMIO_awaiting_completion ) vio->io_state = HVMIO_handle_mmio_awaiting_completion;else - vio->mmio_gva =0 ; + vio->mmio_access = (struct npfec ){};switch ( rc ) {
@@ -124,9 +124,14 @@ int handle_mmio(void)return 1 ; } -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 = ¤t->arch.hvm_vcpu.hvm_io; + + vio->mmio_access = access.gla_valid && + access.kind == npfec_kind_with_gla + ? access : (struct npfec ){}; vio->mmio_gva = gva &PAGE_MASK ; vio->mmio_gpfn = gpfn;return handle_mmio ();
xen/arch/x86/mm/shadow/multi.c +10 -2
@@ -2824,6 +2824,11 @@ static int sh_page_fault(struct vcpu *v,p2m_type_t p2mt;uint32_t rc;int version; +struct npfec access = { + .read_access =1 , + .gla_valid =1 , + .kind = npfec_kind_with_gla + };#if SHADOW_OPTIMIZATIONS &SHOPT_FAST_EMULATION int fast_emul =0 ;#endif
@@ -2834,6 +2839,9 @@ static int sh_page_fault(struct vcpu *v,perfc_incr (shadow_fault); +if ( regs->error_code & PFEC_write_access ) + access.write_access =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 ); not_a_shadow_fault:
xen/include/asm-x86/hvm/io.h +2 -1
@@ -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);
xen/include/asm-x86/hvm/vcpu.h +2 -1
@@ -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;
EPT: utilize GLA->GPA translation known for certain faults
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)
xen/arch/x86/hvm/emulate.c +6 -4
@@ -481,10 +481,11 @@ static int __hvmemul_read(while ( off & (chunk -1 ) ) chunk >>=1 ; -if (unlikely (vio->mmio_gva == (addr &PAGE_MASK )) && vio->mmio_gva ) +if ( ((access_type != hvm_access_insn_fetch + ? vio->mmio_access.read_access + : vio->mmio_access.insn_fetch)) && + (vio->mmio_gva == (addr &PAGE_MASK )) ) { -if ( access_type == hvm_access_insn_fetch ) -return X86EMUL_UNHANDLEABLE ; gpa = (((paddr_t )vio->mmio_gpfn <<PAGE_SHIFT ) | off);while ( (off + chunk) <=PAGE_SIZE ) {
@@ -624,7 +625,8 @@ static int hvmemul_write(while ( off & (chunk -1 ) ) chunk >>=1 ; -if (unlikely (vio->mmio_gva == (addr &PAGE_MASK )) && vio->mmio_gva ) +if ( vio->mmio_access.write_access && + (vio->mmio_gva == (addr &PAGE_MASK )) ) { gpa = (((paddr_t )vio->mmio_gpfn <<PAGE_SHIFT ) | off);while ( (off + chunk) <=PAGE_SIZE )
xen/arch/x86/hvm/hvm.c +2 -2
@@ -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 =1 ; goto out;
@@ -2862,7 +2862,7 @@ int hvm_hap_nested_page_fault(paddr_t gpif (unlikely (is_pvh_vcpu (v)) ) goto out; -if ( !handle_mmio () ) +if ( !handle_mmio_with_translation (gla, gpa >>PAGE_SHIFT , npfec) )hvm_inject_hw_exception (TRAP_gp_fault,0 ); rc =1 ; goto out;
xen/arch/x86/hvm/io.c +7 -2
@@ -95,7 +95,7 @@ int handle_mmio(void)if ( vio->io_state == HVMIO_awaiting_completion ) vio->io_state = HVMIO_handle_mmio_awaiting_completion;else - vio->mmio_gva =0 ; + vio->mmio_access = (struct npfec ){};switch ( rc ) {
@@ -124,9 +124,14 @@ int handle_mmio(void)return 1 ; } -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 = ¤t->arch.hvm_vcpu.hvm_io; + + vio->mmio_access = access.gla_valid && + access.kind == npfec_kind_with_gla + ? access : (struct npfec ){}; vio->mmio_gva = gva &PAGE_MASK ; vio->mmio_gpfn = gpfn;return handle_mmio ();
xen/arch/x86/mm/shadow/multi.c +10 -2
@@ -2824,6 +2824,11 @@ static int sh_page_fault(struct vcpu *v,p2m_type_t p2mt;uint32_t rc;int version; +struct npfec access = { + .read_access =1 , + .gla_valid =1 , + .kind = npfec_kind_with_gla + };#if SHADOW_OPTIMIZATIONS &SHOPT_FAST_EMULATION int fast_emul =0 ;#endif
@@ -2834,6 +2839,9 @@ static int sh_page_fault(struct vcpu *v,perfc_incr (shadow_fault); +if ( regs->error_code & PFEC_write_access ) + access.write_access =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 ); not_a_shadow_fault:
xen/include/asm-x86/hvm/io.h +2 -1
@@ -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);
xen/include/asm-x86/hvm/vcpu.h +2 -1
@@ -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;
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 10 lines · Jan Beulich
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 ofhandle_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)
Understood. I think the test you have is not quite right:
xen/arch/x86/hvm/emulate.c static int __hvmemul_read( · line 487
Quoted 13 lines
Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/hvm/emulate.c +4
-1@@ -481,10 +481,11 @@ static int __hvmemul_read(while ( off & (chunk -1 ) ) chunk >>=1 ; -if (unlikely (vio->mmio_gva == (addr &PAGE_MASK )) && vio->mmio_gva ) +if ( ((access_type != hvm_access_insn_fetch + ? vio->mmio_access.read_access + : vio->mmio_access.insn_fetch)) && + (vio->mmio_gva == (addr &PAGE_MASK )) )
This seems like it will follow the fast path for some instruction fetches, which might be OK, but doesn't quite match your description.
If that's the behaviour you want then I think we can simplify the test a lot because we don't care whether this is an ifetch or not; only whether the mapping is valid (assuming we model a coherent icache/dcache and itlb/dtlb here).
But IIRC we don't really support fetching instructions from MMIO addresses anyway (raising questions about, e.g. operation sizes, idempotence and readahead) so we probably want:
if ( access_type != hvm_access_insn_fetch &&
vio->mmio_access.read_access &&
vio->mmio_gva == (addr & PAGE_MASK) )For the rest, Reviewed-by: Tim Deegan <[email protected]>
Cheers,
Tim.
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 28.08.14 at 21:20, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
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 ofhandle_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)Understood. I think the test you have is not quite right:
Quoted 8 lines
Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/hvm/emulate.c +0
-0@@ -481,10 +481,11 @@ static int __hvmemul_read(while ( off & (chunk -1 ) ) chunk >>=1 ;
Quoted 8 lines
xen/arch/x86/hvm/emulate.c static int __hvmemul_read( · line 487
Quoted 5 lines
xen/arch/x86/hvm/emulate.c +4
-1-if (unlikely (vio->mmio_gva == (addr &PAGE_MASK )) && vio->mmio_gva ) +if ( ((access_type != hvm_access_insn_fetch + ? vio->mmio_access.read_access + : vio->mmio_access.insn_fetch)) && + (vio->mmio_gva == (addr &PAGE_MASK )) )This seems like it will follow the fast path for some instruction fetches, which might be OK, but doesn't quite match your description.
To me it does.
Quoted 4 lines
If that's the behaviour you want then I think we can simplify the test a lot because we don't care whether this is an ifetch or not; only whether the mapping is valid (assuming we model a coherent icache/dcache and itlb/dtlb here).
Perhaps, but I wanted to be as restrictive as possible (and the hit rate of the accelerated case of over 99.9% tells me that this extra [possibly paranoid] care doesn't really hurt). Otherwise I can't see why we couldn't drop the read_access check too, and similarly the write_access one in the write path.
Quoted 7 lines
But IIRC we don't really support fetching instructions from MMIO addresses anyway (raising questions about, e.g. operation sizes, idempotence and readahead) so we probably want:
if ( access_type != hvm_access_insn_fetch && vio->mmio_access.read_access && vio->mmio_gva == (addr & PAGE_MASK) )
Getting us back to the previous behavior. While fetching instructions from MMIO is suspicious in some cases, I think whoever does so has to be prepared for side effects to potentially occur more than once. My perspective is more that on side effect free memory (e.g. VRAM), fetching instructions is quite fine.
Jan
Quoted 8 lines · Jan Beulich
Quoted 4 lines
If that's the behaviour you want then I think we can simplify the test a lot because we don't care whether this is an ifetch or not; only whether the mapping is valid (assuming we model a coherent icache/dcache and itlb/dtlb here).
Perhaps, but I wanted to be as restrictive as possible (and the hit rate of the accelerated case of over 99.9% tells me that this extra [possibly paranoid] care doesn't really hurt).
OK, sure. I withdraw the objection and the patch can have my
reviewed-by: as it stands.
Quoted 3 lines
Otherwise I can't see
why we couldn't drop theread_accesscheck too, and similarly thewrite_accessone in the write path.
We probably can drop those, yes.
Cheers,
Tim.