]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39236c6e | 2 | * Copyright (c) 2008 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
6d2010ae | 4 | * @APPLE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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 | |
6d2010ae A |
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. | |
0a7de745 | 12 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 20 | * |
6d2010ae A |
21 | * @APPLE_LICENSE_HEADER_END@ |
22 | */ | |
23 | ||
39037602 A |
24 | #include <sys/time.h> |
25 | #include <mach/mach_time.h> | |
26 | #include <machine/cpu_capabilities.h> | |
5ba3f43e A |
27 | #include <os/overflow.h> |
28 | #include <kern/arithmetic_128.h> | |
39037602 A |
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 | ||
5ba3f43e A |
35 | int |
36 | __commpage_gettimeofday(struct timeval *tp) | |
37 | { | |
38 | return __commpage_gettimeofday_internal(tp, NULL); | |
39 | } | |
39037602 | 40 | |
39037602 A |
41 | int |
42 | __commpage_gettimeofday_internal(struct timeval *tp, uint64_t *tbr_out) | |
43 | { | |
5ba3f43e | 44 | uint64_t now, over; |
0a7de745 | 45 | uint64_t delta, frac; |
5ba3f43e A |
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 | ||
c3c9b80d A |
58 | COMM_PAGE_SLOT_TYPE(new_commpage_timeofday_data_t) commpage_timeofday_datap = |
59 | COMM_PAGE_SLOT(new_commpage_timeofday_data_t, NEWTIMEOFDAY_DATA); | |
5ba3f43e A |
60 | |
61 | gtod_TimeStamp_tick_p = &commpage_timeofday_datap->TimeStamp_tick; | |
62 | gtod_TimeStamp_sec_p = &commpage_timeofday_datap->TimeStamp_sec; | |
63 | gtod_TimeStamp_frac_p = &commpage_timeofday_datap->TimeStamp_frac; | |
64 | gtod_Ticks_scale_p = &commpage_timeofday_datap->Ticks_scale; | |
65 | gtod_Ticks_per_sec_p = &commpage_timeofday_datap->Ticks_per_sec; | |
66 | ||
67 | do { | |
68 | TimeStamp_tick = *gtod_TimeStamp_tick_p; | |
5ba3f43e A |
69 | /* |
70 | * This call contains an instruction barrier which will ensure that the | |
71 | * second read of the abs time isn't speculated above the reads of the | |
72 | * other values above | |
73 | */ | |
74 | now = mach_absolute_time(); | |
d26ffc64 A |
75 | TimeStamp_sec = *gtod_TimeStamp_sec_p; |
76 | TimeStamp_frac = *gtod_TimeStamp_frac_p; | |
77 | Tick_scale = *gtod_Ticks_scale_p; | |
78 | Ticks_per_sec = *gtod_Ticks_per_sec_p; | |
79 | /* | |
80 | * This barrier prevents the reordering of the second read of gtod_TimeStamp_tick_p | |
81 | * w.r.t the values read just after mach_absolute_time is invoked. | |
82 | */ | |
0a7de745 A |
83 | #if (__ARM_ARCH__ >= 7) |
84 | __asm__ volatile ("dmb ishld" ::: "memory"); | |
d26ffc64 | 85 | #endif |
5ba3f43e A |
86 | } while (TimeStamp_tick != *gtod_TimeStamp_tick_p); |
87 | ||
0a7de745 A |
88 | if (TimeStamp_tick == 0) { |
89 | return 1; | |
90 | } | |
5ba3f43e A |
91 | |
92 | delta = now - TimeStamp_tick; | |
93 | ||
94 | /* If more than one second force a syscall */ | |
0a7de745 A |
95 | if (delta >= Ticks_per_sec) { |
96 | return 1; | |
97 | } | |
5ba3f43e | 98 | |
d9a64523 | 99 | if (TimeStamp_sec > __LONG_MAX__) { |
0a7de745 | 100 | return 1; |
d9a64523 A |
101 | } |
102 | ||
103 | tp->tv_sec = (__darwin_time_t)TimeStamp_sec; | |
5ba3f43e A |
104 | |
105 | over = multi_overflow(Tick_scale, delta); | |
0a7de745 | 106 | if (over) { |
5ba3f43e | 107 | tp->tv_sec += over; |
39037602 | 108 | } |
39037602 | 109 | |
5ba3f43e A |
110 | /* Sum scale*delta to TimeStamp_frac, if it overflows increment sec */ |
111 | frac = TimeStamp_frac; | |
112 | frac += Tick_scale * delta; | |
0a7de745 | 113 | if (TimeStamp_frac > frac) { |
5ba3f43e | 114 | tp->tv_sec++; |
0a7de745 | 115 | } |
5ba3f43e A |
116 | |
117 | /* | |
118 | * Convert frac (64 bit frac of a sec) to usec | |
119 | * usec = frac * USEC_PER_SEC / 2^64 | |
120 | */ | |
121 | tp->tv_usec = ((uint64_t)1000000 * (uint32_t)(frac >> 32)) >> 32; | |
122 | ||
123 | if (tbr_out) { | |
124 | *tbr_out = now; | |
125 | } | |
126 | ||
0a7de745 | 127 | return 0; |
5ba3f43e | 128 | } |