Skip to content

Commit f832da0

Browse files
author
Ian Campbell
committed
xen: arm: implement remap interfaces needed for privcmd mappings.
We use XENMEM_add_to_physmap_range which is the preferred interface for foreign mappings. Acked-by: Mukesh Rathor <mukesh.rathor@oracle.com> Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Signed-off-by: Ian Campbell <ian.campbell@citrix.com> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent 7892f69 commit f832da0

File tree

4 files changed

+141
-5
lines changed

4 files changed

+141
-5
lines changed

arch/arm/include/asm/xen/interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ DEFINE_GUEST_HANDLE(void);
4949
DEFINE_GUEST_HANDLE(uint64_t);
5050
DEFINE_GUEST_HANDLE(uint32_t);
5151
DEFINE_GUEST_HANDLE(xen_pfn_t);
52+
DEFINE_GUEST_HANDLE(xen_ulong_t);
5253

5354
/* Maximum number of virtual CPUs in multi-processor guests. */
5455
#define MAX_VIRT_CPUS 1

arch/arm/xen/enlighten.c

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <xen/platform_pci.h>
1010
#include <xen/xenbus.h>
1111
#include <xen/page.h>
12+
#include <xen/xen-ops.h>
1213
#include <asm/xen/hypervisor.h>
1314
#include <asm/xen/hypercall.h>
1415
#include <linux/interrupt.h>
@@ -18,6 +19,8 @@
1819
#include <linux/of_irq.h>
1920
#include <linux/of_address.h>
2021

22+
#include <linux/mm.h>
23+
2124
struct start_info _xen_start_info;
2225
struct start_info *xen_start_info = &_xen_start_info;
2326
EXPORT_SYMBOL_GPL(xen_start_info);
@@ -43,15 +46,106 @@ EXPORT_SYMBOL_GPL(xen_platform_pci_unplug);
4346

4447
static __read_mostly int xen_events_irq = -1;
4548

49+
/* map fgmfn of domid to lpfn in the current domain */
50+
static int map_foreign_page(unsigned long lpfn, unsigned long fgmfn,
51+
unsigned int domid)
52+
{
53+
int rc;
54+
struct xen_add_to_physmap_range xatp = {
55+
.domid = DOMID_SELF,
56+
.foreign_domid = domid,
57+
.size = 1,
58+
.space = XENMAPSPACE_gmfn_foreign,
59+
};
60+
xen_ulong_t idx = fgmfn;
61+
xen_pfn_t gpfn = lpfn;
62+
63+
set_xen_guest_handle(xatp.idxs, &idx);
64+
set_xen_guest_handle(xatp.gpfns, &gpfn);
65+
66+
rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
67+
if (rc) {
68+
pr_warn("Failed to map pfn to mfn rc:%d pfn:%lx mfn:%lx\n",
69+
rc, lpfn, fgmfn);
70+
return 1;
71+
}
72+
return 0;
73+
}
74+
75+
struct remap_data {
76+
xen_pfn_t fgmfn; /* foreign domain's gmfn */
77+
pgprot_t prot;
78+
domid_t domid;
79+
struct vm_area_struct *vma;
80+
int index;
81+
struct page **pages;
82+
struct xen_remap_mfn_info *info;
83+
};
84+
85+
static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
86+
void *data)
87+
{
88+
struct remap_data *info = data;
89+
struct page *page = info->pages[info->index++];
90+
unsigned long pfn = page_to_pfn(page);
91+
pte_t pte = pfn_pte(pfn, info->prot);
92+
93+
if (map_foreign_page(pfn, info->fgmfn, info->domid))
94+
return -EFAULT;
95+
set_pte_at(info->vma->vm_mm, addr, ptep, pte);
96+
97+
return 0;
98+
}
99+
46100
int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
47101
unsigned long addr,
48-
unsigned long mfn, int nr,
49-
pgprot_t prot, unsigned domid)
102+
xen_pfn_t mfn, int nr,
103+
pgprot_t prot, unsigned domid,
104+
struct page **pages)
50105
{
51-
return -ENOSYS;
106+
int err;
107+
struct remap_data data;
108+
109+
/* TBD: Batching, current sole caller only does page at a time */
110+
if (nr > 1)
111+
return -EINVAL;
112+
113+
data.fgmfn = mfn;
114+
data.prot = prot;
115+
data.domid = domid;
116+
data.vma = vma;
117+
data.index = 0;
118+
data.pages = pages;
119+
err = apply_to_page_range(vma->vm_mm, addr, nr << PAGE_SHIFT,
120+
remap_pte_fn, &data);
121+
return err;
52122
}
53123
EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
54124

