#include <sys/types.h>
#include <sys/systm.h>
#include <sys/mutex.h>
#include <sys/time.h>
#include <sys/clock.h>
#include <sys/archsystm.h>
#include <sys/modctl.h>
#include <sys/autoconf.h>
#include <sys/cmn_err.h>
#include <sys/atomic.h>
#include <sys/hypervisor.h>
static timestruc_t
todxen_get(tod_ops_t *top)
{
todinfo_t tod;
timestruc_t ts, wcts;
shared_info_t *si = HYPERVISOR_shared_info;
uint32_t xen_wc_version;
hrtime_t now;
ASSERT(MUTEX_HELD(&tod_lock));
do {
xen_wc_version = si->wc_version;
membar_consumer();
wcts.tv_sec = si->wc_sec;
wcts.tv_nsec = si->wc_nsec;
membar_consumer();
} while ((si->wc_version & 1) | (xen_wc_version ^ si->wc_version));
now = xpv_getsystime() +
(hrtime_t)wcts.tv_nsec + (hrtime_t)wcts.tv_sec * NANOSEC;
ts.tv_sec = (time_t)(now / NANOSEC);
ts.tv_nsec = (long)(now % NANOSEC);
ts.tv_sec += ggmtl();
tod = utc_to_tod(ts.tv_sec);
if (tod.tod_year < 69) {
static int range_warn = 1;
if (range_warn) {
if (DOMAIN_IS_INITDOMAIN(xen_info))
(void) TODOP_GET(top->tod_next);
if (tod.tod_year > 38)
cmn_err(CE_WARN,
"hypervisor wall clock is out "
"of range -- time needs to be reset");
range_warn = 0;
}
tod.tod_year += 100;
ts.tv_sec = tod_to_utc(tod);
}
return (ts);
}
static void
todxen_set(tod_ops_t *top, timestruc_t ts)
{
xen_platform_op_t op;
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
ASSERT(MUTEX_HELD(&tod_lock));
TODOP_SET(top->tod_next, ts);
op.cmd = XENPF_settime;
op.interface_version = XENPF_INTERFACE_VERSION;
op.u.settime.secs = ts.tv_sec - ggmtl();
op.u.settime.nsecs = ts.tv_nsec;
op.u.settime.system_time = xpv_getsystime();
(void) HYPERVISOR_platform_op(&op);
}
}
static tod_ops_t todxen_ops = {
TOD_OPS_VERSION,
todxen_get,
todxen_set,
NULL
};
static struct modlmisc modlmisc = {
&mod_miscops,
"TOD module for i86xpv"
};
static struct modlinkage modl = {
MODREV_1,
&modlmisc
};
int
_init(void)
{
if (tod_ops->tod_version != todxen_ops.tod_version)
panic("TOD module version mismatch");
todxen_ops.tod_next = tod_ops;
tod_ops = &todxen_ops;
return (mod_install(&modl));
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfo)
{
return (mod_info(&modl, modinfo));
}