]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_time.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
58 #include <sys/param.h>
59 #include <sys/resourcevar.h>
60 #include <sys/kernel.h>
61 #include <sys/systm.h>
62 #include <sys/proc_internal.h>
63 #include <sys/kauth.h>
64 #include <sys/vnode.h>
66 #include <sys/mount_internal.h>
67 #include <sys/sysproto.h>
68 #include <sys/signalvar.h>
70 #include <kern/clock.h>
71 #include <kern/thread_call.h>
73 #define HZ 100 /* XXX */
75 /* simple lock used to access timezone, tz structure */
76 lck_spin_t
* tz_slock
;
77 lck_grp_t
* tz_slock_grp
;
78 lck_attr_t
* tz_slock_attr
;
79 lck_grp_attr_t
*tz_slock_grp_attr
;
81 static void setthetime(
84 void time_zone_slock_init(void);
86 int gettimeofday(struct proc
*p
,
88 struct ppc_gettimeofday_args
*uap
,
90 struct gettimeofday_args
*uap
,
95 * Time of day and interval timer support.
97 * These routines provide the kernel entry points to get and set
98 * the time-of-day and per-process interval timers. Subroutines
99 * here provide support for adding and subtracting timeval structures
100 * and decrementing interval timers, optionally reloading the interval
101 * timers when they expire.
103 * XXX Y2038 bug because of clock_get_calendar_microtime() first argument
107 gettimeofday(__unused
struct proc
*p
,
109 register struct ppc_gettimeofday_args
*uap
,
111 register struct gettimeofday_args
*uap
,
113 __unused register_t
*retval
)
117 struct timezone ltz
; /* local copy */
119 /* NOTE THIS implementation is for non ppc architectures only */
122 clock_get_calendar_microtime((uint32_t *)&atv
.tv_sec
, &atv
.tv_usec
);
123 if (IS_64BIT_PROCESS(p
)) {
124 struct user_timeval user_atv
;
125 user_atv
.tv_sec
= atv
.tv_sec
;
126 user_atv
.tv_usec
= atv
.tv_usec
;
128 * This cast is not necessary for PPC, but is
131 error
= copyout(&user_atv
, CAST_USER_ADDR_T(uap
->tp
), sizeof(struct user_timeval
));
133 error
= copyout(&atv
, CAST_USER_ADDR_T(uap
->tp
), sizeof(struct timeval
));
140 lck_spin_lock(tz_slock
);
142 lck_spin_unlock(tz_slock
);
143 error
= copyout((caddr_t
)<z
, CAST_USER_ADDR_T(uap
->tzp
),
151 * XXX Y2038 bug because of setthetime() argument
155 settimeofday(struct proc
*p
, struct settimeofday_args
*uap
, __unused register_t
*retval
)
161 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
163 /* Verify all parameters before changing time */
165 if (IS_64BIT_PROCESS(p
)) {
166 struct user_timeval user_atv
;
167 error
= copyin(uap
->tv
, &user_atv
, sizeof(struct user_timeval
));
168 atv
.tv_sec
= user_atv
.tv_sec
;
169 atv
.tv_usec
= user_atv
.tv_usec
;
171 error
= copyin(uap
->tv
, &atv
, sizeof(struct timeval
));
176 if (uap
->tzp
&& (error
= copyin(uap
->tzp
, (caddr_t
)&atz
, sizeof(atz
))))
180 if (atv
.tv_sec
< 0 || (atv
.tv_sec
== 0 && atv
.tv_usec
< 0))
185 lck_spin_lock(tz_slock
);
187 lck_spin_unlock(tz_slock
);
196 clock_set_calendar_microtime(tv
->tv_sec
, tv
->tv_usec
);
200 * XXX Y2038 bug because of clock_adjtime() first argument
204 adjtime(struct proc
*p
, register struct adjtime_args
*uap
, __unused register_t
*retval
)
209 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
211 if (IS_64BIT_PROCESS(p
)) {
212 struct user_timeval user_atv
;
213 error
= copyin(uap
->delta
, &user_atv
, sizeof(struct user_timeval
));
214 atv
.tv_sec
= user_atv
.tv_sec
;
215 atv
.tv_usec
= user_atv
.tv_usec
;
217 error
= copyin(uap
->delta
, &atv
, sizeof(struct timeval
));
223 * Compute the total correction and the rate at which to apply it.
225 clock_adjtime((int32_t *)&atv
.tv_sec
, &atv
.tv_usec
);
228 if (IS_64BIT_PROCESS(p
)) {
229 struct user_timeval user_atv
;
230 user_atv
.tv_sec
= atv
.tv_sec
;
231 user_atv
.tv_usec
= atv
.tv_usec
;
232 error
= copyout(&user_atv
, uap
->olddelta
, sizeof(struct user_timeval
));
234 error
= copyout(&atv
, uap
->olddelta
, sizeof(struct timeval
));
242 * Verify the calendar value. If negative,
243 * reset to zero (the epoch).
247 __unused
time_t base
)
253 * The calendar has already been
254 * set up from the platform clock.
256 * The value returned by microtime()
257 * is gotten from the calendar.
261 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0) {
262 printf ("WARNING: preposterous time in Real Time Clock");
263 tv
.tv_sec
= 0; /* the UNIX epoch */
266 printf(" -- CHECK AND RESET THE DATE!\n");
273 uint32_t sec
, nanosec
;
274 clock_get_boottime_nanotime(&sec
, &nanosec
);
278 uint64_t tvtoabstime(struct timeval
*tvp
);
281 * Get value of an interval timer. The process virtual and
282 * profiling virtual time timers are kept internally in the
283 * way they are specified externally: in time until they expire.
285 * The real time interval timer expiration time (p_rtime)
286 * is kept as an absolute time rather than as a delta, so that
287 * it is easy to keep periodic real-time signals from drifting.
289 * Virtual time timers are processed in the hardclock() routine of
290 * kern_clock.c. The real time timer is processed by a callout
291 * routine. Since a callout may be delayed in real time due to
292 * other processing in the system, it is possible for the real
293 * time callout routine (realitexpire, given below), to be delayed
294 * in real time past when it is supposed to occur. It does not
295 * suffice, therefore, to reload the real time .it_value from the
296 * real time .it_interval. Rather, we compute the next time in
297 * absolute time when the timer should go off.
302 getitimer(struct proc
*p
, register struct getitimer_args
*uap
, __unused register_t
*retval
)
304 struct itimerval aitv
;
306 if (uap
->which
> ITIMER_PROF
)
308 if (uap
->which
== ITIMER_REAL
) {
310 * If time for real time timer has passed return 0,
311 * else return difference between current time and
312 * time for the timer to go off.
314 aitv
= p
->p_realtimer
;
315 if (timerisset(&p
->p_rtime
)) {
319 if (timercmp(&p
->p_rtime
, &now
, <))
320 timerclear(&aitv
.it_value
);
322 aitv
.it_value
= p
->p_rtime
;
323 timevalsub(&aitv
.it_value
, &now
);
327 timerclear(&aitv
.it_value
);
330 aitv
= p
->p_stats
->p_timer
[uap
->which
];
332 if (IS_64BIT_PROCESS(p
)) {
333 struct user_itimerval user_itv
;
334 user_itv
.it_interval
.tv_sec
= aitv
.it_interval
.tv_sec
;
335 user_itv
.it_interval
.tv_usec
= aitv
.it_interval
.tv_usec
;
336 user_itv
.it_value
.tv_sec
= aitv
.it_value
.tv_sec
;
337 user_itv
.it_value
.tv_usec
= aitv
.it_value
.tv_usec
;
338 return (copyout((caddr_t
)&user_itv
, uap
->itv
, sizeof (struct user_itimerval
)));
340 return (copyout((caddr_t
)&aitv
, uap
->itv
, sizeof (struct itimerval
)));
346 setitimer(p
, uap
, retval
)
348 register struct setitimer_args
*uap
;
351 struct itimerval aitv
;
355 if (uap
->which
> ITIMER_PROF
)
357 if ((itvp
= uap
->itv
)) {
358 if (IS_64BIT_PROCESS(p
)) {
359 struct user_itimerval user_itv
;
360 if ((error
= copyin(itvp
, (caddr_t
)&user_itv
, sizeof (struct user_itimerval
))))
362 aitv
.it_interval
.tv_sec
= user_itv
.it_interval
.tv_sec
;
363 aitv
.it_interval
.tv_usec
= user_itv
.it_interval
.tv_usec
;
364 aitv
.it_value
.tv_sec
= user_itv
.it_value
.tv_sec
;
365 aitv
.it_value
.tv_usec
= user_itv
.it_value
.tv_usec
;
367 if ((error
= copyin(itvp
, (caddr_t
)&aitv
, sizeof (struct itimerval
))))
371 if ((uap
->itv
= uap
->oitv
) && (error
= getitimer(p
, (struct getitimer_args
*)uap
, retval
)))
375 if (itimerfix(&aitv
.it_value
) || itimerfix(&aitv
.it_interval
))
377 if (uap
->which
== ITIMER_REAL
) {
378 thread_call_func_cancel((thread_call_func_t
)realitexpire
, (void *)p
->p_pid
, FALSE
);
379 if (timerisset(&aitv
.it_value
)) {
380 microuptime(&p
->p_rtime
);
381 timevaladd(&p
->p_rtime
, &aitv
.it_value
);
382 thread_call_func_delayed(
383 (thread_call_func_t
)realitexpire
, (void *)p
->p_pid
,
384 tvtoabstime(&p
->p_rtime
));
387 timerclear(&p
->p_rtime
);
389 p
->p_realtimer
= aitv
;
392 p
->p_stats
->p_timer
[uap
->which
] = aitv
;
398 * Real interval timer expired:
399 * send process whose timer expired an alarm signal.
400 * If time is not set up to reload, then just return.
401 * Else compute next time timer should go off which is > current time.
402 * This is where delay in processing this timeout causes multiple
403 * SIGALRM calls to be compressed into one.
409 register struct proc
*p
;
411 boolean_t funnel_state
;
413 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
414 p
= pfind((pid_t
)pid
);
416 (void) thread_funnel_set(kernel_flock
, FALSE
);
420 if (!timerisset(&p
->p_realtimer
.it_interval
)) {
421 timerclear(&p
->p_rtime
);
424 (void) thread_funnel_set(kernel_flock
, FALSE
);
429 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
430 if (timercmp(&p
->p_rtime
, &now
, <=)) {
431 if ((p
->p_rtime
.tv_sec
+ 2) >= now
.tv_sec
) {
433 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
434 if (timercmp(&p
->p_rtime
, &now
, >))
439 p
->p_rtime
= p
->p_realtimer
.it_interval
;
440 timevaladd(&p
->p_rtime
, &now
);
446 thread_call_func_delayed((thread_call_func_t
)realitexpire
, pid
, tvtoabstime(&p
->p_rtime
));
448 (void) thread_funnel_set(kernel_flock
, FALSE
);
452 * Check that a proposed value to load into the .it_value or
453 * .it_interval part of an interval timer is acceptable, and
454 * fix it to have at least minimal value (i.e. if it is less
455 * than the resolution of the clock, round it up.)
462 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
463 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
465 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
471 * Decrement an interval timer by a specified number
472 * of microseconds, which must be less than a second,
473 * i.e. < 1000000. If the timer expires, then reload
474 * it. In this case, carry over (usec - old value) to
475 * reducint the value reloaded into the timer so that
476 * the timer does not drift. This routine assumes
477 * that it is called in a context where the timers
478 * on which it is operating cannot change in value.
481 itimerdecr(itp
, usec
)
482 register struct itimerval
*itp
;
486 if (itp
->it_value
.tv_usec
< usec
) {
487 if (itp
->it_value
.tv_sec
== 0) {
488 /* expired, and already in next interval */
489 usec
-= itp
->it_value
.tv_usec
;
492 itp
->it_value
.tv_usec
+= 1000000;
493 itp
->it_value
.tv_sec
--;
495 itp
->it_value
.tv_usec
-= usec
;
497 if (timerisset(&itp
->it_value
))
499 /* expired, exactly at end of interval */
501 if (timerisset(&itp
->it_interval
)) {
502 itp
->it_value
= itp
->it_interval
;
503 itp
->it_value
.tv_usec
-= usec
;
504 if (itp
->it_value
.tv_usec
< 0) {
505 itp
->it_value
.tv_usec
+= 1000000;
506 itp
->it_value
.tv_sec
--;
509 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
514 * Add and subtract routines for timevals.
515 * N.B.: subtract routine doesn't deal with
516 * results which are before the beginning,
517 * it just gets very confused in this case.
526 t1
->tv_sec
+= t2
->tv_sec
;
527 t1
->tv_usec
+= t2
->tv_usec
;
536 t1
->tv_sec
-= t2
->tv_sec
;
537 t1
->tv_usec
-= t2
->tv_usec
;
545 if (t1
->tv_usec
< 0) {
547 t1
->tv_usec
+= 1000000;
549 if (t1
->tv_usec
>= 1000000) {
551 t1
->tv_usec
-= 1000000;
556 * Return the best possible estimate of the time in the timeval
557 * to which tvp points.
563 clock_get_calendar_microtime((uint32_t *)&tvp
->tv_sec
, &tvp
->tv_usec
);
570 clock_get_system_microtime((uint32_t *)&tvp
->tv_sec
, &tvp
->tv_usec
);
574 * Ditto for timespec.
578 struct timespec
*tsp
)
580 clock_get_calendar_nanotime((uint32_t *)&tsp
->tv_sec
, (uint32_t *)&tsp
->tv_nsec
);
585 struct timespec
*tsp
)
587 clock_get_system_nanotime((uint32_t *)&tsp
->tv_sec
, (uint32_t *)&tsp
->tv_nsec
);
594 uint64_t result
, usresult
;
596 clock_interval_to_absolutetime_interval(
597 tvp
->tv_sec
, NSEC_PER_SEC
, &result
);
598 clock_interval_to_absolutetime_interval(
599 tvp
->tv_usec
, NSEC_PER_USEC
, &usresult
);
601 return (result
+ usresult
);
604 time_zone_slock_init(void)
606 /* allocate lock group attribute and group */
607 tz_slock_grp_attr
= lck_grp_attr_alloc_init();
608 lck_grp_attr_setstat(tz_slock_grp_attr
);
610 tz_slock_grp
= lck_grp_alloc_init("tzlock", tz_slock_grp_attr
);
612 /* Allocate lock attribute */
613 tz_slock_attr
= lck_attr_alloc_init();
614 //lck_attr_setdebug(tz_slock_attr);
616 /* Allocate the spin lock */
617 tz_slock
= lck_spin_alloc_init(tz_slock_grp
, tz_slock_attr
);