]>
Commit | Line | Data |
---|---|---|
55e303ae A |
1 | /* |
2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
55e303ae | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
55e303ae A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
55e303ae A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | ||
23 | #define ASSEMBLER | |
24 | #include <sys/appleapiopts.h> | |
25 | #include <ppc/asm.h> // EXT, LEXT | |
26 | #include <machine/cpu_capabilities.h> | |
27 | #include <machine/commpage.h> | |
28 | ||
29 | #define USEC_PER_SEC 1000000 | |
30 | ||
31 | ||
32 | /* The red zone is used to move data between GPRs and FPRs: */ | |
33 | ||
34 | #define rzTicks -8 // elapsed ticks since timestamp (double) | |
35 | #define rzSeconds -16 // seconds since timestamp (double) | |
36 | #define rzUSeconds -24 // useconds since timestamp (double) | |
37 | ||
38 | ||
39 | .text | |
40 | .align 2 | |
41 | .globl EXT(gettimeofday_32) | |
42 | .globl EXT(gettimeofday_64) | |
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 | // | |
53 | // _COMM_PAGE_TIMESTAMP = a BSD-style pair of uint_32's for seconds and microseconds | |
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 | ||
70 | gettimeofday_32: // int gettimeofday_32(timeval *tp); | |
71 | 0: | |
72 | lwz r5,_COMM_PAGE_TIMEBASE+0(0) // r5,r6 = TBR at timestamp | |
73 | lwz r6,_COMM_PAGE_TIMEBASE+4(0) | |
74 | lwz r7,_COMM_PAGE_TIMESTAMP+0(0) // r7 = timestamp seconds | |
75 | lwz r8,_COMM_PAGE_TIMESTAMP+4(0) // r8 = timestamp microseconds | |
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 r2,_COMM_PAGE_TIMESTAMP+0(0) | |
90 | lwz r9,_COMM_PAGE_TIMESTAMP+4(0) | |
91 | cmplw cr6,r5,r0 // did we read a consistent set? | |
92 | cmplw cr7,r6,r12 | |
93 | beq- 3f // timestamp is disabled so return bad status | |
94 | cmplw cr1,r2,r7 | |
95 | cmplw cr5,r9,r8 | |
96 | crand cr0_eq,cr6_eq,cr7_eq | |
97 | crand cr1_eq,cr1_eq,cr5_eq | |
98 | crand cr0_eq,cr0_eq,cr1_eq | |
99 | bne- 0b // loop until we have a consistent set of data | |
100 | ||
101 | subfc r11,r6,r11 // compute ticks since timestamp | |
102 | lwz r9,_COMM_PAGE_2_TO_52(0) // get exponent for (2**52) | |
103 | subfe r10,r5,r10 // complete 64-bit subtract | |
104 | lfd f2,_COMM_PAGE_2_TO_52(0) // f3 <- (2**52) | |
105 | srwi. r0,r10,2 // if more than 2**34 ticks have elapsed... | |
106 | stw r11,rzTicks+4(r1) // store elapsed ticks into red zone | |
107 | or r10,r10,r9 // convert long-long in (r10,r11) into double | |
108 | bne- 3f // ...call kernel to reprime timestamp | |
109 | ||
110 | stw r10,rzTicks(r1) // complete double | |
111 | lis r12,hi16(USEC_PER_SEC) | |
112 | ori r12,r12,lo16(USEC_PER_SEC) | |
113 | ||
114 | lfd f3,rzTicks(r1) // get elapsed ticks since timestamp + 2**52 | |
115 | fsub f4,f3,f2 // subtract 2**52 and normalize | |
116 | fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp | |
117 | lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6 | |
118 | fctiwz f6,f5 // convert to integer | |
119 | stfd f6,rzSeconds(r1) // store integer seconds into red zone | |
120 | stw r9,rzSeconds(r1) // prepare to reload as floating pt | |
121 | lfd f6,rzSeconds(r1) // get seconds + 2**52 | |
122 | fsub f6,f6,f2 // f6 <- integral seconds | |
123 | fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds | |
124 | fmul f6,f6,f3 // f6 <- fractional elapsed useconds | |
125 | fctiwz f6,f6 // convert useconds to integer | |
126 | stfd f6,rzUSeconds(r1) // store useconds into red zone | |
127 | ||
128 | lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp | |
129 | lwz r6,rzUSeconds+4(r1) // r6 <- useconds since timestamp | |
130 | add r7,r7,r5 // add elapsed seconds to timestamp seconds | |
131 | add r8,r8,r6 // ditto useconds | |
132 | ||
133 | cmplw r8,r12 // r8 >= USEC_PER_SEC ? | |
134 | blt 2f // no | |
135 | addi r7,r7,1 // add 1 to secs | |
136 | sub r8,r8,r12 // subtract USEC_PER_SEC from usecs | |
137 | 2: | |
138 | stw r7,0(r3) // store secs//usecs into user's timeval | |
139 | stw r8,4(r3) | |
140 | li r3,0 // return success | |
141 | blr | |
142 | 3: // too long since last timestamp or this code is disabled | |
143 | li r3,1 // return bad status so our caller will make syscall | |
144 | blr | |
145 | ||
146 | COMMPAGE_DESCRIPTOR(gettimeofday_32,_COMM_PAGE_GETTIMEOFDAY,0,k64Bit,kCommPageSYNC) | |
147 | ||
148 | ||
149 | // ********************************* | |
150 | // * G E T T I M E O F D A Y _ 6 4 * | |
151 | // ********************************* | |
152 | ||
153 | gettimeofday_64: // int gettimeofday_64(timeval *tp); | |
154 | 0: | |
155 | ld r6,_COMM_PAGE_TIMEBASE(0) // r6 = TBR at timestamp | |
156 | ld r8,_COMM_PAGE_TIMESTAMP(0) // r8 = timestamp (seconds,useconds) | |
157 | lfd f1,_COMM_PAGE_SEC_PER_TICK(0) | |
158 | mftb r10 // r10 = get current timebase | |
159 | lwsync // create a barrier if MP (patched to NOP if UP) | |
160 | ld r11,_COMM_PAGE_TIMEBASE(0) // then get data a 2nd time | |
161 | ld r12,_COMM_PAGE_TIMESTAMP(0) | |
162 | cmpdi cr1,r6,0 // is the timestamp disabled? | |
163 | cmpld cr6,r6,r11 // did we read a consistent set? | |
164 | cmpld cr7,r8,r12 | |
165 | beq-- cr1,3f // exit if timestamp disabled | |
166 | crand cr6_eq,cr7_eq,cr6_eq | |
167 | sub r11,r10,r6 // compute elapsed ticks from timestamp | |
168 | bne-- cr6,0b // loop until we have a consistent set of data | |
169 | ||
170 | srdi. r0,r11,35 // has it been more than 2**35 ticks since last timestamp? | |
171 | std r11,rzTicks(r1) // put ticks in redzone where we can "lfd" it | |
172 | bne-- 3f // timestamp too old, so reprime | |
173 | ||
174 | lfd f3,rzTicks(r1) // get elapsed ticks since timestamp (fixed pt) | |
175 | fcfid f4,f3 // float the tick count | |
176 | fmul f5,f4,f1 // f5 <- elapsed seconds since timestamp | |
177 | lfd f3,_COMM_PAGE_10_TO_6(0) // get 10**6 | |
178 | fctidz f6,f5 // convert integer seconds to fixed pt | |
179 | stfd f6,rzSeconds(r1) // save fixed pt integer seconds in red zone | |
180 | fcfid f6,f6 // float the integer seconds | |
181 | fsub f6,f5,f6 // f6 <- fractional part of elapsed seconds | |
182 | fmul f6,f6,f3 // f6 <- fractional elapsed useconds | |
183 | fctidz f6,f6 // convert useconds to fixed pt integer | |
184 | stfd f6,rzUSeconds(r1) // store useconds into red zone | |
185 | ||
186 | lis r12,hi16(USEC_PER_SEC) // r12 <- 10**6 | |
187 | srdi r7,r8,32 // extract seconds from doubleword timestamp | |
188 | lwz r5,rzSeconds+4(r1) // r5 <- seconds since timestamp | |
189 | ori r12,r12,lo16(USEC_PER_SEC) | |
190 | lwz r6,rzUSeconds+4(r1) // r6 <- useconds since timestamp | |
191 | add r7,r7,r5 // add elapsed seconds to timestamp seconds | |
192 | add r8,r8,r6 // ditto useconds | |
193 | ||
194 | cmplw r8,r12 // r8 >= USEC_PER_SEC ? | |
195 | blt 2f // no | |
196 | addi r7,r7,1 // add 1 to secs | |
197 | sub r8,r8,r12 // subtract USEC_PER_SEC from usecs | |
198 | 2: | |
199 | stw r7,0(r3) // store secs//usecs into user's timeval | |
200 | stw r8,4(r3) | |
201 | li r3,0 // return success | |
202 | blr | |
203 | 3: // too long since last timestamp or this code is disabled | |
204 | li r3,1 // return bad status so our caller will make syscall | |
205 | blr | |
206 | ||
207 | COMMPAGE_DESCRIPTOR(gettimeofday_64,_COMM_PAGE_GETTIMEOFDAY,k64Bit,0,kCommPageSYNC) | |
208 | ||
209 |