]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_time.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_time.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b
A
69
70#include <sys/param.h>
71#include <sys/resourcevar.h>
72#include <sys/kernel.h>
73#include <sys/systm.h>
91447636
A
74#include <sys/proc_internal.h>
75#include <sys/kauth.h>
1c79356b 76#include <sys/vnode.h>
b0d623f7 77#include <sys/time.h>
1c79356b 78
91447636
A
79#include <sys/mount_internal.h>
80#include <sys/sysproto.h>
81#include <sys/signalvar.h>
1c79356b 82
1c79356b 83#include <kern/clock.h>
2d21ac55 84#include <kern/task.h>
91447636 85#include <kern/thread_call.h>
2d21ac55
A
86#if CONFIG_MACF
87#include <security/mac_framework.h>
88#endif
1c79356b
A
89
90#define HZ 100 /* XXX */
91
9bccf70c 92/* simple lock used to access timezone, tz structure */
91447636
A
93lck_spin_t * tz_slock;
94lck_grp_t * tz_slock_grp;
95lck_attr_t * tz_slock_attr;
96lck_grp_attr_t *tz_slock_grp_attr;
97
98static void setthetime(
99 struct timeval *tv);
100
2d21ac55 101void time_zone_slock_init(void) __attribute__((section("__TEXT, initcode")));
91447636 102
1c79356b
A
103/*
104 * Time of day and interval timer support.
105 *
106 * These routines provide the kernel entry points to get and set
107 * the time-of-day and per-process interval timers. Subroutines
108 * here provide support for adding and subtracting timeval structures
109 * and decrementing interval timers, optionally reloading the interval
110 * timers when they expire.
111 */
1c79356b
A
112/* ARGSUSED */
113int
0c530ab8
A
114gettimeofday(
115__unused struct proc *p,
116 struct gettimeofday_args *uap,
b0d623f7 117 int32_t *retval)
1c79356b 118{
1c79356b 119 int error = 0;
9bccf70c
A
120 struct timezone ltz; /* local copy */
121
b0d623f7
A
122 if (uap->tp) {
123 clock_sec_t secs;
124 clock_usec_t usecs;
125
126 clock_gettimeofday(&secs, &usecs);
127 retval[0] = secs;
128 retval[1] = usecs;
129 }
1c79356b 130
9bccf70c 131 if (uap->tzp) {
91447636 132 lck_spin_lock(tz_slock);
9bccf70c 133 ltz = tz;
91447636 134 lck_spin_unlock(tz_slock);
0c530ab8
A
135
136 error = copyout((caddr_t)&ltz, CAST_USER_ADDR_T(uap->tzp), sizeof (tz));
9bccf70c 137 }
1c79356b 138
0c530ab8 139 return (error);
1c79356b
A
140}
141
91447636
A
142/*
143 * XXX Y2038 bug because of setthetime() argument
144 */
1c79356b
A
145/* ARGSUSED */
146int
b0d623f7 147settimeofday(__unused struct proc *p, struct settimeofday_args *uap, __unused int32_t *retval)
1c79356b
A
148{
149 struct timeval atv;
150 struct timezone atz;
91447636 151 int error;
1c79356b 152
b0d623f7
A
153 bzero(&atv, sizeof(atv));
154
2d21ac55
A
155#if CONFIG_MACF
156 error = mac_system_check_settime(kauth_cred_get());
157 if (error)
158 return (error);
159#endif
160#ifndef CONFIG_EMBEDDED
91447636 161 if ((error = suser(kauth_cred_get(), &p->p_acflag)))
1c79356b 162 return (error);
2d21ac55 163#endif
91447636
A
164 /* Verify all parameters before changing time */
165 if (uap->tv) {
166 if (IS_64BIT_PROCESS(p)) {
b0d623f7
A
167 struct user64_timeval user_atv;
168 error = copyin(uap->tv, &user_atv, sizeof(user_atv));
91447636
A
169 atv.tv_sec = user_atv.tv_sec;
170 atv.tv_usec = user_atv.tv_usec;
171 } else {
b0d623f7
A
172 struct user32_timeval user_atv;
173 error = copyin(uap->tv, &user_atv, sizeof(user_atv));
174 atv.tv_sec = user_atv.tv_sec;
175 atv.tv_usec = user_atv.tv_usec;
91447636
A
176 }
177 if (error)
178 return (error);
179 }
180 if (uap->tzp && (error = copyin(uap->tzp, (caddr_t)&atz, sizeof(atz))))
1c79356b 181 return (error);
91447636
A
182 if (uap->tv) {
183 timevalfix(&atv);
184 if (atv.tv_sec < 0 || (atv.tv_sec == 0 && atv.tv_usec < 0))
185 return (EPERM);
1c79356b 186 setthetime(&atv);
91447636 187 }
9bccf70c 188 if (uap->tzp) {
91447636 189 lck_spin_lock(tz_slock);
1c79356b 190 tz = atz;
91447636 191 lck_spin_unlock(tz_slock);
9bccf70c 192 }
1c79356b
A
193 return (0);
194}
195
91447636
A
196static void
197setthetime(
198 struct timeval *tv)
1c79356b 199{
55e303ae 200 clock_set_calendar_microtime(tv->tv_sec, tv->tv_usec);
1c79356b
A
201}
202
91447636
A
203/*
204 * XXX Y2038 bug because of clock_adjtime() first argument
205 */
1c79356b
A
206/* ARGSUSED */
207int
b0d623f7 208adjtime(struct proc *p, struct adjtime_args *uap, __unused int32_t *retval)
1c79356b 209{
9bccf70c 210 struct timeval atv;
9bccf70c 211 int error;
1c79356b 212
2d21ac55
A
213#if CONFIG_MACF
214 error = mac_system_check_settime(kauth_cred_get());
215 if (error)
216 return (error);
217#endif
91447636 218 if ((error = suser(kauth_cred_get(), &p->p_acflag)))
1c79356b 219 return (error);
91447636 220 if (IS_64BIT_PROCESS(p)) {
b0d623f7
A
221 struct user64_timeval user_atv;
222 error = copyin(uap->delta, &user_atv, sizeof(user_atv));
91447636
A
223 atv.tv_sec = user_atv.tv_sec;
224 atv.tv_usec = user_atv.tv_usec;
225 } else {
b0d623f7
A
226 struct user32_timeval user_atv;
227 error = copyin(uap->delta, &user_atv, sizeof(user_atv));
228 atv.tv_sec = user_atv.tv_sec;
229 atv.tv_usec = user_atv.tv_usec;
91447636
A
230 }
231 if (error)
9bccf70c 232 return (error);
1c79356b 233
91447636
A
234 /*
235 * Compute the total correction and the rate at which to apply it.
236 */
b0d623f7 237 clock_adjtime(&atv.tv_sec, &atv.tv_usec);
1c79356b 238
1c79356b 239 if (uap->olddelta) {
91447636 240 if (IS_64BIT_PROCESS(p)) {
b0d623f7 241 struct user64_timeval user_atv;
91447636
A
242 user_atv.tv_sec = atv.tv_sec;
243 user_atv.tv_usec = atv.tv_usec;
b0d623f7 244 error = copyout(&user_atv, uap->olddelta, sizeof(user_atv));
91447636 245 } else {
b0d623f7
A
246 struct user32_timeval user_atv;
247 user_atv.tv_sec = atv.tv_sec;
248 user_atv.tv_usec = atv.tv_usec;
249 error = copyout(&user_atv, uap->olddelta, sizeof(user_atv));
91447636 250 }
1c79356b 251 }
1c79356b 252
9bccf70c 253 return (0);
1c79356b
A
254}
255
1c79356b 256/*
91447636
A
257 * Verify the calendar value. If negative,
258 * reset to zero (the epoch).
1c79356b
A
259 */
260void
91447636
A
261inittodr(
262 __unused time_t base)
1c79356b 263{
55e303ae
A
264 struct timeval tv;
265
1c79356b 266 /*
0b4e3aa0
A
267 * Assertion:
268 * The calendar has already been
91447636 269 * set up from the platform clock.
0b4e3aa0 270 *
1c79356b
A
271 * The value returned by microtime()
272 * is gotten from the calendar.
273 */
55e303ae 274 microtime(&tv);
1c79356b 275
91447636 276 if (tv.tv_sec < 0 || tv.tv_usec < 0) {
1c79356b 277 printf ("WARNING: preposterous time in Real Time Clock");
91447636
A
278 tv.tv_sec = 0; /* the UNIX epoch */
279 tv.tv_usec = 0;
280 setthetime(&tv);
1c79356b
A
281 printf(" -- CHECK AND RESET THE DATE!\n");
282 }
1c79356b
A
283}
284
91447636
A
285time_t
286boottime_sec(void)
287{
b0d623f7
A
288 clock_sec_t secs;
289 clock_nsec_t nanosecs;
9bccf70c 290
b0d623f7
A
291 clock_get_boottime_nanotime(&secs, &nanosecs);
292 return (secs);
293}
9bccf70c 294
1c79356b
A
295/*
296 * Get value of an interval timer. The process virtual and
9bccf70c 297 * profiling virtual time timers are kept internally in the
1c79356b
A
298 * way they are specified externally: in time until they expire.
299 *
9bccf70c
A
300 * The real time interval timer expiration time (p_rtime)
301 * is kept as an absolute time rather than as a delta, so that
302 * it is easy to keep periodic real-time signals from drifting.
1c79356b 303 *
2d21ac55
A
304 * The real time timer is processed by a callout routine.
305 * Since a callout may be delayed in real time due to
9bccf70c
A
306 * other processing in the system, it is possible for the real
307 * time callout routine (realitexpire, given below), to be delayed
308 * in real time past when it is supposed to occur. It does not
309 * suffice, therefore, to reload the real time .it_value from the
310 * real time .it_interval. Rather, we compute the next time in
311 * absolute time when the timer should go off.
2d21ac55
A
312 *
313 * Returns: 0 Success
314 * EINVAL Invalid argument
315 * copyout:EFAULT Bad address
1c79356b 316 */
1c79356b
A
317/* ARGSUSED */
318int
b0d623f7 319getitimer(struct proc *p, struct getitimer_args *uap, __unused int32_t *retval)
1c79356b
A
320{
321 struct itimerval aitv;
1c79356b
A
322
323 if (uap->which > ITIMER_PROF)
324 return(EINVAL);
2d21ac55 325
b0d623f7
A
326 bzero(&aitv, sizeof(aitv));
327
2d21ac55
A
328 proc_spinlock(p);
329 switch (uap->which) {
330
331 case ITIMER_REAL:
1c79356b 332 /*
9bccf70c
A
333 * If time for real time timer has passed return 0,
334 * else return difference between current time and
335 * time for the timer to go off.
1c79356b
A
336 */
337 aitv = p->p_realtimer;
9bccf70c
A
338 if (timerisset(&p->p_rtime)) {
339 struct timeval now;
340
341 microuptime(&now);
342 if (timercmp(&p->p_rtime, &now, <))
1c79356b 343 timerclear(&aitv.it_value);
9bccf70c
A
344 else {
345 aitv.it_value = p->p_rtime;
346 timevalsub(&aitv.it_value, &now);
347 }
348 }
349 else
350 timerclear(&aitv.it_value);
2d21ac55
A
351 break;
352
353 case ITIMER_VIRTUAL:
354 aitv = p->p_vtimer_user;
355 break;
356
357 case ITIMER_PROF:
358 aitv = p->p_vtimer_prof;
359 break;
9bccf70c 360 }
2d21ac55
A
361
362 proc_spinunlock(p);
9bccf70c 363
91447636 364 if (IS_64BIT_PROCESS(p)) {
b0d623f7 365 struct user64_itimerval user_itv;
91447636
A
366 user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec;
367 user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec;
368 user_itv.it_value.tv_sec = aitv.it_value.tv_sec;
369 user_itv.it_value.tv_usec = aitv.it_value.tv_usec;
b0d623f7 370 return (copyout((caddr_t)&user_itv, uap->itv, sizeof (user_itv)));
91447636 371 } else {
b0d623f7
A
372 struct user32_itimerval user_itv;
373 user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec;
374 user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec;
375 user_itv.it_value.tv_sec = aitv.it_value.tv_sec;
376 user_itv.it_value.tv_usec = aitv.it_value.tv_usec;
377 return (copyout((caddr_t)&user_itv, uap->itv, sizeof (user_itv)));
91447636 378 }
1c79356b
A
379}
380
2d21ac55
A
381/*
382 * Returns: 0 Success
383 * EINVAL Invalid argument
384 * copyin:EFAULT Bad address
385 * getitimer:EINVAL Invalid argument
386 * getitimer:EFAULT Bad address
387 */
1c79356b
A
388/* ARGSUSED */
389int
b0d623f7 390setitimer(struct proc *p, struct setitimer_args *uap, int32_t *retval)
1c79356b
A
391{
392 struct itimerval aitv;
91447636 393 user_addr_t itvp;
9bccf70c 394 int error;
1c79356b 395
b0d623f7
A
396 bzero(&aitv, sizeof(aitv));
397
1c79356b 398 if (uap->which > ITIMER_PROF)
9bccf70c 399 return (EINVAL);
91447636
A
400 if ((itvp = uap->itv)) {
401 if (IS_64BIT_PROCESS(p)) {
b0d623f7
A
402 struct user64_itimerval user_itv;
403 if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof (user_itv))))
91447636
A
404 return (error);
405 aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec;
406 aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec;
407 aitv.it_value.tv_sec = user_itv.it_value.tv_sec;
408 aitv.it_value.tv_usec = user_itv.it_value.tv_usec;
409 } else {
b0d623f7
A
410 struct user32_itimerval user_itv;
411 if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof (user_itv))))
91447636 412 return (error);
b0d623f7
A
413 aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec;
414 aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec;
415 aitv.it_value.tv_sec = user_itv.it_value.tv_sec;
416 aitv.it_value.tv_usec = user_itv.it_value.tv_usec;
91447636
A
417 }
418 }
419 if ((uap->itv = uap->oitv) && (error = getitimer(p, (struct getitimer_args *)uap, retval)))
1c79356b
A
420 return (error);
421 if (itvp == 0)
422 return (0);
423 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
424 return (EINVAL);
2d21ac55
A
425
426 switch (uap->which) {
427
428 case ITIMER_REAL:
429 proc_spinlock(p);
1c79356b 430 if (timerisset(&aitv.it_value)) {
9bccf70c
A
431 microuptime(&p->p_rtime);
432 timevaladd(&p->p_rtime, &aitv.it_value);
2d21ac55
A
433 p->p_realtimer = aitv;
434 if (!thread_call_enter_delayed(p->p_rcall, tvtoabstime(&p->p_rtime)))
435 p->p_ractive++;
436 } else {
437 timerclear(&p->p_rtime);
438 p->p_realtimer = aitv;
439 if (thread_call_cancel(p->p_rcall))
440 p->p_ractive--;
1c79356b 441 }
2d21ac55
A
442 proc_spinunlock(p);
443
444 break;
445
446
447 case ITIMER_VIRTUAL:
448 if (timerisset(&aitv.it_value))
449 task_vtimer_set(p->task, TASK_VTIMER_USER);
450 else
451 task_vtimer_clear(p->task, TASK_VTIMER_USER);
452
453 proc_spinlock(p);
454 p->p_vtimer_user = aitv;
455 proc_spinunlock(p);
456 break;
457
458 case ITIMER_PROF:
459 if (timerisset(&aitv.it_value))
460 task_vtimer_set(p->task, TASK_VTIMER_PROF);
9bccf70c 461 else
2d21ac55 462 task_vtimer_clear(p->task, TASK_VTIMER_PROF);
9bccf70c 463
2d21ac55
A
464 proc_spinlock(p);
465 p->p_vtimer_prof = aitv;
466 proc_spinunlock(p);
467 break;
9bccf70c 468 }
9bccf70c
A
469
470 return (0);
1c79356b
A
471}
472
473/*
474 * Real interval timer expired:
475 * send process whose timer expired an alarm signal.
476 * If time is not set up to reload, then just return.
477 * Else compute next time timer should go off which is > current time.
478 * This is where delay in processing this timeout causes multiple
479 * SIGALRM calls to be compressed into one.
480 */
481void
9bccf70c 482realitexpire(
2d21ac55 483 struct proc *p)
1c79356b 484{
2d21ac55
A
485 struct proc *r;
486 struct timeval t;
487
488 r = proc_find(p->p_pid);
489
490 proc_spinlock(p);
491
492 if (--p->p_ractive > 0 || r != p) {
493 proc_spinunlock(p);
494
495 if (r != NULL)
496 proc_rele(r);
1c79356b
A
497 return;
498 }
2d21ac55 499
9bccf70c
A
500 if (!timerisset(&p->p_realtimer.it_interval)) {
501 timerclear(&p->p_rtime);
2d21ac55 502 proc_spinunlock(p);
9bccf70c 503
2d21ac55
A
504 psignal(p, SIGALRM);
505 proc_rele(p);
1c79356b 506 return;
1c79356b 507 }
9bccf70c 508
2d21ac55 509 microuptime(&t);
9bccf70c 510 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
2d21ac55
A
511 if (timercmp(&p->p_rtime, &t, <=)) {
512 if ((p->p_rtime.tv_sec + 2) >= t.tv_sec) {
9bccf70c
A
513 for (;;) {
514 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
2d21ac55 515 if (timercmp(&p->p_rtime, &t, >))
9bccf70c
A
516 break;
517 }
518 }
519 else {
520 p->p_rtime = p->p_realtimer.it_interval;
2d21ac55 521 timevaladd(&p->p_rtime, &t);
1c79356b 522 }
1c79356b 523 }
9bccf70c 524
2d21ac55
A
525 if (!thread_call_enter_delayed(p->p_rcall, tvtoabstime(&p->p_rtime)))
526 p->p_ractive++;
527 proc_spinunlock(p);
55e303ae 528
2d21ac55
A
529 psignal(p, SIGALRM);
530 proc_rele(p);
1c79356b
A
531}
532
533/*
534 * Check that a proposed value to load into the .it_value or
2d21ac55 535 * .it_interval part of an interval timer is acceptable.
1c79356b
A
536 */
537int
2d21ac55
A
538itimerfix(
539 struct timeval *tv)
1c79356b
A
540{
541
542 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
543 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
544 return (EINVAL);
1c79356b
A
545 return (0);
546}
547
548/*
549 * Decrement an interval timer by a specified number
550 * of microseconds, which must be less than a second,
551 * i.e. < 1000000. If the timer expires, then reload
552 * it. In this case, carry over (usec - old value) to
2d21ac55 553 * reduce the value reloaded into the timer so that
1c79356b
A
554 * the timer does not drift. This routine assumes
555 * that it is called in a context where the timers
556 * on which it is operating cannot change in value.
557 */
558int
2d21ac55
A
559itimerdecr(proc_t p,
560 struct itimerval *itp, int usec)
1c79356b
A
561{
562
2d21ac55
A
563 proc_spinlock(p);
564
1c79356b
A
565 if (itp->it_value.tv_usec < usec) {
566 if (itp->it_value.tv_sec == 0) {
567 /* expired, and already in next interval */
568 usec -= itp->it_value.tv_usec;
569 goto expire;
570 }
571 itp->it_value.tv_usec += 1000000;
572 itp->it_value.tv_sec--;
573 }
574 itp->it_value.tv_usec -= usec;
575 usec = 0;
2d21ac55
A
576 if (timerisset(&itp->it_value)) {
577 proc_spinunlock(p);
1c79356b 578 return (1);
2d21ac55 579 }
1c79356b
A
580 /* expired, exactly at end of interval */
581expire:
582 if (timerisset(&itp->it_interval)) {
583 itp->it_value = itp->it_interval;
2d21ac55 584 if (itp->it_value.tv_sec > 0) {
1c79356b
A
585 itp->it_value.tv_usec -= usec;
586 if (itp->it_value.tv_usec < 0) {
587 itp->it_value.tv_usec += 1000000;
588 itp->it_value.tv_sec--;
2d21ac55 589 }
1c79356b
A
590 }
591 } else
592 itp->it_value.tv_usec = 0; /* sec is already 0 */
2d21ac55 593 proc_spinunlock(p);
1c79356b
A
594 return (0);
595}
596
597/*
598 * Add and subtract routines for timevals.
599 * N.B.: subtract routine doesn't deal with
600 * results which are before the beginning,
601 * it just gets very confused in this case.
602 * Caveat emptor.
603 */
604void
9bccf70c
A
605timevaladd(
606 struct timeval *t1,
607 struct timeval *t2)
1c79356b
A
608{
609
610 t1->tv_sec += t2->tv_sec;
611 t1->tv_usec += t2->tv_usec;
612 timevalfix(t1);
613}
614void
9bccf70c
A
615timevalsub(
616 struct timeval *t1,
617 struct timeval *t2)
1c79356b
A
618{
619
620 t1->tv_sec -= t2->tv_sec;
621 t1->tv_usec -= t2->tv_usec;
622 timevalfix(t1);
623}
624void
9bccf70c
A
625timevalfix(
626 struct timeval *t1)
1c79356b
A
627{
628
629 if (t1->tv_usec < 0) {
630 t1->tv_sec--;
631 t1->tv_usec += 1000000;
632 }
633 if (t1->tv_usec >= 1000000) {
634 t1->tv_sec++;
635 t1->tv_usec -= 1000000;
636 }
637}
638
639/*
640 * Return the best possible estimate of the time in the timeval
641 * to which tvp points.
642 */
643void
9bccf70c
A
644microtime(
645 struct timeval *tvp)
1c79356b 646{
b0d623f7
A
647 clock_sec_t tv_sec;
648 clock_usec_t tv_usec;
649
650 clock_get_calendar_microtime(&tv_sec, &tv_usec);
651
652 tvp->tv_sec = tv_sec;
653 tvp->tv_usec = tv_usec;
1c79356b 654}
9bccf70c
A
655
656void
657microuptime(
658 struct timeval *tvp)
659{
b0d623f7
A
660 clock_sec_t tv_sec;
661 clock_usec_t tv_usec;
662
663 clock_get_system_microtime(&tv_sec, &tv_usec);
664
665 tvp->tv_sec = tv_sec;
666 tvp->tv_usec = tv_usec;
9bccf70c
A
667}
668
669/*
670 * Ditto for timespec.
671 */
672void
673nanotime(
674 struct timespec *tsp)
675{
b0d623f7
A
676 clock_sec_t tv_sec;
677 clock_nsec_t tv_nsec;
678
679 clock_get_calendar_nanotime(&tv_sec, &tv_nsec);
680
681 tsp->tv_sec = tv_sec;
682 tsp->tv_nsec = tv_nsec;
9bccf70c
A
683}
684
685void
686nanouptime(
687 struct timespec *tsp)
688{
b0d623f7
A
689 clock_sec_t tv_sec;
690 clock_nsec_t tv_nsec;
691
692 clock_get_system_nanotime(&tv_sec, &tv_nsec);
693
694 tsp->tv_sec = tv_sec;
695 tsp->tv_nsec = tv_nsec;
9bccf70c
A
696}
697
698uint64_t
699tvtoabstime(
700 struct timeval *tvp)
701{
702 uint64_t result, usresult;
703
704 clock_interval_to_absolutetime_interval(
705 tvp->tv_sec, NSEC_PER_SEC, &result);
706 clock_interval_to_absolutetime_interval(
707 tvp->tv_usec, NSEC_PER_USEC, &usresult);
708
709 return (result + usresult);
710}
711void
712time_zone_slock_init(void)
713{
91447636
A
714 /* allocate lock group attribute and group */
715 tz_slock_grp_attr = lck_grp_attr_alloc_init();
9bccf70c 716
91447636 717 tz_slock_grp = lck_grp_alloc_init("tzlock", tz_slock_grp_attr);
9bccf70c 718
91447636
A
719 /* Allocate lock attribute */
720 tz_slock_attr = lck_attr_alloc_init();
9bccf70c 721
91447636
A
722 /* Allocate the spin lock */
723 tz_slock = lck_spin_alloc_init(tz_slock_grp, tz_slock_attr);
9bccf70c 724}