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