hypervisor_machdep.c revision 1.2 1 /* $NetBSD: hypervisor_machdep.c,v 1.2 2007/11/22 16:17:03 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.2 2007/11/22 16:17:03 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 #include <machine/atomic.h>
71
72 #include "opt_xen.h"
73
74 // #define PORT_DEBUG 4
75 // #define EARLY_DEBUG_EVENT
76
77 #ifdef EARLY_DEBUG_EVENT
78 extern int debug_port;
79 extern int xen_debug_handler(void *);
80 #endif
81
82 int stipending(void);
83 int
84 stipending()
85 {
86 uint32_t l1;
87 unsigned long l2;
88 unsigned int l1i, l2i, port;
89 volatile shared_info_t *s = HYPERVISOR_shared_info;
90 struct cpu_info *ci;
91 int ret;
92
93 ret = 0;
94 ci = curcpu();
95
96 #if 0
97 if (HYPERVISOR_shared_info->events)
98 printf("stipending events %08lx mask %08lx ilevel %d\n",
99 HYPERVISOR_shared_info->events,
100 HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
101 #endif
102
103 #ifdef EARLY_DEBUG_EVENT
104 if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
105 xen_debug_handler(NULL);
106 xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
107 }
108 #endif
109
110 /*
111 * we're only called after STIC, so we know that we'll have to
112 * STI at the end
113 */
114 while (s->vcpu_info[0].evtchn_upcall_pending) {
115 cli();
116 s->vcpu_info[0].evtchn_upcall_pending = 0;
117 /* NB. No need for a barrier here -- XCHG is a barrier
118 * on x86. */
119 #ifdef XEN3
120 l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
121 #else
122 l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
123 #endif
124 while ((l1i = ffs(l1)) != 0) {
125 l1i--;
126 l1 &= ~(1 << l1i);
127
128 l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
129 /*
130 * mask and clear event. More efficient than calling
131 * hypervisor_mask/clear_event for each event.
132 */
133 xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
134 xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
135 while ((l2i = ffs(l2)) != 0) {
136 l2i--;
137 l2 &= ~(1 << l2i);
138
139 port = (l1i << 5) + l2i;
140 if (evtsource[port]) {
141 hypervisor_set_ipending(
142 evtsource[port]->ev_imask,
143 l1i, l2i);
144 evtsource[port]->ev_evcnt.ev_count++;
145 if (ret == 0 && ci->ci_ilevel <
146 evtsource[port]->ev_maxlevel)
147 ret = 1;
148 }
149 #ifdef DOM0OPS
150 else
151 xenevt_event(port);
152 #endif
153 }
154 }
155 sti();
156 }
157
158 #if 0
159 if (ci->ci_ipending & 0x1)
160 printf("stipending events %08lx mask %08lx ilevel %d ipending %08x\n",
161 HYPERVISOR_shared_info->events,
162 HYPERVISOR_shared_info->events_mask, ci->ci_ilevel,
163 ci->ci_ipending);
164 #endif
165
166 return (ret);
167 }
168
169 void
170 do_hypervisor_callback(struct intrframe *regs)
171 {
172 uint32_t l1;
173 unsigned long l2;
174 unsigned int l1i, l2i, port;
175 volatile shared_info_t *s = HYPERVISOR_shared_info;
176 struct cpu_info *ci;
177 int level;
178
179 ci = curcpu();
180 level = ci->ci_ilevel;
181
182 // DDD printf("do_hypervisor_callback\n");
183
184 #ifdef EARLY_DEBUG_EVENT
185 if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
186 xen_debug_handler(NULL);
187 xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
188 }
189 #endif
190
191 while (s->vcpu_info[0].evtchn_upcall_pending) {
192 s->vcpu_info[0].evtchn_upcall_pending = 0;
193 /* NB. No need for a barrier here -- XCHG is a barrier
194 * on x86. */
195 #ifdef XEN3
196 l1 = xen_atomic_xchg(&s->vcpu_info[0].evtchn_pending_sel, 0);
197 #else
198 l1 = xen_atomic_xchg(&s->evtchn_pending_sel, 0);
199 #endif
200 while ((l1i = ffs(l1)) != 0) {
201 l1i--;
202 l1 &= ~(1 << l1i);
203
204 l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i];
205 /*
206 * mask and clear the pending events.
207 * Doing it here for all event that will be processed
208 * avoids a race with stipending (which can be called
209 * though evtchn_do_event->splx) that could cause an event to
210 * be both processed and marked pending.
211 */
212 xen_atomic_setbits_l(&s->evtchn_mask[l1i], l2);
213 xen_atomic_clearbits_l(&s->evtchn_pending[l1i], l2);
214
215 while ((l2i = ffs(l2)) != 0) {
216 l2i--;
217 l2 &= ~(1 << l2i);
218
219 port = (l1i << 5) + l2i;
220 #ifdef PORT_DEBUG
221 if (port == PORT_DEBUG)
222 printf("do_hypervisor_callback event %d\n", port);
223 #endif
224 if (evtsource[port])
225 call_evtchn_do_event(port, regs);
226 #ifdef DOM0OPS
227 else
228 xenevt_event(port);
229 #endif
230 }
231 }
232 }
233
234 #ifdef DIAGNOSTIC
235 if (level != ci->ci_ilevel)
236 printf("hypervisor done %08x level %d/%d ipending %08x\n",
237 #ifdef XEN3
238 (uint)HYPERVISOR_shared_info->vcpu_info[0].evtchn_pending_sel,
239 #else
240 (uint)HYPERVISOR_shared_info->evtchn_pending_sel,
241 #endif
242 level, ci->ci_ilevel, ci->ci_ipending);
243 #endif
244 }
245
246 void
247 hypervisor_unmask_event(unsigned int ev)
248 {
249 volatile shared_info_t *s = HYPERVISOR_shared_info;
250 #ifdef PORT_DEBUG
251 if (ev == PORT_DEBUG)
252 printf("hypervisor_unmask_event %d\n", ev);
253 #endif
254
255 xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
256 /*
257 * The following is basically the equivalent of
258 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
259 * interrupt edge' if the channel is masked.
260 */
261 if (xen_atomic_test_bit(&s->evtchn_pending[0], ev) &&
262 #ifdef XEN3
263 !xen_atomic_test_and_set_bit(&s->vcpu_info[0].evtchn_pending_sel, ev>>5)) {
264 #else
265 !xen_atomic_test_and_set_bit(&s->evtchn_pending_sel, ev>>5)) {
266 #endif
267 xen_atomic_set_bit(&s->vcpu_info[0].evtchn_upcall_pending, 0);
268 if (!s->vcpu_info[0].evtchn_upcall_mask)
269 hypervisor_force_callback();
270 }
271 }
272
273 void
274 hypervisor_mask_event(unsigned int ev)
275 {
276 volatile shared_info_t *s = HYPERVISOR_shared_info;
277 #ifdef PORT_DEBUG
278 if (ev == PORT_DEBUG)
279 printf("hypervisor_mask_event %d\n", ev);
280 #endif
281
282 xen_atomic_set_bit(&s->evtchn_mask[0], ev);
283 }
284
285 void
286 hypervisor_clear_event(unsigned int ev)
287 {
288 volatile shared_info_t *s = HYPERVISOR_shared_info;
289 #ifdef PORT_DEBUG
290 if (ev == PORT_DEBUG)
291 printf("hypervisor_clear_event %d\n", ev);
292 #endif
293
294 xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
295 }
296
297 void
298 hypervisor_enable_ipl(unsigned int ipl)
299 {
300 u_int32_t l1, l2;
301 int l1i, l2i;
302 struct cpu_info *ci = curcpu();
303
304 /*
305 * enable all events for ipl. As we only set an event in ipl_evt_mask
306 * for its lowest IPL, and pending IPLs are processed high to low,
307 * we know that all callback for this event have been processed.
308 */
309
310 l1 = ci->ci_isources[ipl]->ipl_evt_mask1;
311 ci->ci_isources[ipl]->ipl_evt_mask1 = 0;
312 while ((l1i = ffs(l1)) != 0) {
313 l1i--;
314 l1 &= ~(1 << l1i);
315 l2 = ci->ci_isources[ipl]->ipl_evt_mask2[l1i];
316 ci->ci_isources[ipl]->ipl_evt_mask2[l1i] = 0;
317 while ((l2i = ffs(l2)) != 0) {
318 int evtch;
319
320 l2i--;
321 l2 &= ~(1 << l2i);
322
323 evtch = (l1i << 5) + l2i;
324 hypervisor_enable_event(evtch);
325 }
326 }
327 }
328
329 void
330 hypervisor_set_ipending(u_int32_t iplmask, int l1, int l2)
331 {
332 int ipl;
333 struct cpu_info *ci = curcpu();
334
335 /* set pending bit for the appropriate IPLs */
336 ci->ci_ipending |= iplmask;
337
338 /*
339 * And set event pending bit for the lowest IPL. As IPL are handled
340 * from high to low, this ensure that all callbacks will have been
341 * called when we ack the event
342 */
343 ipl = ffs(iplmask);
344 KASSERT(ipl > 0);
345 ipl--;
346 ci->ci_isources[ipl]->ipl_evt_mask1 |= 1 << l1;
347 ci->ci_isources[ipl]->ipl_evt_mask2[l1] |= 1 << l2;
348 }
349