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>
51 typedef unsigned long clock_sec_t
;
52 typedef unsigned int clock_usec_t
, clock_nsec_t
;
56 typedef uint32_t clock_sec_t
;
57 typedef uint32_t clock_usec_t
, clock_nsec_t
;
61 #ifdef MACH_KERNEL_PRIVATE
63 #include <kern/queue.h>
66 * Clock operations list structure. Contains vectors to machine
67 * dependent clock routines.
70 int (*c_config
)(void); /* configuration */
72 int (*c_init
)(void); /* initialize */
74 kern_return_t (*c_gettime
)( /* get time */
75 mach_timespec_t
*cur_time
);
77 kern_return_t (*c_getattr
)( /* get attributes */
78 clock_flavor_t flavor
,
80 mach_msg_type_number_t
*count
);
82 typedef const struct clock_ops
*clock_ops_t
;
83 typedef struct clock_ops clock_ops_data_t
;
86 * Actual clock object data structure. Contains the machine
87 * dependent operations list and clock operation ports.
90 clock_ops_t cl_ops
; /* operations list */
91 struct ipc_port
*cl_service
; /* service port */
92 struct ipc_port
*cl_control
; /* control port */
94 typedef struct clock clock_data_t
;
97 * Configure the clock system.
99 extern void clock_config(void);
100 extern void clock_oldconfig(void);
103 * Initialize the clock system.
105 extern void clock_init(void);
106 extern void clock_oldinit(void);
108 extern void clock_timebase_init(void);
111 * Initialize the clock ipc service facility.
113 extern void clock_service_create(void);
115 extern void clock_gettimeofday_set_commpage(
120 uint64_t tick_per_sec
);
122 extern void machine_delay_until(uint64_t interval
,
125 extern uint32_t hz_tick_interval
;
127 extern void nanotime_to_absolutetime(
129 clock_nsec_t nanosecs
,
132 #endif /* MACH_KERNEL_PRIVATE */
136 #ifdef XNU_KERNEL_PRIVATE
138 extern void clock_adjtime(
142 extern void clock_initialize_calendar(void);
144 extern void clock_wakeup_calendar(void);
146 extern void clock_update_calendar(void);
148 extern void clock_get_calendar_uptime(clock_sec_t
*secs
);
150 extern void clock_gettimeofday_new(clock_sec_t
*secs
,
151 clock_usec_t
*microsecs
);
152 extern void clock_gettimeofday(
154 clock_usec_t
*microsecs
);
156 extern void clock_gettimeofday_and_absolute_time(
158 clock_usec_t
*microsecs
,
159 uint64_t *absolute_time
);
162 extern void clock_set_calendar_microtime(
164 clock_usec_t microsecs
);
166 extern void clock_get_boottime_nanotime(
168 clock_nsec_t
*nanosecs
);
170 extern void clock_get_boottime_microtime(
172 clock_nsec_t
*microsecs
);
174 extern void absolutetime_to_microtime(
177 clock_usec_t
*microsecs
);
179 extern void clock_deadline_for_periodic_event(
186 extern void clock_get_calendar_nanotime_nowait(
188 clock_nsec_t
*nanosecs
);
190 #endif /* CONFIG_DTRACE */
192 boolean_t
kdp_clock_is_locked(void);
194 #endif /* XNU_KERNEL_PRIVATE */
196 extern void clock_get_calendar_microtime(
198 clock_usec_t
*microsecs
);
200 extern void clock_get_calendar_absolute_and_microtime(
202 clock_usec_t
*microsecs
,
205 extern void clock_get_calendar_nanotime(
207 clock_nsec_t
*nanosecs
);
209 extern void clock_get_system_microtime(
211 clock_usec_t
*microsecs
);
213 extern void clock_get_system_nanotime(
215 clock_nsec_t
*nanosecs
);
217 extern void clock_timebase_info(
218 mach_timebase_info_t info
);
220 extern void clock_get_uptime(
223 extern void clock_interval_to_deadline(
225 uint32_t scale_factor
,
228 extern void clock_interval_to_absolutetime_interval(
230 uint32_t scale_factor
,
233 extern void clock_absolutetime_interval_to_deadline(
237 extern void clock_continuoustime_interval_to_deadline(
241 extern void clock_delay_until(
244 extern void absolutetime_to_nanoseconds(
248 extern void nanoseconds_to_absolutetime(
249 uint64_t nanoseconds
,
253 * Absolute <-> Continuous Time conversion routines
255 * It is the caller's responsibility to ensure that these functions are
256 * synchronized with respect to updates to the continuous timebase. The
257 * returned value is only valid until the next update to the continuous
260 * If the value to be returned by continuoustime_to_absolutetime would be
261 * negative, zero is returned. This occurs when the provided continuous time
262 * is less the amount of the time the system spent asleep and /must/ be
265 extern uint64_t absolutetime_to_continuoustime(
267 extern uint64_t continuoustime_to_absolutetime(
270 extern uint64_t mach_absolutetime_asleep
;
271 extern uint64_t mach_absolutetime_last_sleep
;
273 #ifdef KERNEL_PRIVATE
276 * Obsolete interfaces.
281 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
282 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
284 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
285 MACH_TIMESPEC_SEC_MAX, \
286 MACH_TIMESPEC_NSEC_MAX } )
287 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
289 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
291 (t1)->tv_nsec += (clock_res_t)(nsec); \
292 if ((clock_res_t)(nsec) > 0 && \
293 (t1)->tv_nsec >= NSEC_PER_SEC) { \
294 (t1)->tv_nsec -= NSEC_PER_SEC; \
297 else if ((clock_res_t)(nsec) < 0 && \
298 (t1)->tv_nsec < 0) { \
299 (t1)->tv_nsec += NSEC_PER_SEC; \
304 #include <Availability.h>
306 /* Use mach_absolute_time() */
307 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
);
309 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
);
313 #ifdef XNU_KERNEL_PRIVATE
315 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
317 #endif /* XNU_KERNEL_PRIVATE */
319 #endif /* __LP64__ */
321 extern void delay_for_interval(
323 uint32_t scale_factor
);
325 extern void delay_for_interval_with_leeway(
328 uint32_t scale_factor
);
330 #ifdef XNU_KERNEL_PRIVATE
331 extern void delay(int usec
);
332 #endif /* XNU_KERNEL_PRIVATE */
334 #endif /* KERNEL_PRIVATE */
338 #endif /* _KERN_CLOCK_H_ */