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