Skip to contentSkip to conversation

Provide support for multiple frame buffers in Xen

PATCH In review Robert Phillips 2 participants 3 messages Nov 12, 2012 – Nov 27, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Provide support for multiple frame buffers in Xen

Support is provided for both shadow and hardware assisted paging (HAP) modes.
This code bookkeeps the set of video frame buffers (vram),
detects when the guest has modified any of those buffers and, upon request,
returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged. (Version 3 of this patch.)

---
 tools/libxc/xenctrl.h            |   17 +-
 xen/arch/x86/hvm/Makefile        |    2 +-
 xen/arch/x86/hvm/hvm.c           |   16 +-
 xen/arch/x86/mm/Makefile         |    1 +
 xen/arch/x86/mm/dirty_vram.c     |  992 ++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/mm/hap/hap.c        |  140 +-----
 xen/arch/x86/mm/paging.c         |  196 ++------
 xen/arch/x86/mm/shadow/common.c  |  335 +++++++------
 xen/arch/x86/mm/shadow/multi.c   |  174 +++----
 xen/arch/x86/mm/shadow/multi.h   |    7 +-
 xen/arch/x86/mm/shadow/types.h   |    1 +
 xen/include/asm-x86/dirty_vram.h |  201 ++++++++
 xen/include/asm-x86/hap.h        |    4 -
 xen/include/asm-x86/hvm/domain.h |    2 +-
 xen/include/asm-x86/paging.h     |   17 +-
 xen/include/asm-x86/shadow.h     |    6 -
 16 files changed, 1549 insertions(+), 562 deletions(-)
 create mode 100644 xen/arch/x86/mm/dirty_vram.c
 create mode 100644 xen/include/asm-x86/dirty_vram.h
