]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/clock.h
xnu-3789.51.2.tar.gz
[apple/xnu.git] / osfmk / kern / clock.h
1 /*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 */
33
34 #ifndef _KERN_CLOCK_H_
35 #define _KERN_CLOCK_H_
36
37 #include <stdint.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>
43
44 #include <kern/kern_types.h>
45
46 #include <sys/cdefs.h>
47
48 #ifdef __LP64__
49
50 typedef unsigned long clock_sec_t;
51 typedef unsigned int clock_usec_t, clock_nsec_t;
52
53 #else /* __LP64__ */
54
55 typedef uint32_t clock_sec_t;
56 typedef uint32_t clock_usec_t, clock_nsec_t;
57
58 #endif /* __LP64__ */
59
60 #ifdef MACH_KERNEL_PRIVATE
61
62 #include <kern/queue.h>
63
64 /*
65 * Clock operations list structure. Contains vectors to machine
66 * dependent clock routines.
67 */
68 struct clock_ops {
69 int (*c_config)(void); /* configuration */
70
71 int (*c_init)(void); /* initialize */
72
73 kern_return_t (*c_gettime)( /* get time */
74 mach_timespec_t *cur_time);
75
76 kern_return_t (*c_getattr)( /* get attributes */
77 clock_flavor_t flavor,
78 clock_attr_t attr,
79 mach_msg_type_number_t *count);
80 };
81 typedef struct clock_ops *clock_ops_t;
82 typedef struct clock_ops clock_ops_data_t;
83
84 /*
85 * Actual clock object data structure. Contains the machine
86 * dependent operations list and clock operation ports.
87 */
88 struct clock {
89 clock_ops_t cl_ops; /* operations list */
90 struct ipc_port *cl_service; /* service port */
91 struct ipc_port *cl_control; /* control port */
92 };
93 typedef struct clock clock_data_t;
94
95 /*
96 * Configure the clock system.
97 */
98 extern void clock_config(void);
99 extern void clock_oldconfig(void);
100
101 /*
102 * Initialize the clock system.
103 */
104 extern void clock_init(void);
105 extern void clock_oldinit(void);
106
107 extern void clock_timebase_init(void);
108
109 /*
110 * Initialize the clock ipc service facility.
111 */
112 extern void clock_service_create(void);
113
114 extern void clock_gettimeofday_set_commpage(
115 uint64_t abstime,
116 uint64_t epoch,
117 uint64_t offset,
118 clock_sec_t *secs,
119 clock_usec_t *microsecs);
120
121 extern void machine_delay_until(uint64_t interval,
122 uint64_t deadline);
123
124 extern uint32_t hz_tick_interval;
125
126 extern void nanotime_to_absolutetime(
127 clock_sec_t secs,
128 clock_nsec_t nanosecs,
129 uint64_t *result);
130
131 #endif /* MACH_KERNEL_PRIVATE */
132
133 __BEGIN_DECLS
134
135 #ifdef XNU_KERNEL_PRIVATE
136
137 extern void clock_adjtime(
138 long *secs,
139 int *microsecs);
140
141 extern void clock_initialize_calendar(void);
142
143 extern void clock_wakeup_calendar(void);
144
145 extern void clock_gettimeofday(
146 clock_sec_t *secs,
147 clock_usec_t *microsecs);
148
149 extern void clock_gettimeofday_and_absolute_time(
150 clock_sec_t *secs,
151 clock_usec_t *microsecs,
152 uint64_t *absolute_time);
153
154
155 extern void clock_set_calendar_microtime(
156 clock_sec_t secs,
157 clock_usec_t microsecs);
158
159 extern void clock_get_boottime_nanotime(
160 clock_sec_t *secs,
161 clock_nsec_t *nanosecs);
162
163 extern void clock_get_boottime_microtime(
164 clock_sec_t *secs,
165 clock_nsec_t *microsecs);
166
167 extern void absolutetime_to_microtime(
168 uint64_t abstime,
169 clock_sec_t *secs,
170 clock_usec_t *microsecs);
171
172 extern void clock_deadline_for_periodic_event(
173 uint64_t interval,
174 uint64_t abstime,
175 uint64_t *deadline);
176
177 #if CONFIG_DTRACE
178
179 extern void clock_get_calendar_nanotime_nowait(
180 clock_sec_t *secs,
181 clock_nsec_t *nanosecs);
182
183 #endif /* CONFIG_DTRACE */
184
185 boolean_t kdp_clock_is_locked(void);
186
187 #endif /* XNU_KERNEL_PRIVATE */
188
189 extern void clock_get_calendar_microtime(
190 clock_sec_t *secs,
191 clock_usec_t *microsecs);
192
193 extern void clock_get_calendar_absolute_and_microtime(
194 clock_sec_t *secs,
195 clock_usec_t *microsecs,
196 uint64_t *abstime);
197
198 extern void clock_get_calendar_nanotime(
199 clock_sec_t *secs,
200 clock_nsec_t *nanosecs);
201
202 extern void clock_get_system_microtime(
203 clock_sec_t *secs,
204 clock_usec_t *microsecs);
205
206 extern void clock_get_system_nanotime(
207 clock_sec_t *secs,
208 clock_nsec_t *nanosecs);
209
210 extern void clock_timebase_info(
211 mach_timebase_info_t info);
212
213 extern void clock_get_uptime(
214 uint64_t *result);
215
216 extern void clock_interval_to_deadline(
217 uint32_t interval,
218 uint32_t scale_factor,
219 uint64_t *result);
220
221 extern void clock_interval_to_absolutetime_interval(
222 uint32_t interval,
223 uint32_t scale_factor,
224 uint64_t *result);
225
226 extern void clock_absolutetime_interval_to_deadline(
227 uint64_t abstime,
228 uint64_t *result);
229
230 extern void clock_continuoustime_interval_to_deadline(
231 uint64_t abstime,
232 uint64_t *result);
233
234 extern void clock_delay_until(
235 uint64_t deadline);
236
237 extern void absolutetime_to_nanoseconds(
238 uint64_t abstime,
239 uint64_t *result);
240
241 extern void nanoseconds_to_absolutetime(
242 uint64_t nanoseconds,
243 uint64_t *result);
244
245 /*
246 * Absolute <-> Continuous Time conversion routines
247 *
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
251 * timebase.
252 *
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
256 * handled.
257 */
258 extern uint64_t absolutetime_to_continuoustime(
259 uint64_t abstime);
260 extern uint64_t continuoustime_to_absolutetime(
261 uint64_t conttime);
262
263 extern uint64_t mach_absolutetime_asleep;
264 extern uint64_t mach_absolutetime_last_sleep;
265
266 #ifdef KERNEL_PRIVATE
267
268 /*
269 * Obsolete interfaces.
270 */
271
272 #ifndef __LP64__
273
274 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
275 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
276
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 } )
281
282 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
283 do { \
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; \
288 (t1)->tv_sec += 1; \
289 } \
290 else if ((clock_res_t)(nsec) < 0 && \
291 (t1)->tv_nsec < 0) { \
292 (t1)->tv_nsec += NSEC_PER_SEC; \
293 (t1)->tv_sec -= 1; \
294 } \
295 } while (0)
296
297 #include <Availability.h>
298
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);
301
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);
303
304 #else /* __LP64__ */
305
306 #ifdef XNU_KERNEL_PRIVATE
307
308 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
309
310 #endif /* XNU_KERNEL_PRIVATE */
311
312 #endif /* __LP64__ */
313
314 extern void delay_for_interval(
315 uint32_t interval,
316 uint32_t scale_factor);
317
318 extern void delay_for_interval_with_leeway(
319 uint32_t interval,
320 uint32_t leeway,
321 uint32_t scale_factor);
322
323 #ifdef XNU_KERNEL_PRIVATE
324 extern void delay(int usec);
325 #endif /* XNU_KERNEL_PRIVATE */
326
327 #endif /* KERNEL_PRIVATE */
328
329 __END_DECLS
330
331 #endif /* _KERN_CLOCK_H_ */