]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/clock.h
5ca49ea7457db29998fa6bda339063b5eb93ab2a
[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
43 #include <kern/kern_types.h>
44
45 #include <sys/cdefs.h>
46
47 #ifdef MACH_KERNEL_PRIVATE
48
49 #include <kern/queue.h>
50
51 /*
52 * Clock operations list structure. Contains vectors to machine
53 * dependent clock routines.
54 */
55 struct clock_ops {
56 int (*c_config)(void); /* configuration */
57
58 int (*c_init)(void); /* initialize */
59
60 kern_return_t (*c_gettime)( /* get time */
61 mach_timespec_t *cur_time);
62
63 kern_return_t (*c_getattr)( /* get attributes */
64 clock_flavor_t flavor,
65 clock_attr_t attr,
66 mach_msg_type_number_t *count);
67 };
68 typedef struct clock_ops *clock_ops_t;
69 typedef struct clock_ops clock_ops_data_t;
70
71 /*
72 * Actual clock object data structure. Contains the machine
73 * dependent operations list and clock operation ports.
74 */
75 struct clock {
76 clock_ops_t cl_ops; /* operations list */
77 struct ipc_port *cl_service; /* service port */
78 struct ipc_port *cl_control; /* control port */
79 };
80 typedef struct clock clock_data_t;
81
82 /*
83 * Configure the clock system.
84 */
85 extern void clock_config(void);
86 extern void clock_oldconfig(void);
87
88 /*
89 * Initialize the clock system.
90 */
91 extern void clock_init(void);
92 extern void clock_oldinit(void);
93
94 extern void clock_timebase_init(void);
95
96 /*
97 * Initialize the clock ipc service facility.
98 */
99 extern void clock_service_create(void);
100
101 extern void clock_gettimeofday_set_commpage(
102 uint64_t abstime,
103 uint64_t epoch,
104 uint64_t offset,
105 uint32_t *secs,
106 uint32_t *microsecs);
107
108 extern void machine_delay_until(
109 uint64_t deadline);
110
111 #include <stat_time.h>
112
113 #if STAT_TIME || GPROF
114
115 extern void hertz_tick(
116 natural_t ticks,
117 boolean_t usermode, /* executing user code */
118 natural_t pc);
119
120 #endif /* STAT_TIME */
121
122 extern uint32_t hz_tick_interval;
123
124 extern void absolutetime_to_nanotime(
125 uint64_t abstime,
126 uint32_t *secs,
127 uint32_t *nanosecs);
128
129 extern void nanotime_to_absolutetime(
130 uint32_t secs,
131 uint32_t nanosecs,
132 uint64_t *result);
133
134 #endif /* MACH_KERNEL_PRIVATE */
135
136 __BEGIN_DECLS
137
138 #ifdef XNU_KERNEL_PRIVATE
139
140 extern void clock_adjtime(
141 int32_t *secs,
142 int32_t *microsecs);
143
144 extern void clock_initialize_calendar(void);
145
146 extern void clock_wakeup_calendar(void);
147
148 extern void clock_gettimeofday(
149 uint32_t *secs,
150 uint32_t *microsecs);
151
152 extern void clock_set_calendar_microtime(
153 uint32_t secs,
154 uint32_t microsecs);
155
156 extern void clock_get_boottime_nanotime(
157 uint32_t *secs,
158 uint32_t *nanosecs);
159
160 extern void absolutetime_to_microtime(
161 uint64_t abstime,
162 uint32_t *secs,
163 uint32_t *microsecs);
164
165 extern void clock_deadline_for_periodic_event(
166 uint64_t interval,
167 uint64_t abstime,
168 uint64_t *deadline);
169
170 #endif /* XNU_KERNEL_PRIVATE */
171
172 extern void clock_get_calendar_microtime(
173 uint32_t *secs,
174 uint32_t *microsecs);
175
176 extern void clock_get_calendar_nanotime(
177 uint32_t *secs,
178 uint32_t *nanosecs);
179
180 /*
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
184 */
185 #if CONFIG_DTRACE
186 extern void clock_get_calendar_nanotime_nowait(
187 uint32_t *secs,
188 uint32_t *nanosecs);
189 #endif /* CONFIG_DTRACE */
190
191 extern void clock_get_system_microtime(
192 uint32_t *secs,
193 uint32_t *microsecs);
194
195 extern void clock_get_system_nanotime(
196 uint32_t *secs,
197 uint32_t *nanosecs);
198
199 extern void clock_timebase_info(
200 mach_timebase_info_t info);
201
202 extern void clock_get_uptime(
203 uint64_t *result);
204
205 extern void clock_interval_to_deadline(
206 uint32_t interval,
207 uint32_t scale_factor,
208 uint64_t *result);
209
210 extern void clock_interval_to_absolutetime_interval(
211 uint32_t interval,
212 uint32_t scale_factor,
213 uint64_t *result);
214
215 extern void clock_absolutetime_interval_to_deadline(
216 uint64_t abstime,
217 uint64_t *result);
218
219 extern void clock_delay_until(
220 uint64_t deadline);
221
222 extern void absolutetime_to_nanoseconds(
223 uint64_t abstime,
224 uint64_t *result);
225
226 extern void nanoseconds_to_absolutetime(
227 uint64_t nanoseconds,
228 uint64_t *result);
229
230 #ifdef KERNEL_PRIVATE
231
232 /*
233 * Obsolete interfaces.
234 */
235
236 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
237 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
238
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 } )
243
244 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
245 do { \
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; \
250 (t1)->tv_sec += 1; \
251 } \
252 else if ((clock_res_t)(nsec) < 0 && \
253 (t1)->tv_nsec < 0) { \
254 (t1)->tv_nsec += NSEC_PER_SEC; \
255 (t1)->tv_sec -= 1; \
256 } \
257 } while (0)
258
259
260 extern mach_timespec_t clock_get_system_value(void);
261
262 extern mach_timespec_t clock_get_calendar_value(void);
263
264 extern void delay_for_interval(
265 uint32_t interval,
266 uint32_t scale_factor);
267 #ifndef MACH_KERNEL_PRIVATE
268
269 #ifndef ABSOLUTETIME_SCALAR_TYPE
270
271 #define clock_get_uptime(a) \
272 clock_get_uptime(__OSAbsoluteTimePtr(a))
273
274 #define clock_interval_to_deadline(a, b, c) \
275 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
276
277 #define clock_interval_to_absolutetime_interval(a, b, c) \
278 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
279
280 #define clock_absolutetime_interval_to_deadline(a, b) \
281 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
282
283 #define clock_deadline_for_periodic_event(a, b, c) \
284 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
285
286 #define clock_delay_until(a) \
287 clock_delay_until(__OSAbsoluteTime(a))
288
289 #define absolutetime_to_nanoseconds(a, b) \
290 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
291
292 #define nanoseconds_to_absolutetime(a, b) \
293 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
294
295 #endif /* ABSOLUTETIME_SCALAR_TYPE */
296
297 #endif /* !MACH_KERNEL_PRIVATE */
298
299 #endif /* KERNEL_PRIVATE */
300
301 __END_DECLS
302
303 #endif /* _KERN_CLOCK_H_ */