Nested VMX: Add virtual EPT & VPID support to L1 VMM
v3 cover Nested VMX: Add virtual EPT & VPID support to L1 VMM
From: Zhang Xiantao <[email protected]>
With virtual EPT support, L1 hyerpvisor can use EPT hardware for L2 guest's memory virtualization. In this way, L2 guest's performance can be improved sharply.
According to our testing, some benchmarks can show > 5x performance gain.
Changes from v1:
Update the patches according to Tim's comments.
1. Patch 03: Enhance the virtual EPT's walker logic.
2. Patch 04: Add a new field in struct p2m_domain, and use it to store
EPT-specific data. For host p2m, it saves L1 VMM's EPT data,
and for nested p2m, it saves nested EPT's data 3. Patch 07: strictly check host's p2m access type.
4. Other patches: some whitespace mangling fixes.
Changes form v2:
Addressed comments from Jan and Jun:
1. Add Acked-by message for reviewed patches by Tim.
2. Fixed one whilespace mangling issue in PATCH 08
3. Add some comments to describe the meaning of the return value of hvm_hap_nested_page_fault in PATCH 05.
4. Add the logic for handling default case of two switch statements.
Zhang Xiantao (10):
nestedhap: Change hostcr3 and p2m->cr3 to meaningful words
nestedhap: Change nested p2m's walker to vendor-specific
nested_ept: Implement guest ept's walker
EPT: Make ept data structure or operations neutral
nEPT: Try to enable EPT paging for L2 guest.
nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
nEPT: Use minimal permission for nested p2m.
nEPT: handle invept instruction from L1 VMM
nVMX: virutalize VPID capability to nested VMM.
nEPT: expost EPT & VPID capablities to L1 VMM
xen/arch/x86/hvm/hvm.c | 7 +- xen/arch/x86/hvm/svm/nestedsvm.c | 31 ++++ xen/arch/x86/hvm/svm/svm.c | 3 +- xen/arch/x86/hvm/vmx/vmcs.c | 9 +- xen/arch/x86/hvm/vmx/vmx.c | 91 ++++------ xen/arch/x86/hvm/vmx/vvmx.c | 215 ++++++++++++++++++++++-- xen/arch/x86/mm/guest_walk.c | 16 +- xen/arch/x86/mm/hap/Makefile | 1 + xen/arch/x86/mm/hap/nested_ept.c | 286 +++++++++++++++++++++++++++++++ xen/arch/x86/mm/hap/nested_hap.c | 95 ++++++----- xen/arch/x86/mm/mm-locks.h | 2 +- xen/arch/x86/mm/p2m-ept.c | 104 +++++++++--- xen/arch/x86/mm/p2m.c | 49 +++--- xen/arch/x86/mm/shadow/multi.c | 2 +- xen/include/asm-x86/guest_pt.h | 8 + xen/include/asm-x86/hvm/hvm.h | 9 +- xen/include/asm-x86/hvm/nestedhvm.h | 2 + xen/include/asm-x86/hvm/svm/nestedsvm.h | 3 + xen/include/asm-x86/hvm/vmx/vmcs.h | 24 ++-- xen/include/asm-x86/hvm/vmx/vmx.h | 38 ++++- xen/include/asm-x86/hvm/vmx/vvmx.h | 30 +++- xen/include/asm-x86/p2m.h | 20 ++- 22 files changed, 852 insertions(+), 193 deletions(-) create mode 100644 xen/arch/x86/mm/hap/nested_ept.c
PATCH v3 1/10 nestedhap: Change hostcr3 and p2m->cr3 to meaningful words
From: Zhang Xiantao <[email protected]>
VMX doesn't have the concept about host cr3 for nested p2m, and only SVM has, so change it to netural words.
--- xen/arch/x86/hvm/hvm.c | 6 +++--- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/hvm/vmx/vmx.c | 2 +- xen/arch/x86/hvm/vmx/vvmx.c | 2 +- xen/arch/x86/mm/hap/nested_hap.c | 15 ++++++++------- xen/arch/x86/mm/mm-locks.h | 2 +- xen/arch/x86/mm/p2m.c | 26 +++++++++++++------------- xen/include/asm-x86/hvm/hvm.h | 4 ++-- xen/include/asm-x86/hvm/vmx/vvmx.h | 2 +- xen/include/asm-x86/p2m.h | 16 ++++++++-------- 10 files changed, 39 insertions(+), 38 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -3
@@ -4536,10 +4536,10 @@ uint64_t nhvm_vcpu_guestcr3(struct vcpu *v)return -EOPNOTSUPP ; } -uint64_t nhvm_vcpu_hostcr3 (struct vcpu *v) +uint64_t nhvm_vcpu_p2m_base (struct vcpu *v) { -if (hvm_funcs.nhvm_vcpu_hostcr3) -return hvm_funcs.nhvm_vcpu_hostcr3 (v); +if (hvm_funcs.nhvm_vcpu_p2m_base) +return hvm_funcs.nhvm_vcpu_p2m_base (v);return -EOPNOTSUPP ; }
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -2003,7 +2003,7 @@ static struct hvm_function_table __read_mostly svm_function_table = { .nhvm_vcpu_vmexit = nsvm_vcpu_vmexit_inject, .nhvm_vcpu_vmexit_trap = nsvm_vcpu_vmexit_trap, .nhvm_vcpu_guestcr3 = nsvm_vcpu_guestcr3, - .nhvm_vcpu_hostcr3 = nsvm_vcpu_hostcr3, + .nhvm_vcpu_p2m_base = nsvm_vcpu_hostcr3, .nhvm_vcpu_asid = nsvm_vcpu_asid, .nhvm_vmcx_guest_intercepts_trap = nsvm_vmcb_guest_intercepts_trap, .nhvm_vmcx_hap_enabled = nsvm_vmcb_hap_enabled,
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -1504,7 +1504,7 @@ static struct hvm_function_table __read_mostly vmx_function_table = { .nhvm_vcpu_destroy = nvmx_vcpu_destroy, .nhvm_vcpu_reset = nvmx_vcpu_reset, .nhvm_vcpu_guestcr3 = nvmx_vcpu_guestcr3, - .nhvm_vcpu_hostcr3 = nvmx_vcpu_hostcr3, + .nhvm_vcpu_p2m_base = nvmx_vcpu_eptp_base, .nhvm_vcpu_asid = nvmx_vcpu_asid, .nhvm_vmcx_guest_intercepts_trap = nvmx_intercepts_exception, .nhvm_vcpu_vmexit_trap = nvmx_vmexit_trap,
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
@@ -94,7 +94,7 @@ uint64_t nvmx_vcpu_guestcr3(struct vcpu *v)return 0 ; } -uint64_t nvmx_vcpu_hostcr3 (struct vcpu *v) +uint64_t nvmx_vcpu_eptp_base (struct vcpu *v) {/* TODO */ ASSERT (0 );
xen/arch/x86/mm/hap/nested_hap.c +8 -7
@@ -48,9 +48,10 @@* 1. If #NPF is from L1 guest, then we crash the guest VM (same as old * code) * 2. If #NPF is from L2 guest, then we continue from (3) -* 3. Get h_cr3 from L1 guest. Map h_cr3 into L0 hypervisor address space. -* 4. Walk the h_cr3 page table -* 5. - if not present, then we inject #NPF back to L1 guest and +* 3. Get np2m base from L1 guest. Map np2m base into L0 hypervisor address space. +* 4. Walk the np2m's page table +* 5. - if not present or permission check failure, then we inject #NPF back to +* L1 guest and * re-launch L1 guest (L1 guest will either treat this #NPF as MMIO, * or fix its p2m table for L2 guest) * 6. - if present, then we will get the a new translated value L1-GPA
@@ -89,7 +90,7 @@ nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn,if (old_flags & _PAGE_PRESENT)flush_tlb_mask (p2m->dirty_cpumask); - +paging_unlock (d); }
@@ -110,7 +111,7 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,/* If this p2m table has been flushed or recycled under our feet, * leave it alone. We'll pick up the right one as we try to * vmenter the guest. */ -if ( p2m->cr3 ==nhvm_vcpu_hostcr3 (v) ) +if ( p2m->np2m_base ==nhvm_vcpu_p2m_base (v) ) {unsigned long gfn, mask;mfn_t mfn;
@@ -186,7 +187,7 @@ nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,uint32_t pfec;unsigned long nested_cr3, gfn; - nested_cr3 =nhvm_vcpu_hostcr3 (v); + nested_cr3 =nhvm_vcpu_p2m_base (v); pfec = PFEC_user_mode | PFEC_page_present;if (access_w)
@@ -221,7 +222,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,p2m_type_t p2mt_10; p2m =p2m_get_hostp2m (d);/* L0 p2m */ - nested_p2m =p2m_get_nestedp2m (v,nhvm_vcpu_hostcr3 (v)); + nested_p2m =p2m_get_nestedp2m (v,nhvm_vcpu_p2m_base (v));/* walk the L1 P2M table */ rv = nestedhap_walk_L1_p2m(v, *L2_gpa, &L1_gpa, &page_order_21,
xen/arch/x86/mm/mm-locks.h +1 -1
@@ -249,7 +249,7 @@ declare_mm_order_constraint(per_page_sharing)* A per-domain lock that protects the mapping from nested-CR3 to * nested-p2m. In particular it covers: * - the array of nested-p2m tables, and all LRU activity therein; and -* - setting the "cr3" field of any p2m table to a non-CR3_EADDR value. +* - setting the "cr3" field of any p2m table to a non-P2M_BASE_EAADR value. * (i.e. assigning a p2m table to be the shadow of that cr3 */ /* PoD lock (per-p2m-table)
xen/arch/x86/mm/p2m.c +13 -13
@@ -69,7 +69,7 @@ static void p2m_initialise(struct domain *d, struct p2m_domain *p2m) p2m->domain = d; p2m->default_access = p2m_access_rwx; - p2m->cr3 =CR3_EADDR ; + p2m->np2m_base =P2M_BASE_EADDR ;if (hap_enabled (d) && cpu_has_vmx )ept_p2m_init (p2m);
@@ -1433,7 +1433,7 @@ p2m_flush_table(struct p2m_domain *p2m)ASSERT (page_list_empty (&p2m->pod.single));/* This is no longer a valid nested p2m for any address space */ - p2m->cr3 =CR3_EADDR ; + p2m->np2m_base =P2M_BASE_EADDR ;/* Zap the top level of the trie */ top =mfn_to_page (pagetable_get_mfn (p2m_get_pagetable (p2m)));
@@ -1471,7 +1471,7 @@ p2m_flush_nestedp2m(struct domain *d) }struct p2m_domain * -p2m_get_nestedp2m (struct vcpu *v,uint64_t cr3) +p2m_get_nestedp2m (struct vcpu *v,uint64_t np2m_base) {/* Use volatile to prevent gcc to cache nv->nv_p2m in a cpu register as * this may change within the loop by an other (v)cpu.
@@ -1480,8 +1480,8 @@ p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)struct domain *d;struct p2m_domain *p2m; -/* Mask out low bits; this avoids collisions with CR3_EADDR */ - cr3 &= ~(0xfffull ); +/* Mask out low bits; this avoids collisions with P2M_BASE_EADDR */ + np2m_base &= ~(0xfffull );if (nv->nv_flushp2m && nv->nv_p2m) { nv->nv_p2m =NULL ;
@@ -1493,14 +1493,14 @@ p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)if ( p2m ) {p2m_lock (p2m); -if ( p2m->cr3 == cr3 || p2m->cr3 ==CR3_EADDR ) +if ( p2m->np2m_base == np2m_base || p2m->np2m_base ==P2M_BASE_EADDR ) { nv->nv_flushp2m =0 ;p2m_getlru_nestedp2m (d, p2m); nv->nv_p2m = p2m; -if (p2m->cr3 ==CR3_EADDR ) +if (p2m->np2m_base ==P2M_BASE_EADDR )hvm_asid_flush_vcpu (v); - p2m->cr3 = cr3; + p2m->np2m_base = np2m_base;cpumask_set_cpu (v->processor, p2m->dirty_cpumask);p2m_unlock (p2m);nestedp2m_unlock (d);
@@ -1515,7 +1515,7 @@ p2m_get_nestedp2m(struct vcpu *v, uint64_t cr3)p2m_flush_table (p2m);p2m_lock (p2m); nv->nv_p2m = p2m; - p2m->cr3 = cr3; + p2m->np2m_base = np2m_base; nv->nv_flushp2m =0 ;hvm_asid_flush_vcpu (v);cpumask_set_cpu (v->processor, p2m->dirty_cpumask);
@@ -1531,7 +1531,7 @@ p2m_get_p2m(struct vcpu *v)if (!nestedhvm_is_n2 (v))return p2m_get_hostp2m (v->domain); -return p2m_get_nestedp2m (v,nhvm_vcpu_hostcr3 (v)); +return p2m_get_nestedp2m (v,nhvm_vcpu_p2m_base (v)); } unsigned long paging_gva_to_gfn(struct vcpu *v,
@@ -1549,15 +1549,15 @@ unsigned long paging_gva_to_gfn(struct vcpu *v,struct p2m_domain *p2m;const struct paging_mode *mode;uint32_t pfec_21 = *pfec; -uint64_t ncr3 =nhvm_vcpu_hostcr3 (v); +uint64_t np2m_base =nhvm_vcpu_p2m_base (v);/* translate l2 guest va into l2 guest gfn */ - p2m =p2m_get_nestedp2m (v, ncr3); + p2m =p2m_get_nestedp2m (v, np2m_base); mode =paging_get_nestedmode (v); gfn = mode->gva_to_gfn (v, p2m, va, pfec);/* translate l2 guest gfn into l1 guest gfn */ -return hostmode->p2m_ga_to_gfn (v, hostp2m, ncr3, +return hostmode->p2m_ga_to_gfn (v, hostp2m, np2m_base, gfn <<PAGE_SHIFT , &pfec_21,NULL ); }
xen/include/asm-x86/hvm/hvm.h +2 -2
@@ -170,7 +170,7 @@ struct hvm_function_table { uint64_t exitcode);int (*nhvm_vcpu_vmexit_trap)(struct vcpu *v,struct hvm_trap *trap);uint64_t (*nhvm_vcpu_guestcr3)(struct vcpu *v); -uint64_t (*nhvm_vcpu_hostcr3)(struct vcpu *v); +uint64_t (*nhvm_vcpu_p2m_base)(struct vcpu *v);uint32_t (*nhvm_vcpu_asid)(struct vcpu *v);int (*nhvm_vmcx_guest_intercepts_trap)(struct vcpu *v,unsigned int trapnr,int errcode);
@@ -475,7 +475,7 @@ uint64_t nhvm_vcpu_guestcr3(struct vcpu *v);/* returns l1 guest's cr3 that points to the page table used to * translate l2 guest physical address to l1 guest physical address. */ -uint64_t nhvm_vcpu_hostcr3 (struct vcpu *v); +uint64_t nhvm_vcpu_p2m_base (struct vcpu *v);/* returns the asid number l1 guest wants to use to run the l2 guest */ uint32_t nhvm_vcpu_asid (struct vcpu *v);
xen/include/asm-x86/hvm/vmx/vvmx.h +1 -1
@@ -99,7 +99,7 @@ int nvmx_vcpu_initialise(struct vcpu *v);void nvmx_vcpu_destroy (struct vcpu *v);int nvmx_vcpu_reset (struct vcpu *v);uint64_t nvmx_vcpu_guestcr3 (struct vcpu *v); -uint64_t nvmx_vcpu_hostcr3 (struct vcpu *v); +uint64_t nvmx_vcpu_eptp_base (struct vcpu *v);uint32_t nvmx_vcpu_asid (struct vcpu *v);enum hvm_intblk nvmx_intr_blocked (struct vcpu *v); int nvmx_intercepts_exception(struct vcpu *v,
xen/include/asm-x86/p2m.h +8 -8
@@ -197,17 +197,17 @@ struct p2m_domain {struct domain *domain;/* back pointer to domain */ -/* Nested p2ms only: nested-CR3 value that this p2m shadows. -* This can be cleared to CR3_EADDR under the per-p2m lock but +/* Nested p2ms only: nested p2m base value that this p2m shadows. +* This can be cleared to P2M_BASE_EADDR under the per-p2m lock but * needs both the per-p2m lock and the per-domain nestedp2m lock * to set it to any other value. */ -#define CR3_EADDR (~0ULL) -uint64_t cr3; +#define P2M_BASE_EADDR (~0ULL) +uint64_t np2m_base;/* Nested p2ms: linked list of n2pms allocated to this domain. * The host p2m hasolds the head of the list and the np2ms are * threaded on in LRU order. */ -struct list_head np2m_list; +struct list_head np2m_list;/* Host p2m: when this flag is set, don't flush all the nested-p2m
@@ -282,11 +282,11 @@ struct p2m_domain {/* get host p2m table */ #define p2m_get_hostp2m (d) ((d)->arch.p2m) -/* Get p2m table (re)usable for specified cr3. +/* Get p2m table (re)usable for specified np2m base. * Automatically destroys and re-initializes a p2m if none found. -* If cr3 == 0 then v->arch.hvm_vcpu.guest_cr[3] is used. +* If np2m_base == 0 then v->arch.hvm_vcpu.guest_cr[3] is used. */ -struct p2m_domain *p2m_get_nestedp2m (struct vcpu *v,uint64_t cr3); +struct p2m_domain *p2m_get_nestedp2m (struct vcpu *v,uint64_t np2m_base);/* If vcpu is in host mode then behaviour matches p2m_get_hostp2m(). * If vcpu is in guest mode then behaviour matches p2m_get_nestedp2m().
Signature · 1 line
1.7.1
Re: [PATCH v3 01/10] nestedhap: Change hostcr3 and p2m->cr3 to meaningful words
Quoted 7 lines · Xiantao Zhang
From: Zhang Xiantao <[email protected]>
VMX doesn't have the concept about host cr3 for nested p2m, and only SVM has, so change it to netural words.
Signed-off-by Zhang Xiantao <[email protected]>Acded-by Tim Deegan <[email protected]>
Ac_k_ed-by. :)
PATCH v3 2/10 nestedhap: Change nested p2m's walker to vendor-specific
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
EPT and NPT adopts differnt formats for each-level entry, so change the walker functions to vendor-specific.
--- xen/arch/x86/hvm/svm/nestedsvm.c | 31 +++++++++++++++++++++ xen/arch/x86/hvm/svm/svm.c | 1 + xen/arch/x86/hvm/vmx/vmx.c | 3 +- xen/arch/x86/hvm/vmx/vvmx.c | 13 +++++++++ xen/arch/x86/mm/hap/nested_hap.c | 46 +++++++++++-------------------- xen/include/asm-x86/hvm/hvm.h | 5 +++ xen/include/asm-x86/hvm/svm/nestedsvm.h | 3 ++ xen/include/asm-x86/hvm/vmx/vvmx.h | 5 +++ 8 files changed, 76 insertions(+), 31 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +31 -0
@@ -1171,6 +1171,37 @@ nsvm_vmcb_hap_enabled(struct vcpu *v)return vcpu_nestedsvm (v).ns_hap_enabled; } +/* This function uses L2_gpa to walk the P2M page table in L1. If the +* walk is successful, the translated value is returned in +* L1_gpa. The result value tells what to do next. +*/ +int +nsvm_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, +unsigned int *page_order, +bool_t access_r,bool_t access_w,bool_t access_x) +{ +uint32_t pfec; +unsigned long nested_cr3, gfn; + + nested_cr3 =nhvm_vcpu_p2m_base (v); + + pfec = PFEC_user_mode | PFEC_page_present; +if (access_w) + pfec |= PFEC_write_access; +if (access_x) + pfec |= PFEC_insn_fetch; + +/* Walk the guest-supplied NPT table, just as if it were a pagetable */ + gfn =paging_ga_to_gfn_cr3 (v, nested_cr3, L2_gpa, &pfec, page_order); + +if ( gfn ==INVALID_GFN ) +return NESTEDHVM_PAGEFAULT_INJECT ; + + *L1_gpa = (gfn <<PAGE_SHIFT ) + (L2_gpa & ~PAGE_MASK ); +return NESTEDHVM_PAGEFAULT_DONE ; +} + +enum hvm_intblk nsvm_intr_blocked (struct vcpu *v) {struct nestedsvm *svm = &vcpu_nestedsvm (v);
xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -2008,6 +2008,7 @@ static struct hvm_function_table __read_mostly svm_function_table = { .nhvm_vmcx_guest_intercepts_trap = nsvm_vmcb_guest_intercepts_trap, .nhvm_vmcx_hap_enabled = nsvm_vmcb_hap_enabled, .nhvm_intr_blocked = nsvm_intr_blocked, + .nhvm_hap_walk_L1_p2m = nsvm_hap_walk_L1_p2m, };void svm_vmexit_handler (struct cpu_user_regs *regs)
xen/arch/x86/hvm/vmx/vmx.c +2 -1
@@ -1511,7 +1511,8 @@ static struct hvm_function_table __read_mostly vmx_function_table = { .nhvm_intr_blocked = nvmx_intr_blocked, .nhvm_domain_relinquish_resources = nvmx_domain_relinquish_resources, .update_eoi_exit_bitmap = vmx_update_eoi_exit_bitmap, - .virtual_intr_delivery_enabled = vmx_virtual_intr_delivery_enabled + .virtual_intr_delivery_enabled = vmx_virtual_intr_delivery_enabled, + .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m, };struct hvm_function_table *__init start_vmx (void )
xen/arch/x86/hvm/vmx/vvmx.c +13 -0
@@ -1445,6 +1445,19 @@ int nvmx_msr_write_intercept(unsigned int msr, u64 msr_content)return 1 ; } +/* This function uses L2_gpa to walk the P2M page table in L1. If the +* walk is successful, the translated value is returned in +* L1_gpa. The result value tells what to do next. +*/ +int +nvmx_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, +unsigned int *page_order, +bool_t access_r,bool_t access_w,bool_t access_x) +{ +/*TODO:*/ +return 0 ; +} +void nvmx_idtv_handling (void ) {struct vcpu *v = current;
xen/arch/x86/mm/hap/nested_hap.c +16 -30
58 lines not on this page · raw message
xen/include/asm-x86/hvm/hvm.h +5 -0
@@ -183,6 +183,11 @@ struct hvm_function_table {/* Virtual interrupt delivery */ void (*update_eoi_exit_bitmap)(struct vcpu *v,u8 vector,u8 trig);int (*virtual_intr_delivery_enabled)(void ); + +/*Walk nested p2m */ +int (*nhvm_hap_walk_L1_p2m)(struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, +unsigned int *page_order, +bool_t access_r,bool_t access_w,bool_t access_x); };extern struct hvm_function_table hvm_funcs;
xen/include/asm-x86/hvm/svm/nestedsvm.h +3 -0
@@ -133,6 +133,9 @@ int nsvm_wrmsr(struct vcpu *v, unsigned int msr, uint64_t msr_content);void svm_vmexit_do_clgi (struct cpu_user_regs *regs,struct vcpu *v);void svm_vmexit_do_stgi (struct cpu_user_regs *regs,struct vcpu *v);bool_t nestedsvm_gif_isset (struct vcpu *v); +int nsvm_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, +unsigned int *page_order, +bool_t access_r,bool_t access_w,bool_t access_x);#define NSVM_INTR_NOTHANDLED 3#define NSVM_INTR_NOTINTERCEPTED 2
xen/include/asm-x86/hvm/vmx/vvmx.h +5 -0
@@ -108,6 +108,11 @@ void nvmx_domain_relinquish_resources(struct domain *d);int nvmx_handle_vmxon (struct cpu_user_regs *regs);int nvmx_handle_vmxoff (struct cpu_user_regs *regs); + +int +nvmx_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, +unsigned int *page_order, +bool_t access_r,bool_t access_w,bool_t access_x);/* * Virtual VMCS layout *
Signature · 1 line
1.7.1
PATCH v3 3/10 nested_ept: Implement guest ept's walker
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Implment guest EPT PT walker, some logic is based on shadow's ia32e PT walker. During the PT walking, if the target pages are not in memory, use RETRY mechanism and get a chance to let the target page back.
--- xen/arch/x86/hvm/hvm.c | 1 + xen/arch/x86/hvm/vmx/vvmx.c | 44 +++++- xen/arch/x86/mm/guest_walk.c | 16 ++- xen/arch/x86/mm/hap/Makefile | 1 + xen/arch/x86/mm/hap/nested_ept.c | 280 +++++++++++++++++++++++++++++++++++ xen/arch/x86/mm/hap/nested_hap.c | 2 +- xen/arch/x86/mm/shadow/multi.c | 2 +- xen/include/asm-x86/guest_pt.h | 8 + xen/include/asm-x86/hvm/nestedhvm.h | 2 + xen/include/asm-x86/hvm/vmx/vmcs.h | 1 + xen/include/asm-x86/hvm/vmx/vmx.h | 28 ++++ xen/include/asm-x86/hvm/vmx/vvmx.h | 15 ++ 12 files changed, 390 insertions(+), 10 deletions(-) create mode 100644 xen/arch/x86/mm/hap/nested_ept.c
xen/arch/x86/hvm/hvm.c +1 -0
@@ -1324,6 +1324,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, access_r, access_w, access_x);switch (rv) {case NESTEDHVM_PAGEFAULT_DONE : +case NESTEDHVM_PAGEFAULT_RETRY :return 1 ;case NESTEDHVM_PAGEFAULT_L1_ERROR :/* An error occured while translating gpa from
xen/arch/x86/hvm/vmx/vvmx.c +42 -2
59 lines not on this page · raw message
xen/arch/x86/mm/guest_walk.c +10 -6
@@ -88,18 +88,19 @@ static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty)/* If the map is non-NULL, we leave this function having * acquired an extra ref on mfn_to_page(*mfn) */ -static inline void *map_domain_gfn (struct p2m_domain *p2m, -gfn_t gfn, +void *map_domain_gfn (struct p2m_domain *p2m, +gfn_t gfn,mfn_t *mfn,p2m_type_t *p2mt, -uint32_t *rc) +p2m_query_t q, +uint32_t *rc) {struct page_info *page;void *map;/* Translate the gfn, unsharing if shared */ page =get_page_from_gfn_p2m (p2m->domain, p2m,gfn_x (gfn), p2mt,NULL , -P2M_ALLOC |P2M_UNSHARE ); + q);if (p2m_is_paging (*p2mt) ) {ASSERT (!p2m_is_nestedp2m (p2m));
@@ -128,7 +129,6 @@ static inline void *map_domain_gfn(struct p2m_domain *p2m,return map; } -/* Walk the guest pagetables, after the manner of a hardware walker. */ /* Because the walk is essentially random, it can cause a deadlock * warning in the p2m locking code. Highly unlikely this is an actual
@@ -149,6 +149,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,uint32_t gflags, mflags, iflags, rc =0 ;int smep;bool_t pse1G =0 , pse2M =0 ; +p2m_query_t qt =P2M_ALLOC |P2M_UNSHARE ;perfc_incr (guest_walk);memset (gw,0 ,sizeof (*gw));
@@ -188,7 +189,8 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, l3p =map_domain_gfn (p2m,guest_l4e_get_gfn (gw->l4e), &gw->l3mfn, - &p2mt, + &p2mt, + qt, &rc);if (l3p ==NULL ) goto out;
@@ -249,6 +251,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,guest_l3e_get_gfn (gw->l3e), &gw->l2mfn, &p2mt, + qt, &rc);if (l2p ==NULL ) goto out;
@@ -322,6 +325,7 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,guest_l2e_get_gfn (gw->l2e), &gw->l1mfn, &p2mt, + qt, &rc);if (l1p ==NULL ) goto out;
xen/arch/x86/mm/hap/Makefile +1 -0
@@ -3,6 +3,7 @@ obj-y += guest_walk_2level.o obj-y += guest_walk_3level.o obj-$(x86_64) += guest_walk_4level.o obj-y += nested_hap.o +obj-y += nested_ept.o guest_walk_%level.o: guest_walk.c Makefile $(CC) $(CFLAGS) -DGUEST_PAGING_LEVELS=$* -c $< -o $@
xen/arch/x86/mm/hap/nested_ept.c new file +280 -0
280 lines not on this page · raw message
xen/arch/x86/mm/hap/nested_hap.c +1 -1
@@ -217,7 +217,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,/* let caller to handle these two cases */ switch (rv) {case NESTEDHVM_PAGEFAULT_INJECT : -return rv; +case NESTEDHVM_PAGEFAULT_RETRY :case NESTEDHVM_PAGEFAULT_L1_ERROR :return rv;case NESTEDHVM_PAGEFAULT_DONE :
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -4582,7 +4582,7 @@ static mfn_t emulate_gva_to_mfn(struct vcpu *v,/* Translate the GFN to an MFN */ ASSERT (!paging_locked_by_me (v->domain)); mfn =get_gfn (v->domain,_gfn (gfn), &p2mt); - +if (p2m_is_readonly (p2mt) ) {put_gfn (v->domain, gfn);
xen/include/asm-x86/guest_pt.h +8 -0
@@ -315,6 +315,14 @@ guest_walk_to_page_order(walk_t *gw)#define GPT_RENAME2 (_n, _l) _n ## _ ## _l ## _levels#define GPT_RENAME (_n, _l) GPT_RENAME2(_n, _l)#define guest_walk_tables GPT_RENAME(guest_walk_tables, GUEST_PAGING_LEVELS) +#define map_domain_gfn GPT_RENAME(map_domain_gfn, GUEST_PAGING_LEVELS) + +extern void *map_domain_gfn (struct p2m_domain *p2m, +gfn_t gfn, +mfn_t *mfn, +p2m_type_t *p2mt, +p2m_query_t q, +uint32_t *rc); extern uint32_t guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, unsigned long va,
xen/include/asm-x86/hvm/nestedhvm.h +2 -0
@@ -47,11 +47,13 @@ bool_t nestedhvm_vcpu_in_guestmode(struct vcpu *v);vcpu_nestedhvm (v).nv_guestmode =0 /* Nested paging */ +#define NESTEDHVM_PAGEFAULT_UNHANDLED -1#define NESTEDHVM_PAGEFAULT_DONE 0#define NESTEDHVM_PAGEFAULT_INJECT 1#define NESTEDHVM_PAGEFAULT_L1_ERROR 2#define NESTEDHVM_PAGEFAULT_L0_ERROR 3#define NESTEDHVM_PAGEFAULT_MMIO 4 +#define NESTEDHVM_PAGEFAULT_RETRY 5int nestedhvm_hap_nested_page_fault (struct vcpu *v,paddr_t *L2_gpa,bool_t access_r,bool_t access_w,bool_t access_x);
xen/include/asm-x86/hvm/vmx/vmcs.h +1 -0
@@ -194,6 +194,7 @@ extern u32 vmx_secondary_exec_control;extern bool_t cpu_has_vmx_ins_outs_instr_info; +#define VMX_EPT_EXEC_ONLY_SUPPORTED 0x00000001#define VMX_EPT_WALK_LENGTH_4_SUPPORTED 0x00000040#define VMX_EPT_MEMORY_TYPE_UC 0x00000100#define VMX_EPT_MEMORY_TYPE_WB 0x00004000
xen/include/asm-x86/hvm/vmx/vmx.h +28 -0
@@ -51,6 +51,11 @@ typedef union {u64 epte; } ept_entry_t; +typedef struct { +/*use lxe[0] to save result */ +ept_entry_t lxe[5 ]; +}ept_walk_t ; +#define EPT_TABLE_ORDER 9#define EPTE_SUPER_PAGE_MASK 0x80#define EPTE_MFN_MASK 0xffffffffff000ULL
@@ -60,6 +65,28 @@ typedef union {#define EPTE_AVAIL1_SHIFT 8#define EPTE_EMT_SHIFT 3#define EPTE_IGMT_SHIFT 6 +#define EPTE_RWX_MASK 0x7 +#define EPTE_FLAG_MASK 0x7f + +#define EPT_EMT_UC 0 +#define EPT_EMT_WC 1 +#define EPT_EMT_RSV0 2 +#define EPT_EMT_RSV1 3 +#define EPT_EMT_WT 4 +#define EPT_EMT_WP 5 +#define EPT_EMT_WB 6 +#define EPT_EMT_RSV2 7 + +typedef enum { + ept_access_n =0 ,/* No access permissions allowed */ + ept_access_r =1 , + ept_access_w =2 , + ept_access_rw =3 , + ept_access_x =4 , + ept_access_rx =5 , + ept_access_wx =6 , + ept_access_all =7 , +}ept_access_t ;void vmx_asm_vmexit_handler (struct cpu_user_regs );void vmx_asm_do_vmentry (void );
@@ -419,6 +446,7 @@ void update_guest_eip(void);#define _EPT_GLA_FAULT 8#define EPT_GLA_FAULT (1UL<<_EPT_GLA_FAULT) +#define EPT_L4_PAGETABLE_SHIFT 39#define EPT_PAGETABLE_ENTRIES 512#endif /* __ASM_X86_HVM_VMX_VMX_H__ */
xen/include/asm-x86/hvm/vmx/vvmx.h +15 -0
@@ -32,6 +32,10 @@ struct nestedvmx {unsigned long intr_info;u32 error_code; } intr; +struct { +uint32_t exit_reason; +uint32_t exit_qual; + } ept_exit; };#define vcpu_2_nvmx (v) (vcpu_nestedhvm(v).u.nvmx)
@@ -109,6 +113,13 @@ void nvmx_domain_relinquish_resources(struct domain *d);int nvmx_handle_vmxon (struct cpu_user_regs *regs);int nvmx_handle_vmxoff (struct cpu_user_regs *regs); +#define EPT_TRANSLATE_UNSUPPORTED -1 +#define EPT_TRANSLATE_SUCCEED 0 +#define EPT_TRANSLATE_VIOLATION 1 +#define EPT_TRANSLATE_ERR_PAGE 2 +#define EPT_TRANSLATE_MISCONFIG 3 +#define EPT_TRANSLATE_RETRY 4 + int nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa, unsigned int *page_order,
@@ -192,5 +203,9 @@ u64 nvmx_get_tsc_offset(struct vcpu *v);int nvmx_n2_vmexit_handler (struct cpu_user_regs *regs,unsigned int exit_reason); +int nept_translate_l2ga (struct vcpu *v,paddr_t l2ga, +unsigned int *page_order,uint32_t rwx_acc, +unsigned long *l1gfn,uint64_t *exit_qual, +uint32_t *exit_reason);#endif /* __ASM_X86_HVM_VVMX_H__ */
Signature · 1 line
1.7.1
Re: [PATCH v3 03/10] nested_ept: Implement guest ept's walker
Quoted 8 lines · Xiantao Zhang
From: Zhang Xiantao <[email protected]>
Implment guest EPT PT walker, some logic is based on shadow's ia32e PT walker. During the PT walking, if the target pages are not in memory, use RETRY mechanism and get a chance to let the target page back.
Signed-off-by Zhang Xiantao <[email protected]>
This is much nicer than v1, thanks. I have some comments below, and the whole thing needs to be checked for whitespace mangling.
xen/arch/x86/mm/hap/nested_ept.c line 105
Quoted 9 lines
xen/arch/x86/mm/hap/nested_ept.c new file +9
-0+static bool_t nept_rwx_bits_check (ept_entry_t e) { +/*write only or write/execute only*/ +uint8_t rwx_bits = e.epte &EPTE_RWX_MASK ; + +if ( rwx_bits == ept_access_w || rwx_bits == ept_access_wx ) +return 1 ; + +if ( rwx_bits == ept_access_x && !(NEPT_VPID_CAP_BITS & +VMX_EPT_EXEC_ONLY_SUPPORTED ))
In a later patch you add VMX_EPT_EXEC_ONLY_SUPPORTED to this field. How can that work when running on a CPU that doesn't support exec-only? The nested-ept tables will have exec-only mapping in them which the CPU will reject.
xen/arch/x86/mm/hap/nested_ept.c line 202
Quoted 9 lines
xen/arch/x86/mm/hap/nested_ept.c new file +9
-0+done: + ret =EPT_TRANSLATE_SUCCEED ; + goto out; + +map_err: +if ( rc == _PAGE_PAGED ) + ret =EPT_TRANSLATE_RETRY ; +else + ret =EPT_TRANSLATE_ERR_PAGE ;
What does this error code mean? The caller just retries the faulting instruction when it sees it, which sounds wrong. Why not just return EPT_TRANSLATE_MISCONFIG if the guest uses an unmappable frame for EPT tables?
xen/arch/x86/mm/hap/nested_ept.c line 276
Quoted 3 lines
xen/arch/x86/mm/hap/nested_ept.c new file +3
-0+default : + rc =EPT_TRANSLATE_UNSUPPORTED ; +gdprintk (XENLOG_ERR ,"Unsupported ept translation type!:%d\n" , rc);
Just BUG() here and get rid of EPT_TRANSLATE_UNSUPPORTED and NESTEDHVM_PAGEFAULT_UNHANDLED. The function that provided rc is right above and we can see it hasn't got any other return values.
xen/arch/x86/mm/shadow/multi.c static mfn_t emulate_gva_to_mfn(struct vcpu *v, · line 4585
Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c +1
-1@@ -4582,7 +4582,7 @@ static mfn_t emulate_gva_to_mfn(struct vcpu *v,/* Translate the GFN to an MFN */ ASSERT (!paging_locked_by_me (v->domain)); mfn =get_gfn (v->domain,_gfn (gfn), &p2mt); - +
This stray change should be dropped.
xen/arch/x86/mm/shadow/multi.c static mfn_t emulate_gva_to_mfn(struct vcpu *v,
Quoted 10 lines
xen/arch/x86/mm/shadow/multi.c +10
-0+typedef enum { + ept_access_n =0 ,/* No access permissions allowed */ + ept_access_r =1 , + ept_access_w =2 , + ept_access_rw =3 , + ept_access_x =4 , + ept_access_rx =5 , + ept_access_wx =6 , + ept_access_all =7 , +}ept_access_t ;
This enum isn't used anywhere.
Cheers,
Tim.
Re: [PATCH v3 03/10] nested_ept: Implement guest ept's walker
Quoted 23 lines
-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Thursday, December 20, 2012 8:52 PM
To: Zhang, Xiantao
Cc: [email protected]; [email protected]; Nakajima, Jun; Dong, Eddie; [email protected]
Subject: Re: [Xen-devel] [PATCH v3 03/10]nested_ept: Implement guest ept's walkerQuoted 7 lines · Xiantao Zhang
From: Zhang Xiantao <[email protected]>
Implment guest EPT PT walker, some logic is based on shadow's ia32e PT walker. During the PT walking, if the target pages are not in memory, use RETRY mechanism and get a chance to let the target page back.
Signed-off-by Zhang Xiantao <[email protected]>This is much nicer than v1, thanks. I have some comments below, and the whole thing needs to be checked for whitespace mangling.
Quoted 14 lines
xen/arch/x86/mm/hap/nested_ept.c line 105
Quoted 9 lines
xen/arch/x86/mm/hap/nested_ept.c new file +9
-0+static bool_t nept_rwx_bits_check (ept_entry_t e) { +/*write only or write/execute only*/ +uint8_t rwx_bits = e.epte &EPTE_RWX_MASK ; + +if ( rwx_bits == ept_access_w || rwx_bits == ept_access_wx ) +return 1 ; + +if ( rwx_bits == ept_access_x && !(NEPT_VPID_CAP_BITS & +VMX_EPT_EXEC_ONLY_SUPPORTED ))In a later patch you add
VMX_EPT_EXEC_ONLY_SUPPORTEDto this field. How can that work when running on a CPU that doesn't support exec-only? The nested-ept tables will have exec-only mapping in them which the CPU will reject.
Fixed, and will consult host's capability first.
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/hap/nested_ept.c new file +6
-0+done: + ret =EPT_TRANSLATE_SUCCEED ; + goto out; + +map_err: +if ( rc == _PAGE_PAGED )
Quoted 8 lines
xen/arch/x86/mm/hap/nested_ept.c line 202
Quoted 3 lines
xen/arch/x86/mm/hap/nested_ept.c new file +3
-0+ ret =EPT_TRANSLATE_RETRY ; + else + ret =EPT_TRANSLATE_ERR_PAGE ;What does this error code mean? The caller just retries the faulting instruction when it sees it, which sounds wrong. Why not just return
EPT_TRANSLATE_MISCONFIGif the guest uses an unmappable frame for EPT tables?
Okay, although this doesn't fully follow SDM, injecting a EPT misconfiguration in this case should be a better way instead of hanging there.
Quoted 8 lines
xen/arch/x86/mm/hap/nested_ept.c
Quoted 4 lines
xen/arch/x86/mm/hap/nested_ept.c new file +4
-0+default : + rc =EPT_TRANSLATE_UNSUPPORTED ; +gdprintk (XENLOG_ERR ,"Unsupported ept translation + type!:%d\n", rc);Just
BUG()here and get rid ofEPT_TRANSLATE_UNSUPPORTEDandNESTEDHVM_PAGEFAULT_UNHANDLED. The function that provided rc is right above and we can see it hasn't got any other return values.
Okay, this is also what the version 1 does.
Quoted 10 lines
xen/arch/x86/mm/shadow/multi.c static mfn_t emulate_gva_to_mfn(struct vcpu *v, · line 4585
Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c +1
-1@@ -4582,7 +4582,7 @@ static mfn_t emulate_gva_to_mfn(struct vcpu *v,/* Translate the GFN to an MFN */ ASSERT (!paging_locked_by_me (v->domain)); mfn =get_gfn (v->domain,_gfn (gfn), &p2mt); - +This stray change should be dropped.
Dropped.
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/shadow/multi.c +4
-0+typedef enum { + ept_access_n = 0, /* No access permissions allowed */ + ept_access_r = 1, + ept_access_w = 2,
Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c
Quoted 6 lines
xen/arch/x86/mm/shadow/multi.c +6
-0+ ept_access_rw =3 , + ept_access_x =4 , + ept_access_rx =5 , + ept_access_wx =6 , + ept_access_all =7 , +} ept_access_t;This enum isn't used anywhere.
Actually, it is used in the function nept_rwx_bits_check. :)
Xiantao
Re: [PATCH v3 03/10] nested_ept: Implement guest ept's walker
Quoted 13 lines · Xiantao
Quoted 4 lines
Quoted 4 lines
xen/include/asm-x86/hvm/vmx/vmx.h +4
-0+typedef enum { + ept_access_n = 0, /* No access permissions allowed */ + ept_access_r = 1, + ept_access_w = 2,Quoted 8 lines
xen/include/asm-x86/hvm/vmx/vmx.h typedef union { · line 89
Quoted 6 lines
xen/include/asm-x86/hvm/vmx/vmx.h +6
-0+ ept_access_rw =3 , + ept_access_x =4 , + ept_access_rx =5 , + ept_access_wx =6 , + ept_access_all =7 , +} ept_access_t;This enum isn't used anywhere.
Quoted 1 line
Actually, it is used in the function
nept_rwx_bits_check. :)
Oops - so it is. :)
Tim.
PATCH v3 4/10 EPT: Make ept data structure or operations neutral
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Share the current EPT logic with nested EPT case, so make the related data structure or operations netural to comment EPT and nested EPT.
--- xen/arch/x86/hvm/vmx/vmcs.c | 9 +++- xen/arch/x86/hvm/vmx/vmx.c | 53 ++----------------- xen/arch/x86/mm/p2m-ept.c | 104 ++++++++++++++++++++++++++++-------- xen/arch/x86/mm/p2m.c | 23 ++++++--- xen/include/asm-x86/hvm/vmx/vmcs.h | 23 ++++---- xen/include/asm-x86/hvm/vmx/vmx.h | 10 +++- xen/include/asm-x86/p2m.h | 4 ++ 7 files changed, 133 insertions(+), 93 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +7 -2
@@ -941,8 +941,13 @@ static int construct_vmcs(struct vcpu *v)__vmwrite (TPR_THRESHOLD ,0 ); } -if (paging_mode_hap (d) ) -__vmwrite (EPT_POINTER , d->arch.hvm_domain.vmx.ept_control.eptp); +if (paging_mode_hap (d) ) { +struct p2m_domain *p2m =p2m_get_hostp2m (d); +struct ept_data *ept = &p2m->ept; + + ept->asr =pagetable_get_pfn (p2m_get_pagetable (p2m)); +__vmwrite (EPT_POINTER ,ept_get_eptp (ept)); + }if ( cpu_has_vmx_pat &&paging_mode_hap (d) ) {
xen/arch/x86/hvm/vmx/vmx.c +4 -49
91 lines not on this page · raw message
xen/arch/x86/mm/p2m-ept.c +82 -22
216 lines not on this page · raw message
xen/arch/x86/mm/p2m.c +16 -7
@@ -57,8 +57,10 @@ boolean_param("hap_2mb", opt_hap_2mb);/* Init the datastructures for later use by the p2m code */ -static void p2m_initialise (struct domain *d,struct p2m_domain *p2m) +static int p2m_initialise (struct domain *d,struct p2m_domain *p2m) { +int ret =0 ; +mm_rwlock_init (&p2m->lock);mm_lock_init (&p2m->pod.lock);INIT_LIST_HEAD (&p2m->np2m_list);
@@ -72,11 +74,11 @@ static void p2m_initialise(struct domain *d, struct p2m_domain *p2m) p2m->np2m_base =P2M_BASE_EADDR ;if (hap_enabled (d) && cpu_has_vmx ) -ept_p2m_init (p2m); + ret =ept_p2m_init (p2m);else p2m_pt_init (p2m); -return ; +return ret; } static int
@@ -119,7 +121,7 @@ int p2m_init(struct domain *d)* since nestedhvm_enabled(d) returns false here. * (p2m_init runs too early for HVM_PARAM_* options) */ rc =p2m_init_nestedp2m (d); -if ( rc ) +if ( rc )p2m_final_teardown (d);return rc; }
@@ -424,12 +426,16 @@ void p2m_teardown(struct p2m_domain *p2m)static void p2m_teardown_nestedp2m (struct domain *d) {uint8_t i; +struct p2m_domain *p2m;for (i =0 ; i <MAX_NESTEDP2M ; i++) {if ( !d->arch.nested_p2m[i] )continue ; -free_cpumask_var (d->arch.nested_p2m[i]->dirty_cpumask); -xfree (d->arch.nested_p2m[i]); + p2m = d->arch.nested_p2m[i]; +free_cpumask_var (p2m->dirty_cpumask); +if (hap_enabled (d) && cpu_has_vmx ) +ept_p2m_uninit (p2m); +xfree (p2m); d->arch.nested_p2m[i] =NULL ; } }
@@ -437,9 +443,12 @@ static void p2m_teardown_nestedp2m(struct domain *d)void p2m_final_teardown (struct domain *d) {/* Iterate over all p2m tables per domain */ -if ( d->arch.p2m ) +struct p2m_domain *p2m =p2m_get_hostp2m (d); +if ( p2m ) {free_cpumask_var (d->arch.p2m->dirty_cpumask); +if (hap_enabled (d) && cpu_has_vmx ) +ept_p2m_uninit (p2m);xfree (d->arch.p2m); d->arch.p2m =NULL ; }
xen/include/asm-x86/hvm/vmx/vmcs.h +12 -11
@@ -56,26 +56,27 @@ struct vmx_msr_state {#define EPT_DEFAULT_MT MTRR_TYPE_WRBACK -struct vmx_domain { -unsigned long apic_access_mfn; +struct ept_data {union { -struct { +struct {u64 ept_mt :3 , ept_wl :3 , rsvd :6 , asr :52 ; };u64 eptp; - } ept_control; -cpumask_var_t ept_synced; + }; +cpumask_var_t synced_mask; +}; + +struct vmx_domain { +unsigned long apic_access_mfn; }; -#define ept_get_wl (d) \ - ((d)->arch.hvm_domain.vmx.ept_control.ept_wl) -#define ept_get_asr (d) \ - ((d)->arch.hvm_domain.vmx.ept_control.asr) -#define ept_get_eptp (d) \ - ((d)->arch.hvm_domain.vmx.ept_control.eptp) +#define ept_get_wl (ept) ((ept)->ept_wl) +#define ept_get_asr (ept) ((ept)->asr) +#define ept_get_eptp (ept) ((ept)->eptp) +#define ept_get_synced_mask (ept) ((ept)->synced_mask)struct arch_vmx_struct {/* Virtual address of VMCS. */
xen/include/asm-x86/hvm/vmx/vmx.h +8 -2
@@ -360,7 +360,7 @@ static inline void ept_sync_all(void)__invept (INVEPT_ALL_CONTEXT ,0 ,0 ); } -void ept_sync_domain (struct domain *d); +void ept_sync_domain (struct p2m_domain *p2m);static inline void vpid_sync_vcpu_gva (struct vcpu *v,unsigned long gva) {
@@ -422,12 +422,18 @@ void vmx_get_segment_register(struct vcpu *, enum x86_segment,void vmx_inject_extint (int trap);void vmx_inject_nmi (void ); -void ept_p2m_init (struct p2m_domain *p2m); +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);void setup_ept_dump (void );void update_guest_eip (void ); +int alloc_p2m_hap_data (struct p2m_domain *p2m); +void free_p2m_hap_data (struct p2m_domain *p2m); +void p2m_init_hap_data (struct p2m_domain *p2m); +/* EPT violation qualifications definitions */ #define _EPT_READ_VIOLATION 0#define EPT_READ_VIOLATION (1UL<<_EPT_READ_VIOLATION)
xen/include/asm-x86/p2m.h +4 -0
@@ -277,6 +277,10 @@ struct p2m_domain {mm_lock_t lock;/* Locking of private pod structs, * * not relying on the p2m lock. */ } pod; +union { +struct ept_data ept; +/* NPT-equivalent structure could be added here. */ + }; };/* get host p2m table */
Signature · 1 line
1.7.1
Re: [PATCH v3 04/10] EPT: Make ept data structure or operations neutral
Quoted 7 lines · Xiantao Zhang
From: Zhang Xiantao <[email protected]>
Share the current EPT logic with nested EPT case, so make the related data structure or operations netural to comment EPT and nested EPT.
Signed-off-by Zhang Xiantao <[email protected]>
Looking good. I would ack this, except for for one thing -- you've made p2m_initialise() return an error code but not updated either of the callers to use that code.
Cheers,
Tim.
PATCH v3 5/10 nEPT: Try to enable EPT paging for L2 guest.
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Once found EPT is enabled by L1 VMM, enabled nested EPT support for L2 guest.
--- xen/arch/x86/hvm/vmx/vmx.c | 16 +++++++++-- xen/arch/x86/hvm/vmx/vvmx.c | 48 +++++++++++++++++++++++++++-------- xen/include/asm-x86/hvm/vmx/vvmx.h | 5 +++- 3 files changed, 54 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +13 -3
@@ -1461,6 +1461,7 @@ static struct hvm_function_table __read_mostly vmx_function_table = { .nhvm_vcpu_guestcr3 = nvmx_vcpu_guestcr3, .nhvm_vcpu_p2m_base = nvmx_vcpu_eptp_base, .nhvm_vcpu_asid = nvmx_vcpu_asid, + .nhvm_vmcx_hap_enabled = nvmx_ept_enabled, .nhvm_vmcx_guest_intercepts_trap = nvmx_intercepts_exception, .nhvm_vcpu_vmexit_trap = nvmx_vmexit_trap, .nhvm_intr_blocked = nvmx_intr_blocked,
@@ -2003,6 +2004,7 @@ static void ept_handle_violation(unsigned long qualification, paddr_t gpa)unsigned long gla, gfn = gpa >>PAGE_SHIFT ;mfn_t mfn;p2m_type_t p2mt; +int ret;struct domain *d = current->domain;if ( tb_init_done )
@@ -2017,18 +2019,26 @@ static void ept_handle_violation(unsigned long qualification, paddr_t gpa) _d.gpa = gpa; _d.qualification = qualification; _d.mfn =mfn_x (get_gfn_query_unlocked (d, gfn, &_d.p2mt)); - +__trace_var (TRC_HVM_NPF ,0 ,sizeof (_d), &_d); } -if (hvm_hap_nested_page_fault (gpa, + ret =hvm_hap_nested_page_fault (gpa, qualification &EPT_GLA_VALID ?1 :0 , qualification &EPT_GLA_VALID ?__vmread (GUEST_LINEAR_ADDRESS ) : ~0ull , qualification &EPT_READ_VIOLATION ?1 :0 , qualification &EPT_WRITE_VIOLATION ?1 :0 , - qualification &EPT_EXEC_VIOLATION ?1 :0 ) ) + qualification &EPT_EXEC_VIOLATION ?1 :0 ); +switch ( ret ) { +case 0 :// Unhandled L1 EPT violation +break ; +case 1 :// This violation is handled completly return ; +case -1 :// This vioaltion should be injected to L1 VMM +vcpu_nestedhvm (current).nv_vmexit_pending =1 ; +return ; + }/* Everything else is an error. */ mfn =get_gfn_query_unlocked (d, gfn, &p2mt);
xen/arch/x86/hvm/vmx/vvmx.c +37 -11
101 lines not on this page · raw message
xen/include/asm-x86/hvm/vmx/vvmx.h +4 -1
@@ -33,9 +33,10 @@ struct nestedvmx {u32 error_code; } intr;struct { +bool_t enabled;uint32_t exit_reason;uint32_t exit_qual; - } ept_exit; + } ept; };#define vcpu_2_nvmx (v) (vcpu_nestedhvm(v).u.nvmx)
@@ -110,6 +111,8 @@ int nvmx_intercepts_exception(struct vcpu *v, unsigned int trap, int error_code);void nvmx_domain_relinquish_resources (struct domain *d); +bool_t nvmx_ept_enabled (struct vcpu *v); +int nvmx_handle_vmxon (struct cpu_user_regs *regs);int nvmx_handle_vmxoff (struct cpu_user_regs *regs);
Signature · 1 line
1.7.1
PATCH v3 6/10 nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
For PAE L2 guest, GUEST_DPPTR registers needs to be synced for each virtual vmentry.
--- xen/arch/x86/hvm/vmx/vvmx.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +8 -1
@@ -826,7 +826,14 @@ static void load_shadow_guest_state(struct vcpu *v)vvmcs_to_shadow (vvmcs,CR0_GUEST_HOST_MASK );vvmcs_to_shadow (vvmcs,CR4_GUEST_HOST_MASK ); -/* TODO: PDPTRs for nested ept */ +if (nvmx_ept_enabled (v) &&hvm_pae_enabled (v) && + (v->arch.hvm_vcpu.guest_efer &EFER_LMA ) ) { +vvmcs_to_shadow (vvmcs,GUEST_PDPTR0 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR1 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR2 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR3 ); + } +/* TODO: CR3 target control */ }
Signature · 1 line
1.7.1
Re: [PATCH v3 06/10] nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Xiantao Zhang
From: Zhang Xiantao <[email protected]>
For PAE L2 guest,
GUEST_DPPTRregisters needs to be synced for each virtual vmentry.Signed-off-by Zhang Xiantao <[email protected]>--- xen/arch/x86/hvm/vmx/vvmx.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)xen/arch/x86/hvm/vmx/vvmx.c +0
-0
xen/arch/x86/hvm/vmx/vvmx.c static void load_shadow_guest_state(struct vcpu *v) · line 834
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +6
-1@@ -826,7 +826,14 @@ static void load_shadow_guest_state(struct vcpu *v)vvmcs_to_shadow (vvmcs,CR0_GUEST_HOST_MASK );vvmcs_to_shadow (vvmcs,CR4_GUEST_HOST_MASK ); -/* TODO: PDPTRs for nested ept */ +if (nvmx_ept_enabled (v) &&hvm_pae_enabled (v) && + (v->arch.hvm_vcpu.guest_efer &EFER_LMA ) ) { +vvmcs_to_shadow (vvmcs,GUEST_PDPTR0 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR1 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR2 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR3 );
When I commented on the previous version's white space issue in one of the patches, I really expected you to check all patches. Yet here we have tabs again, and the placement of the opening brace isn't right either, nor is the indentation of the continued if() clause.
Please, before re-submitting, make sure you look through all of the patches for further coding style issues.
Jan
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +2
-0+ } +/* TODO: CR3 target control */ }Signature · 1 line
1.7.1
Re: [PATCH v3 06/10] nEPT: Sync PDPTR fields if L2 guest in PAE paging mode
In reply to Xiantao ZhangQuoted 5 lines · Xiantao Zhang
From: Zhang Xiantao <[email protected]>
For PAE L2 guest,
GUEST_DPPTRregisters needs to be synced for each virtual vmentry.Signed-off-by Zhang Xiantao <[email protected]>
Apart from the whitespace mangling that Jan pointed out,
Quoted 32 lines
--- xen/arch/x86/hvm/vmx/vvmx.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)xen/arch/x86/hvm/vmx/vvmx.c +8
-1@@ -826,7 +826,14 @@ static void load_shadow_guest_state(struct vcpu *v)vvmcs_to_shadow (vvmcs,CR0_GUEST_HOST_MASK );vvmcs_to_shadow (vvmcs,CR4_GUEST_HOST_MASK ); -/* TODO: PDPTRs for nested ept */ +if (nvmx_ept_enabled (v) &&hvm_pae_enabled (v) && + (v->arch.hvm_vcpu.guest_efer &EFER_LMA ) ) { +vvmcs_to_shadow (vvmcs,GUEST_PDPTR0 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR1 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR2 ); +vvmcs_to_shadow (vvmcs,GUEST_PDPTR3 ); + } +/* TODO: CR3 target control */ }Signature · 7 lines
1.7.1 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH v3 7/10 nEPT: Use minimal permission for nested p2m.
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Emulate permission check for the nested p2m. Current solution is to use minimal permission, and once meet permission violation in L0, then determin whether it is caused by guest EPT or host EPT
--- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/vmx/vvmx.c | 4 +- xen/arch/x86/mm/hap/nested_ept.c | 5 ++- xen/arch/x86/mm/hap/nested_hap.c | 38 +++++++++++++++++++++++------- xen/include/asm-x86/hvm/hvm.h | 2 +- xen/include/asm-x86/hvm/svm/nestedsvm.h | 2 +- xen/include/asm-x86/hvm/vmx/vvmx.h | 6 ++-- 7 files changed, 40 insertions(+), 19 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -1177,7 +1177,7 @@ nsvm_vmcb_hap_enabled(struct vcpu *v)*/ int nsvm_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, -unsigned int *page_order, +unsigned int *page_order,uint8_t *p2m_acc,bool_t access_r,bool_t access_w,bool_t access_x) {uint32_t pfec;
xen/arch/x86/hvm/vmx/vvmx.c +2 -2
@@ -1493,7 +1493,7 @@ int nvmx_msr_write_intercept(unsigned int msr, u64 msr_content)*/ int nvmx_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, -unsigned int *page_order, +unsigned int *page_order,uint8_t *p2m_acc,bool_t access_r,bool_t access_w,bool_t access_x) {uint64_t exit_qual =__vmread (EXIT_QUALIFICATION );
@@ -1503,7 +1503,7 @@ nvmx_hap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,uint32_t rwx_rights = (access_x <<2 ) | (access_w <<1 ) | access_r;struct nestedvmx *nvmx = &vcpu_2_nvmx (v); - rc =nept_translate_l2ga (v, L2_gpa, page_order, rwx_rights, &gfn, + rc =nept_translate_l2ga (v, L2_gpa, page_order, rwx_rights, &gfn, p2m_acc, &exit_qual, &exit_reason);switch ( rc ) {case EPT_TRANSLATE_SUCCEED :
xen/arch/x86/mm/hap/nested_ept.c +3 -2
@@ -217,8 +217,8 @@ out:int nept_translate_l2ga (struct vcpu *v,paddr_t l2ga,unsigned int *page_order,uint32_t rwx_acc, -unsigned long *l1gfn,uint64_t *exit_qual, -uint32_t *exit_reason) +unsigned long *l1gfn,uint8_t *p2m_acc, +uint64_t *exit_qual,uint32_t *exit_reason) {uint32_t rc, rwx_bits =0 ;ept_walk_t gw;
@@ -253,6 +253,7 @@ int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga,if (nept_permission_check (rwx_acc, rwx_bits) ) { *l1gfn = gw.lxe[0 ].mfn; + *p2m_acc = (uint8_t )rwx_bits;break ; } rc =EPT_TRANSLATE_VIOLATION ;
xen/arch/x86/mm/hap/nested_hap.c +29 -9
@@ -142,12 +142,12 @@ nestedhap_fix_p2m(struct vcpu *v, struct p2m_domain *p2m,*/ static int nestedhap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, -unsigned int *page_order, +unsigned int *page_order,uint8_t *p2m_acc,bool_t access_r,bool_t access_w,bool_t access_x) {ASSERT (hvm_funcs.nhvm_hap_walk_L1_p2m); -return hvm_funcs.nhvm_hap_walk_L1_p2m (v, L2_gpa, L1_gpa, page_order, +return hvm_funcs.nhvm_hap_walk_L1_p2m (v, L2_gpa, L1_gpa, page_order, p2m_acc, access_r, access_w, access_x); }
@@ -158,16 +158,15 @@ nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa,*/ static int nestedhap_walk_L0_p2m (struct p2m_domain *p2m,paddr_t L1_gpa,paddr_t *L0_gpa, -p2m_type_t *p2mt, +p2m_type_t *p2mt,p2m_access_t *p2ma,unsigned int *page_order,bool_t access_r,bool_t access_w,bool_t access_x) {mfn_t mfn; -p2m_access_t p2ma;int rc;/* walk L0 P2M table */ - mfn =get_gfn_type_access (p2m, L1_gpa >>PAGE_SHIFT , p2mt, &p2ma, + mfn =get_gfn_type_access (p2m, L1_gpa >>PAGE_SHIFT , p2mt, p2ma,0 , page_order); rc =NESTEDHVM_PAGEFAULT_MMIO ;
@@ -206,12 +205,14 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,struct p2m_domain *p2m, *nested_p2m;unsigned int page_order_21, page_order_10, page_order_20;p2m_type_t p2mt_10; +p2m_access_t p2ma_10 = p2m_access_rwx; +uint8_t p2ma_21; p2m =p2m_get_hostp2m (d);/* L0 p2m */ nested_p2m =p2m_get_nestedp2m (v,nhvm_vcpu_p2m_base (v));/* walk the L1 P2M table */ - rv =nestedhap_walk_L1_p2m (v, *L2_gpa, &L1_gpa, &page_order_21, + rv =nestedhap_walk_L1_p2m (v, *L2_gpa, &L1_gpa, &page_order_21, &p2ma_21, access_r, access_w, access_x);/* let caller to handle these two cases */
@@ -229,7 +230,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,/* ==> we have to walk L0 P2M */ rv =nestedhap_walk_L0_p2m (p2m, L1_gpa, &L0_gpa, - &p2mt_10, &page_order_10, + &p2mt_10, &p2ma_10, &page_order_10, access_r, access_w, access_x);/* let upper level caller to handle these two cases */
@@ -250,10 +251,29 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, page_order_20 =min (page_order_21, page_order_10); +ASSERT (p2ma_10 <= p2m_access_n2rwx); +/*NOTE: if assert fails, needs to handle new access type here */ + +switch ( p2ma_10 ) { + case p2m_access_n ... p2m_access_rwx: +break ; +case p2m_access_rx2rw: + p2ma_10 = p2m_access_rx; +break ; +case p2m_access_n2rwx: + p2ma_10 = p2m_access_n; +break ; +default : + p2ma_10 = p2m_access_n; +/* For safety, remove all permissions. */ +gdprintk (XENLOG_ERR ,"Unhandled p2m access type:%d\n" , p2ma_10); + } +/* Use minimal permission for nested p2m. */ + p2ma_10 &= (p2m_access_t )p2ma_21; +/* fix p2m_get_pagetable(nested_p2m) */ nestedhap_fix_p2m (v, nested_p2m, *L2_gpa, L0_gpa, page_order_20, - p2mt_10, - p2m_access_rwx/* FIXME: Should use minimum permission. */ ); + p2mt_10, p2ma_10);return NESTEDHVM_PAGEFAULT_DONE ; }
xen/include/asm-x86/hvm/hvm.h +1 -1
@@ -186,7 +186,7 @@ struct hvm_function_table {/*Walk nested p2m */ int (*nhvm_hap_walk_L1_p2m)(struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, -unsigned int *page_order, +unsigned int *page_order,uint8_t *p2m_acc,bool_t access_r,bool_t access_w,bool_t access_x); };
xen/include/asm-x86/hvm/svm/nestedsvm.h +1 -1
@@ -134,7 +134,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs *regs, struct vcpu *v);void svm_vmexit_do_stgi (struct cpu_user_regs *regs,struct vcpu *v);bool_t nestedsvm_gif_isset (struct vcpu *v);int nsvm_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, -unsigned int *page_order, +unsigned int *page_order,uint8_t *p2m_acc,bool_t access_r,bool_t access_w,bool_t access_x);#define NSVM_INTR_NOTHANDLED 3
xen/include/asm-x86/hvm/vmx/vvmx.h +3 -3
@@ -125,7 +125,7 @@ int nvmx_handle_vmxoff(struct cpu_user_regs *regs);int nvmx_hap_walk_L1_p2m (struct vcpu *v,paddr_t L2_gpa,paddr_t *L1_gpa, -unsigned int *page_order, +unsigned int *page_order,uint8_t *p2m_acc,bool_t access_r,bool_t access_w,bool_t access_x);/* * Virtual VMCS layout
@@ -208,7 +208,7 @@ int nvmx_n2_vmexit_handler(struct cpu_user_regs *regs,int nept_translate_l2ga (struct vcpu *v,paddr_t l2ga,unsigned int *page_order,uint32_t rwx_acc, -unsigned long *l1gfn,uint64_t *exit_qual, -uint32_t *exit_reason); +unsigned long *l1gfn,uint8_t *p2m_acc, +uint64_t *exit_qual,uint32_t *exit_reason);#endif /* __ASM_X86_HVM_VVMX_H__ */
Signature · 1 line
1.7.1
Re: [PATCH v3 07/10] nEPT: Use minimal permission for nested p2m.
Quoted 4 lines · Xiantao Zhang
xen/arch/x86/mm/hap/nested_hap.c +0
-0@@ -206,12 +205,14 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,struct p2m_domain *p2m, *nested_p2m;unsigned int page_order_21, page_order_10, page_order_20;p2m_type_t p2mt_10;
xen/arch/x86/mm/hap/nested_hap.c nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, · line 216
Quoted 10 lines
xen/arch/x86/mm/hap/nested_hap.c +3
-1+p2m_access_t p2ma_10 = p2m_access_rwx; +uint8_t p2ma_21; p2m =p2m_get_hostp2m (d);/* L0 p2m */ nested_p2m =p2m_get_nestedp2m (v,nhvm_vcpu_p2m_base (v));/* walk the L1 P2M table */ - rv =nestedhap_walk_L1_p2m (v, *L2_gpa, &L1_gpa, &page_order_21, + rv =nestedhap_walk_L1_p2m (v, *L2_gpa, &L1_gpa, &page_order_21, &p2ma_21, access_r, access_w, access_x);
Once again, please either initialise p2ma_21 to rwx or have the SVM version of this lookup set it to something sensible.
Cheers,
Tim.
Hi,
Quoted 5 lines · Xiantao Zhang
Received: from hax-build.sh.intel.com ([10.239.48.28])
by fmsmga001.fm.intel.com with ESMTP; 19 Dec 2012 19:59:04 -0800From: Xiantao Zhang <[email protected]>
To: [email protected]
Date: Thu, 20 Dec 2012 23:43:41 +0800
I think the clock on your computer or your email client is confused:
your email is datestamped about 12 hours in the future.
Tim.
Oops! Seems my dev machine has wrong date setting. Anyway, it tells us the End of the World is not real, because we have some mails sent out after the END. :) Xiantao
Quoted 22 lines
-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Thursday, December 20, 2012 9:56 PM
To: Zhang, Xiantao
Cc: [email protected]; [email protected]; Nakajima, Jun; Dong, Eddie; [email protected]
Subject: Re: [Xen-devel] [PATCH v3 00/10] Nested VMX: Add virtual EPT & VPID support to L1 VMMHi,
Quoted 5 lines · Xiantao Zhang
Received: from hax-build.sh.intel.com ([10.239.48.28])
by fmsmga001.fm.intel.com with ESMTP; 19 Dec 2012 19:59:04 -0800From: Xiantao Zhang <[email protected]>
To: [email protected]
Date: Thu, 20 Dec 2012 23:43:41 +0800I think the clock on your computer or your email client is confused:
your email is datestamped about 12 hours in the future.Tim.
PATCH v3 8/10 nEPT: handle invept instruction from L1 VMM
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Add the INVEPT instruction emulation logic.
--- xen/arch/x86/hvm/vmx/vmx.c | 6 ++++- xen/arch/x86/hvm/vmx/vvmx.c | 39 ++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/vmx/vvmx.h | 1 + 3 files changed, 45 insertions(+), 1 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +5 -1
@@ -2573,10 +2573,14 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)update_guest_eip ();break ; +case EXIT_REASON_INVEPT : +if (nvmx_handle_invept (regs) ==X86EMUL_OKAY ) +update_guest_eip (); +break ; +case EXIT_REASON_MWAIT_INSTRUCTION :case EXIT_REASON_MONITOR_INSTRUCTION :case EXIT_REASON_GETSEC : -case EXIT_REASON_INVEPT :case EXIT_REASON_INVVPID :/* * We should never exit on GETSEC because CR4.SMXE is always 0 when
xen/arch/x86/hvm/vmx/vvmx.c +39 -0
@@ -1356,6 +1356,45 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } +int nvmx_handle_invept (struct cpu_user_regs *regs) +{ +struct vmx_inst_decoded decode; +unsigned long eptp; +u64 inv_type; + +if ( !cpu_has_vmx_ept ) +return X86EMUL_EXCEPTION ; + +if (decode_vmx_inst (regs, &decode, &eptp,0 ) + !=X86EMUL_OKAY ) +return X86EMUL_EXCEPTION ; + + inv_type =reg_read (regs, decode.reg2); +gdprintk (XENLOG_DEBUG ,"inv_type:%ld, eptp:%lx\n" , inv_type, eptp); + +switch ( inv_type ) { +case INVEPT_SINGLE_CONTEXT : + { +struct p2m_domain *p2m =vcpu_nestedhvm (current).nv_p2m; +if ( p2m ) + { +p2m_flush (current, p2m); +ept_sync_domain (p2m); + } +break ; + } +case INVEPT_ALL_CONTEXT : +p2m_flush_nestedp2m (current->domain); +__invept (INVEPT_ALL_CONTEXT ,0 ,0 ); +break ; +default : +return X86EMUL_EXCEPTION ; + } +vmreturn (regs,VMSUCCEED ); +return X86EMUL_OKAY ; +} + +#define __emul_value (enable1, default1) \ ((enable1 | default1) << 32 | (default1))
xen/include/asm-x86/hvm/vmx/vvmx.h +1 -0
@@ -191,6 +191,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs);int nvmx_handle_vmwrite (struct cpu_user_regs *regs);int nvmx_handle_vmresume (struct cpu_user_regs *regs);int nvmx_handle_vmlaunch (struct cpu_user_regs *regs); +int nvmx_handle_invept (struct cpu_user_regs *regs);int nvmx_msr_read_intercept (unsigned int msr,u64 *msr_content); int nvmx_msr_write_intercept(unsigned int msr,
Signature · 1 line
1.7.1
Re: [PATCH v3 08/10] nEPT: handle invept instruction from L1 VMM
xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2580
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Xiantao Zhang
xen/arch/x86/hvm/vmx/vmx.c +5
-0@@ -2573,10 +2573,14 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)update_guest_eip ();break ; +case EXIT_REASON_INVEPT : +if (nvmx_handle_invept (regs) ==X86EMUL_OKAY ) +update_guest_eip (); +break ; +
I realize that you're just copying code written the same way elsewhere, but: What if (here and elsewhere) X86EMUL_OKAY is not being returned (e.g. in the non-nested case)? Without the nested VMX code, all of these would have ended up at the default case (crashing the guest). Iiuc the correct action would be to inject an exception at least when X86EMUL_EXCEPTION is being returned here - whether that's done here or (perhaps better, as only it can know _what_ exception to inject) by the callee is another thing to decide.
Also, at the example of nvmx_handle_vmclear() I see that it produces exceptions in most of the cases, but I think all of the related code needs auditing that things are being handled consistently _and_ completely (constructs like
if ( ... != X86EMUL_OKAY )
return X86EMUL_EXCEPTION;are definitely not okay, as there are further X86EMUL_* values that can occur; if you know only the two must ever occur at a given place, ASSERT() so, making things clear to the reader without having to follow all code paths).
Jan
Re: [PATCH v3 08/10] nEPT: handle invept instruction from L1 VMM
Quoted 37 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, December 20, 2012 5:55 PM
To: Zhang, Xiantao
Cc: Dong, Eddie; Nakajima, Jun; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 08/10] nEPT: handle invept instruction from L1 VMMxen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Xiantao Zhang
xen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -2573,10 +2573,14 @@ void vmx_vmexit_handler(struct
cpu_user_regs*regs)xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2580
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +5
-0update_guest_eip ();break ; +case EXIT_REASON_INVEPT : +if (nvmx_handle_invept (regs) ==X86EMUL_OKAY ) +update_guest_eip (); +break ; +I realize that you're just copying code written the same way elsewhere, but:
What if (here and elsewhere)X86EMUL_OKAYis not being returned (e.g. in the non-nested case)? Without the nested VMX code, all of these would have ended up at the default case (crashing the guest). Iiuc the correct action would be to inject an exception at least whenX86EMUL_EXCEPTIONis being returned here - whether that's done here or (perhaps better, as only it can know _what_ exception to inject) by the callee is another thing to decide.Also, at the example of
nvmx_handle_vmclear()I see that it produces exceptions in most of the cases, but I think all of the related code needs auditing that things are being handled consistently _and_ completely (constructs like
Quoted 6 lines
if ( ... != X86EMUL_OKAY ) return X86EMUL_EXCEPTION;are definitely not okay, as there are further X86EMUL_* values that can occur; if you know only the two must ever occur at a given place,
ASSERT()so, making things clear to the reader without having to follow all code paths).
Hi, Jan
I think it is better that the callee should be responsible for handling the exception before returning X86EMUL_EXCEPTION to its caller. so for the newly-introduced two functions, I will add the logic to handle its possible exceptions before its return. Xiantao
PATCH v3 9/10 nVMX: virutalize VPID capability to nested VMM.
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Virtualize VPID for the nested vmm, use host's VPID to emualte guest's VPID. For each virtual vmentry, if guest'v vpid is changed, allocate a new host VPID for L2 guest.
--- xen/arch/x86/hvm/vmx/vmx.c | 11 ++++++- xen/arch/x86/hvm/vmx/vvmx.c | 56 ++++++++++++++++++++++++++++++++++- xen/include/asm-x86/hvm/vmx/vvmx.h | 2 + 3 files changed, 65 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +9 -2
@@ -2578,10 +2578,14 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)update_guest_eip ();break ; +case EXIT_REASON_INVVPID : +if (nvmx_handle_invvpid (regs) ==X86EMUL_OKAY ) +update_guest_eip (); +break ; +case EXIT_REASON_MWAIT_INSTRUCTION :case EXIT_REASON_MONITOR_INSTRUCTION :case EXIT_REASON_GETSEC : -case EXIT_REASON_INVVPID :/* * We should never exit on GETSEC because CR4.SMXE is always 0 when * running in guest context, and the CPU checks that before getting
@@ -2699,8 +2703,11 @@ void vmx_vmenter_helper(void)if ( !cpu_has_vmx_vpid ) goto out; +if (nestedhvm_vcpu_in_guestmode (curr) ) + p_asid = &vcpu_nestedhvm (curr).nv_n2asid; +else + p_asid = &curr->arch.hvm_vcpu.n1asid; - p_asid = &curr->arch.hvm_vcpu.n1asid; old_asid = p_asid->asid; need_flush =hvm_asid_handle_vmenter (p_asid); new_asid = p_asid->asid;
xen/arch/x86/hvm/vmx/vvmx.c +54 -2
92 lines not on this page · raw message
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -0
@@ -37,6 +37,7 @@ struct nestedvmx {uint32_t exit_reason;uint32_t exit_qual; } ept; +uint32_t guest_vpid; };#define vcpu_2_nvmx (v) (vcpu_nestedhvm(v).u.nvmx)
@@ -192,6 +193,7 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs);int nvmx_handle_vmresume (struct cpu_user_regs *regs);int nvmx_handle_vmlaunch (struct cpu_user_regs *regs);int nvmx_handle_invept (struct cpu_user_regs *regs); +int nvmx_handle_invvpid (struct cpu_user_regs *regs);int nvmx_msr_read_intercept (unsigned int msr,u64 *msr_content); int nvmx_msr_write_intercept(unsigned int msr,
Signature · 1 line
1.7.1
Re: [PATCH v3 09/10] nVMX: virutalize VPID capability to nested VMM.
xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1434
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Xiantao Zhang
xen/arch/x86/hvm/vmx/vvmx.c +8
-0+int nvmx_handle_invvpid (struct cpu_user_regs *regs) +{ +struct vmx_inst_decoded decode; +unsigned long vpid; +u64 inv_type; + +if ( !cpu_has_vmx_vpid ) +return X86EMUL_EXCEPTION ;
Same problem here - you mustn't return X86EMUL_EXCEPTION without also raising an exception.
Jan
Quoted 25 lines
xen/arch/x86/hvm/vmx/vvmx.c +22
-0+ +if (decode_vmx_inst (regs, &decode, &vpid,0 ) !=X86EMUL_OKAY ) +return X86EMUL_EXCEPTION ; + + inv_type =reg_read (regs, decode.reg2); +gdprintk (XENLOG_DEBUG ,"inv_type:%ld, vpid:%lx\n" , inv_type, vpid); + +switch ( inv_type ) { +/* Just invalidate all tlb entries for all types! */ +case INVVPID_INDIVIDUAL_ADDR : +case INVVPID_SINGLE_CONTEXT : +case INVVPID_ALL_CONTEXT : +hvm_asid_flush_vcpu_asid (&vcpu_nestedhvm (current).nv_n2asid); +break ; +default : +return X86EMUL_EXCEPTION ; + } +vmreturn (regs,VMSUCCEED ); + +return X86EMUL_OKAY ; +} +/* * Capability reporting */
PATCH v3 10/10 nEPT: expost EPT & VPID capablities to L1 VMM
In reply to Xiantao ZhangFrom: Zhang Xiantao <[email protected]>
Expose EPT's and VPID 's basic features to L1 VMM. For EPT, no EPT A/D bit feature supported. For VPID, exposes all features to L1 VMM
--- xen/arch/x86/hvm/vmx/vvmx.c | 17 +++++++++++++++-- xen/arch/x86/mm/hap/nested_ept.c | 19 ++++++++++++------- xen/include/asm-x86/hvm/vmx/vvmx.h | 2 ++ 3 files changed, 29 insertions(+), 9 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +15 -2
@@ -1484,6 +1484,8 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content)break ;case MSR_IA32_VMX_PROCBASED_CTLS :case MSR_IA32_VMX_TRUE_PROCBASED_CTLS : + { +u32 default1_bits =VMX_PROCBASED_CTLS_DEFAULT1 ;/* 1-seetings */ data =CPU_BASED_HLT_EXITING | CPU_BASED_VIRTUAL_INTR_PENDING |
@@ -1505,12 +1507,20 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content)CPU_BASED_PAUSE_EXITING |CPU_BASED_RDPMC_EXITING |CPU_BASED_ACTIVATE_SECONDARY_CONTROLS ; - data =gen_vmx_msr (data,VMX_PROCBASED_CTLS_DEFAULT1 , host_data); + +if ( msr ==MSR_IA32_VMX_TRUE_PROCBASED_CTLS ) + default1_bits &= ~(CPU_BASED_CR3_LOAD_EXITING | +CPU_BASED_CR3_STORE_EXITING |CPU_BASED_INVLPG_EXITING ); + + data =gen_vmx_msr (data, default1_bits, host_data);break ; + }case MSR_IA32_VMX_PROCBASED_CTLS2 :/* 1-seetings */ data =SECONDARY_EXEC_DESCRIPTOR_TABLE_EXITING | -SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES ; +SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | +SECONDARY_EXEC_ENABLE_VPID | +SECONDARY_EXEC_ENABLE_EPT ; data =gen_vmx_msr (data,0 , host_data);break ;case MSR_IA32_VMX_EXIT_CTLS :
@@ -1563,6 +1573,9 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content)case MSR_IA32_VMX_MISC :gdprintk (XENLOG_WARNING ,"VMX MSR %x not fully supported yet.\n" , msr);break ; +case MSR_IA32_VMX_EPT_VPID_CAP : + data =nept_get_ept_vpid_cap (); +break ;default : r =0 ;break ;
xen/arch/x86/mm/hap/nested_ept.c +12 -7
@@ -43,12 +43,15 @@#define EPT_MUST_RSV_BITS (((1ull << PADDR_BITS) -1) & \ ~((1ull << paddr_bits) - 1)) -/* -*TODO: Just leave it as 0 here for compile pass, will -* define real capabilities in the subsequent patches. -*/ -#define NEPT_VPID_CAP_BITS 0 - +#define NEPT_VPID_CAP_BITS \ + (VMX_EPT_INVEPT_ALL_CONTEXT | VMX_EPT_INVEPT_SINGLE_CONTEXT | \ + VMX_EPT_INVEPT_INSTRUCTION | VMX_EPT_SUPERPAGE_1GB | \ + VMX_EPT_SUPERPAGE_2MB | VMX_EPT_MEMORY_TYPE_WB | \ + VMX_EPT_MEMORY_TYPE_UC | VMX_EPT_WALK_LENGTH_4_SUPPORTED | \ + VMX_EPT_EXEC_ONLY_SUPPORTED | VMX_VPID_INVVPID_INSTRUCTION | \ + VMX_VPID_INVVPID_INDIVIDUAL_ADDR | \ + VMX_VPID_INVVPID_SINGLE_CONTEXT | VMX_VPID_INVVPID_ALL_CONTEXT |\ + VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL)#define NEPT_1G_ENTRY_FLAG (1 << 11)#define NEPT_2M_ENTRY_FLAG (1 << 10)
@@ -131,7 +134,9 @@ static bool_t nept_non_present_check(ept_entry_t e)uint64_t nept_get_ept_vpid_cap (void ) { -return NEPT_VPID_CAP_BITS ; +if ( cpu_has_vmx_ept && cpu_has_vmx_vpid ) +return NEPT_VPID_CAP_BITS ; +return 0 ; }static int ept_lvl_table_offset (unsigned long gpa,int lvl)
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -0
@@ -209,6 +209,8 @@ u64 nvmx_get_tsc_offset(struct vcpu *v);int nvmx_n2_vmexit_handler (struct cpu_user_regs *regs,unsigned int exit_reason); +uint64_t nept_get_ept_vpid_cap (void ); + int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, unsigned int *page_order, uint32_t rwx_acc, unsigned long *l1gfn, uint8_t *p2m_acc,
Signature · 1 line
1.7.1