]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_time.c
e161878c38bf8f5dd9c54f865ebfdd07c7cdc624
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
59 #include <sys/param.h>
60 #include <sys/resourcevar.h>
61 #include <sys/kernel.h>
62 #include <sys/systm.h>
63 #include <sys/proc_internal.h>
64 #include <sys/kauth.h>
65 #include <sys/vnode.h>
67 #include <sys/mount_internal.h>
68 #include <sys/sysproto.h>
69 #include <sys/signalvar.h>
71 #include <kern/clock.h>
72 #include <kern/thread_call.h>
74 #define HZ 100 /* XXX */
76 /* simple lock used to access timezone, tz structure */
77 lck_spin_t
* tz_slock
;
78 lck_grp_t
* tz_slock_grp
;
79 lck_attr_t
* tz_slock_attr
;
80 lck_grp_attr_t
*tz_slock_grp_attr
;
82 static void setthetime(
85 void time_zone_slock_init(void);
87 int gettimeofday(struct proc
*p
,
89 struct ppc_gettimeofday_args
*uap
,
91 struct gettimeofday_args
*uap
,
96 * Time of day and interval timer support.
98 * These routines provide the kernel entry points to get and set
99 * the time-of-day and per-process interval timers. Subroutines
100 * here provide support for adding and subtracting timeval structures
101 * and decrementing interval timers, optionally reloading the interval
102 * timers when they expire.
104 * XXX Y2038 bug because of clock_get_calendar_microtime() first argument
108 gettimeofday(__unused
struct proc
*p
,
110 register struct ppc_gettimeofday_args
*uap
,
112 register struct gettimeofday_args
*uap
,
114 __unused register_t
*retval
)
118 struct timezone ltz
; /* local copy */
120 /* NOTE THIS implementation is for non ppc architectures only */
123 clock_get_calendar_microtime((uint32_t *)&atv
.tv_sec
, &atv
.tv_usec
);
124 if (IS_64BIT_PROCESS(p
)) {
125 struct user_timeval user_atv
;
126 user_atv
.tv_sec
= atv
.tv_sec
;
127 user_atv
.tv_usec
= atv
.tv_usec
;
129 * This cast is not necessary for PPC, but is
132 error
= copyout(&user_atv
, CAST_USER_ADDR_T(uap
->tp
), sizeof(struct user_timeval
));
134 error
= copyout(&atv
, CAST_USER_ADDR_T(uap
->tp
), sizeof(struct timeval
));
141 lck_spin_lock(tz_slock
);
143 lck_spin_unlock(tz_slock
);
144 error
= copyout((caddr_t
)<z
, CAST_USER_ADDR_T(uap
->tzp
),
152 * XXX Y2038 bug because of setthetime() argument
156 settimeofday(struct proc
*p
, struct settimeofday_args
*uap
, __unused register_t
*retval
)
162 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
164 /* Verify all parameters before changing time */
166 if (IS_64BIT_PROCESS(p
)) {
167 struct user_timeval user_atv
;
168 error
= copyin(uap
->tv
, &user_atv
, sizeof(struct user_timeval
));
169 atv
.tv_sec
= user_atv
.tv_sec
;
170 atv
.tv_usec
= user_atv
.tv_usec
;
172 error
= copyin(uap
->tv
, &atv
, sizeof(struct timeval
));
177 if (uap
->tzp
&& (error
= copyin(uap
->tzp
, (caddr_t
)&atz
, sizeof(atz
))))
181 if (atv
.tv_sec
< 0 || (atv
.tv_sec
== 0 && atv
.tv_usec
< 0))
186 lck_spin_lock(tz_slock
);
188 lck_spin_unlock(tz_slock
);
197 clock_set_calendar_microtime(tv
->tv_sec
, tv
->tv_usec
);
201 * XXX Y2038 bug because of clock_adjtime() first argument
205 adjtime(struct proc
*p
, register struct adjtime_args
*uap
, __unused register_t
*retval
)
210 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
212 if (IS_64BIT_PROCESS(p
)) {
213 struct user_timeval user_atv
;
214 error
= copyin(uap
->delta
, &user_atv
, sizeof(struct user_timeval
));
215 atv
.tv_sec
= user_atv
.tv_sec
;
216 atv
.tv_usec
= user_atv
.tv_usec
;
218 error
= copyin(uap
->delta
, &atv
, sizeof(struct timeval
));
224 * Compute the total correction and the rate at which to apply it.
226 clock_adjtime((int32_t *)&atv
.tv_sec
, &atv
.tv_usec
);
229 if (IS_64BIT_PROCESS(p
)) {
230 struct user_timeval user_atv
;
231 user_atv
.tv_sec
= atv
.tv_sec
;
232 user_atv
.tv_usec
= atv
.tv_usec
;
233 error
= copyout(&user_atv
, uap
->olddelta
, sizeof(struct user_timeval
));
235 error
= copyout(&atv
, uap
->olddelta
, sizeof(struct timeval
));
243 * Verify the calendar value. If negative,
244 * reset to zero (the epoch).
248 __unused
time_t base
)
254 * The calendar has already been
255 * set up from the platform clock.
257 * The value returned by microtime()
258 * is gotten from the calendar.
262 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0) {
263 printf ("WARNING: preposterous time in Real Time Clock");
264 tv
.tv_sec
= 0; /* the UNIX epoch */
267 printf(" -- CHECK AND RESET THE DATE!\n");
274 uint32_t sec
, nanosec
;
275 clock_get_boottime_nanotime(&sec
, &nanosec
);
279 uint64_t tvtoabstime(struct timeval
*tvp
);
282 * Get value of an interval timer. The process virtual and
283 * profiling virtual time timers are kept internally in the
284 * way they are specified externally: in time until they expire.
286 * The real time interval timer expiration time (p_rtime)
287 * is kept as an absolute time rather than as a delta, so that
288 * it is easy to keep periodic real-time signals from drifting.
290 * Virtual time timers are processed in the hardclock() routine of
291 * kern_clock.c. The real time timer is processed by a callout
292 * routine. Since a callout may be delayed in real time due to
293 * other processing in the system, it is possible for the real
294 * time callout routine (realitexpire, given below), to be delayed
295 * in real time past when it is supposed to occur. It does not
296 * suffice, therefore, to reload the real time .it_value from the
297 * real time .it_interval. Rather, we compute the next time in
298 * absolute time when the timer should go off.
303 getitimer(struct proc
*p
, register struct getitimer_args
*uap
, __unused register_t
*retval
)
305 struct itimerval aitv
;
307 if (uap
->which
> ITIMER_PROF
)
309 if (uap
->which
== ITIMER_REAL
) {
311 * If time for real time timer has passed return 0,
312 * else return difference between current time and
313 * time for the timer to go off.
315 aitv
= p
->p_realtimer
;
316 if (timerisset(&p
->p_rtime
)) {
320 if (timercmp(&p
->p_rtime
, &now
, <))
321 timerclear(&aitv
.it_value
);
323 aitv
.it_value
= p
->p_rtime
;
324 timevalsub(&aitv
.it_value
, &now
);
328 timerclear(&aitv
.it_value
);
331 aitv
= p
->p_stats
->p_timer
[uap
->which
];
333 if (IS_64BIT_PROCESS(p
)) {
334 struct user_itimerval user_itv
;
335 user_itv
.it_interval
.tv_sec
= aitv
.it_interval
.tv_sec
;
336 user_itv
.it_interval
.tv_usec
= aitv
.it_interval
.tv_usec
;
337 user_itv
.it_value
.tv_sec
= aitv
.it_value
.tv_sec
;
338 user_itv
.it_value
.tv_usec
= aitv
.it_value
.tv_usec
;
339 return (copyout((caddr_t
)&user_itv
, uap
->itv
, sizeof (struct user_itimerval
)));
341 return (copyout((caddr_t
)&aitv
, uap
->itv
, sizeof (struct itimerval
)));
347 setitimer(p
, uap
, retval
)
349 register struct setitimer_args
*uap
;
352 struct itimerval aitv
;
356 if (uap
->which
> ITIMER_PROF
)
358 if ((itvp
= uap
->itv
)) {
359 if (IS_64BIT_PROCESS(p
)) {
360 struct user_itimerval user_itv
;
361 if ((error
= copyin(itvp
, (caddr_t
)&user_itv
, sizeof (struct user_itimerval
))))
363 aitv
.it_interval
.tv_sec
= user_itv
.it_interval
.tv_sec
;
364 aitv
.it_interval
.tv_usec
= user_itv
.it_interval
.tv_usec
;
365 aitv
.it_value
.tv_sec
= user_itv
.it_value
.tv_sec
;
366 aitv
.it_value
.tv_usec
= user_itv
.it_value
.tv_usec
;
368 if ((error
= copyin(itvp
, (caddr_t
)&aitv
, sizeof (struct itimerval
))))
372 if ((uap
->itv
= uap
->oitv
) && (error
= getitimer(p
, (struct getitimer_args
*)uap
, retval
)))
376 if (itimerfix(&aitv
.it_value
) || itimerfix(&aitv
.it_interval
))
378 if (uap
->which
== ITIMER_REAL
) {
379 thread_call_func_cancel((thread_call_func_t
)realitexpire
, (void *)p
->p_pid
, FALSE
);
380 if (timerisset(&aitv
.it_value
)) {
381 microuptime(&p
->p_rtime
);
382 timevaladd(&p
->p_rtime
, &aitv
.it_value
);
383 thread_call_func_delayed(
384 (thread_call_func_t
)realitexpire
, (void *)p
->p_pid
,
385 tvtoabstime(&p
->p_rtime
));
388 timerclear(&p
->p_rtime
);
390 p
->p_realtimer
= aitv
;
393 p
->p_stats
->p_timer
[uap
->which
] = aitv
;
399 * Real interval timer expired:
400 * send process whose timer expired an alarm signal.
401 * If time is not set up to reload, then just return.
402 * Else compute next time timer should go off which is > current time.
403 * This is where delay in processing this timeout causes multiple
404 * SIGALRM calls to be compressed into one.
410 register struct proc
*p
;
412 boolean_t funnel_state
;
414 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
415 p
= pfind((pid_t
)pid
);
417 (void) thread_funnel_set(kernel_flock
, FALSE
);
421 if (!timerisset(&p
->p_realtimer
.it_interval
)) {
422 timerclear(&p
->p_rtime
);
425 (void) thread_funnel_set(kernel_flock
, FALSE
);
430 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
431 if (timercmp(&p
->p_rtime
, &now
, <=)) {
432 if ((p
->p_rtime
.tv_sec
+ 2) >= now
.tv_sec
) {
434 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
435 if (timercmp(&p
->p_rtime
, &now
, >))
440 p
->p_rtime
= p
->p_realtimer
.it_interval
;
441 timevaladd(&p
->p_rtime
, &now
);
447 thread_call_func_delayed((thread_call_func_t
)realitexpire
, pid
, tvtoabstime(&p
->p_rtime
));
449 (void) thread_funnel_set(kernel_flock
, FALSE
);
453 * Check that a proposed value to load into the .it_value or
454 * .it_interval part of an interval timer is acceptable, and
455 * fix it to have at least minimal value (i.e. if it is less
456 * than the resolution of the clock, round it up.)
463 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
464 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
466 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
472 * Decrement an interval timer by a specified number
473 * of microseconds, which must be less than a second,
474 * i.e. < 1000000. If the timer expires, then reload
475 * it. In this case, carry over (usec - old value) to
476 * reducint the value reloaded into the timer so that
477 * the timer does not drift. This routine assumes
478 * that it is called in a context where the timers
479 * on which it is operating cannot change in value.
482 itimerdecr(itp
, usec
)
483 register struct itimerval
*itp
;
487 if (itp
->it_value
.tv_usec
< usec
) {
488 if (itp
->it_value
.tv_sec
== 0) {
489 /* expired, and already in next interval */
490 usec
-= itp
->it_value
.tv_usec
;
493 itp
->it_value
.tv_usec
+= 1000000;
494 itp
->it_value
.tv_sec
--;
496 itp
->it_value
.tv_usec
-= usec
;
498 if (timerisset(&itp
->it_value
))
500 /* expired, exactly at end of interval */
502 if (timerisset(&itp
->it_interval
)) {
503 itp
->it_value
= itp
->it_interval
;
504 itp
->it_value
.tv_usec
-= usec
;
505 if (itp
->it_value
.tv_usec
< 0) {
506 itp
->it_value
.tv_usec
+= 1000000;
507 itp
->it_value
.tv_sec
--;
510 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
515 * Add and subtract routines for timevals.
516 * N.B.: subtract routine doesn't deal with
517 * results which are before the beginning,
518 * it just gets very confused in this case.
527 t1
->tv_sec
+= t2
->tv_sec
;
528 t1
->tv_usec
+= t2
->tv_usec
;
537 t1
->tv_sec
-= t2
->tv_sec
;
538 t1
->tv_usec
-= t2
->tv_usec
;
546 if (t1
->tv_usec
< 0) {
548 t1
->tv_usec
+= 1000000;
550 if (t1
->tv_usec
>= 1000000) {
552 t1
->tv_usec
-= 1000000;
557 * Return the best possible estimate of the time in the timeval
558 * to which tvp points.
564 clock_get_calendar_microtime((uint32_t *)&tvp
->tv_sec
, &tvp
->tv_usec
);
571 clock_get_system_microtime((uint32_t *)&tvp
->tv_sec
, &tvp
->tv_usec
);
575 * Ditto for timespec.
579 struct timespec
*tsp
)
581 clock_get_calendar_nanotime((uint32_t *)&tsp
->tv_sec
, (uint32_t *)&tsp
->tv_nsec
);
586 struct timespec
*tsp
)
588 clock_get_system_nanotime((uint32_t *)&tsp
->tv_sec
, (uint32_t *)&tsp
->tv_nsec
);
595 uint64_t result
, usresult
;
597 clock_interval_to_absolutetime_interval(
598 tvp
->tv_sec
, NSEC_PER_SEC
, &result
);
599 clock_interval_to_absolutetime_interval(
600 tvp
->tv_usec
, NSEC_PER_USEC
, &usresult
);
602 return (result
+ usresult
);
605 time_zone_slock_init(void)
607 /* allocate lock group attribute and group */
608 tz_slock_grp_attr
= lck_grp_attr_alloc_init();
609 lck_grp_attr_setstat(tz_slock_grp_attr
);
611 tz_slock_grp
= lck_grp_alloc_init("tzlock", tz_slock_grp_attr
);
613 /* Allocate lock attribute */
614 tz_slock_attr
= lck_attr_alloc_init();
615 //lck_attr_setdebug(tz_slock_attr);
617 /* Allocate the spin lock */
618 tz_slock
= lck_spin_alloc_init(tz_slock_grp
, tz_slock_attr
);