xpv_support.c revision ea8190a2a3b5243d911d4bd8185a4161b02d09c0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <sys/ddi_subrdefs.h>
#include <sys/xpv_support.h>
#include <sys/xen_errno.h>
#include <sys/hypervisor.h>
#include <sys/xenbus_comms.h>
#include <sys/xenbus_impl.h>
#include <sys/sysmacros.h>
#include <sys/x86_archext.h>
#include <vm/seg_kmem.h>
#define XPV_MINOR 0
#define XPV_BUFSIZE 128
/*
* This structure is ordinarily constructed by Xen. In the HVM world, we
* manually fill in the few fields the PV drivers need.
*/
/* Xen version number. */
/* Metadata page shared between domain and Xen */
/* Page containing code to issue hypercalls. */
extern caddr_t hypercall_page;
/* Is the hypervisor 64-bit? */
int xen_is_64bit = -1;
/* virtual addr for the store_mfn page */
static dev_info_t *xpvd_dip;
/* saved pfn of the shared info page */
static pfn_t shared_info_frame;
#ifdef DEBUG
int xen_suspend_debug;
#else
#define SUSPEND_DEBUG(...)
#endif
/*
* Forward declarations
*/
static struct cb_ops xpv_cb_ops = {
nulldev, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
xpv_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
NULL,
D_MP,
NULL,
};
static struct dev_ops xpv_dv_ops = {
0,
nulldev, /* identify */
nulldev, /* probe */
nodev, /* reset */
NULL, /* struct bus_ops */
NULL /* power */
};
"xpv driver %I%",
};
static struct modlinkage modl = {
{
(void *)&modldrv,
NULL /* null termination */
}
};
static ddi_dma_attr_t xpv_dma_attr = {
DMA_ATTR_V0, /* version of this structure */
0, /* lowest usable address */
0xffffffffffffffffULL, /* highest usable address */
0x7fffffff, /* maximum DMAable byte count */
MMU_PAGESIZE, /* alignment in bytes */
0x7ff, /* bitmap of burst sizes */
1, /* minimum transfer */
0xffffffffU, /* maximum transfer */
0x7fffffffULL, /* maximum segment length */
1, /* maximum number of segments */
1, /* granularity */
0, /* flags (reserved) */
};
static ddi_device_acc_attr_t xpv_accattr = {
};
#define MAX_ALLOCATIONS 10
static int xen_alloc_cnt = 0;
void *
{
int a = xen_alloc_cnt++;
&xpv_dma_handle[a]) != DDI_SUCCESS)
return (NULL);
return (NULL);
}
return (addr);
}
/*
* This function is invoked twice, first time with reprogram=0 to set up
* the xpvd portion of the device tree. The second time it is ignored.
*/
static void
xpv_enumerate(int reprogram)
{
if (reprogram != 0)
return;
(void) ndi_devi_bind_driver(dip, 0);
/*
* Too early to enumerate split device drivers in domU
* since we need to create taskq thread during enumeration.
* So, we only enumerate softdevs and console here.
*/
}
/*
* Translate a hypervisor errcode to a Solaris error code.
*/
int
xen_xlate_errcode(int error)
{
switch (-error) {
default:
}
return (error);
}
/*PRINTFLIKE1*/
void
xen_printf(const char *fmt, ...)
{
}
/*
* Stub functions to get the FE drivers to build, and to catch drivers that
* misbehave in HVM domains.
*/
/*ARGSUSED*/
void
{
panic("xen_release_pfn() is not supported in HVM domains");
}
/*ARGSUSED*/
void
{
panic("reassign_pfn() is not supported in HVM domains");
}
/*ARGSUSED*/
long
{
panic("balloon_free_pages() is not supported in HVM domains");
return (0);
}
/*ARGSUSED*/
void
{
panic("balloon_drv_added() is not supported in HVM domains");
}
/*
* Add a mapping for the machine page at the given virtual address.
*/
void
{
}
static uint64_t
hvm_get_param(int param_id)
{
struct xen_hvm_param xhp;
return (-1);
}
static struct xenbus_watch shutdown_watch;
#define SHUTDOWN_INVALID -1
#define SHUTDOWN_POWEROFF 0
#define SHUTDOWN_REBOOT 1
#define SHUTDOWN_SUSPEND 2
#define SHUTDOWN_HALT 3
#define SHUTDOWN_MAX 4
static const char *cmd_strings[SHUTDOWN_MAX] = {
"poweroff",
"reboot",
"suspend",
"halt"
};
int
{
int error;
char buf[XPV_BUFSIZE];
SUSPEND_DEBUG("xen_suspend_devices\n");
return (ENXIO);
continue;
if (!i_ddi_devi_attached(dip)) {
error = DDI_FAILURE;
} else {
}
if (error == DDI_SUCCESS) {
} else {
SUSPEND_DEBUG("WARNING: Unable to suspend device %s\n",
return (ENXIO);
}
}
return (0);
}
int
{
int did_suspend;
int error = resume_failed;
char buf[XPV_BUFSIZE];
SUSPEND_DEBUG("xen_resume_devices\n");
}
/*
* cpr is the only one that uses this field and the device
* itself hasn't resumed yet, there is no need to use a
* lock, even though kernel threads are active by now.
*/
if (did_suspend)
/*
* There may be background attaches happening on devices
* that were not originally suspended by cpr, so resume
* only devices that were suspended by cpr. Also, stop
* resuming after the first resume failure, but traverse
* the entire tree to clear the suspend flag.
*/
if (did_suspend && !error) {
SUSPEND_DEBUG("Resuming device %s\n",
/*
* If a device suspended by cpr gets detached during
* the resume process (for example, due to hotplugging)
* before cpr gets around to issuing it a DDI_RESUME,
* we'll have problems.
*/
if (!i_ddi_devi_attached(dip)) {
"not ready for resume",
} else {
DDI_SUCCESS) {
}
}
}
}
}
return (error);
}
/*ARGSUSED*/
static int
{
char *name;
return (DDI_WALK_CONTINUE);
} else {
return (DDI_WALK_TERMINATE);
}
}
/*
*/
void
xen_suspend_domain(void)
{
extern void rtcsync(void);
extern void ec_resume(void);
struct xen_add_to_physmap xatp;
int err;
SUSPEND_DEBUG("xen_suspend_domain\n");
/*
* We only want to suspend the PV devices, since the emulated devices
* are suspended by saving the emulated device state. The PV devices
* are all children of the xpvd nexus device. So we search the
* device tree for the xpvd node to use as the root of the tree to
* be suspended.
*/
/*
* suspend interrupts and devices
*/
else
SUSPEND_DEBUG("xenbus_suspend\n");
/*
* Suspend on vcpu 0
*/
if (ncpus > 1)
/*
* We can grab the ec_lock as it's a spinlock with a high SPL. Hence
* any holder would have dropped it to get through pause_cpus().
*/
/*
* From here on in, we can't take locks.
*/
flags = intr_clear();
SUSPEND_DEBUG("HYPERVISOR_suspend\n");
/*
* At this point we suspend and sometime later resume.
* Note that this call may return with an indication of a cancelled
* for now no matter ehat the return we do a full resume of all
* suspended drivers, etc.
*/
(void) HYPERVISOR_shutdown(SHUTDOWN_suspend);
/*
* Point HYPERVISOR_shared_info to the proper place.
*/
SUSPEND_DEBUG("gnttab_resume\n");
SUSPEND_DEBUG("ec_resume\n");
ec_resume();
if (ncpus > 1)
start_cpus();
/*
* Now we can take locks again.
*/
rtcsync();
SUSPEND_DEBUG("xenbus_resume\n");
SUSPEND_DEBUG("xen_resume_devices\n");
SUSPEND_DEBUG("finished xen_suspend_domain\n");
}
static void
xen_dirty_shutdown(void *arg)
{
"timed out.\nShutting down.\n");
switch (cmd) {
case SHUTDOWN_HALT:
case SHUTDOWN_POWEROFF:
break;
case SHUTDOWN_REBOOT:
break;
}
}
static void
xen_shutdown(void *arg)
{
int err;
if (cmd == SHUTDOWN_SUSPEND) {
return;
}
if (err != DDI_SUCCESS)
goto failure;
if (err != DDI_SUCCESS)
goto failure;
goto failure;
(void) sysevent_attach_attributes(event,
if (err != 0)
goto failure;
return;
}
/*ARGSUSED*/
static void
unsigned int len)
{
char *str;
unsigned int slen;
if (err)
return;
return;
}
/*
* If this is a watch fired from our write below, check out early to
* avoid an infinite loop.
*/
(void) xenbus_transaction_end(xbt, 0);
return;
} else {
}
goto again;
}
if (shutdown_code != SHUTDOWN_INVALID) {
(void *)(intptr_t)shutdown_code, 0);
}
}
static int
{
struct cpuid_regs cp;
char *xen_str;
struct xen_add_to_physmap xatp;
int err;
/*
* Xen's pseudo-cpuid function 0x40000000 returns a string
* representing the Xen signature in %ebx, %ecx, and %edx.
* %eax contains the maximum supported cpuid function.
*/
(void) __cpuid_insn(&cp);
xen_signature[3] = 0;
xen_str = (char *)xen_signature;
"Attempting to load Xen drivers on non-Xen system");
return (-1);
}
/*
* cpuid function 0x40000001 returns the Xen version in %eax. The
* top 16 bits are the major version, the bottom 16 are the minor
* version.
*/
(void) __cpuid_insn(&cp);
/*
* The xpv driver is incompatible with xen versions older than 3.1. This
* is due to the changes in the vcpu_info and shared_info structs used
* to communicate with the hypervisor (the event channels in particular)
* that were introduced with 3.1.
*/
return (-1);
}
/*
* cpuid function 0x40000002 returns information about the
* hypercall page. %eax nominally contains the number of pages
* with hypercall code, but according to the Xen guys, "I'll
* guarantee that remains one forever more, so you can just
* allocate a single page and get quite upset if you ever see CPUID
* return more than one page." %ebx contains an MSR we use to ask
* Xen to remap each page at a specific pfn.
*/
(void) __cpuid_insn(&cp);
/*
* Let Xen know where we want the hypercall page mapped. We
* already have a page allocated in the .text section to simplify
* the wrapper code.
*/
/* Fill in the xen_info data */
/* Figure out whether the hypervisor is 32-bit or 64-bit. */
xen_is_64bit = 1;
xen_is_64bit = 0;
}
if (xen_is_64bit < 0) {
return (-1);
}
#ifdef __amd64
#endif
/*
* Allocate space for the shared_info page and tell Xen where it
* is.
*/
" error: %d", err);
return (-1);
}
/* Set up the grant tables. */
gnttab_init();
/* Set up event channel support */
return (-1);
/* Set up xenbus */
xs_domu_init();
return (0);
}
static void
{
ec_fini();
}
/*ARGSUSED*/
static int
{
return (DDI_FAILURE);
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
break;
case DDI_INFO_DEVT2INSTANCE:
*result = 0;
break;
default:
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
{
if (cmd != DDI_ATTACH)
return (DDI_FAILURE);
return (DDI_FAILURE);
if (xen_pv_init(dip) != 0)
return (DDI_FAILURE);
/*
* If the memscrubber attempts to scrub the pages we hand to Xen,
* the domain will panic.
*/
return (DDI_SUCCESS);
}
/*
* Attempts to reload the PV driver plumbing hang on Intel platforms, so
* we don't want to unload the framework by accident.
*/
int xpv_allow_detach = 0;
static int
{
return (DDI_FAILURE);
xen_pv_fini();
}
return (DDI_SUCCESS);
}
/*ARGSUSED1*/
static int
{
}
/*ARGSUSED*/
static int
int *rval_p)
{
return (EINVAL);
}
int
_init(void)
{
int err;
return (err);
return (0);
}
int
_fini(void)
{
int err;
return (err);
return (0);
}
int
{
}