]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/commpage/gettimeofday.s
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / ppc / commpage / gettimeofday.s
CommitLineData
55e303ae 1/*
21362eb3 2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
55e303ae 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
55e303ae 5 *
8f6c56a5
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
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
55e303ae
A
27 */
28
29#define ASSEMBLER
30#include <sys/appleapiopts.h>
31#include <ppc/asm.h> // EXT, LEXT
32#include <machine/cpu_capabilities.h>
33#include <machine/commpage.h>
34
21362eb3
A
35#define USEC_PER_SEC 1000000
36
37
55e303ae
A
38/* The red zone is used to move data between GPRs and FPRs: */
39
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)
43
44
45 .text
46 .align 2
55e303ae
A
47
48
49// *********************************
50// * G E T T I M E O F D A Y _ 3 2 *
51// *********************************
52//
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:
56//
21362eb3 57// _COMM_PAGE_TIMESTAMP = a BSD-style pair of uint_32's for seconds and microseconds
55e303ae
A
58//
59// _COMM_PAGE_TIMEBASE = the timebase at which the timestamp was valid
60//
61// _COMM_PAGE_SEC_PER_TICK = multiply timebase ticks by this to get seconds (double)
62//
63// _COMM_PAGE_2_TO_52 = double precision constant 2**52
64//
65// _COMM_PAGE_10_TO_6 = double precision constant 10**6
66//
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.
71//
72// r3 = ptr to user's timeval structure (should not be null)
73
91447636 74gettimeofday_32: // int gettimeofday(timeval *tp);
55e303ae
A
750:
76 lwz r5,_COMM_PAGE_TIMEBASE+0(0) // r5,r6 = TBR at timestamp
77 lwz r6,_COMM_PAGE_TIMEBASE+4(0)
21362eb3
A
78 lwz r7,_COMM_PAGE_TIMESTAMP+0(0) // r7 = timestamp seconds
79 lwz r8,_COMM_PAGE_TIMESTAMP+4(0) // r8 = timestamp microseconds
55e303ae
A
80 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
811:
82 mftbu r10 // r10,r11 = current timebase
83 mftb r11
84 mftbu r12
85 cmplw r10,r12
86 bne- 1b
87 or. r0,r5,r6 // timebase 0? (ie, is timestamp invalid?)
88
89 sync // create a barrier (patched to NOP if UP)
90
91 lwz r0,_COMM_PAGE_TIMEBASE+0(0) // then load data a 2nd time
92 lwz r12,_COMM_PAGE_TIMEBASE+4(0)
21362eb3 93 lwz r2,_COMM_PAGE_TIMESTAMP+0(0)
55e303ae
A
94 lwz r9,_COMM_PAGE_TIMESTAMP+4(0)
95 cmplw cr6,r5,r0 // did we read a consistent set?
96 cmplw cr7,r6,r12
97 beq- 3f // timestamp is disabled so return bad status
21362eb3 98 cmplw cr1,r2,r7
55e303ae
A
99 cmplw cr5,r9,r8
100 crand cr0_eq,cr6_eq,cr7_eq
21362eb3
A
101 crand cr1_eq,cr1_eq,cr5_eq
102 crand cr0_eq,cr0_eq,cr1_eq
55e303ae
A
103 bne- 0b // loop until we have a consistent set of data
104
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
21362eb3 108 lfd f2,_COMM_PAGE_2_TO_52(0) // f3 <- (2**52)
55e303ae
A
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
113
114 stw r10,rzTicks(r1) // complete double
21362eb3
A
115 lis r12,hi16(USEC_PER_SEC)
116 ori r12,r12,lo16(USEC_PER_SEC)
117
55e303ae
A
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
131
132 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
21362eb3
A
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
55e303ae 136
21362eb3
A
137 cmplw r8,r12 // r8 >= USEC_PER_SEC ?
138 blt 2f // no
139 addi r7,r7,1 // add 1 to secs
140 sub r8,r8,r12 // subtract USEC_PER_SEC from usecs
1412:
142 stw r7,0(r3) // store secs//usecs into user's timeval
143 stw r8,4(r3)
55e303ae
A
144 li r3,0 // return success
145 blr
1463: // too long since last timestamp or this code is disabled
147 li r3,1 // return bad status so our caller will make syscall
148 blr
149
91447636 150 COMMPAGE_DESCRIPTOR(gettimeofday_32,_COMM_PAGE_GETTIMEOFDAY,0,k64Bit,kCommPageSYNC+kCommPage32)
55e303ae
A
151
152
91447636
A
153// ***************************************
154// * G E T T I M E O F D A Y _ G 5 _ 3 2 *
155// ***************************************
156//
157// This routine is called in 32-bit mode on 64-bit processors. A timeval is a struct of
21362eb3 158// a long seconds and int useconds, so it's size depends on mode.
55e303ae 159
91447636 160gettimeofday_g5_32: // int gettimeofday(timeval *tp);
55e303ae
A
1610:
162 ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp
21362eb3 163 ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds,useconds)
55e303ae
A
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?
171 cmpld cr7,r8,r12
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
176
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
180
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
192
21362eb3
A
193 lis r12,hi16(USEC_PER_SEC) // r12 <- 10**6
194 srdi r7,r8,32 // extract seconds from doubleword timestamp
55e303ae 195 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
21362eb3
A
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
55e303ae 200
21362eb3
A
201 cmplw r8,r12 // r8 >= USEC_PER_SEC ?
202 blt 2f // no
203 addi r7,r7,1 // add 1 to secs
204 sub r8,r8,r12 // subtract USEC_PER_SEC from usecs
2052:
206 stw r7,0(r3) // store secs//usecs into user's timeval
207 stw r8,4(r3)
55e303ae
A
208 li r3,0 // return success
209 blr
2103: // too long since last timestamp or this code is disabled
211 li r3,1 // return bad status so our caller will make syscall
212 blr
213
91447636
A
214 COMMPAGE_DESCRIPTOR(gettimeofday_g5_32,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC+kCommPage32)
215
216
217// ***************************************
218// * G E T T I M E O F D A Y _ G 5 _ 6 4 *
219// ***************************************
220//
221// This routine is called in 64-bit mode on 64-bit processors. A timeval is a struct of
21362eb3 222// a long seconds and int useconds, so it's size depends on mode.
91447636
A
223
224gettimeofday_g5_64: // int gettimeofday(timeval *tp);
2250:
226 ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp
21362eb3 227 ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds,useconds)
91447636
A
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?
235 cmpld cr7,r8,r12
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
240
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
244
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
256
21362eb3
A
257 lis r12,hi16(USEC_PER_SEC) // r12 <- 10**6
258 srdi r7,r8,32 // extract seconds from doubleword timestamp
91447636 259 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
21362eb3
A
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
91447636 264
21362eb3
A
265 cmplw r8,r12 // r8 >= USEC_PER_SEC ?
266 blt 2f // no
267 addi r7,r7,1 // add 1 to secs
268 sub r8,r8,r12 // subtract USEC_PER_SEC from usecs
2692:
270 std r7,0(r3) // store secs//usecs into user's timeval
271 stw r8,8(r3)
91447636
A
272 li r3,0 // return success
273 blr
2743: // too long since last timestamp or this code is disabled
275 li r3,1 // return bad status so our caller will make syscall
276 blr
277
278 COMMPAGE_DESCRIPTOR(gettimeofday_g5_64,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC+kCommPage64)
55e303ae
A
279
280