xref: /netbsd-src/sys/arch/xen/xen/xengnt.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*      $NetBSD: xengnt.c,v 1.7 2008/04/27 19:28:45 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  * 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.7 2008/04/27 19:28:45 bouyer 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 static int
121 xengnt_more_entries()
122 {
123 	gnttab_setup_table_t setup;
124 	unsigned long *pages;
125 	int nframes_new = gnt_nr_grant_frames + 1;
126 	int i;
127 
128 	if (gnt_nr_grant_frames == gnt_max_grant_frames)
129 		return ENOMEM;
130 
131 	pages = malloc(nframes_new * sizeof(long), M_DEVBUF, M_NOWAIT);
132 	if (pages == NULL)
133 		return ENOMEM;
134 
135 	setup.dom = DOMID_SELF;
136 	setup.nr_frames = nframes_new;
137 	setup.frame_list = pages;
138 
139 	if (HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1) != 0)
140 		panic("xengnt_more_entries: setup table failed");
141 	if (setup.status != 0) {
142 		printf("xengnt_more_entries: setup table returned %d\n",
143 		    setup.status);
144 		free(pages, M_DEVBUF);
145 		return ENOMEM;
146 	}
147 
148 	DPRINTF(("xengnt_more_entries: map 0x%lx -> %p\n",
149 	    pages[gnt_nr_grant_frames],
150 	    (char *)grant_table + gnt_nr_grant_frames * PAGE_SIZE));
151 
152 	pmap_kenter_ma(((vaddr_t)grant_table) + gnt_nr_grant_frames * PAGE_SIZE,
153 	    pages[gnt_nr_grant_frames] << PAGE_SHIFT, VM_PROT_WRITE);
154 
155 
156 	for (i = gnt_nr_grant_frames * NR_GRANT_ENTRIES_PER_PAGE;
157 	    i < nframes_new * NR_GRANT_ENTRIES_PER_PAGE;
158 	    i++) {
159 		KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
160 		gnt_entries[last_gnt_entry] = i;
161 		last_gnt_entry++;
162 	}
163 	gnt_nr_grant_frames = nframes_new;
164 	free(pages, M_DEVBUF);
165 	return 0;
166 }
167 
168 static grant_ref_t
169 xengnt_get_entry()
170 {
171 	grant_ref_t entry;
172 	int s = splvm();
173 	static struct timeval xengnt_nonmemtime;
174 	const static struct timeval xengnt_nonmemintvl = {5,0};
175 
176 	if (last_gnt_entry == 0) {
177 		if (xengnt_more_entries()) {
178 			splx(s);
179 			if (ratecheck(&xengnt_nonmemtime, &xengnt_nonmemintvl))
180 				printf("xengnt_get_entry: out of grant "
181 				    "table entries\n");
182 			return XENGNT_NO_ENTRY;
183 		}
184 	}
185 	KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
186 	last_gnt_entry--;
187 	entry = gnt_entries[last_gnt_entry];
188 	gnt_entries[last_gnt_entry] = XENGNT_NO_ENTRY;
189 	splx(s);
190 	KASSERT(entry != XENGNT_NO_ENTRY);
191 	KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE);
192 	return entry;
193 }
194 
195 static void
196 xengnt_free_entry(grant_ref_t entry)
197 {
198 	int s = splvm();
199 	KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY);
200 	KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE);
201 	gnt_entries[last_gnt_entry] = entry;
202 	last_gnt_entry++;
203 	splx(s);
204 }
205 
206 int
207 xengnt_grant_access(domid_t dom, paddr_t ma, int ro, grant_ref_t *entryp)
208 {
209 	*entryp = xengnt_get_entry();
210 	if (__predict_false(*entryp == XENGNT_NO_ENTRY))
211 		return ENOMEM;
212 
213 	grant_table[*entryp].frame = ma >> PAGE_SHIFT;
214 	grant_table[*entryp].domid  = dom;
215 	x86_lfence();
216 	grant_table[*entryp].flags =
217 	    GTF_permit_access | (ro ? GTF_readonly : 0);
218 	return 0;
219 }
220 
221 void
222 xengnt_revoke_access(grant_ref_t entry)
223 {
224 	uint16_t flags, nflags;
225 
226 	nflags = grant_table[entry].flags;
227 
228 	do {
229 		if ((flags = nflags) & (GTF_reading|GTF_writing))
230 			panic("xengnt_revoke_access: still in use");
231 		nflags = xen_atomic_cmpxchg16(&grant_table[entry].flags,
232 		    flags, 0);
233 	} while (nflags != flags);
234 	xengnt_free_entry(entry);
235 }
236 
237 int
238 xengnt_grant_transfer(domid_t dom, grant_ref_t *entryp)
239 {
240 	*entryp = xengnt_get_entry();
241 	if (__predict_false(*entryp == XENGNT_NO_ENTRY))
242 		return ENOMEM;
243 
244 	grant_table[*entryp].frame = 0;
245 	grant_table[*entryp].domid  =dom;
246 	x86_lfence();
247 	grant_table[*entryp].flags = GTF_accept_transfer;
248 	return 0;
249 }
250 
251 paddr_t
252 xengnt_revoke_transfer(grant_ref_t entry)
253 {
254 	paddr_t page;
255 	uint16_t flags;
256 
257 	/* if the transfer has not started, free the entry and return 0 */
258 	while (!((flags = grant_table[entry].flags) & GTF_transfer_committed)) {
259 		if (xen_atomic_cmpxchg16(&grant_table[entry].flags,
260 		    flags, 0) == flags ) {
261 			xengnt_free_entry(entry);
262 			return 0;
263 		}
264 		HYPERVISOR_yield();
265 	}
266 
267 	/* If transfer in progress, wait for completion */
268 	while (!((flags = grant_table[entry].flags) & GTF_transfer_completed))
269 		HYPERVISOR_yield();
270 
271 	/* Read the frame number /after/ reading completion status. */
272 	__insn_barrier();
273 	page = grant_table[entry].frame;
274 	if (page == 0)
275 		printf("xengnt_revoke_transfer: guest sent pa 0\n");
276 
277 	xengnt_free_entry(entry);
278 	return page;
279 }
280 
281 int
282 xengnt_status(grant_ref_t entry)
283 {
284 	return (grant_table[entry].flags & (GTF_reading|GTF_writing));
285 }
286