add new p2m type class and new p2m type
v5 cover add new p2m type class and new p2m type
XenGT (Intel Graphics Virtualization technology, please refer to
https://01.org/xen/blogs/srclarkx/2013/graphics-virtualization-
xengt) driver runs inside Dom0 as a virtual graphics device model, and needs to trap and emulate the guest's write operations to some specific memory pages, like memory pages used by guest graphics driver as PPGTT(per-process graphics translation table). We added a new p2m type, p2m_mmio_write_dm, to trap and emulate the write operations on these graphic page tables.
Handling of this new p2m type are similar with existing p2m_ram_ro in most condition checks, with only difference on final policy of emulation vs. drop. For p2m_ram_ro types, write operations will not trigger the device model, and will be discarded later in __hvm_copy(); while for the p2m_mmio_write_dm type pages, writes will go to the device model via ioreq-server.
Previously, the conclusion in our v3 patch review is to provide a more generalized HVMOP_map_io_range_to_ioreq_server hypercall, by seperating rangesets inside a ioreq server to read-protected/write- protected/both-prtected. Yet, after offline discussion with Paul, we believe a more simplified solution may suffice. We can keep the existing HVMOP_map_io_range_to_ioreq_server hypercall, and let the user decide whether or not a p2m type change is necessary, because in most cases the emulator will already use the p2m_mmio_dm type.
Changes from v4:
- A new p2m type class, P2M_DISCARD_WRITE_TYPES, is added;
- A new predicate, p2m_is_discard_write, is used in __hvm_copy()/ __hvm_clear()/emulate_gva_to_mfn()/hvm_hap_nested_page_fault(), to discard the write operations;
- The new p2m type, p2m_mmio_write_dm, is added to P2M_RO_TYPES;
- Coding style changes;
Changes from v3:
- Use the existing HVMOP_map_io_range_to_ioreq_server hypercall to add write protected range;
- Modify the HVMOP_set_mem_type hypercall to support the new p2m type for this range.
Changes from v2:
- Remove excute attribute of the new p2m type p2m_mmio_write_dm;
- Use existing rangeset for keeping the write protection page range instead of introducing hash table;
- Some code style fix.
Changes from v1:
- Changes the new p2m type name from p2m_ram_wp to p2m_mmio_write_dm. This means that we treat the pages as a special mmio range instead of ram;
- Move macros to c file since only this file is using them.
- Address various comments from Jan.
Yu Zhang (2):
Add a new p2m type class - P2M_DISCARD_WRITE_TYPES add a new p2m type - p2m_mmio_write_dm
xen/arch/x86/hvm/hvm.c | 25 ++++++++++--------------- xen/arch/x86/mm/p2m-ept.c | 1 + xen/arch/x86/mm/p2m-pt.c | 1 + xen/arch/x86/mm/shadow/multi.c | 2 +- xen/include/asm-x86/p2m.h | 9 ++++++++- xen/include/public/hvm/hvm_op.h | 1 + 6 files changed, 22 insertions(+), 17 deletions(-)
Signature · 1 line
1.9.1
PATCH v5 1/2 add a new p2m type class - P2M_DISCARD_WRITE_TYPES
From: Yu Zhang <[email protected]>
Currently, the P2M_RO_TYPES bears 2 meanings: one is "_PAGE_RW bit is clear in their PTEs", and another is to discard the write operations on these pages. This patch adds a p2m type class, P2M_DISCARD_WRITE_TYPES, to bear the second meaning, so we can use this type class instead of the P2M_RO_TYPES, to decide if a write operation is to be ignored.
--- xen/arch/x86/hvm/hvm.c | 16 +++------------- xen/arch/x86/mm/shadow/multi.c | 2 +- xen/include/asm-x86/p2m.h | 5 +++++ 3 files changed, 9 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -13
@@ -2837,7 +2837,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,* to the mmio handler. */ if ( (p2mt == p2m_mmio_dm) || - (npfec.write_access && (p2mt == p2m_ram_ro)) ) + (npfec.write_access && (p2m_is_discard_write (p2mt))) ) {put_gfn (p2m->domain, gfn);
@@ -2882,16 +2882,6 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, goto out_put_gfn; } -/* Shouldn't happen: Maybe the guest was writing to a r/o grant mapping? */ -if ( npfec.write_access && (p2mt == p2m_grant_map_ro) ) - { -gdprintk (XENLOG_WARNING , -"trying to write to read-only grant mapping\n" ); -hvm_inject_hw_exception (TRAP_gp_fault,0 ); - rc =1 ; - goto out_put_gfn; - } -/* If we fell through, the vcpu will retry now that access restrictions have * been removed. It may fault again if the p2m entry type still requires so. * Otherwise, this is an error condition. */
@@ -3941,7 +3931,7 @@ static enum hvm_copy_result __hvm_copy(if ( flags & HVMCOPY_to_guest ) { -if ( p2mt == p2m_ram_ro ) +if (p2m_is_discard_write (p2mt) ) {static unsigned long lastpage;if (xchg (&lastpage, gfn) != gfn )
@@ -4035,7 +4025,7 @@ static enum hvm_copy_result __hvm_clear(paddr_t addr, int size) p = (char *)__map_domain_page (page) + (addr & ~PAGE_MASK ); -if ( p2mt == p2m_ram_ro ) +if (p2m_is_discard_write (p2mt) ) {static unsigned long lastpage;if (xchg (&lastpage, gfn) != gfn )
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -4575,7 +4575,7 @@ static mfn_t emulate_gva_to_mfn(struct vcpu *v, {return _mfn (BAD_GFN_TO_MFN ); } -if (p2m_is_readonly (p2mt) ) +if (p2m_is_discard_write (p2mt) ) {put_page (page);return _mfn (READONLY_GFN );
xen/include/asm-x86/p2m.h +5 -0
@@ -113,6 +113,10 @@ typedef unsigned int p2m_query_t; | p2m_to_mask(p2m_grant_map_ro) \ | p2m_to_mask(p2m_ram_shared) ) +/* Write-discard types, which should discard the write operations */ +#define P2M_DISCARD_WRITE_TYPES (p2m_to_mask(p2m_ram_ro) \ + | p2m_to_mask(p2m_grant_map_ro)) +/* Types that can be subject to bulk transitions. */ #define P2M_CHANGEABLE_TYPES (p2m_to_mask(p2m_ram_rw) \ | p2m_to_mask(p2m_ram_logdirty) )
@@ -145,6 +149,7 @@ typedef unsigned int p2m_query_t;#define p2m_is_hole (_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)#define p2m_is_mmio (_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)#define p2m_is_readonly (_t) (p2m_to_mask(_t) & P2M_RO_TYPES) +#define p2m_is_discard_write (_t) (p2m_to_mask(_t) & P2M_DISCARD_WRITE_TYPES)#define p2m_is_changeable (_t) (p2m_to_mask(_t) & P2M_CHANGEABLE_TYPES)#define p2m_is_pod (_t) (p2m_to_mask(_t) & P2M_POD_TYPES)#define p2m_is_grant (_t) (p2m_to_mask(_t) & P2M_GRANT_TYPES)
Signature · 1 line
1.9.1
PATCH v5 2/2 add a new p2m type - p2m_mmio_write_dm
In reply to Yu ZhangFrom: Yu Zhang <[email protected]>
A new p2m type, p2m_mmio_write_dm, is added to trap and emulate the write operations on GPU's page tables. Handling of this new p2m type are similar with existing p2m_ram_ro in most condition checks, with only difference on final policy of emulation vs. drop. For p2m_ram_ro types, write operations will not trigger the device model, and will be discarded later in __hvm_copy(); while for the p2m_mmio_write_dm type pages, writes will go to the device model via ioreq-server.
--- xen/arch/x86/hvm/hvm.c | 11 ++++++++--- xen/arch/x86/mm/p2m-ept.c | 1 + xen/arch/x86/mm/p2m-pt.c | 1 + xen/include/asm-x86/p2m.h | 4 +++- xen/include/public/hvm/hvm_op.h | 1 + 5 files changed, 14 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +8 -3
@@ -2837,7 +2837,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,* to the mmio handler. */ if ( (p2mt == p2m_mmio_dm) || - (npfec.write_access && (p2m_is_discard_write (p2mt))) ) + (npfec.write_access && + (p2m_is_discard_write (p2mt) || (p2mt == p2m_mmio_write_dm))) ) {put_gfn (p2m->domain, gfn);
@@ -5904,6 +5905,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)get_gfn_query_unlocked (d, a.pfn, &t);if (p2m_is_mmio (t) ) a.mem_type = HVMMEM_mmio_dm; +else if ( t == p2m_mmio_write_dm ) + a.mem_type = HVMMEM_mmio_write_dm;else if (p2m_is_readonly (t) ) a.mem_type = HVMMEM_ram_ro;else if (p2m_is_ram (t) )
@@ -5931,7 +5934,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)static const p2m_type_t memtype[] = { [HVMMEM_ram_rw] = p2m_ram_rw, [HVMMEM_ram_ro] = p2m_ram_ro, - [HVMMEM_mmio_dm] = p2m_mmio_dm + [HVMMEM_mmio_dm] = p2m_mmio_dm, + [HVMMEM_mmio_write_dm] = p2m_mmio_write_dm };if (copy_from_guest (&a, arg,1 ) )
@@ -5978,7 +5982,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) goto param_fail4; }if ( !p2m_is_ram (t) && - (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) ) + (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) && + t != p2m_mmio_write_dm ) {put_gfn (d, pfn); goto param_fail4;
xen/arch/x86/mm/p2m-ept.c +1 -0
@@ -136,6 +136,7 @@ static void ept_p2m_type_to_flags(ept_entry_t *entry, p2m_type_t type, p2m_acces entry->x =0 ;break ;case p2m_grant_map_ro: +case p2m_mmio_write_dm: entry->r =1 ; entry->w = entry->x =0 ;break ;
xen/arch/x86/mm/p2m-pt.c +1 -0
@@ -94,6 +94,7 @@ static unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn)default :return flags | _PAGE_NX_BIT;case p2m_grant_map_ro: +case p2m_mmio_write_dm:return flags |P2M_BASE_FLAGS | _PAGE_NX_BIT;case p2m_ram_ro:case p2m_ram_logdirty:
xen/include/asm-x86/p2m.h +3 -1
@@ -72,6 +72,7 @@ typedef enum { p2m_ram_shared =12 ,/* Shared or sharable memory */ p2m_ram_broken =13 ,/* Broken page, access cause domain crash */ p2m_map_foreign =14 ,/* ram pages from foreign domain */ + p2m_mmio_write_dm =15 ,/* Read-only; writes go to the device model */ } p2m_type_t;/* Modifiers to the query */
@@ -111,7 +112,8 @@ typedef unsigned int p2m_query_t;#define P2M_RO_TYPES (p2m_to_mask(p2m_ram_logdirty) \ | p2m_to_mask(p2m_ram_ro) \ | p2m_to_mask(p2m_grant_map_ro) \ - | p2m_to_mask(p2m_ram_shared) ) + | p2m_to_mask(p2m_ram_shared) \ + | p2m_to_mask(p2m_mmio_write_dm))/* Write-discard types, which should discard the write operations */ #define P2M_DISCARD_WRITE_TYPES (p2m_to_mask(p2m_ram_ro) \
xen/include/public/hvm/hvm_op.h +1 -0
@@ -81,6 +81,7 @@ typedef enum { HVMMEM_ram_rw,/* Normal read/write guest RAM */ HVMMEM_ram_ro,/* Read-only; writes are discarded */ HVMMEM_mmio_dm,/* Reads and write go to the device model */ + HVMMEM_mmio_write_dm /* Read-only; writes go to the device model */ } hvmmem_type_t;/* Following tools-only interfaces may change in future. */
Signature · 1 line
1.9.1
Re: [PATCH v5 1/2] add a new p2m type class - P2M_DISCARD_WRITE_TYPES
In reply to Yu ZhangQuoted 11 lines · Yu Zhang
From: Yu Zhang <[email protected]>
Currently, the
P2M_RO_TYPESbears 2 meanings: one is "_PAGE_RWbit is clear in their PTEs", and another is to discard the write operations on these pages. This patch adds a p2m type class,P2M_DISCARD_WRITE_TYPES, to bear the second meaning, so we can use this type class instead of theP2M_RO_TYPES, to decide if a write operation is to be ignored.Signed-off-by Yu Zhang <[email protected]>
Re: [PATCH v5 2/2] add a new p2m type - p2m_mmio_write_dm
In reply to Yu ZhangHi,
Quoted 11 lines · Yu Zhang
A new p2m type,
p2m_mmio_write_dm, is added to trap and emulate the write operations on GPU's page tables. Handling of this new p2m type are similar with existingp2m_ram_roin most condition checks, with only difference on final policy of emulation vs. drop. Forp2m_ram_rotypes, write operations will not trigger the device model, and will be discarded later in__hvm_copy(); while for thep2m_mmio_write_dmtype pages, writes will go to the device model via ioreq-server.Signed-off-by Yu Zhang <[email protected]>Signed-off-by Wei Ye <[email protected]>
Thanks for this -- only two comments:
xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5986
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +2
-1@@ -5978,7 +5982,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) goto param_fail4; }if ( !p2m_is_ram (t) && - (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) ) + (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) && + t != p2m_mmio_write_dm )
I think that Jan already brough this up, and maybe I missed your
answer: this realaxation looks wrong to me. I would have thought that transition between p2m_mmio_write_dm and p2m_ram_rw/p2m_ram_logdirty would be the only ones you would want to allow.
xen/include/asm-x86/p2m.h typedef unsigned int p2m_query_t; · line 116
Quoted 7 lines
xen/include/asm-x86/p2m.h +2
-1@@ -111,7 +112,8 @@ typedef unsigned int p2m_query_t;#define P2M_RO_TYPES (p2m_to_mask(p2m_ram_logdirty) \ | p2m_to_mask(p2m_ram_ro) \ | p2m_to_mask(p2m_grant_map_ro) \ - | p2m_to_mask(p2m_ram_shared) ) + | p2m_to_mask(p2m_ram_shared) \ + | p2m_to_mask(p2m_mmio_write_dm))
Nit: please align the '\' with the others above it.
Cheers,
Tim.
Re: [PATCH v5 2/2] add a new p2m type - p2m_mmio_write_dm
Quoted 17 lines · Tim Deegan
Hi,
Quoted 11 lines · Yu Zhang
A new p2m type,
p2m_mmio_write_dm, is added to trap and emulate the write operations on GPU's page tables. Handling of this new p2m type are similar with existingp2m_ram_roin most condition checks, with only difference on final policy of emulation vs. drop. Forp2m_ram_rotypes, write operations will not trigger the device model, and will be discarded later in__hvm_copy(); while for thep2m_mmio_write_dmtype pages, writes will go to the device model via ioreq-server.Signed-off-by Yu Zhang <[email protected]>Signed-off-by Wei Ye <[email protected]>Thanks for this -- only two comments:
Quoted 12 lines
xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5986
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +2
-1@@ -5978,7 +5982,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) goto param_fail4; }if ( !p2m_is_ram (t) && - (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) ) + (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) && + t != p2m_mmio_write_dm )I think that Jan already brough this up, and maybe I missed your
answer: this realaxation looks wrong to me. I would have thought that transition betweenp2m_mmio_write_dmand p2m_ram_rw/p2m_ram_logdirty would be the only ones you would want to allow.
Ha. Sorry, my negligence, and thanks for pointing out. :)
The transition we use now is only between p2m_mmio_write_dm and p2m_ram_rw. So how about this:
if ( !p2m_is_ram(t) &&
(!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) &&
(t != p2m_mmio_write_dm || a.hvmmem_type != HVMMEM_ram_rw) )Quoted 10 lines
xen/include/asm-x86/p2m.h typedef unsigned int p2m_query_t; · line 116
Quoted 8 lines
xen/include/asm-x86/p2m.h +2
-1@@ -111,7 +112,8 @@ typedef unsigned int p2m_query_t;#define P2M_RO_TYPES (p2m_to_mask(p2m_ram_logdirty) \ | p2m_to_mask(p2m_ram_ro) \ | p2m_to_mask(p2m_grant_map_ro) \ - | p2m_to_mask(p2m_ram_shared) ) + | p2m_to_mask(p2m_ram_shared) \ + | p2m_to_mask(p2m_mmio_write_dm))Nit: please align the '\' with the others above it.
Got it, and thanks.
B.R.
Yu
Quoted 6 lines
Cheers,
Tim.
Re: [PATCH v5 2/2] add a new p2m type - p2m_mmio_write_dm
Hi,
Quoted 13 lines · Zhang
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0
-0@@ -5978,7 +5982,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) goto param_fail4; } if ( !p2m_is_ram(t) &&Quoted 8 lines
xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 5986
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +2
-1- (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) ) + (!p2m_is_hole (t) || a.hvmmem_type != HVMMEM_mmio_dm) && + t != p2m_mmio_write_dm )I think that Jan already brough this up, and maybe I missed your
answer: this realaxation looks wrong to me. I would have thought that transition betweenp2m_mmio_write_dmand p2m_ram_rw/p2m_ram_logdirty would be the only ones you would want to allow.
Quoted 7 lines
Ha. Sorry, my negligence, and thanks for pointing out. :) The transition we use now is only between
p2m_mmio_write_dmandp2m_ram_rw. So how about this:if ( !p2m_is_ram(t) && (!p2m_is_hole(t) || a.hvmmem_type != HVMMEM_mmio_dm) && (t != p2m_mmio_write_dm || a.hvmmem_type != HVMMEM_ram_rw) )
Yes, I think that's right.
Cheers,
Tim.