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