/*
*
* Copyright (c) 2004 Christian Limpach.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/******************************************************************************
* hypervisor.c
*
* Communication to/from hypervisor.
*
* Copyright (c) 2002-2004, K A Fraser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifdef XENPV
/*
* arch-dependent p2m frame lists list (L3 and L2)
* used by Xen for save/restore mappings
*/
static unsigned long * l3_p2m_page;
static unsigned long * l2_p2m_page;
static int l2_p2m_page_size; /* size of L2 page, in pages */
/*
* Set per-cpu "pending" information for outstanding events that
* cannot be processed now.
*/
static inline void
evt_set_pending(unsigned int port, unsigned int l1i,
unsigned int l2i, void *args)
{
KASSERT(args != NULL);
int *ret = args;
struct intrhand *ih;
if (evtsource[port]) {
hypervisor_set_ipending(evtsource[port]->ev_imask, l1i, l2i);
evtsource[port]->ev_evcnt.ev_count++;
ih = evtsource[port]->ev_handlers;
while (ih != NULL) {
ih->ih_pending++;
ih = ih->ih_evt_next;
}
void
hypervisor_enable_sir(unsigned int sir)
{
struct cpu_info *ci = curcpu();
/*
* enable all events for ipl. As we only set an event in ipl_evt_mask
* for its lowest IPL, and pending IPLs are processed high to low,
* we know that all callback for this event have been processed.
*/
void
hypervisor_set_ipending(uint64_t imask, int l1, int l2)
{
/* This function is not re-entrant */
KASSERT(x86_read_psl() != 0);
int sir;
struct cpu_info *ci = curcpu();
/* set pending bit for the appropriate IPLs */
ci->ci_ipending |= imask;
/*
* And set event pending bit for the lowest IPL. As IPL are handled
* from high to low, this ensure that all callbacks will have been
* called when we ack the event
*/
sir = ffs(imask);
KASSERT(sir > SIR_XENIPL_VM);
sir--;
KASSERT(sir <= SIR_XENIPL_HIGH);
KASSERT(ci->ci_isources[sir] != NULL);
ci->ci_isources[sir]->ipl_evt_mask1 |= 1UL << l1;
ci->ci_isources[sir]->ipl_evt_mask2[l1] |= 1UL << l2;
KASSERT(ci == curcpu());
#if 0
if (__predict_false(ci != curcpu())) {
if (xen_send_ipi(ci, XEN_IPI_HVCB)) {
panic("hypervisor_set_ipending: "
"xen_send_ipi(cpu%d id %d, XEN_IPI_HVCB) failed\n",
(int) ci->ci_cpuid, ci->ci_vcpuid);
}
}
#endif
}
void
hypervisor_machdep_attach(void)
{
#ifdef XENPV
/* dom0 does not require the arch-dependent P2M translation table */
if (!xendomain_is_dom0()) {
build_p2m_frame_list_list();
sysctl_xen_suspend_setup();
}
#endif
}
void
hypervisor_machdep_resume(void)
{
#ifdef XENPV
/* dom0 does not require the arch-dependent P2M translation table */
if (!xendomain_is_dom0())
update_p2m_frame_list_list();
#endif
}
/*
* idle_block()
*
* Called from the idle loop when we have nothing to do but wait
* for an interrupt.
*/
static void
idle_block(void)
{
KASSERT(curcpu()->ci_ipending == 0);
HYPERVISOR_block();
KASSERT(curcpu()->ci_ipending == 0);
}
#ifdef XENPV
/*
* Generate the p2m_frame_list_list table,
* needed for guest save/restore
*/
static void
build_p2m_frame_list_list(void)
{
int fpp; /* number of page (frame) pointer per page */
unsigned long max_pfn;
/*
* The p2m list is composed of three levels of indirection,
* each layer containing MFNs pointing to lower level pages
* The indirection is used to convert a given PFN to its MFN
* Each N level page can point to @fpp (N-1) level pages
* For example, for x86 32bit, we have:
* - PAGE_SIZE: 4096 bytes
* - fpp: 1024 (one L3 page can address 1024 L2 pages)
* A L1 page contains the list of MFN we are looking for
*/
max_pfn = xen_start_info.nr_pages;
fpp = PAGE_SIZE / sizeof(xen_pfn_t);
/* we only need one L3 page */
l3_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map, PAGE_SIZE,
PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
if (l3_p2m_page == NULL)
panic("could not allocate memory for l3_p2m_page");
/*
* Determine how many L2 pages we need for the mapping
* Each L2 can map a total of @fpp L1 pages
*/
l2_p2m_page_size = howmany(max_pfn, fpp);
l2_p2m_page = (vaddr_t *)uvm_km_alloc(kernel_map,
l2_p2m_page_size * PAGE_SIZE,
PAGE_SIZE, UVM_KMF_WIRED | UVM_KMF_NOWAIT);
if (l2_p2m_page == NULL)
panic("could not allocate memory for l2_p2m_page");
/* We now have L3 and L2 pages ready, update L1 mapping */
update_p2m_frame_list_list();
}
/*
* Update the L1 p2m_frame_list_list mapping (during guest boot or resume)
*/
static void
update_p2m_frame_list_list(void)
{
int i;
int fpp; /* number of page (frame) pointer per page */
unsigned long max_pfn;
for (i = 0; i < l2_p2m_page_size; i++) {
/*
* Each time we start a new L2 page,
* store its MFN in the L3 page
*/
if ((i % fpp) == 0) {
l3_p2m_page[i/fpp] = vtomfn(
(vaddr_t)&l2_p2m_page[i]);
}
/*
* we use a shortcut
* since @xpmap_phys_to_machine_mapping array
* already contains PFN to MFN mapping, we just
* set the l2_p2m_page MFN pointer to the MFN of the
* according frame of @xpmap_phys_to_machine_mapping
*/
l2_p2m_page[i] = vtomfn((vaddr_t)
&xpmap_phys_to_machine_mapping[i*fpp]);
}