3 * Userland implementation of gettimeofday() for 64 bits processes in a
4 * ppc64 kernel for use in the vDSO
6 * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org),
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <asm/processor.h>
16 #include <asm/ppc_asm.h>
18 #include <asm/asm-offsets.h>
19 #include <asm/unistd.h>
23 * Exact prototype of gettimeofday
25 * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
28 V_FUNCTION_BEGIN(__kernel_gettimeofday)
33 mr r11,r3 /* r11 holds tv */
34 mr r10,r4 /* r10 holds tz */
35 bl V_LOCAL_FUNC(__get_datapage) /* get data page */
36 bl V_LOCAL_FUNC(__do_get_xsec) /* get xsec from tb & kernel */
37 lis r7,15 /* r7 = 1000000 = USEC_PER_SEC */
39 rldicl r5,r4,44,20 /* r5 = sec = xsec / XSEC_PER_SEC */
40 rldicr r6,r5,20,43 /* r6 = sec * XSEC_PER_SEC */
41 std r5,TVAL64_TV_SEC(r11) /* store sec in tv */
42 subf r0,r6,r4 /* r0 = xsec = (xsec - r6) */
43 mulld r0,r0,r7 /* usec = (xsec * USEC_PER_SEC) /
47 cmpldi cr0,r10,0 /* check if tz is NULL */
48 std r0,TVAL64_TV_USEC(r11) /* store usec in tv */
50 lwz r4,CFG_TZ_MINUTEWEST(r3)/* fill tz */
51 lwz r5,CFG_TZ_DSTTIME(r3)
52 stw r4,TZONE_TZ_MINWEST(r10)
53 stw r5,TZONE_TZ_DSTTIME(r10)
55 li r3,0 /* always success */
58 V_FUNCTION_END(__kernel_gettimeofday)
62 * Exact prototype of clock_gettime()
64 * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
67 V_FUNCTION_BEGIN(__kernel_clock_gettime)
69 /* Check for supported clock IDs */
70 cmpwi cr0,r3,CLOCK_REALTIME
71 cmpwi cr1,r3,CLOCK_MONOTONIC
72 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
75 mflr r12 /* r12 saves lr */
77 mr r10,r3 /* r10 saves id */
78 mr r11,r4 /* r11 saves tp */
79 bl V_LOCAL_FUNC(__get_datapage) /* get data page */
80 beq cr1,50f /* if monotonic -> jump there */
86 bl V_LOCAL_FUNC(__do_get_xsec) /* get xsec from tb & kernel */
88 lis r7,15 /* r7 = 1000000 = USEC_PER_SEC */
90 rldicl r5,r4,44,20 /* r5 = sec = xsec / XSEC_PER_SEC */
91 rldicr r6,r5,20,43 /* r6 = sec * XSEC_PER_SEC */
92 std r5,TSPC64_TV_SEC(r11) /* store sec in tv */
93 subf r0,r6,r4 /* r0 = xsec = (xsec - r6) */
94 mulld r0,r0,r7 /* usec = (xsec * USEC_PER_SEC) /
98 mulli r0,r0,1000 /* nsec = usec * 1000 */
99 std r0,TSPC64_TV_NSEC(r11) /* store nsec in tp */
109 50: bl V_LOCAL_FUNC(__do_get_xsec) /* get xsec from tb & kernel */
111 lis r7,15 /* r7 = 1000000 = USEC_PER_SEC */
113 rldicl r5,r4,44,20 /* r5 = sec = xsec / XSEC_PER_SEC */
114 rldicr r6,r5,20,43 /* r6 = sec * XSEC_PER_SEC */
115 subf r0,r6,r4 /* r0 = xsec = (xsec - r6) */
116 mulld r0,r0,r7 /* usec = (xsec * USEC_PER_SEC) /
120 mulli r6,r6,1000 /* nsec = usec * 1000 */
122 /* now we must fixup using wall to monotonic. We need to snapshot
123 * that value and do the counter trick again. Fortunately, we still
124 * have the counter value in r8 that was returned by __do_get_xsec.
125 * At this point, r5,r6 contain our sec/nsec values.
129 lwa r4,WTOM_CLOCK_SEC(r3)
130 lwa r7,WTOM_CLOCK_NSEC(r3)
132 /* We now have our result in r4,r7. We create a fake dependency
133 * on that result and re-check the counter
138 ld r0,CFG_TB_UPDATE_COUNT(r3)
139 cmpld cr0,r0,r8 /* check if updated */
142 /* Calculate and store result. Note that this mimmics the C code,
143 * which may cause funny results if nsec goes negative... is that
148 lis r9,NSEC_PER_SEC@h
149 ori r9,r9,NSEC_PER_SEC@l
158 1: std r4,TSPC64_TV_SEC(r11)
159 std r7,TSPC64_TV_NSEC(r11)
173 li r0,__NR_clock_gettime
177 V_FUNCTION_END(__kernel_clock_gettime)
181 * Exact prototype of clock_getres()
183 * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
186 V_FUNCTION_BEGIN(__kernel_clock_getres)
188 /* Check for supported clock IDs */
189 cmpwi cr0,r3,CLOCK_REALTIME
190 cmpwi cr1,r3,CLOCK_MONOTONIC
191 cror cr0*4+eq,cr0*4+eq,cr1*4+eq
197 lis r5,CLOCK_REALTIME_RES@h
198 ori r5,r5,CLOCK_REALTIME_RES@l
199 std r3,TSPC64_TV_SEC(r4)
200 std r5,TSPC64_TV_NSEC(r4)
207 li r0,__NR_clock_getres
211 V_FUNCTION_END(__kernel_clock_getres)
215 * This is the core of gettimeofday(), it returns the xsec
216 * value in r4 and expects the datapage ptr (non clobbered)
217 * in r3. clobbers r0,r4,r5,r6,r7,r8
218 * When returning, r8 contains the counter value that can be reused
220 V_FUNCTION_BEGIN(__do_get_xsec)
222 /* check for update count & load values */
223 1: ld r8,CFG_TB_UPDATE_COUNT(r3)
224 andi. r0,r4,1 /* pending update ? loop */
226 xor r0,r4,r4 /* create dependency */
229 /* Get TB & offset it */
231 ld r9,CFG_TB_ORIG_STAMP(r3)
235 ld r5,CFG_TB_TO_XS(r3)
238 /* Add stamp since epoch */
239 ld r6,CFG_STAMP_XSEC(r3)
244 ld r0,CFG_TB_UPDATE_COUNT(r3)
245 cmpld cr0,r0,r8 /* check if updated */
249 V_FUNCTION_END(__do_get_xsec)