2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
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,
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>
79 #include <sys/mount_internal.h>
80 #include <sys/sysproto.h>
81 #include <sys/signalvar.h>
83 #include <kern/clock.h>
84 #include <kern/task.h>
85 #include <kern/thread_call.h>
87 #include <security/mac_framework.h>
90 #define HZ 100 /* XXX */
92 /* simple lock used to access timezone, tz structure */
93 lck_spin_t
* tz_slock
;
94 lck_grp_t
* tz_slock_grp
;
95 lck_attr_t
* tz_slock_attr
;
96 lck_grp_attr_t
*tz_slock_grp_attr
;
98 static void setthetime(
101 void time_zone_slock_init(void) __attribute__((section("__TEXT, initcode")));
104 * Time of day and interval timer support.
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.
115 __unused
struct proc
*p
,
116 struct gettimeofday_args
*uap
,
120 struct timezone ltz
; /* local copy */
126 clock_gettimeofday(&secs
, &usecs
);
132 lck_spin_lock(tz_slock
);
134 lck_spin_unlock(tz_slock
);
136 error
= copyout((caddr_t
)<z
, CAST_USER_ADDR_T(uap
->tzp
), sizeof (tz
));
143 * XXX Y2038 bug because of setthetime() argument
147 settimeofday(__unused
struct proc
*p
, struct settimeofday_args
*uap
, __unused
int32_t *retval
)
153 bzero(&atv
, sizeof(atv
));
156 error
= mac_system_check_settime(kauth_cred_get());
160 #ifndef CONFIG_EMBEDDED
161 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
164 /* Verify all parameters before changing time */
166 if (IS_64BIT_PROCESS(p
)) {
167 struct user64_timeval user_atv
;
168 error
= copyin(uap
->tv
, &user_atv
, sizeof(user_atv
));
169 atv
.tv_sec
= user_atv
.tv_sec
;
170 atv
.tv_usec
= user_atv
.tv_usec
;
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
;
180 if (uap
->tzp
&& (error
= copyin(uap
->tzp
, (caddr_t
)&atz
, sizeof(atz
))))
184 if (atv
.tv_sec
< 0 || (atv
.tv_sec
== 0 && atv
.tv_usec
< 0))
189 lck_spin_lock(tz_slock
);
191 lck_spin_unlock(tz_slock
);
200 clock_set_calendar_microtime(tv
->tv_sec
, tv
->tv_usec
);
204 * XXX Y2038 bug because of clock_adjtime() first argument
208 adjtime(struct proc
*p
, struct adjtime_args
*uap
, __unused
int32_t *retval
)
214 error
= mac_system_check_settime(kauth_cred_get());
218 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
220 if (IS_64BIT_PROCESS(p
)) {
221 struct user64_timeval user_atv
;
222 error
= copyin(uap
->delta
, &user_atv
, sizeof(user_atv
));
223 atv
.tv_sec
= user_atv
.tv_sec
;
224 atv
.tv_usec
= user_atv
.tv_usec
;
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
;
235 * Compute the total correction and the rate at which to apply it.
237 clock_adjtime(&atv
.tv_sec
, &atv
.tv_usec
);
240 if (IS_64BIT_PROCESS(p
)) {
241 struct user64_timeval user_atv
;
242 user_atv
.tv_sec
= atv
.tv_sec
;
243 user_atv
.tv_usec
= atv
.tv_usec
;
244 error
= copyout(&user_atv
, uap
->olddelta
, sizeof(user_atv
));
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
));
257 * Verify the calendar value. If negative,
258 * reset to zero (the epoch).
262 __unused
time_t base
)
268 * The calendar has already been
269 * set up from the platform clock.
271 * The value returned by microtime()
272 * is gotten from the calendar.
276 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0) {
277 printf ("WARNING: preposterous time in Real Time Clock");
278 tv
.tv_sec
= 0; /* the UNIX epoch */
281 printf(" -- CHECK AND RESET THE DATE!\n");
289 clock_nsec_t nanosecs
;
291 clock_get_boottime_nanotime(&secs
, &nanosecs
);
296 * Get value of an interval timer. The process virtual and
297 * profiling virtual time timers are kept internally in the
298 * way they are specified externally: in time until they expire.
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.
304 * The real time timer is processed by a callout routine.
305 * Since a callout may be delayed in real time due to
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.
314 * EINVAL Invalid argument
315 * copyout:EFAULT Bad address
319 getitimer(struct proc
*p
, struct getitimer_args
*uap
, __unused
int32_t *retval
)
321 struct itimerval aitv
;
323 if (uap
->which
> ITIMER_PROF
)
326 bzero(&aitv
, sizeof(aitv
));
329 switch (uap
->which
) {
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.
337 aitv
= p
->p_realtimer
;
338 if (timerisset(&p
->p_rtime
)) {
342 if (timercmp(&p
->p_rtime
, &now
, <))
343 timerclear(&aitv
.it_value
);
345 aitv
.it_value
= p
->p_rtime
;
346 timevalsub(&aitv
.it_value
, &now
);
350 timerclear(&aitv
.it_value
);
354 aitv
= p
->p_vtimer_user
;
358 aitv
= p
->p_vtimer_prof
;
364 if (IS_64BIT_PROCESS(p
)) {
365 struct user64_itimerval user_itv
;
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
;
370 return (copyout((caddr_t
)&user_itv
, uap
->itv
, sizeof (user_itv
)));
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
)));
383 * EINVAL Invalid argument
384 * copyin:EFAULT Bad address
385 * getitimer:EINVAL Invalid argument
386 * getitimer:EFAULT Bad address
390 setitimer(struct proc
*p
, struct setitimer_args
*uap
, int32_t *retval
)
392 struct itimerval aitv
;
396 bzero(&aitv
, sizeof(aitv
));
398 if (uap
->which
> ITIMER_PROF
)
400 if ((itvp
= uap
->itv
)) {
401 if (IS_64BIT_PROCESS(p
)) {
402 struct user64_itimerval user_itv
;
403 if ((error
= copyin(itvp
, (caddr_t
)&user_itv
, sizeof (user_itv
))))
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
;
410 struct user32_itimerval user_itv
;
411 if ((error
= copyin(itvp
, (caddr_t
)&user_itv
, sizeof (user_itv
))))
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
;
419 if ((uap
->itv
= uap
->oitv
) && (error
= getitimer(p
, (struct getitimer_args
*)uap
, retval
)))
423 if (itimerfix(&aitv
.it_value
) || itimerfix(&aitv
.it_interval
))
426 switch (uap
->which
) {
430 if (timerisset(&aitv
.it_value
)) {
431 microuptime(&p
->p_rtime
);
432 timevaladd(&p
->p_rtime
, &aitv
.it_value
);
433 p
->p_realtimer
= aitv
;
434 if (!thread_call_enter_delayed(p
->p_rcall
, tvtoabstime(&p
->p_rtime
)))
437 timerclear(&p
->p_rtime
);
438 p
->p_realtimer
= aitv
;
439 if (thread_call_cancel(p
->p_rcall
))
448 if (timerisset(&aitv
.it_value
))
449 task_vtimer_set(p
->task
, TASK_VTIMER_USER
);
451 task_vtimer_clear(p
->task
, TASK_VTIMER_USER
);
454 p
->p_vtimer_user
= aitv
;
459 if (timerisset(&aitv
.it_value
))
460 task_vtimer_set(p
->task
, TASK_VTIMER_PROF
);
462 task_vtimer_clear(p
->task
, TASK_VTIMER_PROF
);
465 p
->p_vtimer_prof
= aitv
;
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.
488 r
= proc_find(p
->p_pid
);
492 if (--p
->p_ractive
> 0 || r
!= p
) {
500 if (!timerisset(&p
->p_realtimer
.it_interval
)) {
501 timerclear(&p
->p_rtime
);
510 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
511 if (timercmp(&p
->p_rtime
, &t
, <=)) {
512 if ((p
->p_rtime
.tv_sec
+ 2) >= t
.tv_sec
) {
514 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
515 if (timercmp(&p
->p_rtime
, &t
, >))
520 p
->p_rtime
= p
->p_realtimer
.it_interval
;
521 timevaladd(&p
->p_rtime
, &t
);
525 if (!thread_call_enter_delayed(p
->p_rcall
, tvtoabstime(&p
->p_rtime
)))
534 * Check that a proposed value to load into the .it_value or
535 * .it_interval part of an interval timer is acceptable.
542 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
543 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
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
553 * reduce the value reloaded into the timer so that
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.
560 struct itimerval
*itp
, int usec
)
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
;
571 itp
->it_value
.tv_usec
+= 1000000;
572 itp
->it_value
.tv_sec
--;
574 itp
->it_value
.tv_usec
-= usec
;
576 if (timerisset(&itp
->it_value
)) {
580 /* expired, exactly at end of interval */
582 if (timerisset(&itp
->it_interval
)) {
583 itp
->it_value
= itp
->it_interval
;
584 if (itp
->it_value
.tv_sec
> 0) {
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
--;
592 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
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.
610 t1
->tv_sec
+= t2
->tv_sec
;
611 t1
->tv_usec
+= t2
->tv_usec
;
620 t1
->tv_sec
-= t2
->tv_sec
;
621 t1
->tv_usec
-= t2
->tv_usec
;
629 if (t1
->tv_usec
< 0) {
631 t1
->tv_usec
+= 1000000;
633 if (t1
->tv_usec
>= 1000000) {
635 t1
->tv_usec
-= 1000000;
640 * Return the best possible estimate of the time in the timeval
641 * to which tvp points.
648 clock_usec_t tv_usec
;
650 clock_get_calendar_microtime(&tv_sec
, &tv_usec
);
652 tvp
->tv_sec
= tv_sec
;
653 tvp
->tv_usec
= tv_usec
;
661 clock_usec_t tv_usec
;
663 clock_get_system_microtime(&tv_sec
, &tv_usec
);
665 tvp
->tv_sec
= tv_sec
;
666 tvp
->tv_usec
= tv_usec
;
670 * Ditto for timespec.
674 struct timespec
*tsp
)
677 clock_nsec_t tv_nsec
;
679 clock_get_calendar_nanotime(&tv_sec
, &tv_nsec
);
681 tsp
->tv_sec
= tv_sec
;
682 tsp
->tv_nsec
= tv_nsec
;
687 struct timespec
*tsp
)
690 clock_nsec_t tv_nsec
;
692 clock_get_system_nanotime(&tv_sec
, &tv_nsec
);
694 tsp
->tv_sec
= tv_sec
;
695 tsp
->tv_nsec
= tv_nsec
;
702 uint64_t result
, usresult
;
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
);
709 return (result
+ usresult
);
712 time_zone_slock_init(void)
714 /* allocate lock group attribute and group */
715 tz_slock_grp_attr
= lck_grp_attr_alloc_init();
717 tz_slock_grp
= lck_grp_alloc_init("tzlock", tz_slock_grp_attr
);
719 /* Allocate lock attribute */
720 tz_slock_attr
= lck_attr_alloc_init();
722 /* Allocate the spin lock */
723 tz_slock
= lck_spin_alloc_init(tz_slock_grp
, tz_slock_attr
);