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