Home | History | Annotate | Line # | Download | only in x86
hypervisor_machdep.c revision 1.14.2.2
      1 /*	$NetBSD: hypervisor_machdep.c,v 1.14.2.2 2011/08/04 09:07:47 cherry 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.14.2.2 2011/08/04 09:07:47 cherry Exp $");
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/kmem.h>
     62 
     63 #include <uvm/uvm_extern.h>
     64 
     65 #include <machine/vmparam.h>
     66 #include <machine/pmap.h>
     67 
     68 #include <xen/xen.h>
     69 #include <xen/hypervisor.h>
     70 #include <xen/evtchn.h>
     71 #include <xen/xenpmap.h>
     72 
     73 #include "opt_xen.h"
     74 
     75 /*
     76  * arch-dependent p2m frame lists list (L3 and L2)
     77  * used by Xen for save/restore mappings
     78  */
     79 static unsigned long * l3_p2m_page;
     80 static unsigned long * l2_p2m_page;
     81 static int l2_p2m_page_size; /* size of L2 page, in pages */
     82 
     83 static void build_p2m_frame_list_list(void);
     84 static void update_p2m_frame_list_list(void);
     85 
     86 // #define PORT_DEBUG 4
     87 // #define EARLY_DEBUG_EVENT
     88 
     89 static inline unsigned int
     90 evt_bitstr_to_port(unsigned long l1, unsigned long l2)
     91 {
     92 	unsigned int l1i, l2i, port;
     93 
     94 	l1i = xen_ffs(l1) - 1;
     95 	l2i = xen_ffs(l2) - 1;
     96 
     97 	port = (l1i << LONG_SHIFT) + l2i;
     98 	return port;
     99 }
    100 
    101 /* callback function type */
    102 typedef void (*iterate_func_t)(struct cpu_info *,
    103 			       unsigned int,
    104 			       unsigned int,
    105 			       unsigned int,
    106 			       void *);
    107 
    108 
    109 static inline void
    110 evt_iterate_pending(struct cpu_info *ci,
    111 		    volatile unsigned long *pendingl1,
    112 		    volatile unsigned long *pendingl2,
    113 		    volatile unsigned long *mask,
    114 		    iterate_func_t iterate_pending,
    115 		    void *iterate_args)
    116 {
    117 
    118 	KASSERT(pendingl1 != NULL);
    119 	KASSERT(pendingl2 != NULL);
    120 
    121 	unsigned long l1, l2;
    122 	unsigned int l1i, l2i, port;
    123 
    124 	l1 = xen_atomic_xchg(pendingl1, 0);
    125 	while ((l1i = xen_ffs(l1)) != 0) {
    126 		l1i--;
    127 		l1 &= ~(1UL << l1i);
    128 
    129 		l2 = pendingl2[l1i] & (mask != NULL ? ~mask[l1i] : -1UL);
    130 
    131 		if (mask != NULL) xen_atomic_setbits_l(&mask[l1i], l2);
    132 		xen_atomic_clearbits_l(&pendingl2[l1i], l2);
    133 
    134 		while ((l2i = xen_ffs(l2)) != 0) {
    135 			l2i--;
    136 			l2 &= ~(1UL << l2i);
    137 
    138 			port = (l1i << LONG_SHIFT) + l2i;
    139 
    140 			iterate_pending(ci, port, l1i, l2i, iterate_args);
    141 		}
    142 	}
    143 }
    144 
    145 /*
    146  * Set per-cpu "pending" information for outstanding events that
    147  * cannot be processed now.
    148  */
    149 
    150 static inline void
    151 evt_set_pending(struct cpu_info *ci,
    152 		unsigned int port,
    153 		unsigned int l1i,
    154 		unsigned int l2i,
    155 		void *args)
    156 {
    157 
    158 	KASSERT(args != NULL);
    159 	KASSERT(ci != NULL);
    160 
    161 	int *ret = args;
    162 
    163 	if (evtsource[port]) {
    164 		hypervisor_set_ipending(ci,
    165 			evtsource[port]->ev_imask,
    166 			l1i, l2i);
    167 		evtsource[port]->ev_evcnt.ev_count++;
    168 		if (*ret == 0 && ci->ci_ilevel <
    169 		    evtsource[port]->ev_maxlevel)
    170 			*ret = 1;
    171 	}
    172 #ifdef DOM0OPS
    173 	else  {
    174 		/* set pending event */
    175 		xenevt_setipending(l1i, l2i);
    176 	}
    177 #endif
    178 }
    179 
    180 int stipending(void);
    181 int
    182 stipending(void)
    183 {
    184 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    185 	struct cpu_info *ci;
    186 	volatile struct vcpu_info *vci;
    187 	int ret;
    188 
    189 	ret = 0;
    190 	ci = curcpu();
    191 	vci = ci->ci_vcpu;
    192 
    193 #if 0
    194 	if (HYPERVISOR_shared_info->events)
    195 		printf("stipending events %08lx mask %08lx ilevel %d\n",
    196 		    HYPERVISOR_shared_info->events,
    197 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel);
    198 #endif
    199 
    200 #ifdef EARLY_DEBUG_EVENT
    201 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    202 		xen_debug_handler(NULL);
    203 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    204 	}
    205 #endif
    206 
    207 	/*
    208 	 * we're only called after STIC, so we know that we'll have to
    209 	 * STI at the end
    210 	 */
    211 
    212 	while (vci->evtchn_upcall_pending) {
    213 		cli();
    214 
    215 		vci->evtchn_upcall_pending = 0;
    216 
    217 		evt_iterate_pending(ci,
    218 				    &vci->evtchn_pending_sel,
    219 				    s->evtchn_pending,
    220 				    s->evtchn_mask,
    221 				    evt_set_pending,
    222 				    &ret);
    223 
    224 		sti();
    225 	}
    226 
    227 #if 0
    228 	if (ci->ci_ipending & 0x1)
    229 		printf("stipending events %08lx mask %08lx ilevel %d ipending %08x\n",
    230 		    HYPERVISOR_shared_info->events,
    231 		    HYPERVISOR_shared_info->events_mask, ci->ci_ilevel,
    232 		    ci->ci_ipending);
    233 #endif
    234 
    235 	return (ret);
    236 }
    237 
    238 /* Iterate through pending events and call the event handler */
    239 
    240 static inline void
    241 evt_do_hypervisor_callback(struct cpu_info *ci,
    242 			   unsigned int port,
    243 			   unsigned int l1i,
    244 			   unsigned int l2i,
    245 			   void *args)
    246 {
    247 	KASSERT(args != NULL);
    248 	KASSERT(ci == curcpu());
    249 
    250 	struct intrframe *regs = args;
    251 
    252 #ifdef PORT_DEBUG
    253 	if (port == PORT_DEBUG)
    254 		printf("do_hypervisor_callback event %d\n", port);
    255 #endif
    256 	if (evtsource[port])
    257 		call_evtchn_do_event(port, regs);
    258 #ifdef DOM0OPS
    259 	else  {
    260 		if (ci->ci_ilevel < IPL_HIGH) {
    261 			/* fast path */
    262 			int oipl = ci->ci_ilevel;
    263 			ci->ci_ilevel = IPL_HIGH;
    264 			call_xenevt_event(port);
    265 			ci->ci_ilevel = oipl;
    266 		} else {
    267 			/* set pending event */
    268 			xenevt_setipending(l1i, l2i);
    269 		}
    270 	}
    271 #endif
    272 }
    273 
    274 void
    275 do_hypervisor_callback(struct intrframe *regs)
    276 {
    277 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    278 	struct cpu_info *ci;
    279 	volatile struct vcpu_info *vci;
    280 	int level;
    281 
    282 	ci = curcpu();
    283 	vci = ci->ci_vcpu;
    284 	level = ci->ci_ilevel;
    285 
    286 	// DDD printf("do_hypervisor_callback\n");
    287 
    288 #ifdef EARLY_DEBUG_EVENT
    289 	if (xen_atomic_test_bit(&s->evtchn_pending[0], debug_port)) {
    290 		xen_debug_handler(NULL);
    291 		xen_atomic_clear_bit(&s->evtchn_pending[0], debug_port);
    292 	}
    293 #endif
    294 
    295 	while (vci->evtchn_upcall_pending) {
    296 		vci->evtchn_upcall_pending = 0;
    297 
    298 		evt_iterate_pending(ci,
    299 				    &vci->evtchn_pending_sel,
    300 				    s->evtchn_pending,
    301 				    s->evtchn_mask,
    302 				    evt_do_hypervisor_callback,
    303 				    regs);
    304 	}
    305 
    306 #ifdef DIAGNOSTIC
    307 	if (level != ci->ci_ilevel)
    308 		printf("hypervisor done %08x level %d/%d ipending %08x\n",
    309 		    (uint)vci->evtchn_pending_sel,
    310 		    level, ci->ci_ilevel, ci->ci_ipending);
    311 #endif
    312 }
    313 
    314 void
    315 hypervisor_unmask_event(unsigned int ev)
    316 {
    317 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    318 	volatile struct vcpu_info *vci = curcpu()->ci_vcpu;
    319 
    320 #ifdef PORT_DEBUG
    321 	if (ev == PORT_DEBUG)
    322 		printf("hypervisor_unmask_event %d\n", ev);
    323 #endif
    324 
    325 	xen_atomic_clear_bit(&s->evtchn_mask[0], ev);
    326 	/*
    327 	 * The following is basically the equivalent of
    328 	 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
    329 	 * interrupt edge' if the channel is masked.
    330 	 */
    331 	if (xen_atomic_test_bit(&s->evtchn_pending[0], ev) &&
    332 	    !xen_atomic_test_and_set_bit(&vci->evtchn_pending_sel, ev>>LONG_SHIFT)) {
    333 		xen_atomic_set_bit(&vci->evtchn_upcall_pending, 0);
    334 		if (!vci->evtchn_upcall_mask)
    335 			hypervisor_force_callback();
    336 	}
    337 }
    338 
    339 void
    340 hypervisor_mask_event(unsigned int ev)
    341 {
    342 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    343 #ifdef PORT_DEBUG
    344 	if (ev == PORT_DEBUG)
    345 		printf("hypervisor_mask_event %d\n", ev);
    346 #endif
    347 
    348 	xen_atomic_set_bit(&s->evtchn_mask[0], ev);
    349 }
    350 
    351 void
    352 hypervisor_clear_event(unsigned int ev)
    353 {
    354 	volatile shared_info_t *s = HYPERVISOR_shared_info;
    355 #ifdef PORT_DEBUG
    356 	if (ev == PORT_DEBUG)
    357 		printf("hypervisor_clear_event %d\n", ev);
    358 #endif
    359 
    360 	xen_atomic_clear_bit(&s->evtchn_pending[0], ev);
    361 }
    362 
    363 static inline void
    364 evt_enable_event(struct cpu_info *ci,
    365 	       unsigned int port,
    366 	       unsigned int l1i,
    367 	       unsigned int l2i,
    368 	       void *args)
    369 {
    370 	KASSERT(ci != NULL);
    371 	KASSERT(args == NULL);
    372 	hypervisor_enable_event(port);
    373 }
    374 
    375 void
    376 hypervisor_enable_ipl(unsigned int ipl)
    377 {
    378 	struct cpu_info *ci = curcpu();
    379 
    380 	/*
    381 	 * enable all events for ipl. As we only set an event in ipl_evt_mask
    382 	 * for its lowest IPL, and pending IPLs are processed high to low,
    383 	 * we know that all callback for this event have been processed.
    384 	 */
    385 
    386 	evt_iterate_pending(ci,
    387 			    &ci->ci_isources[ipl]->ipl_evt_mask1,
    388 			    ci->ci_isources[ipl]->ipl_evt_mask2,
    389 			    NULL,
    390 			    evt_enable_event,
    391 			    NULL);
    392 
    393 }
    394 
    395 void
    396 hypervisor_set_ipending(struct cpu_info *ci,
    397 			uint32_t iplmask,
    398 			int l1, int l2)
    399 {
    400 	int ipl;
    401 
    402 	/* set pending bit for the appropriate IPLs */
    403 	ci->ci_ipending |= iplmask;
    404 
    405 	/*
    406 	 * And set event pending bit for the lowest IPL. As IPL are handled
    407 	 * from high to low, this ensure that all callbacks will have been
    408 	 * called when we ack the event
    409 	 */
    410 	ipl = ffs(iplmask);
    411 	KASSERT(ipl > 0);
    412 	ipl--;
    413 	KASSERT(ipl < NIPL);
    414 	KASSERT(ci->ci_isources[ipl] != NULL);
    415 	ci->ci_isources[ipl]->ipl_evt_mask1 |= 1UL << l1;
    416 	ci->ci_isources[ipl]->ipl_evt_mask2[l1] |= 1UL << l2;
    417 }
    418 
    419 void
    420 hypervisor_machdep_attach(void)
    421 {
    422  	/* dom0 does not require the arch-dependent P2M translation table */
    423 	if ( !xendomain_is_dom0() ) {
    424 		build_p2m_frame_list_list();
    425 	}
    426 }
    427 
    428 /*
    429  * Generate the p2m_frame_list_list table,
    430  * needed for guest save/restore
    431  */
    432 static void
    433 build_p2m_frame_list_list(void)
    434 {
    435         int fpp; /* number of page (frame) pointer per page */
    436         unsigned long max_pfn;
    437         /*
    438          * The p2m list is composed of three levels of indirection,
    439          * each layer containing MFNs pointing to lower level pages
    440          * The indirection is used to convert a given PFN to its MFN
    441          * Each N level page can point to @fpp (N-1) level pages
    442          * For example, for x86 32bit, we have:
    443          * - PAGE_SIZE: 4096 bytes
    444          * - fpp: 1024 (one L3 page can address 1024 L2 pages)
    445          * A L1 page contains the list of MFN we are looking for
    446          */
    447         max_pfn = xen_start_info.nr_pages;
    448         fpp = PAGE_SIZE / sizeof(xen_pfn_t);
    449 
    450         /* we only need one L3 page */
    451         l3_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map, PAGE_SIZE,
    452 	    PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
    453         if (l3_p2m_page == NULL)
    454                 panic("could not allocate memory for l3_p2m_page");
    455 
    456         /*
    457          * Determine how many L2 pages we need for the mapping
    458          * Each L2 can map a total of @fpp L1 pages
    459          */
    460         l2_p2m_page_size = howmany(max_pfn, fpp);
    461 
    462         l2_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map,
    463 	    l2_p2m_page_size * PAGE_SIZE,
    464 	    PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
    465         if (l2_p2m_page == NULL)
    466                 panic("could not allocate memory for l2_p2m_page");
    467 
    468         /* We now have L3 and L2 pages ready, update L1 mapping */
    469         update_p2m_frame_list_list();
    470 
    471 }
    472 
    473 /*
    474  * Update the L1 p2m_frame_list_list mapping (during guest boot or resume)
    475  */
    476 static void
    477 update_p2m_frame_list_list(void)
    478 {
    479         int i;
    480         int fpp; /* number of page (frame) pointer per page */
    481         unsigned long max_pfn;
    482 
    483         max_pfn = xen_start_info.nr_pages;
    484         fpp = PAGE_SIZE / sizeof(xen_pfn_t);
    485 
    486         for (i = 0; i < l2_p2m_page_size; i++) {
    487                 /*
    488                  * Each time we start a new L2 page,
    489                  * store its MFN in the L3 page
    490                  */
    491                 if ((i % fpp) == 0) {
    492                         l3_p2m_page[i/fpp] = vtomfn(
    493                                 (vaddr_t)&l2_p2m_page[i]);
    494                 }
    495                 /*
    496                  * we use a shortcut
    497                  * since @xpmap_phys_to_machine_mapping array
    498                  * already contains PFN to MFN mapping, we just
    499                  * set the l2_p2m_page MFN pointer to the MFN of the
    500                  * according frame of @xpmap_phys_to_machine_mapping
    501                  */
    502                 l2_p2m_page[i] = vtomfn((vaddr_t)
    503                         &xpmap_phys_to_machine_mapping[i*fpp]);
    504         }
    505 
    506         HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
    507                                         vtomfn((vaddr_t)l3_p2m_page);
    508         HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
    509 
    510 }
    511