2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
35 * Purpose: Data structures for the kernel alarm clock
36 * facility. This file is used only by kernel
37 * level clock facility routines.
40 #ifndef _KERN_CLOCK_H_
41 #define _KERN_CLOCK_H_
44 #include <mach/mach_types.h>
45 #include <mach/clock_types.h>
46 #include <mach/message.h>
47 #include <mach/mach_time.h>
49 #include <kern/kern_types.h>
51 #include <sys/cdefs.h>
53 #ifdef MACH_KERNEL_PRIVATE
56 * Actual clock alarm structure. Used for user clock_sleep() and
57 * clock_alarm() calls. Alarms are allocated from the alarm free
58 * list and entered in time priority order into the active alarm
59 * chain of the target clock.
62 struct alarm
*al_next
; /* next alarm in chain */
63 struct alarm
*al_prev
; /* previous alarm in chain */
64 int al_status
; /* alarm status */
65 mach_timespec_t al_time
; /* alarm time */
66 struct { /* message alarm data */
67 int type
; /* alarm type */
68 ipc_port_t port
; /* alarm port */
70 port_type
; /* alarm port type */
71 struct clock
*clock
; /* alarm clock */
72 void *data
; /* alarm data */
74 #define al_type al_alrm.type
75 #define al_port al_alrm.port
76 #define al_port_type al_alrm.port_type
77 #define al_clock al_alrm.clock
78 #define al_data al_alrm.data
79 long al_seqno
; /* alarm sequence number */
81 typedef struct alarm alarm_data_t
;
84 #define ALARM_FREE 0 /* alarm is on free list */
85 #define ALARM_SLEEP 1 /* active clock_sleep() */
86 #define ALARM_CLOCK 2 /* active clock_alarm() */
87 #define ALARM_DONE 4 /* alarm has expired */
90 * Clock operations list structure. Contains vectors to machine
91 * dependent clock routines. The routines c_config, c_init, and
92 * c_gettime must be implemented for every clock device.
95 int (*c_config
)(void); /* configuration */
97 int (*c_init
)(void); /* initialize */
99 kern_return_t (*c_gettime
)( /* get time */
100 mach_timespec_t
*cur_time
);
102 kern_return_t (*c_settime
)( /* set time */
103 mach_timespec_t
*clock_time
);
105 kern_return_t (*c_getattr
)( /* get attributes */
106 clock_flavor_t flavor
,
108 mach_msg_type_number_t
*count
);
110 kern_return_t (*c_setattr
)( /* set attributes */
111 clock_flavor_t flavor
,
113 mach_msg_type_number_t count
);
115 void (*c_setalrm
)( /* set next alarm */
116 mach_timespec_t
*alarm_time
);
118 typedef struct clock_ops
*clock_ops_t
;
119 typedef struct clock_ops clock_ops_data_t
;
122 * Actual clock object data structure. Contains the machine
123 * dependent operations list, clock operations ports, and a
124 * chain of pending alarms.
127 clock_ops_t cl_ops
; /* operations list */
128 struct ipc_port
*cl_service
; /* service port */
129 struct ipc_port
*cl_control
; /* control port */
130 struct { /* alarm chain head */
131 struct alarm
*al_next
;
134 typedef struct clock clock_data_t
;
137 * Configure the clock system.
139 extern void clock_config(void);
142 * Initialize the clock system.
144 extern void clock_init(void);
146 extern void clock_timebase_init(void);
149 * Initialize the clock ipc service facility.
151 extern void clock_service_create(void);
154 * Service clock alarm interrupts. Called from machine dependent
155 * layer at splclock(). The clock_id argument specifies the clock,
156 * and the clock_time argument gives that clock's current time.
158 extern void clock_alarm_intr(
160 mach_timespec_t
*clock_time
);
162 extern kern_return_t
clock_sleep_internal(
164 sleep_type_t sleep_type
,
165 mach_timespec_t
*sleep_time
);
167 typedef void (*clock_timer_func_t
)(
170 extern void clock_set_timer_func(
171 clock_timer_func_t func
);
173 extern void clock_set_timer_deadline(
176 extern uint32_t clock_set_calendar_adjtime(
180 extern uint32_t clock_adjust_calendar(void);
182 extern void machine_delay_until(
185 #include <stat_time.h>
187 extern void hertz_tick(
190 #endif /* STAT_TIME */
191 boolean_t usermode
, /* executing user code */
194 extern void absolutetime_to_microtime(
197 uint32_t *microsecs
);
199 #endif /* MACH_KERNEL_PRIVATE */
203 #ifdef XNU_KERNEL_PRIVATE
205 extern void clock_adjtime(
209 extern void clock_initialize_calendar(void);
211 extern void clock_wakeup_calendar(void);
213 extern void clock_gettimeofday(
215 uint32_t *microsecs
);
217 extern void clock_set_calendar_microtime(
221 extern void clock_get_boottime_nanotime(
225 extern void clock_deadline_for_periodic_event(
230 #endif /* XNU_KERNEL_PRIVATE */
233 extern void clock_get_calendar_microtime(
235 uint32_t *microsecs
);
237 extern void clock_get_calendar_nanotime(
241 extern void clock_get_system_microtime(
243 uint32_t *microsecs
);
245 extern void clock_get_system_nanotime(
249 extern void clock_timebase_info(
250 mach_timebase_info_t info
);
252 extern void clock_get_uptime(
255 extern void clock_interval_to_deadline(
257 uint32_t scale_factor
,
260 extern void clock_interval_to_absolutetime_interval(
262 uint32_t scale_factor
,
265 extern void clock_absolutetime_interval_to_deadline(
269 extern void clock_delay_until(
272 extern void absolutetime_to_nanoseconds(
276 extern void nanoseconds_to_absolutetime(
277 uint64_t nanoseconds
,
280 #ifdef KERNEL_PRIVATE
283 * Obsolete interfaces.
286 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
287 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
289 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
290 MACH_TIMESPEC_SEC_MAX, \
291 MACH_TIMESPEC_NSEC_MAX } )
292 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
294 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
296 (t1)->tv_nsec += (clock_res_t)(nsec); \
297 if ((clock_res_t)(nsec) > 0 && \
298 (t1)->tv_nsec >= NSEC_PER_SEC) { \
299 (t1)->tv_nsec -= NSEC_PER_SEC; \
302 else if ((clock_res_t)(nsec) < 0 && \
303 (t1)->tv_nsec < 0) { \
304 (t1)->tv_nsec += NSEC_PER_SEC; \
310 extern mach_timespec_t
clock_get_system_value(void);
312 extern mach_timespec_t
clock_get_calendar_value(void);
314 extern void delay_for_interval(
316 uint32_t scale_factor
);
317 #ifndef MACH_KERNEL_PRIVATE
319 #ifndef ABSOLUTETIME_SCALAR_TYPE
321 #define clock_get_uptime(a) \
322 clock_get_uptime(__OSAbsoluteTimePtr(a))
324 #define clock_interval_to_deadline(a, b, c) \
325 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
327 #define clock_interval_to_absolutetime_interval(a, b, c) \
328 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
330 #define clock_absolutetime_interval_to_deadline(a, b) \
331 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
333 #define clock_deadline_for_periodic_event(a, b, c) \
334 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
336 #define clock_delay_until(a) \
337 clock_delay_until(__OSAbsoluteTime(a))
339 #define absolutetime_to_nanoseconds(a, b) \
340 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
342 #define nanoseconds_to_absolutetime(a, b) \
343 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
345 #endif /* ABSOLUTETIME_SCALAR_TYPE */
347 #endif /* !MACH_KERNEL_PRIVATE */
349 #endif /* KERNEL_PRIVATE */
353 #endif /* _KERN_CLOCK_H_ */