]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/clock.h
xnu-517.tar.gz
[apple/xnu.git] / osfmk / kern / clock.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
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
1c79356b
A
38#include <mach/message.h>
39#include <mach/clock_types.h>
40#include <mach/mach_time.h>
41
9bccf70c
A
42#include <sys/appleapiopts.h>
43
44#ifdef __APPLE_API_PRIVATE
45
46#ifdef MACH_KERNEL_PRIVATE
47
1c79356b
A
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 */
56struct 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};
76typedef 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 */
89struct 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};
113typedef struct clock_ops *clock_ops_t;
114typedef 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 */
121struct 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};
129typedef struct clock clock_data_t;
130
131/*
132 * Configure the clock system.
133 */
134extern void clock_config(void);
55e303ae 135
1c79356b
A
136/*
137 * Initialize the clock system.
138 */
139extern void clock_init(void);
140
55e303ae
A
141extern void clock_timebase_init(void);
142
1c79356b
A
143/*
144 * Initialize the clock ipc service facility.
145 */
146extern void clock_service_create(void);
147
148/*
149 * Service clock alarm interrupts. Called from machine dependent
150 * layer at splclock(). The clock_id argument specifies the clock,
151 * and the clock_time argument gives that clock's current time.
152 */
153extern void clock_alarm_intr(
154 clock_id_t clock_id,
155 mach_timespec_t *clock_time);
156
157extern kern_return_t clock_sleep_internal(
158 clock_t clock,
159 sleep_type_t sleep_type,
160 mach_timespec_t *sleep_time);
161
162typedef void (*clock_timer_func_t)(
0b4e3aa0 163 uint64_t timestamp);
1c79356b
A
164
165extern void clock_set_timer_func(
166 clock_timer_func_t func);
167
168extern void clock_set_timer_deadline(
0b4e3aa0 169 uint64_t deadline);
1c79356b
A
170
171extern void mk_timebase_info(
172 uint32_t *delta,
173 uint32_t *abs_to_ns_numer,
174 uint32_t *abs_to_ns_denom,
175 uint32_t *proc_to_abs_numer,
176 uint32_t *proc_to_abs_denom);
177
55e303ae
A
178extern uint32_t clock_set_calendar_adjtime(
179 int32_t *secs,
180 int32_t *microsecs);
9bccf70c 181
55e303ae 182extern uint32_t clock_adjust_calendar(void);
9bccf70c 183
1c79356b
A
184#endif /* MACH_KERNEL_PRIVATE */
185
55e303ae
A
186extern void clock_get_calendar_microtime(
187 uint32_t *secs,
188 uint32_t *microsecs);
189
190extern void clock_get_calendar_nanotime(
191 uint32_t *secs,
192 uint32_t *nanosecs);
193
194extern void clock_set_calendar_microtime(
195 uint32_t secs,
196 uint32_t microsecs);
9bccf70c 197
55e303ae
A
198extern void clock_get_system_microtime(
199 uint32_t *secs,
200 uint32_t *microsecs);
201
202extern void clock_get_system_nanotime(
203 uint32_t *secs,
204 uint32_t *nanosecs);
205
206extern void clock_adjtime(
207 int32_t *secs,
208 int32_t *microsecs);
9bccf70c
A
209
210extern void clock_initialize_calendar(void);
211
55e303ae
A
212extern void clock_wakeup_calendar(void);
213
214extern void clock_gettimeofday(
215 uint32_t *secs,
216 uint32_t *microsecs);
217
9bccf70c
A
218#endif /* __APPLE_API_PRIVATE */
219
220#ifdef __APPLE_API_UNSTABLE
221
1c79356b
A
222#define MACH_TIMESPEC_SEC_MAX (0 - 1)
223#define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
224
225#define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
226 MACH_TIMESPEC_SEC_MAX, \
227 MACH_TIMESPEC_NSEC_MAX } )
228#define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
229
230#define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
231 do { \
232 (t1)->tv_nsec += (clock_res_t)(nsec); \
233 if ((clock_res_t)(nsec) > 0 && \
234 (t1)->tv_nsec >= NSEC_PER_SEC) { \
235 (t1)->tv_nsec -= NSEC_PER_SEC; \
236 (t1)->tv_sec += 1; \
237 } \
238 else if ((clock_res_t)(nsec) < 0 && \
239 (t1)->tv_nsec < 0) { \
240 (t1)->tv_nsec += NSEC_PER_SEC; \
241 (t1)->tv_sec -= 1; \
242 } \
243 } while (0)
244
9bccf70c
A
245#endif /* __APPLE_API_UNSTABLE */
246
1c79356b
A
247extern mach_timespec_t clock_get_system_value(void);
248
249extern mach_timespec_t clock_get_calendar_value(void);
250
0b4e3aa0
A
251extern void clock_timebase_info(
252 mach_timebase_info_t info);
253
254extern void clock_get_uptime(
255 uint64_t *result);
256
257extern void clock_interval_to_deadline(
258 uint32_t interval,
259 uint32_t scale_factor,
260 uint64_t *result);
261
262extern void clock_interval_to_absolutetime_interval(
263 uint32_t interval,
264 uint32_t scale_factor,
265 uint64_t *result);
266
267extern void clock_absolutetime_interval_to_deadline(
268 uint64_t abstime,
269 uint64_t *result);
270
271extern void clock_deadline_for_periodic_event(
272 uint64_t interval,
273 uint64_t abstime,
274 uint64_t *deadline);
275
276extern void clock_delay_for_interval(
277 uint32_t interval,
278 uint32_t scale_factor);
279
280extern void clock_delay_until(
281 uint64_t deadline);
282
283extern void absolutetime_to_nanoseconds(
284 uint64_t abstime,
285 uint64_t *result);
286
287extern void nanoseconds_to_absolutetime(
288 uint64_t nanoseconds,
289 uint64_t *result);
290
291#if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
292
293#include <libkern/OSBase.h>
294
295#define clock_get_uptime(a) \
296 clock_get_uptime(__OSAbsoluteTimePtr(a))
297
298#define clock_interval_to_deadline(a, b, c) \
299 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
300
301#define clock_interval_to_absolutetime_interval(a, b, c) \
302 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
303
304#define clock_absolutetime_interval_to_deadline(a, b) \
305 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
306
307#define clock_deadline_for_periodic_event(a, b, c) \
308 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
309
310#define clock_delay_until(a) \
311 clock_delay_until(__OSAbsoluteTime(a))
312
313#define absolutetime_to_nanoseconds(a, b) \
314 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
315
316#define nanoseconds_to_absolutetime(a, b) \
317 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
318
1c79356b
A
319#define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
320
321/* t1 < = > t2 */
322#define CMP_ABSOLUTETIME(t1, t2) \
323 (AbsoluteTime_to_scalar(t1) > \
324 AbsoluteTime_to_scalar(t2)? (int)+1 : \
325 (AbsoluteTime_to_scalar(t1) < \
326 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
327
328/* t1 += t2 */
329#define ADD_ABSOLUTETIME(t1, t2) \
330 (AbsoluteTime_to_scalar(t1) += \
331 AbsoluteTime_to_scalar(t2))
332
333/* t1 -= t2 */
334#define SUB_ABSOLUTETIME(t1, t2) \
335 (AbsoluteTime_to_scalar(t1) -= \
336 AbsoluteTime_to_scalar(t2))
337
338#define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
339 (AbsoluteTime_to_scalar(t1) += \
0b4e3aa0 340 (int32_t)(ticks))
1c79356b 341
0b4e3aa0 342#endif
1c79356b
A
343
344#endif /* _KERN_CLOCK_H_ */