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