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