2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 * @APPLE_LICENSE_HEADER_END@
28 * Purpose: Data structures for the kernel alarm clock
29 * facility. This file is used only by kernel
30 * level clock facility routines.
33 #ifndef _KERN_CLOCK_H_
34 #define _KERN_CLOCK_H_
37 #include <mach/mach_types.h>
38 #include <mach/clock_types.h>
39 #include <mach/message.h>
40 #include <mach/mach_time.h>
42 #include <kern/kern_types.h>
44 #include <sys/cdefs.h>
46 #ifdef MACH_KERNEL_PRIVATE
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.
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 */
63 port_type
; /* alarm port type */
64 struct clock
*clock
; /* alarm clock */
65 void *data
; /* alarm data */
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 */
74 typedef struct alarm alarm_data_t
;
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 */
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.
88 int (*c_config
)(void); /* configuration */
90 int (*c_init
)(void); /* initialize */
92 kern_return_t (*c_gettime
)( /* get time */
93 mach_timespec_t
*cur_time
);
95 kern_return_t (*c_settime
)( /* set time */
96 mach_timespec_t
*clock_time
);
98 kern_return_t (*c_getattr
)( /* get attributes */
99 clock_flavor_t flavor
,
101 mach_msg_type_number_t
*count
);
103 kern_return_t (*c_setattr
)( /* set attributes */
104 clock_flavor_t flavor
,
106 mach_msg_type_number_t count
);
108 void (*c_setalrm
)( /* set next alarm */
109 mach_timespec_t
*alarm_time
);
111 typedef struct clock_ops
*clock_ops_t
;
112 typedef struct clock_ops clock_ops_data_t
;
115 * Actual clock object data structure. Contains the machine
116 * dependent operations list, clock operations ports, and a
117 * chain of pending alarms.
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
;
127 typedef struct clock clock_data_t
;
130 * Configure the clock system.
132 extern void clock_config(void);
135 * Initialize the clock system.
137 extern void clock_init(void);
139 extern void clock_timebase_init(void);
142 * Initialize the clock ipc service facility.
144 extern void clock_service_create(void);
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.
151 extern void clock_alarm_intr(
153 mach_timespec_t
*clock_time
);
155 extern kern_return_t
clock_sleep_internal(
157 sleep_type_t sleep_type
,
158 mach_timespec_t
*sleep_time
);
160 typedef void (*clock_timer_func_t
)(
163 extern void clock_set_timer_func(
164 clock_timer_func_t func
);
166 extern void clock_set_timer_deadline(
169 extern uint32_t clock_set_calendar_adjtime(
173 extern uint32_t clock_adjust_calendar(void);
175 extern void machine_delay_until(
178 #include <stat_time.h>
180 extern void hertz_tick(
183 #endif /* STAT_TIME */
184 boolean_t usermode
, /* executing user code */
187 extern void absolutetime_to_microtime(
190 uint32_t *microsecs
);
192 #endif /* MACH_KERNEL_PRIVATE */
196 #ifdef XNU_KERNEL_PRIVATE
198 extern void clock_adjtime(
202 extern void clock_initialize_calendar(void);
204 extern void clock_wakeup_calendar(void);
206 extern void clock_gettimeofday(
208 uint32_t *microsecs
);
210 extern void clock_set_calendar_microtime(
214 extern void clock_get_boottime_nanotime(
218 extern void clock_deadline_for_periodic_event(
223 #endif /* XNU_KERNEL_PRIVATE */
226 extern void clock_get_calendar_microtime(
228 uint32_t *microsecs
);
230 extern void clock_get_calendar_nanotime(
234 extern void clock_get_system_microtime(
236 uint32_t *microsecs
);
238 extern void clock_get_system_nanotime(
242 extern void clock_timebase_info(
243 mach_timebase_info_t info
);
245 extern void clock_get_uptime(
248 extern void clock_interval_to_deadline(
250 uint32_t scale_factor
,
253 extern void clock_interval_to_absolutetime_interval(
255 uint32_t scale_factor
,
258 extern void clock_absolutetime_interval_to_deadline(
262 extern void clock_delay_until(
265 extern void absolutetime_to_nanoseconds(
269 extern void nanoseconds_to_absolutetime(
270 uint64_t nanoseconds
,
273 #ifdef KERNEL_PRIVATE
276 * Obsolete interfaces.
279 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
280 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
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 } )
287 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
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; \
295 else if ((clock_res_t)(nsec) < 0 && \
296 (t1)->tv_nsec < 0) { \
297 (t1)->tv_nsec += NSEC_PER_SEC; \
303 extern mach_timespec_t
clock_get_system_value(void);
305 extern mach_timespec_t
clock_get_calendar_value(void);
307 extern void delay_for_interval(
309 uint32_t scale_factor
);
310 #ifndef MACH_KERNEL_PRIVATE
312 #ifndef ABSOLUTETIME_SCALAR_TYPE
314 #define clock_get_uptime(a) \
315 clock_get_uptime(__OSAbsoluteTimePtr(a))
317 #define clock_interval_to_deadline(a, b, c) \
318 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
320 #define clock_interval_to_absolutetime_interval(a, b, c) \
321 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
323 #define clock_absolutetime_interval_to_deadline(a, b) \
324 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
326 #define clock_deadline_for_periodic_event(a, b, c) \
327 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
329 #define clock_delay_until(a) \
330 clock_delay_until(__OSAbsoluteTime(a))
332 #define absolutetime_to_nanoseconds(a, b) \
333 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
335 #define nanoseconds_to_absolutetime(a, b) \
336 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
338 #endif /* ABSOLUTETIME_SCALAR_TYPE */
340 #endif /* !MACH_KERNEL_PRIVATE */
342 #endif /* KERNEL_PRIVATE */
346 #endif /* _KERN_CLOCK_H_ */