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