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