2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
27 * Purpose: Data structures for the kernel alarm clock
28 * facility. This file is used only by kernel
29 * level clock facility routines.
32 #ifndef _KERN_CLOCK_H_
33 #define _KERN_CLOCK_H_
36 #include <mach/mach_types.h>
37 #include <mach/clock_types.h>
38 #include <mach/message.h>
39 #include <mach/mach_time.h>
41 #include <kern/kern_types.h>
43 #include <sys/cdefs.h>
45 #ifdef MACH_KERNEL_PRIVATE
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.
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 */
62 port_type
; /* alarm port type */
63 struct clock
*clock
; /* alarm clock */
64 void *data
; /* alarm data */
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 */
73 typedef struct alarm alarm_data_t
;
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 */
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.
87 int (*c_config
)(void); /* configuration */
89 int (*c_init
)(void); /* initialize */
91 kern_return_t (*c_gettime
)( /* get time */
92 mach_timespec_t
*cur_time
);
94 kern_return_t (*c_settime
)( /* set time */
95 mach_timespec_t
*clock_time
);
97 kern_return_t (*c_getattr
)( /* get attributes */
98 clock_flavor_t flavor
,
100 mach_msg_type_number_t
*count
);
102 kern_return_t (*c_setattr
)( /* set attributes */
103 clock_flavor_t flavor
,
105 mach_msg_type_number_t count
);
107 void (*c_setalrm
)( /* set next alarm */
108 mach_timespec_t
*alarm_time
);
110 typedef struct clock_ops
*clock_ops_t
;
111 typedef struct clock_ops clock_ops_data_t
;
114 * Actual clock object data structure. Contains the machine
115 * dependent operations list, clock operations ports, and a
116 * chain of pending alarms.
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
;
126 typedef struct clock clock_data_t
;
129 * Configure the clock system.
131 extern void clock_config(void);
134 * Initialize the clock system.
136 extern void clock_init(void);
138 extern void clock_timebase_init(void);
141 * Initialize the clock ipc service facility.
143 extern void clock_service_create(void);
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.
150 extern void clock_alarm_intr(
152 mach_timespec_t
*clock_time
);
154 extern kern_return_t
clock_sleep_internal(
156 sleep_type_t sleep_type
,
157 mach_timespec_t
*sleep_time
);
159 typedef void (*clock_timer_func_t
)(
162 extern void clock_set_timer_func(
163 clock_timer_func_t func
);
165 extern void clock_set_timer_deadline(
168 extern uint32_t clock_set_calendar_adjtime(
172 extern uint32_t clock_adjust_calendar(void);
174 extern void machine_delay_until(
177 #include <stat_time.h>
179 extern void hertz_tick(
182 #endif /* STAT_TIME */
183 boolean_t usermode
, /* executing user code */
186 extern void absolutetime_to_microtime(
189 uint32_t *microsecs
);
191 #endif /* MACH_KERNEL_PRIVATE */
195 #ifdef XNU_KERNEL_PRIVATE
197 extern void clock_adjtime(
201 extern void clock_initialize_calendar(void);
203 extern void clock_wakeup_calendar(void);
205 extern void clock_gettimeofday(
207 uint32_t *microsecs
);
209 extern void clock_set_calendar_microtime(
213 extern void clock_get_boottime_nanotime(
217 extern void clock_deadline_for_periodic_event(
222 #endif /* XNU_KERNEL_PRIVATE */
225 extern void clock_get_calendar_microtime(
227 uint32_t *microsecs
);
229 extern void clock_get_calendar_nanotime(
233 extern void clock_get_system_microtime(
235 uint32_t *microsecs
);
237 extern void clock_get_system_nanotime(
241 extern void clock_timebase_info(
242 mach_timebase_info_t info
);
244 extern void clock_get_uptime(
247 extern void clock_interval_to_deadline(
249 uint32_t scale_factor
,
252 extern void clock_interval_to_absolutetime_interval(
254 uint32_t scale_factor
,
257 extern void clock_absolutetime_interval_to_deadline(
261 extern void clock_delay_until(
264 extern void absolutetime_to_nanoseconds(
268 extern void nanoseconds_to_absolutetime(
269 uint64_t nanoseconds
,
272 #ifdef KERNEL_PRIVATE
275 * Obsolete interfaces.
278 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
279 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
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 } )
286 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
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; \
294 else if ((clock_res_t)(nsec) < 0 && \
295 (t1)->tv_nsec < 0) { \
296 (t1)->tv_nsec += NSEC_PER_SEC; \
302 extern mach_timespec_t
clock_get_system_value(void);
304 extern mach_timespec_t
clock_get_calendar_value(void);
306 extern void delay_for_interval(
308 uint32_t scale_factor
);
309 #ifndef MACH_KERNEL_PRIVATE
311 #ifndef ABSOLUTETIME_SCALAR_TYPE
313 #define clock_get_uptime(a) \
314 clock_get_uptime(__OSAbsoluteTimePtr(a))
316 #define clock_interval_to_deadline(a, b, c) \
317 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
319 #define clock_interval_to_absolutetime_interval(a, b, c) \
320 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
322 #define clock_absolutetime_interval_to_deadline(a, b) \
323 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
325 #define clock_deadline_for_periodic_event(a, b, c) \
326 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
328 #define clock_delay_until(a) \
329 clock_delay_until(__OSAbsoluteTime(a))
331 #define absolutetime_to_nanoseconds(a, b) \
332 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
334 #define nanoseconds_to_absolutetime(a, b) \
335 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
337 #endif /* ABSOLUTETIME_SCALAR_TYPE */
339 #endif /* !MACH_KERNEL_PRIVATE */
341 #endif /* KERNEL_PRIVATE */
345 #endif /* _KERN_CLOCK_H_ */