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