xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: tim@xen.org, Ian.Campbell@citrix.com,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Subject: [PATCH v3 2/3] xen: implement guest_physmap_(un)pin_range
Date: Mon, 5 Aug 2013 17:40:55 +0100	[thread overview]
Message-ID: <1375720856-8593-2-git-send-email-stefano.stabellini@eu.citrix.com> (raw)
In-Reply-To: <alpine.DEB.2.02.1308051731100.4893@kaball.uk.xensource.com>

guest_physmap_pin_range pins a range of guest pages so that they p2m
mappings won't be changed.
guest_physmap_unpin_range unpins the previously pinned pages.

The pinning is done using one of the spare bits in the p2m ptes.

Provide empty stubs for x86.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
 xen/arch/arm/p2m.c         |  131 ++++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/mm.h   |    4 +
 xen/include/asm-arm/page.h |    6 ++-
 xen/include/asm-x86/p2m.h  |   12 ++++
 4 files changed, 152 insertions(+), 1 deletions(-)

diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c
index 307c6d4..7543ca8 100644
--- a/xen/arch/arm/p2m.c
+++ b/xen/arch/arm/p2m.c
@@ -75,6 +75,129 @@ done:
     return maddr;
 }
 
+
+int guest_physmap_pin_range(struct domain *d,
+                            xen_pfn_t gpfn,
+                            unsigned int order)
+{
+    lpae_t *first = NULL, *second = NULL, *third = NULL, pte;
+    paddr_t addr = gpfn << PAGE_SHIFT;
+    struct p2m_domain *p2m = &d->arch.p2m;
+    int rc = -EFAULT, i;
+    unsigned long cur_first_offset = ~0, cur_second_offset = ~0;
+
+    spin_lock(&p2m->lock);
+
+    first = __map_domain_page(p2m->first_level);
+
+    if ( !first ||
+            !first[first_table_offset(addr)].p2m.valid ||
+            !first[first_table_offset(addr)].p2m.table )
+        goto err;
+
+    for ( i = 0; i < (1UL << order); i++ )
+    {
+        if ( cur_first_offset != first_table_offset(addr) )
+        {
+            if (second) unmap_domain_page(second);
+            second = map_domain_page(first[first_table_offset(addr)].p2m.base);
+            cur_first_offset = first_table_offset(addr);
+        }
+        if ( !second ||
+                !second[second_table_offset(addr)].p2m.valid ||
+                !second[second_table_offset(addr)].p2m.table )
+            goto err;
+
+        if ( cur_second_offset != second_table_offset(addr) )
+        {
+            if (third) unmap_domain_page(third);
+            third = map_domain_page(second[second_table_offset(addr)].p2m.base);
+            cur_second_offset = second_table_offset(addr);
+        }
+        if ( !third ||
+                !third[third_table_offset(addr)].p2m.valid ||
+                third[third_table_offset(addr)].p2m.avail & P2M_DMA_PIN )
+            goto err;
+
+        pte = third[third_table_offset(addr)];
+        pte.p2m.avail |= P2M_DMA_PIN;
+        write_pte(&third[third_table_offset(addr)], pte);
+
+        addr += PAGE_SIZE;
+    }
+
+    rc = 0;
+
+err:
+    if ( second ) unmap_domain_page(second);
+    if ( first ) unmap_domain_page(first);
+
+    spin_unlock(&p2m->lock);
+
+    return rc;
+}
+
+int guest_physmap_unpin_range(struct domain *d,
+                              xen_pfn_t gpfn,
+                              unsigned int order)
+{
+    lpae_t *first = NULL, *second = NULL, *third = NULL, pte;
+    paddr_t addr = gpfn << PAGE_SHIFT;
+    struct p2m_domain *p2m = &d->arch.p2m;
+    int rc = -EFAULT, i;
+    unsigned long cur_first_offset = ~0, cur_second_offset = ~0;
+
+    spin_lock(&p2m->lock);
+
+    first = __map_domain_page(p2m->first_level);
+
+    if ( !first ||
+            !first[first_table_offset(addr)].p2m.valid ||
+            !first[first_table_offset(addr)].p2m.table )
+        goto err;
+
+    for ( i = 0; i < (1UL << order); i++ )
+    {
+        if ( cur_first_offset != first_table_offset(addr) )
+        {
+            if (second) unmap_domain_page(second);
+            second = map_domain_page(first[first_table_offset(addr)].p2m.base);
+            cur_first_offset = first_table_offset(addr);
+        }
+        if ( !second ||
+                !second[second_table_offset(addr)].p2m.valid ||
+                !second[second_table_offset(addr)].p2m.table )
+            goto err;
+
+        if ( cur_second_offset != second_table_offset(addr) )
+        {
+            if (third) unmap_domain_page(third);
+            third = map_domain_page(second[second_table_offset(addr)].p2m.base);
+            cur_second_offset = second_table_offset(addr);
+        }
+        if ( !third ||
+                !third[third_table_offset(addr)].p2m.valid ||
+                !(third[third_table_offset(addr)].p2m.avail & P2M_DMA_PIN) )
+            goto err;
+
+        pte = third[third_table_offset(addr)];
+        pte.p2m.avail &= ~P2M_DMA_PIN;
+        write_pte(&third[third_table_offset(addr)], pte);
+
+        addr += PAGE_SIZE;
+    }
+
+    rc = 0;
+
+err:
+    if ( second ) unmap_domain_page(second);
+    if ( first ) unmap_domain_page(first);
+
+    spin_unlock(&p2m->lock);
+
+    return rc;
+}
+
 int guest_physmap_mark_populate_on_demand(struct domain *d,
                                           unsigned long gfn,
                                           unsigned int order)
