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>
43 #include <kern/kern_types.h>
45 #include <sys/cdefs.h>
47 #ifdef MACH_KERNEL_PRIVATE
49 #include <kern/queue.h>
52 * Clock operations list structure. Contains vectors to machine
53 * dependent clock routines.
56 int (*c_config
)(void); /* configuration */
58 int (*c_init
)(void); /* initialize */
60 kern_return_t (*c_gettime
)( /* get time */
61 mach_timespec_t
*cur_time
);
63 kern_return_t (*c_getattr
)( /* get attributes */
64 clock_flavor_t flavor
,
66 mach_msg_type_number_t
*count
);
68 typedef struct clock_ops
*clock_ops_t
;
69 typedef struct clock_ops clock_ops_data_t
;
72 * Actual clock object data structure. Contains the machine
73 * dependent operations list and clock operation ports.
76 clock_ops_t cl_ops
; /* operations list */
77 struct ipc_port
*cl_service
; /* service port */
78 struct ipc_port
*cl_control
; /* control port */
80 typedef struct clock clock_data_t
;
83 * Configure the clock system.
85 extern void clock_config(void);
86 extern void clock_oldconfig(void);
89 * Initialize the clock system.
91 extern void clock_init(void);
92 extern void clock_oldinit(void);
94 extern void clock_timebase_init(void);
97 * Initialize the clock ipc service facility.
99 extern void clock_service_create(void);
101 extern void clock_gettimeofday_set_commpage(
106 uint32_t *microsecs
);
108 extern void machine_delay_until(
111 #include <stat_time.h>
113 #if STAT_TIME || GPROF
115 extern void hertz_tick(
117 boolean_t usermode
, /* executing user code */
120 #endif /* STAT_TIME */
122 extern uint32_t hz_tick_interval
;
124 extern void absolutetime_to_nanotime(
129 extern void nanotime_to_absolutetime(
134 #endif /* MACH_KERNEL_PRIVATE */
138 #ifdef XNU_KERNEL_PRIVATE
140 extern void clock_adjtime(
144 extern void clock_initialize_calendar(void);
146 extern void clock_wakeup_calendar(void);
148 extern void clock_gettimeofday(
150 uint32_t *microsecs
);
152 extern void clock_set_calendar_microtime(
156 extern void clock_get_boottime_nanotime(
160 extern void absolutetime_to_microtime(
163 uint32_t *microsecs
);
165 extern void clock_deadline_for_periodic_event(
170 #endif /* XNU_KERNEL_PRIVATE */
172 extern void clock_get_calendar_microtime(
174 uint32_t *microsecs
);
176 extern void clock_get_calendar_nanotime(
181 * Gah! This file is included everywhere. The other domains do not correctly
182 * include config_dtrace headers, so this isn't being defined. The last test
183 * I ran stopped with a build failure in pexpert/i386/kd.c
186 extern void clock_get_calendar_nanotime_nowait(
189 #endif /* CONFIG_DTRACE */
191 extern void clock_get_system_microtime(
193 uint32_t *microsecs
);
195 extern void clock_get_system_nanotime(
199 extern void clock_timebase_info(
200 mach_timebase_info_t info
);
202 extern void clock_get_uptime(
205 extern void clock_interval_to_deadline(
207 uint32_t scale_factor
,
210 extern void clock_interval_to_absolutetime_interval(
212 uint32_t scale_factor
,
215 extern void clock_absolutetime_interval_to_deadline(
219 extern void clock_delay_until(
222 extern void absolutetime_to_nanoseconds(
226 extern void nanoseconds_to_absolutetime(
227 uint64_t nanoseconds
,
230 #ifdef KERNEL_PRIVATE
233 * Obsolete interfaces.
236 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
237 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
239 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
240 MACH_TIMESPEC_SEC_MAX, \
241 MACH_TIMESPEC_NSEC_MAX } )
242 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
244 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
246 (t1)->tv_nsec += (clock_res_t)(nsec); \
247 if ((clock_res_t)(nsec) > 0 && \
248 (t1)->tv_nsec >= NSEC_PER_SEC) { \
249 (t1)->tv_nsec -= NSEC_PER_SEC; \
252 else if ((clock_res_t)(nsec) < 0 && \
253 (t1)->tv_nsec < 0) { \
254 (t1)->tv_nsec += NSEC_PER_SEC; \
260 extern mach_timespec_t
clock_get_system_value(void);
262 extern mach_timespec_t
clock_get_calendar_value(void);
264 extern void delay_for_interval(
266 uint32_t scale_factor
);
267 #ifndef MACH_KERNEL_PRIVATE
269 #ifndef ABSOLUTETIME_SCALAR_TYPE
271 #define clock_get_uptime(a) \
272 clock_get_uptime(__OSAbsoluteTimePtr(a))
274 #define clock_interval_to_deadline(a, b, c) \
275 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
277 #define clock_interval_to_absolutetime_interval(a, b, c) \
278 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
280 #define clock_absolutetime_interval_to_deadline(a, b) \
281 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
283 #define clock_deadline_for_periodic_event(a, b, c) \
284 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
286 #define clock_delay_until(a) \
287 clock_delay_until(__OSAbsoluteTime(a))
289 #define absolutetime_to_nanoseconds(a, b) \
290 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
292 #define nanoseconds_to_absolutetime(a, b) \
293 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
295 #endif /* ABSOLUTETIME_SCALAR_TYPE */
297 #endif /* !MACH_KERNEL_PRIVATE */
299 #endif /* KERNEL_PRIVATE */
303 #endif /* _KERN_CLOCK_H_ */