Skip to contentSkip to conversation

Mapping grant references into HVM guests, take 2

PATCH Steven Smith 1 participant 1 message Jul 13, 2009

lore
Share & tools
lore
#0 lore raw

After some discussion, here's a second version of the patch I posted a couple of weeks back to map grant references into HVM guests. As before, this is done by modifying the P2M map, but this time there's no new hypercall to do it. Instead, the existing GNTTABOP_map is overloaded to perform a P2M mapping if called from a shadow mode translate guest. This matches the IA64 API.

The patch should apply cleanly to xen-unstable cset 19921:8ee92b9f890f.

xen/arch/x86/hvm/emulate.c +7 -2
diff -r 8ee92b9f890f xen/arch/x86/hvm/emulate.c
@@ -411,6 +411,9 @@
     if ( rc == HVMCOPY_bad_gva_to_gfn )
         return X86EMUL_EXCEPTION;
 
+    if ( rc == HVMCOPY_unemulatable )
+        return X86EMUL_UNHANDLEABLE;
+
     if ( rc == HVMCOPY_bad_gfn_to_mfn )
     {
         if ( access_type == hvm_access_insn_fetch )
@@ -498,6 +501,8 @@
     rc = hvm_copy_to_guest_virt(addr, p_data, bytes, pfec);
     if ( rc == HVMCOPY_bad_gva_to_gfn )
         return X86EMUL_EXCEPTION;
+    if ( rc == HVMCOPY_unemulatable )
+        return X86EMUL_UNHANDLEABLE;
 
     if ( rc == HVMCOPY_bad_gfn_to_mfn )
     {
@@ -636,12 +641,12 @@
         return rc;
 
     (void)gfn_to_mfn_current(sgpa >> PAGE_SHIFT, &p2mt);
-    if ( !p2m_is_ram(p2mt) )
+    if ( !p2m_is_ram(p2mt) && !p2m_is_grant(p2mt) )
         return hvmemul_do_mmio(
             sgpa, reps, bytes_per_rep, dgpa, IOREQ_READ, df, NULL);
 
     (void)gfn_to_mfn_current(dgpa >> PAGE_SHIFT, &p2mt);
-    if ( !p2m_is_ram(p2mt) )
+    if ( !p2m_is_ram(p2mt) && !p2m_is_grant(p2mt) )
         return hvmemul_do_mmio(
             dgpa, reps, bytes_per_rep, sgpa, IOREQ_WRITE, df, NULL);
 
xen/arch/x86/hvm/hvm.c +41 -6

100 lines not on this page · raw message

xen/arch/x86/hvm/stdvga.c +4 -4
diff -r 8ee92b9f890f xen/arch/x86/hvm/stdvga.c
@@ -478,8 +478,8 @@
             for ( i = 0; i < p->count; i++ ) 
             {
                 tmp = stdvga_mem_read(addr, p->size);
-                if ( hvm_copy_to_guest_phys(data, &tmp, p->size) ==
-                     HVMCOPY_bad_gfn_to_mfn )
+                if ( hvm_copy_to_guest_phys(data, &tmp, p->size) !=
+                     HVMCOPY_okay )
                 {
                     (void)gfn_to_mfn_current(data >> PAGE_SHIFT, &p2mt);
                     /*
@@ -500,8 +500,8 @@
             uint32_t addr = p->addr, data = p->data, tmp;
             for ( i = 0; i < p->count; i++ )
             {
-                if ( hvm_copy_from_guest_phys(&tmp, data, p->size) ==
-                     HVMCOPY_bad_gfn_to_mfn )
+                if ( hvm_copy_from_guest_phys(&tmp, data, p->size) !=
+                     HVMCOPY_okay )
                 {
                     (void)gfn_to_mfn_current(data >> PAGE_SHIFT, &p2mt);
                     if ( (p2mt != p2m_mmio_dm) || (data < VGA_MEM_BASE) ||
xen/arch/x86/hvm/svm/emulate.c +3 -1
diff -r 8ee92b9f890f xen/arch/x86/hvm/svm/emulate.c
@@ -126,13 +126,15 @@
         /* OK just to give up; we'll have injected #PF already */
         return 0;
     case HVMCOPY_bad_gfn_to_mfn:
-    default:
+    case HVMCOPY_unemulatable:
         /* Not OK: fetches from non-RAM pages are not supportable. */
         gdprintk(XENLOG_WARNING, "Bad instruction fetch at %#lx (%#lx)\n",
                  (unsigned long) guest_cpu_user_regs()->eip, addr);
         hvm_inject_exception(TRAP_gp_fault, 0, 0);
         return 0;
     }
+    BUG();
+    return 0;
 }
 
 int __get_instruction_length_from_list(struct vcpu *v,
xen/arch/x86/hvm/svm/svm.c +21 -2
diff -r 8ee92b9f890f xen/arch/x86/hvm/svm/svm.c
@@ -938,8 +938,27 @@
     }
 
     /* Log-dirty: mark the page dirty and let the guest write it again */
-    paging_mark_dirty(current->domain, mfn_x(mfn));
-    p2m_change_type(current->domain, gfn, p2m_ram_logdirty, p2m_ram_rw);
+    if ( p2mt == p2m_ram_logdirty )
+    {
+        paging_mark_dirty(current->domain, mfn_x(mfn));
+        p2m_change_type(current->domain, gfn, p2m_ram_logdirty, p2m_ram_rw);
+        return;
+    }
+
+    /* Okay, this shouldn't happen.  Maybe the guest was writing to a
+       read-only grant mapping? */
+    if ( p2mt == p2m_grant_map_ro )
+    {
+        /* Naughty... */
+        gdprintk(XENLOG_WARNING,
+                 "trying to write to read-only grant mapping\n");
+        hvm_inject_exception(TRAP_gp_fault, 0, 0);
+        return;
+    }
+
+    /* Something bad has happened; either Xen or the hardware have
+       screwed up. */
+    gdprintk(XENLOG_WARNING, "unexpected SVM nested page fault\n");
 }
 
 static void svm_fpu_dirty_intercept(void)
xen/arch/x86/mm.c +61 -3

94 lines not on this page · raw message

xen/arch/x86/mm/hap/p2m-ept.c +2 -0
diff -r 8ee92b9f890f xen/arch/x86/mm/hap/p2m-ept.c
@@ -39,10 +39,12 @@
             return;
         case p2m_ram_rw:
         case p2m_mmio_direct:
+        case p2m_grant_map_rw:
              entry->r = entry->w = entry->x = 1;
             return;
         case p2m_ram_logdirty:
         case p2m_ram_ro:
+        case p2m_grant_map_ro:
              entry->r = entry->x = 1;
              entry->w = 0;
             return;
xen/arch/x86/mm/p2m.c +57 -13

167 lines not on this page · raw message

xen/arch/x86/mm/paging.c +2 -1
diff -r 8ee92b9f890f xen/arch/x86/mm/paging.c
@@ -272,7 +272,8 @@
 
     gmfn = _mfn(guest_mfn);
 
-    if ( !paging_mode_log_dirty(d) || !mfn_valid(gmfn) )
+    if ( !paging_mode_log_dirty(d) || !mfn_valid(gmfn) ||
+         page_get_owner(mfn_to_page(gmfn)) != d )
         return;
 
     log_dirty_lock(d);
xen/arch/x86/mm/shadow/common.c +5 -3
diff -r 8ee92b9f890f xen/arch/x86/mm/shadow/common.c
@@ -172,10 +172,12 @@
         return X86EMUL_OKAY;
     case HVMCOPY_bad_gva_to_gfn:
         return X86EMUL_EXCEPTION;
-    default:
-        break;
+    case HVMCOPY_bad_gfn_to_mfn:
+    case HVMCOPY_unemulatable:
+        return X86EMUL_UNHANDLEABLE;
     }
 
