Home | History | Annotate | Line # | Download | only in x86
xen_shm_machdep.c revision 1.11
      1  1.11  jdolecek /*      $NetBSD: xen_shm_machdep.c,v 1.11 2018/06/24 20:28:57 jdolecek 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.2    bouyer 
     28   1.3    bouyer #include <sys/cdefs.h>
     29  1.11  jdolecek __KERNEL_RCSID(0, "$NetBSD: xen_shm_machdep.c,v 1.11 2018/06/24 20:28:57 jdolecek Exp $");
     30   1.3    bouyer 
     31   1.3    bouyer 
     32   1.2    bouyer #include <sys/types.h>
     33   1.2    bouyer #include <sys/param.h>
     34   1.2    bouyer #include <sys/systm.h>
     35   1.2    bouyer #include <sys/queue.h>
     36   1.2    bouyer #include <sys/vmem.h>
     37   1.2    bouyer #include <sys/kernel.h>
     38   1.2    bouyer #include <uvm/uvm.h>
     39   1.2    bouyer 
     40   1.2    bouyer #include <machine/pmap.h>
     41   1.2    bouyer #include <xen/hypervisor.h>
     42   1.2    bouyer #include <xen/xen.h>
     43   1.2    bouyer #include <xen/evtchn.h>
     44   1.2    bouyer #include <xen/xen_shm.h>
     45   1.2    bouyer 
     46   1.2    bouyer /*
     47   1.2    bouyer  * Helper routines for the backend drivers. This implement the necessary
     48   1.2    bouyer  * functions to map a bunch of pages from foreign domains in our kernel VM
     49   1.2    bouyer  * space, do I/O to it, and unmap it.
     50   1.2    bouyer  *
     51   1.9       jym  * At boot time, we grab some kernel VM space that we'll use to map the foreign
     52   1.2    bouyer  * pages. We also maintain a virtual to machine mapping table to give back
     53   1.2    bouyer  * the appropriate address to bus_dma if requested.
     54   1.2    bouyer  * If no more VM space is available, we return an error. The caller can then
     55   1.2    bouyer  * register a callback which will be called when the required VM space is
     56   1.2    bouyer  * available.
     57   1.2    bouyer  */
     58   1.2    bouyer 
     59   1.2    bouyer /* pointers to our VM space */
     60   1.2    bouyer static vaddr_t xen_shm_base_address;
     61   1.2    bouyer static u_long xen_shm_base_address_pg;
     62   1.2    bouyer static vaddr_t xen_shm_end_address;
     63   1.2    bouyer 
     64   1.8       snj /* Grab enough VM space to map an entire vbd ring. */
     65   1.2    bouyer /* Xen3 linux guests seems to eat more pages, gives enough for 10 vbd rings */
     66   1.2    bouyer #define BLKIF_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
     67   1.2    bouyer #define XENSHM_NPAGES (BLKIF_RING_SIZE * (BLKIF_MAX_SEGMENTS_PER_REQUEST + 1) * 10)
     68   1.2    bouyer 
     69   1.2    bouyer static vsize_t xen_shm_size = (XENSHM_NPAGES * PAGE_SIZE);
     70   1.2    bouyer 
     71   1.2    bouyer /* vm space management */
     72   1.2    bouyer static vmem_t *xen_shm_arena;
     73   1.2    bouyer 
     74   1.2    bouyer /* callbacks are registered in a FIFO list. */
     75   1.2    bouyer 
     76   1.2    bouyer static SIMPLEQ_HEAD(xen_shm_callback_head, xen_shm_callback_entry)
     77   1.2    bouyer     xen_shm_callbacks;
     78   1.2    bouyer struct xen_shm_callback_entry {
     79   1.2    bouyer 	SIMPLEQ_ENTRY(xen_shm_callback_entry) xshmc_entries;
     80   1.2    bouyer 	int (*xshmc_callback)(void *); /* our callback */
     81   1.2    bouyer 	void *xshmc_arg; /* cookie passed to the callback */
     82   1.2    bouyer };
     83   1.2    bouyer /* a pool of struct xen_shm_callback_entry */
     84   1.2    bouyer static struct pool xen_shm_callback_pool;
     85   1.2    bouyer 
     86   1.2    bouyer #ifdef DEBUG
     87   1.2    bouyer /* for ratecheck(9) */
     88   1.2    bouyer static struct timeval xen_shm_errintvl = { 60, 0 };  /* a minute, each */
     89   1.2    bouyer #endif
     90   1.2    bouyer 
     91   1.2    bouyer void
     92   1.5    cegger xen_shm_init(void)
     93   1.2    bouyer {
     94   1.2    bouyer 	SIMPLEQ_INIT(&xen_shm_callbacks);
     95   1.2    bouyer 	pool_init(&xen_shm_callback_pool, sizeof(struct xen_shm_callback_entry),
     96   1.2    bouyer 	    0, 0, 0, "xshmc", NULL, IPL_VM);
     97   1.2    bouyer 	/* ensure we'll always get items */
     98   1.2    bouyer 	if (pool_prime(&xen_shm_callback_pool,
     99   1.2    bouyer 	    PAGE_SIZE / sizeof(struct xen_shm_callback_entry)) != 0) {
    100   1.2    bouyer 		panic("xen_shm_init can't prime pool");
    101   1.2    bouyer 	}
    102   1.2    bouyer 
    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.2    bouyer 	xen_shm_arena = vmem_create("xen_shm",
    111   1.2    bouyer 	    xen_shm_base_address_pg,
    112   1.2    bouyer 	    (xen_shm_end_address >> PAGE_SHIFT) - 1 - xen_shm_base_address_pg,
    113   1.2    bouyer 	    1, NULL, NULL, NULL, 1, VM_NOSLEEP, IPL_VM);
    114   1.2    bouyer 	if (xen_shm_arena == NULL) {
    115   1.2    bouyer 		panic("xen_shm_init no arena");
    116   1.2    bouyer 	}
    117   1.2    bouyer }
    118   1.2    bouyer 
    119   1.2    bouyer int
    120   1.2    bouyer xen_shm_map(int nentries, int domid, grant_ref_t *grefp, vaddr_t *vap,
    121   1.2    bouyer     grant_handle_t *handlep, int flags)
    122   1.2    bouyer {
    123   1.2    bouyer 	int s, i;
    124   1.2    bouyer 	vaddr_t new_va;
    125  1.10    dyoung 	vmem_addr_t new_va_pg;
    126   1.2    bouyer 	int err;
    127   1.2    bouyer 	gnttab_map_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
    128   1.2    bouyer 
    129   1.2    bouyer #ifdef DIAGNOSTIC
    130   1.2    bouyer 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    131   1.2    bouyer 		printf("xen_shm_map: %d entries\n", nentries);
    132   1.2    bouyer 		panic("xen_shm_map");
    133   1.2    bouyer 	}
    134   1.2    bouyer #endif
    135  1.11  jdolecek 	/* XXXSMP */
    136   1.2    bouyer 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    137   1.2    bouyer 	/*
    138   1.2    bouyer 	 * if a driver is waiting for ressources, don't try to allocate
    139   1.2    bouyer 	 * yet. This is to avoid a flood of small requests stalling large
    140   1.2    bouyer 	 * ones.
    141   1.2    bouyer 	 */
    142   1.2    bouyer 	if (__predict_false(SIMPLEQ_FIRST(&xen_shm_callbacks) != NULL) &&
    143   1.2    bouyer 	    (flags & XSHM_CALLBACK) == 0) {
    144   1.2    bouyer #ifdef DEBUG
    145   1.2    bouyer 		static struct timeval lasttime;
    146   1.2    bouyer #endif
    147   1.2    bouyer 		splx(s);
    148   1.2    bouyer #ifdef DEBUG
    149   1.2    bouyer 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    150   1.2    bouyer 			printf("xen_shm_map: ENOMEM1\n");
    151   1.2    bouyer #endif
    152   1.2    bouyer 		return ENOMEM;
    153   1.2    bouyer 	}
    154   1.2    bouyer 	/* allocate the needed virtual space */
    155  1.10    dyoung 	if (vmem_alloc(xen_shm_arena, nentries,
    156  1.10    dyoung 	    VM_INSTANTFIT | VM_NOSLEEP, &new_va_pg) != 0) {
    157   1.2    bouyer #ifdef DEBUG
    158   1.2    bouyer 		static struct timeval lasttime;
    159   1.2    bouyer #endif
    160   1.2    bouyer 		splx(s);
    161   1.2    bouyer #ifdef DEBUG
    162   1.2    bouyer 		if (ratecheck(&lasttime, &xen_shm_errintvl))
    163   1.2    bouyer 			printf("xen_shm_map: ENOMEM\n");
    164   1.2    bouyer #endif
    165   1.2    bouyer 		return ENOMEM;
    166   1.2    bouyer 	}
    167   1.2    bouyer 	splx(s);
    168   1.2    bouyer 
    169   1.2    bouyer 	new_va = new_va_pg << PAGE_SHIFT;
    170   1.2    bouyer 	for (i = 0; i < nentries; i++) {
    171   1.2    bouyer 		op[i].host_addr = new_va + i * PAGE_SIZE;
    172   1.2    bouyer 		op[i].dom = domid;
    173   1.2    bouyer 		op[i].ref = grefp[i];
    174   1.2    bouyer 		op[i].flags = GNTMAP_host_map |
    175   1.2    bouyer 		    ((flags & XSHM_RO) ? GNTMAP_readonly : 0);
    176   1.2    bouyer 	}
    177   1.2    bouyer 	err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, op, nentries);
    178   1.2    bouyer 	if (__predict_false(err))
    179   1.2    bouyer 		panic("xen_shm_map: HYPERVISOR_grant_table_op failed");
    180   1.2    bouyer 	for (i = 0; i < nentries; i++) {
    181   1.2    bouyer 		if (__predict_false(op[i].status))
    182   1.2    bouyer 			return op[i].status;
    183   1.2    bouyer 		handlep[i] = op[i].handle;
    184   1.2    bouyer 	}
    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 	int ret;
    194   1.2    bouyer 	int i;
    195   1.2    bouyer 	int s;
    196   1.2    bouyer 	struct xen_shm_callback_entry *xshmc;
    197   1.2    bouyer 
    198   1.2    bouyer #ifdef DIAGNOSTIC
    199   1.2    bouyer 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
    200   1.2    bouyer 		printf("xen_shm_unmap: %d entries\n", nentries);
    201   1.2    bouyer 		panic("xen_shm_unmap");
    202   1.2    bouyer 	}
    203   1.2    bouyer #endif
    204   1.2    bouyer 
    205   1.2    bouyer 	for (i = 0; i < nentries; i++) {
    206   1.2    bouyer 		op[i].host_addr = va + i * PAGE_SIZE;
    207   1.2    bouyer 		op[i].dev_bus_addr = 0;
    208   1.2    bouyer 		op[i].handle = handlep[i];
    209   1.2    bouyer 	}
    210   1.2    bouyer 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
    211   1.2    bouyer 	    op, nentries);
    212   1.2    bouyer 	if (__predict_false(ret))
    213   1.2    bouyer 		panic("xen_shm_unmap: unmap failed");
    214   1.2    bouyer 	va = va >> PAGE_SHIFT;
    215  1.11  jdolecek 	/* XXXSMP */
    216   1.2    bouyer 	s = splvm(); /* splvm is the lowest level blocking disk and net IRQ */
    217   1.2    bouyer 	vmem_free(xen_shm_arena, va, nentries);
    218   1.2    bouyer 	while (__predict_false((xshmc = SIMPLEQ_FIRST(&xen_shm_callbacks))
    219   1.2    bouyer 	    != NULL)) {
    220   1.2    bouyer 		SIMPLEQ_REMOVE_HEAD(&xen_shm_callbacks, xshmc_entries);
    221   1.2    bouyer 		splx(s);
    222   1.2    bouyer 		if (xshmc->xshmc_callback(xshmc->xshmc_arg) == 0) {
    223   1.2    bouyer 			/* callback succeeded */
    224  1.11  jdolecek 			s = splvm(); /* XXXSMP */
    225   1.2    bouyer 			pool_put(&xen_shm_callback_pool, xshmc);
    226   1.2    bouyer 		} else {
    227   1.2    bouyer 			/* callback failed, probably out of ressources */
    228  1.11  jdolecek 			s = splvm(); /* XXXSMP */
    229   1.2    bouyer 			SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc,
    230   1.2    bouyer 					    xshmc_entries);
    231   1.2    bouyer 
    232   1.2    bouyer 			break;
    233   1.2    bouyer 		}
    234   1.2    bouyer 	}
    235   1.2    bouyer 	splx(s);
    236   1.2    bouyer }
    237   1.2    bouyer 
    238   1.2    bouyer int
    239   1.2    bouyer xen_shm_callback(int (*callback)(void *), void *arg)
    240   1.2    bouyer {
    241   1.2    bouyer 	struct xen_shm_callback_entry *xshmc;
    242   1.2    bouyer 	int s;
    243   1.2    bouyer 
    244  1.11  jdolecek 	s = splvm(); /* XXXSMP */
    245   1.2    bouyer 	xshmc = pool_get(&xen_shm_callback_pool, PR_NOWAIT);
    246   1.2    bouyer 	if (xshmc == NULL) {
    247   1.2    bouyer 		splx(s);
    248   1.2    bouyer 		return ENOMEM;
    249   1.2    bouyer 	}
    250   1.2    bouyer 	xshmc->xshmc_arg = arg;
    251   1.2    bouyer 	xshmc->xshmc_callback = callback;
    252   1.2    bouyer 	SIMPLEQ_INSERT_TAIL(&xen_shm_callbacks, xshmc, xshmc_entries);
    253   1.2    bouyer 	splx(s);
    254   1.2    bouyer 	return 0;
    255   1.2    bouyer }
    256