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