#include <sys/param.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/cpuvar.h>
#include <sys/timer.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/cyclic.h>
static void realitexpire(void *);
static void realprofexpire(void *);
static void timeval_advance(struct timeval *, struct timeval *);
kmutex_t tod_lock;
int itimer_realprof_minimum = 500;
#define TVTSCMP(tvp, tsp, cmp) \
\
((tvp)->tv_sec cmp (tsp)->tv_sec || \
((tvp)->tv_sec == (tsp)->tv_sec && \
\
(tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec))
void
uniqtime(struct timeval *tv)
{
static struct timeval last;
static int last_timechanged;
timestruc_t ts;
time_t sec;
int usec, nsec;
mutex_enter(&tod_lock);
gethrestime(&ts);
nsec = ts.tv_nsec;
usec = nsec + (nsec >> 2);
usec = nsec + (usec >> 1);
usec = nsec + (usec >> 2);
usec = nsec + (usec >> 4);
usec = nsec - (usec >> 3);
usec = nsec + (usec >> 2);
usec = nsec + (usec >> 3);
usec = nsec + (usec >> 4);
usec = nsec + (usec >> 1);
usec = nsec + (usec >> 6);
usec = usec >> 10;
sec = ts.tv_sec;
if (last_timechanged != timechanged) {
last_timechanged = timechanged;
}
else if ((sec <= last.tv_sec) &&
((sec != last.tv_sec) ||
(usec <= last.tv_usec)) &&
((last.tv_sec - sec) <= 5)) {
sec = last.tv_sec;
usec = last.tv_usec + 1;
if (usec >= MICROSEC) {
usec -= MICROSEC;
sec++;
}
}
last.tv_sec = sec;
last.tv_usec = usec;
mutex_exit(&tod_lock);
tv->tv_sec = sec;
tv->tv_usec = usec;
}
void
uniqtime32(struct timeval32 *tv32p)
{
struct timeval tv;
uniqtime(&tv);
TIMEVAL_TO_TIMEVAL32(tv32p, &tv);
}
int
gettimeofday(struct timeval *tp)
{
struct timeval atv;
if (tp) {
uniqtime(&atv);
if (get_udatamodel() == DATAMODEL_NATIVE) {
if (copyout(&atv, tp, sizeof (atv)))
return (set_errno(EFAULT));
} else {
struct timeval32 tv32;
if (TIMEVAL_OVERFLOW(&atv))
return (set_errno(EOVERFLOW));
TIMEVAL_TO_TIMEVAL32(&tv32, &atv);
if (copyout(&tv32, tp, sizeof (tv32)))
return (set_errno(EFAULT));
}
}
return (0);
}
int
getitimer(uint_t which, struct itimerval *itv)
{
int error;
if (get_udatamodel() == DATAMODEL_NATIVE)
error = xgetitimer(which, itv, 0);
else {
struct itimerval kitv;
if ((error = xgetitimer(which, &kitv, 1)) == 0) {
if (ITIMERVAL_OVERFLOW(&kitv)) {
error = EOVERFLOW;
} else {
struct itimerval32 itv32;
ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv);
if (copyout(&itv32, itv, sizeof (itv32)) != 0)
error = EFAULT;
}
}
}
return (error ? (set_errno(error)) : 0);
}
int
xgetitimer(uint_t which, struct itimerval *itv, int iskaddr)
{
struct proc *p = curproc;
struct timeval now;
struct itimerval aitv;
hrtime_t ts, first, interval, remain;
mutex_enter(&p->p_lock);
switch (which) {
case ITIMER_VIRTUAL:
case ITIMER_PROF:
aitv = ttolwp(curthread)->lwp_timer[which];
break;
case ITIMER_REAL:
uniqtime(&now);
aitv = p->p_realitimer;
if (timerisset(&aitv.it_value)) {
if (timercmp(&aitv.it_value, &now, <)) {
timerclear(&aitv.it_value);
} else {
timevalsub(&aitv.it_value, &now);
}
}
break;
case ITIMER_REALPROF:
if (curproc->p_rprof_cyclic == CYCLIC_NONE) {
bzero(&aitv, sizeof (aitv));
break;
}
aitv = curproc->p_rprof_timer;
first = tv2hrt(&aitv.it_value);
interval = tv2hrt(&aitv.it_interval);
if ((ts = gethrtime()) < first) {
remain = first - ts;
} else {
if (interval == 0) {
remain = 0;
} else {
remain = interval - ((ts - first) % interval);
}
}
hrt2tv(remain, &aitv.it_value);
break;
default:
mutex_exit(&p->p_lock);
return (EINVAL);
}
mutex_exit(&p->p_lock);
if (iskaddr) {
bcopy(&aitv, itv, sizeof (*itv));
} else {
ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
if (copyout(&aitv, itv, sizeof (*itv)))
return (EFAULT);
}
return (0);
}
int
setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv)
{
int error;
if (oitv != NULL)
if ((error = getitimer(which, oitv)) != 0)
return (error);
if (itv == NULL)
return (0);
if (get_udatamodel() == DATAMODEL_NATIVE)
error = xsetitimer(which, itv, 0);
else {
struct itimerval32 itv32;
struct itimerval kitv;
if (copyin(itv, &itv32, sizeof (itv32)))
error = EFAULT;
ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32);
error = xsetitimer(which, &kitv, 1);
}
return (error ? (set_errno(error)) : 0);
}
int
xsetitimer(uint_t which, struct itimerval *itv, int iskaddr)
{
struct itimerval aitv;
struct timeval now;
struct proc *p = curproc;
kthread_t *t;
timeout_id_t tmp_id;
cyc_handler_t hdlr;
cyc_time_t when;
cyclic_id_t cyclic;
hrtime_t ts;
int min;
if (itv == NULL)
return (0);
if (iskaddr) {
bcopy(itv, &aitv, sizeof (aitv));
} else {
ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
if (copyin(itv, &aitv, sizeof (aitv)))
return (EFAULT);
}
if (which == ITIMER_REALPROF) {
min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)),
itimer_realprof_minimum);
} else {
min = usec_per_tick;
}
if (itimerfix(&aitv.it_value, min) ||
(itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value)))
return (EINVAL);
mutex_enter(&p->p_lock);
switch (which) {
case ITIMER_REAL:
if (p->p_flag & SITBUSY) {
mutex_exit(&p->p_lock);
return (0);
}
p->p_flag |= SITBUSY;
while ((tmp_id = p->p_itimerid) != 0) {
p->p_itimerid = 0;
mutex_exit(&p->p_lock);
(void) untimeout(tmp_id);
mutex_enter(&p->p_lock);
}
if (timerisset(&aitv.it_value)) {
uniqtime(&now);
timevaladd(&aitv.it_value, &now);
p->p_itimerid = realtime_timeout(realitexpire,
p, hzto(&aitv.it_value));
}
p->p_realitimer = aitv;
p->p_flag &= ~SITBUSY;
break;
case ITIMER_REALPROF:
cyclic = p->p_rprof_cyclic;
p->p_rprof_cyclic = CYCLIC_NONE;
mutex_exit(&p->p_lock);
mutex_enter(&cpu_lock);
if (cyclic != CYCLIC_NONE)
cyclic_remove(cyclic);
if (!timerisset(&aitv.it_value)) {
mutex_exit(&cpu_lock);
return (0);
}
hdlr.cyh_func = realprofexpire;
hdlr.cyh_arg = p;
hdlr.cyh_level = CY_LOW_LEVEL;
when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value));
when.cyt_interval = tv2hrt(&aitv.it_interval);
if (when.cyt_interval == 0) {
when.cyt_interval = INT64_MAX - when.cyt_when;
}
cyclic = cyclic_add(&hdlr, &when);
mutex_exit(&cpu_lock);
mutex_enter(&p->p_lock);
if (p->p_rprof_cyclic != CYCLIC_NONE) {
mutex_exit(&p->p_lock);
mutex_enter(&cpu_lock);
cyclic_remove(cyclic);
mutex_exit(&cpu_lock);
return (0);
}
hrt2tv(ts, &aitv.it_value);
p->p_rprof_timer = aitv;
p->p_rprof_cyclic = cyclic;
t = p->p_tlist;
do {
struct itimerval *itvp;
itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF];
timerclear(&itvp->it_interval);
timerclear(&itvp->it_value);
if (t->t_rprof != NULL)
continue;
t->t_rprof =
kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP);
aston(t);
} while ((t = t->t_forw) != p->p_tlist);
break;
case ITIMER_VIRTUAL:
ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv;
break;
case ITIMER_PROF:
if (p->p_rprof_cyclic != CYCLIC_NONE) {
break;
}
ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv;
break;
default:
mutex_exit(&p->p_lock);
return (EINVAL);
}
mutex_exit(&p->p_lock);
return (0);
}
void
delete_itimer_realprof(void)
{
kthread_t *t = curthread;
struct proc *p = ttoproc(t);
klwp_t *lwp = ttolwp(t);
cyclic_id_t cyclic;
mutex_enter(&p->p_lock);
ASSERT(t == p->p_tlist && t == t->t_forw);
if ((cyclic = p->p_rprof_cyclic) == CYCLIC_NONE) {
mutex_exit(&p->p_lock);
} else {
p->p_rprof_cyclic = CYCLIC_NONE;
if (lwp->lwp_cursig == SIGPROF) {
lwp->lwp_cursig = 0;
lwp->lwp_extsig = 0;
if (lwp->lwp_curinfo) {
siginfofree(lwp->lwp_curinfo);
lwp->lwp_curinfo = NULL;
}
}
sigdelset(&p->p_sig, SIGPROF);
sigdelset(&p->p_extsig, SIGPROF);
sigdelq(p, NULL, SIGPROF);
sigdelset(&t->t_sig, SIGPROF);
sigdelset(&t->t_extsig, SIGPROF);
sigdelq(p, t, SIGPROF);
mutex_exit(&p->p_lock);
mutex_enter(&cpu_lock);
cyclic_remove(cyclic);
mutex_exit(&cpu_lock);
}
}
static void
realitexpire(void *arg)
{
struct proc *p = arg;
struct timeval *valp = &p->p_realitimer.it_value;
struct timeval *intervalp = &p->p_realitimer.it_interval;
#if !defined(_LP64)
clock_t ticks;
#endif
mutex_enter(&p->p_lock);
#if !defined(_LP64)
if ((ticks = hzto(valp)) > 1) {
p->p_itimerid = realtime_timeout(realitexpire, p, ticks);
mutex_exit(&p->p_lock);
return;
}
#endif
sigtoproc(p, NULL, SIGALRM);
if (!timerisset(intervalp)) {
timerclear(valp);
p->p_itimerid = 0;
} else {
timeval_advance(valp, intervalp);
p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp));
}
mutex_exit(&p->p_lock);
}
static void
realprofexpire(void *arg)
{
struct proc *p = arg;
kthread_t *t;
mutex_enter(&p->p_lock);
if (p->p_rprof_cyclic == CYCLIC_NONE ||
(t = p->p_tlist) == NULL) {
mutex_exit(&p->p_lock);
return;
}
do {
int mstate;
if (t->t_rprof == NULL)
t->t_rprof = kmem_zalloc(sizeof (struct rprof),
KM_NOSLEEP);
if (t->t_rprof == NULL)
continue;
thread_lock(t);
switch (t->t_state) {
case TS_SLEEP:
if (!(t->t_schedflag & TS_LOAD)) {
mstate = LMS_SLEEP;
break;
}
switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) {
case LMS_TFAULT:
case LMS_DFAULT:
case LMS_KFAULT:
case LMS_USER_LOCK:
break;
default:
mstate = LMS_SLEEP;
break;
}
break;
case TS_RUN:
case TS_WAIT:
mstate = LMS_WAIT_CPU;
break;
case TS_ONPROC:
switch (mstate = t->t_mstate) {
case LMS_USER:
case LMS_SYSTEM:
case LMS_TRAP:
break;
default:
mstate = LMS_SYSTEM;
break;
}
break;
default:
mstate = t->t_mstate;
break;
}
t->t_rprof->rp_anystate = 1;
t->t_rprof->rp_state[mstate]++;
aston(t);
if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
poke_cpu(t->t_cpu->cpu_id);
thread_unlock(t);
} while ((t = t->t_forw) != p->p_tlist);
mutex_exit(&p->p_lock);
}
static void
timeval_advance(struct timeval *valp, struct timeval *intervalp)
{
int cnt2nth;
struct timeval interval2nth;
for (;;) {
interval2nth = *intervalp;
for (cnt2nth = 0; ; cnt2nth++) {
timevaladd(valp, &interval2nth);
if (TVTSCMP(valp, &hrestime, >))
break;
timevaladd(&interval2nth, &interval2nth);
}
if (cnt2nth == 0)
break;
timevalsub(valp, &interval2nth);
}
}
int
itimerfix(struct timeval *tv, int minimum)
{
if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
tv->tv_usec < 0 || tv->tv_usec >= MICROSEC)
return (EINVAL);
if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum)
tv->tv_usec = minimum;
return (0);
}
int
itimerspecfix(timespec_t *tv)
{
if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC)
return (EINVAL);
return (0);
}
int
itimerdecr(struct itimerval *itp, int usec)
{
if (itp->it_value.tv_usec < usec) {
if (itp->it_value.tv_sec == 0) {
usec -= itp->it_value.tv_usec;
goto expire;
}
itp->it_value.tv_usec += MICROSEC;
itp->it_value.tv_sec--;
}
itp->it_value.tv_usec -= usec;
usec = 0;
if (timerisset(&itp->it_value))
return (1);
expire:
if (timerisset(&itp->it_interval)) {
itp->it_value = itp->it_interval;
itp->it_value.tv_usec -= usec;
if (itp->it_value.tv_usec < 0) {
itp->it_value.tv_usec += MICROSEC;
itp->it_value.tv_sec--;
}
} else
itp->it_value.tv_usec = 0;
return (0);
}
void
timevaladd(struct timeval *t1, struct timeval *t2)
{
t1->tv_sec += t2->tv_sec;
t1->tv_usec += t2->tv_usec;
timevalfix(t1);
}
void
timevalsub(struct timeval *t1, struct timeval *t2)
{
t1->tv_sec -= t2->tv_sec;
t1->tv_usec -= t2->tv_usec;
timevalfix(t1);
}
void
timevalfix(struct timeval *t1)
{
if (t1->tv_usec < 0) {
t1->tv_sec--;
t1->tv_usec += MICROSEC;
}
if (t1->tv_usec >= MICROSEC) {
t1->tv_sec++;
t1->tv_usec -= MICROSEC;
}
}
void
timespecadd(timespec_t *t1, timespec_t *t2)
{
t1->tv_sec += t2->tv_sec;
t1->tv_nsec += t2->tv_nsec;
timespecfix(t1);
}
void
timespecsub(timespec_t *t1, timespec_t *t2)
{
t1->tv_sec -= t2->tv_sec;
t1->tv_nsec -= t2->tv_nsec;
timespecfix(t1);
}
void
timespecfix(timespec_t *t1)
{
if (t1->tv_nsec < 0) {
t1->tv_sec--;
t1->tv_nsec += NANOSEC;
} else {
if (t1->tv_nsec >= NANOSEC) {
t1->tv_sec++;
t1->tv_nsec -= NANOSEC;
}
}
}
clock_t
hzto(struct timeval *tv)
{
timespec_t ts, now;
ts.tv_sec = tv->tv_sec;
ts.tv_nsec = tv->tv_usec * 1000;
gethrestime_lasttick(&now);
return (timespectohz(&ts, now));
}
clock_t
timespectohz(timespec_t *tv, timespec_t now)
{
clock_t ticks;
time_t sec;
int nsec;
sec = tv->tv_sec - now.tv_sec;
nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1;
if (nsec < 0) {
sec--;
nsec += NANOSEC;
} else if (nsec >= NANOSEC) {
sec++;
nsec -= NANOSEC;
}
ticks = NSEC_TO_TICK(nsec);
if (sec < 0 || (sec == 0 && ticks < 1))
ticks = 1;
else if (sec > (LONG_MAX - ticks) / hz)
ticks = LONG_MAX;
else
ticks += sec * hz;
return (ticks);
}
int64_t
timespectohz64(timespec_t *tv)
{
int64_t ticks;
int64_t sec;
int64_t nsec;
sec = tv->tv_sec;
nsec = tv->tv_nsec + nsec_per_tick - 1;
if (nsec < 0) {
sec--;
nsec += NANOSEC;
} else if (nsec >= NANOSEC) {
sec++;
nsec -= NANOSEC;
}
ticks = NSEC_TO_TICK(nsec);
if (sec < 0 || (sec == 0 && ticks < 1))
ticks = 1;
else if (sec > (((~0ULL) >> 1) - ticks) / hz)
ticks = (~0ULL) >> 1;
else
ticks += sec * hz;
return (ticks);
}
void
hrt2ts(hrtime_t hrt, timestruc_t *tsp)
{
#if defined(__amd64)
tsp->tv_sec = hrt / NANOSEC;
tsp->tv_nsec = hrt % NANOSEC;
#else
uint32_t sec, nsec, tmp;
tmp = (uint32_t)(hrt >> 30);
sec = tmp - (tmp >> 2);
sec = tmp - (sec >> 5);
sec = tmp + (sec >> 1);
sec = tmp - (sec >> 6) + 7;
sec = tmp - (sec >> 3);
sec = tmp + (sec >> 1);
sec = tmp + (sec >> 3);
sec = tmp + (sec >> 4);
tmp = (sec << 7) - sec - sec - sec;
tmp = (tmp << 7) - tmp - tmp - tmp;
tmp = (tmp << 7) - tmp - tmp - tmp;
nsec = (uint32_t)hrt - (tmp << 9);
while (nsec >= NANOSEC) {
nsec -= NANOSEC;
sec++;
}
tsp->tv_sec = (time_t)sec;
tsp->tv_nsec = nsec;
#endif
}
hrtime_t
ts2hrt(const timestruc_t *tsp)
{
#if defined(__x86)
return ((tsp->tv_sec * NANOSEC) + tsp->tv_nsec);
#else
hrtime_t hrt;
hrt = tsp->tv_sec;
hrt = (hrt << 7) - hrt - hrt - hrt;
hrt = (hrt << 7) - hrt - hrt - hrt;
hrt = (hrt << 7) - hrt - hrt - hrt;
hrt = (hrt << 9) + tsp->tv_nsec;
return (hrt);
#endif
}
void
hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p)
{
timestruc_t ts;
hrt2ts(hrt, &ts);
TIMESPEC_TO_TIMESPEC32(ts32p, &ts);
}
hrtime_t
tv2hrt(struct timeval *tvp)
{
return ((hrtime_t)tvp->tv_sec * NANOSEC +
(hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC));
}
void
hrt2tv(hrtime_t hrt, struct timeval *tvp)
{
#if defined(__amd64)
tvp->tv_sec = hrt / NANOSEC;
tvp->tv_usec = (hrt % NANOSEC) / (NANOSEC / MICROSEC);
#else
uint32_t sec, nsec, tmp;
uint32_t q, r, t;
tmp = (uint32_t)(hrt >> 30);
sec = tmp - (tmp >> 2);
sec = tmp - (sec >> 5);
sec = tmp + (sec >> 1);
sec = tmp - (sec >> 6) + 7;
sec = tmp - (sec >> 3);
sec = tmp + (sec >> 1);
sec = tmp + (sec >> 3);
sec = tmp + (sec >> 4);
tmp = (sec << 7) - sec - sec - sec;
tmp = (tmp << 7) - tmp - tmp - tmp;
tmp = (tmp << 7) - tmp - tmp - tmp;
nsec = (uint32_t)hrt - (tmp << 9);
while (nsec >= NANOSEC) {
nsec -= NANOSEC;
sec++;
}
tvp->tv_sec = (time_t)sec;
t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12);
q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14);
q = q >> 9;
r = nsec - q*1000;
tvp->tv_usec = q + ((r + 24) >> 10);
#endif
}
int
nanosleep(timespec_t *rqtp, timespec_t *rmtp)
{
timespec_t rqtime;
timespec_t rmtime;
timespec_t now;
int timecheck;
int ret = 1;
model_t datamodel = get_udatamodel();
timecheck = timechanged;
gethrestime(&now);
if (datamodel == DATAMODEL_NATIVE) {
if (copyin(rqtp, &rqtime, sizeof (rqtime)))
return (set_errno(EFAULT));
} else {
timespec32_t rqtime32;
if (copyin(rqtp, &rqtime32, sizeof (rqtime32)))
return (set_errno(EFAULT));
TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32);
}
if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 ||
rqtime.tv_nsec >= NANOSEC)
return (set_errno(EINVAL));
if (timerspecisset(&rqtime)) {
timespecadd(&rqtime, &now);
mutex_enter(&curthread->t_delay_lock);
while ((ret = cv_waituntil_sig(&curthread->t_delay_cv,
&curthread->t_delay_lock, &rqtime, timecheck)) > 0)
continue;
mutex_exit(&curthread->t_delay_lock);
}
if (rmtp) {
rmtime.tv_sec = rmtime.tv_nsec = 0;
if (ret == 0) {
timespec_t delta = rqtime;
gethrestime(&now);
timespecsub(&delta, &now);
if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
delta.tv_nsec > 0))
rmtime = delta;
}
if (datamodel == DATAMODEL_NATIVE) {
if (copyout(&rmtime, rmtp, sizeof (rmtime)))
return (set_errno(EFAULT));
} else {
timespec32_t rmtime32;
TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
if (copyout(&rmtime32, rmtp, sizeof (rmtime32)))
return (set_errno(EFAULT));
}
}
if (ret == 0)
return (set_errno(EINTR));
return (0);
}
static int days_thru_month[64] = {
0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0,
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
};
todinfo_t saved_tod;
int saved_utc = -60;
todinfo_t
utc_to_tod(time_t utc)
{
long dse, day, month, year;
todinfo_t tod;
ASSERT(MUTEX_HELD(&tod_lock));
if (utc < 0)
utc = 0;
saved_tod.tod_sec += utc - saved_utc;
saved_utc = utc;
if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60)
return (saved_tod);
dse = utc / 86400;
tod.tod_sec = utc % 60;
tod.tod_min = (utc % 3600) / 60;
tod.tod_hour = (utc % 86400) / 3600;
tod.tod_dow = (dse + 4) % 7 + 1;
year = dse / 365 + 72;
do {
year--;
day = dse - 365 * (year - 70) - ((year - 69) >> 2);
} while (day < 0);
month = ((year & 3) << 4) + 1;
while (day >= days_thru_month[month + 1])
month++;
tod.tod_day = day - days_thru_month[month] + 1;
tod.tod_month = month & 15;
tod.tod_year = year;
saved_tod = tod;
return (tod);
}
time_t
tod_to_utc(todinfo_t tod)
{
time_t utc;
int year = tod.tod_year;
int month = tod.tod_month + ((year & 3) << 4);
#ifdef DEBUG
static int year_warn = 1;
static int month_warn = 1;
static int day_warn = 1;
static int hour_warn = 1;
static int min_warn = 1;
static int sec_warn = 1;
int days_diff = days_thru_month[month + 1] - days_thru_month[month];
#endif
ASSERT(MUTEX_HELD(&tod_lock));
#ifdef DEBUG
if (year_warn && (year < 70 || year > 8029)) {
cmn_err(CE_WARN,
"The hardware real-time clock appears to have the "
"wrong years value %d -- time needs to be reset\n",
year);
year_warn = 0;
}
if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) {
cmn_err(CE_WARN,
"The hardware real-time clock appears to have the "
"wrong months value %d -- time needs to be reset\n",
tod.tod_month);
month_warn = 0;
}
if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) {
cmn_err(CE_WARN,
"The hardware real-time clock appears to have the "
"wrong days value %d -- time needs to be reset\n",
tod.tod_day);
day_warn = 0;
}
if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) {
cmn_err(CE_WARN,
"The hardware real-time clock appears to have the "
"wrong hours value %d -- time needs to be reset\n",
tod.tod_hour);
hour_warn = 0;
}
if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) {
cmn_err(CE_WARN,
"The hardware real-time clock appears to have the "
"wrong minutes value %d -- time needs to be reset\n",
tod.tod_min);
min_warn = 0;
}
if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) {
cmn_err(CE_WARN,
"The hardware real-time clock appears to have the "
"wrong seconds value %d -- time needs to be reset\n",
tod.tod_sec);
sec_warn = 0;
}
#endif
utc = (year - 70);
utc += (utc << 3) + (utc << 6);
utc += (utc << 2) + ((year - 69) >> 2);
utc += days_thru_month[month] + tod.tod_day - 1;
utc = (utc << 3) + (utc << 4) + tod.tod_hour;
utc = (utc << 6) - (utc << 2) + tod.tod_min;
utc = (utc << 6) - (utc << 2) + tod.tod_sec;
return (utc);
}