]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_time.c
af3e1a9ca18f37ef5c086dff34a585bd3b8ab01e
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
66 #include <sys/param.h>
67 #include <sys/resourcevar.h>
68 #include <sys/kernel.h>
69 #include <sys/systm.h>
70 #include <sys/proc_internal.h>
71 #include <sys/kauth.h>
72 #include <sys/vnode.h>
74 #include <sys/mount_internal.h>
75 #include <sys/sysproto.h>
76 #include <sys/signalvar.h>
78 #include <kern/clock.h>
79 #include <kern/thread_call.h>
81 #define HZ 100 /* XXX */
83 /* simple lock used to access timezone, tz structure */
84 lck_spin_t
* tz_slock
;
85 lck_grp_t
* tz_slock_grp
;
86 lck_attr_t
* tz_slock_attr
;
87 lck_grp_attr_t
*tz_slock_grp_attr
;
89 static void setthetime(
92 void time_zone_slock_init(void);
94 int gettimeofday(struct proc
*p
,
96 struct ppc_gettimeofday_args
*uap
,
98 struct gettimeofday_args
*uap
,
103 * Time of day and interval timer support.
105 * These routines provide the kernel entry points to get and set
106 * the time-of-day and per-process interval timers. Subroutines
107 * here provide support for adding and subtracting timeval structures
108 * and decrementing interval timers, optionally reloading the interval
109 * timers when they expire.
111 * XXX Y2038 bug because of clock_get_calendar_microtime() first argument
115 gettimeofday(__unused
struct proc
*p
,
117 register struct ppc_gettimeofday_args
*uap
,
119 register struct gettimeofday_args
*uap
,
121 __unused register_t
*retval
)
125 struct timezone ltz
; /* local copy */
127 /* NOTE THIS implementation is for non ppc architectures only */
130 clock_get_calendar_microtime((uint32_t *)&atv
.tv_sec
, &atv
.tv_usec
);
131 if (IS_64BIT_PROCESS(p
)) {
132 struct user_timeval user_atv
;
133 user_atv
.tv_sec
= atv
.tv_sec
;
134 user_atv
.tv_usec
= atv
.tv_usec
;
136 * This cast is not necessary for PPC, but is
139 error
= copyout(&user_atv
, CAST_USER_ADDR_T(uap
->tp
), sizeof(struct user_timeval
));
141 error
= copyout(&atv
, CAST_USER_ADDR_T(uap
->tp
), sizeof(struct timeval
));
148 lck_spin_lock(tz_slock
);
150 lck_spin_unlock(tz_slock
);
151 error
= copyout((caddr_t
)<z
, CAST_USER_ADDR_T(uap
->tzp
),
159 * XXX Y2038 bug because of setthetime() argument
163 settimeofday(struct proc
*p
, struct settimeofday_args
*uap
, __unused register_t
*retval
)
169 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
171 /* Verify all parameters before changing time */
173 if (IS_64BIT_PROCESS(p
)) {
174 struct user_timeval user_atv
;
175 error
= copyin(uap
->tv
, &user_atv
, sizeof(struct user_timeval
));
176 atv
.tv_sec
= user_atv
.tv_sec
;
177 atv
.tv_usec
= user_atv
.tv_usec
;
179 error
= copyin(uap
->tv
, &atv
, sizeof(struct timeval
));
184 if (uap
->tzp
&& (error
= copyin(uap
->tzp
, (caddr_t
)&atz
, sizeof(atz
))))
188 if (atv
.tv_sec
< 0 || (atv
.tv_sec
== 0 && atv
.tv_usec
< 0))
193 lck_spin_lock(tz_slock
);
195 lck_spin_unlock(tz_slock
);
204 clock_set_calendar_microtime(tv
->tv_sec
, tv
->tv_usec
);
208 * XXX Y2038 bug because of clock_adjtime() first argument
212 adjtime(struct proc
*p
, register struct adjtime_args
*uap
, __unused register_t
*retval
)
217 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
219 if (IS_64BIT_PROCESS(p
)) {
220 struct user_timeval user_atv
;
221 error
= copyin(uap
->delta
, &user_atv
, sizeof(struct user_timeval
));
222 atv
.tv_sec
= user_atv
.tv_sec
;
223 atv
.tv_usec
= user_atv
.tv_usec
;
225 error
= copyin(uap
->delta
, &atv
, sizeof(struct timeval
));
231 * Compute the total correction and the rate at which to apply it.
233 clock_adjtime((int32_t *)&atv
.tv_sec
, &atv
.tv_usec
);
236 if (IS_64BIT_PROCESS(p
)) {
237 struct user_timeval user_atv
;
238 user_atv
.tv_sec
= atv
.tv_sec
;
239 user_atv
.tv_usec
= atv
.tv_usec
;
240 error
= copyout(&user_atv
, uap
->olddelta
, sizeof(struct user_timeval
));
242 error
= copyout(&atv
, uap
->olddelta
, sizeof(struct timeval
));
250 * Verify the calendar value. If negative,
251 * reset to zero (the epoch).
255 __unused
time_t base
)
261 * The calendar has already been
262 * set up from the platform clock.
264 * The value returned by microtime()
265 * is gotten from the calendar.
269 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0) {
270 printf ("WARNING: preposterous time in Real Time Clock");
271 tv
.tv_sec
= 0; /* the UNIX epoch */
274 printf(" -- CHECK AND RESET THE DATE!\n");
281 uint32_t sec
, nanosec
;
282 clock_get_boottime_nanotime(&sec
, &nanosec
);
286 uint64_t tvtoabstime(struct timeval
*tvp
);
289 * Get value of an interval timer. The process virtual and
290 * profiling virtual time timers are kept internally in the
291 * way they are specified externally: in time until they expire.
293 * The real time interval timer expiration time (p_rtime)
294 * is kept as an absolute time rather than as a delta, so that
295 * it is easy to keep periodic real-time signals from drifting.
297 * Virtual time timers are processed in the hardclock() routine of
298 * kern_clock.c. The real time timer is processed by a callout
299 * routine. Since a callout may be delayed in real time due to
300 * other processing in the system, it is possible for the real
301 * time callout routine (realitexpire, given below), to be delayed
302 * in real time past when it is supposed to occur. It does not
303 * suffice, therefore, to reload the real time .it_value from the
304 * real time .it_interval. Rather, we compute the next time in
305 * absolute time when the timer should go off.
310 getitimer(struct proc
*p
, register struct getitimer_args
*uap
, __unused register_t
*retval
)
312 struct itimerval aitv
;
314 if (uap
->which
> ITIMER_PROF
)
316 if (uap
->which
== ITIMER_REAL
) {
318 * If time for real time timer has passed return 0,
319 * else return difference between current time and
320 * time for the timer to go off.
322 aitv
= p
->p_realtimer
;
323 if (timerisset(&p
->p_rtime
)) {
327 if (timercmp(&p
->p_rtime
, &now
, <))
328 timerclear(&aitv
.it_value
);
330 aitv
.it_value
= p
->p_rtime
;
331 timevalsub(&aitv
.it_value
, &now
);
335 timerclear(&aitv
.it_value
);
338 aitv
= p
->p_stats
->p_timer
[uap
->which
];
340 if (IS_64BIT_PROCESS(p
)) {
341 struct user_itimerval user_itv
;
342 user_itv
.it_interval
.tv_sec
= aitv
.it_interval
.tv_sec
;
343 user_itv
.it_interval
.tv_usec
= aitv
.it_interval
.tv_usec
;
344 user_itv
.it_value
.tv_sec
= aitv
.it_value
.tv_sec
;
345 user_itv
.it_value
.tv_usec
= aitv
.it_value
.tv_usec
;
346 return (copyout((caddr_t
)&user_itv
, uap
->itv
, sizeof (struct user_itimerval
)));
348 return (copyout((caddr_t
)&aitv
, uap
->itv
, sizeof (struct itimerval
)));
354 setitimer(p
, uap
, retval
)
356 register struct setitimer_args
*uap
;
359 struct itimerval aitv
;
363 if (uap
->which
> ITIMER_PROF
)
365 if ((itvp
= uap
->itv
)) {
366 if (IS_64BIT_PROCESS(p
)) {
367 struct user_itimerval user_itv
;
368 if ((error
= copyin(itvp
, (caddr_t
)&user_itv
, sizeof (struct user_itimerval
))))
370 aitv
.it_interval
.tv_sec
= user_itv
.it_interval
.tv_sec
;
371 aitv
.it_interval
.tv_usec
= user_itv
.it_interval
.tv_usec
;
372 aitv
.it_value
.tv_sec
= user_itv
.it_value
.tv_sec
;
373 aitv
.it_value
.tv_usec
= user_itv
.it_value
.tv_usec
;
375 if ((error
= copyin(itvp
, (caddr_t
)&aitv
, sizeof (struct itimerval
))))
379 if ((uap
->itv
= uap
->oitv
) && (error
= getitimer(p
, (struct getitimer_args
*)uap
, retval
)))
383 if (itimerfix(&aitv
.it_value
) || itimerfix(&aitv
.it_interval
))
385 if (uap
->which
== ITIMER_REAL
) {
386 thread_call_func_cancel((thread_call_func_t
)realitexpire
, (void *)p
->p_pid
, FALSE
);
387 if (timerisset(&aitv
.it_value
)) {
388 microuptime(&p
->p_rtime
);
389 timevaladd(&p
->p_rtime
, &aitv
.it_value
);
390 thread_call_func_delayed(
391 (thread_call_func_t
)realitexpire
, (void *)p
->p_pid
,
392 tvtoabstime(&p
->p_rtime
));
395 timerclear(&p
->p_rtime
);
397 p
->p_realtimer
= aitv
;
400 p
->p_stats
->p_timer
[uap
->which
] = aitv
;
406 * Real interval timer expired:
407 * send process whose timer expired an alarm signal.
408 * If time is not set up to reload, then just return.
409 * Else compute next time timer should go off which is > current time.
410 * This is where delay in processing this timeout causes multiple
411 * SIGALRM calls to be compressed into one.
417 register struct proc
*p
;
419 boolean_t funnel_state
;
421 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
422 p
= pfind((pid_t
)pid
);
424 (void) thread_funnel_set(kernel_flock
, FALSE
);
428 if (!timerisset(&p
->p_realtimer
.it_interval
)) {
429 timerclear(&p
->p_rtime
);
432 (void) thread_funnel_set(kernel_flock
, FALSE
);
437 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
438 if (timercmp(&p
->p_rtime
, &now
, <=)) {
439 if ((p
->p_rtime
.tv_sec
+ 2) >= now
.tv_sec
) {
441 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
442 if (timercmp(&p
->p_rtime
, &now
, >))
447 p
->p_rtime
= p
->p_realtimer
.it_interval
;
448 timevaladd(&p
->p_rtime
, &now
);
454 thread_call_func_delayed((thread_call_func_t
)realitexpire
, pid
, tvtoabstime(&p
->p_rtime
));
456 (void) thread_funnel_set(kernel_flock
, FALSE
);
460 * Check that a proposed value to load into the .it_value or
461 * .it_interval part of an interval timer is acceptable, and
462 * fix it to have at least minimal value (i.e. if it is less
463 * than the resolution of the clock, round it up.)
470 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
471 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
473 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
479 * Decrement an interval timer by a specified number
480 * of microseconds, which must be less than a second,
481 * i.e. < 1000000. If the timer expires, then reload
482 * it. In this case, carry over (usec - old value) to
483 * reducint the value reloaded into the timer so that
484 * the timer does not drift. This routine assumes
485 * that it is called in a context where the timers
486 * on which it is operating cannot change in value.
489 itimerdecr(itp
, usec
)
490 register struct itimerval
*itp
;
494 if (itp
->it_value
.tv_usec
< usec
) {
495 if (itp
->it_value
.tv_sec
== 0) {
496 /* expired, and already in next interval */
497 usec
-= itp
->it_value
.tv_usec
;
500 itp
->it_value
.tv_usec
+= 1000000;
501 itp
->it_value
.tv_sec
--;
503 itp
->it_value
.tv_usec
-= usec
;
505 if (timerisset(&itp
->it_value
))
507 /* expired, exactly at end of interval */
509 if (timerisset(&itp
->it_interval
)) {
510 itp
->it_value
= itp
->it_interval
;
511 itp
->it_value
.tv_usec
-= usec
;
512 if (itp
->it_value
.tv_usec
< 0) {
513 itp
->it_value
.tv_usec
+= 1000000;
514 itp
->it_value
.tv_sec
--;
517 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
522 * Add and subtract routines for timevals.
523 * N.B.: subtract routine doesn't deal with
524 * results which are before the beginning,
525 * it just gets very confused in this case.
534 t1
->tv_sec
+= t2
->tv_sec
;
535 t1
->tv_usec
+= t2
->tv_usec
;
544 t1
->tv_sec
-= t2
->tv_sec
;
545 t1
->tv_usec
-= t2
->tv_usec
;
553 if (t1
->tv_usec
< 0) {
555 t1
->tv_usec
+= 1000000;
557 if (t1
->tv_usec
>= 1000000) {
559 t1
->tv_usec
-= 1000000;
564 * Return the best possible estimate of the time in the timeval
565 * to which tvp points.
571 clock_get_calendar_microtime((uint32_t *)&tvp
->tv_sec
, &tvp
->tv_usec
);
578 clock_get_system_microtime((uint32_t *)&tvp
->tv_sec
, &tvp
->tv_usec
);
582 * Ditto for timespec.
586 struct timespec
*tsp
)
588 clock_get_calendar_nanotime((uint32_t *)&tsp
->tv_sec
, (uint32_t *)&tsp
->tv_nsec
);
593 struct timespec
*tsp
)
595 clock_get_system_nanotime((uint32_t *)&tsp
->tv_sec
, (uint32_t *)&tsp
->tv_nsec
);
602 uint64_t result
, usresult
;
604 clock_interval_to_absolutetime_interval(
605 tvp
->tv_sec
, NSEC_PER_SEC
, &result
);
606 clock_interval_to_absolutetime_interval(
607 tvp
->tv_usec
, NSEC_PER_USEC
, &usresult
);
609 return (result
+ usresult
);
612 time_zone_slock_init(void)
614 /* allocate lock group attribute and group */
615 tz_slock_grp_attr
= lck_grp_attr_alloc_init();
616 lck_grp_attr_setstat(tz_slock_grp_attr
);
618 tz_slock_grp
= lck_grp_alloc_init("tzlock", tz_slock_grp_attr
);
620 /* Allocate lock attribute */
621 tz_slock_attr
= lck_attr_alloc_init();
622 //lck_attr_setdebug(tz_slock_attr);
624 /* Allocate the spin lock */
625 tz_slock
= lck_spin_alloc_init(tz_slock_grp
, tz_slock_attr
);