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