2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
61 #include <sys/param.h>
62 #include <sys/resourcevar.h>
63 #include <sys/kernel.h>
64 #include <sys/systm.h>
66 #include <sys/vnode.h>
68 #include <sys/mount.h>
70 #include <kern/clock.h>
72 #define HZ 100 /* XXX */
74 volatile struct timeval time
;
75 /* simple lock used to access timezone, tz structure */
76 decl_simple_lock_data(, tz_slock
);
78 * Time of day and interval timer support.
80 * These routines provide the kernel entry points to get and set
81 * the time-of-day and per-process interval timers. Subroutines
82 * here provide support for adding and subtracting timeval structures
83 * and decrementing interval timers, optionally reloading the interval
84 * timers when they expire.
86 struct gettimeofday_args
{
92 gettimeofday(p
, uap
, retval
)
94 register struct gettimeofday_args
*uap
;
99 extern simple_lock_data_t tz_slock
;
100 struct timezone ltz
; /* local copy */
102 /* NOTE THIS implementation is for non ppc architectures only */
106 if (error
= copyout((caddr_t
)&atv
, (caddr_t
)uap
->tp
,
112 usimple_lock(&tz_slock
);
114 usimple_unlock(&tz_slock
);
115 error
= copyout((caddr_t
)<z
, (caddr_t
)uap
->tzp
,
122 struct settimeofday_args
{
124 struct timezone
*tzp
;
128 settimeofday(p
, uap
, retval
)
130 struct settimeofday_args
*uap
;
136 extern simple_lock_data_t tz_slock
;
138 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
140 /* Verify all parameters before changing time. */
141 if (uap
->tv
&& (error
= copyin((caddr_t
)uap
->tv
,
142 (caddr_t
)&atv
, sizeof(atv
))))
144 if (uap
->tzp
&& (error
= copyin((caddr_t
)uap
->tzp
,
145 (caddr_t
)&atz
, sizeof(atz
))))
150 usimple_lock(&tz_slock
);
152 usimple_unlock(&tz_slock
);
160 long delta
= tv
->tv_sec
- time
.tv_sec
;
163 now
.tv_sec
= tv
->tv_sec
;
164 now
.tv_nsec
= tv
->tv_usec
* NSEC_PER_USEC
;
166 clock_set_calendar_value(now
);
167 boottime
.tv_sec
+= delta
;
168 #if NFSCLIENT || NFSSERVER
169 lease_updatetime(delta
);
173 #define tickadj (40 * NSEC_PER_USEC) /* "standard" skew, ns / 10 ms */
174 #define bigadj (1 * NSEC_PER_SEC) /* use 10x skew above bigadj ns */
176 struct adjtime_args
{
177 struct timeval
*delta
;
178 struct timeval
*olddelta
;
182 adjtime(p
, uap
, retval
)
184 register struct adjtime_args
*uap
;
192 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
194 if (error
= copyin((caddr_t
)uap
->delta
,
195 (caddr_t
)&atv
, sizeof (struct timeval
)))
199 * Compute the total correction and the rate at which to apply it.
201 total
= (int64_t)atv
.tv_sec
* NSEC_PER_SEC
+ atv
.tv_usec
* NSEC_PER_USEC
;
202 if (total
> bigadj
|| total
< -bigadj
)
203 delta
= 10 * tickadj
;
207 total
= clock_set_calendar_adjtime(total
, delta
);
210 atv
.tv_sec
= total
/ NSEC_PER_SEC
;
211 atv
.tv_usec
= (total
/ NSEC_PER_USEC
) % USEC_PER_SEC
;
212 (void) copyout((caddr_t
)&atv
,
213 (caddr_t
)uap
->olddelta
, sizeof (struct timeval
));
220 * Initialze the time of day register.
221 * Trust the RTC except for the case where it is set before
222 * the UNIX epoch. In that case use the the UNIX epoch.
223 * The argument passed in is ignored.
231 * The calendar has already been
232 * set up from the battery clock.
234 * The value returned by microtime()
235 * is gotten from the calendar.
240 * This variable still exists to keep
241 * 'w' happy. It should only be considered
244 boottime
.tv_sec
= time
.tv_sec
;
245 boottime
.tv_usec
= 0;
248 * If the RTC does not have acceptable value, i.e. time before
249 * the UNIX epoch, set it to the UNIX epoch
251 if (time
.tv_sec
< 0) {
252 printf ("WARNING: preposterous time in Real Time Clock");
253 time
.tv_sec
= 0; /* the UNIX epoch */
257 printf(" -- CHECK AND RESET THE DATE!\n");
274 struct timeval
*tvp
);
277 * Get value of an interval timer. The process virtual and
278 * profiling virtual time timers are kept internally in the
279 * way they are specified externally: in time until they expire.
281 * The real time interval timer expiration time (p_rtime)
282 * is kept as an absolute time rather than as a delta, so that
283 * it is easy to keep periodic real-time signals from drifting.
285 * Virtual time timers are processed in the hardclock() routine of
286 * kern_clock.c. The real time timer is processed by a callout
287 * routine. Since a callout may be delayed in real time due to
288 * other processing in the system, it is possible for the real
289 * time callout routine (realitexpire, given below), to be delayed
290 * in real time past when it is supposed to occur. It does not
291 * suffice, therefore, to reload the real time .it_value from the
292 * real time .it_interval. Rather, we compute the next time in
293 * absolute time when the timer should go off.
296 struct getitimer_args
{
298 struct itimerval
*itv
;
302 getitimer(p
, uap
, retval
)
304 register struct getitimer_args
*uap
;
307 struct itimerval aitv
;
309 if (uap
->which
> ITIMER_PROF
)
311 if (uap
->which
== ITIMER_REAL
) {
313 * If time for real time timer has passed return 0,
314 * else return difference between current time and
315 * time for the timer to go off.
317 aitv
= p
->p_realtimer
;
318 if (timerisset(&p
->p_rtime
)) {
322 if (timercmp(&p
->p_rtime
, &now
, <))
323 timerclear(&aitv
.it_value
);
325 aitv
.it_value
= p
->p_rtime
;
326 timevalsub(&aitv
.it_value
, &now
);
330 timerclear(&aitv
.it_value
);
333 aitv
= p
->p_stats
->p_timer
[uap
->which
];
335 return (copyout((caddr_t
)&aitv
,
336 (caddr_t
)uap
->itv
, sizeof (struct itimerval
)));
339 struct setitimer_args
{
341 struct itimerval
*itv
;
342 struct itimerval
*oitv
;
346 setitimer(p
, uap
, retval
)
348 register struct setitimer_args
*uap
;
351 struct itimerval aitv
;
352 register struct itimerval
*itvp
;
355 if (uap
->which
> ITIMER_PROF
)
357 if ((itvp
= uap
->itv
) &&
358 (error
= copyin((caddr_t
)itvp
,
359 (caddr_t
)&aitv
, sizeof (struct itimerval
))))
361 if ((uap
->itv
= uap
->oitv
) && (error
= getitimer(p
, uap
, retval
)))
365 if (itimerfix(&aitv
.it_value
) || itimerfix(&aitv
.it_interval
))
367 if (uap
->which
== ITIMER_REAL
) {
368 thread_call_func_cancel(realitexpire
, (void *)p
->p_pid
, FALSE
);
369 if (timerisset(&aitv
.it_value
)) {
370 microuptime(&p
->p_rtime
);
371 timevaladd(&p
->p_rtime
, &aitv
.it_value
);
372 thread_call_func_delayed(
373 realitexpire
, (void *)p
->p_pid
,
374 tvtoabstime(&p
->p_rtime
));
377 timerclear(&p
->p_rtime
);
379 p
->p_realtimer
= aitv
;
382 p
->p_stats
->p_timer
[uap
->which
] = aitv
;
388 * Real interval timer expired:
389 * send process whose timer expired an alarm signal.
390 * If time is not set up to reload, then just return.
391 * Else compute next time timer should go off which is > current time.
392 * This is where delay in processing this timeout causes multiple
393 * SIGALRM calls to be compressed into one.
399 register struct proc
*p
;
401 boolean_t funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
403 p
= pfind((pid_t
)pid
);
405 (void) thread_funnel_set(kernel_flock
, FALSE
);
409 if (!timerisset(&p
->p_realtimer
.it_interval
)) {
410 timerclear(&p
->p_rtime
);
413 (void) thread_funnel_set(kernel_flock
, FALSE
);
418 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
419 if (timercmp(&p
->p_rtime
, &now
, <=)) {
420 if ((p
->p_rtime
.tv_sec
+ 2) >= now
.tv_sec
) {
422 timevaladd(&p
->p_rtime
, &p
->p_realtimer
.it_interval
);
423 if (timercmp(&p
->p_rtime
, &now
, >))
428 p
->p_rtime
= p
->p_realtimer
.it_interval
;
429 timevaladd(&p
->p_rtime
, &now
);
433 thread_call_func_delayed(realitexpire
, pid
, tvtoabstime(&p
->p_rtime
));
437 (void) thread_funnel_set(kernel_flock
, FALSE
);
441 * Check that a proposed value to load into the .it_value or
442 * .it_interval part of an interval timer is acceptable, and
443 * fix it to have at least minimal value (i.e. if it is less
444 * than the resolution of the clock, round it up.)
451 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
452 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
454 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
460 * Decrement an interval timer by a specified number
461 * of microseconds, which must be less than a second,
462 * i.e. < 1000000. If the timer expires, then reload
463 * it. In this case, carry over (usec - old value) to
464 * reducint the value reloaded into the timer so that
465 * the timer does not drift. This routine assumes
466 * that it is called in a context where the timers
467 * on which it is operating cannot change in value.
470 itimerdecr(itp
, usec
)
471 register struct itimerval
*itp
;
475 if (itp
->it_value
.tv_usec
< usec
) {
476 if (itp
->it_value
.tv_sec
== 0) {
477 /* expired, and already in next interval */
478 usec
-= itp
->it_value
.tv_usec
;
481 itp
->it_value
.tv_usec
+= 1000000;
482 itp
->it_value
.tv_sec
--;
484 itp
->it_value
.tv_usec
-= usec
;
486 if (timerisset(&itp
->it_value
))
488 /* expired, exactly at end of interval */
490 if (timerisset(&itp
->it_interval
)) {
491 itp
->it_value
= itp
->it_interval
;
492 itp
->it_value
.tv_usec
-= usec
;
493 if (itp
->it_value
.tv_usec
< 0) {
494 itp
->it_value
.tv_usec
+= 1000000;
495 itp
->it_value
.tv_sec
--;
498 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
503 * Add and subtract routines for timevals.
504 * N.B.: subtract routine doesn't deal with
505 * results which are before the beginning,
506 * it just gets very confused in this case.
515 t1
->tv_sec
+= t2
->tv_sec
;
516 t1
->tv_usec
+= t2
->tv_usec
;
525 t1
->tv_sec
-= t2
->tv_sec
;
526 t1
->tv_usec
-= t2
->tv_usec
;
534 if (t1
->tv_usec
< 0) {
536 t1
->tv_usec
+= 1000000;
538 if (t1
->tv_usec
>= 1000000) {
540 t1
->tv_usec
-= 1000000;
545 * Return the best possible estimate of the time in the timeval
546 * to which tvp points.
552 mach_timespec_t now
= clock_get_calendar_value();
554 tvp
->tv_sec
= now
.tv_sec
;
555 tvp
->tv_usec
= now
.tv_nsec
/ NSEC_PER_USEC
;
562 mach_timespec_t now
= clock_get_system_value();
564 tvp
->tv_sec
= now
.tv_sec
;
565 tvp
->tv_usec
= now
.tv_nsec
/ NSEC_PER_USEC
;
569 * Ditto for timespec.
573 struct timespec
*tsp
)
575 mach_timespec_t now
= clock_get_calendar_value();
577 tsp
->tv_sec
= now
.tv_sec
;
578 tsp
->tv_nsec
= now
.tv_nsec
;
583 struct timespec
*tsp
)
585 mach_timespec_t now
= clock_get_system_value();
587 tsp
->tv_sec
= now
.tv_sec
;
588 tsp
->tv_nsec
= now
.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 extern simple_lock_data_t tz_slock
;
609 simple_lock_init(&tz_slock
);