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