hypervisor_machdep.c revision 1.5 1 /* $NetBSD: hypervisor_machdep.c,v 1.5 2008/02/19 13:25:53 bouyer Exp $ */
2
3 /*
4 *
5 * Copyright (c) 2004 Christian Limpach.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christian Limpach.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /******************************************************************************
35 * hypervisor.c
36 *
37 * Communication to/from hypervisor.
38 *
39 * Copyright (c) 2002-2004, K A Fraser
40 *
41 * Permission is hereby granted, free of charge, to any person obtaining a copy
42 * of this software and associated documentation files (the "Software"), to
43 * deal in the Software without restriction, including without limitation the
44 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
45 * sell copies of the Software, and to permit persons to whom the Software is
46 * furnished to do so, subject to the following conditions:
47 *
48 * The above copyright notice and this permission notice shall be included in
49 * all copies or substantial portions of the Software.
50 *
51 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
56 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
57 * DEALINGS IN THE SOFTWARE.
58 */
59
60
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: hypervisor_machdep.c,v 1.5 2008/02/19 13:25:53 bouyer Exp $");
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66
67 #include <xen/xen.h>
68 #include <xen/hypervisor.h>
69 #include <xen/evtchn.h>
70
71 #include "opt_xen.h"
72
73 // #define PORT_DEBUG 4
74 // #define EARLY_DEBUG_EVENT
75
76 int stipending(void);
77 int
78 stipending()
79 {
80 uint32_t l1;
81 unsigned long l2;
82 unsigned int l1i, l2i, port;
83 volatile shared_info_t *s = HYPERVISOR_shared_info;
84 struct cpu_info *ci;
85 int ret;
86
87 ret = 0;
88 ci = curcpu();
89
90 #if 0
91 if (HYPERVISOR_shared_info->events)
92 printf("stipending events %08lx mask %08lx ilevel %d\n",
93 HYPERVISOR_shared_info->events,
94 HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
95 #endif
96
97 #ifdef EARLY_DEBUG_EVENT
98 if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
99 xen_debug_handler(NULL);
100 xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
101 }
102 #endif
103
104 /*
105 * we're only called after STIC, so we know that we'll have to
106 * STI at the end
107 */
108 while (s->vcpu_info[0].evtchn_upcall_pending) {
109 cli();
110 s->vcpu_info[0].evtchn_upcall_pending = 0;
111 /* NB. No need for a barrier here -- XCHG is a barrier
112 * on x86. */
113 #ifdef XEN3
114 l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
115 #else
116 l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
117 #endif
118 while ((l1i = ffs(l1)) != 0) {
119 l1i--;
120 l1 &= ~(1 << l1i);
121
122 l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
123 /*
124 * mask and clear event. More efficient than calling
125 * hypervisor_mask/clear_event for each event.
126 */
127 xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
128 xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
129 while ((l2i = ffs(l2)) != 0) {
130 l2i--;
131 l2 &= ~(1 << l2i);
132
133 port = (l1i << 5) + l2i;
134 if (evtsource[port]) {
135 hypervisor_set_ipending(
136 evtsource[port]->ev_imask,
137 l1i, l2i);
138 evtsource[port]->ev_evcnt.ev_count++;
139 if (ret == 0 && ci->ci_ilevel <
140 evtsource[port]->ev_maxlevel)
141 ret = 1;
142 }
143 #ifdef DOM0OPS
144 else {
145 /* set pending event */
146 xenevt_setipending(l1i, l2i);
147 }
148 #endif
149 }
150 }
151 sti();
152 }
153
154 #if 0
155 if (ci->ci_ipending & 0x1)
156 printf("stipending events %08lx mask %08lx ilevel %d ipending %08x\n",
157 HYPERVISOR_shared_info->events,
158 HYPERVISOR_shared_info->events_mask, ci->ci_ilevel,
159 ci->ci_ipending);
160 #endif
161
162 return (ret);
163 }
164
165 void
166 do_hypervisor_callback(struct intrframe *regs)
167 {
168 uint32_t l1;
169 unsigned long l2;
170 unsigned int l1i, l2i, port;
171 volatile shared_info_t *s = HYPERVISOR_shared_info;
172 struct cpu_info *ci;
173 int level;
174
175 ci = curcpu();
176 level = ci->ci_ilevel;
177
178 // DDD printf("do_hypervisor_callback\n");
179
180 #ifdef EARLY_DEBUG_EVENT
181 if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
182 xen_debug_handler(NULL);
183 xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
184 }
185 #endif
186
187 while (s->vcpu_info[0].evtchn_upcall_pending) {
188 s->vcpu_info[0].evtchn_upcall_pending = 0;
189 /* NB. No need for a barrier here -- XCHG is a barrier
190 * on x86. */
191 #ifdef XEN3
192 l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
193 #else
194 l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
195 #endif
196 while ((l1i = ffs(l1)) != 0) {
197 l1i--;
198 l1 &= ~(1 << l1i);
199
200 l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
201 /*
202 * mask and clear the pending events.
203 * Doing it here for all event that will be processed
204 * avoids a race with stipending (which can be called
205 * though evtchn_do_event->splx) that could cause an event to
206 * be both processed and marked pending.
207 */
208 xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
209 xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
210
211 while ((l2i = ffs(l2)) != 0) {
212 l2i--;
213 l2 &= ~(1 << l2i);
214
215 port = (l1i << 5) + l2i;
216 #ifdef PORT_DEBUG
217 if (port == PORT_DEBUG)
218 printf("do_hypervisor_callback event %d\n", port);
219 #endif
220 if (evtsource[port])
221 call_evtchn_do_event(port, regs);
222 #ifdef DOM0OPS
223 else {
224 if (ci->ci_ilevel < IPL_HIGH) {
225 /* fast path */
226 int oipl = ci->ci_ilevel;
227 ci->ci_ilevel = IPL_HIGH;
228 xenevt_event(port);
229 ci->ci_ilevel = oipl;
230 } else {
231 /* set pending event */
232 xenevt_setipending(l1i, l2i);
233 }
234 }
235 #endif
236 }
237 }
238 }
239
240 #ifdef DIAGNOSTIC
241 if (level != ci->ci_ilevel)
242 printf("hypervisor done %08x level %d/%d ipending %08x\n",
243 #ifdef XEN3
244 (uint)HYPERVISOR_shared_info->vcpu_info[0].evtchn_pending_sel,
245 #else
246 (uint)HYPERVISOR_shared_info->evtchn_pending_sel,
247 #endif
248 level, ci->ci_ilevel, ci->ci_ipending);
249 #endif
250 }
251
252 void
253 hypervisor_unmask_event(unsigned int ev)
254 {
255 volatile shared_info_t *s = HYPERVISOR_shared_info;
256 #ifdef PORT_DEBUG
257 if (ev == PORT_DEBUG)
258 printf("hypervisor_unmask_event %d\n", ev);
259 #endif
260
261 xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
262 /*
263 * The following is basically the equivalent of
264 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
265 * interrupt edge' if the channel is masked.
266 */
267 if (xen_atomic_test_bit(&s->evtchn_pending[0], ev) &&
268 #ifdef XEN3
269 !xen_atomic_test_and_set_bit(&s->vcpu_info[0].evtchn_pending_sel, ev>>5)) {
270 #else
271 !xen_atomic_test_and_set_bit(&s->evtchn_pending_sel, ev>>5)) {
272 #endif
273 xen_atomic_set_bit(&s->vcpu_info[0].evtchn_upcall_pending, 0);
274 if (!s->vcpu_info[0].evtchn_upcall_mask)
275 hypervisor_force_callback();
276 }
277 }
278
279 void
280 hypervisor_mask_event(unsigned int ev)
281 {
282 volatile shared_info_t *s = HYPERVISOR_shared_info;
283 #ifdef PORT_DEBUG
284 if (ev == PORT_DEBUG)
285 printf("hypervisor_mask_event %d\n", ev);
286 #endif
287
288 xen_atomic_set_bit(&s->evtchn_mask[0], ev);
289 }
290
291 void
292 hypervisor_clear_event(unsigned int ev)
293 {
294 volatile shared_info_t *s = HYPERVISOR_shared_info;
295 #ifdef PORT_DEBUG
296 if (ev == PORT_DEBUG)
297 printf("hypervisor_clear_event %d\n", ev);
298 #endif
299
300 xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
301 }
302
303 void
304 hypervisor_enable_ipl(unsigned int ipl)
305 {
306 u_int32_t l1, l2;
307 int l1i, l2i;
308 struct cpu_info *ci = curcpu();
309
310 /*
311 * enable all events for ipl. As we only set an event in ipl_evt_mask
312 * for its lowest IPL, and pending IPLs are processed high to low,
313 * we know that all callback for this event have been processed.
314 */
315
316 l1 = ci->ci_isources[ipl]->ipl_evt_mask1;
317 ci->ci_isources[ipl]->ipl_evt_mask1 = 0;
318 while ((l1i = ffs(l1)) != 0) {
319 l1i--;
320 l1 &= ~(1 << l1i);
321 l2 = ci->ci_isources[ipl]->ipl_evt_mask2[l1i];
322 ci->ci_isources[ipl]->ipl_evt_mask2[l1i] = 0;
323 while ((l2i = ffs(l2)) != 0) {
324 int evtch;
325
326 l2i--;
327 l2 &= ~(1 << l2i);
328
329 evtch = (l1i << 5) + l2i;
330 hypervisor_enable_event(evtch);
331 }
332 }
333 }
334
335 void
336 hypervisor_set_ipending(u_int32_t iplmask, int l1, int l2)
337 {
338 int ipl;
339 struct cpu_info *ci = curcpu();
340
341 /* set pending bit for the appropriate IPLs */
342 ci->ci_ipending |= iplmask;
343
344 /*
345 * And set event pending bit for the lowest IPL. As IPL are handled
346 * from high to low, this ensure that all callbacks will have been
347 * called when we ack the event
348 */
349 ipl = ffs(iplmask);
350 KASSERT(ipl > 0);
351 ipl--;
352 ci->ci_isources[ipl]->ipl_evt_mask1 |= 1 << l1;
353 ci->ci_isources[ipl]->ipl_evt_mask2[l1] |= 1 << l2;
354 }
355