2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * Purpose: Data structures for the kernel alarm clock
34 * facility. This file is used only by kernel
35 * level clock facility routines.
38 #ifndef _KERN_CLOCK_H_
39 #define _KERN_CLOCK_H_
42 #include <mach/mach_types.h>
43 #include <mach/clock_types.h>
44 #include <mach/message.h>
45 #include <mach/mach_time.h>
47 #include <kern/kern_types.h>
49 #include <sys/cdefs.h>
51 #ifdef MACH_KERNEL_PRIVATE
54 * Actual clock alarm structure. Used for user clock_sleep() and
55 * clock_alarm() calls. Alarms are allocated from the alarm free
56 * list and entered in time priority order into the active alarm
57 * chain of the target clock.
60 struct alarm
*al_next
; /* next alarm in chain */
61 struct alarm
*al_prev
; /* previous alarm in chain */
62 int al_status
; /* alarm status */
63 mach_timespec_t al_time
; /* alarm time */
64 struct { /* message alarm data */
65 int type
; /* alarm type */
66 ipc_port_t port
; /* alarm port */
68 port_type
; /* alarm port type */
69 struct clock
*clock
; /* alarm clock */
70 void *data
; /* alarm data */
72 #define al_type al_alrm.type
73 #define al_port al_alrm.port
74 #define al_port_type al_alrm.port_type
75 #define al_clock al_alrm.clock
76 #define al_data al_alrm.data
77 long al_seqno
; /* alarm sequence number */
79 typedef struct alarm alarm_data_t
;
82 #define ALARM_FREE 0 /* alarm is on free list */
83 #define ALARM_SLEEP 1 /* active clock_sleep() */
84 #define ALARM_CLOCK 2 /* active clock_alarm() */
85 #define ALARM_DONE 4 /* alarm has expired */
88 * Clock operations list structure. Contains vectors to machine
89 * dependent clock routines. The routines c_config, c_init, and
90 * c_gettime must be implemented for every clock device.
93 int (*c_config
)(void); /* configuration */
95 int (*c_init
)(void); /* initialize */
97 kern_return_t (*c_gettime
)( /* get time */
98 mach_timespec_t
*cur_time
);
100 kern_return_t (*c_settime
)( /* set time */
101 mach_timespec_t
*clock_time
);
103 kern_return_t (*c_getattr
)( /* get attributes */
104 clock_flavor_t flavor
,
106 mach_msg_type_number_t
*count
);
108 kern_return_t (*c_setattr
)( /* set attributes */
109 clock_flavor_t flavor
,
111 mach_msg_type_number_t count
);
113 void (*c_setalrm
)( /* set next alarm */
114 mach_timespec_t
*alarm_time
);
116 typedef struct clock_ops
*clock_ops_t
;
117 typedef struct clock_ops clock_ops_data_t
;
120 * Actual clock object data structure. Contains the machine
121 * dependent operations list, clock operations ports, and a
122 * chain of pending alarms.
125 clock_ops_t cl_ops
; /* operations list */
126 struct ipc_port
*cl_service
; /* service port */
127 struct ipc_port
*cl_control
; /* control port */
128 struct { /* alarm chain head */
129 struct alarm
*al_next
;
132 typedef struct clock clock_data_t
;
135 * Configure the clock system.
137 extern void clock_config(void);
140 * Initialize the clock system.
142 extern void clock_init(void);
144 extern void clock_timebase_init(void);
147 * Initialize the clock ipc service facility.
149 extern void clock_service_create(void);
152 * Service clock alarm interrupts. Called from machine dependent
153 * layer at splclock(). The clock_id argument specifies the clock,
154 * and the clock_time argument gives that clock's current time.
156 extern void clock_alarm_intr(
158 mach_timespec_t
*clock_time
);
160 extern kern_return_t
clock_sleep_internal(
162 sleep_type_t sleep_type
,
163 mach_timespec_t
*sleep_time
);
165 typedef void (*clock_timer_func_t
)(
168 extern void clock_set_timer_func(
169 clock_timer_func_t func
);
171 extern void clock_set_timer_deadline(
174 extern uint32_t clock_set_calendar_adjtime(
178 extern uint32_t clock_adjust_calendar(void);
180 extern void machine_delay_until(
183 #include <stat_time.h>
185 extern void hertz_tick(
188 #endif /* STAT_TIME */
189 boolean_t usermode
, /* executing user code */
192 extern void absolutetime_to_microtime(
195 uint32_t *microsecs
);
197 #endif /* MACH_KERNEL_PRIVATE */
201 #ifdef XNU_KERNEL_PRIVATE
203 extern void clock_adjtime(
207 extern void clock_initialize_calendar(void);
209 extern void clock_wakeup_calendar(void);
211 extern void clock_gettimeofday(
213 uint32_t *microsecs
);
215 extern void clock_set_calendar_microtime(
219 extern void clock_get_boottime_nanotime(
223 extern void clock_deadline_for_periodic_event(
228 #endif /* XNU_KERNEL_PRIVATE */
231 extern void clock_get_calendar_microtime(
233 uint32_t *microsecs
);
235 extern void clock_get_calendar_nanotime(
239 extern void clock_get_system_microtime(
241 uint32_t *microsecs
);
243 extern void clock_get_system_nanotime(
247 extern void clock_timebase_info(
248 mach_timebase_info_t info
);
250 extern void clock_get_uptime(
253 extern void clock_interval_to_deadline(
255 uint32_t scale_factor
,
258 extern void clock_interval_to_absolutetime_interval(
260 uint32_t scale_factor
,
263 extern void clock_absolutetime_interval_to_deadline(
267 extern void clock_delay_until(
270 extern void absolutetime_to_nanoseconds(
274 extern void nanoseconds_to_absolutetime(
275 uint64_t nanoseconds
,
278 #ifdef KERNEL_PRIVATE
281 * Obsolete interfaces.
284 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
285 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
287 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
288 MACH_TIMESPEC_SEC_MAX, \
289 MACH_TIMESPEC_NSEC_MAX } )
290 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
292 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
294 (t1)->tv_nsec += (clock_res_t)(nsec); \
295 if ((clock_res_t)(nsec) > 0 && \
296 (t1)->tv_nsec >= NSEC_PER_SEC) { \
297 (t1)->tv_nsec -= NSEC_PER_SEC; \
300 else if ((clock_res_t)(nsec) < 0 && \
301 (t1)->tv_nsec < 0) { \
302 (t1)->tv_nsec += NSEC_PER_SEC; \
308 extern mach_timespec_t
clock_get_system_value(void);
310 extern mach_timespec_t
clock_get_calendar_value(void);
312 extern void delay_for_interval(
314 uint32_t scale_factor
);
315 #ifndef MACH_KERNEL_PRIVATE
317 #ifndef ABSOLUTETIME_SCALAR_TYPE
319 #define clock_get_uptime(a) \
320 clock_get_uptime(__OSAbsoluteTimePtr(a))
322 #define clock_interval_to_deadline(a, b, c) \
323 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
325 #define clock_interval_to_absolutetime_interval(a, b, c) \
326 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
328 #define clock_absolutetime_interval_to_deadline(a, b) \
329 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
331 #define clock_deadline_for_periodic_event(a, b, c) \
332 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
334 #define clock_delay_until(a) \
335 clock_delay_until(__OSAbsoluteTime(a))
337 #define absolutetime_to_nanoseconds(a, b) \
338 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
340 #define nanoseconds_to_absolutetime(a, b) \
341 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
343 #endif /* ABSOLUTETIME_SCALAR_TYPE */
345 #endif /* !MACH_KERNEL_PRIVATE */
347 #endif /* KERNEL_PRIVATE */
351 #endif /* _KERN_CLOCK_H_ */