]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/clock.h
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / kern / clock.h
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ff6e181a
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
1c79356b 12 *
ff6e181a
A
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * @OSF_COPYRIGHT@
25 */
26/*
27 * File: kern/clock.h
28 * Purpose: Data structures for the kernel alarm clock
29 * facility. This file is used only by kernel
30 * level clock facility routines.
31 */
32
33#ifndef _KERN_CLOCK_H_
34#define _KERN_CLOCK_H_
35
91447636
A
36#include <stdint.h>
37#include <mach/mach_types.h>
1c79356b 38#include <mach/clock_types.h>
91447636 39#include <mach/message.h>
1c79356b
A
40#include <mach/mach_time.h>
41
91447636 42#include <kern/kern_types.h>
9bccf70c 43
91447636 44#include <sys/cdefs.h>
9bccf70c
A
45
46#ifdef MACH_KERNEL_PRIVATE
47
1c79356b
A
48/*
49 * Actual clock alarm structure. Used for user clock_sleep() and
50 * clock_alarm() calls. Alarms are allocated from the alarm free
51 * list and entered in time priority order into the active alarm
52 * chain of the target clock.
53 */
54struct alarm {
55 struct alarm *al_next; /* next alarm in chain */
56 struct alarm *al_prev; /* previous alarm in chain */
57 int al_status; /* alarm status */
58 mach_timespec_t al_time; /* alarm time */
59 struct { /* message alarm data */
60 int type; /* alarm type */
61 ipc_port_t port; /* alarm port */
62 mach_msg_type_name_t
63 port_type; /* alarm port type */
64 struct clock *clock; /* alarm clock */
65 void *data; /* alarm data */
66 } al_alrm;
67#define al_type al_alrm.type
68#define al_port al_alrm.port
69#define al_port_type al_alrm.port_type
70#define al_clock al_alrm.clock
71#define al_data al_alrm.data
72 long al_seqno; /* alarm sequence number */
73};
74typedef struct alarm alarm_data_t;
75
76/* alarm status */
77#define ALARM_FREE 0 /* alarm is on free list */
78#define ALARM_SLEEP 1 /* active clock_sleep() */
79#define ALARM_CLOCK 2 /* active clock_alarm() */
80#define ALARM_DONE 4 /* alarm has expired */
81
82/*
83 * Clock operations list structure. Contains vectors to machine
84 * dependent clock routines. The routines c_config, c_init, and
85 * c_gettime must be implemented for every clock device.
86 */
87struct clock_ops {
88 int (*c_config)(void); /* configuration */
89
90 int (*c_init)(void); /* initialize */
91
92 kern_return_t (*c_gettime)( /* get time */
93 mach_timespec_t *cur_time);
94
95 kern_return_t (*c_settime)( /* set time */
96 mach_timespec_t *clock_time);
97
98 kern_return_t (*c_getattr)( /* get attributes */
99 clock_flavor_t flavor,
100 clock_attr_t attr,
101 mach_msg_type_number_t *count);
102
103 kern_return_t (*c_setattr)( /* set attributes */
104 clock_flavor_t flavor,
105 clock_attr_t attr,
106 mach_msg_type_number_t count);
107
108 void (*c_setalrm)( /* set next alarm */
109 mach_timespec_t *alarm_time);
110};
111typedef struct clock_ops *clock_ops_t;
112typedef struct clock_ops clock_ops_data_t;
113
114/*
115 * Actual clock object data structure. Contains the machine
116 * dependent operations list, clock operations ports, and a
117 * chain of pending alarms.
118 */
119struct clock {
120 clock_ops_t cl_ops; /* operations list */
121 struct ipc_port *cl_service; /* service port */
122 struct ipc_port *cl_control; /* control port */
123 struct { /* alarm chain head */
124 struct alarm *al_next;
125 } cl_alarm;
126};
127typedef struct clock clock_data_t;
128
129/*
130 * Configure the clock system.
131 */
132extern void clock_config(void);
55e303ae 133
1c79356b
A
134/*
135 * Initialize the clock system.
136 */
137extern void clock_init(void);
138
55e303ae
A
139extern void clock_timebase_init(void);
140
1c79356b
A
141/*
142 * Initialize the clock ipc service facility.
143 */
144extern void clock_service_create(void);
145
146/*
147 * Service clock alarm interrupts. Called from machine dependent
148 * layer at splclock(). The clock_id argument specifies the clock,
149 * and the clock_time argument gives that clock's current time.
150 */
151extern void clock_alarm_intr(
152 clock_id_t clock_id,
153 mach_timespec_t *clock_time);
154
155extern kern_return_t clock_sleep_internal(
156 clock_t clock,
157 sleep_type_t sleep_type,
158 mach_timespec_t *sleep_time);
159
160typedef void (*clock_timer_func_t)(
0b4e3aa0 161 uint64_t timestamp);
1c79356b
A
162
163extern void clock_set_timer_func(
164 clock_timer_func_t func);
165
166extern void clock_set_timer_deadline(
0b4e3aa0 167 uint64_t deadline);
1c79356b 168
55e303ae
A
169extern uint32_t clock_set_calendar_adjtime(
170 int32_t *secs,
171 int32_t *microsecs);
9bccf70c 172
55e303ae 173extern uint32_t clock_adjust_calendar(void);
9bccf70c 174
91447636
A
175extern void machine_delay_until(
176 uint64_t deadline);
1c79356b 177
91447636 178#include <stat_time.h>
55e303ae 179
91447636
A
180extern void hertz_tick(
181#if STAT_TIME
182 natural_t ticks,
183#endif /* STAT_TIME */
184 boolean_t usermode, /* executing user code */
185 natural_t pc);
55e303ae 186
91447636
A
187extern void absolutetime_to_microtime(
188 uint64_t abstime,
189 uint32_t *secs,
190 uint32_t *microsecs);
9bccf70c 191
91447636 192#endif /* MACH_KERNEL_PRIVATE */
55e303ae 193
91447636
A
194__BEGIN_DECLS
195
196#ifdef XNU_KERNEL_PRIVATE
55e303ae
A
197
198extern void clock_adjtime(
199 int32_t *secs,
200 int32_t *microsecs);
9bccf70c
A
201
202extern void clock_initialize_calendar(void);
203
55e303ae
A
204extern void clock_wakeup_calendar(void);
205
206extern void clock_gettimeofday(
207 uint32_t *secs,
208 uint32_t *microsecs);
209
91447636
A
210extern void clock_set_calendar_microtime(
211 uint32_t secs,
212 uint32_t microsecs);
9bccf70c 213
91447636
A
214extern void clock_get_boottime_nanotime(
215 uint32_t *secs,
216 uint32_t *nanosecs);
9bccf70c 217
91447636
A
218extern void clock_deadline_for_periodic_event(
219 uint64_t interval,
220 uint64_t abstime,
221 uint64_t *deadline);
1c79356b 222
91447636 223#endif /* XNU_KERNEL_PRIVATE */
1c79356b 224
1c79356b 225
91447636
A
226extern void clock_get_calendar_microtime(
227 uint32_t *secs,
228 uint32_t *microsecs);
9bccf70c 229
91447636
A
230extern void clock_get_calendar_nanotime(
231 uint32_t *secs,
232 uint32_t *nanosecs);
1c79356b 233
91447636
A
234extern void clock_get_system_microtime(
235 uint32_t *secs,
236 uint32_t *microsecs);
237
238extern void clock_get_system_nanotime(
239 uint32_t *secs,
240 uint32_t *nanosecs);
1c79356b 241
0b4e3aa0
A
242extern void clock_timebase_info(
243 mach_timebase_info_t info);
244
245extern void clock_get_uptime(
246 uint64_t *result);
247
248extern void clock_interval_to_deadline(
249 uint32_t interval,
250 uint32_t scale_factor,
251 uint64_t *result);
252
253extern void clock_interval_to_absolutetime_interval(
254 uint32_t interval,
255 uint32_t scale_factor,
256 uint64_t *result);
257
258extern void clock_absolutetime_interval_to_deadline(
259 uint64_t abstime,
260 uint64_t *result);
261
0b4e3aa0
A
262extern void clock_delay_until(
263 uint64_t deadline);
264
265extern void absolutetime_to_nanoseconds(
266 uint64_t abstime,
267 uint64_t *result);
268
269extern void nanoseconds_to_absolutetime(
270 uint64_t nanoseconds,
271 uint64_t *result);
272
91447636 273#ifdef KERNEL_PRIVATE
0b4e3aa0 274
91447636
A
275/*
276 * Obsolete interfaces.
277 */
278
279#define MACH_TIMESPEC_SEC_MAX (0 - 1)
280#define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
281
282#define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
283 MACH_TIMESPEC_SEC_MAX, \
284 MACH_TIMESPEC_NSEC_MAX } )
285#define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
286
287#define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
288 do { \
289 (t1)->tv_nsec += (clock_res_t)(nsec); \
290 if ((clock_res_t)(nsec) > 0 && \
291 (t1)->tv_nsec >= NSEC_PER_SEC) { \
292 (t1)->tv_nsec -= NSEC_PER_SEC; \
293 (t1)->tv_sec += 1; \
294 } \
295 else if ((clock_res_t)(nsec) < 0 && \
296 (t1)->tv_nsec < 0) { \
297 (t1)->tv_nsec += NSEC_PER_SEC; \
298 (t1)->tv_sec -= 1; \
299 } \
300 } while (0)
301
302
303extern mach_timespec_t clock_get_system_value(void);
304
305extern mach_timespec_t clock_get_calendar_value(void);
306
307extern void delay_for_interval(
308 uint32_t interval,
309 uint32_t scale_factor);
310#ifndef MACH_KERNEL_PRIVATE
311
312#ifndef ABSOLUTETIME_SCALAR_TYPE
0b4e3aa0
A
313
314#define clock_get_uptime(a) \
315 clock_get_uptime(__OSAbsoluteTimePtr(a))
316
317#define clock_interval_to_deadline(a, b, c) \
318 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
319
320#define clock_interval_to_absolutetime_interval(a, b, c) \
321 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
322
323#define clock_absolutetime_interval_to_deadline(a, b) \
324 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
325
326#define clock_deadline_for_periodic_event(a, b, c) \
327 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
328
329#define clock_delay_until(a) \
330 clock_delay_until(__OSAbsoluteTime(a))
331
332#define absolutetime_to_nanoseconds(a, b) \
333 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
334
335#define nanoseconds_to_absolutetime(a, b) \
336 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
337
91447636 338#endif /* ABSOLUTETIME_SCALAR_TYPE */
1c79356b 339
91447636 340#endif /* !MACH_KERNEL_PRIVATE */
1c79356b 341
91447636 342#endif /* KERNEL_PRIVATE */
1c79356b 343
91447636 344__END_DECLS
1c79356b
A
345
346#endif /* _KERN_CLOCK_H_ */