2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34 #include <mach/mach_types.h>
37 #include <kern/sched_prim.h>
38 #include <kern/thread.h>
39 #include <kern/clock.h>
40 #include <kern/host_notify.h>
42 #include <IOKit/IOPlatformExpert.h>
44 #include <machine/commpage.h>
46 #include <mach/mach_traps.h>
47 #include <mach/mach_time.h>
49 #include <sys/kdebug.h>
51 uint32_t hz_tick_interval
= 1;
54 decl_simple_lock_data(,clock_lock
)
56 #define clock_lock() \
57 simple_lock(&clock_lock)
59 #define clock_unlock() \
60 simple_unlock(&clock_lock)
62 #define clock_lock_init() \
63 simple_lock_init(&clock_lock, 0)
67 * Time of day (calendar) variables.
71 * TOD <- (seconds + epoch, fraction) <- CONV(current absolute time + offset)
73 * where CONV converts absolute time units into seconds and a fraction.
75 static struct clock_calend
{
78 uint64_t epoch_absolute
;
80 int32_t adjdelta
; /* Nanosecond time delta for this adjustment period */
81 uint64_t adjstart
; /* Absolute time value for start of this adjustment period */
82 uint32_t adjoffset
; /* Absolute time offset for this adjustment period as absolute value */
88 * Unlocked calendar flipflop; this is used to track a clock_calend such
89 * that we can safely access a snapshot of a valid clock_calend structure
90 * without needing to take any locks to do it.
92 * The trick is to use a generation count and set the low bit when it is
93 * being updated/read; by doing this, we guarantee, through use of the
94 * hw_atomic functions, that the generation is incremented when the bit
95 * is cleared atomically (by using a 1 bit add).
97 static struct unlocked_clock_calend
{
98 struct clock_calend calend
; /* copy of calendar */
99 uint32_t gen
; /* generation count */
102 static void clock_track_calend_nowait(void);
107 * Calendar adjustment variables and values.
109 #define calend_adjperiod (NSEC_PER_SEC / 100) /* adjustment period, ns */
110 #define calend_adjskew (40 * NSEC_PER_USEC) /* "standard" skew, ns / period */
111 #define calend_adjbig (NSEC_PER_SEC) /* use 10x skew above adjbig ns */
113 static int64_t calend_adjtotal
; /* Nanosecond remaining total adjustment */
114 static uint64_t calend_adjdeadline
; /* Absolute time value for next adjustment period */
115 static uint32_t calend_adjinterval
; /* Absolute time interval of adjustment period */
117 static timer_call_data_t calend_adjcall
;
118 static uint32_t calend_adjactive
;
120 static uint32_t calend_set_adjustment(
124 static void calend_adjust_call(void);
125 static uint32_t calend_adjust(void);
127 void _clock_delay_until_deadline(uint64_t interval
,
129 void _clock_delay_until_deadline_with_leeway(uint64_t interval
,
133 static uint64_t clock_boottime
; /* Seconds boottime epoch */
135 #define TIME_ADD(rsecs, secs, rfrac, frac, unit) \
137 if (((rfrac) += (frac)) >= (unit)) { \
144 #define TIME_SUB(rsecs, secs, rfrac, frac, unit) \
146 if ((int)((rfrac) -= (frac)) < 0) { \
156 * Called once at boot to configure the clock subsystem.
163 timer_call_setup(&calend_adjcall
, (timer_call_func_t
)calend_adjust_call
, NULL
);
171 * Called on a processor each time started.
180 * clock_timebase_init:
182 * Called by machine dependent code
183 * to initialize areas dependent on the
184 * timebase value. May be called multiple
185 * times during start up.
188 clock_timebase_init(void)
192 nanoseconds_to_absolutetime(calend_adjperiod
, &abstime
);
193 calend_adjinterval
= (uint32_t)abstime
;
195 nanoseconds_to_absolutetime(NSEC_PER_SEC
/ 100, &abstime
);
196 hz_tick_interval
= (uint32_t)abstime
;
198 sched_timebase_init();
202 * mach_timebase_info_trap:
204 * User trap returns timebase constant.
207 mach_timebase_info_trap(
208 struct mach_timebase_info_trap_args
*args
)
210 mach_vm_address_t out_info_addr
= args
->info
;
211 mach_timebase_info_data_t info
;
213 clock_timebase_info(&info
);
215 copyout((void *)&info
, out_info_addr
, sizeof (info
));
217 return (KERN_SUCCESS
);
225 * clock_get_calendar_microtime:
227 * Returns the current calendar value,
228 * microseconds as the fraction.
231 clock_get_calendar_microtime(
233 clock_usec_t
*microsecs
)
235 clock_get_calendar_absolute_and_microtime(secs
, microsecs
, NULL
);
239 * clock_get_calendar_absolute_and_microtime:
241 * Returns the current calendar value,
242 * microseconds as the fraction. Also
243 * returns mach_absolute_time if abstime
247 clock_get_calendar_absolute_and_microtime(
249 clock_usec_t
*microsecs
,
258 now
= mach_absolute_time();
262 if (clock_calend
.adjdelta
< 0) {
266 * Since offset is decremented during a negative adjustment,
267 * ensure that time increases monotonically without going
268 * temporarily backwards.
269 * If the delta has not yet passed, now is set to the start
270 * of the current adjustment period; otherwise, we're between
271 * the expiry of the delta and the next call to calend_adjust(),
272 * and we offset accordingly.
274 if (now
> clock_calend
.adjstart
) {
275 t32
= (uint32_t)(now
- clock_calend
.adjstart
);
277 if (t32
> clock_calend
.adjoffset
)
278 now
-= clock_calend
.adjoffset
;
280 now
= clock_calend
.adjstart
;
284 now
+= clock_calend
.offset
;
286 absolutetime_to_microtime(now
, secs
, microsecs
);
288 *secs
+= (clock_sec_t
)clock_calend
.epoch
;
295 * clock_get_calendar_nanotime:
297 * Returns the current calendar value,
298 * nanoseconds as the fraction.
300 * Since we do not have an interface to
301 * set the calendar with resolution greater
302 * than a microsecond, we honor that here.
305 clock_get_calendar_nanotime(
307 clock_nsec_t
*nanosecs
)
315 now
= mach_absolute_time();
317 if (clock_calend
.adjdelta
< 0) {
320 if (now
> clock_calend
.adjstart
) {
321 t32
= (uint32_t)(now
- clock_calend
.adjstart
);
323 if (t32
> clock_calend
.adjoffset
)
324 now
-= clock_calend
.adjoffset
;
326 now
= clock_calend
.adjstart
;
330 now
+= clock_calend
.offset
;
332 absolutetime_to_microtime(now
, secs
, nanosecs
);
334 *nanosecs
*= NSEC_PER_USEC
;
336 *secs
+= (clock_sec_t
)clock_calend
.epoch
;
343 * clock_gettimeofday:
345 * Kernel interface for commpage implementation of
346 * gettimeofday() syscall.
348 * Returns the current calendar value, and updates the
349 * commpage info as appropriate. Because most calls to
350 * gettimeofday() are handled in user mode by the commpage,
351 * this routine should be used infrequently.
356 clock_usec_t
*microsecs
)
364 now
= mach_absolute_time();
366 if (clock_calend
.adjdelta
>= 0) {
367 clock_gettimeofday_set_commpage(now
, clock_calend
.epoch
, clock_calend
.offset
, secs
, microsecs
);
372 if (now
> clock_calend
.adjstart
) {
373 t32
= (uint32_t)(now
- clock_calend
.adjstart
);
375 if (t32
> clock_calend
.adjoffset
)
376 now
-= clock_calend
.adjoffset
;
378 now
= clock_calend
.adjstart
;
381 now
+= clock_calend
.offset
;
383 absolutetime_to_microtime(now
, secs
, microsecs
);
385 *secs
+= (clock_sec_t
)clock_calend
.epoch
;
393 * clock_set_calendar_microtime:
395 * Sets the current calendar value by
396 * recalculating the epoch and offset
397 * from the system clock.
399 * Also adjusts the boottime to keep the
400 * value consistent, writes the new
401 * calendar value to the platform clock,
402 * and sends calendar change notifications.
405 clock_set_calendar_microtime(
407 clock_usec_t microsecs
)
410 clock_usec_t microsys
;
412 clock_usec_t newmicrosecs
;
416 newmicrosecs
= microsecs
;
421 commpage_disable_timestamp();
424 * Calculate the new calendar epoch based on
425 * the new value and the system clock.
427 clock_get_system_microtime(&sys
, µsys
);
428 TIME_SUB(secs
, sys
, microsecs
, microsys
, USEC_PER_SEC
);
431 * Adjust the boottime based on the delta.
433 clock_boottime
+= secs
- clock_calend
.epoch
;
436 * Set the new calendar epoch.
438 clock_calend
.epoch
= secs
;
440 nanoseconds_to_absolutetime((uint64_t)microsecs
* NSEC_PER_USEC
, &clock_calend
.offset
);
442 clock_interval_to_absolutetime_interval((uint32_t) secs
, NSEC_PER_SEC
, &clock_calend
.epoch_absolute
);
443 clock_calend
.epoch_absolute
+= clock_calend
.offset
;
446 * Cancel any adjustment in progress.
448 calend_adjtotal
= clock_calend
.adjdelta
= 0;
453 * Set the new value for the platform clock.
455 PESetUTCTimeOfDay(newsecs
, newmicrosecs
);
460 * Send host notifications.
462 host_notify_calendar_change();
465 clock_track_calend_nowait();
470 * clock_initialize_calendar:
472 * Set the calendar and related clocks
473 * from the platform clock at boot or
476 * Also sends host notifications.
479 uint64_t mach_absolutetime_asleep
;
480 uint64_t mach_absolutetime_last_sleep
;
483 clock_initialize_calendar(void)
485 clock_sec_t sys
, secs
;
486 clock_usec_t microsys
, microsecs
;
490 PEGetUTCTimeOfDay(&secs
, µsecs
);
495 commpage_disable_timestamp();
497 if ((long)secs
>= (long)clock_boottime
) {
499 * Initialize the boot time based on the platform clock.
501 if (clock_boottime
== 0)
502 clock_boottime
= secs
;
505 * Calculate the new calendar epoch based on
506 * the platform clock and the system clock.
508 clock_get_system_microtime(&sys
, µsys
);
509 TIME_SUB(secs
, sys
, microsecs
, microsys
, USEC_PER_SEC
);
512 * Set the new calendar epoch.
515 clock_calend
.epoch
= secs
;
517 nanoseconds_to_absolutetime((uint64_t)microsecs
* NSEC_PER_USEC
, &clock_calend
.offset
);
519 clock_interval_to_absolutetime_interval((uint32_t) secs
, NSEC_PER_SEC
, &new_epoch
);
520 new_epoch
+= clock_calend
.offset
;
522 if (clock_calend
.epoch_absolute
)
524 mach_absolutetime_last_sleep
= new_epoch
- clock_calend
.epoch_absolute
;
525 mach_absolutetime_asleep
+= mach_absolutetime_last_sleep
;
526 KERNEL_DEBUG_CONSTANT(
527 MACHDBG_CODE(DBG_MACH_CLOCK
,MACH_EPOCH_CHANGE
) | DBG_FUNC_NONE
,
528 (uintptr_t) mach_absolutetime_last_sleep
,
529 (uintptr_t) mach_absolutetime_asleep
,
530 (uintptr_t) (mach_absolutetime_last_sleep
>> 32),
531 (uintptr_t) (mach_absolutetime_asleep
>> 32),
534 clock_calend
.epoch_absolute
= new_epoch
;
537 * Cancel any adjustment in progress.
539 calend_adjtotal
= clock_calend
.adjdelta
= 0;
546 * Send host notifications.
548 host_notify_calendar_change();
551 clock_track_calend_nowait();
556 * clock_get_boottime_nanotime:
558 * Return the boottime, used by sysctl.
561 clock_get_boottime_nanotime(
563 clock_nsec_t
*nanosecs
)
570 *secs
= (clock_sec_t
)clock_boottime
;
580 * Interface to adjtime() syscall.
582 * Calculates adjustment variables and
583 * initiates adjustment.
596 interval
= calend_set_adjustment(secs
, microsecs
);
598 calend_adjdeadline
= mach_absolute_time() + interval
;
599 if (!timer_call_enter(&calend_adjcall
, calend_adjdeadline
, TIMER_CALL_SYS_CRITICAL
))
603 if (timer_call_cancel(&calend_adjcall
))
611 calend_set_adjustment(
616 int64_t total
, ototal
;
617 uint32_t interval
= 0;
620 * Compute the total adjustment time in nanoseconds.
622 total
= ((int64_t)*secs
* (int64_t)NSEC_PER_SEC
) + (*microsecs
* (int64_t)NSEC_PER_USEC
);
625 * Disable commpage gettimeofday().
627 commpage_disable_timestamp();
630 * Get current absolute time.
632 now
= mach_absolute_time();
635 * Save the old adjustment total for later return.
637 ototal
= calend_adjtotal
;
640 * Is a new correction specified?
644 * Set delta to the standard, small, adjustment skew.
646 int32_t delta
= calend_adjskew
;
650 * Positive adjustment. If greater than the preset 'big'
651 * threshold, slew at a faster rate, capping if necessary.
653 if (total
> (int64_t) calend_adjbig
)
656 delta
= (int32_t)total
;
659 * Convert the delta back from ns to absolute time and store in adjoffset.
661 nanoseconds_to_absolutetime((uint64_t)delta
, &t64
);
662 clock_calend
.adjoffset
= (uint32_t)t64
;
666 * Negative adjustment; therefore, negate the delta. If
667 * greater than the preset 'big' threshold, slew at a faster
668 * rate, capping if necessary.
670 if (total
< (int64_t) -calend_adjbig
)
674 delta
= (int32_t)total
;
677 * Save the current absolute time. Subsequent time operations occuring
678 * during this negative correction can make use of this value to ensure
679 * that time increases monotonically.
681 clock_calend
.adjstart
= now
;
684 * Convert the delta back from ns to absolute time and store in adjoffset.
686 nanoseconds_to_absolutetime((uint64_t)-delta
, &t64
);
687 clock_calend
.adjoffset
= (uint32_t)t64
;
691 * Store the total adjustment time in ns.
693 calend_adjtotal
= total
;
696 * Store the delta for this adjustment period in ns.
698 clock_calend
.adjdelta
= delta
;
701 * Set the interval in absolute time for later return.
703 interval
= calend_adjinterval
;
707 * No change; clear any prior adjustment.
709 calend_adjtotal
= clock_calend
.adjdelta
= 0;
713 * If an prior correction was in progress, return the
714 * remaining uncorrected time from it.
717 *secs
= (long)(ototal
/ (long)NSEC_PER_SEC
);
718 *microsecs
= (int)((ototal
% (int)NSEC_PER_SEC
) / (int)NSEC_PER_USEC
);
721 *secs
= *microsecs
= 0;
724 clock_track_calend_nowait();
731 calend_adjust_call(void)
739 if (--calend_adjactive
== 0) {
740 interval
= calend_adjust();
742 clock_deadline_for_periodic_event(interval
, mach_absolute_time(), &calend_adjdeadline
);
744 if (!timer_call_enter(&calend_adjcall
, calend_adjdeadline
, TIMER_CALL_SYS_CRITICAL
))
758 uint32_t interval
= 0;
760 commpage_disable_timestamp();
762 now
= mach_absolute_time();
764 delta
= clock_calend
.adjdelta
;
767 clock_calend
.offset
+= clock_calend
.adjoffset
;
769 calend_adjtotal
-= delta
;
770 if (delta
> calend_adjtotal
) {
771 clock_calend
.adjdelta
= delta
= (int32_t)calend_adjtotal
;
773 nanoseconds_to_absolutetime((uint64_t)delta
, &t64
);
774 clock_calend
.adjoffset
= (uint32_t)t64
;
779 clock_calend
.offset
-= clock_calend
.adjoffset
;
781 calend_adjtotal
-= delta
;
782 if (delta
< calend_adjtotal
) {
783 clock_calend
.adjdelta
= delta
= (int32_t)calend_adjtotal
;
785 nanoseconds_to_absolutetime((uint64_t)-delta
, &t64
);
786 clock_calend
.adjoffset
= (uint32_t)t64
;
789 if (clock_calend
.adjdelta
!= 0)
790 clock_calend
.adjstart
= now
;
793 if (clock_calend
.adjdelta
!= 0)
794 interval
= calend_adjinterval
;
797 clock_track_calend_nowait();
804 * Wait / delay routines.
807 mach_wait_until_continue(
808 __unused
void *parameter
,
809 wait_result_t wresult
)
811 thread_syscall_return((wresult
== THREAD_INTERRUPTED
)? KERN_ABORTED
: KERN_SUCCESS
);
816 * mach_wait_until_trap: Suspend execution of calling thread until the specified time has passed
818 * Parameters: args->deadline Amount of time to wait
825 mach_wait_until_trap(
826 struct mach_wait_until_trap_args
*args
)
828 uint64_t deadline
= args
->deadline
;
829 wait_result_t wresult
;
831 wresult
= assert_wait_deadline_with_leeway((event_t
)mach_wait_until_trap
, THREAD_ABORTSAFE
,
832 TIMEOUT_URGENCY_USER_NORMAL
, deadline
, 0);
833 if (wresult
== THREAD_WAITING
)
834 wresult
= thread_block(mach_wait_until_continue
);
836 return ((wresult
== THREAD_INTERRUPTED
)? KERN_ABORTED
: KERN_SUCCESS
);
843 uint64_t now
= mach_absolute_time();
848 _clock_delay_until_deadline(deadline
- now
, deadline
);
852 * Preserve the original precise interval that the client
853 * requested for comparison to the spin threshold.
856 _clock_delay_until_deadline(
860 _clock_delay_until_deadline_with_leeway(interval
, deadline
, 0);
864 * Like _clock_delay_until_deadline, but it accepts a
868 _clock_delay_until_deadline_with_leeway(
877 if ( ml_delay_should_spin(interval
) ||
878 get_preemption_level() != 0 ||
879 ml_get_interrupts_enabled() == FALSE
) {
880 machine_delay_until(interval
, deadline
);
883 * For now, assume a leeway request of 0 means the client does not want a leeway
884 * value. We may want to change this interpretation in the future.
888 assert_wait_deadline_with_leeway((event_t
)clock_delay_until
, THREAD_UNINT
, TIMEOUT_URGENCY_LEEWAY
, deadline
, leeway
);
890 assert_wait_deadline((event_t
)clock_delay_until
, THREAD_UNINT
, deadline
);
893 thread_block(THREAD_CONTINUE_NULL
);
900 uint32_t scale_factor
)
904 clock_interval_to_absolutetime_interval(interval
, scale_factor
, &abstime
);
906 _clock_delay_until_deadline(abstime
, mach_absolute_time() + abstime
);
910 delay_for_interval_with_leeway(
913 uint32_t scale_factor
)
915 uint64_t abstime_interval
;
916 uint64_t abstime_leeway
;
918 clock_interval_to_absolutetime_interval(interval
, scale_factor
, &abstime_interval
);
919 clock_interval_to_absolutetime_interval(leeway
, scale_factor
, &abstime_leeway
);
921 _clock_delay_until_deadline_with_leeway(abstime_interval
, mach_absolute_time() + abstime_interval
, abstime_leeway
);
928 delay_for_interval((usec
< 0)? -usec
: usec
, NSEC_PER_USEC
);
932 * Miscellaneous routines.
935 clock_interval_to_deadline(
937 uint32_t scale_factor
,
942 clock_interval_to_absolutetime_interval(interval
, scale_factor
, &abstime
);
944 *result
= mach_absolute_time() + abstime
;
948 clock_absolutetime_interval_to_deadline(
952 *result
= mach_absolute_time() + abstime
;
959 *result
= mach_absolute_time();
963 clock_deadline_for_periodic_event(
968 assert(interval
!= 0);
970 *deadline
+= interval
;
972 if (*deadline
<= abstime
) {
973 *deadline
= abstime
+ interval
;
974 abstime
= mach_absolute_time();
976 if (*deadline
<= abstime
)
977 *deadline
= abstime
+ interval
;
984 * clock_get_calendar_nanotime_nowait
986 * Description: Non-blocking version of clock_get_calendar_nanotime()
988 * Notes: This function operates by separately tracking calendar time
989 * updates using a two element structure to copy the calendar
990 * state, which may be asynchronously modified. It utilizes
991 * barrier instructions in the tracking process and in the local
992 * stable snapshot process in order to ensure that a consistent
993 * snapshot is used to perform the calculation.
996 clock_get_calendar_nanotime_nowait(
998 clock_nsec_t
*nanosecs
)
1002 struct unlocked_clock_calend stable
;
1005 stable
= flipflop
[i
]; /* take snapshot */
1008 * Use a barrier instructions to ensure atomicity. We AND
1009 * off the "in progress" bit to get the current generation
1012 (void)hw_atomic_and(&stable
.gen
, ~(uint32_t)1);
1015 * If an update _is_ in progress, the generation count will be
1016 * off by one, if it _was_ in progress, it will be off by two,
1017 * and if we caught it at a good time, it will be equal (and
1018 * our snapshot is threfore stable).
1020 if (flipflop
[i
].gen
== stable
.gen
)
1023 /* Switch to the oher element of the flipflop, and try again. */
1027 now
= mach_absolute_time();
1029 if (stable
.calend
.adjdelta
< 0) {
1032 if (now
> stable
.calend
.adjstart
) {
1033 t32
= (uint32_t)(now
- stable
.calend
.adjstart
);
1035 if (t32
> stable
.calend
.adjoffset
)
1036 now
-= stable
.calend
.adjoffset
;
1038 now
= stable
.calend
.adjstart
;
1042 now
+= stable
.calend
.offset
;
1044 absolutetime_to_microtime(now
, secs
, nanosecs
);
1045 *nanosecs
*= NSEC_PER_USEC
;
1047 *secs
+= (clock_sec_t
)stable
.calend
.epoch
;
1051 clock_track_calend_nowait(void)
1055 for (i
= 0; i
< 2; i
++) {
1056 struct clock_calend tmp
= clock_calend
;
1059 * Set the low bit if the generation count; since we use a
1060 * barrier instruction to do this, we are guaranteed that this
1061 * will flag an update in progress to an async caller trying
1062 * to examine the contents.
1064 (void)hw_atomic_or(&flipflop
[i
].gen
, 1);
1066 flipflop
[i
].calend
= tmp
;
1069 * Increment the generation count to clear the low bit to
1070 * signal completion. If a caller compares the generation
1071 * count after taking a copy while in progress, the count
1072 * will be off by two.
1074 (void)hw_atomic_add(&flipflop
[i
].gen
, 1);
1078 #endif /* CONFIG_DTRACE */