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>
63 #include <sys/vnode.h>
65 #include <sys/mount.h>
67 #include <kern/clock.h>
69 #define HZ 100 /* XXX */
71 volatile struct timeval time
;
72 /* simple lock used to access timezone, tz structure */
73 decl_simple_lock_data(, tz_slock
);
75 * Time of day and interval timer support.
77 * These routines provide the kernel entry points to get and set
78 * the time-of-day and per-process interval timers. Subroutines
79 * here provide support for adding and subtracting timeval structures
80 * and decrementing interval timers, optionally reloading the interval
81 * timers when they expire.
83 struct gettimeofday_args
{
89 gettimeofday(p
, uap
, retval
)
91 register struct gettimeofday_args
*uap
;
96 extern simple_lock_data_t tz_slock
;
97 struct timezone ltz
; /* local copy */
99 /* NOTE THIS implementation is for non ppc architectures only */
102 clock_get_calendar_microtime(&atv
.tv_sec
, &atv
.tv_usec
);
103 if (error
= copyout((caddr_t
)&atv
, (caddr_t
)uap
->tp
,
109 usimple_lock(&tz_slock
);
111 usimple_unlock(&tz_slock
);
112 error
= copyout((caddr_t
)<z
, (caddr_t
)uap
->tzp
,
119 struct settimeofday_args
{
121 struct timezone
*tzp
;
125 settimeofday(p
, uap
, retval
)
127 struct settimeofday_args
*uap
;
133 extern simple_lock_data_t tz_slock
;
135 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
137 /* Verify all parameters before changing time. */
138 if (uap
->tv
&& (error
= copyin((caddr_t
)uap
->tv
,
139 (caddr_t
)&atv
, sizeof(atv
))))
141 if (uap
->tzp
&& (error
= copyin((caddr_t
)uap
->tzp
,
142 (caddr_t
)&atz
, sizeof(atz
))))
147 usimple_lock(&tz_slock
);
149 usimple_unlock(&tz_slock
);
157 long delta
= tv
->tv_sec
- time
.tv_sec
;
159 clock_set_calendar_microtime(tv
->tv_sec
, tv
->tv_usec
);
160 boottime
.tv_sec
+= delta
;
161 #if NFSCLIENT || NFSSERVER
162 lease_updatetime(delta
);
166 struct adjtime_args
{
167 struct timeval
*delta
;
168 struct timeval
*olddelta
;
172 adjtime(p
, uap
, retval
)
174 register struct adjtime_args
*uap
;
180 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
182 if (error
= copyin((caddr_t
)uap
->delta
,
183 (caddr_t
)&atv
, sizeof (struct timeval
)))
187 * Compute the total correction and the rate at which to apply it.
189 clock_adjtime(&atv
.tv_sec
, &atv
.tv_usec
);
192 (void) copyout((caddr_t
)&atv
,
193 (caddr_t
)uap
->olddelta
, sizeof (struct timeval
));
200 * Initialze the time of day register.
201 * Trust the RTC except for the case where it is set before
202 * the UNIX epoch. In that case use the the UNIX epoch.
203 * The argument passed in is ignored.
213 * The calendar has already been
214 * set up from the battery clock.
216 * The value returned by microtime()
217 * is gotten from the calendar.
222 boottime
.tv_sec
= tv
.tv_sec
;
223 boottime
.tv_usec
= 0;
226 * If the RTC does not have acceptable value, i.e. time before
227 * the UNIX epoch, set it to the UNIX epoch
230 printf ("WARNING: preposterous time in Real Time Clock");
231 time
.tv_sec
= 0; /* the UNIX epoch */
235 printf(" -- CHECK AND RESET THE DATE!\n");
252 struct timeval
*tvp
);
255 * Get value of an interval timer. The process virtual and
256 * profiling virtual time timers are kept internally in the
257 * way they are specified externally: in time until they expire.
259 * The real time interval timer expiration time (p_rtime)
260 * is kept as an absolute time rather than as a delta, so that
261 * it is easy to keep periodic real-time signals from drifting.
263 * Virtual time timers are processed in the hardclock() routine of
264 * kern_clock.c. The real time timer is processed by a callout
265 * routine. Since a callout may be delayed in real time due to
266 * other processing in the system, it is possible for the real
267 * time callout routine (realitexpire, given below), to be delayed
268 * in real time past when it is supposed to occur. It does not
269 * suffice, therefore, to reload the real time .it_value from the
270 * real time .it_interval. Rather, we compute the next time in
271 * absolute time when the timer should go off.
274 struct getitimer_args
{
276 struct itimerval
*itv
;
280 getitimer(p
, uap
, retval
)
282 register struct getitimer_args
*uap
;
285 struct itimerval aitv
;
287 if (uap
->which
> ITIMER_PROF
)
289 if (uap
->which
== ITIMER_REAL
) {
291 * If time for real time timer has passed return 0,
292 * else return difference between current time and
293 * time for the timer to go off.
295 aitv
= p
->p_realtimer
;
296 if (timerisset(&p
->p_rtime
)) {
300 if (timercmp(&p
->p_rtime
, &now
, <))
301 timerclear(&aitv
.it_value
);
303 aitv
.it_value
= p
->p_rtime
;
304 timevalsub(&aitv
.it_value
, &now
);
308 timerclear(&aitv
.it_value
);
311 aitv
= p
->p_stats
->p_timer
[uap
->which
];
313 return (copyout((caddr_t
)&aitv
,
314 (caddr_t
)uap
->itv
, sizeof (struct itimerval
)));
317 struct setitimer_args
{
319 struct itimerval
*itv
;
320 struct itimerval
*oitv
;
324 setitimer(p
, uap
, retval
)
326 register struct setitimer_args
*uap
;
329 struct itimerval aitv
;
330 register struct itimerval
*itvp
;
333 if (uap
->which
> ITIMER_PROF
)
335 if ((itvp
= uap
->itv
) &&
336 (error
= copyin((caddr_t
)itvp
,
337 (caddr_t
)&aitv
, sizeof (struct itimerval
))))
339 if ((uap
->itv
= uap
->oitv
) && (error
= getitimer(p
, uap
, retval
)))
343 if (itimerfix(&aitv
.it_value
) || itimerfix(&aitv
.it_interval
))
345 if (uap
->which
== ITIMER_REAL
) {
346 thread_call_func_cancel(realitexpire
, (void *)p
->p_pid
, FALSE
);
347 if (timerisset(&aitv
.it_value
)) {
348 microuptime(&p
->p_rtime
);
349 timevaladd(&p
->p_rtime
, &aitv
.it_value
);
350 thread_call_func_delayed(
351 realitexpire
, (void *)p
->p_pid
,
352 tvtoabstime(&p
->p_rtime
));
355 timerclear(&p
->p_rtime
);
357 p
->p_realtimer
= aitv
;
360 p
->p_stats
->p_timer
[uap
->which
] = aitv
;
366 * Real interval timer expired:
367 * send process whose timer expired an alarm signal.
368 * If time is not set up to reload, then just return.
369 * Else compute next time timer should go off which is > current time.
370 * This is where delay in processing this timeout causes multiple
371 * SIGALRM calls to be compressed into one.
377 register struct proc
*p
;
379 boolean_t funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
381 p
= pfind((pid_t
)pid
);
383 (void) thread_funnel_set(kernel_flock
, FALSE
);
387 if (!timerisset(&p
->p_realtimer
.it_interval
)) {
388 timerclear(&p
->p_rtime
);
391 (void) thread_funnel_set(kernel_flock
, FALSE
);
396 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
397 if (timercmp(&p
->p_rtime
, &now
, <=)) {
398 if ((p
->p_rtime
.tv_sec
+ 2) >= now
.tv_sec
) {
400 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
401 if (timercmp(&p
->p_rtime
, &now
, >))
406 p
->p_rtime
= p
->p_realtimer
.it_interval
;
407 timevaladd(&p
->p_rtime
, &now
);
413 thread_call_func_delayed(realitexpire
, pid
, tvtoabstime(&p
->p_rtime
));
415 (void) thread_funnel_set(kernel_flock
, FALSE
);
419 * Check that a proposed value to load into the .it_value or
420 * .it_interval part of an interval timer is acceptable, and
421 * fix it to have at least minimal value (i.e. if it is less
422 * than the resolution of the clock, round it up.)
429 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
430 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
432 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
438 * Decrement an interval timer by a specified number
439 * of microseconds, which must be less than a second,
440 * i.e. < 1000000. If the timer expires, then reload
441 * it. In this case, carry over (usec - old value) to
442 * reducint the value reloaded into the timer so that
443 * the timer does not drift. This routine assumes
444 * that it is called in a context where the timers
445 * on which it is operating cannot change in value.
448 itimerdecr(itp
, usec
)
449 register struct itimerval
*itp
;
453 if (itp
->it_value
.tv_usec
< usec
) {
454 if (itp
->it_value
.tv_sec
== 0) {
455 /* expired, and already in next interval */
456 usec
-= itp
->it_value
.tv_usec
;
459 itp
->it_value
.tv_usec
+= 1000000;
460 itp
->it_value
.tv_sec
--;
462 itp
->it_value
.tv_usec
-= usec
;
464 if (timerisset(&itp
->it_value
))
466 /* expired, exactly at end of interval */
468 if (timerisset(&itp
->it_interval
)) {
469 itp
->it_value
= itp
->it_interval
;
470 itp
->it_value
.tv_usec
-= usec
;
471 if (itp
->it_value
.tv_usec
< 0) {
472 itp
->it_value
.tv_usec
+= 1000000;
473 itp
->it_value
.tv_sec
--;
476 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
481 * Add and subtract routines for timevals.
482 * N.B.: subtract routine doesn't deal with
483 * results which are before the beginning,
484 * it just gets very confused in this case.
493 t1
->tv_sec
+= t2
->tv_sec
;
494 t1
->tv_usec
+= t2
->tv_usec
;
503 t1
->tv_sec
-= t2
->tv_sec
;
504 t1
->tv_usec
-= t2
->tv_usec
;
512 if (t1
->tv_usec
< 0) {
514 t1
->tv_usec
+= 1000000;
516 if (t1
->tv_usec
>= 1000000) {
518 t1
->tv_usec
-= 1000000;
523 * Return the best possible estimate of the time in the timeval
524 * to which tvp points.
530 clock_get_calendar_microtime(&tvp
->tv_sec
, &tvp
->tv_usec
);
537 clock_get_system_microtime(&tvp
->tv_sec
, &tvp
->tv_usec
);
541 * Ditto for timespec.
545 struct timespec
*tsp
)
547 clock_get_calendar_nanotime((uint32_t *)&tsp
->tv_sec
, &tsp
->tv_nsec
);
552 struct timespec
*tsp
)
554 clock_get_system_nanotime((uint32_t *)&tsp
->tv_sec
, &tsp
->tv_nsec
);
561 uint64_t result
, usresult
;
563 clock_interval_to_absolutetime_interval(
564 tvp
->tv_sec
, NSEC_PER_SEC
, &result
);
565 clock_interval_to_absolutetime_interval(
566 tvp
->tv_usec
, NSEC_PER_USEC
, &usresult
);
568 return (result
+ usresult
);
571 time_zone_slock_init(void)
573 extern simple_lock_data_t tz_slock
;
575 simple_lock_init(&tz_slock
);