xref: /netbsd-src/sys/arch/xen/xen/xengnt.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*      $NetBSD: xengnt.c,v 1.16 2009/11/07 07:27:49 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  *
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 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: xengnt.c,v 1.16 2009/11/07 07:27:49 cegger Exp $");
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/queue.h>
36 #include <sys/extent.h>
37 #include <sys/kernel.h>
38 #include <uvm/uvm.h>
39 
40 #include <xen/hypervisor.h>
41 #include <xen/xen.h>
42 #include <xen/granttables.h>
43 
44 /* #define XENDEBUG */
45 #ifdef XENDEBUG
46 #define DPRINTF(x) printf x
47 #else
48 #define DPRINTF(x)
49 #endif
50 
51 #define NR_GRANT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(grant_entry_t))
52 
53 /* Current number of frames making up the grant table */
54 int gnt_nr_grant_frames;
55 /* Maximum number of frames that can make up the grant table */
56 int gnt_max_grant_frames;
57 
58 /* table of free grant entries */
59 grant_ref_t *gnt_entries;
60 /* last free entry */
61 int last_gnt_entry;
62 /* empty entry in the list */
63 #define XENGNT_NO_ENTRY 0xffffffff
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 static void xengnt_free_entry(grant_ref_t);
70 static void xengnt_resume(void);
71 static int xengnt_more_entries(void);
72 
73 void
74 xengnt_init(void)
75 {
76 	struct gnttab_query_size query;
77 	int rc;
78 	int nr_grant_entries;
79 	int i;
80 
81 	query.dom = DOMID_SELF;
82 	rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
83 	if ((rc < 0) || (query.status != GNTST_okay))
84 		gnt_max_grant_frames = 4; /* Legacy max number of frames */
85 	else
86 		gnt_max_grant_frames = query.max_nr_frames;
87 	gnt_nr_grant_frames = 0;
88 
89 	nr_grant_entries =
90 	    gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE;
91 
92 	grant_table = (void *)uvm_km_alloc(kernel_map,
93 	    gnt_max_grant_frames * PAGE_SIZE, 0, UVM_KMF_VAONLY);
94 	if (grant_table == NULL)
95 		panic("xengnt_init() no VM space");
96 	gnt_entries = malloc((nr_grant_entries + 1) * sizeof(grant_ref_t),
97 	    M_DEVBUF, M_NOWAIT);
98 	if (gnt_entries == NULL)
99 		panic("xengnt_init() no space for bitmask");
100 	for (i = 0; i <= nr_grant_entries; i++)
101 		gnt_entries[i] = XENGNT_NO_ENTRY;
102 
103 	last_gnt_entry = 0;
104 	xengnt_resume();
105 
106 }
107 
108 /*
109  * Resume grant table state
110  */
111 static void
112 xengnt_resume(void)
113 {
114 	int previous_nr_grant_frames = gnt_nr_grant_frames;
115 	gnt_nr_grant_frames = 0;
116 	while (gnt_nr_grant_frames < previous_nr_grant_frames) {
117 		if (xengnt_more_entries() != 0)
118 			panic("xengnt_resume: can't restore grant frames");
119 	}
120 }
121 
122 /*
123  * Add another page to the grant table
124  * Returns 0 on success, ENOMEM on failure
125  */
126 static int
127 xengnt_more_entries(void)
128 {
129 	gnttab_setup_table_t setup;
130 	unsigned long *pages;
131 	int nframes_new = gnt_nr_grant_frames + 1;
132 	int i;
133 
134 	if (gnt_nr_grant_frames == gnt_max_grant_frames)
135 		return ENOMEM;
136 
137 	pages = malloc(nframes_new * sizeof(long), M_DEVBUF, M_NOWAIT);
138 	if (pages == NULL)
139 		return ENOMEM;
140 
141 	setup.dom = DOMID_SELF;
142 	setup.nr_frames = nframes_new;
143 	xenguest_handle(setup.frame_list) = pages;
144 
145 	/*
146 	 * setup the grant table, made of nframes_new frames
147 	 * and return the list of their virtual addresses
148 	 * in 'pages'
149 	 */
150 	if (HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1) != 0)
151 		panic("%s: setup table failed", __func__);
152 	if (setup.status != GNTST_okay) {
153 		aprint_error("%s: setup table returned %d\n",
154 		    __func__, setup.status);
155 		free(pages, M_DEVBUF);
156 		return ENOMEM;
157 	}
158 
159 	DPRINTF(("xengnt_more_entries: map 0x%lx -> %p\n",
160 	    pages[gnt_nr_grant_frames],
161 	    (char *)grant_table + gnt_nr_grant_frames * PAGE_SIZE));
162 
163 	/*
164 	 * map between grant_table addresses and the machine addresses of
165 	 * the grant table frames
166 	 */
167 	pmap_kenter_ma(((vaddr_t)grant_table) + gnt_nr_grant_frames * PAGE_SIZE,
168 	    pages[gnt_nr_grant_frames] << PAGE_SHIFT, VM_PROT_WRITE, 0);
169 
170 	/*
171 	 * add the grant entries associated to the last grant table frame
172 	 * and mark them as free
173 	 */
174 	for (i = gnt_nr_grant_frames * NR_GRANT_ENTRIES_PER_PAGE;
175 	    i < nframes_new * NR_GRANT_ENTRIES_PER_PAGE;
176 	    i++) {
177 		KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
178 		gnt_entries[last_gnt_entry] = i;
179 		last_gnt_entry++;
180 	}
181 	gnt_nr_grant_frames = nframes_new;
182 	free(pages, M_DEVBUF);
183 	return 0;
184 }
185 
186 /*
187  * Returns a reference to the first free entry in grant table
188  */
189 static grant_ref_t
190 xengnt_get_entry(void)
191 {
192 	grant_ref_t entry;
193 	int s = splvm();
194 	static struct timeval xengnt_nonmemtime;
195 	static const struct timeval xengnt_nonmemintvl = {5,0};
196 
197 	if (last_gnt_entry == 0) {
198 		if (xengnt_more_entries()) {
199 			splx(s);
200 			if (ratecheck(&xengnt_nonmemtime, &xengnt_nonmemintvl))
201 				printf("xengnt_get_entry: out of grant "
202 				    "table entries\n");
203 			return XENGNT_NO_ENTRY;
204 		}
205 	}
206 	KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
207 	last_gnt_entry--;
208 	entry = gnt_entries[last_gnt_entry];
209 	gnt_entries[last_gnt_entry] = XENGNT_NO_ENTRY;
210 	splx(s);
211 	KASSERT(entry != XENGNT_NO_ENTRY);
212 	KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE);
213 	return entry;
214 }
215 
216 /*
217  * Mark the grant table entry as free
218  */
219 static void
220 xengnt_free_entry(grant_ref_t entry)
221 {
222 	int s = splvm();
223 	KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
224 	KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE);
225 	gnt_entries[last_gnt_entry] = entry;
226 	last_gnt_entry++;
227 	splx(s);
228 }
229 
230 int
231 xengnt_grant_access(domid_t dom, paddr_t ma, int ro, grant_ref_t *entryp)
232 {
233 	*entryp = xengnt_get_entry();
234 	if (__predict_false(*entryp == XENGNT_NO_ENTRY))
235 		return ENOMEM;
236 
237 	grant_table[*entryp].frame = ma >> PAGE_SHIFT;
238 	grant_table[*entryp].domid = dom;
239 	/*
240 	 * ensure that the above values reach global visibility
241 	 * before permitting frame's access (done when we set flags)
242 	 */
243 	xen_rmb();
244 	grant_table[*entryp].flags =
245 	    GTF_permit_access | (ro ? GTF_readonly : 0);
246 	return 0;
247 }
248 
249 void
250 xengnt_revoke_access(grant_ref_t entry)
251 {
252 	uint16_t flags, nflags;
253 
254 	nflags = grant_table[entry].flags;
255 
256 	do {
257 		if ((flags = nflags) & (GTF_reading|GTF_writing))
258 			panic("xengnt_revoke_access: still in use");
259 		nflags = xen_atomic_cmpxchg16(&grant_table[entry].flags,
260 		    flags, 0);
261 	} while (nflags != flags);
262 	xengnt_free_entry(entry);
263 }
264 
265 int
266 xengnt_grant_transfer(domid_t dom, grant_ref_t *entryp)
267 {
268 	*entryp = xengnt_get_entry();
269 	if (__predict_false(*entryp == XENGNT_NO_ENTRY))
270 		return ENOMEM;
271 
272 	grant_table[*entryp].frame = 0;
273 	grant_table[*entryp].domid = dom;
274 	/*
275 	 * ensure that the above values reach global visibility
276 	 * before permitting frame's transfer (done when we set flags)
277 	 */
278 	xen_rmb();
279 	grant_table[*entryp].flags = GTF_accept_transfer;
280 	return 0;
281 }
282 
283 paddr_t
284 xengnt_revoke_transfer(grant_ref_t entry)
285 {
286 	paddr_t page;
287 	uint16_t flags;
288 
289 	/* if the transfer has not started, free the entry and return 0 */
290 	while (!((flags = grant_table[entry].flags) & GTF_transfer_committed)) {
291 		if (xen_atomic_cmpxchg16(&grant_table[entry].flags,
292 		    flags, 0) == flags ) {
293 			xengnt_free_entry(entry);
294 			return 0;
295 		}
296 		HYPERVISOR_yield();
297 	}
298 
299 	/* If transfer in progress, wait for completion */
300 	while (!((flags = grant_table[entry].flags) & GTF_transfer_completed))
301 		HYPERVISOR_yield();
302 
303 	/* Read the frame number /after/ reading completion status. */
304 	__insn_barrier();
305 	page = grant_table[entry].frame;
306 	if (page == 0)
307 		printf("xengnt_revoke_transfer: guest sent pa 0\n");
308 
309 	xengnt_free_entry(entry);
310 	return page;
311 }
312 
313 int
314 xengnt_status(grant_ref_t entry)
315 {
316 	return (grant_table[entry].flags & (GTF_reading|GTF_writing));
317 }
318