xref: /freebsd/sys/dev/xen/privcmd/privcmd.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
1 /*
2  * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/uio.h>
33 #include <sys/bus.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/rwlock.h>
39 #include <sys/selinfo.h>
40 #include <sys/poll.h>
41 #include <sys/conf.h>
42 #include <sys/fcntl.h>
43 #include <sys/ioccom.h>
44 #include <sys/rman.h>
45 #include <sys/tree.h>
46 #include <sys/module.h>
47 #include <sys/proc.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vm_phys.h>
58 
59 #include <machine/md_var.h>
60 
61 #include <xen/xen-os.h>
62 #include <xen/hypervisor.h>
63 #include <xen/privcmd.h>
64 #include <xen/error.h>
65 
66 MALLOC_DEFINE(M_PRIVCMD, "privcmd_dev", "Xen privcmd user-space device");
67 
68 struct privcmd_map {
69 	vm_object_t mem;
70 	vm_size_t size;
71 	struct resource *pseudo_phys_res;
72 	int pseudo_phys_res_id;
73 	vm_paddr_t phys_base_addr;
74 	boolean_t mapped;
75 	int *errs;
76 };
77 
78 static d_ioctl_t     privcmd_ioctl;
79 static d_mmap_single_t	privcmd_mmap_single;
80 
81 static struct cdevsw privcmd_devsw = {
82 	.d_version = D_VERSION,
83 	.d_ioctl = privcmd_ioctl,
84 	.d_mmap_single = privcmd_mmap_single,
85 	.d_name = "privcmd",
86 };
87 
88 static int privcmd_pg_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
89     vm_ooffset_t foff, struct ucred *cred, u_short *color);
90 static void privcmd_pg_dtor(void *handle);
91 static int privcmd_pg_fault(vm_object_t object, vm_ooffset_t offset,
92     int prot, vm_page_t *mres);
93 
94 static struct cdev_pager_ops privcmd_pg_ops = {
95 	.cdev_pg_fault = privcmd_pg_fault,
96 	.cdev_pg_ctor =	privcmd_pg_ctor,
97 	.cdev_pg_dtor =	privcmd_pg_dtor,
98 };
99 
100 static device_t privcmd_dev = NULL;
101 
102 /*------------------------- Privcmd Pager functions --------------------------*/
103 static int
104 privcmd_pg_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
105     vm_ooffset_t foff, struct ucred *cred, u_short *color)
106 {
107 
108 	return (0);
109 }
110 
111 static void
112 privcmd_pg_dtor(void *handle)
113 {
114 	struct xen_remove_from_physmap rm = { .domid = DOMID_SELF };
115 	struct privcmd_map *map = handle;
116 	int error;
117 	vm_size_t i;
118 	vm_page_t m;
119 
120 	/*
121 	 * Remove the mappings from the used pages. This will remove the
122 	 * underlying p2m bindings in Xen second stage translation.
123 	 */
124 	if (map->mapped == true) {
125 		VM_OBJECT_WLOCK(map->mem);
126 retry:
127 		for (i = 0; i < map->size; i++) {
128 			m = vm_page_lookup(map->mem, i);
129 			if (m == NULL)
130 				continue;
131 			if (vm_page_sleep_if_busy(m, "pcmdum"))
132 				goto retry;
133 			cdev_pager_free_page(map->mem, m);
134 		}
135 		VM_OBJECT_WUNLOCK(map->mem);
136 
137 		for (i = 0; i < map->size; i++) {
138 			rm.gpfn = atop(map->phys_base_addr) + i;
139 			HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &rm);
140 		}
141 		free(map->errs, M_PRIVCMD);
142 	}
143 
144 	vm_phys_fictitious_unreg_range(map->phys_base_addr,
145 	    map->phys_base_addr + map->size * PAGE_SIZE);
146 
147 	error = bus_release_resource(privcmd_dev, SYS_RES_MEMORY,
148 	    map->pseudo_phys_res_id, map->pseudo_phys_res);
149 	KASSERT(error == 0, ("Unable to release memory resource: %d", error));
150 
151 	free(map, M_PRIVCMD);
152 }
153 
154 static int
155 privcmd_pg_fault(vm_object_t object, vm_ooffset_t offset,
156     int prot, vm_page_t *mres)
157 {
158 	struct privcmd_map *map = object->handle;
159 	vm_pindex_t pidx;
160 	vm_page_t page, oldm;
161 
162 	if (map->mapped != true)
163 		return (VM_PAGER_FAIL);
164 
165 	pidx = OFF_TO_IDX(offset);
166 	if (pidx >= map->size || map->errs[pidx] != 0)
167 		return (VM_PAGER_FAIL);
168 
169 	page = PHYS_TO_VM_PAGE(map->phys_base_addr + offset);
170 	if (page == NULL)
171 		return (VM_PAGER_FAIL);
172 
173 	KASSERT((page->flags & PG_FICTITIOUS) != 0,
174 	    ("not fictitious %p", page));
175 	KASSERT(page->wire_count == 1, ("wire_count not 1 %p", page));
176 	KASSERT(vm_page_busied(page) == 0, ("page %p is busy", page));
177 
178 	if (*mres != NULL) {
179 		oldm = *mres;
180 		vm_page_lock(oldm);
181 		vm_page_free(oldm);
182 		vm_page_unlock(oldm);
183 		*mres = NULL;
184 	}
185 
186 	vm_page_insert(page, object, pidx);
187 	page->valid = VM_PAGE_BITS_ALL;
188 	vm_page_xbusy(page);
189 	*mres = page;
190 	return (VM_PAGER_OK);
191 }
192 
193 /*----------------------- Privcmd char device methods ------------------------*/
194 static int
195 privcmd_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
196     vm_object_t *object, int nprot)
197 {
198 	struct privcmd_map *map;
199 	int error;
200 
201 	map = malloc(sizeof(*map), M_PRIVCMD, M_WAITOK | M_ZERO);
202 
203 	map->size = OFF_TO_IDX(size);
204 	map->pseudo_phys_res_id = 0;
205 
206 	map->pseudo_phys_res = bus_alloc_resource(privcmd_dev, SYS_RES_MEMORY,
207 	    &map->pseudo_phys_res_id, 0, ~0, size, RF_ACTIVE);
208 	if (map->pseudo_phys_res == NULL) {
209 		free(map, M_PRIVCMD);
210 		return (ENOMEM);
211 	}
212 
213 	map->phys_base_addr = rman_get_start(map->pseudo_phys_res);
214 
215 	error = vm_phys_fictitious_reg_range(map->phys_base_addr,
216 	    map->phys_base_addr + size, VM_MEMATTR_DEFAULT);
217 	if (error) {
218 		bus_release_resource(privcmd_dev, SYS_RES_MEMORY,
219 		    map->pseudo_phys_res_id, map->pseudo_phys_res);
220 		free(map, M_PRIVCMD);
221 		return (error);
222 	}
223 
224 	map->mem = cdev_pager_allocate(map, OBJT_MGTDEVICE, &privcmd_pg_ops,
225 	    size, nprot, *offset, NULL);
226 	if (map->mem == NULL) {
227 		bus_release_resource(privcmd_dev, SYS_RES_MEMORY,
228 		    map->pseudo_phys_res_id, map->pseudo_phys_res);
229 		free(map, M_PRIVCMD);
230 		return (ENOMEM);
231 	}
232 
233 	*object = map->mem;
234 
235 	return (0);
236 }
237 
238 static int
239 privcmd_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg,
240 	      int mode, struct thread *td)
241 {
242 	int error, i;
243 
244 	switch (cmd) {
245 	case IOCTL_PRIVCMD_HYPERCALL: {
246 		struct ioctl_privcmd_hypercall *hcall;
247 
248 		hcall = (struct ioctl_privcmd_hypercall *)arg;
249 
250 		error = privcmd_hypercall(hcall->op, hcall->arg[0],
251 		    hcall->arg[1], hcall->arg[2], hcall->arg[3], hcall->arg[4]);
252 		if (error >= 0) {
253 			hcall->retval = error;
254 			error = 0;
255 		} else {
256 			error = xen_translate_error(error);
257 			hcall->retval = 0;
258 		}
259 		break;
260 	}
261 	case IOCTL_PRIVCMD_MMAPBATCH: {
262 		struct ioctl_privcmd_mmapbatch *mmap;
263 		vm_map_t map;
264 		vm_map_entry_t entry;
265 		vm_object_t mem;
266 		vm_pindex_t index;
267 		vm_prot_t prot;
268 		boolean_t wired;
269 		struct xen_add_to_physmap_range add;
270 		xen_ulong_t *idxs;
271 		xen_pfn_t *gpfns;
272 		int *errs;
273 		struct privcmd_map *umap;
274 
275 		mmap = (struct ioctl_privcmd_mmapbatch *)arg;
276 
277 		if ((mmap->num == 0) ||
278 		    ((mmap->addr & PAGE_MASK) != 0)) {
279 			error = EINVAL;
280 			break;
281 		}
282 
283 		map = &td->td_proc->p_vmspace->vm_map;
284 		error = vm_map_lookup(&map, mmap->addr, VM_PROT_NONE, &entry,
285 		    &mem, &index, &prot, &wired);
286 		if (error != KERN_SUCCESS) {
287 			error = EINVAL;
288 			break;
289 		}
290 		if ((entry->start != mmap->addr) ||
291 		    (entry->end != mmap->addr + (mmap->num * PAGE_SIZE))) {
292 			vm_map_lookup_done(map, entry);
293 			error = EINVAL;
294 			break;
295 		}
296 		vm_map_lookup_done(map, entry);
297 		if ((mem->type != OBJT_MGTDEVICE) ||
298 		    (mem->un_pager.devp.ops != &privcmd_pg_ops)) {
299 			error = EINVAL;
300 			break;
301 		}
302 		umap = mem->handle;
303 
304 		add.domid = DOMID_SELF;
305 		add.space = XENMAPSPACE_gmfn_foreign;
306 		add.size = mmap->num;
307 		add.foreign_domid = mmap->dom;
308 
309 		idxs = malloc(sizeof(*idxs) * mmap->num, M_PRIVCMD,
310 		    M_WAITOK | M_ZERO);
311 		gpfns = malloc(sizeof(*gpfns) * mmap->num, M_PRIVCMD,
312 		    M_WAITOK | M_ZERO);
313 		errs = malloc(sizeof(*errs) * mmap->num, M_PRIVCMD,
314 		    M_WAITOK | M_ZERO);
315 
316 		set_xen_guest_handle(add.idxs, idxs);
317 		set_xen_guest_handle(add.gpfns, gpfns);
318 		set_xen_guest_handle(add.errs, errs);
319 
320 		error = copyin(&mmap->arr[0], idxs,
321 		    sizeof(idxs[0]) * mmap->num);
322 		if (error != 0)
323 			goto mmap_out;
324 
325 		for (i = 0; i < mmap->num; i++)
326 			gpfns[i] = atop(umap->phys_base_addr + i * PAGE_SIZE);
327 
328 		error = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &add);
329 		if (error) {
330 			error = xen_translate_error(error);
331 			goto mmap_out;
332 		}
333 
334 		for (i = 0; i < mmap->num; i++) {
335 			if (errs[i] != 0)
336 				errs[i] = xen_translate_error(errs[i]);
337 		}
338 
339 		/*
340 		 * Save errs, so we know which pages have been
341 		 * successfully mapped.
342 		 */
343 		umap->errs = errs;
344 		umap->mapped = true;
345 
346 		error = copyout(errs, &mmap->err[0],
347 		    sizeof(errs[0]) * mmap->num);
348 
349 mmap_out:
350 		free(idxs, M_PRIVCMD);
351 		free(gpfns, M_PRIVCMD);
352 		if (!umap->mapped)
353 			free(errs, M_PRIVCMD);
354 
355 		break;
356 	}
357 
358 	default:
359 		error = ENOSYS;
360 		break;
361 	}
362 
363 	return (error);
364 }
365 
366 /*------------------ Private Device Attachment Functions  --------------------*/
367 static void
368 privcmd_identify(driver_t *driver, device_t parent)
369 {
370 
371 	KASSERT(xen_domain(),
372 	    ("Trying to attach privcmd device on non Xen domain"));
373 
374 	if (BUS_ADD_CHILD(parent, 0, "privcmd", 0) == NULL)
375 		panic("unable to attach privcmd user-space device");
376 }
377 
378 static int
379 privcmd_probe(device_t dev)
380 {
381 
382 	privcmd_dev = dev;
383 	device_set_desc(dev, "Xen privileged interface user-space device");
384 	return (BUS_PROBE_NOWILDCARD);
385 }
386 
387 static int
388 privcmd_attach(device_t dev)
389 {
390 
391 	make_dev_credf(MAKEDEV_ETERNAL, &privcmd_devsw, 0, NULL, UID_ROOT,
392 	    GID_WHEEL, 0600, "xen/privcmd");
393 	return (0);
394 }
395 
396 /*-------------------- Private Device Attachment Data  -----------------------*/
397 static device_method_t privcmd_methods[] = {
398 	DEVMETHOD(device_identify,	privcmd_identify),
399 	DEVMETHOD(device_probe,		privcmd_probe),
400 	DEVMETHOD(device_attach,	privcmd_attach),
401 
402 	DEVMETHOD_END
403 };
404 
405 static driver_t privcmd_driver = {
406 	"privcmd",
407 	privcmd_methods,
408 	0,
409 };
410 
411 devclass_t privcmd_devclass;
412 
413 DRIVER_MODULE(privcmd, xenpv, privcmd_driver, privcmd_devclass, 0, 0);
414 MODULE_DEPEND(privcmd, xenpv, 1, 1, 1);
415