]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/clock.h
xnu-344.23.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 * Initialize the clock system.
134 */
135 extern void clock_init(void);
136
137 /*
138 * Initialize the clock ipc service facility.
139 */
140 extern void clock_service_create(void);
141
142 /*
143 * Service clock alarm interrupts. Called from machine dependent
144 * layer at splclock(). The clock_id argument specifies the clock,
145 * and the clock_time argument gives that clock's current time.
146 */
147 extern void clock_alarm_intr(
148 clock_id_t clock_id,
149 mach_timespec_t *clock_time);
150
151 extern kern_return_t clock_sleep_internal(
152 clock_t clock,
153 sleep_type_t sleep_type,
154 mach_timespec_t *sleep_time);
155
156 typedef void (*clock_timer_func_t)(
157 uint64_t timestamp);
158
159 extern void clock_set_timer_func(
160 clock_timer_func_t func);
161
162 extern void clock_set_timer_deadline(
163 uint64_t deadline);
164
165 extern void mk_timebase_info(
166 uint32_t *delta,
167 uint32_t *abs_to_ns_numer,
168 uint32_t *abs_to_ns_denom,
169 uint32_t *proc_to_abs_numer,
170 uint32_t *proc_to_abs_denom);
171
172 extern void clock_adjust_calendar(
173 clock_res_t nsec);
174
175 extern mach_timespec_t
176 clock_get_calendar_offset(void);
177
178 #endif /* MACH_KERNEL_PRIVATE */
179
180 extern void clock_set_calendar_value(
181 mach_timespec_t value);
182
183 extern int64_t clock_set_calendar_adjtime(
184 int64_t total,
185 uint32_t delta);
186
187 extern void clock_initialize_calendar(void);
188
189 #endif /* __APPLE_API_PRIVATE */
190
191 #ifdef __APPLE_API_UNSTABLE
192
193 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
194 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
195
196 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
197 MACH_TIMESPEC_SEC_MAX, \
198 MACH_TIMESPEC_NSEC_MAX } )
199 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
200
201 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
202 do { \
203 (t1)->tv_nsec += (clock_res_t)(nsec); \
204 if ((clock_res_t)(nsec) > 0 && \
205 (t1)->tv_nsec >= NSEC_PER_SEC) { \
206 (t1)->tv_nsec -= NSEC_PER_SEC; \
207 (t1)->tv_sec += 1; \
208 } \
209 else if ((clock_res_t)(nsec) < 0 && \
210 (t1)->tv_nsec < 0) { \
211 (t1)->tv_nsec += NSEC_PER_SEC; \
212 (t1)->tv_sec -= 1; \
213 } \
214 } while (0)
215
216 #endif /* __APPLE_API_UNSTABLE */
217
218 extern mach_timespec_t clock_get_system_value(void);
219
220 extern mach_timespec_t clock_get_calendar_value(void);
221
222 extern void clock_timebase_info(
223 mach_timebase_info_t info);
224
225 extern void clock_get_uptime(
226 uint64_t *result);
227
228 extern void clock_interval_to_deadline(
229 uint32_t interval,
230 uint32_t scale_factor,
231 uint64_t *result);
232
233 extern void clock_interval_to_absolutetime_interval(
234 uint32_t interval,
235 uint32_t scale_factor,
236 uint64_t *result);
237
238 extern void clock_absolutetime_interval_to_deadline(
239 uint64_t abstime,
240 uint64_t *result);
241
242 extern void clock_deadline_for_periodic_event(
243 uint64_t interval,
244 uint64_t abstime,
245 uint64_t *deadline);
246
247 extern void clock_delay_for_interval(
248 uint32_t interval,
249 uint32_t scale_factor);
250
251 extern void clock_delay_until(
252 uint64_t deadline);
253
254 extern void absolutetime_to_nanoseconds(
255 uint64_t abstime,
256 uint64_t *result);
257
258 extern void nanoseconds_to_absolutetime(
259 uint64_t nanoseconds,
260 uint64_t *result);
261
262 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
263
264 #include <libkern/OSBase.h>
265
266 #define clock_get_uptime(a) \
267 clock_get_uptime(__OSAbsoluteTimePtr(a))
268
269 #define clock_interval_to_deadline(a, b, c) \
270 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
271
272 #define clock_interval_to_absolutetime_interval(a, b, c) \
273 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
274
275 #define clock_absolutetime_interval_to_deadline(a, b) \
276 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
277
278 #define clock_deadline_for_periodic_event(a, b, c) \
279 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
280
281 #define clock_delay_until(a) \
282 clock_delay_until(__OSAbsoluteTime(a))
283
284 #define absolutetime_to_nanoseconds(a, b) \
285 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
286
287 #define nanoseconds_to_absolutetime(a, b) \
288 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
289
290 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
291
292 /* t1 < = > t2 */
293 #define CMP_ABSOLUTETIME(t1, t2) \
294 (AbsoluteTime_to_scalar(t1) > \
295 AbsoluteTime_to_scalar(t2)? (int)+1 : \
296 (AbsoluteTime_to_scalar(t1) < \
297 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
298
299 /* t1 += t2 */
300 #define ADD_ABSOLUTETIME(t1, t2) \
301 (AbsoluteTime_to_scalar(t1) += \
302 AbsoluteTime_to_scalar(t2))
303
304 /* t1 -= t2 */
305 #define SUB_ABSOLUTETIME(t1, t2) \
306 (AbsoluteTime_to_scalar(t1) -= \
307 AbsoluteTime_to_scalar(t2))
308
309 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
310 (AbsoluteTime_to_scalar(t1) += \
311 (int32_t)(ticks))
312
313 #endif
314
315 #endif /* _KERN_CLOCK_H_ */