1/*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 */
33
34#ifndef	_KERN_CLOCK_H_
35#define	_KERN_CLOCK_H_
36
37#include <stdint.h>
38#include <mach/mach_types.h>
39#include <mach/clock_types.h>
40#include <mach/message.h>
41#include <mach/mach_time.h>
42
43#include <kern/kern_types.h>
44
45#include <sys/cdefs.h>
46
47#ifdef	__LP64__
48
49typedef unsigned long		clock_sec_t;
50typedef unsigned int		clock_usec_t, clock_nsec_t;
51
52#else	/* __LP64__ */
53
54typedef uint32_t			clock_sec_t;
55typedef uint32_t			clock_usec_t, clock_nsec_t;
56
57#endif	/* __LP64__ */
58
59#ifdef	MACH_KERNEL_PRIVATE
60
61#include <kern/queue.h>
62
63/*
64 * Clock operations list structure. Contains vectors to machine
65 * dependent clock routines.
66 */
67struct	clock_ops {
68	int		(*c_config)(void);		/* configuration */
69
70	int		(*c_init)(void);		/* initialize */
71
72	kern_return_t	(*c_gettime)(	/* get time */
73				mach_timespec_t			*cur_time);
74
75	kern_return_t	(*c_getattr)(	/* get attributes */
76				clock_flavor_t			flavor,
77				clock_attr_t			attr,
78				mach_msg_type_number_t	*count);
79};
80typedef struct clock_ops	*clock_ops_t;
81typedef struct clock_ops	clock_ops_data_t;
82
83/*
84 * Actual clock object data structure. Contains the machine
85 * dependent operations list and clock operation ports.
86 */
87struct	clock {
88	clock_ops_t			cl_ops;			/* operations list */
89	struct ipc_port		*cl_service;	/* service port */
90	struct ipc_port		*cl_control;	/* control port */
91};
92typedef struct clock		clock_data_t;
93
94/*
95 * Configure the clock system.
96 */
97extern void		clock_config(void);
98extern void		clock_oldconfig(void);
99
100/*
101 * Initialize the clock system.
102 */
103extern void		clock_init(void);
104extern void		clock_oldinit(void);
105
106extern void		clock_timebase_init(void);
107
108/*
109 * Initialize the clock ipc service facility.
110 */
111extern void		clock_service_create(void);
112
113extern void			clock_gettimeofday_set_commpage(
114						uint64_t				abstime,
115						uint64_t				epoch,
116						uint64_t				offset,
117						clock_sec_t				*secs,
118						clock_usec_t			*microsecs);
119
120extern void			machine_delay_until(uint64_t interval,
121						uint64_t		deadline);
122
123extern uint32_t		hz_tick_interval;
124
125extern void		absolutetime_to_nanotime(
126					uint64_t		abstime,
127					clock_sec_t		*secs,
128					clock_nsec_t	*nanosecs);
129
130extern void		nanotime_to_absolutetime(
131					clock_sec_t		secs,
132					clock_nsec_t	nanosecs,
133					uint64_t		*result);
134
135#endif /* MACH_KERNEL_PRIVATE */
136
137__BEGIN_DECLS
138
139#ifdef	XNU_KERNEL_PRIVATE
140
141extern void			clock_adjtime(
142						long		*secs,
143						int			*microsecs);
144
145extern void			clock_initialize_calendar(void);
146
147extern void			clock_wakeup_calendar(void);
148
149extern void			clock_gettimeofday(
150						clock_sec_t			*secs,
151						clock_usec_t		*microsecs);
152
153extern void			clock_set_calendar_microtime(
154						clock_sec_t			secs,
155						clock_usec_t		microsecs);
156
157extern void			clock_get_boottime_nanotime(
158						clock_sec_t			*secs,
159						clock_nsec_t		*nanosecs);
160
161extern void			absolutetime_to_microtime(
162						uint64_t			abstime,
163						clock_sec_t			*secs,
164						clock_usec_t		*microsecs);
165
166extern void			clock_deadline_for_periodic_event(
167						uint64_t			interval,
168						uint64_t			abstime,
169						uint64_t			*deadline);
170
171#if	CONFIG_DTRACE
172
173extern void			clock_get_calendar_nanotime_nowait(
174						clock_sec_t			*secs,
175						clock_nsec_t		*nanosecs);
176
177#endif	/* CONFIG_DTRACE */
178
179#endif	/* XNU_KERNEL_PRIVATE */
180
181extern void			clock_get_calendar_microtime(
182						clock_sec_t			*secs,
183						clock_usec_t		*microsecs);
184
185extern void			clock_get_calendar_nanotime(
186						clock_sec_t			*secs,
187						clock_nsec_t		*nanosecs);
188
189extern void			clock_get_system_microtime(
190						clock_sec_t			*secs,
191						clock_usec_t		*microsecs);
192
193extern void			clock_get_system_nanotime(
194						clock_sec_t			*secs,
195						clock_nsec_t		*nanosecs);
196
197extern void				clock_timebase_info(
198							mach_timebase_info_t	info);
199
200extern void				clock_get_uptime(
201							uint64_t		*result);
202
203extern void				clock_interval_to_deadline(
204							uint32_t		interval,
205							uint32_t		scale_factor,
206							uint64_t		*result);
207
208extern void				clock_interval_to_absolutetime_interval(
209							uint32_t		interval,
210							uint32_t		scale_factor,
211							uint64_t		*result);
212
213extern void				clock_absolutetime_interval_to_deadline(
214							uint64_t		abstime,
215							uint64_t		*result);
216
217extern void				clock_delay_until(
218							uint64_t		deadline);
219
220extern void				absolutetime_to_nanoseconds(
221							uint64_t		abstime,
222							uint64_t		*result);
223
224extern void             nanoseconds_to_absolutetime(
225							uint64_t		nanoseconds,
226							uint64_t		*result);
227
228#ifdef	KERNEL_PRIVATE
229
230/*
231 * Obsolete interfaces.
232 */
233
234#ifndef	__LP64__
235
236#define MACH_TIMESPEC_SEC_MAX		(0 - 1)
237#define MACH_TIMESPEC_NSEC_MAX		(NSEC_PER_SEC - 1)
238
239#define MACH_TIMESPEC_MAX	((mach_timespec_t) {				\
240									MACH_TIMESPEC_SEC_MAX,		\
241									MACH_TIMESPEC_NSEC_MAX } )
242#define MACH_TIMESPEC_ZERO	((mach_timespec_t) { 0, 0 } )
243
244#define ADD_MACH_TIMESPEC_NSEC(t1, nsec)		\
245  do {											\
246	(t1)->tv_nsec += (clock_res_t)(nsec);		\
247	if ((clock_res_t)(nsec) > 0 &&				\
248			(t1)->tv_nsec >= NSEC_PER_SEC) {	\
249		(t1)->tv_nsec -= NSEC_PER_SEC;			\
250		(t1)->tv_sec += 1;						\
251	}											\
252	else if ((clock_res_t)(nsec) < 0 &&			\
253				 (t1)->tv_nsec < 0) {			\
254		(t1)->tv_nsec += NSEC_PER_SEC;			\
255		(t1)->tv_sec -= 1;						\
256	}											\
257  } while (0)
258
259#include <Availability.h>
260
261/* Use mach_absolute_time() */
262extern mach_timespec_t	clock_get_system_value(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_2_0, __IPHONE_NA);
263
264extern mach_timespec_t	clock_get_calendar_value(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_8, __IPHONE_2_0, __IPHONE_NA);
265
266#else	/* __LP64__ */
267
268#ifdef	XNU_KERNEL_PRIVATE
269
270#define MACH_TIMESPEC_ZERO	((mach_timespec_t) { 0, 0 } )
271
272#endif	/* XNU_KERNEL_PRIVATE */
273
274#endif	/* __LP64__ */
275
276extern void				delay_for_interval(
277							uint32_t		interval,
278							uint32_t		scale_factor);
279
280#ifndef	MACH_KERNEL_PRIVATE
281
282#ifndef	__LP64__
283
284#ifndef	ABSOLUTETIME_SCALAR_TYPE
285
286#define clock_get_uptime(a)		\
287	clock_get_uptime(__OSAbsoluteTimePtr(a))
288
289#define clock_interval_to_deadline(a, b, c)		\
290	clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
291
292#define clock_interval_to_absolutetime_interval(a, b, c)	\
293	clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
294
295#define clock_absolutetime_interval_to_deadline(a, b)	\
296	clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
297
298#define clock_deadline_for_periodic_event(a, b, c)	\
299	clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
300
301#define clock_delay_until(a)	\
302	clock_delay_until(__OSAbsoluteTime(a))
303
304#define absolutetime_to_nanoseconds(a, b)	\
305	absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
306
307#define nanoseconds_to_absolutetime(a, b)	\
308	nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
309
310#endif	/* ABSOLUTETIME_SCALAR_TYPE */
311
312#endif	/* __LP64__ */
313
314#endif	/* MACH_KERNEL_PRIVATE */
315
316#endif	/* KERNEL_PRIVATE */
317
318__END_DECLS
319
320#endif	/* _KERN_CLOCK_H_ */
321