x86/mm: Add mem access rights to NPT
PATCH v4 1/1 x86/mm: Add mem access rights to NPT
From: Isaila Alexandru <[email protected]>
This patch adds access control for NPT mode.
There aren’t enough extra bits to store the access rights in the NPT p2m table, so we add a radix tree to store the rights. For efficiency, remove entries which match the default permissions rather than continuing to store them.
Modify p2m-pt.c:p2m_type_to_flags() to mirror the ept version: taking an access, and removing / adding RW or NX flags as appropriate.
Note: It was tested with xen-access write
---
Changes since V3:
- Add p2m_pt_check_access() to filter n, w, wx, n2rwx from supported page rights
- Add rights check for the default_access change in the IVALID_GFN case
- Add blank lines
- Remove cpu_has_svm if from p2m_mem_access_check()
- Add xfree(msr_bitmap) in case of error on xalloc(raxid_tree_root).
--- xen/arch/x86/mm/mem_access.c | 17 +++--- xen/arch/x86/mm/p2m-ept.c | 7 +++ xen/arch/x86/mm/p2m-pt.c | 124 ++++++++++++++++++++++++++++++++++----- xen/arch/x86/mm/p2m.c | 6 ++ xen/arch/x86/monitor.c | 15 +++++ xen/include/asm-x86/mem_access.h | 2 +- xen/include/asm-x86/p2m.h | 7 +++ 7 files changed, 156 insertions(+), 22 deletions(-)
xen/arch/x86/mm/mem_access.c +10 -7
@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, { req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; - -if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; +else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; req->u.mem_access.flags |= npfec.read_access ?MEM_ACCESS_R :0 ; req->u.mem_access.flags |= npfec.write_access ?MEM_ACCESS_W :0 ; req->u.mem_access.flags |= npfec.insn_fetch ?MEM_ACCESS_X :0 ;
@@ -366,8 +366,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,/* If request to set default access. */ if (gfn_eq (gfn,INVALID_GFN ) ) { - p2m->default_access = a; -return 0 ; +if ( (rc = p2m->check_access (a)) ==0 ) + { + p2m->default_access = a; +return 0 ; + } }p2m_lock (p2m);
xen/arch/x86/mm/p2m-ept.c +7 -0
@@ -667,6 +667,12 @@ bool_t ept_handle_misconfig(uint64_t gpa)return spurious ? (rc >=0 ) : (rc >0 ); } +int ept_check_access (p2m_access_t p2ma) +{ +/* All access is permitted */ +return 0 ; +} +/* * ept_set_entry() computes 'need_modify_vtd_table' for itself, * by observing whether any gfn->mfn translations are modified.
@@ -1255,6 +1261,7 @@ int ept_p2m_init(struct p2m_domain *p2m) p2m->change_entry_type_global = ept_change_entry_type_global; p2m->change_entry_type_range = ept_change_entry_type_range; p2m->memory_type_changed = ept_memory_type_changed; + p2m->check_access = ept_check_access; p2m->audit_p2m =NULL ; p2m->tlb_flush = ept_tlb_flush;
xen/arch/x86/mm/p2m-pt.c +110 -14
263 lines not on this page · raw message
xen/arch/x86/mm/p2m.c +6 -0
@@ -675,6 +675,12 @@ void p2m_teardown(struct p2m_domain *p2m) d = p2m->domain; +if ( p2m->mem_access_settings ) + { +radix_tree_destroy (p2m->mem_access_settings,NULL ); +xfree (p2m->mem_access_settings); + } +p2m_lock (p2m);ASSERT (atomic_read (&d->shr_pages) ==0 ); p2m->phys_table =pagetable_null ();
xen/arch/x86/monitor.c +15 -0
@@ -20,10 +20,13 @@*/ #include <asm/monitor.h> +#include <asm/p2m.h> #include <public/vm_event.h> int arch_monitor_init_domain (struct domain *d) { +struct p2m_domain *p2m =p2m_get_hostp2m (d); +if ( !d->arch.monitor.msr_bitmap ) d->arch.monitor.msr_bitmap = xzalloc_array(struct monitor_msr_bitmap,2 );
@@ -31,6 +34,18 @@ int arch_monitor_init_domain(struct domain *d)if ( !d->arch.monitor.msr_bitmap )return -ENOMEM ; +if ( cpu_has_svm && !p2m->mem_access_settings ) + { + p2m->mem_access_settings = xmalloc(struct radix_tree_root); + +if ( !p2m->mem_access_settings ) + { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + } +return 0 ; }
xen/include/asm-x86/mem_access.h +1 -1
@@ -46,7 +46,7 @@ bool p2m_mem_access_emulate_check(struct vcpu *v,/* Sanity check for mem_access hardware support */ static inline bool p2m_mem_access_sanity_check (struct domain *d) { -return is_hvm_domain (d) && cpu_has_vmx &&hap_enabled (d); +return is_hvm_domain (d) &&hap_enabled (d); }#endif /*__ASM_X86_MEM_ACCESS_H__ */
xen/include/asm-x86/p2m.h +7 -0
@@ -266,6 +266,7 @@ struct p2m_domain { unsigned long gfn, l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);long (*audit_p2m)(struct p2m_domain *p2m); +int (*check_access)(p2m_access_t p2ma);/* * P2M updates may require TLBs to be flushed (invalidated).
@@ -288,6 +289,12 @@ struct p2m_domain {* retyped get this access type. See definition of p2m_access_t. */ p2m_access_t default_access; +/* +* Radix tree to store the p2m_access_t settings as the pte's don't have +* enough available bits to store this information. +*/ +struct radix_tree_root *mem_access_settings; +/* If true, and an access fault comes in and there is no vm_event listener, * pause domain. Otherwise, remove access restrictions. */ bool_t access_required;
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
xen/arch/x86/mm/mem_access.c +0
-1@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, { req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; -
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, · line 229
Quoted 10 lines
xen/arch/x86/mm/mem_access.c +5
-4-if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; +else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ;
Without explanation in the commit message and without comment this change is a no-go imo: I consider it at least questionable to have npfec_kind_with_gla without .gla_valid set to true. Perhaps it's just a naming issue, but that would then still require half a sentence to explain.
xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 374
Quoted 12 lines
xen/arch/x86/mm/mem_access.c +5
-2@@ -366,8 +366,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,/* If request to set default access. */ if (gfn_eq (gfn,INVALID_GFN ) ) { - p2m->default_access = a; -return 0 ; +if ( (rc = p2m->check_access (a)) ==0 ) + { + p2m->default_access = a; +return 0 ; + } }
This latching into rc makes subsequent code yield inconsistent behavior.
xen/arch/x86/mm/p2m-ept.c bool_t ept_handle_misconfig(uint64_t gpa) · line 674
Quoted 11 lines
xen/arch/x86/mm/p2m-ept.c +5
-0@@ -667,6 +667,12 @@ bool_t ept_handle_misconfig(uint64_t gpa)return spurious ? (rc >=0 ) : (rc >0 ); } +int ept_check_access (p2m_access_t p2ma) +{ +/* All access is permitted */ +return 0 ; +}
With this I'd rather see the hook omitted here, to avoid the indirect call. Perhaps you'll want a wrapper around the indirect call, abstracting away the NULL check for callers.
xen/arch/x86/mm/p2m-ept.c bool_t ept_handle_misconfig(uint64_t gpa)
Quoted 7 lines
xen/arch/x86/mm/p2m-ept.c +7
-0+static void p2m_set_access (struct p2m_domain *p2m,unsigned long gfn, +p2m_access_t a) +{ +int rc; + +if ( !p2m->mem_access_settings ) +return ;
No error indication?
xen/arch/x86/mm/p2m-ept.c
Quoted 5 lines
xen/arch/x86/mm/p2m-ept.c +5
-0+if ( p2m_access_rwx == a ) + { +radix_tree_delete (p2m->mem_access_settings, gfn); +return ; + }
Is it really p2m_access_rwx that you want to special case here, rather than ->default_access?
xen/arch/x86/mm/p2m-ept.c int arch_monitor_init_domain(struct domain *d) · line 41
Quoted 9 lines
xen/arch/x86/mm/p2m-ept.c +5
-0@@ -31,6 +34,18 @@ int arch_monitor_init_domain(struct domain *d)if ( !d->arch.monitor.msr_bitmap )return -ENOMEM ; +if ( cpu_has_svm && !p2m->mem_access_settings ) + { + p2m->mem_access_settings = xmalloc(struct radix_tree_root); + +if ( !p2m->mem_access_settings )
Style.
xen/arch/x86/mm/p2m-ept.c int arch_monitor_init_domain(struct domain *d) · line 47
Quoted 6 lines
xen/arch/x86/mm/p2m-ept.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }
What's the SVM connection here? Please don't forget that p2m-pt.c also serves the shadow case. Perhaps struct p2m_domain should contain a boolean indicator whether this auxiliary data structure is needed?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 13 lines · Jan Beulich
xen/arch/x86/monitor.c int arch_monitor_init_domain(struct domain *d) · line 41
Quoted 5 lines
xen/arch/x86/monitor.c +5
-0+if ( cpu_has_svm && !p2m->mem_access_settings ) + { + p2m->mem_access_settings = xmalloc(struct radix_tree_root); + +if ( !p2m->mem_access_settings )Style.
xen/arch/x86/monitor.c int arch_monitor_init_domain(struct domain *d) · line 47
Quoted 6 lines
xen/arch/x86/monitor.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }
Quoted 4 lines
What's the SVM connection here? Please don't forget that
p2m-pt.calso serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is needed?
Would it not work to simply check for "if ( cpu_has_svm && p2m_is_hostp2m(p2m) && !p2m->mem_access_settings )" here?
In the shadow case, will not p2m->p2m_class be p2m_nested?
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 24.07.18 at 11:28, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
xen/arch/x86/monitor.c int arch_monitor_init_domain(struct domain *d) · line 41
Quoted 5 lines
xen/arch/x86/monitor.c +5
-0+if ( cpu_has_svm && !p2m->mem_access_settings ) + { + p2m->mem_access_settings = xmalloc(struct radix_tree_root); + +if ( !p2m->mem_access_settings )Style.
xen/arch/x86/monitor.c int arch_monitor_init_domain(struct domain *d) · line 47
Quoted 6 lines
xen/arch/x86/monitor.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }Quoted 4 lines
What's the SVM connection here? Please don't forget that
p2m-pt.calso serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is needed?
Quoted 4 lines
Would it not work to simply check for "if (
cpu_has_svm&&p2m_is_hostp2m(p2m)&& !p2m->mem_access_settings )" here?In the shadow case, will not
p2m->p2m_classbep2m_nested?
Maybe, but that wasn't the point of my remark. I want to get rid of the cpu_has_svm here, not have it amended.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
xen/arch/x86/mm/mem_access.c +3
-5@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, { req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; - -if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ;
Quoted 8 lines
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, · line 229
Quoted 2 lines
xen/arch/x86/mm/mem_access.c +2
-0+else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ;Without explanation in the commit message and without comment this change is a no-go imo: I consider it at least questionable to have
npfec_kind_with_glawithout .gla_valid set to true. Perhaps it's just a naming issue, but that would then still require half a sentence to explain.
The naming here is confusing, but it seems to be based on the AMD manual naming convention (IIRC from my skim through the manual last week). "With gla" in this context means, "The nested fault happend while trying to translate the final guest linear address of the access", as opposed to "The nested fault happend while trying to translate one of the page tables, before the guest linear address for the virtual address could be calculated." It's a perfectly valid setting on AMD box, in spite of the fact that AMD doesn't report the virt -> gla translation.
I'd be in favor of renaming the variable, but that shouldn't be Alexandru's job.
What about adding a comment like this:
"Naming is confusing here; 'with_gla' simply means the fault happened as the result of a translating the final gla, as opposed to translating one of the pagetables."
[snip]
Quoted 11 lines
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla,
Quoted 6 lines
xen/arch/x86/mm/mem_access.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }What's the SVM connection here? Please don't forget that
p2m-pt.calso serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is needed?
It's basically just "hap_enabled()" isn't it?
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 24.07.18 at 13:26, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
xen/arch/x86/mm/mem_access.c +0
-0@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned longgla,
Quoted 12 lines
Quoted 12 lines
xen/arch/x86/mm/mem_access.c +3
-5{ req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; - -if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ;Quoted 8 lines
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, · line 229
Quoted 2 lines
xen/arch/x86/mm/mem_access.c +2
-0+else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ;Without explanation in the commit message and without comment this change is a no-go imo: I consider it at least questionable to have
npfec_kind_with_glawithout .gla_valid set to true. Perhaps it's just a naming issue, but that would then still require half a sentence to explain.The naming here is confusing, but it seems to be based on the AMD manual naming convention (IIRC from my skim through the manual last week). "With gla" in this context means, "The nested fault happend while trying to translate the final guest linear address of the access", as opposed to "The nested fault happend while trying to translate one of the page tables, before the guest linear address for the virtual address could be calculated." It's a perfectly valid setting on AMD box, in spite of the fact that AMD doesn't report the virt -> gla translation.
Quoted 8 lines
I'd be in favor of renaming the variable, but that shouldn't be Alexandru's job.
What about adding a comment like this:
"Naming is confusing here; '
with_gla' simply means the fault happened as the result of a translating the final gla, as opposed to translating one of the pagetables."
Yes, that would clarify thnigs enough, I think.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
xen/arch/x86/mm/mem_access.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }
Quoted 6 lines
Quoted 4 lines
What's the SVM connection here? Please don't forget that
p2m-pt.calso serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is needed?It's basically just "
hap_enabled()" isn't it?
Only if we can't make it there when EPT is active.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c static void p2m_add_iommu_flags(l1_pgentry_t *p2m_entry, · line 226
Quoted 9 lines
+static void p2m_set_access(struct
p2m_domain*p2m, unsigned long gfn,
+ p2m_access_t a)xen/arch/x86/mm/p2m-pt.c +5
-0+{ + int rc; + + if ( !p2m->mem_access_settings ) + return;No error indication?
I would say ASSERT is a better choice if the code got this far and it could not allocate memory
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 12 lines
Quoted 11 lines
xen/arch/x86/mm/p2m-pt.c static void p2m_add_iommu_flags(l1_pgentry_t *p2m_entry, · line 226
Quoted 10 lines · On 25.07.18 at 10:29, <[email protected]> wrote:
+static void p2m_set_access(struct
p2m_domain*p2m, unsigned long gfn,xen/arch/x86/mm/p2m-pt.c +6
-0+ p2m_access_t a) +{ +int rc; + +if ( !p2m->mem_access_settings ) +return ;No error indication?
Quoted 2 lines
I would say ASSERT is a better choice if the code got this far and it could not allocate memory
For one ASSERT() is a no-op in release builds. And then it is extremely bad practices to bring down the host when an operation targeting just a single guest has failed. You either return an error indicator here (and pass it up the call tree), or if that's really unfeasible then you crash the affected domain (we do so in quite a few other situations). But you'd need to make clear (if it's not obvious) why passing up an error is unacceptable here.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 55 lines · Jan Beulich
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 24.07.18 at 13:26, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
xen/arch/x86/mm/mem_access.c +0
-0@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned longgla,
Quoted 12 lines
Quoted 12 lines
xen/arch/x86/mm/mem_access.c +3
-5{ req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; - -if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ;Quoted 8 lines
xen/arch/x86/mm/mem_access.c bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, · line 229
Quoted 2 lines
xen/arch/x86/mm/mem_access.c +2
-0+else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ;Without explanation in the commit message and without comment this change is a no-go imo: I consider it at least questionable to have
npfec_kind_with_glawithout .gla_valid set to true. Perhaps it's just a naming issue, but that would then still require half a sentence to explain.The naming here is confusing, but it seems to be based on the AMD manual naming convention (IIRC from my skim through the manual last week). "With gla" in this context means, "The nested fault happend while trying to translate the final guest linear address of the access", as opposed to "The nested fault happend while trying to translate one of the page tables, before the guest linear address for the virtual address could be calculated." It's a perfectly valid setting on AMD box, in spite of the fact that AMD doesn't report the virt -> gla translation.
Quoted 8 lines
I'd be in favor of renaming the variable, but that shouldn't be Alexandru's job.
What about adding a comment like this:
"Naming is confusing here; '
with_gla' simply means the fault happened as the result of a translating the final gla, as opposed to translating one of the pagetables."Yes, that would clarify thnigs enough, I think.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
xen/arch/x86/mm/mem_access.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
What's the SVM connection here? Please don't forget that
p2m-pt.calso serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is needed?It's basically just "
hap_enabled()" isn't it?Only if we can't make it there when EPT is active.
It can make it here when VMX is active and shadow is enabled, but it shouldn't be able to get here when EPT is active. We could add an ASSERT() to that effect; it should be safe in production, as the only side effect should be that we do a small pointless allocation.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 18 lines · Jan Beulich
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines · On 25.07.18 at 10:29, <[email protected]> wrote:
Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c static void p2m_add_iommu_flags(l1_pgentry_t *p2m_entry, · line 226
Quoted 9 lines
+static void p2m_set_access(struct
p2m_domain*p2m, unsigned long
gfn,
+ p2m_access_t a)xen/arch/x86/mm/p2m-pt.c +5
-0+{ + int rc; + + if ( !p2m->mem_access_settings ) + return;No error indication?
Quoted 3 lines
I would say ASSERT is a better choice if the code got this far and it
could not allocate memory
Quoted 8 lines
For one
ASSERT()is a no-op in release builds. And then it is extremely bad practices to bring down the host when an operation targeting just a single guest has failed. You either return an error indicator here (and pass it up the call tree), or if that's really unfeasible then you crash the affected domain (we do so in quite a few other situations). But you'd need to make clear (if it's not obvious) why passing up an error is unacceptable here.
By this time in the code the radix tree should be in place. If it is not then the domain should crash because something is wrong and the mem access feature will not function so passing the error up will have a result of crashing the domain later after checking.
I will add a domain crash here and a comment regarding it.
Thanks,
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 25.07.18 at 11:25, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 24.07.18 at 13:26, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/arch/x86/monitor.c int arch_monitor_init_domain(struct domain *d) · line 47
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
xen/arch/x86/monitor.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }What's the SVM connection here? Please don't forget that
p2m-pt.calso serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is needed?
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
It's basically just "
hap_enabled()" isn't it?Only if we can't make it there when EPT is active.
It can make it here when VMX is active and shadow is enabled, but it shouldn't be able to get here when EPT is active. We could add an
ASSERT()to that effect; it should be safe in production, as the only side effect should be that we do a small pointless allocation.
So I've looked a little more closely: This is being added to arch_monitor_init_domain(), called from vm_event_domctl(). I can't see why this wouldn't be reachable with EPT enabled.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 36 lines · Jan Beulich
Quoted 28 lines
Quoted 4 lines
Quoted 4 lines · On 25.07.18 at 11:25, <[email protected]> wrote:
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 5 lines
Quoted 5 lines · On 24.07.18 at 13:26, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 11 lines
Quoted 5 lines
Quoted 5 lines · On 23.07.18 at 15:48, <[email protected]> wrote:
+ {
+ xfree(d->arch.monitor.msr_bitmap); + return-ENOMEM;
+ }
+ radix_tree_init(p2m->mem_access_settings); + }What's the SVM connection here? Please don't forget that p2m-
pt.c
also serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is
needed?Quoted 8 lines
Quoted 2 lines
Quoted 1 line
It's basically just "
hap_enabled()" isn't it?Only if we can't make it there when EPT is active.
It can make it here when VMX is active and shadow is enabled, but it
shouldn't be able to get here when EPT is active. We could add anASSERT()to that effect; it should be safe in production, as the only
side effect should be that we do a small pointless allocation.
Quoted 4 lines
So I've looked a little more closely: This is being added to
arch_monitor_init_domain(), called fromvm_event_domctl(). I can't see why this wouldn't be reachable with EPT enabled.
Hi George,
Any thoughts on this?
Thanks,
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 33 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 25.07.18 at 11:25, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 24.07.18 at 13:26, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
xen/arch/x86/monitor.c int arch_monitor_init_domain(struct domain *d) · line 47
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
xen/arch/x86/monitor.c +6
-0+ { +xfree (d->arch.monitor.msr_bitmap); +return -ENOMEM ; + } +radix_tree_init (p2m->mem_access_settings); + }Quoted 6 lines
What's the SVM connection here? Please don't forget that p2m-
pt.c
also serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is
needed?It's basically just "
hap_enabled()" isn't it?Quoted 8 lines
Quoted 1 line
Only if we can't make it there when EPT is active.
It can make it here when VMX is active and shadow is enabled, but it
shouldn't be able to get here when EPT is active. We could add anASSERT()to that effect; it should be safe in production, as the only
side effect should be that we do a small pointless allocation.
Quoted 4 lines
So I've looked a little more closely: This is being added to
arch_monitor_init_domain(), called fromvm_event_domctl(). I can't see why this wouldn't be reachable with EPT enabled.
Hi George,
Any input on this?
Regards,
Alex
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 34 lines · Isaila Alexandru
Quoted 33 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 25.07.18 at 11:25, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 24.07.18 at 13:26, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Review
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 23.07.18 at 15:48, <[email protected]> wrote:
(quoted diff) +6
-0+ { + xfree(d->arch.monitor.msr_bitmap); + return -ENOMEM; + } + radix_tree_init(p2m->mem_access_settings); + }What's the SVM connection here? Please don't forget that p2m-
pt.c
also serves the shadow case. Perhaps structp2m_domainshould contain a boolean indicator whether this auxiliary data structure is
needed?Quoted 3 lines
Quoted 1 line
It's basically just "
hap_enabled()" isn't it?Only if we can't make it there when EPT is active.
It can make it here when VMX is active and shadow is enabled, but it
shouldn't be able to get here when EPT is active. We could add anASSERT()to that effect; it should be safe in production, as the only
side effect should be that we do a small pointless allocation.
Quoted 7 lines
Quoted 4 lines
So I've looked a little more closely: This is being added to
arch_monitor_init_domain(), called fromvm_event_domctl(). I can't see why this wouldn't be reachable with EPT enabled.Hi George,
Any input on this?
Sorry, you're still waiting for me to weigh in on whether you can get to arch_monitor_init_domain() with EPT enabled?
The obvious answer is 'yes'; I hope you don't need me to tell you that. :-)
Going back through this conversation, I'm not 100% clear what I meant with my "hap_enabled()" comment -- my best guess is that I was suggesting the check be ( cpu_has_svm || !hap_enabled() ).
But in any case, on the whole patch, I think that:
1) The feature is not abstracted well enough. mem_access.c should not have to know whether additional storage is required or not; that should be all taken care of within p2m-pt.c (or p2m-ept.c, should that ever become necessary).
2) We've been talking for a long time about having a place to store additional per-pfn information; it would be good if this mechanism were made general enough to be used for other types of storage.
Let me play around with what you have and see if I can get a mock-up of something like what I'm looking for.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 61 lines · Alexandru Isaila
From: Isaila Alexandru <[email protected]>
This patch adds access control for NPT mode.
There aren’t enough extra bits to store the access rights in the NPT p2m table, so we add a radix tree to store the rights. For efficiency, remove entries which match the default permissions rather than continuing to store them.
Modify p2m-pt.c:p2m_type_to_flags() to mirror the ept version: taking an access, and removing / adding RW or NX flags as appropriate.
Note: It was tested with xen-access write
Signed-off-by Alexandru Isaila <[email protected]>---
Changes since V3:
- Addp2m_pt_check_access()to filter n, w, wx, n2rwx from supported page rights
- Add rights check for thedefault_accesschange in theIVALID_GFNcase
- Add blank lines
- Removecpu_has_svmif fromp2m_mem_access_check()
- Addxfree(msr_bitmap)in case of error onxalloc(raxid_tree_root).--- xen/arch/x86/mm/mem_access.c | 17 +++--- xen/arch/x86/mm/p2m-ept.c | 7 +++ xen/arch/x86/mm/p2m-pt.c | 124 ++++++++++++++++++++++++++++++++++----- xen/arch/x86/mm/p2m.c | 6 ++ xen/arch/x86/monitor.c | 15 +++++ xen/include/asm-x86/mem_access.h | 2 +- xen/include/asm-x86/p2m.h | 7 +++ 7 files changed, 156 insertions(+), 22 deletions(-)xen/arch/x86/mm/mem_access.c +5
-5@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, { req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; - -if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; +else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; req->u.mem_access.flags |= npfec.read_access ?MEM_ACCESS_R :0 ; req->u.mem_access.flags |= npfec.write_access ?MEM_ACCESS_W :0 ; req->u.mem_access.flags |= npfec.insn_fetch ?MEM_ACCESS_X :0 ;@@ -366,8 +366,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,/* If request to set default access. */
xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 374
Quoted 10 lines
xen/arch/x86/mm/mem_access.c +5
-2if (gfn_eq (gfn,INVALID_GFN ) ) { - p2m->default_access = a; -return 0 ; +if ( (rc = p2m->check_access (a)) ==0 ) + { + p2m->default_access = a; +return 0 ; + } }
BTW this introduces a bug -- if the check fails, this will fall through into the gfn loop below, rather than returning the error.
xen/arch/x86/mm/mem_access.c static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m, · line 92
Quoted 7 lines
xen/arch/x86/mm/mem_access.c +2
-1@@ -87,23 +88,27 @@ static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m,case p2m_ram_paged:case p2m_ram_paging_in:default : -return flags | _PAGE_NX_BIT; + flags |=P2M_BASE_FLAGS | _PAGE_NX_BIT; +break ;
Why did you add in P2M_BASE_FLAGS here?
xen/arch/x86/mm/mem_access.c static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m, · line 94
Quoted 2 lines
xen/arch/x86/mm/mem_access.c +0
-0case p2m_grant_map_ro:return flags |P2M_BASE_FLAGS | _PAGE_NX_BIT;
And is this return left here on purpose, or was it missed?
xen/arch/x86/mm/mem_access.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 271
Quoted 8 lines
xen/arch/x86/mm/mem_access.c +1
-0/* Returns: 0 for success, -errno for failure */ static int p2m_next_level(struct p2m_domain *p2m, void **table,@@ -201,6 +268,7 @@ p2m_next_level(struct p2m_domain *p2m, void **table, new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); +p2m_set_access (p2m, gfn, p2m->default_access);
This is clearly wrong -- this isn't a leaf node, it's an intermediate p2m table; and p2m_next_level() is just acting "under the covers", filling in missing bits of the p2m table or breaking down superpages. Since the access information is in a completely separate structure, it shouldn't need to be modified here at all (indeed, it would be a bug to do so).
But that does bring up an important issue -- it would appear that this code is incorrect when setting superpages -- if we set a 2MiB page but then read a non-2MiB-aligned entry within that page, we'll get the default access rather than the one we set; same thing with splintering a superpage into smaller pages.
There's a draft patch addressing this on the way.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
[Resending]
Quoted 115 lines · George Dunlap
Quoted 61 lines · Alexandru Isaila
From: Isaila Alexandru <[email protected]>
This patch adds access control for NPT mode.
There aren’t enough extra bits to store the access rights in the NPT p2m table, so we add a radix tree to store the rights. For efficiency, remove entries which match the default permissions rather than continuing to store them.
Modify p2m-pt.c:p2m_type_to_flags() to mirror the ept version: taking an access, and removing / adding RW or NX flags as appropriate.
Note: It was tested with xen-access write
Signed-off-by Alexandru Isaila <[email protected]>---
Changes since V3:
- Addp2m_pt_check_access()to filter n, w, wx, n2rwx from supported page rights
- Add rights check for thedefault_accesschange in theIVALID_GFNcase
- Add blank lines
- Removecpu_has_svmif fromp2m_mem_access_check()
- Addxfree(msr_bitmap)in case of error onxalloc(raxid_tree_root).--- xen/arch/x86/mm/mem_access.c | 17 +++--- xen/arch/x86/mm/p2m-ept.c | 7 +++ xen/arch/x86/mm/p2m-pt.c | 124 ++++++++++++++++++++++++++++++++++----- xen/arch/x86/mm/p2m.c | 6 ++ xen/arch/x86/monitor.c | 15 +++++ xen/include/asm-x86/mem_access.h | 2 +- xen/include/asm-x86/p2m.h | 7 +++ 7 files changed, 156 insertions(+), 22 deletions(-)xen/arch/x86/mm/mem_access.c +5
-5@@ -221,12 +221,12 @@ bool p2m_mem_access_check(paddr_t gpa, unsigned long gla, { req->u.mem_access.flags |=MEM_ACCESS_GLA_VALID ; req->u.mem_access.gla = gla; - -if ( npfec.kind == npfec_kind_with_gla ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; -else if ( npfec.kind == npfec_kind_in_gpt ) - req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; } + +if ( npfec.kind == npfec_kind_with_gla ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_WITH_GLA ; +else if ( npfec.kind == npfec_kind_in_gpt ) + req->u.mem_access.flags |=MEM_ACCESS_FAULT_IN_GPT ; req->u.mem_access.flags |= npfec.read_access ?MEM_ACCESS_R :0 ; req->u.mem_access.flags |= npfec.write_access ?MEM_ACCESS_W :0 ; req->u.mem_access.flags |= npfec.insn_fetch ?MEM_ACCESS_X :0 ;@@ -366,8 +366,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,/* If request to set default access. */ xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 374
Quoted 10 lines
xen/arch/x86/mm/mem_access.c +5
-2if (gfn_eq (gfn,INVALID_GFN ) ) { - p2m->default_access = a; -return 0 ; +if ( (rc = p2m->check_access (a)) ==0 ) + { + p2m->default_access = a; +return 0 ; + } }BTW this introduces a bug -- if the check fails, this will fall through into the gfn loop below, rather than returning the error.
xen/arch/x86/mm/mem_access.c static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m, · line 92
Quoted 7 lines
xen/arch/x86/mm/mem_access.c +2
-1@@ -87,23 +88,27 @@ static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m,case p2m_ram_paged:case p2m_ram_paging_in:default : -return flags | _PAGE_NX_BIT; + flags |=P2M_BASE_FLAGS | _PAGE_NX_BIT; +break ;Why did you add in
P2M_BASE_FLAGShere?xen/arch/x86/mm/mem_access.c static unsigned long p2m_type_to_flags(const struct p2m_domain *p2m, · line 94
Quoted 2 lines
xen/arch/x86/mm/mem_access.c +0
-0case p2m_grant_map_ro:return flags |P2M_BASE_FLAGS | _PAGE_NX_BIT;And is this
returnleft here on purpose, or was it missed?xen/arch/x86/mm/mem_access.c p2m_next_level(struct p2m_domain *p2m, void **table, · line 271
Quoted 8 lines
xen/arch/x86/mm/mem_access.c +1
-0/* Returns: 0 for success, -errno for failure */ static int p2m_next_level(struct p2m_domain *p2m, void **table,@@ -201,6 +268,7 @@ p2m_next_level(struct p2m_domain *p2m, void **table, new_entry =l1e_from_mfn (mfn,P2M_BASE_FLAGS | _PAGE_RW);p2m_add_iommu_flags (&new_entry, level, IOMMUF_readable|IOMMUF_writable); +p2m_set_access (p2m, gfn, p2m->default_access);This is clearly wrong -- this isn't a leaf node, it's an intermediate p2m table; and
p2m_next_level()is just acting "under the covers", filling in missing bits of the p2m table or breaking down superpages. Since the access information is in a completely separate structure, it shouldn't need to be modified here at all (indeed, it would be a bug to do so).But that does bring up an important issue -- it would appear that this code is incorrect when setting superpages -- if we set a 2MiB page but then read a non-2MiB-aligned entry within that page, we'll get the default access rather than the one we set; same thing with splintering a superpage into smaller pages.
Quoted 3 lines
There's a draft patch addressing this on the way.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel