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