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