xref: /openbmc/linux/drivers/xen/privcmd.c (revision 4e8c0c8c4bf3a5b5c98046e146ab3884bf7a7d0e)
1d8414d3cSBastian Blank /******************************************************************************
2d8414d3cSBastian Blank  * privcmd.c
3d8414d3cSBastian Blank  *
4d8414d3cSBastian Blank  * Interface to privileged domain-0 commands.
5d8414d3cSBastian Blank  *
6d8414d3cSBastian Blank  * Copyright (c) 2002-2004, K A Fraser, B Dragovic
7d8414d3cSBastian Blank  */
8d8414d3cSBastian Blank 
9283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
10283c0972SJoe Perches 
11d8414d3cSBastian Blank #include <linux/kernel.h>
12d8414d3cSBastian Blank #include <linux/module.h>
13d8414d3cSBastian Blank #include <linux/sched.h>
14d8414d3cSBastian Blank #include <linux/slab.h>
15d8414d3cSBastian Blank #include <linux/string.h>
16d8414d3cSBastian Blank #include <linux/errno.h>
17d8414d3cSBastian Blank #include <linux/mm.h>
18d8414d3cSBastian Blank #include <linux/mman.h>
19d8414d3cSBastian Blank #include <linux/uaccess.h>
20d8414d3cSBastian Blank #include <linux/swap.h>
21d8414d3cSBastian Blank #include <linux/highmem.h>
22d8414d3cSBastian Blank #include <linux/pagemap.h>
23d8414d3cSBastian Blank #include <linux/seq_file.h>
24d8414d3cSBastian Blank #include <linux/miscdevice.h>
25d8414d3cSBastian Blank 
26d8414d3cSBastian Blank #include <asm/pgalloc.h>
27d8414d3cSBastian Blank #include <asm/pgtable.h>
28d8414d3cSBastian Blank #include <asm/tlb.h>
29d8414d3cSBastian Blank #include <asm/xen/hypervisor.h>
30d8414d3cSBastian Blank #include <asm/xen/hypercall.h>
31d8414d3cSBastian Blank 
32d8414d3cSBastian Blank #include <xen/xen.h>
33d8414d3cSBastian Blank #include <xen/privcmd.h>
34d8414d3cSBastian Blank #include <xen/interface/xen.h>
35d8414d3cSBastian Blank #include <xen/features.h>
36d8414d3cSBastian Blank #include <xen/page.h>
37d8414d3cSBastian Blank #include <xen/xen-ops.h>
38d71f5139SMukesh Rathor #include <xen/balloon.h>
39d8414d3cSBastian Blank 
40d8414d3cSBastian Blank #include "privcmd.h"
41d8414d3cSBastian Blank 
42d8414d3cSBastian Blank MODULE_LICENSE("GPL");
43d8414d3cSBastian Blank 
44d71f5139SMukesh Rathor #define PRIV_VMA_LOCKED ((void *)1)
45d71f5139SMukesh Rathor 
46a5deabe0SAndres Lagar-Cavilla static int privcmd_vma_range_is_mapped(
47a5deabe0SAndres Lagar-Cavilla                struct vm_area_struct *vma,
48a5deabe0SAndres Lagar-Cavilla                unsigned long addr,
49a5deabe0SAndres Lagar-Cavilla                unsigned long nr_pages);
50d8414d3cSBastian Blank 
51d8414d3cSBastian Blank static long privcmd_ioctl_hypercall(void __user *udata)
52d8414d3cSBastian Blank {
53d8414d3cSBastian Blank 	struct privcmd_hypercall hypercall;
54d8414d3cSBastian Blank 	long ret;
55d8414d3cSBastian Blank 
56d8414d3cSBastian Blank 	if (copy_from_user(&hypercall, udata, sizeof(hypercall)))
57d8414d3cSBastian Blank 		return -EFAULT;
58d8414d3cSBastian Blank 
59fdfd811dSDavid Vrabel 	xen_preemptible_hcall_begin();
60d8414d3cSBastian Blank 	ret = privcmd_call(hypercall.op,
61d8414d3cSBastian Blank 			   hypercall.arg[0], hypercall.arg[1],
62d8414d3cSBastian Blank 			   hypercall.arg[2], hypercall.arg[3],
63d8414d3cSBastian Blank 			   hypercall.arg[4]);
64fdfd811dSDavid Vrabel 	xen_preemptible_hcall_end();
65d8414d3cSBastian Blank 
66d8414d3cSBastian Blank 	return ret;
67d8414d3cSBastian Blank }
68d8414d3cSBastian Blank 
69d8414d3cSBastian Blank static void free_page_list(struct list_head *pages)
70d8414d3cSBastian Blank {
71d8414d3cSBastian Blank 	struct page *p, *n;
72d8414d3cSBastian Blank 
73d8414d3cSBastian Blank 	list_for_each_entry_safe(p, n, pages, lru)
74d8414d3cSBastian Blank 		__free_page(p);
75d8414d3cSBastian Blank 
76d8414d3cSBastian Blank 	INIT_LIST_HEAD(pages);
77d8414d3cSBastian Blank }
78d8414d3cSBastian Blank 
79d8414d3cSBastian Blank /*
80d8414d3cSBastian Blank  * Given an array of items in userspace, return a list of pages
81d8414d3cSBastian Blank  * containing the data.  If copying fails, either because of memory
82d8414d3cSBastian Blank  * allocation failure or a problem reading user memory, return an
83d8414d3cSBastian Blank  * error code; its up to the caller to dispose of any partial list.
84d8414d3cSBastian Blank  */
85d8414d3cSBastian Blank static int gather_array(struct list_head *pagelist,
86d8414d3cSBastian Blank 			unsigned nelem, size_t size,
87ceb90fa0SAndres Lagar-Cavilla 			const void __user *data)
88d8414d3cSBastian Blank {
89d8414d3cSBastian Blank 	unsigned pageidx;
90d8414d3cSBastian Blank 	void *pagedata;
91d8414d3cSBastian Blank 	int ret;
92d8414d3cSBastian Blank 
93d8414d3cSBastian Blank 	if (size > PAGE_SIZE)
94d8414d3cSBastian Blank 		return 0;
95d8414d3cSBastian Blank 
96d8414d3cSBastian Blank 	pageidx = PAGE_SIZE;
97d8414d3cSBastian Blank 	pagedata = NULL;	/* quiet, gcc */
98d8414d3cSBastian Blank 	while (nelem--) {
99d8414d3cSBastian Blank 		if (pageidx > PAGE_SIZE-size) {
100d8414d3cSBastian Blank 			struct page *page = alloc_page(GFP_KERNEL);
101d8414d3cSBastian Blank 
102d8414d3cSBastian Blank 			ret = -ENOMEM;
103d8414d3cSBastian Blank 			if (page == NULL)
104d8414d3cSBastian Blank 				goto fail;
105d8414d3cSBastian Blank 
106d8414d3cSBastian Blank 			pagedata = page_address(page);
107d8414d3cSBastian Blank 
108d8414d3cSBastian Blank 			list_add_tail(&page->lru, pagelist);
109d8414d3cSBastian Blank 			pageidx = 0;
110d8414d3cSBastian Blank 		}
111d8414d3cSBastian Blank 
112d8414d3cSBastian Blank 		ret = -EFAULT;
113d8414d3cSBastian Blank 		if (copy_from_user(pagedata + pageidx, data, size))
114d8414d3cSBastian Blank 			goto fail;
115d8414d3cSBastian Blank 
116d8414d3cSBastian Blank 		data += size;
117d8414d3cSBastian Blank 		pageidx += size;
118d8414d3cSBastian Blank 	}
119d8414d3cSBastian Blank 
120d8414d3cSBastian Blank 	ret = 0;
121d8414d3cSBastian Blank 
122d8414d3cSBastian Blank fail:
123d8414d3cSBastian Blank 	return ret;
124d8414d3cSBastian Blank }
125d8414d3cSBastian Blank 
126d8414d3cSBastian Blank /*
127d8414d3cSBastian Blank  * Call function "fn" on each element of the array fragmented
128d8414d3cSBastian Blank  * over a list of pages.
129d8414d3cSBastian Blank  */
130d8414d3cSBastian Blank static int traverse_pages(unsigned nelem, size_t size,
131d8414d3cSBastian Blank 			  struct list_head *pos,
132d8414d3cSBastian Blank 			  int (*fn)(void *data, void *state),
133d8414d3cSBastian Blank 			  void *state)
134d8414d3cSBastian Blank {
135d8414d3cSBastian Blank 	void *pagedata;
136d8414d3cSBastian Blank 	unsigned pageidx;
137d8414d3cSBastian Blank 	int ret = 0;
138d8414d3cSBastian Blank 
139d8414d3cSBastian Blank 	BUG_ON(size > PAGE_SIZE);
140d8414d3cSBastian Blank 
141d8414d3cSBastian Blank 	pageidx = PAGE_SIZE;
142d8414d3cSBastian Blank 	pagedata = NULL;	/* hush, gcc */
143d8414d3cSBastian Blank 
144d8414d3cSBastian Blank 	while (nelem--) {
145d8414d3cSBastian Blank 		if (pageidx > PAGE_SIZE-size) {
146d8414d3cSBastian Blank 			struct page *page;
147d8414d3cSBastian Blank 			pos = pos->next;
148d8414d3cSBastian Blank 			page = list_entry(pos, struct page, lru);
149d8414d3cSBastian Blank 			pagedata = page_address(page);
150d8414d3cSBastian Blank 			pageidx = 0;
151d8414d3cSBastian Blank 		}
152d8414d3cSBastian Blank 
153d8414d3cSBastian Blank 		ret = (*fn)(pagedata + pageidx, state);
154d8414d3cSBastian Blank 		if (ret)
155d8414d3cSBastian Blank 			break;
156d8414d3cSBastian Blank 		pageidx += size;
157d8414d3cSBastian Blank 	}
158d8414d3cSBastian Blank 
159d8414d3cSBastian Blank 	return ret;
160d8414d3cSBastian Blank }
161d8414d3cSBastian Blank 
162*4e8c0c8cSDavid Vrabel /*
163*4e8c0c8cSDavid Vrabel  * Similar to traverse_pages, but use each page as a "block" of
164*4e8c0c8cSDavid Vrabel  * data to be processed as one unit.
165*4e8c0c8cSDavid Vrabel  */
166*4e8c0c8cSDavid Vrabel static int traverse_pages_block(unsigned nelem, size_t size,
167*4e8c0c8cSDavid Vrabel 				struct list_head *pos,
168*4e8c0c8cSDavid Vrabel 				int (*fn)(void *data, int nr, void *state),
169*4e8c0c8cSDavid Vrabel 				void *state)
170*4e8c0c8cSDavid Vrabel {
171*4e8c0c8cSDavid Vrabel 	void *pagedata;
172*4e8c0c8cSDavid Vrabel 	unsigned pageidx;
173*4e8c0c8cSDavid Vrabel 	int ret = 0;
174*4e8c0c8cSDavid Vrabel 
175*4e8c0c8cSDavid Vrabel 	BUG_ON(size > PAGE_SIZE);
176*4e8c0c8cSDavid Vrabel 
177*4e8c0c8cSDavid Vrabel 	pageidx = PAGE_SIZE;
178*4e8c0c8cSDavid Vrabel 
179*4e8c0c8cSDavid Vrabel 	while (nelem) {
180*4e8c0c8cSDavid Vrabel 		int nr = (PAGE_SIZE/size);
181*4e8c0c8cSDavid Vrabel 		struct page *page;
182*4e8c0c8cSDavid Vrabel 		if (nr > nelem)
183*4e8c0c8cSDavid Vrabel 			nr = nelem;
184*4e8c0c8cSDavid Vrabel 		pos = pos->next;
185*4e8c0c8cSDavid Vrabel 		page = list_entry(pos, struct page, lru);
186*4e8c0c8cSDavid Vrabel 		pagedata = page_address(page);
187*4e8c0c8cSDavid Vrabel 		ret = (*fn)(pagedata, nr, state);
188*4e8c0c8cSDavid Vrabel 		if (ret)
189*4e8c0c8cSDavid Vrabel 			break;
190*4e8c0c8cSDavid Vrabel 		nelem -= nr;
191*4e8c0c8cSDavid Vrabel 	}
192*4e8c0c8cSDavid Vrabel 
193*4e8c0c8cSDavid Vrabel 	return ret;
194*4e8c0c8cSDavid Vrabel }
195*4e8c0c8cSDavid Vrabel 
196d8414d3cSBastian Blank struct mmap_mfn_state {
197d8414d3cSBastian Blank 	unsigned long va;
198d8414d3cSBastian Blank 	struct vm_area_struct *vma;
199d8414d3cSBastian Blank 	domid_t domain;
200d8414d3cSBastian Blank };
201d8414d3cSBastian Blank 
202d8414d3cSBastian Blank static int mmap_mfn_range(void *data, void *state)
203d8414d3cSBastian Blank {
204d8414d3cSBastian Blank 	struct privcmd_mmap_entry *msg = data;
205d8414d3cSBastian Blank 	struct mmap_mfn_state *st = state;
206d8414d3cSBastian Blank 	struct vm_area_struct *vma = st->vma;
207d8414d3cSBastian Blank 	int rc;
208d8414d3cSBastian Blank 
209d8414d3cSBastian Blank 	/* Do not allow range to wrap the address space. */
210d8414d3cSBastian Blank 	if ((msg->npages > (LONG_MAX >> PAGE_SHIFT)) ||
211d8414d3cSBastian Blank 	    ((unsigned long)(msg->npages << PAGE_SHIFT) >= -st->va))
212d8414d3cSBastian Blank 		return -EINVAL;
213d8414d3cSBastian Blank 
214d8414d3cSBastian Blank 	/* Range chunks must be contiguous in va space. */
215d8414d3cSBastian Blank 	if ((msg->va != st->va) ||
216d8414d3cSBastian Blank 	    ((msg->va+(msg->npages<<PAGE_SHIFT)) > vma->vm_end))
217d8414d3cSBastian Blank 		return -EINVAL;
218d8414d3cSBastian Blank 
219d8414d3cSBastian Blank 	rc = xen_remap_domain_mfn_range(vma,
220d8414d3cSBastian Blank 					msg->va & PAGE_MASK,
221d8414d3cSBastian Blank 					msg->mfn, msg->npages,
222d8414d3cSBastian Blank 					vma->vm_page_prot,
2239a032e39SIan Campbell 					st->domain, NULL);
224d8414d3cSBastian Blank 	if (rc < 0)
225d8414d3cSBastian Blank 		return rc;
226d8414d3cSBastian Blank 
227d8414d3cSBastian Blank 	st->va += msg->npages << PAGE_SHIFT;
228d8414d3cSBastian Blank 
229d8414d3cSBastian Blank 	return 0;
230d8414d3cSBastian Blank }
231d8414d3cSBastian Blank 
232d8414d3cSBastian Blank static long privcmd_ioctl_mmap(void __user *udata)
233d8414d3cSBastian Blank {
234d8414d3cSBastian Blank 	struct privcmd_mmap mmapcmd;
235d8414d3cSBastian Blank 	struct mm_struct *mm = current->mm;
236d8414d3cSBastian Blank 	struct vm_area_struct *vma;
237d8414d3cSBastian Blank 	int rc;
238d8414d3cSBastian Blank 	LIST_HEAD(pagelist);
239d8414d3cSBastian Blank 	struct mmap_mfn_state state;
240d8414d3cSBastian Blank 
241d71f5139SMukesh Rathor 	/* We only support privcmd_ioctl_mmap_batch for auto translated. */
242d71f5139SMukesh Rathor 	if (xen_feature(XENFEAT_auto_translated_physmap))
243d71f5139SMukesh Rathor 		return -ENOSYS;
244d71f5139SMukesh Rathor 
245d8414d3cSBastian Blank 	if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
246d8414d3cSBastian Blank 		return -EFAULT;
247d8414d3cSBastian Blank 
248d8414d3cSBastian Blank 	rc = gather_array(&pagelist,
249d8414d3cSBastian Blank 			  mmapcmd.num, sizeof(struct privcmd_mmap_entry),
250d8414d3cSBastian Blank 			  mmapcmd.entry);
251d8414d3cSBastian Blank 
252d8414d3cSBastian Blank 	if (rc || list_empty(&pagelist))
253d8414d3cSBastian Blank 		goto out;
254d8414d3cSBastian Blank 
255d8414d3cSBastian Blank 	down_write(&mm->mmap_sem);
256d8414d3cSBastian Blank 
257d8414d3cSBastian Blank 	{
258d8414d3cSBastian Blank 		struct page *page = list_first_entry(&pagelist,
259d8414d3cSBastian Blank 						     struct page, lru);
260d8414d3cSBastian Blank 		struct privcmd_mmap_entry *msg = page_address(page);
261d8414d3cSBastian Blank 
262d8414d3cSBastian Blank 		vma = find_vma(mm, msg->va);
263d8414d3cSBastian Blank 		rc = -EINVAL;
264d8414d3cSBastian Blank 
265a5deabe0SAndres Lagar-Cavilla 		if (!vma || (msg->va != vma->vm_start) || vma->vm_private_data)
266d8414d3cSBastian Blank 			goto out_up;
267a5deabe0SAndres Lagar-Cavilla 		vma->vm_private_data = PRIV_VMA_LOCKED;
268d8414d3cSBastian Blank 	}
269d8414d3cSBastian Blank 
270d8414d3cSBastian Blank 	state.va = vma->vm_start;
271d8414d3cSBastian Blank 	state.vma = vma;
272d8414d3cSBastian Blank 	state.domain = mmapcmd.dom;
273d8414d3cSBastian Blank 
274d8414d3cSBastian Blank 	rc = traverse_pages(mmapcmd.num, sizeof(struct privcmd_mmap_entry),
275d8414d3cSBastian Blank 			    &pagelist,
276d8414d3cSBastian Blank 			    mmap_mfn_range, &state);
277d8414d3cSBastian Blank 
278d8414d3cSBastian Blank 
279d8414d3cSBastian Blank out_up:
280d8414d3cSBastian Blank 	up_write(&mm->mmap_sem);
281d8414d3cSBastian Blank 
282d8414d3cSBastian Blank out:
283d8414d3cSBastian Blank 	free_page_list(&pagelist);
284d8414d3cSBastian Blank 
285d8414d3cSBastian Blank 	return rc;
286d8414d3cSBastian Blank }
287d8414d3cSBastian Blank 
288d8414d3cSBastian Blank struct mmap_batch_state {
289d8414d3cSBastian Blank 	domid_t domain;
290d8414d3cSBastian Blank 	unsigned long va;
291d8414d3cSBastian Blank 	struct vm_area_struct *vma;
292d71f5139SMukesh Rathor 	int index;
293ceb90fa0SAndres Lagar-Cavilla 	/* A tristate:
294ceb90fa0SAndres Lagar-Cavilla 	 *      0 for no errors
295ceb90fa0SAndres Lagar-Cavilla 	 *      1 if at least one error has happened (and no
296ceb90fa0SAndres Lagar-Cavilla 	 *          -ENOENT errors have happened)
297ceb90fa0SAndres Lagar-Cavilla 	 *      -ENOENT if at least 1 -ENOENT has happened.
298ceb90fa0SAndres Lagar-Cavilla 	 */
299ceb90fa0SAndres Lagar-Cavilla 	int global_error;
30099beae6cSAndres Lagar-Cavilla 	int version;
301d8414d3cSBastian Blank 
302ceb90fa0SAndres Lagar-Cavilla 	/* User-space mfn array to store errors in the second pass for V1. */
303ceb90fa0SAndres Lagar-Cavilla 	xen_pfn_t __user *user_mfn;
30499beae6cSAndres Lagar-Cavilla 	/* User-space int array to store errors in the second pass for V2. */
30599beae6cSAndres Lagar-Cavilla 	int __user *user_err;
306d8414d3cSBastian Blank };
307d8414d3cSBastian Blank 
308d71f5139SMukesh Rathor /* auto translated dom0 note: if domU being created is PV, then mfn is
309d71f5139SMukesh Rathor  * mfn(addr on bus). If it's auto xlated, then mfn is pfn (input to HAP).
310d71f5139SMukesh Rathor  */
311*4e8c0c8cSDavid Vrabel static int mmap_batch_fn(void *data, int nr, void *state)
312d8414d3cSBastian Blank {
313d8414d3cSBastian Blank 	xen_pfn_t *mfnp = data;
314d8414d3cSBastian Blank 	struct mmap_batch_state *st = state;
315d71f5139SMukesh Rathor 	struct vm_area_struct *vma = st->vma;
316d71f5139SMukesh Rathor 	struct page **pages = vma->vm_private_data;
317*4e8c0c8cSDavid Vrabel 	struct page **cur_pages = NULL;
318ceb90fa0SAndres Lagar-Cavilla 	int ret;
319d8414d3cSBastian Blank 
320d71f5139SMukesh Rathor 	if (xen_feature(XENFEAT_auto_translated_physmap))
321*4e8c0c8cSDavid Vrabel 		cur_pages = &pages[st->index];
322d71f5139SMukesh Rathor 
323*4e8c0c8cSDavid Vrabel 	BUG_ON(nr < 0);
324*4e8c0c8cSDavid Vrabel 	ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
325*4e8c0c8cSDavid Vrabel 					 (int *)mfnp, st->vma->vm_page_prot,
326*4e8c0c8cSDavid Vrabel 					 st->domain, cur_pages);
327ceb90fa0SAndres Lagar-Cavilla 
328*4e8c0c8cSDavid Vrabel 	/* Adjust the global_error? */
329*4e8c0c8cSDavid Vrabel 	if (ret != nr) {
330ceb90fa0SAndres Lagar-Cavilla 		if (ret == -ENOENT)
331ceb90fa0SAndres Lagar-Cavilla 			st->global_error = -ENOENT;
332ceb90fa0SAndres Lagar-Cavilla 		else {
333ceb90fa0SAndres Lagar-Cavilla 			/* Record that at least one error has happened. */
334ceb90fa0SAndres Lagar-Cavilla 			if (st->global_error == 0)
335ceb90fa0SAndres Lagar-Cavilla 				st->global_error = 1;
336ceb90fa0SAndres Lagar-Cavilla 		}
337d8414d3cSBastian Blank 	}
338*4e8c0c8cSDavid Vrabel 	st->va += PAGE_SIZE * nr;
339*4e8c0c8cSDavid Vrabel 	st->index += nr;
340d8414d3cSBastian Blank 
341d8414d3cSBastian Blank 	return 0;
342d8414d3cSBastian Blank }
343d8414d3cSBastian Blank 
344*4e8c0c8cSDavid Vrabel static int mmap_return_error(int err, struct mmap_batch_state *st)
345d8414d3cSBastian Blank {
346*4e8c0c8cSDavid Vrabel 	int ret;
347d8414d3cSBastian Blank 
34899beae6cSAndres Lagar-Cavilla 	if (st->version == 1) {
349*4e8c0c8cSDavid Vrabel 		if (err) {
350*4e8c0c8cSDavid Vrabel 			xen_pfn_t mfn;
351*4e8c0c8cSDavid Vrabel 
352*4e8c0c8cSDavid Vrabel 			ret = get_user(mfn, st->user_mfn);
353*4e8c0c8cSDavid Vrabel 			if (ret < 0)
354*4e8c0c8cSDavid Vrabel 				return ret;
355*4e8c0c8cSDavid Vrabel 			/*
356*4e8c0c8cSDavid Vrabel 			 * V1 encodes the error codes in the 32bit top
357*4e8c0c8cSDavid Vrabel 			 * nibble of the mfn (with its known
358*4e8c0c8cSDavid Vrabel 			 * limitations vis-a-vis 64 bit callers).
359*4e8c0c8cSDavid Vrabel 			 */
360*4e8c0c8cSDavid Vrabel 			mfn |= (err == -ENOENT) ?
361*4e8c0c8cSDavid Vrabel 				PRIVCMD_MMAPBATCH_PAGED_ERROR :
362*4e8c0c8cSDavid Vrabel 				PRIVCMD_MMAPBATCH_MFN_ERROR;
363*4e8c0c8cSDavid Vrabel 			return __put_user(mfn, st->user_mfn++);
364*4e8c0c8cSDavid Vrabel 		} else
36599beae6cSAndres Lagar-Cavilla 			st->user_mfn++;
36699beae6cSAndres Lagar-Cavilla 	} else { /* st->version == 2 */
36799beae6cSAndres Lagar-Cavilla 		if (err)
36899beae6cSAndres Lagar-Cavilla 			return __put_user(err, st->user_err++);
36999beae6cSAndres Lagar-Cavilla 		else
37099beae6cSAndres Lagar-Cavilla 			st->user_err++;
37199beae6cSAndres Lagar-Cavilla 	}
37299beae6cSAndres Lagar-Cavilla 
37399beae6cSAndres Lagar-Cavilla 	return 0;
374d8414d3cSBastian Blank }
375d8414d3cSBastian Blank 
376*4e8c0c8cSDavid Vrabel static int mmap_return_errors(void *data, int nr, void *state)
377*4e8c0c8cSDavid Vrabel {
378*4e8c0c8cSDavid Vrabel 	struct mmap_batch_state *st = state;
379*4e8c0c8cSDavid Vrabel 	int *errs = data;
380*4e8c0c8cSDavid Vrabel 	int i;
381*4e8c0c8cSDavid Vrabel 	int ret;
382*4e8c0c8cSDavid Vrabel 
383*4e8c0c8cSDavid Vrabel 	for (i = 0; i < nr; i++) {
384*4e8c0c8cSDavid Vrabel 		ret = mmap_return_error(errs[i], st);
385*4e8c0c8cSDavid Vrabel 		if (ret < 0)
386*4e8c0c8cSDavid Vrabel 			return ret;
387*4e8c0c8cSDavid Vrabel 	}
388*4e8c0c8cSDavid Vrabel 	return 0;
389*4e8c0c8cSDavid Vrabel }
390*4e8c0c8cSDavid Vrabel 
391d71f5139SMukesh Rathor /* Allocate pfns that are then mapped with gmfns from foreign domid. Update
392d71f5139SMukesh Rathor  * the vma with the page info to use later.
393d71f5139SMukesh Rathor  * Returns: 0 if success, otherwise -errno
394d71f5139SMukesh Rathor  */
395d71f5139SMukesh Rathor static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)
396d71f5139SMukesh Rathor {
397d71f5139SMukesh Rathor 	int rc;
398d71f5139SMukesh Rathor 	struct page **pages;
399d71f5139SMukesh Rathor 
400d71f5139SMukesh Rathor 	pages = kcalloc(numpgs, sizeof(pages[0]), GFP_KERNEL);
401d71f5139SMukesh Rathor 	if (pages == NULL)
402d71f5139SMukesh Rathor 		return -ENOMEM;
403d71f5139SMukesh Rathor 
404d71f5139SMukesh Rathor 	rc = alloc_xenballooned_pages(numpgs, pages, 0);
405d71f5139SMukesh Rathor 	if (rc != 0) {
406d71f5139SMukesh Rathor 		pr_warn("%s Could not alloc %d pfns rc:%d\n", __func__,
407d71f5139SMukesh Rathor 			numpgs, rc);
408d71f5139SMukesh Rathor 		kfree(pages);
409d71f5139SMukesh Rathor 		return -ENOMEM;
410d71f5139SMukesh Rathor 	}
411a5deabe0SAndres Lagar-Cavilla 	BUG_ON(vma->vm_private_data != NULL);
412d71f5139SMukesh Rathor 	vma->vm_private_data = pages;
413d71f5139SMukesh Rathor 
414d71f5139SMukesh Rathor 	return 0;
415d71f5139SMukesh Rathor }
416d71f5139SMukesh Rathor 
417d8414d3cSBastian Blank static struct vm_operations_struct privcmd_vm_ops;
418d8414d3cSBastian Blank 
419ceb90fa0SAndres Lagar-Cavilla static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
420d8414d3cSBastian Blank {
421d8414d3cSBastian Blank 	int ret;
422ceb90fa0SAndres Lagar-Cavilla 	struct privcmd_mmapbatch_v2 m;
423d8414d3cSBastian Blank 	struct mm_struct *mm = current->mm;
424d8414d3cSBastian Blank 	struct vm_area_struct *vma;
425d8414d3cSBastian Blank 	unsigned long nr_pages;
426d8414d3cSBastian Blank 	LIST_HEAD(pagelist);
427d8414d3cSBastian Blank 	struct mmap_batch_state state;
428d8414d3cSBastian Blank 
429ceb90fa0SAndres Lagar-Cavilla 	switch (version) {
430ceb90fa0SAndres Lagar-Cavilla 	case 1:
431ceb90fa0SAndres Lagar-Cavilla 		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
432d8414d3cSBastian Blank 			return -EFAULT;
433ceb90fa0SAndres Lagar-Cavilla 		/* Returns per-frame error in m.arr. */
434ceb90fa0SAndres Lagar-Cavilla 		m.err = NULL;
435ceb90fa0SAndres Lagar-Cavilla 		if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
436ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
437ceb90fa0SAndres Lagar-Cavilla 		break;
438ceb90fa0SAndres Lagar-Cavilla 	case 2:
439ceb90fa0SAndres Lagar-Cavilla 		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
440ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
441ceb90fa0SAndres Lagar-Cavilla 		/* Returns per-frame error code in m.err. */
442ceb90fa0SAndres Lagar-Cavilla 		if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
443ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
444ceb90fa0SAndres Lagar-Cavilla 		break;
445ceb90fa0SAndres Lagar-Cavilla 	default:
446ceb90fa0SAndres Lagar-Cavilla 		return -EINVAL;
447ceb90fa0SAndres Lagar-Cavilla 	}
448d8414d3cSBastian Blank 
449d8414d3cSBastian Blank 	nr_pages = m.num;
450d8414d3cSBastian Blank 	if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
451d8414d3cSBastian Blank 		return -EINVAL;
452d8414d3cSBastian Blank 
453ceb90fa0SAndres Lagar-Cavilla 	ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t), m.arr);
454d8414d3cSBastian Blank 
455ceb90fa0SAndres Lagar-Cavilla 	if (ret)
456d8414d3cSBastian Blank 		goto out;
457ceb90fa0SAndres Lagar-Cavilla 	if (list_empty(&pagelist)) {
458ceb90fa0SAndres Lagar-Cavilla 		ret = -EINVAL;
459ceb90fa0SAndres Lagar-Cavilla 		goto out;
460ceb90fa0SAndres Lagar-Cavilla 	}
461ceb90fa0SAndres Lagar-Cavilla 
46299beae6cSAndres Lagar-Cavilla 	if (version == 2) {
46399beae6cSAndres Lagar-Cavilla 		/* Zero error array now to only copy back actual errors. */
46499beae6cSAndres Lagar-Cavilla 		if (clear_user(m.err, sizeof(int) * m.num)) {
46599beae6cSAndres Lagar-Cavilla 			ret = -EFAULT;
466ceb90fa0SAndres Lagar-Cavilla 			goto out;
467ceb90fa0SAndres Lagar-Cavilla 		}
46899beae6cSAndres Lagar-Cavilla 	}
469d8414d3cSBastian Blank 
470d8414d3cSBastian Blank 	down_write(&mm->mmap_sem);
471d8414d3cSBastian Blank 
472d8414d3cSBastian Blank 	vma = find_vma(mm, m.addr);
473d8414d3cSBastian Blank 	if (!vma ||
474a5deabe0SAndres Lagar-Cavilla 	    vma->vm_ops != &privcmd_vm_ops) {
47568fa965dSMats Petersson 		ret = -EINVAL;
476a5deabe0SAndres Lagar-Cavilla 		goto out_unlock;
477a5deabe0SAndres Lagar-Cavilla 	}
478a5deabe0SAndres Lagar-Cavilla 
479a5deabe0SAndres Lagar-Cavilla 	/*
480a5deabe0SAndres Lagar-Cavilla 	 * Caller must either:
481a5deabe0SAndres Lagar-Cavilla 	 *
482a5deabe0SAndres Lagar-Cavilla 	 * Map the whole VMA range, which will also allocate all the
483a5deabe0SAndres Lagar-Cavilla 	 * pages required for the auto_translated_physmap case.
484a5deabe0SAndres Lagar-Cavilla 	 *
485a5deabe0SAndres Lagar-Cavilla 	 * Or
486a5deabe0SAndres Lagar-Cavilla 	 *
487a5deabe0SAndres Lagar-Cavilla 	 * Map unmapped holes left from a previous map attempt (e.g.,
488a5deabe0SAndres Lagar-Cavilla 	 * because those foreign frames were previously paged out).
489a5deabe0SAndres Lagar-Cavilla 	 */
490a5deabe0SAndres Lagar-Cavilla 	if (vma->vm_private_data == NULL) {
491a5deabe0SAndres Lagar-Cavilla 		if (m.addr != vma->vm_start ||
492a5deabe0SAndres Lagar-Cavilla 		    m.addr + (nr_pages << PAGE_SHIFT) != vma->vm_end) {
493a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
494a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
495d8414d3cSBastian Blank 		}
496d71f5139SMukesh Rathor 		if (xen_feature(XENFEAT_auto_translated_physmap)) {
497d71f5139SMukesh Rathor 			ret = alloc_empty_pages(vma, m.num);
498a5deabe0SAndres Lagar-Cavilla 			if (ret < 0)
499a5deabe0SAndres Lagar-Cavilla 				goto out_unlock;
500a5deabe0SAndres Lagar-Cavilla 		} else
501a5deabe0SAndres Lagar-Cavilla 			vma->vm_private_data = PRIV_VMA_LOCKED;
502a5deabe0SAndres Lagar-Cavilla 	} else {
503a5deabe0SAndres Lagar-Cavilla 		if (m.addr < vma->vm_start ||
504a5deabe0SAndres Lagar-Cavilla 		    m.addr + (nr_pages << PAGE_SHIFT) > vma->vm_end) {
505a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
506a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
507a5deabe0SAndres Lagar-Cavilla 		}
508a5deabe0SAndres Lagar-Cavilla 		if (privcmd_vma_range_is_mapped(vma, m.addr, nr_pages)) {
509a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
510a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
511d71f5139SMukesh Rathor 		}
512d71f5139SMukesh Rathor 	}
513d8414d3cSBastian Blank 
514d8414d3cSBastian Blank 	state.domain        = m.dom;
515d8414d3cSBastian Blank 	state.vma           = vma;
516d8414d3cSBastian Blank 	state.va            = m.addr;
517d71f5139SMukesh Rathor 	state.index         = 0;
518ceb90fa0SAndres Lagar-Cavilla 	state.global_error  = 0;
51999beae6cSAndres Lagar-Cavilla 	state.version       = version;
520d8414d3cSBastian Blank 
521ceb90fa0SAndres Lagar-Cavilla 	/* mmap_batch_fn guarantees ret == 0 */
522*4e8c0c8cSDavid Vrabel 	BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
523ceb90fa0SAndres Lagar-Cavilla 				    &pagelist, mmap_batch_fn, &state));
524d8414d3cSBastian Blank 
525d8414d3cSBastian Blank 	up_write(&mm->mmap_sem);
526d8414d3cSBastian Blank 
52768fa965dSMats Petersson 	if (state.global_error) {
528ceb90fa0SAndres Lagar-Cavilla 		/* Write back errors in second pass. */
529ceb90fa0SAndres Lagar-Cavilla 		state.user_mfn = (xen_pfn_t *)m.arr;
53099beae6cSAndres Lagar-Cavilla 		state.user_err = m.err;
531*4e8c0c8cSDavid Vrabel 		ret = traverse_pages_block(m.num, sizeof(xen_pfn_t),
53299beae6cSAndres Lagar-Cavilla 					   &pagelist, mmap_return_errors, &state);
53368fa965dSMats Petersson 	} else
53468fa965dSMats Petersson 		ret = 0;
53568fa965dSMats Petersson 
536ceb90fa0SAndres Lagar-Cavilla 	/* If we have not had any EFAULT-like global errors then set the global
537ceb90fa0SAndres Lagar-Cavilla 	 * error to -ENOENT if necessary. */
538ceb90fa0SAndres Lagar-Cavilla 	if ((ret == 0) && (state.global_error == -ENOENT))
539ceb90fa0SAndres Lagar-Cavilla 		ret = -ENOENT;
540d8414d3cSBastian Blank 
541d8414d3cSBastian Blank out:
542d8414d3cSBastian Blank 	free_page_list(&pagelist);
543d8414d3cSBastian Blank 	return ret;
544a5deabe0SAndres Lagar-Cavilla 
545a5deabe0SAndres Lagar-Cavilla out_unlock:
546a5deabe0SAndres Lagar-Cavilla 	up_write(&mm->mmap_sem);
547a5deabe0SAndres Lagar-Cavilla 	goto out;
548d8414d3cSBastian Blank }
549d8414d3cSBastian Blank 
550d8414d3cSBastian Blank static long privcmd_ioctl(struct file *file,
551d8414d3cSBastian Blank 			  unsigned int cmd, unsigned long data)
552d8414d3cSBastian Blank {
553d8414d3cSBastian Blank 	int ret = -ENOSYS;
554d8414d3cSBastian Blank 	void __user *udata = (void __user *) data;
555d8414d3cSBastian Blank 
556d8414d3cSBastian Blank 	switch (cmd) {
557d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_HYPERCALL:
558d8414d3cSBastian Blank 		ret = privcmd_ioctl_hypercall(udata);
559d8414d3cSBastian Blank 		break;
560d8414d3cSBastian Blank 
561d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_MMAP:
562d8414d3cSBastian Blank 		ret = privcmd_ioctl_mmap(udata);
563d8414d3cSBastian Blank 		break;
564d8414d3cSBastian Blank 
565d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_MMAPBATCH:
566ceb90fa0SAndres Lagar-Cavilla 		ret = privcmd_ioctl_mmap_batch(udata, 1);
567ceb90fa0SAndres Lagar-Cavilla 		break;
568ceb90fa0SAndres Lagar-Cavilla 
569ceb90fa0SAndres Lagar-Cavilla 	case IOCTL_PRIVCMD_MMAPBATCH_V2:
570ceb90fa0SAndres Lagar-Cavilla 		ret = privcmd_ioctl_mmap_batch(udata, 2);
571d8414d3cSBastian Blank 		break;
572d8414d3cSBastian Blank 
573d8414d3cSBastian Blank 	default:
574d8414d3cSBastian Blank 		ret = -EINVAL;
575d8414d3cSBastian Blank 		break;
576d8414d3cSBastian Blank 	}
577d8414d3cSBastian Blank 
578d8414d3cSBastian Blank 	return ret;
579d8414d3cSBastian Blank }
580d8414d3cSBastian Blank 
581d71f5139SMukesh Rathor static void privcmd_close(struct vm_area_struct *vma)
582d71f5139SMukesh Rathor {
583d71f5139SMukesh Rathor 	struct page **pages = vma->vm_private_data;
584d71f5139SMukesh Rathor 	int numpgs = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
585b6497b38SIan Campbell 	int rc;
586d71f5139SMukesh Rathor 
5879eff37a8SDan Carpenter 	if (!xen_feature(XENFEAT_auto_translated_physmap) || !numpgs || !pages)
588d71f5139SMukesh Rathor 		return;
589d71f5139SMukesh Rathor 
590b6497b38SIan Campbell 	rc = xen_unmap_domain_mfn_range(vma, numpgs, pages);
591b6497b38SIan Campbell 	if (rc == 0)
592d71f5139SMukesh Rathor 		free_xenballooned_pages(numpgs, pages);
593b6497b38SIan Campbell 	else
594b6497b38SIan Campbell 		pr_crit("unable to unmap MFN range: leaking %d pages. rc=%d\n",
595b6497b38SIan Campbell 			numpgs, rc);
596d71f5139SMukesh Rathor 	kfree(pages);
597d71f5139SMukesh Rathor }
598d71f5139SMukesh Rathor 
599d8414d3cSBastian Blank static int privcmd_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
600d8414d3cSBastian Blank {
601d8414d3cSBastian Blank 	printk(KERN_DEBUG "privcmd_fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
602d8414d3cSBastian Blank 	       vma, vma->vm_start, vma->vm_end,
603d8414d3cSBastian Blank 	       vmf->pgoff, vmf->virtual_address);
604d8414d3cSBastian Blank 
605d8414d3cSBastian Blank 	return VM_FAULT_SIGBUS;
606d8414d3cSBastian Blank }
607d8414d3cSBastian Blank 
608d8414d3cSBastian Blank static struct vm_operations_struct privcmd_vm_ops = {
609d71f5139SMukesh Rathor 	.close = privcmd_close,
610d8414d3cSBastian Blank 	.fault = privcmd_fault
611d8414d3cSBastian Blank };
612d8414d3cSBastian Blank 
613d8414d3cSBastian Blank static int privcmd_mmap(struct file *file, struct vm_area_struct *vma)
614d8414d3cSBastian Blank {
615d8414d3cSBastian Blank 	/* DONTCOPY is essential for Xen because copy_page_range doesn't know
616d8414d3cSBastian Blank 	 * how to recreate these mappings */
617314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTCOPY |
618314e51b9SKonstantin Khlebnikov 			 VM_DONTEXPAND | VM_DONTDUMP;
619d8414d3cSBastian Blank 	vma->vm_ops = &privcmd_vm_ops;
620d8414d3cSBastian Blank 	vma->vm_private_data = NULL;
621d8414d3cSBastian Blank 
622d8414d3cSBastian Blank 	return 0;
623d8414d3cSBastian Blank }
624d8414d3cSBastian Blank 
625a5deabe0SAndres Lagar-Cavilla /*
626a5deabe0SAndres Lagar-Cavilla  * For MMAPBATCH*. This allows asserting the singleshot mapping
627a5deabe0SAndres Lagar-Cavilla  * on a per pfn/pte basis. Mapping calls that fail with ENOENT
628a5deabe0SAndres Lagar-Cavilla  * can be then retried until success.
629a5deabe0SAndres Lagar-Cavilla  */
630a5deabe0SAndres Lagar-Cavilla static int is_mapped_fn(pte_t *pte, struct page *pmd_page,
631a5deabe0SAndres Lagar-Cavilla 	                unsigned long addr, void *data)
632d8414d3cSBastian Blank {
633a5deabe0SAndres Lagar-Cavilla 	return pte_none(*pte) ? 0 : -EBUSY;
634a5deabe0SAndres Lagar-Cavilla }
635a5deabe0SAndres Lagar-Cavilla 
636a5deabe0SAndres Lagar-Cavilla static int privcmd_vma_range_is_mapped(
637a5deabe0SAndres Lagar-Cavilla 	           struct vm_area_struct *vma,
638a5deabe0SAndres Lagar-Cavilla 	           unsigned long addr,
639a5deabe0SAndres Lagar-Cavilla 	           unsigned long nr_pages)
640a5deabe0SAndres Lagar-Cavilla {
641a5deabe0SAndres Lagar-Cavilla 	return apply_to_page_range(vma->vm_mm, addr, nr_pages << PAGE_SHIFT,
642a5deabe0SAndres Lagar-Cavilla 				   is_mapped_fn, NULL) != 0;
643d8414d3cSBastian Blank }
644d8414d3cSBastian Blank 
645d8414d3cSBastian Blank const struct file_operations xen_privcmd_fops = {
646d8414d3cSBastian Blank 	.owner = THIS_MODULE,
647d8414d3cSBastian Blank 	.unlocked_ioctl = privcmd_ioctl,
648d8414d3cSBastian Blank 	.mmap = privcmd_mmap,
649d8414d3cSBastian Blank };
650d8414d3cSBastian Blank EXPORT_SYMBOL_GPL(xen_privcmd_fops);
651d8414d3cSBastian Blank 
652d8414d3cSBastian Blank static struct miscdevice privcmd_dev = {
653d8414d3cSBastian Blank 	.minor = MISC_DYNAMIC_MINOR,
654d8414d3cSBastian Blank 	.name = "xen/privcmd",
655d8414d3cSBastian Blank 	.fops = &xen_privcmd_fops,
656d8414d3cSBastian Blank };
657d8414d3cSBastian Blank 
658d8414d3cSBastian Blank static int __init privcmd_init(void)
659d8414d3cSBastian Blank {
660d8414d3cSBastian Blank 	int err;
661d8414d3cSBastian Blank 
662d8414d3cSBastian Blank 	if (!xen_domain())
663d8414d3cSBastian Blank 		return -ENODEV;
664d8414d3cSBastian Blank 
665d8414d3cSBastian Blank 	err = misc_register(&privcmd_dev);
666d8414d3cSBastian Blank 	if (err != 0) {
667283c0972SJoe Perches 		pr_err("Could not register Xen privcmd device\n");
668d8414d3cSBastian Blank 		return err;
669d8414d3cSBastian Blank 	}
670d8414d3cSBastian Blank 	return 0;
671d8414d3cSBastian Blank }
672d8414d3cSBastian Blank 
673d8414d3cSBastian Blank static void __exit privcmd_exit(void)
674d8414d3cSBastian Blank {
675d8414d3cSBastian Blank 	misc_deregister(&privcmd_dev);
676d8414d3cSBastian Blank }
677d8414d3cSBastian Blank 
678d8414d3cSBastian Blank module_init(privcmd_init);
679d8414d3cSBastian Blank module_exit(privcmd_exit);
680