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