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