blob: 0a32a41d50b0bcfba3158514c68b0822a7b626c8 [file] [log] [blame]
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +11001/*
2 * Userland implementation of gettimeofday() for 32 bits processes in a
3 * ppc64 kernel for use in the vDSO
4 *
5 * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org,
6 * IBM Corp.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13#include <linux/config.h>
14#include <asm/processor.h>
15#include <asm/ppc_asm.h>
16#include <asm/vdso.h>
17#include <asm/asm-offsets.h>
18#include <asm/unistd.h>
19
20 .text
21/*
22 * Exact prototype of gettimeofday
23 *
24 * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
25 *
26 */
27V_FUNCTION_BEGIN(__kernel_gettimeofday)
28 .cfi_startproc
29 mflr r12
30 .cfi_register lr,r12
31
32 mr r10,r3 /* r10 saves tv */
33 mr r11,r4 /* r11 saves tz */
34 bl __get_datapage@local /* get data page */
35 mr r9, r3 /* datapage ptr in r9 */
36 bl __do_get_xsec@local /* get xsec from tb & kernel */
37 bne- 2f /* out of line -> do syscall */
38
39 /* seconds are xsec >> 20 */
40 rlwinm r5,r4,12,20,31
41 rlwimi r5,r3,12,0,19
42 stw r5,TVAL32_TV_SEC(r10)
43
44 /* get remaining xsec and convert to usec. we scale
45 * up remaining xsec by 12 bits and get the top 32 bits
46 * of the multiplication
47 */
48 rlwinm r5,r4,12,0,19
49 lis r6,1000000@h
50 ori r6,r6,1000000@l
51 mulhwu r5,r5,r6
52 stw r5,TVAL32_TV_USEC(r10)
53
54 cmpli cr0,r11,0 /* check if tz is NULL */
55 beq 1f
56 lwz r4,CFG_TZ_MINUTEWEST(r9)/* fill tz */
57 lwz r5,CFG_TZ_DSTTIME(r9)
58 stw r4,TZONE_TZ_MINWEST(r11)
59 stw r5,TZONE_TZ_DSTTIME(r11)
60
611: mtlr r12
62 li r3,0
63 blr
64
652:
66 mtlr r12
67 mr r3,r10
68 mr r4,r11
69 li r0,__NR_gettimeofday
70 sc
71 blr
72 .cfi_endproc
73V_FUNCTION_END(__kernel_gettimeofday)
74
75/*
76 * Exact prototype of clock_gettime()
77 *
78 * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
79 *
80 */
81V_FUNCTION_BEGIN(__kernel_clock_gettime)
82 .cfi_startproc
83 /* Check for supported clock IDs */
84 cmpli cr0,r3,CLOCK_REALTIME
85 cmpli cr1,r3,CLOCK_MONOTONIC
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +110086 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +110087 bne cr0,99f
88
89 mflr r12 /* r12 saves lr */
90 .cfi_register lr,r12
91 mr r10,r3 /* r10 saves id */
92 mr r11,r4 /* r11 saves tp */
93 bl __get_datapage@local /* get data page */
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +110094 mr r9,r3 /* datapage ptr in r9 */
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +110095 beq cr1,50f /* if monotonic -> jump there */
96
97 /*
98 * CLOCK_REALTIME
99 */
100
101 bl __do_get_xsec@local /* get xsec from tb & kernel */
102 bne- 98f /* out of line -> do syscall */
103
104 /* seconds are xsec >> 20 */
105 rlwinm r5,r4,12,20,31
106 rlwimi r5,r3,12,0,19
107 stw r5,TSPC32_TV_SEC(r11)
108
109 /* get remaining xsec and convert to nsec. we scale
110 * up remaining xsec by 12 bits and get the top 32 bits
111 * of the multiplication, then we multiply by 1000
112 */
113 rlwinm r5,r4,12,0,19
114 lis r6,1000000@h
115 ori r6,r6,1000000@l
116 mulhwu r5,r5,r6
117 mulli r5,r5,1000
118 stw r5,TSPC32_TV_NSEC(r11)
119 mtlr r12
120 li r3,0
121 blr
122
123 /*
124 * CLOCK_MONOTONIC
125 */
126
12750: bl __do_get_xsec@local /* get xsec from tb & kernel */
128 bne- 98f /* out of line -> do syscall */
129
130 /* seconds are xsec >> 20 */
131 rlwinm r6,r4,12,20,31
132 rlwimi r6,r3,12,0,19
133
134 /* get remaining xsec and convert to nsec. we scale
135 * up remaining xsec by 12 bits and get the top 32 bits
136 * of the multiplication, then we multiply by 1000
137 */
138 rlwinm r7,r4,12,0,19
139 lis r5,1000000@h
140 ori r5,r5,1000000@l
141 mulhwu r7,r7,r5
142 mulli r7,r7,1000
143
144 /* now we must fixup using wall to monotonic. We need to snapshot
145 * that value and do the counter trick again. Fortunately, we still
146 * have the counter value in r8 that was returned by __do_get_xsec.
147 * At this point, r6,r7 contain our sec/nsec values, r3,r4 and r5
148 * can be used
149 */
150
151 lwz r3,WTOM_CLOCK_SEC(r9)
152 lwz r4,WTOM_CLOCK_NSEC(r9)
153
154 /* We now have our result in r3,r4. We create a fake dependency
155 * on that result and re-check the counter
156 */
157 or r5,r4,r3
158 xor r0,r5,r5
159 add r9,r9,r0
160#ifdef CONFIG_PPC64
161 lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
162#else
163 lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
164#endif
165 cmpl cr0,r8,r0 /* check if updated */
166 bne- 50b
167
168 /* Calculate and store result. Note that this mimmics the C code,
169 * which may cause funny results if nsec goes negative... is that
170 * possible at all ?
171 */
172 add r3,r3,r6
173 add r4,r4,r7
174 lis r5,NSEC_PER_SEC@h
175 ori r5,r5,NSEC_PER_SEC@l
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +1100176 cmpl cr0,r4,r5
177 cmpli cr1,r4,0
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +1100178 blt 1f
179 subf r4,r5,r4
180 addi r3,r3,1
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +11001811: bge cr1,1f
182 addi r3,r3,-1
183 add r4,r4,r5
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +11001841: stw r3,TSPC32_TV_SEC(r11)
185 stw r4,TSPC32_TV_NSEC(r11)
186
187 mtlr r12
188 li r3,0
189 blr
190
191 /*
192 * syscall fallback
193 */
19498:
195 mtlr r12
196 mr r3,r10
197 mr r4,r11
19899:
199 li r0,__NR_clock_gettime
200 sc
201 blr
202 .cfi_endproc
203V_FUNCTION_END(__kernel_clock_gettime)
204
205
206/*
207 * Exact prototype of clock_getres()
208 *
209 * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
210 *
211 */
212V_FUNCTION_BEGIN(__kernel_clock_getres)
213 .cfi_startproc
214 /* Check for supported clock IDs */
215 cmpwi cr0,r3,CLOCK_REALTIME
216 cmpwi cr1,r3,CLOCK_MONOTONIC
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +1100217 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +1100218 bne cr0,99f
219
220 li r3,0
221 cmpli cr0,r4,0
222 beqlr
223 lis r5,CLOCK_REALTIME_RES@h
224 ori r5,r5,CLOCK_REALTIME_RES@l
225 stw r3,TSPC32_TV_SEC(r4)
226 stw r5,TSPC32_TV_NSEC(r4)
227 blr
228
229 /*
230 * syscall fallback
231 */
23299:
233 li r0,__NR_clock_getres
234 sc
235 blr
236 .cfi_endproc
237V_FUNCTION_END(__kernel_clock_getres)
238
239
240/*
241 * This is the core of gettimeofday() & friends, it returns the xsec
242 * value in r3 & r4 and expects the datapage ptr (non clobbered)
243 * in r9. clobbers r0,r4,r5,r6,r7,r8.
244 * When returning, r8 contains the counter value that can be reused
245 * by the monotonic clock implementation
246 */
247__do_get_xsec:
248 .cfi_startproc
249 /* Check for update count & load values. We use the low
250 * order 32 bits of the update count
251 */
252#ifdef CONFIG_PPC64
2531: lwz r8,(CFG_TB_UPDATE_COUNT+4)(r9)
254#else
2551: lwz r8,(CFG_TB_UPDATE_COUNT)(r9)
256#endif
257 andi. r0,r8,1 /* pending update ? loop */
258 bne- 1b
259 xor r0,r8,r8 /* create dependency */
260 add r9,r9,r0
261
262 /* Load orig stamp (offset to TB) */
263 lwz r5,CFG_TB_ORIG_STAMP(r9)
264 lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
265
266 /* Get a stable TB value */
2672: mftbu r3
268 mftbl r4
269 mftbu r0
270 cmpl cr0,r3,r0
271 bne- 2b
272
273 /* Substract tb orig stamp. If the high part is non-zero, we jump to
274 * the slow path which call the syscall.
275 * If it's ok, then we have our 32 bits tb_ticks value in r7
276 */
277 subfc r7,r6,r4
278 subfe. r0,r5,r3
279 bne- 3f
280
281 /* Load scale factor & do multiplication */
282 lwz r5,CFG_TB_TO_XS(r9) /* load values */
283 lwz r6,(CFG_TB_TO_XS+4)(r9)
284 mulhwu r4,r7,r5
285 mulhwu r6,r7,r6
286 mullw r0,r7,r5
287 addc r6,r6,r0
288
289 /* At this point, we have the scaled xsec value in r4 + XER:CA
290 * we load & add the stamp since epoch
291 */
292 lwz r5,CFG_STAMP_XSEC(r9)
293 lwz r6,(CFG_STAMP_XSEC+4)(r9)
294 adde r4,r4,r6
295 addze r3,r5
296
297 /* We now have our result in r3,r4. We create a fake dependency
298 * on that result and re-check the counter
299 */
300 or r6,r4,r3
301 xor r0,r6,r6
302 add r9,r9,r0
303#ifdef CONFIG_PPC64
304 lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
305#else
306 lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
307#endif
308 cmpl cr0,r8,r0 /* check if updated */
309 bne- 1b
310
311 /* Warning ! The caller expects CR:EQ to be set to indicate a
312 * successful calculation (so it won't fallback to the syscall
313 * method). We have overriden that CR bit in the counter check,
314 * but fortunately, the loop exit condition _is_ CR:EQ set, so
315 * we can exit safely here. If you change this code, be careful
316 * of that side effect.
317 */
3183: blr
319 .cfi_endproc