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