2 * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
38 #include <sys/rwlock.h>
39 #include <sys/selinfo.h>
42 #include <sys/fcntl.h>
43 #include <sys/ioccom.h>
46 #include <sys/module.h>
48 #include <sys/bitset.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_pager.h>
59 #include <machine/md_var.h>
61 #include <xen/xen-os.h>
62 #include <xen/hypervisor.h>
63 #include <xen/privcmd.h>
64 #include <xen/error.h>
66 MALLOC_DEFINE(M_PRIVCMD, "privcmd_dev", "Xen privcmd user-space device");
71 struct resource *pseudo_phys_res;
72 int pseudo_phys_res_id;
73 vm_paddr_t phys_base_addr;
75 BITSET_DEFINE_VAR() *err;
78 static d_ioctl_t privcmd_ioctl;
79 static d_mmap_single_t privcmd_mmap_single;
81 static struct cdevsw privcmd_devsw = {
82 .d_version = D_VERSION,
83 .d_ioctl = privcmd_ioctl,
84 .d_mmap_single = privcmd_mmap_single,
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);
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,
100 static device_t privcmd_dev = NULL;
102 /*------------------------- Privcmd Pager functions --------------------------*/
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)
112 privcmd_pg_dtor(void *handle)
114 struct xen_remove_from_physmap rm = { .domid = DOMID_SELF };
115 struct privcmd_map *map = handle;
121 * Remove the mappings from the used pages. This will remove the
122 * underlying p2m bindings in Xen second stage translation.
124 if (map->mapped == true) {
125 VM_OBJECT_WLOCK(map->mem);
127 for (i = 0; i < map->size; i++) {
128 m = vm_page_lookup(map->mem, i);
131 if (vm_page_sleep_if_busy(m, "pcmdum"))
133 cdev_pager_free_page(map->mem, m);
135 VM_OBJECT_WUNLOCK(map->mem);
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);
141 free(map->err, M_PRIVCMD);
144 error = xenmem_free(privcmd_dev, map->pseudo_phys_res_id,
145 map->pseudo_phys_res);
146 KASSERT(error == 0, ("Unable to release memory resource: %d", error));
148 free(map, M_PRIVCMD);
152 privcmd_pg_fault(vm_object_t object, vm_ooffset_t offset,
153 int prot, vm_page_t *mres)
155 struct privcmd_map *map = object->handle;
157 vm_page_t page, oldm;
159 if (map->mapped != true)
160 return (VM_PAGER_FAIL);
162 pidx = OFF_TO_IDX(offset);
163 if (pidx >= map->size || BIT_ISSET(map->size, pidx, map->err))
164 return (VM_PAGER_FAIL);
166 page = PHYS_TO_VM_PAGE(map->phys_base_addr + offset);
168 return (VM_PAGER_FAIL);
170 KASSERT((page->flags & PG_FICTITIOUS) != 0,
171 ("not fictitious %p", page));
172 KASSERT(page->wire_count == 1, ("wire_count not 1 %p", page));
173 KASSERT(vm_page_busied(page) == 0, ("page %p is busy", page));
179 vm_page_unlock(oldm);
183 vm_page_insert(page, object, pidx);
184 page->valid = VM_PAGE_BITS_ALL;
187 return (VM_PAGER_OK);
190 /*----------------------- Privcmd char device methods ------------------------*/
192 privcmd_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
193 vm_object_t *object, int nprot)
195 struct privcmd_map *map;
197 map = malloc(sizeof(*map), M_PRIVCMD, M_WAITOK | M_ZERO);
199 map->size = OFF_TO_IDX(size);
200 map->pseudo_phys_res_id = 0;
202 map->pseudo_phys_res = xenmem_alloc(privcmd_dev,
203 &map->pseudo_phys_res_id, size);
204 if (map->pseudo_phys_res == NULL) {
205 free(map, M_PRIVCMD);
209 map->phys_base_addr = rman_get_start(map->pseudo_phys_res);
210 map->mem = cdev_pager_allocate(map, OBJT_MGTDEVICE, &privcmd_pg_ops,
211 size, nprot, *offset, NULL);
212 if (map->mem == NULL) {
213 xenmem_free(privcmd_dev, map->pseudo_phys_res_id,
214 map->pseudo_phys_res);
215 free(map, M_PRIVCMD);
225 privcmd_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg,
226 int mode, struct thread *td)
231 case IOCTL_PRIVCMD_HYPERCALL: {
232 struct ioctl_privcmd_hypercall *hcall;
234 hcall = (struct ioctl_privcmd_hypercall *)arg;
236 error = privcmd_hypercall(hcall->op, hcall->arg[0],
237 hcall->arg[1], hcall->arg[2], hcall->arg[3], hcall->arg[4]);
239 hcall->retval = error;
242 error = xen_translate_error(error);
247 case IOCTL_PRIVCMD_MMAPBATCH: {
248 struct ioctl_privcmd_mmapbatch *mmap;
250 vm_map_entry_t entry;
255 struct xen_add_to_physmap_range add;
259 struct privcmd_map *umap;
262 mmap = (struct ioctl_privcmd_mmapbatch *)arg;
264 if ((mmap->num == 0) ||
265 ((mmap->addr & PAGE_MASK) != 0)) {
270 map = &td->td_proc->p_vmspace->vm_map;
271 error = vm_map_lookup(&map, mmap->addr, VM_PROT_NONE, &entry,
272 &mem, &pindex, &prot, &wired);
273 if (error != KERN_SUCCESS) {
277 if ((entry->start != mmap->addr) ||
278 (entry->end != mmap->addr + (mmap->num * PAGE_SIZE))) {
279 vm_map_lookup_done(map, entry);
283 vm_map_lookup_done(map, entry);
284 if ((mem->type != OBJT_MGTDEVICE) ||
285 (mem->un_pager.devp.ops != &privcmd_pg_ops)) {
291 add.domid = DOMID_SELF;
292 add.space = XENMAPSPACE_gmfn_foreign;
293 add.foreign_domid = mmap->dom;
296 * The 'size' field in the xen_add_to_physmap_range only
297 * allows for UINT16_MAX mappings in a single hypercall.
299 num = MIN(mmap->num, UINT16_MAX);
301 idxs = malloc(sizeof(*idxs) * num, M_PRIVCMD, M_WAITOK);
302 gpfns = malloc(sizeof(*gpfns) * num, M_PRIVCMD, M_WAITOK);
303 errs = malloc(sizeof(*errs) * num, M_PRIVCMD, M_WAITOK);
305 set_xen_guest_handle(add.idxs, idxs);
306 set_xen_guest_handle(add.gpfns, gpfns);
307 set_xen_guest_handle(add.errs, errs);
309 /* Allocate a bitset to store broken page mappings. */
310 umap->err = BITSET_ALLOC(mmap->num, M_PRIVCMD,
313 for (index = 0; index < mmap->num; index += num) {
314 num = MIN(mmap->num - index, UINT16_MAX);
317 error = copyin(&mmap->arr[index], idxs,
318 sizeof(idxs[0]) * num);
322 for (i = 0; i < num; i++)
323 gpfns[i] = atop(umap->phys_base_addr +
324 (i + index) * PAGE_SIZE);
326 bzero(errs, sizeof(*errs) * num);
328 error = HYPERVISOR_memory_op(
329 XENMEM_add_to_physmap_range, &add);
331 error = xen_translate_error(error);
335 for (i = 0; i < num; i++) {
337 errs[i] = xen_translate_error(errs[i]);
339 /* Mark the page as invalid. */
340 BIT_SET(mmap->num, index + i,
345 error = copyout(errs, &mmap->err[index],
346 sizeof(errs[0]) * num);
354 free(idxs, M_PRIVCMD);
355 free(gpfns, M_PRIVCMD);
356 free(errs, M_PRIVCMD);
358 free(umap->err, M_PRIVCMD);
371 /*------------------ Private Device Attachment Functions --------------------*/
373 privcmd_identify(driver_t *driver, device_t parent)
376 KASSERT(xen_domain(),
377 ("Trying to attach privcmd device on non Xen domain"));
379 if (BUS_ADD_CHILD(parent, 0, "privcmd", 0) == NULL)
380 panic("unable to attach privcmd user-space device");
384 privcmd_probe(device_t dev)
388 device_set_desc(dev, "Xen privileged interface user-space device");
389 return (BUS_PROBE_NOWILDCARD);
393 privcmd_attach(device_t dev)
396 make_dev_credf(MAKEDEV_ETERNAL, &privcmd_devsw, 0, NULL, UID_ROOT,
397 GID_WHEEL, 0600, "xen/privcmd");
401 /*-------------------- Private Device Attachment Data -----------------------*/
402 static device_method_t privcmd_methods[] = {
403 DEVMETHOD(device_identify, privcmd_identify),
404 DEVMETHOD(device_probe, privcmd_probe),
405 DEVMETHOD(device_attach, privcmd_attach),
410 static driver_t privcmd_driver = {
416 devclass_t privcmd_devclass;
418 DRIVER_MODULE(privcmd, xenpv, privcmd_driver, privcmd_devclass, 0, 0);
419 MODULE_DEPEND(privcmd, xenpv, 1, 1, 1);