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 #ifndef _KERN_CLOCK_H_
35 #define _KERN_CLOCK_H_
38 #include <mach/mach_types.h>
39 #include <mach/clock_types.h>
40 #include <mach/message.h>
41 #include <mach/mach_time.h>
42 #include <mach/boolean.h>
44 #include <kern/kern_types.h>
46 #include <sys/cdefs.h>
50 typedef unsigned long clock_sec_t
;
51 typedef unsigned int clock_usec_t
, clock_nsec_t
;
55 typedef uint32_t clock_sec_t
;
56 typedef uint32_t clock_usec_t
, clock_nsec_t
;
60 #ifdef MACH_KERNEL_PRIVATE
62 #include <kern/queue.h>
65 * Clock operations list structure. Contains vectors to machine
66 * dependent clock routines.
69 int (*c_config
)(void); /* configuration */
71 int (*c_init
)(void); /* initialize */
73 kern_return_t (*c_gettime
)( /* get time */
74 mach_timespec_t
*cur_time
);
76 kern_return_t (*c_getattr
)( /* get attributes */
77 clock_flavor_t flavor
,
79 mach_msg_type_number_t
*count
);
81 typedef struct clock_ops
*clock_ops_t
;
82 typedef struct clock_ops clock_ops_data_t
;
85 * Actual clock object data structure. Contains the machine
86 * dependent operations list and clock operation ports.
89 clock_ops_t cl_ops
; /* operations list */
90 struct ipc_port
*cl_service
; /* service port */
91 struct ipc_port
*cl_control
; /* control port */
93 typedef struct clock clock_data_t
;
96 * Configure the clock system.
98 extern void clock_config(void);
99 extern void clock_oldconfig(void);
102 * Initialize the clock system.
104 extern void clock_init(void);
105 extern void clock_oldinit(void);
107 extern void clock_timebase_init(void);
110 * Initialize the clock ipc service facility.
112 extern void clock_service_create(void);
114 extern void clock_gettimeofday_set_commpage(
119 clock_usec_t
*microsecs
);
121 extern void machine_delay_until(uint64_t interval
,
124 extern uint32_t hz_tick_interval
;
126 extern void nanotime_to_absolutetime(
128 clock_nsec_t nanosecs
,
131 #endif /* MACH_KERNEL_PRIVATE */
135 #ifdef XNU_KERNEL_PRIVATE
137 extern void clock_adjtime(
141 extern void clock_initialize_calendar(void);
143 extern void clock_wakeup_calendar(void);
145 extern void clock_gettimeofday(
147 clock_usec_t
*microsecs
);
149 extern void clock_gettimeofday_and_absolute_time(
151 clock_usec_t
*microsecs
,
152 uint64_t *absolute_time
);
155 extern void clock_set_calendar_microtime(
157 clock_usec_t microsecs
);
159 extern void clock_get_boottime_nanotime(
161 clock_nsec_t
*nanosecs
);
163 extern void clock_get_boottime_microtime(
165 clock_nsec_t
*microsecs
);
167 extern void absolutetime_to_microtime(
170 clock_usec_t
*microsecs
);
172 extern void clock_deadline_for_periodic_event(
179 extern void clock_get_calendar_nanotime_nowait(
181 clock_nsec_t
*nanosecs
);
183 #endif /* CONFIG_DTRACE */
185 boolean_t
kdp_clock_is_locked(void);
187 #endif /* XNU_KERNEL_PRIVATE */
189 extern void clock_get_calendar_microtime(
191 clock_usec_t
*microsecs
);
193 extern void clock_get_calendar_absolute_and_microtime(
195 clock_usec_t
*microsecs
,
198 extern void clock_get_calendar_nanotime(
200 clock_nsec_t
*nanosecs
);
202 extern void clock_get_system_microtime(
204 clock_usec_t
*microsecs
);
206 extern void clock_get_system_nanotime(
208 clock_nsec_t
*nanosecs
);
210 extern void clock_timebase_info(
211 mach_timebase_info_t info
);
213 extern void clock_get_uptime(
216 extern void clock_interval_to_deadline(
218 uint32_t scale_factor
,
221 extern void clock_interval_to_absolutetime_interval(
223 uint32_t scale_factor
,
226 extern void clock_absolutetime_interval_to_deadline(
230 extern void clock_continuoustime_interval_to_deadline(
234 extern void clock_delay_until(
237 extern void absolutetime_to_nanoseconds(
241 extern void nanoseconds_to_absolutetime(
242 uint64_t nanoseconds
,
246 * Absolute <-> Continuous Time conversion routines
248 * It is the caller's responsibility to ensure that these functions are
249 * synchronized with respect to updates to the continuous timebase. The
250 * returned value is only valid until the next update to the continuous
253 * If the value to be returned by continuoustime_to_absolutetime would be
254 * negative, zero is returned. This occurs when the provided continuous time
255 * is less the amount of the time the system spent asleep and /must/ be
258 extern uint64_t absolutetime_to_continuoustime(
260 extern uint64_t continuoustime_to_absolutetime(
263 extern uint64_t mach_absolutetime_asleep
;
264 extern uint64_t mach_absolutetime_last_sleep
;
266 #ifdef KERNEL_PRIVATE
269 * Obsolete interfaces.
274 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
275 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
277 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
278 MACH_TIMESPEC_SEC_MAX, \
279 MACH_TIMESPEC_NSEC_MAX } )
280 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
282 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
284 (t1)->tv_nsec += (clock_res_t)(nsec); \
285 if ((clock_res_t)(nsec) > 0 && \
286 (t1)->tv_nsec >= NSEC_PER_SEC) { \
287 (t1)->tv_nsec -= NSEC_PER_SEC; \
290 else if ((clock_res_t)(nsec) < 0 && \
291 (t1)->tv_nsec < 0) { \
292 (t1)->tv_nsec += NSEC_PER_SEC; \
297 #include <Availability.h>
299 /* Use mach_absolute_time() */
300 extern mach_timespec_t
clock_get_system_value(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_8
, __IPHONE_2_0
, __IPHONE_6_0
);
302 extern mach_timespec_t
clock_get_calendar_value(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0
, __MAC_10_8
, __IPHONE_2_0
, __IPHONE_6_0
);
306 #ifdef XNU_KERNEL_PRIVATE
308 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
310 #endif /* XNU_KERNEL_PRIVATE */
312 #endif /* __LP64__ */
314 extern void delay_for_interval(
316 uint32_t scale_factor
);
318 extern void delay_for_interval_with_leeway(
321 uint32_t scale_factor
);
323 #ifdef XNU_KERNEL_PRIVATE
324 extern void delay(int usec
);
325 #endif /* XNU_KERNEL_PRIVATE */
327 #endif /* KERNEL_PRIVATE */
331 #endif /* _KERN_CLOCK_H_ */