Home | History | Annotate | Line # | Download | only in x86
xen_shm_machdep.c revision 1.14
      1 /*      $NetBSD: xen_shm_machdep.c,v 1.14 2020/04/13 00:27:16 chs 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  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: xen_shm_machdep.c,v 1.14 2020/04/13 00:27:16 chs Exp $");
     29 
     30 #include <sys/types.h>
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/queue.h>
     34 #include <sys/vmem.h>
     35 #include <sys/kernel.h>
     36 #include <uvm/uvm.h>
     37 
     38 #include <machine/pmap.h>
     39 #include <xen/hypervisor.h>
     40 #include <xen/xen.h>
     41 #include <xen/evtchn.h>
     42 #include <xen/xen_shm.h>
     43 
     44 /*
     45  * Helper routines for the backend drivers. This implements the necessary
     46  * functions to map a bunch of pages from foreign domains into our kernel VM
     47  * space, do I/O to it, and unmap it.
     48  *
     49  * At boot time, we grab some kernel VM space that we'll use to map the foreign
     50  * pages. We also maintain a virtual-to-machine mapping table to give back
     51  * the appropriate address to bus_dma if requested.
     52  *
     53  * If no more VM space is available, we return an error. The caller can then
     54  * register a callback which will be called when the required VM space is
     55  * available.
     56  */
     57 
     58 /* Grab enough VM space to map an entire vbd ring. */
     59 /* Xen3 linux guests seems to eat more pages, gives enough for 10 vbd rings */
     60 #define BLKIF_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
     61 #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1) * 10)
     62 
     63 /* vm space management */
     64 static vmem_t *xen_shm_arena __read_mostly;
     65 
     66 /* callbacks are registered in a FIFO list. */
     67 static SIMPLEQ_HEAD(xen_shm_callback_head, xen_shm_callback_entry)
     68     xen_shm_callbacks;
     69 
     70 struct xen_shm_callback_entry {
     71 	SIMPLEQ_ENTRY(xen_shm_callback_entry) xshmc_entries;
     72 	int (*xshmc_callback)(void *); /* our callback */
     73 	void *xshmc_arg; /* cookie passed to the callback */
     74 };
     75 
     76 /* a pool of struct xen_shm_callback_entry */
     77 static struct pool xen_shm_callback_pool;
     78 
     79 #ifdef DEBUG
     80 /* for ratecheck(9) */
     81 static struct timeval xen_shm_errintvl = { 60, 0 };  /* a minute, each */
     82 #endif
     83 
     84 void
     85 xen_shm_init(void)
     86 {
     87 	vaddr_t xen_shm_base_address;
     88 	vaddr_t xen_shm_end_address;
     89 	u_long xen_shm_base_address_pg;
     90 	vsize_t xen_shm_size;
     91 
     92 	SIMPLEQ_INIT(&xen_shm_callbacks);
     93 	pool_init(&xen_shm_callback_pool, sizeof(struct xen_shm_callback_entry),
     94 	    0, 0, 0, "xshmc", NULL, IPL_VM);
     95 	/* ensure we'll always get items */
     96 	pool_prime(&xen_shm_callback_pool, 1);
     97 
     98 	xen_shm_size = (XENSHM_NPAGES * PAGE_SIZE);
     99 
    100 	xen_shm_base_address = uvm_km_alloc(kernel_map, xen_shm_size, 0,
    101 	    UVM_KMF_VAONLY);
    102 	xen_shm_end_address = xen_shm_base_address + xen_shm_size;
    103 	xen_shm_base_address_pg = xen_shm_base_address >> PAGE_SHIFT;
    104 	if (xen_shm_base_address == 0) {
    105 		panic("xen_shm_init no VM space");
    106 	}
    107 	xen_shm_arena = vmem_create("xen_shm", xen_shm_base_address_pg,
    108 	    (xen_shm_end_address >> PAGE_SHIFT) - 1 - xen_shm_base_address_pg,
    109 	    1, NULL, NULL, NULL, 1, VM_NOSLEEP, IPL_VM);
    110 	if (xen_shm_arena == NULL) {
    111 		panic("xen_shm_init no arena");
    112 	}
    113 }
    114 
    115 int
    116 xen_shm_map(int nentries, int domid, grant_ref_t *grefp, vaddr_t *vap,
    117     grant_handle_t *handlep, int flags)
    118 {
    119 	gnttab_map_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    120 	vmem_addr_t new_va_pg;
    121 	vaddr_t new_va;
    122 	int ret, i, s;
    123 
    124 #ifdef DIAGNOSTIC
    125 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    126 		panic("xen_shm_map: %d entries", nentries);
    127 	}
    128 #endif
    129 
    130 	/* XXXSMP */
    131 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    132 
    133 	/*
    134 	 * If a driver is waiting for resources, don't try to allocate
    135 	 * yet. This is to avoid a flood of small requests stalling large
    136 	 * ones.
    137 	 */
    138 	if (__predict_false(SIMPLEQ_FIRST(&xen_shm_callbacks) != NULL) &&
    139 	    (flags & XSHM_CALLBACK) == 0) {
    140 		splx(s);
    141 #ifdef DEBUG
    142 		static struct timeval lasttime;
    143 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    144 			printf("xen_shm_map: ENOMEM1\n");
    145 #endif
    146 		return ENOMEM;
    147 	}
    148 
    149 	/* Allocate the needed virtual space. */
    150 	if (vmem_alloc(xen_shm_arena, nentries,
    151 	    VM_INSTANTFIT | VM_NOSLEEP, &new_va_pg) != 0) {
    152 		splx(s);
    153 #ifdef DEBUG
    154 		static struct timeval lasttime;
    155 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    156 			printf("xen_shm_map: ENOMEM\n");
    157 #endif
    158 		return ENOMEM;
    159 	}
    160 	splx(s);
    161 
    162 	new_va = new_va_pg << PAGE_SHIFT;
    163 	for (i = 0; i < nentries; i++) {
    164 		op[i].host_addr = new_va + i * PAGE_SIZE;
    165 		op[i].dom = domid;
    166 		op[i].ref = grefp[i];
    167 		op[i].flags = GNTMAP_host_map |
    168 		    ((flags & XSHM_RO) ? GNTMAP_readonly : 0);
    169 	}
    170 
    171 	ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, op, nentries);
    172 	if (__predict_false(ret)) {
    173 		panic("xen_shm_map: HYPERVISOR_grant_table_op failed");
    174 	}
    175 
    176 	for (i = 0; i < nentries; i++) {
    177 		if (__predict_false(op[i].status))
    178 			return op[i].status;
    179 		handlep[i] = op[i].handle;
    180 	}
    181 
    182 	*vap = new_va;
    183 	return 0;
    184 }
    185 
    186 void
    187 xen_shm_unmap(vaddr_t va, int nentries, grant_handle_t *handlep)
    188 {
    189 	gnttab_unmap_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    190 	struct xen_shm_callback_entry *xshmc;
    191 	int ret, i, s;
    192 
    193 #ifdef DIAGNOSTIC
    194 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    195 		panic("xen_shm_unmap: %d entries", nentries);
    196 	}
    197 #endif
    198 
    199 	for (i = 0; i < nentries; i++) {
    200 		op[i].host_addr = va + i * PAGE_SIZE;
    201 		op[i].dev_bus_addr = 0;
    202 		op[i].handle = handlep[i];
    203 	}
    204 
    205 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
    206 	    op, nentries);
    207 	if (__predict_false(ret)) {
    208 		panic("xen_shm_unmap: unmap failed");
    209 	}
    210 
    211 	va = va >> PAGE_SHIFT;
    212 
    213 	/* XXXSMP */
    214 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    215 
    216 	vmem_free(xen_shm_arena, va, nentries);
    217 	while (__predict_false((xshmc = SIMPLEQ_FIRST(&xen_shm_callbacks))
    218 	    != NULL)) {
    219 		SIMPLEQ_REMOVE_HEAD(&xen_shm_callbacks, xshmc_entries);
    220 		splx(s);
    221 		if (xshmc->xshmc_callback(xshmc->xshmc_arg) == 0) {
    222 			/* callback succeeded */
    223 			s = splvm(); /* XXXSMP */
    224 			pool_put(&xen_shm_callback_pool, xshmc);
    225 		} else {
    226 			/* callback failed, probably out of resources */
    227 			s = splvm(); /* XXXSMP */
    228 			SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc,
    229 			    xshmc_entries);
    230 			break;
    231 		}
    232 	}
    233 
    234 	splx(s);
    235 }
    236 
    237 int
    238 xen_shm_callback(int (*callback)(void *), void *arg)
    239 {
    240 	struct xen_shm_callback_entry *xshmc;
    241 	int s;
    242 
    243 	s = splvm(); /* XXXSMP */
    244 	xshmc = pool_get(&xen_shm_callback_pool, PR_NOWAIT);
    245 	if (xshmc == NULL) {
    246 		splx(s);
    247 		return ENOMEM;
    248 	}
    249 	xshmc->xshmc_arg = arg;
    250 	xshmc->xshmc_callback = callback;
    251 	SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc, xshmc_entries);
    252 	splx(s);
    253 	return 0;
    254 }
    255