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
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
61#ifdef MACH_KERNEL_PRIVATE
62
63#include <kern/queue.h>
64
65/*
66 * Clock operations list structure. Contains vectors to machine
67 * dependent clock routines.
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
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);
81};
82typedef const struct clock_ops *clock_ops_t;
83typedef struct clock_ops clock_ops_data_t;
84
85/*
86 * Actual clock object data structure. Contains the machine
87 * dependent operations list and clock operation ports.
88 */
89struct clock {
90 clock_ops_t cl_ops; /* operations list */
91 struct ipc_port *cl_service; /* service port */
92};
93typedef struct clock clock_data_t;
94
95/*
96 * Configure the clock system.
97 */
98extern void clock_config(void);
99extern void clock_oldconfig(void);
100
101/*
102 * Initialize the clock system.
103 */
104extern void clock_init(void);
105extern void clock_oldinit(void);
106
107extern void clock_timebase_init(void);
108
109/*
110 * Initialize the clock ipc service facility.
111 */
112extern void clock_service_create(void);
113
114extern void clock_gettimeofday_set_commpage(
115 uint64_t abstime,
116 uint64_t sec,
117 uint64_t frac,
118 uint64_t scale,
119 uint64_t tick_per_sec);
120
121extern void machine_delay_until(uint64_t interval,
122 uint64_t deadline);
123
124extern uint32_t hz_tick_interval;
125
126extern 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
137extern void clock_adjtime(
138 long *secs,
139 int *microsecs);
140
141extern void clock_initialize_calendar(void);
142
143extern void clock_wakeup_calendar(void);
144
145extern void clock_update_calendar(void);
146
147extern void clock_get_calendar_uptime(clock_sec_t *secs);
148
149extern void clock_gettimeofday_new(clock_sec_t *secs,
150 clock_usec_t *microsecs);
151extern void clock_gettimeofday(
152 clock_sec_t *secs,
153 clock_usec_t *microsecs);
154
155extern void clock_gettimeofday_and_absolute_time(
156 clock_sec_t *secs,
157 clock_usec_t *microsecs,
158 uint64_t *absolute_time);
159
160
161extern void clock_set_calendar_microtime(
162 clock_sec_t secs,
163 clock_usec_t microsecs);
164
165extern void clock_get_boottime_nanotime(
166 clock_sec_t *secs,
167 clock_nsec_t *nanosecs);
168
169extern void clock_get_boottime_microtime(
170 clock_sec_t *secs,
171 clock_nsec_t *microsecs);
172
173extern void clock_deadline_for_periodic_event(
174 uint64_t interval,
175 uint64_t abstime,
176 uint64_t *deadline);
177
178#if CONFIG_DTRACE
179
180extern void clock_get_calendar_nanotime_nowait(
181 clock_sec_t *secs,
182 clock_nsec_t *nanosecs);
183
184#endif /* CONFIG_DTRACE */
185
186boolean_t kdp_clock_is_locked(void);
187
188#endif /* XNU_KERNEL_PRIVATE */
189
190extern void clock_get_calendar_microtime(
191 clock_sec_t *secs,
192 clock_usec_t *microsecs);
193
194extern void clock_get_calendar_absolute_and_microtime(
195 clock_sec_t *secs,
196 clock_usec_t *microsecs,
197 uint64_t *abstime);
198
199extern void clock_get_calendar_nanotime(
200 clock_sec_t *secs,
201 clock_nsec_t *nanosecs);
202
203extern void clock_get_system_microtime(
204 clock_sec_t *secs,
205 clock_usec_t *microsecs);
206
207extern void clock_get_system_nanotime(
208 clock_sec_t *secs,
209 clock_nsec_t *nanosecs);
210
211extern void clock_timebase_info(
212 mach_timebase_info_t info);
213
214extern void clock_get_uptime(
215 uint64_t *result);
216
217extern void clock_interval_to_deadline(
218 uint32_t interval,
219 uint32_t scale_factor,
220 uint64_t *result);
221
222extern void nanoseconds_to_deadline(
223 uint64_t interval,
224 uint64_t *result);
225
226extern void clock_interval_to_absolutetime_interval(
227 uint32_t interval,
228 uint32_t scale_factor,
229 uint64_t *result);
230
231extern void clock_absolutetime_interval_to_deadline(
232 uint64_t abstime,
233 uint64_t *result);
234
235extern void clock_continuoustime_interval_to_deadline(
236 uint64_t abstime,
237 uint64_t *result);
238
239extern void clock_delay_until(
240 uint64_t deadline);
241
242extern void absolutetime_to_nanoseconds(
243 uint64_t abstime,
244 uint64_t *result);
245
246extern void nanoseconds_to_absolutetime(
247 uint64_t nanoseconds,
248 uint64_t *result);
249
250extern void absolutetime_to_microtime(
251 uint64_t abstime,
252 clock_sec_t *secs,
253 clock_usec_t *microsecs);
254
255/*
256 * Absolute <-> Continuous Time conversion routines
257 *
258 * It is the caller's responsibility to ensure that these functions are
259 * synchronized with respect to updates to the continuous timebase. The
260 * returned value is only valid until the next update to the continuous
261 * timebase.
262 *
263 * If the value to be returned by continuoustime_to_absolutetime would be
264 * negative, zero is returned. This occurs when the provided continuous time
265 * is less the amount of the time the system spent asleep and /must/ be
266 * handled.
267 */
268extern uint64_t absolutetime_to_continuoustime(
269 uint64_t abstime);
270extern uint64_t continuoustime_to_absolutetime(
271 uint64_t conttime);
272
273extern uint64_t mach_absolutetime_asleep;
274extern uint64_t mach_absolutetime_last_sleep;
275#if HIBERNATION && HAS_CONTINUOUS_HWCLOCK
276extern uint64_t hwclock_conttime_offset;
277#endif
278
279#ifdef KERNEL_PRIVATE
280
281/*
282 * Obsolete interfaces.
283 */
284
285#ifndef __LP64__
286
287#define MACH_TIMESPEC_SEC_MAX (0 - 1)
288#define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
289
290#define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
291 MACH_TIMESPEC_SEC_MAX, \
292 MACH_TIMESPEC_NSEC_MAX } )
293#define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
294
295#define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
296 do { \
297 (t1)->tv_nsec += (clock_res_t)(nsec); \
298 if ((clock_res_t)(nsec) > 0 && \
299 (t1)->tv_nsec >= NSEC_PER_SEC) { \
300 (t1)->tv_nsec -= NSEC_PER_SEC; \
301 (t1)->tv_sec += 1; \
302 } \
303 else if ((clock_res_t)(nsec) < 0 && \
304 (t1)->tv_nsec < 0) { \
305 (t1)->tv_nsec += NSEC_PER_SEC; \
306 (t1)->tv_sec -= 1; \
307 } \
308 } while (0)
309
310#include <Availability.h>
311
312/* Use mach_absolute_time() */
313extern mach_timespec_t clock_get_system_value(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_2_0, __IPHONE_6_0);
314
315extern mach_timespec_t clock_get_calendar_value(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_2_0, __IPHONE_6_0);
316
317#else /* __LP64__ */
318
319#ifdef XNU_KERNEL_PRIVATE
320
321#define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
322
323#endif /* XNU_KERNEL_PRIVATE */
324
325#endif /* __LP64__ */
326
327extern void delay_for_interval(
328 uint32_t interval,
329 uint32_t scale_factor);
330
331extern void delay_for_interval_with_leeway(
332 uint32_t interval,
333 uint32_t leeway,
334 uint32_t scale_factor);
335
336#ifdef XNU_KERNEL_PRIVATE
337extern void delay(int usec);
338#endif /* XNU_KERNEL_PRIVATE */
339
340#endif /* KERNEL_PRIVATE */
341
342__END_DECLS
343
344#endif /* _KERN_CLOCK_H_ */
345