xref: /titanic_53/usr/src/uts/common/os/timers.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1982, 1986 Regents of the University of California.
10*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
11*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
12*7c478bd9Sstevel@tonic-gate  */
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
15*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
16*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
17*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
18*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
19*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
20*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
21*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
22*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
23*7c478bd9Sstevel@tonic-gate #include <sys/timer.h>
24*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
25*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
26*7c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate static void	realitexpire(void *);
29*7c478bd9Sstevel@tonic-gate static void	realprofexpire(void *);
30*7c478bd9Sstevel@tonic-gate static void	timeval_advance(struct timeval *, struct timeval *);
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate kmutex_t tod_lock;	/* protects time-of-day stuff */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * Constant to define the minimum interval value of the ITIMER_REALPROF timer.
36*7c478bd9Sstevel@tonic-gate  * Value is in microseconds; defaults to 500 usecs.  Setting this value
37*7c478bd9Sstevel@tonic-gate  * significantly lower may allow for denial-of-service attacks.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate int itimer_realprof_minimum = 500;
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * macro to compare a timeval to a timestruc
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	TVTSCMP(tvp, tsp, cmp) \
46*7c478bd9Sstevel@tonic-gate 	/* CSTYLED */ \
47*7c478bd9Sstevel@tonic-gate 	((tvp)->tv_sec cmp (tsp)->tv_sec || \
48*7c478bd9Sstevel@tonic-gate 	((tvp)->tv_sec == (tsp)->tv_sec && \
49*7c478bd9Sstevel@tonic-gate 	/* CSTYLED */ \
50*7c478bd9Sstevel@tonic-gate 	(tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec))
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Time of day and interval timer support.
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  * These routines provide the kernel entry points to get and set
56*7c478bd9Sstevel@tonic-gate  * the time-of-day and per-process interval timers.  Subroutines
57*7c478bd9Sstevel@tonic-gate  * here provide support for adding and subtracting timeval structures
58*7c478bd9Sstevel@tonic-gate  * and decrementing interval timers, optionally reloading the interval
59*7c478bd9Sstevel@tonic-gate  * timers when they expire.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * SunOS function to generate monotonically increasing time values.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate void
66*7c478bd9Sstevel@tonic-gate uniqtime(struct timeval *tv)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	static struct timeval last;
69*7c478bd9Sstevel@tonic-gate 	timestruc_t ts;
70*7c478bd9Sstevel@tonic-gate 	time_t sec;
71*7c478bd9Sstevel@tonic-gate 	int usec, nsec;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	 * protect modification of last
75*7c478bd9Sstevel@tonic-gate 	 */
76*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
77*7c478bd9Sstevel@tonic-gate 	gethrestime(&ts);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	/*
80*7c478bd9Sstevel@tonic-gate 	 * Fast algorithm to convert nsec to usec -- see hrt2ts()
81*7c478bd9Sstevel@tonic-gate 	 * in common/os/timers.c for a full description.
82*7c478bd9Sstevel@tonic-gate 	 */
83*7c478bd9Sstevel@tonic-gate 	nsec = ts.tv_nsec;
84*7c478bd9Sstevel@tonic-gate 	usec = nsec + (nsec >> 2);
85*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 1);
86*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 2);
87*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 4);
88*7c478bd9Sstevel@tonic-gate 	usec = nsec - (usec >> 3);
89*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 2);
90*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 3);
91*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 4);
92*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 1);
93*7c478bd9Sstevel@tonic-gate 	usec = nsec + (usec >> 6);
94*7c478bd9Sstevel@tonic-gate 	usec = usec >> 10;
95*7c478bd9Sstevel@tonic-gate 	sec = ts.tv_sec;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	/*
98*7c478bd9Sstevel@tonic-gate 	 * Try to keep timestamps unique, but don't be obsessive about
99*7c478bd9Sstevel@tonic-gate 	 * it in the face of large differences.
100*7c478bd9Sstevel@tonic-gate 	 */
101*7c478bd9Sstevel@tonic-gate 	if ((sec <= last.tv_sec) &&		/* same or lower seconds, and */
102*7c478bd9Sstevel@tonic-gate 	    ((sec != last.tv_sec) ||		/* either different second or */
103*7c478bd9Sstevel@tonic-gate 	    (usec <= last.tv_usec)) &&		/* lower microsecond, and */
104*7c478bd9Sstevel@tonic-gate 	    ((last.tv_sec - sec) <= 5)) {	/* not way back in time */
105*7c478bd9Sstevel@tonic-gate 		sec = last.tv_sec;
106*7c478bd9Sstevel@tonic-gate 		usec = last.tv_usec + 1;
107*7c478bd9Sstevel@tonic-gate 		if (usec >= MICROSEC) {
108*7c478bd9Sstevel@tonic-gate 			usec -= MICROSEC;
109*7c478bd9Sstevel@tonic-gate 			sec++;
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	last.tv_sec = sec;
113*7c478bd9Sstevel@tonic-gate 	last.tv_usec = usec;
114*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	tv->tv_sec = sec;
117*7c478bd9Sstevel@tonic-gate 	tv->tv_usec = usec;
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * Timestamps are exported from the kernel in several places.
122*7c478bd9Sstevel@tonic-gate  * Such timestamps are commonly used for either uniqueness or for
123*7c478bd9Sstevel@tonic-gate  * sequencing - truncation to 32-bits is fine for uniqueness,
124*7c478bd9Sstevel@tonic-gate  * but sequencing is going to take more work as we get closer to 2038!
125*7c478bd9Sstevel@tonic-gate  */
126*7c478bd9Sstevel@tonic-gate void
127*7c478bd9Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	struct timeval tv;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	uniqtime(&tv);
132*7c478bd9Sstevel@tonic-gate 	TIMEVAL_TO_TIMEVAL32(tv32p, &tv);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate int
136*7c478bd9Sstevel@tonic-gate gettimeofday(struct timeval *tp)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	struct timeval atv;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if (tp) {
141*7c478bd9Sstevel@tonic-gate 		uniqtime(&atv);
142*7c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
143*7c478bd9Sstevel@tonic-gate 			if (copyout(&atv, tp, sizeof (atv)))
144*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
145*7c478bd9Sstevel@tonic-gate 		} else {
146*7c478bd9Sstevel@tonic-gate 			struct timeval32 tv32;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 			if (TIMEVAL_OVERFLOW(&atv))
149*7c478bd9Sstevel@tonic-gate 				return (set_errno(EOVERFLOW));
150*7c478bd9Sstevel@tonic-gate 			TIMEVAL_TO_TIMEVAL32(&tv32, &atv);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 			if (copyout(&tv32, tp, sizeof (tv32)))
153*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
154*7c478bd9Sstevel@tonic-gate 		}
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	return (0);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate int
160*7c478bd9Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv)
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	int error;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE)
165*7c478bd9Sstevel@tonic-gate 		error = xgetitimer(which, itv, 0);
166*7c478bd9Sstevel@tonic-gate 	else {
167*7c478bd9Sstevel@tonic-gate 		struct itimerval kitv;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		if ((error = xgetitimer(which, &kitv, 1)) == 0) {
170*7c478bd9Sstevel@tonic-gate 			if (ITIMERVAL_OVERFLOW(&kitv)) {
171*7c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
172*7c478bd9Sstevel@tonic-gate 			} else {
173*7c478bd9Sstevel@tonic-gate 				struct itimerval32 itv32;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 				ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv);
176*7c478bd9Sstevel@tonic-gate 				if (copyout(&itv32, itv, sizeof (itv32)) != 0)
177*7c478bd9Sstevel@tonic-gate 					error = EFAULT;
178*7c478bd9Sstevel@tonic-gate 			}
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate int
186*7c478bd9Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
189*7c478bd9Sstevel@tonic-gate 	struct timeval now;
190*7c478bd9Sstevel@tonic-gate 	struct itimerval aitv;
191*7c478bd9Sstevel@tonic-gate 	hrtime_t ts, first, interval, remain;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	switch (which) {
196*7c478bd9Sstevel@tonic-gate 	case ITIMER_VIRTUAL:
197*7c478bd9Sstevel@tonic-gate 	case ITIMER_PROF:
198*7c478bd9Sstevel@tonic-gate 		aitv = ttolwp(curthread)->lwp_timer[which];
199*7c478bd9Sstevel@tonic-gate 		break;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	case ITIMER_REAL:
202*7c478bd9Sstevel@tonic-gate 		uniqtime(&now);
203*7c478bd9Sstevel@tonic-gate 		aitv = p->p_realitimer;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		if (timerisset(&aitv.it_value)) {
206*7c478bd9Sstevel@tonic-gate 			/*CSTYLED*/
207*7c478bd9Sstevel@tonic-gate 			if (timercmp(&aitv.it_value, &now, <)) {
208*7c478bd9Sstevel@tonic-gate 				timerclear(&aitv.it_value);
209*7c478bd9Sstevel@tonic-gate 			} else {
210*7c478bd9Sstevel@tonic-gate 				timevalsub(&aitv.it_value, &now);
211*7c478bd9Sstevel@tonic-gate 			}
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 		break;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	case ITIMER_REALPROF:
216*7c478bd9Sstevel@tonic-gate 		if (curproc->p_rprof_cyclic == CYCLIC_NONE) {
217*7c478bd9Sstevel@tonic-gate 			bzero(&aitv, sizeof (aitv));
218*7c478bd9Sstevel@tonic-gate 			break;
219*7c478bd9Sstevel@tonic-gate 		}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		aitv = curproc->p_rprof_timer;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		first = tv2hrt(&aitv.it_value);
224*7c478bd9Sstevel@tonic-gate 		interval = tv2hrt(&aitv.it_interval);
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		if ((ts = gethrtime()) < first) {
227*7c478bd9Sstevel@tonic-gate 			/*
228*7c478bd9Sstevel@tonic-gate 			 * We haven't gone off for the first time; the time
229*7c478bd9Sstevel@tonic-gate 			 * remaining is simply the first time we will go
230*7c478bd9Sstevel@tonic-gate 			 * off minus the current time.
231*7c478bd9Sstevel@tonic-gate 			 */
232*7c478bd9Sstevel@tonic-gate 			remain = first - ts;
233*7c478bd9Sstevel@tonic-gate 		} else {
234*7c478bd9Sstevel@tonic-gate 			if (interval == 0) {
235*7c478bd9Sstevel@tonic-gate 				/*
236*7c478bd9Sstevel@tonic-gate 				 * This was set as a one-shot, and we've
237*7c478bd9Sstevel@tonic-gate 				 * already gone off; there is no time
238*7c478bd9Sstevel@tonic-gate 				 * remaining.
239*7c478bd9Sstevel@tonic-gate 				 */
240*7c478bd9Sstevel@tonic-gate 				remain = 0;
241*7c478bd9Sstevel@tonic-gate 			} else {
242*7c478bd9Sstevel@tonic-gate 				/*
243*7c478bd9Sstevel@tonic-gate 				 * We have a non-zero interval; we need to
244*7c478bd9Sstevel@tonic-gate 				 * determine how far we are into the current
245*7c478bd9Sstevel@tonic-gate 				 * interval, and subtract that from the
246*7c478bd9Sstevel@tonic-gate 				 * interval to determine the time remaining.
247*7c478bd9Sstevel@tonic-gate 				 */
248*7c478bd9Sstevel@tonic-gate 				remain = interval - ((ts - first) % interval);
249*7c478bd9Sstevel@tonic-gate 			}
250*7c478bd9Sstevel@tonic-gate 		}
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 		hrt2tv(remain, &aitv.it_value);
253*7c478bd9Sstevel@tonic-gate 		break;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	default:
256*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
257*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (iskaddr) {
263*7c478bd9Sstevel@tonic-gate 		bcopy(&aitv, itv, sizeof (*itv));
264*7c478bd9Sstevel@tonic-gate 	} else {
265*7c478bd9Sstevel@tonic-gate 		ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
266*7c478bd9Sstevel@tonic-gate 		if (copyout(&aitv, itv, sizeof (*itv)))
267*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	return (0);
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate int
275*7c478bd9Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv)
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	int error;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	if (oitv != NULL)
280*7c478bd9Sstevel@tonic-gate 		if ((error = getitimer(which, oitv)) != 0)
281*7c478bd9Sstevel@tonic-gate 			return (error);
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	if (itv == NULL)
284*7c478bd9Sstevel@tonic-gate 		return (0);
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE)
287*7c478bd9Sstevel@tonic-gate 		error = xsetitimer(which, itv, 0);
288*7c478bd9Sstevel@tonic-gate 	else {
289*7c478bd9Sstevel@tonic-gate 		struct itimerval32 itv32;
290*7c478bd9Sstevel@tonic-gate 		struct itimerval kitv;
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 		if (copyin(itv, &itv32, sizeof (itv32)))
293*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
294*7c478bd9Sstevel@tonic-gate 		ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32);
295*7c478bd9Sstevel@tonic-gate 		error = xsetitimer(which, &kitv, 1);
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate int
302*7c478bd9Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr)
303*7c478bd9Sstevel@tonic-gate {
304*7c478bd9Sstevel@tonic-gate 	struct itimerval aitv;
305*7c478bd9Sstevel@tonic-gate 	struct timeval now;
306*7c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
307*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
308*7c478bd9Sstevel@tonic-gate 	timeout_id_t tmp_id;
309*7c478bd9Sstevel@tonic-gate 	cyc_handler_t hdlr;
310*7c478bd9Sstevel@tonic-gate 	cyc_time_t when;
311*7c478bd9Sstevel@tonic-gate 	cyclic_id_t cyclic;
312*7c478bd9Sstevel@tonic-gate 	hrtime_t ts;
313*7c478bd9Sstevel@tonic-gate 	int min;
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	if (itv == NULL)
316*7c478bd9Sstevel@tonic-gate 		return (0);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	if (iskaddr) {
319*7c478bd9Sstevel@tonic-gate 		bcopy(itv, &aitv, sizeof (aitv));
320*7c478bd9Sstevel@tonic-gate 	} else {
321*7c478bd9Sstevel@tonic-gate 		ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
322*7c478bd9Sstevel@tonic-gate 		if (copyin(itv, &aitv, sizeof (aitv)))
323*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	if (which == ITIMER_REALPROF) {
327*7c478bd9Sstevel@tonic-gate 		min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)),
328*7c478bd9Sstevel@tonic-gate 		    itimer_realprof_minimum);
329*7c478bd9Sstevel@tonic-gate 	} else {
330*7c478bd9Sstevel@tonic-gate 		min = usec_per_tick;
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	if (itimerfix(&aitv.it_value, min) ||
334*7c478bd9Sstevel@tonic-gate 	    (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value)))
335*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
338*7c478bd9Sstevel@tonic-gate 	switch (which) {
339*7c478bd9Sstevel@tonic-gate 	case ITIMER_REAL:
340*7c478bd9Sstevel@tonic-gate 		/*
341*7c478bd9Sstevel@tonic-gate 		 * The SITBUSY flag prevents conflicts with multiple
342*7c478bd9Sstevel@tonic-gate 		 * threads attempting to perform setitimer(ITIMER_REAL)
343*7c478bd9Sstevel@tonic-gate 		 * at the same time, even when we drop p->p_lock below.
344*7c478bd9Sstevel@tonic-gate 		 * Any blocked thread returns successfully because the
345*7c478bd9Sstevel@tonic-gate 		 * effect is the same as if it got here first, finished,
346*7c478bd9Sstevel@tonic-gate 		 * and the other thread then came through and destroyed
347*7c478bd9Sstevel@tonic-gate 		 * what it did.  We are just protecting the system from
348*7c478bd9Sstevel@tonic-gate 		 * malfunctioning due to the race condition.
349*7c478bd9Sstevel@tonic-gate 		 */
350*7c478bd9Sstevel@tonic-gate 		if (p->p_flag & SITBUSY) {
351*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
352*7c478bd9Sstevel@tonic-gate 			return (0);
353*7c478bd9Sstevel@tonic-gate 		}
354*7c478bd9Sstevel@tonic-gate 		p->p_flag |= SITBUSY;
355*7c478bd9Sstevel@tonic-gate 		while ((tmp_id = p->p_itimerid) != 0) {
356*7c478bd9Sstevel@tonic-gate 			/*
357*7c478bd9Sstevel@tonic-gate 			 * Avoid deadlock in callout_delete (called from
358*7c478bd9Sstevel@tonic-gate 			 * untimeout) which may go to sleep (while holding
359*7c478bd9Sstevel@tonic-gate 			 * p_lock). Drop p_lock and re-acquire it after
360*7c478bd9Sstevel@tonic-gate 			 * untimeout returns. Need to clear p_itimerid
361*7c478bd9Sstevel@tonic-gate 			 * while holding p_lock.
362*7c478bd9Sstevel@tonic-gate 			 */
363*7c478bd9Sstevel@tonic-gate 			p->p_itimerid = 0;
364*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
365*7c478bd9Sstevel@tonic-gate 			(void) untimeout(tmp_id);
366*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
367*7c478bd9Sstevel@tonic-gate 		}
368*7c478bd9Sstevel@tonic-gate 		if (timerisset(&aitv.it_value)) {
369*7c478bd9Sstevel@tonic-gate 			uniqtime(&now);
370*7c478bd9Sstevel@tonic-gate 			timevaladd(&aitv.it_value, &now);
371*7c478bd9Sstevel@tonic-gate 			p->p_itimerid = realtime_timeout(realitexpire,
372*7c478bd9Sstevel@tonic-gate 			    p, hzto(&aitv.it_value));
373*7c478bd9Sstevel@tonic-gate 		}
374*7c478bd9Sstevel@tonic-gate 		p->p_realitimer = aitv;
375*7c478bd9Sstevel@tonic-gate 		p->p_flag &= ~SITBUSY;
376*7c478bd9Sstevel@tonic-gate 		break;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	case ITIMER_REALPROF:
379*7c478bd9Sstevel@tonic-gate 		cyclic = p->p_rprof_cyclic;
380*7c478bd9Sstevel@tonic-gate 		p->p_rprof_cyclic = CYCLIC_NONE;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 		/*
385*7c478bd9Sstevel@tonic-gate 		 * We're now going to acquire cpu_lock, remove the old cyclic
386*7c478bd9Sstevel@tonic-gate 		 * if necessary, and add our new cyclic.
387*7c478bd9Sstevel@tonic-gate 		 */
388*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 		if (cyclic != CYCLIC_NONE)
391*7c478bd9Sstevel@tonic-gate 			cyclic_remove(cyclic);
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 		if (!timerisset(&aitv.it_value)) {
394*7c478bd9Sstevel@tonic-gate 			/*
395*7c478bd9Sstevel@tonic-gate 			 * If we were passed a value of 0, we're done.
396*7c478bd9Sstevel@tonic-gate 			 */
397*7c478bd9Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
398*7c478bd9Sstevel@tonic-gate 			return (0);
399*7c478bd9Sstevel@tonic-gate 		}
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 		hdlr.cyh_func = realprofexpire;
402*7c478bd9Sstevel@tonic-gate 		hdlr.cyh_arg = p;
403*7c478bd9Sstevel@tonic-gate 		hdlr.cyh_level = CY_LOW_LEVEL;
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 		when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value));
406*7c478bd9Sstevel@tonic-gate 		when.cyt_interval = tv2hrt(&aitv.it_interval);
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 		if (when.cyt_interval == 0) {
409*7c478bd9Sstevel@tonic-gate 			/*
410*7c478bd9Sstevel@tonic-gate 			 * Using the same logic as for CLOCK_HIGHRES timers, we
411*7c478bd9Sstevel@tonic-gate 			 * set the interval to be INT64_MAX - when.cyt_when to
412*7c478bd9Sstevel@tonic-gate 			 * effect a one-shot; see the comment in clock_highres.c
413*7c478bd9Sstevel@tonic-gate 			 * for more details on why this works.
414*7c478bd9Sstevel@tonic-gate 			 */
415*7c478bd9Sstevel@tonic-gate 			when.cyt_interval = INT64_MAX - when.cyt_when;
416*7c478bd9Sstevel@tonic-gate 		}
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 		cyclic = cyclic_add(&hdlr, &when);
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 		/*
423*7c478bd9Sstevel@tonic-gate 		 * We have now successfully added the cyclic.  Reacquire
424*7c478bd9Sstevel@tonic-gate 		 * p_lock, and see if anyone has snuck in.
425*7c478bd9Sstevel@tonic-gate 		 */
426*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 		if (p->p_rprof_cyclic != CYCLIC_NONE) {
429*7c478bd9Sstevel@tonic-gate 			/*
430*7c478bd9Sstevel@tonic-gate 			 * We're racing with another thread establishing an
431*7c478bd9Sstevel@tonic-gate 			 * ITIMER_REALPROF interval timer.  We'll let the other
432*7c478bd9Sstevel@tonic-gate 			 * thread win (this is a race at the application level,
433*7c478bd9Sstevel@tonic-gate 			 * so letting the other thread win is acceptable).
434*7c478bd9Sstevel@tonic-gate 			 */
435*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
436*7c478bd9Sstevel@tonic-gate 			mutex_enter(&cpu_lock);
437*7c478bd9Sstevel@tonic-gate 			cyclic_remove(cyclic);
438*7c478bd9Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 			return (0);
441*7c478bd9Sstevel@tonic-gate 		}
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 		/*
444*7c478bd9Sstevel@tonic-gate 		 * Success.  Set our tracking variables in the proc structure,
445*7c478bd9Sstevel@tonic-gate 		 * cancel any outstanding ITIMER_PROF, and allocate the
446*7c478bd9Sstevel@tonic-gate 		 * per-thread SIGPROF buffers, if possible.
447*7c478bd9Sstevel@tonic-gate 		 */
448*7c478bd9Sstevel@tonic-gate 		hrt2tv(ts, &aitv.it_value);
449*7c478bd9Sstevel@tonic-gate 		p->p_rprof_timer = aitv;
450*7c478bd9Sstevel@tonic-gate 		p->p_rprof_cyclic = cyclic;
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 		t = p->p_tlist;
453*7c478bd9Sstevel@tonic-gate 		do {
454*7c478bd9Sstevel@tonic-gate 			struct itimerval *itvp;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 			itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF];
457*7c478bd9Sstevel@tonic-gate 			timerclear(&itvp->it_interval);
458*7c478bd9Sstevel@tonic-gate 			timerclear(&itvp->it_value);
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 			if (t->t_rprof != NULL)
461*7c478bd9Sstevel@tonic-gate 				continue;
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 			t->t_rprof =
464*7c478bd9Sstevel@tonic-gate 			    kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP);
465*7c478bd9Sstevel@tonic-gate 			aston(t);
466*7c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 		break;
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	case ITIMER_VIRTUAL:
471*7c478bd9Sstevel@tonic-gate 		ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv;
472*7c478bd9Sstevel@tonic-gate 		break;
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	case ITIMER_PROF:
475*7c478bd9Sstevel@tonic-gate 		if (p->p_rprof_cyclic != CYCLIC_NONE) {
476*7c478bd9Sstevel@tonic-gate 			/*
477*7c478bd9Sstevel@tonic-gate 			 * Silently ignore ITIMER_PROF if ITIMER_REALPROF
478*7c478bd9Sstevel@tonic-gate 			 * is in effect.
479*7c478bd9Sstevel@tonic-gate 			 */
480*7c478bd9Sstevel@tonic-gate 			break;
481*7c478bd9Sstevel@tonic-gate 		}
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 		ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv;
484*7c478bd9Sstevel@tonic-gate 		break;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	default:
487*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
488*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
491*7c478bd9Sstevel@tonic-gate 	return (0);
492*7c478bd9Sstevel@tonic-gate }
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate /*
495*7c478bd9Sstevel@tonic-gate  * Real interval timer expired:
496*7c478bd9Sstevel@tonic-gate  * send process whose timer expired an alarm signal.
497*7c478bd9Sstevel@tonic-gate  * If time is not set up to reload, then just return.
498*7c478bd9Sstevel@tonic-gate  * Else compute next time timer should go off which is > current time.
499*7c478bd9Sstevel@tonic-gate  * This is where delay in processing this timeout causes multiple
500*7c478bd9Sstevel@tonic-gate  * SIGALRM calls to be compressed into one.
501*7c478bd9Sstevel@tonic-gate  */
502*7c478bd9Sstevel@tonic-gate static void
503*7c478bd9Sstevel@tonic-gate realitexpire(void *arg)
504*7c478bd9Sstevel@tonic-gate {
505*7c478bd9Sstevel@tonic-gate 	struct proc *p = arg;
506*7c478bd9Sstevel@tonic-gate 	struct timeval *valp = &p->p_realitimer.it_value;
507*7c478bd9Sstevel@tonic-gate 	struct timeval *intervalp = &p->p_realitimer.it_interval;
508*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
509*7c478bd9Sstevel@tonic-gate 	clock_t	ticks;
510*7c478bd9Sstevel@tonic-gate #endif
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
513*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
514*7c478bd9Sstevel@tonic-gate 	if ((ticks = hzto(valp)) > 1) {
515*7c478bd9Sstevel@tonic-gate 		/*
516*7c478bd9Sstevel@tonic-gate 		 * If we are executing before we were meant to, it must be
517*7c478bd9Sstevel@tonic-gate 		 * because of an overflow in a prior hzto() calculation.
518*7c478bd9Sstevel@tonic-gate 		 * In this case, we want to go to sleep for the recalculated
519*7c478bd9Sstevel@tonic-gate 		 * number of ticks. For the special meaning of the value "1"
520*7c478bd9Sstevel@tonic-gate 		 * see comment in timespectohz().
521*7c478bd9Sstevel@tonic-gate 		 */
522*7c478bd9Sstevel@tonic-gate 		p->p_itimerid = realtime_timeout(realitexpire, p, ticks);
523*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
524*7c478bd9Sstevel@tonic-gate 		return;
525*7c478bd9Sstevel@tonic-gate 	}
526*7c478bd9Sstevel@tonic-gate #endif
527*7c478bd9Sstevel@tonic-gate 	sigtoproc(p, NULL, SIGALRM);
528*7c478bd9Sstevel@tonic-gate 	if (!timerisset(intervalp)) {
529*7c478bd9Sstevel@tonic-gate 		timerclear(valp);
530*7c478bd9Sstevel@tonic-gate 		p->p_itimerid = 0;
531*7c478bd9Sstevel@tonic-gate 	} else {
532*7c478bd9Sstevel@tonic-gate 		/* advance timer value past current time */
533*7c478bd9Sstevel@tonic-gate 		timeval_advance(valp, intervalp);
534*7c478bd9Sstevel@tonic-gate 		p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp));
535*7c478bd9Sstevel@tonic-gate 	}
536*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
537*7c478bd9Sstevel@tonic-gate }
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate /*
540*7c478bd9Sstevel@tonic-gate  * Real time profiling interval timer expired:
541*7c478bd9Sstevel@tonic-gate  * Increment microstate counters for each lwp in the process
542*7c478bd9Sstevel@tonic-gate  * and ensure that running lwps are kicked into the kernel.
543*7c478bd9Sstevel@tonic-gate  * If time is not set up to reload, then just return.
544*7c478bd9Sstevel@tonic-gate  * Else compute next time timer should go off which is > current time,
545*7c478bd9Sstevel@tonic-gate  * as above.
546*7c478bd9Sstevel@tonic-gate  */
547*7c478bd9Sstevel@tonic-gate static void
548*7c478bd9Sstevel@tonic-gate realprofexpire(void *arg)
549*7c478bd9Sstevel@tonic-gate {
550*7c478bd9Sstevel@tonic-gate 	struct proc *p = arg;
551*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
554*7c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) == NULL) {
555*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
556*7c478bd9Sstevel@tonic-gate 		return;
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 	do {
559*7c478bd9Sstevel@tonic-gate 		int mstate;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 		/*
562*7c478bd9Sstevel@tonic-gate 		 * Attempt to allocate the SIGPROF buffer, but don't sleep.
563*7c478bd9Sstevel@tonic-gate 		 */
564*7c478bd9Sstevel@tonic-gate 		if (t->t_rprof == NULL)
565*7c478bd9Sstevel@tonic-gate 			t->t_rprof = kmem_zalloc(sizeof (struct rprof),
566*7c478bd9Sstevel@tonic-gate 			    KM_NOSLEEP);
567*7c478bd9Sstevel@tonic-gate 		if (t->t_rprof == NULL)
568*7c478bd9Sstevel@tonic-gate 			continue;
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 		thread_lock(t);
571*7c478bd9Sstevel@tonic-gate 		switch (t->t_state) {
572*7c478bd9Sstevel@tonic-gate 		case TS_SLEEP:
573*7c478bd9Sstevel@tonic-gate 			/*
574*7c478bd9Sstevel@tonic-gate 			 * Don't touch the lwp is it is swapped out.
575*7c478bd9Sstevel@tonic-gate 			 */
576*7c478bd9Sstevel@tonic-gate 			if (!(t->t_schedflag & TS_LOAD)) {
577*7c478bd9Sstevel@tonic-gate 				mstate = LMS_SLEEP;
578*7c478bd9Sstevel@tonic-gate 				break;
579*7c478bd9Sstevel@tonic-gate 			}
580*7c478bd9Sstevel@tonic-gate 			switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) {
581*7c478bd9Sstevel@tonic-gate 			case LMS_TFAULT:
582*7c478bd9Sstevel@tonic-gate 			case LMS_DFAULT:
583*7c478bd9Sstevel@tonic-gate 			case LMS_KFAULT:
584*7c478bd9Sstevel@tonic-gate 			case LMS_USER_LOCK:
585*7c478bd9Sstevel@tonic-gate 				break;
586*7c478bd9Sstevel@tonic-gate 			default:
587*7c478bd9Sstevel@tonic-gate 				mstate = LMS_SLEEP;
588*7c478bd9Sstevel@tonic-gate 				break;
589*7c478bd9Sstevel@tonic-gate 			}
590*7c478bd9Sstevel@tonic-gate 			break;
591*7c478bd9Sstevel@tonic-gate 		case TS_RUN:
592*7c478bd9Sstevel@tonic-gate 			mstate = LMS_WAIT_CPU;
593*7c478bd9Sstevel@tonic-gate 			break;
594*7c478bd9Sstevel@tonic-gate 		case TS_ONPROC:
595*7c478bd9Sstevel@tonic-gate 			switch (mstate = t->t_mstate) {
596*7c478bd9Sstevel@tonic-gate 			case LMS_USER:
597*7c478bd9Sstevel@tonic-gate 			case LMS_SYSTEM:
598*7c478bd9Sstevel@tonic-gate 			case LMS_TRAP:
599*7c478bd9Sstevel@tonic-gate 				break;
600*7c478bd9Sstevel@tonic-gate 			default:
601*7c478bd9Sstevel@tonic-gate 				mstate = LMS_SYSTEM;
602*7c478bd9Sstevel@tonic-gate 				break;
603*7c478bd9Sstevel@tonic-gate 			}
604*7c478bd9Sstevel@tonic-gate 			break;
605*7c478bd9Sstevel@tonic-gate 		default:
606*7c478bd9Sstevel@tonic-gate 			mstate = t->t_mstate;
607*7c478bd9Sstevel@tonic-gate 			break;
608*7c478bd9Sstevel@tonic-gate 		}
609*7c478bd9Sstevel@tonic-gate 		t->t_rprof->rp_anystate = 1;
610*7c478bd9Sstevel@tonic-gate 		t->t_rprof->rp_state[mstate]++;
611*7c478bd9Sstevel@tonic-gate 		aston(t);
612*7c478bd9Sstevel@tonic-gate 		/*
613*7c478bd9Sstevel@tonic-gate 		 * force the thread into the kernel
614*7c478bd9Sstevel@tonic-gate 		 * if it is not already there.
615*7c478bd9Sstevel@tonic-gate 		 */
616*7c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
617*7c478bd9Sstevel@tonic-gate 			poke_cpu(t->t_cpu->cpu_id);
618*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
619*7c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
622*7c478bd9Sstevel@tonic-gate }
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate /*
625*7c478bd9Sstevel@tonic-gate  * Advances timer value past the current time of day.  See the detailed
626*7c478bd9Sstevel@tonic-gate  * comment for this logic in realitsexpire(), above.
627*7c478bd9Sstevel@tonic-gate  */
628*7c478bd9Sstevel@tonic-gate static void
629*7c478bd9Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp)
630*7c478bd9Sstevel@tonic-gate {
631*7c478bd9Sstevel@tonic-gate 	int cnt2nth;
632*7c478bd9Sstevel@tonic-gate 	struct timeval interval2nth;
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 	for (;;) {
635*7c478bd9Sstevel@tonic-gate 		interval2nth = *intervalp;
636*7c478bd9Sstevel@tonic-gate 		for (cnt2nth = 0; ; cnt2nth++) {
637*7c478bd9Sstevel@tonic-gate 			timevaladd(valp, &interval2nth);
638*7c478bd9Sstevel@tonic-gate 			/*CSTYLED*/
639*7c478bd9Sstevel@tonic-gate 			if (TVTSCMP(valp, &hrestime, >))
640*7c478bd9Sstevel@tonic-gate 				break;
641*7c478bd9Sstevel@tonic-gate 			timevaladd(&interval2nth, &interval2nth);
642*7c478bd9Sstevel@tonic-gate 		}
643*7c478bd9Sstevel@tonic-gate 		if (cnt2nth == 0)
644*7c478bd9Sstevel@tonic-gate 			break;
645*7c478bd9Sstevel@tonic-gate 		timevalsub(valp, &interval2nth);
646*7c478bd9Sstevel@tonic-gate 	}
647*7c478bd9Sstevel@tonic-gate }
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate /*
650*7c478bd9Sstevel@tonic-gate  * Check that a proposed value to load into the .it_value or .it_interval
651*7c478bd9Sstevel@tonic-gate  * part of an interval timer is acceptable, and set it to at least a
652*7c478bd9Sstevel@tonic-gate  * specified minimal value.
653*7c478bd9Sstevel@tonic-gate  */
654*7c478bd9Sstevel@tonic-gate int
655*7c478bd9Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum)
656*7c478bd9Sstevel@tonic-gate {
657*7c478bd9Sstevel@tonic-gate 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
658*7c478bd9Sstevel@tonic-gate 	    tv->tv_usec < 0 || tv->tv_usec >= MICROSEC)
659*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
660*7c478bd9Sstevel@tonic-gate 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum)
661*7c478bd9Sstevel@tonic-gate 		tv->tv_usec = minimum;
662*7c478bd9Sstevel@tonic-gate 	return (0);
663*7c478bd9Sstevel@tonic-gate }
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate /*
666*7c478bd9Sstevel@tonic-gate  * Same as itimerfix, except a) it takes a timespec instead of a timeval and
667*7c478bd9Sstevel@tonic-gate  * b) it doesn't truncate based on timeout granularity; consumers of this
668*7c478bd9Sstevel@tonic-gate  * interface (e.g. timer_settime()) depend on the passed timespec not being
669*7c478bd9Sstevel@tonic-gate  * modified implicitly.
670*7c478bd9Sstevel@tonic-gate  */
671*7c478bd9Sstevel@tonic-gate int
672*7c478bd9Sstevel@tonic-gate itimerspecfix(timespec_t *tv)
673*7c478bd9Sstevel@tonic-gate {
674*7c478bd9Sstevel@tonic-gate 	if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC)
675*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
676*7c478bd9Sstevel@tonic-gate 	return (0);
677*7c478bd9Sstevel@tonic-gate }
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate /*
680*7c478bd9Sstevel@tonic-gate  * Decrement an interval timer by a specified number
681*7c478bd9Sstevel@tonic-gate  * of microseconds, which must be less than a second,
682*7c478bd9Sstevel@tonic-gate  * i.e. < 1000000.  If the timer expires, then reload
683*7c478bd9Sstevel@tonic-gate  * it.  In this case, carry over (usec - old value) to
684*7c478bd9Sstevel@tonic-gate  * reducint the value reloaded into the timer so that
685*7c478bd9Sstevel@tonic-gate  * the timer does not drift.  This routine assumes
686*7c478bd9Sstevel@tonic-gate  * that it is called in a context where the timers
687*7c478bd9Sstevel@tonic-gate  * on which it is operating cannot change in value.
688*7c478bd9Sstevel@tonic-gate  */
689*7c478bd9Sstevel@tonic-gate int
690*7c478bd9Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec)
691*7c478bd9Sstevel@tonic-gate {
692*7c478bd9Sstevel@tonic-gate 	if (itp->it_value.tv_usec < usec) {
693*7c478bd9Sstevel@tonic-gate 		if (itp->it_value.tv_sec == 0) {
694*7c478bd9Sstevel@tonic-gate 			/* expired, and already in next interval */
695*7c478bd9Sstevel@tonic-gate 			usec -= itp->it_value.tv_usec;
696*7c478bd9Sstevel@tonic-gate 			goto expire;
697*7c478bd9Sstevel@tonic-gate 		}
698*7c478bd9Sstevel@tonic-gate 		itp->it_value.tv_usec += MICROSEC;
699*7c478bd9Sstevel@tonic-gate 		itp->it_value.tv_sec--;
700*7c478bd9Sstevel@tonic-gate 	}
701*7c478bd9Sstevel@tonic-gate 	itp->it_value.tv_usec -= usec;
702*7c478bd9Sstevel@tonic-gate 	usec = 0;
703*7c478bd9Sstevel@tonic-gate 	if (timerisset(&itp->it_value))
704*7c478bd9Sstevel@tonic-gate 		return (1);
705*7c478bd9Sstevel@tonic-gate 	/* expired, exactly at end of interval */
706*7c478bd9Sstevel@tonic-gate expire:
707*7c478bd9Sstevel@tonic-gate 	if (timerisset(&itp->it_interval)) {
708*7c478bd9Sstevel@tonic-gate 		itp->it_value = itp->it_interval;
709*7c478bd9Sstevel@tonic-gate 		itp->it_value.tv_usec -= usec;
710*7c478bd9Sstevel@tonic-gate 		if (itp->it_value.tv_usec < 0) {
711*7c478bd9Sstevel@tonic-gate 			itp->it_value.tv_usec += MICROSEC;
712*7c478bd9Sstevel@tonic-gate 			itp->it_value.tv_sec--;
713*7c478bd9Sstevel@tonic-gate 		}
714*7c478bd9Sstevel@tonic-gate 	} else
715*7c478bd9Sstevel@tonic-gate 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
716*7c478bd9Sstevel@tonic-gate 	return (0);
717*7c478bd9Sstevel@tonic-gate }
718*7c478bd9Sstevel@tonic-gate 
719*7c478bd9Sstevel@tonic-gate /*
720*7c478bd9Sstevel@tonic-gate  * Add and subtract routines for timevals.
721*7c478bd9Sstevel@tonic-gate  * N.B.: subtract routine doesn't deal with
722*7c478bd9Sstevel@tonic-gate  * results which are before the beginning,
723*7c478bd9Sstevel@tonic-gate  * it just gets very confused in this case.
724*7c478bd9Sstevel@tonic-gate  * Caveat emptor.
725*7c478bd9Sstevel@tonic-gate  */
726*7c478bd9Sstevel@tonic-gate void
727*7c478bd9Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2)
728*7c478bd9Sstevel@tonic-gate {
729*7c478bd9Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
730*7c478bd9Sstevel@tonic-gate 	t1->tv_usec += t2->tv_usec;
731*7c478bd9Sstevel@tonic-gate 	timevalfix(t1);
732*7c478bd9Sstevel@tonic-gate }
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate void
735*7c478bd9Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2)
736*7c478bd9Sstevel@tonic-gate {
737*7c478bd9Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
738*7c478bd9Sstevel@tonic-gate 	t1->tv_usec -= t2->tv_usec;
739*7c478bd9Sstevel@tonic-gate 	timevalfix(t1);
740*7c478bd9Sstevel@tonic-gate }
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate void
743*7c478bd9Sstevel@tonic-gate timevalfix(struct timeval *t1)
744*7c478bd9Sstevel@tonic-gate {
745*7c478bd9Sstevel@tonic-gate 	if (t1->tv_usec < 0) {
746*7c478bd9Sstevel@tonic-gate 		t1->tv_sec--;
747*7c478bd9Sstevel@tonic-gate 		t1->tv_usec += MICROSEC;
748*7c478bd9Sstevel@tonic-gate 	}
749*7c478bd9Sstevel@tonic-gate 	if (t1->tv_usec >= MICROSEC) {
750*7c478bd9Sstevel@tonic-gate 		t1->tv_sec++;
751*7c478bd9Sstevel@tonic-gate 		t1->tv_usec -= MICROSEC;
752*7c478bd9Sstevel@tonic-gate 	}
753*7c478bd9Sstevel@tonic-gate }
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate /*
756*7c478bd9Sstevel@tonic-gate  * Same as the routines above. These routines take a timespec instead
757*7c478bd9Sstevel@tonic-gate  * of a timeval.
758*7c478bd9Sstevel@tonic-gate  */
759*7c478bd9Sstevel@tonic-gate void
760*7c478bd9Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2)
761*7c478bd9Sstevel@tonic-gate {
762*7c478bd9Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
763*7c478bd9Sstevel@tonic-gate 	t1->tv_nsec += t2->tv_nsec;
764*7c478bd9Sstevel@tonic-gate 	timespecfix(t1);
765*7c478bd9Sstevel@tonic-gate }
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate void
768*7c478bd9Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2)
769*7c478bd9Sstevel@tonic-gate {
770*7c478bd9Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
771*7c478bd9Sstevel@tonic-gate 	t1->tv_nsec -= t2->tv_nsec;
772*7c478bd9Sstevel@tonic-gate 	timespecfix(t1);
773*7c478bd9Sstevel@tonic-gate }
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate void
776*7c478bd9Sstevel@tonic-gate timespecfix(timespec_t *t1)
777*7c478bd9Sstevel@tonic-gate {
778*7c478bd9Sstevel@tonic-gate 	if (t1->tv_nsec < 0) {
779*7c478bd9Sstevel@tonic-gate 		t1->tv_sec--;
780*7c478bd9Sstevel@tonic-gate 		t1->tv_nsec += NANOSEC;
781*7c478bd9Sstevel@tonic-gate 	} else {
782*7c478bd9Sstevel@tonic-gate 		if (t1->tv_nsec >= NANOSEC) {
783*7c478bd9Sstevel@tonic-gate 			t1->tv_sec++;
784*7c478bd9Sstevel@tonic-gate 			t1->tv_nsec -= NANOSEC;
785*7c478bd9Sstevel@tonic-gate 		}
786*7c478bd9Sstevel@tonic-gate 	}
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate /*
790*7c478bd9Sstevel@tonic-gate  * Compute number of hz until specified time.
791*7c478bd9Sstevel@tonic-gate  * Used to compute third argument to timeout() from an absolute time.
792*7c478bd9Sstevel@tonic-gate  */
793*7c478bd9Sstevel@tonic-gate clock_t
794*7c478bd9Sstevel@tonic-gate hzto(struct timeval *tv)
795*7c478bd9Sstevel@tonic-gate {
796*7c478bd9Sstevel@tonic-gate 	timespec_t ts, now;
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 	ts.tv_sec = tv->tv_sec;
799*7c478bd9Sstevel@tonic-gate 	ts.tv_nsec = tv->tv_usec * 1000;
800*7c478bd9Sstevel@tonic-gate 	gethrestime_lasttick(&now);
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 	return (timespectohz(&ts, now));
803*7c478bd9Sstevel@tonic-gate }
804*7c478bd9Sstevel@tonic-gate 
805*7c478bd9Sstevel@tonic-gate /*
806*7c478bd9Sstevel@tonic-gate  * Compute number of hz until specified time for a given timespec value.
807*7c478bd9Sstevel@tonic-gate  * Used to compute third argument to timeout() from an absolute time.
808*7c478bd9Sstevel@tonic-gate  */
809*7c478bd9Sstevel@tonic-gate clock_t
810*7c478bd9Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now)
811*7c478bd9Sstevel@tonic-gate {
812*7c478bd9Sstevel@tonic-gate 	clock_t	ticks;
813*7c478bd9Sstevel@tonic-gate 	time_t	sec;
814*7c478bd9Sstevel@tonic-gate 	int	nsec;
815*7c478bd9Sstevel@tonic-gate 
816*7c478bd9Sstevel@tonic-gate 	/*
817*7c478bd9Sstevel@tonic-gate 	 * Compute number of ticks we will see between now and
818*7c478bd9Sstevel@tonic-gate 	 * the target time; returns "1" if the destination time
819*7c478bd9Sstevel@tonic-gate 	 * is before the next tick, so we always get some delay,
820*7c478bd9Sstevel@tonic-gate 	 * and returns LONG_MAX ticks if we would overflow.
821*7c478bd9Sstevel@tonic-gate 	 */
822*7c478bd9Sstevel@tonic-gate 	sec = tv->tv_sec - now.tv_sec;
823*7c478bd9Sstevel@tonic-gate 	nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1;
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate 	if (nsec < 0) {
826*7c478bd9Sstevel@tonic-gate 		sec--;
827*7c478bd9Sstevel@tonic-gate 		nsec += NANOSEC;
828*7c478bd9Sstevel@tonic-gate 	} else if (nsec >= NANOSEC) {
829*7c478bd9Sstevel@tonic-gate 		sec++;
830*7c478bd9Sstevel@tonic-gate 		nsec -= NANOSEC;
831*7c478bd9Sstevel@tonic-gate 	}
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate 	ticks = NSEC_TO_TICK(nsec);
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 	/*
836*7c478bd9Sstevel@tonic-gate 	 * Compute ticks, accounting for negative and overflow as above.
837*7c478bd9Sstevel@tonic-gate 	 * Overflow protection kicks in at about 70 weeks for hz=50
838*7c478bd9Sstevel@tonic-gate 	 * and at about 35 weeks for hz=100. (Rather longer for the 64-bit
839*7c478bd9Sstevel@tonic-gate 	 * kernel :-)
840*7c478bd9Sstevel@tonic-gate 	 */
841*7c478bd9Sstevel@tonic-gate 	if (sec < 0 || (sec == 0 && ticks < 1))
842*7c478bd9Sstevel@tonic-gate 		ticks = 1;			/* protect vs nonpositive */
843*7c478bd9Sstevel@tonic-gate 	else if (sec > (LONG_MAX - ticks) / hz)
844*7c478bd9Sstevel@tonic-gate 		ticks = LONG_MAX;		/* protect vs overflow */
845*7c478bd9Sstevel@tonic-gate 	else
846*7c478bd9Sstevel@tonic-gate 		ticks += sec * hz;		/* common case */
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	return (ticks);
849*7c478bd9Sstevel@tonic-gate }
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate /*
852*7c478bd9Sstevel@tonic-gate  * Same as timespectohz() except that we adjust the clock ticks down a bit.
853*7c478bd9Sstevel@tonic-gate  * If we will be waiting for a long time, we may encounter skewing problems
854*7c478bd9Sstevel@tonic-gate  * due to adjtime() system calls.  Since we can skew up to 1/16 lbolt rate
855*7c478bd9Sstevel@tonic-gate  * if adjtime is going crazy, we reduce the time delta since timeout() takes
856*7c478bd9Sstevel@tonic-gate  * clock ticks rather than wallclock elapsed time.  This may cause the caller
857*7c478bd9Sstevel@tonic-gate  * (who calls timeout()) to return with a timeout prematurely and callers
858*7c478bd9Sstevel@tonic-gate  * must accommodate this.  See lwp_timeout(), queue_lwptimer() and
859*7c478bd9Sstevel@tonic-gate  * cv_waituntil_sig(), currently the only callers of this function.
860*7c478bd9Sstevel@tonic-gate  */
861*7c478bd9Sstevel@tonic-gate clock_t
862*7c478bd9Sstevel@tonic-gate timespectohz_adj(timespec_t *tv, timespec_t now)
863*7c478bd9Sstevel@tonic-gate {
864*7c478bd9Sstevel@tonic-gate 	timespec_t wait_time = *tv;
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate 	timespecsub(&wait_time, &now);
867*7c478bd9Sstevel@tonic-gate 	wait_time.tv_sec -= wait_time.tv_sec >> 4;
868*7c478bd9Sstevel@tonic-gate 	wait_time.tv_nsec -= wait_time.tv_nsec >> 4;
869*7c478bd9Sstevel@tonic-gate 	timespecadd(&wait_time, &now);
870*7c478bd9Sstevel@tonic-gate 	return (timespectohz(&wait_time, now));
871*7c478bd9Sstevel@tonic-gate }
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate /*
874*7c478bd9Sstevel@tonic-gate  * hrt2ts(): convert from hrtime_t to timestruc_t.
875*7c478bd9Sstevel@tonic-gate  *
876*7c478bd9Sstevel@tonic-gate  * All this routine really does is:
877*7c478bd9Sstevel@tonic-gate  *
878*7c478bd9Sstevel@tonic-gate  *	tsp->sec  = hrt / NANOSEC;
879*7c478bd9Sstevel@tonic-gate  *	tsp->nsec = hrt % NANOSEC;
880*7c478bd9Sstevel@tonic-gate  *
881*7c478bd9Sstevel@tonic-gate  * The black magic below avoids doing a 64-bit by 32-bit integer divide,
882*7c478bd9Sstevel@tonic-gate  * which is quite expensive.  There's actually much more going on here than
883*7c478bd9Sstevel@tonic-gate  * it might first appear -- don't try this at home.
884*7c478bd9Sstevel@tonic-gate  *
885*7c478bd9Sstevel@tonic-gate  * For the adventuresome, here's an explanation of how it works.
886*7c478bd9Sstevel@tonic-gate  *
887*7c478bd9Sstevel@tonic-gate  * Multiplication by a fixed constant is easy -- you just do the appropriate
888*7c478bd9Sstevel@tonic-gate  * shifts and adds.  For example, to multiply by 10, we observe that
889*7c478bd9Sstevel@tonic-gate  *
890*7c478bd9Sstevel@tonic-gate  *	x * 10	= x * (8 + 2)
891*7c478bd9Sstevel@tonic-gate  *		= (x * 8) + (x * 2)
892*7c478bd9Sstevel@tonic-gate  *		= (x << 3) + (x << 1).
893*7c478bd9Sstevel@tonic-gate  *
894*7c478bd9Sstevel@tonic-gate  * In general, you can read the algorithm right off the bits: the number 10
895*7c478bd9Sstevel@tonic-gate  * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3).
896*7c478bd9Sstevel@tonic-gate  *
897*7c478bd9Sstevel@tonic-gate  * Sometimes you can do better.  For example, 15 is 1111 binary, so the normal
898*7c478bd9Sstevel@tonic-gate  * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3).
899*7c478bd9Sstevel@tonic-gate  * But, it's cheaper if you capitalize on the fact that you have a run of ones:
900*7c478bd9Sstevel@tonic-gate  * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0).  [You would never
901*7c478bd9Sstevel@tonic-gate  * actually perform the operation << 0, since it's a no-op; I'm just writing
902*7c478bd9Sstevel@tonic-gate  * it that way for clarity.]
903*7c478bd9Sstevel@tonic-gate  *
904*7c478bd9Sstevel@tonic-gate  * The other way you can win is if you get lucky with the prime factorization
905*7c478bd9Sstevel@tonic-gate  * of your constant.  The number 1,000,000,000, which we have to multiply
906*7c478bd9Sstevel@tonic-gate  * by below, is a good example.  One billion is 111011100110101100101000000000
907*7c478bd9Sstevel@tonic-gate  * in binary.  If you apply the bit-grouping trick, it doesn't buy you very
908*7c478bd9Sstevel@tonic-gate  * much, because it's only a win for groups of three or more equal bits:
909*7c478bd9Sstevel@tonic-gate  *
910*7c478bd9Sstevel@tonic-gate  * 111011100110101100101000000000 = 1000000000000000000000000000000
911*7c478bd9Sstevel@tonic-gate  *				  -  000100011001010011011000000000
912*7c478bd9Sstevel@tonic-gate  *
913*7c478bd9Sstevel@tonic-gate  * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS,
914*7c478bd9Sstevel@tonic-gate  * we have reduced this to 10 shift/add pairs (20 operations) on the RHS.
915*7c478bd9Sstevel@tonic-gate  * This is better, but not great.
916*7c478bd9Sstevel@tonic-gate  *
917*7c478bd9Sstevel@tonic-gate  * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125,
918*7c478bd9Sstevel@tonic-gate  * and multiply by each factor.  Multiplication by 125 is particularly easy,
919*7c478bd9Sstevel@tonic-gate  * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four
920*7c478bd9Sstevel@tonic-gate  * operations.  So, to multiply by 1,000,000,000, we perform three multipli-
921*7c478bd9Sstevel@tonic-gate  * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations.
922*7c478bd9Sstevel@tonic-gate  * This is the algorithm we actually use in both hrt2ts() and ts2hrt().
923*7c478bd9Sstevel@tonic-gate  *
924*7c478bd9Sstevel@tonic-gate  * Division is harder; there is no equivalent of the simple shift-add algorithm
925*7c478bd9Sstevel@tonic-gate  * we used for multiplication.  However, we can convert the division problem
926*7c478bd9Sstevel@tonic-gate  * into a multiplication problem by pre-computing the binary representation
927*7c478bd9Sstevel@tonic-gate  * of the reciprocal of the divisor.  For the case of interest, we have
928*7c478bd9Sstevel@tonic-gate  *
929*7c478bd9Sstevel@tonic-gate  *	1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30,
930*7c478bd9Sstevel@tonic-gate  *
931*7c478bd9Sstevel@tonic-gate  * to 32 bits of precision.  (The notation B-30 means "* 2^-30", just like
932*7c478bd9Sstevel@tonic-gate  * E-18 means "* 10^-18".)
933*7c478bd9Sstevel@tonic-gate  *
934*7c478bd9Sstevel@tonic-gate  * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit
935*7c478bd9Sstevel@tonic-gate  * integer 10001001011100000101111101000001, then normalize (shift) the
936*7c478bd9Sstevel@tonic-gate  * result.  This constant has several large bits runs, so the multiply
937*7c478bd9Sstevel@tonic-gate  * is relatively cheap:
938*7c478bd9Sstevel@tonic-gate  *
939*7c478bd9Sstevel@tonic-gate  *	10001001011100000101111101000001 = 10001001100000000110000001000001
940*7c478bd9Sstevel@tonic-gate  *					 - 00000000000100000000000100000000
941*7c478bd9Sstevel@tonic-gate  *
942*7c478bd9Sstevel@tonic-gate  * Again, you can just read the algorithm right off the bits:
943*7c478bd9Sstevel@tonic-gate  *
944*7c478bd9Sstevel@tonic-gate  *			sec = hrt;
945*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 6);
946*7c478bd9Sstevel@tonic-gate  *			sec -= (hrt << 8);
947*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 13);
948*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 14);
949*7c478bd9Sstevel@tonic-gate  *			sec -= (hrt << 20);
950*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 23);
951*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 24);
952*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 27);
953*7c478bd9Sstevel@tonic-gate  *			sec += (hrt << 31);
954*7c478bd9Sstevel@tonic-gate  *			sec >>= (32 + 30);
955*7c478bd9Sstevel@tonic-gate  *
956*7c478bd9Sstevel@tonic-gate  * Voila!  The only problem is, since hrt is 64 bits, we need to use 96-bit
957*7c478bd9Sstevel@tonic-gate  * arithmetic to perform this calculation.  That's a waste, because ultimately
958*7c478bd9Sstevel@tonic-gate  * we only need the highest 32 bits of the result.
959*7c478bd9Sstevel@tonic-gate  *
960*7c478bd9Sstevel@tonic-gate  * The first thing we do is to realize that we don't need to use all of hrt
961*7c478bd9Sstevel@tonic-gate  * in the calculation.  The lowest 30 bits can contribute at most 1 to the
962*7c478bd9Sstevel@tonic-gate  * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later.
963*7c478bd9Sstevel@tonic-gate  * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t.
964*7c478bd9Sstevel@tonic-gate  * Thus, the only bits of hrt that matter for division are bits 30..61.
965*7c478bd9Sstevel@tonic-gate  * These 32 bits are just the lower-order word of (hrt >> 30).  This brings
966*7c478bd9Sstevel@tonic-gate  * us down from 96-bit math to 64-bit math, and our algorithm becomes:
967*7c478bd9Sstevel@tonic-gate  *
968*7c478bd9Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
969*7c478bd9Sstevel@tonic-gate  *			sec = tmp;
970*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 6);
971*7c478bd9Sstevel@tonic-gate  *			sec -= (tmp << 8);
972*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 13);
973*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 14);
974*7c478bd9Sstevel@tonic-gate  *			sec -= (tmp << 20);
975*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 23);
976*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 24);
977*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 27);
978*7c478bd9Sstevel@tonic-gate  *			sec += (tmp << 31);
979*7c478bd9Sstevel@tonic-gate  *			sec >>= 32;
980*7c478bd9Sstevel@tonic-gate  *
981*7c478bd9Sstevel@tonic-gate  * Next, we're going to reduce this 64-bit computation to a 32-bit
982*7c478bd9Sstevel@tonic-gate  * computation.  We begin by rewriting the above algorithm to use relative
983*7c478bd9Sstevel@tonic-gate  * shifts instead of absolute shifts.  That is, instead of computing
984*7c478bd9Sstevel@tonic-gate  * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally:
985*7c478bd9Sstevel@tonic-gate  * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc:
986*7c478bd9Sstevel@tonic-gate  *
987*7c478bd9Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
988*7c478bd9Sstevel@tonic-gate  *			sec = tmp;
989*7c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec += tmp;
990*7c478bd9Sstevel@tonic-gate  *			tmp <<= 2; sec -= tmp;
991*7c478bd9Sstevel@tonic-gate  *			tmp <<= 5; sec += tmp;
992*7c478bd9Sstevel@tonic-gate  *			tmp <<= 1; sec += tmp;
993*7c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec -= tmp;
994*7c478bd9Sstevel@tonic-gate  *			tmp <<= 3; sec += tmp;
995*7c478bd9Sstevel@tonic-gate  *			tmp <<= 1; sec += tmp;
996*7c478bd9Sstevel@tonic-gate  *			tmp <<= 3; sec += tmp;
997*7c478bd9Sstevel@tonic-gate  *			tmp <<= 4; sec += tmp;
998*7c478bd9Sstevel@tonic-gate  *			sec >>= 32;
999*7c478bd9Sstevel@tonic-gate  *
1000*7c478bd9Sstevel@tonic-gate  * Now for the final step.  Instead of throwing away the low 32 bits at
1001*7c478bd9Sstevel@tonic-gate  * the end, we can throw them away as we go, only keeping the high 32 bits
1002*7c478bd9Sstevel@tonic-gate  * of the product at each step.  So, for example, where we now have
1003*7c478bd9Sstevel@tonic-gate  *
1004*7c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec = sec + tmp;
1005*7c478bd9Sstevel@tonic-gate  * we will instead have
1006*7c478bd9Sstevel@tonic-gate  *			tmp <<= 6; sec = (sec + tmp) >> 6;
1007*7c478bd9Sstevel@tonic-gate  * which is equivalent to
1008*7c478bd9Sstevel@tonic-gate  *			sec = (sec >> 6) + tmp;
1009*7c478bd9Sstevel@tonic-gate  *
1010*7c478bd9Sstevel@tonic-gate  * The final shift ("sec >>= 32") goes away.
1011*7c478bd9Sstevel@tonic-gate  *
1012*7c478bd9Sstevel@tonic-gate  * All we're really doing here is long multiplication, just like we learned in
1013*7c478bd9Sstevel@tonic-gate  * grade school, except that at each step, we only look at the leftmost 32
1014*7c478bd9Sstevel@tonic-gate  * columns.  The cumulative error is, at most, the sum of all the bits we
1015*7c478bd9Sstevel@tonic-gate  * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32.
1016*7c478bd9Sstevel@tonic-gate  * Thus, the final result ("sec") is correct to +/- 1.
1017*7c478bd9Sstevel@tonic-gate  *
1018*7c478bd9Sstevel@tonic-gate  * It turns out to be important to keep "sec" positive at each step, because
1019*7c478bd9Sstevel@tonic-gate  * we don't want to have to explicitly extend the sign bit.  Therefore,
1020*7c478bd9Sstevel@tonic-gate  * starting with the last line of code above, each line that would have read
1021*7c478bd9Sstevel@tonic-gate  * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and
1022*7c478bd9Sstevel@tonic-gate  * the operators (+ or -) in all previous lines must be toggled accordingly.
1023*7c478bd9Sstevel@tonic-gate  * Thus, we end up with:
1024*7c478bd9Sstevel@tonic-gate  *
1025*7c478bd9Sstevel@tonic-gate  *			tmp = (uint32_t) (hrt >> 30);
1026*7c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 6);
1027*7c478bd9Sstevel@tonic-gate  *			sec = tmp - (tmp >> 2);
1028*7c478bd9Sstevel@tonic-gate  *			sec = tmp - (sec >> 5);
1029*7c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 1);
1030*7c478bd9Sstevel@tonic-gate  *			sec = tmp - (sec >> 6);
1031*7c478bd9Sstevel@tonic-gate  *			sec = tmp - (sec >> 3);
1032*7c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 1);
1033*7c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 3);
1034*7c478bd9Sstevel@tonic-gate  *			sec = tmp + (sec >> 4);
1035*7c478bd9Sstevel@tonic-gate  *
1036*7c478bd9Sstevel@tonic-gate  * This yields a value for sec that is accurate to +1/-1, so we have two
1037*7c478bd9Sstevel@tonic-gate  * cases to deal with.  The mysterious-looking "+ 7" in the code below biases
1038*7c478bd9Sstevel@tonic-gate  * the rounding toward zero, so that sec is always less than or equal to
1039*7c478bd9Sstevel@tonic-gate  * the correct value.  With this modified code, sec is accurate to +0/-2, with
1040*7c478bd9Sstevel@tonic-gate  * the -2 case being very rare in practice.  With this change, we only have to
1041*7c478bd9Sstevel@tonic-gate  * deal with one case (sec too small) in the cleanup code.
1042*7c478bd9Sstevel@tonic-gate  *
1043*7c478bd9Sstevel@tonic-gate  * The other modification we make is to delete the second line above
1044*7c478bd9Sstevel@tonic-gate  * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is
1045*7c478bd9Sstevel@tonic-gate  * set, and the cleanup code can handle that rare case.  This reduces the
1046*7c478bd9Sstevel@tonic-gate  * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases.
1047*7c478bd9Sstevel@tonic-gate  *
1048*7c478bd9Sstevel@tonic-gate  * Finally, we compute nsec = hrt - (sec * 1,000,000,000).  nsec will always
1049*7c478bd9Sstevel@tonic-gate  * be positive (since sec is never too large), and will at most be equal to
1050*7c478bd9Sstevel@tonic-gate  * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt.
1051*7c478bd9Sstevel@tonic-gate  * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can
1052*7c478bd9Sstevel@tonic-gate  * safely assume that nsec fits in 32 bits.  Consequently, when we compute
1053*7c478bd9Sstevel@tonic-gate  * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit
1054*7c478bd9Sstevel@tonic-gate  * arithmetic and let the high-order bits fall off the end.
1055*7c478bd9Sstevel@tonic-gate  *
1056*7c478bd9Sstevel@tonic-gate  * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop:
1057*7c478bd9Sstevel@tonic-gate  *
1058*7c478bd9Sstevel@tonic-gate  *			while (nsec >= NANOSEC) {
1059*7c478bd9Sstevel@tonic-gate  *				nsec -= NANOSEC;
1060*7c478bd9Sstevel@tonic-gate  *				sec++;
1061*7c478bd9Sstevel@tonic-gate  *			}
1062*7c478bd9Sstevel@tonic-gate  *
1063*7c478bd9Sstevel@tonic-gate  * is guaranteed to complete in at most 4 iterations.  In practice, the loop
1064*7c478bd9Sstevel@tonic-gate  * completes in 0 or 1 iteration over 95% of the time.
1065*7c478bd9Sstevel@tonic-gate  *
1066*7c478bd9Sstevel@tonic-gate  * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about
1067*7c478bd9Sstevel@tonic-gate  * 35 usec for software division -- about 20 times faster.
1068*7c478bd9Sstevel@tonic-gate  */
1069*7c478bd9Sstevel@tonic-gate void
1070*7c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
1071*7c478bd9Sstevel@tonic-gate {
1072*7c478bd9Sstevel@tonic-gate 	uint32_t sec, nsec, tmp;
1073*7c478bd9Sstevel@tonic-gate 
1074*7c478bd9Sstevel@tonic-gate 	tmp = (uint32_t)(hrt >> 30);
1075*7c478bd9Sstevel@tonic-gate 	sec = tmp - (tmp >> 2);
1076*7c478bd9Sstevel@tonic-gate 	sec = tmp - (sec >> 5);
1077*7c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
1078*7c478bd9Sstevel@tonic-gate 	sec = tmp - (sec >> 6) + 7;
1079*7c478bd9Sstevel@tonic-gate 	sec = tmp - (sec >> 3);
1080*7c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 1);
1081*7c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 3);
1082*7c478bd9Sstevel@tonic-gate 	sec = tmp + (sec >> 4);
1083*7c478bd9Sstevel@tonic-gate 	tmp = (sec << 7) - sec - sec - sec;
1084*7c478bd9Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
1085*7c478bd9Sstevel@tonic-gate 	tmp = (tmp << 7) - tmp - tmp - tmp;
1086*7c478bd9Sstevel@tonic-gate 	nsec = (uint32_t)hrt - (tmp << 9);
1087*7c478bd9Sstevel@tonic-gate 	while (nsec >= NANOSEC) {
1088*7c478bd9Sstevel@tonic-gate 		nsec -= NANOSEC;
1089*7c478bd9Sstevel@tonic-gate 		sec++;
1090*7c478bd9Sstevel@tonic-gate 	}
1091*7c478bd9Sstevel@tonic-gate 	tsp->tv_sec = (time_t)sec;
1092*7c478bd9Sstevel@tonic-gate 	tsp->tv_nsec = nsec;
1093*7c478bd9Sstevel@tonic-gate }
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate /*
1096*7c478bd9Sstevel@tonic-gate  * Convert from timestruc_t to hrtime_t.
1097*7c478bd9Sstevel@tonic-gate  *
1098*7c478bd9Sstevel@tonic-gate  * The code below is equivalent to:
1099*7c478bd9Sstevel@tonic-gate  *
1100*7c478bd9Sstevel@tonic-gate  *	hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec;
1101*7c478bd9Sstevel@tonic-gate  *
1102*7c478bd9Sstevel@tonic-gate  * but requires no integer multiply.
1103*7c478bd9Sstevel@tonic-gate  */
1104*7c478bd9Sstevel@tonic-gate hrtime_t
1105*7c478bd9Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp)
1106*7c478bd9Sstevel@tonic-gate {
1107*7c478bd9Sstevel@tonic-gate 	hrtime_t hrt;
1108*7c478bd9Sstevel@tonic-gate 
1109*7c478bd9Sstevel@tonic-gate 	hrt = tsp->tv_sec;
1110*7c478bd9Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
1111*7c478bd9Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
1112*7c478bd9Sstevel@tonic-gate 	hrt = (hrt << 7) - hrt - hrt - hrt;
1113*7c478bd9Sstevel@tonic-gate 	hrt = (hrt << 9) + tsp->tv_nsec;
1114*7c478bd9Sstevel@tonic-gate 	return (hrt);
1115*7c478bd9Sstevel@tonic-gate }
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate /*
1118*7c478bd9Sstevel@tonic-gate  * For the various 32-bit "compatibility" paths in the system.
1119*7c478bd9Sstevel@tonic-gate  */
1120*7c478bd9Sstevel@tonic-gate void
1121*7c478bd9Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p)
1122*7c478bd9Sstevel@tonic-gate {
1123*7c478bd9Sstevel@tonic-gate 	timestruc_t ts;
1124*7c478bd9Sstevel@tonic-gate 
1125*7c478bd9Sstevel@tonic-gate 	hrt2ts(hrt, &ts);
1126*7c478bd9Sstevel@tonic-gate 	TIMESPEC_TO_TIMESPEC32(ts32p, &ts);
1127*7c478bd9Sstevel@tonic-gate }
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate /*
1130*7c478bd9Sstevel@tonic-gate  * If this ever becomes performance critical (ha!), we can borrow the
1131*7c478bd9Sstevel@tonic-gate  * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the
1132*7c478bd9Sstevel@tonic-gate  * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by
1133*7c478bd9Sstevel@tonic-gate  * 1,000.  For now, we'll opt for readability (besides, the compiler does
1134*7c478bd9Sstevel@tonic-gate  * a passable job of optimizing constant multiplication into shifts and adds).
1135*7c478bd9Sstevel@tonic-gate  */
1136*7c478bd9Sstevel@tonic-gate hrtime_t
1137*7c478bd9Sstevel@tonic-gate tv2hrt(struct timeval *tvp)
1138*7c478bd9Sstevel@tonic-gate {
1139*7c478bd9Sstevel@tonic-gate 	return ((hrtime_t)tvp->tv_sec * NANOSEC +
1140*7c478bd9Sstevel@tonic-gate 	    (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC));
1141*7c478bd9Sstevel@tonic-gate }
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate void
1144*7c478bd9Sstevel@tonic-gate hrt2tv(hrtime_t ts, struct timeval *tvp)
1145*7c478bd9Sstevel@tonic-gate {
1146*7c478bd9Sstevel@tonic-gate 	tvp->tv_sec = ts / NANOSEC;
1147*7c478bd9Sstevel@tonic-gate 	tvp->tv_usec = (ts % NANOSEC) / (NANOSEC / MICROSEC);
1148*7c478bd9Sstevel@tonic-gate }
1149*7c478bd9Sstevel@tonic-gate 
1150*7c478bd9Sstevel@tonic-gate int
1151*7c478bd9Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp)
1152*7c478bd9Sstevel@tonic-gate {
1153*7c478bd9Sstevel@tonic-gate 	timespec_t rqtime;
1154*7c478bd9Sstevel@tonic-gate 	timespec_t rmtime;
1155*7c478bd9Sstevel@tonic-gate 	timespec_t now;
1156*7c478bd9Sstevel@tonic-gate 	int timecheck;
1157*7c478bd9Sstevel@tonic-gate 	int ret = 1;
1158*7c478bd9Sstevel@tonic-gate 	model_t datamodel = get_udatamodel();
1159*7c478bd9Sstevel@tonic-gate 
1160*7c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
1161*7c478bd9Sstevel@tonic-gate 		if (copyin(rqtp, &rqtime, sizeof (rqtime)))
1162*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1163*7c478bd9Sstevel@tonic-gate 	} else {
1164*7c478bd9Sstevel@tonic-gate 		timespec32_t rqtime32;
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate 		if (copyin(rqtp, &rqtime32, sizeof (rqtime32)))
1167*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1168*7c478bd9Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32);
1169*7c478bd9Sstevel@tonic-gate 	}
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate 	if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 ||
1172*7c478bd9Sstevel@tonic-gate 	    rqtime.tv_nsec >= NANOSEC)
1173*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1174*7c478bd9Sstevel@tonic-gate 
1175*7c478bd9Sstevel@tonic-gate 	if (timerspecisset(&rqtime)) {
1176*7c478bd9Sstevel@tonic-gate 		timecheck = timechanged;
1177*7c478bd9Sstevel@tonic-gate 		gethrestime(&now);
1178*7c478bd9Sstevel@tonic-gate 		timespecadd(&rqtime, &now);
1179*7c478bd9Sstevel@tonic-gate 		mutex_enter(&curthread->t_delay_lock);
1180*7c478bd9Sstevel@tonic-gate 		while ((ret = cv_waituntil_sig(&curthread->t_delay_cv,
1181*7c478bd9Sstevel@tonic-gate 		    &curthread->t_delay_lock, &rqtime, timecheck)) > 0)
1182*7c478bd9Sstevel@tonic-gate 			continue;
1183*7c478bd9Sstevel@tonic-gate 		mutex_exit(&curthread->t_delay_lock);
1184*7c478bd9Sstevel@tonic-gate 	}
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate 	if (rmtp) {
1187*7c478bd9Sstevel@tonic-gate 		/*
1188*7c478bd9Sstevel@tonic-gate 		 * If cv_waituntil_sig() returned due to a signal, and
1189*7c478bd9Sstevel@tonic-gate 		 * there is time remaining, then set the time remaining.
1190*7c478bd9Sstevel@tonic-gate 		 * Else set time remaining to zero
1191*7c478bd9Sstevel@tonic-gate 		 */
1192*7c478bd9Sstevel@tonic-gate 		rmtime.tv_sec = rmtime.tv_nsec = 0;
1193*7c478bd9Sstevel@tonic-gate 		if (ret == 0) {
1194*7c478bd9Sstevel@tonic-gate 			gethrestime(&now);
1195*7c478bd9Sstevel@tonic-gate 			if ((now.tv_sec < rqtime.tv_sec) ||
1196*7c478bd9Sstevel@tonic-gate 			    ((now.tv_sec == rqtime.tv_sec) &&
1197*7c478bd9Sstevel@tonic-gate 			    (now.tv_nsec < rqtime.tv_nsec))) {
1198*7c478bd9Sstevel@tonic-gate 				rmtime = rqtime;
1199*7c478bd9Sstevel@tonic-gate 				timespecsub(&rmtime, &now);
1200*7c478bd9Sstevel@tonic-gate 			}
1201*7c478bd9Sstevel@tonic-gate 		}
1202*7c478bd9Sstevel@tonic-gate 
1203*7c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
1204*7c478bd9Sstevel@tonic-gate 			if (copyout(&rmtime, rmtp, sizeof (rmtime)))
1205*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1206*7c478bd9Sstevel@tonic-gate 		} else {
1207*7c478bd9Sstevel@tonic-gate 			timespec32_t rmtime32;
1208*7c478bd9Sstevel@tonic-gate 
1209*7c478bd9Sstevel@tonic-gate 			TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
1210*7c478bd9Sstevel@tonic-gate 			if (copyout(&rmtime32, rmtp, sizeof (rmtime32)))
1211*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1212*7c478bd9Sstevel@tonic-gate 		}
1213*7c478bd9Sstevel@tonic-gate 	}
1214*7c478bd9Sstevel@tonic-gate 
1215*7c478bd9Sstevel@tonic-gate 	if (ret == 0)
1216*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
1217*7c478bd9Sstevel@tonic-gate 	return (0);
1218*7c478bd9Sstevel@tonic-gate }
1219*7c478bd9Sstevel@tonic-gate 
1220*7c478bd9Sstevel@tonic-gate /*
1221*7c478bd9Sstevel@tonic-gate  * Routines to convert standard UNIX time (seconds since Jan 1, 1970)
1222*7c478bd9Sstevel@tonic-gate  * into year/month/day/hour/minute/second format, and back again.
1223*7c478bd9Sstevel@tonic-gate  * Note: these routines require tod_lock held to protect cached state.
1224*7c478bd9Sstevel@tonic-gate  */
1225*7c478bd9Sstevel@tonic-gate static int days_thru_month[64] = {
1226*7c478bd9Sstevel@tonic-gate 	0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0,
1227*7c478bd9Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
1228*7c478bd9Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
1229*7c478bd9Sstevel@tonic-gate 	0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
1230*7c478bd9Sstevel@tonic-gate };
1231*7c478bd9Sstevel@tonic-gate 
1232*7c478bd9Sstevel@tonic-gate todinfo_t saved_tod;
1233*7c478bd9Sstevel@tonic-gate int saved_utc = -60;
1234*7c478bd9Sstevel@tonic-gate 
1235*7c478bd9Sstevel@tonic-gate todinfo_t
1236*7c478bd9Sstevel@tonic-gate utc_to_tod(time_t utc)
1237*7c478bd9Sstevel@tonic-gate {
1238*7c478bd9Sstevel@tonic-gate 	long dse, day, month, year;
1239*7c478bd9Sstevel@tonic-gate 	todinfo_t tod;
1240*7c478bd9Sstevel@tonic-gate 
1241*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate 	if (utc < 0)			/* should never happen */
1244*7c478bd9Sstevel@tonic-gate 		utc = 0;
1245*7c478bd9Sstevel@tonic-gate 
1246*7c478bd9Sstevel@tonic-gate 	saved_tod.tod_sec += utc - saved_utc;
1247*7c478bd9Sstevel@tonic-gate 	saved_utc = utc;
1248*7c478bd9Sstevel@tonic-gate 	if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60)
1249*7c478bd9Sstevel@tonic-gate 		return (saved_tod);	/* only the seconds changed */
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 	dse = utc / 86400;		/* days since epoch */
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate 	tod.tod_sec = utc % 60;
1254*7c478bd9Sstevel@tonic-gate 	tod.tod_min = (utc % 3600) / 60;
1255*7c478bd9Sstevel@tonic-gate 	tod.tod_hour = (utc % 86400) / 3600;
1256*7c478bd9Sstevel@tonic-gate 	tod.tod_dow = (dse + 4) % 7 + 1;	/* epoch was a Thursday */
1257*7c478bd9Sstevel@tonic-gate 
1258*7c478bd9Sstevel@tonic-gate 	year = dse / 365 + 72;	/* first guess -- always a bit too large */
1259*7c478bd9Sstevel@tonic-gate 	do {
1260*7c478bd9Sstevel@tonic-gate 		year--;
1261*7c478bd9Sstevel@tonic-gate 		day = dse - 365 * (year - 70) - ((year - 69) >> 2);
1262*7c478bd9Sstevel@tonic-gate 	} while (day < 0);
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 	month = ((year & 3) << 4) + 1;
1265*7c478bd9Sstevel@tonic-gate 	while (day >= days_thru_month[month + 1])
1266*7c478bd9Sstevel@tonic-gate 		month++;
1267*7c478bd9Sstevel@tonic-gate 
1268*7c478bd9Sstevel@tonic-gate 	tod.tod_day = day - days_thru_month[month] + 1;
1269*7c478bd9Sstevel@tonic-gate 	tod.tod_month = month & 15;
1270*7c478bd9Sstevel@tonic-gate 	tod.tod_year = year;
1271*7c478bd9Sstevel@tonic-gate 
1272*7c478bd9Sstevel@tonic-gate 	saved_tod = tod;
1273*7c478bd9Sstevel@tonic-gate 	return (tod);
1274*7c478bd9Sstevel@tonic-gate }
1275*7c478bd9Sstevel@tonic-gate 
1276*7c478bd9Sstevel@tonic-gate time_t
1277*7c478bd9Sstevel@tonic-gate tod_to_utc(todinfo_t tod)
1278*7c478bd9Sstevel@tonic-gate {
1279*7c478bd9Sstevel@tonic-gate 	time_t utc;
1280*7c478bd9Sstevel@tonic-gate 	int year = tod.tod_year;
1281*7c478bd9Sstevel@tonic-gate 	int month = tod.tod_month + ((year & 3) << 4);
1282*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
1283*7c478bd9Sstevel@tonic-gate 	/* only warn once, not each time called */
1284*7c478bd9Sstevel@tonic-gate 	static int year_warn = 1;
1285*7c478bd9Sstevel@tonic-gate 	static int month_warn = 1;
1286*7c478bd9Sstevel@tonic-gate 	static int day_warn = 1;
1287*7c478bd9Sstevel@tonic-gate 	static int hour_warn = 1;
1288*7c478bd9Sstevel@tonic-gate 	static int min_warn = 1;
1289*7c478bd9Sstevel@tonic-gate 	static int sec_warn = 1;
1290*7c478bd9Sstevel@tonic-gate 	int days_diff = days_thru_month[month + 1] - days_thru_month[month];
1291*7c478bd9Sstevel@tonic-gate #endif
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1294*7c478bd9Sstevel@tonic-gate 
1295*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
1296*7c478bd9Sstevel@tonic-gate 	if (year_warn && (year < 70 || year > 8029)) {
1297*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1298*7c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
1299*7c478bd9Sstevel@tonic-gate 			"wrong years value %d -- time needs to be reset\n",
1300*7c478bd9Sstevel@tonic-gate 			year);
1301*7c478bd9Sstevel@tonic-gate 		year_warn = 0;
1302*7c478bd9Sstevel@tonic-gate 	}
1303*7c478bd9Sstevel@tonic-gate 
1304*7c478bd9Sstevel@tonic-gate 	if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) {
1305*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1306*7c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
1307*7c478bd9Sstevel@tonic-gate 			"wrong months value %d -- time needs to be reset\n",
1308*7c478bd9Sstevel@tonic-gate 			tod.tod_month);
1309*7c478bd9Sstevel@tonic-gate 		month_warn = 0;
1310*7c478bd9Sstevel@tonic-gate 	}
1311*7c478bd9Sstevel@tonic-gate 
1312*7c478bd9Sstevel@tonic-gate 	if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) {
1313*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1314*7c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
1315*7c478bd9Sstevel@tonic-gate 			"wrong days value %d -- time needs to be reset\n",
1316*7c478bd9Sstevel@tonic-gate 			tod.tod_day);
1317*7c478bd9Sstevel@tonic-gate 		day_warn = 0;
1318*7c478bd9Sstevel@tonic-gate 	}
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate 	if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) {
1321*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1322*7c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
1323*7c478bd9Sstevel@tonic-gate 			"wrong hours value %d -- time needs to be reset\n",
1324*7c478bd9Sstevel@tonic-gate 			tod.tod_hour);
1325*7c478bd9Sstevel@tonic-gate 		hour_warn = 0;
1326*7c478bd9Sstevel@tonic-gate 	}
1327*7c478bd9Sstevel@tonic-gate 
1328*7c478bd9Sstevel@tonic-gate 	if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) {
1329*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1330*7c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
1331*7c478bd9Sstevel@tonic-gate 			"wrong minutes value %d -- time needs to be reset\n",
1332*7c478bd9Sstevel@tonic-gate 			tod.tod_min);
1333*7c478bd9Sstevel@tonic-gate 		min_warn = 0;
1334*7c478bd9Sstevel@tonic-gate 	}
1335*7c478bd9Sstevel@tonic-gate 
1336*7c478bd9Sstevel@tonic-gate 	if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) {
1337*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1338*7c478bd9Sstevel@tonic-gate 			"The hardware real-time clock appears to have the "
1339*7c478bd9Sstevel@tonic-gate 			"wrong seconds value %d -- time needs to be reset\n",
1340*7c478bd9Sstevel@tonic-gate 			tod.tod_sec);
1341*7c478bd9Sstevel@tonic-gate 		sec_warn = 0;
1342*7c478bd9Sstevel@tonic-gate 	}
1343*7c478bd9Sstevel@tonic-gate #endif
1344*7c478bd9Sstevel@tonic-gate 
1345*7c478bd9Sstevel@tonic-gate 	utc = (year - 70);		/* next 3 lines: utc = 365y + y/4 */
1346*7c478bd9Sstevel@tonic-gate 	utc += (utc << 3) + (utc << 6);
1347*7c478bd9Sstevel@tonic-gate 	utc += (utc << 2) + ((year - 69) >> 2);
1348*7c478bd9Sstevel@tonic-gate 	utc += days_thru_month[month] + tod.tod_day - 1;
1349*7c478bd9Sstevel@tonic-gate 	utc = (utc << 3) + (utc << 4) + tod.tod_hour;	/* 24 * day + hour */
1350*7c478bd9Sstevel@tonic-gate 	utc = (utc << 6) - (utc << 2) + tod.tod_min;	/* 60 * hour + min */
1351*7c478bd9Sstevel@tonic-gate 	utc = (utc << 6) - (utc << 2) + tod.tod_sec;	/* 60 * min + sec */
1352*7c478bd9Sstevel@tonic-gate 
1353*7c478bd9Sstevel@tonic-gate 	return (utc);
1354*7c478bd9Sstevel@tonic-gate }
1355