xen_machdep.c revision 843e19887f64dde75055cf8842fc4db2171eff45
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* derived from netbsd's xen_machdep.c 1.1.2.1 */
/*
*
* 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
* 3. This section intentionally left blank.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 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.
*/
/*
* Section 3 of the above license was updated in response to bug 6379571.
*/
#include <sys/segments.h>
#include <sys/hypervisor.h>
#include <sys/machsystm.h>
#include <sys/bootconf.h>
#include <sys/bootinfo.h>
#include <sys/evtchn_impl.h>
#include <sys/archsystm.h>
#include <sys/mach_mmu.h>
#include <sys/processor.h>
#include <sys/xen_errno.h>
#include <sys/xpv_panic.h>
#include <sys/smp_impldefs.h>
#include <sys/balloon_impl.h>
/*
* Hypervisor-specific utility routines - these can be invoked from the
* normal control flow. It might be useful to partition these into
* different files, but let's see how it looks before we get too
* carried away with that idea.
*/
/*
* In the current absence of any useful way to debug domains that are hung
* whilst suspending, we have a more clumsy approach...
*/
#ifdef DEBUG
#else
#define SUSPEND_DEBUG(...)
#endif
int cpr_debug;
volatile int xen_suspending_cpus;
static int xen_suspend_debug;
void
{
struct callback_register cb;
#if defined(__amd64)
#endif
/*
* XXPV always ignore return value for NMI
*/
type != CALLBACKTYPE_nmi)
panic("HYPERVISOR_callback_op failed");
}
void
xen_init_callbacks(void)
{
/*
* register event (interrupt) handler.
*/
/*
* failsafe handler.
*/
/*
* NMI handler.
*/
/*
* system call handler
* XXPV move to init_cpu_syscall?
*/
#if defined(__amd64)
#endif /* __amd64 */
}
/*
* cmn_err() followed by a 1/4 second delay; this gives the
* logging service a chance to flush messages and helps avoid
* intermixing output from prom_printf().
* XXPV: doesn't exactly help us on UP though.
*/
/*PRINTFLIKE2*/
void
{
}
void
xen_suspend_devices(void)
{
int rc;
SUSPEND_DEBUG("xen_suspend_devices\n");
}
void
xen_resume_devices(void)
{
int rc;
SUSPEND_DEBUG("xen_resume_devices\n");
}
/*
* The list of mfn pages is out of date. Recompute it.
* XXPV: can we race against another suspend call? Think not.
*/
static void
rebuild_mfn_list(void)
{
int i = 0;
SUSPEND_DEBUG("rebuild_mfn_list\n");
if (((j * sizeof (mfn_t)) & MMU_PAGEOFFSET) == 0) {
(caddr_t)&mfn_list_pages[j]);
}
}
= pfn_to_mfn(pfn);
}
static void
suspend_cpus(void)
{
int i;
SUSPEND_DEBUG("suspend_cpus\n");
xen_suspending_cpus = 1;
SUSPEND_DEBUG("waiting for offline CPUs\n");
/*
* For us to proceed safely, all CPUs except the current one must be
* present in cpu_suspend_set. Running CPUs will participate in
* pause_cpus(), and eventually reach mach_cpu_pause(). Powered-off
* VCPUs will already be in the set, again in mach_cpu_pause().
* Finally, offline CPUs will be sitting in mach_cpu_idle().
*/
SMT_PAUSE();
for (i = 1; i < ncpus; i++) {
if (!CPU_IN_SET(cpu_suspend_lost_set, i)) {
SUSPEND_DEBUG("xen_vcpu_down %d\n", i);
(void) xen_vcpu_down(i);
}
mach_cpucontext_reset(cpu[i]);
}
}
static void
resume_cpus(void)
{
int i;
xen_suspending_cpus = 0;
for (i = 1; i < ncpus; i++) {
continue;
if (!CPU_IN_SET(cpu_suspend_lost_set, i)) {
SUSPEND_DEBUG("xen_vcpu_up %d\n", i);
(void) xen_vcpu_up(i);
}
}
start_cpus();
}
/*
*/
void
xen_suspend_domain(void)
{
extern void rtcsync(void);
extern hrtime_t hres_last_tick;
int i;
/*
* XXPV - Are we definitely OK to suspend by the time we've connected
* the handler?
*/
SUSPEND_DEBUG("xen_suspend_domain\n");
/*
* suspend interrupts and devices
* cpr) and for migration. Would be nice to know the difference if
* may want to do more of the things that cpr does. (i.e. notify user
* processes, shrink memory footprint for faster restore, etc.)
*/
SUSPEND_DEBUG("xenbus_suspend\n");
/*
* XXPV: cpu hotplug can hold this under a xenbus watch. Are we safe
* wrt xenbus being suspended here?
*/
/*
* Suspend must be done on vcpu 0, as no context for other CPUs is
* saved.
*
* XXPV - add to taskq API ?
*/
SUSPEND_DEBUG("xen_start_migrate\n");
if (ncpus > 1)
suspend_cpus();
/*
* 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 suspend_cpus().
*/
/*
* From here on in, we can't take locks.
*/
SUSPEND_DEBUG("ec_suspend\n");
ec_suspend();
SUSPEND_DEBUG("gnttab_suspend\n");
flags = intr_clear();
/*
* Currently, the hypervisor incorrectly fails to bring back
* powered-down VCPUs. Thus we need to record any powered-down VCPUs
* to prevent any attempts to operate on them. But we have to do this
* *after* the very first time we do ec_suspend().
*/
for (i = 1; i < ncpus; i++) {
continue;
}
/*
* these into PFNs, but expects them to be, so we do it here.
* We don't use mfn_to_pfn() because so many OS services have
* been disabled at this point.
*/
prom_printf("xen_suspend_domain(): "
"CPU->cpu_m.mcpu_vcpu_info->evtchn_upcall_mask not set\n");
(void) HYPERVISOR_shutdown(SHUTDOWN_crash);
}
0, UVMF_INVLPG)) {
prom_printf("xen_suspend_domain(): "
"HYPERVISOR_update_va_mapping() failed\n");
(void) HYPERVISOR_shutdown(SHUTDOWN_crash);
}
SUSPEND_DEBUG("HYPERVISOR_suspend\n");
/*
* At this point we suspend and sometime later resume.
*/
if (HYPERVISOR_suspend(start_info_mfn)) {
prom_printf("xen_suspend_domain(): "
"HYPERVISOR_suspend() failed\n");
(void) HYPERVISOR_shutdown(SHUTDOWN_crash);
}
/*
* Point HYPERVISOR_shared_info to its new value.
*/
(void) HYPERVISOR_shutdown(SHUTDOWN_crash);
prom_printf("xen_suspend_domain(): number of pages"
" changed, was 0x%lx, now 0x%lx\n", mfn_count,
(void) HYPERVISOR_shutdown(SHUTDOWN_crash);
}
cached_max_mfn = 0;
SUSPEND_DEBUG("gnttab_resume\n");
/* XXPV: add a note that this must be lockless. */
SUSPEND_DEBUG("ec_resume\n");
ec_resume();
if (ncpus > 1)
resume_cpus();
/*
* Now we can take locks again.
*/
/*
* Force the tick value used for tv_nsec in hres_tick() to be up to
* date. rtcsync() will reset the hrestime value appropriately.
*/
/*
* XXPV: we need to have resumed the CPUs since this takes locks, but
* can remote CPUs see bad state? Presumably yes. Should probably nest
* taking of todlock inside of cpu_lock, or vice versa, then provide an
* unlocked version. Probably need to call clkinitf to reset cpu freq
* and re-calibrate if we migrated to a different speed cpu. Also need
* to make a (re)init_cpu_info call to update processor info structs
* and device tree info. That remains to be written at the moment.
*/
rtcsync();
SUSPEND_DEBUG("xenbus_resume\n");
SUSPEND_DEBUG("xenbus_resume_devices\n");
SUSPEND_DEBUG("finished xen_suspend_domain\n");
}
/*ARGSUSED*/
int
xen_debug_handler(void *arg)
{
debug_enter("External debug event received");
/*
* If we've not got KMDB loaded, output some stuff difficult to capture
* from a domain core.
*/
int i;
prom_printf("evtchn_pending [ ");
for (i = 0; i < 8; i++)
prom_printf("]\nevtchn_mask [ ");
for (i = 0; i < 8; i++)
prom_printf("]\n");
for (i = 0; i < ncpus; i++) {
continue;
prom_printf("CPU%d pending %d mask %d sel %lx\n",
}
}
return (0);
}
/*ARGSUSED*/
static void
unsigned int len)
{
char key = '\0';
int ret;
if (xenbus_transaction_start(&xbt)) {
return;
}
/*
* ENOENT happens in response to our own xenbus_rm.
* XXPV - this happens spuriously on boot?
*/
goto out;
}
goto out;
}
goto retry;
/*
* Somewhat arbitrary - on Linux this means 'reboot'. We could just
* accept any key, but this might increase the risk of sending a
* harmless sysrq to the wrong domain...
*/
if (key == 'b')
(void) xen_debug_handler(NULL);
else
return;
out:
}
volatile int shutdown_req_active;
#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"
};
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) {
shutdown_req_active = 0;
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 {
}
/*
* XXPV Should we check the value of xenbus_write() too, or are all
* errors automatically folded into xenbus_transaction_end() ??
*/
goto again;
}
if (shutdown_code != SHUTDOWN_INVALID) {
if (shutdown_code == SHUTDOWN_SUSPEND) {
while (shutdown_req_active)
SMT_PAUSE();
}
shutdown_req_active = 1;
(void *)(intptr_t)shutdown_code, 0);
}
}
static struct xenbus_watch shutdown_watch;
static struct xenbus_watch sysrq_watch;
void
xen_late_startup(void)
{
if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
if (register_xenbus_watch(&sysrq_watch))
}
}
#ifdef DEBUG
#define XEN_PRINTF_BUFSIZE 1024
/*
* Printf function that calls hypervisor directly. For DomU it only
* works when running on a xen hypervisor built with debug on. Works
* always since no I/O ring interaction is needed.
*/
/*PRINTFLIKE1*/
void
xen_printf(const char *fmt, ...)
{
(void) HYPERVISOR_console_io(CONSOLEIO_write,
}
#else
void
xen_printf(const char *fmt, ...)
{
}
#endif /* DEBUG */
/*
* Determine helpful version information.
*
* (And leave a copy around in the data segment so we can look
* at them later with e.g. kmdb.)
*/
struct xenver {
char *xv_ver;
char *xv_chgset;
char *xv_compiler;
char *xv_compile_date;
char *xv_compile_by;
char *xv_compile_domain;
char *xv_caps;
} xenver;
static char *
sprintf_alloc(const char *fmt, ...)
{
char *p;
return (p);
}
void
xen_version(void)
{
static const char strfmt[] = "%s";
union {
} else
}
/*
* XXPV - Solaris guests currently require special version of
* the hypervisor from Sun to function properly called "3.0.4-1-xvm".
* This version is based on "3.0.4-1" plus changes from
* Sun that are a work-in-progress.
*
* This version check will disappear after appropriate fixes
* are accepted upstream.
*/
}
}
/*
* Capabilities are a set of space separated ascii strings
* e.g. 'xen-3.1-x86_32p' or 'hvm-3.2-x86_64'
*/
}
}
/*
* Miscellaneous hypercall wrappers with slightly more verbose diagnostics.
*/
void
{
int err;
/*
* X_EINVAL: reserved entry or bad frames
* X_EFAULT: bad address
*/
panic("xen_set_gdt(%p, %d): error %d",
}
}
void
{
long err;
panic("xen_set_ldt(%p, %d): error %d",
}
}
void
{
long err;
/*
* X_EPERM: bad selector
*/
-(int)err);
}
}
long
{
long err;
/*
* X_EFAULT: bad address
* X_EPERM: bad selector
*/
-(int)err);
}
return (err);
}
#if defined(__amd64)
void
{
long err;
/*
* X_EFAULT: bad address
* X_EINVAL: bad type
*/
panic("xen_set_segment_base(%d, %lx): error %d",
}
}
#endif /* __amd64 */
/*
* Translate a hypervisor errcode to a Solaris error code.
*/
int
xen_xlate_errcode(int error)
{
switch (-error) {
/*
* Translate hypervisor errno's into native errno's
*/
default:
}
return (error);
}
/*
* Raise PS_IOPL on current vcpu to user level.
* Caller responsible for preventing kernel preemption.
*/
void
xen_enable_user_iopl(void)
{
}
/*
* Drop PS_IOPL on current vcpu to kernel level
*/
void
xen_disable_user_iopl(void)
{
}
int
{
int err;
#if defined(__amd64)
if (prot & PROT_WRITE)
pt_bits |= PT_WRITABLE;
#endif
MMU_PAGESIZE, prot)) != 0)
goto done;
#if defined(__amd64)
#endif
done:
if (err) {
err);
}
return (err);
}
int
{
int err;
#if defined(__amd64)
if (prot & PROT_WRITE)
pt_bits |= PT_WRITABLE;
#endif /* __amd64 */
goto done;
#if defined(__amd64)
while (npgs--) {
pt_bits)) != 0)
break;
}
#endif /* __amd64 */
done:
if (err) {
(void *)lva,
}
return (err);
}