xref: /freebsd/sys/dev/xen/privcmd/privcmd.c (revision ed78016d005c9ec97883a33c4468052ca9880c4f)
1bf7313e3SRoger Pau Monné /*
2bf7313e3SRoger Pau Monné  * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
3bf7313e3SRoger Pau Monné  * All rights reserved.
4bf7313e3SRoger Pau Monné  *
5bf7313e3SRoger Pau Monné  * Redistribution and use in source and binary forms, with or without
6bf7313e3SRoger Pau Monné  * modification, are permitted provided that the following conditions
7bf7313e3SRoger Pau Monné  * are met:
8bf7313e3SRoger Pau Monné  * 1. Redistributions of source code must retain the above copyright
9bf7313e3SRoger Pau Monné  *    notice, this list of conditions and the following disclaimer.
10bf7313e3SRoger Pau Monné  * 2. Redistributions in binary form must reproduce the above copyright
11bf7313e3SRoger Pau Monné  *    notice, this list of conditions and the following disclaimer in the
12bf7313e3SRoger Pau Monné  *    documentation and/or other materials provided with the distribution.
13bf7313e3SRoger Pau Monné  *
14bf7313e3SRoger Pau Monné  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15bf7313e3SRoger Pau Monné  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16bf7313e3SRoger Pau Monné  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17bf7313e3SRoger Pau Monné  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18bf7313e3SRoger Pau Monné  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19bf7313e3SRoger Pau Monné  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20bf7313e3SRoger Pau Monné  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21bf7313e3SRoger Pau Monné  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22bf7313e3SRoger Pau Monné  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23bf7313e3SRoger Pau Monné  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24bf7313e3SRoger Pau Monné  * SUCH DAMAGE.
25bf7313e3SRoger Pau Monné  */
26bf7313e3SRoger Pau Monné 
27bf7313e3SRoger Pau Monné #include <sys/cdefs.h>
28bf7313e3SRoger Pau Monné __FBSDID("$FreeBSD$");
29bf7313e3SRoger Pau Monné 
30bf7313e3SRoger Pau Monné #include <sys/param.h>
31bf7313e3SRoger Pau Monné #include <sys/systm.h>
32bf7313e3SRoger Pau Monné #include <sys/uio.h>
33bf7313e3SRoger Pau Monné #include <sys/bus.h>
34bf7313e3SRoger Pau Monné #include <sys/malloc.h>
35bf7313e3SRoger Pau Monné #include <sys/kernel.h>
36bf7313e3SRoger Pau Monné #include <sys/lock.h>
37bf7313e3SRoger Pau Monné #include <sys/mutex.h>
38bf7313e3SRoger Pau Monné #include <sys/rwlock.h>
39bf7313e3SRoger Pau Monné #include <sys/selinfo.h>
40bf7313e3SRoger Pau Monné #include <sys/poll.h>
41bf7313e3SRoger Pau Monné #include <sys/conf.h>
42bf7313e3SRoger Pau Monné #include <sys/fcntl.h>
43bf7313e3SRoger Pau Monné #include <sys/ioccom.h>
44bf7313e3SRoger Pau Monné #include <sys/rman.h>
45bf7313e3SRoger Pau Monné #include <sys/tree.h>
46bf7313e3SRoger Pau Monné #include <sys/module.h>
47bf7313e3SRoger Pau Monné #include <sys/proc.h>
48288b2385SRoger Pau Monné #include <sys/bitset.h>
49bf7313e3SRoger Pau Monné 
50bf7313e3SRoger Pau Monné #include <vm/vm.h>
51bf7313e3SRoger Pau Monné #include <vm/vm_param.h>
52bf7313e3SRoger Pau Monné #include <vm/vm_extern.h>
53bf7313e3SRoger Pau Monné #include <vm/vm_kern.h>
54bf7313e3SRoger Pau Monné #include <vm/vm_page.h>
55bf7313e3SRoger Pau Monné #include <vm/vm_map.h>
56bf7313e3SRoger Pau Monné #include <vm/vm_object.h>
57bf7313e3SRoger Pau Monné #include <vm/vm_pager.h>
58bf7313e3SRoger Pau Monné 
59bf7313e3SRoger Pau Monné #include <machine/md_var.h>
60bf7313e3SRoger Pau Monné 
61bf7313e3SRoger Pau Monné #include <xen/xen-os.h>
62bf7313e3SRoger Pau Monné #include <xen/hypervisor.h>
63bf7313e3SRoger Pau Monné #include <xen/privcmd.h>
64bf7313e3SRoger Pau Monné #include <xen/error.h>
65bf7313e3SRoger Pau Monné 
66bf7313e3SRoger Pau Monné MALLOC_DEFINE(M_PRIVCMD, "privcmd_dev", "Xen privcmd user-space device");
67bf7313e3SRoger Pau Monné 
68*ed78016dSRoger Pau Monne #define MAX_DMOP_BUFFERS 16
69*ed78016dSRoger Pau Monne 
70bf7313e3SRoger Pau Monné struct privcmd_map {
71bf7313e3SRoger Pau Monné 	vm_object_t mem;
72bf7313e3SRoger Pau Monné 	vm_size_t size;
73bf7313e3SRoger Pau Monné 	struct resource *pseudo_phys_res;
74bf7313e3SRoger Pau Monné 	int pseudo_phys_res_id;
75bf7313e3SRoger Pau Monné 	vm_paddr_t phys_base_addr;
76bf7313e3SRoger Pau Monné 	boolean_t mapped;
77288b2385SRoger Pau Monné 	BITSET_DEFINE_VAR() *err;
78bf7313e3SRoger Pau Monné };
79bf7313e3SRoger Pau Monné 
80bf7313e3SRoger Pau Monné static d_ioctl_t     privcmd_ioctl;
81bf7313e3SRoger Pau Monné static d_mmap_single_t	privcmd_mmap_single;
82bf7313e3SRoger Pau Monné 
83bf7313e3SRoger Pau Monné static struct cdevsw privcmd_devsw = {
84bf7313e3SRoger Pau Monné 	.d_version = D_VERSION,
85bf7313e3SRoger Pau Monné 	.d_ioctl = privcmd_ioctl,
86bf7313e3SRoger Pau Monné 	.d_mmap_single = privcmd_mmap_single,
87bf7313e3SRoger Pau Monné 	.d_name = "privcmd",
88bf7313e3SRoger Pau Monné };
89bf7313e3SRoger Pau Monné 
90bf7313e3SRoger Pau Monné static int privcmd_pg_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
91bf7313e3SRoger Pau Monné     vm_ooffset_t foff, struct ucred *cred, u_short *color);
92bf7313e3SRoger Pau Monné static void privcmd_pg_dtor(void *handle);
93bf7313e3SRoger Pau Monné static int privcmd_pg_fault(vm_object_t object, vm_ooffset_t offset,
94bf7313e3SRoger Pau Monné     int prot, vm_page_t *mres);
95bf7313e3SRoger Pau Monné 
96bf7313e3SRoger Pau Monné static struct cdev_pager_ops privcmd_pg_ops = {
97bf7313e3SRoger Pau Monné 	.cdev_pg_fault = privcmd_pg_fault,
98bf7313e3SRoger Pau Monné 	.cdev_pg_ctor =	privcmd_pg_ctor,
99bf7313e3SRoger Pau Monné 	.cdev_pg_dtor =	privcmd_pg_dtor,
100bf7313e3SRoger Pau Monné };
101bf7313e3SRoger Pau Monné 
102bf7313e3SRoger Pau Monné static device_t privcmd_dev = NULL;
103bf7313e3SRoger Pau Monné 
104bf7313e3SRoger Pau Monné /*------------------------- Privcmd Pager functions --------------------------*/
105bf7313e3SRoger Pau Monné static int
106bf7313e3SRoger Pau Monné privcmd_pg_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
107bf7313e3SRoger Pau Monné     vm_ooffset_t foff, struct ucred *cred, u_short *color)
108bf7313e3SRoger Pau Monné {
109bf7313e3SRoger Pau Monné 
110bf7313e3SRoger Pau Monné 	return (0);
111bf7313e3SRoger Pau Monné }
112bf7313e3SRoger Pau Monné 
113bf7313e3SRoger Pau Monné static void
114bf7313e3SRoger Pau Monné privcmd_pg_dtor(void *handle)
115bf7313e3SRoger Pau Monné {
116bf7313e3SRoger Pau Monné 	struct xen_remove_from_physmap rm = { .domid = DOMID_SELF };
117bf7313e3SRoger Pau Monné 	struct privcmd_map *map = handle;
118bf7313e3SRoger Pau Monné 	int error;
119bf7313e3SRoger Pau Monné 	vm_size_t i;
120bf7313e3SRoger Pau Monné 	vm_page_t m;
121bf7313e3SRoger Pau Monné 
122bf7313e3SRoger Pau Monné 	/*
123bf7313e3SRoger Pau Monné 	 * Remove the mappings from the used pages. This will remove the
124bf7313e3SRoger Pau Monné 	 * underlying p2m bindings in Xen second stage translation.
125bf7313e3SRoger Pau Monné 	 */
126bf7313e3SRoger Pau Monné 	if (map->mapped == true) {
127bf7313e3SRoger Pau Monné 		VM_OBJECT_WLOCK(map->mem);
128bf7313e3SRoger Pau Monné retry:
129bf7313e3SRoger Pau Monné 		for (i = 0; i < map->size; i++) {
130bf7313e3SRoger Pau Monné 			m = vm_page_lookup(map->mem, i);
131bf7313e3SRoger Pau Monné 			if (m == NULL)
132bf7313e3SRoger Pau Monné 				continue;
133c7575748SJeff Roberson 			if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
134bf7313e3SRoger Pau Monné 				goto retry;
135bf7313e3SRoger Pau Monné 			cdev_pager_free_page(map->mem, m);
136bf7313e3SRoger Pau Monné 		}
137bf7313e3SRoger Pau Monné 		VM_OBJECT_WUNLOCK(map->mem);
138bf7313e3SRoger Pau Monné 
139bf7313e3SRoger Pau Monné 		for (i = 0; i < map->size; i++) {
140bf7313e3SRoger Pau Monné 			rm.gpfn = atop(map->phys_base_addr) + i;
141bf7313e3SRoger Pau Monné 			HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &rm);
142bf7313e3SRoger Pau Monné 		}
143288b2385SRoger Pau Monné 		free(map->err, M_PRIVCMD);
144bf7313e3SRoger Pau Monné 	}
145bf7313e3SRoger Pau Monné 
1460df8b29dSRoger Pau Monné 	error = xenmem_free(privcmd_dev, map->pseudo_phys_res_id,
1470df8b29dSRoger Pau Monné 	    map->pseudo_phys_res);
148bf7313e3SRoger Pau Monné 	KASSERT(error == 0, ("Unable to release memory resource: %d", error));
149bf7313e3SRoger Pau Monné 
150bf7313e3SRoger Pau Monné 	free(map, M_PRIVCMD);
151bf7313e3SRoger Pau Monné }
152bf7313e3SRoger Pau Monné 
153bf7313e3SRoger Pau Monné static int
154bf7313e3SRoger Pau Monné privcmd_pg_fault(vm_object_t object, vm_ooffset_t offset,
155bf7313e3SRoger Pau Monné     int prot, vm_page_t *mres)
156bf7313e3SRoger Pau Monné {
157bf7313e3SRoger Pau Monné 	struct privcmd_map *map = object->handle;
158bf7313e3SRoger Pau Monné 	vm_pindex_t pidx;
1593cf3b4e6SJeff Roberson 	vm_page_t page;
160bf7313e3SRoger Pau Monné 
161bf7313e3SRoger Pau Monné 	if (map->mapped != true)
162bf7313e3SRoger Pau Monné 		return (VM_PAGER_FAIL);
163bf7313e3SRoger Pau Monné 
164bf7313e3SRoger Pau Monné 	pidx = OFF_TO_IDX(offset);
165288b2385SRoger Pau Monné 	if (pidx >= map->size || BIT_ISSET(map->size, pidx, map->err))
166bf7313e3SRoger Pau Monné 		return (VM_PAGER_FAIL);
167bf7313e3SRoger Pau Monné 
168bf7313e3SRoger Pau Monné 	page = PHYS_TO_VM_PAGE(map->phys_base_addr + offset);
169bf7313e3SRoger Pau Monné 	if (page == NULL)
170bf7313e3SRoger Pau Monné 		return (VM_PAGER_FAIL);
171bf7313e3SRoger Pau Monné 
172bf7313e3SRoger Pau Monné 	KASSERT((page->flags & PG_FICTITIOUS) != 0,
173bf7313e3SRoger Pau Monné 	    ("not fictitious %p", page));
174fee2a2faSMark Johnston 	KASSERT(vm_page_wired(page), ("page %p not wired", page));
175fee2a2faSMark Johnston 	KASSERT(!vm_page_busied(page), ("page %p is busy", page));
176bf7313e3SRoger Pau Monné 
17763e97555SJeff Roberson 	vm_page_busy_acquire(page, 0);
1780012f373SJeff Roberson 	vm_page_valid(page);
1793cf3b4e6SJeff Roberson 
1803cf3b4e6SJeff Roberson 	if (*mres != NULL)
1813cf3b4e6SJeff Roberson 		vm_page_replace(page, object, pidx, *mres);
1823cf3b4e6SJeff Roberson 	else
183bf7313e3SRoger Pau Monné 		vm_page_insert(page, object, pidx);
184bf7313e3SRoger Pau Monné 	*mres = page;
185bf7313e3SRoger Pau Monné 	return (VM_PAGER_OK);
186bf7313e3SRoger Pau Monné }
187bf7313e3SRoger Pau Monné 
188bf7313e3SRoger Pau Monné /*----------------------- Privcmd char device methods ------------------------*/
189bf7313e3SRoger Pau Monné static int
190bf7313e3SRoger Pau Monné privcmd_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
191bf7313e3SRoger Pau Monné     vm_object_t *object, int nprot)
192bf7313e3SRoger Pau Monné {
193bf7313e3SRoger Pau Monné 	struct privcmd_map *map;
194bf7313e3SRoger Pau Monné 
195bf7313e3SRoger Pau Monné 	map = malloc(sizeof(*map), M_PRIVCMD, M_WAITOK | M_ZERO);
196bf7313e3SRoger Pau Monné 
197bf7313e3SRoger Pau Monné 	map->size = OFF_TO_IDX(size);
198bf7313e3SRoger Pau Monné 	map->pseudo_phys_res_id = 0;
199bf7313e3SRoger Pau Monné 
2000df8b29dSRoger Pau Monné 	map->pseudo_phys_res = xenmem_alloc(privcmd_dev,
2010df8b29dSRoger Pau Monné 	    &map->pseudo_phys_res_id, size);
202bf7313e3SRoger Pau Monné 	if (map->pseudo_phys_res == NULL) {
203bf7313e3SRoger Pau Monné 		free(map, M_PRIVCMD);
204bf7313e3SRoger Pau Monné 		return (ENOMEM);
205bf7313e3SRoger Pau Monné 	}
206bf7313e3SRoger Pau Monné 
207bf7313e3SRoger Pau Monné 	map->phys_base_addr = rman_get_start(map->pseudo_phys_res);
208bf7313e3SRoger Pau Monné 	map->mem = cdev_pager_allocate(map, OBJT_MGTDEVICE, &privcmd_pg_ops,
209bf7313e3SRoger Pau Monné 	    size, nprot, *offset, NULL);
210bf7313e3SRoger Pau Monné 	if (map->mem == NULL) {
2110df8b29dSRoger Pau Monné 		xenmem_free(privcmd_dev, map->pseudo_phys_res_id,
2120df8b29dSRoger Pau Monné 		    map->pseudo_phys_res);
213bf7313e3SRoger Pau Monné 		free(map, M_PRIVCMD);
214bf7313e3SRoger Pau Monné 		return (ENOMEM);
215bf7313e3SRoger Pau Monné 	}
216bf7313e3SRoger Pau Monné 
217bf7313e3SRoger Pau Monné 	*object = map->mem;
218bf7313e3SRoger Pau Monné 
219bf7313e3SRoger Pau Monné 	return (0);
220bf7313e3SRoger Pau Monné }
221bf7313e3SRoger Pau Monné 
222147e5939SRoger Pau Monné static struct privcmd_map *
223147e5939SRoger Pau Monné setup_virtual_area(struct thread *td, unsigned long addr, unsigned long num)
224147e5939SRoger Pau Monné {
225147e5939SRoger Pau Monné 	vm_map_t map;
226147e5939SRoger Pau Monné 	vm_map_entry_t entry;
227147e5939SRoger Pau Monné 	vm_object_t mem;
228147e5939SRoger Pau Monné 	vm_pindex_t pindex;
229147e5939SRoger Pau Monné 	vm_prot_t prot;
230147e5939SRoger Pau Monné 	boolean_t wired;
231147e5939SRoger Pau Monné 	struct privcmd_map *umap;
232147e5939SRoger Pau Monné 	int error;
233147e5939SRoger Pau Monné 
234147e5939SRoger Pau Monné 	if ((num == 0) || ((addr & PAGE_MASK) != 0))
235147e5939SRoger Pau Monné 		return NULL;
236147e5939SRoger Pau Monné 
237147e5939SRoger Pau Monné 	map = &td->td_proc->p_vmspace->vm_map;
238147e5939SRoger Pau Monné 	error = vm_map_lookup(&map, addr, VM_PROT_NONE, &entry, &mem, &pindex,
239147e5939SRoger Pau Monné 	    &prot, &wired);
240147e5939SRoger Pau Monné 	if (error != KERN_SUCCESS || (entry->start != addr) ||
241147e5939SRoger Pau Monné 	    (entry->end != addr + (num * PAGE_SIZE)))
242147e5939SRoger Pau Monné 		return NULL;
243147e5939SRoger Pau Monné 
244147e5939SRoger Pau Monné 	vm_map_lookup_done(map, entry);
245147e5939SRoger Pau Monné 	if ((mem->type != OBJT_MGTDEVICE) ||
246147e5939SRoger Pau Monné 	    (mem->un_pager.devp.ops != &privcmd_pg_ops))
247147e5939SRoger Pau Monné 		return NULL;
248147e5939SRoger Pau Monné 
249147e5939SRoger Pau Monné 	umap = mem->handle;
250147e5939SRoger Pau Monné 	/* Allocate a bitset to store broken page mappings. */
251147e5939SRoger Pau Monné 	umap->err = BITSET_ALLOC(num, M_PRIVCMD, M_WAITOK | M_ZERO);
252147e5939SRoger Pau Monné 
253147e5939SRoger Pau Monné 	return umap;
254147e5939SRoger Pau Monné }
255147e5939SRoger Pau Monné 
256bf7313e3SRoger Pau Monné static int
257bf7313e3SRoger Pau Monné privcmd_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg,
258bf7313e3SRoger Pau Monné 	      int mode, struct thread *td)
259bf7313e3SRoger Pau Monné {
260f713a5b3SRoger Pau Monné 	int error;
261f713a5b3SRoger Pau Monné 	unsigned int i;
262bf7313e3SRoger Pau Monné 
263bf7313e3SRoger Pau Monné 	switch (cmd) {
264bf7313e3SRoger Pau Monné 	case IOCTL_PRIVCMD_HYPERCALL: {
265bf7313e3SRoger Pau Monné 		struct ioctl_privcmd_hypercall *hcall;
266bf7313e3SRoger Pau Monné 
267bf7313e3SRoger Pau Monné 		hcall = (struct ioctl_privcmd_hypercall *)arg;
2685ff6c7f3SRoger Pau Monné #ifdef __amd64__
2695ff6c7f3SRoger Pau Monné 		/*
2705ff6c7f3SRoger Pau Monné 		 * The hypervisor page table walker will refuse to access
2715ff6c7f3SRoger Pau Monné 		 * user-space pages if SMAP is enabled, so temporary disable it
2725ff6c7f3SRoger Pau Monné 		 * while performing the hypercall.
2735ff6c7f3SRoger Pau Monné 		 */
2745ff6c7f3SRoger Pau Monné 		if (cpu_stdext_feature & CPUID_STDEXT_SMAP)
2755ff6c7f3SRoger Pau Monné 			stac();
2765ff6c7f3SRoger Pau Monné #endif
277bf7313e3SRoger Pau Monné 		error = privcmd_hypercall(hcall->op, hcall->arg[0],
278bf7313e3SRoger Pau Monné 		    hcall->arg[1], hcall->arg[2], hcall->arg[3], hcall->arg[4]);
2795ff6c7f3SRoger Pau Monné #ifdef __amd64__
2805ff6c7f3SRoger Pau Monné 		if (cpu_stdext_feature & CPUID_STDEXT_SMAP)
2815ff6c7f3SRoger Pau Monné 			clac();
2825ff6c7f3SRoger Pau Monné #endif
283bf7313e3SRoger Pau Monné 		if (error >= 0) {
284bf7313e3SRoger Pau Monné 			hcall->retval = error;
285bf7313e3SRoger Pau Monné 			error = 0;
286bf7313e3SRoger Pau Monné 		} else {
287bf7313e3SRoger Pau Monné 			error = xen_translate_error(error);
288bf7313e3SRoger Pau Monné 			hcall->retval = 0;
289bf7313e3SRoger Pau Monné 		}
290bf7313e3SRoger Pau Monné 		break;
291bf7313e3SRoger Pau Monné 	}
292bf7313e3SRoger Pau Monné 	case IOCTL_PRIVCMD_MMAPBATCH: {
293bf7313e3SRoger Pau Monné 		struct ioctl_privcmd_mmapbatch *mmap;
294bf7313e3SRoger Pau Monné 		struct xen_add_to_physmap_range add;
295bf7313e3SRoger Pau Monné 		xen_ulong_t *idxs;
296bf7313e3SRoger Pau Monné 		xen_pfn_t *gpfns;
297f713a5b3SRoger Pau Monné 		int *errs;
298f713a5b3SRoger Pau Monné 		unsigned int index;
299bf7313e3SRoger Pau Monné 		struct privcmd_map *umap;
300288b2385SRoger Pau Monné 		uint16_t num;
301bf7313e3SRoger Pau Monné 
302bf7313e3SRoger Pau Monné 		mmap = (struct ioctl_privcmd_mmapbatch *)arg;
303bf7313e3SRoger Pau Monné 
304147e5939SRoger Pau Monné 		umap = setup_virtual_area(td, mmap->addr, mmap->num);
305147e5939SRoger Pau Monné 		if (umap == NULL) {
306bf7313e3SRoger Pau Monné 			error = EINVAL;
307bf7313e3SRoger Pau Monné 			break;
308bf7313e3SRoger Pau Monné 		}
309bf7313e3SRoger Pau Monné 
310bf7313e3SRoger Pau Monné 		add.domid = DOMID_SELF;
311bf7313e3SRoger Pau Monné 		add.space = XENMAPSPACE_gmfn_foreign;
312bf7313e3SRoger Pau Monné 		add.foreign_domid = mmap->dom;
313bf7313e3SRoger Pau Monné 
314288b2385SRoger Pau Monné 		/*
315288b2385SRoger Pau Monné 		 * The 'size' field in the xen_add_to_physmap_range only
316288b2385SRoger Pau Monné 		 * allows for UINT16_MAX mappings in a single hypercall.
317288b2385SRoger Pau Monné 		 */
318288b2385SRoger Pau Monné 		num = MIN(mmap->num, UINT16_MAX);
319288b2385SRoger Pau Monné 
320288b2385SRoger Pau Monné 		idxs = malloc(sizeof(*idxs) * num, M_PRIVCMD, M_WAITOK);
321288b2385SRoger Pau Monné 		gpfns = malloc(sizeof(*gpfns) * num, M_PRIVCMD, M_WAITOK);
322288b2385SRoger Pau Monné 		errs = malloc(sizeof(*errs) * num, M_PRIVCMD, M_WAITOK);
323bf7313e3SRoger Pau Monné 
324bf7313e3SRoger Pau Monné 		set_xen_guest_handle(add.idxs, idxs);
325bf7313e3SRoger Pau Monné 		set_xen_guest_handle(add.gpfns, gpfns);
326bf7313e3SRoger Pau Monné 		set_xen_guest_handle(add.errs, errs);
327bf7313e3SRoger Pau Monné 
328288b2385SRoger Pau Monné 		for (index = 0; index < mmap->num; index += num) {
329288b2385SRoger Pau Monné 			num = MIN(mmap->num - index, UINT16_MAX);
330288b2385SRoger Pau Monné 			add.size = num;
331288b2385SRoger Pau Monné 
332288b2385SRoger Pau Monné 			error = copyin(&mmap->arr[index], idxs,
333288b2385SRoger Pau Monné 			    sizeof(idxs[0]) * num);
334bf7313e3SRoger Pau Monné 			if (error != 0)
335bf7313e3SRoger Pau Monné 				goto mmap_out;
336bf7313e3SRoger Pau Monné 
337288b2385SRoger Pau Monné 			for (i = 0; i < num; i++)
338288b2385SRoger Pau Monné 				gpfns[i] = atop(umap->phys_base_addr +
339288b2385SRoger Pau Monné 				    (i + index) * PAGE_SIZE);
340bf7313e3SRoger Pau Monné 
341288b2385SRoger Pau Monné 			bzero(errs, sizeof(*errs) * num);
342288b2385SRoger Pau Monné 
343288b2385SRoger Pau Monné 			error = HYPERVISOR_memory_op(
344288b2385SRoger Pau Monné 			    XENMEM_add_to_physmap_range, &add);
345288b2385SRoger Pau Monné 			if (error != 0) {
346bf7313e3SRoger Pau Monné 				error = xen_translate_error(error);
347bf7313e3SRoger Pau Monné 				goto mmap_out;
348bf7313e3SRoger Pau Monné 			}
349bf7313e3SRoger Pau Monné 
350288b2385SRoger Pau Monné 			for (i = 0; i < num; i++) {
351288b2385SRoger Pau Monné 				if (errs[i] != 0) {
352bf7313e3SRoger Pau Monné 					errs[i] = xen_translate_error(errs[i]);
353288b2385SRoger Pau Monné 
354288b2385SRoger Pau Monné 					/* Mark the page as invalid. */
355288b2385SRoger Pau Monné 					BIT_SET(mmap->num, index + i,
356288b2385SRoger Pau Monné 					    umap->err);
357288b2385SRoger Pau Monné 				}
358bf7313e3SRoger Pau Monné 			}
359bf7313e3SRoger Pau Monné 
360288b2385SRoger Pau Monné 			error = copyout(errs, &mmap->err[index],
361288b2385SRoger Pau Monné 			    sizeof(errs[0]) * num);
362288b2385SRoger Pau Monné 			if (error != 0)
363288b2385SRoger Pau Monné 				goto mmap_out;
364288b2385SRoger Pau Monné 		}
365bf7313e3SRoger Pau Monné 
366288b2385SRoger Pau Monné 		umap->mapped = true;
367bf7313e3SRoger Pau Monné 
368bf7313e3SRoger Pau Monné mmap_out:
369bf7313e3SRoger Pau Monné 		free(idxs, M_PRIVCMD);
370bf7313e3SRoger Pau Monné 		free(gpfns, M_PRIVCMD);
371bf7313e3SRoger Pau Monné 		free(errs, M_PRIVCMD);
372288b2385SRoger Pau Monné 		if (!umap->mapped)
373288b2385SRoger Pau Monné 			free(umap->err, M_PRIVCMD);
374bf7313e3SRoger Pau Monné 
375bf7313e3SRoger Pau Monné 		break;
376bf7313e3SRoger Pau Monné 	}
377658860e2SRoger Pau Monne 	case IOCTL_PRIVCMD_MMAP_RESOURCE: {
378658860e2SRoger Pau Monne 		struct ioctl_privcmd_mmapresource *mmap;
379658860e2SRoger Pau Monne 		struct xen_mem_acquire_resource adq;
380658860e2SRoger Pau Monne 		xen_pfn_t *gpfns;
381658860e2SRoger Pau Monne 		struct privcmd_map *umap;
382bf7313e3SRoger Pau Monné 
383658860e2SRoger Pau Monne 		mmap = (struct ioctl_privcmd_mmapresource *)arg;
384658860e2SRoger Pau Monne 
385658860e2SRoger Pau Monne 		bzero(&adq, sizeof(adq));
386658860e2SRoger Pau Monne 
387658860e2SRoger Pau Monne 		adq.domid = mmap->dom;
388658860e2SRoger Pau Monne 		adq.type = mmap->type;
389658860e2SRoger Pau Monne 		adq.id = mmap->id;
390658860e2SRoger Pau Monne 
391658860e2SRoger Pau Monne 		/* Shortcut for getting the resource size. */
392658860e2SRoger Pau Monne 		if (mmap->addr == 0 && mmap->num == 0) {
393658860e2SRoger Pau Monne 			error = HYPERVISOR_memory_op(XENMEM_acquire_resource,
394658860e2SRoger Pau Monne 			    &adq);
395658860e2SRoger Pau Monne 			if (error != 0) {
396658860e2SRoger Pau Monne 				error = xen_translate_error(error);
397658860e2SRoger Pau Monne 				break;
398658860e2SRoger Pau Monne 			}
399658860e2SRoger Pau Monne 			error = copyout(&adq.nr_frames, &mmap->num,
400658860e2SRoger Pau Monne 			    sizeof(mmap->num));
401658860e2SRoger Pau Monne 			break;
402658860e2SRoger Pau Monne 		}
403658860e2SRoger Pau Monne 
404658860e2SRoger Pau Monne 		umap = setup_virtual_area(td, mmap->addr, mmap->num);
405658860e2SRoger Pau Monne 		if (umap == NULL) {
406658860e2SRoger Pau Monne 			error = EINVAL;
407658860e2SRoger Pau Monne 			break;
408658860e2SRoger Pau Monne 		}
409658860e2SRoger Pau Monne 
410658860e2SRoger Pau Monne 		adq.nr_frames = mmap->num;
411658860e2SRoger Pau Monne 		adq.frame = mmap->idx;
412658860e2SRoger Pau Monne 
413658860e2SRoger Pau Monne 		gpfns = malloc(sizeof(*gpfns) * mmap->num, M_PRIVCMD, M_WAITOK);
414658860e2SRoger Pau Monne 		for (i = 0; i < mmap->num; i++)
415658860e2SRoger Pau Monne 			gpfns[i] = atop(umap->phys_base_addr) + i;
416658860e2SRoger Pau Monne 		set_xen_guest_handle(adq.frame_list, gpfns);
417658860e2SRoger Pau Monne 
418658860e2SRoger Pau Monne 		error = HYPERVISOR_memory_op(XENMEM_acquire_resource, &adq);
419658860e2SRoger Pau Monne 		if (error != 0)
420658860e2SRoger Pau Monne 			error = xen_translate_error(error);
421658860e2SRoger Pau Monne 		else
422658860e2SRoger Pau Monne 			umap->mapped = true;
423658860e2SRoger Pau Monne 
424658860e2SRoger Pau Monne 		free(gpfns, M_PRIVCMD);
425658860e2SRoger Pau Monne 		if (!umap->mapped)
426658860e2SRoger Pau Monne 			free(umap->err, M_PRIVCMD);
427658860e2SRoger Pau Monne 
428658860e2SRoger Pau Monne 		break;
429658860e2SRoger Pau Monne 	}
430*ed78016dSRoger Pau Monne 	case IOCTL_PRIVCMD_DM_OP: {
431*ed78016dSRoger Pau Monne 		const struct ioctl_privcmd_dmop *dmop;
432*ed78016dSRoger Pau Monne 		struct privcmd_dmop_buf *bufs;
433*ed78016dSRoger Pau Monne 		struct xen_dm_op_buf *hbufs;
434*ed78016dSRoger Pau Monne 
435*ed78016dSRoger Pau Monne 		dmop = (struct ioctl_privcmd_dmop *)arg;
436*ed78016dSRoger Pau Monne 
437*ed78016dSRoger Pau Monne 		if (dmop->num == 0)
438*ed78016dSRoger Pau Monne 			break;
439*ed78016dSRoger Pau Monne 
440*ed78016dSRoger Pau Monne 		if (dmop->num > MAX_DMOP_BUFFERS) {
441*ed78016dSRoger Pau Monne 			error = E2BIG;
442*ed78016dSRoger Pau Monne 			break;
443*ed78016dSRoger Pau Monne 		}
444*ed78016dSRoger Pau Monne 
445*ed78016dSRoger Pau Monne 		bufs = malloc(sizeof(*bufs) * dmop->num, M_PRIVCMD, M_WAITOK);
446*ed78016dSRoger Pau Monne 
447*ed78016dSRoger Pau Monne 		error = copyin(dmop->ubufs, bufs, sizeof(*bufs) * dmop->num);
448*ed78016dSRoger Pau Monne 		if (error != 0) {
449*ed78016dSRoger Pau Monne 			free(bufs, M_PRIVCMD);
450*ed78016dSRoger Pau Monne 			break;
451*ed78016dSRoger Pau Monne 		}
452*ed78016dSRoger Pau Monne 
453*ed78016dSRoger Pau Monne 		hbufs = malloc(sizeof(*hbufs) * dmop->num, M_PRIVCMD, M_WAITOK);
454*ed78016dSRoger Pau Monne 		for (i = 0; i < dmop->num; i++) {
455*ed78016dSRoger Pau Monne 			set_xen_guest_handle(hbufs[i].h, bufs[i].uptr);
456*ed78016dSRoger Pau Monne 			hbufs[i].size = bufs[i].size;
457*ed78016dSRoger Pau Monne 		}
458*ed78016dSRoger Pau Monne 
459*ed78016dSRoger Pau Monne #ifdef __amd64__
460*ed78016dSRoger Pau Monne 		if (cpu_stdext_feature & CPUID_STDEXT_SMAP)
461*ed78016dSRoger Pau Monne 			stac();
462*ed78016dSRoger Pau Monne #endif
463*ed78016dSRoger Pau Monne 		error = HYPERVISOR_dm_op(dmop->dom, dmop->num, hbufs);
464*ed78016dSRoger Pau Monne #ifdef __amd64__
465*ed78016dSRoger Pau Monne 		if (cpu_stdext_feature & CPUID_STDEXT_SMAP)
466*ed78016dSRoger Pau Monne 			clac();
467*ed78016dSRoger Pau Monne #endif
468*ed78016dSRoger Pau Monne 		if (error != 0)
469*ed78016dSRoger Pau Monne 			error = xen_translate_error(error);
470*ed78016dSRoger Pau Monne 
471*ed78016dSRoger Pau Monne 		free(bufs, M_PRIVCMD);
472*ed78016dSRoger Pau Monne 		free(hbufs, M_PRIVCMD);
473*ed78016dSRoger Pau Monne 
474*ed78016dSRoger Pau Monne 
475*ed78016dSRoger Pau Monne 		break;
476*ed78016dSRoger Pau Monne 	}
477bf7313e3SRoger Pau Monné 	default:
478bf7313e3SRoger Pau Monné 		error = ENOSYS;
479bf7313e3SRoger Pau Monné 		break;
480bf7313e3SRoger Pau Monné 	}
481bf7313e3SRoger Pau Monné 
482bf7313e3SRoger Pau Monné 	return (error);
483bf7313e3SRoger Pau Monné }
484bf7313e3SRoger Pau Monné 
485bf7313e3SRoger Pau Monné /*------------------ Private Device Attachment Functions  --------------------*/
486bf7313e3SRoger Pau Monné static void
487bf7313e3SRoger Pau Monné privcmd_identify(driver_t *driver, device_t parent)
488bf7313e3SRoger Pau Monné {
489bf7313e3SRoger Pau Monné 
490bf7313e3SRoger Pau Monné 	KASSERT(xen_domain(),
491bf7313e3SRoger Pau Monné 	    ("Trying to attach privcmd device on non Xen domain"));
492bf7313e3SRoger Pau Monné 
493bf7313e3SRoger Pau Monné 	if (BUS_ADD_CHILD(parent, 0, "privcmd", 0) == NULL)
494bf7313e3SRoger Pau Monné 		panic("unable to attach privcmd user-space device");
495bf7313e3SRoger Pau Monné }
496bf7313e3SRoger Pau Monné 
497bf7313e3SRoger Pau Monné static int
498bf7313e3SRoger Pau Monné privcmd_probe(device_t dev)
499bf7313e3SRoger Pau Monné {
500bf7313e3SRoger Pau Monné 
501bf7313e3SRoger Pau Monné 	privcmd_dev = dev;
502bf7313e3SRoger Pau Monné 	device_set_desc(dev, "Xen privileged interface user-space device");
503bf7313e3SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
504bf7313e3SRoger Pau Monné }
505bf7313e3SRoger Pau Monné 
506bf7313e3SRoger Pau Monné static int
507bf7313e3SRoger Pau Monné privcmd_attach(device_t dev)
508bf7313e3SRoger Pau Monné {
509bf7313e3SRoger Pau Monné 
510bf7313e3SRoger Pau Monné 	make_dev_credf(MAKEDEV_ETERNAL, &privcmd_devsw, 0, NULL, UID_ROOT,
511bf7313e3SRoger Pau Monné 	    GID_WHEEL, 0600, "xen/privcmd");
512bf7313e3SRoger Pau Monné 	return (0);
513bf7313e3SRoger Pau Monné }
514bf7313e3SRoger Pau Monné 
515bf7313e3SRoger Pau Monné /*-------------------- Private Device Attachment Data  -----------------------*/
516bf7313e3SRoger Pau Monné static device_method_t privcmd_methods[] = {
517bf7313e3SRoger Pau Monné 	DEVMETHOD(device_identify,	privcmd_identify),
518bf7313e3SRoger Pau Monné 	DEVMETHOD(device_probe,		privcmd_probe),
519bf7313e3SRoger Pau Monné 	DEVMETHOD(device_attach,	privcmd_attach),
520bf7313e3SRoger Pau Monné 
521bf7313e3SRoger Pau Monné 	DEVMETHOD_END
522bf7313e3SRoger Pau Monné };
523bf7313e3SRoger Pau Monné 
524bf7313e3SRoger Pau Monné static driver_t privcmd_driver = {
525bf7313e3SRoger Pau Monné 	"privcmd",
526bf7313e3SRoger Pau Monné 	privcmd_methods,
527bf7313e3SRoger Pau Monné 	0,
528bf7313e3SRoger Pau Monné };
529bf7313e3SRoger Pau Monné 
530bf7313e3SRoger Pau Monné devclass_t privcmd_devclass;
531bf7313e3SRoger Pau Monné 
532bf7313e3SRoger Pau Monné DRIVER_MODULE(privcmd, xenpv, privcmd_driver, privcmd_devclass, 0, 0);
533bf7313e3SRoger Pau Monné MODULE_DEPEND(privcmd, xenpv, 1, 1, 1);
534