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