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