xref: /openbmc/linux/drivers/xen/privcmd.c (revision c51b3c639e01f20559531eef3c5919feae23c55a)
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>
25ab520be8SPaul Durrant #include <linux/moduleparam.h>
26d8414d3cSBastian Blank 
27d8414d3cSBastian Blank #include <asm/pgalloc.h>
28d8414d3cSBastian Blank #include <asm/pgtable.h>
29d8414d3cSBastian Blank #include <asm/tlb.h>
30d8414d3cSBastian Blank #include <asm/xen/hypervisor.h>
31d8414d3cSBastian Blank #include <asm/xen/hypercall.h>
32d8414d3cSBastian Blank 
33d8414d3cSBastian Blank #include <xen/xen.h>
34d8414d3cSBastian Blank #include <xen/privcmd.h>
35d8414d3cSBastian Blank #include <xen/interface/xen.h>
363ad08765SPaul Durrant #include <xen/interface/memory.h>
37ab520be8SPaul Durrant #include <xen/interface/hvm/dm_op.h>
38d8414d3cSBastian Blank #include <xen/features.h>
39d8414d3cSBastian Blank #include <xen/page.h>
40d8414d3cSBastian Blank #include <xen/xen-ops.h>
41d71f5139SMukesh Rathor #include <xen/balloon.h>
42d8414d3cSBastian Blank 
43d8414d3cSBastian Blank #include "privcmd.h"
44d8414d3cSBastian Blank 
45d8414d3cSBastian Blank MODULE_LICENSE("GPL");
46d8414d3cSBastian Blank 
47d71f5139SMukesh Rathor #define PRIV_VMA_LOCKED ((void *)1)
48d71f5139SMukesh Rathor 
49ab520be8SPaul Durrant static unsigned int privcmd_dm_op_max_num = 16;
50ab520be8SPaul Durrant module_param_named(dm_op_max_nr_bufs, privcmd_dm_op_max_num, uint, 0644);
51ab520be8SPaul Durrant MODULE_PARM_DESC(dm_op_max_nr_bufs,
52ab520be8SPaul Durrant 		 "Maximum number of buffers per dm_op hypercall");
53ab520be8SPaul Durrant 
54ab520be8SPaul Durrant static unsigned int privcmd_dm_op_buf_max_size = 4096;
55ab520be8SPaul Durrant module_param_named(dm_op_buf_max_size, privcmd_dm_op_buf_max_size, uint,
56ab520be8SPaul Durrant 		   0644);
57ab520be8SPaul Durrant MODULE_PARM_DESC(dm_op_buf_max_size,
58ab520be8SPaul Durrant 		 "Maximum size of a dm_op hypercall buffer");
59ab520be8SPaul Durrant 
604610d240SPaul Durrant struct privcmd_data {
614610d240SPaul Durrant 	domid_t domid;
624610d240SPaul Durrant };
634610d240SPaul Durrant 
64a5deabe0SAndres Lagar-Cavilla static int privcmd_vma_range_is_mapped(
65a5deabe0SAndres Lagar-Cavilla                struct vm_area_struct *vma,
66a5deabe0SAndres Lagar-Cavilla                unsigned long addr,
67a5deabe0SAndres Lagar-Cavilla                unsigned long nr_pages);
68d8414d3cSBastian Blank 
694610d240SPaul Durrant static long privcmd_ioctl_hypercall(struct file *file, void __user *udata)
70d8414d3cSBastian Blank {
714610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
72d8414d3cSBastian Blank 	struct privcmd_hypercall hypercall;
73d8414d3cSBastian Blank 	long ret;
74d8414d3cSBastian Blank 
754610d240SPaul Durrant 	/* Disallow arbitrary hypercalls if restricted */
764610d240SPaul Durrant 	if (data->domid != DOMID_INVALID)
774610d240SPaul Durrant 		return -EPERM;
784610d240SPaul Durrant 
79d8414d3cSBastian Blank 	if (copy_from_user(&hypercall, udata, sizeof(hypercall)))
80d8414d3cSBastian Blank 		return -EFAULT;
81d8414d3cSBastian Blank 
82fdfd811dSDavid Vrabel 	xen_preemptible_hcall_begin();
83d8414d3cSBastian Blank 	ret = privcmd_call(hypercall.op,
84d8414d3cSBastian Blank 			   hypercall.arg[0], hypercall.arg[1],
85d8414d3cSBastian Blank 			   hypercall.arg[2], hypercall.arg[3],
86d8414d3cSBastian Blank 			   hypercall.arg[4]);
87fdfd811dSDavid Vrabel 	xen_preemptible_hcall_end();
88d8414d3cSBastian Blank 
89d8414d3cSBastian Blank 	return ret;
90d8414d3cSBastian Blank }
91d8414d3cSBastian Blank 
92d8414d3cSBastian Blank static void free_page_list(struct list_head *pages)
93d8414d3cSBastian Blank {
94d8414d3cSBastian Blank 	struct page *p, *n;
95d8414d3cSBastian Blank 
96d8414d3cSBastian Blank 	list_for_each_entry_safe(p, n, pages, lru)
97d8414d3cSBastian Blank 		__free_page(p);
98d8414d3cSBastian Blank 
99d8414d3cSBastian Blank 	INIT_LIST_HEAD(pages);
100d8414d3cSBastian Blank }
101d8414d3cSBastian Blank 
102d8414d3cSBastian Blank /*
103d8414d3cSBastian Blank  * Given an array of items in userspace, return a list of pages
104d8414d3cSBastian Blank  * containing the data.  If copying fails, either because of memory
105d8414d3cSBastian Blank  * allocation failure or a problem reading user memory, return an
106d8414d3cSBastian Blank  * error code; its up to the caller to dispose of any partial list.
107d8414d3cSBastian Blank  */
108d8414d3cSBastian Blank static int gather_array(struct list_head *pagelist,
109d8414d3cSBastian Blank 			unsigned nelem, size_t size,
110ceb90fa0SAndres Lagar-Cavilla 			const void __user *data)
111d8414d3cSBastian Blank {
112d8414d3cSBastian Blank 	unsigned pageidx;
113d8414d3cSBastian Blank 	void *pagedata;
114d8414d3cSBastian Blank 	int ret;
115d8414d3cSBastian Blank 
116d8414d3cSBastian Blank 	if (size > PAGE_SIZE)
117d8414d3cSBastian Blank 		return 0;
118d8414d3cSBastian Blank 
119d8414d3cSBastian Blank 	pageidx = PAGE_SIZE;
120d8414d3cSBastian Blank 	pagedata = NULL;	/* quiet, gcc */
121d8414d3cSBastian Blank 	while (nelem--) {
122d8414d3cSBastian Blank 		if (pageidx > PAGE_SIZE-size) {
123d8414d3cSBastian Blank 			struct page *page = alloc_page(GFP_KERNEL);
124d8414d3cSBastian Blank 
125d8414d3cSBastian Blank 			ret = -ENOMEM;
126d8414d3cSBastian Blank 			if (page == NULL)
127d8414d3cSBastian Blank 				goto fail;
128d8414d3cSBastian Blank 
129d8414d3cSBastian Blank 			pagedata = page_address(page);
130d8414d3cSBastian Blank 
131d8414d3cSBastian Blank 			list_add_tail(&page->lru, pagelist);
132d8414d3cSBastian Blank 			pageidx = 0;
133d8414d3cSBastian Blank 		}
134d8414d3cSBastian Blank 
135d8414d3cSBastian Blank 		ret = -EFAULT;
136d8414d3cSBastian Blank 		if (copy_from_user(pagedata + pageidx, data, size))
137d8414d3cSBastian Blank 			goto fail;
138d8414d3cSBastian Blank 
139d8414d3cSBastian Blank 		data += size;
140d8414d3cSBastian Blank 		pageidx += size;
141d8414d3cSBastian Blank 	}
142d8414d3cSBastian Blank 
143d8414d3cSBastian Blank 	ret = 0;
144d8414d3cSBastian Blank 
145d8414d3cSBastian Blank fail:
146d8414d3cSBastian Blank 	return ret;
147d8414d3cSBastian Blank }
148d8414d3cSBastian Blank 
149d8414d3cSBastian Blank /*
150d8414d3cSBastian Blank  * Call function "fn" on each element of the array fragmented
151d8414d3cSBastian Blank  * over a list of pages.
152d8414d3cSBastian Blank  */
153d8414d3cSBastian Blank static int traverse_pages(unsigned nelem, size_t size,
154d8414d3cSBastian Blank 			  struct list_head *pos,
155d8414d3cSBastian Blank 			  int (*fn)(void *data, void *state),
156d8414d3cSBastian Blank 			  void *state)
157d8414d3cSBastian Blank {
158d8414d3cSBastian Blank 	void *pagedata;
159d8414d3cSBastian Blank 	unsigned pageidx;
160d8414d3cSBastian Blank 	int ret = 0;
161d8414d3cSBastian Blank 
162d8414d3cSBastian Blank 	BUG_ON(size > PAGE_SIZE);
163d8414d3cSBastian Blank 
164d8414d3cSBastian Blank 	pageidx = PAGE_SIZE;
165d8414d3cSBastian Blank 	pagedata = NULL;	/* hush, gcc */
166d8414d3cSBastian Blank 
167d8414d3cSBastian Blank 	while (nelem--) {
168d8414d3cSBastian Blank 		if (pageidx > PAGE_SIZE-size) {
169d8414d3cSBastian Blank 			struct page *page;
170d8414d3cSBastian Blank 			pos = pos->next;
171d8414d3cSBastian Blank 			page = list_entry(pos, struct page, lru);
172d8414d3cSBastian Blank 			pagedata = page_address(page);
173d8414d3cSBastian Blank 			pageidx = 0;
174d8414d3cSBastian Blank 		}
175d8414d3cSBastian Blank 
176d8414d3cSBastian Blank 		ret = (*fn)(pagedata + pageidx, state);
177d8414d3cSBastian Blank 		if (ret)
178d8414d3cSBastian Blank 			break;
179d8414d3cSBastian Blank 		pageidx += size;
180d8414d3cSBastian Blank 	}
181d8414d3cSBastian Blank 
182d8414d3cSBastian Blank 	return ret;
183d8414d3cSBastian Blank }
184d8414d3cSBastian Blank 
1854e8c0c8cSDavid Vrabel /*
1864e8c0c8cSDavid Vrabel  * Similar to traverse_pages, but use each page as a "block" of
1874e8c0c8cSDavid Vrabel  * data to be processed as one unit.
1884e8c0c8cSDavid Vrabel  */
1894e8c0c8cSDavid Vrabel static int traverse_pages_block(unsigned nelem, size_t size,
1904e8c0c8cSDavid Vrabel 				struct list_head *pos,
1914e8c0c8cSDavid Vrabel 				int (*fn)(void *data, int nr, void *state),
1924e8c0c8cSDavid Vrabel 				void *state)
1934e8c0c8cSDavid Vrabel {
1944e8c0c8cSDavid Vrabel 	void *pagedata;
1954e8c0c8cSDavid Vrabel 	int ret = 0;
1964e8c0c8cSDavid Vrabel 
1974e8c0c8cSDavid Vrabel 	BUG_ON(size > PAGE_SIZE);
1984e8c0c8cSDavid Vrabel 
1994e8c0c8cSDavid Vrabel 	while (nelem) {
2004e8c0c8cSDavid Vrabel 		int nr = (PAGE_SIZE/size);
2014e8c0c8cSDavid Vrabel 		struct page *page;
2024e8c0c8cSDavid Vrabel 		if (nr > nelem)
2034e8c0c8cSDavid Vrabel 			nr = nelem;
2044e8c0c8cSDavid Vrabel 		pos = pos->next;
2054e8c0c8cSDavid Vrabel 		page = list_entry(pos, struct page, lru);
2064e8c0c8cSDavid Vrabel 		pagedata = page_address(page);
2074e8c0c8cSDavid Vrabel 		ret = (*fn)(pagedata, nr, state);
2084e8c0c8cSDavid Vrabel 		if (ret)
2094e8c0c8cSDavid Vrabel 			break;
2104e8c0c8cSDavid Vrabel 		nelem -= nr;
2114e8c0c8cSDavid Vrabel 	}
2124e8c0c8cSDavid Vrabel 
2134e8c0c8cSDavid Vrabel 	return ret;
2144e8c0c8cSDavid Vrabel }
2154e8c0c8cSDavid Vrabel 
216a13d7201SJulien Grall struct mmap_gfn_state {
217d8414d3cSBastian Blank 	unsigned long va;
218d8414d3cSBastian Blank 	struct vm_area_struct *vma;
219d8414d3cSBastian Blank 	domid_t domain;
220d8414d3cSBastian Blank };
221d8414d3cSBastian Blank 
222a13d7201SJulien Grall static int mmap_gfn_range(void *data, void *state)
223d8414d3cSBastian Blank {
224d8414d3cSBastian Blank 	struct privcmd_mmap_entry *msg = data;
225a13d7201SJulien Grall 	struct mmap_gfn_state *st = state;
226d8414d3cSBastian Blank 	struct vm_area_struct *vma = st->vma;
227d8414d3cSBastian Blank 	int rc;
228d8414d3cSBastian Blank 
229d8414d3cSBastian Blank 	/* Do not allow range to wrap the address space. */
230d8414d3cSBastian Blank 	if ((msg->npages > (LONG_MAX >> PAGE_SHIFT)) ||
231d8414d3cSBastian Blank 	    ((unsigned long)(msg->npages << PAGE_SHIFT) >= -st->va))
232d8414d3cSBastian Blank 		return -EINVAL;
233d8414d3cSBastian Blank 
234d8414d3cSBastian Blank 	/* Range chunks must be contiguous in va space. */
235d8414d3cSBastian Blank 	if ((msg->va != st->va) ||
236d8414d3cSBastian Blank 	    ((msg->va+(msg->npages<<PAGE_SHIFT)) > vma->vm_end))
237d8414d3cSBastian Blank 		return -EINVAL;
238d8414d3cSBastian Blank 
239a13d7201SJulien Grall 	rc = xen_remap_domain_gfn_range(vma,
240d8414d3cSBastian Blank 					msg->va & PAGE_MASK,
241d8414d3cSBastian Blank 					msg->mfn, msg->npages,
242d8414d3cSBastian Blank 					vma->vm_page_prot,
2439a032e39SIan Campbell 					st->domain, NULL);
244d8414d3cSBastian Blank 	if (rc < 0)
245d8414d3cSBastian Blank 		return rc;
246d8414d3cSBastian Blank 
247d8414d3cSBastian Blank 	st->va += msg->npages << PAGE_SHIFT;
248d8414d3cSBastian Blank 
249d8414d3cSBastian Blank 	return 0;
250d8414d3cSBastian Blank }
251d8414d3cSBastian Blank 
2524610d240SPaul Durrant static long privcmd_ioctl_mmap(struct file *file, void __user *udata)
253d8414d3cSBastian Blank {
2544610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
255d8414d3cSBastian Blank 	struct privcmd_mmap mmapcmd;
256d8414d3cSBastian Blank 	struct mm_struct *mm = current->mm;
257d8414d3cSBastian Blank 	struct vm_area_struct *vma;
258d8414d3cSBastian Blank 	int rc;
259d8414d3cSBastian Blank 	LIST_HEAD(pagelist);
260a13d7201SJulien Grall 	struct mmap_gfn_state state;
261d8414d3cSBastian Blank 
262d71f5139SMukesh Rathor 	/* We only support privcmd_ioctl_mmap_batch for auto translated. */
263d71f5139SMukesh Rathor 	if (xen_feature(XENFEAT_auto_translated_physmap))
264d71f5139SMukesh Rathor 		return -ENOSYS;
265d71f5139SMukesh Rathor 
266d8414d3cSBastian Blank 	if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
267d8414d3cSBastian Blank 		return -EFAULT;
268d8414d3cSBastian Blank 
2694610d240SPaul Durrant 	/* If restriction is in place, check the domid matches */
2704610d240SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != mmapcmd.dom)
2714610d240SPaul Durrant 		return -EPERM;
2724610d240SPaul Durrant 
273d8414d3cSBastian Blank 	rc = gather_array(&pagelist,
274d8414d3cSBastian Blank 			  mmapcmd.num, sizeof(struct privcmd_mmap_entry),
275d8414d3cSBastian Blank 			  mmapcmd.entry);
276d8414d3cSBastian Blank 
277d8414d3cSBastian Blank 	if (rc || list_empty(&pagelist))
278d8414d3cSBastian Blank 		goto out;
279d8414d3cSBastian Blank 
280d8414d3cSBastian Blank 	down_write(&mm->mmap_sem);
281d8414d3cSBastian Blank 
282d8414d3cSBastian Blank 	{
283d8414d3cSBastian Blank 		struct page *page = list_first_entry(&pagelist,
284d8414d3cSBastian Blank 						     struct page, lru);
285d8414d3cSBastian Blank 		struct privcmd_mmap_entry *msg = page_address(page);
286d8414d3cSBastian Blank 
287d8414d3cSBastian Blank 		vma = find_vma(mm, msg->va);
288d8414d3cSBastian Blank 		rc = -EINVAL;
289d8414d3cSBastian Blank 
290a5deabe0SAndres Lagar-Cavilla 		if (!vma || (msg->va != vma->vm_start) || vma->vm_private_data)
291d8414d3cSBastian Blank 			goto out_up;
292a5deabe0SAndres Lagar-Cavilla 		vma->vm_private_data = PRIV_VMA_LOCKED;
293d8414d3cSBastian Blank 	}
294d8414d3cSBastian Blank 
295d8414d3cSBastian Blank 	state.va = vma->vm_start;
296d8414d3cSBastian Blank 	state.vma = vma;
297d8414d3cSBastian Blank 	state.domain = mmapcmd.dom;
298d8414d3cSBastian Blank 
299d8414d3cSBastian Blank 	rc = traverse_pages(mmapcmd.num, sizeof(struct privcmd_mmap_entry),
300d8414d3cSBastian Blank 			    &pagelist,
301a13d7201SJulien Grall 			    mmap_gfn_range, &state);
302d8414d3cSBastian Blank 
303d8414d3cSBastian Blank 
304d8414d3cSBastian Blank out_up:
305d8414d3cSBastian Blank 	up_write(&mm->mmap_sem);
306d8414d3cSBastian Blank 
307d8414d3cSBastian Blank out:
308d8414d3cSBastian Blank 	free_page_list(&pagelist);
309d8414d3cSBastian Blank 
310d8414d3cSBastian Blank 	return rc;
311d8414d3cSBastian Blank }
312d8414d3cSBastian Blank 
313d8414d3cSBastian Blank struct mmap_batch_state {
314d8414d3cSBastian Blank 	domid_t domain;
315d8414d3cSBastian Blank 	unsigned long va;
316d8414d3cSBastian Blank 	struct vm_area_struct *vma;
317d71f5139SMukesh Rathor 	int index;
318ceb90fa0SAndres Lagar-Cavilla 	/* A tristate:
319ceb90fa0SAndres Lagar-Cavilla 	 *      0 for no errors
320ceb90fa0SAndres Lagar-Cavilla 	 *      1 if at least one error has happened (and no
321ceb90fa0SAndres Lagar-Cavilla 	 *          -ENOENT errors have happened)
322ceb90fa0SAndres Lagar-Cavilla 	 *      -ENOENT if at least 1 -ENOENT has happened.
323ceb90fa0SAndres Lagar-Cavilla 	 */
324ceb90fa0SAndres Lagar-Cavilla 	int global_error;
32599beae6cSAndres Lagar-Cavilla 	int version;
326d8414d3cSBastian Blank 
327a13d7201SJulien Grall 	/* User-space gfn array to store errors in the second pass for V1. */
328a13d7201SJulien Grall 	xen_pfn_t __user *user_gfn;
32999beae6cSAndres Lagar-Cavilla 	/* User-space int array to store errors in the second pass for V2. */
33099beae6cSAndres Lagar-Cavilla 	int __user *user_err;
331d8414d3cSBastian Blank };
332d8414d3cSBastian Blank 
333a13d7201SJulien Grall /* auto translated dom0 note: if domU being created is PV, then gfn is
334a13d7201SJulien Grall  * mfn(addr on bus). If it's auto xlated, then gfn is pfn (input to HAP).
335d71f5139SMukesh Rathor  */
3364e8c0c8cSDavid Vrabel static int mmap_batch_fn(void *data, int nr, void *state)
337d8414d3cSBastian Blank {
338a13d7201SJulien Grall 	xen_pfn_t *gfnp = data;
339d8414d3cSBastian Blank 	struct mmap_batch_state *st = state;
340d71f5139SMukesh Rathor 	struct vm_area_struct *vma = st->vma;
341d71f5139SMukesh Rathor 	struct page **pages = vma->vm_private_data;
3424e8c0c8cSDavid Vrabel 	struct page **cur_pages = NULL;
343ceb90fa0SAndres Lagar-Cavilla 	int ret;
344d8414d3cSBastian Blank 
345d71f5139SMukesh Rathor 	if (xen_feature(XENFEAT_auto_translated_physmap))
3464e8c0c8cSDavid Vrabel 		cur_pages = &pages[st->index];
347d71f5139SMukesh Rathor 
3484e8c0c8cSDavid Vrabel 	BUG_ON(nr < 0);
349a13d7201SJulien Grall 	ret = xen_remap_domain_gfn_array(st->vma, st->va & PAGE_MASK, gfnp, nr,
350a13d7201SJulien Grall 					 (int *)gfnp, st->vma->vm_page_prot,
3514e8c0c8cSDavid Vrabel 					 st->domain, cur_pages);
352ceb90fa0SAndres Lagar-Cavilla 
3534e8c0c8cSDavid Vrabel 	/* Adjust the global_error? */
3544e8c0c8cSDavid Vrabel 	if (ret != nr) {
355ceb90fa0SAndres Lagar-Cavilla 		if (ret == -ENOENT)
356ceb90fa0SAndres Lagar-Cavilla 			st->global_error = -ENOENT;
357ceb90fa0SAndres Lagar-Cavilla 		else {
358ceb90fa0SAndres Lagar-Cavilla 			/* Record that at least one error has happened. */
359ceb90fa0SAndres Lagar-Cavilla 			if (st->global_error == 0)
360ceb90fa0SAndres Lagar-Cavilla 				st->global_error = 1;
361ceb90fa0SAndres Lagar-Cavilla 		}
362d8414d3cSBastian Blank 	}
363753c09b5SJulien Grall 	st->va += XEN_PAGE_SIZE * nr;
364753c09b5SJulien Grall 	st->index += nr / XEN_PFN_PER_PAGE;
365d8414d3cSBastian Blank 
366d8414d3cSBastian Blank 	return 0;
367d8414d3cSBastian Blank }
368d8414d3cSBastian Blank 
3694e8c0c8cSDavid Vrabel static int mmap_return_error(int err, struct mmap_batch_state *st)
370d8414d3cSBastian Blank {
3714e8c0c8cSDavid Vrabel 	int ret;
372d8414d3cSBastian Blank 
37399beae6cSAndres Lagar-Cavilla 	if (st->version == 1) {
3744e8c0c8cSDavid Vrabel 		if (err) {
375a13d7201SJulien Grall 			xen_pfn_t gfn;
3764e8c0c8cSDavid Vrabel 
377a13d7201SJulien Grall 			ret = get_user(gfn, st->user_gfn);
3784e8c0c8cSDavid Vrabel 			if (ret < 0)
3794e8c0c8cSDavid Vrabel 				return ret;
3804e8c0c8cSDavid Vrabel 			/*
3814e8c0c8cSDavid Vrabel 			 * V1 encodes the error codes in the 32bit top
382a13d7201SJulien Grall 			 * nibble of the gfn (with its known
3834e8c0c8cSDavid Vrabel 			 * limitations vis-a-vis 64 bit callers).
3844e8c0c8cSDavid Vrabel 			 */
385a13d7201SJulien Grall 			gfn |= (err == -ENOENT) ?
3864e8c0c8cSDavid Vrabel 				PRIVCMD_MMAPBATCH_PAGED_ERROR :
3874e8c0c8cSDavid Vrabel 				PRIVCMD_MMAPBATCH_MFN_ERROR;
388a13d7201SJulien Grall 			return __put_user(gfn, st->user_gfn++);
3894e8c0c8cSDavid Vrabel 		} else
390a13d7201SJulien Grall 			st->user_gfn++;
39199beae6cSAndres Lagar-Cavilla 	} else { /* st->version == 2 */
39299beae6cSAndres Lagar-Cavilla 		if (err)
39399beae6cSAndres Lagar-Cavilla 			return __put_user(err, st->user_err++);
39499beae6cSAndres Lagar-Cavilla 		else
39599beae6cSAndres Lagar-Cavilla 			st->user_err++;
39699beae6cSAndres Lagar-Cavilla 	}
39799beae6cSAndres Lagar-Cavilla 
39899beae6cSAndres Lagar-Cavilla 	return 0;
399d8414d3cSBastian Blank }
400d8414d3cSBastian Blank 
4014e8c0c8cSDavid Vrabel static int mmap_return_errors(void *data, int nr, void *state)
4024e8c0c8cSDavid Vrabel {
4034e8c0c8cSDavid Vrabel 	struct mmap_batch_state *st = state;
4044e8c0c8cSDavid Vrabel 	int *errs = data;
4054e8c0c8cSDavid Vrabel 	int i;
4064e8c0c8cSDavid Vrabel 	int ret;
4074e8c0c8cSDavid Vrabel 
4084e8c0c8cSDavid Vrabel 	for (i = 0; i < nr; i++) {
4094e8c0c8cSDavid Vrabel 		ret = mmap_return_error(errs[i], st);
4104e8c0c8cSDavid Vrabel 		if (ret < 0)
4114e8c0c8cSDavid Vrabel 			return ret;
4124e8c0c8cSDavid Vrabel 	}
4134e8c0c8cSDavid Vrabel 	return 0;
4144e8c0c8cSDavid Vrabel }
4154e8c0c8cSDavid Vrabel 
416a13d7201SJulien Grall /* Allocate pfns that are then mapped with gfns from foreign domid. Update
417d71f5139SMukesh Rathor  * the vma with the page info to use later.
418d71f5139SMukesh Rathor  * Returns: 0 if success, otherwise -errno
419d71f5139SMukesh Rathor  */
420d71f5139SMukesh Rathor static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)
421d71f5139SMukesh Rathor {
422d71f5139SMukesh Rathor 	int rc;
423d71f5139SMukesh Rathor 	struct page **pages;
424d71f5139SMukesh Rathor 
425d71f5139SMukesh Rathor 	pages = kcalloc(numpgs, sizeof(pages[0]), GFP_KERNEL);
426d71f5139SMukesh Rathor 	if (pages == NULL)
427d71f5139SMukesh Rathor 		return -ENOMEM;
428d71f5139SMukesh Rathor 
42981b286e0SDavid Vrabel 	rc = alloc_xenballooned_pages(numpgs, pages);
430d71f5139SMukesh Rathor 	if (rc != 0) {
431d71f5139SMukesh Rathor 		pr_warn("%s Could not alloc %d pfns rc:%d\n", __func__,
432d71f5139SMukesh Rathor 			numpgs, rc);
433d71f5139SMukesh Rathor 		kfree(pages);
434d71f5139SMukesh Rathor 		return -ENOMEM;
435d71f5139SMukesh Rathor 	}
436a5deabe0SAndres Lagar-Cavilla 	BUG_ON(vma->vm_private_data != NULL);
437d71f5139SMukesh Rathor 	vma->vm_private_data = pages;
438d71f5139SMukesh Rathor 
439d71f5139SMukesh Rathor 	return 0;
440d71f5139SMukesh Rathor }
441d71f5139SMukesh Rathor 
4427cbea8dcSKirill A. Shutemov static const struct vm_operations_struct privcmd_vm_ops;
443d8414d3cSBastian Blank 
4444610d240SPaul Durrant static long privcmd_ioctl_mmap_batch(
4454610d240SPaul Durrant 	struct file *file, void __user *udata, int version)
446d8414d3cSBastian Blank {
4474610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
448d8414d3cSBastian Blank 	int ret;
449ceb90fa0SAndres Lagar-Cavilla 	struct privcmd_mmapbatch_v2 m;
450d8414d3cSBastian Blank 	struct mm_struct *mm = current->mm;
451d8414d3cSBastian Blank 	struct vm_area_struct *vma;
452d8414d3cSBastian Blank 	unsigned long nr_pages;
453d8414d3cSBastian Blank 	LIST_HEAD(pagelist);
454d8414d3cSBastian Blank 	struct mmap_batch_state state;
455d8414d3cSBastian Blank 
456ceb90fa0SAndres Lagar-Cavilla 	switch (version) {
457ceb90fa0SAndres Lagar-Cavilla 	case 1:
458ceb90fa0SAndres Lagar-Cavilla 		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
459d8414d3cSBastian Blank 			return -EFAULT;
460ceb90fa0SAndres Lagar-Cavilla 		/* Returns per-frame error in m.arr. */
461ceb90fa0SAndres Lagar-Cavilla 		m.err = NULL;
462ceb90fa0SAndres Lagar-Cavilla 		if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
463ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
464ceb90fa0SAndres Lagar-Cavilla 		break;
465ceb90fa0SAndres Lagar-Cavilla 	case 2:
466ceb90fa0SAndres Lagar-Cavilla 		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
467ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
468ceb90fa0SAndres Lagar-Cavilla 		/* Returns per-frame error code in m.err. */
469ceb90fa0SAndres Lagar-Cavilla 		if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
470ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
471ceb90fa0SAndres Lagar-Cavilla 		break;
472ceb90fa0SAndres Lagar-Cavilla 	default:
473ceb90fa0SAndres Lagar-Cavilla 		return -EINVAL;
474ceb90fa0SAndres Lagar-Cavilla 	}
475d8414d3cSBastian Blank 
4764610d240SPaul Durrant 	/* If restriction is in place, check the domid matches */
4774610d240SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != m.dom)
4784610d240SPaul Durrant 		return -EPERM;
4794610d240SPaul Durrant 
4805995a68aSJulien Grall 	nr_pages = DIV_ROUND_UP(m.num, XEN_PFN_PER_PAGE);
481d8414d3cSBastian Blank 	if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
482d8414d3cSBastian Blank 		return -EINVAL;
483d8414d3cSBastian Blank 
484ceb90fa0SAndres Lagar-Cavilla 	ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t), m.arr);
485d8414d3cSBastian Blank 
486ceb90fa0SAndres Lagar-Cavilla 	if (ret)
487d8414d3cSBastian Blank 		goto out;
488ceb90fa0SAndres Lagar-Cavilla 	if (list_empty(&pagelist)) {
489ceb90fa0SAndres Lagar-Cavilla 		ret = -EINVAL;
490ceb90fa0SAndres Lagar-Cavilla 		goto out;
491ceb90fa0SAndres Lagar-Cavilla 	}
492ceb90fa0SAndres Lagar-Cavilla 
49399beae6cSAndres Lagar-Cavilla 	if (version == 2) {
49499beae6cSAndres Lagar-Cavilla 		/* Zero error array now to only copy back actual errors. */
49599beae6cSAndres Lagar-Cavilla 		if (clear_user(m.err, sizeof(int) * m.num)) {
49699beae6cSAndres Lagar-Cavilla 			ret = -EFAULT;
497ceb90fa0SAndres Lagar-Cavilla 			goto out;
498ceb90fa0SAndres Lagar-Cavilla 		}
49999beae6cSAndres Lagar-Cavilla 	}
500d8414d3cSBastian Blank 
501d8414d3cSBastian Blank 	down_write(&mm->mmap_sem);
502d8414d3cSBastian Blank 
503d8414d3cSBastian Blank 	vma = find_vma(mm, m.addr);
504d8414d3cSBastian Blank 	if (!vma ||
505a5deabe0SAndres Lagar-Cavilla 	    vma->vm_ops != &privcmd_vm_ops) {
50668fa965dSMats Petersson 		ret = -EINVAL;
507a5deabe0SAndres Lagar-Cavilla 		goto out_unlock;
508a5deabe0SAndres Lagar-Cavilla 	}
509a5deabe0SAndres Lagar-Cavilla 
510a5deabe0SAndres Lagar-Cavilla 	/*
511a5deabe0SAndres Lagar-Cavilla 	 * Caller must either:
512a5deabe0SAndres Lagar-Cavilla 	 *
513a5deabe0SAndres Lagar-Cavilla 	 * Map the whole VMA range, which will also allocate all the
514a5deabe0SAndres Lagar-Cavilla 	 * pages required for the auto_translated_physmap case.
515a5deabe0SAndres Lagar-Cavilla 	 *
516a5deabe0SAndres Lagar-Cavilla 	 * Or
517a5deabe0SAndres Lagar-Cavilla 	 *
518a5deabe0SAndres Lagar-Cavilla 	 * Map unmapped holes left from a previous map attempt (e.g.,
519a5deabe0SAndres Lagar-Cavilla 	 * because those foreign frames were previously paged out).
520a5deabe0SAndres Lagar-Cavilla 	 */
521a5deabe0SAndres Lagar-Cavilla 	if (vma->vm_private_data == NULL) {
522a5deabe0SAndres Lagar-Cavilla 		if (m.addr != vma->vm_start ||
523a5deabe0SAndres Lagar-Cavilla 		    m.addr + (nr_pages << PAGE_SHIFT) != vma->vm_end) {
524a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
525a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
526d8414d3cSBastian Blank 		}
527d71f5139SMukesh Rathor 		if (xen_feature(XENFEAT_auto_translated_physmap)) {
5285995a68aSJulien Grall 			ret = alloc_empty_pages(vma, nr_pages);
529a5deabe0SAndres Lagar-Cavilla 			if (ret < 0)
530a5deabe0SAndres Lagar-Cavilla 				goto out_unlock;
531a5deabe0SAndres Lagar-Cavilla 		} else
532a5deabe0SAndres Lagar-Cavilla 			vma->vm_private_data = PRIV_VMA_LOCKED;
533a5deabe0SAndres Lagar-Cavilla 	} else {
534a5deabe0SAndres Lagar-Cavilla 		if (m.addr < vma->vm_start ||
535a5deabe0SAndres Lagar-Cavilla 		    m.addr + (nr_pages << PAGE_SHIFT) > vma->vm_end) {
536a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
537a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
538a5deabe0SAndres Lagar-Cavilla 		}
539a5deabe0SAndres Lagar-Cavilla 		if (privcmd_vma_range_is_mapped(vma, m.addr, nr_pages)) {
540a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
541a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
542d71f5139SMukesh Rathor 		}
543d71f5139SMukesh Rathor 	}
544d8414d3cSBastian Blank 
545d8414d3cSBastian Blank 	state.domain        = m.dom;
546d8414d3cSBastian Blank 	state.vma           = vma;
547d8414d3cSBastian Blank 	state.va            = m.addr;
548d71f5139SMukesh Rathor 	state.index         = 0;
549ceb90fa0SAndres Lagar-Cavilla 	state.global_error  = 0;
55099beae6cSAndres Lagar-Cavilla 	state.version       = version;
551d8414d3cSBastian Blank 
5525995a68aSJulien Grall 	BUILD_BUG_ON(((PAGE_SIZE / sizeof(xen_pfn_t)) % XEN_PFN_PER_PAGE) != 0);
553ceb90fa0SAndres Lagar-Cavilla 	/* mmap_batch_fn guarantees ret == 0 */
5544e8c0c8cSDavid Vrabel 	BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
555ceb90fa0SAndres Lagar-Cavilla 				    &pagelist, mmap_batch_fn, &state));
556d8414d3cSBastian Blank 
557d8414d3cSBastian Blank 	up_write(&mm->mmap_sem);
558d8414d3cSBastian Blank 
55968fa965dSMats Petersson 	if (state.global_error) {
560ceb90fa0SAndres Lagar-Cavilla 		/* Write back errors in second pass. */
561a13d7201SJulien Grall 		state.user_gfn = (xen_pfn_t *)m.arr;
56299beae6cSAndres Lagar-Cavilla 		state.user_err = m.err;
5634e8c0c8cSDavid Vrabel 		ret = traverse_pages_block(m.num, sizeof(xen_pfn_t),
56499beae6cSAndres Lagar-Cavilla 					   &pagelist, mmap_return_errors, &state);
56568fa965dSMats Petersson 	} else
56668fa965dSMats Petersson 		ret = 0;
56768fa965dSMats Petersson 
568ceb90fa0SAndres Lagar-Cavilla 	/* If we have not had any EFAULT-like global errors then set the global
569ceb90fa0SAndres Lagar-Cavilla 	 * error to -ENOENT if necessary. */
570ceb90fa0SAndres Lagar-Cavilla 	if ((ret == 0) && (state.global_error == -ENOENT))
571ceb90fa0SAndres Lagar-Cavilla 		ret = -ENOENT;
572d8414d3cSBastian Blank 
573d8414d3cSBastian Blank out:
574d8414d3cSBastian Blank 	free_page_list(&pagelist);
575d8414d3cSBastian Blank 	return ret;
576a5deabe0SAndres Lagar-Cavilla 
577a5deabe0SAndres Lagar-Cavilla out_unlock:
578a5deabe0SAndres Lagar-Cavilla 	up_write(&mm->mmap_sem);
579a5deabe0SAndres Lagar-Cavilla 	goto out;
580d8414d3cSBastian Blank }
581d8414d3cSBastian Blank 
582ab520be8SPaul Durrant static int lock_pages(
583ab520be8SPaul Durrant 	struct privcmd_dm_op_buf kbufs[], unsigned int num,
584ab520be8SPaul Durrant 	struct page *pages[], unsigned int nr_pages)
585ab520be8SPaul Durrant {
586ab520be8SPaul Durrant 	unsigned int i;
587ab520be8SPaul Durrant 
588ab520be8SPaul Durrant 	for (i = 0; i < num; i++) {
589ab520be8SPaul Durrant 		unsigned int requested;
590ab520be8SPaul Durrant 		int pinned;
591ab520be8SPaul Durrant 
592ab520be8SPaul Durrant 		requested = DIV_ROUND_UP(
593ab520be8SPaul Durrant 			offset_in_page(kbufs[i].uptr) + kbufs[i].size,
594ab520be8SPaul Durrant 			PAGE_SIZE);
595ab520be8SPaul Durrant 		if (requested > nr_pages)
596ab520be8SPaul Durrant 			return -ENOSPC;
597ab520be8SPaul Durrant 
598ab520be8SPaul Durrant 		pinned = get_user_pages_fast(
599ab520be8SPaul Durrant 			(unsigned long) kbufs[i].uptr,
600ab520be8SPaul Durrant 			requested, FOLL_WRITE, pages);
601ab520be8SPaul Durrant 		if (pinned < 0)
602ab520be8SPaul Durrant 			return pinned;
603ab520be8SPaul Durrant 
604ab520be8SPaul Durrant 		nr_pages -= pinned;
605ab520be8SPaul Durrant 		pages += pinned;
606ab520be8SPaul Durrant 	}
607ab520be8SPaul Durrant 
608ab520be8SPaul Durrant 	return 0;
609ab520be8SPaul Durrant }
610ab520be8SPaul Durrant 
611ab520be8SPaul Durrant static void unlock_pages(struct page *pages[], unsigned int nr_pages)
612ab520be8SPaul Durrant {
613ab520be8SPaul Durrant 	unsigned int i;
614ab520be8SPaul Durrant 
615ab520be8SPaul Durrant 	if (!pages)
616ab520be8SPaul Durrant 		return;
617ab520be8SPaul Durrant 
618ab520be8SPaul Durrant 	for (i = 0; i < nr_pages; i++) {
619ab520be8SPaul Durrant 		if (pages[i])
620ab520be8SPaul Durrant 			put_page(pages[i]);
621ab520be8SPaul Durrant 	}
622ab520be8SPaul Durrant }
623ab520be8SPaul Durrant 
6244610d240SPaul Durrant static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
625ab520be8SPaul Durrant {
6264610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
627ab520be8SPaul Durrant 	struct privcmd_dm_op kdata;
628ab520be8SPaul Durrant 	struct privcmd_dm_op_buf *kbufs;
629ab520be8SPaul Durrant 	unsigned int nr_pages = 0;
630ab520be8SPaul Durrant 	struct page **pages = NULL;
631ab520be8SPaul Durrant 	struct xen_dm_op_buf *xbufs = NULL;
632ab520be8SPaul Durrant 	unsigned int i;
633ab520be8SPaul Durrant 	long rc;
634ab520be8SPaul Durrant 
635ab520be8SPaul Durrant 	if (copy_from_user(&kdata, udata, sizeof(kdata)))
636ab520be8SPaul Durrant 		return -EFAULT;
637ab520be8SPaul Durrant 
6384610d240SPaul Durrant 	/* If restriction is in place, check the domid matches */
6394610d240SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
6404610d240SPaul Durrant 		return -EPERM;
6414610d240SPaul Durrant 
642ab520be8SPaul Durrant 	if (kdata.num == 0)
643ab520be8SPaul Durrant 		return 0;
644ab520be8SPaul Durrant 
645ab520be8SPaul Durrant 	if (kdata.num > privcmd_dm_op_max_num)
646ab520be8SPaul Durrant 		return -E2BIG;
647ab520be8SPaul Durrant 
648ab520be8SPaul Durrant 	kbufs = kcalloc(kdata.num, sizeof(*kbufs), GFP_KERNEL);
649ab520be8SPaul Durrant 	if (!kbufs)
650ab520be8SPaul Durrant 		return -ENOMEM;
651ab520be8SPaul Durrant 
652ab520be8SPaul Durrant 	if (copy_from_user(kbufs, kdata.ubufs,
653ab520be8SPaul Durrant 			   sizeof(*kbufs) * kdata.num)) {
654ab520be8SPaul Durrant 		rc = -EFAULT;
655ab520be8SPaul Durrant 		goto out;
656ab520be8SPaul Durrant 	}
657ab520be8SPaul Durrant 
658ab520be8SPaul Durrant 	for (i = 0; i < kdata.num; i++) {
659ab520be8SPaul Durrant 		if (kbufs[i].size > privcmd_dm_op_buf_max_size) {
660ab520be8SPaul Durrant 			rc = -E2BIG;
661ab520be8SPaul Durrant 			goto out;
662ab520be8SPaul Durrant 		}
663ab520be8SPaul Durrant 
664ab520be8SPaul Durrant 		if (!access_ok(VERIFY_WRITE, kbufs[i].uptr,
665ab520be8SPaul Durrant 			       kbufs[i].size)) {
666ab520be8SPaul Durrant 			rc = -EFAULT;
667ab520be8SPaul Durrant 			goto out;
668ab520be8SPaul Durrant 		}
669ab520be8SPaul Durrant 
670ab520be8SPaul Durrant 		nr_pages += DIV_ROUND_UP(
671ab520be8SPaul Durrant 			offset_in_page(kbufs[i].uptr) + kbufs[i].size,
672ab520be8SPaul Durrant 			PAGE_SIZE);
673ab520be8SPaul Durrant 	}
674ab520be8SPaul Durrant 
675ab520be8SPaul Durrant 	pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
676ab520be8SPaul Durrant 	if (!pages) {
677ab520be8SPaul Durrant 		rc = -ENOMEM;
678ab520be8SPaul Durrant 		goto out;
679ab520be8SPaul Durrant 	}
680ab520be8SPaul Durrant 
681ab520be8SPaul Durrant 	xbufs = kcalloc(kdata.num, sizeof(*xbufs), GFP_KERNEL);
682ab520be8SPaul Durrant 	if (!xbufs) {
683ab520be8SPaul Durrant 		rc = -ENOMEM;
684ab520be8SPaul Durrant 		goto out;
685ab520be8SPaul Durrant 	}
686ab520be8SPaul Durrant 
687ab520be8SPaul Durrant 	rc = lock_pages(kbufs, kdata.num, pages, nr_pages);
688ab520be8SPaul Durrant 	if (rc)
689ab520be8SPaul Durrant 		goto out;
690ab520be8SPaul Durrant 
691ab520be8SPaul Durrant 	for (i = 0; i < kdata.num; i++) {
692ab520be8SPaul Durrant 		set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr);
693ab520be8SPaul Durrant 		xbufs[i].size = kbufs[i].size;
694ab520be8SPaul Durrant 	}
695ab520be8SPaul Durrant 
696ab520be8SPaul Durrant 	xen_preemptible_hcall_begin();
697ab520be8SPaul Durrant 	rc = HYPERVISOR_dm_op(kdata.dom, kdata.num, xbufs);
698ab520be8SPaul Durrant 	xen_preemptible_hcall_end();
699ab520be8SPaul Durrant 
700ab520be8SPaul Durrant out:
701ab520be8SPaul Durrant 	unlock_pages(pages, nr_pages);
702ab520be8SPaul Durrant 	kfree(xbufs);
703ab520be8SPaul Durrant 	kfree(pages);
704ab520be8SPaul Durrant 	kfree(kbufs);
705ab520be8SPaul Durrant 
706ab520be8SPaul Durrant 	return rc;
707ab520be8SPaul Durrant }
708ab520be8SPaul Durrant 
7094610d240SPaul Durrant static long privcmd_ioctl_restrict(struct file *file, void __user *udata)
7104610d240SPaul Durrant {
7114610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
7124610d240SPaul Durrant 	domid_t dom;
7134610d240SPaul Durrant 
7144610d240SPaul Durrant 	if (copy_from_user(&dom, udata, sizeof(dom)))
7154610d240SPaul Durrant 		return -EFAULT;
7164610d240SPaul Durrant 
7174610d240SPaul Durrant 	/* Set restriction to the specified domain, or check it matches */
7184610d240SPaul Durrant 	if (data->domid == DOMID_INVALID)
7194610d240SPaul Durrant 		data->domid = dom;
7204610d240SPaul Durrant 	else if (data->domid != dom)
7214610d240SPaul Durrant 		return -EINVAL;
7224610d240SPaul Durrant 
7234610d240SPaul Durrant 	return 0;
7244610d240SPaul Durrant }
7254610d240SPaul Durrant 
7263ad08765SPaul Durrant struct remap_pfn {
7273ad08765SPaul Durrant 	struct mm_struct *mm;
7283ad08765SPaul Durrant 	struct page **pages;
7293ad08765SPaul Durrant 	pgprot_t prot;
7303ad08765SPaul Durrant 	unsigned long i;
7313ad08765SPaul Durrant };
7323ad08765SPaul Durrant 
7333ad08765SPaul Durrant static int remap_pfn_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
7343ad08765SPaul Durrant 			void *data)
7353ad08765SPaul Durrant {
7363ad08765SPaul Durrant 	struct remap_pfn *r = data;
7373ad08765SPaul Durrant 	struct page *page = r->pages[r->i];
7383ad08765SPaul Durrant 	pte_t pte = pte_mkspecial(pfn_pte(page_to_pfn(page), r->prot));
7393ad08765SPaul Durrant 
7403ad08765SPaul Durrant 	set_pte_at(r->mm, addr, ptep, pte);
7413ad08765SPaul Durrant 	r->i++;
7423ad08765SPaul Durrant 
7433ad08765SPaul Durrant 	return 0;
7443ad08765SPaul Durrant }
7453ad08765SPaul Durrant 
7463ad08765SPaul Durrant static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
7473ad08765SPaul Durrant {
7483ad08765SPaul Durrant 	struct privcmd_data *data = file->private_data;
7493ad08765SPaul Durrant 	struct mm_struct *mm = current->mm;
7503ad08765SPaul Durrant 	struct vm_area_struct *vma;
7513ad08765SPaul Durrant 	struct privcmd_mmap_resource kdata;
7523ad08765SPaul Durrant 	xen_pfn_t *pfns = NULL;
7533ad08765SPaul Durrant 	struct xen_mem_acquire_resource xdata;
7543ad08765SPaul Durrant 	int rc;
7553ad08765SPaul Durrant 
7563ad08765SPaul Durrant 	if (copy_from_user(&kdata, udata, sizeof(kdata)))
7573ad08765SPaul Durrant 		return -EFAULT;
7583ad08765SPaul Durrant 
7593ad08765SPaul Durrant 	/* If restriction is in place, check the domid matches */
7603ad08765SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
7613ad08765SPaul Durrant 		return -EPERM;
7623ad08765SPaul Durrant 
7633ad08765SPaul Durrant 	down_write(&mm->mmap_sem);
7643ad08765SPaul Durrant 
7653ad08765SPaul Durrant 	vma = find_vma(mm, kdata.addr);
7663ad08765SPaul Durrant 	if (!vma || vma->vm_ops != &privcmd_vm_ops) {
7673ad08765SPaul Durrant 		rc = -EINVAL;
7683ad08765SPaul Durrant 		goto out;
7693ad08765SPaul Durrant 	}
7703ad08765SPaul Durrant 
7713ad08765SPaul Durrant 	pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL);
7723ad08765SPaul Durrant 	if (!pfns) {
7733ad08765SPaul Durrant 		rc = -ENOMEM;
7743ad08765SPaul Durrant 		goto out;
7753ad08765SPaul Durrant 	}
7763ad08765SPaul Durrant 
7773ad08765SPaul Durrant 	if (xen_feature(XENFEAT_auto_translated_physmap)) {
7783ad08765SPaul Durrant 		unsigned int nr = DIV_ROUND_UP(kdata.num, XEN_PFN_PER_PAGE);
7793ad08765SPaul Durrant 		struct page **pages;
7803ad08765SPaul Durrant 		unsigned int i;
7813ad08765SPaul Durrant 
7823ad08765SPaul Durrant 		rc = alloc_empty_pages(vma, nr);
7833ad08765SPaul Durrant 		if (rc < 0)
7843ad08765SPaul Durrant 			goto out;
7853ad08765SPaul Durrant 
7863ad08765SPaul Durrant 		pages = vma->vm_private_data;
7873ad08765SPaul Durrant 		for (i = 0; i < kdata.num; i++) {
7883ad08765SPaul Durrant 			xen_pfn_t pfn =
7893ad08765SPaul Durrant 				page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
7903ad08765SPaul Durrant 
7913ad08765SPaul Durrant 			pfns[i] = pfn + (i % XEN_PFN_PER_PAGE);
7923ad08765SPaul Durrant 		}
7933ad08765SPaul Durrant 	} else
7943ad08765SPaul Durrant 		vma->vm_private_data = PRIV_VMA_LOCKED;
7953ad08765SPaul Durrant 
7963ad08765SPaul Durrant 	memset(&xdata, 0, sizeof(xdata));
7973ad08765SPaul Durrant 	xdata.domid = kdata.dom;
7983ad08765SPaul Durrant 	xdata.type = kdata.type;
7993ad08765SPaul Durrant 	xdata.id = kdata.id;
8003ad08765SPaul Durrant 	xdata.frame = kdata.idx;
8013ad08765SPaul Durrant 	xdata.nr_frames = kdata.num;
8023ad08765SPaul Durrant 	set_xen_guest_handle(xdata.frame_list, pfns);
8033ad08765SPaul Durrant 
8043ad08765SPaul Durrant 	xen_preemptible_hcall_begin();
8053ad08765SPaul Durrant 	rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata);
8063ad08765SPaul Durrant 	xen_preemptible_hcall_end();
8073ad08765SPaul Durrant 
8083ad08765SPaul Durrant 	if (rc)
8093ad08765SPaul Durrant 		goto out;
8103ad08765SPaul Durrant 
8113ad08765SPaul Durrant 	if (xen_feature(XENFEAT_auto_translated_physmap)) {
8123ad08765SPaul Durrant 		struct remap_pfn r = {
8133ad08765SPaul Durrant 			.mm = vma->vm_mm,
8143ad08765SPaul Durrant 			.pages = vma->vm_private_data,
8153ad08765SPaul Durrant 			.prot = vma->vm_page_prot,
8163ad08765SPaul Durrant 		};
8173ad08765SPaul Durrant 
8183ad08765SPaul Durrant 		rc = apply_to_page_range(r.mm, kdata.addr,
8193ad08765SPaul Durrant 					 kdata.num << PAGE_SHIFT,
8203ad08765SPaul Durrant 					 remap_pfn_fn, &r);
8213ad08765SPaul Durrant 	} else {
8223ad08765SPaul Durrant 		unsigned int domid =
8233ad08765SPaul Durrant 			(xdata.flags & XENMEM_rsrc_acq_caller_owned) ?
8243ad08765SPaul Durrant 			DOMID_SELF : kdata.dom;
8253ad08765SPaul Durrant 		int num;
8263ad08765SPaul Durrant 
8273ad08765SPaul Durrant 		num = xen_remap_domain_mfn_array(vma,
8283ad08765SPaul Durrant 						 kdata.addr & PAGE_MASK,
8293ad08765SPaul Durrant 						 pfns, kdata.num, (int *)pfns,
8303ad08765SPaul Durrant 						 vma->vm_page_prot,
8313ad08765SPaul Durrant 						 domid,
8323ad08765SPaul Durrant 						 vma->vm_private_data);
8333ad08765SPaul Durrant 		if (num < 0)
8343ad08765SPaul Durrant 			rc = num;
8353ad08765SPaul Durrant 		else if (num != kdata.num) {
8363ad08765SPaul Durrant 			unsigned int i;
8373ad08765SPaul Durrant 
8383ad08765SPaul Durrant 			for (i = 0; i < num; i++) {
8393ad08765SPaul Durrant 				rc = pfns[i];
8403ad08765SPaul Durrant 				if (rc < 0)
8413ad08765SPaul Durrant 					break;
8423ad08765SPaul Durrant 			}
8433ad08765SPaul Durrant 		} else
8443ad08765SPaul Durrant 			rc = 0;
8453ad08765SPaul Durrant 	}
8463ad08765SPaul Durrant 
8473ad08765SPaul Durrant out:
8483ad08765SPaul Durrant 	up_write(&mm->mmap_sem);
8493ad08765SPaul Durrant 	kfree(pfns);
8503ad08765SPaul Durrant 
8513ad08765SPaul Durrant 	return rc;
8523ad08765SPaul Durrant }
8533ad08765SPaul Durrant 
854d8414d3cSBastian Blank static long privcmd_ioctl(struct file *file,
855d8414d3cSBastian Blank 			  unsigned int cmd, unsigned long data)
856d8414d3cSBastian Blank {
857dc9eab6fSPaul Durrant 	int ret = -ENOTTY;
858d8414d3cSBastian Blank 	void __user *udata = (void __user *) data;
859d8414d3cSBastian Blank 
860d8414d3cSBastian Blank 	switch (cmd) {
861d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_HYPERCALL:
8624610d240SPaul Durrant 		ret = privcmd_ioctl_hypercall(file, udata);
863d8414d3cSBastian Blank 		break;
864d8414d3cSBastian Blank 
865d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_MMAP:
8664610d240SPaul Durrant 		ret = privcmd_ioctl_mmap(file, udata);
867d8414d3cSBastian Blank 		break;
868d8414d3cSBastian Blank 
869d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_MMAPBATCH:
8704610d240SPaul Durrant 		ret = privcmd_ioctl_mmap_batch(file, udata, 1);
871ceb90fa0SAndres Lagar-Cavilla 		break;
872ceb90fa0SAndres Lagar-Cavilla 
873ceb90fa0SAndres Lagar-Cavilla 	case IOCTL_PRIVCMD_MMAPBATCH_V2:
8744610d240SPaul Durrant 		ret = privcmd_ioctl_mmap_batch(file, udata, 2);
875d8414d3cSBastian Blank 		break;
876d8414d3cSBastian Blank 
877ab520be8SPaul Durrant 	case IOCTL_PRIVCMD_DM_OP:
8784610d240SPaul Durrant 		ret = privcmd_ioctl_dm_op(file, udata);
8794610d240SPaul Durrant 		break;
8804610d240SPaul Durrant 
8814610d240SPaul Durrant 	case IOCTL_PRIVCMD_RESTRICT:
8824610d240SPaul Durrant 		ret = privcmd_ioctl_restrict(file, udata);
883ab520be8SPaul Durrant 		break;
884ab520be8SPaul Durrant 
8853ad08765SPaul Durrant 	case IOCTL_PRIVCMD_MMAP_RESOURCE:
8863ad08765SPaul Durrant 		ret = privcmd_ioctl_mmap_resource(file, udata);
8873ad08765SPaul Durrant 		break;
8883ad08765SPaul Durrant 
889d8414d3cSBastian Blank 	default:
890d8414d3cSBastian Blank 		break;
891d8414d3cSBastian Blank 	}
892d8414d3cSBastian Blank 
893d8414d3cSBastian Blank 	return ret;
894d8414d3cSBastian Blank }
895d8414d3cSBastian Blank 
8964610d240SPaul Durrant static int privcmd_open(struct inode *ino, struct file *file)
8974610d240SPaul Durrant {
8984610d240SPaul Durrant 	struct privcmd_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
8994610d240SPaul Durrant 
9004610d240SPaul Durrant 	if (!data)
9014610d240SPaul Durrant 		return -ENOMEM;
9024610d240SPaul Durrant 
9034610d240SPaul Durrant 	/* DOMID_INVALID implies no restriction */
9044610d240SPaul Durrant 	data->domid = DOMID_INVALID;
9054610d240SPaul Durrant 
9064610d240SPaul Durrant 	file->private_data = data;
9074610d240SPaul Durrant 	return 0;
9084610d240SPaul Durrant }
9094610d240SPaul Durrant 
9104610d240SPaul Durrant static int privcmd_release(struct inode *ino, struct file *file)
9114610d240SPaul Durrant {
9124610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
9134610d240SPaul Durrant 
9144610d240SPaul Durrant 	kfree(data);
9154610d240SPaul Durrant 	return 0;
9164610d240SPaul Durrant }
9174610d240SPaul Durrant 
918d71f5139SMukesh Rathor static void privcmd_close(struct vm_area_struct *vma)
919d71f5139SMukesh Rathor {
920d71f5139SMukesh Rathor 	struct page **pages = vma->vm_private_data;
921c7ebf9d9SMuhammad Falak R Wani 	int numpgs = vma_pages(vma);
9225995a68aSJulien Grall 	int numgfns = (vma->vm_end - vma->vm_start) >> XEN_PAGE_SHIFT;
923b6497b38SIan Campbell 	int rc;
924d71f5139SMukesh Rathor 
9259eff37a8SDan Carpenter 	if (!xen_feature(XENFEAT_auto_translated_physmap) || !numpgs || !pages)
926d71f5139SMukesh Rathor 		return;
927d71f5139SMukesh Rathor 
9285995a68aSJulien Grall 	rc = xen_unmap_domain_gfn_range(vma, numgfns, pages);
929b6497b38SIan Campbell 	if (rc == 0)
930d71f5139SMukesh Rathor 		free_xenballooned_pages(numpgs, pages);
931b6497b38SIan Campbell 	else
932b6497b38SIan Campbell 		pr_crit("unable to unmap MFN range: leaking %d pages. rc=%d\n",
933b6497b38SIan Campbell 			numpgs, rc);
934d71f5139SMukesh Rathor 	kfree(pages);
935d71f5139SMukesh Rathor }
936d71f5139SMukesh Rathor 
9374bf2cc96SSouptick Joarder static vm_fault_t privcmd_fault(struct vm_fault *vmf)
938d8414d3cSBastian Blank {
939d8414d3cSBastian Blank 	printk(KERN_DEBUG "privcmd_fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
94011bac800SDave Jiang 	       vmf->vma, vmf->vma->vm_start, vmf->vma->vm_end,
9411a29d85eSJan Kara 	       vmf->pgoff, (void *)vmf->address);
942d8414d3cSBastian Blank 
943d8414d3cSBastian Blank 	return VM_FAULT_SIGBUS;
944d8414d3cSBastian Blank }
945d8414d3cSBastian Blank 
9467cbea8dcSKirill A. Shutemov static const struct vm_operations_struct privcmd_vm_ops = {
947d71f5139SMukesh Rathor 	.close = privcmd_close,
948d8414d3cSBastian Blank 	.fault = privcmd_fault
949d8414d3cSBastian Blank };
950d8414d3cSBastian Blank 
951d8414d3cSBastian Blank static int privcmd_mmap(struct file *file, struct vm_area_struct *vma)
952d8414d3cSBastian Blank {
953d8414d3cSBastian Blank 	/* DONTCOPY is essential for Xen because copy_page_range doesn't know
954d8414d3cSBastian Blank 	 * how to recreate these mappings */
955314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTCOPY |
956314e51b9SKonstantin Khlebnikov 			 VM_DONTEXPAND | VM_DONTDUMP;
957d8414d3cSBastian Blank 	vma->vm_ops = &privcmd_vm_ops;
958d8414d3cSBastian Blank 	vma->vm_private_data = NULL;
959d8414d3cSBastian Blank 
960d8414d3cSBastian Blank 	return 0;
961d8414d3cSBastian Blank }
962d8414d3cSBastian Blank 
963a5deabe0SAndres Lagar-Cavilla /*
964a5deabe0SAndres Lagar-Cavilla  * For MMAPBATCH*. This allows asserting the singleshot mapping
965a5deabe0SAndres Lagar-Cavilla  * on a per pfn/pte basis. Mapping calls that fail with ENOENT
966a5deabe0SAndres Lagar-Cavilla  * can be then retried until success.
967a5deabe0SAndres Lagar-Cavilla  */
968a5deabe0SAndres Lagar-Cavilla static int is_mapped_fn(pte_t *pte, struct page *pmd_page,
969a5deabe0SAndres Lagar-Cavilla 	                unsigned long addr, void *data)
970d8414d3cSBastian Blank {
971a5deabe0SAndres Lagar-Cavilla 	return pte_none(*pte) ? 0 : -EBUSY;
972a5deabe0SAndres Lagar-Cavilla }
973a5deabe0SAndres Lagar-Cavilla 
974a5deabe0SAndres Lagar-Cavilla static int privcmd_vma_range_is_mapped(
975a5deabe0SAndres Lagar-Cavilla 	           struct vm_area_struct *vma,
976a5deabe0SAndres Lagar-Cavilla 	           unsigned long addr,
977a5deabe0SAndres Lagar-Cavilla 	           unsigned long nr_pages)
978a5deabe0SAndres Lagar-Cavilla {
979a5deabe0SAndres Lagar-Cavilla 	return apply_to_page_range(vma->vm_mm, addr, nr_pages << PAGE_SHIFT,
980a5deabe0SAndres Lagar-Cavilla 				   is_mapped_fn, NULL) != 0;
981d8414d3cSBastian Blank }
982d8414d3cSBastian Blank 
983d8414d3cSBastian Blank const struct file_operations xen_privcmd_fops = {
984d8414d3cSBastian Blank 	.owner = THIS_MODULE,
985d8414d3cSBastian Blank 	.unlocked_ioctl = privcmd_ioctl,
9864610d240SPaul Durrant 	.open = privcmd_open,
9874610d240SPaul Durrant 	.release = privcmd_release,
988d8414d3cSBastian Blank 	.mmap = privcmd_mmap,
989d8414d3cSBastian Blank };
990d8414d3cSBastian Blank EXPORT_SYMBOL_GPL(xen_privcmd_fops);
991d8414d3cSBastian Blank 
992d8414d3cSBastian Blank static struct miscdevice privcmd_dev = {
993d8414d3cSBastian Blank 	.minor = MISC_DYNAMIC_MINOR,
994d8414d3cSBastian Blank 	.name = "xen/privcmd",
995d8414d3cSBastian Blank 	.fops = &xen_privcmd_fops,
996d8414d3cSBastian Blank };
997d8414d3cSBastian Blank 
998d8414d3cSBastian Blank static int __init privcmd_init(void)
999d8414d3cSBastian Blank {
1000d8414d3cSBastian Blank 	int err;
1001d8414d3cSBastian Blank 
1002d8414d3cSBastian Blank 	if (!xen_domain())
1003d8414d3cSBastian Blank 		return -ENODEV;
1004d8414d3cSBastian Blank 
1005d8414d3cSBastian Blank 	err = misc_register(&privcmd_dev);
1006d8414d3cSBastian Blank 	if (err != 0) {
1007283c0972SJoe Perches 		pr_err("Could not register Xen privcmd device\n");
1008d8414d3cSBastian Blank 		return err;
1009d8414d3cSBastian Blank 	}
1010*c51b3c63SJuergen Gross 
1011*c51b3c63SJuergen Gross 	err = misc_register(&xen_privcmdbuf_dev);
1012*c51b3c63SJuergen Gross 	if (err != 0) {
1013*c51b3c63SJuergen Gross 		pr_err("Could not register Xen hypercall-buf device\n");
1014*c51b3c63SJuergen Gross 		misc_deregister(&privcmd_dev);
1015*c51b3c63SJuergen Gross 		return err;
1016*c51b3c63SJuergen Gross 	}
1017*c51b3c63SJuergen Gross 
1018d8414d3cSBastian Blank 	return 0;
1019d8414d3cSBastian Blank }
1020d8414d3cSBastian Blank 
1021d8414d3cSBastian Blank static void __exit privcmd_exit(void)
1022d8414d3cSBastian Blank {
1023d8414d3cSBastian Blank 	misc_deregister(&privcmd_dev);
1024*c51b3c63SJuergen Gross 	misc_deregister(&xen_privcmdbuf_dev);
1025d8414d3cSBastian Blank }
1026d8414d3cSBastian Blank 
1027d8414d3cSBastian Blank module_init(privcmd_init);
1028d8414d3cSBastian Blank module_exit(privcmd_exit);
1029