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 */
22 #define TSPEC_TV_SEC TSPC64_TV_SEC+LOPART
25 #define TSPEC_TV_SEC TSPC32_TV_SEC
30 * Exact prototype of gettimeofday
32 * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
35 V_FUNCTION_BEGIN(__kernel_gettimeofday)
40 mr r10,r3 /* r10 saves tv */
41 mr r11,r4 /* r11 saves tz */
42 bl __get_datapage@local /* get data page */
43 mr r9, r3 /* datapage ptr in r9 */
44 cmplwi r10,0 /* check if tv is NULL */
46 lis r7,1000000@ha /* load up USEC_PER_SEC */
47 addi r7,r7,1000000@l /* so we get microseconds in r4 */
48 bl __do_get_tspec@local /* get sec/usec from tb & kernel */
49 stw r3,TVAL32_TV_SEC(r10)
50 stw r4,TVAL32_TV_USEC(r10)
52 3: cmplwi r11,0 /* check if tz is NULL */
54 lwz r4,CFG_TZ_MINUTEWEST(r9)/* fill tz */
55 lwz r5,CFG_TZ_DSTTIME(r9)
56 stw r4,TZONE_TZ_MINWEST(r11)
57 stw r5,TZONE_TZ_DSTTIME(r11)
64 V_FUNCTION_END(__kernel_gettimeofday)
67 * Exact prototype of clock_gettime()
69 * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
72 V_FUNCTION_BEGIN(__kernel_clock_gettime)
74 /* Check for supported clock IDs */
75 cmpli cr0,r3,CLOCK_REALTIME
76 cmpli cr1,r3,CLOCK_MONOTONIC
77 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
80 mflr r12 /* r12 saves lr */
82 mr r11,r4 /* r11 saves tp */
83 bl __get_datapage@local /* get data page */
84 mr r9,r3 /* datapage ptr in r9 */
85 lis r7,NSEC_PER_SEC@h /* want nanoseconds */
86 ori r7,r7,NSEC_PER_SEC@l
87 50: bl __do_get_tspec@local /* get sec/nsec from tb & kernel */
88 bne cr1,80f /* not monotonic -> all done */
94 /* now we must fixup using wall to monotonic. We need to snapshot
95 * that value and do the counter trick again. Fortunately, we still
96 * have the counter value in r8 that was returned by __do_get_xsec.
97 * At this point, r3,r4 contain our sec/nsec values, r5 and r6
98 * can be used, r7 contains NSEC_PER_SEC.
101 lwz r5,(WTOM_CLOCK_SEC+LOPART)(r9)
102 lwz r6,WTOM_CLOCK_NSEC(r9)
104 /* We now have our offset in r5,r6. We create a fake dependency
105 * on that value and re-check the counter
110 lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
111 cmpl cr0,r8,r0 /* check if updated */
114 /* Calculate and store result. Note that this mimics the C code,
115 * which may cause funny results if nsec goes negative... is that
129 80: stw r3,TSPC32_TV_SEC(r11)
130 stw r4,TSPC32_TV_NSEC(r11)
141 li r0,__NR_clock_gettime
146 V_FUNCTION_END(__kernel_clock_gettime)
150 * Exact prototype of clock_getres()
152 * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
155 V_FUNCTION_BEGIN(__kernel_clock_getres)
157 /* Check for supported clock IDs */
158 cmpwi cr0,r3,CLOCK_REALTIME
159 cmpwi cr1,r3,CLOCK_MONOTONIC
160 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
165 bl __get_datapage@local /* get data page */
166 lwz r5, CLOCK_HRTIMER_RES(r3)
172 stw r3,TSPC32_TV_SEC(r4)
173 stw r5,TSPC32_TV_NSEC(r4)
180 li r0,__NR_clock_getres
184 V_FUNCTION_END(__kernel_clock_getres)
188 * Exact prototype of time()
190 * time_t time(time *t);
193 V_FUNCTION_BEGIN(__kernel_time)
198 mr r11,r3 /* r11 holds t */
199 bl __get_datapage@local
200 mr r9, r3 /* datapage ptr in r9 */
202 lwz r3,STAMP_XTIME+TSPEC_TV_SEC(r9)
204 cmplwi r11,0 /* check if t is NULL */
206 stw r3,0(r11) /* store result at *t */
211 V_FUNCTION_END(__kernel_time)
214 * This is the core of clock_gettime() and gettimeofday(),
215 * it returns the current time in r3 (seconds) and r4.
216 * On entry, r7 gives the resolution of r4, either USEC_PER_SEC
217 * or NSEC_PER_SEC, giving r4 in microseconds or nanoseconds.
218 * It expects the datapage ptr in r9 and doesn't clobber it.
219 * It clobbers r0, r5 and r6.
220 * On return, r8 contains the counter value that can be reused.
221 * This clobbers cr0 but not any other cr field.
225 /* Check for update count & load values. We use the low
226 * order 32 bits of the update count
228 1: lwz r8,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
229 andi. r0,r8,1 /* pending update ? loop */
231 xor r0,r8,r8 /* create dependency */
234 /* Load orig stamp (offset to TB) */
235 lwz r5,CFG_TB_ORIG_STAMP(r9)
236 lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
238 /* Get a stable TB value */
245 /* Subtract tb orig stamp and shift left 12 bits.
250 rlwimi. r0,r4,12,20,31
254 * Load scale factor & do multiplication.
255 * We only use the high 32 bits of the tb_to_xs value.
256 * Even with a 1GHz timebase clock, the high 32 bits of
257 * tb_to_xs will be at least 4 million, so the error from
258 * ignoring the low 32 bits will be no more than 0.25ppm.
259 * The error will just make the clock run very very slightly
260 * slow until the next time the kernel updates the VDSO data,
261 * at which point the clock will catch up to the kernel's value,
262 * so there is no long-term error accumulation.
264 lwz r5,CFG_TB_TO_XS(r9) /* load values */
268 beq+ 4f /* skip high part computation if 0 */
274 /* At this point, we have seconds since the xtime stamp
275 * as a 32.32 fixed-point number in r3 and r4.
276 * Load & add the xtime stamp.
278 lwz r5,STAMP_XTIME+TSPEC_TV_SEC(r9)
279 lwz r6,STAMP_SEC_FRAC(r9)
283 /* We create a fake dependency on the result in r3/r4
284 * and re-check the counter
289 lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
290 cmplw cr0,r8,r0 /* check if updated */
293 mulhwu r4,r4,r7 /* convert to micro or nanoseconds */