]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/clock.h
1460af70603b5dae2ecc01c290024d34d605f26c
[apple/xnu.git] / osfmk / kern / clock.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * File: kern/clock.h
28 * Purpose: Data structures for the kernel alarm clock
29 * facility. This file is used only by kernel
30 * level clock facility routines.
31 */
32
33 #ifndef _KERN_CLOCK_H_
34 #define _KERN_CLOCK_H_
35
36 #include <stdint.h>
37 #include <mach/mach_types.h>
38 #include <mach/clock_types.h>
39 #include <mach/message.h>
40 #include <mach/mach_time.h>
41
42 #include <kern/kern_types.h>
43
44 #include <sys/cdefs.h>
45
46 #ifdef MACH_KERNEL_PRIVATE
47
48 /*
49 * Actual clock alarm structure. Used for user clock_sleep() and
50 * clock_alarm() calls. Alarms are allocated from the alarm free
51 * list and entered in time priority order into the active alarm
52 * chain of the target clock.
53 */
54 struct alarm {
55 struct alarm *al_next; /* next alarm in chain */
56 struct alarm *al_prev; /* previous alarm in chain */
57 int al_status; /* alarm status */
58 mach_timespec_t al_time; /* alarm time */
59 struct { /* message alarm data */
60 int type; /* alarm type */
61 ipc_port_t port; /* alarm port */
62 mach_msg_type_name_t
63 port_type; /* alarm port type */
64 struct clock *clock; /* alarm clock */
65 void *data; /* alarm data */
66 } al_alrm;
67 #define al_type al_alrm.type
68 #define al_port al_alrm.port
69 #define al_port_type al_alrm.port_type
70 #define al_clock al_alrm.clock
71 #define al_data al_alrm.data
72 long al_seqno; /* alarm sequence number */
73 };
74 typedef struct alarm alarm_data_t;
75
76 /* alarm status */
77 #define ALARM_FREE 0 /* alarm is on free list */
78 #define ALARM_SLEEP 1 /* active clock_sleep() */
79 #define ALARM_CLOCK 2 /* active clock_alarm() */
80 #define ALARM_DONE 4 /* alarm has expired */
81
82 /*
83 * Clock operations list structure. Contains vectors to machine
84 * dependent clock routines. The routines c_config, c_init, and
85 * c_gettime must be implemented for every clock device.
86 */
87 struct clock_ops {
88 int (*c_config)(void); /* configuration */
89
90 int (*c_init)(void); /* initialize */
91
92 kern_return_t (*c_gettime)( /* get time */
93 mach_timespec_t *cur_time);
94
95 kern_return_t (*c_settime)( /* set time */
96 mach_timespec_t *clock_time);
97
98 kern_return_t (*c_getattr)( /* get attributes */
99 clock_flavor_t flavor,
100 clock_attr_t attr,
101 mach_msg_type_number_t *count);
102
103 kern_return_t (*c_setattr)( /* set attributes */
104 clock_flavor_t flavor,
105 clock_attr_t attr,
106 mach_msg_type_number_t count);
107
108 void (*c_setalrm)( /* set next alarm */
109 mach_timespec_t *alarm_time);
110 };
111 typedef struct clock_ops *clock_ops_t;
112 typedef struct clock_ops clock_ops_data_t;
113
114 /*
115 * Actual clock object data structure. Contains the machine
116 * dependent operations list, clock operations ports, and a
117 * chain of pending alarms.
118 */
119 struct clock {
120 clock_ops_t cl_ops; /* operations list */
121 struct ipc_port *cl_service; /* service port */
122 struct ipc_port *cl_control; /* control port */
123 struct { /* alarm chain head */
124 struct alarm *al_next;
125 } cl_alarm;
126 };
127 typedef struct clock clock_data_t;
128
129 /*
130 * Configure the clock system.
131 */
132 extern void clock_config(void);
133
134 /*
135 * Initialize the clock system.
136 */
137 extern void clock_init(void);
138
139 extern void clock_timebase_init(void);
140
141 /*
142 * Initialize the clock ipc service facility.
143 */
144 extern void clock_service_create(void);
145
146 /*
147 * Service clock alarm interrupts. Called from machine dependent
148 * layer at splclock(). The clock_id argument specifies the clock,
149 * and the clock_time argument gives that clock's current time.
150 */
151 extern void clock_alarm_intr(
152 clock_id_t clock_id,
153 mach_timespec_t *clock_time);
154
155 extern kern_return_t clock_sleep_internal(
156 clock_t clock,
157 sleep_type_t sleep_type,
158 mach_timespec_t *sleep_time);
159
160 typedef void (*clock_timer_func_t)(
161 uint64_t timestamp);
162
163 extern void clock_set_timer_func(
164 clock_timer_func_t func);
165
166 extern void clock_set_timer_deadline(
167 uint64_t deadline);
168
169 extern uint32_t clock_set_calendar_adjtime(
170 int32_t *secs,
171 int32_t *microsecs);
172
173 extern uint32_t clock_adjust_calendar(void);
174
175 extern void machine_delay_until(
176 uint64_t deadline);
177
178 #include <stat_time.h>
179
180 extern void hertz_tick(
181 #if STAT_TIME
182 natural_t ticks,
183 #endif /* STAT_TIME */
184 boolean_t usermode, /* executing user code */
185 natural_t pc);
186
187 extern void absolutetime_to_microtime(
188 uint64_t abstime,
189 uint32_t *secs,
190 uint32_t *microsecs);
191
192 #endif /* MACH_KERNEL_PRIVATE */
193
194 __BEGIN_DECLS
195
196 #ifdef XNU_KERNEL_PRIVATE
197
198 extern void clock_adjtime(
199 int32_t *secs,
200 int32_t *microsecs);
201
202 extern void clock_initialize_calendar(void);
203
204 extern void clock_wakeup_calendar(void);
205
206 extern void clock_gettimeofday(
207 uint32_t *secs,
208 uint32_t *microsecs);
209
210 extern void clock_set_calendar_microtime(
211 uint32_t secs,
212 uint32_t microsecs);
213
214 extern void clock_get_boottime_nanotime(
215 uint32_t *secs,
216 uint32_t *nanosecs);
217
218 extern void clock_deadline_for_periodic_event(
219 uint64_t interval,
220 uint64_t abstime,
221 uint64_t *deadline);
222
223 #endif /* XNU_KERNEL_PRIVATE */
224
225
226 extern void clock_get_calendar_microtime(
227 uint32_t *secs,
228 uint32_t *microsecs);
229
230 extern void clock_get_calendar_nanotime(
231 uint32_t *secs,
232 uint32_t *nanosecs);
233
234 extern void clock_get_system_microtime(
235 uint32_t *secs,
236 uint32_t *microsecs);
237
238 extern void clock_get_system_nanotime(
239 uint32_t *secs,
240 uint32_t *nanosecs);
241
242 extern void clock_timebase_info(
243 mach_timebase_info_t info);
244
245 extern void clock_get_uptime(
246 uint64_t *result);
247
248 extern void clock_interval_to_deadline(
249 uint32_t interval,
250 uint32_t scale_factor,
251 uint64_t *result);
252
253 extern void clock_interval_to_absolutetime_interval(
254 uint32_t interval,
255 uint32_t scale_factor,
256 uint64_t *result);
257
258 extern void clock_absolutetime_interval_to_deadline(
259 uint64_t abstime,
260 uint64_t *result);
261
262 extern void clock_delay_until(
263 uint64_t deadline);
264
265 extern void absolutetime_to_nanoseconds(
266 uint64_t abstime,
267 uint64_t *result);
268
269 extern void nanoseconds_to_absolutetime(
270 uint64_t nanoseconds,
271 uint64_t *result);
272
273 #ifdef KERNEL_PRIVATE
274
275 /*
276 * Obsolete interfaces.
277 */
278
279 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
280 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
281
282 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
283 MACH_TIMESPEC_SEC_MAX, \
284 MACH_TIMESPEC_NSEC_MAX } )
285 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
286
287 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
288 do { \
289 (t1)->tv_nsec += (clock_res_t)(nsec); \
290 if ((clock_res_t)(nsec) > 0 && \
291 (t1)->tv_nsec >= NSEC_PER_SEC) { \
292 (t1)->tv_nsec -= NSEC_PER_SEC; \
293 (t1)->tv_sec += 1; \
294 } \
295 else if ((clock_res_t)(nsec) < 0 && \
296 (t1)->tv_nsec < 0) { \
297 (t1)->tv_nsec += NSEC_PER_SEC; \
298 (t1)->tv_sec -= 1; \
299 } \
300 } while (0)
301
302
303 extern mach_timespec_t clock_get_system_value(void);
304
305 extern mach_timespec_t clock_get_calendar_value(void);
306
307 extern void delay_for_interval(
308 uint32_t interval,
309 uint32_t scale_factor);
310 #ifndef MACH_KERNEL_PRIVATE
311
312 #ifndef ABSOLUTETIME_SCALAR_TYPE
313
314 #define clock_get_uptime(a) \
315 clock_get_uptime(__OSAbsoluteTimePtr(a))
316
317 #define clock_interval_to_deadline(a, b, c) \
318 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
319
320 #define clock_interval_to_absolutetime_interval(a, b, c) \
321 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
322
323 #define clock_absolutetime_interval_to_deadline(a, b) \
324 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
325
326 #define clock_deadline_for_periodic_event(a, b, c) \
327 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
328
329 #define clock_delay_until(a) \
330 clock_delay_until(__OSAbsoluteTime(a))
331
332 #define absolutetime_to_nanoseconds(a, b) \
333 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
334
335 #define nanoseconds_to_absolutetime(a, b) \
336 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
337
338 #endif /* ABSOLUTETIME_SCALAR_TYPE */
339
340 #endif /* !MACH_KERNEL_PRIVATE */
341
342 #endif /* KERNEL_PRIVATE */
343
344 __END_DECLS
345
346 #endif /* _KERN_CLOCK_H_ */