]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/clock.h
xnu-792.6.61.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 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
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.
1c79356b
A
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
91447636
A
35#include <stdint.h>
36#include <mach/mach_types.h>
1c79356b 37#include <mach/clock_types.h>
91447636 38#include <mach/message.h>
1c79356b
A
39#include <mach/mach_time.h>
40
91447636 41#include <kern/kern_types.h>
9bccf70c 42
91447636 43#include <sys/cdefs.h>
9bccf70c
A
44
45#ifdef MACH_KERNEL_PRIVATE
46
1c79356b
A
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 */
53struct 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};
73typedef 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 */
86struct 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};
110typedef struct clock_ops *clock_ops_t;
111typedef 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 */
118struct 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};
126typedef struct clock clock_data_t;
127
128/*
129 * Configure the clock system.
130 */
131extern void clock_config(void);
55e303ae 132
1c79356b
A
133/*
134 * Initialize the clock system.
135 */
136extern void clock_init(void);
137
55e303ae
A
138extern void clock_timebase_init(void);
139
1c79356b
A
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 167
55e303ae
A
168extern uint32_t clock_set_calendar_adjtime(
169 int32_t *secs,
170 int32_t *microsecs);
9bccf70c 171
55e303ae 172extern uint32_t clock_adjust_calendar(void);
9bccf70c 173
91447636
A
174extern void machine_delay_until(
175 uint64_t deadline);
1c79356b 176
91447636 177#include <stat_time.h>
55e303ae 178
91447636
A
179extern void hertz_tick(
180#if STAT_TIME
181 natural_t ticks,
182#endif /* STAT_TIME */
183 boolean_t usermode, /* executing user code */
184 natural_t pc);
55e303ae 185
91447636
A
186extern void absolutetime_to_microtime(
187 uint64_t abstime,
188 uint32_t *secs,
189 uint32_t *microsecs);
9bccf70c 190
91447636 191#endif /* MACH_KERNEL_PRIVATE */
55e303ae 192
91447636
A
193__BEGIN_DECLS
194
195#ifdef XNU_KERNEL_PRIVATE
55e303ae
A
196
197extern void clock_adjtime(
198 int32_t *secs,
199 int32_t *microsecs);
9bccf70c
A
200
201extern void clock_initialize_calendar(void);
202
55e303ae
A
203extern void clock_wakeup_calendar(void);
204
205extern void clock_gettimeofday(
206 uint32_t *secs,
207 uint32_t *microsecs);
208
91447636
A
209extern void clock_set_calendar_microtime(
210 uint32_t secs,
211 uint32_t microsecs);
9bccf70c 212
91447636
A
213extern void clock_get_boottime_nanotime(
214 uint32_t *secs,
215 uint32_t *nanosecs);
9bccf70c 216
91447636
A
217extern void clock_deadline_for_periodic_event(
218 uint64_t interval,
219 uint64_t abstime,
220 uint64_t *deadline);
1c79356b 221
91447636 222#endif /* XNU_KERNEL_PRIVATE */
1c79356b 223
1c79356b 224
91447636
A
225extern void clock_get_calendar_microtime(
226 uint32_t *secs,
227 uint32_t *microsecs);
9bccf70c 228
91447636
A
229extern void clock_get_calendar_nanotime(
230 uint32_t *secs,
231 uint32_t *nanosecs);
1c79356b 232
91447636
A
233extern void clock_get_system_microtime(
234 uint32_t *secs,
235 uint32_t *microsecs);
236
237extern void clock_get_system_nanotime(
238 uint32_t *secs,
239 uint32_t *nanosecs);
1c79356b 240
0b4e3aa0
A
241extern void clock_timebase_info(
242 mach_timebase_info_t info);
243
244extern void clock_get_uptime(
245 uint64_t *result);
246
247extern void clock_interval_to_deadline(
248 uint32_t interval,
249 uint32_t scale_factor,
250 uint64_t *result);
251
252extern void clock_interval_to_absolutetime_interval(
253 uint32_t interval,
254 uint32_t scale_factor,
255 uint64_t *result);
256
257extern void clock_absolutetime_interval_to_deadline(
258 uint64_t abstime,
259 uint64_t *result);
260
0b4e3aa0
A
261extern void clock_delay_until(
262 uint64_t deadline);
263
264extern void absolutetime_to_nanoseconds(
265 uint64_t abstime,
266 uint64_t *result);
267
268extern void nanoseconds_to_absolutetime(
269 uint64_t nanoseconds,
270 uint64_t *result);
271
91447636 272#ifdef KERNEL_PRIVATE
0b4e3aa0 273
91447636
A
274/*
275 * Obsolete interfaces.
276 */
277
278#define MACH_TIMESPEC_SEC_MAX (0 - 1)
279#define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
280
281#define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
282 MACH_TIMESPEC_SEC_MAX, \
283 MACH_TIMESPEC_NSEC_MAX } )
284#define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
285
286#define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
287 do { \
288 (t1)->tv_nsec += (clock_res_t)(nsec); \
289 if ((clock_res_t)(nsec) > 0 && \
290 (t1)->tv_nsec >= NSEC_PER_SEC) { \
291 (t1)->tv_nsec -= NSEC_PER_SEC; \
292 (t1)->tv_sec += 1; \
293 } \
294 else if ((clock_res_t)(nsec) < 0 && \
295 (t1)->tv_nsec < 0) { \
296 (t1)->tv_nsec += NSEC_PER_SEC; \
297 (t1)->tv_sec -= 1; \
298 } \
299 } while (0)
300
301
302extern mach_timespec_t clock_get_system_value(void);
303
304extern mach_timespec_t clock_get_calendar_value(void);
305
306extern void delay_for_interval(
307 uint32_t interval,
308 uint32_t scale_factor);
309#ifndef MACH_KERNEL_PRIVATE
310
311#ifndef ABSOLUTETIME_SCALAR_TYPE
0b4e3aa0
A
312
313#define clock_get_uptime(a) \
314 clock_get_uptime(__OSAbsoluteTimePtr(a))
315
316#define clock_interval_to_deadline(a, b, c) \
317 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
318
319#define clock_interval_to_absolutetime_interval(a, b, c) \
320 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
321
322#define clock_absolutetime_interval_to_deadline(a, b) \
323 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
324
325#define clock_deadline_for_periodic_event(a, b, c) \
326 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
327
328#define clock_delay_until(a) \
329 clock_delay_until(__OSAbsoluteTime(a))
330
331#define absolutetime_to_nanoseconds(a, b) \
332 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
333
334#define nanoseconds_to_absolutetime(a, b) \
335 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
336
91447636 337#endif /* ABSOLUTETIME_SCALAR_TYPE */
1c79356b 338
91447636 339#endif /* !MACH_KERNEL_PRIVATE */
1c79356b 340
91447636 341#endif /* KERNEL_PRIVATE */
1c79356b 342
91447636 343__END_DECLS
1c79356b
A
344
345#endif /* _KERN_CLOCK_H_ */