+    BUG();
     return X86EMUL_UNHANDLEABLE;
 }
 
@@ -3431,7 +3433,7 @@
     {
         mfn_t mfn = _mfn(l1e_get_pfn(*p));
         p2m_type_t p2mt = p2m_flags_to_type(l1e_get_flags(*p));
-        if ( p2m_is_valid(p2mt) && mfn_valid(mfn) ) 
+        if ( (p2m_is_valid(p2mt) || p2m_is_grant(p2mt)) && mfn_valid(mfn) ) 
         {
             sh_remove_all_shadows_and_parents(v, mfn);
             if ( sh_remove_all_mappings(v, mfn) )
xen/arch/x86/mm/shadow/multi.c +51 -23

202 lines not on this page · raw message

xen/common/grant_table.c +26 -4
diff -r 8ee92b9f890f xen/common/grant_table.c
@@ -226,6 +226,15 @@
         return;
     }
 
+    if ( unlikely(paging_mode_external(ld) &&
+                  (op->flags & (GNTMAP_device_map|GNTMAP_application_map|
+                            GNTMAP_contains_pte))) )
+    {
+        gdprintk(XENLOG_INFO, "No device mapping in HVM domain.\n");
+        op->status = GNTST_general_error;
+        return;
+    }
+
     if ( unlikely((rd = rcu_lock_domain_by_id(op->dom)) == NULL) )
     {
         gdprintk(XENLOG_INFO, "Could not find domain %d\n", op->dom);
@@ -343,6 +352,13 @@
         if ( mfn_valid(frame) )
             put_page(mfn_to_page(frame));
 
+        if ( paging_mode_external(ld) )
+        {
+            gdprintk(XENLOG_WARNING, "HVM guests can't grant map iomem\n");
+            rc = GNTST_general_error;
+            goto undo_out;
+        }
+
         if ( !iomem_access_permitted(rd, frame, frame) )
         {
             gdprintk(XENLOG_WARNING,
@@ -395,7 +411,12 @@
          !(old_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) &&
          (act_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) )
     {
-        if ( iommu_map_page(ld, mfn_to_gmfn(ld, frame), frame) )
+        /* Shouldn't happen, because you can't use iommu in a HVM
+         * domain. */
+        BUG_ON(paging_mode_translate(ld));
+        /* We're not translated, so we know that gmfns and mfns are
+           the same things, so the IOMMU entry is always 1-to-1. */
+        if ( iommu_map_page(ld, frame, frame) )
         {
             rc = GNTST_general_error;
             goto undo_out;
@@ -573,7 +594,8 @@
          (old_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) &&
          !(act->pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) )
     {
-        if ( iommu_unmap_page(ld, mfn_to_gmfn(ld, op->frame)) )
+        BUG_ON(paging_mode_translate(ld));
+        if ( iommu_unmap_page(ld, op->frame) )
         {
             rc = GNTST_general_error;
             goto unmap_out;
@@ -1717,7 +1739,7 @@
             {
                 BUG_ON(!(act->pin & GNTPIN_hstr_mask));
                 act->pin -= GNTPIN_hstr_inc;
-                if ( gnttab_release_host_mappings &&
+                if ( gnttab_release_host_mappings(d) &&
                      !is_iomem_page(act->frame) )
                     put_page(mfn_to_page(act->frame));
             }
@@ -1736,7 +1758,7 @@
             {
                 BUG_ON(!(act->pin & GNTPIN_hstw_mask));
                 act->pin -= GNTPIN_hstw_inc;
-                if ( gnttab_release_host_mappings &&
+                if ( gnttab_release_host_mappings(d) &&
                      !is_iomem_page(act->frame) )
                 {
                     if ( gnttab_host_mapping_get_page_type(map, d, rd) )
xen/include/asm-ia64/grant_table.h +1 -1
diff -r 8ee92b9f890f xen/include/asm-ia64/grant_table.h
@@ -68,7 +68,7 @@
 #define gnttab_host_mapping_get_page_type(op, ld, rd)   \
     (!((op)->flags & GNTMAP_readonly))
 
-#define gnttab_release_host_mappings 1
+#define gnttab_release_host_mappings(domain) 1
 
 static inline int replace_grant_supported(void)
 {
xen/include/asm-x86/grant_table.h +1 -1
diff -r 8ee92b9f890f xen/include/asm-x86/grant_table.h
@@ -44,7 +44,7 @@
      (((ld) == (rd)) || !paging_mode_external(rd)))
 
 /* Done implicitly when page tables are destroyed. */
-#define gnttab_release_host_mappings 0
+#define gnttab_release_host_mappings(domain) ( paging_mode_external(domain) )
 
 static inline int replace_grant_supported(void)
 {
xen/include/asm-x86/hvm/domain.h +1 -0
diff -r 8ee92b9f890f xen/include/asm-x86/hvm/domain.h
@@ -31,6 +31,7 @@
 #include <asm/hvm/viridian.h>
 #include <asm/hvm/vmx/vmcs.h>
 #include <asm/hvm/svm/vmcb.h>
+#include <public/grant_table.h>
 #include <public/hvm/params.h>
 #include <public/hvm/save.h>
 
xen/include/asm-x86/hvm/support.h +2 -1
diff -r 8ee92b9f890f xen/include/asm-x86/hvm/support.h
@@ -71,7 +71,8 @@
 enum hvm_copy_result {
     HVMCOPY_okay = 0,
     HVMCOPY_bad_gva_to_gfn,
-    HVMCOPY_bad_gfn_to_mfn
+    HVMCOPY_bad_gfn_to_mfn,
+    HVMCOPY_unemulatable
 };
 
 /*
xen/include/asm-x86/p2m.h +26 -13
diff -r 8ee92b9f890f xen/include/asm-x86/p2m.h
@@ -45,6 +45,10 @@
  */
 #define phys_to_machine_mapping ((l1_pgentry_t *)RO_MPT_VIRT_START)
 
+#ifdef __x86_64__
+#define HAVE_GRANT_MAP_P2M
+#endif
+
 /*
  * The upper levels of the p2m pagetable always contain full rights; all 
  * variation in the access control bits is made in the level-1 PTEs.
@@ -65,6 +69,12 @@
     p2m_mmio_dm = 4,            /* Reads and write go to the device model */
     p2m_mmio_direct = 5,        /* Read/write mapping of genuine MMIO area */
     p2m_populate_on_demand = 6, /* Place-holder for empty memory */
+
+    /* Note that these can only be used if HAVE_GRANT_MAP_P2M is
+       defined.  They get defined anyway so as to avoid lots of
+       #ifdef's everywhere else. */
+    p2m_grant_map_rw = 7,       /* Read/write grant mapping */
+    p2m_grant_map_ro = 8,       /* Read-only grant mapping */
 } p2m_type_t;
 
 typedef enum {
@@ -81,13 +91,19 @@
                        | p2m_to_mask(p2m_ram_logdirty)  \
                        | p2m_to_mask(p2m_ram_ro))
 
+/* Grant mapping types, which map to a real machine frame in another
+ * VM */
+#define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw)  \
+                         | p2m_to_mask(p2m_grant_map_ro) )
+
 /* MMIO types, which don't have to map to anything in the frametable */
 #define P2M_MMIO_TYPES (p2m_to_mask(p2m_mmio_dm)        \
                         | p2m_to_mask(p2m_mmio_direct))
 
 /* Read-only types, which must have the _PAGE_RW bit clear in their PTEs */
 #define P2M_RO_TYPES (p2m_to_mask(p2m_ram_logdirty)     \
-                      | p2m_to_mask(p2m_ram_ro))
+                      | p2m_to_mask(p2m_ram_ro)         \
+                      | p2m_to_mask(p2m_grant_map_ro) )
 
 #define P2M_MAGIC_TYPES (p2m_to_mask(p2m_populate_on_demand))
 
@@ -96,6 +112,10 @@
 #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_magic(_t) (p2m_to_mask(_t) & P2M_MAGIC_TYPES)
+#define p2m_is_grant(_t) (p2m_to_mask(_t) & P2M_GRANT_TYPES)
+/* Grant types are *not* considered valid, because they can be
+   unmapped at any time and, unless you happen to be the shadow or p2m
+   implementations, there's no way of synchronising against that. */
 #define p2m_is_valid(_t) (p2m_to_mask(_t) & (P2M_RAM_TYPES | P2M_MMIO_TYPES))
 
 /* Populate-on-demand */
@@ -161,8 +181,12 @@
 /* Extract the type from the PTE flags that store it */
 static inline p2m_type_t p2m_flags_to_type(unsigned long flags)
 {
-    /* Type is stored in the "available" bits, 9, 10 and 11 */
+    /* Type is stored in the "available" bits */
+#ifdef __x86_64__
+    return (flags >> 9) & 0x3fff;
+#else
     return (flags >> 9) & 0x7;
+#endif
 }
 
 /* Read the current domain's p2m table.  Do not populate PoD pages. */
@@ -225,17 +249,6 @@
     else
         return mfn_x(mfn);
 }
-
-/* Translate the frame number held in an l1e from guest to machine */
-static inline l1_pgentry_t
-gl1e_to_ml1e(struct domain *d, l1_pgentry_t l1e)
-{
-    if ( unlikely(paging_mode_translate(d)) )
-        l1e = l1e_from_pfn(gmfn_to_mfn(d, l1e_get_pfn(l1e)),
-                           l1e_get_flags(l1e));
-    return l1e;
-}
-
 
 /* Init the datastructures for later use by the p2m code */
 int p2m_init(struct domain *d);

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