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