hypervisor_machdep.c revision 1.38 1 /* $NetBSD: hypervisor_machdep.c,v 1.38 2020/04/25 15:26:17 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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /******************************************************************************
30 * hypervisor.c
31 *
32 * Communication to/from hypervisor.
33 *
34 * Copyright (c) 2002-2004, K A Fraser
35 *
36 * Permission is hereby granted, free of charge, to any person obtaining a copy
37 * of this software and associated documentation files (the "Software"), to
38 * deal in the Software without restriction, including without limitation the
39 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
40 * sell copies of the Software, and to permit persons to whom the Software is
41 * furnished to do so, subject to the following conditions:
42 *
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
53 */
54
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: hypervisor_machdep.c,v 1.38 2020/04/25 15:26:17 bouyer Exp $");
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kmem.h>
62 #include <sys/cpu.h>
63
64 #include <uvm/uvm_extern.h>
65
66 #include <machine/vmparam.h>
67 #include <machine/pmap.h>
68
69 #include <x86/machdep.h>
70 #include <x86/cpuvar.h>
71
72 #include <xen/xen.h>
73 #include <xen/intr.h>
74 #include <xen/hypervisor.h>
75 #include <xen/evtchn.h>
76 #include <xen/xenpmap.h>
77
78 #include "opt_xen.h"
79 #include "isa.h"
80 #include "pci.h"
81
82 #ifdef XENPV
83 /*
84 * arch-dependent p2m frame lists list (L3 and L2)
85 * used by Xen for save/restore mappings
86 */
87 static unsigned long * l3_p2m_page;
88 static unsigned long * l2_p2m_page;
89 static int l2_p2m_page_size; /* size of L2 page, in pages */
90
91 static void build_p2m_frame_list_list(void);
92 static void update_p2m_frame_list_list(void);
93
94 #endif
95
96 // #define PORT_DEBUG 4
97 // #define EARLY_DEBUG_EVENT
98
99 /* callback function type */
100 typedef void (*iterate_func_t)(unsigned int, unsigned int,
101 unsigned int, void *);
102
103 static inline void
104 evt_iterate_bits(volatile unsigned long *pendingl1,
105 volatile unsigned long *pendingl2,
106 volatile unsigned long *mask,
107 iterate_func_t iterate_pending, void *iterate_args)
108 {
109
110 KASSERT(pendingl1 != NULL);
111 KASSERT(pendingl2 != NULL);
112
113 unsigned long l1, l2;
114 unsigned int l1i, l2i, port;
115
116 l1 = xen_atomic_xchg(pendingl1, 0);
117 while ((l1i = xen_ffs(l1)) != 0) {
118 l1i--;
119 l1 &= ~(1UL << l1i);
120
121 l2 = pendingl2[l1i] & (mask != NULL ? ~mask[l1i] : -1UL);
122 l2 &= curcpu()->ci_evtmask[l1i];
123
124 if (mask != NULL) xen_atomic_setbits_l(&mask[l1i], l2);
125 xen_atomic_clearbits_l(&pendingl2[l1i], l2);
126
127 while ((l2i = xen_ffs(l2)) != 0) {
128 l2i--;
129 l2 &= ~(1UL << l2i);
130
131 port = (l1i << LONG_SHIFT) + l2i;
132
133 iterate_pending(port, l1i, l2i, iterate_args);
134 }
135 }
136 }
137
138 /*
139 * Set per-cpu "pending" information for outstanding events that
140 * cannot be processed now.
141 */
142
143 static inline void
144 evt_set_pending(unsigned int port, unsigned int l1i,
145 unsigned int l2i, void *args)
146 {
147
148 KASSERT(args != NULL);
149
150 int *ret = args;
151
152 if (evtsource[port]) {
153 hypervisor_set_ipending(evtsource[port]->ev_imask, l1i, l2i);
154 evtsource[port]->ev_evcnt.ev_count++;
155 if (*ret == 0 && curcpu()->ci_ilevel <
156 evtsource[port]->ev_maxlevel)
157 *ret = 1;
158 }
159 #ifdef DOM0OPS
160 else {
161 /* set pending event */
162 xenevt_setipending(l1i, l2i);
163 }
164 #endif
165 }
166
167 int stipending(void);
168 int
169 stipending(void)
170 {
171 volatile shared_info_t *s = HYPERVISOR_shared_info;
172 struct cpu_info *ci;
173 volatile struct vcpu_info *vci;
174 int ret;
175
176 ret = 0;
177 ci = curcpu();
178 vci = ci->ci_vcpu;
179
180 #if 0
181 if (HYPERVISOR_shared_info->events)
182 printf("stipending events %08lx mask %08lx ilevel %d\n",
183 HYPERVISOR_shared_info->events,
184 HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
185 #endif
186
187 #ifdef EARLY_DEBUG_EVENT
188 if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
189 xen_debug_handler(NULL);
190 xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
191 }
192 #endif
193
194 /*
195 * we're only called after STIC, so we know that we'll have to
196 * STI at the end
197 */
198
199 while (vci->evtchn_upcall_pending) {
200 x86_disable_intr();
201
202 vci->evtchn_upcall_pending = 0;
203
204 evt_iterate_bits(&vci->evtchn_pending_sel,
205 s->evtchn_pending, s->evtchn_mask,
206 evt_set_pending, &ret);
207
208 x86_enable_intr();
209 }
210
211 return (ret);
212 }
213
214 /* Iterate through pending events and call the event handler */
215
216 static inline void
217 evt_do_hypervisor_callback(unsigned int port, unsigned int l1i,
218 unsigned int l2i, void *args)
219 {
220 KASSERT(args != NULL);
221
222 #ifdef DOM0OPS
223 struct cpu_info *ci = curcpu();
224 #endif
225 struct intrframe *regs = args;
226
227 #ifdef PORT_DEBUG
228 if (port == PORT_DEBUG)
229 printf("do_hypervisor_callback event %d\n", port);
230 #endif
231 if (evtsource[port]) {
232 KASSERT(cpu_intr_p());
233 evtchn_do_event(port, regs);
234 }
235 #ifdef DOM0OPS
236 else {
237 if (ci->ci_ilevel < IPL_HIGH) {
238 /* fast path */
239 int oipl = ci->ci_ilevel;
240 ci->ci_ilevel = IPL_HIGH;
241 KASSERT(cpu_intr_p());
242 xenevt_event(port);
243 ci->ci_ilevel = oipl;
244 } else {
245 /* set pending event */
246 xenevt_setipending(l1i, l2i);
247 }
248 }
249 #endif
250 }
251
252 void
253 do_hypervisor_callback(struct intrframe *regs)
254 {
255 volatile shared_info_t *s = HYPERVISOR_shared_info;
256 struct cpu_info *ci;
257 volatile struct vcpu_info *vci;
258 int level __diagused;
259
260 ci = curcpu();
261 vci = ci->ci_vcpu;
262 level = ci->ci_ilevel;
263
264 /* Save trapframe for clock handler */
265 KASSERT(regs != NULL);
266 ci->ci_xen_clockf_usermode = USERMODE(regs->_INTRFRAME_CS);
267 ci->ci_xen_clockf_pc = regs->_INTRFRAME_IP;
268
269 // DDD printf("do_hypervisor_callback\n");
270
271 #ifdef EARLY_DEBUG_EVENT
272 if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
273 xen_debug_handler(NULL);
274 xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
275 }
276 #endif
277
278 while (vci->evtchn_upcall_pending) {
279 vci->evtchn_upcall_pending = 0;
280
281 evt_iterate_bits(&vci->evtchn_pending_sel,
282 s->evtchn_pending, s->evtchn_mask,
283 evt_do_hypervisor_callback, regs);
284 }
285
286 #ifdef DIAGNOSTIC
287 if (level != ci->ci_ilevel)
288 printf("hypervisor done %08x level %d/%d ipending %08x\n",
289 (uint)vci->evtchn_pending_sel,
290 level, ci->ci_ilevel, ci->ci_ipending);
291 #endif
292 }
293
294 #if 0
295 void
296 hypervisor_send_event(struct cpu_info *ci, unsigned int ev)
297 {
298 KASSERT(ci != NULL);
299
300 volatile shared_info_t *s = HYPERVISOR_shared_info;
301 volatile struct vcpu_info *vci = ci->ci_vcpu;
302
303 #ifdef PORT_DEBUG
304 if (ev == PORT_DEBUG)
305 printf("hypervisor_send_event %d\n", ev);
306 #endif
307
308 xen_atomic_set_bit(&s->evtchn_pending[0], ev);
309
310 if (__predict_false(ci == curcpu())) {
311 xen_atomic_set_bit(&vci->evtchn_pending_sel,
312 ev >> LONG_SHIFT);
313 xen_atomic_set_bit(&vci->evtchn_upcall_pending, 0);
314 }
315
316 xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
317
318 if (__predict_true(ci == curcpu())) {
319 hypervisor_force_callback();
320 } else {
321 if (__predict_false(xen_send_ipi(ci, XEN_IPI_HVCB))) {
322 panic("xen_send_ipi(cpu%d id %d, XEN_IPI_HVCB) failed\n",
323 (int) ci->ci_cpuid, ci->ci_vcpuid);
324 }
325 }
326 }
327 #endif
328
329 void
330 hypervisor_unmask_event(unsigned int ev)
331 {
332
333 KASSERT(ev > 0 && ev < NR_EVENT_CHANNELS);
334
335 #ifdef PORT_DEBUG
336 if (ev == PORT_DEBUG)
337 printf("hypervisor_unmask_event %d\n", ev);
338 #endif
339
340 /* Xen unmasks the evtchn_mask[0]:ev bit for us. */
341 evtchn_op_t op;
342 op.cmd = EVTCHNOP_unmask;
343 op.u.unmask.port = ev;
344 if (HYPERVISOR_event_channel_op(&op) != 0)
345 panic("Failed to unmask event %d\n", ev);
346
347 return;
348 }
349
350 void
351 hypervisor_mask_event(unsigned int ev)
352 {
353 volatile shared_info_t *s = HYPERVISOR_shared_info;
354 #ifdef PORT_DEBUG
355 if (ev == PORT_DEBUG)
356 printf("hypervisor_mask_event %d\n", ev);
357 #endif
358
359 xen_atomic_set_bit(&s->evtchn_mask[0], ev);
360 }
361
362 void
363 hypervisor_clear_event(unsigned int ev)
364 {
365 volatile shared_info_t *s = HYPERVISOR_shared_info;
366 #ifdef PORT_DEBUG
367 if (ev == PORT_DEBUG)
368 printf("hypervisor_clear_event %d\n", ev);
369 #endif
370
371 xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
372 }
373
374 static inline void
375 evt_enable_event(unsigned int port, unsigned int l1i,
376 unsigned int l2i, void *args)
377 {
378 KASSERT(args == NULL);
379 hypervisor_unmask_event(port);
380 }
381
382 void
383 hypervisor_enable_sir(unsigned int sir)
384 {
385 struct cpu_info *ci = curcpu();
386
387 /*
388 * enable all events for ipl. As we only set an event in ipl_evt_mask
389 * for its lowest IPL, and pending IPLs are processed high to low,
390 * we know that all callback for this event have been processed.
391 */
392
393 evt_iterate_bits(&ci->ci_isources[sir]->ipl_evt_mask1,
394 ci->ci_isources[sir]->ipl_evt_mask2, NULL,
395 evt_enable_event, NULL);
396
397 }
398
399 void
400 hypervisor_set_ipending(uint32_t imask, int l1, int l2)
401 {
402
403 /* This function is not re-entrant */
404 KASSERT(x86_read_psl() != 0);
405
406 int sir;
407 struct cpu_info *ci = curcpu();
408
409 /* set pending bit for the appropriate IPLs */
410 ci->ci_ipending |= imask;
411
412 /*
413 * And set event pending bit for the lowest IPL. As IPL are handled
414 * from high to low, this ensure that all callbacks will have been
415 * called when we ack the event
416 */
417 sir = ffs(imask);
418 KASSERT(sir > SIR_XENIPL_VM);
419 sir--;
420 KASSERT(sir <= SIR_XENIPL_HIGH);
421 KASSERT(ci->ci_isources[sir] != NULL);
422 ci->ci_isources[sir]->ipl_evt_mask1 |= 1UL << l1;
423 ci->ci_isources[sir]->ipl_evt_mask2[l1] |= 1UL << l2;
424 KASSERT(ci == curcpu());
425 #if 0
426 if (__predict_false(ci != curcpu())) {
427 if (xen_send_ipi(ci, XEN_IPI_HVCB)) {
428 panic("hypervisor_set_ipending: "
429 "xen_send_ipi(cpu%d id %d, XEN_IPI_HVCB) failed\n",
430 (int) ci->ci_cpuid, ci->ci_vcpuid);
431 }
432 }
433 #endif
434 }
435
436 void
437 hypervisor_machdep_attach(void)
438 {
439 #ifdef XENPV
440 /* dom0 does not require the arch-dependent P2M translation table */
441 if (!xendomain_is_dom0()) {
442 build_p2m_frame_list_list();
443 sysctl_xen_suspend_setup();
444 }
445 #endif
446 }
447
448 void
449 hypervisor_machdep_resume(void)
450 {
451 #ifdef XENPV
452 /* dom0 does not require the arch-dependent P2M translation table */
453 if (!xendomain_is_dom0())
454 update_p2m_frame_list_list();
455 #endif
456 }
457
458 /*
459 * idle_block()
460 *
461 * Called from the idle loop when we have nothing to do but wait
462 * for an interrupt.
463 */
464 static void
465 idle_block(void)
466 {
467 KASSERT(curcpu()->ci_ipending == 0);
468 HYPERVISOR_block();
469 KASSERT(curcpu()->ci_ipending == 0);
470 }
471
472 void
473 x86_cpu_idle_xen(void)
474 {
475 struct cpu_info *ci = curcpu();
476
477 KASSERT(ci->ci_ilevel == IPL_NONE);
478
479 x86_disable_intr();
480 if (__predict_false(!ci->ci_want_resched)) {
481 idle_block();
482 } else {
483 x86_enable_intr();
484 }
485 }
486
487 #ifdef XENPV
488 /*
489 * Generate the p2m_frame_list_list table,
490 * needed for guest save/restore
491 */
492 static void
493 build_p2m_frame_list_list(void)
494 {
495 int fpp; /* number of page (frame) pointer per page */
496 unsigned long max_pfn;
497 /*
498 * The p2m list is composed of three levels of indirection,
499 * each layer containing MFNs pointing to lower level pages
500 * The indirection is used to convert a given PFN to its MFN
501 * Each N level page can point to @fpp (N-1) level pages
502 * For example, for x86 32bit, we have:
503 * - PAGE_SIZE: 4096 bytes
504 * - fpp: 1024 (one L3 page can address 1024 L2 pages)
505 * A L1 page contains the list of MFN we are looking for
506 */
507 max_pfn = xen_start_info.nr_pages;
508 fpp = PAGE_SIZE / sizeof(xen_pfn_t);
509
510 /* we only need one L3 page */
511 l3_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map, PAGE_SIZE,
512 PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
513 if (l3_p2m_page == NULL)
514 panic("could not allocate memory for l3_p2m_page");
515
516 /*
517 * Determine how many L2 pages we need for the mapping
518 * Each L2 can map a total of @fpp L1 pages
519 */
520 l2_p2m_page_size = howmany(max_pfn, fpp);
521
522 l2_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map,
523 l2_p2m_page_size * PAGE_SIZE,
524 PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
525 if (l2_p2m_page == NULL)
526 panic("could not allocate memory for l2_p2m_page");
527
528 /* We now have L3 and L2 pages ready, update L1 mapping */
529 update_p2m_frame_list_list();
530
531 }
532
533 /*
534 * Update the L1 p2m_frame_list_list mapping (during guest boot or resume)
535 */
536 static void
537 update_p2m_frame_list_list(void)
538 {
539 int i;
540 int fpp; /* number of page (frame) pointer per page */
541 unsigned long max_pfn;
542
543 max_pfn = xen_start_info.nr_pages;
544 fpp = PAGE_SIZE / sizeof(xen_pfn_t);
545
546 for (i = 0; i < l2_p2m_page_size; i++) {
547 /*
548 * Each time we start a new L2 page,
549 * store its MFN in the L3 page
550 */
551 if ((i % fpp) == 0) {
552 l3_p2m_page[i/fpp] = vtomfn(
553 (vaddr_t)&l2_p2m_page[i]);
554 }
555 /*
556 * we use a shortcut
557 * since @xpmap_phys_to_machine_mapping array
558 * already contains PFN to MFN mapping, we just
559 * set the l2_p2m_page MFN pointer to the MFN of the
560 * according frame of @xpmap_phys_to_machine_mapping
561 */
562 l2_p2m_page[i] = vtomfn((vaddr_t)
563 &xpmap_phys_to_machine_mapping[i*fpp]);
564 }
565
566 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
567 vtomfn((vaddr_t)l3_p2m_page);
568 HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
569
570 }
571 #endif /* XENPV */
572