2 * Userland implementation of gettimeofday() for 32 bits processes in a
3 * ppc64 kernel for use in the vDSO
5 * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org,
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.
13 #include <linux/config.h>
14 #include <asm/processor.h>
15 #include <asm/ppc_asm.h>
17 #include <asm/asm-offsets.h>
18 #include <asm/unistd.h>
22 * Exact prototype of gettimeofday
24 * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
27 V_FUNCTION_BEGIN(__kernel_gettimeofday)
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 */
39 /* seconds are xsec >> 20 */
42 stw r5,TVAL32_TV_SEC(r10)
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
52 stw r5,TVAL32_TV_USEC(r10)
54 cmpli cr0,r11,0 /* check if tz is NULL */
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)
69 li r0,__NR_gettimeofday
73 V_FUNCTION_END(__kernel_gettimeofday)
76 * Exact prototype of clock_gettime()
78 * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
81 V_FUNCTION_BEGIN(__kernel_clock_gettime)
83 /* Check for supported clock IDs */
84 cmpli cr0,r3,CLOCK_REALTIME
85 cmpli cr1,r3,CLOCK_MONOTONIC
86 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
89 mflr r12 /* r12 saves lr */
91 mr r10,r3 /* r10 saves id */
92 mr r11,r4 /* r11 saves tp */
93 bl __get_datapage@local /* get data page */
94 mr r9,r3 /* datapage ptr in r9 */
95 beq cr1,50f /* if monotonic -> jump there */
101 bl __do_get_xsec@local /* get xsec from tb & kernel */
102 bne- 98f /* out of line -> do syscall */
104 /* seconds are xsec >> 20 */
105 rlwinm r5,r4,12,20,31
107 stw r5,TSPC32_TV_SEC(r11)
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
118 stw r5,TSPC32_TV_NSEC(r11)
127 50: bl __do_get_xsec@local /* get xsec from tb & kernel */
128 bne- 98f /* out of line -> do syscall */
130 /* seconds are xsec >> 20 */
131 rlwinm r6,r4,12,20,31
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
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
151 lwz r3,WTOM_CLOCK_SEC(r9)
152 lwz r4,WTOM_CLOCK_NSEC(r9)
154 /* We now have our result in r3,r4. We create a fake dependency
155 * on that result and re-check the counter
161 lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
163 lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
165 cmpl cr0,r8,r0 /* check if updated */
168 /* Calculate and store result. Note that this mimmics the C code,
169 * which may cause funny results if nsec goes negative... is that
174 lis r5,NSEC_PER_SEC@h
175 ori r5,r5,NSEC_PER_SEC@l
184 1: stw r3,TSPC32_TV_SEC(r11)
185 stw r4,TSPC32_TV_NSEC(r11)
199 li r0,__NR_clock_gettime
203 V_FUNCTION_END(__kernel_clock_gettime)
207 * Exact prototype of clock_getres()
209 * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
212 V_FUNCTION_BEGIN(__kernel_clock_getres)
214 /* Check for supported clock IDs */
215 cmpwi cr0,r3,CLOCK_REALTIME
216 cmpwi cr1,r3,CLOCK_MONOTONIC
217 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
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)
233 li r0,__NR_clock_getres
237 V_FUNCTION_END(__kernel_clock_getres)
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
249 /* Check for update count & load values. We use the low
250 * order 32 bits of the update count
253 1: lwz r8,(CFG_TB_UPDATE_COUNT+4)(r9)
255 1: lwz r8,(CFG_TB_UPDATE_COUNT)(r9)
257 andi. r0,r8,1 /* pending update ? loop */
259 xor r0,r8,r8 /* create dependency */
262 /* Load orig stamp (offset to TB) */
263 lwz r5,CFG_TB_ORIG_STAMP(r9)
264 lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
266 /* Get a stable TB value */
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
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)
289 /* At this point, we have the scaled xsec value in r4 + XER:CA
290 * we load & add the stamp since epoch
292 lwz r5,CFG_STAMP_XSEC(r9)
293 lwz r6,(CFG_STAMP_XSEC+4)(r9)
297 /* We now have our result in r3,r4. We create a fake dependency
298 * on that result and re-check the counter
304 lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
306 lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
308 cmpl cr0,r8,r0 /* check if updated */
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.