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