xref: /onnv-gate/usr/src/uts/common/os/timers.c (revision 3792)
10Sstevel@tonic-gate /*
23642Sqiao  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  * Copyright (c) 1982, 1986 Regents of the University of California.
100Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
110Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #include <sys/param.h>
150Sstevel@tonic-gate #include <sys/user.h>
160Sstevel@tonic-gate #include <sys/vnode.h>
170Sstevel@tonic-gate #include <sys/proc.h>
180Sstevel@tonic-gate #include <sys/time.h>
190Sstevel@tonic-gate #include <sys/systm.h>
200Sstevel@tonic-gate #include <sys/kmem.h>
210Sstevel@tonic-gate #include <sys/cmn_err.h>
220Sstevel@tonic-gate #include <sys/cpuvar.h>
230Sstevel@tonic-gate #include <sys/timer.h>
240Sstevel@tonic-gate #include <sys/debug.h>
250Sstevel@tonic-gate #include <sys/sysmacros.h>
260Sstevel@tonic-gate #include <sys/cyclic.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate static void	realitexpire(void *);
290Sstevel@tonic-gate static void	realprofexpire(void *);
300Sstevel@tonic-gate static void	timeval_advance(struct timeval *, struct timeval *);
310Sstevel@tonic-gate 
320Sstevel@tonic-gate kmutex_t tod_lock;	/* protects time-of-day stuff */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * Constant to define the minimum interval value of the ITIMER_REALPROF timer.
360Sstevel@tonic-gate  * Value is in microseconds; defaults to 500 usecs.  Setting this value
370Sstevel@tonic-gate  * significantly lower may allow for denial-of-service attacks.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate int itimer_realprof_minimum = 500;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * macro to compare a timeval to a timestruc
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	TVTSCMP(tvp, tsp, cmp) \
460Sstevel@tonic-gate 	/* CSTYLED */ \
470Sstevel@tonic-gate 	((tvp)->tv_sec cmp (tsp)->tv_sec || \
480Sstevel@tonic-gate 	((tvp)->tv_sec == (tsp)->tv_sec && \
490Sstevel@tonic-gate 	/* CSTYLED */ \
500Sstevel@tonic-gate 	(tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec))
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Time of day and interval timer support.
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * These routines provide the kernel entry points to get and set
560Sstevel@tonic-gate  * the time-of-day and per-process interval timers.  Subroutines
570Sstevel@tonic-gate  * here provide support for adding and subtracting timeval structures
580Sstevel@tonic-gate  * and decrementing interval timers, optionally reloading the interval
590Sstevel@tonic-gate  * timers when they expire.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * SunOS function to generate monotonically increasing time values.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate void
660Sstevel@tonic-gate uniqtime(struct timeval *tv)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	static struct timeval last;
690Sstevel@tonic-gate 	timestruc_t ts;
700Sstevel@tonic-gate 	time_t sec;
710Sstevel@tonic-gate 	int usec, nsec;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * protect modification of last
750Sstevel@tonic-gate 	 */
760Sstevel@tonic-gate 	mutex_enter(&tod_lock);
770Sstevel@tonic-gate 	gethrestime(&ts);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * Fast algorithm to convert nsec to usec -- see hrt2ts()
810Sstevel@tonic-gate 	 * in common/os/timers.c for a full description.
820Sstevel@tonic-gate 	 */
830Sstevel@tonic-gate 	nsec = ts.tv_nsec;
840Sstevel@tonic-gate 	usec = nsec + (nsec >> 2);
850Sstevel@tonic-gate 	usec = nsec + (usec >> 1);
860Sstevel@tonic-gate 	usec = nsec + (usec >> 2);
870Sstevel@tonic-gate 	usec = nsec + (usec >> 4);
880Sstevel@tonic-gate 	usec = nsec - (usec >> 3);
890Sstevel@tonic-gate 	usec = nsec + (usec >> 2);
900Sstevel@tonic-gate 	usec = nsec + (usec >> 3);
910Sstevel@tonic-gate 	usec = nsec + (usec >> 4);
920Sstevel@tonic-gate 	usec = nsec + (usec >> 1);
930Sstevel@tonic-gate 	usec = nsec + (usec >> 6);
940Sstevel@tonic-gate 	usec = usec >> 10;
950Sstevel@tonic-gate 	sec = ts.tv_sec;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	/*
980Sstevel@tonic-gate 	 * Try to keep timestamps unique, but don't be obsessive about
990Sstevel@tonic-gate 	 * it in the face of large differences.
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	if ((sec <= last.tv_sec) &&		/* same or lower seconds, and */
1020Sstevel@tonic-gate 	    ((sec != last.tv_sec) ||		/* either different second or */
1030Sstevel@tonic-gate 	    (usec <= last.tv_usec)) &&		/* lower microsecond, and */
1040Sstevel@tonic-gate 	    ((last.tv_sec - sec) <= 5)) {	/* not way back in time */
1050Sstevel@tonic-gate 		sec = last.tv_sec;
1060Sstevel@tonic-gate 		usec = last.tv_usec + 1;
1070Sstevel@tonic-gate 		if (usec >= MICROSEC) {
1080Sstevel@tonic-gate 			usec -= MICROSEC;
1090Sstevel@tonic-gate 			sec++;
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	last.tv_sec = sec;
1130Sstevel@tonic-gate 	last.tv_usec = usec;
1140Sstevel@tonic-gate 	mutex_exit(&tod_lock);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	tv->tv_sec = sec;
1170Sstevel@tonic-gate 	tv->tv_usec = usec;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Timestamps are exported from the kernel in several places.
1220Sstevel@tonic-gate  * Such timestamps are commonly used for either uniqueness or for
1230Sstevel@tonic-gate  * sequencing - truncation to 32-bits is fine for uniqueness,
1240Sstevel@tonic-gate  * but sequencing is going to take more work as we get closer to 2038!
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate void
1270Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	struct timeval tv;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	uniqtime(&tv);
1320Sstevel@tonic-gate 	TIMEVAL_TO_TIMEVAL32(tv32p, &tv);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate int
1360Sstevel@tonic-gate gettimeofday(struct timeval *tp)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	struct timeval atv;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (tp) {
1410Sstevel@tonic-gate 		uniqtime(&atv);
1420Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1430Sstevel@tonic-gate 			if (copyout(&atv, tp, sizeof (atv)))
1440Sstevel@tonic-gate 				return (set_errno(EFAULT));
1450Sstevel@tonic-gate 		} else {
1460Sstevel@tonic-gate 			struct timeval32 tv32;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 			if (TIMEVAL_OVERFLOW(&atv))
1490Sstevel@tonic-gate 				return (set_errno(EOVERFLOW));
1500Sstevel@tonic-gate 			TIMEVAL_TO_TIMEVAL32(&tv32, &atv);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 			if (copyout(&tv32, tp, sizeof (tv32)))
1530Sstevel@tonic-gate 				return (set_errno(EFAULT));
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	return (0);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int
1600Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	int error;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE)
1650Sstevel@tonic-gate 		error = xgetitimer(which, itv, 0);
1660Sstevel@tonic-gate 	else {
1670Sstevel@tonic-gate 		struct itimerval kitv;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		if ((error = xgetitimer(which, &kitv, 1)) == 0) {
1700Sstevel@tonic-gate 			if (ITIMERVAL_OVERFLOW(&kitv)) {
1710Sstevel@tonic-gate 				error = EOVERFLOW;
1720Sstevel@tonic-gate 			} else {
1730Sstevel@tonic-gate 				struct itimerval32 itv32;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 				ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv);
1760Sstevel@tonic-gate 				if (copyout(&itv32, itv, sizeof (itv32)) != 0)
1770Sstevel@tonic-gate 					error = EFAULT;
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate int
1860Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	struct proc *p = curproc;
1890Sstevel@tonic-gate 	struct timeval now;
1900Sstevel@tonic-gate 	struct itimerval aitv;
1910Sstevel@tonic-gate 	hrtime_t ts, first, interval, remain;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	switch (which) {
1960Sstevel@tonic-gate 	case ITIMER_VIRTUAL:
1970Sstevel@tonic-gate 	case ITIMER_PROF:
1980Sstevel@tonic-gate 		aitv = ttolwp(curthread)->lwp_timer[which];
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	case ITIMER_REAL:
2020Sstevel@tonic-gate 		uniqtime(&now);
2030Sstevel@tonic-gate 		aitv = p->p_realitimer;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		if (timerisset(&aitv.it_value)) {
2060Sstevel@tonic-gate 			/*CSTYLED*/
2070Sstevel@tonic-gate 			if (timercmp(&aitv.it_value, &now, <)) {
2080Sstevel@tonic-gate 				timerclear(&aitv.it_value);
2090Sstevel@tonic-gate 			} else {
2100Sstevel@tonic-gate 				timevalsub(&aitv.it_value, &now);
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 		break;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	case ITIMER_REALPROF:
2160Sstevel@tonic-gate 		if (curproc->p_rprof_cyclic == CYCLIC_NONE) {
2170Sstevel@tonic-gate 			bzero(&aitv, sizeof (aitv));
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		aitv = curproc->p_rprof_timer;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		first = tv2hrt(&aitv.it_value);
2240Sstevel@tonic-gate 		interval = tv2hrt(&aitv.it_interval);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		if ((ts = gethrtime()) < first) {
2270Sstevel@tonic-gate 			/*
2280Sstevel@tonic-gate 			 * We haven't gone off for the first time; the time
2290Sstevel@tonic-gate 			 * remaining is simply the first time we will go
2300Sstevel@tonic-gate 			 * off minus the current time.
2310Sstevel@tonic-gate 			 */
2320Sstevel@tonic-gate 			remain = first - ts;
2330Sstevel@tonic-gate 		} else {
2340Sstevel@tonic-gate 			if (interval == 0) {
2350Sstevel@tonic-gate 				/*
2360Sstevel@tonic-gate 				 * This was set as a one-shot, and we've
2370Sstevel@tonic-gate 				 * already gone off; there is no time
2380Sstevel@tonic-gate 				 * remaining.
2390Sstevel@tonic-gate 				 */
2400Sstevel@tonic-gate 				remain = 0;
2410Sstevel@tonic-gate 			} else {
2420Sstevel@tonic-gate 				/*
2430Sstevel@tonic-gate 				 * We have a non-zero interval; we need to
2440Sstevel@tonic-gate 				 * determine how far we are into the current
2450Sstevel@tonic-gate 				 * interval, and subtract that from the
2460Sstevel@tonic-gate 				 * interval to determine the time remaining.
2470Sstevel@tonic-gate 				 */
2480Sstevel@tonic-gate 				remain = interval - ((ts - first) % interval);
2490Sstevel@tonic-gate 			}
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		hrt2tv(remain, &aitv.it_value);
2530Sstevel@tonic-gate 		break;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	default:
2560Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2570Sstevel@tonic-gate 		return (EINVAL);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if (iskaddr) {
2630Sstevel@tonic-gate 		bcopy(&aitv, itv, sizeof (*itv));
2640Sstevel@tonic-gate 	} else {
2650Sstevel@tonic-gate 		ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
2660Sstevel@tonic-gate 		if (copyout(&aitv, itv, sizeof (*itv)))
2670Sstevel@tonic-gate 			return (EFAULT);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate int
2750Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	int error;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (oitv != NULL)
2800Sstevel@tonic-gate 		if ((error = getitimer(which, oitv)) != 0)
2810Sstevel@tonic-gate 			return (error);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (itv == NULL)
2840Sstevel@tonic-gate 		return (0);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE)
2870Sstevel@tonic-gate 		error = xsetitimer(which, itv, 0);
2880Sstevel@tonic-gate 	else {
2890Sstevel@tonic-gate 		struct itimerval32 itv32;
2900Sstevel@tonic-gate 		struct itimerval kitv;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		if (copyin(itv, &itv32, sizeof (itv32)))
2930Sstevel@tonic-gate 			error = EFAULT;
2940Sstevel@tonic-gate 		ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32);
2950Sstevel@tonic-gate 		error = xsetitimer(which, &kitv, 1);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate int
3020Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	struct itimerval aitv;
3050Sstevel@tonic-gate 	struct timeval now;
3060Sstevel@tonic-gate 	struct proc *p = curproc;
3070Sstevel@tonic-gate 	kthread_t *t;
3080Sstevel@tonic-gate 	timeout_id_t tmp_id;
3090Sstevel@tonic-gate 	cyc_handler_t hdlr;
3100Sstevel@tonic-gate 	cyc_time_t when;
3110Sstevel@tonic-gate 	cyclic_id_t cyclic;
3120Sstevel@tonic-gate 	hrtime_t ts;
3130Sstevel@tonic-gate 	int min;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (itv == NULL)
3160Sstevel@tonic-gate 		return (0);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (iskaddr) {
3190Sstevel@tonic-gate 		bcopy(itv, &aitv, sizeof (aitv));
3200Sstevel@tonic-gate 	} else {
3210Sstevel@tonic-gate 		ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
3220Sstevel@tonic-gate 		if (copyin(itv, &aitv, sizeof (aitv)))
3230Sstevel@tonic-gate 			return (EFAULT);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (which == ITIMER_REALPROF) {
3270Sstevel@tonic-gate 		min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)),
3280Sstevel@tonic-gate 		    itimer_realprof_minimum);
3290Sstevel@tonic-gate 	} else {
3300Sstevel@tonic-gate 		min = usec_per_tick;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (itimerfix(&aitv.it_value, min) ||
3340Sstevel@tonic-gate 	    (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value)))
3350Sstevel@tonic-gate 		return (EINVAL);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3380Sstevel@tonic-gate 	switch (which) {
3390Sstevel@tonic-gate 	case ITIMER_REAL:
3400Sstevel@tonic-gate 		/*
3410Sstevel@tonic-gate 		 * The SITBUSY flag prevents conflicts with multiple
3420Sstevel@tonic-gate 		 * threads attempting to perform setitimer(ITIMER_REAL)
3430Sstevel@tonic-gate 		 * at the same time, even when we drop p->p_lock below.
3440Sstevel@tonic-gate 		 * Any blocked thread returns successfully because the
3450Sstevel@tonic-gate 		 * effect is the same as if it got here first, finished,
3460Sstevel@tonic-gate 		 * and the other thread then came through and destroyed
3470Sstevel@tonic-gate 		 * what it did.  We are just protecting the system from
3480Sstevel@tonic-gate 		 * malfunctioning due to the race condition.
3490Sstevel@tonic-gate 		 */
3500Sstevel@tonic-gate 		if (p->p_flag & SITBUSY) {
3510Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3520Sstevel@tonic-gate 			return (0);
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 		p->p_flag |= SITBUSY;
3550Sstevel@tonic-gate 		while ((tmp_id = p->p_itimerid) != 0) {
3560Sstevel@tonic-gate 			/*
3570Sstevel@tonic-gate 			 * Avoid deadlock in callout_delete (called from
3580Sstevel@tonic-gate 			 * untimeout) which may go to sleep (while holding
3590Sstevel@tonic-gate 			 * p_lock). Drop p_lock and re-acquire it after
3600Sstevel@tonic-gate 			 * untimeout returns. Need to clear p_itimerid
3610Sstevel@tonic-gate 			 * while holding p_lock.
3620Sstevel@tonic-gate 			 */
3630Sstevel@tonic-gate 			p->p_itimerid = 0;
3640Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3650Sstevel@tonic-gate 			(void) untimeout(tmp_id);
3660Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 		if (timerisset(&aitv.it_value)) {
3690Sstevel@tonic-gate 			uniqtime(&now);
3700Sstevel@tonic-gate 			timevaladd(&aitv.it_value, &now);
3710Sstevel@tonic-gate 			p->p_itimerid = realtime_timeout(realitexpire,
3720Sstevel@tonic-gate 			    p, hzto(&aitv.it_value));
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 		p->p_realitimer = aitv;
3750Sstevel@tonic-gate 		p->p_flag &= ~SITBUSY;
3760Sstevel@tonic-gate 		break;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	case ITIMER_REALPROF:
3790Sstevel@tonic-gate 		cyclic = p->p_rprof_cyclic;
3800Sstevel@tonic-gate 		p->p_rprof_cyclic = CYCLIC_NONE;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		/*
3850Sstevel@tonic-gate 		 * We're now going to acquire cpu_lock, remove the old cyclic
3860Sstevel@tonic-gate 		 * if necessary, and add our new cyclic.
3870Sstevel@tonic-gate 		 */
3880Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		if (cyclic != CYCLIC_NONE)
3910Sstevel@tonic-gate 			cyclic_remove(cyclic);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		if (!timerisset(&aitv.it_value)) {
3940Sstevel@tonic-gate 			/*
3950Sstevel@tonic-gate 			 * If we were passed a value of 0, we're done.
3960Sstevel@tonic-gate 			 */
3970Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
3980Sstevel@tonic-gate 			return (0);
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		hdlr.cyh_func = realprofexpire;
4020Sstevel@tonic-gate 		hdlr.cyh_arg = p;
4030Sstevel@tonic-gate 		hdlr.cyh_level = CY_LOW_LEVEL;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value));
4060Sstevel@tonic-gate 		when.cyt_interval = tv2hrt(&aitv.it_interval);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		if (when.cyt_interval == 0) {
4090Sstevel@tonic-gate 			/*
4100Sstevel@tonic-gate 			 * Using the same logic as for CLOCK_HIGHRES timers, we
4110Sstevel@tonic-gate 			 * set the interval to be INT64_MAX - when.cyt_when to
4120Sstevel@tonic-gate 			 * effect a one-shot; see the comment in clock_highres.c
4130Sstevel@tonic-gate 			 * for more details on why this works.
4140Sstevel@tonic-gate 			 */
4150Sstevel@tonic-gate 			when.cyt_interval = INT64_MAX - when.cyt_when;
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		cyclic = cyclic_add(&hdlr, &when);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 		/*
4230Sstevel@tonic-gate 		 * We have now successfully added the cyclic.  Reacquire
4240Sstevel@tonic-gate 		 * p_lock, and see if anyone has snuck in.
4250Sstevel@tonic-gate 		 */
4260Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 		if (p->p_rprof_cyclic != CYCLIC_NONE) {
4290Sstevel@tonic-gate 			/*
4300Sstevel@tonic-gate 			 * We're racing with another thread establishing an
4310Sstevel@tonic-gate 			 * ITIMER_REALPROF interval timer.  We'll let the other
4320Sstevel@tonic-gate 			 * thread win (this is a race at the application level,
4330Sstevel@tonic-gate 			 * so letting the other thread win is acceptable).
4340Sstevel@tonic-gate 			 */
4350Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4360Sstevel@tonic-gate 			mutex_enter(&cpu_lock);
4370Sstevel@tonic-gate 			cyclic_remove(cyclic);
4380Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 			return (0);
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		/*
4440Sstevel@tonic-gate 		 * Success.  Set our tracking variables in the proc structure,
4450Sstevel@tonic-gate 		 * cancel any outstanding ITIMER_PROF, and allocate the
4460Sstevel@tonic-gate 		 * per-thread SIGPROF buffers, if possible.
4470Sstevel@tonic-gate 		 */
4480Sstevel@tonic-gate 		hrt2tv(ts, &aitv.it_value);
4490Sstevel@tonic-gate 		p->p_rprof_timer = aitv;
4500Sstevel@tonic-gate 		p->p_rprof_cyclic = cyclic;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		t = p->p_tlist;
4530Sstevel@tonic-gate 		do {
4540Sstevel@tonic-gate 			struct itimerval *itvp;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 			itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF];
4570Sstevel@tonic-gate 			timerclear(&itvp->it_interval);
4580Sstevel@tonic-gate 			timerclear(&itvp->it_value);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 			if (t->t_rprof != NULL)
4610Sstevel@tonic-gate 				continue;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 			t->t_rprof =
4640Sstevel@tonic-gate 			    kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP);
4650Sstevel@tonic-gate 			aston(t);
4660Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		break;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	case ITIMER_VIRTUAL:
4710Sstevel@tonic-gate 		ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv;
4720Sstevel@tonic-gate 		break;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	case ITIMER_PROF:
4750Sstevel@tonic-gate 		if (p->p_rprof_cyclic != CYCLIC_NONE) {
4760Sstevel@tonic-gate 			/*
4770Sstevel@tonic-gate 			 * Silently ignore ITIMER_PROF if ITIMER_REALPROF
4780Sstevel@tonic-gate 			 * is in effect.
4790Sstevel@tonic-gate 			 */
4800Sstevel@tonic-gate 			break;
4810Sstevel@tonic-gate 		}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 		ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv;
4840Sstevel@tonic-gate 		break;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	default:
4870Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4880Sstevel@tonic-gate 		return (EINVAL);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4910Sstevel@tonic-gate 	return (0);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * Real interval timer expired:
4960Sstevel@tonic-gate  * send process whose timer expired an alarm signal.
4970Sstevel@tonic-gate  * If time is not set up to reload, then just return.
4980Sstevel@tonic-gate  * Else compute next time timer should go off which is > current time.
4990Sstevel@tonic-gate  * This is where delay in processing this timeout causes multiple
5000Sstevel@tonic-gate  * SIGALRM calls to be compressed into one.
5010Sstevel@tonic-gate  */
5020Sstevel@tonic-gate static void
5030Sstevel@tonic-gate realitexpire(void *arg)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	struct proc *p = arg;
5060Sstevel@tonic-gate 	struct timeval *valp = &p->p_realitimer.it_value;
5070Sstevel@tonic-gate 	struct timeval *intervalp = &p->p_realitimer.it_interval;
5080Sstevel@tonic-gate #if !defined(_LP64)
5090Sstevel@tonic-gate 	clock_t	ticks;
5100Sstevel@tonic-gate #endif
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5130Sstevel@tonic-gate #if !defined(_LP64)
5140Sstevel@tonic-gate 	if ((ticks = hzto(valp)) > 1) {
5150Sstevel@tonic-gate 		/*
5160Sstevel@tonic-gate 		 * If we are executing before we were meant to, it must be
5170Sstevel@tonic-gate 		 * because of an overflow in a prior hzto() calculation.
5180Sstevel@tonic-gate 		 * In this case, we want to go to sleep for the recalculated
5190Sstevel@tonic-gate 		 * number of ticks. For the special meaning of the value "1"
5200Sstevel@tonic-gate 		 * see comment in timespectohz().
5210Sstevel@tonic-gate 		 */
5220Sstevel@tonic-gate 		p->p_itimerid = realtime_timeout(realitexpire, p, ticks);
5230Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5240Sstevel@tonic-gate 		return;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate #endif
5270Sstevel@tonic-gate 	sigtoproc(p, NULL, SIGALRM);
5280Sstevel@tonic-gate 	if (!timerisset(intervalp)) {
5290Sstevel@tonic-gate 		timerclear(valp);
5300Sstevel@tonic-gate 		p->p_itimerid = 0;
5310Sstevel@tonic-gate 	} else {
5320Sstevel@tonic-gate 		/* advance timer value past current time */
5330Sstevel@tonic-gate 		timeval_advance(valp, intervalp);
5340Sstevel@tonic-gate 		p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp));
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * Real time profiling interval timer expired:
5410Sstevel@tonic-gate  * Increment microstate counters for each lwp in the process
5420Sstevel@tonic-gate  * and ensure that running lwps are kicked into the kernel.
5430Sstevel@tonic-gate  * If time is not set up to reload, then just return.
5440Sstevel@tonic-gate  * Else compute next time timer should go off which is > current time,
5450Sstevel@tonic-gate  * as above.
5460Sstevel@tonic-gate  */
5470Sstevel@tonic-gate static void
5480Sstevel@tonic-gate realprofexpire(void *arg)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	struct proc *p = arg;
5510Sstevel@tonic-gate 	kthread_t *t;
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5540Sstevel@tonic-gate 	if ((t = p->p_tlist) == NULL) {
5550Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5560Sstevel@tonic-gate 		return;
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 	do {
5590Sstevel@tonic-gate 		int mstate;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		/*
5620Sstevel@tonic-gate 		 * Attempt to allocate the SIGPROF buffer, but don't sleep.
5630Sstevel@tonic-gate 		 */
5640Sstevel@tonic-gate 		if (t->t_rprof == NULL)
5650Sstevel@tonic-gate 			t->t_rprof = kmem_zalloc(sizeof (struct rprof),
5660Sstevel@tonic-gate 			    KM_NOSLEEP);
5670Sstevel@tonic-gate 		if (t->t_rprof == NULL)
5680Sstevel@tonic-gate 			continue;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		thread_lock(t);
5710Sstevel@tonic-gate 		switch (t->t_state) {
5720Sstevel@tonic-gate 		case TS_SLEEP:
5730Sstevel@tonic-gate 			/*
5740Sstevel@tonic-gate 			 * Don't touch the lwp is it is swapped out.
5750Sstevel@tonic-gate 			 */
5760Sstevel@tonic-gate 			if (!(t->t_schedflag & TS_LOAD)) {
5770Sstevel@tonic-gate 				mstate = LMS_SLEEP;
5780Sstevel@tonic-gate 				break;
5790Sstevel@tonic-gate 			}
5800Sstevel@tonic-gate 			switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) {
5810Sstevel@tonic-gate 			case LMS_TFAULT:
5820Sstevel@tonic-gate 			case LMS_DFAULT:
5830Sstevel@tonic-gate 			case LMS_KFAULT:
5840Sstevel@tonic-gate 			case LMS_USER_LOCK:
5850Sstevel@tonic-gate 				break;
5860Sstevel@tonic-gate 			default:
5870Sstevel@tonic-gate 				mstate = LMS_SLEEP;
5880Sstevel@tonic-gate 				break;
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 			break;
5910Sstevel@tonic-gate 		case TS_RUN:
592*3792Sakolb 		case TS_WAIT:
5930Sstevel@tonic-gate 			mstate = LMS_WAIT_CPU;
5940Sstevel@tonic-gate 			break;
5950Sstevel@tonic-gate 		case TS_ONPROC:
5960Sstevel@tonic-gate 			switch (mstate = t->t_mstate) {
5970Sstevel@tonic-gate 			case LMS_USER:
5980Sstevel@tonic-gate 			case LMS_SYSTEM:
5990Sstevel@tonic-gate 			case LMS_TRAP:
6000Sstevel@tonic-gate 				break;
6010Sstevel@tonic-gate 			default:
6020Sstevel@tonic-gate 				mstate = LMS_SYSTEM;
6030Sstevel@tonic-gate 				break;
6040Sstevel@tonic-gate 			}
6050Sstevel@tonic-gate 			break;
6060Sstevel@tonic-gate 		default:
6070Sstevel@tonic-gate 			mstate = t->t_mstate;
6080Sstevel@tonic-gate 			break;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 		t->t_rprof->rp_anystate = 1;
6110Sstevel@tonic-gate 		t->t_rprof->rp_state[mstate]++;
6120Sstevel@tonic-gate 		aston(t);
6130Sstevel@tonic-gate 		/*
6140Sstevel@tonic-gate 		 * force the thread into the kernel
6150Sstevel@tonic-gate 		 * if it is not already there.
6160Sstevel@tonic-gate 		 */
6170Sstevel@tonic-gate 		if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
6180Sstevel@tonic-gate 			poke_cpu(t->t_cpu->cpu_id);
6190Sstevel@tonic-gate 		thread_unlock(t);
6200Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Advances timer value past the current time of day.  See the detailed
6270Sstevel@tonic-gate  * comment for this logic in realitsexpire(), above.
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate static void
6300Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	int cnt2nth;
6330Sstevel@tonic-gate 	struct timeval interval2nth;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	for (;;) {
6360Sstevel@tonic-gate 		interval2nth = *intervalp;
6370Sstevel@tonic-gate 		for (cnt2nth = 0; ; cnt2nth++) {
6380Sstevel@tonic-gate 			timevaladd(valp, &interval2nth);
6390Sstevel@tonic-gate 			/*CSTYLED*/
6400Sstevel@tonic-gate 			if (TVTSCMP(valp, &hrestime, >))
6410Sstevel@tonic-gate 				break;
6420Sstevel@tonic-gate 			timevaladd(&interval2nth, &interval2nth);
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 		if (cnt2nth == 0)
6450Sstevel@tonic-gate 			break;
6460Sstevel@tonic-gate 		timevalsub(valp, &interval2nth);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * Check that a proposed value to load into the .it_value or .it_interval
6520Sstevel@tonic-gate  * part of an interval timer is acceptable, and set it to at least a
6530Sstevel@tonic-gate  * specified minimal value.
6540Sstevel@tonic-gate  */
6550Sstevel@tonic-gate int
6560Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
6590Sstevel@tonic-gate 	    tv->tv_usec < 0 || tv->tv_usec >= MICROSEC)
6600Sstevel@tonic-gate 		return (EINVAL);
6610Sstevel@tonic-gate 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum)
6620Sstevel@tonic-gate 		tv->tv_usec = minimum;
6630Sstevel@tonic-gate 	return (0);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * Same as itimerfix, except a) it takes a timespec instead of a timeval and
6680Sstevel@tonic-gate  * b) it doesn't truncate based on timeout granularity; consumers of this
6690Sstevel@tonic-gate  * interface (e.g. timer_settime()) depend on the passed timespec not being
6700Sstevel@tonic-gate  * modified implicitly.
6710Sstevel@tonic-gate  */
6720Sstevel@tonic-gate int
6730Sstevel@tonic-gate itimerspecfix(timespec_t *tv)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC)
6760Sstevel@tonic-gate 		return (EINVAL);
6770Sstevel@tonic-gate 	return (0);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate  * Decrement an interval timer by a specified number
6820Sstevel@tonic-gate  * of microseconds, which must be less than a second,
6830Sstevel@tonic-gate  * i.e. < 1000000.  If the timer expires, then reload
6840Sstevel@tonic-gate  * it.  In this case, carry over (usec - old value) to
6850Sstevel@tonic-gate  * reducint the value reloaded into the timer so that
6860Sstevel@tonic-gate  * the timer does not drift.  This routine assumes
6870Sstevel@tonic-gate  * that it is called in a context where the timers
6880Sstevel@tonic-gate  * on which it is operating cannot change in value.
6890Sstevel@tonic-gate  */
6900Sstevel@tonic-gate int
6910Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	if (itp->it_value.tv_usec < usec) {
6940Sstevel@tonic-gate 		if (itp->it_value.tv_sec == 0) {
6950Sstevel@tonic-gate 			/* expired, and already in next interval */
6960Sstevel@tonic-gate 			usec -= itp->it_value.tv_usec;
6970Sstevel@tonic-gate 			goto expire;
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		itp->it_value.tv_usec += MICROSEC;
7000Sstevel@tonic-gate 		itp->it_value.tv_sec--;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	itp->it_value.tv_usec -= usec;
7030Sstevel@tonic-gate 	usec = 0;
7040Sstevel@tonic-gate 	if (timerisset(&itp->it_value))
7050Sstevel@tonic-gate 		return (1);
7060Sstevel@tonic-gate 	/* expired, exactly at end of interval */
7070Sstevel@tonic-gate expire:
7080Sstevel@tonic-gate 	if (timerisset(&itp->it_interval)) {
7090Sstevel@tonic-gate 		itp->it_value = itp->it_interval;
7100Sstevel@tonic-gate 		itp->it_value.tv_usec -= usec;
7110Sstevel@tonic-gate 		if (itp->it_value.tv_usec < 0) {
7120Sstevel@tonic-gate 			itp->it_value.tv_usec += MICROSEC;
7130Sstevel@tonic-gate 			itp->it_value.tv_sec--;
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 	} else
7160Sstevel@tonic-gate 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
7170Sstevel@tonic-gate 	return (0);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate  * Add and subtract routines for timevals.
7220Sstevel@tonic-gate  * N.B.: subtract routine doesn't deal with
7230Sstevel@tonic-gate  * results which are before the beginning,
7240Sstevel@tonic-gate  * it just gets very confused in this case.
7250Sstevel@tonic-gate  * Caveat emptor.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate void
7280Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
7310Sstevel@tonic-gate 	t1->tv_usec += t2->tv_usec;
7320Sstevel@tonic-gate 	timevalfix(t1);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate void
7360Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
7390Sstevel@tonic-gate 	t1->tv_usec -= t2->tv_usec;
7400Sstevel@tonic-gate 	timevalfix(t1);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate void
7440Sstevel@tonic-gate timevalfix(struct timeval *t1)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	if (t1->tv_usec < 0) {
7470Sstevel@tonic-gate 		t1->tv_sec--;
7480Sstevel@tonic-gate 		t1->tv_usec += MICROSEC;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	if (t1->tv_usec >= MICROSEC) {
7510Sstevel@tonic-gate 		t1->tv_sec++;
7520Sstevel@tonic-gate 		t1->tv_usec -= MICROSEC;
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate  * Same as the routines above. These routines take a timespec instead
7580Sstevel@tonic-gate  * of a timeval.
7590Sstevel@tonic-gate  */
7600Sstevel@tonic-gate void
7610Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
7640Sstevel@tonic-gate 	t1->tv_nsec += t2->tv_nsec;
7650Sstevel@tonic-gate 	timespecfix(t1);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate void
7690Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
7720Sstevel@tonic-gate 	t1->tv_nsec -= t2->tv_nsec;
7730Sstevel@tonic-gate 	timespecfix(t1);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate void
7770Sstevel@tonic-gate timespecfix(timespec_t *t1)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	if (t1->tv_nsec < 0) {
7800Sstevel@tonic-gate 		t1->tv_sec--;
7810Sstevel@tonic-gate 		t1->tv_nsec += NANOSEC;
7820Sstevel@tonic-gate 	} else {
7830Sstevel@tonic-gate 		if (t1->tv_nsec >= NANOSEC) {
7840Sstevel@tonic-gate 			t1->tv_sec++;
7850Sstevel@tonic-gate 			t1->tv_nsec -= NANOSEC;
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate  * Compute number of hz until specified time.
7920Sstevel@tonic-gate  * Used to compute third argument to timeout() from an absolute time.
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate clock_t
7950Sstevel@tonic-gate hzto(struct timeval *tv)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	timespec_t ts, now;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	ts.tv_sec = tv->tv_sec;
8000Sstevel@tonic-gate 	ts.tv_nsec = tv->tv_usec * 1000;
8010Sstevel@tonic-gate 	gethrestime_lasttick(&now);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	return (timespectohz(&ts, now));
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*
8070Sstevel@tonic-gate  * Compute number of hz until specified time for a given timespec value.
8080Sstevel@tonic-gate  * Used to compute third argument to timeout() from an absolute time.
8090Sstevel@tonic-gate  */
8100Sstevel@tonic-gate clock_t
8110Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	clock_t	ticks;
8140Sstevel@tonic-gate 	time_t	sec;
8150Sstevel@tonic-gate 	int	nsec;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * Compute number of ticks we will see between now and
8190Sstevel@tonic-gate 	 * the target time; returns "1" if the destination time
8200Sstevel@tonic-gate 	 * is before the next tick, so we always get some delay,
8210Sstevel@tonic-gate 	 * and returns LONG_MAX ticks if we would overflow.
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	sec = tv->tv_sec - now.tv_sec;
8240Sstevel@tonic-gate 	nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (nsec < 0) {
8270Sstevel@tonic-gate 		sec--;
8280Sstevel@tonic-gate 		nsec += NANOSEC;
8290Sstevel@tonic-gate 	} else if (nsec >= NANOSEC) {
8300Sstevel@tonic-gate 		sec++;
8310Sstevel@tonic-gate 		nsec -= NANOSEC;
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	ticks = NSEC_TO_TICK(nsec);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Compute ticks, accounting for negative and overflow as above.
8380Sstevel@tonic-gate 	 * Overflow protection kicks in at about 70 weeks for hz=50
8390Sstevel@tonic-gate 	 * and at about 35 weeks for hz=100. (Rather longer for the 64-bit
8400Sstevel@tonic-gate 	 * kernel :-)
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 	if (sec < 0 || (sec == 0 && ticks < 1))
8430Sstevel@tonic-gate 		ticks = 1;			/* protect vs nonpositive */
8440Sstevel@tonic-gate 	else if (sec > (LONG_MAX - ticks) / hz)
8450Sstevel@tonic-gate 		ticks = LONG_MAX;		/* protect vs overflow */
8460Sstevel@tonic-gate 	else
8470Sstevel@tonic-gate 		ticks += sec * hz;		/* common case */
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	return (ticks);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate  * hrt2ts(): convert from hrtime_t to timestruc_t.
8540Sstevel@tonic-gate  *
8550Sstevel@tonic-gate  * All this routine really does is:
8560Sstevel@tonic-gate  *
8570Sstevel@tonic-gate  *	tsp->sec  = hrt / NANOSEC;
8580Sstevel@tonic-gate  *	tsp->nsec = hrt % NANOSEC;
8590Sstevel@tonic-gate  *
8600Sstevel@tonic-gate  * The black magic below avoids doing a 64-bit by 32-bit integer divide,
8610Sstevel@tonic-gate  * which is quite expensive.  There's actually much more going on here than
8620Sstevel@tonic-gate  * it might first appear -- don't try this at home.
8630Sstevel@tonic-gate  *
8640Sstevel@tonic-gate  * For the adventuresome, here's an explanation of how it works.
8650Sstevel@tonic-gate  *
8660Sstevel@tonic-gate  * Multiplication by a fixed constant is easy -- you just do the appropriate
8670Sstevel@tonic-gate  * shifts and adds.  For example, to multiply by 10, we observe that
8680Sstevel@tonic-gate  *
8690Sstevel@tonic-gate  *	x * 10	= x * (8 + 2)
8700Sstevel@tonic-gate  *		= (x * 8) + (x * 2)
8710Sstevel@tonic-gate  *		= (x << 3) + (x << 1).
8720Sstevel@tonic-gate  *
8730Sstevel@tonic-gate  * In general, you can read the algorithm right off the bits: the number 10
8740Sstevel@tonic-gate  * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3).
8750Sstevel@tonic-gate  *
8760Sstevel@tonic-gate  * Sometimes you can do better.  For example, 15 is 1111 binary, so the normal
8770Sstevel@tonic-gate  * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3).
8780Sstevel@tonic-gate  * But, it's cheaper if you capitalize on the fact that you have a run of ones:
8790Sstevel@tonic-gate  * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0).  [You would never
8800Sstevel@tonic-gate  * actually perform the operation << 0, since it's a no-op; I'm just writing
8810Sstevel@tonic-gate  * it that way for clarity.]
8820Sstevel@tonic-gate  *
8830Sstevel@tonic-gate  * The other way you can win is if you get lucky with the prime factorization
8840Sstevel@tonic-gate  * of your constant.  The number 1,000,000,000, which we have to multiply
8850Sstevel@tonic-gate  * by below, is a good example.  One billion is 111011100110101100101000000000
8860Sstevel@tonic-gate  * in binary.  If you apply the bit-grouping trick, it doesn't buy you very
8870Sstevel@tonic-gate  * much, because it's only a win for groups of three or more equal bits:
8880Sstevel@tonic-gate  *
8890Sstevel@tonic-gate  * 111011100110101100101000000000 = 1000000000000000000000000000000
8900Sstevel@tonic-gate  *				  -  000100011001010011011000000000
8910Sstevel@tonic-gate  *
8920Sstevel@tonic-gate  * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS,
8930Sstevel@tonic-gate  * we have reduced this to 10 shift/add pairs (20 operations) on the RHS.
8940Sstevel@tonic-gate  * This is better, but not great.
8950Sstevel@tonic-gate  *
8960Sstevel@tonic-gate  * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125,
8970Sstevel@tonic-gate  * and multiply by each factor.  Multiplication by 125 is particularly easy,
8980Sstevel@tonic-gate  * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four
8990Sstevel@tonic-gate  * operations.  So, to multiply by 1,000,000,000, we perform three multipli-
9000Sstevel@tonic-gate  * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations.
9010Sstevel@tonic-gate  * This is the algorithm we actually use in both hrt2ts() and ts2hrt().
9020Sstevel@tonic-gate  *
9030Sstevel@tonic-gate  * Division is harder; there is no equivalent of the simple shift-add algorithm
9040Sstevel@tonic-gate  * we used for multiplication.  However, we can convert the division problem
9050Sstevel@tonic-gate  * into a multiplication problem by pre-computing the binary representation
9060Sstevel@tonic-gate  * of the reciprocal of the divisor.  For the case of interest, we have
9070Sstevel@tonic-gate  *
9080Sstevel@tonic-gate  *	1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30,
9090Sstevel@tonic-gate  *
9100Sstevel@tonic-gate  * to 32 bits of precision.  (The notation B-30 means "* 2^-30", just like
9110Sstevel@tonic-gate  * E-18 means "* 10^-18".)
9120Sstevel@tonic-gate  *
9130Sstevel@tonic-gate  * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit
9140Sstevel@tonic-gate  * integer 10001001011100000101111101000001, then normalize (shift) the
9150Sstevel@tonic-gate  * result.  This constant has several large bits runs, so the multiply
9160Sstevel@tonic-gate  * is relatively cheap:
9170Sstevel@tonic-gate  *
9180Sstevel@tonic-gate  *	10001001011100000101111101000001 = 10001001100000000110000001000001
9190Sstevel@tonic-gate  *					 - 00000000000100000000000100000000
9200Sstevel@tonic-gate  *
9210Sstevel@tonic-gate  * Again, you can just read the algorithm right off the bits:
9220Sstevel@tonic-gate  *
9230Sstevel@tonic-gate  *			sec = hrt;
9240Sstevel@tonic-gate  *			sec += (hrt << 6);
9250Sstevel@tonic-gate  *			sec -= (hrt << 8);
9260Sstevel@tonic-gate  *			sec += (hrt << 13);
9270Sstevel@tonic-gate  *			sec += (hrt << 14);
9280Sstevel@tonic-gate  *			sec -= (hrt << 20);
9290Sstevel@tonic-gate  *			sec += (hrt << 23);
9300Sstevel@tonic-gate  *			sec += (hrt << 24);
9310Sstevel@tonic-gate  *			sec += (hrt << 27);
9320Sstevel@tonic-gate  *			sec += (hrt << 31);
9330Sstevel@tonic-gate  *			sec >>= (32 + 30);
9340Sstevel@tonic-gate  *
9350Sstevel@tonic-gate  * Voila!  The only problem is, since hrt is 64 bits, we need to use 96-bit
9360Sstevel@tonic-gate  * arithmetic to perform this calculation.  That's a waste, because ultimately
9370Sstevel@tonic-gate  * we only need the highest 32 bits of the result.
9380Sstevel@tonic-gate  *
9390Sstevel@tonic-gate  * The first thing we do is to realize that we don't need to use all of hrt
9400Sstevel@tonic-gate  * in the calculation.  The lowest 30 bits can contribute at most 1 to the
9410Sstevel@tonic-gate  * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later.
9420Sstevel@tonic-gate  * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t.
9430Sstevel@tonic-gate  * Thus, the only bits of hrt that matter for division are bits 30..61.
9440Sstevel@tonic-gate  * These 32 bits are just the lower-order word of (hrt >> 30).  This brings
9450Sstevel@tonic-gate  * us down from 96-bit math to 64-bit math, and our algorithm becomes:
9460Sstevel@tonic-gate  *
9470Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
9480Sstevel@tonic-gate  *			sec = tmp;
9490Sstevel@tonic-gate  *			sec += (tmp << 6);
9500Sstevel@tonic-gate  *			sec -= (tmp << 8);
9510Sstevel@tonic-gate  *			sec += (tmp << 13);
9520Sstevel@tonic-gate  *			sec += (tmp << 14);
9530Sstevel@tonic-gate  *			sec -= (tmp << 20);
9540Sstevel@tonic-gate  *			sec += (tmp << 23);
9550Sstevel@tonic-gate  *			sec += (tmp << 24);
9560Sstevel@tonic-gate  *			sec += (tmp << 27);
9570Sstevel@tonic-gate  *			sec += (tmp << 31);
9580Sstevel@tonic-gate  *			sec >>= 32;
9590Sstevel@tonic-gate  *
9600Sstevel@tonic-gate  * Next, we're going to reduce this 64-bit computation to a 32-bit
9610Sstevel@tonic-gate  * computation.  We begin by rewriting the above algorithm to use relative
9620Sstevel@tonic-gate  * shifts instead of absolute shifts.  That is, instead of computing
9630Sstevel@tonic-gate  * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally:
9640Sstevel@tonic-gate  * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc:
9650Sstevel@tonic-gate  *
9660Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
9670Sstevel@tonic-gate  *			sec = tmp;
9680Sstevel@tonic-gate  *			tmp <<= 6; sec += tmp;
9690Sstevel@tonic-gate  *			tmp <<= 2; sec -= tmp;
9700Sstevel@tonic-gate  *			tmp <<= 5; sec += tmp;
9710Sstevel@tonic-gate  *			tmp <<= 1; sec += tmp;
9720Sstevel@tonic-gate  *			tmp <<= 6; sec -= tmp;
9730Sstevel@tonic-gate  *			tmp <<= 3; sec += tmp;
9740Sstevel@tonic-gate  *			tmp <<= 1; sec += tmp;
9750Sstevel@tonic-gate  *			tmp <<= 3; sec += tmp;
9760Sstevel@tonic-gate  *			tmp <<= 4; sec += tmp;
9770Sstevel@tonic-gate  *			sec >>= 32;
9780Sstevel@tonic-gate  *
9790Sstevel@tonic-gate  * Now for the final step.  Instead of throwing away the low 32 bits at
9800Sstevel@tonic-gate  * the end, we can throw them away as we go, only keeping the high 32 bits
9810Sstevel@tonic-gate  * of the product at each step.  So, for example, where we now have
9820Sstevel@tonic-gate  *
9830Sstevel@tonic-gate  *			tmp <<= 6; sec = sec + tmp;
9840Sstevel@tonic-gate  * we will instead have
9850Sstevel@tonic-gate  *			tmp <<= 6; sec = (sec + tmp) >> 6;
9860Sstevel@tonic-gate  * which is equivalent to
9870Sstevel@tonic-gate  *			sec = (sec >> 6) + tmp;
9880Sstevel@tonic-gate  *
9890Sstevel@tonic-gate  * The final shift ("sec >>= 32") goes away.
9900Sstevel@tonic-gate  *
9910Sstevel@tonic-gate  * All we're really doing here is long multiplication, just like we learned in
9920Sstevel@tonic-gate  * grade school, except that at each step, we only look at the leftmost 32
9930Sstevel@tonic-gate  * columns.  The cumulative error is, at most, the sum of all the bits we
9940Sstevel@tonic-gate  * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32.
9950Sstevel@tonic-gate  * Thus, the final result ("sec") is correct to +/- 1.
9960Sstevel@tonic-gate  *
9970Sstevel@tonic-gate  * It turns out to be important to keep "sec" positive at each step, because
9980Sstevel@tonic-gate  * we don't want to have to explicitly extend the sign bit.  Therefore,
9990Sstevel@tonic-gate  * starting with the last line of code above, each line that would have read
10000Sstevel@tonic-gate  * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and
10010Sstevel@tonic-gate  * the operators (+ or -) in all previous lines must be toggled accordingly.
10020Sstevel@tonic-gate  * Thus, we end up with:
10030Sstevel@tonic-gate  *
10040Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
10050Sstevel@tonic-gate  *			sec = tmp + (sec >> 6);
10060Sstevel@tonic-gate  *			sec = tmp - (tmp >> 2);
10070Sstevel@tonic-gate  *			sec = tmp - (sec >> 5);
10080Sstevel@tonic-gate  *			sec = tmp + (sec >> 1);
10090Sstevel@tonic-gate  *			sec = tmp - (sec >> 6);
10100Sstevel@tonic-gate  *			sec = tmp - (sec >> 3);
10110Sstevel@tonic-gate  *			sec = tmp + (sec >> 1);
10120Sstevel@tonic-gate  *			sec = tmp + (sec >> 3);
10130Sstevel@tonic-gate  *			sec = tmp + (sec >> 4);
10140Sstevel@tonic-gate  *
10150Sstevel@tonic-gate  * This yields a value for sec that is accurate to +1/-1, so we have two
10160Sstevel@tonic-gate  * cases to deal with.  The mysterious-looking "+ 7" in the code below biases
10170Sstevel@tonic-gate  * the rounding toward zero, so that sec is always less than or equal to
10180Sstevel@tonic-gate  * the correct value.  With this modified code, sec is accurate to +0/-2, with
10190Sstevel@tonic-gate  * the -2 case being very rare in practice.  With this change, we only have to
10200Sstevel@tonic-gate  * deal with one case (sec too small) in the cleanup code.
10210Sstevel@tonic-gate  *
10220Sstevel@tonic-gate  * The other modification we make is to delete the second line above
10230Sstevel@tonic-gate  * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is
10240Sstevel@tonic-gate  * set, and the cleanup code can handle that rare case.  This reduces the
10250Sstevel@tonic-gate  * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases.
10260Sstevel@tonic-gate  *
10270Sstevel@tonic-gate  * Finally, we compute nsec = hrt - (sec * 1,000,000,000).  nsec will always
10280Sstevel@tonic-gate  * be positive (since sec is never too large), and will at most be equal to
10290Sstevel@tonic-gate  * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt.
10300Sstevel@tonic-gate  * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can
10310Sstevel@tonic-gate  * safely assume that nsec fits in 32 bits.  Consequently, when we compute
10320Sstevel@tonic-gate  * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit
10330Sstevel@tonic-gate  * arithmetic and let the high-order bits fall off the end.
10340Sstevel@tonic-gate  *
10350Sstevel@tonic-gate  * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop:
10360Sstevel@tonic-gate  *
10370Sstevel@tonic-gate  *			while (nsec >= NANOSEC) {
10380Sstevel@tonic-gate  *				nsec -= NANOSEC;
10390Sstevel@tonic-gate  *				sec++;
10400Sstevel@tonic-gate  *			}
10410Sstevel@tonic-gate  *
10420Sstevel@tonic-gate  * is guaranteed to complete in at most 4 iterations.  In practice, the loop
10430Sstevel@tonic-gate  * completes in 0 or 1 iteration over 95% of the time.
10440Sstevel@tonic-gate  *
10450Sstevel@tonic-gate  * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about
10460Sstevel@tonic-gate  * 35 usec for software division -- about 20 times faster.
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate void
10490Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate 	uint32_t sec, nsec, tmp;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	tmp = (uint32_t)(hrt >> 30);
10540Sstevel@tonic-gate 	sec = tmp - (tmp >> 2);
10550Sstevel@tonic-gate 	sec = tmp - (sec >> 5);
10560Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
10570Sstevel@tonic-gate 	sec = tmp - (sec >> 6) + 7;
10580Sstevel@tonic-gate 	sec = tmp - (sec >> 3);
10590Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
10600Sstevel@tonic-gate 	sec = tmp + (sec >> 3);
10610Sstevel@tonic-gate 	sec = tmp + (sec >> 4);
10620Sstevel@tonic-gate 	tmp = (sec << 7) - sec - sec - sec;
10630Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
10640Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
10650Sstevel@tonic-gate 	nsec = (uint32_t)hrt - (tmp << 9);
10660Sstevel@tonic-gate 	while (nsec >= NANOSEC) {
10670Sstevel@tonic-gate 		nsec -= NANOSEC;
10680Sstevel@tonic-gate 		sec++;
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 	tsp->tv_sec = (time_t)sec;
10710Sstevel@tonic-gate 	tsp->tv_nsec = nsec;
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate /*
10750Sstevel@tonic-gate  * Convert from timestruc_t to hrtime_t.
10760Sstevel@tonic-gate  *
10770Sstevel@tonic-gate  * The code below is equivalent to:
10780Sstevel@tonic-gate  *
10790Sstevel@tonic-gate  *	hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec;
10800Sstevel@tonic-gate  *
10810Sstevel@tonic-gate  * but requires no integer multiply.
10820Sstevel@tonic-gate  */
10830Sstevel@tonic-gate hrtime_t
10840Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp)
10850Sstevel@tonic-gate {
10860Sstevel@tonic-gate 	hrtime_t hrt;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	hrt = tsp->tv_sec;
10890Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
10900Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
10910Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
10920Sstevel@tonic-gate 	hrt = (hrt << 9) + tsp->tv_nsec;
10930Sstevel@tonic-gate 	return (hrt);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate  * For the various 32-bit "compatibility" paths in the system.
10980Sstevel@tonic-gate  */
10990Sstevel@tonic-gate void
11000Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	timestruc_t ts;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	hrt2ts(hrt, &ts);
11050Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(ts32p, &ts);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate  * If this ever becomes performance critical (ha!), we can borrow the
11100Sstevel@tonic-gate  * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the
11110Sstevel@tonic-gate  * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by
11120Sstevel@tonic-gate  * 1,000.  For now, we'll opt for readability (besides, the compiler does
11130Sstevel@tonic-gate  * a passable job of optimizing constant multiplication into shifts and adds).
11140Sstevel@tonic-gate  */
11150Sstevel@tonic-gate hrtime_t
11160Sstevel@tonic-gate tv2hrt(struct timeval *tvp)
11170Sstevel@tonic-gate {
11180Sstevel@tonic-gate 	return ((hrtime_t)tvp->tv_sec * NANOSEC +
11190Sstevel@tonic-gate 	    (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC));
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate void
11231432Sandyb hrt2tv(hrtime_t hrt, struct timeval *tvp)
11240Sstevel@tonic-gate {
11251432Sandyb 	uint32_t sec, nsec, tmp;
11261432Sandyb 	uint32_t q, r, t;
11271432Sandyb 
11281432Sandyb 	tmp = (uint32_t)(hrt >> 30);
11291432Sandyb 	sec = tmp - (tmp >> 2);
11301432Sandyb 	sec = tmp - (sec >> 5);
11311432Sandyb 	sec = tmp + (sec >> 1);
11321432Sandyb 	sec = tmp - (sec >> 6) + 7;
11331432Sandyb 	sec = tmp - (sec >> 3);
11341432Sandyb 	sec = tmp + (sec >> 1);
11351432Sandyb 	sec = tmp + (sec >> 3);
11361432Sandyb 	sec = tmp + (sec >> 4);
11371432Sandyb 	tmp = (sec << 7) - sec - sec - sec;
11381432Sandyb 	tmp = (tmp << 7) - tmp - tmp - tmp;
11391432Sandyb 	tmp = (tmp << 7) - tmp - tmp - tmp;
11401432Sandyb 	nsec = (uint32_t)hrt - (tmp << 9);
11411432Sandyb 	while (nsec >= NANOSEC) {
11421432Sandyb 		nsec -= NANOSEC;
11431432Sandyb 		sec++;
11441432Sandyb 	}
11451432Sandyb 	tvp->tv_sec = (time_t)sec;
11461432Sandyb /*
11471432Sandyb  * this routine is very similar to hr2ts, but requires microseconds
11481432Sandyb  * instead of nanoseconds, so an interger divide by 1000 routine
11491432Sandyb  * completes the conversion
11501432Sandyb  */
11511432Sandyb 	t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12);
11521432Sandyb 	q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14);
11531432Sandyb 	q = q >> 9;
11541432Sandyb 	r = nsec - q*1000;
11551432Sandyb 	tvp->tv_usec = q + ((r + 24) >> 10);
11561432Sandyb 
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate int
11600Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	timespec_t rqtime;
11630Sstevel@tonic-gate 	timespec_t rmtime;
11640Sstevel@tonic-gate 	timespec_t now;
11650Sstevel@tonic-gate 	int ret = 1;
11660Sstevel@tonic-gate 	model_t datamodel = get_udatamodel();
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
11690Sstevel@tonic-gate 		if (copyin(rqtp, &rqtime, sizeof (rqtime)))
11700Sstevel@tonic-gate 			return (set_errno(EFAULT));
11710Sstevel@tonic-gate 	} else {
11720Sstevel@tonic-gate 		timespec32_t rqtime32;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		if (copyin(rqtp, &rqtime32, sizeof (rqtime32)))
11750Sstevel@tonic-gate 			return (set_errno(EFAULT));
11760Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32);
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 ||
11800Sstevel@tonic-gate 	    rqtime.tv_nsec >= NANOSEC)
11810Sstevel@tonic-gate 		return (set_errno(EINVAL));
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	if (timerspecisset(&rqtime)) {
11840Sstevel@tonic-gate 		gethrestime(&now);
11850Sstevel@tonic-gate 		timespecadd(&rqtime, &now);
11860Sstevel@tonic-gate 		mutex_enter(&curthread->t_delay_lock);
11870Sstevel@tonic-gate 		while ((ret = cv_waituntil_sig(&curthread->t_delay_cv,
11883642Sqiao 		    &curthread->t_delay_lock, &rqtime)) > 0)
11890Sstevel@tonic-gate 			continue;
11900Sstevel@tonic-gate 		mutex_exit(&curthread->t_delay_lock);
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	if (rmtp) {
11940Sstevel@tonic-gate 		/*
11950Sstevel@tonic-gate 		 * If cv_waituntil_sig() returned due to a signal, and
11960Sstevel@tonic-gate 		 * there is time remaining, then set the time remaining.
11970Sstevel@tonic-gate 		 * Else set time remaining to zero
11980Sstevel@tonic-gate 		 */
11990Sstevel@tonic-gate 		rmtime.tv_sec = rmtime.tv_nsec = 0;
12000Sstevel@tonic-gate 		if (ret == 0) {
12013346Svb160487 			timespec_t delta = rqtime;
12023346Svb160487 
12030Sstevel@tonic-gate 			gethrestime(&now);
12043346Svb160487 			timespecsub(&delta, &now);
12053346Svb160487 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
12063346Svb160487 			    delta.tv_nsec > 0))
12073346Svb160487 				rmtime = delta;
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
12110Sstevel@tonic-gate 			if (copyout(&rmtime, rmtp, sizeof (rmtime)))
12120Sstevel@tonic-gate 				return (set_errno(EFAULT));
12130Sstevel@tonic-gate 		} else {
12140Sstevel@tonic-gate 			timespec32_t rmtime32;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 			TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
12170Sstevel@tonic-gate 			if (copyout(&rmtime32, rmtp, sizeof (rmtime32)))
12180Sstevel@tonic-gate 				return (set_errno(EFAULT));
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	if (ret == 0)
12230Sstevel@tonic-gate 		return (set_errno(EINTR));
12240Sstevel@tonic-gate 	return (0);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate  * Routines to convert standard UNIX time (seconds since Jan 1, 1970)
12290Sstevel@tonic-gate  * into year/month/day/hour/minute/second format, and back again.
12300Sstevel@tonic-gate  * Note: these routines require tod_lock held to protect cached state.
12310Sstevel@tonic-gate  */
12320Sstevel@tonic-gate static int days_thru_month[64] = {
12330Sstevel@tonic-gate 	0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0,
12340Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
12350Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
12360Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
12370Sstevel@tonic-gate };
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate todinfo_t saved_tod;
12400Sstevel@tonic-gate int saved_utc = -60;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate todinfo_t
12430Sstevel@tonic-gate utc_to_tod(time_t utc)
12440Sstevel@tonic-gate {
12450Sstevel@tonic-gate 	long dse, day, month, year;
12460Sstevel@tonic-gate 	todinfo_t tod;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	if (utc < 0)			/* should never happen */
12510Sstevel@tonic-gate 		utc = 0;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	saved_tod.tod_sec += utc - saved_utc;
12540Sstevel@tonic-gate 	saved_utc = utc;
12550Sstevel@tonic-gate 	if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60)
12560Sstevel@tonic-gate 		return (saved_tod);	/* only the seconds changed */
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	dse = utc / 86400;		/* days since epoch */
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	tod.tod_sec = utc % 60;
12610Sstevel@tonic-gate 	tod.tod_min = (utc % 3600) / 60;
12620Sstevel@tonic-gate 	tod.tod_hour = (utc % 86400) / 3600;
12630Sstevel@tonic-gate 	tod.tod_dow = (dse + 4) % 7 + 1;	/* epoch was a Thursday */
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	year = dse / 365 + 72;	/* first guess -- always a bit too large */
12660Sstevel@tonic-gate 	do {
12670Sstevel@tonic-gate 		year--;
12680Sstevel@tonic-gate 		day = dse - 365 * (year - 70) - ((year - 69) >> 2);
12690Sstevel@tonic-gate 	} while (day < 0);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	month = ((year & 3) << 4) + 1;
12720Sstevel@tonic-gate 	while (day >= days_thru_month[month + 1])
12730Sstevel@tonic-gate 		month++;
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	tod.tod_day = day - days_thru_month[month] + 1;
12760Sstevel@tonic-gate 	tod.tod_month = month & 15;
12770Sstevel@tonic-gate 	tod.tod_year = year;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	saved_tod = tod;
12800Sstevel@tonic-gate 	return (tod);
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate time_t
12840Sstevel@tonic-gate tod_to_utc(todinfo_t tod)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate 	time_t utc;
12870Sstevel@tonic-gate 	int year = tod.tod_year;
12880Sstevel@tonic-gate 	int month = tod.tod_month + ((year & 3) << 4);
12890Sstevel@tonic-gate #ifdef DEBUG
12900Sstevel@tonic-gate 	/* only warn once, not each time called */
12910Sstevel@tonic-gate 	static int year_warn = 1;
12920Sstevel@tonic-gate 	static int month_warn = 1;
12930Sstevel@tonic-gate 	static int day_warn = 1;
12940Sstevel@tonic-gate 	static int hour_warn = 1;
12950Sstevel@tonic-gate 	static int min_warn = 1;
12960Sstevel@tonic-gate 	static int sec_warn = 1;
12970Sstevel@tonic-gate 	int days_diff = days_thru_month[month + 1] - days_thru_month[month];
12980Sstevel@tonic-gate #endif
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate #ifdef DEBUG
13030Sstevel@tonic-gate 	if (year_warn && (year < 70 || year > 8029)) {
13040Sstevel@tonic-gate 		cmn_err(CE_WARN,
13050Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13060Sstevel@tonic-gate 			"wrong years value %d -- time needs to be reset\n",
13070Sstevel@tonic-gate 			year);
13080Sstevel@tonic-gate 		year_warn = 0;
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) {
13120Sstevel@tonic-gate 		cmn_err(CE_WARN,
13130Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13140Sstevel@tonic-gate 			"wrong months value %d -- time needs to be reset\n",
13150Sstevel@tonic-gate 			tod.tod_month);
13160Sstevel@tonic-gate 		month_warn = 0;
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) {
13200Sstevel@tonic-gate 		cmn_err(CE_WARN,
13210Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13220Sstevel@tonic-gate 			"wrong days value %d -- time needs to be reset\n",
13230Sstevel@tonic-gate 			tod.tod_day);
13240Sstevel@tonic-gate 		day_warn = 0;
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) {
13280Sstevel@tonic-gate 		cmn_err(CE_WARN,
13290Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13300Sstevel@tonic-gate 			"wrong hours value %d -- time needs to be reset\n",
13310Sstevel@tonic-gate 			tod.tod_hour);
13320Sstevel@tonic-gate 		hour_warn = 0;
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) {
13360Sstevel@tonic-gate 		cmn_err(CE_WARN,
13370Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13380Sstevel@tonic-gate 			"wrong minutes value %d -- time needs to be reset\n",
13390Sstevel@tonic-gate 			tod.tod_min);
13400Sstevel@tonic-gate 		min_warn = 0;
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) {
13440Sstevel@tonic-gate 		cmn_err(CE_WARN,
13450Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13460Sstevel@tonic-gate 			"wrong seconds value %d -- time needs to be reset\n",
13470Sstevel@tonic-gate 			tod.tod_sec);
13480Sstevel@tonic-gate 		sec_warn = 0;
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate #endif
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	utc = (year - 70);		/* next 3 lines: utc = 365y + y/4 */
13530Sstevel@tonic-gate 	utc += (utc << 3) + (utc << 6);
13540Sstevel@tonic-gate 	utc += (utc << 2) + ((year - 69) >> 2);
13550Sstevel@tonic-gate 	utc += days_thru_month[month] + tod.tod_day - 1;
13560Sstevel@tonic-gate 	utc = (utc << 3) + (utc << 4) + tod.tod_hour;	/* 24 * day + hour */
13570Sstevel@tonic-gate 	utc = (utc << 6) - (utc << 2) + tod.tod_min;	/* 60 * hour + min */
13580Sstevel@tonic-gate 	utc = (utc << 6) - (utc << 2) + tod.tod_sec;	/* 60 * min + sec */
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	return (utc);
13610Sstevel@tonic-gate }
1362