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