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 <asm/processor.h>
14 #include <asm/ppc_asm.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/unistd.h>
19 /* Offset for the low 32-bit part of a field of long type */
28 * Exact prototype of gettimeofday
30 * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
33 V_FUNCTION_BEGIN(__kernel_gettimeofday)
38 mr r10,r3 /* r10 saves tv */
39 mr r11,r4 /* r11 saves tz */
40 bl __get_datapage@local /* get data page */
41 mr r9, r3 /* datapage ptr in r9 */
42 cmplwi r10,0 /* check if tv is NULL */
44 bl __do_get_xsec@local /* get xsec from tb & kernel */
45 bne- 2f /* out of line -> do syscall */
47 /* seconds are xsec >> 20 */
50 stw r5,TVAL32_TV_SEC(r10)
52 /* get remaining xsec and convert to usec. we scale
53 * up remaining xsec by 12 bits and get the top 32 bits
54 * of the multiplication
60 stw r5,TVAL32_TV_USEC(r10)
62 3: cmplwi r11,0 /* check if tz is NULL */
64 lwz r4,CFG_TZ_MINUTEWEST(r9)/* fill tz */
65 lwz r5,CFG_TZ_DSTTIME(r9)
66 stw r4,TZONE_TZ_MINWEST(r11)
67 stw r5,TZONE_TZ_DSTTIME(r11)
78 li r0,__NR_gettimeofday
82 V_FUNCTION_END(__kernel_gettimeofday)
85 * Exact prototype of clock_gettime()
87 * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
90 V_FUNCTION_BEGIN(__kernel_clock_gettime)
92 /* Check for supported clock IDs */
93 cmpli cr0,r3,CLOCK_REALTIME
94 cmpli cr1,r3,CLOCK_MONOTONIC
95 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
98 mflr r12 /* r12 saves lr */
100 mr r11,r4 /* r11 saves tp */
101 bl __get_datapage@local /* get data page */
102 mr r9,r3 /* datapage ptr in r9 */
104 50: bl __do_get_tspec@local /* get sec/nsec from tb & kernel */
105 bne cr1,80f /* not monotonic -> all done */
111 /* now we must fixup using wall to monotonic. We need to snapshot
112 * that value and do the counter trick again. Fortunately, we still
113 * have the counter value in r8 that was returned by __do_get_xsec.
114 * At this point, r3,r4 contain our sec/nsec values, r5 and r6
115 * can be used, r7 contains NSEC_PER_SEC.
118 lwz r5,WTOM_CLOCK_SEC(r9)
119 lwz r6,WTOM_CLOCK_NSEC(r9)
121 /* We now have our offset in r5,r6. We create a fake dependency
122 * on that value and re-check the counter
127 lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
128 cmpl cr0,r8,r0 /* check if updated */
131 /* Calculate and store result. Note that this mimics the C code,
132 * which may cause funny results if nsec goes negative... is that
146 80: stw r3,TSPC32_TV_SEC(r11)
147 stw r4,TSPC32_TV_NSEC(r11)
158 li r0,__NR_clock_gettime
162 V_FUNCTION_END(__kernel_clock_gettime)
166 * Exact prototype of clock_getres()
168 * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
171 V_FUNCTION_BEGIN(__kernel_clock_getres)
173 /* Check for supported clock IDs */
174 cmpwi cr0,r3,CLOCK_REALTIME
175 cmpwi cr1,r3,CLOCK_MONOTONIC
176 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
183 lis r5,CLOCK_REALTIME_RES@h
184 ori r5,r5,CLOCK_REALTIME_RES@l
185 stw r3,TSPC32_TV_SEC(r4)
186 stw r5,TSPC32_TV_NSEC(r4)
193 li r0,__NR_clock_getres
197 V_FUNCTION_END(__kernel_clock_getres)
201 * This is the core of gettimeofday() & friends, it returns the xsec
202 * value in r3 & r4 and expects the datapage ptr (non clobbered)
203 * in r9. clobbers r0,r4,r5,r6,r7,r8.
204 * When returning, r8 contains the counter value that can be reused
205 * by the monotonic clock implementation
209 /* Check for update count & load values. We use the low
210 * order 32 bits of the update count
212 1: lwz r8,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
213 andi. r0,r8,1 /* pending update ? loop */
215 xor r0,r8,r8 /* create dependency */
218 /* Load orig stamp (offset to TB) */
219 lwz r5,CFG_TB_ORIG_STAMP(r9)
220 lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
222 /* Get a stable TB value */
229 /* Substract tb orig stamp. If the high part is non-zero, we jump to
230 * the slow path which call the syscall.
231 * If it's ok, then we have our 32 bits tb_ticks value in r7
237 /* Load scale factor & do multiplication */
238 lwz r5,CFG_TB_TO_XS(r9) /* load values */
239 lwz r6,(CFG_TB_TO_XS+4)(r9)
245 /* At this point, we have the scaled xsec value in r4 + XER:CA
246 * we load & add the stamp since epoch
248 lwz r5,CFG_STAMP_XSEC(r9)
249 lwz r6,(CFG_STAMP_XSEC+4)(r9)
253 /* We now have our result in r3,r4. We create a fake dependency
254 * on that result and re-check the counter
259 lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
260 cmpl cr0,r8,r0 /* check if updated */
263 /* Warning ! The caller expects CR:EQ to be set to indicate a
264 * successful calculation (so it won't fallback to the syscall
265 * method). We have overriden that CR bit in the counter check,
266 * but fortunately, the loop exit condition _is_ CR:EQ set, so
267 * we can exit safely here. If you change this code, be careful
268 * of that side effect.
274 * This is the core of clock_gettime(), it returns the current
275 * time in seconds and nanoseconds in r3 and r4.
276 * It expects the datapage ptr in r9 and doesn't clobber it.
277 * It clobbers r0, r5, r6, r10 and returns NSEC_PER_SEC in r7.
278 * On return, r8 contains the counter value that can be reused.
279 * This clobbers cr0 but not any other cr field.
283 /* Check for update count & load values. We use the low
284 * order 32 bits of the update count
286 1: lwz r8,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
287 andi. r0,r8,1 /* pending update ? loop */
289 xor r0,r8,r8 /* create dependency */
292 /* Load orig stamp (offset to TB) */
293 lwz r5,CFG_TB_ORIG_STAMP(r9)
294 lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
296 /* Get a stable TB value */
303 /* Subtract tb orig stamp and shift left 12 bits.
308 rlwimi. r0,r7,12,20,31
311 /* Load scale factor & do multiplication */
312 lwz r5,CFG_TB_TO_XS(r9) /* load values */
313 lwz r6,(CFG_TB_TO_XS+4)(r9)
320 beq+ 4f /* skip high part computation if 0 */
331 4: addze r4,r4 /* add in carry */
332 lis r7,NSEC_PER_SEC@h
333 ori r7,r7,NSEC_PER_SEC@l
334 mulhwu r4,r4,r7 /* convert to nanoseconds */
336 /* At this point, we have seconds & nanoseconds since the xtime
337 * stamp in r3+CA and r4. Load & add the xtime stamp.
340 lwz r5,STAMP_XTIME+TSPC64_TV_SEC+LOPART(r9)
341 lwz r6,STAMP_XTIME+TSPC64_TV_NSEC+LOPART(r9)
343 lwz r5,STAMP_XTIME+TSPC32_TV_SEC(r9)
344 lwz r6,STAMP_XTIME+TSPC32_TV_NSEC(r9)
349 /* We now have our result in r3,r4. We create a fake dependency
350 * on that result and re-check the counter
355 lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
356 cmpl cr0,r8,r0 /* check if updated */
359 /* check for nanosecond overflow and adjust if necessary */
361 bltlr /* all done if no overflow */
362 subf r4,r7,r4 /* adjust if overflow */