]> git.saurik.com Git - apple/xnu.git/blob - libsyscall/wrappers/__commpage_gettimeofday.c
0ebfc53180a7952e338de559a3fd79a84996c04f
[apple/xnu.git] / libsyscall / wrappers / __commpage_gettimeofday.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/time.h>
25 #include <mach/mach_time.h>
26 #include <machine/cpu_capabilities.h>
27 #include <os/overflow.h>
28 #include <kern/arithmetic_128.h>
29
30 int __commpage_gettimeofday(struct timeval *);
31
32 __attribute__((visibility("hidden")))
33 int __commpage_gettimeofday_internal(struct timeval *tp, uint64_t *tbr_out);
34
35 int
36 __commpage_gettimeofday(struct timeval *tp)
37 {
38 return __commpage_gettimeofday_internal(tp, NULL);
39 }
40
41 int
42 __commpage_gettimeofday_internal(struct timeval *tp, uint64_t *tbr_out)
43 {
44 uint64_t now, over;
45 uint64_t delta,frac;
46 uint64_t TimeStamp_tick;
47 uint64_t TimeStamp_sec;
48 uint64_t TimeStamp_frac;
49 uint64_t Tick_scale;
50 uint64_t Ticks_per_sec;
51
52 volatile uint64_t *gtod_TimeStamp_tick_p;
53 volatile uint64_t *gtod_TimeStamp_sec_p;
54 volatile uint64_t *gtod_TimeStamp_frac_p;
55 volatile uint64_t *gtod_Ticks_scale_p;
56 volatile uint64_t *gtod_Ticks_per_sec_p;
57
58 new_commpage_timeofday_data_t *commpage_timeofday_datap;
59
60 commpage_timeofday_datap = (new_commpage_timeofday_data_t *)_COMM_PAGE_NEWTIMEOFDAY_DATA;
61
62 gtod_TimeStamp_tick_p = &commpage_timeofday_datap->TimeStamp_tick;
63 gtod_TimeStamp_sec_p = &commpage_timeofday_datap->TimeStamp_sec;
64 gtod_TimeStamp_frac_p = &commpage_timeofday_datap->TimeStamp_frac;
65 gtod_Ticks_scale_p = &commpage_timeofday_datap->Ticks_scale;
66 gtod_Ticks_per_sec_p = &commpage_timeofday_datap->Ticks_per_sec;
67
68 do {
69 TimeStamp_tick = *gtod_TimeStamp_tick_p;
70 TimeStamp_sec = *gtod_TimeStamp_sec_p;
71 TimeStamp_frac = *gtod_TimeStamp_frac_p;
72 Tick_scale = *gtod_Ticks_scale_p;
73 Ticks_per_sec = *gtod_Ticks_per_sec_p;
74
75 /*
76 * This call contains an instruction barrier which will ensure that the
77 * second read of the abs time isn't speculated above the reads of the
78 * other values above
79 */
80 now = mach_absolute_time();
81 } while (TimeStamp_tick != *gtod_TimeStamp_tick_p);
82
83 if (TimeStamp_tick == 0)
84 return(1);
85
86 delta = now - TimeStamp_tick;
87
88 /* If more than one second force a syscall */
89 if (delta >= Ticks_per_sec)
90 return(1);
91
92 tp->tv_sec = TimeStamp_sec;
93
94 over = multi_overflow(Tick_scale, delta);
95 if(over){
96 tp->tv_sec += over;
97 }
98
99 /* Sum scale*delta to TimeStamp_frac, if it overflows increment sec */
100 frac = TimeStamp_frac;
101 frac += Tick_scale * delta;
102 if( TimeStamp_frac > frac )
103 tp->tv_sec++;
104
105 /*
106 * Convert frac (64 bit frac of a sec) to usec
107 * usec = frac * USEC_PER_SEC / 2^64
108 */
109 tp->tv_usec = ((uint64_t)1000000 * (uint32_t)(frac >> 32)) >> 32;
110
111 if (tbr_out) {
112 *tbr_out = now;
113 }
114
115 return(0);
116 }