125+
int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
126+
int nr, struct page **pages)
127+
{
128+
int i;
129+
130+
for (i = 0; i < nr; i++) {
131+
struct xen_remove_from_physmap xrp;
132+
unsigned long rc, pfn;
133+
134+
pfn = page_to_pfn(pages[i]);
135+
136+
xrp.domid = DOMID_SELF;
137+
xrp.gpfn = pfn;
138+
rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
139+
if (rc) {
140+
pr_warn("Failed to unmap pfn:%lx rc:%ld\n",
141+
pfn, rc);
142+
return rc;
143+
}
144+
}
145+
return 0;
146+
}
147+
EXPORT_SYMBOL_GPL(xen_unmap_domain_mfn_range);
148+
55149
/*
56150
* see Documentation/devicetree/bindings/arm/xen.txt for the
57151
* documentation of the Xen Device Tree format.

arch/x86/include/asm/xen/interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ DEFINE_GUEST_HANDLE(void);
6363
DEFINE_GUEST_HANDLE(uint64_t);
6464
DEFINE_GUEST_HANDLE(uint32_t);
6565
DEFINE_GUEST_HANDLE(xen_pfn_t);
66+
DEFINE_GUEST_HANDLE(xen_ulong_t);
6667
#endif
6768

6869
#ifndef HYPERVISOR_VIRT_START

include/xen/interface/memory.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ struct xen_machphys_mapping {
153153
};
154154
DEFINE_GUEST_HANDLE_STRUCT(xen_machphys_mapping_t);
155155

156+
#define XENMAPSPACE_shared_info 0 /* shared info page */
157+
#define XENMAPSPACE_grant_table 1 /* grant table page */
158+
#define XENMAPSPACE_gmfn 2 /* GMFN */
159+
#define XENMAPSPACE_gmfn_range 3 /* GMFN range, XENMEM_add_to_physmap only. */
160+
#define XENMAPSPACE_gmfn_foreign 4 /* GMFN from another dom,
161+
* XENMEM_add_to_physmap_range only.
162+
*/
163+
156164
/*
157165
* Sets the GPFN at which a particular page appears in the specified guest's
158166
* pseudophysical address space.
@@ -167,8 +175,6 @@ struct xen_add_to_physmap {
167175
uint16_t size;
168176

169177
/* Source mapping space. */
170-
#define XENMAPSPACE_shared_info 0 /* shared info page */
171-
#define XENMAPSPACE_grant_table 1 /* grant table page */
172178
unsigned int space;
173179

174180
/* Index into source mapping space. */
@@ -182,6 +188,24 @@ DEFINE_GUEST_HANDLE_STRUCT(xen_add_to_physmap);
182188
/*** REMOVED ***/
183189
/*#define XENMEM_translate_gpfn_list 8*/
184190

191+
#define XENMEM_add_to_physmap_range 23
192+
struct xen_add_to_physmap_range {
193+
/* Which domain to change the mapping for. */
194+
domid_t domid;
195+
uint16_t space; /* => enum phys_map_space */
196+
197+
/* Number of pages to go through */
198+
uint16_t size;
199+
domid_t foreign_domid; /* IFF gmfn_foreign */
200+
201+
/* Indexes into space being mapped. */
202+
GUEST_HANDLE(xen_ulong_t) idxs;
203+
204+
/* GPFN in domid where the source mapping page should appear. */
205+
GUEST_HANDLE(xen_pfn_t) gpfns;
206+
};
207+
DEFINE_GUEST_HANDLE_STRUCT(xen_add_to_physmap_range);
208+
185209
/*
186210
* Returns the pseudo-physical memory map as it was when the domain
187211
* was started (specified by XENMEM_set_memory_map).
@@ -217,4 +241,20 @@ DEFINE_GUEST_HANDLE_STRUCT(xen_memory_map);
217241
* during a driver critical region.
218242
*/
219243
extern spinlock_t xen_reservation_lock;
244+
245+
/*
246+
* Unmaps the page appearing at a particular GPFN from the specified guest's
247+
* pseudophysical address space.
248+
* arg == addr of xen_remove_from_physmap_t.
249+
*/
250+
#define XENMEM_remove_from_physmap 15
251+
struct xen_remove_from_physmap {
252+
/* Which domain to change the mapping for. */
253+
domid_t domid;
254+
255+
/* GPFN of the current mapping of the page. */
256+
xen_pfn_t gpfn;
257+
};
258+
DEFINE_GUEST_HANDLE_STRUCT(xen_remove_from_physmap);
259+
220260
#endif /* __XEN_PUBLIC_MEMORY_H__ */

0 commit comments

Comments
 (0)