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