Home | History | Annotate | Line # | Download | only in x86
hypervisor_machdep.c revision 1.43
      1 /*	$NetBSD: hypervisor_machdep.c,v 1.43 2022/05/31 18:01:22 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.43 2022/05/31 18:01:22 bouyer Exp $");
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/kmem.h>
     62 #include <sys/cpu.h>
     63 #include <sys/ksyms.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 #include <machine/vmparam.h>
     68 #include <machine/pmap.h>
     69 
     70 #include <x86/machdep.h>
     71 #include <x86/cpuvar.h>
     72 
     73 #include <xen/xen.h>
     74 #include <xen/intr.h>
     75 #include <xen/hypervisor.h>
     76 #include <xen/evtchn.h>
     77 #include <xen/xenpmap.h>
     78 
     79 #include "opt_xen.h"
     80 #include "opt_modular.h"
     81 #include "opt_ddb.h"
     82 #include "isa.h"
     83 #include "pci.h"
     84 #include "ksyms.h"
     85 
     86 #ifdef DDB
     87 #include <machine/db_machdep.h>
     88 #include <ddb/db_extern.h>
     89 #include <ddb/db_output.h>
     90 #include <ddb/db_interface.h>
     91 #endif
     92 
     93 #ifdef XENPV
     94 /*
     95  * arch-dependent p2m frame lists list (L3 and L2)
     96  * used by Xen for save/restore mappings
     97  */
     98 static unsigned long * l3_p2m_page;
     99 static unsigned long * l2_p2m_page;
    100 static int l2_p2m_page_size; /* size of L2 page, in pages */
    101 
    102 static void build_p2m_frame_list_list(void);
    103 static void update_p2m_frame_list_list(void);
    104 
    105 #endif
    106 
    107 // #define PORT_DEBUG 4
    108 // #define EARLY_DEBUG_EVENT
    109 
    110 /* callback function type */
    111 typedef void (*iterate_func_t)(unsigned int, unsigned int,
    112 			       unsigned int, void *);
    113 
    114 static inline void
    115 evt_iterate_bits(volatile unsigned long *pendingl1,
    116 		 volatile unsigned long *pendingl2,
    117 		 volatile unsigned long *mask,
    118 		 iterate_func_t iterate_pending, void *iterate_args)
    119 {
    120 
    121 	KASSERT(pendingl1 != NULL);
    122 	KASSERT(pendingl2 != NULL);
    123 
    124 	unsigned long l1, l2;
    125 	unsigned int l1i, l2i, port;
    126 
    127 	l1 = xen_atomic_xchg(pendingl1, 0);
    128 	while ((l1i = xen_ffs(l1)) != 0) {
    129 		l1i--;
    130 		l1 &= ~(1UL << l1i);
    131 
    132 		l2 = pendingl2[l1i] & (mask != NULL ? ~mask[l1i] : -1UL);
    133 		l2 &= curcpu()->ci_evtmask[l1i];
    134 
    135 		if (mask != NULL) xen_atomic_setbits_l(&mask[l1i], l2);
    136 		xen_atomic_clearbits_l(&pendingl2[l1i], l2);
    137 
    138 		while ((l2i = xen_ffs(l2)) != 0) {
    139 			l2i--;
    140 			l2 &= ~(1UL << l2i);
    141 
    142 			port = (l1i << LONG_SHIFT) + l2i;
    143 
    144 			iterate_pending(port, l1i, l2i, iterate_args);
    145 		}
    146 	}
    147 }
    148 
    149 /*
    150  * Set per-cpu "pending" information for outstanding events that
    151  * cannot be processed now.
    152  */
    153 
    154 static inline void
    155 evt_set_pending(unsigned int port, unsigned int l1i,
    156 		unsigned int l2i, void *args)
    157 {
    158 
    159 	KASSERT(args != NULL);
    160 
    161 	int *ret = args;
    162 	struct intrhand *ih;
    163 
    164 	if (evtsource[port]) {
    165 		hypervisor_set_ipending(evtsource[port]->ev_imask, l1i, l2i);
    166 		evtsource[port]->ev_evcnt.ev_count++;
    167 		ih = evtsource[port]->ev_handlers;
    168 		while (ih != NULL) {
    169 			ih->ih_pending++;
    170 			ih = ih->ih_evt_next;
    171 		}
    172 
    173 		if (*ret == 0 && curcpu()->ci_ilevel <
    174 		    evtsource[port]->ev_maxlevel)
    175 			*ret = 1;
    176 	}
    177 #ifdef DOM0OPS
    178 	else  {
    179 		/* set pending event */
    180 		xenevt_setipending(l1i, l2i);
    181 	}
    182 #endif
    183 }
    184 
    185 int stipending(void);
    186 int
    187 stipending(void)
    188 {
    189 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    190 	struct cpu_info *ci;
    191 	volatile struct vcpu_info *vci;
    192 	int ret;
    193 
    194 	ret = 0;
    195 	ci = curcpu();
    196 	vci = ci->ci_vcpu;
    197 
    198 #if 0
    199 	if (HYPERVISOR_shared_info->events)
    200 		printf("stipending events %08lx mask %08lx ilevel %d\n",
    201 		    HYPERVISOR_shared_info->events,
    202 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
    203 #endif
    204 
    205 #ifdef EARLY_DEBUG_EVENT
    206 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    207 		xen_debug_handler(NULL);
    208 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    209 	}
    210 #endif
    211 
    212 	/*
    213 	 * we're only called after STIC, so we know that we'll have to
    214 	 * STI at the end
    215 	 */
    216 
    217 	while (vci->evtchn_upcall_pending) {
    218 		x86_disable_intr();
    219 
    220 		vci->evtchn_upcall_pending = 0;
    221 
    222 		evt_iterate_bits(&vci->evtchn_pending_sel,
    223 		    s->evtchn_pending, s->evtchn_mask,
    224 		    evt_set_pending, &ret);
    225 
    226 		x86_enable_intr();
    227 	}
    228 
    229 	return (ret);
    230 }
    231 
    232 /* Iterate through pending events and call the event handler */
    233 
    234 static inline void
    235 evt_do_hypervisor_callback(unsigned int port, unsigned int l1i,
    236 			   unsigned int l2i, void *args)
    237 {
    238 	KASSERT(args != NULL);
    239 
    240 #ifdef DOM0OPS
    241 	struct cpu_info *ci = curcpu();
    242 #endif
    243 	struct intrframe *regs = args;
    244 
    245 #ifdef PORT_DEBUG
    246 	if (port == PORT_DEBUG)
    247 		printf("do_hypervisor_callback event %d\n", port);
    248 #endif
    249 	if (evtsource[port]) {
    250 		KASSERT(cpu_intr_p());
    251 		evtchn_do_event(port, regs);
    252 	}
    253 #ifdef DOM0OPS
    254 	else  {
    255 		if (ci->ci_ilevel < IPL_HIGH) {
    256 			/* fast path */
    257 			int oipl = ci->ci_ilevel;
    258 			ci->ci_ilevel = IPL_HIGH;
    259 			KASSERT(cpu_intr_p());
    260 			xenevt_event(port);
    261 			ci->ci_ilevel = oipl;
    262 		} else {
    263 			/* set pending event */
    264 			xenevt_setipending(l1i, l2i);
    265 		}
    266 	}
    267 #endif
    268 }
    269 
    270 void
    271 do_hypervisor_callback(struct intrframe *regs)
    272 {
    273 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    274 	struct cpu_info *ci;
    275 	volatile struct vcpu_info *vci;
    276 	int level __diagused;
    277 
    278 	ci = curcpu();
    279 	vci = ci->ci_vcpu;
    280 	level = ci->ci_ilevel;
    281 
    282 	/* Save trapframe for clock handler */
    283 	KASSERT(regs != NULL);
    284 	ci->ci_xen_clockf_usermode = USERMODE(regs->_INTRFRAME_CS);
    285 	ci->ci_xen_clockf_pc = regs->_INTRFRAME_IP;
    286 
    287 	// DDD printf("do_hypervisor_callback\n");
    288 
    289 #ifdef EARLY_DEBUG_EVENT
    290 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    291 		xen_debug_handler(NULL);
    292 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    293 	}
    294 #endif
    295 
    296 	while (vci->evtchn_upcall_pending) {
    297 		vci->evtchn_upcall_pending = 0;
    298 
    299 		evt_iterate_bits(&vci->evtchn_pending_sel,
    300 		    s->evtchn_pending, s->evtchn_mask,
    301 		    evt_do_hypervisor_callback, regs);
    302 	}
    303 
    304 #ifdef DIAGNOSTIC
    305 	if (level != ci->ci_ilevel)
    306 		printf("hypervisor done %08x level %d/%d ipending %08x\n",
    307 		    (uint)vci->evtchn_pending_sel,
    308 		    level, ci->ci_ilevel, ci->ci_ipending);
    309 #endif
    310 }
    311 
    312 #if 0
    313 void
    314 hypervisor_send_event(struct cpu_info *ci, unsigned int ev)
    315 {
    316 	KASSERT(ci != NULL);
    317 
    318 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    319 	volatile struct vcpu_info *vci = ci->ci_vcpu;
    320 
    321 #ifdef PORT_DEBUG
    322 	if (ev == PORT_DEBUG)
    323 		printf("hypervisor_send_event %d\n", ev);
    324 #endif
    325 
    326 	xen_atomic_set_bit(&s->evtchn_pending[0], ev);
    327 
    328 	if (__predict_false(ci == curcpu())) {
    329 		xen_atomic_set_bit(&vci->evtchn_pending_sel,
    330 		    ev >> LONG_SHIFT);
    331 		xen_atomic_set_bit(&vci->evtchn_upcall_pending, 0);
    332 	}
    333 
    334 	xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
    335 
    336 	if (__predict_true(ci == curcpu())) {
    337 		hypervisor_force_callback();
    338 	} else {
    339 		if (__predict_false(xen_send_ipi(ci, XEN_IPI_HVCB))) {
    340 			panic("xen_send_ipi(cpu%d id %d, XEN_IPI_HVCB) failed\n",
    341 			    (int) ci->ci_cpuid, ci->ci_vcpuid);
    342 		}
    343 	}
    344 }
    345 #endif
    346 
    347 void
    348 hypervisor_unmask_event(unsigned int ev)
    349 {
    350 
    351 	KASSERT(ev > 0 && ev < NR_EVENT_CHANNELS);
    352 
    353 #ifdef PORT_DEBUG
    354 	if (ev == PORT_DEBUG)
    355 		printf("hypervisor_unmask_event %d\n", ev);
    356 #endif
    357 
    358 	/* Xen unmasks the evtchn_mask[0]:ev bit for us. */
    359 	evtchn_op_t op;
    360 	op.cmd = EVTCHNOP_unmask;
    361 	op.u.unmask.port = ev;
    362 	if (HYPERVISOR_event_channel_op(&op) != 0)
    363 		panic("Failed to unmask event %d\n", ev);
    364 
    365 	return;
    366 }
    367 
    368 void
    369 hypervisor_mask_event(unsigned int ev)
    370 {
    371 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    372 #ifdef PORT_DEBUG
    373 	if (ev == PORT_DEBUG)
    374 		printf("hypervisor_mask_event %d\n", ev);
    375 #endif
    376 
    377 	xen_atomic_set_bit(&s->evtchn_mask[0], ev);
    378 }
    379 
    380 void
    381 hypervisor_clear_event(unsigned int ev)
    382 {
    383 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    384 #ifdef PORT_DEBUG
    385 	if (ev == PORT_DEBUG)
    386 		printf("hypervisor_clear_event %d\n", ev);
    387 #endif
    388 
    389 	xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
    390 }
    391 
    392 static inline void
    393 evt_enable_event(unsigned int port, unsigned int l1i,
    394 		 unsigned int l2i, void *args)
    395 {
    396 	KASSERT(args == NULL);
    397 	hypervisor_unmask_event(port);
    398 #if defined(XENPV) && (NPCI > 0 || NISA > 0)
    399 	hypervisor_ack_pirq_event(port);
    400 #endif /* NPCI > 0 || NISA > 0 */
    401 }
    402 
    403 void
    404 hypervisor_enable_sir(unsigned int sir)
    405 {
    406 	struct cpu_info *ci = curcpu();
    407 
    408 	/*
    409 	 * enable all events for ipl. As we only set an event in ipl_evt_mask
    410 	 * for its lowest IPL, and pending IPLs are processed high to low,
    411 	 * we know that all callback for this event have been processed.
    412 	 */
    413 
    414 	evt_iterate_bits(&ci->ci_isources[sir]->ipl_evt_mask1,
    415 	    ci->ci_isources[sir]->ipl_evt_mask2, NULL,
    416 	    evt_enable_event, NULL);
    417 
    418 }
    419 
    420 void
    421 hypervisor_set_ipending(uint32_t imask, int l1, int l2)
    422 {
    423 
    424 	/* This function is not re-entrant */
    425 	KASSERT(x86_read_psl() != 0);
    426 
    427 	int sir;
    428 	struct cpu_info *ci = curcpu();
    429 
    430 	/* set pending bit for the appropriate IPLs */
    431 	ci->ci_ipending |= imask;
    432 
    433 	/*
    434 	 * And set event pending bit for the lowest IPL. As IPL are handled
    435 	 * from high to low, this ensure that all callbacks will have been
    436 	 * called when we ack the event
    437 	 */
    438 	sir = ffs(imask);
    439 	KASSERT(sir > SIR_XENIPL_VM);
    440 	sir--;
    441 	KASSERT(sir <= SIR_XENIPL_HIGH);
    442 	KASSERT(ci->ci_isources[sir] != NULL);
    443 	ci->ci_isources[sir]->ipl_evt_mask1 |= 1UL << l1;
    444 	ci->ci_isources[sir]->ipl_evt_mask2[l1] |= 1UL << l2;
    445 	KASSERT(ci == curcpu());
    446 #if 0
    447 	if (__predict_false(ci != curcpu())) {
    448 		if (xen_send_ipi(ci, XEN_IPI_HVCB)) {
    449 			panic("hypervisor_set_ipending: "
    450 			    "xen_send_ipi(cpu%d id %d, XEN_IPI_HVCB) failed\n",
    451 			    (int) ci->ci_cpuid, ci->ci_vcpuid);
    452 		}
    453 	}
    454 #endif
    455 }
    456 
    457 void
    458 hypervisor_machdep_attach(void)
    459 {
    460 #ifdef XENPV
    461  	/* dom0 does not require the arch-dependent P2M translation table */
    462 	if (!xendomain_is_dom0()) {
    463 		build_p2m_frame_list_list();
    464 		sysctl_xen_suspend_setup();
    465 	}
    466 #endif
    467 }
    468 
    469 void
    470 hypervisor_machdep_resume(void)
    471 {
    472 #ifdef XENPV
    473 	/* dom0 does not require the arch-dependent P2M translation table */
    474 	if (!xendomain_is_dom0())
    475 		update_p2m_frame_list_list();
    476 #endif
    477 }
    478 
    479 /*
    480  * idle_block()
    481  *
    482  *	Called from the idle loop when we have nothing to do but wait
    483  *	for an interrupt.
    484  */
    485 static void
    486 idle_block(void)
    487 {
    488 	KASSERT(curcpu()->ci_ipending == 0);
    489 	HYPERVISOR_block();
    490 	KASSERT(curcpu()->ci_ipending == 0);
    491 }
    492 
    493 void
    494 x86_cpu_idle_xen(void)
    495 {
    496 	struct cpu_info *ci = curcpu();
    497 
    498 	KASSERT(ci->ci_ilevel == IPL_NONE);
    499 
    500 	x86_disable_intr();
    501 	if (__predict_false(!ci->ci_want_resched)) {
    502 		idle_block();
    503 	} else {
    504 		x86_enable_intr();
    505 	}
    506 }
    507 
    508 #ifdef XENPV
    509 /*
    510  * Generate the p2m_frame_list_list table,
    511  * needed for guest save/restore
    512  */
    513 static void
    514 build_p2m_frame_list_list(void)
    515 {
    516         int fpp; /* number of page (frame) pointer per page */
    517         unsigned long max_pfn;
    518         /*
    519          * The p2m list is composed of three levels of indirection,
    520          * each layer containing MFNs pointing to lower level pages
    521          * The indirection is used to convert a given PFN to its MFN
    522          * Each N level page can point to @fpp (N-1) level pages
    523          * For example, for x86 32bit, we have:
    524          * - PAGE_SIZE: 4096 bytes
    525          * - fpp: 1024 (one L3 page can address 1024 L2 pages)
    526          * A L1 page contains the list of MFN we are looking for
    527          */
    528         max_pfn = xen_start_info.nr_pages;
    529         fpp = PAGE_SIZE / sizeof(xen_pfn_t);
    530 
    531         /* we only need one L3 page */
    532         l3_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map, PAGE_SIZE,
    533 	    PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
    534         if (l3_p2m_page == NULL)
    535                 panic("could not allocate memory for l3_p2m_page");
    536 
    537         /*
    538          * Determine how many L2 pages we need for the mapping
    539          * Each L2 can map a total of @fpp L1 pages
    540          */
    541         l2_p2m_page_size = howmany(max_pfn, fpp);
    542 
    543         l2_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map,
    544 	    l2_p2m_page_size * PAGE_SIZE,
    545 	    PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
    546         if (l2_p2m_page == NULL)
    547                 panic("could not allocate memory for l2_p2m_page");
    548 
    549         /* We now have L3 and L2 pages ready, update L1 mapping */
    550         update_p2m_frame_list_list();
    551 
    552 }
    553 
    554 /*
    555  * Update the L1 p2m_frame_list_list mapping (during guest boot or resume)
    556  */
    557 static void
    558 update_p2m_frame_list_list(void)
    559 {
    560         int i;
    561         int fpp; /* number of page (frame) pointer per page */
    562         unsigned long max_pfn;
    563 
    564         max_pfn = xen_start_info.nr_pages;
    565         fpp = PAGE_SIZE / sizeof(xen_pfn_t);
    566 
    567         for (i = 0; i < l2_p2m_page_size; i++) {
    568                 /*
    569                  * Each time we start a new L2 page,
    570                  * store its MFN in the L3 page
    571                  */
    572                 if ((i % fpp) == 0) {
    573                         l3_p2m_page[i/fpp] = vtomfn(
    574                                 (vaddr_t)&l2_p2m_page[i]);
    575                 }
    576                 /*
    577                  * we use a shortcut
    578                  * since @xpmap_phys_to_machine_mapping array
    579                  * already contains PFN to MFN mapping, we just
    580                  * set the l2_p2m_page MFN pointer to the MFN of the
    581                  * according frame of @xpmap_phys_to_machine_mapping
    582                  */
    583                 l2_p2m_page[i] = vtomfn((vaddr_t)
    584                         &xpmap_phys_to_machine_mapping[i*fpp]);
    585         }
    586 
    587         HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
    588                                         vtomfn((vaddr_t)l3_p2m_page);
    589         HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
    590 
    591 }
    592 #endif /* XENPV */
    593 
    594 void
    595 xen_init_ksyms(void)
    596 {
    597 #if NKSYMS || defined(DDB) || defined(MODULAR)
    598 	extern int end;
    599 	extern int *esym;
    600 #ifdef DDB
    601 	db_machine_init();
    602 #endif
    603 
    604 #ifdef XENPV
    605 	esym = xen_start_info.mod_start ?
    606 	    (void *)xen_start_info.mod_start :
    607 	    (void *)xen_start_info.mfn_list;
    608 #endif /* XENPV */
    609 	/* for PVH, esym is set in locore.S */
    610 	ksyms_addsyms_elf(*(int *)(void *)&end,
    611 	    ((int *)(void *)&end) + 1, esym);
    612 #endif
    613 }
    614