]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/clock.h
xnu-517.9.4.tar.gz
[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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * File: kern/clock.h
27 * Purpose: Data structures for the kernel alarm clock
28 * facility. This file is used only by kernel
29 * level clock facility routines.
30 */
31
32 #ifndef _KERN_CLOCK_H_
33 #define _KERN_CLOCK_H_
34
35 #include <mach/message.h>
36 #include <mach/clock_types.h>
37 #include <mach/mach_time.h>
38
39 #include <sys/appleapiopts.h>
40
41 #ifdef __APPLE_API_PRIVATE
42
43 #ifdef MACH_KERNEL_PRIVATE
44
45 #include <ipc/ipc_port.h>
46
47 /*
48 * Actual clock alarm structure. Used for user clock_sleep() and
49 * clock_alarm() calls. Alarms are allocated from the alarm free
50 * list and entered in time priority order into the active alarm
51 * chain of the target clock.
52 */
53 struct alarm {
54 struct alarm *al_next; /* next alarm in chain */
55 struct alarm *al_prev; /* previous alarm in chain */
56 int al_status; /* alarm status */
57 mach_timespec_t al_time; /* alarm time */
58 struct { /* message alarm data */
59 int type; /* alarm type */
60 ipc_port_t port; /* alarm port */
61 mach_msg_type_name_t
62 port_type; /* alarm port type */
63 struct clock *clock; /* alarm clock */
64 void *data; /* alarm data */
65 } al_alrm;
66 #define al_type al_alrm.type
67 #define al_port al_alrm.port
68 #define al_port_type al_alrm.port_type
69 #define al_clock al_alrm.clock
70 #define al_data al_alrm.data
71 long al_seqno; /* alarm sequence number */
72 };
73 typedef struct alarm alarm_data_t;
74
75 /* alarm status */
76 #define ALARM_FREE 0 /* alarm is on free list */
77 #define ALARM_SLEEP 1 /* active clock_sleep() */
78 #define ALARM_CLOCK 2 /* active clock_alarm() */
79 #define ALARM_DONE 4 /* alarm has expired */
80
81 /*
82 * Clock operations list structure. Contains vectors to machine
83 * dependent clock routines. The routines c_config, c_init, and
84 * c_gettime must be implemented for every clock device.
85 */
86 struct clock_ops {
87 int (*c_config)(void); /* configuration */
88
89 int (*c_init)(void); /* initialize */
90
91 kern_return_t (*c_gettime)( /* get time */
92 mach_timespec_t *cur_time);
93
94 kern_return_t (*c_settime)( /* set time */
95 mach_timespec_t *clock_time);
96
97 kern_return_t (*c_getattr)( /* get attributes */
98 clock_flavor_t flavor,
99 clock_attr_t attr,
100 mach_msg_type_number_t *count);
101
102 kern_return_t (*c_setattr)( /* set attributes */
103 clock_flavor_t flavor,
104 clock_attr_t attr,
105 mach_msg_type_number_t count);
106
107 void (*c_setalrm)( /* set next alarm */
108 mach_timespec_t *alarm_time);
109 };
110 typedef struct clock_ops *clock_ops_t;
111 typedef struct clock_ops clock_ops_data_t;
112
113 /*
114 * Actual clock object data structure. Contains the machine
115 * dependent operations list, clock operations ports, and a
116 * chain of pending alarms.
117 */
118 struct clock {
119 clock_ops_t cl_ops; /* operations list */
120 struct ipc_port *cl_service; /* service port */
121 struct ipc_port *cl_control; /* control port */
122 struct { /* alarm chain head */
123 struct alarm *al_next;
124 } cl_alarm;
125 };
126 typedef struct clock clock_data_t;
127
128 /*
129 * Configure the clock system.
130 */
131 extern void clock_config(void);
132
133 /*
134 * Initialize the clock system.
135 */
136 extern void clock_init(void);
137
138 extern void clock_timebase_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 uint32_t clock_set_calendar_adjtime(
176 int32_t *secs,
177 int32_t *microsecs);
178
179 extern uint32_t clock_adjust_calendar(void);
180
181 #endif /* MACH_KERNEL_PRIVATE */
182
183 extern void clock_get_calendar_microtime(
184 uint32_t *secs,
185 uint32_t *microsecs);
186
187 extern void clock_get_calendar_nanotime(
188 uint32_t *secs,
189 uint32_t *nanosecs);
190
191 extern void clock_set_calendar_microtime(
192 uint32_t secs,
193 uint32_t microsecs);
194
195 extern void clock_get_system_microtime(
196 uint32_t *secs,
197 uint32_t *microsecs);
198
199 extern void clock_get_system_nanotime(
200 uint32_t *secs,
201 uint32_t *nanosecs);
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 #endif /* __APPLE_API_PRIVATE */
216
217 #ifdef __APPLE_API_UNSTABLE
218
219 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
220 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
221
222 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
223 MACH_TIMESPEC_SEC_MAX, \
224 MACH_TIMESPEC_NSEC_MAX } )
225 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
226
227 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
228 do { \
229 (t1)->tv_nsec += (clock_res_t)(nsec); \
230 if ((clock_res_t)(nsec) > 0 && \
231 (t1)->tv_nsec >= NSEC_PER_SEC) { \
232 (t1)->tv_nsec -= NSEC_PER_SEC; \
233 (t1)->tv_sec += 1; \
234 } \
235 else if ((clock_res_t)(nsec) < 0 && \
236 (t1)->tv_nsec < 0) { \
237 (t1)->tv_nsec += NSEC_PER_SEC; \
238 (t1)->tv_sec -= 1; \
239 } \
240 } while (0)
241
242 #endif /* __APPLE_API_UNSTABLE */
243
244 extern mach_timespec_t clock_get_system_value(void);
245
246 extern mach_timespec_t clock_get_calendar_value(void);
247
248 extern void clock_timebase_info(
249 mach_timebase_info_t info);
250
251 extern void clock_get_uptime(
252 uint64_t *result);
253
254 extern void clock_interval_to_deadline(
255 uint32_t interval,
256 uint32_t scale_factor,
257 uint64_t *result);
258
259 extern void clock_interval_to_absolutetime_interval(
260 uint32_t interval,
261 uint32_t scale_factor,
262 uint64_t *result);
263
264 extern void clock_absolutetime_interval_to_deadline(
265 uint64_t abstime,
266 uint64_t *result);
267
268 extern void clock_deadline_for_periodic_event(
269 uint64_t interval,
270 uint64_t abstime,
271 uint64_t *deadline);
272
273 extern void clock_delay_for_interval(
274 uint32_t interval,
275 uint32_t scale_factor);
276
277 extern void clock_delay_until(
278 uint64_t deadline);
279
280 extern void absolutetime_to_nanoseconds(
281 uint64_t abstime,
282 uint64_t *result);
283
284 extern void nanoseconds_to_absolutetime(
285 uint64_t nanoseconds,
286 uint64_t *result);
287
288 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
289
290 #include <libkern/OSBase.h>
291
292 #define clock_get_uptime(a) \
293 clock_get_uptime(__OSAbsoluteTimePtr(a))
294
295 #define clock_interval_to_deadline(a, b, c) \
296 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
297
298 #define clock_interval_to_absolutetime_interval(a, b, c) \
299 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
300
301 #define clock_absolutetime_interval_to_deadline(a, b) \
302 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
303
304 #define clock_deadline_for_periodic_event(a, b, c) \
305 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
306
307 #define clock_delay_until(a) \
308 clock_delay_until(__OSAbsoluteTime(a))
309
310 #define absolutetime_to_nanoseconds(a, b) \
311 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
312
313 #define nanoseconds_to_absolutetime(a, b) \
314 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
315
316 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
317
318 /* t1 < = > t2 */
319 #define CMP_ABSOLUTETIME(t1, t2) \
320 (AbsoluteTime_to_scalar(t1) > \
321 AbsoluteTime_to_scalar(t2)? (int)+1 : \
322 (AbsoluteTime_to_scalar(t1) < \
323 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
324
325 /* t1 += t2 */
326 #define ADD_ABSOLUTETIME(t1, t2) \
327 (AbsoluteTime_to_scalar(t1) += \
328 AbsoluteTime_to_scalar(t2))
329
330 /* t1 -= t2 */
331 #define SUB_ABSOLUTETIME(t1, t2) \
332 (AbsoluteTime_to_scalar(t1) -= \
333 AbsoluteTime_to_scalar(t2))
334
335 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
336 (AbsoluteTime_to_scalar(t1) += \
337 (int32_t)(ticks))
338
339 #endif
340
341 #endif /* _KERN_CLOCK_H_ */