2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <sys/appleapiopts.h>
31 #include <ppc/asm.h> // EXT, LEXT
32 #include <machine/cpu_capabilities.h>
33 #include <machine/commpage.h>
35 #define USEC_PER_SEC 1000000
38 /* The red zone is used to move data between GPRs and FPRs: */
40 #define rzTicks -8 // elapsed ticks since timestamp (double)
41 #define rzSeconds -16 // seconds since timestamp (double)
42 #define rzUSeconds -24 // useconds since timestamp (double)
49 // *********************************
50 // * G E T T I M E O F D A Y _ 3 2 *
51 // *********************************
53 // This is a subroutine of gettimeofday.c that gets the seconds and microseconds
54 // in user mode, usually without having to make a system call. We do not deal with
55 // the timezone. The kernel maintains the following values in the comm page:
57 // _COMM_PAGE_TIMESTAMP = a BSD-style pair of uint_32's for seconds and microseconds
59 // _COMM_PAGE_TIMEBASE = the timebase at which the timestamp was valid
61 // _COMM_PAGE_SEC_PER_TICK = multiply timebase ticks by this to get seconds (double)
63 // _COMM_PAGE_2_TO_52 = double precision constant 2**52
65 // _COMM_PAGE_10_TO_6 = double precision constant 10**6
67 // We have to be careful to read these values atomically. The kernel updates them
68 // asynchronously to account for drift or time changes (eg, ntp.) We adopt the
69 // convention that (timebase==0) means the timestamp is invalid, in which case we
70 // return a bad status so our caller can make the system call.
72 // r3 = ptr to user's timeval structure (should not be null)
74 gettimeofday_32: // int gettimeofday(timeval *tp);
76 lwz r5,_COMM_PAGE_TIMEBASE+0(0) // r5,r6 = TBR at timestamp
77 lwz r6,_COMM_PAGE_TIMEBASE+4(0)
78 lwz r7,_COMM_PAGE_TIMESTAMP+0(0) // r7 = timestamp seconds
79 lwz r8,_COMM_PAGE_TIMESTAMP+4(0) // r8 = timestamp microseconds
80 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
82 mftbu r10 // r10,r11 = current timebase
87 or. r0,r5,r6 // timebase 0? (ie, is timestamp invalid?)
89 sync // create a barrier (patched to NOP if UP)
91 lwz r0,_COMM_PAGE_TIMEBASE+0(0) // then load data a 2nd time
92 lwz r12,_COMM_PAGE_TIMEBASE+4(0)
93 lwz r2,_COMM_PAGE_TIMESTAMP+0(0)
94 lwz r9,_COMM_PAGE_TIMESTAMP+4(0)
95 cmplw cr6,r5,r0 // did we read a consistent set?
97 beq- 3f // timestamp is disabled so return bad status
100 crand cr0_eq,cr6_eq,cr7_eq
101 crand cr1_eq,cr1_eq,cr5_eq
102 crand cr0_eq,cr0_eq,cr1_eq
103 bne- 0b // loop until we have a consistent set of data
105 subfc r11,r6,r11 // compute ticks since timestamp
106 lwz r9,_COMM_PAGE_2_TO_52(0) // get exponent for (2**52)
107 subfe r10,r5,r10 // complete 64-bit subtract
108 lfd f2,_COMM_PAGE_2_TO_52(0) // f3 <- (2**52)
109 srwi. r0,r10,2 // if more than 2**34 ticks have elapsed...
110 stw r11,rzTicks+4(r1) // store elapsed ticks into red zone
111 or r10,r10,r9 // convert long-long in (r10,r11) into double
112 bne- 3f // ...call kernel to reprime timestamp
114 stw r10,rzTicks(r1) // complete double
115 lis r12,hi16(USEC_PER_SEC)
116 ori r12,r12,lo16(USEC_PER_SEC)
118 lfd f3,rzTicks(r1) // get elapsed ticks since timestamp + 2**52
119 fsub f4,f3,f2 // subtract 2**52 and normalize
120 fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp
121 lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6
122 fctiwz f6,f5 // convert to integer
123 stfd f6,rzSeconds(r1) // store integer seconds into red zone
124 stw r9,rzSeconds(r1) // prepare to reload as floating pt
125 lfd f6,rzSeconds(r1) // get seconds + 2**52
126 fsub f6,f6,f2 // f6 <- integral seconds
127 fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds
128 fmul f6,f6,f3 // f6 <- fractional elapsed useconds
129 fctiwz f6,f6 // convert useconds to integer
130 stfd f6,rzUSeconds(r1) // store useconds into red zone
132 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
133 lwz r6,rzUSeconds+4(r1) // r6 <- useconds since timestamp
134 add r7,r7,r5 // add elapsed seconds to timestamp seconds
135 add r8,r8,r6 // ditto useconds
137 cmplw r8,r12 // r8 >= USEC_PER_SEC ?
139 addi r7,r7,1 // add 1 to secs
140 sub r8,r8,r12 // subtract USEC_PER_SEC from usecs
142 stw r7,0(r3) // store secs//usecs into user's timeval
144 li r3,0 // return success
146 3: // too long since last timestamp or this code is disabled
147 li r3,1 // return bad status so our caller will make syscall
150 COMMPAGE_DESCRIPTOR(gettimeofday_32,_COMM_PAGE_GETTIMEOFDAY,0,k64Bit,kCommPageSYNC+kCommPage32)
153 // ***************************************
154 // * G E T T I M E O F D A Y _ G 5 _ 3 2 *
155 // ***************************************
157 // This routine is called in 32-bit mode on 64-bit processors. A timeval is a struct of
158 // a long seconds and int useconds, so it's size depends on mode.
160 gettimeofday_g5_32: // int gettimeofday(timeval *tp);
162 ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp
163 ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds,useconds)
164 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
165 mftb r10 // r10 = get current timebase
166 lwsync // create a barrier if MP (patched to NOP if UP)
167 ld r11,_COMM_PAGE_TIMEBASE(0) // then get data a 2nd time
168 ld r12,_COMM_PAGE_TIMESTAMP(0)
169 cmpdi cr1,r6,0 // is the timestamp disabled?
170 cmpld cr6,r6,r11 // did we read a consistent set?
172 beq-- cr1,3f // exit if timestamp disabled
173 crand cr6_eq,cr7_eq,cr6_eq
174 sub r11,r10,r6 // compute elapsed ticks from timestamp
175 bne-- cr6,0b // loop until we have a consistent set of data
177 srdi. r0,r11,35 // has it been more than 2**35 ticks since last timestamp?
178 std r11,rzTicks(r1) // put ticks in redzone where we can "lfd" it
179 bne-- 3f // timestamp too old, so reprime
181 lfd f3,rzTicks(r1) // get elapsed ticks since timestamp (fixed pt)
182 fcfid f4,f3 // float the tick count
183 fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp
184 lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6
185 fctidz f6,f5 // convert integer seconds to fixed pt
186 stfd f6,rzSeconds(r1) // save fixed pt integer seconds in red zone
187 fcfid f6,f6 // float the integer seconds
188 fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds
189 fmul f6,f6,f3 // f6 <- fractional elapsed useconds
190 fctidz f6,f6 // convert useconds to fixed pt integer
191 stfd f6,rzUSeconds(r1) // store useconds into red zone
193 lis r12,hi16(USEC_PER_SEC) // r12 <- 10**6
194 srdi r7,r8,32 // extract seconds from doubleword timestamp
195 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
196 ori r12,r12,lo16(USEC_PER_SEC)
197 lwz r6,rzUSeconds+4(r1) // r6 <- useconds since timestamp
198 add r7,r7,r5 // add elapsed seconds to timestamp seconds
199 add r8,r8,r6 // ditto useconds
201 cmplw r8,r12 // r8 >= USEC_PER_SEC ?
203 addi r7,r7,1 // add 1 to secs
204 sub r8,r8,r12 // subtract USEC_PER_SEC from usecs
206 stw r7,0(r3) // store secs//usecs into user's timeval
208 li r3,0 // return success
210 3: // too long since last timestamp or this code is disabled
211 li r3,1 // return bad status so our caller will make syscall
214 COMMPAGE_DESCRIPTOR(gettimeofday_g5_32,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC+kCommPage32)
217 // ***************************************
218 // * G E T T I M E O F D A Y _ G 5 _ 6 4 *
219 // ***************************************
221 // This routine is called in 64-bit mode on 64-bit processors. A timeval is a struct of
222 // a long seconds and int useconds, so it's size depends on mode.
224 gettimeofday_g5_64: // int gettimeofday(timeval *tp);
226 ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp
227 ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds,useconds)
228 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
229 mftb r10 // r10 = get current timebase
230 lwsync // create a barrier if MP (patched to NOP if UP)
231 ld r11,_COMM_PAGE_TIMEBASE(0) // then get data a 2nd time
232 ld r12,_COMM_PAGE_TIMESTAMP(0)
233 cmpdi cr1,r6,0 // is the timestamp disabled?
234 cmpld cr6,r6,r11 // did we read a consistent set?
236 beq-- cr1,3f // exit if timestamp disabled
237 crand cr6_eq,cr7_eq,cr6_eq
238 sub r11,r10,r6 // compute elapsed ticks from timestamp
239 bne-- cr6,0b // loop until we have a consistent set of data
241 srdi. r0,r11,35 // has it been more than 2**35 ticks since last timestamp?
242 std r11,rzTicks(r1) // put ticks in redzone where we can "lfd" it
243 bne-- 3f // timestamp too old, so reprime
245 lfd f3,rzTicks(r1) // get elapsed ticks since timestamp (fixed pt)
246 fcfid f4,f3 // float the tick count
247 fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp
248 lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6
249 fctidz f6,f5 // convert integer seconds to fixed pt
250 stfd f6,rzSeconds(r1) // save fixed pt integer seconds in red zone
251 fcfid f6,f6 // float the integer seconds
252 fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds
253 fmul f6,f6,f3 // f6 <- fractional elapsed useconds
254 fctidz f6,f6 // convert useconds to fixed pt integer
255 stfd f6,rzUSeconds(r1) // store useconds into red zone
257 lis r12,hi16(USEC_PER_SEC) // r12 <- 10**6
258 srdi r7,r8,32 // extract seconds from doubleword timestamp
259 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
260 ori r12,r12,lo16(USEC_PER_SEC)
261 lwz r6,rzUSeconds+4(r1) // r6 <- useconds since timestamp
262 add r7,r7,r5 // add elapsed seconds to timestamp seconds
263 add r8,r8,r6 // ditto useconds
265 cmplw r8,r12 // r8 >= USEC_PER_SEC ?
267 addi r7,r7,1 // add 1 to secs
268 sub r8,r8,r12 // subtract USEC_PER_SEC from usecs
270 std r7,0(r3) // store secs//usecs into user's timeval
272 li r3,0 // return success
274 3: // too long since last timestamp or this code is disabled
275 li r3,1 // return bad status so our caller will make syscall
278 COMMPAGE_DESCRIPTOR(gettimeofday_g5_64,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC+kCommPage64)