xref: /openbmc/linux/drivers/xen/privcmd.c (revision ff669aa8124035a432d2b7eecdd876b308cd48ee)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d8414d3cSBastian Blank /******************************************************************************
3d8414d3cSBastian Blank  * privcmd.c
4d8414d3cSBastian Blank  *
5d8414d3cSBastian Blank  * Interface to privileged domain-0 commands.
6d8414d3cSBastian Blank  *
7d8414d3cSBastian Blank  * Copyright (c) 2002-2004, K A Fraser, B Dragovic
8d8414d3cSBastian Blank  */
9d8414d3cSBastian Blank 
10283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
11283c0972SJoe Perches 
12d8414d3cSBastian Blank #include <linux/kernel.h>
13d8414d3cSBastian Blank #include <linux/module.h>
14d8414d3cSBastian Blank #include <linux/sched.h>
15d8414d3cSBastian Blank #include <linux/slab.h>
16d8414d3cSBastian Blank #include <linux/string.h>
17d8414d3cSBastian Blank #include <linux/errno.h>
18d8414d3cSBastian Blank #include <linux/mm.h>
19d8414d3cSBastian Blank #include <linux/mman.h>
20d8414d3cSBastian Blank #include <linux/uaccess.h>
21d8414d3cSBastian Blank #include <linux/swap.h>
22d8414d3cSBastian Blank #include <linux/highmem.h>
23d8414d3cSBastian Blank #include <linux/pagemap.h>
24d8414d3cSBastian Blank #include <linux/seq_file.h>
25d8414d3cSBastian Blank #include <linux/miscdevice.h>
26ab520be8SPaul Durrant #include <linux/moduleparam.h>
27d8414d3cSBastian Blank 
28d8414d3cSBastian Blank #include <asm/pgalloc.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>
353ad08765SPaul Durrant #include <xen/interface/memory.h>
36ab520be8SPaul Durrant #include <xen/interface/hvm/dm_op.h>
37d8414d3cSBastian Blank #include <xen/features.h>
38d8414d3cSBastian Blank #include <xen/page.h>
39d8414d3cSBastian Blank #include <xen/xen-ops.h>
40d71f5139SMukesh Rathor #include <xen/balloon.h>
41d8414d3cSBastian Blank 
42d8414d3cSBastian Blank #include "privcmd.h"
43d8414d3cSBastian Blank 
44d8414d3cSBastian Blank MODULE_LICENSE("GPL");
45d8414d3cSBastian Blank 
46d71f5139SMukesh Rathor #define PRIV_VMA_LOCKED ((void *)1)
47d71f5139SMukesh Rathor 
48ab520be8SPaul Durrant static unsigned int privcmd_dm_op_max_num = 16;
49ab520be8SPaul Durrant module_param_named(dm_op_max_nr_bufs, privcmd_dm_op_max_num, uint, 0644);
50ab520be8SPaul Durrant MODULE_PARM_DESC(dm_op_max_nr_bufs,
51ab520be8SPaul Durrant 		 "Maximum number of buffers per dm_op hypercall");
52ab520be8SPaul Durrant 
53ab520be8SPaul Durrant static unsigned int privcmd_dm_op_buf_max_size = 4096;
54ab520be8SPaul Durrant module_param_named(dm_op_buf_max_size, privcmd_dm_op_buf_max_size, uint,
55ab520be8SPaul Durrant 		   0644);
56ab520be8SPaul Durrant MODULE_PARM_DESC(dm_op_buf_max_size,
57ab520be8SPaul Durrant 		 "Maximum size of a dm_op hypercall buffer");
58ab520be8SPaul Durrant 
594610d240SPaul Durrant struct privcmd_data {
604610d240SPaul Durrant 	domid_t domid;
614610d240SPaul Durrant };
624610d240SPaul Durrant 
63a5deabe0SAndres Lagar-Cavilla static int privcmd_vma_range_is_mapped(
64a5deabe0SAndres Lagar-Cavilla                struct vm_area_struct *vma,
65a5deabe0SAndres Lagar-Cavilla                unsigned long addr,
66a5deabe0SAndres Lagar-Cavilla                unsigned long nr_pages);
67d8414d3cSBastian Blank 
684610d240SPaul Durrant static long privcmd_ioctl_hypercall(struct file *file, void __user *udata)
69d8414d3cSBastian Blank {
704610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
71d8414d3cSBastian Blank 	struct privcmd_hypercall hypercall;
72d8414d3cSBastian Blank 	long ret;
73d8414d3cSBastian Blank 
744610d240SPaul Durrant 	/* Disallow arbitrary hypercalls if restricted */
754610d240SPaul Durrant 	if (data->domid != DOMID_INVALID)
764610d240SPaul Durrant 		return -EPERM;
774610d240SPaul Durrant 
78d8414d3cSBastian Blank 	if (copy_from_user(&hypercall, udata, sizeof(hypercall)))
79d8414d3cSBastian Blank 		return -EFAULT;
80d8414d3cSBastian Blank 
81fdfd811dSDavid Vrabel 	xen_preemptible_hcall_begin();
82d8414d3cSBastian Blank 	ret = privcmd_call(hypercall.op,
83d8414d3cSBastian Blank 			   hypercall.arg[0], hypercall.arg[1],
84d8414d3cSBastian Blank 			   hypercall.arg[2], hypercall.arg[3],
85d8414d3cSBastian Blank 			   hypercall.arg[4]);
86fdfd811dSDavid Vrabel 	xen_preemptible_hcall_end();
87d8414d3cSBastian Blank 
88d8414d3cSBastian Blank 	return ret;
89d8414d3cSBastian Blank }
90d8414d3cSBastian Blank 
91d8414d3cSBastian Blank static void free_page_list(struct list_head *pages)
92d8414d3cSBastian Blank {
93d8414d3cSBastian Blank 	struct page *p, *n;
94d8414d3cSBastian Blank 
95d8414d3cSBastian Blank 	list_for_each_entry_safe(p, n, pages, lru)
96d8414d3cSBastian Blank 		__free_page(p);
97d8414d3cSBastian Blank 
98d8414d3cSBastian Blank 	INIT_LIST_HEAD(pages);
99d8414d3cSBastian Blank }
100d8414d3cSBastian Blank 
101d8414d3cSBastian Blank /*
102d8414d3cSBastian Blank  * Given an array of items in userspace, return a list of pages
103d8414d3cSBastian Blank  * containing the data.  If copying fails, either because of memory
104d8414d3cSBastian Blank  * allocation failure or a problem reading user memory, return an
105d8414d3cSBastian Blank  * error code; its up to the caller to dispose of any partial list.
106d8414d3cSBastian Blank  */
107d8414d3cSBastian Blank static int gather_array(struct list_head *pagelist,
108d8414d3cSBastian Blank 			unsigned nelem, size_t size,
109ceb90fa0SAndres Lagar-Cavilla 			const void __user *data)
110d8414d3cSBastian Blank {
111d8414d3cSBastian Blank 	unsigned pageidx;
112d8414d3cSBastian Blank 	void *pagedata;
113d8414d3cSBastian Blank 	int ret;
114d8414d3cSBastian Blank 
115d8414d3cSBastian Blank 	if (size > PAGE_SIZE)
116d8414d3cSBastian Blank 		return 0;
117d8414d3cSBastian Blank 
118d8414d3cSBastian Blank 	pageidx = PAGE_SIZE;
119d8414d3cSBastian Blank 	pagedata = NULL;	/* quiet, gcc */
120d8414d3cSBastian Blank 	while (nelem--) {
121d8414d3cSBastian Blank 		if (pageidx > PAGE_SIZE-size) {
122d8414d3cSBastian Blank 			struct page *page = alloc_page(GFP_KERNEL);
123d8414d3cSBastian Blank 
124d8414d3cSBastian Blank 			ret = -ENOMEM;
125d8414d3cSBastian Blank 			if (page == NULL)
126d8414d3cSBastian Blank 				goto fail;
127d8414d3cSBastian Blank 
128d8414d3cSBastian Blank 			pagedata = page_address(page);
129d8414d3cSBastian Blank 
130d8414d3cSBastian Blank 			list_add_tail(&page->lru, pagelist);
131d8414d3cSBastian Blank 			pageidx = 0;
132d8414d3cSBastian Blank 		}
133d8414d3cSBastian Blank 
134d8414d3cSBastian Blank 		ret = -EFAULT;
135d8414d3cSBastian Blank 		if (copy_from_user(pagedata + pageidx, data, size))
136d8414d3cSBastian Blank 			goto fail;
137d8414d3cSBastian Blank 
138d8414d3cSBastian Blank 		data += size;
139d8414d3cSBastian Blank 		pageidx += size;
140d8414d3cSBastian Blank 	}
141d8414d3cSBastian Blank 
142d8414d3cSBastian Blank 	ret = 0;
143d8414d3cSBastian Blank 
144d8414d3cSBastian Blank fail:
145d8414d3cSBastian Blank 	return ret;
146d8414d3cSBastian Blank }
147d8414d3cSBastian Blank 
148d8414d3cSBastian Blank /*
149d8414d3cSBastian Blank  * Call function "fn" on each element of the array fragmented
150d8414d3cSBastian Blank  * over a list of pages.
151d8414d3cSBastian Blank  */
152d8414d3cSBastian Blank static int traverse_pages(unsigned nelem, size_t size,
153d8414d3cSBastian Blank 			  struct list_head *pos,
154d8414d3cSBastian Blank 			  int (*fn)(void *data, void *state),
155d8414d3cSBastian Blank 			  void *state)
156d8414d3cSBastian Blank {
157d8414d3cSBastian Blank 	void *pagedata;
158d8414d3cSBastian Blank 	unsigned pageidx;
159d8414d3cSBastian Blank 	int ret = 0;
160d8414d3cSBastian Blank 
161d8414d3cSBastian Blank 	BUG_ON(size > PAGE_SIZE);
162d8414d3cSBastian Blank 
163d8414d3cSBastian Blank 	pageidx = PAGE_SIZE;
164d8414d3cSBastian Blank 	pagedata = NULL;	/* hush, gcc */
165d8414d3cSBastian Blank 
166d8414d3cSBastian Blank 	while (nelem--) {
167d8414d3cSBastian Blank 		if (pageidx > PAGE_SIZE-size) {
168d8414d3cSBastian Blank 			struct page *page;
169d8414d3cSBastian Blank 			pos = pos->next;
170d8414d3cSBastian Blank 			page = list_entry(pos, struct page, lru);
171d8414d3cSBastian Blank 			pagedata = page_address(page);
172d8414d3cSBastian Blank 			pageidx = 0;
173d8414d3cSBastian Blank 		}
174d8414d3cSBastian Blank 
175d8414d3cSBastian Blank 		ret = (*fn)(pagedata + pageidx, state);
176d8414d3cSBastian Blank 		if (ret)
177d8414d3cSBastian Blank 			break;
178d8414d3cSBastian Blank 		pageidx += size;
179d8414d3cSBastian Blank 	}
180d8414d3cSBastian Blank 
181d8414d3cSBastian Blank 	return ret;
182d8414d3cSBastian Blank }
183d8414d3cSBastian Blank 
1844e8c0c8cSDavid Vrabel /*
1854e8c0c8cSDavid Vrabel  * Similar to traverse_pages, but use each page as a "block" of
1864e8c0c8cSDavid Vrabel  * data to be processed as one unit.
1874e8c0c8cSDavid Vrabel  */
1884e8c0c8cSDavid Vrabel static int traverse_pages_block(unsigned nelem, size_t size,
1894e8c0c8cSDavid Vrabel 				struct list_head *pos,
1904e8c0c8cSDavid Vrabel 				int (*fn)(void *data, int nr, void *state),
1914e8c0c8cSDavid Vrabel 				void *state)
1924e8c0c8cSDavid Vrabel {
1934e8c0c8cSDavid Vrabel 	void *pagedata;
1944e8c0c8cSDavid Vrabel 	int ret = 0;
1954e8c0c8cSDavid Vrabel 
1964e8c0c8cSDavid Vrabel 	BUG_ON(size > PAGE_SIZE);
1974e8c0c8cSDavid Vrabel 
1984e8c0c8cSDavid Vrabel 	while (nelem) {
1994e8c0c8cSDavid Vrabel 		int nr = (PAGE_SIZE/size);
2004e8c0c8cSDavid Vrabel 		struct page *page;
2014e8c0c8cSDavid Vrabel 		if (nr > nelem)
2024e8c0c8cSDavid Vrabel 			nr = nelem;
2034e8c0c8cSDavid Vrabel 		pos = pos->next;
2044e8c0c8cSDavid Vrabel 		page = list_entry(pos, struct page, lru);
2054e8c0c8cSDavid Vrabel 		pagedata = page_address(page);
2064e8c0c8cSDavid Vrabel 		ret = (*fn)(pagedata, nr, state);
2074e8c0c8cSDavid Vrabel 		if (ret)
2084e8c0c8cSDavid Vrabel 			break;
2094e8c0c8cSDavid Vrabel 		nelem -= nr;
2104e8c0c8cSDavid Vrabel 	}
2114e8c0c8cSDavid Vrabel 
2124e8c0c8cSDavid Vrabel 	return ret;
2134e8c0c8cSDavid Vrabel }
2144e8c0c8cSDavid Vrabel 
215a13d7201SJulien Grall struct mmap_gfn_state {
216d8414d3cSBastian Blank 	unsigned long va;
217d8414d3cSBastian Blank 	struct vm_area_struct *vma;
218d8414d3cSBastian Blank 	domid_t domain;
219d8414d3cSBastian Blank };
220d8414d3cSBastian Blank 
221a13d7201SJulien Grall static int mmap_gfn_range(void *data, void *state)
222d8414d3cSBastian Blank {
223d8414d3cSBastian Blank 	struct privcmd_mmap_entry *msg = data;
224a13d7201SJulien Grall 	struct mmap_gfn_state *st = state;
225d8414d3cSBastian Blank 	struct vm_area_struct *vma = st->vma;
226d8414d3cSBastian Blank 	int rc;
227d8414d3cSBastian Blank 
228d8414d3cSBastian Blank 	/* Do not allow range to wrap the address space. */
229d8414d3cSBastian Blank 	if ((msg->npages > (LONG_MAX >> PAGE_SHIFT)) ||
230d8414d3cSBastian Blank 	    ((unsigned long)(msg->npages << PAGE_SHIFT) >= -st->va))
231d8414d3cSBastian Blank 		return -EINVAL;
232d8414d3cSBastian Blank 
233d8414d3cSBastian Blank 	/* Range chunks must be contiguous in va space. */
234d8414d3cSBastian Blank 	if ((msg->va != st->va) ||
235d8414d3cSBastian Blank 	    ((msg->va+(msg->npages<<PAGE_SHIFT)) > vma->vm_end))
236d8414d3cSBastian Blank 		return -EINVAL;
237d8414d3cSBastian Blank 
238a13d7201SJulien Grall 	rc = xen_remap_domain_gfn_range(vma,
239d8414d3cSBastian Blank 					msg->va & PAGE_MASK,
240d8414d3cSBastian Blank 					msg->mfn, msg->npages,
241d8414d3cSBastian Blank 					vma->vm_page_prot,
2429a032e39SIan Campbell 					st->domain, NULL);
243d8414d3cSBastian Blank 	if (rc < 0)
244d8414d3cSBastian Blank 		return rc;
245d8414d3cSBastian Blank 
246d8414d3cSBastian Blank 	st->va += msg->npages << PAGE_SHIFT;
247d8414d3cSBastian Blank 
248d8414d3cSBastian Blank 	return 0;
249d8414d3cSBastian Blank }
250d8414d3cSBastian Blank 
2514610d240SPaul Durrant static long privcmd_ioctl_mmap(struct file *file, void __user *udata)
252d8414d3cSBastian Blank {
2534610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
254d8414d3cSBastian Blank 	struct privcmd_mmap mmapcmd;
255d8414d3cSBastian Blank 	struct mm_struct *mm = current->mm;
256d8414d3cSBastian Blank 	struct vm_area_struct *vma;
257d8414d3cSBastian Blank 	int rc;
258d8414d3cSBastian Blank 	LIST_HEAD(pagelist);
259a13d7201SJulien Grall 	struct mmap_gfn_state state;
260d8414d3cSBastian Blank 
261d71f5139SMukesh Rathor 	/* We only support privcmd_ioctl_mmap_batch for auto translated. */
262d71f5139SMukesh Rathor 	if (xen_feature(XENFEAT_auto_translated_physmap))
263d71f5139SMukesh Rathor 		return -ENOSYS;
264d71f5139SMukesh Rathor 
265d8414d3cSBastian Blank 	if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
266d8414d3cSBastian Blank 		return -EFAULT;
267d8414d3cSBastian Blank 
2684610d240SPaul Durrant 	/* If restriction is in place, check the domid matches */
2694610d240SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != mmapcmd.dom)
2704610d240SPaul Durrant 		return -EPERM;
2714610d240SPaul Durrant 
272d8414d3cSBastian Blank 	rc = gather_array(&pagelist,
273d8414d3cSBastian Blank 			  mmapcmd.num, sizeof(struct privcmd_mmap_entry),
274d8414d3cSBastian Blank 			  mmapcmd.entry);
275d8414d3cSBastian Blank 
276d8414d3cSBastian Blank 	if (rc || list_empty(&pagelist))
277d8414d3cSBastian Blank 		goto out;
278d8414d3cSBastian Blank 
279d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
280d8414d3cSBastian Blank 
281d8414d3cSBastian Blank 	{
282d8414d3cSBastian Blank 		struct page *page = list_first_entry(&pagelist,
283d8414d3cSBastian Blank 						     struct page, lru);
284d8414d3cSBastian Blank 		struct privcmd_mmap_entry *msg = page_address(page);
285d8414d3cSBastian Blank 
286d8414d3cSBastian Blank 		vma = find_vma(mm, msg->va);
287d8414d3cSBastian Blank 		rc = -EINVAL;
288d8414d3cSBastian Blank 
289a5deabe0SAndres Lagar-Cavilla 		if (!vma || (msg->va != vma->vm_start) || vma->vm_private_data)
290d8414d3cSBastian Blank 			goto out_up;
291a5deabe0SAndres Lagar-Cavilla 		vma->vm_private_data = PRIV_VMA_LOCKED;
292d8414d3cSBastian Blank 	}
293d8414d3cSBastian Blank 
294d8414d3cSBastian Blank 	state.va = vma->vm_start;
295d8414d3cSBastian Blank 	state.vma = vma;
296d8414d3cSBastian Blank 	state.domain = mmapcmd.dom;
297d8414d3cSBastian Blank 
298d8414d3cSBastian Blank 	rc = traverse_pages(mmapcmd.num, sizeof(struct privcmd_mmap_entry),
299d8414d3cSBastian Blank 			    &pagelist,
300a13d7201SJulien Grall 			    mmap_gfn_range, &state);
301d8414d3cSBastian Blank 
302d8414d3cSBastian Blank 
303d8414d3cSBastian Blank out_up:
304d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
305d8414d3cSBastian Blank 
306d8414d3cSBastian Blank out:
307d8414d3cSBastian Blank 	free_page_list(&pagelist);
308d8414d3cSBastian Blank 
309d8414d3cSBastian Blank 	return rc;
310d8414d3cSBastian Blank }
311d8414d3cSBastian Blank 
312d8414d3cSBastian Blank struct mmap_batch_state {
313d8414d3cSBastian Blank 	domid_t domain;
314d8414d3cSBastian Blank 	unsigned long va;
315d8414d3cSBastian Blank 	struct vm_area_struct *vma;
316d71f5139SMukesh Rathor 	int index;
317ceb90fa0SAndres Lagar-Cavilla 	/* A tristate:
318ceb90fa0SAndres Lagar-Cavilla 	 *      0 for no errors
319ceb90fa0SAndres Lagar-Cavilla 	 *      1 if at least one error has happened (and no
320ceb90fa0SAndres Lagar-Cavilla 	 *          -ENOENT errors have happened)
321ceb90fa0SAndres Lagar-Cavilla 	 *      -ENOENT if at least 1 -ENOENT has happened.
322ceb90fa0SAndres Lagar-Cavilla 	 */
323ceb90fa0SAndres Lagar-Cavilla 	int global_error;
32499beae6cSAndres Lagar-Cavilla 	int version;
325d8414d3cSBastian Blank 
326a13d7201SJulien Grall 	/* User-space gfn array to store errors in the second pass for V1. */
327a13d7201SJulien Grall 	xen_pfn_t __user *user_gfn;
32899beae6cSAndres Lagar-Cavilla 	/* User-space int array to store errors in the second pass for V2. */
32999beae6cSAndres Lagar-Cavilla 	int __user *user_err;
330d8414d3cSBastian Blank };
331d8414d3cSBastian Blank 
332a13d7201SJulien Grall /* auto translated dom0 note: if domU being created is PV, then gfn is
333a13d7201SJulien Grall  * mfn(addr on bus). If it's auto xlated, then gfn is pfn (input to HAP).
334d71f5139SMukesh Rathor  */
3354e8c0c8cSDavid Vrabel static int mmap_batch_fn(void *data, int nr, void *state)
336d8414d3cSBastian Blank {
337a13d7201SJulien Grall 	xen_pfn_t *gfnp = data;
338d8414d3cSBastian Blank 	struct mmap_batch_state *st = state;
339d71f5139SMukesh Rathor 	struct vm_area_struct *vma = st->vma;
340d71f5139SMukesh Rathor 	struct page **pages = vma->vm_private_data;
3414e8c0c8cSDavid Vrabel 	struct page **cur_pages = NULL;
342ceb90fa0SAndres Lagar-Cavilla 	int ret;
343d8414d3cSBastian Blank 
344d71f5139SMukesh Rathor 	if (xen_feature(XENFEAT_auto_translated_physmap))
3454e8c0c8cSDavid Vrabel 		cur_pages = &pages[st->index];
346d71f5139SMukesh Rathor 
3474e8c0c8cSDavid Vrabel 	BUG_ON(nr < 0);
348a13d7201SJulien Grall 	ret = xen_remap_domain_gfn_array(st->vma, st->va & PAGE_MASK, gfnp, nr,
349a13d7201SJulien Grall 					 (int *)gfnp, st->vma->vm_page_prot,
3504e8c0c8cSDavid Vrabel 					 st->domain, cur_pages);
351ceb90fa0SAndres Lagar-Cavilla 
3524e8c0c8cSDavid Vrabel 	/* Adjust the global_error? */
3534e8c0c8cSDavid Vrabel 	if (ret != nr) {
354ceb90fa0SAndres Lagar-Cavilla 		if (ret == -ENOENT)
355ceb90fa0SAndres Lagar-Cavilla 			st->global_error = -ENOENT;
356ceb90fa0SAndres Lagar-Cavilla 		else {
357ceb90fa0SAndres Lagar-Cavilla 			/* Record that at least one error has happened. */
358ceb90fa0SAndres Lagar-Cavilla 			if (st->global_error == 0)
359ceb90fa0SAndres Lagar-Cavilla 				st->global_error = 1;
360ceb90fa0SAndres Lagar-Cavilla 		}
361d8414d3cSBastian Blank 	}
362753c09b5SJulien Grall 	st->va += XEN_PAGE_SIZE * nr;
363753c09b5SJulien Grall 	st->index += nr / XEN_PFN_PER_PAGE;
364d8414d3cSBastian Blank 
365d8414d3cSBastian Blank 	return 0;
366d8414d3cSBastian Blank }
367d8414d3cSBastian Blank 
3684e8c0c8cSDavid Vrabel static int mmap_return_error(int err, struct mmap_batch_state *st)
369d8414d3cSBastian Blank {
3704e8c0c8cSDavid Vrabel 	int ret;
371d8414d3cSBastian Blank 
37299beae6cSAndres Lagar-Cavilla 	if (st->version == 1) {
3734e8c0c8cSDavid Vrabel 		if (err) {
374a13d7201SJulien Grall 			xen_pfn_t gfn;
3754e8c0c8cSDavid Vrabel 
376a13d7201SJulien Grall 			ret = get_user(gfn, st->user_gfn);
3774e8c0c8cSDavid Vrabel 			if (ret < 0)
3784e8c0c8cSDavid Vrabel 				return ret;
3794e8c0c8cSDavid Vrabel 			/*
3804e8c0c8cSDavid Vrabel 			 * V1 encodes the error codes in the 32bit top
381a13d7201SJulien Grall 			 * nibble of the gfn (with its known
3824e8c0c8cSDavid Vrabel 			 * limitations vis-a-vis 64 bit callers).
3834e8c0c8cSDavid Vrabel 			 */
384a13d7201SJulien Grall 			gfn |= (err == -ENOENT) ?
3854e8c0c8cSDavid Vrabel 				PRIVCMD_MMAPBATCH_PAGED_ERROR :
3864e8c0c8cSDavid Vrabel 				PRIVCMD_MMAPBATCH_MFN_ERROR;
387a13d7201SJulien Grall 			return __put_user(gfn, st->user_gfn++);
3884e8c0c8cSDavid Vrabel 		} else
389a13d7201SJulien Grall 			st->user_gfn++;
39099beae6cSAndres Lagar-Cavilla 	} else { /* st->version == 2 */
39199beae6cSAndres Lagar-Cavilla 		if (err)
39299beae6cSAndres Lagar-Cavilla 			return __put_user(err, st->user_err++);
39399beae6cSAndres Lagar-Cavilla 		else
39499beae6cSAndres Lagar-Cavilla 			st->user_err++;
39599beae6cSAndres Lagar-Cavilla 	}
39699beae6cSAndres Lagar-Cavilla 
39799beae6cSAndres Lagar-Cavilla 	return 0;
398d8414d3cSBastian Blank }
399d8414d3cSBastian Blank 
4004e8c0c8cSDavid Vrabel static int mmap_return_errors(void *data, int nr, void *state)
4014e8c0c8cSDavid Vrabel {
4024e8c0c8cSDavid Vrabel 	struct mmap_batch_state *st = state;
4034e8c0c8cSDavid Vrabel 	int *errs = data;
4044e8c0c8cSDavid Vrabel 	int i;
4054e8c0c8cSDavid Vrabel 	int ret;
4064e8c0c8cSDavid Vrabel 
4074e8c0c8cSDavid Vrabel 	for (i = 0; i < nr; i++) {
4084e8c0c8cSDavid Vrabel 		ret = mmap_return_error(errs[i], st);
4094e8c0c8cSDavid Vrabel 		if (ret < 0)
4104e8c0c8cSDavid Vrabel 			return ret;
4114e8c0c8cSDavid Vrabel 	}
4124e8c0c8cSDavid Vrabel 	return 0;
4134e8c0c8cSDavid Vrabel }
4144e8c0c8cSDavid Vrabel 
415a13d7201SJulien Grall /* Allocate pfns that are then mapped with gfns from foreign domid. Update
416d71f5139SMukesh Rathor  * the vma with the page info to use later.
417d71f5139SMukesh Rathor  * Returns: 0 if success, otherwise -errno
418d71f5139SMukesh Rathor  */
419d71f5139SMukesh Rathor static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)
420d71f5139SMukesh Rathor {
421d71f5139SMukesh Rathor 	int rc;
422d71f5139SMukesh Rathor 	struct page **pages;
423d71f5139SMukesh Rathor 
424d71f5139SMukesh Rathor 	pages = kcalloc(numpgs, sizeof(pages[0]), GFP_KERNEL);
425d71f5139SMukesh Rathor 	if (pages == NULL)
426d71f5139SMukesh Rathor 		return -ENOMEM;
427d71f5139SMukesh Rathor 
42881b286e0SDavid Vrabel 	rc = alloc_xenballooned_pages(numpgs, pages);
429d71f5139SMukesh Rathor 	if (rc != 0) {
430d71f5139SMukesh Rathor 		pr_warn("%s Could not alloc %d pfns rc:%d\n", __func__,
431d71f5139SMukesh Rathor 			numpgs, rc);
432d71f5139SMukesh Rathor 		kfree(pages);
433d71f5139SMukesh Rathor 		return -ENOMEM;
434d71f5139SMukesh Rathor 	}
435a5deabe0SAndres Lagar-Cavilla 	BUG_ON(vma->vm_private_data != NULL);
436d71f5139SMukesh Rathor 	vma->vm_private_data = pages;
437d71f5139SMukesh Rathor 
438d71f5139SMukesh Rathor 	return 0;
439d71f5139SMukesh Rathor }
440d71f5139SMukesh Rathor 
4417cbea8dcSKirill A. Shutemov static const struct vm_operations_struct privcmd_vm_ops;
442d8414d3cSBastian Blank 
4434610d240SPaul Durrant static long privcmd_ioctl_mmap_batch(
4444610d240SPaul Durrant 	struct file *file, void __user *udata, int version)
445d8414d3cSBastian Blank {
4464610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
447d8414d3cSBastian Blank 	int ret;
448ceb90fa0SAndres Lagar-Cavilla 	struct privcmd_mmapbatch_v2 m;
449d8414d3cSBastian Blank 	struct mm_struct *mm = current->mm;
450d8414d3cSBastian Blank 	struct vm_area_struct *vma;
451d8414d3cSBastian Blank 	unsigned long nr_pages;
452d8414d3cSBastian Blank 	LIST_HEAD(pagelist);
453d8414d3cSBastian Blank 	struct mmap_batch_state state;
454d8414d3cSBastian Blank 
455ceb90fa0SAndres Lagar-Cavilla 	switch (version) {
456ceb90fa0SAndres Lagar-Cavilla 	case 1:
457ceb90fa0SAndres Lagar-Cavilla 		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
458d8414d3cSBastian Blank 			return -EFAULT;
459ceb90fa0SAndres Lagar-Cavilla 		/* Returns per-frame error in m.arr. */
460ceb90fa0SAndres Lagar-Cavilla 		m.err = NULL;
46196d4f267SLinus Torvalds 		if (!access_ok(m.arr, m.num * sizeof(*m.arr)))
462ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
463ceb90fa0SAndres Lagar-Cavilla 		break;
464ceb90fa0SAndres Lagar-Cavilla 	case 2:
465ceb90fa0SAndres Lagar-Cavilla 		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
466ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
467ceb90fa0SAndres Lagar-Cavilla 		/* Returns per-frame error code in m.err. */
46896d4f267SLinus Torvalds 		if (!access_ok(m.err, m.num * (sizeof(*m.err))))
469ceb90fa0SAndres Lagar-Cavilla 			return -EFAULT;
470ceb90fa0SAndres Lagar-Cavilla 		break;
471ceb90fa0SAndres Lagar-Cavilla 	default:
472ceb90fa0SAndres Lagar-Cavilla 		return -EINVAL;
473ceb90fa0SAndres Lagar-Cavilla 	}
474d8414d3cSBastian Blank 
4754610d240SPaul Durrant 	/* If restriction is in place, check the domid matches */
4764610d240SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != m.dom)
4774610d240SPaul Durrant 		return -EPERM;
4784610d240SPaul Durrant 
4795995a68aSJulien Grall 	nr_pages = DIV_ROUND_UP(m.num, XEN_PFN_PER_PAGE);
480d8414d3cSBastian Blank 	if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
481d8414d3cSBastian Blank 		return -EINVAL;
482d8414d3cSBastian Blank 
483ceb90fa0SAndres Lagar-Cavilla 	ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t), m.arr);
484d8414d3cSBastian Blank 
485ceb90fa0SAndres Lagar-Cavilla 	if (ret)
486d8414d3cSBastian Blank 		goto out;
487ceb90fa0SAndres Lagar-Cavilla 	if (list_empty(&pagelist)) {
488ceb90fa0SAndres Lagar-Cavilla 		ret = -EINVAL;
489ceb90fa0SAndres Lagar-Cavilla 		goto out;
490ceb90fa0SAndres Lagar-Cavilla 	}
491ceb90fa0SAndres Lagar-Cavilla 
49299beae6cSAndres Lagar-Cavilla 	if (version == 2) {
49399beae6cSAndres Lagar-Cavilla 		/* Zero error array now to only copy back actual errors. */
49499beae6cSAndres Lagar-Cavilla 		if (clear_user(m.err, sizeof(int) * m.num)) {
49599beae6cSAndres Lagar-Cavilla 			ret = -EFAULT;
496ceb90fa0SAndres Lagar-Cavilla 			goto out;
497ceb90fa0SAndres Lagar-Cavilla 		}
49899beae6cSAndres Lagar-Cavilla 	}
499d8414d3cSBastian Blank 
500d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
501d8414d3cSBastian Blank 
502d8414d3cSBastian Blank 	vma = find_vma(mm, m.addr);
503d8414d3cSBastian Blank 	if (!vma ||
504a5deabe0SAndres Lagar-Cavilla 	    vma->vm_ops != &privcmd_vm_ops) {
50568fa965dSMats Petersson 		ret = -EINVAL;
506a5deabe0SAndres Lagar-Cavilla 		goto out_unlock;
507a5deabe0SAndres Lagar-Cavilla 	}
508a5deabe0SAndres Lagar-Cavilla 
509a5deabe0SAndres Lagar-Cavilla 	/*
510a5deabe0SAndres Lagar-Cavilla 	 * Caller must either:
511a5deabe0SAndres Lagar-Cavilla 	 *
512a5deabe0SAndres Lagar-Cavilla 	 * Map the whole VMA range, which will also allocate all the
513a5deabe0SAndres Lagar-Cavilla 	 * pages required for the auto_translated_physmap case.
514a5deabe0SAndres Lagar-Cavilla 	 *
515a5deabe0SAndres Lagar-Cavilla 	 * Or
516a5deabe0SAndres Lagar-Cavilla 	 *
517a5deabe0SAndres Lagar-Cavilla 	 * Map unmapped holes left from a previous map attempt (e.g.,
518a5deabe0SAndres Lagar-Cavilla 	 * because those foreign frames were previously paged out).
519a5deabe0SAndres Lagar-Cavilla 	 */
520a5deabe0SAndres Lagar-Cavilla 	if (vma->vm_private_data == NULL) {
521a5deabe0SAndres Lagar-Cavilla 		if (m.addr != vma->vm_start ||
522a5deabe0SAndres Lagar-Cavilla 		    m.addr + (nr_pages << PAGE_SHIFT) != vma->vm_end) {
523a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
524a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
525d8414d3cSBastian Blank 		}
526d71f5139SMukesh Rathor 		if (xen_feature(XENFEAT_auto_translated_physmap)) {
5275995a68aSJulien Grall 			ret = alloc_empty_pages(vma, nr_pages);
528a5deabe0SAndres Lagar-Cavilla 			if (ret < 0)
529a5deabe0SAndres Lagar-Cavilla 				goto out_unlock;
530a5deabe0SAndres Lagar-Cavilla 		} else
531a5deabe0SAndres Lagar-Cavilla 			vma->vm_private_data = PRIV_VMA_LOCKED;
532a5deabe0SAndres Lagar-Cavilla 	} else {
533a5deabe0SAndres Lagar-Cavilla 		if (m.addr < vma->vm_start ||
534a5deabe0SAndres Lagar-Cavilla 		    m.addr + (nr_pages << PAGE_SHIFT) > vma->vm_end) {
535a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
536a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
537a5deabe0SAndres Lagar-Cavilla 		}
538a5deabe0SAndres Lagar-Cavilla 		if (privcmd_vma_range_is_mapped(vma, m.addr, nr_pages)) {
539a5deabe0SAndres Lagar-Cavilla 			ret = -EINVAL;
540a5deabe0SAndres Lagar-Cavilla 			goto out_unlock;
541d71f5139SMukesh Rathor 		}
542d71f5139SMukesh Rathor 	}
543d8414d3cSBastian Blank 
544d8414d3cSBastian Blank 	state.domain        = m.dom;
545d8414d3cSBastian Blank 	state.vma           = vma;
546d8414d3cSBastian Blank 	state.va            = m.addr;
547d71f5139SMukesh Rathor 	state.index         = 0;
548ceb90fa0SAndres Lagar-Cavilla 	state.global_error  = 0;
54999beae6cSAndres Lagar-Cavilla 	state.version       = version;
550d8414d3cSBastian Blank 
5515995a68aSJulien Grall 	BUILD_BUG_ON(((PAGE_SIZE / sizeof(xen_pfn_t)) % XEN_PFN_PER_PAGE) != 0);
552ceb90fa0SAndres Lagar-Cavilla 	/* mmap_batch_fn guarantees ret == 0 */
5534e8c0c8cSDavid Vrabel 	BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
554ceb90fa0SAndres Lagar-Cavilla 				    &pagelist, mmap_batch_fn, &state));
555d8414d3cSBastian Blank 
556d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
557d8414d3cSBastian Blank 
55868fa965dSMats Petersson 	if (state.global_error) {
559ceb90fa0SAndres Lagar-Cavilla 		/* Write back errors in second pass. */
560a13d7201SJulien Grall 		state.user_gfn = (xen_pfn_t *)m.arr;
56199beae6cSAndres Lagar-Cavilla 		state.user_err = m.err;
5624e8c0c8cSDavid Vrabel 		ret = traverse_pages_block(m.num, sizeof(xen_pfn_t),
56399beae6cSAndres Lagar-Cavilla 					   &pagelist, mmap_return_errors, &state);
56468fa965dSMats Petersson 	} else
56568fa965dSMats Petersson 		ret = 0;
56668fa965dSMats Petersson 
567ceb90fa0SAndres Lagar-Cavilla 	/* If we have not had any EFAULT-like global errors then set the global
568ceb90fa0SAndres Lagar-Cavilla 	 * error to -ENOENT if necessary. */
569ceb90fa0SAndres Lagar-Cavilla 	if ((ret == 0) && (state.global_error == -ENOENT))
570ceb90fa0SAndres Lagar-Cavilla 		ret = -ENOENT;
571d8414d3cSBastian Blank 
572d8414d3cSBastian Blank out:
573d8414d3cSBastian Blank 	free_page_list(&pagelist);
574d8414d3cSBastian Blank 	return ret;
575a5deabe0SAndres Lagar-Cavilla 
576a5deabe0SAndres Lagar-Cavilla out_unlock:
577d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
578a5deabe0SAndres Lagar-Cavilla 	goto out;
579d8414d3cSBastian Blank }
580d8414d3cSBastian Blank 
581ab520be8SPaul Durrant static int lock_pages(
582ab520be8SPaul Durrant 	struct privcmd_dm_op_buf kbufs[], unsigned int num,
583e398fb4bSSouptick Joarder 	struct page *pages[], unsigned int nr_pages, unsigned int *pinned)
584ab520be8SPaul Durrant {
585ab520be8SPaul Durrant 	unsigned int i;
586ab520be8SPaul Durrant 
587ab520be8SPaul Durrant 	for (i = 0; i < num; i++) {
588ab520be8SPaul Durrant 		unsigned int requested;
589e398fb4bSSouptick Joarder 		int page_count;
590ab520be8SPaul Durrant 
591ab520be8SPaul Durrant 		requested = DIV_ROUND_UP(
592ab520be8SPaul Durrant 			offset_in_page(kbufs[i].uptr) + kbufs[i].size,
593ab520be8SPaul Durrant 			PAGE_SIZE);
594ab520be8SPaul Durrant 		if (requested > nr_pages)
595ab520be8SPaul Durrant 			return -ENOSPC;
596ab520be8SPaul Durrant 
597*ff669aa8SSouptick Joarder 		page_count = pin_user_pages_fast(
598ab520be8SPaul Durrant 			(unsigned long) kbufs[i].uptr,
599ab520be8SPaul Durrant 			requested, FOLL_WRITE, pages);
600e398fb4bSSouptick Joarder 		if (page_count < 0)
601e398fb4bSSouptick Joarder 			return page_count;
602ab520be8SPaul Durrant 
603e398fb4bSSouptick Joarder 		*pinned += page_count;
604e398fb4bSSouptick Joarder 		nr_pages -= page_count;
605e398fb4bSSouptick Joarder 		pages += page_count;
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 {
613*ff669aa8SSouptick Joarder 	unpin_user_pages_dirty_lock(pages, nr_pages, true);
614a0c34d22SSouptick Joarder }
615ab520be8SPaul Durrant 
6164610d240SPaul Durrant static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
617ab520be8SPaul Durrant {
6184610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
619ab520be8SPaul Durrant 	struct privcmd_dm_op kdata;
620ab520be8SPaul Durrant 	struct privcmd_dm_op_buf *kbufs;
621ab520be8SPaul Durrant 	unsigned int nr_pages = 0;
622ab520be8SPaul Durrant 	struct page **pages = NULL;
623ab520be8SPaul Durrant 	struct xen_dm_op_buf *xbufs = NULL;
624ab520be8SPaul Durrant 	unsigned int i;
625ab520be8SPaul Durrant 	long rc;
626e398fb4bSSouptick Joarder 	unsigned int pinned = 0;
627ab520be8SPaul Durrant 
628ab520be8SPaul Durrant 	if (copy_from_user(&kdata, udata, sizeof(kdata)))
629ab520be8SPaul Durrant 		return -EFAULT;
630ab520be8SPaul Durrant 
6314610d240SPaul Durrant 	/* If restriction is in place, check the domid matches */
6324610d240SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
6334610d240SPaul Durrant 		return -EPERM;
6344610d240SPaul Durrant 
635ab520be8SPaul Durrant 	if (kdata.num == 0)
636ab520be8SPaul Durrant 		return 0;
637ab520be8SPaul Durrant 
638ab520be8SPaul Durrant 	if (kdata.num > privcmd_dm_op_max_num)
639ab520be8SPaul Durrant 		return -E2BIG;
640ab520be8SPaul Durrant 
641ab520be8SPaul Durrant 	kbufs = kcalloc(kdata.num, sizeof(*kbufs), GFP_KERNEL);
642ab520be8SPaul Durrant 	if (!kbufs)
643ab520be8SPaul Durrant 		return -ENOMEM;
644ab520be8SPaul Durrant 
645ab520be8SPaul Durrant 	if (copy_from_user(kbufs, kdata.ubufs,
646ab520be8SPaul Durrant 			   sizeof(*kbufs) * kdata.num)) {
647ab520be8SPaul Durrant 		rc = -EFAULT;
648ab520be8SPaul Durrant 		goto out;
649ab520be8SPaul Durrant 	}
650ab520be8SPaul Durrant 
651ab520be8SPaul Durrant 	for (i = 0; i < kdata.num; i++) {
652ab520be8SPaul Durrant 		if (kbufs[i].size > privcmd_dm_op_buf_max_size) {
653ab520be8SPaul Durrant 			rc = -E2BIG;
654ab520be8SPaul Durrant 			goto out;
655ab520be8SPaul Durrant 		}
656ab520be8SPaul Durrant 
65796d4f267SLinus Torvalds 		if (!access_ok(kbufs[i].uptr,
658ab520be8SPaul Durrant 			       kbufs[i].size)) {
659ab520be8SPaul Durrant 			rc = -EFAULT;
660ab520be8SPaul Durrant 			goto out;
661ab520be8SPaul Durrant 		}
662ab520be8SPaul Durrant 
663ab520be8SPaul Durrant 		nr_pages += DIV_ROUND_UP(
664ab520be8SPaul Durrant 			offset_in_page(kbufs[i].uptr) + kbufs[i].size,
665ab520be8SPaul Durrant 			PAGE_SIZE);
666ab520be8SPaul Durrant 	}
667ab520be8SPaul Durrant 
668ab520be8SPaul Durrant 	pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
669ab520be8SPaul Durrant 	if (!pages) {
670ab520be8SPaul Durrant 		rc = -ENOMEM;
671ab520be8SPaul Durrant 		goto out;
672ab520be8SPaul Durrant 	}
673ab520be8SPaul Durrant 
674ab520be8SPaul Durrant 	xbufs = kcalloc(kdata.num, sizeof(*xbufs), GFP_KERNEL);
675ab520be8SPaul Durrant 	if (!xbufs) {
676ab520be8SPaul Durrant 		rc = -ENOMEM;
677ab520be8SPaul Durrant 		goto out;
678ab520be8SPaul Durrant 	}
679ab520be8SPaul Durrant 
680e398fb4bSSouptick Joarder 	rc = lock_pages(kbufs, kdata.num, pages, nr_pages, &pinned);
681e398fb4bSSouptick Joarder 	if (rc < 0) {
682e398fb4bSSouptick Joarder 		nr_pages = pinned;
683ab520be8SPaul Durrant 		goto out;
684e398fb4bSSouptick Joarder 	}
685ab520be8SPaul Durrant 
686ab520be8SPaul Durrant 	for (i = 0; i < kdata.num; i++) {
687ab520be8SPaul Durrant 		set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr);
688ab520be8SPaul Durrant 		xbufs[i].size = kbufs[i].size;
689ab520be8SPaul Durrant 	}
690ab520be8SPaul Durrant 
691ab520be8SPaul Durrant 	xen_preemptible_hcall_begin();
692ab520be8SPaul Durrant 	rc = HYPERVISOR_dm_op(kdata.dom, kdata.num, xbufs);
693ab520be8SPaul Durrant 	xen_preemptible_hcall_end();
694ab520be8SPaul Durrant 
695ab520be8SPaul Durrant out:
696ab520be8SPaul Durrant 	unlock_pages(pages, nr_pages);
697ab520be8SPaul Durrant 	kfree(xbufs);
698ab520be8SPaul Durrant 	kfree(pages);
699ab520be8SPaul Durrant 	kfree(kbufs);
700ab520be8SPaul Durrant 
701ab520be8SPaul Durrant 	return rc;
702ab520be8SPaul Durrant }
703ab520be8SPaul Durrant 
7044610d240SPaul Durrant static long privcmd_ioctl_restrict(struct file *file, void __user *udata)
7054610d240SPaul Durrant {
7064610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
7074610d240SPaul Durrant 	domid_t dom;
7084610d240SPaul Durrant 
7094610d240SPaul Durrant 	if (copy_from_user(&dom, udata, sizeof(dom)))
7104610d240SPaul Durrant 		return -EFAULT;
7114610d240SPaul Durrant 
7124610d240SPaul Durrant 	/* Set restriction to the specified domain, or check it matches */
7134610d240SPaul Durrant 	if (data->domid == DOMID_INVALID)
7144610d240SPaul Durrant 		data->domid = dom;
7154610d240SPaul Durrant 	else if (data->domid != dom)
7164610d240SPaul Durrant 		return -EINVAL;
7174610d240SPaul Durrant 
7184610d240SPaul Durrant 	return 0;
7194610d240SPaul Durrant }
7204610d240SPaul Durrant 
7213ad08765SPaul Durrant static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
7223ad08765SPaul Durrant {
7233ad08765SPaul Durrant 	struct privcmd_data *data = file->private_data;
7243ad08765SPaul Durrant 	struct mm_struct *mm = current->mm;
7253ad08765SPaul Durrant 	struct vm_area_struct *vma;
7263ad08765SPaul Durrant 	struct privcmd_mmap_resource kdata;
7273ad08765SPaul Durrant 	xen_pfn_t *pfns = NULL;
7283ad08765SPaul Durrant 	struct xen_mem_acquire_resource xdata;
7293ad08765SPaul Durrant 	int rc;
7303ad08765SPaul Durrant 
7313ad08765SPaul Durrant 	if (copy_from_user(&kdata, udata, sizeof(kdata)))
7323ad08765SPaul Durrant 		return -EFAULT;
7333ad08765SPaul Durrant 
7343ad08765SPaul Durrant 	/* If restriction is in place, check the domid matches */
7353ad08765SPaul Durrant 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
7363ad08765SPaul Durrant 		return -EPERM;
7373ad08765SPaul Durrant 
738d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
7393ad08765SPaul Durrant 
7403ad08765SPaul Durrant 	vma = find_vma(mm, kdata.addr);
7413ad08765SPaul Durrant 	if (!vma || vma->vm_ops != &privcmd_vm_ops) {
7423ad08765SPaul Durrant 		rc = -EINVAL;
7433ad08765SPaul Durrant 		goto out;
7443ad08765SPaul Durrant 	}
7453ad08765SPaul Durrant 
7463ad08765SPaul Durrant 	pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL);
7473ad08765SPaul Durrant 	if (!pfns) {
7483ad08765SPaul Durrant 		rc = -ENOMEM;
7493ad08765SPaul Durrant 		goto out;
7503ad08765SPaul Durrant 	}
7513ad08765SPaul Durrant 
752a78d14a3SArnd Bergmann 	if (IS_ENABLED(CONFIG_XEN_AUTO_XLATE) &&
753a78d14a3SArnd Bergmann 	    xen_feature(XENFEAT_auto_translated_physmap)) {
7543ad08765SPaul Durrant 		unsigned int nr = DIV_ROUND_UP(kdata.num, XEN_PFN_PER_PAGE);
7553ad08765SPaul Durrant 		struct page **pages;
7563ad08765SPaul Durrant 		unsigned int i;
7573ad08765SPaul Durrant 
7583ad08765SPaul Durrant 		rc = alloc_empty_pages(vma, nr);
7593ad08765SPaul Durrant 		if (rc < 0)
7603ad08765SPaul Durrant 			goto out;
7613ad08765SPaul Durrant 
7623ad08765SPaul Durrant 		pages = vma->vm_private_data;
7633ad08765SPaul Durrant 		for (i = 0; i < kdata.num; i++) {
7643ad08765SPaul Durrant 			xen_pfn_t pfn =
7653ad08765SPaul Durrant 				page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
7663ad08765SPaul Durrant 
7673ad08765SPaul Durrant 			pfns[i] = pfn + (i % XEN_PFN_PER_PAGE);
7683ad08765SPaul Durrant 		}
7693ad08765SPaul Durrant 	} else
7703ad08765SPaul Durrant 		vma->vm_private_data = PRIV_VMA_LOCKED;
7713ad08765SPaul Durrant 
7723ad08765SPaul Durrant 	memset(&xdata, 0, sizeof(xdata));
7733ad08765SPaul Durrant 	xdata.domid = kdata.dom;
7743ad08765SPaul Durrant 	xdata.type = kdata.type;
7753ad08765SPaul Durrant 	xdata.id = kdata.id;
7763ad08765SPaul Durrant 	xdata.frame = kdata.idx;
7773ad08765SPaul Durrant 	xdata.nr_frames = kdata.num;
7783ad08765SPaul Durrant 	set_xen_guest_handle(xdata.frame_list, pfns);
7793ad08765SPaul Durrant 
7803ad08765SPaul Durrant 	xen_preemptible_hcall_begin();
7813ad08765SPaul Durrant 	rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata);
7823ad08765SPaul Durrant 	xen_preemptible_hcall_end();
7833ad08765SPaul Durrant 
7843ad08765SPaul Durrant 	if (rc)
7853ad08765SPaul Durrant 		goto out;
7863ad08765SPaul Durrant 
787a78d14a3SArnd Bergmann 	if (IS_ENABLED(CONFIG_XEN_AUTO_XLATE) &&
788a78d14a3SArnd Bergmann 	    xen_feature(XENFEAT_auto_translated_physmap)) {
789a78d14a3SArnd Bergmann 		rc = xen_remap_vma_range(vma, kdata.addr, kdata.num << PAGE_SHIFT);
7903ad08765SPaul Durrant 	} else {
7913ad08765SPaul Durrant 		unsigned int domid =
7923ad08765SPaul Durrant 			(xdata.flags & XENMEM_rsrc_acq_caller_owned) ?
7933ad08765SPaul Durrant 			DOMID_SELF : kdata.dom;
7943ad08765SPaul Durrant 		int num;
7953ad08765SPaul Durrant 
7963ad08765SPaul Durrant 		num = xen_remap_domain_mfn_array(vma,
7973ad08765SPaul Durrant 						 kdata.addr & PAGE_MASK,
7983ad08765SPaul Durrant 						 pfns, kdata.num, (int *)pfns,
7993ad08765SPaul Durrant 						 vma->vm_page_prot,
8003ad08765SPaul Durrant 						 domid,
8013ad08765SPaul Durrant 						 vma->vm_private_data);
8023ad08765SPaul Durrant 		if (num < 0)
8033ad08765SPaul Durrant 			rc = num;
8043ad08765SPaul Durrant 		else if (num != kdata.num) {
8053ad08765SPaul Durrant 			unsigned int i;
8063ad08765SPaul Durrant 
8073ad08765SPaul Durrant 			for (i = 0; i < num; i++) {
8083ad08765SPaul Durrant 				rc = pfns[i];
8093ad08765SPaul Durrant 				if (rc < 0)
8103ad08765SPaul Durrant 					break;
8113ad08765SPaul Durrant 			}
8123ad08765SPaul Durrant 		} else
8133ad08765SPaul Durrant 			rc = 0;
8143ad08765SPaul Durrant 	}
8153ad08765SPaul Durrant 
8163ad08765SPaul Durrant out:
817d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
8183ad08765SPaul Durrant 	kfree(pfns);
8193ad08765SPaul Durrant 
8203ad08765SPaul Durrant 	return rc;
8213ad08765SPaul Durrant }
8223ad08765SPaul Durrant 
823d8414d3cSBastian Blank static long privcmd_ioctl(struct file *file,
824d8414d3cSBastian Blank 			  unsigned int cmd, unsigned long data)
825d8414d3cSBastian Blank {
826dc9eab6fSPaul Durrant 	int ret = -ENOTTY;
827d8414d3cSBastian Blank 	void __user *udata = (void __user *) data;
828d8414d3cSBastian Blank 
829d8414d3cSBastian Blank 	switch (cmd) {
830d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_HYPERCALL:
8314610d240SPaul Durrant 		ret = privcmd_ioctl_hypercall(file, udata);
832d8414d3cSBastian Blank 		break;
833d8414d3cSBastian Blank 
834d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_MMAP:
8354610d240SPaul Durrant 		ret = privcmd_ioctl_mmap(file, udata);
836d8414d3cSBastian Blank 		break;
837d8414d3cSBastian Blank 
838d8414d3cSBastian Blank 	case IOCTL_PRIVCMD_MMAPBATCH:
8394610d240SPaul Durrant 		ret = privcmd_ioctl_mmap_batch(file, udata, 1);
840ceb90fa0SAndres Lagar-Cavilla 		break;
841ceb90fa0SAndres Lagar-Cavilla 
842ceb90fa0SAndres Lagar-Cavilla 	case IOCTL_PRIVCMD_MMAPBATCH_V2:
8434610d240SPaul Durrant 		ret = privcmd_ioctl_mmap_batch(file, udata, 2);
844d8414d3cSBastian Blank 		break;
845d8414d3cSBastian Blank 
846ab520be8SPaul Durrant 	case IOCTL_PRIVCMD_DM_OP:
8474610d240SPaul Durrant 		ret = privcmd_ioctl_dm_op(file, udata);
8484610d240SPaul Durrant 		break;
8494610d240SPaul Durrant 
8504610d240SPaul Durrant 	case IOCTL_PRIVCMD_RESTRICT:
8514610d240SPaul Durrant 		ret = privcmd_ioctl_restrict(file, udata);
852ab520be8SPaul Durrant 		break;
853ab520be8SPaul Durrant 
8543ad08765SPaul Durrant 	case IOCTL_PRIVCMD_MMAP_RESOURCE:
8553ad08765SPaul Durrant 		ret = privcmd_ioctl_mmap_resource(file, udata);
8563ad08765SPaul Durrant 		break;
8573ad08765SPaul Durrant 
858d8414d3cSBastian Blank 	default:
859d8414d3cSBastian Blank 		break;
860d8414d3cSBastian Blank 	}
861d8414d3cSBastian Blank 
862d8414d3cSBastian Blank 	return ret;
863d8414d3cSBastian Blank }
864d8414d3cSBastian Blank 
8654610d240SPaul Durrant static int privcmd_open(struct inode *ino, struct file *file)
8664610d240SPaul Durrant {
8674610d240SPaul Durrant 	struct privcmd_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
8684610d240SPaul Durrant 
8694610d240SPaul Durrant 	if (!data)
8704610d240SPaul Durrant 		return -ENOMEM;
8714610d240SPaul Durrant 
8724610d240SPaul Durrant 	/* DOMID_INVALID implies no restriction */
8734610d240SPaul Durrant 	data->domid = DOMID_INVALID;
8744610d240SPaul Durrant 
8754610d240SPaul Durrant 	file->private_data = data;
8764610d240SPaul Durrant 	return 0;
8774610d240SPaul Durrant }
8784610d240SPaul Durrant 
8794610d240SPaul Durrant static int privcmd_release(struct inode *ino, struct file *file)
8804610d240SPaul Durrant {
8814610d240SPaul Durrant 	struct privcmd_data *data = file->private_data;
8824610d240SPaul Durrant 
8834610d240SPaul Durrant 	kfree(data);
8844610d240SPaul Durrant 	return 0;
8854610d240SPaul Durrant }
8864610d240SPaul Durrant 
887d71f5139SMukesh Rathor static void privcmd_close(struct vm_area_struct *vma)
888d71f5139SMukesh Rathor {
889d71f5139SMukesh Rathor 	struct page **pages = vma->vm_private_data;
890c7ebf9d9SMuhammad Falak R Wani 	int numpgs = vma_pages(vma);
8915995a68aSJulien Grall 	int numgfns = (vma->vm_end - vma->vm_start) >> XEN_PAGE_SHIFT;
892b6497b38SIan Campbell 	int rc;
893d71f5139SMukesh Rathor 
8949eff37a8SDan Carpenter 	if (!xen_feature(XENFEAT_auto_translated_physmap) || !numpgs || !pages)
895d71f5139SMukesh Rathor 		return;
896d71f5139SMukesh Rathor 
8975995a68aSJulien Grall 	rc = xen_unmap_domain_gfn_range(vma, numgfns, pages);
898b6497b38SIan Campbell 	if (rc == 0)
899d71f5139SMukesh Rathor 		free_xenballooned_pages(numpgs, pages);
900b6497b38SIan Campbell 	else
901b6497b38SIan Campbell 		pr_crit("unable to unmap MFN range: leaking %d pages. rc=%d\n",
902b6497b38SIan Campbell 			numpgs, rc);
903d71f5139SMukesh Rathor 	kfree(pages);
904d71f5139SMukesh Rathor }
905d71f5139SMukesh Rathor 
9064bf2cc96SSouptick Joarder static vm_fault_t privcmd_fault(struct vm_fault *vmf)
907d8414d3cSBastian Blank {
908d8414d3cSBastian Blank 	printk(KERN_DEBUG "privcmd_fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
90911bac800SDave Jiang 	       vmf->vma, vmf->vma->vm_start, vmf->vma->vm_end,
9101a29d85eSJan Kara 	       vmf->pgoff, (void *)vmf->address);
911d8414d3cSBastian Blank 
912d8414d3cSBastian Blank 	return VM_FAULT_SIGBUS;
913d8414d3cSBastian Blank }
914d8414d3cSBastian Blank 
9157cbea8dcSKirill A. Shutemov static const struct vm_operations_struct privcmd_vm_ops = {
916d71f5139SMukesh Rathor 	.close = privcmd_close,
917d8414d3cSBastian Blank 	.fault = privcmd_fault
918d8414d3cSBastian Blank };
919d8414d3cSBastian Blank 
920d8414d3cSBastian Blank static int privcmd_mmap(struct file *file, struct vm_area_struct *vma)
921d8414d3cSBastian Blank {
922d8414d3cSBastian Blank 	/* DONTCOPY is essential for Xen because copy_page_range doesn't know
923d8414d3cSBastian Blank 	 * how to recreate these mappings */
924314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTCOPY |
925314e51b9SKonstantin Khlebnikov 			 VM_DONTEXPAND | VM_DONTDUMP;
926d8414d3cSBastian Blank 	vma->vm_ops = &privcmd_vm_ops;
927d8414d3cSBastian Blank 	vma->vm_private_data = NULL;
928d8414d3cSBastian Blank 
929d8414d3cSBastian Blank 	return 0;
930d8414d3cSBastian Blank }
931d8414d3cSBastian Blank 
932a5deabe0SAndres Lagar-Cavilla /*
933a5deabe0SAndres Lagar-Cavilla  * For MMAPBATCH*. This allows asserting the singleshot mapping
934a5deabe0SAndres Lagar-Cavilla  * on a per pfn/pte basis. Mapping calls that fail with ENOENT
935a5deabe0SAndres Lagar-Cavilla  * can be then retried until success.
936a5deabe0SAndres Lagar-Cavilla  */
9378b1e0f81SAnshuman Khandual static int is_mapped_fn(pte_t *pte, unsigned long addr, void *data)
938d8414d3cSBastian Blank {
939a5deabe0SAndres Lagar-Cavilla 	return pte_none(*pte) ? 0 : -EBUSY;
940a5deabe0SAndres Lagar-Cavilla }
941a5deabe0SAndres Lagar-Cavilla 
942a5deabe0SAndres Lagar-Cavilla static int privcmd_vma_range_is_mapped(
943a5deabe0SAndres Lagar-Cavilla 	           struct vm_area_struct *vma,
944a5deabe0SAndres Lagar-Cavilla 	           unsigned long addr,
945a5deabe0SAndres Lagar-Cavilla 	           unsigned long nr_pages)
946a5deabe0SAndres Lagar-Cavilla {
947a5deabe0SAndres Lagar-Cavilla 	return apply_to_page_range(vma->vm_mm, addr, nr_pages << PAGE_SHIFT,
948a5deabe0SAndres Lagar-Cavilla 				   is_mapped_fn, NULL) != 0;
949d8414d3cSBastian Blank }
950d8414d3cSBastian Blank 
951d8414d3cSBastian Blank const struct file_operations xen_privcmd_fops = {
952d8414d3cSBastian Blank 	.owner = THIS_MODULE,
953d8414d3cSBastian Blank 	.unlocked_ioctl = privcmd_ioctl,
9544610d240SPaul Durrant 	.open = privcmd_open,
9554610d240SPaul Durrant 	.release = privcmd_release,
956d8414d3cSBastian Blank 	.mmap = privcmd_mmap,
957d8414d3cSBastian Blank };
958d8414d3cSBastian Blank EXPORT_SYMBOL_GPL(xen_privcmd_fops);
959d8414d3cSBastian Blank 
960d8414d3cSBastian Blank static struct miscdevice privcmd_dev = {
961d8414d3cSBastian Blank 	.minor = MISC_DYNAMIC_MINOR,
962d8414d3cSBastian Blank 	.name = "xen/privcmd",
963d8414d3cSBastian Blank 	.fops = &xen_privcmd_fops,
964d8414d3cSBastian Blank };
965d8414d3cSBastian Blank 
966d8414d3cSBastian Blank static int __init privcmd_init(void)
967d8414d3cSBastian Blank {
968d8414d3cSBastian Blank 	int err;
969d8414d3cSBastian Blank 
970d8414d3cSBastian Blank 	if (!xen_domain())
971d8414d3cSBastian Blank 		return -ENODEV;
972d8414d3cSBastian Blank 
973d8414d3cSBastian Blank 	err = misc_register(&privcmd_dev);
974d8414d3cSBastian Blank 	if (err != 0) {
975283c0972SJoe Perches 		pr_err("Could not register Xen privcmd device\n");
976d8414d3cSBastian Blank 		return err;
977d8414d3cSBastian Blank 	}
978c51b3c63SJuergen Gross 
979c51b3c63SJuergen Gross 	err = misc_register(&xen_privcmdbuf_dev);
980c51b3c63SJuergen Gross 	if (err != 0) {
981c51b3c63SJuergen Gross 		pr_err("Could not register Xen hypercall-buf device\n");
982c51b3c63SJuergen Gross 		misc_deregister(&privcmd_dev);
983c51b3c63SJuergen Gross 		return err;
984c51b3c63SJuergen Gross 	}
985c51b3c63SJuergen Gross 
986d8414d3cSBastian Blank 	return 0;
987d8414d3cSBastian Blank }
988d8414d3cSBastian Blank 
989d8414d3cSBastian Blank static void __exit privcmd_exit(void)
990d8414d3cSBastian Blank {
991d8414d3cSBastian Blank 	misc_deregister(&privcmd_dev);
992c51b3c63SJuergen Gross 	misc_deregister(&xen_privcmdbuf_dev);
993d8414d3cSBastian Blank }
994d8414d3cSBastian Blank 
995d8414d3cSBastian Blank module_init(privcmd_init);
996d8414d3cSBastian Blank module_exit(privcmd_exit);
997