Home | History | Annotate | Line # | Download | only in x86
xen_shm_machdep.c revision 1.2.4.2
      1 /*      $NetBSD: xen_shm_machdep.c,v 1.2.4.2 2007/12/03 19:04:45 ad Exp $      */
      2 
      3 /*
      4  * Copyright (c) 2006 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/malloc.h>
     37 #include <sys/queue.h>
     38 #include <sys/vmem.h>
     39 #include <sys/kernel.h>
     40 #include <uvm/uvm.h>
     41 
     42 #include <machine/pmap.h>
     43 #include <xen/hypervisor.h>
     44 #include <xen/xen.h>
     45 #include <xen/evtchn.h>
     46 #include <xen/xen_shm.h>
     47 
     48 /*
     49  * Helper routines for the backend drivers. This implement the necessary
     50  * functions to map a bunch of pages from foreign domains in our kernel VM
     51  * space, do I/O to it, and unmap it.
     52  *
     53  * At boot time, we grap some kernel VM space that we'll use to map the foreign
     54  * pages. We also maintain a virtual to machine mapping table to give back
     55  * the appropriate address to bus_dma if requested.
     56  * If no more VM space is available, we return an error. The caller can then
     57  * register a callback which will be called when the required VM space is
     58  * available.
     59  */
     60 
     61 /* pointers to our VM space */
     62 static vaddr_t xen_shm_base_address;
     63 static u_long xen_shm_base_address_pg;
     64 static vaddr_t xen_shm_end_address;
     65 
     66 /* Grab enouth VM space to map an entire vbd ring. */
     67 #ifdef XEN3
     68 /* Xen3 linux guests seems to eat more pages, gives enough for 10 vbd rings */
     69 #define BLKIF_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
     70 #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1) * 10)
     71 #else
     72 #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1))
     73 #endif
     74 
     75 static vsize_t xen_shm_size = (XENSHM_NPAGES * PAGE_SIZE);
     76 
     77 /* vm space management */
     78 static vmem_t *xen_shm_arena;
     79 
     80 /* callbacks are registered in a FIFO list. */
     81 
     82 static SIMPLEQ_HEAD(xen_shm_callback_head, xen_shm_callback_entry)
     83     xen_shm_callbacks;
     84 struct xen_shm_callback_entry {
     85 	SIMPLEQ_ENTRY(xen_shm_callback_entry) xshmc_entries;
     86 	int (*xshmc_callback)(void *); /* our callback */
     87 	void *xshmc_arg; /* cookie passed to the callback */
     88 };
     89 /* a pool of struct xen_shm_callback_entry */
     90 static struct pool xen_shm_callback_pool;
     91 
     92 #ifdef DEBUG
     93 /* for ratecheck(9) */
     94 static struct timeval xen_shm_errintvl = { 60, 0 };  /* a minute, each */
     95 #endif
     96 
     97 void
     98 xen_shm_init()
     99 {
    100 	SIMPLEQ_INIT(&xen_shm_callbacks);
    101 	pool_init(&xen_shm_callback_pool, sizeof(struct xen_shm_callback_entry),
    102 	    0, 0, 0, "xshmc", NULL, IPL_VM);
    103 	/* ensure we'll always get items */
    104 	if (pool_prime(&xen_shm_callback_pool,
    105 	    PAGE_SIZE / sizeof(struct xen_shm_callback_entry)) != 0) {
    106 		panic("xen_shm_init can't prime pool");
    107 	}
    108 
    109 	xen_shm_base_address = uvm_km_alloc(kernel_map, xen_shm_size, 0,
    110 	    UVM_KMF_VAONLY);
    111 	xen_shm_end_address = xen_shm_base_address + xen_shm_size;
    112 	xen_shm_base_address_pg = xen_shm_base_address >> PAGE_SHIFT;
    113 	if (xen_shm_base_address == 0) {
    114 		panic("xen_shm_init no VM space");
    115 	}
    116 	xen_shm_arena = vmem_create("xen_shm",
    117 	    xen_shm_base_address_pg,
    118 	    (xen_shm_end_address >> PAGE_SHIFT) - 1 - xen_shm_base_address_pg,
    119 	    1, NULL, NULL, NULL, 1, VM_NOSLEEP, IPL_VM);
    120 	if (xen_shm_arena == NULL) {
    121 		panic("xen_shm_init no arena");
    122 	}
    123 }
    124 
    125 int
    126 #ifdef XEN3
    127 xen_shm_map(int nentries, int domid, grant_ref_t *grefp, vaddr_t *vap,
    128     grant_handle_t *handlep, int flags)
    129 #else
    130 xen_shm_map(paddr_t *ma, int nentries, int domid, vaddr_t *vap, int flags)
    131 #endif
    132 {
    133 	int s, i;
    134 	vaddr_t new_va;
    135 	u_long new_va_pg;
    136 #ifdef XEN3
    137 	int err;
    138 	gnttab_map_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    139 #else
    140 	multicall_entry_t mcl[XENSHM_MAX_PAGES_PER_REQUEST];
    141 	int remap_prot = PG_V | PG_RW | PG_U | PG_M;
    142 #endif
    143 
    144 #ifdef DIAGNOSTIC
    145 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    146 		printf("xen_shm_map: %d entries\n", nentries);
    147 		panic("xen_shm_map");
    148 	}
    149 #endif
    150 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    151 	/*
    152 	 * if a driver is waiting for ressources, don't try to allocate
    153 	 * yet. This is to avoid a flood of small requests stalling large
    154 	 * ones.
    155 	 */
    156 	if (__predict_false(SIMPLEQ_FIRST(&xen_shm_callbacks) != NULL) &&
    157 	    (flags & XSHM_CALLBACK) == 0) {
    158 #ifdef DEBUG
    159 		static struct timeval lasttime;
    160 #endif
    161 		splx(s);
    162 #ifdef DEBUG
    163 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    164 			printf("xen_shm_map: ENOMEM1\n");
    165 #endif
    166 		return ENOMEM;
    167 	}
    168 	/* allocate the needed virtual space */
    169 	new_va_pg = vmem_alloc(xen_shm_arena, nentries,
    170 	    VM_INSTANTFIT | VM_NOSLEEP);
    171 	if (new_va_pg == 0) {
    172 #ifdef DEBUG
    173 		static struct timeval lasttime;
    174 #endif
    175 		splx(s);
    176 #ifdef DEBUG
    177 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    178 			printf("xen_shm_map: ENOMEM\n");
    179 #endif
    180 		return ENOMEM;
    181 	}
    182 	splx(s);
    183 
    184 	new_va = new_va_pg << PAGE_SHIFT;
    185 #ifdef XEN3
    186 	for (i = 0; i < nentries; i++) {
    187 		op[i].host_addr = new_va + i * PAGE_SIZE;
    188 		op[i].dom = domid;
    189 		op[i].ref = grefp[i];
    190 		op[i].flags = GNTMAP_host_map |
    191 		    ((flags & XSHM_RO) ? GNTMAP_readonly : 0);
    192 	}
    193 	err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, op, nentries);
    194 	if (__predict_false(err))
    195 		panic("xen_shm_map: HYPERVISOR_grant_table_op failed");
    196 	for (i = 0; i < nentries; i++) {
    197 		if (__predict_false(op[i].status))
    198 			return op[i].status;
    199 		handlep[i] = op[i].handle;
    200 	}
    201 #else /* !XEN3 */
    202 	for (i = 0; i < nentries; i++, new_va_pg++) {
    203 		mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain;
    204 		mcl[i].args[0] = new_va_pg;
    205 		mcl[i].args[1] = ma[i] | remap_prot;
    206 		mcl[i].args[2] = 0;
    207 		mcl[i].args[3] = domid;
    208 	}
    209 	if (HYPERVISOR_multicall(mcl, nentries) != 0)
    210 	    panic("xen_shm_map: HYPERVISOR_multicall");
    211 
    212 	for (i = 0; i < nentries; i++) {
    213 		if ((mcl[i].args[5] != 0)) {
    214 			printf("xen_shm_map: mcl[%d] failed\n", i);
    215 			xen_shm_unmap(new_va, ma, nentries, domid);
    216 			return EINVAL;
    217 		}
    218 	}
    219 #endif /* !XEN3 */
    220 	*vap = new_va;
    221 	return 0;
    222 }
    223 
    224 void
    225 #ifdef XEN3
    226 xen_shm_unmap(vaddr_t va, int nentries, grant_handle_t *handlep)
    227 #else
    228 xen_shm_unmap(vaddr_t va, paddr_t *pa, int nentries, int domid)
    229 #endif
    230 {
    231 #ifdef XEN3
    232 	gnttab_unmap_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    233 	int ret;
    234 #else
    235 	multicall_entry_t mcl[XENSHM_MAX_PAGES_PER_REQUEST];
    236 #endif
    237 	int i;
    238 	int s;
    239 	struct xen_shm_callback_entry *xshmc;
    240 
    241 #ifdef DIAGNOSTIC
    242 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    243 		printf("xen_shm_unmap: %d entries\n", nentries);
    244 		panic("xen_shm_unmap");
    245 	}
    246 #endif
    247 
    248 #ifdef XEN3
    249 	for (i = 0; i < nentries; i++) {
    250 		op[i].host_addr = va + i * PAGE_SIZE;
    251 		op[i].dev_bus_addr = 0;
    252 		op[i].handle = handlep[i];
    253 	}
    254 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
    255 	    op, nentries);
    256 	if (__predict_false(ret))
    257 		panic("xen_shm_unmap: unmap failed");
    258 	va = va >> PAGE_SHIFT;
    259 #else /* !XEN3 */
    260 	va = va >> PAGE_SHIFT;
    261 	for (i = 0; i < nentries; i++) {
    262 		mcl[i].op = __HYPERVISOR_update_va_mapping;
    263 		mcl[i].args[0] = va + i;
    264 		mcl[i].args[1] = 0;
    265 		mcl[i].args[2] = 0;
    266 	}
    267 	mcl[nentries - 1].args[2] = UVMF_FLUSH_TLB;
    268 	if (HYPERVISOR_multicall(mcl, nentries) != 0)
    269 		panic("xen_shm_unmap");
    270 #endif /* !XEN3 */
    271 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    272 	vmem_free(xen_shm_arena, va, nentries);
    273 	while (__predict_false((xshmc = SIMPLEQ_FIRST(&xen_shm_callbacks))
    274 	    != NULL)) {
    275 		SIMPLEQ_REMOVE_HEAD(&xen_shm_callbacks, xshmc_entries);
    276 		splx(s);
    277 		if (xshmc->xshmc_callback(xshmc->xshmc_arg) == 0) {
    278 			/* callback succeeded */
    279 			s = splvm();
    280 			pool_put(&xen_shm_callback_pool, xshmc);
    281 		} else {
    282 			/* callback failed, probably out of ressources */
    283 			s = splvm();
    284 			SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc,
    285 					    xshmc_entries);
    286 
    287 			break;
    288 		}
    289 	}
    290 	splx(s);
    291 }
    292 
    293 int
    294 xen_shm_callback(int (*callback)(void *), void *arg)
    295 {
    296 	struct xen_shm_callback_entry *xshmc;
    297 	int s;
    298 
    299 	s = splvm();
    300 	xshmc = pool_get(&xen_shm_callback_pool, PR_NOWAIT);
    301 	if (xshmc == NULL) {
    302 		splx(s);
    303 		return ENOMEM;
    304 	}
    305 	xshmc->xshmc_arg = arg;
    306 	xshmc->xshmc_callback = callback;
    307 	SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc, xshmc_entries);
    308 	splx(s);
    309 	return 0;
    310 }
    311 /*      $NetBSD: xen_shm_machdep.c,v 1.2.4.2 2007/12/03 19:04:45 ad Exp $      */
    312 
    313 /*
    314  * Copyright (c) 2006 Manuel Bouyer.
    315  *
    316  * Redistribution and use in source and binary forms, with or without
    317  * modification, are permitted provided that the following conditions
    318  * are met:
    319  * 1. Redistributions of source code must retain the above copyright
    320  *    notice, this list of conditions and the following disclaimer.
    321  * 2. Redistributions in binary form must reproduce the above copyright
    322  *    notice, this list of conditions and the following disclaimer in the
    323  *    documentation and/or other materials provided with the distribution.
    324  * 3. All advertising materials mentioning features or use of this software
    325  *    must display the following acknowledgement:
    326  *      This product includes software developed by Manuel Bouyer.
    327  * 4. The name of the author may not be used to endorse or promote products
    328  *    derived from this software without specific prior written permission.
    329  *
    330  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    331  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    332  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    333  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    334  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    335  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    336  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    337  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    338  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    339  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    340  *
    341  */
    342 
    343 #include <sys/types.h>
    344 #include <sys/param.h>
    345 #include <sys/systm.h>
    346 #include <sys/malloc.h>
    347 #include <sys/queue.h>
    348 #include <sys/vmem.h>
    349 #include <sys/kernel.h>
    350 #include <uvm/uvm.h>
    351 
    352 #include <machine/pmap.h>
    353 #include <xen/hypervisor.h>
    354 #include <xen/xen.h>
    355 #include <xen/evtchn.h>
    356 #include <xen/xen_shm.h>
    357 
    358 /*
    359  * Helper routines for the backend drivers. This implement the necessary
    360  * functions to map a bunch of pages from foreign domains in our kernel VM
    361  * space, do I/O to it, and unmap it.
    362  *
    363  * At boot time, we grap some kernel VM space that we'll use to map the foreign
    364  * pages. We also maintain a virtual to machine mapping table to give back
    365  * the appropriate address to bus_dma if requested.
    366  * If no more VM space is available, we return an error. The caller can then
    367  * register a callback which will be called when the required VM space is
    368  * available.
    369  */
    370 
    371 /* pointers to our VM space */
    372 static vaddr_t xen_shm_base_address;
    373 static u_long xen_shm_base_address_pg;
    374 static vaddr_t xen_shm_end_address;
    375 
    376 /* Grab enouth VM space to map an entire vbd ring. */
    377 #ifdef XEN3
    378 /* Xen3 linux guests seems to eat more pages, gives enough for 10 vbd rings */
    379 #define BLKIF_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
    380 #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1) * 10)
    381 #else
    382 #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1))
    383 #endif
    384 
    385 static vsize_t xen_shm_size = (XENSHM_NPAGES * PAGE_SIZE);
    386 
    387 /* vm space management */
    388 static vmem_t *xen_shm_arena;
    389 
    390 /* callbacks are registered in a FIFO list. */
    391 
    392 static SIMPLEQ_HEAD(xen_shm_callback_head, xen_shm_callback_entry)
    393     xen_shm_callbacks;
    394 struct xen_shm_callback_entry {
    395 	SIMPLEQ_ENTRY(xen_shm_callback_entry) xshmc_entries;
    396 	int (*xshmc_callback)(void *); /* our callback */
    397 	void *xshmc_arg; /* cookie passed to the callback */
    398 };
    399 /* a pool of struct xen_shm_callback_entry */
    400 static struct pool xen_shm_callback_pool;
    401 
    402 #ifdef DEBUG
    403 /* for ratecheck(9) */
    404 static struct timeval xen_shm_errintvl = { 60, 0 };  /* a minute, each */
    405 #endif
    406 
    407 void
    408 xen_shm_init()
    409 {
    410 	SIMPLEQ_INIT(&xen_shm_callbacks);
    411 	pool_init(&xen_shm_callback_pool, sizeof(struct xen_shm_callback_entry),
    412 	    0, 0, 0, "xshmc", NULL, IPL_VM);
    413 	/* ensure we'll always get items */
    414 	if (pool_prime(&xen_shm_callback_pool,
    415 	    PAGE_SIZE / sizeof(struct xen_shm_callback_entry)) != 0) {
    416 		panic("xen_shm_init can't prime pool");
    417 	}
    418 
    419 	xen_shm_base_address = uvm_km_alloc(kernel_map, xen_shm_size, 0,
    420 	    UVM_KMF_VAONLY);
    421 	xen_shm_end_address = xen_shm_base_address + xen_shm_size;
    422 	xen_shm_base_address_pg = xen_shm_base_address >> PAGE_SHIFT;
    423 	if (xen_shm_base_address == 0) {
    424 		panic("xen_shm_init no VM space");
    425 	}
    426 	xen_shm_arena = vmem_create("xen_shm",
    427 	    xen_shm_base_address_pg,
    428 	    (xen_shm_end_address >> PAGE_SHIFT) - 1 - xen_shm_base_address_pg,
    429 	    1, NULL, NULL, NULL, 1, VM_NOSLEEP, IPL_VM);
    430 	if (xen_shm_arena == NULL) {
    431 		panic("xen_shm_init no arena");
    432 	}
    433 }
    434 
    435 int
    436 #ifdef XEN3
    437 xen_shm_map(int nentries, int domid, grant_ref_t *grefp, vaddr_t *vap,
    438     grant_handle_t *handlep, int flags)
    439 #else
    440 xen_shm_map(paddr_t *ma, int nentries, int domid, vaddr_t *vap, int flags)
    441 #endif
    442 {
    443 	int s, i;
    444 	vaddr_t new_va;
    445 	u_long new_va_pg;
    446 #ifdef XEN3
    447 	int err;
    448 	gnttab_map_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    449 #else
    450 	multicall_entry_t mcl[XENSHM_MAX_PAGES_PER_REQUEST];
    451 	int remap_prot = PG_V | PG_RW | PG_U | PG_M;
    452 #endif
    453 
    454 #ifdef DIAGNOSTIC
    455 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    456 		printf("xen_shm_map: %d entries\n", nentries);
    457 		panic("xen_shm_map");
    458 	}
    459 #endif
    460 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    461 	/*
    462 	 * if a driver is waiting for ressources, don't try to allocate
    463 	 * yet. This is to avoid a flood of small requests stalling large
    464 	 * ones.
    465 	 */
    466 	if (__predict_false(SIMPLEQ_FIRST(&xen_shm_callbacks) != NULL) &&
    467 	    (flags & XSHM_CALLBACK) == 0) {
    468 #ifdef DEBUG
    469 		static struct timeval lasttime;
    470 #endif
    471 		splx(s);
    472 #ifdef DEBUG
    473 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    474 			printf("xen_shm_map: ENOMEM1\n");
    475 #endif
    476 		return ENOMEM;
    477 	}
    478 	/* allocate the needed virtual space */
    479 	new_va_pg = vmem_alloc(xen_shm_arena, nentries,
    480 	    VM_INSTANTFIT | VM_NOSLEEP);
    481 	if (new_va_pg == 0) {
    482 #ifdef DEBUG
    483 		static struct timeval lasttime;
    484 #endif
    485 		splx(s);
    486 #ifdef DEBUG
    487 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    488 			printf("xen_shm_map: ENOMEM\n");
    489 #endif
    490 		return ENOMEM;
    491 	}
    492 	splx(s);
    493 
    494 	new_va = new_va_pg << PAGE_SHIFT;
    495 #ifdef XEN3
    496 	for (i = 0; i < nentries; i++) {
    497 		op[i].host_addr = new_va + i * PAGE_SIZE;
    498 		op[i].dom = domid;
    499 		op[i].ref = grefp[i];
    500 		op[i].flags = GNTMAP_host_map |
    501 		    ((flags & XSHM_RO) ? GNTMAP_readonly : 0);
    502 	}
    503 	err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, op, nentries);
    504 	if (__predict_false(err))
    505 		panic("xen_shm_map: HYPERVISOR_grant_table_op failed");
    506 	for (i = 0; i < nentries; i++) {
    507 		if (__predict_false(op[i].status))
    508 			return op[i].status;
    509 		handlep[i] = op[i].handle;
    510 	}
    511 #else /* !XEN3 */
    512 	for (i = 0; i < nentries; i++, new_va_pg++) {
    513 		mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain;
    514 		mcl[i].args[0] = new_va_pg;
    515 		mcl[i].args[1] = ma[i] | remap_prot;
    516 		mcl[i].args[2] = 0;
    517 		mcl[i].args[3] = domid;
    518 	}
    519 	if (HYPERVISOR_multicall(mcl, nentries) != 0)
    520 	    panic("xen_shm_map: HYPERVISOR_multicall");
    521 
    522 	for (i = 0; i < nentries; i++) {
    523 		if ((mcl[i].args[5] != 0)) {
    524 			printf("xen_shm_map: mcl[%d] failed\n", i);
    525 			xen_shm_unmap(new_va, ma, nentries, domid);
    526 			return EINVAL;
    527 		}
    528 	}
    529 #endif /* !XEN3 */
    530 	*vap = new_va;
    531 	return 0;
    532 }
    533 
    534 void
    535 #ifdef XEN3
    536 xen_shm_unmap(vaddr_t va, int nentries, grant_handle_t *handlep)
    537 #else
    538 xen_shm_unmap(vaddr_t va, paddr_t *pa, int nentries, int domid)
    539 #endif
    540 {
    541 #ifdef XEN3
    542 	gnttab_unmap_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    543 	int ret;
    544 #else
    545 	multicall_entry_t mcl[XENSHM_MAX_PAGES_PER_REQUEST];
    546 #endif
    547 	int i;
    548 	int s;
    549 	struct xen_shm_callback_entry *xshmc;
    550 
    551 #ifdef DIAGNOSTIC
    552 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    553 		printf("xen_shm_unmap: %d entries\n", nentries);
    554 		panic("xen_shm_unmap");
    555 	}
    556 #endif
    557 
    558 #ifdef XEN3
    559 	for (i = 0; i < nentries; i++) {
    560 		op[i].host_addr = va + i * PAGE_SIZE;
    561 		op[i].dev_bus_addr = 0;
    562 		op[i].handle = handlep[i];
    563 	}
    564 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
    565 	    op, nentries);
    566 	if (__predict_false(ret))
    567 		panic("xen_shm_unmap: unmap failed");
    568 	va = va >> PAGE_SHIFT;
    569 #else /* !XEN3 */
    570 	va = va >> PAGE_SHIFT;
    571 	for (i = 0; i < nentries; i++) {
    572 		mcl[i].op = __HYPERVISOR_update_va_mapping;
    573 		mcl[i].args[0] = va + i;
    574 		mcl[i].args[1] = 0;
    575 		mcl[i].args[2] = 0;
    576 	}
    577 	mcl[nentries - 1].args[2] = UVMF_FLUSH_TLB;
    578 	if (HYPERVISOR_multicall(mcl, nentries) != 0)
    579 		panic("xen_shm_unmap");
    580 #endif /* !XEN3 */
    581 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    582 	vmem_free(xen_shm_arena, va, nentries);
    583 	while (__predict_false((xshmc = SIMPLEQ_FIRST(&xen_shm_callbacks))
    584 	    != NULL)) {
    585 		SIMPLEQ_REMOVE_HEAD(&xen_shm_callbacks, xshmc_entries);
    586 		splx(s);
    587 		if (xshmc->xshmc_callback(xshmc->xshmc_arg) == 0) {
    588 			/* callback succeeded */
    589 			s = splvm();
    590 			pool_put(&xen_shm_callback_pool, xshmc);
    591 		} else {
    592 			/* callback failed, probably out of ressources */
    593 			s = splvm();
    594 			SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc,
    595 					    xshmc_entries);
    596 
    597 			break;
    598 		}
    599 	}
    600 	splx(s);
    601 }
    602 
    603 int
    604 xen_shm_callback(int (*callback)(void *), void *arg)
    605 {
    606 	struct xen_shm_callback_entry *xshmc;
    607 	int s;
    608 
    609 	s = splvm();
    610 	xshmc = pool_get(&xen_shm_callback_pool, PR_NOWAIT);
    611 	if (xshmc == NULL) {
    612 		splx(s);
    613 		return ENOMEM;
    614 	}
    615 	xshmc->xshmc_arg = arg;
    616 	xshmc->xshmc_callback = callback;
    617 	SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc, xshmc_entries);
    618 	splx(s);
    619 	return 0;
    620 }
    621