@@ -214,6 +214,79 @@ err:
return maddr;
}
+int guest_physmap_pin_range(struct domain *d,
+ xen_pfn_t mfn,
+ unsigned int order)
+{
+ int i;
+ struct page_info *page;
+
+ for ( i = 0; i < (1UL << order); i++ )
+ {
+ if ( !mfn_valid(mfn + i) )
+ return -EINVAL;
+
+ page = mfn_to_page(mfn + i);
+ if ( !page )
+ return -EINVAL;
+
+ if ( !get_page_type(page, PGT_writable_page) )
+ return -EINVAL;
+
+ if ( test_and_set_bit(_PGC_p2m_pinned, &page->count_info) )
+ return -EBUSY;
+ }
+ return 0;
+}
+
+int guest_physmap_unpin_range(struct domain *d,
+ xen_pfn_t mfn,
+ unsigned int order)
+{
+ int i;
+ struct page_info *page;
+
+ for ( i = 0; i < (1UL << order); i++ )
+ {
+ if ( !mfn_valid(mfn + i) )
+ return -EINVAL;
+
+ page = mfn_to_page(mfn + i);
+ if ( !page )
+ return -EINVAL;
+
+ if ( !test_and_clear_bit(_PGC_p2m_pinned, &page->count_info) )
+ return -EINVAL;
+ }
+ return 0;
+}
+
+int guest_physmap_pinned_range(struct domain *d,
+ xen_pfn_t mfn,
+ unsigned int order)
+{
+ int i, pins = 0;
+ struct page_info *page;
+
+ for ( i = 0; i < (1UL << order); i++ )
+ {
+ if ( !mfn_valid(mfn + i) )
+ return 0;
+
+ page = mfn_to_page(mfn + i);
+ if ( !page )
+ return 0;
+
+ if ( test_bit(_PGC_p2m_pinned, &page->count_info) )
+ pins++;
+ }
+
+ if ( i && (i == pins) )
+ return 1;
+
+ return 0;
+}
+
int guest_physmap_mark_populate_on_demand(struct domain *d,
unsigned long gfn,
unsigned int order)
@@ -478,10 +551,18 @@ static int apply_one_level(struct domain *d,
struct p2m_domain *p2m = &d->arch.p2m;
lpae_t pte;
const lpae_t orig_pte = *entry;
+ struct page_info *page = NULL;
int rc;
BUG_ON(level > 3);
+ if ( guest_physmap_pinned_range(d, orig_pte.p2m.base, 0) )
+ {
+ gdprintk(XENLOG_WARNING, "cannot change p2m mapping for paddr=%"PRIpaddr
+ " domid=%d, the page is pinned count_info %lu\n", *addr, d->domain_id, page->count_info);
+ return -EINVAL;
+ }
+
switch ( op )
{
case ALLOCATE:
@@ -819,6 +900,7 @@ static int apply_p2m_changes(struct domain *d,
&addr, &maddr, &flush,
mattr, t);
if ( ret < 0 ) { rc = ret ; goto out; }
+
/* L3 had better have done something! We cannot descend any further */
BUG_ON(ret == P2M_ONE_DESCEND);
count += ret;
@@ -92,6 +92,10 @@ struct page_info
/* Page is Xen heap? */
#define _PGC_xen_heap PG_shift(2)
#define PGC_xen_heap PG_mask(1, 2)
+/* The page belongs to a guest and it has been pinned. */
+#define _PGC_p2m_pinned PG_shift(3)
+#define PGC_p2m_pinned PG_mask(1, 3)
+
/* ... */
/* Page is broken? */
#define _PGC_broken PG_shift(7)
@@ -340,6 +344,13 @@ 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, paddr_t mfn,
+ unsigned int order);
+int guest_physmap_unpin_range(struct domain *d, paddr_t mfn,
+ unsigned int order);
+int guest_physmap_pinned_range(struct domain *d, paddr_t mfn,
+ unsigned int order);
+
extern void put_page_type(struct page_info *page);
static inline void put_page_and_type(struct page_info *page)
{
@@ -514,6 +514,26 @@ void guest_physmap_remove_page(struct domain *d,
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,
+ paddr_t mfn,
+ unsigned int order)
+{
+ return -ENOSYS;
+}
+static inline int guest_physmap_unpin_range(struct domain *d,
+ paddr_t mfn,
+ unsigned int order)
+{
+ return -ENOSYS;
+}
+
+static inline int guest_physmap_pinned_range(struct domain *d,
+ paddr_t mfn,
+ unsigned int order)
+{
+ return -ENOSYS;
+}
+
/* Change types across all p2m entries in a domain */
void p2m_change_entry_type_global(struct domain *d,
p2m_type_t ot, p2m_type_t nt);