@@ -184,6 +307,14 @@ static int create_p2m_entries(struct domain *d,
             cur_second_offset = second_table_offset(addr);
         }
 
+        if ( third[third_table_offset(addr)].p2m.avail & P2M_DMA_PIN )
+        {
+            rc = -EINVAL;
+            printk("%s: cannot change p2m mapping for paddr=%"PRIpaddr
+                    " domid=%d, the page is pinned\n", __func__, addr, d->domain_id);
+            goto out;
+        }
+
         flush = third[third_table_offset(addr)].p2m.valid;
 
         /* Allocate a new RAM page and attach */
diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index 5e7c5a3..8db8816 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -319,6 +319,10 @@ void free_init_memory(void);
 
 int guest_physmap_mark_populate_on_demand(struct domain *d, unsigned long gfn,
                                           unsigned int order);
+int guest_physmap_pin_range(struct domain *d, xen_pfn_t gpfn,
+                            unsigned int order);
+int guest_physmap_unpin_range(struct domain *d, xen_pfn_t gpfn,
+                              unsigned int order);
 
 extern void put_page_type(struct page_info *page);
 static inline void put_page_and_type(struct page_info *page)
diff --git a/xen/include/asm-arm/page.h b/xen/include/asm-arm/page.h
index 41e9eff..12087d0 100644
--- a/xen/include/asm-arm/page.h
+++ b/xen/include/asm-arm/page.h
@@ -153,11 +153,15 @@ typedef struct {
     unsigned long hint:1;       /* In a block of 16 contiguous entries */
     unsigned long sbz2:1;
     unsigned long xn:1;         /* eXecute-Never */
-    unsigned long avail:4;      /* Ignored by hardware */
+    unsigned long avail:4;      /* Ignored by hardware, see below */
 
     unsigned long sbz1:5;
 } __attribute__((__packed__)) lpae_p2m_t;
 
+/* Xen "avail" bits allocation in third level entries */
+#define P2M_DMA_PIN     (1<<0)  /* The page has been "pinned": the hypervisor
+                                   promises not to change the p2m mapping. */
+
 /*
  * Walk is the common bits of p2m and pt entries which are needed to
  * simply walk the table (e.g. for debug).
diff --git a/xen/include/asm-x86/p2m.h b/xen/include/asm-x86/p2m.h
index 43583b2..afc7738 100644
--- a/xen/include/asm-x86/p2m.h
+++ b/xen/include/asm-x86/p2m.h
@@ -492,6 +492,18 @@ void guest_physmap_remove_page(struct domain *d,
 /* Set a p2m range as populate-on-demand */
 int guest_physmap_mark_populate_on_demand(struct domain *d, unsigned long gfn,
                                           unsigned int order);
+static inline int guest_physmap_pin_range(struct domain *d,
+                                          xen_pfn_t gpfn,
+                                          unsigned int order)
+{
+    return -ENOSYS;
+}
+int guest_physmap_unpin_range(struct domain *d,
+                              xen_pfn_t gpfn,
+                              unsigned int order)
+{
+    return -ENOSYS;
+}
 
 /* Change types across all p2m entries in a domain */
 void p2m_change_entry_type_global(struct domain *d, 
-- 
1.7.2.5

  parent reply	other threads:[~2013-08-05 16:40 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-05 16:40 [PATCH v3 0/3] introduce XENMEM_get_dma_buf and XENMEM_put_dma_buf Stefano Stabellini
2013-08-05 16:40 ` [PATCH v3 1/3] xen/arm: implement steal_page Stefano Stabellini
2013-08-08  9:55   ` Ian Campbell
2013-08-08 10:37     ` Stefano Stabellini
2013-08-09 15:46   ` Konrad Rzeszutek Wilk
2013-08-14 13:40     ` Stefano Stabellini
2013-08-05 16:40 ` Stefano Stabellini [this message]
2013-08-08 10:52   ` [PATCH v3 2/3] xen: implement guest_physmap_(un)pin_range Ian Campbell
2013-08-14 16:08     ` Stefano Stabellini
2013-08-15 14:32       ` Ian Campbell
2013-08-05 16:40 ` [PATCH v3 3/3] xen: introduce XENMEM_get_dma_buf and XENMEM_put_dma_buf Stefano Stabellini
2013-08-08 10:40   ` Ian Campbell
2013-08-08 14:12     ` Keir Fraser
2013-08-08 14:16       ` Stefano Stabellini
2013-08-08 14:26         ` Tim Deegan
2013-08-08 15:04           ` Stefano Stabellini
2013-08-08 14:27         ` Ian Campbell
2013-08-08 14:47         ` Keir Fraser
2013-08-13 16:05     ` Stefano Stabellini
2013-08-13 22:07       ` Ian Campbell
2013-08-08 11:43   ` Jan Beulich
2013-08-08 14:12     ` Stefano Stabellini
2013-08-08 14:38       ` Jan Beulich
2013-08-08 14:44         ` Stefano Stabellini
2013-08-08 14:50           ` Jan Beulich
2013-08-08 14:54             ` Stefano Stabellini
2013-08-08 14:58               ` Jan Beulich
2013-08-08 15:25   ` Ian Campbell
2013-08-14 16:17     ` Stefano Stabellini
2013-08-15 14:32       ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1375720856-8593-2-git-send-email-stefano.stabellini@eu.citrix.com \
    --to=stefano.stabellini@eu.citrix.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).