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