tools/libxc/xenctrl.h +11 -6
index 7eb5743..693d7fe 100644
@@ -1552,15 +1552,20 @@ int xc_hvm_inject_msi(
     xc_interface *xch, domid_t dom, uint64_t addr, uint32_t data);
 
 /*
- * Track dirty bit changes in the VRAM area
+ * Track dirty bit changes in a VRAM region defined by
+ * [ first_pfn : first_pfn + nr - 1 ]
  *
  * All of this is done atomically:
- * - get the dirty bitmap since the last call
- * - set up dirty tracking area for period up to the next call
- * - clear the dirty tracking area.
+ * - gets the dirty bitmap since the last call, all zeroes for
+ *   the first call with some new region
+ * - sets up a dirty tracking region for period up to the next call
+ * - clears the specified dirty tracking region.
  *
- * Returns -ENODATA and does not fill bitmap if the area has changed since the
- * last call.
+ * Creating a new region causes any existing regions that it overlaps
+ * to be discarded.
+ *
+ * Specifying nr == 0 causes all regions to be discarded and
+ * disables dirty bit tracking.
  */
 int xc_hvm_track_dirty_vram(
     xc_interface *xch, domid_t dom,
xen/arch/x86/hvm/Makefile +1 -1
index eea5555..e374aac 100644
@@ -22,4 +22,4 @@ obj-y += vlapic.o
 obj-y += vmsi.o
 obj-y += vpic.o
 obj-y += vpt.o
-obj-y += vpmu.o
\ No newline at end of file
+obj-y += vpmu.o
xen/arch/x86/hvm/hvm.c +15 -1
index 34da2f5..3a3e5e4 100644
@@ -57,6 +57,7 @@
 #include <asm/hvm/cacheattr.h>
 #include <asm/hvm/trace.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/dirty_vram.h>
 #include <asm/mtrr.h>
 #include <asm/apic.h>
 #include <public/sched.h>
@@ -66,6 +67,7 @@
 #include <asm/mem_event.h>
 #include <asm/mem_access.h>
 #include <public/mem_event.h>
+#include "../mm/mm-locks.h"
 
 bool_t __read_mostly hvm_enabled;
 
@@ -1433,8 +1435,20 @@ int hvm_hap_nested_page_fault(paddr_t gpa,
          */
         if ( access_w )
         {
+            p2m_type_t pt;
+            pt = p2m_change_type(v->domain, gfn, p2m_ram_logdirty, p2m_ram_rw);
+            
+            paging_lock(v->domain);
+            if ( pt == p2m_ram_logdirty )
+            {
+                dv_range_t *range;
+                v->domain->arch.paging.log_dirty.dirty_count++;
+                range = dirty_vram_range_find_gfn(v->domain, gfn);
+                if ( range )
+                    range->dirty_count++;
+            }
             paging_mark_dirty(v->domain, mfn_x(mfn));
-            p2m_change_type(v->domain, gfn, p2m_ram_logdirty, p2m_ram_rw);
+            paging_unlock(v->domain);
         }
         rc = 1;
         goto out_put_gfn;
xen/arch/x86/mm/Makefile +1 -0
index 73dcdf4..becd0c9 100644
@@ -5,6 +5,7 @@ obj-y += paging.o
 obj-y += p2m.o p2m-pt.o p2m-ept.o p2m-pod.o
 obj-y += guest_walk_2.o
 obj-y += guest_walk_3.o
+obj-y += dirty_vram.o
 obj-$(x86_64) += guest_walk_4.o
 obj-$(x86_64) += mem_event.o
 obj-$(x86_64) += mem_paging.o
xen/arch/x86/mm/dirty_vram.c new file +992 -0

992 lines not on this page · raw message

xen/arch/x86/mm/hap/hap.c +3 -137

162 lines not on this page · raw message

xen/arch/x86/mm/paging.c +51 -145

241 lines not on this page · raw message

xen/arch/x86/mm/shadow/common.c +182 -153

379 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +80 -94

255 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.h +6 -1
index 835121e..436a4ac 100644
@@ -66,7 +66,12 @@ SHADOW_INTERNAL_NAME(sh_rm_write_access_from_l1, GUEST_LEVELS)
 extern int
 SHADOW_INTERNAL_NAME(sh_rm_mappings_from_l1, GUEST_LEVELS)
     (struct vcpu *v, mfn_t sl1mfn, mfn_t target_mfn);
-
+extern int
+SHADOW_INTERNAL_NAME(sh_find_vram_mappings_in_l1, GUEST_LEVELS)
+     (struct vcpu *v, mfn_t sl1mfn, 
+      unsigned long begin_pfn,
+      unsigned long end_pfn,
+      int *removed);
 extern void
 SHADOW_INTERNAL_NAME(sh_clear_shadow_entry, GUEST_LEVELS)
     (struct vcpu *v, void *ep, mfn_t smfn);
xen/arch/x86/mm/shadow/types.h +1 -0
index 43ce1db..5b0f9f7 100644
@@ -229,6 +229,7 @@ static inline shadow_l4e_t shadow_l4e_from_mfn(mfn_t mfn, u32 flags)
 #define sh_update_cr3              INTERNAL_NAME(sh_update_cr3)
 #define sh_rm_write_access_from_l1 INTERNAL_NAME(sh_rm_write_access_from_l1)
 #define sh_rm_mappings_from_l1     INTERNAL_NAME(sh_rm_mappings_from_l1)
+#define sh_find_vram_mappings_in_l1 INTERNAL_NAME(sh_find_vram_mappings_in_l1)
 #define sh_remove_l1_shadow        INTERNAL_NAME(sh_remove_l1_shadow)
 #define sh_remove_l2_shadow        INTERNAL_NAME(sh_remove_l2_shadow)
 #define sh_remove_l3_shadow        INTERNAL_NAME(sh_remove_l3_shadow)
xen/include/asm-x86/dirty_vram.h new file +201 -0

201 lines not on this page · raw message

xen/include/asm-x86/hap.h +0 -4
index 916a35b..3e3a1f5 100644
@@ -57,10 +57,6 @@ void  hap_final_teardown(struct domain *d);
 void  hap_teardown(struct domain *d);
 void  hap_vcpu_init(struct vcpu *v);
 void  hap_logdirty_init(struct domain *d);
-int   hap_track_dirty_vram(struct domain *d,
-                           unsigned long begin_pfn,
-                           unsigned long nr,
-                           XEN_GUEST_HANDLE_64(uint8) dirty_bitmap);
 
 extern const struct paging_mode *hap_paging_get_mode(struct vcpu *);
 
xen/include/asm-x86/hvm/domain.h +1 -1
index 27b3de5..6146542 100644
@@ -74,7 +74,7 @@ struct hvm_domain {
     struct list_head       pinned_cacheattr_ranges;
 
     /* VRAM dirty support. */
-    struct sh_dirty_vram *dirty_vram;
+    struct dv_dirty_vram * dirty_vram;
 
     /* If one of vcpus of this domain is in no_fill_mode or
      * mtrr/pat between vcpus is not the same, set is_in_uc_mode
xen/include/asm-x86/paging.h +4 -13
index 9a40f2c..f96ca3b 100644
@@ -137,10 +137,10 @@ struct paging_mode {
 void paging_free_log_dirty_bitmap(struct domain *d);
 
 /* get the dirty bitmap for a specific range of pfns */
-int paging_log_dirty_range(struct domain *d,
-                           unsigned long begin_pfn,
-                           unsigned long nr,
-                           XEN_GUEST_HANDLE_64(uint8) dirty_bitmap);
+void paging_log_dirty_range(struct domain *d,
+                            unsigned long begin_pfn,
+                            unsigned long nr,
+                            uint8_t *dirty_bitmap);
 
 /* enable log dirty */
 int paging_log_dirty_enable(struct domain *d);
@@ -183,15 +183,6 @@ int paging_mfn_is_dirty(struct domain *d, mfn_t gmfn);
 #define L4_LOGDIRTY_IDX(pfn) 0
 #endif
 
-/* VRAM dirty tracking support */
-struct sh_dirty_vram {
-    unsigned long begin_pfn;
-    unsigned long end_pfn;
-    paddr_t *sl1ma;
-    uint8_t *dirty_bitmap;
-    s_time_t last_dirty;
-};
-
 /*****************************************************************************
  * Entry points into the paging-assistance code */
 
xen/include/asm-x86/shadow.h +0 -6
index 2eb6efc..940d7fd 100644
@@ -62,12 +62,6 @@ void shadow_vcpu_init(struct vcpu *v);
 /* Enable an arbitrary shadow mode.  Call once at domain creation. */
 int shadow_enable(struct domain *d, u32 mode);
 
-/* Enable VRAM dirty bit tracking. */
-int shadow_track_dirty_vram(struct domain *d,
-                            unsigned long first_pfn,
-                            unsigned long nr,
-                            XEN_GUEST_HANDLE_64(uint8) dirty_bitmap);
-
 /* Handler for shadow control ops: operations from user-space to enable
  * and disable ephemeral shadow modes (test mode and log-dirty mode) and
  * manipulate the log-dirty bitmap. */
Signature · 1 line
1.7.9.5
#1 lore raw

Hi,

We're very nearly there now. I think I agree on almost all the technical decisions but there are still a few things to tidy up (some of which I mentioned before).

Quoted 8 lines · Robert Phillips

Support is provided for both shadow and hardware assisted paging (HAP) modes.
This code bookkeeps the set of video frame buffers (vram),
detects when the guest has modified any of those buffers and, upon request,
returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor (or monitors) that have changed. Each monitor has a frame buffer of some size at some position in guest physical memory. The set of frame buffers being tracked can change over time as monitors are plugged and unplugged. (Version 3 of this patch.)

Please linewrap at something less than 80 characters.

xen/arch/x86/hvm/Makefile obj-y += vlapic.o · line 25

Quoted 11 lines
xen/arch/x86/hvm/Makefile +1 -1
index eea5555..e374aac 100644
@@ -22,4 +22,4 @@ obj-y += vlapic.o
 obj-y += vmsi.o
 obj-y += vpic.o
 obj-y += vpt.o
-obj-y += vpmu.o
\ No newline at end of file
+obj-y += vpmu.o

This is an unrelated fix, so doesn't belong in this changeset.

Quoted 29 lines
xen/arch/x86/hvm/hvm.c +7 -0
index 34da2f5..3a3e5e4 100644
@@ -57,6 +57,7 @@
 #include <asm/hvm/cacheattr.h>
 #include <asm/hvm/trace.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/dirty_vram.h>
 #include <asm/mtrr.h>
 #include <asm/apic.h>
 #include <public/sched.h>
@@ -66,6 +67,7 @@
 #include <asm/mem_event.h>
 #include <asm/mem_access.h>
 #include <public/mem_event.h>
+#include "../mm/mm-locks.h"
 
 bool_t __read_mostly hvm_enabled;
 
@@ -1433,8 +1435,20 @@ int hvm_hap_nested_page_fault(paddr_t gpa,
          */
         if ( access_w )
         {
+            p2m_type_t pt;
+            pt = p2m_change_type(v->domain, gfn, p2m_ram_logdirty, p2m_ram_rw);
+            
+            paging_lock(v->domain);
+            if ( pt == p2m_ram_logdirty )

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, · line 1451

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +8 -1
+            {
+                dv_range_t *range;
+                v->domain->arch.paging.log_dirty.dirty_count++;
+                range = dirty_vram_range_find_gfn(v->domain, gfn);
+                if ( range )
+                    range->dirty_count++;
+            }
             paging_mark_dirty(v->domain, mfn_x(mfn));
-            p2m_change_type(v->domain, gfn, p2m_ram_logdirty, p2m_ram_rw);
+            paging_unlock(v->domain);

This is much nicer than the previous version, but I think it would be even better if this bookkeeping went into paging_mark_dirty() so that the other callers of paging_mark_dirty() also DTRT with the vram map. That would avoid leaking mm-locks.h into this non-mm code, too.

Then this change becomes just swapping the order of the two lines (and perhaps a comment to say why).

xen/arch/x86/mm/dirty_vram.c line 5

Quoted 11 lines
xen/arch/x86/mm/dirty_vram.c new file +5 -0
new file mode 100644
index 0000000..e3c7c1f
@@ -0,0 +1,992 @@
+/*
+ * arch/x86/mm/dirty_vram.c: Bookkeep/query dirty VRAM pages
+ * with support for multiple frame buffers.
+ *
+ * Copyright (c) 2012, Citrix Systems, Inc. (Robert Phillips)

Please bring in the copyright and authorship notices for the files you copied code from. That's at least mm/shadow/common.c and mm/hap/hap.c.

Apart from that this is looking good.

Are you willing to take on maintainership of this feature (that is, to respond to questions and fix bugs)? If so, we should make an update to the MAINTAINERS file for xen/arch/x86/mm/dirty_vram.c and xen/include/asm-x86/dirty_vram.h. That can happen separately, as it'll need an ack from the other maintainers.

Cheers,

Tim.

#2 lore raw

Hi,

The updated patch was sent out just a few minutes ago.

Quoted 21 lines

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Thursday, November 15, 2012 8:23 AM
To: Robert Phillips
Cc: [email protected]
Subject: Re: [Xen-devel] [PATCH] Provide support for multiple frame buffers in Xen

Hi,

We're very nearly there now. I think I agree on almost all the technical decisions but there are still a few things to tidy up (some of which I mentioned before).

Quoted 1 line · Robert Phillips

Support is provided for both shadow and hardware assisted paging (HAP)

modes.

Quoted 4 lines

This code bookkeeps the set of video frame buffers (vram), detects when the guest has modified any of those buffers and, upon request, returns a bitmap of the modified pages.
This lets other software components re-paint the portions of the monitor

Quoted 8 lines

(or monitors) that have changed.

Quoted 1 line

Each monitor has a frame buffer of some size at some position in guest

physical memory.

Quoted 1 line

The set of frame buffers being tracked can change over time as monitors

are plugged and unplugged.

Quoted 1 line

(Version 3 of this patch.)

Please linewrap at something less than 80 characters.

Done

Quoted 14 lines

xen/arch/x86/hvm/Makefile obj-y += vlapic.o · line 25

Quoted 12 lines
xen/arch/x86/hvm/Makefile +1 -1
index eea5555..e374aac 100644
@@ -22,4 +22,4 @@ obj-y += vlapic.o
 obj-y += vmsi.o
 obj-y += vpic.o
 obj-y += vpt.o
-obj-y += vpmu.o
\ No newline at end of file
+obj-y += vpmu.o

This is an unrelated fix, so doesn't belong in this changeset.

Removed

Quoted 43 lines
Quoted 30 lines
xen/arch/x86/hvm/hvm.c index renamed from xen/arch/x86/hvm/hvm.c +0 -0

34da2f5..3a3e5e4 100644

xen/arch/x86/hvm/hvm.c +7 -0
@@ -57,6 +57,7 @@
 #include <asm/hvm/cacheattr.h>
 #include <asm/hvm/trace.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/dirty_vram.h>
 #include <asm/mtrr.h>
 #include <asm/apic.h>
 #include <public/sched.h>
@@ -66,6 +67,7 @@
 #include <asm/mem_event.h>
 #include <asm/mem_access.h>
 #include <public/mem_event.h>
+#include "../mm/mm-locks.h"
 
 bool_t __read_mostly hvm_enabled;
 
@@ -1433,8 +1435,20 @@ int hvm_hap_nested_page_fault(paddr_t gpa,
          */
         if ( access_w )
         {
+            p2m_type_t pt;
+            pt = p2m_change_type(v->domain, gfn, p2m_ram_logdirty,
+ p2m_ram_rw);
+
+            paging_lock(v->domain);

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, · line 1437

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +8 -1
+            if ( pt == p2m_ram_logdirty )
+            {
+                dv_range_t *range;
+                v->domain->arch.paging.log_dirty.dirty_count++;
+                range = dirty_vram_range_find_gfn(v->domain, gfn);
+                if ( range )
+                    range->dirty_count++;
+            }
             paging_mark_dirty(v->domain, mfn_x(mfn));
-            p2m_change_type(v->domain, gfn, p2m_ram_logdirty,

p2m_ram_rw);

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, · line 1451

Quoted 1 line
xen/arch/x86/hvm/hvm.c +1 -0
+            paging_unlock(v->domain);
Quoted 7 lines

This is much nicer than the previous version, but I think it would be even better if this bookkeeping went into paging_mark_dirty() so that the other callers of paging_mark_dirty() also DTRT with the vram map. That would avoid leaking mm-locks.h into this non-mm code, too.

Then this change becomes just swapping the order of the two lines (and perhaps a comment to say why).

Done. I had to split paging_mark_dirty() into two functions -- the old function does the gfn-to-mfn translation and then calls the new function paging_mark_dirty_gpfn() which does the heavy lifiting. The code above now calls the new function thereby bypassing the gfn-to-mfn translation.

Quoted 15 lines

xen/arch/x86/mm/dirty_vram.c line 5

Quoted 12 lines
(quoted diff) +0 -0

b/xen/arch/x86/mm/dirty_vram.c new file mode 100644 index 0000000..e3c7c1f

xen/arch/x86/mm/dirty_vram.c new file +5 -0
@@ -0,0 +1,992 @@
+/*
+ * arch/x86/mm/dirty_vram.c: Bookkeep/query dirty VRAM pages
+ * with support for multiple frame buffers.
+ *
+ * Copyright (c) 2012, Citrix Systems, Inc. (Robert Phillips)

Please bring in the copyright and authorship notices for the files you copied code from. That's at least mm/shadow/common.c and mm/hap/hap.c.

Done

Quoted 5 lines

Apart from that this is looking good.

Are you willing to take on maintainership of this feature (that is, to respond to questions and fix bugs)?

Yes

Quoted 8 lines

If so, we should make an update to the
MAINTAINERS file for xen/arch/x86/mm/dirty_vram.c and xen/include/asm- x86/dirty_vram.h. That can happen separately, as it'll need an ack from the other maintainers.

Cheers,

Tim.