various EPT and related adjustments
v3 cover various EPT and related adjustments
1: EPT: force re-evaluation of memory type as necessary
2: EPT: split super pages upon mismatching memory types
3: EPT: IOMMU snoop capability should not affect memory type selection 4: fix pinned cache attribute handling
5: x86/MSI: drop workaround for insecure Dom0 kernels
---
v3: New patch 5. Code movement in 1st patch (helping patches yet
to be posted).
PATCH v3 1/5 EPT: force re‑evaluation of memory type as necessary
The main goal here is to drop the bogus dependency of epte_get_entry_emt() on d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT].
Any change to state influencing epte_get_entry_emt()'s decision needs to result in re-calculation. Do this by using the EPT_MISCONFIG VM exit, storing an invalid memory type into EPT's emt field (leaving the IOMMU, which doesn't care about memory types, unaffected).
This is being done in a hierarchical manner to keep execution time
down: Initially only the top level directory gets invalidated this way. Upon access, the involved intermediate page table levels get cleared back to zero, and the leaf entry gets its field properly set. For 4k leaves all other entries in the same directory also get processed to amortize the cost of the extra VM exit (which halved the number of these VM exits in my testing).
This restoring can result in spurious EPT_MISCONFIG VM exits (since two vCPU-s may access addresses involving identical page table structures). Rather than simply returning in such cases (and risking that such a VM exit results from a real mis-configuration, which would then result in an endless loop rather than killing the VM), a per-vCPU flag is being introduced indicating when such a spurious VM exit might validly happen - if another one occurs right after VM re- entry, the flag would generally end up being clear, causing the VM to be killed as before on such VM exits.
Note that putting a reserved memory type value in the EPT structures isn't formally sanctioned by the specification. Intel isn't willing to adjust the specification to make this or a similar use of the EPT_MISCONFIG VM exit formally possible, but they have indicated that us using this is low risk wrt forward compatibility.
---
v3: Code movement.
xen/arch/x86/domctl.c +6 -0
@@ -84,6 +84,8 @@ long arch_do_domctl( ret =ioports_permit_access (d, fp, fp + np -1 ); else ret =ioports_deny_access (d, fp, fp + np -1 ); +if ( !ret ) +memory_type_changed (d); }break ;
@@ -705,6 +707,8 @@ long arch_do_domctl( ret, add ?"removing" :"denying" , d->domain_id, mfn, mfn + nr_mfns - 1); } +/* Do this unconditionally to cover errors on above failure paths. */ +memory_type_changed (d); }break ;
@@ -791,6 +795,8 @@ long arch_do_domctl("ioport_map: error %ld denying dom%d access to [%x,%x]\n" , ret, d->domain_id, fmp, fmp + np - 1); } +if ( !ret ) +memory_type_changed (d); }break ;
xen/arch/x86/hvm/hvm.c +3 -0
@@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct vcpu *v, u6if ( !hvm_funcs.set_guest_pat (v, guest_pat) ) v->arch.hvm_vcpu.pat_cr = guest_pat; + +memory_type_changed (v->domain); +return 1 ; }
xen/arch/x86/hvm/mtrr.c +16 -4
@@ -431,8 +431,12 @@ bool_t mtrr_def_type_msr_set(struct domareturn 0 ; } - m->enabled = enabled; - m->def_type = def_type; +if ( m->enabled != enabled || m->def_type != def_type ) + { + m->enabled = enabled; + m->def_type = def_type; +memory_type_changed (d); + }return 1 ; }
@@ -452,6 +456,7 @@ bool_t mtrr_fix_range_msr_set(struct domreturn 0 ; fixed_range_base[row] = msr_content; +memory_type_changed (d); }return 1 ;
@@ -496,6 +501,8 @@ bool_t mtrr_var_range_msr_set( m->overlapped =is_var_mtrr_overlapped (m); +memory_type_changed (d); +return 1 ; }
@@ -690,6 +697,12 @@ static int hvm_load_mtrr_msr(struct domaHVM_REGISTER_SAVE_RESTORE (MTRR , hvm_save_mtrr_msr, hvm_load_mtrr_msr,1 ,HVMSR_PER_VCPU ); +void memory_type_changed (struct domain *d) +{ +if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0 ] ) +p2m_memory_type_changed (d); +} +uint8_t epte_get_entry_emt (struct domain *d,unsigned long gfn,mfn_t mfn,uint8_t *ipat,bool_t direct_mmio) {
@@ -733,8 +746,7 @@ uint8_t epte_get_entry_emt(struct domainreturn MTRR_TYPE_WRBACK ; } - gmtrr_mtype =is_hvm_domain (d) && v && - d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT ] ? + gmtrr_mtype =is_hvm_domain (d) && v ?get_mtrr_type (&v->arch.hvm_vcpu.mtrr, (gfn <<PAGE_SHIFT )) :MTRR_TYPE_WRBACK ;
xen/arch/x86/hvm/vmx/vmx.c +10 -0
@@ -3016,6 +3016,16 @@ void vmx_vmexit_handler(struct cpu_user_break ; } +case EXIT_REASON_EPT_MISCONFIG : + { +paddr_t gpa; + +__vmread (GUEST_PHYSICAL_ADDRESS , &gpa); +if ( !ept_handle_misconfig (gpa) ) + goto exit_and_crash; +break ; + } +case EXIT_REASON_MONITOR_TRAP_FLAG : v->arch.hvm_vmx.exec_control &= ~CPU_BASED_MONITOR_TRAP_FLAG ;vmx_update_cpu_exec_control (v);
xen/arch/x86/mm/p2m.c +12 -0
@@ -200,6 +200,18 @@ void p2m_change_entry_type_global(structp2m_unlock (p2m); } +void p2m_memory_type_changed (struct domain *d) +{ +struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if ( p2m->memory_type_changed ) + { +p2m_lock (p2m); + p2m->memory_type_changed (p2m); +p2m_unlock (p2m); + } +} +mfn_t __get_gfn_type_access (struct p2m_domain *p2m,unsigned long gfn,p2m_type_t *t,p2m_access_t *a,p2m_query_t q,unsigned int *page_order,bool_t locked)
xen/arch/x86/mm/p2m-ept.c +137 -1
179 lines not on this page · raw message
xen/common/domctl.c +4 -0
@@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe ret =iomem_permit_access (d, mfn, mfn + nr_mfns -1 ); else ret =iomem_deny_access (d, mfn, mfn + nr_mfns -1 ); +#ifdef CONFIG_X86 +if ( !ret ) +memory_type_changed (d); +#endif }break ;
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
@@ -124,6 +124,9 @@ struct arch_vmx_struct {unsigned long host_cr0; +/* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */ +bool_t ept_spurious_misconfig; +/* Is the guest in real mode? */ uint8_t vmx_realmode;/* Are we emulating rather than VMENTERing? */
xen/include/asm-x86/hvm/vmx/vmx.h +1 -0
@@ -520,6 +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m)void ept_p2m_uninit (struct p2m_domain *p2m);void ept_walk_table (struct domain *d,unsigned long gfn); +bool_t ept_handle_misconfig (uint64_t gpa);void setup_ept_dump (void );void update_guest_eip (void );
xen/include/asm-x86/mtrr.h +1 -0
@@ -88,6 +88,7 @@ extern bool_t mtrr_fix_range_msr_set(str uint32_t row, uint64_t msr_content);extern bool_t mtrr_def_type_msr_set (struct domain *,struct mtrr_state *,uint64_t msr_content); +extern void memory_type_changed (struct domain *);extern bool_t pat_msr_set (uint64_t *pat,uint64_t msr);bool_t is_var_mtrr_overlapped (struct mtrr_state *m);
xen/include/asm-x86/p2m.h +4 -0
@@ -233,6 +233,7 @@ struct p2m_domain {void (*change_entry_type_global)(struct p2m_domain *p2m,p2m_type_t ot,p2m_type_t nt); +void (*memory_type_changed)(struct p2m_domain *p2m); void (*write_p2m_entry)(struct p2m_domain *p2m, unsigned long gfn, l1_pgentry_t *p,
@@ -506,6 +507,9 @@ void p2m_change_type_range(struct domainp2m_type_t p2m_change_type (struct domain *d,unsigned long gfn,p2m_type_t ot,p2m_type_t nt); +/* Report a change affecting memory types. */ +void p2m_memory_type_changed (struct domain *d); +/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn);int clear_mmio_p2m_entry (struct domain *d,unsigned long gfn);
x86/EPT: force re-evaluation of memory type as necessary
The main goal here is to drop the bogus dependency of epte_get_entry_emt() on d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT].
Any change to state influencing epte_get_entry_emt()'s decision needs to result in re-calculation. Do this by using the EPT_MISCONFIG VM exit, storing an invalid memory type into EPT's emt field (leaving the IOMMU, which doesn't care about memory types, unaffected).
This is being done in a hierarchical manner to keep execution time
down: Initially only the top level directory gets invalidated this way. Upon access, the involved intermediate page table levels get cleared back to zero, and the leaf entry gets its field properly set. For 4k leaves all other entries in the same directory also get processed to amortize the cost of the extra VM exit (which halved the number of these VM exits in my testing).
This restoring can result in spurious EPT_MISCONFIG VM exits (since two vCPU-s may access addresses involving identical page table structures). Rather than simply returning in such cases (and risking that such a VM exit results from a real mis-configuration, which would then result in an endless loop rather than killing the VM), a per-vCPU flag is being introduced indicating when such a spurious VM exit might validly happen - if another one occurs right after VM re- entry, the flag would generally end up being clear, causing the VM to be killed as before on such VM exits.
Note that putting a reserved memory type value in the EPT structures isn't formally sanctioned by the specification. Intel isn't willing to adjust the specification to make this or a similar use of the EPT_MISCONFIG VM exit formally possible, but they have indicated that us using this is low risk wrt forward compatibility.
---
v3: Code movement.
xen/arch/x86/domctl.c +6 -0
@@ -84,6 +84,8 @@ long arch_do_domctl( ret =ioports_permit_access (d, fp, fp + np -1 ); else ret =ioports_deny_access (d, fp, fp + np -1 ); +if ( !ret ) +memory_type_changed (d); }break ;
@@ -705,6 +707,8 @@ long arch_do_domctl( ret, add ?"removing" :"denying" , d->domain_id, mfn, mfn + nr_mfns - 1); } +/* Do this unconditionally to cover errors on above failure paths. */ +memory_type_changed (d); }break ;
@@ -791,6 +795,8 @@ long arch_do_domctl("ioport_map: error %ld denying dom%d access to [%x,%x]\n" , ret, d->domain_id, fmp, fmp + np - 1); } +if ( !ret ) +memory_type_changed (d); }break ;
xen/arch/x86/hvm/hvm.c +3 -0
@@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct vcpu *v, u6if ( !hvm_funcs.set_guest_pat (v, guest_pat) ) v->arch.hvm_vcpu.pat_cr = guest_pat; + +memory_type_changed (v->domain); +return 1 ; }
xen/arch/x86/hvm/mtrr.c +16 -4
@@ -431,8 +431,12 @@ bool_t mtrr_def_type_msr_set(struct domareturn 0 ; } - m->enabled = enabled; - m->def_type = def_type; +if ( m->enabled != enabled || m->def_type != def_type ) + { + m->enabled = enabled; + m->def_type = def_type; +memory_type_changed (d); + }return 1 ; }
@@ -452,6 +456,7 @@ bool_t mtrr_fix_range_msr_set(struct domreturn 0 ; fixed_range_base[row] = msr_content; +memory_type_changed (d); }return 1 ;
@@ -496,6 +501,8 @@ bool_t mtrr_var_range_msr_set( m->overlapped =is_var_mtrr_overlapped (m); +memory_type_changed (d); +return 1 ; }
@@ -690,6 +697,12 @@ static int hvm_load_mtrr_msr(struct domaHVM_REGISTER_SAVE_RESTORE (MTRR , hvm_save_mtrr_msr, hvm_load_mtrr_msr,1 ,HVMSR_PER_VCPU ); +void memory_type_changed (struct domain *d) +{ +if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0 ] ) +p2m_memory_type_changed (d); +} +uint8_t epte_get_entry_emt (struct domain *d,unsigned long gfn,mfn_t mfn,uint8_t *ipat,bool_t direct_mmio) {
@@ -733,8 +746,7 @@ uint8_t epte_get_entry_emt(struct domainreturn MTRR_TYPE_WRBACK ; } - gmtrr_mtype =is_hvm_domain (d) && v && - d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT ] ? + gmtrr_mtype =is_hvm_domain (d) && v ?get_mtrr_type (&v->arch.hvm_vcpu.mtrr, (gfn <<PAGE_SHIFT )) :MTRR_TYPE_WRBACK ;
xen/arch/x86/hvm/vmx/vmx.c +10 -0
@@ -3016,6 +3016,16 @@ void vmx_vmexit_handler(struct cpu_user_break ; } +case EXIT_REASON_EPT_MISCONFIG : + { +paddr_t gpa; + +__vmread (GUEST_PHYSICAL_ADDRESS , &gpa); +if ( !ept_handle_misconfig (gpa) ) + goto exit_and_crash; +break ; + } +case EXIT_REASON_MONITOR_TRAP_FLAG : v->arch.hvm_vmx.exec_control &= ~CPU_BASED_MONITOR_TRAP_FLAG ;vmx_update_cpu_exec_control (v);
xen/arch/x86/mm/p2m.c +12 -0
@@ -200,6 +200,18 @@ void p2m_change_entry_type_global(structp2m_unlock (p2m); } +void p2m_memory_type_changed (struct domain *d) +{ +struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if ( p2m->memory_type_changed ) + { +p2m_lock (p2m); + p2m->memory_type_changed (p2m); +p2m_unlock (p2m); + } +} +mfn_t __get_gfn_type_access (struct p2m_domain *p2m,unsigned long gfn,p2m_type_t *t,p2m_access_t *a,p2m_query_t q,unsigned int *page_order,bool_t locked)
xen/arch/x86/mm/p2m-ept.c +137 -1
179 lines not on this page · raw message
xen/common/domctl.c +4 -0
@@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe ret =iomem_permit_access (d, mfn, mfn + nr_mfns -1 ); else ret =iomem_deny_access (d, mfn, mfn + nr_mfns -1 ); +#ifdef CONFIG_X86 +if ( !ret ) +memory_type_changed (d); +#endif }break ;
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
@@ -124,6 +124,9 @@ struct arch_vmx_struct {unsigned long host_cr0; +/* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */ +bool_t ept_spurious_misconfig; +/* Is the guest in real mode? */ uint8_t vmx_realmode;/* Are we emulating rather than VMENTERing? */
xen/include/asm-x86/hvm/vmx/vmx.h +1 -0
@@ -520,6 +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m)void ept_p2m_uninit (struct p2m_domain *p2m);void ept_walk_table (struct domain *d,unsigned long gfn); +bool_t ept_handle_misconfig (uint64_t gpa);void setup_ept_dump (void );void update_guest_eip (void );
xen/include/asm-x86/mtrr.h +1 -0
@@ -88,6 +88,7 @@ extern bool_t mtrr_fix_range_msr_set(str uint32_t row, uint64_t msr_content);extern bool_t mtrr_def_type_msr_set (struct domain *,struct mtrr_state *,uint64_t msr_content); +extern void memory_type_changed (struct domain *);extern bool_t pat_msr_set (uint64_t *pat,uint64_t msr);bool_t is_var_mtrr_overlapped (struct mtrr_state *m);
xen/include/asm-x86/p2m.h +4 -0
@@ -233,6 +233,7 @@ struct p2m_domain {void (*change_entry_type_global)(struct p2m_domain *p2m,p2m_type_t ot,p2m_type_t nt); +void (*memory_type_changed)(struct p2m_domain *p2m); void (*write_p2m_entry)(struct p2m_domain *p2m, unsigned long gfn, l1_pgentry_t *p,
@@ -506,6 +507,9 @@ void p2m_change_type_range(struct domainp2m_type_t p2m_change_type (struct domain *d,unsigned long gfn,p2m_type_t ot,p2m_type_t nt); +/* Report a change affecting memory types. */ +void p2m_memory_type_changed (struct domain *d); +/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn);int clear_mmio_p2m_entry (struct domain *d,unsigned long gfn);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v3 1/5] EPT: force re‑evaluation of memory type as necessary
Quoted 36 lines
From: Jan Beulich
Sent: Monday, April 07, 2014 6:08 PMThe main goal here is to drop the bogus dependency of
epte_get_entry_emt()on d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT].Any change to state influencing epte_get_entry_emt()'s decision needs to result in re-calculation. Do this by using the
EPT_MISCONFIGVM exit, storing an invalid memory type into EPT's emt field (leaving the IOMMU, which doesn't care about memory types, unaffected).This is being done in a hierarchical manner to keep execution time
down: Initially only the top level directory gets invalidated this way. Upon access, the involved intermediate page table levels get cleared back to zero, and the leaf entry gets its field properly set. For 4k leaves all other entries in the same directory also get processed to amortize the cost of the extra VM exit (which halved the number of these VM exits in my testing).This restoring can result in spurious
EPT_MISCONFIGVM exits (since two vCPU-s may access addresses involving identical page table structures). Rather than simply returning in such cases (and risking that such a VM exit results from a real mis-configuration, which would then result in an endless loop rather than killing the VM), a per-vCPU flag is being introduced indicating when such a spurious VM exit might validly happen - if another one occurs right after VM re- entry, the flag would generally end up being clear, causing the VM to be killed as before on such VM exits.Note that putting a reserved memory type value in the EPT structures isn't formally sanctioned by the specification. Intel isn't willing to adjust the specification to make this or a similar use of the
EPT_MISCONFIGVM exit formally possible, but they have indicated that us using this is low risk wrt forward compatibility.
Quoted 4 lines
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Tim Deegan <[email protected]>---
v3: Code movement.
any data on improvement against original code?
one small comment is whether it's clearer to define a new macro to indicate the purpose instead of using MTRR_NUM_TYPES?
Thanks
Kevin
Quoted 410 lines
xen/arch/x86/domctl.c +6
-0@@ -84,6 +84,8 @@ long arch_do_domctl( ret =ioports_permit_access (d, fp, fp + np -1 ); else ret =ioports_deny_access (d, fp, fp + np -1 ); +if ( !ret ) +memory_type_changed (d); }break ;@@ -705,6 +707,8 @@ long arch_do_domctl( ret, add ?"removing" :"denying" , d->domain_id, mfn, mfn + nr_mfns - 1); } +/* Do this unconditionally to cover errors on above failure paths. */ +memory_type_changed (d); }break ;@@ -791,6 +795,8 @@ long arch_do_domctl("ioport_map: error %ld denying dom%d access to [%x,%x]\n", ret, d->domain_id, fmp, fmp + np - 1); } +if ( !ret ) +memory_type_changed (d); }break ;xen/arch/x86/hvm/hvm.c +3
-0@@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct vcpu *v, u6if ( !hvm_funcs.set_guest_pat (v, guest_pat) ) v->arch.hvm_vcpu.pat_cr = guest_pat; + +memory_type_changed (v->domain); +return 1 ; }xen/arch/x86/hvm/mtrr.c +15
-2@@ -431,8 +431,12 @@ bool_t mtrr_def_type_msr_set(struct domareturn 0 ; } - m->enabled = enabled; - m->def_type = def_type; +if ( m->enabled != enabled || m->def_type != def_type ) + { + m->enabled = enabled; + m->def_type = def_type; +memory_type_changed (d); + }return 1 ; }@@ -452,6 +456,7 @@ bool_t mtrr_fix_range_msr_set(struct domreturn 0 ; fixed_range_base[row] = msr_content; +memory_type_changed (d); }return 1 ;@@ -496,6 +501,8 @@ bool_t mtrr_var_range_msr_set( m->overlapped =is_var_mtrr_overlapped (m); +memory_type_changed (d); +return 1 ; }@@ -690,6 +697,12 @@ static int hvm_load_mtrr_msr(struct domaHVM_REGISTER_SAVE_RESTORE (MTRR , hvm_save_mtrr_msr, hvm_load_mtrr_msr,1 ,HVMSR_PER_VCPU ); +void memory_type_changed (struct domain *d) +{ +if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0 ] ) +p2m_memory_type_changed (d); +} +uint8_t epte_get_entry_emt (struct domain *d,unsigned long gfn,mfn_t mfn,uint8_t *ipat,bool_t direct_mmio){
xen/arch/x86/hvm/mtrr.c +0
-2@@ -733,8 +746,7 @@ uint8_t epte_get_entry_emt(struct domainreturn MTRR_TYPE_WRBACK ; } - gmtrr_mtype = is_hvm_domain(d) && v && -d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] ?
+gmtrr_mtype=is_hvm_domain(d)&& v ?get_mtrr_type(&v->arch.hvm_vcpu.mtrr, (gfn << PAGE_SHIFT)) : MTRR_TYPE_WRBACK;xen/arch/x86/hvm/vmx/vmx.c +10
-0@@ -3016,6 +3016,16 @@ void vmx_vmexit_handler(struct cpu_user_break ; } +case EXIT_REASON_EPT_MISCONFIG : + { +paddr_t gpa; + +__vmread (GUEST_PHYSICAL_ADDRESS , &gpa); +if ( !ept_handle_misconfig (gpa) ) + goto exit_and_crash; +break ; + } +case EXIT_REASON_MONITOR_TRAP_FLAG : v->arch.hvm_vmx.exec_control &= ~CPU_BASED_MONITOR_TRAP_FLAG ;vmx_update_cpu_exec_control(v);xen/arch/x86/mm/p2m.c +12
-0@@ -200,6 +200,18 @@ void p2m_change_entry_type_global(structp2m_unlock (p2m); } +void p2m_memory_type_changed (struct domain *d) +{ +struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if ( p2m->memory_type_changed ) + { +p2m_lock (p2m); + p2m->memory_type_changed (p2m); +p2m_unlock (p2m); + } +} +mfn_t __get_gfn_type_access (struct p2m_domain *p2m,unsigned long gfn,p2m_type_t *t,p2m_access_t *a,p2m_query_t q,unsigned int *page_order,bool_t locked)xen/arch/x86/mm/p2m-ept.c +76
-0@@ -270,6 +270,124 @@ static int ept_next_level(struct p2m_domreturn GUEST_TABLE_NORMAL_PAGE ; } +static bool_t ept_invalidate_emt (mfn_t mfn) +{ +ept_entry_t *epte =map_domain_page (mfn_x (mfn)); +unsigned int i; +bool_t changed =0 ; + +for ( i =0 ; i <EPT_PAGETABLE_ENTRIES ; i++ ) + { +ept_entry_t e =atomic_read_ept_entry (&epte[i]); + +if ( !is_epte_valid (&e) || !is_epte_present (&e) || + e.emt ==MTRR_NUM_TYPES ) +continue ; + + e.emt =MTRR_NUM_TYPES ; +atomic_write_ept_entry (&epte[i], e); + changed =1 ; + } + +unmap_domain_page (epte); + +return changed; +} + +bool_t ept_handle_misconfig (uint64_t gpa) +{ +struct vcpu *curr = current; +struct p2m_domain *p2m =p2m_get_hostp2m (curr->domain); +struct ept_data *ept = &p2m->ept; +unsigned int level =ept_get_wl (ept); +unsigned long gfn =PFN_DOWN (gpa); +unsigned long mfn =ept_get_asr (ept); +ept_entry_t *epte; +int okay; + +if ( !mfn ) +return 0 ; + +p2m_lock (p2m); + + okay = -curr->arch.hvm_vmx.ept_spurious_misconfig; +for ( ; ; --level ) + { +ept_entry_t e; +unsigned int i; + + epte =map_domain_page (mfn); + i = (gfn >> (level *EPT_TABLE_ORDER )) & (EPT_PAGETABLE_ENTRIES -1 ); + e =atomic_read_ept_entry (&epte[i]); + +if ( level ==0 ||is_epte_superpage (&e) ) + { +uint8_t ipat =0 ; + +if ( e.emt !=MTRR_NUM_TYPES ) +break ; + +if ( level ==0 ) + { +for ( gfn -= i, i =0 ; i <EPT_PAGETABLE_ENTRIES ; ++i ) + { + e =atomic_read_ept_entry (&epte[i]); +if ( e.emt ==MTRR_NUM_TYPES ) + e.emt =0 ; +if ( !is_epte_valid (&e) || !is_epte_present (&e) ) +continue ; + e.emt =epte_get_entry_emt (p2m->domain, gfn + i, +_mfn (e.mfn), &ipat, + e.sa_p2mt == p2m_mmio_direct); + e.ipat = ipat; +atomic_write_ept_entry (&epte[i], e); + } + } +else + { + e.emt = epte_get_entry_emt(p2m->domain, gfn,
_mfn(e.mfn),xen/arch/x86/mm/p2m-ept.c +2
-0+ &ipat, + e.sa_p2mt ==
p2m_mmio_direct);xen/arch/x86/mm/p2m-ept.c +58
-0+ e.ipat = ipat; +atomic_write_ept_entry (&epte[i], e); + } + + okay =1 ; +break ; + } + +if ( e.emt ==MTRR_NUM_TYPES ) + { +ASSERT (is_epte_present (&e)); +ept_invalidate_emt (_mfn (e.mfn)); + e.emt =0 ; +atomic_write_ept_entry (&epte[i], e); +unmap_domain_page (epte); + okay =1 ; + } +else if (is_epte_present (&e) && !e.emt ) +unmap_domain_page (epte); +else +break ; + + mfn = e.mfn; + } + +unmap_domain_page (epte); +if ( okay >0 ) + { +struct vcpu *v; + +for_each_vcpu ( curr->domain, v ) + v->arch.hvm_vmx.ept_spurious_misconfig = 1; + } + curr->arch.hvm_vmx.ept_spurious_misconfig =0 ; +ept_sync_domain (p2m); +p2m_unlock (p2m); + +return !!okay; +} +/* * ept_set_entry() computes 'need_modify_vtd_table' for itself, * by observing whether any gfn->mfn translations are modified. @@ -660,6 +778,17 @@ static void ept_change_entry_type_globalept_sync_domain (p2m); } +static void ept_memory_type_changed (struct p2m_domain *p2m) +{ +unsigned long mfn =ept_get_asr (&p2m->ept); + +if ( !mfn ) +return ; + +if (ept_invalidate_emt (_mfn (mfn)) ) +ept_sync_domain (p2m); +} +static void __ept_sync_domain (void *info) {struct ept_data *ept = &((struct p2m_domain *)info)->ept;@@ -697,6 +826,7 @@ int ept_p2m_init(struct p2m_domain *p2m) p2m->set_entry = ept_set_entry; p2m->get_entry = ept_get_entry; p2m->change_entry_type_global = ept_change_entry_type_global; + p2m->memory_type_changed = ept_memory_type_changed; p2m->audit_p2m =NULL ;/* Set the memory type used when accessing EPT paging structures. */ @@ -737,6 +867,7 @@ static void ept_dump_p2m_table(unsigned [MTRR_TYPE_WRTHROUGH] = "WT", [MTRR_TYPE_WRPROT] = "WP", [MTRR_TYPE_WRBACK] = "WB", + [MTRR_NUM_TYPES] = "??" };for_each_domain (d)@@ -750,11 +881,16 @@ static void ept_dump_p2m_table(unsignedfor ( gfn =0 ; gfn <= p2m->max_mapped_pfn; gfn +=1UL << order ) { +char c =0 ; + gfn_remainder = gfn; table =map_domain_page (pagetable_get_pfn (p2m_get_pagetable (p2m)));for ( i =ept_get_wl (ept); i >0 ; i-- ) { + ept_entry = table + (gfn_remainder >> (i *EPT_TABLE_ORDER )); +if ( ept_entry->emt ==MTRR_NUM_TYPES ) + c ='?' ; ret =ept_next_level (p2m,1 , &table, &gfn_remainder, i);if ( ret != GUEST_TABLE_NORMAL_PAGE ) break;xen/arch/x86/mm/p2m-ept.c +1
-1@@ -775,7 +911,7 @@ static void ept_dump_p2m_table(unsigned memory_types[ept_entry->emt][0 ], memory_types[ept_entry->emt][1 ] ?: ept_entry->emt +'0' , - ept_entry->ipat ? '!' : ' '); + c ?: ept_entry->ipat ? '!' : ' ');if ( !(record_counter++ %100 ) )process_pending_softirqs ();xen/common/domctl.c +4
-0@@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe ret =iomem_permit_access (d, mfn, mfn + nr_mfns -1 ); else ret =iomem_deny_access (d, mfn, mfn + nr_mfns -1 ); +#ifdef CONFIG_X86 +if ( !ret ) +memory_type_changed (d); +#endif }break ;xen/include/asm-x86/hvm/vmx/vmcs.h +3
-0@@ -124,6 +124,9 @@ struct arch_vmx_struct {unsigned long host_cr0; +/* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */ +bool_t ept_spurious_misconfig; +/* Is the guest in real mode? */ uint8_t vmx_realmode;/* Are we emulating rather than VMENTERing? */ xen/include/asm-x86/hvm/vmx/vmx.h +1
-0@@ -520,6 +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m)void ept_p2m_uninit (struct p2m_domain *p2m);void ept_walk_table (struct domain *d,unsigned long gfn); +bool_t ept_handle_misconfig (uint64_t gpa);void setup_ept_dump (void );void update_guest_eip (void );xen/include/asm-x86/mtrr.h +1
-0@@ -88,6 +88,7 @@ extern bool_t mtrr_fix_range_msr_set(struint32_t row, uint64_t msr_content);extern bool_t mtrr_def_type_msr_set (struct domain *,struct mtrr_state *,uint64_t msr_content); +extern void memory_type_changed (struct domain *);extern bool_t pat_msr_set (uint64_t *pat,uint64_t msr);
bool_tis_var_mtrr_overlapped(struct mtrr_state *m);xen/include/asm-x86/p2m.h +1
-0@@ -233,6 +233,7 @@ struct p2m_domain {void (*change_entry_type_global)(struct p2m_domain *p2m,p2m_type_t ot,p2m_type_t nt); + void (*memory_type_changed)(struct p2m_domain*p2m);
void (*write_p2m_entry)(struct
p2m_domain*p2m,
unsigned long gfn,l1_pgentry_t*p,xen/include/asm-x86/p2m.h +3
-0@@ -506,6 +507,9 @@ void p2m_change_type_range(struct domainp2m_type_t p2m_change_type (struct domain *d,unsigned long gfn,p2m_type_t ot,p2m_type_t nt); +/* Report a change affecting memory types. */ +void p2m_memory_type_changed (struct domain *d); +/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn);int clear_mmio_p2m_entry (struct domain *d,unsigned long gfn);
Re: [PATCH v3 1/5] EPT: force re‑evaluation of memory type as necessary
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.14 at 10:52, <[email protected]> wrote:
any data on improvement against original code?
What would you expect here? I didn't observe an actual problem, but I connected reports of GPU pass-through problems with the obvious problems the old code had.
Quoted 2 lines
one small comment is whether it's clearer to define a new macro to indicate the purpose instead of using
MTRR_NUM_TYPES?
Yeah, but it's easily enough searchable for, so we could replace this if the need ever arises.
Jan
Re: [PATCH v3 1/5] EPT: force re‑evaluation of memory type as necessary
Quoted 10 lines
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, April 10, 2014 6:20 PMQuoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.14 at 10:52, <[email protected]> wrote:
any data on improvement against original code?
What would you expect here? I didn't observe an actual problem, but I connected reports of GPU pass-through problems with the obvious problems the old code had.
any link to some of those reported problems?
Thanks
Kevin
Re: [PATCH v3 1/5] EPT: force re‑evaluation of memory type as necessary
Quoted 5 lines
Quoted 5 lines
Quoted 1 line · On 10.04.14 at 12:25, <[email protected]> wrote:
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, April 10, 2014 6:20 PMQuoted 1 line
Quoted 1 line
Quoted 1 line · On 10.04.14 at 10:52, <[email protected]> wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
any data on improvement against original code?
What would you expect here? I didn't observe an actual problem, but I connected reports of GPU pass-through problems with the obvious problems the old code had.
any link to some of those reported problems?
No, sorry.
Jan
PATCH v3 2/5 EPT: split super pages upon mismatching memory types
In reply to Jan Beulich... between constituent pages. To indicate such, the page order is being passed down to the vMTRR routines, with a negative return value (possible only on order-non-zero pages) indicating such collisions.
Some code redundancy reduction is being done to ept_set_entry() along the way, allowing the new handling to be centralized to a single place there.
In order to keep ept_set_entry() fast and simple, the actual splitting is being deferred to the EPT_MISCONFIG VM exit handler.
---
v3: Adjust for code movement in preceding patch.
xen/arch/x86/hvm/mtrr.c +46 -30
@@ -222,30 +222,40 @@ void hvm_vcpu_cacheattr_destroy(struct v/* * Get MTRR memory type for physical address pa. +* +* May return a negative value when order > 0, indicating to the caller +* that the respective mapping needs splitting. */ -static uint8_t get_mtrr_type (struct mtrr_state *m,paddr_t pa) +static int get_mtrr_type (const struct mtrr_state *m, +paddr_t pa,unsigned int order) { -int32_t addr, seg, index;uint8_t overlap_mtrr =0 ;uint8_t overlap_mtrr_pos =0 ; -uint64_t phys_base; -uint64_t phys_mask; -uint8_t num_var_ranges = m->mtrr_cap &0xff ; +uint64_t mask = -(uint64_t )PAGE_SIZE << order; +unsigned int seg, num_var_ranges = m->mtrr_cap &0xff ;if (unlikely (!(m->enabled &0x2 )) )return MTRR_TYPE_UNCACHABLE ; + pa &= mask;if ( (pa <0x100000 ) && (m->enabled &1 ) ) { -/* Fixed range MTRR takes effective */ - addr = (uint32_t ) pa; +/* Fixed range MTRR takes effect. */ +uint32_t addr = (uint32_t )pa, index; +if ( addr <0x80000 ) { +/* 0x00000 ... 0x7FFFF in 64k steps */ +if ( order >4 ) +return -1 ; seg = (addr >>16 );return m->fixed_ranges[seg]; }else if ( addr <0xc0000 ) { +/* 0x80000 ... 0xBFFFF in 16k steps */ +if ( order >2 ) +return -1 ; seg = (addr -0x80000 ) >>14 ; index = (seg >>3 ) +1 ; seg &=7 ;/* select 0-7 segments */
@@ -253,7 +263,9 @@ static uint8_t get_mtrr_type(struct mtrr }else { -/* 0xC0000 --- 0x100000 */ +/* 0xC0000 ... 0xFFFFF in 4k steps */ +if ( order ) +return -1 ; seg = (addr -0xc0000 ) >>12 ; index = (seg >>3 ) +3 ; seg &=7 ;/* select 0-7 segments */
@@ -264,14 +276,15 @@ static uint8_t get_mtrr_type(struct mtrr/* Match with variable MTRRs. */ for ( seg =0 ; seg < num_var_ranges; seg++ ) { - phys_base = ((uint64_t *)m->var_ranges)[seg*2 ]; - phys_mask = ((uint64_t *)m->var_ranges)[seg*2 +1 ]; +uint64_t phys_base = m->var_ranges[seg].base; +uint64_t phys_mask = m->var_ranges[seg].mask; +if ( phys_mask &MTRR_PHYSMASK_VALID ) { -if ( ((uint64_t ) pa & phys_mask) >>MTRR_PHYSMASK_SHIFT == - (phys_base & phys_mask) >>MTRR_PHYSMASK_SHIFT ) + phys_mask &= mask; +if ( (pa & phys_mask) == (phys_base & phys_mask) ) { -if (unlikely (m->overlapped) ) +if (unlikely (m->overlapped) || order ) { overlap_mtrr |=1 << (phys_base &MTRR_PHYSBASE_TYPE_MASK ); overlap_mtrr_pos = phys_base &MTRR_PHYSBASE_TYPE_MASK ;
@@ -285,23 +298,24 @@ static uint8_t get_mtrr_type(struct mtrr } } -/* Overlapped or not found. */ +/* Not found? */ if (unlikely (overlap_mtrr ==0 ) )return m->def_type; -if (likely (!(overlap_mtrr & ~( ((uint8_t )1 ) << overlap_mtrr_pos ))) ) -/* Covers both one variable memory range matches and -* two or more identical match. -*/ +/* One match, or multiple identical ones? */ +if (likely (overlap_mtrr == (1 << overlap_mtrr_pos)) )return overlap_mtrr_pos; +if ( order ) +return -1 ; + +/* Two or more matches, one being UC? */ if ( overlap_mtrr & (1 <<MTRR_TYPE_UNCACHABLE ) ) -/* Two or more match, one is UC. */ return MTRR_TYPE_UNCACHABLE ; -if ( !(overlap_mtrr & - ~((1 <<MTRR_TYPE_WRTHROUGH ) | (1 <<MTRR_TYPE_WRBACK ))) ) -/* Two or more match, WT and WB. */ +/* Two or more matches, all of them WT and WB? */ +if ( overlap_mtrr == + ((1 <<MTRR_TYPE_WRTHROUGH ) | (1 <<MTRR_TYPE_WRBACK )) )return MTRR_TYPE_WRTHROUGH ;/* Behaviour is undefined, but return the last overlapped type. */
@@ -341,7 +355,7 @@ static uint8_t effective_mm_type(struct* just use it */ if ( gmtrr_mtype ==NO_HARDCODE_MEM_TYPE ) - mtrr_mtype =get_mtrr_type (m, gpa); + mtrr_mtype =get_mtrr_type (m, gpa,0 );else mtrr_mtype = gmtrr_mtype;
@@ -370,7 +384,7 @@ uint32_t get_pat_flags(struct vcpu *v, guest_eff_mm_type =effective_mm_type (g, pat, gpaddr, gl1e_flags, gmtrr_mtype);/* 2. Get the memory type of host physical address, with MTRR */ - shadow_mtrr_type =get_mtrr_type (&mtrr_state, spaddr); + shadow_mtrr_type =get_mtrr_type (&mtrr_state, spaddr,0 );/* 3. Find the memory type in PAT, with host MTRR memory type * and guest effective memory type.
@@ -703,10 +717,10 @@ void memory_type_changed(struct domain *p2m_memory_type_changed (d); } -uint8_t epte_get_entry_emt (struct domain *d,unsigned long gfn,mfn_t mfn, -uint8_t *ipat,bool_t direct_mmio) +int epte_get_entry_emt (struct domain *d,unsigned long gfn,mfn_t mfn, +unsigned int order,uint8_t *ipat,bool_t direct_mmio) { -uint8_t gmtrr_mtype, hmtrr_mtype; +int gmtrr_mtype, hmtrr_mtype;uint32_t type;struct vcpu *v = current;
@@ -747,10 +761,12 @@ uint8_t epte_get_entry_emt(struct domain } gmtrr_mtype =is_hvm_domain (d) && v ? -get_mtrr_type (&v->arch.hvm_vcpu.mtrr, (gfn <<PAGE_SHIFT )) : +get_mtrr_type (&v->arch.hvm_vcpu.mtrr, + gfn <<PAGE_SHIFT , order) :MTRR_TYPE_WRBACK ; - - hmtrr_mtype =get_mtrr_type (&mtrr_state, (mfn_x (mfn) <<PAGE_SHIFT )); + hmtrr_mtype =get_mtrr_type (&mtrr_state,mfn_x (mfn) <<PAGE_SHIFT , order); +if ( gmtrr_mtype <0 || hmtrr_mtype <0 ) +return -1 ;/* If both types match we're fine. */ if (likely (gmtrr_mtype == hmtrr_mtype) )
xen/arch/x86/mm/p2m-ept.c +32 -32
114 lines not on this page · raw message
xen/include/asm-x86/mtrr.h +3 -2
@@ -72,8 +72,9 @@ extern int mtrr_del_page(int reg, unsignextern void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi);extern u32 get_pat_flags (struct vcpu *v,u32 gl1e_flags,paddr_t gpaddr,paddr_t spaddr,uint8_t gmtrr_mtype); -extern uint8_t epte_get_entry_emt (struct domain *d,unsigned long gfn, -mfn_t mfn,uint8_t *ipat,bool_t direct_mmio); +extern int epte_get_entry_emt (struct domain *,unsigned long gfn,mfn_t mfn, +unsigned int order,uint8_t *ipat, +bool_t direct_mmio);extern void ept_change_entry_emt_with_range (struct domain *d,unsigned long start_gfn,unsigned long end_gfn);extern unsigned char pat_type_2_pte_flags (unsigned char pat_type);
x86/EPT: split super pages upon mismatching memory types
... between constituent pages. To indicate such, the page order is being passed down to the vMTRR routines, with a negative return value (possible only on order-non-zero pages) indicating such collisions.
Some code redundancy reduction is being done to ept_set_entry() along the way, allowing the new handling to be centralized to a single place there.
In order to keep ept_set_entry() fast and simple, the actual splitting is being deferred to the EPT_MISCONFIG VM exit handler.
---
v3: Adjust for code movement in preceding patch.
xen/arch/x86/hvm/mtrr.c +46 -30
154 lines not on this page · raw message
xen/arch/x86/mm/p2m-ept.c +32 -32
114 lines not on this page · raw message
xen/include/asm-x86/mtrr.h +3 -2
@@ -72,8 +72,9 @@ extern int mtrr_del_page(int reg, unsignextern void mtrr_centaur_report_mcr (int mcr,u32 lo,u32 hi);extern u32 get_pat_flags (struct vcpu *v,u32 gl1e_flags,paddr_t gpaddr,paddr_t spaddr,uint8_t gmtrr_mtype); -extern uint8_t epte_get_entry_emt (struct domain *d,unsigned long gfn, -mfn_t mfn,uint8_t *ipat,bool_t direct_mmio); +extern int epte_get_entry_emt (struct domain *,unsigned long gfn,mfn_t mfn, +unsigned int order,uint8_t *ipat, +bool_t direct_mmio);extern void ept_change_entry_emt_with_range (struct domain *d,unsigned long start_gfn,unsigned long end_gfn);extern unsigned char pat_type_2_pte_flags (unsigned char pat_type);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 3/5 EPT: IOMMU snoop capability should not affect memory type selection
In reply to Jan BeulichThis capability solely makes a statement on cache coherency guarantees by the IOMMU. It does specifically not imply any further guarantees implied by certain memory types (cachability, ordering).
---
This already drops a few bits of the changes done for XSA-60. I think that if the earlier patches (and particularly the use of the EPT_MISCONFIG VM exit) are being accepted, we should switch that whole mechanism, avoiding the need to play games with the guest's PAT MSR.
xen/arch/x86/hvm/hvm.c +11 -16
@@ -1950,6 +1950,7 @@ static void hvm_update_cr(struct vcpu *vint hvm_set_cr0 (unsigned long value) {struct vcpu *v = current; +struct domain *d = v->domain;unsigned long gfn, old_value = v->arch.hvm_vcpu.guest_cr[0 ];struct page_info *page;
@@ -1973,8 +1974,8 @@ int hvm_set_cr0(unsigned long value) goto gpf;/* A pvh is not expected to change to real mode. */ -if (is_pvh_vcpu (v) - && (value & (X86_CR0_PE |X86_CR0_PG )) != (X86_CR0_PG |X86_CR0_PE ) ) +if (is_pvh_domain (d) && + (value & (X86_CR0_PE |X86_CR0_PG )) != (X86_CR0_PG |X86_CR0_PE ) ) {printk (XENLOG_G_WARNING "PVH attempting to turn off PE/PG. CR0:%lx\n" , value);
@@ -1996,16 +1997,16 @@ int hvm_set_cr0(unsigned long value)hvm_update_guest_efer (v); } -if ( !paging_mode_hap (v->domain) ) +if ( !paging_mode_hap (d) ) {/* The guest CR3 must be pointing to the guest physical. */ gfn = v->arch.hvm_vcpu.guest_cr[3 ]>>PAGE_SHIFT ; - page =get_page_from_gfn (v->domain, gfn,NULL ,P2M_ALLOC ); + page =get_page_from_gfn (d, gfn,NULL ,P2M_ALLOC );if ( !page ) {gdprintk (XENLOG_ERR ,"Invalid CR3 value = %lx\n" , v->arch.hvm_vcpu.guest_cr[3 ]); -domain_crash (v->domain); +domain_crash (d);return X86EMUL_UNHANDLEABLE ; }
@@ -2032,24 +2033,18 @@ int hvm_set_cr0(unsigned long value)hvm_update_guest_efer (v); } -if ( !paging_mode_hap (v->domain) ) +if ( !paging_mode_hap (d) ) {put_page (pagetable_get_page (v->arch.guest_table)); v->arch.guest_table =pagetable_null (); } } -/* -* When cr0.cd setting -* 1. For guest w/o VT-d, and for guest with VT-d but snooped, Xen need not -* do anything, since hardware snoop mechanism has ensured cache coherency; -* 2. For guest with VT-d but non-snooped, cache coherency cannot be -* guaranteed by h/w so need emulate UC memory type to guest. -*/ if ( ((value ^ old_value) &X86_CR0_CD ) && -has_arch_pdevs (v->domain) && - iommu_enabled && !iommu_snoop && - hvm_funcs.handle_cd ) + iommu_enabled && hvm_funcs.handle_cd && + (!rangeset_is_empty (d->iomem_caps) || + !rangeset_is_empty (d->arch.ioport_caps) || +has_arch_pdevs (d)) ) hvm_funcs.handle_cd (v, value);hvm_update_cr (v,0 , value);
xen/arch/x86/hvm/mtrr.c +1 -7
@@ -713,7 +713,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_savevoid memory_type_changed (struct domain *d) { -if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0 ] ) +if ( iommu_enabled && d->vcpu && d->vcpu[0 ] )p2m_memory_type_changed (d); }
@@ -754,12 +754,6 @@ int epte_get_entry_emt(struct domain *d,return MTRR_TYPE_WRBACK ; } -if ( iommu_snoop ) - { - *ipat =1 ; -return MTRR_TYPE_WRBACK ; - } - gmtrr_mtype =is_hvm_domain (d) && v ?get_mtrr_type (&v->arch.hvm_vcpu.mtrr, gfn << PAGE_SHIFT, order) :
x86/EPT: IOMMU snoop capability should not affect memory type selection
This capability solely makes a statement on cache coherency guarantees by the IOMMU. It does specifically not imply any further guarantees implied by certain memory types (cachability, ordering).
---
This already drops a few bits of the changes done for XSA-60. I think that if the earlier patches (and particularly the use of the EPT_MISCONFIG VM exit) are being accepted, we should switch that whole mechanism, avoiding the need to play games with the guest's PAT MSR.
xen/arch/x86/hvm/hvm.c +11 -16
@@ -1950,6 +1950,7 @@ static void hvm_update_cr(struct vcpu *vint hvm_set_cr0 (unsigned long value) {struct vcpu *v = current; +struct domain *d = v->domain;unsigned long gfn, old_value = v->arch.hvm_vcpu.guest_cr[0 ];struct page_info *page;
@@ -1973,8 +1974,8 @@ int hvm_set_cr0(unsigned long value) goto gpf;/* A pvh is not expected to change to real mode. */ -if (is_pvh_vcpu (v) - && (value & (X86_CR0_PE |X86_CR0_PG )) != (X86_CR0_PG |X86_CR0_PE ) ) +if (is_pvh_domain (d) && + (value & (X86_CR0_PE |X86_CR0_PG )) != (X86_CR0_PG |X86_CR0_PE ) ) {printk (XENLOG_G_WARNING "PVH attempting to turn off PE/PG. CR0:%lx\n" , value);
@@ -1996,16 +1997,16 @@ int hvm_set_cr0(unsigned long value)hvm_update_guest_efer (v); } -if ( !paging_mode_hap (v->domain) ) +if ( !paging_mode_hap (d) ) {/* The guest CR3 must be pointing to the guest physical. */ gfn = v->arch.hvm_vcpu.guest_cr[3 ]>>PAGE_SHIFT ; - page =get_page_from_gfn (v->domain, gfn,NULL ,P2M_ALLOC ); + page =get_page_from_gfn (d, gfn,NULL ,P2M_ALLOC );if ( !page ) {gdprintk (XENLOG_ERR ,"Invalid CR3 value = %lx\n" , v->arch.hvm_vcpu.guest_cr[3 ]); -domain_crash (v->domain); +domain_crash (d);return X86EMUL_UNHANDLEABLE ; }
@@ -2032,24 +2033,18 @@ int hvm_set_cr0(unsigned long value)hvm_update_guest_efer (v); } -if ( !paging_mode_hap (v->domain) ) +if ( !paging_mode_hap (d) ) {put_page (pagetable_get_page (v->arch.guest_table)); v->arch.guest_table =pagetable_null (); } } -/* -* When cr0.cd setting -* 1. For guest w/o VT-d, and for guest with VT-d but snooped, Xen need not -* do anything, since hardware snoop mechanism has ensured cache coherency; -* 2. For guest with VT-d but non-snooped, cache coherency cannot be -* guaranteed by h/w so need emulate UC memory type to guest. -*/ if ( ((value ^ old_value) &X86_CR0_CD ) && -has_arch_pdevs (v->domain) && - iommu_enabled && !iommu_snoop && - hvm_funcs.handle_cd ) + iommu_enabled && hvm_funcs.handle_cd && + (!rangeset_is_empty (d->iomem_caps) || + !rangeset_is_empty (d->arch.ioport_caps) || +has_arch_pdevs (d)) ) hvm_funcs.handle_cd (v, value);hvm_update_cr (v,0 , value);
xen/arch/x86/hvm/mtrr.c +1 -7
@@ -713,7 +713,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_savevoid memory_type_changed (struct domain *d) { -if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0 ] ) +if ( iommu_enabled && d->vcpu && d->vcpu[0 ] )p2m_memory_type_changed (d); }
@@ -754,12 +754,6 @@ int epte_get_entry_emt(struct domain *d,return MTRR_TYPE_WRBACK ; } -if ( iommu_snoop ) - { - *ipat =1 ; -return MTRR_TYPE_WRBACK ; - } - gmtrr_mtype =is_hvm_domain (d) && v ?get_mtrr_type (&v->arch.hvm_vcpu.mtrr, gfn << PAGE_SHIFT, order) :
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 4/5 x86/HVM: fix pinned cache attribute handling
In reply to Jan Beulich- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)
Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.
xen/arch/x86/hvm/mtrr.c +82 -7
@@ -551,6 +551,15 @@ bool_t mtrr_pat_not_equal(struct vcpu *vreturn 0 ; } +struct hvm_mem_pinned_cacheattr_range { +struct list_head list; +uint64_t start, end; +uint32_t type; +struct rcu_head rcu; +}; + +static DEFINE_RCU_READ_LOCK (pinned_cacheattr_rcu_lock); +void hvm_init_cacheattr_region_list (struct domain *d) {
@@ -573,30 +582,47 @@ void hvm_destroy_cacheattr_region_list( } } -int32_t hvm_get_mem_pinned_cacheattr ( +int hvm_get_mem_pinned_cacheattr (struct domain *d,uint64_t guest_fn, +unsigned int order,uint32_t *type) {struct hvm_mem_pinned_cacheattr_range *range; +int rc =0 ; - *type =0 ; + *type = ~0 ;if ( !is_hvm_domain (d) )return 0 ; +rcu_read_lock (&pinned_cacheattr_rcu_lock);list_for_each_entry_rcu ( range, &d->arch.hvm_domain.pinned_cacheattr_ranges, list ) { -if ( (guest_fn >= range->start) && (guest_fn <= range->end) ) +if ( (guest_fn >= range->start) && + (guest_fn + (1UL << order) -1 <= range->end) ) { *type = range->type; -return 1 ; + rc =1 ; +break ; + } +if ( (guest_fn <= range->end) && + (range->start <= guest_fn + (1UL << order) -1 ) ) + { + rc =-1 ; +break ; } } +rcu_read_unlock (&pinned_cacheattr_rcu_lock); -return 0 ; +return rc; +} + +static void free_pinned_cacheattr_entry (struct rcu_head *rcu) +{ +xfree (container_of(rcu ,struct hvm_mem_pinned_cacheattr_range ,rcu )); } int32_t hvm_set_mem_pinned_cacheattr(
@@ -606,6 +632,28 @@ int32_t hvm_set_mem_pinned_cacheattr( uint32_t type) {struct hvm_mem_pinned_cacheattr_range *range; +int rc =1 ; + +if ( !is_hvm_domain (d) || gfn_end < gfn_start ) +return 0 ; + +if ( type ==XEN_DOMCTL_DELETE_MEM_CACHEATTR ) + { +/* Remove the requested range. */ +rcu_read_lock (&pinned_cacheattr_rcu_lock); +list_for_each_entry_rcu ( range, + &d->arch.hvm_domain.pinned_cacheattr_ranges, + list ) +if ( range->start == gfn_start && range->end == gfn_end ) + { +rcu_read_unlock (&pinned_cacheattr_rcu_lock); +list_del_rcu (&range->list); +call_rcu (&range->rcu, free_pinned_cacheattr_entry); +return 0 ; + } +rcu_read_unlock (&pinned_cacheattr_rcu_lock); +return -ENOENT ; + } if ( !((type == PAT_TYPE_UNCACHABLE) || (type == PAT_TYPE_WRCOMB) ||
@@ -616,6 +664,27 @@ int32_t hvm_set_mem_pinned_cacheattr( !is_hvm_domain(d) )return -EINVAL ; +rcu_read_lock (&pinned_cacheattr_rcu_lock); +list_for_each_entry_rcu ( range, + &d->arch.hvm_domain.pinned_cacheattr_ranges, + list ) + { +if ( range->start == gfn_start && range->end == gfn_end ) + { + range->type = type; + rc =0 ; +break ; + } +if ( range->start <= gfn_end && gfn_start <= range->end ) + { + rc = -EBUSY ; +break ; + } + } +rcu_read_unlock (&pinned_cacheattr_rcu_lock); +if ( rc <=0 ) +return rc; + range = xzalloc(struct hvm_mem_pinned_cacheattr_range);if ( range ==NULL )return -ENOMEM ;
@@ -732,8 +801,14 @@ int epte_get_entry_emt(struct domain *d,if ( !mfn_valid (mfn_x (mfn)) )return MTRR_TYPE_UNCACHABLE ; -if (hvm_get_mem_pinned_cacheattr (d, gfn, &type) ) -return type; +switch (hvm_get_mem_pinned_cacheattr (d, gfn, order, &type) ) + { +case 1 : + *ipat =1 ; +return type !=PAT_TYPE_UC_MINUS ? type :PAT_TYPE_UNCACHABLE ; +case -1 : +return -1 ; + } if ( !iommu_enabled || (rangeset_is_empty(d->iomem_caps) &&
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -601,7 +601,7 @@ _sh_propagate(struct vcpu *v,* 3) if disables snoop control, compute the PAT index with * gMTRR and gPAT. */ -if (hvm_get_mem_pinned_cacheattr (d,gfn_x (target_gfn), &type) ) +if (hvm_get_mem_pinned_cacheattr (d,gfn_x (target_gfn),0 , &type) ) sflags |=pat_type_2_pte_flags (type);else if ( d->arch.hvm_domain.is_in_uc_mode ) sflags |=pat_type_2_pte_flags (PAT_TYPE_UNCACHABLE );
xen/include/asm-x86/hvm/cacheattr.h +4 -8
@@ -1,12 +1,6 @@#ifndef __HVM_CACHEATTR_H__#define __HVM_CACHEATTR_H__ -struct hvm_mem_pinned_cacheattr_range { -struct list_head list; -uint64_t start, end; -uint32_t type; -}; -void hvm_init_cacheattr_region_list (struct domain *d); void hvm_destroy_cacheattr_region_list(
@@ -15,11 +9,13 @@ void hvm_destroy_cacheattr_region_list(/* * To see guest_fn is in the pinned range or not, * if yes, return 1, and set type to value in this range -* if no, return 0, and set type to 0 +* if no, return 0, setting type to ~0 +* if ambiguous, return -1, setting type to ~0 (possible only for order > 0) */ -int32_t hvm_get_mem_pinned_cacheattr ( +int hvm_get_mem_pinned_cacheattr (struct domain *d,uint64_t guest_fn, +unsigned int order,uint32_t *type);
xen/include/public/domctl.h +1 -0
@@ -555,6 +555,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_iopor#define XEN_DOMCTL_MEM_CACHEATTR_WP 5#define XEN_DOMCTL_MEM_CACHEATTR_WB 6#define XEN_DOMCTL_MEM_CACHEATTR_UCM 7 +#define XEN_DOMCTL_DELETE_MEM_CACHEATTR (~(uint32_t)0)struct xen_domctl_pin_mem_cacheattr {uint64_aligned_t start, end;uint32_t type;/* XEN_DOMCTL_MEM_CACHEATTR_* */
x86: fix pinned cache attribute handling
- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)
Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.
xen/arch/x86/hvm/mtrr.c +82 -7
138 lines not on this page · raw message
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -601,7 +601,7 @@ _sh_propagate(struct vcpu *v,* 3) if disables snoop control, compute the PAT index with * gMTRR and gPAT. */ -if (hvm_get_mem_pinned_cacheattr (d,gfn_x (target_gfn), &type) ) +if (hvm_get_mem_pinned_cacheattr (d,gfn_x (target_gfn),0 , &type) ) sflags |=pat_type_2_pte_flags (type);else if ( d->arch.hvm_domain.is_in_uc_mode ) sflags |=pat_type_2_pte_flags (PAT_TYPE_UNCACHABLE );
xen/include/asm-x86/hvm/cacheattr.h +4 -8
@@ -1,12 +1,6 @@#ifndef __HVM_CACHEATTR_H__#define __HVM_CACHEATTR_H__ -struct hvm_mem_pinned_cacheattr_range { -struct list_head list; -uint64_t start, end; -uint32_t type; -}; -void hvm_init_cacheattr_region_list (struct domain *d); void hvm_destroy_cacheattr_region_list(
@@ -15,11 +9,13 @@ void hvm_destroy_cacheattr_region_list(/* * To see guest_fn is in the pinned range or not, * if yes, return 1, and set type to value in this range -* if no, return 0, and set type to 0 +* if no, return 0, setting type to ~0 +* if ambiguous, return -1, setting type to ~0 (possible only for order > 0) */ -int32_t hvm_get_mem_pinned_cacheattr ( +int hvm_get_mem_pinned_cacheattr (struct domain *d,uint64_t guest_fn, +unsigned int order,uint32_t *type);
xen/include/public/domctl.h +1 -0
@@ -555,6 +555,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_iopor#define XEN_DOMCTL_MEM_CACHEATTR_WP 5#define XEN_DOMCTL_MEM_CACHEATTR_WB 6#define XEN_DOMCTL_MEM_CACHEATTR_UCM 7 +#define XEN_DOMCTL_DELETE_MEM_CACHEATTR (~(uint32_t)0)struct xen_domctl_pin_mem_cacheattr {uint64_aligned_t start, end;uint32_t type;/* XEN_DOMCTL_MEM_CACHEATTR_* */
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v3 4/5] x86/HVM: fix pinned cache attribute handling
Quoted 7 lines
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, April 07, 2014 6:10 PM- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
could you elaborate a bit how an EPT page split is forced with this change?
Quoted 22 lines
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag inepte_get_entry_emt()when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move structhvm_mem_pinned_cacheattr_rangefrom header to C file (used only there)Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Tim Deegan <[email protected]>xen/arch/x86/hvm/mtrr.c +0
-1@@ -732,8 +801,14 @@ int epte_get_entry_emt(struct domain *d,if ( !mfn_valid (mfn_x (mfn)) )return MTRR_TYPE_UNCACHABLE ; -if (hvm_get_mem_pinned_cacheattr (d, gfn, &type) )
xen/arch/x86/hvm/mtrr.c int epte_get_entry_emt(struct domain *d, · line 812
Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +8
-1-return type; +switch (hvm_get_mem_pinned_cacheattr (d, gfn, order, &type) ) + { +case 1 : + *ipat =1 ; +return type !=PAT_TYPE_UC_MINUS ? type :PAT_TYPE_UNCACHABLE ; +case -1 : +return -1 ; + }
what does '-1' mean for a cache type? I didn't see callers check this value, e.g:
/* Construct the new entry, and then write it once */
new_entry.emt = epte_get_entry_emt(p2m->domain, gfn, mfn, &ipat,
direct_mmio);Thanks
Kevin
Re: [PATCH v3 4/5] x86/HVM: fix pinned cache attribute handling
In reply to Jan Beulichforgot the dumb questions. some problem on my mail rules, so only [4/5] reaches my inbox, while others are redirected to other folders. will review them together. :-)
Quoted 59 lines
-----Original Message-----
From: Tian, Kevin
Sent: Thursday, April 10, 2014 4:31 PM
To: 'Jan Beulich'; xen-devel
Cc: Dong, Eddie; Nakajima, Jun; Keir Fraser; Tim Deegan
Subject: RE: [PATCH v3 4/5] x86/HVM: fix pinned cache attribute handlingQuoted 7 lines
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, April 07, 2014 6:10 PM- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)could you elaborate a bit how an EPT page split is forced with this change?
Quoted 22 lines
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag inepte_get_entry_emt()when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move structhvm_mem_pinned_cacheattr_rangefrom header to C file (used only there)Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Tim Deegan <[email protected]>xen/arch/x86/hvm/mtrr.c +0
-1@@ -732,8 +801,14 @@ int epte_get_entry_emt(struct domain *d,if ( !mfn_valid (mfn_x (mfn)) )return MTRR_TYPE_UNCACHABLE ; -if (hvm_get_mem_pinned_cacheattr (d, gfn, &type) )xen/arch/x86/hvm/mtrr.c int epte_get_entry_emt(struct domain *d, · line 812
Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +8
-1-return type; +switch (hvm_get_mem_pinned_cacheattr (d, gfn, order, &type) ) + { +case 1 : + *ipat =1 ; +return type !=PAT_TYPE_UC_MINUS ? type :PAT_TYPE_UNCACHABLE ; +case -1 : +return -1 ; + }what does '-1' mean for a cache type? I didn't see callers check this value, e.g:
/* Construct the new entry, and then write it once */ new_entry.emt = epte_get_entry_emt(p2m->domain, gfn, mfn, &ipat, direct_mmio);Thanks
Kevin
Re: [PATCH v3 4/5] x86/HVM: fix pinned cache attribute handling
In reply to Jan BeulichQuoted 16 lines
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, April 07, 2014 6:10 PM- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag inepte_get_entry_emt()when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move structhvm_mem_pinned_cacheattr_rangefrom header to C file (used only there)
Quoted 7 lines
Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Tim Deegan <[email protected]>
Quoted 205 lines
xen/arch/x86/hvm/mtrr.c +31
-5@@ -551,6 +551,15 @@ bool_t mtrr_pat_not_equal(struct vcpu *vreturn 0 ; } +struct hvm_mem_pinned_cacheattr_range { +struct list_head list; +uint64_t start, end; +uint32_t type; +struct rcu_head rcu; +}; + +static DEFINE_RCU_READ_LOCK (pinned_cacheattr_rcu_lock); +void hvm_init_cacheattr_region_list (struct domain *d) {@@ -573,30 +582,47 @@ void hvm_destroy_cacheattr_region_list( } } -int32_t hvm_get_mem_pinned_cacheattr ( +int hvm_get_mem_pinned_cacheattr (struct domain *d,uint64_t guest_fn, +unsigned int order,uint32_t *type) {struct hvm_mem_pinned_cacheattr_range *range; +int rc =0 ; - *type =0 ; + *type = ~0 ;if ( !is_hvm_domain (d) )return 0 ; +rcu_read_lock (&pinned_cacheattr_rcu_lock);list_for_each_entry_rcu ( range, &d->arch.hvm_domain.pinned_cacheattr_ranges, list ) { -if ( (guest_fn >= range->start) && (guest_fn <= range->end) ) +if ( (guest_fn >= range->start) && + (guest_fn + (1UL << order) -1 <= range->end) ) { *type = range->type; -return 1 ; + rc =1 ; +break ; + } +if ( (guest_fn <= range->end) && + (range->start <= guest_fn + (1UL << order) -1 ) ) + { + rc =-1 ; +break ; } } +rcu_read_unlock (&pinned_cacheattr_rcu_lock); -return 0 ; +return rc; +} + +static void free_pinned_cacheattr_entry (struct rcu_head *rcu) +{ +xfree (container_of(rcu ,struct hvm_mem_pinned_cacheattr_range ,rcu )); }
int32_thvm_set_mem_pinned_cacheattr(xen/arch/x86/hvm/mtrr.c +22
-0@@ -606,6 +632,28 @@ int32_t hvm_set_mem_pinned_cacheattr( uint32_t type) {struct hvm_mem_pinned_cacheattr_range *range; +int rc =1 ; + +if ( !is_hvm_domain (d) || gfn_end < gfn_start ) +return 0 ; + +if ( type ==XEN_DOMCTL_DELETE_MEM_CACHEATTR ) + { +/* Remove the requested range. */ +rcu_read_lock (&pinned_cacheattr_rcu_lock); +list_for_each_entry_rcu ( range, + &d->arch.hvm_domain.pinned_cacheattr_ranges, + list ) +if ( range->start == gfn_start && range->end == gfn_end ) + { +rcu_read_unlock (&pinned_cacheattr_rcu_lock); +list_del_rcu (&range->list); +call_rcu (&range->rcu, free_pinned_cacheattr_entry); +return 0 ; + } +rcu_read_unlock (&pinned_cacheattr_rcu_lock); +return -ENOENT ; + } if ( !((type == PAT_TYPE_UNCACHABLE) ||(type == PAT_TYPE_WRCOMB) ||xen/arch/x86/hvm/mtrr.c +21
-0@@ -616,6 +664,27 @@ int32_t hvm_set_mem_pinned_cacheattr( !is_hvm_domain(d) )return -EINVAL ; +rcu_read_lock (&pinned_cacheattr_rcu_lock); +list_for_each_entry_rcu ( range, + &d->arch.hvm_domain.pinned_cacheattr_ranges, + list ) + { +if ( range->start == gfn_start && range->end == gfn_end ) + { + range->type = type; + rc =0 ; +break ; + } +if ( range->start <= gfn_end && gfn_start <= range->end ) + { + rc = -EBUSY ; +break ; + } + } +rcu_read_unlock (&pinned_cacheattr_rcu_lock); +if ( rc <=0 ) +return rc; + range = xzalloc(struct hvm_mem_pinned_cacheattr_range);if ( range ==NULL )return -ENOMEM;xen/arch/x86/hvm/mtrr.c +8
-2@@ -732,8 +801,14 @@ int epte_get_entry_emt(struct domain *d,if ( !mfn_valid (mfn_x (mfn)) )return MTRR_TYPE_UNCACHABLE ; -if (hvm_get_mem_pinned_cacheattr (d, gfn, &type) ) -return type; +switch (hvm_get_mem_pinned_cacheattr (d, gfn, order, &type) ) + { +case 1 : + *ipat =1 ; +return type !=PAT_TYPE_UC_MINUS ? type :PAT_TYPE_UNCACHABLE ; +case -1 : +return -1 ; + } if ( !iommu_enabled ||(rangeset_is_empty(d->iomem_caps) &&xen/arch/x86/mm/shadow/multi.c +1
-1@@ -601,7 +601,7 @@ _sh_propagate(struct vcpu *v,* 3) if disables snoop control, compute the PAT index with * gMTRR and gPAT. */ -if (hvm_get_mem_pinned_cacheattr (d,gfn_x (target_gfn), &type) ) +if (hvm_get_mem_pinned_cacheattr (d,gfn_x (target_gfn),0 , &type) ) sflags |=pat_type_2_pte_flags (type);else if ( d->arch.hvm_domain.is_in_uc_mode )sflags |= pat_type_2_pte_flags(PAT_TYPE_UNCACHABLE);xen/include/asm-x86/hvm/cacheattr.h +4
-8@@ -1,12 +1,6 @@#ifndef __HVM_CACHEATTR_H__#define __HVM_CACHEATTR_H__ -struct hvm_mem_pinned_cacheattr_range { -struct list_head list; -uint64_t start, end; -uint32_t type; -}; -void hvm_init_cacheattr_region_list (struct domain *d); void hvm_destroy_cacheattr_region_list(@@ -15,11 +9,13 @@ void hvm_destroy_cacheattr_region_list(/* * To see guest_fn is in the pinned range or not, * if yes, return 1, and set type to value in this range -* if no, return 0, and set type to 0 +* if no, return 0, setting type to ~0 +* if ambiguous, return -1, setting type to ~0 (possible only for order > 0) */ -int32_t hvm_get_mem_pinned_cacheattr ( +int hvm_get_mem_pinned_cacheattr (struct domain *d,uint64_t guest_fn, +unsigned int order,uint32_t *type);xen/include/public/domctl.h +1
-0@@ -555,6 +555,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_iopor#define XEN_DOMCTL_MEM_CACHEATTR_WP 5#define XEN_DOMCTL_MEM_CACHEATTR_WB 6#define XEN_DOMCTL_MEM_CACHEATTR_UCM 7 +#define XEN_DOMCTL_DELETE_MEM_CACHEATTR (~(uint32_t)0)struct xen_domctl_pin_mem_cacheattr {uint64_aligned_t start, end;uint32_t type;/* XEN_DOMCTL_MEM_CACHEATTR_* */
PATCH v3 5/5 x86/MSI: drop workaround for insecure Dom0 kernels
In reply to Jan BeulichConsidering that
- the workaround is expensive (iterating through the entire P2M space of a domain),
- the planned elimination of the expensiveness (by propagating the type change step by step to the individual P2M leaves) wouldn't address the IOMMU side of things (as for it to obey to the changed permissions the adjustments must be pushed down immediately through the entire tree)
- the proper solution (PHYSDEVOP_msix_prepare) should by now be implemented by all security conscious Dom0 kernels
remove the workaround, killing eventual guests that would be known to become a security risk instead.
xen/arch/x86/mm/p2m-ept.c +1 -1
@@ -770,7 +770,7 @@ static void ept_change_entry_type_globalreturn ;BUG_ON (p2m_is_grant (ot) ||p2m_is_grant (nt)); -BUG_ON (ot != nt && (ot == p2m_mmio_direct || nt == p2m_mmio_direct)); +BUG_ON (p2m_is_mmio (ot) ||p2m_is_mmio (nt));ept_change_entry_type_page (_mfn (ept_get_asr (ept)),ept_get_wl (ept), ot, nt);
xen/arch/x86/msi.c +14 -24
@@ -825,32 +825,22 @@ static int msix_capability_init(struct p msix->pba.last) )WARN (); -if ( dev->domain ) -p2m_change_entry_type_global (dev->domain, - p2m_mmio_direct, p2m_mmio_direct); -if ( desc && (!dev->domain || !paging_mode_translate (dev->domain)) ) +if ( desc ) { -struct domain *d = dev->domain; +struct domain *currd = current->domain; +struct domain *d = dev->domain ?: currd; -if ( !d ) -for_each_domain (d) -if ( !paging_mode_translate (d) && - (iomem_access_permitted (d, msix->table.first, - msix->table.last) || -iomem_access_permitted (d, msix->pba.first, - msix->pba.last)) ) -break ; -if ( d ) - { -if ( !is_hardware_domain (d) && msix->warned != d->domain_id ) - { - msix->warned = d->domain_id; -printk (XENLOG_ERR -"Potentially insecure use of MSI-X on %04x:%02x:%02x.%u by Dom%d\n" , - seg, bus, slot, func, d->domain_id); - } -/* XXX How to deal with existing mappings? */ - } +if ( !is_hardware_domain (currd) || d != currd ) +printk ("%s use of MSI-X on %04x:%02x:%02x.%u by Dom%d\n" , +is_hardware_domain (currd) + ?XENLOG_WARNING "Potentially insecure" + :XENLOG_ERR "Insecure" , + seg, bus, slot, func, d->domain_id); +if ( !is_hardware_domain (d) && +/* Assume a domain without memory has no mappings yet. */ + (!is_hardware_domain (currd) || d->tot_pages) ) +domain_crash (d); +/* XXX How to deal with existing mappings? */ } }WARN_ON (msix->nr_entries != nr_entries);
x86/MSI: drop workaround for insecure Dom0 kernels
Considering that
- the workaround is expensive (iterating through the entire P2M space of a domain),
- the planned elimination of the expensiveness (by propagating the type change step by step to the individual P2M leaves) wouldn't address the IOMMU side of things (as for it to obey to the changed permissions the adjustments must be pushed down immediately through the entire tree)
- the proper solution (PHYSDEVOP_msix_prepare) should by now be implemented by all security conscious Dom0 kernels
remove the workaround, killing eventual guests that would be known to become a security risk instead.
xen/arch/x86/mm/p2m-ept.c +1 -1
@@ -770,7 +770,7 @@ static void ept_change_entry_type_globalreturn ;BUG_ON (p2m_is_grant (ot) ||p2m_is_grant (nt)); -BUG_ON (ot != nt && (ot == p2m_mmio_direct || nt == p2m_mmio_direct)); +BUG_ON (p2m_is_mmio (ot) ||p2m_is_mmio (nt));ept_change_entry_type_page (_mfn (ept_get_asr (ept)),ept_get_wl (ept), ot, nt);
xen/arch/x86/msi.c +14 -24
@@ -825,32 +825,22 @@ static int msix_capability_init(struct p msix->pba.last) )WARN (); -if ( dev->domain ) -p2m_change_entry_type_global (dev->domain, - p2m_mmio_direct, p2m_mmio_direct); -if ( desc && (!dev->domain || !paging_mode_translate (dev->domain)) ) +if ( desc ) { -struct domain *d = dev->domain; +struct domain *currd = current->domain; +struct domain *d = dev->domain ?: currd; -if ( !d ) -for_each_domain (d) -if ( !paging_mode_translate (d) && - (iomem_access_permitted (d, msix->table.first, - msix->table.last) || -iomem_access_permitted (d, msix->pba.first, - msix->pba.last)) ) -break ; -if ( d ) - { -if ( !is_hardware_domain (d) && msix->warned != d->domain_id ) - { - msix->warned = d->domain_id; -printk (XENLOG_ERR -"Potentially insecure use of MSI-X on %04x:%02x:%02x.%u by Dom%d\n" , - seg, bus, slot, func, d->domain_id); - } -/* XXX How to deal with existing mappings? */ - } +if ( !is_hardware_domain (currd) || d != currd ) +printk ("%s use of MSI-X on %04x:%02x:%02x.%u by Dom%d\n" , +is_hardware_domain (currd) + ?XENLOG_WARNING "Potentially insecure" + :XENLOG_ERR "Insecure" , + seg, bus, slot, func, d->domain_id); +if ( !is_hardware_domain (d) && +/* Assume a domain without memory has no mappings yet. */ + (!is_hardware_domain (currd) || d->tot_pages) ) +domain_crash (d); +/* XXX How to deal with existing mappings? */ } }WARN_ON (msix->nr_entries != nr_entries);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v3 5/5] x86/MSI: drop workaround for insecure Dom0 kernels
Quoted 16 lines
From: Jan Beulich
Sent: Monday, April 07, 2014 6:12 PMConsidering that
- the workaround is expensive (iterating through the entire P2M space of a domain),
- the planned elimination of the expensiveness (by propagating the type change step by step to the individual P2M leaves) wouldn't address the IOMMU side of things (as for it to obey to the changed permissions the adjustments must be pushed down immediately through the entire tree)
- the proper solution (PHYSDEVOP_msix_prepare) should by now be implemented by all security conscious Dom0 kernels
remove the workaround, killing eventual guests that would be known to become a security risk instead.
above looks reasonable to me... but I'm not familiar with original security issue on MSI. Could you give me a link to previous conversation?
Thanks
Kevin
Quoted 72 lines
Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/mm/p2m-ept.c +1
-1@@ -770,7 +770,7 @@ static void ept_change_entry_type_globalreturn ;BUG_ON (p2m_is_grant (ot) ||p2m_is_grant (nt)); -BUG_ON (ot != nt && (ot == p2m_mmio_direct || nt == p2m_mmio_direct)); +BUG_ON (p2m_is_mmio (ot) ||p2m_is_mmio (nt)); ept_change_entry_type_page(_mfn(ept_get_asr(ept)),ept_get_wl(ept), ot, nt);xen/arch/x86/msi.c +3
-10@@ -825,32 +825,22 @@ static int msix_capability_init(struct p msix->pba.last) )WARN (); -if ( dev->domain ) -p2m_change_entry_type_global (dev->domain, - p2m_mmio_direct, p2m_mmio_direct); -if ( desc && (!dev->domain || !paging_mode_translate(dev->domain)) ) +if ( desc ) { -struct domain *d = dev->domain; +struct domain *currd = current->domain; +struct domain *d = dev->domain ?: currd; -if ( !d ) -for_each_domain (d) - if ( !paging_mode_translate(d) && - (iomem_access_permitted(d, msix->table.first, - msix->table.last)||
xen/arch/x86/msi.c +0
-6-iomem_access_permitted (d, msix->pba.first, - msix->pba.last)) ) - break; - if ( d ) - { - if ( !is_hardware_domain(d) && msix->warned !=
d->domain_id)xen/arch/x86/msi.c +0
-4- { - msix->warned = d->domain_id; - printk(XENLOG_ERR - "Potentially insecure use of MSI-Xon %04x:%02x:%02x.%u by Dom%d\n",
xen/arch/x86/msi.c +2
-4- seg, bus, slot, func, d->domain_id); - } -/* XXX How to deal with existing mappings? */ - } +if ( !is_hardware_domain (currd) || d != currd ) + printk("%s use of MSI-X on %04x:%02x:%02x.%u byDom%d\n",
xen/arch/x86/msi.c +6
-0+is_hardware_domain (currd) + ?XENLOG_WARNING "Potentially insecure" + :XENLOG_ERR "Insecure" , + seg, bus, slot, func, d->domain_id); + if ( !is_hardware_domain(d) && +/* Assume a domain without memory has no mappings yet. */
xen/arch/x86/msi.c +3
-0+ (!is_hardware_domain(currd) || d->tot_pages) ) +domain_crash (d); +/* XXX How to deal with existing mappings? */ } }WARN_ON (msix->nr_entries != nr_entries);
Re: [PATCH v3 5/5] x86/MSI: drop workaround for insecure Dom0 kernels
Quoted 18 lines
Quoted 4 lines
Quoted 1 line · On 10.04.14 at 11:02, <[email protected]> wrote:
From: Jan Beulich
Sent: Monday, April 07, 2014 6:12 PMQuoted 13 lines
Considering that
- the workaround is expensive (iterating through the entire P2M space of a domain),
- the planned elimination of the expensiveness (by propagating the type change step by step to the individual P2M leaves) wouldn't address the IOMMU side of things (as for it to obey to the changed permissions the adjustments must be pushed down immediately through the entire tree)
- the proper solution (PHYSDEVOP_msix_prepare) should by now be implemented by all security conscious Dom0 kernels
remove the workaround, killing eventual guests that would be known to become a security risk instead.
Quoted 2 lines
above looks reasonable to me... but I'm not familiar with original security issue on MSI. Could you give me a link to previous conversation?
That'll be hard to locate, because it had been taking ages until we settled on a reasonable strategy and implemented all the pieces here and there. Hence perhaps it's best if you just looked at the respective commits:
http://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=b5aadd4248d64249ed6d1f98659a3b35ca9e91bd
http://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=4245d331e0e75de8d1bddbbb518f3a8ce6d0bb7e
Jan
Tian, Kevin
Acked-byRe: [PATCH v3 5/5] x86/MSI: drop workaround for insecure Dom0 kernels
Quoted 29 lines
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, April 10, 2014 6:26 PMQuoted 18 lines
Quoted 4 lines
Quoted 1 line · On 10.04.14 at 11:02, <[email protected]> wrote:
From: Jan Beulich
Sent: Monday, April 07, 2014 6:12 PMQuoted 13 lines
Considering that
- the workaround is expensive (iterating through the entire P2M space of a domain),
- the planned elimination of the expensiveness (by propagating the type change step by step to the individual P2M leaves) wouldn't address the IOMMU side of things (as for it to obey to the changed permissions the adjustments must be pushed down immediately through the entire tree)
- the proper solution (PHYSDEVOP_msix_prepare) should by now be implemented by all security conscious Dom0 kernels
remove the workaround, killing eventual guests that would be known to become a security risk instead.Quoted 2 lines
above looks reasonable to me... but I'm not familiar with original security issue on MSI. Could you give me a link to previous conversation?
That'll be hard to locate, because it had been taking ages until we settled on a reasonable strategy and implemented all the pieces here and there. Hence perhaps it's best if you just looked at the respective commits:
Quoted 6 lines
http://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=b5aadd4248d64249e
d6d1f98659a3b35ca9e91bd
http://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=4245d331e0e75de8d1bddbbb518f3a8ce6d0bb7eJan
Ping: [PATCH v3 5/5] x86/MSI: drop workaround for insecure Dom0 kernels
In reply to Tian, KevinQuoted 85 lines
Quoted 85 lines
Considering that
- the workaround is expensive (iterating through the entire P2M space of a domain),
- the planned elimination of the expensiveness (by propagating the type change step by step to the individual P2M leaves) wouldn't address the IOMMU side of things (as for it to obey to the changed permissions the adjustments must be pushed down immediately through the entire tree)
- the proper solution (PHYSDEVOP_msix_prepare) should by now be implemented by all security conscious Dom0 kernels
remove the workaround, killing eventual guests that would be known to become a security risk instead.Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/mm/p2m-ept.c +1
-1@@ -770,7 +770,7 @@ static void ept_change_entry_type_globalreturn ;BUG_ON (p2m_is_grant (ot) ||p2m_is_grant (nt)); -BUG_ON (ot != nt && (ot == p2m_mmio_direct || nt == p2m_mmio_direct)); +BUG_ON (p2m_is_mmio (ot) ||p2m_is_mmio (nt)); ept_change_entry_type_page(_mfn(ept_get_asr(ept)),ept_get_wl(ept), ot, nt);xen/arch/x86/msi.c +3
-10@@ -825,32 +825,22 @@ static int msix_capability_init(struct p msix->pba.last) )WARN (); -if ( dev->domain ) -p2m_change_entry_type_global (dev->domain, - p2m_mmio_direct, p2m_mmio_direct); -if ( desc && (!dev->domain || !paging_mode_translate(dev->domain)) ) +if ( desc ) { -struct domain *d = dev->domain; +struct domain *currd = current->domain; +struct domain *d = dev->domain ?: currd; -if ( !d ) -for_each_domain (d) - if ( !paging_mode_translate(d) && - (iomem_access_permitted(d, msix->table.first, - msix->table.last)||
xen/arch/x86/msi.c +0
-6-iomem_access_permitted (d, msix->pba.first, - msix->pba.last)) ) - break; - if ( d ) - { - if ( !is_hardware_domain(d) && msix->warned !=
d->domain_id)xen/arch/x86/msi.c +0
-4- { - msix->warned = d->domain_id; - printk(XENLOG_ERR - "Potentially insecure use of MSI-Xon %04x:%02x:%02x.%u by Dom%d\n",
xen/arch/x86/msi.c +2
-4- seg, bus, slot, func, d->domain_id); - } -/* XXX How to deal with existing mappings? */ - } +if ( !is_hardware_domain (currd) || d != currd ) + printk("%s use of MSI-X on %04x:%02x:%02x.%u byDom%d\n",
xen/arch/x86/msi.c +6
-0+is_hardware_domain (currd) + ?XENLOG_WARNING "Potentially insecure" + :XENLOG_ERR "Insecure" , + seg, bus, slot, func, d->domain_id); + if ( !is_hardware_domain(d) && +/* Assume a domain without memory has no mappings yet. */
xen/arch/x86/msi.c +3
-0+ (!is_hardware_domain(currd) || d->tot_pages) ) +domain_crash (d); +/* XXX How to deal with existing mappings? */ } }WARN_ON (msix->nr_entries != nr_entries);