]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_time.c
af55a09ed5a80334fc91176a2a5fc4be8c88b006
[apple/xnu.git] / bsd / kern / kern_time.c
1 /*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
62 */
63 /*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70 #include <sys/param.h>
71 #include <sys/resourcevar.h>
72 #include <sys/kernel.h>
73 #include <sys/systm.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/vnode.h>
77 #include <sys/time.h>
78 #include <sys/priv.h>
79
80 #include <sys/mount_internal.h>
81 #include <sys/sysproto.h>
82 #include <sys/signalvar.h>
83 #include <sys/protosw.h> /* for net_uptime2timeval() */
84
85 #include <kern/clock.h>
86 #include <kern/task.h>
87 #include <kern/thread_call.h>
88 #if CONFIG_MACF
89 #include <security/mac_framework.h>
90 #endif
91
92 #define HZ 100 /* XXX */
93
94 /* simple lock used to access timezone, tz structure */
95 lck_spin_t * tz_slock;
96 lck_grp_t * tz_slock_grp;
97 lck_attr_t * tz_slock_attr;
98 lck_grp_attr_t *tz_slock_grp_attr;
99
100 static void setthetime(
101 struct timeval *tv);
102
103 void time_zone_slock_init(void);
104
105 /*
106 * Time of day and interval timer support.
107 *
108 * These routines provide the kernel entry points to get and set
109 * the time-of-day and per-process interval timers. Subroutines
110 * here provide support for adding and subtracting timeval structures
111 * and decrementing interval timers, optionally reloading the interval
112 * timers when they expire.
113 */
114 /* ARGSUSED */
115 int
116 gettimeofday(
117 struct proc *p,
118 struct gettimeofday_args *uap,
119 __unused int32_t *retval)
120 {
121 int error = 0;
122 struct timezone ltz; /* local copy */
123 clock_sec_t secs;
124 clock_usec_t usecs;
125 uint64_t mach_time;
126
127 if (uap->tp || uap->mach_absolute_time) {
128 clock_gettimeofday_and_absolute_time(&secs, &usecs, &mach_time);
129 }
130
131 if (uap->tp) {
132 /* Casting secs through a uint32_t to match arm64 commpage */
133 if (IS_64BIT_PROCESS(p)) {
134 struct user64_timeval user_atv = {};
135 user_atv.tv_sec = (uint32_t)secs;
136 user_atv.tv_usec = usecs;
137 error = copyout(&user_atv, uap->tp, sizeof(user_atv));
138 } else {
139 struct user32_timeval user_atv = {};
140 user_atv.tv_sec = (uint32_t)secs;
141 user_atv.tv_usec = usecs;
142 error = copyout(&user_atv, uap->tp, sizeof(user_atv));
143 }
144 if (error) {
145 return error;
146 }
147 }
148
149 if (uap->tzp) {
150 lck_spin_lock(tz_slock);
151 ltz = tz;
152 lck_spin_unlock(tz_slock);
153
154 error = copyout((caddr_t)&ltz, CAST_USER_ADDR_T(uap->tzp), sizeof(tz));
155 }
156
157 if (error == 0 && uap->mach_absolute_time) {
158 error = copyout(&mach_time, uap->mach_absolute_time, sizeof(mach_time));
159 }
160
161 return error;
162 }
163
164 /*
165 * XXX Y2038 bug because of setthetime() argument
166 */
167 /* ARGSUSED */
168 int
169 settimeofday(__unused struct proc *p, struct settimeofday_args *uap, __unused int32_t *retval)
170 {
171 struct timeval atv;
172 struct timezone atz;
173 int error;
174
175 bzero(&atv, sizeof(atv));
176
177 #if CONFIG_MACF
178 error = mac_system_check_settime(kauth_cred_get());
179 if (error)
180 return (error);
181 #endif
182 if ((error = suser(kauth_cred_get(), &p->p_acflag)))
183 return (error);
184 /* Verify all parameters before changing time */
185 if (uap->tv) {
186 if (IS_64BIT_PROCESS(p)) {
187 struct user64_timeval user_atv;
188 error = copyin(uap->tv, &user_atv, sizeof(user_atv));
189 atv.tv_sec = user_atv.tv_sec;
190 atv.tv_usec = user_atv.tv_usec;
191 } else {
192 struct user32_timeval user_atv;
193 error = copyin(uap->tv, &user_atv, sizeof(user_atv));
194 atv.tv_sec = user_atv.tv_sec;
195 atv.tv_usec = user_atv.tv_usec;
196 }
197 if (error)
198 return (error);
199 }
200 if (uap->tzp && (error = copyin(uap->tzp, (caddr_t)&atz, sizeof(atz))))
201 return (error);
202 if (uap->tv) {
203 timevalfix(&atv);
204 if (atv.tv_sec < 0 || (atv.tv_sec == 0 && atv.tv_usec < 0))
205 return (EPERM);
206 setthetime(&atv);
207 }
208 if (uap->tzp) {
209 lck_spin_lock(tz_slock);
210 tz = atz;
211 lck_spin_unlock(tz_slock);
212 }
213 return (0);
214 }
215
216 static void
217 setthetime(
218 struct timeval *tv)
219 {
220 clock_set_calendar_microtime(tv->tv_sec, tv->tv_usec);
221 }
222
223 /*
224 * XXX Y2038 bug because of clock_adjtime() first argument
225 */
226 /* ARGSUSED */
227 int
228 adjtime(struct proc *p, struct adjtime_args *uap, __unused int32_t *retval)
229 {
230 struct timeval atv;
231 int error;
232
233 #if CONFIG_MACF
234 error = mac_system_check_settime(kauth_cred_get());
235 if (error)
236 return (error);
237 #endif
238 if ((error = priv_check_cred(kauth_cred_get(), PRIV_ADJTIME, 0)))
239 return (error);
240 if (IS_64BIT_PROCESS(p)) {
241 struct user64_timeval user_atv;
242 error = copyin(uap->delta, &user_atv, sizeof(user_atv));
243 atv.tv_sec = user_atv.tv_sec;
244 atv.tv_usec = user_atv.tv_usec;
245 } else {
246 struct user32_timeval user_atv;
247 error = copyin(uap->delta, &user_atv, sizeof(user_atv));
248 atv.tv_sec = user_atv.tv_sec;
249 atv.tv_usec = user_atv.tv_usec;
250 }
251 if (error)
252 return (error);
253
254 /*
255 * Compute the total correction and the rate at which to apply it.
256 */
257 clock_adjtime(&atv.tv_sec, &atv.tv_usec);
258
259 if (uap->olddelta) {
260 if (IS_64BIT_PROCESS(p)) {
261 struct user64_timeval user_atv;
262 user_atv.tv_sec = atv.tv_sec;
263 user_atv.tv_usec = atv.tv_usec;
264 error = copyout(&user_atv, uap->olddelta, sizeof(user_atv));
265 } else {
266 struct user32_timeval user_atv;
267 user_atv.tv_sec = atv.tv_sec;
268 user_atv.tv_usec = atv.tv_usec;
269 error = copyout(&user_atv, uap->olddelta, sizeof(user_atv));
270 }
271 }
272
273 return (0);
274 }
275
276 /*
277 * Verify the calendar value. If negative,
278 * reset to zero (the epoch).
279 */
280 void
281 inittodr(
282 __unused time_t base)
283 {
284 struct timeval tv;
285
286 /*
287 * Assertion:
288 * The calendar has already been
289 * set up from the platform clock.
290 *
291 * The value returned by microtime()
292 * is gotten from the calendar.
293 */
294 microtime(&tv);
295
296 if (tv.tv_sec < 0 || tv.tv_usec < 0) {
297 printf ("WARNING: preposterous time in Real Time Clock");
298 tv.tv_sec = 0; /* the UNIX epoch */
299 tv.tv_usec = 0;
300 setthetime(&tv);
301 printf(" -- CHECK AND RESET THE DATE!\n");
302 }
303 }
304
305 time_t
306 boottime_sec(void)
307 {
308 clock_sec_t secs;
309 clock_nsec_t nanosecs;
310
311 clock_get_boottime_nanotime(&secs, &nanosecs);
312 return (secs);
313 }
314
315 void
316 boottime_timeval(struct timeval *tv)
317 {
318 clock_sec_t secs;
319 clock_usec_t microsecs;
320
321 clock_get_boottime_microtime(&secs, &microsecs);
322
323 tv->tv_sec = secs;
324 tv->tv_usec = microsecs;
325 }
326
327 /*
328 * Get value of an interval timer. The process virtual and
329 * profiling virtual time timers are kept internally in the
330 * way they are specified externally: in time until they expire.
331 *
332 * The real time interval timer expiration time (p_rtime)
333 * is kept as an absolute time rather than as a delta, so that
334 * it is easy to keep periodic real-time signals from drifting.
335 *
336 * The real time timer is processed by a callout routine.
337 * Since a callout may be delayed in real time due to
338 * other processing in the system, it is possible for the real
339 * time callout routine (realitexpire, given below), to be delayed
340 * in real time past when it is supposed to occur. It does not
341 * suffice, therefore, to reload the real time .it_value from the
342 * real time .it_interval. Rather, we compute the next time in
343 * absolute time when the timer should go off.
344 *
345 * Returns: 0 Success
346 * EINVAL Invalid argument
347 * copyout:EFAULT Bad address
348 */
349 /* ARGSUSED */
350 int
351 getitimer(struct proc *p, struct getitimer_args *uap, __unused int32_t *retval)
352 {
353 struct itimerval aitv;
354
355 if (uap->which > ITIMER_PROF)
356 return(EINVAL);
357
358 bzero(&aitv, sizeof(aitv));
359
360 proc_spinlock(p);
361 switch (uap->which) {
362
363 case ITIMER_REAL:
364 /*
365 * If time for real time timer has passed return 0,
366 * else return difference between current time and
367 * time for the timer to go off.
368 */
369 aitv = p->p_realtimer;
370 if (timerisset(&p->p_rtime)) {
371 struct timeval now;
372
373 microuptime(&now);
374 if (timercmp(&p->p_rtime, &now, <))
375 timerclear(&aitv.it_value);
376 else {
377 aitv.it_value = p->p_rtime;
378 timevalsub(&aitv.it_value, &now);
379 }
380 }
381 else
382 timerclear(&aitv.it_value);
383 break;
384
385 case ITIMER_VIRTUAL:
386 aitv = p->p_vtimer_user;
387 break;
388
389 case ITIMER_PROF:
390 aitv = p->p_vtimer_prof;
391 break;
392 }
393
394 proc_spinunlock(p);
395
396 if (IS_64BIT_PROCESS(p)) {
397 struct user64_itimerval user_itv;
398 user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec;
399 user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec;
400 user_itv.it_value.tv_sec = aitv.it_value.tv_sec;
401 user_itv.it_value.tv_usec = aitv.it_value.tv_usec;
402 return (copyout((caddr_t)&user_itv, uap->itv, sizeof (user_itv)));
403 } else {
404 struct user32_itimerval user_itv;
405 user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec;
406 user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec;
407 user_itv.it_value.tv_sec = aitv.it_value.tv_sec;
408 user_itv.it_value.tv_usec = aitv.it_value.tv_usec;
409 return (copyout((caddr_t)&user_itv, uap->itv, sizeof (user_itv)));
410 }
411 }
412
413 /*
414 * Returns: 0 Success
415 * EINVAL Invalid argument
416 * copyin:EFAULT Bad address
417 * getitimer:EINVAL Invalid argument
418 * getitimer:EFAULT Bad address
419 */
420 /* ARGSUSED */
421 int
422 setitimer(struct proc *p, struct setitimer_args *uap, int32_t *retval)
423 {
424 struct itimerval aitv;
425 user_addr_t itvp;
426 int error;
427
428 bzero(&aitv, sizeof(aitv));
429
430 if (uap->which > ITIMER_PROF)
431 return (EINVAL);
432 if ((itvp = uap->itv)) {
433 if (IS_64BIT_PROCESS(p)) {
434 struct user64_itimerval user_itv;
435 if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof (user_itv))))
436 return (error);
437 aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec;
438 aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec;
439 aitv.it_value.tv_sec = user_itv.it_value.tv_sec;
440 aitv.it_value.tv_usec = user_itv.it_value.tv_usec;
441 } else {
442 struct user32_itimerval user_itv;
443 if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof (user_itv))))
444 return (error);
445 aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec;
446 aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec;
447 aitv.it_value.tv_sec = user_itv.it_value.tv_sec;
448 aitv.it_value.tv_usec = user_itv.it_value.tv_usec;
449 }
450 }
451 if ((uap->itv = uap->oitv) && (error = getitimer(p, (struct getitimer_args *)uap, retval)))
452 return (error);
453 if (itvp == 0)
454 return (0);
455 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
456 return (EINVAL);
457
458 switch (uap->which) {
459
460 case ITIMER_REAL:
461 proc_spinlock(p);
462 if (timerisset(&aitv.it_value)) {
463 microuptime(&p->p_rtime);
464 timevaladd(&p->p_rtime, &aitv.it_value);
465 p->p_realtimer = aitv;
466 if (!thread_call_enter_delayed_with_leeway(p->p_rcall, NULL,
467 tvtoabstime(&p->p_rtime), 0, THREAD_CALL_DELAY_USER_NORMAL))
468 p->p_ractive++;
469 } else {
470 timerclear(&p->p_rtime);
471 p->p_realtimer = aitv;
472 if (thread_call_cancel(p->p_rcall))
473 p->p_ractive--;
474 }
475 proc_spinunlock(p);
476
477 break;
478
479
480 case ITIMER_VIRTUAL:
481 if (timerisset(&aitv.it_value))
482 task_vtimer_set(p->task, TASK_VTIMER_USER);
483 else
484 task_vtimer_clear(p->task, TASK_VTIMER_USER);
485
486 proc_spinlock(p);
487 p->p_vtimer_user = aitv;
488 proc_spinunlock(p);
489 break;
490
491 case ITIMER_PROF:
492 if (timerisset(&aitv.it_value))
493 task_vtimer_set(p->task, TASK_VTIMER_PROF);
494 else
495 task_vtimer_clear(p->task, TASK_VTIMER_PROF);
496
497 proc_spinlock(p);
498 p->p_vtimer_prof = aitv;
499 proc_spinunlock(p);
500 break;
501 }
502
503 return (0);
504 }
505
506 /*
507 * Real interval timer expired:
508 * send process whose timer expired an alarm signal.
509 * If time is not set up to reload, then just return.
510 * Else compute next time timer should go off which is > current time.
511 * This is where delay in processing this timeout causes multiple
512 * SIGALRM calls to be compressed into one.
513 */
514 void
515 realitexpire(
516 struct proc *p)
517 {
518 struct proc *r;
519 struct timeval t;
520
521 r = proc_find(p->p_pid);
522
523 proc_spinlock(p);
524
525 assert(p->p_ractive > 0);
526
527 if (--p->p_ractive > 0 || r != p) {
528 /*
529 * bail, because either proc is exiting
530 * or there's another active thread call
531 */
532 proc_spinunlock(p);
533
534 if (r != NULL)
535 proc_rele(r);
536 return;
537 }
538
539 if (!timerisset(&p->p_realtimer.it_interval)) {
540 /*
541 * p_realtimer was cleared while this call was pending,
542 * send one last SIGALRM, but don't re-arm
543 */
544 timerclear(&p->p_rtime);
545 proc_spinunlock(p);
546
547 psignal(p, SIGALRM);
548 proc_rele(p);
549 return;
550 }
551
552 proc_spinunlock(p);
553
554 /*
555 * Send the signal before re-arming the next thread call,
556 * so in case psignal blocks, we won't create yet another thread call.
557 */
558
559 psignal(p, SIGALRM);
560
561 proc_spinlock(p);
562
563 /* Should we still re-arm the next thread call? */
564 if (!timerisset(&p->p_realtimer.it_interval)) {
565 timerclear(&p->p_rtime);
566 proc_spinunlock(p);
567
568 proc_rele(p);
569 return;
570 }
571
572 microuptime(&t);
573 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
574
575 if (timercmp(&p->p_rtime, &t, <=)) {
576 if ((p->p_rtime.tv_sec + 2) >= t.tv_sec) {
577 for (;;) {
578 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
579 if (timercmp(&p->p_rtime, &t, >))
580 break;
581 }
582 } else {
583 p->p_rtime = p->p_realtimer.it_interval;
584 timevaladd(&p->p_rtime, &t);
585 }
586 }
587
588 assert(p->p_rcall != NULL);
589
590 if (!thread_call_enter_delayed_with_leeway(p->p_rcall, NULL, tvtoabstime(&p->p_rtime), 0,
591 THREAD_CALL_DELAY_USER_NORMAL)) {
592 p->p_ractive++;
593 }
594
595 proc_spinunlock(p);
596
597 proc_rele(p);
598 }
599
600 /*
601 * Called once in proc_exit to clean up after an armed or pending realitexpire
602 *
603 * This will only be called after the proc refcount is drained,
604 * so realitexpire cannot be currently holding a proc ref.
605 * i.e. it will/has gotten PROC_NULL from proc_find.
606 */
607 void
608 proc_free_realitimer(proc_t p)
609 {
610 proc_spinlock(p);
611
612 assert(p->p_rcall != NULL);
613 assert(p->p_refcount == 0);
614
615 timerclear(&p->p_realtimer.it_interval);
616
617 if (thread_call_cancel(p->p_rcall)) {
618 assert(p->p_ractive > 0);
619 p->p_ractive--;
620 }
621
622 while (p->p_ractive > 0) {
623 proc_spinunlock(p);
624
625 delay(1);
626
627 proc_spinlock(p);
628 }
629
630 thread_call_t call = p->p_rcall;
631 p->p_rcall = NULL;
632
633 proc_spinunlock(p);
634
635 thread_call_free(call);
636 }
637
638 /*
639 * Check that a proposed value to load into the .it_value or
640 * .it_interval part of an interval timer is acceptable.
641 */
642 int
643 itimerfix(
644 struct timeval *tv)
645 {
646
647 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
648 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
649 return (EINVAL);
650 return (0);
651 }
652
653 int
654 timespec_is_valid(const struct timespec *ts)
655 {
656 /* The INT32_MAX limit ensures the timespec is safe for clock_*() functions
657 * which accept 32-bit ints. */
658 if (ts->tv_sec < 0 || ts->tv_sec > INT32_MAX ||
659 ts->tv_nsec < 0 || (unsigned long long)ts->tv_nsec > NSEC_PER_SEC) {
660 return 0;
661 }
662 return 1;
663 }
664
665 /*
666 * Decrement an interval timer by a specified number
667 * of microseconds, which must be less than a second,
668 * i.e. < 1000000. If the timer expires, then reload
669 * it. In this case, carry over (usec - old value) to
670 * reduce the value reloaded into the timer so that
671 * the timer does not drift. This routine assumes
672 * that it is called in a context where the timers
673 * on which it is operating cannot change in value.
674 */
675 int
676 itimerdecr(proc_t p,
677 struct itimerval *itp, int usec)
678 {
679
680 proc_spinlock(p);
681
682 if (itp->it_value.tv_usec < usec) {
683 if (itp->it_value.tv_sec == 0) {
684 /* expired, and already in next interval */
685 usec -= itp->it_value.tv_usec;
686 goto expire;
687 }
688 itp->it_value.tv_usec += 1000000;
689 itp->it_value.tv_sec--;
690 }
691 itp->it_value.tv_usec -= usec;
692 usec = 0;
693 if (timerisset(&itp->it_value)) {
694 proc_spinunlock(p);
695 return (1);
696 }
697 /* expired, exactly at end of interval */
698 expire:
699 if (timerisset(&itp->it_interval)) {
700 itp->it_value = itp->it_interval;
701 if (itp->it_value.tv_sec > 0) {
702 itp->it_value.tv_usec -= usec;
703 if (itp->it_value.tv_usec < 0) {
704 itp->it_value.tv_usec += 1000000;
705 itp->it_value.tv_sec--;
706 }
707 }
708 } else
709 itp->it_value.tv_usec = 0; /* sec is already 0 */
710 proc_spinunlock(p);
711 return (0);
712 }
713
714 /*
715 * Add and subtract routines for timevals.
716 * N.B.: subtract routine doesn't deal with
717 * results which are before the beginning,
718 * it just gets very confused in this case.
719 * Caveat emptor.
720 */
721 void
722 timevaladd(
723 struct timeval *t1,
724 struct timeval *t2)
725 {
726
727 t1->tv_sec += t2->tv_sec;
728 t1->tv_usec += t2->tv_usec;
729 timevalfix(t1);
730 }
731 void
732 timevalsub(
733 struct timeval *t1,
734 struct timeval *t2)
735 {
736
737 t1->tv_sec -= t2->tv_sec;
738 t1->tv_usec -= t2->tv_usec;
739 timevalfix(t1);
740 }
741 void
742 timevalfix(
743 struct timeval *t1)
744 {
745
746 if (t1->tv_usec < 0) {
747 t1->tv_sec--;
748 t1->tv_usec += 1000000;
749 }
750 if (t1->tv_usec >= 1000000) {
751 t1->tv_sec++;
752 t1->tv_usec -= 1000000;
753 }
754 }
755
756 /*
757 * Return the best possible estimate of the time in the timeval
758 * to which tvp points.
759 */
760 void
761 microtime(
762 struct timeval *tvp)
763 {
764 clock_sec_t tv_sec;
765 clock_usec_t tv_usec;
766
767 clock_get_calendar_microtime(&tv_sec, &tv_usec);
768
769 tvp->tv_sec = tv_sec;
770 tvp->tv_usec = tv_usec;
771 }
772
773 void
774 microtime_with_abstime(
775 struct timeval *tvp, uint64_t *abstime)
776 {
777 clock_sec_t tv_sec;
778 clock_usec_t tv_usec;
779
780 clock_get_calendar_absolute_and_microtime(&tv_sec, &tv_usec, abstime);
781
782 tvp->tv_sec = tv_sec;
783 tvp->tv_usec = tv_usec;
784 }
785
786 void
787 microuptime(
788 struct timeval *tvp)
789 {
790 clock_sec_t tv_sec;
791 clock_usec_t tv_usec;
792
793 clock_get_system_microtime(&tv_sec, &tv_usec);
794
795 tvp->tv_sec = tv_sec;
796 tvp->tv_usec = tv_usec;
797 }
798
799 /*
800 * Ditto for timespec.
801 */
802 void
803 nanotime(
804 struct timespec *tsp)
805 {
806 clock_sec_t tv_sec;
807 clock_nsec_t tv_nsec;
808
809 clock_get_calendar_nanotime(&tv_sec, &tv_nsec);
810
811 tsp->tv_sec = tv_sec;
812 tsp->tv_nsec = tv_nsec;
813 }
814
815 void
816 nanouptime(
817 struct timespec *tsp)
818 {
819 clock_sec_t tv_sec;
820 clock_nsec_t tv_nsec;
821
822 clock_get_system_nanotime(&tv_sec, &tv_nsec);
823
824 tsp->tv_sec = tv_sec;
825 tsp->tv_nsec = tv_nsec;
826 }
827
828 uint64_t
829 tvtoabstime(
830 struct timeval *tvp)
831 {
832 uint64_t result, usresult;
833
834 clock_interval_to_absolutetime_interval(
835 tvp->tv_sec, NSEC_PER_SEC, &result);
836 clock_interval_to_absolutetime_interval(
837 tvp->tv_usec, NSEC_PER_USEC, &usresult);
838
839 return (result + usresult);
840 }
841
842 uint64_t
843 tstoabstime(struct timespec *ts)
844 {
845 uint64_t abstime_s, abstime_ns;
846 clock_interval_to_absolutetime_interval(ts->tv_sec, NSEC_PER_SEC, &abstime_s);
847 clock_interval_to_absolutetime_interval(ts->tv_nsec, 1, &abstime_ns);
848 return abstime_s + abstime_ns;
849 }
850
851 #if NETWORKING
852 /*
853 * ratecheck(): simple time-based rate-limit checking.
854 */
855 int
856 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
857 {
858 struct timeval tv, delta;
859 int rv = 0;
860
861 net_uptime2timeval(&tv);
862 delta = tv;
863 timevalsub(&delta, lasttime);
864
865 /*
866 * check for 0,0 is so that the message will be seen at least once,
867 * even if interval is huge.
868 */
869 if (timevalcmp(&delta, mininterval, >=) ||
870 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
871 *lasttime = tv;
872 rv = 1;
873 }
874
875 return (rv);
876 }
877
878 /*
879 * ppsratecheck(): packets (or events) per second limitation.
880 */
881 int
882 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
883 {
884 struct timeval tv, delta;
885 int rv;
886
887 net_uptime2timeval(&tv);
888
889 timersub(&tv, lasttime, &delta);
890
891 /*
892 * Check for 0,0 so that the message will be seen at least once.
893 * If more than one second has passed since the last update of
894 * lasttime, reset the counter.
895 *
896 * we do increment *curpps even in *curpps < maxpps case, as some may
897 * try to use *curpps for stat purposes as well.
898 */
899 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
900 delta.tv_sec >= 1) {
901 *lasttime = tv;
902 *curpps = 0;
903 rv = 1;
904 } else if (maxpps < 0)
905 rv = 1;
906 else if (*curpps < maxpps)
907 rv = 1;
908 else
909 rv = 0;
910
911 #if 1 /* DIAGNOSTIC? */
912 /* be careful about wrap-around */
913 if (*curpps + 1 > 0)
914 *curpps = *curpps + 1;
915 #else
916 /*
917 * assume that there's not too many calls to this function.
918 * not sure if the assumption holds, as it depends on *caller's*
919 * behavior, not the behavior of this function.
920 * IMHO it is wrong to make assumption on the caller's behavior,
921 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
922 */
923 *curpps = *curpps + 1;
924 #endif
925
926 return (rv);
927 }
928 #endif /* NETWORKING */
929
930 void
931 time_zone_slock_init(void)
932 {
933 /* allocate lock group attribute and group */
934 tz_slock_grp_attr = lck_grp_attr_alloc_init();
935
936 tz_slock_grp = lck_grp_alloc_init("tzlock", tz_slock_grp_attr);
937
938 /* Allocate lock attribute */
939 tz_slock_attr = lck_attr_alloc_init();
940
941 /* Allocate the spin lock */
942 tz_slock = lck_spin_alloc_init(tz_slock_grp, tz_slock_attr);
943 }