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