xref: /netbsd-src/sys/arch/xen/x86/xen_shm_machdep.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /*      $NetBSD: xen_shm_machdep.c,v 1.16 2020/04/25 15:26:17 bouyer 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.16 2020/04/25 15:26:17 bouyer 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 int
59 xen_shm_map(int nentries, int domid, grant_ref_t *grefp, vaddr_t va,
60     grant_handle_t *handlep, int flags)
61 {
62 	gnttab_map_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
63 	int ret, i;
64 
65 #ifdef DIAGNOSTIC
66 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
67 		panic("xen_shm_map: %d entries", nentries);
68 	}
69 #endif
70 
71 	for (i = 0; i < nentries; i++) {
72 		op[i].host_addr = va + i * PAGE_SIZE;
73 		op[i].dom = domid;
74 		op[i].ref = grefp[i];
75 		op[i].flags = GNTMAP_host_map |
76 		    ((flags & XSHM_RO) ? GNTMAP_readonly : 0);
77 	}
78 
79 	ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, op, nentries);
80 	if (__predict_false(ret)) {
81 		printf("%s: HYPERVISOR_grant_table_op failed\n", __func__);
82 		return EINVAL;
83 	}
84 
85 	for (i = 0; i < nentries; i++) {
86 #ifdef DIAGNOSTIC
87 		if (__predict_false(op[i].status))
88 			panic("%s: op[%d] status %d", __func__, i,
89 			    op[i].status);
90 #endif
91 		handlep[i] = op[i].handle;
92 	}
93 
94 	return 0;
95 }
96 
97 void
98 xen_shm_unmap(vaddr_t va, int nentries, grant_handle_t *handlep)
99 {
100 	gnttab_unmap_grant_ref_t op[XENSHM_MAX_PAGES_PER_REQUEST];
101 	int ret, i;
102 
103 #ifdef DIAGNOSTIC
104 	if (nentries > XENSHM_MAX_PAGES_PER_REQUEST) {
105 		panic("xen_shm_unmap: %d entries", nentries);
106 	}
107 #endif
108 
109 	for (i = 0; i < nentries; i++) {
110 		op[i].host_addr = va + i * PAGE_SIZE;
111 		op[i].dev_bus_addr = 0;
112 		op[i].handle = handlep[i];
113 	}
114 
115 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
116 	    op, nentries);
117 	if (__predict_false(ret)) {
118 		panic("xen_shm_unmap: unmap failed");
119 	}
120 }
121