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