Skip to contentSkip to conversation

add new p2m type class and new p2m type

PATCH Superseded Yu Zhang 3 participants 10 messages Dec 6, 2014 – Dec 11, 2014

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

v6 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 v5:
- Stricter type checks for p2m type transitions;
- One code style change.

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
author
#1 lore raw

PATCH v6 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
index 51ffc90..967f822 100644
@@ -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
index 225290e..94cf06d 100644
@@ -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
index 5f7fe71..42de75d 100644
@@ -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
author
#2 lore raw

PATCH v6 2/2 add a new p2m type - p2m_mmio_write_dm

In reply to Yu Zhang

From: 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
index 967f822..25114fc 100644
@@ -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 || a.hvmmem_type != HVMMEM_ram_rw) )
             {
                 put_gfn(d, pfn);
                 goto param_fail4;
xen/arch/x86/mm/p2m-ept.c +1 -0
index 15c6e83..e21a92d 100644
@@ -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
index e48b63a..26fb18d 100644
@@ -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
index 42de75d..2cf73ca 100644
@@ -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
index eeb0a60..a4e5345 100644
@@ -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
Reviewed-by on v6 2/2
#3 lore raw

Re: [PATCH v6 2/2] add a new p2m type - p2m_mmio_write_dm

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 06.12.14 at 04:55, <[email protected]> wrote:

From: 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.

Quoted 2 lines
author
#6 lore raw
In reply to Yu Zhang

Hi Tim & Jan,

Thank you very much for your review.
And could you please also help me about how to get an ACK? I'm not
sure what's the next action I need to take. :-)

B.R.
Yu

Quoted 68 lines · Yu Zhang

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 v5:
- Stricter type checks for p2m type transitions;
- One code style change.

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(-)
#7 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 09.12.14 at 03:02, <[email protected]> wrote:

Thank you very much for your review.
And could you please also help me about how to get an ACK? I'm not
sure what's the next action I need to take. :-)

I don't think you need to take any action at this point. The second patch will need Tim's ack, yes, but that's nothing to worry about (yet), since even with his ack the two patches wouldn't go in until after 4.5 got branched off of staging.

Jan

author
#8 lore raw
Quoted 10 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 09.12.14 at 03:02, <[email protected]> wrote:

Thank you very much for your review.
And could you please also help me about how to get an ACK? I'm not
sure what's the next action I need to take. :-)

I don't think you need to take any action at this point. The second patch will need Tim's ack, yes, but that's nothing to worry about (yet), since even with his ack the two patches wouldn't go in until after 4.5 got branched off of staging.

Got it, and thanks!

Yu

Quoted 9 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw
In reply to Yu, Zhang
Quoted 5 lines · Zhang

Hi Tim & Jan,

Thank you very much for your review.
And could you please also help me about how to get an ACK? I'm not
sure what's the next action I need to take. :-)

I'll review it on Thursday; I'll probably ack it then, since the changes from v5 should be simple enough. As Jan says, it won't be committed until after 4.5 has been branched anyway.

Cheers,

Tim.

Reviewed-by on v6 2/2
#4 lore raw

Re: [PATCH v6 2/2] add a new p2m type - p2m_mmio_write_dm

In reply to Yu Zhang
Quoted 11 lines · Yu Zhang

From: 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.

Quoted 2 lines

Sorry not to have seen this before, but it looks like the new type isn't handled in the shadow-pagetable code. I think you need this as well:

xen/arch/x86/mm/shadow/multi.c +2 -1
index 225290e..58c0cca 100644
@@ -3181,7 +3181,8 @@ static int sh_page_fault(struct vcpu *v,
     }
 
     /* Need to hand off device-model MMIO to the device model */
-    if ( p2mt == p2m_mmio_dm ) 
+    if ( p2mt == p2m_mmio_dm
+         || p2mt == p2m_mmio_readonly && ft == ft_demand_write )
     {
         gpa = guest_walk_to_gpa(&gw);
         goto mmio;

With that hunk added, you can add

Tim.

#5 lore raw

Re: [PATCH v6 2/2] add a new p2m type - p2m_mmio_write_dm

Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 11.12.14 at 13:04, <[email protected]> wrote:
Quoted 11 lines · Yu Zhang

From: 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.

Quoted 2 lines

Sorry not to have seen this before, but it looks like the new type isn't handled in the shadow-pagetable code. I think you need this as well:

xen/arch/x86/mm/shadow/multi.c +2 -1
index 225290e..58c0cca 100644
@@ -3181,7 +3181,8 @@ static int sh_page_fault(struct vcpu *v,
     }
 
     /* Need to hand off device-model MMIO to the device model */
-    if ( p2mt == p2m_mmio_dm ) 
+    if ( p2mt == p2m_mmio_dm
+         || p2mt == p2m_mmio_readonly && ft == ft_demand_write )
     {
         gpa = guest_walk_to_gpa(&gw);
         goto mmio;
Quoted 3 lines

With that hunk added, you can add

But please add parentheses around the && operands, even if not strictly needed.

Jan