2 * Copyright (c) 2008 Apple 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@
27 #include <sys/syscall.h>
29 #include <mach/clock_types.h>
30 #include <mach/mach.h>
31 #include <mach/mach_time.h>
32 #include <machine/cpu_capabilities.h>
34 int __commpage_gettimeofday(struct timeval
*);
37 #define TIME_ADD(rsecs, secs, rfrac, frac, unit) \
40 while ((rfrac) >= (unit)) { \
48 int __commpage_gettimeofday(struct timeval
*tp
) {
49 commpage_timeofday_data_t
*commpage_timeofday_datap
;
53 uint32_t TimeStamp_usec
;
54 uint32_t TimeStamp_sec
;
55 uint32_t TimeBaseTicks_per_sec
;
56 uint64_t TimeBase_magic
;
57 uint32_t TimeBase_add
;
58 uint32_t TimeBase_shift
;
62 commpage_timeofday_datap
= (commpage_timeofday_data_t
*)_COMM_PAGE_TIMEOFDAY_DATA
;
65 TimeBase
= commpage_timeofday_datap
->TimeBase
;
66 TimeStamp_sec
= commpage_timeofday_datap
->TimeStamp_sec
;
67 TimeStamp_usec
= commpage_timeofday_datap
->TimeStamp_usec
;
68 TimeBaseTicks_per_sec
= commpage_timeofday_datap
->TimeBaseTicks_per_sec
;
69 TimeBase_magic
= commpage_timeofday_datap
->TimeBase_magic
;
70 TimeBase_add
= commpage_timeofday_datap
->TimeBase_add
;
71 TimeBase_shift
= commpage_timeofday_datap
->TimeBase_shift
;
72 } while (TimeBase
!= commpage_timeofday_datap
->TimeBase
);
77 tbr
= mach_absolute_time();
81 if (t64
>= (uint64_t)TimeBaseTicks_per_sec
)
85 q
= ((uint64_t)x
* (uint32_t)(TimeBase_magic
)) >> 32;
86 tp
->tv_usec
= TimeBase_add
? (((x
- q
) >> 1) + q
) >> (TimeBase_shift
- 1) : q
>> TimeBase_shift
;
89 TIME_ADD(tp
->tv_sec
, TimeStamp_sec
, tp
->tv_usec
, TimeStamp_usec
, USEC_PER_SEC
);