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