]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/commpage/gettimeofday.s
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / ppc / commpage / gettimeofday.s
1 /*
2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
35 /* The red zone is used to move data between GPRs and FPRs: */
36
37 #define rzTicks -8 // elapsed ticks since timestamp (double)
38 #define rzSeconds -16 // seconds since timestamp (double)
39 #define rzUSeconds -24 // useconds since timestamp (double)
40
41
42 .text
43 .align 2
44
45
46 // *********************************
47 // * G E T T I M E O F D A Y _ 3 2 *
48 // *********************************
49 //
50 // This is a subroutine of gettimeofday.c that gets the seconds and microseconds
51 // in user mode, usually without having to make a system call. We do not deal with
52 // the timezone. The kernel maintains the following values in the comm page:
53 //
54 // _COMM_PAGE_TIMESTAMP = 64 bit seconds timestamp
55 //
56 // _COMM_PAGE_TIMEBASE = the timebase at which the timestamp was valid
57 //
58 // _COMM_PAGE_SEC_PER_TICK = multiply timebase ticks by this to get seconds (double)
59 //
60 // _COMM_PAGE_2_TO_52 = double precision constant 2**52
61 //
62 // _COMM_PAGE_10_TO_6 = double precision constant 10**6
63 //
64 // We have to be careful to read these values atomically. The kernel updates them
65 // asynchronously to account for drift or time changes (eg, ntp.) We adopt the
66 // convention that (timebase==0) means the timestamp is invalid, in which case we
67 // return a bad status so our caller can make the system call.
68 //
69 // r3 = ptr to user's timeval structure (should not be null)
70
71 gettimeofday_32: // int gettimeofday(timeval *tp);
72 0:
73 lwz r5,_COMM_PAGE_TIMEBASE+0(0) // r5,r6 = TBR at timestamp
74 lwz r6,_COMM_PAGE_TIMEBASE+4(0)
75 lwz r8,_COMM_PAGE_TIMESTAMP+4(0) // r8 = timestamp 32 bit seconds
76 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
77 1:
78 mftbu r10 // r10,r11 = current timebase
79 mftb r11
80 mftbu r12
81 cmplw r10,r12
82 bne- 1b
83 or. r0,r5,r6 // timebase 0? (ie, is timestamp invalid?)
84
85 sync // create a barrier (patched to NOP if UP)
86
87 lwz r0,_COMM_PAGE_TIMEBASE+0(0) // then load data a 2nd time
88 lwz r12,_COMM_PAGE_TIMEBASE+4(0)
89 lwz r9,_COMM_PAGE_TIMESTAMP+4(0)
90 cmplw cr6,r5,r0 // did we read a consistent set?
91 cmplw cr7,r6,r12
92 beq- 3f // timestamp is disabled so return bad status
93 cmplw cr5,r9,r8
94 crand cr0_eq,cr6_eq,cr7_eq
95 crand cr0_eq,cr0_eq,cr5_eq
96 bne- 0b // loop until we have a consistent set of data
97
98 subfc r11,r6,r11 // compute ticks since timestamp
99 lwz r9,_COMM_PAGE_2_TO_52(0) // get exponent for (2**52)
100 subfe r10,r5,r10 // complete 64-bit subtract
101 lfd f2,_COMM_PAGE_2_TO_52(0) // f2 <- (2**52)
102 srwi. r0,r10,2 // if more than 2**34 ticks have elapsed...
103 stw r11,rzTicks+4(r1) // store elapsed ticks into red zone
104 or r10,r10,r9 // convert long-long in (r10,r11) into double
105 bne- 3f // ...call kernel to reprime timestamp
106
107 stw r10,rzTicks(r1) // complete double
108
109 mffs f7
110 mtfsfi 7,1
111 lfd f3,rzTicks(r1) // get elapsed ticks since timestamp + 2**52
112 fsub f4,f3,f2 // subtract 2**52 and normalize
113 fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp
114 lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6
115 fctiwz f6,f5 // convert to integer
116 stfd f6,rzSeconds(r1) // store integer seconds into red zone
117 stw r9,rzSeconds(r1) // prepare to reload as floating pt
118 lfd f6,rzSeconds(r1) // get seconds + 2**52
119 fsub f6,f6,f2 // f6 <- integral seconds
120 fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds
121 fmul f6,f6,f3 // f6 <- fractional elapsed useconds
122 fctiwz f6,f6 // convert useconds to integer
123 stfd f6,rzUSeconds(r1) // store useconds into red zone
124 mtfsf 0xff,f7
125
126 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
127 lwz r7,rzUSeconds+4(r1) // r7 <- useconds since timestamp
128 add r6,r8,r5 // add elapsed seconds to timestamp seconds
129
130 stw r6,0(r3) // store secs//usecs into user's timeval
131 stw r7,4(r3)
132 li r3,0 // return success
133 blr
134 3: // too long since last timestamp or this code is disabled
135 li r3,1 // return bad status so our caller will make syscall
136 blr
137
138 COMMPAGE_DESCRIPTOR(gettimeofday_32,_COMM_PAGE_GETTIMEOFDAY,0,k64Bit,kCommPageSYNC+kCommPage32)
139
140
141 // ***************************************
142 // * G E T T I M E O F D A Y _ G 5 _ 3 2 *
143 // ***************************************
144 //
145 // This routine is called in 32-bit mode on 64-bit processors. A timeval is a struct of
146 // a long seconds and int useconds, so its size depends on mode.
147
148 gettimeofday_g5_32: // int gettimeofday(timeval *tp);
149 0:
150 ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp
151 ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds)
152 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
153 mftb r10 // r10 = get current timebase
154 lwsync // create a barrier if MP (patched to NOP if UP)
155 ld r11,_COMM_PAGE_TIMEBASE(0) // then get data a 2nd time
156 ld r12,_COMM_PAGE_TIMESTAMP(0)
157 cmpdi cr1,r6,0 // is the timestamp disabled?
158 cmpld cr6,r6,r11 // did we read a consistent set?
159 cmpld cr7,r8,r12
160 beq-- cr1,3f // exit if timestamp disabled
161 crand cr6_eq,cr7_eq,cr6_eq
162 sub r11,r10,r6 // compute elapsed ticks from timestamp
163 bne-- cr6,0b // loop until we have a consistent set of data
164
165 srdi. r0,r11,35 // has it been more than 2**35 ticks since last timestamp?
166 std r11,rzTicks(r1) // put ticks in redzone where we can "lfd" it
167 bne-- 3f // timestamp too old, so reprime
168
169 mffs f7
170 mtfsfi 7,1
171 lfd f3,rzTicks(r1) // get elapsed ticks since timestamp (fixed pt)
172 fcfid f4,f3 // float the tick count
173 fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp
174 lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6
175 fctidz f6,f5 // convert integer seconds to fixed pt
176 stfd f6,rzSeconds(r1) // save fixed pt integer seconds in red zone
177 fcfid f6,f6 // float the integer seconds
178 fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds
179 fmul f6,f6,f3 // f6 <- fractional elapsed useconds
180 fctidz f6,f6 // convert useconds to fixed pt integer
181 stfd f6,rzUSeconds(r1) // store useconds into red zone
182 mtfsf 0xff,f7
183
184 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
185 lwz r7,rzUSeconds+4(r1) // r7 <- useconds since timestamp
186 add r6,r8,r5 // add elapsed seconds to timestamp seconds
187
188 stw r6,0(r3) // store secs//usecs into user's timeval
189 stw r7,4(r3)
190 li r3,0 // return success
191 blr
192 3: // too long since last timestamp or this code is disabled
193 li r3,1 // return bad status so our caller will make syscall
194 blr
195
196 COMMPAGE_DESCRIPTOR(gettimeofday_g5_32,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC+kCommPage32)
197
198
199 // ***************************************
200 // * G E T T I M E O F D A Y _ G 5 _ 6 4 *
201 // ***************************************
202 //
203 // This routine is called in 64-bit mode on 64-bit processors. A timeval is a struct of
204 // a long seconds and int useconds, so its size depends on mode.
205
206 gettimeofday_g5_64: // int gettimeofday(timeval *tp);
207 0:
208 ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp
209 ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds)
210 lfd f1,_COMM_PAGE_SEC_PER_TICK(0)
211 mftb r10 // r10 = get current timebase
212 lwsync // create a barrier if MP (patched to NOP if UP)
213 ld r11,_COMM_PAGE_TIMEBASE(0) // then get data a 2nd time
214 ld r12,_COMM_PAGE_TIMESTAMP(0)
215 cmpdi cr1,r6,0 // is the timestamp disabled?
216 cmpld cr6,r6,r11 // did we read a consistent set?
217 cmpld cr7,r8,r12
218 beq-- cr1,3f // exit if timestamp disabled
219 crand cr6_eq,cr7_eq,cr6_eq
220 sub r11,r10,r6 // compute elapsed ticks from timestamp
221 bne-- cr6,0b // loop until we have a consistent set of data
222
223 srdi. r0,r11,35 // has it been more than 2**35 ticks since last timestamp?
224 std r11,rzTicks(r1) // put ticks in redzone where we can "lfd" it
225 bne-- 3f // timestamp too old, so reprime
226
227 mffs f7
228 mtfsfi 7,1
229 lfd f3,rzTicks(r1) // get elapsed ticks since timestamp (fixed pt)
230 fcfid f4,f3 // float the tick count
231 fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp
232 lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6
233 fctidz f6,f5 // convert integer seconds to fixed pt
234 stfd f6,rzSeconds(r1) // save fixed pt integer seconds in red zone
235 fcfid f6,f6 // float the integer seconds
236 fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds
237 fmul f6,f6,f3 // f6 <- fractional elapsed useconds
238 fctidz f6,f6 // convert useconds to fixed pt integer
239 stfd f6,rzUSeconds(r1) // store useconds into red zone
240 mtfsf 0xff,f7
241
242 lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp
243 lwz r7,rzUSeconds+4(r1) // r7 <- useconds since timestamp
244 add r6,r8,r5 // add elapsed seconds to timestamp seconds
245
246 std r6,0(r3) // store secs//usecs into user's timeval
247 stw r7,8(r3)
248 li r3,0 // return success
249 blr
250 3: // too long since last timestamp or this code is disabled
251 li r3,1 // return bad status so our caller will make syscall
252 blr
253
254 COMMPAGE_DESCRIPTOR(gettimeofday_g5_64,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC+kCommPage64)
255
256