xref: /netbsd-src/sys/arch/xen/xen/xengnt.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*      $NetBSD: xengnt.c,v 1.11 2008/11/13 18:44:51 cegger 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  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Manuel Bouyer.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: xengnt.c,v 1.11 2008/11/13 18:44:51 cegger Exp $");
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/extent.h>
42 #include <sys/kernel.h>
43 #include <uvm/uvm.h>
44 
45 #include <xen/hypervisor.h>
46 #include <xen/xen.h>
47 #include <xen/granttables.h>
48 
49 /* #define XENDEBUG */
50 #ifdef XENDEBUG
51 #define DPRINTF(x) printf x
52 #else
53 #define DPRINTF(x)
54 #endif
55 
56 #define NR_GRANT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(grant_entry_t))
57 
58 int gnt_nr_grant_frames;
59 int gnt_max_grant_frames;
60 
61 /* table of free grant entries */
62 grant_ref_t *gnt_entries;
63 int last_gnt_entry;
64 
65 /* VM address of the grant table */
66 grant_entry_t *grant_table;
67 
68 static grant_ref_t xengnt_get_entry(void);
69 #define XENGNT_NO_ENTRY 0xffffffff
70 static void xengnt_free_entry(grant_ref_t);
71 static void xengnt_resume(void);
72 static int xengnt_more_entries(void);
73 
74 void
75 xengnt_init()
76 {
77 	struct gnttab_query_size query;
78 	int rc;
79 	int nr_grant_entries;
80 	int i;
81 
82 	query.dom = DOMID_SELF;
83 	rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
84 	if ((rc < 0) || (query.status != GNTST_okay))
85 		gnt_max_grant_frames = 4; /* Legacy max number of frames */
86 	else
87 		gnt_max_grant_frames = query.max_nr_frames;
88 	gnt_nr_grant_frames = 0;
89 
90 	nr_grant_entries =
91 	    gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE;
92 
93 	grant_table = (void *)uvm_km_alloc(kernel_map,
94 	    gnt_max_grant_frames * PAGE_SIZE, 0, UVM_KMF_VAONLY);
95 	if (grant_table == NULL)
96 		panic("xengnt_init() no VM space");
97 	gnt_entries = malloc((nr_grant_entries + 1) * sizeof(grant_ref_t),
98 	    M_DEVBUF, M_NOWAIT);
99 	if (gnt_entries == NULL)
100 		panic("xengnt_init() no space for bitmask");
101 	for (i = 0; i <= nr_grant_entries; i++)
102 		gnt_entries[i] = XENGNT_NO_ENTRY;
103 
104 	last_gnt_entry = 0;
105 	xengnt_resume();
106 
107 }
108 
109 static void
110 xengnt_resume()
111 {
112 	int previous_nr_grant_frames = gnt_nr_grant_frames;
113 	gnt_nr_grant_frames = 0;
114 	while (gnt_nr_grant_frames < previous_nr_grant_frames) {
115 		if (xengnt_more_entries() != 0)
116 			panic("xengnt_resume: can't restore grant frames");
117 	}
118 }
119 
120 /*
121  * Add another page to the grant table
122  * Returns 0 on success, ENOMEM on failure
123  */
124 static int
125 xengnt_more_entries(void)
126 {
127 	gnttab_setup_table_t setup;
128 	unsigned long *pages;
129 	int nframes_new = gnt_nr_grant_frames + 1;
130 	int i;
131 
132 	if (gnt_nr_grant_frames == gnt_max_grant_frames)
133 		return ENOMEM;
134 
135 	pages = malloc(nframes_new * sizeof(long), M_DEVBUF, M_NOWAIT);
136 	if (pages == NULL)
137 		return ENOMEM;
138 
139 	setup.dom = DOMID_SELF;
140 	setup.nr_frames = nframes_new;
141 	xenguest_handle(setup.frame_list) = pages;
142 
143 	/*
144 	 * setup the grant table, made of nframes_new frames
145 	 * and return the list of their virtual addresses
146 	 * in 'pages'
147 	 */
148 	if (HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1) != 0)
149 		panic("%s: setup table failed", __func__);
150 	if (setup.status != GNTST_okay) {
151 		aprint_error("%s: setup table returned %d\n",
152 		    __func__, setup.status);
153 		free(pages, M_DEVBUF);
154 		return ENOMEM;
155 	}
156 
157 	DPRINTF(("xengnt_more_entries: map 0x%lx -> %p\n",
158 	    pages[gnt_nr_grant_frames],
159 	    (char *)grant_table + gnt_nr_grant_frames * PAGE_SIZE));
160 
161 	/*
162 	 * map between grant_table addresses and the machine addresses of
163 	 * the grant table frames
164 	 */
165 	pmap_kenter_ma(((vaddr_t)grant_table) + gnt_nr_grant_frames * PAGE_SIZE,
166 	    pages[gnt_nr_grant_frames] << PAGE_SHIFT, VM_PROT_WRITE);
167 
168 	/*
169 	 * add the grant entries associated to the last grant table frame
170 	 * and mark them as free
171 	 */
172 	for (i = gnt_nr_grant_frames * NR_GRANT_ENTRIES_PER_PAGE;
173 	    i < nframes_new * NR_GRANT_ENTRIES_PER_PAGE;
174 	    i++) {
175 		KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
176 		gnt_entries[last_gnt_entry] = i;
177 		last_gnt_entry++;
178 	}
179 	gnt_nr_grant_frames = nframes_new;
180 	free(pages, M_DEVBUF);
181 	return 0;
182 }
183 
184 /*
185  * Returns a reference to the first free entry in grant table
186  */
187 static grant_ref_t
188 xengnt_get_entry()
189 {
190 	grant_ref_t entry;
191 	int s = splvm();
192 	static struct timeval xengnt_nonmemtime;
193 	static const struct timeval xengnt_nonmemintvl = {5,0};
194 
195 	if (last_gnt_entry == 0) {
196 		if (xengnt_more_entries()) {
197 			splx(s);
198 			if (ratecheck(&xengnt_nonmemtime, &xengnt_nonmemintvl))
199 				printf("xengnt_get_entry: out of grant "
200 				    "table entries\n");
201 			return XENGNT_NO_ENTRY;
202 		}
203 	}
204 	KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
205 	last_gnt_entry--;
206 	entry = gnt_entries[last_gnt_entry];
207 	gnt_entries[last_gnt_entry] = XENGNT_NO_ENTRY;
208 	splx(s);
209 	KASSERT(entry != XENGNT_NO_ENTRY);
210 	KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE);
211 	return entry;
212 }
213 
214 /*
215  * Mark the grant table entry as free
216  */
217 static void
218 xengnt_free_entry(grant_ref_t entry)
219 {
220 	int s = splvm();
221 	KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
222 	KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE);
223 	gnt_entries[last_gnt_entry] = entry;
224 	last_gnt_entry++;
225 	splx(s);
226 }
227 
228 int
229 xengnt_grant_access(domid_t dom, paddr_t ma, int ro, grant_ref_t *entryp)
230 {
231 	*entryp = xengnt_get_entry();
232 	if (__predict_false(*entryp == XENGNT_NO_ENTRY))
233 		return ENOMEM;
234 
235 	grant_table[*entryp].frame = ma >> PAGE_SHIFT;
236 	grant_table[*entryp].domid  = dom;
237 	x86_lfence();
238 	grant_table[*entryp].flags =
239 	    GTF_permit_access | (ro ? GTF_readonly : 0);
240 	return 0;
241 }
242 
243 void
244 xengnt_revoke_access(grant_ref_t entry)
245 {
246 	uint16_t flags, nflags;
247 
248 	nflags = grant_table[entry].flags;
249 
250 	do {
251 		if ((flags = nflags) & (GTF_reading|GTF_writing))
252 			panic("xengnt_revoke_access: still in use");
253 		nflags = xen_atomic_cmpxchg16(&grant_table[entry].flags,
254 		    flags, 0);
255 	} while (nflags != flags);
256 	xengnt_free_entry(entry);
257 }
258 
259 int
260 xengnt_grant_transfer(domid_t dom, grant_ref_t *entryp)
261 {
262 	*entryp = xengnt_get_entry();
263 	if (__predict_false(*entryp == XENGNT_NO_ENTRY))
264 		return ENOMEM;
265 
266 	grant_table[*entryp].frame = 0;
267 	grant_table[*entryp].domid  =dom;
268 	x86_lfence();
269 	grant_table[*entryp].flags = GTF_accept_transfer;
270 	return 0;
271 }
272 
273 paddr_t
274 xengnt_revoke_transfer(grant_ref_t entry)
275 {
276 	paddr_t page;
277 	uint16_t flags;
278 
279 	/* if the transfer has not started, free the entry and return 0 */
280 	while (!((flags = grant_table[entry].flags) & GTF_transfer_committed)) {
281 		if (xen_atomic_cmpxchg16(&grant_table[entry].flags,
282 		    flags, 0) == flags ) {
283 			xengnt_free_entry(entry);
284 			return 0;
285 		}
286 		HYPERVISOR_yield();
287 	}
288 
289 	/* If transfer in progress, wait for completion */
290 	while (!((flags = grant_table[entry].flags) & GTF_transfer_completed))
291 		HYPERVISOR_yield();
292 
293 	/* Read the frame number /after/ reading completion status. */
294 	__insn_barrier();
295 	page = grant_table[entry].frame;
296 	if (page == 0)
297 		printf("xengnt_revoke_transfer: guest sent pa 0\n");
298 
299 	xengnt_free_entry(entry);
300 	return page;
301 }
302 
303 int
304 xengnt_status(grant_ref_t entry)
305 {
306 	return (grant_table[entry].flags & (GTF_reading|GTF_writing));
307 }
308