xref: /illumos-gate/usr/src/uts/common/os/timers.c (revision 3348528f7ec68bf2f11d0cbd5c3b9932ea7f0d5c)
17c478bd9Sstevel@tonic-gate /*
2*3348528fSdm120769  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * Copyright (c) 1982, 1986 Regents of the University of California.
107c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
117c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <sys/param.h>
157c478bd9Sstevel@tonic-gate #include <sys/user.h>
167c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
177c478bd9Sstevel@tonic-gate #include <sys/proc.h>
187c478bd9Sstevel@tonic-gate #include <sys/time.h>
197c478bd9Sstevel@tonic-gate #include <sys/systm.h>
207c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
217c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
227c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
237c478bd9Sstevel@tonic-gate #include <sys/timer.h>
247c478bd9Sstevel@tonic-gate #include <sys/debug.h>
257c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
267c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate static void	realitexpire(void *);
297c478bd9Sstevel@tonic-gate static void	realprofexpire(void *);
307c478bd9Sstevel@tonic-gate static void	timeval_advance(struct timeval *, struct timeval *);
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate kmutex_t tod_lock;	/* protects time-of-day stuff */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * Constant to define the minimum interval value of the ITIMER_REALPROF timer.
367c478bd9Sstevel@tonic-gate  * Value is in microseconds; defaults to 500 usecs.  Setting this value
377c478bd9Sstevel@tonic-gate  * significantly lower may allow for denial-of-service attacks.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate int itimer_realprof_minimum = 500;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * macro to compare a timeval to a timestruc
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	TVTSCMP(tvp, tsp, cmp) \
467c478bd9Sstevel@tonic-gate 	/* CSTYLED */ \
477c478bd9Sstevel@tonic-gate 	((tvp)->tv_sec cmp (tsp)->tv_sec || \
487c478bd9Sstevel@tonic-gate 	((tvp)->tv_sec == (tsp)->tv_sec && \
497c478bd9Sstevel@tonic-gate 	/* CSTYLED */ \
507c478bd9Sstevel@tonic-gate 	(tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec))
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Time of day and interval timer support.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * These routines provide the kernel entry points to get and set
567c478bd9Sstevel@tonic-gate  * the time-of-day and per-process interval timers.  Subroutines
577c478bd9Sstevel@tonic-gate  * here provide support for adding and subtracting timeval structures
587c478bd9Sstevel@tonic-gate  * and decrementing interval timers, optionally reloading the interval
597c478bd9Sstevel@tonic-gate  * timers when they expire.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * SunOS function to generate monotonically increasing time values.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate void
667c478bd9Sstevel@tonic-gate uniqtime(struct timeval *tv)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	static struct timeval last;
697c478bd9Sstevel@tonic-gate 	timestruc_t ts;
707c478bd9Sstevel@tonic-gate 	time_t sec;
717c478bd9Sstevel@tonic-gate 	int usec, nsec;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * protect modification of last
757c478bd9Sstevel@tonic-gate 	 */
767c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
777c478bd9Sstevel@tonic-gate 	gethrestime(&ts);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * Fast algorithm to convert nsec to usec -- see hrt2ts()
817c478bd9Sstevel@tonic-gate 	 * in common/os/timers.c for a full description.
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	nsec = ts.tv_nsec;
847c478bd9Sstevel@tonic-gate 	usec = nsec + (nsec >> 2);
857c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 1);
867c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 2);
877c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 4);
887c478bd9Sstevel@tonic-gate 	usec = nsec - (usec >> 3);
897c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 2);
907c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 3);
917c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 4);
927c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 1);
937c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 6);
947c478bd9Sstevel@tonic-gate 	usec = usec >> 10;
957c478bd9Sstevel@tonic-gate 	sec = ts.tv_sec;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * Try to keep timestamps unique, but don't be obsessive about
997c478bd9Sstevel@tonic-gate 	 * it in the face of large differences.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	if ((sec <= last.tv_sec) &&		/* same or lower seconds, and */
1027c478bd9Sstevel@tonic-gate 	    ((sec != last.tv_sec) ||		/* either different second or */
1037c478bd9Sstevel@tonic-gate 	    (usec <= last.tv_usec)) &&		/* lower microsecond, and */
1047c478bd9Sstevel@tonic-gate 	    ((last.tv_sec - sec) <= 5)) {	/* not way back in time */
1057c478bd9Sstevel@tonic-gate 		sec = last.tv_sec;
1067c478bd9Sstevel@tonic-gate 		usec = last.tv_usec + 1;
1077c478bd9Sstevel@tonic-gate 		if (usec >= MICROSEC) {
1087c478bd9Sstevel@tonic-gate 			usec -= MICROSEC;
1097c478bd9Sstevel@tonic-gate 			sec++;
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	last.tv_sec = sec;
1137c478bd9Sstevel@tonic-gate 	last.tv_usec = usec;
1147c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	tv->tv_sec = sec;
1177c478bd9Sstevel@tonic-gate 	tv->tv_usec = usec;
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Timestamps are exported from the kernel in several places.
1227c478bd9Sstevel@tonic-gate  * Such timestamps are commonly used for either uniqueness or for
1237c478bd9Sstevel@tonic-gate  * sequencing - truncation to 32-bits is fine for uniqueness,
1247c478bd9Sstevel@tonic-gate  * but sequencing is going to take more work as we get closer to 2038!
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate void
1277c478bd9Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	struct timeval tv;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	uniqtime(&tv);
1327c478bd9Sstevel@tonic-gate 	TIMEVAL_TO_TIMEVAL32(tv32p, &tv);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
1367c478bd9Sstevel@tonic-gate gettimeofday(struct timeval *tp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	struct timeval atv;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (tp) {
1417c478bd9Sstevel@tonic-gate 		uniqtime(&atv);
1427c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1437c478bd9Sstevel@tonic-gate 			if (copyout(&atv, tp, sizeof (atv)))
1447c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1457c478bd9Sstevel@tonic-gate 		} else {
1467c478bd9Sstevel@tonic-gate 			struct timeval32 tv32;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 			if (TIMEVAL_OVERFLOW(&atv))
1497c478bd9Sstevel@tonic-gate 				return (set_errno(EOVERFLOW));
1507c478bd9Sstevel@tonic-gate 			TIMEVAL_TO_TIMEVAL32(&tv32, &atv);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 			if (copyout(&tv32, tp, sizeof (tv32)))
1537c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	return (0);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
1607c478bd9Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int error;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE)
1657c478bd9Sstevel@tonic-gate 		error = xgetitimer(which, itv, 0);
1667c478bd9Sstevel@tonic-gate 	else {
1677c478bd9Sstevel@tonic-gate 		struct itimerval kitv;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		if ((error = xgetitimer(which, &kitv, 1)) == 0) {
1707c478bd9Sstevel@tonic-gate 			if (ITIMERVAL_OVERFLOW(&kitv)) {
1717c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
1727c478bd9Sstevel@tonic-gate 			} else {
1737c478bd9Sstevel@tonic-gate 				struct itimerval32 itv32;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 				ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv);
1767c478bd9Sstevel@tonic-gate 				if (copyout(&itv32, itv, sizeof (itv32)) != 0)
1777c478bd9Sstevel@tonic-gate 					error = EFAULT;
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate int
1867c478bd9Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
1897c478bd9Sstevel@tonic-gate 	struct timeval now;
1907c478bd9Sstevel@tonic-gate 	struct itimerval aitv;
1917c478bd9Sstevel@tonic-gate 	hrtime_t ts, first, interval, remain;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	switch (which) {
1967c478bd9Sstevel@tonic-gate 	case ITIMER_VIRTUAL:
1977c478bd9Sstevel@tonic-gate 	case ITIMER_PROF:
1987c478bd9Sstevel@tonic-gate 		aitv = ttolwp(curthread)->lwp_timer[which];
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	case ITIMER_REAL:
2027c478bd9Sstevel@tonic-gate 		uniqtime(&now);
2037c478bd9Sstevel@tonic-gate 		aitv = p->p_realitimer;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		if (timerisset(&aitv.it_value)) {
2067c478bd9Sstevel@tonic-gate 			/*CSTYLED*/
2077c478bd9Sstevel@tonic-gate 			if (timercmp(&aitv.it_value, &now, <)) {
2087c478bd9Sstevel@tonic-gate 				timerclear(&aitv.it_value);
2097c478bd9Sstevel@tonic-gate 			} else {
2107c478bd9Sstevel@tonic-gate 				timevalsub(&aitv.it_value, &now);
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		break;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	case ITIMER_REALPROF:
2167c478bd9Sstevel@tonic-gate 		if (curproc->p_rprof_cyclic == CYCLIC_NONE) {
2177c478bd9Sstevel@tonic-gate 			bzero(&aitv, sizeof (aitv));
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		aitv = curproc->p_rprof_timer;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		first = tv2hrt(&aitv.it_value);
2247c478bd9Sstevel@tonic-gate 		interval = tv2hrt(&aitv.it_interval);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		if ((ts = gethrtime()) < first) {
2277c478bd9Sstevel@tonic-gate 			/*
2287c478bd9Sstevel@tonic-gate 			 * We haven't gone off for the first time; the time
2297c478bd9Sstevel@tonic-gate 			 * remaining is simply the first time we will go
2307c478bd9Sstevel@tonic-gate 			 * off minus the current time.
2317c478bd9Sstevel@tonic-gate 			 */
2327c478bd9Sstevel@tonic-gate 			remain = first - ts;
2337c478bd9Sstevel@tonic-gate 		} else {
2347c478bd9Sstevel@tonic-gate 			if (interval == 0) {
2357c478bd9Sstevel@tonic-gate 				/*
2367c478bd9Sstevel@tonic-gate 				 * This was set as a one-shot, and we've
2377c478bd9Sstevel@tonic-gate 				 * already gone off; there is no time
2387c478bd9Sstevel@tonic-gate 				 * remaining.
2397c478bd9Sstevel@tonic-gate 				 */
2407c478bd9Sstevel@tonic-gate 				remain = 0;
2417c478bd9Sstevel@tonic-gate 			} else {
2427c478bd9Sstevel@tonic-gate 				/*
2437c478bd9Sstevel@tonic-gate 				 * We have a non-zero interval; we need to
2447c478bd9Sstevel@tonic-gate 				 * determine how far we are into the current
2457c478bd9Sstevel@tonic-gate 				 * interval, and subtract that from the
2467c478bd9Sstevel@tonic-gate 				 * interval to determine the time remaining.
2477c478bd9Sstevel@tonic-gate 				 */
2487c478bd9Sstevel@tonic-gate 				remain = interval - ((ts - first) % interval);
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		hrt2tv(remain, &aitv.it_value);
2537c478bd9Sstevel@tonic-gate 		break;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	default:
2567c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2577c478bd9Sstevel@tonic-gate 		return (EINVAL);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (iskaddr) {
2637c478bd9Sstevel@tonic-gate 		bcopy(&aitv, itv, sizeof (*itv));
2647c478bd9Sstevel@tonic-gate 	} else {
2657c478bd9Sstevel@tonic-gate 		ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
2667c478bd9Sstevel@tonic-gate 		if (copyout(&aitv, itv, sizeof (*itv)))
2677c478bd9Sstevel@tonic-gate 			return (EFAULT);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	return (0);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate int
2757c478bd9Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	int error;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (oitv != NULL)
2807c478bd9Sstevel@tonic-gate 		if ((error = getitimer(which, oitv)) != 0)
2817c478bd9Sstevel@tonic-gate 			return (error);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (itv == NULL)
2847c478bd9Sstevel@tonic-gate 		return (0);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE)
2877c478bd9Sstevel@tonic-gate 		error = xsetitimer(which, itv, 0);
2887c478bd9Sstevel@tonic-gate 	else {
2897c478bd9Sstevel@tonic-gate 		struct itimerval32 itv32;
2907c478bd9Sstevel@tonic-gate 		struct itimerval kitv;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		if (copyin(itv, &itv32, sizeof (itv32)))
2937c478bd9Sstevel@tonic-gate 			error = EFAULT;
2947c478bd9Sstevel@tonic-gate 		ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32);
2957c478bd9Sstevel@tonic-gate 		error = xsetitimer(which, &kitv, 1);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate int
3027c478bd9Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct itimerval aitv;
3057c478bd9Sstevel@tonic-gate 	struct timeval now;
3067c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
3077c478bd9Sstevel@tonic-gate 	kthread_t *t;
3087c478bd9Sstevel@tonic-gate 	timeout_id_t tmp_id;
3097c478bd9Sstevel@tonic-gate 	cyc_handler_t hdlr;
3107c478bd9Sstevel@tonic-gate 	cyc_time_t when;
3117c478bd9Sstevel@tonic-gate 	cyclic_id_t cyclic;
3127c478bd9Sstevel@tonic-gate 	hrtime_t ts;
3137c478bd9Sstevel@tonic-gate 	int min;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (itv == NULL)
3167c478bd9Sstevel@tonic-gate 		return (0);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (iskaddr) {
3197c478bd9Sstevel@tonic-gate 		bcopy(itv, &aitv, sizeof (aitv));
3207c478bd9Sstevel@tonic-gate 	} else {
3217c478bd9Sstevel@tonic-gate 		ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
3227c478bd9Sstevel@tonic-gate 		if (copyin(itv, &aitv, sizeof (aitv)))
3237c478bd9Sstevel@tonic-gate 			return (EFAULT);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (which == ITIMER_REALPROF) {
3277c478bd9Sstevel@tonic-gate 		min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)),
3287c478bd9Sstevel@tonic-gate 		    itimer_realprof_minimum);
3297c478bd9Sstevel@tonic-gate 	} else {
3307c478bd9Sstevel@tonic-gate 		min = usec_per_tick;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (itimerfix(&aitv.it_value, min) ||
3347c478bd9Sstevel@tonic-gate 	    (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value)))
3357c478bd9Sstevel@tonic-gate 		return (EINVAL);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3387c478bd9Sstevel@tonic-gate 	switch (which) {
3397c478bd9Sstevel@tonic-gate 	case ITIMER_REAL:
3407c478bd9Sstevel@tonic-gate 		/*
3417c478bd9Sstevel@tonic-gate 		 * The SITBUSY flag prevents conflicts with multiple
3427c478bd9Sstevel@tonic-gate 		 * threads attempting to perform setitimer(ITIMER_REAL)
3437c478bd9Sstevel@tonic-gate 		 * at the same time, even when we drop p->p_lock below.
3447c478bd9Sstevel@tonic-gate 		 * Any blocked thread returns successfully because the
3457c478bd9Sstevel@tonic-gate 		 * effect is the same as if it got here first, finished,
3467c478bd9Sstevel@tonic-gate 		 * and the other thread then came through and destroyed
3477c478bd9Sstevel@tonic-gate 		 * what it did.  We are just protecting the system from
3487c478bd9Sstevel@tonic-gate 		 * malfunctioning due to the race condition.
3497c478bd9Sstevel@tonic-gate 		 */
3507c478bd9Sstevel@tonic-gate 		if (p->p_flag & SITBUSY) {
3517c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3527c478bd9Sstevel@tonic-gate 			return (0);
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		p->p_flag |= SITBUSY;
3557c478bd9Sstevel@tonic-gate 		while ((tmp_id = p->p_itimerid) != 0) {
3567c478bd9Sstevel@tonic-gate 			/*
3577c478bd9Sstevel@tonic-gate 			 * Avoid deadlock in callout_delete (called from
3587c478bd9Sstevel@tonic-gate 			 * untimeout) which may go to sleep (while holding
3597c478bd9Sstevel@tonic-gate 			 * p_lock). Drop p_lock and re-acquire it after
3607c478bd9Sstevel@tonic-gate 			 * untimeout returns. Need to clear p_itimerid
3617c478bd9Sstevel@tonic-gate 			 * while holding p_lock.
3627c478bd9Sstevel@tonic-gate 			 */
3637c478bd9Sstevel@tonic-gate 			p->p_itimerid = 0;
3647c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3657c478bd9Sstevel@tonic-gate 			(void) untimeout(tmp_id);
3667c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 		if (timerisset(&aitv.it_value)) {
3697c478bd9Sstevel@tonic-gate 			uniqtime(&now);
3707c478bd9Sstevel@tonic-gate 			timevaladd(&aitv.it_value, &now);
3717c478bd9Sstevel@tonic-gate 			p->p_itimerid = realtime_timeout(realitexpire,
3727c478bd9Sstevel@tonic-gate 			    p, hzto(&aitv.it_value));
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		p->p_realitimer = aitv;
3757c478bd9Sstevel@tonic-gate 		p->p_flag &= ~SITBUSY;
3767c478bd9Sstevel@tonic-gate 		break;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	case ITIMER_REALPROF:
3797c478bd9Sstevel@tonic-gate 		cyclic = p->p_rprof_cyclic;
3807c478bd9Sstevel@tonic-gate 		p->p_rprof_cyclic = CYCLIC_NONE;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * We're now going to acquire cpu_lock, remove the old cyclic
3867c478bd9Sstevel@tonic-gate 		 * if necessary, and add our new cyclic.
3877c478bd9Sstevel@tonic-gate 		 */
3887c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		if (cyclic != CYCLIC_NONE)
3917c478bd9Sstevel@tonic-gate 			cyclic_remove(cyclic);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		if (!timerisset(&aitv.it_value)) {
3947c478bd9Sstevel@tonic-gate 			/*
3957c478bd9Sstevel@tonic-gate 			 * If we were passed a value of 0, we're done.
3967c478bd9Sstevel@tonic-gate 			 */
3977c478bd9Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
3987c478bd9Sstevel@tonic-gate 			return (0);
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		hdlr.cyh_func = realprofexpire;
4027c478bd9Sstevel@tonic-gate 		hdlr.cyh_arg = p;
4037c478bd9Sstevel@tonic-gate 		hdlr.cyh_level = CY_LOW_LEVEL;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value));
4067c478bd9Sstevel@tonic-gate 		when.cyt_interval = tv2hrt(&aitv.it_interval);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		if (when.cyt_interval == 0) {
4097c478bd9Sstevel@tonic-gate 			/*
4107c478bd9Sstevel@tonic-gate 			 * Using the same logic as for CLOCK_HIGHRES timers, we
4117c478bd9Sstevel@tonic-gate 			 * set the interval to be INT64_MAX - when.cyt_when to
4127c478bd9Sstevel@tonic-gate 			 * effect a one-shot; see the comment in clock_highres.c
4137c478bd9Sstevel@tonic-gate 			 * for more details on why this works.
4147c478bd9Sstevel@tonic-gate 			 */
4157c478bd9Sstevel@tonic-gate 			when.cyt_interval = INT64_MAX - when.cyt_when;
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		cyclic = cyclic_add(&hdlr, &when);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		/*
4237c478bd9Sstevel@tonic-gate 		 * We have now successfully added the cyclic.  Reacquire
4247c478bd9Sstevel@tonic-gate 		 * p_lock, and see if anyone has snuck in.
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		if (p->p_rprof_cyclic != CYCLIC_NONE) {
4297c478bd9Sstevel@tonic-gate 			/*
4307c478bd9Sstevel@tonic-gate 			 * We're racing with another thread establishing an
4317c478bd9Sstevel@tonic-gate 			 * ITIMER_REALPROF interval timer.  We'll let the other
4327c478bd9Sstevel@tonic-gate 			 * thread win (this is a race at the application level,
4337c478bd9Sstevel@tonic-gate 			 * so letting the other thread win is acceptable).
4347c478bd9Sstevel@tonic-gate 			 */
4357c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4367c478bd9Sstevel@tonic-gate 			mutex_enter(&cpu_lock);
4377c478bd9Sstevel@tonic-gate 			cyclic_remove(cyclic);
4387c478bd9Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			return (0);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		/*
4447c478bd9Sstevel@tonic-gate 		 * Success.  Set our tracking variables in the proc structure,
4457c478bd9Sstevel@tonic-gate 		 * cancel any outstanding ITIMER_PROF, and allocate the
4467c478bd9Sstevel@tonic-gate 		 * per-thread SIGPROF buffers, if possible.
4477c478bd9Sstevel@tonic-gate 		 */
4487c478bd9Sstevel@tonic-gate 		hrt2tv(ts, &aitv.it_value);
4497c478bd9Sstevel@tonic-gate 		p->p_rprof_timer = aitv;
4507c478bd9Sstevel@tonic-gate 		p->p_rprof_cyclic = cyclic;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		t = p->p_tlist;
4537c478bd9Sstevel@tonic-gate 		do {
4547c478bd9Sstevel@tonic-gate 			struct itimerval *itvp;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 			itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF];
4577c478bd9Sstevel@tonic-gate 			timerclear(&itvp->it_interval);
4587c478bd9Sstevel@tonic-gate 			timerclear(&itvp->it_value);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 			if (t->t_rprof != NULL)
4617c478bd9Sstevel@tonic-gate 				continue;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			t->t_rprof =
4647c478bd9Sstevel@tonic-gate 			    kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP);
4657c478bd9Sstevel@tonic-gate 			aston(t);
4667c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		break;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	case ITIMER_VIRTUAL:
4717c478bd9Sstevel@tonic-gate 		ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv;
4727c478bd9Sstevel@tonic-gate 		break;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	case ITIMER_PROF:
4757c478bd9Sstevel@tonic-gate 		if (p->p_rprof_cyclic != CYCLIC_NONE) {
4767c478bd9Sstevel@tonic-gate 			/*
4777c478bd9Sstevel@tonic-gate 			 * Silently ignore ITIMER_PROF if ITIMER_REALPROF
4787c478bd9Sstevel@tonic-gate 			 * is in effect.
4797c478bd9Sstevel@tonic-gate 			 */
4807c478bd9Sstevel@tonic-gate 			break;
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv;
4847c478bd9Sstevel@tonic-gate 		break;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	default:
4877c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4887c478bd9Sstevel@tonic-gate 		return (EINVAL);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4917c478bd9Sstevel@tonic-gate 	return (0);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate  * Real interval timer expired:
4967c478bd9Sstevel@tonic-gate  * send process whose timer expired an alarm signal.
4977c478bd9Sstevel@tonic-gate  * If time is not set up to reload, then just return.
4987c478bd9Sstevel@tonic-gate  * Else compute next time timer should go off which is > current time.
4997c478bd9Sstevel@tonic-gate  * This is where delay in processing this timeout causes multiple
5007c478bd9Sstevel@tonic-gate  * SIGALRM calls to be compressed into one.
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate static void
5037c478bd9Sstevel@tonic-gate realitexpire(void *arg)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	struct proc *p = arg;
5067c478bd9Sstevel@tonic-gate 	struct timeval *valp = &p->p_realitimer.it_value;
5077c478bd9Sstevel@tonic-gate 	struct timeval *intervalp = &p->p_realitimer.it_interval;
5087c478bd9Sstevel@tonic-gate #if !defined(_LP64)
5097c478bd9Sstevel@tonic-gate 	clock_t	ticks;
5107c478bd9Sstevel@tonic-gate #endif
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5137c478bd9Sstevel@tonic-gate #if !defined(_LP64)
5147c478bd9Sstevel@tonic-gate 	if ((ticks = hzto(valp)) > 1) {
5157c478bd9Sstevel@tonic-gate 		/*
5167c478bd9Sstevel@tonic-gate 		 * If we are executing before we were meant to, it must be
5177c478bd9Sstevel@tonic-gate 		 * because of an overflow in a prior hzto() calculation.
5187c478bd9Sstevel@tonic-gate 		 * In this case, we want to go to sleep for the recalculated
5197c478bd9Sstevel@tonic-gate 		 * number of ticks. For the special meaning of the value "1"
5207c478bd9Sstevel@tonic-gate 		 * see comment in timespectohz().
5217c478bd9Sstevel@tonic-gate 		 */
5227c478bd9Sstevel@tonic-gate 		p->p_itimerid = realtime_timeout(realitexpire, p, ticks);
5237c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5247c478bd9Sstevel@tonic-gate 		return;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate #endif
5277c478bd9Sstevel@tonic-gate 	sigtoproc(p, NULL, SIGALRM);
5287c478bd9Sstevel@tonic-gate 	if (!timerisset(intervalp)) {
5297c478bd9Sstevel@tonic-gate 		timerclear(valp);
5307c478bd9Sstevel@tonic-gate 		p->p_itimerid = 0;
5317c478bd9Sstevel@tonic-gate 	} else {
5327c478bd9Sstevel@tonic-gate 		/* advance timer value past current time */
5337c478bd9Sstevel@tonic-gate 		timeval_advance(valp, intervalp);
5347c478bd9Sstevel@tonic-gate 		p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp));
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * Real time profiling interval timer expired:
5417c478bd9Sstevel@tonic-gate  * Increment microstate counters for each lwp in the process
5427c478bd9Sstevel@tonic-gate  * and ensure that running lwps are kicked into the kernel.
5437c478bd9Sstevel@tonic-gate  * If time is not set up to reload, then just return.
5447c478bd9Sstevel@tonic-gate  * Else compute next time timer should go off which is > current time,
5457c478bd9Sstevel@tonic-gate  * as above.
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate static void
5487c478bd9Sstevel@tonic-gate realprofexpire(void *arg)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	struct proc *p = arg;
5517c478bd9Sstevel@tonic-gate 	kthread_t *t;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5547c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) == NULL) {
5557c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5567c478bd9Sstevel@tonic-gate 		return;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	do {
5597c478bd9Sstevel@tonic-gate 		int mstate;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * Attempt to allocate the SIGPROF buffer, but don't sleep.
5637c478bd9Sstevel@tonic-gate 		 */
5647c478bd9Sstevel@tonic-gate 		if (t->t_rprof == NULL)
5657c478bd9Sstevel@tonic-gate 			t->t_rprof = kmem_zalloc(sizeof (struct rprof),
5667c478bd9Sstevel@tonic-gate 			    KM_NOSLEEP);
5677c478bd9Sstevel@tonic-gate 		if (t->t_rprof == NULL)
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		thread_lock(t);
5717c478bd9Sstevel@tonic-gate 		switch (t->t_state) {
5727c478bd9Sstevel@tonic-gate 		case TS_SLEEP:
5737c478bd9Sstevel@tonic-gate 			/*
5747c478bd9Sstevel@tonic-gate 			 * Don't touch the lwp is it is swapped out.
5757c478bd9Sstevel@tonic-gate 			 */
5767c478bd9Sstevel@tonic-gate 			if (!(t->t_schedflag & TS_LOAD)) {
5777c478bd9Sstevel@tonic-gate 				mstate = LMS_SLEEP;
5787c478bd9Sstevel@tonic-gate 				break;
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 			switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) {
5817c478bd9Sstevel@tonic-gate 			case LMS_TFAULT:
5827c478bd9Sstevel@tonic-gate 			case LMS_DFAULT:
5837c478bd9Sstevel@tonic-gate 			case LMS_KFAULT:
5847c478bd9Sstevel@tonic-gate 			case LMS_USER_LOCK:
5857c478bd9Sstevel@tonic-gate 				break;
5867c478bd9Sstevel@tonic-gate 			default:
5877c478bd9Sstevel@tonic-gate 				mstate = LMS_SLEEP;
5887c478bd9Sstevel@tonic-gate 				break;
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 			break;
5917c478bd9Sstevel@tonic-gate 		case TS_RUN:
592c97ad5cdSakolb 		case TS_WAIT:
5937c478bd9Sstevel@tonic-gate 			mstate = LMS_WAIT_CPU;
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 		case TS_ONPROC:
5967c478bd9Sstevel@tonic-gate 			switch (mstate = t->t_mstate) {
5977c478bd9Sstevel@tonic-gate 			case LMS_USER:
5987c478bd9Sstevel@tonic-gate 			case LMS_SYSTEM:
5997c478bd9Sstevel@tonic-gate 			case LMS_TRAP:
6007c478bd9Sstevel@tonic-gate 				break;
6017c478bd9Sstevel@tonic-gate 			default:
6027c478bd9Sstevel@tonic-gate 				mstate = LMS_SYSTEM;
6037c478bd9Sstevel@tonic-gate 				break;
6047c478bd9Sstevel@tonic-gate 			}
6057c478bd9Sstevel@tonic-gate 			break;
6067c478bd9Sstevel@tonic-gate 		default:
6077c478bd9Sstevel@tonic-gate 			mstate = t->t_mstate;
6087c478bd9Sstevel@tonic-gate 			break;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		t->t_rprof->rp_anystate = 1;
6117c478bd9Sstevel@tonic-gate 		t->t_rprof->rp_state[mstate]++;
6127c478bd9Sstevel@tonic-gate 		aston(t);
6137c478bd9Sstevel@tonic-gate 		/*
6147c478bd9Sstevel@tonic-gate 		 * force the thread into the kernel
6157c478bd9Sstevel@tonic-gate 		 * if it is not already there.
6167c478bd9Sstevel@tonic-gate 		 */
6177c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
6187c478bd9Sstevel@tonic-gate 			poke_cpu(t->t_cpu->cpu_id);
6197c478bd9Sstevel@tonic-gate 		thread_unlock(t);
6207c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Advances timer value past the current time of day.  See the detailed
6277c478bd9Sstevel@tonic-gate  * comment for this logic in realitsexpire(), above.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate static void
6307c478bd9Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	int cnt2nth;
6337c478bd9Sstevel@tonic-gate 	struct timeval interval2nth;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	for (;;) {
6367c478bd9Sstevel@tonic-gate 		interval2nth = *intervalp;
6377c478bd9Sstevel@tonic-gate 		for (cnt2nth = 0; ; cnt2nth++) {
6387c478bd9Sstevel@tonic-gate 			timevaladd(valp, &interval2nth);
6397c478bd9Sstevel@tonic-gate 			/*CSTYLED*/
6407c478bd9Sstevel@tonic-gate 			if (TVTSCMP(valp, &hrestime, >))
6417c478bd9Sstevel@tonic-gate 				break;
6427c478bd9Sstevel@tonic-gate 			timevaladd(&interval2nth, &interval2nth);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 		if (cnt2nth == 0)
6457c478bd9Sstevel@tonic-gate 			break;
6467c478bd9Sstevel@tonic-gate 		timevalsub(valp, &interval2nth);
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * Check that a proposed value to load into the .it_value or .it_interval
6527c478bd9Sstevel@tonic-gate  * part of an interval timer is acceptable, and set it to at least a
6537c478bd9Sstevel@tonic-gate  * specified minimal value.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate int
6567c478bd9Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
6597c478bd9Sstevel@tonic-gate 	    tv->tv_usec < 0 || tv->tv_usec >= MICROSEC)
6607c478bd9Sstevel@tonic-gate 		return (EINVAL);
6617c478bd9Sstevel@tonic-gate 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum)
6627c478bd9Sstevel@tonic-gate 		tv->tv_usec = minimum;
6637c478bd9Sstevel@tonic-gate 	return (0);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * Same as itimerfix, except a) it takes a timespec instead of a timeval and
6687c478bd9Sstevel@tonic-gate  * b) it doesn't truncate based on timeout granularity; consumers of this
6697c478bd9Sstevel@tonic-gate  * interface (e.g. timer_settime()) depend on the passed timespec not being
6707c478bd9Sstevel@tonic-gate  * modified implicitly.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate int
6737c478bd9Sstevel@tonic-gate itimerspecfix(timespec_t *tv)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC)
6767c478bd9Sstevel@tonic-gate 		return (EINVAL);
6777c478bd9Sstevel@tonic-gate 	return (0);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * Decrement an interval timer by a specified number
6827c478bd9Sstevel@tonic-gate  * of microseconds, which must be less than a second,
6837c478bd9Sstevel@tonic-gate  * i.e. < 1000000.  If the timer expires, then reload
6847c478bd9Sstevel@tonic-gate  * it.  In this case, carry over (usec - old value) to
6857c478bd9Sstevel@tonic-gate  * reducint the value reloaded into the timer so that
6867c478bd9Sstevel@tonic-gate  * the timer does not drift.  This routine assumes
6877c478bd9Sstevel@tonic-gate  * that it is called in a context where the timers
6887c478bd9Sstevel@tonic-gate  * on which it is operating cannot change in value.
6897c478bd9Sstevel@tonic-gate  */
6907c478bd9Sstevel@tonic-gate int
6917c478bd9Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	if (itp->it_value.tv_usec < usec) {
6947c478bd9Sstevel@tonic-gate 		if (itp->it_value.tv_sec == 0) {
6957c478bd9Sstevel@tonic-gate 			/* expired, and already in next interval */
6967c478bd9Sstevel@tonic-gate 			usec -= itp->it_value.tv_usec;
6977c478bd9Sstevel@tonic-gate 			goto expire;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		itp->it_value.tv_usec += MICROSEC;
7007c478bd9Sstevel@tonic-gate 		itp->it_value.tv_sec--;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	itp->it_value.tv_usec -= usec;
7037c478bd9Sstevel@tonic-gate 	usec = 0;
7047c478bd9Sstevel@tonic-gate 	if (timerisset(&itp->it_value))
7057c478bd9Sstevel@tonic-gate 		return (1);
7067c478bd9Sstevel@tonic-gate 	/* expired, exactly at end of interval */
7077c478bd9Sstevel@tonic-gate expire:
7087c478bd9Sstevel@tonic-gate 	if (timerisset(&itp->it_interval)) {
7097c478bd9Sstevel@tonic-gate 		itp->it_value = itp->it_interval;
7107c478bd9Sstevel@tonic-gate 		itp->it_value.tv_usec -= usec;
7117c478bd9Sstevel@tonic-gate 		if (itp->it_value.tv_usec < 0) {
7127c478bd9Sstevel@tonic-gate 			itp->it_value.tv_usec += MICROSEC;
7137c478bd9Sstevel@tonic-gate 			itp->it_value.tv_sec--;
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 	} else
7167c478bd9Sstevel@tonic-gate 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
7177c478bd9Sstevel@tonic-gate 	return (0);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate  * Add and subtract routines for timevals.
7227c478bd9Sstevel@tonic-gate  * N.B.: subtract routine doesn't deal with
7237c478bd9Sstevel@tonic-gate  * results which are before the beginning,
7247c478bd9Sstevel@tonic-gate  * it just gets very confused in this case.
7257c478bd9Sstevel@tonic-gate  * Caveat emptor.
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate void
7287c478bd9Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
7317c478bd9Sstevel@tonic-gate 	t1->tv_usec += t2->tv_usec;
7327c478bd9Sstevel@tonic-gate 	timevalfix(t1);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate void
7367c478bd9Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
7397c478bd9Sstevel@tonic-gate 	t1->tv_usec -= t2->tv_usec;
7407c478bd9Sstevel@tonic-gate 	timevalfix(t1);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate void
7447c478bd9Sstevel@tonic-gate timevalfix(struct timeval *t1)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	if (t1->tv_usec < 0) {
7477c478bd9Sstevel@tonic-gate 		t1->tv_sec--;
7487c478bd9Sstevel@tonic-gate 		t1->tv_usec += MICROSEC;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	if (t1->tv_usec >= MICROSEC) {
7517c478bd9Sstevel@tonic-gate 		t1->tv_sec++;
7527c478bd9Sstevel@tonic-gate 		t1->tv_usec -= MICROSEC;
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate  * Same as the routines above. These routines take a timespec instead
7587c478bd9Sstevel@tonic-gate  * of a timeval.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate void
7617c478bd9Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
7647c478bd9Sstevel@tonic-gate 	t1->tv_nsec += t2->tv_nsec;
7657c478bd9Sstevel@tonic-gate 	timespecfix(t1);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate void
7697c478bd9Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
7727c478bd9Sstevel@tonic-gate 	t1->tv_nsec -= t2->tv_nsec;
7737c478bd9Sstevel@tonic-gate 	timespecfix(t1);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate void
7777c478bd9Sstevel@tonic-gate timespecfix(timespec_t *t1)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	if (t1->tv_nsec < 0) {
7807c478bd9Sstevel@tonic-gate 		t1->tv_sec--;
7817c478bd9Sstevel@tonic-gate 		t1->tv_nsec += NANOSEC;
7827c478bd9Sstevel@tonic-gate 	} else {
7837c478bd9Sstevel@tonic-gate 		if (t1->tv_nsec >= NANOSEC) {
7847c478bd9Sstevel@tonic-gate 			t1->tv_sec++;
7857c478bd9Sstevel@tonic-gate 			t1->tv_nsec -= NANOSEC;
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * Compute number of hz until specified time.
7927c478bd9Sstevel@tonic-gate  * Used to compute third argument to timeout() from an absolute time.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate clock_t
7957c478bd9Sstevel@tonic-gate hzto(struct timeval *tv)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	timespec_t ts, now;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	ts.tv_sec = tv->tv_sec;
8007c478bd9Sstevel@tonic-gate 	ts.tv_nsec = tv->tv_usec * 1000;
8017c478bd9Sstevel@tonic-gate 	gethrestime_lasttick(&now);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	return (timespectohz(&ts, now));
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * Compute number of hz until specified time for a given timespec value.
8087c478bd9Sstevel@tonic-gate  * Used to compute third argument to timeout() from an absolute time.
8097c478bd9Sstevel@tonic-gate  */
8107c478bd9Sstevel@tonic-gate clock_t
8117c478bd9Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	clock_t	ticks;
8147c478bd9Sstevel@tonic-gate 	time_t	sec;
8157c478bd9Sstevel@tonic-gate 	int	nsec;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	/*
8187c478bd9Sstevel@tonic-gate 	 * Compute number of ticks we will see between now and
8197c478bd9Sstevel@tonic-gate 	 * the target time; returns "1" if the destination time
8207c478bd9Sstevel@tonic-gate 	 * is before the next tick, so we always get some delay,
8217c478bd9Sstevel@tonic-gate 	 * and returns LONG_MAX ticks if we would overflow.
8227c478bd9Sstevel@tonic-gate 	 */
8237c478bd9Sstevel@tonic-gate 	sec = tv->tv_sec - now.tv_sec;
8247c478bd9Sstevel@tonic-gate 	nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (nsec < 0) {
8277c478bd9Sstevel@tonic-gate 		sec--;
8287c478bd9Sstevel@tonic-gate 		nsec += NANOSEC;
8297c478bd9Sstevel@tonic-gate 	} else if (nsec >= NANOSEC) {
8307c478bd9Sstevel@tonic-gate 		sec++;
8317c478bd9Sstevel@tonic-gate 		nsec -= NANOSEC;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	ticks = NSEC_TO_TICK(nsec);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/*
8377c478bd9Sstevel@tonic-gate 	 * Compute ticks, accounting for negative and overflow as above.
8387c478bd9Sstevel@tonic-gate 	 * Overflow protection kicks in at about 70 weeks for hz=50
8397c478bd9Sstevel@tonic-gate 	 * and at about 35 weeks for hz=100. (Rather longer for the 64-bit
8407c478bd9Sstevel@tonic-gate 	 * kernel :-)
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	if (sec < 0 || (sec == 0 && ticks < 1))
8437c478bd9Sstevel@tonic-gate 		ticks = 1;			/* protect vs nonpositive */
8447c478bd9Sstevel@tonic-gate 	else if (sec > (LONG_MAX - ticks) / hz)
8457c478bd9Sstevel@tonic-gate 		ticks = LONG_MAX;		/* protect vs overflow */
8467c478bd9Sstevel@tonic-gate 	else
8477c478bd9Sstevel@tonic-gate 		ticks += sec * hz;		/* common case */
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	return (ticks);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
853*3348528fSdm120769  * Same as timespectohz() except that we adjust the clock ticks down a bit.
854*3348528fSdm120769  * If we will be waiting for a long time, we may encounter skewing problems
855*3348528fSdm120769  * due to adjtime() system calls.  Since we can skew up to 1/16 lbolt rate
856*3348528fSdm120769  * if adjtime is going crazy, we reduce the time delta since timeout() takes
857*3348528fSdm120769  * clock ticks rather than wallclock elapsed time.  This may cause the caller
858*3348528fSdm120769  * (who calls timeout()) to return with a timeout prematurely and callers
859*3348528fSdm120769  * must accommodate this.  See lwp_timeout(), queue_lwptimer() and
860*3348528fSdm120769  * cv_waituntil_sig(), currently the only callers of this function.
861*3348528fSdm120769  */
862*3348528fSdm120769 clock_t
863*3348528fSdm120769 timespectohz_adj(timespec_t *tv, timespec_t now)
864*3348528fSdm120769 {
865*3348528fSdm120769 	timespec_t wait_time = *tv;
866*3348528fSdm120769 
867*3348528fSdm120769 	timespecsub(&wait_time, &now);
868*3348528fSdm120769 	wait_time.tv_sec -= wait_time.tv_sec >> 4;
869*3348528fSdm120769 	wait_time.tv_nsec -= wait_time.tv_nsec >> 4;
870*3348528fSdm120769 	timespecadd(&wait_time, &now);
871*3348528fSdm120769 	return (timespectohz(&wait_time, now));
872*3348528fSdm120769 }
873*3348528fSdm120769 
874*3348528fSdm120769 /*
8757c478bd9Sstevel@tonic-gate  * hrt2ts(): convert from hrtime_t to timestruc_t.
8767c478bd9Sstevel@tonic-gate  *
8777c478bd9Sstevel@tonic-gate  * All this routine really does is:
8787c478bd9Sstevel@tonic-gate  *
8797c478bd9Sstevel@tonic-gate  *	tsp->sec  = hrt / NANOSEC;
8807c478bd9Sstevel@tonic-gate  *	tsp->nsec = hrt % NANOSEC;
8817c478bd9Sstevel@tonic-gate  *
8827c478bd9Sstevel@tonic-gate  * The black magic below avoids doing a 64-bit by 32-bit integer divide,
8837c478bd9Sstevel@tonic-gate  * which is quite expensive.  There's actually much more going on here than
8847c478bd9Sstevel@tonic-gate  * it might first appear -- don't try this at home.
8857c478bd9Sstevel@tonic-gate  *
8867c478bd9Sstevel@tonic-gate  * For the adventuresome, here's an explanation of how it works.
8877c478bd9Sstevel@tonic-gate  *
8887c478bd9Sstevel@tonic-gate  * Multiplication by a fixed constant is easy -- you just do the appropriate
8897c478bd9Sstevel@tonic-gate  * shifts and adds.  For example, to multiply by 10, we observe that
8907c478bd9Sstevel@tonic-gate  *
8917c478bd9Sstevel@tonic-gate  *	x * 10	= x * (8 + 2)
8927c478bd9Sstevel@tonic-gate  *		= (x * 8) + (x * 2)
8937c478bd9Sstevel@tonic-gate  *		= (x << 3) + (x << 1).
8947c478bd9Sstevel@tonic-gate  *
8957c478bd9Sstevel@tonic-gate  * In general, you can read the algorithm right off the bits: the number 10
8967c478bd9Sstevel@tonic-gate  * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3).
8977c478bd9Sstevel@tonic-gate  *
8987c478bd9Sstevel@tonic-gate  * Sometimes you can do better.  For example, 15 is 1111 binary, so the normal
8997c478bd9Sstevel@tonic-gate  * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3).
9007c478bd9Sstevel@tonic-gate  * But, it's cheaper if you capitalize on the fact that you have a run of ones:
9017c478bd9Sstevel@tonic-gate  * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0).  [You would never
9027c478bd9Sstevel@tonic-gate  * actually perform the operation << 0, since it's a no-op; I'm just writing
9037c478bd9Sstevel@tonic-gate  * it that way for clarity.]
9047c478bd9Sstevel@tonic-gate  *
9057c478bd9Sstevel@tonic-gate  * The other way you can win is if you get lucky with the prime factorization
9067c478bd9Sstevel@tonic-gate  * of your constant.  The number 1,000,000,000, which we have to multiply
9077c478bd9Sstevel@tonic-gate  * by below, is a good example.  One billion is 111011100110101100101000000000
9087c478bd9Sstevel@tonic-gate  * in binary.  If you apply the bit-grouping trick, it doesn't buy you very
9097c478bd9Sstevel@tonic-gate  * much, because it's only a win for groups of three or more equal bits:
9107c478bd9Sstevel@tonic-gate  *
9117c478bd9Sstevel@tonic-gate  * 111011100110101100101000000000 = 1000000000000000000000000000000
9127c478bd9Sstevel@tonic-gate  *				  -  000100011001010011011000000000
9137c478bd9Sstevel@tonic-gate  *
9147c478bd9Sstevel@tonic-gate  * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS,
9157c478bd9Sstevel@tonic-gate  * we have reduced this to 10 shift/add pairs (20 operations) on the RHS.
9167c478bd9Sstevel@tonic-gate  * This is better, but not great.
9177c478bd9Sstevel@tonic-gate  *
9187c478bd9Sstevel@tonic-gate  * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125,
9197c478bd9Sstevel@tonic-gate  * and multiply by each factor.  Multiplication by 125 is particularly easy,
9207c478bd9Sstevel@tonic-gate  * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four
9217c478bd9Sstevel@tonic-gate  * operations.  So, to multiply by 1,000,000,000, we perform three multipli-
9227c478bd9Sstevel@tonic-gate  * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations.
9237c478bd9Sstevel@tonic-gate  * This is the algorithm we actually use in both hrt2ts() and ts2hrt().
9247c478bd9Sstevel@tonic-gate  *
9257c478bd9Sstevel@tonic-gate  * Division is harder; there is no equivalent of the simple shift-add algorithm
9267c478bd9Sstevel@tonic-gate  * we used for multiplication.  However, we can convert the division problem
9277c478bd9Sstevel@tonic-gate  * into a multiplication problem by pre-computing the binary representation
9287c478bd9Sstevel@tonic-gate  * of the reciprocal of the divisor.  For the case of interest, we have
9297c478bd9Sstevel@tonic-gate  *
9307c478bd9Sstevel@tonic-gate  *	1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30,
9317c478bd9Sstevel@tonic-gate  *
9327c478bd9Sstevel@tonic-gate  * to 32 bits of precision.  (The notation B-30 means "* 2^-30", just like
9337c478bd9Sstevel@tonic-gate  * E-18 means "* 10^-18".)
9347c478bd9Sstevel@tonic-gate  *
9357c478bd9Sstevel@tonic-gate  * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit
9367c478bd9Sstevel@tonic-gate  * integer 10001001011100000101111101000001, then normalize (shift) the
9377c478bd9Sstevel@tonic-gate  * result.  This constant has several large bits runs, so the multiply
9387c478bd9Sstevel@tonic-gate  * is relatively cheap:
9397c478bd9Sstevel@tonic-gate  *
9407c478bd9Sstevel@tonic-gate  *	10001001011100000101111101000001 = 10001001100000000110000001000001
9417c478bd9Sstevel@tonic-gate  *					 - 00000000000100000000000100000000
9427c478bd9Sstevel@tonic-gate  *
9437c478bd9Sstevel@tonic-gate  * Again, you can just read the algorithm right off the bits:
9447c478bd9Sstevel@tonic-gate  *
9457c478bd9Sstevel@tonic-gate  *			sec = hrt;
9467c478bd9Sstevel@tonic-gate  *			sec += (hrt << 6);
9477c478bd9Sstevel@tonic-gate  *			sec -= (hrt << 8);
9487c478bd9Sstevel@tonic-gate  *			sec += (hrt << 13);
9497c478bd9Sstevel@tonic-gate  *			sec += (hrt << 14);
9507c478bd9Sstevel@tonic-gate  *			sec -= (hrt << 20);
9517c478bd9Sstevel@tonic-gate  *			sec += (hrt << 23);
9527c478bd9Sstevel@tonic-gate  *			sec += (hrt << 24);
9537c478bd9Sstevel@tonic-gate  *			sec += (hrt << 27);
9547c478bd9Sstevel@tonic-gate  *			sec += (hrt << 31);
9557c478bd9Sstevel@tonic-gate  *			sec >>= (32 + 30);
9567c478bd9Sstevel@tonic-gate  *
9577c478bd9Sstevel@tonic-gate  * Voila!  The only problem is, since hrt is 64 bits, we need to use 96-bit
9587c478bd9Sstevel@tonic-gate  * arithmetic to perform this calculation.  That's a waste, because ultimately
9597c478bd9Sstevel@tonic-gate  * we only need the highest 32 bits of the result.
9607c478bd9Sstevel@tonic-gate  *
9617c478bd9Sstevel@tonic-gate  * The first thing we do is to realize that we don't need to use all of hrt
9627c478bd9Sstevel@tonic-gate  * in the calculation.  The lowest 30 bits can contribute at most 1 to the
9637c478bd9Sstevel@tonic-gate  * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later.
9647c478bd9Sstevel@tonic-gate  * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t.
9657c478bd9Sstevel@tonic-gate  * Thus, the only bits of hrt that matter for division are bits 30..61.
9667c478bd9Sstevel@tonic-gate  * These 32 bits are just the lower-order word of (hrt >> 30).  This brings
9677c478bd9Sstevel@tonic-gate  * us down from 96-bit math to 64-bit math, and our algorithm becomes:
9687c478bd9Sstevel@tonic-gate  *
9697c478bd9Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
9707c478bd9Sstevel@tonic-gate  *			sec = tmp;
9717c478bd9Sstevel@tonic-gate  *			sec += (tmp << 6);
9727c478bd9Sstevel@tonic-gate  *			sec -= (tmp << 8);
9737c478bd9Sstevel@tonic-gate  *			sec += (tmp << 13);
9747c478bd9Sstevel@tonic-gate  *			sec += (tmp << 14);
9757c478bd9Sstevel@tonic-gate  *			sec -= (tmp << 20);
9767c478bd9Sstevel@tonic-gate  *			sec += (tmp << 23);
9777c478bd9Sstevel@tonic-gate  *			sec += (tmp << 24);
9787c478bd9Sstevel@tonic-gate  *			sec += (tmp << 27);
9797c478bd9Sstevel@tonic-gate  *			sec += (tmp << 31);
9807c478bd9Sstevel@tonic-gate  *			sec >>= 32;
9817c478bd9Sstevel@tonic-gate  *
9827c478bd9Sstevel@tonic-gate  * Next, we're going to reduce this 64-bit computation to a 32-bit
9837c478bd9Sstevel@tonic-gate  * computation.  We begin by rewriting the above algorithm to use relative
9847c478bd9Sstevel@tonic-gate  * shifts instead of absolute shifts.  That is, instead of computing
9857c478bd9Sstevel@tonic-gate  * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally:
9867c478bd9Sstevel@tonic-gate  * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc:
9877c478bd9Sstevel@tonic-gate  *
9887c478bd9Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
9897c478bd9Sstevel@tonic-gate  *			sec = tmp;
9907c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec += tmp;
9917c478bd9Sstevel@tonic-gate  *			tmp <<= 2; sec -= tmp;
9927c478bd9Sstevel@tonic-gate  *			tmp <<= 5; sec += tmp;
9937c478bd9Sstevel@tonic-gate  *			tmp <<= 1; sec += tmp;
9947c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec -= tmp;
9957c478bd9Sstevel@tonic-gate  *			tmp <<= 3; sec += tmp;
9967c478bd9Sstevel@tonic-gate  *			tmp <<= 1; sec += tmp;
9977c478bd9Sstevel@tonic-gate  *			tmp <<= 3; sec += tmp;
9987c478bd9Sstevel@tonic-gate  *			tmp <<= 4; sec += tmp;
9997c478bd9Sstevel@tonic-gate  *			sec >>= 32;
10007c478bd9Sstevel@tonic-gate  *
10017c478bd9Sstevel@tonic-gate  * Now for the final step.  Instead of throwing away the low 32 bits at
10027c478bd9Sstevel@tonic-gate  * the end, we can throw them away as we go, only keeping the high 32 bits
10037c478bd9Sstevel@tonic-gate  * of the product at each step.  So, for example, where we now have
10047c478bd9Sstevel@tonic-gate  *
10057c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec = sec + tmp;
10067c478bd9Sstevel@tonic-gate  * we will instead have
10077c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec = (sec + tmp) >> 6;
10087c478bd9Sstevel@tonic-gate  * which is equivalent to
10097c478bd9Sstevel@tonic-gate  *			sec = (sec >> 6) + tmp;
10107c478bd9Sstevel@tonic-gate  *
10117c478bd9Sstevel@tonic-gate  * The final shift ("sec >>= 32") goes away.
10127c478bd9Sstevel@tonic-gate  *
10137c478bd9Sstevel@tonic-gate  * All we're really doing here is long multiplication, just like we learned in
10147c478bd9Sstevel@tonic-gate  * grade school, except that at each step, we only look at the leftmost 32
10157c478bd9Sstevel@tonic-gate  * columns.  The cumulative error is, at most, the sum of all the bits we
10167c478bd9Sstevel@tonic-gate  * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32.
10177c478bd9Sstevel@tonic-gate  * Thus, the final result ("sec") is correct to +/- 1.
10187c478bd9Sstevel@tonic-gate  *
10197c478bd9Sstevel@tonic-gate  * It turns out to be important to keep "sec" positive at each step, because
10207c478bd9Sstevel@tonic-gate  * we don't want to have to explicitly extend the sign bit.  Therefore,
10217c478bd9Sstevel@tonic-gate  * starting with the last line of code above, each line that would have read
10227c478bd9Sstevel@tonic-gate  * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and
10237c478bd9Sstevel@tonic-gate  * the operators (+ or -) in all previous lines must be toggled accordingly.
10247c478bd9Sstevel@tonic-gate  * Thus, we end up with:
10257c478bd9Sstevel@tonic-gate  *
10267c478bd9Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
10277c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 6);
10287c478bd9Sstevel@tonic-gate  *			sec = tmp - (tmp >> 2);
10297c478bd9Sstevel@tonic-gate  *			sec = tmp - (sec >> 5);
10307c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 1);
10317c478bd9Sstevel@tonic-gate  *			sec = tmp - (sec >> 6);
10327c478bd9Sstevel@tonic-gate  *			sec = tmp - (sec >> 3);
10337c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 1);
10347c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 3);
10357c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 4);
10367c478bd9Sstevel@tonic-gate  *
10377c478bd9Sstevel@tonic-gate  * This yields a value for sec that is accurate to +1/-1, so we have two
10387c478bd9Sstevel@tonic-gate  * cases to deal with.  The mysterious-looking "+ 7" in the code below biases
10397c478bd9Sstevel@tonic-gate  * the rounding toward zero, so that sec is always less than or equal to
10407c478bd9Sstevel@tonic-gate  * the correct value.  With this modified code, sec is accurate to +0/-2, with
10417c478bd9Sstevel@tonic-gate  * the -2 case being very rare in practice.  With this change, we only have to
10427c478bd9Sstevel@tonic-gate  * deal with one case (sec too small) in the cleanup code.
10437c478bd9Sstevel@tonic-gate  *
10447c478bd9Sstevel@tonic-gate  * The other modification we make is to delete the second line above
10457c478bd9Sstevel@tonic-gate  * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is
10467c478bd9Sstevel@tonic-gate  * set, and the cleanup code can handle that rare case.  This reduces the
10477c478bd9Sstevel@tonic-gate  * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases.
10487c478bd9Sstevel@tonic-gate  *
10497c478bd9Sstevel@tonic-gate  * Finally, we compute nsec = hrt - (sec * 1,000,000,000).  nsec will always
10507c478bd9Sstevel@tonic-gate  * be positive (since sec is never too large), and will at most be equal to
10517c478bd9Sstevel@tonic-gate  * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt.
10527c478bd9Sstevel@tonic-gate  * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can
10537c478bd9Sstevel@tonic-gate  * safely assume that nsec fits in 32 bits.  Consequently, when we compute
10547c478bd9Sstevel@tonic-gate  * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit
10557c478bd9Sstevel@tonic-gate  * arithmetic and let the high-order bits fall off the end.
10567c478bd9Sstevel@tonic-gate  *
10577c478bd9Sstevel@tonic-gate  * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop:
10587c478bd9Sstevel@tonic-gate  *
10597c478bd9Sstevel@tonic-gate  *			while (nsec >= NANOSEC) {
10607c478bd9Sstevel@tonic-gate  *				nsec -= NANOSEC;
10617c478bd9Sstevel@tonic-gate  *				sec++;
10627c478bd9Sstevel@tonic-gate  *			}
10637c478bd9Sstevel@tonic-gate  *
10647c478bd9Sstevel@tonic-gate  * is guaranteed to complete in at most 4 iterations.  In practice, the loop
10657c478bd9Sstevel@tonic-gate  * completes in 0 or 1 iteration over 95% of the time.
10667c478bd9Sstevel@tonic-gate  *
10677c478bd9Sstevel@tonic-gate  * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about
10687c478bd9Sstevel@tonic-gate  * 35 usec for software division -- about 20 times faster.
10697c478bd9Sstevel@tonic-gate  */
10707c478bd9Sstevel@tonic-gate void
10717c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	uint32_t sec, nsec, tmp;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	tmp = (uint32_t)(hrt >> 30);
10767c478bd9Sstevel@tonic-gate 	sec = tmp - (tmp >> 2);
10777c478bd9Sstevel@tonic-gate 	sec = tmp - (sec >> 5);
10787c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
10797c478bd9Sstevel@tonic-gate 	sec = tmp - (sec >> 6) + 7;
10807c478bd9Sstevel@tonic-gate 	sec = tmp - (sec >> 3);
10817c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
10827c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 3);
10837c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 4);
10847c478bd9Sstevel@tonic-gate 	tmp = (sec << 7) - sec - sec - sec;
10857c478bd9Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
10867c478bd9Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
10877c478bd9Sstevel@tonic-gate 	nsec = (uint32_t)hrt - (tmp << 9);
10887c478bd9Sstevel@tonic-gate 	while (nsec >= NANOSEC) {
10897c478bd9Sstevel@tonic-gate 		nsec -= NANOSEC;
10907c478bd9Sstevel@tonic-gate 		sec++;
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 	tsp->tv_sec = (time_t)sec;
10937c478bd9Sstevel@tonic-gate 	tsp->tv_nsec = nsec;
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate /*
10977c478bd9Sstevel@tonic-gate  * Convert from timestruc_t to hrtime_t.
10987c478bd9Sstevel@tonic-gate  *
10997c478bd9Sstevel@tonic-gate  * The code below is equivalent to:
11007c478bd9Sstevel@tonic-gate  *
11017c478bd9Sstevel@tonic-gate  *	hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec;
11027c478bd9Sstevel@tonic-gate  *
11037c478bd9Sstevel@tonic-gate  * but requires no integer multiply.
11047c478bd9Sstevel@tonic-gate  */
11057c478bd9Sstevel@tonic-gate hrtime_t
11067c478bd9Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	hrtime_t hrt;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	hrt = tsp->tv_sec;
11117c478bd9Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
11127c478bd9Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
11137c478bd9Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
11147c478bd9Sstevel@tonic-gate 	hrt = (hrt << 9) + tsp->tv_nsec;
11157c478bd9Sstevel@tonic-gate 	return (hrt);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate /*
11197c478bd9Sstevel@tonic-gate  * For the various 32-bit "compatibility" paths in the system.
11207c478bd9Sstevel@tonic-gate  */
11217c478bd9Sstevel@tonic-gate void
11227c478bd9Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	timestruc_t ts;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	hrt2ts(hrt, &ts);
11277c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(ts32p, &ts);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * If this ever becomes performance critical (ha!), we can borrow the
11327c478bd9Sstevel@tonic-gate  * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the
11337c478bd9Sstevel@tonic-gate  * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by
11347c478bd9Sstevel@tonic-gate  * 1,000.  For now, we'll opt for readability (besides, the compiler does
11357c478bd9Sstevel@tonic-gate  * a passable job of optimizing constant multiplication into shifts and adds).
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate hrtime_t
11387c478bd9Sstevel@tonic-gate tv2hrt(struct timeval *tvp)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	return ((hrtime_t)tvp->tv_sec * NANOSEC +
11417c478bd9Sstevel@tonic-gate 	    (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC));
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate void
1145be2140a8Sandyb hrt2tv(hrtime_t hrt, struct timeval *tvp)
11467c478bd9Sstevel@tonic-gate {
1147be2140a8Sandyb 	uint32_t sec, nsec, tmp;
1148be2140a8Sandyb 	uint32_t q, r, t;
1149be2140a8Sandyb 
1150be2140a8Sandyb 	tmp = (uint32_t)(hrt >> 30);
1151be2140a8Sandyb 	sec = tmp - (tmp >> 2);
1152be2140a8Sandyb 	sec = tmp - (sec >> 5);
1153be2140a8Sandyb 	sec = tmp + (sec >> 1);
1154be2140a8Sandyb 	sec = tmp - (sec >> 6) + 7;
1155be2140a8Sandyb 	sec = tmp - (sec >> 3);
1156be2140a8Sandyb 	sec = tmp + (sec >> 1);
1157be2140a8Sandyb 	sec = tmp + (sec >> 3);
1158be2140a8Sandyb 	sec = tmp + (sec >> 4);
1159be2140a8Sandyb 	tmp = (sec << 7) - sec - sec - sec;
1160be2140a8Sandyb 	tmp = (tmp << 7) - tmp - tmp - tmp;
1161be2140a8Sandyb 	tmp = (tmp << 7) - tmp - tmp - tmp;
1162be2140a8Sandyb 	nsec = (uint32_t)hrt - (tmp << 9);
1163be2140a8Sandyb 	while (nsec >= NANOSEC) {
1164be2140a8Sandyb 		nsec -= NANOSEC;
1165be2140a8Sandyb 		sec++;
1166be2140a8Sandyb 	}
1167be2140a8Sandyb 	tvp->tv_sec = (time_t)sec;
1168be2140a8Sandyb /*
1169be2140a8Sandyb  * this routine is very similar to hr2ts, but requires microseconds
1170be2140a8Sandyb  * instead of nanoseconds, so an interger divide by 1000 routine
1171be2140a8Sandyb  * completes the conversion
1172be2140a8Sandyb  */
1173be2140a8Sandyb 	t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12);
1174be2140a8Sandyb 	q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14);
1175be2140a8Sandyb 	q = q >> 9;
1176be2140a8Sandyb 	r = nsec - q*1000;
1177be2140a8Sandyb 	tvp->tv_usec = q + ((r + 24) >> 10);
1178be2140a8Sandyb 
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate int
11827c478bd9Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp)
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	timespec_t rqtime;
11857c478bd9Sstevel@tonic-gate 	timespec_t rmtime;
11867c478bd9Sstevel@tonic-gate 	timespec_t now;
1187*3348528fSdm120769 	int timecheck;
11887c478bd9Sstevel@tonic-gate 	int ret = 1;
11897c478bd9Sstevel@tonic-gate 	model_t datamodel = get_udatamodel();
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
11927c478bd9Sstevel@tonic-gate 		if (copyin(rqtp, &rqtime, sizeof (rqtime)))
11937c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
11947c478bd9Sstevel@tonic-gate 	} else {
11957c478bd9Sstevel@tonic-gate 		timespec32_t rqtime32;
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		if (copyin(rqtp, &rqtime32, sizeof (rqtime32)))
11987c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
11997c478bd9Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 ||
12037c478bd9Sstevel@tonic-gate 	    rqtime.tv_nsec >= NANOSEC)
12047c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	if (timerspecisset(&rqtime)) {
1207*3348528fSdm120769 		timecheck = timechanged;
12087c478bd9Sstevel@tonic-gate 		gethrestime(&now);
12097c478bd9Sstevel@tonic-gate 		timespecadd(&rqtime, &now);
12107c478bd9Sstevel@tonic-gate 		mutex_enter(&curthread->t_delay_lock);
12117c478bd9Sstevel@tonic-gate 		while ((ret = cv_waituntil_sig(&curthread->t_delay_cv,
1212*3348528fSdm120769 		    &curthread->t_delay_lock, &rqtime, timecheck)) > 0)
12137c478bd9Sstevel@tonic-gate 			continue;
12147c478bd9Sstevel@tonic-gate 		mutex_exit(&curthread->t_delay_lock);
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (rmtp) {
12187c478bd9Sstevel@tonic-gate 		/*
12197c478bd9Sstevel@tonic-gate 		 * If cv_waituntil_sig() returned due to a signal, and
12207c478bd9Sstevel@tonic-gate 		 * there is time remaining, then set the time remaining.
12217c478bd9Sstevel@tonic-gate 		 * Else set time remaining to zero
12227c478bd9Sstevel@tonic-gate 		 */
12237c478bd9Sstevel@tonic-gate 		rmtime.tv_sec = rmtime.tv_nsec = 0;
12247c478bd9Sstevel@tonic-gate 		if (ret == 0) {
1225b2a1c443Svb160487 			timespec_t delta = rqtime;
1226b2a1c443Svb160487 
12277c478bd9Sstevel@tonic-gate 			gethrestime(&now);
1228b2a1c443Svb160487 			timespecsub(&delta, &now);
1229b2a1c443Svb160487 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
1230b2a1c443Svb160487 			    delta.tv_nsec > 0))
1231b2a1c443Svb160487 				rmtime = delta;
12327c478bd9Sstevel@tonic-gate 		}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
12357c478bd9Sstevel@tonic-gate 			if (copyout(&rmtime, rmtp, sizeof (rmtime)))
12367c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
12377c478bd9Sstevel@tonic-gate 		} else {
12387c478bd9Sstevel@tonic-gate 			timespec32_t rmtime32;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 			TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
12417c478bd9Sstevel@tonic-gate 			if (copyout(&rmtime32, rmtp, sizeof (rmtime32)))
12427c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
12437c478bd9Sstevel@tonic-gate 		}
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	if (ret == 0)
12477c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
12487c478bd9Sstevel@tonic-gate 	return (0);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * Routines to convert standard UNIX time (seconds since Jan 1, 1970)
12537c478bd9Sstevel@tonic-gate  * into year/month/day/hour/minute/second format, and back again.
12547c478bd9Sstevel@tonic-gate  * Note: these routines require tod_lock held to protect cached state.
12557c478bd9Sstevel@tonic-gate  */
12567c478bd9Sstevel@tonic-gate static int days_thru_month[64] = {
12577c478bd9Sstevel@tonic-gate 	0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0,
12587c478bd9Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
12597c478bd9Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
12607c478bd9Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
12617c478bd9Sstevel@tonic-gate };
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate todinfo_t saved_tod;
12647c478bd9Sstevel@tonic-gate int saved_utc = -60;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate todinfo_t
12677c478bd9Sstevel@tonic-gate utc_to_tod(time_t utc)
12687c478bd9Sstevel@tonic-gate {
12697c478bd9Sstevel@tonic-gate 	long dse, day, month, year;
12707c478bd9Sstevel@tonic-gate 	todinfo_t tod;
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	if (utc < 0)			/* should never happen */
12757c478bd9Sstevel@tonic-gate 		utc = 0;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	saved_tod.tod_sec += utc - saved_utc;
12787c478bd9Sstevel@tonic-gate 	saved_utc = utc;
12797c478bd9Sstevel@tonic-gate 	if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60)
12807c478bd9Sstevel@tonic-gate 		return (saved_tod);	/* only the seconds changed */
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	dse = utc / 86400;		/* days since epoch */
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	tod.tod_sec = utc % 60;
12857c478bd9Sstevel@tonic-gate 	tod.tod_min = (utc % 3600) / 60;
12867c478bd9Sstevel@tonic-gate 	tod.tod_hour = (utc % 86400) / 3600;
12877c478bd9Sstevel@tonic-gate 	tod.tod_dow = (dse + 4) % 7 + 1;	/* epoch was a Thursday */
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	year = dse / 365 + 72;	/* first guess -- always a bit too large */
12907c478bd9Sstevel@tonic-gate 	do {
12917c478bd9Sstevel@tonic-gate 		year--;
12927c478bd9Sstevel@tonic-gate 		day = dse - 365 * (year - 70) - ((year - 69) >> 2);
12937c478bd9Sstevel@tonic-gate 	} while (day < 0);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	month = ((year & 3) << 4) + 1;
12967c478bd9Sstevel@tonic-gate 	while (day >= days_thru_month[month + 1])
12977c478bd9Sstevel@tonic-gate 		month++;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	tod.tod_day = day - days_thru_month[month] + 1;
13007c478bd9Sstevel@tonic-gate 	tod.tod_month = month & 15;
13017c478bd9Sstevel@tonic-gate 	tod.tod_year = year;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	saved_tod = tod;
13047c478bd9Sstevel@tonic-gate 	return (tod);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate time_t
13087c478bd9Sstevel@tonic-gate tod_to_utc(todinfo_t tod)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	time_t utc;
13117c478bd9Sstevel@tonic-gate 	int year = tod.tod_year;
13127c478bd9Sstevel@tonic-gate 	int month = tod.tod_month + ((year & 3) << 4);
13137c478bd9Sstevel@tonic-gate #ifdef DEBUG
13147c478bd9Sstevel@tonic-gate 	/* only warn once, not each time called */
13157c478bd9Sstevel@tonic-gate 	static int year_warn = 1;
13167c478bd9Sstevel@tonic-gate 	static int month_warn = 1;
13177c478bd9Sstevel@tonic-gate 	static int day_warn = 1;
13187c478bd9Sstevel@tonic-gate 	static int hour_warn = 1;
13197c478bd9Sstevel@tonic-gate 	static int min_warn = 1;
13207c478bd9Sstevel@tonic-gate 	static int sec_warn = 1;
13217c478bd9Sstevel@tonic-gate 	int days_diff = days_thru_month[month + 1] - days_thru_month[month];
13227c478bd9Sstevel@tonic-gate #endif
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate #ifdef DEBUG
13277c478bd9Sstevel@tonic-gate 	if (year_warn && (year < 70 || year > 8029)) {
13287c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
13297c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13307c478bd9Sstevel@tonic-gate 			"wrong years value %d -- time needs to be reset\n",
13317c478bd9Sstevel@tonic-gate 			year);
13327c478bd9Sstevel@tonic-gate 		year_warn = 0;
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) {
13367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
13377c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13387c478bd9Sstevel@tonic-gate 			"wrong months value %d -- time needs to be reset\n",
13397c478bd9Sstevel@tonic-gate 			tod.tod_month);
13407c478bd9Sstevel@tonic-gate 		month_warn = 0;
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) {
13447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
13457c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13467c478bd9Sstevel@tonic-gate 			"wrong days value %d -- time needs to be reset\n",
13477c478bd9Sstevel@tonic-gate 			tod.tod_day);
13487c478bd9Sstevel@tonic-gate 		day_warn = 0;
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) {
13527c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
13537c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13547c478bd9Sstevel@tonic-gate 			"wrong hours value %d -- time needs to be reset\n",
13557c478bd9Sstevel@tonic-gate 			tod.tod_hour);
13567c478bd9Sstevel@tonic-gate 		hour_warn = 0;
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) {
13607c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
13617c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13627c478bd9Sstevel@tonic-gate 			"wrong minutes value %d -- time needs to be reset\n",
13637c478bd9Sstevel@tonic-gate 			tod.tod_min);
13647c478bd9Sstevel@tonic-gate 		min_warn = 0;
13657c478bd9Sstevel@tonic-gate 	}
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) {
13687c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
13697c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
13707c478bd9Sstevel@tonic-gate 			"wrong seconds value %d -- time needs to be reset\n",
13717c478bd9Sstevel@tonic-gate 			tod.tod_sec);
13727c478bd9Sstevel@tonic-gate 		sec_warn = 0;
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate #endif
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	utc = (year - 70);		/* next 3 lines: utc = 365y + y/4 */
13777c478bd9Sstevel@tonic-gate 	utc += (utc << 3) + (utc << 6);
13787c478bd9Sstevel@tonic-gate 	utc += (utc << 2) + ((year - 69) >> 2);
13797c478bd9Sstevel@tonic-gate 	utc += days_thru_month[month] + tod.tod_day - 1;
13807c478bd9Sstevel@tonic-gate 	utc = (utc << 3) + (utc << 4) + tod.tod_hour;	/* 24 * day + hour */
13817c478bd9Sstevel@tonic-gate 	utc = (utc << 6) - (utc << 2) + tod.tod_min;	/* 60 * hour + min */
13827c478bd9Sstevel@tonic-gate 	utc = (utc << 6) - (utc << 2) + tod.tod_sec;	/* 60 * min + sec */
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	return (utc);
13857c478bd9Sstevel@tonic-gate }
1386