]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/clock.h
58c78efd5cd6ab74585a51906bdf1aa5fa54ddb8
[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 #ifdef MACH_KERNEL_PRIVATE
40 #include <ipc/ipc_port.h>
41
42 /*
43 * Actual clock alarm structure. Used for user clock_sleep() and
44 * clock_alarm() calls. Alarms are allocated from the alarm free
45 * list and entered in time priority order into the active alarm
46 * chain of the target clock.
47 */
48 struct alarm {
49 struct alarm *al_next; /* next alarm in chain */
50 struct alarm *al_prev; /* previous alarm in chain */
51 int al_status; /* alarm status */
52 mach_timespec_t al_time; /* alarm time */
53 struct { /* message alarm data */
54 int type; /* alarm type */
55 ipc_port_t port; /* alarm port */
56 mach_msg_type_name_t
57 port_type; /* alarm port type */
58 struct clock *clock; /* alarm clock */
59 void *data; /* alarm data */
60 } al_alrm;
61 #define al_type al_alrm.type
62 #define al_port al_alrm.port
63 #define al_port_type al_alrm.port_type
64 #define al_clock al_alrm.clock
65 #define al_data al_alrm.data
66 long al_seqno; /* alarm sequence number */
67 };
68 typedef struct alarm alarm_data_t;
69
70 /* alarm status */
71 #define ALARM_FREE 0 /* alarm is on free list */
72 #define ALARM_SLEEP 1 /* active clock_sleep() */
73 #define ALARM_CLOCK 2 /* active clock_alarm() */
74 #define ALARM_DONE 4 /* alarm has expired */
75
76 /*
77 * Clock operations list structure. Contains vectors to machine
78 * dependent clock routines. The routines c_config, c_init, and
79 * c_gettime must be implemented for every clock device.
80 */
81 struct clock_ops {
82 int (*c_config)(void); /* configuration */
83
84 int (*c_init)(void); /* initialize */
85
86 kern_return_t (*c_gettime)( /* get time */
87 mach_timespec_t *cur_time);
88
89 kern_return_t (*c_settime)( /* set time */
90 mach_timespec_t *clock_time);
91
92 kern_return_t (*c_getattr)( /* get attributes */
93 clock_flavor_t flavor,
94 clock_attr_t attr,
95 mach_msg_type_number_t *count);
96
97 kern_return_t (*c_setattr)( /* set attributes */
98 clock_flavor_t flavor,
99 clock_attr_t attr,
100 mach_msg_type_number_t count);
101
102 void (*c_setalrm)( /* set next alarm */
103 mach_timespec_t *alarm_time);
104 };
105 typedef struct clock_ops *clock_ops_t;
106 typedef struct clock_ops clock_ops_data_t;
107
108 /*
109 * Actual clock object data structure. Contains the machine
110 * dependent operations list, clock operations ports, and a
111 * chain of pending alarms.
112 */
113 struct clock {
114 clock_ops_t cl_ops; /* operations list */
115 struct ipc_port *cl_service; /* service port */
116 struct ipc_port *cl_control; /* control port */
117 struct { /* alarm chain head */
118 struct alarm *al_next;
119 } cl_alarm;
120 };
121 typedef struct clock clock_data_t;
122
123 /*
124 * Configure the clock system.
125 */
126 extern void clock_config(void);
127 /*
128 * Initialize the clock system.
129 */
130 extern void clock_init(void);
131
132 /*
133 * Initialize the clock ipc service facility.
134 */
135 extern void clock_service_create(void);
136
137 /*
138 * Service clock alarm interrupts. Called from machine dependent
139 * layer at splclock(). The clock_id argument specifies the clock,
140 * and the clock_time argument gives that clock's current time.
141 */
142 extern void clock_alarm_intr(
143 clock_id_t clock_id,
144 mach_timespec_t *clock_time);
145
146 extern kern_return_t clock_sleep_internal(
147 clock_t clock,
148 sleep_type_t sleep_type,
149 mach_timespec_t *sleep_time);
150
151 typedef void (*clock_timer_func_t)(
152 uint64_t timestamp);
153
154 extern void clock_set_timer_func(
155 clock_timer_func_t func);
156
157 extern void clock_set_timer_deadline(
158 uint64_t deadline);
159
160 extern void mk_timebase_info(
161 uint32_t *delta,
162 uint32_t *abs_to_ns_numer,
163 uint32_t *abs_to_ns_denom,
164 uint32_t *proc_to_abs_numer,
165 uint32_t *proc_to_abs_denom);
166
167 #endif /* MACH_KERNEL_PRIVATE */
168
169 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
170 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
171
172 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
173 MACH_TIMESPEC_SEC_MAX, \
174 MACH_TIMESPEC_NSEC_MAX } )
175 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
176
177 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
178 do { \
179 (t1)->tv_nsec += (clock_res_t)(nsec); \
180 if ((clock_res_t)(nsec) > 0 && \
181 (t1)->tv_nsec >= NSEC_PER_SEC) { \
182 (t1)->tv_nsec -= NSEC_PER_SEC; \
183 (t1)->tv_sec += 1; \
184 } \
185 else if ((clock_res_t)(nsec) < 0 && \
186 (t1)->tv_nsec < 0) { \
187 (t1)->tv_nsec += NSEC_PER_SEC; \
188 (t1)->tv_sec -= 1; \
189 } \
190 } while (0)
191
192 extern mach_timespec_t clock_get_system_value(void);
193
194 extern mach_timespec_t clock_get_calendar_value(void);
195
196 extern void clock_set_calendar_value(
197 mach_timespec_t value);
198
199 extern void clock_adjust_calendar(
200 clock_res_t nsec);
201
202 extern void clock_initialize_calendar(void);
203
204 extern mach_timespec_t clock_get_calendar_offset(void);
205
206 extern void clock_timebase_info(
207 mach_timebase_info_t info);
208
209 extern void clock_get_uptime(
210 uint64_t *result);
211
212 extern void clock_interval_to_deadline(
213 uint32_t interval,
214 uint32_t scale_factor,
215 uint64_t *result);
216
217 extern void clock_interval_to_absolutetime_interval(
218 uint32_t interval,
219 uint32_t scale_factor,
220 uint64_t *result);
221
222 extern void clock_absolutetime_interval_to_deadline(
223 uint64_t abstime,
224 uint64_t *result);
225
226 extern void clock_deadline_for_periodic_event(
227 uint64_t interval,
228 uint64_t abstime,
229 uint64_t *deadline);
230
231 extern void clock_delay_for_interval(
232 uint32_t interval,
233 uint32_t scale_factor);
234
235 extern void clock_delay_until(
236 uint64_t deadline);
237
238 extern void absolutetime_to_nanoseconds(
239 uint64_t abstime,
240 uint64_t *result);
241
242 extern void nanoseconds_to_absolutetime(
243 uint64_t nanoseconds,
244 uint64_t *result);
245
246 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
247
248 #include <libkern/OSBase.h>
249
250 #define clock_get_uptime(a) \
251 clock_get_uptime(__OSAbsoluteTimePtr(a))
252
253 #define clock_interval_to_deadline(a, b, c) \
254 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
255
256 #define clock_interval_to_absolutetime_interval(a, b, c) \
257 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
258
259 #define clock_absolutetime_interval_to_deadline(a, b) \
260 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
261
262 #define clock_deadline_for_periodic_event(a, b, c) \
263 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
264
265 #define clock_delay_until(a) \
266 clock_delay_until(__OSAbsoluteTime(a))
267
268 #define absolutetime_to_nanoseconds(a, b) \
269 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
270
271 #define nanoseconds_to_absolutetime(a, b) \
272 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
273
274 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
275
276 /* t1 < = > t2 */
277 #define CMP_ABSOLUTETIME(t1, t2) \
278 (AbsoluteTime_to_scalar(t1) > \
279 AbsoluteTime_to_scalar(t2)? (int)+1 : \
280 (AbsoluteTime_to_scalar(t1) < \
281 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
282
283 /* t1 += t2 */
284 #define ADD_ABSOLUTETIME(t1, t2) \
285 (AbsoluteTime_to_scalar(t1) += \
286 AbsoluteTime_to_scalar(t2))
287
288 /* t1 -= t2 */
289 #define SUB_ABSOLUTETIME(t1, t2) \
290 (AbsoluteTime_to_scalar(t1) -= \
291 AbsoluteTime_to_scalar(t2))
292
293 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
294 (AbsoluteTime_to_scalar(t1) += \
295 (int32_t)(ticks))
296
297 #endif
298
299 #endif /* _KERN_CLOCK_H_ */