| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 1 |  | 
|  | 2 | /* | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 3 | * Userland implementation of gettimeofday() for 64 bits processes in a | 
|  | 4 | * ppc64 kernel for use in the vDSO | 
|  | 5 | * | 
|  | 6 | * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org), | 
|  | 7 | *                    IBM Corp. | 
|  | 8 | * | 
|  | 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. | 
|  | 13 | */ | 
|  | 14 | #include <linux/config.h> | 
|  | 15 | #include <asm/processor.h> | 
|  | 16 | #include <asm/ppc_asm.h> | 
|  | 17 | #include <asm/vdso.h> | 
|  | 18 | #include <asm/asm-offsets.h> | 
|  | 19 | #include <asm/unistd.h> | 
|  | 20 |  | 
|  | 21 | .text | 
|  | 22 | /* | 
|  | 23 | * Exact prototype of gettimeofday | 
|  | 24 | * | 
|  | 25 | * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz); | 
|  | 26 | * | 
|  | 27 | */ | 
|  | 28 | V_FUNCTION_BEGIN(__kernel_gettimeofday) | 
|  | 29 | .cfi_startproc | 
|  | 30 | mflr	r12 | 
|  | 31 | .cfi_register lr,r12 | 
|  | 32 |  | 
|  | 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 */ | 
|  | 38 | ori     r7,r7,16960 | 
|  | 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) / | 
|  | 44 | * XSEC_PER_SEC | 
|  | 45 | */ | 
|  | 46 | rldicl  r0,r0,44,20 | 
|  | 47 | cmpldi	cr0,r10,0		/* check if tz is NULL */ | 
|  | 48 | std	r0,TVAL64_TV_USEC(r11)	/* store usec in tv */ | 
|  | 49 | beq	1f | 
|  | 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) | 
|  | 54 | 1:	mtlr	r12 | 
| Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 55 | crclr	cr0*4+so | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 56 | li	r3,0			/* always success */ | 
|  | 57 | blr | 
|  | 58 | .cfi_endproc | 
|  | 59 | V_FUNCTION_END(__kernel_gettimeofday) | 
|  | 60 |  | 
|  | 61 |  | 
|  | 62 | /* | 
|  | 63 | * Exact prototype of clock_gettime() | 
|  | 64 | * | 
|  | 65 | * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); | 
|  | 66 | * | 
|  | 67 | */ | 
|  | 68 | V_FUNCTION_BEGIN(__kernel_clock_gettime) | 
|  | 69 | .cfi_startproc | 
|  | 70 | /* Check for supported clock IDs */ | 
|  | 71 | cmpwi	cr0,r3,CLOCK_REALTIME | 
|  | 72 | cmpwi	cr1,r3,CLOCK_MONOTONIC | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 73 | cror	cr0*4+eq,cr0*4+eq,cr1*4+eq | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 74 | bne	cr0,99f | 
|  | 75 |  | 
|  | 76 | mflr	r12			/* r12 saves lr */ | 
|  | 77 | .cfi_register lr,r12 | 
|  | 78 | mr	r10,r3			/* r10 saves id */ | 
|  | 79 | mr	r11,r4			/* r11 saves tp */ | 
|  | 80 | bl	V_LOCAL_FUNC(__get_datapage)	/* get data page */ | 
|  | 81 | beq	cr1,50f			/* if monotonic -> jump there */ | 
|  | 82 |  | 
|  | 83 | /* | 
|  | 84 | * CLOCK_REALTIME | 
|  | 85 | */ | 
|  | 86 |  | 
|  | 87 | bl	V_LOCAL_FUNC(__do_get_xsec)	/* get xsec from tb & kernel */ | 
|  | 88 |  | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 89 | lis     r7,15			/* r7 = 1000000 = USEC_PER_SEC */ | 
|  | 90 | ori     r7,r7,16960 | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 91 | rldicl  r5,r4,44,20		/* r5 = sec = xsec / XSEC_PER_SEC */ | 
|  | 92 | rldicr  r6,r5,20,43		/* r6 = sec * XSEC_PER_SEC */ | 
|  | 93 | std	r5,TSPC64_TV_SEC(r11)	/* store sec in tv */ | 
|  | 94 | subf	r0,r6,r4		/* r0 = xsec = (xsec - r6) */ | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 95 | mulld   r0,r0,r7		/* usec = (xsec * USEC_PER_SEC) / | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 96 | * XSEC_PER_SEC | 
|  | 97 | */ | 
|  | 98 | rldicl  r0,r0,44,20 | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 99 | mulli	r0,r0,1000		/* nsec = usec * 1000 */ | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 100 | std	r0,TSPC64_TV_NSEC(r11)	/* store nsec in tp */ | 
|  | 101 |  | 
|  | 102 | mtlr	r12 | 
| Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 103 | crclr	cr0*4+so | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 104 | li	r3,0 | 
|  | 105 | blr | 
|  | 106 |  | 
|  | 107 | /* | 
|  | 108 | * CLOCK_MONOTONIC | 
|  | 109 | */ | 
|  | 110 |  | 
|  | 111 | 50:	bl	V_LOCAL_FUNC(__do_get_xsec)	/* get xsec from tb & kernel */ | 
|  | 112 |  | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 113 | lis     r7,15			/* r7 = 1000000 = USEC_PER_SEC */ | 
|  | 114 | ori     r7,r7,16960 | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 115 | rldicl  r5,r4,44,20		/* r5 = sec = xsec / XSEC_PER_SEC */ | 
|  | 116 | rldicr  r6,r5,20,43		/* r6 = sec * XSEC_PER_SEC */ | 
|  | 117 | subf	r0,r6,r4		/* r0 = xsec = (xsec - r6) */ | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 118 | mulld   r0,r0,r7		/* usec = (xsec * USEC_PER_SEC) / | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 119 | * XSEC_PER_SEC | 
|  | 120 | */ | 
|  | 121 | rldicl  r6,r0,44,20 | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 122 | mulli	r6,r6,1000		/* nsec = usec * 1000 */ | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 123 |  | 
|  | 124 | /* now we must fixup using wall to monotonic. We need to snapshot | 
|  | 125 | * that value and do the counter trick again. Fortunately, we still | 
|  | 126 | * have the counter value in r8 that was returned by __do_get_xsec. | 
|  | 127 | * At this point, r5,r6 contain our sec/nsec values. | 
|  | 128 | * can be used | 
|  | 129 | */ | 
|  | 130 |  | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 131 | lwa	r4,WTOM_CLOCK_SEC(r3) | 
|  | 132 | lwa	r7,WTOM_CLOCK_NSEC(r3) | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 133 |  | 
|  | 134 | /* We now have our result in r4,r7. We create a fake dependency | 
|  | 135 | * on that result and re-check the counter | 
|  | 136 | */ | 
|  | 137 | or	r9,r4,r7 | 
|  | 138 | xor	r0,r9,r9 | 
|  | 139 | add	r3,r3,r0 | 
|  | 140 | ld	r0,CFG_TB_UPDATE_COUNT(r3) | 
|  | 141 | cmpld   cr0,r0,r8		/* check if updated */ | 
|  | 142 | bne-	50b | 
|  | 143 |  | 
|  | 144 | /* Calculate and store result. Note that this mimmics the C code, | 
|  | 145 | * which may cause funny results if nsec goes negative... is that | 
|  | 146 | * possible at all ? | 
|  | 147 | */ | 
|  | 148 | add	r4,r4,r5 | 
|  | 149 | add	r7,r7,r6 | 
|  | 150 | lis	r9,NSEC_PER_SEC@h | 
|  | 151 | ori	r9,r9,NSEC_PER_SEC@l | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 152 | cmpl	cr0,r7,r9 | 
|  | 153 | cmpli	cr1,r7,0 | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 154 | blt	1f | 
|  | 155 | subf	r7,r9,r7 | 
|  | 156 | addi	r4,r4,1 | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 157 | 1:	bge	cr1,1f | 
|  | 158 | addi	r4,r4,-1 | 
|  | 159 | add	r7,r7,r9 | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 160 | 1:	std	r4,TSPC64_TV_SEC(r11) | 
|  | 161 | std	r7,TSPC64_TV_NSEC(r11) | 
|  | 162 |  | 
|  | 163 | mtlr	r12 | 
| Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 164 | crclr	cr0*4+so | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 165 | li	r3,0 | 
|  | 166 | blr | 
|  | 167 |  | 
|  | 168 | /* | 
|  | 169 | * syscall fallback | 
|  | 170 | */ | 
|  | 171 | 98: | 
|  | 172 | mtlr	r12 | 
|  | 173 | mr	r3,r10 | 
|  | 174 | mr	r4,r11 | 
|  | 175 | 99: | 
|  | 176 | li	r0,__NR_clock_gettime | 
|  | 177 | sc | 
|  | 178 | blr | 
|  | 179 | .cfi_endproc | 
|  | 180 | V_FUNCTION_END(__kernel_clock_gettime) | 
|  | 181 |  | 
|  | 182 |  | 
|  | 183 | /* | 
|  | 184 | * Exact prototype of clock_getres() | 
|  | 185 | * | 
|  | 186 | * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); | 
|  | 187 | * | 
|  | 188 | */ | 
|  | 189 | V_FUNCTION_BEGIN(__kernel_clock_getres) | 
|  | 190 | .cfi_startproc | 
|  | 191 | /* Check for supported clock IDs */ | 
|  | 192 | cmpwi	cr0,r3,CLOCK_REALTIME | 
|  | 193 | cmpwi	cr1,r3,CLOCK_MONOTONIC | 
| Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 194 | cror	cr0*4+eq,cr0*4+eq,cr1*4+eq | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 195 | bne	cr0,99f | 
|  | 196 |  | 
|  | 197 | li	r3,0 | 
|  | 198 | cmpli	cr0,r4,0 | 
| Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 199 | crclr	cr0*4+so | 
| Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 200 | beqlr | 
|  | 201 | lis	r5,CLOCK_REALTIME_RES@h | 
|  | 202 | ori	r5,r5,CLOCK_REALTIME_RES@l | 
|  | 203 | std	r3,TSPC64_TV_SEC(r4) | 
|  | 204 | std	r5,TSPC64_TV_NSEC(r4) | 
|  | 205 | blr | 
|  | 206 |  | 
|  | 207 | /* | 
|  | 208 | * syscall fallback | 
|  | 209 | */ | 
|  | 210 | 99: | 
|  | 211 | li	r0,__NR_clock_getres | 
|  | 212 | sc | 
|  | 213 | blr | 
|  | 214 | .cfi_endproc | 
|  | 215 | V_FUNCTION_END(__kernel_clock_getres) | 
|  | 216 |  | 
|  | 217 |  | 
|  | 218 | /* | 
|  | 219 | * This is the core of gettimeofday(), it returns the xsec | 
|  | 220 | * value in r4 and expects the datapage ptr (non clobbered) | 
|  | 221 | * in r3. clobbers r0,r4,r5,r6,r7,r8 | 
|  | 222 | * When returning, r8 contains the counter value that can be reused | 
|  | 223 | */ | 
|  | 224 | V_FUNCTION_BEGIN(__do_get_xsec) | 
|  | 225 | .cfi_startproc | 
|  | 226 | /* check for update count & load values */ | 
|  | 227 | 1:	ld	r8,CFG_TB_UPDATE_COUNT(r3) | 
|  | 228 | andi.	r0,r4,1			/* pending update ? loop */ | 
|  | 229 | bne-	1b | 
|  | 230 | xor	r0,r4,r4		/* create dependency */ | 
|  | 231 | add	r3,r3,r0 | 
|  | 232 |  | 
|  | 233 | /* Get TB & offset it */ | 
|  | 234 | mftb	r7 | 
|  | 235 | ld	r9,CFG_TB_ORIG_STAMP(r3) | 
|  | 236 | subf	r7,r9,r7 | 
|  | 237 |  | 
|  | 238 | /* Scale result */ | 
|  | 239 | ld	r5,CFG_TB_TO_XS(r3) | 
|  | 240 | mulhdu	r7,r7,r5 | 
|  | 241 |  | 
|  | 242 | /* Add stamp since epoch */ | 
|  | 243 | ld	r6,CFG_STAMP_XSEC(r3) | 
|  | 244 | add	r4,r6,r7 | 
|  | 245 |  | 
|  | 246 | xor	r0,r4,r4 | 
|  | 247 | add	r3,r3,r0 | 
|  | 248 | ld	r0,CFG_TB_UPDATE_COUNT(r3) | 
|  | 249 | cmpld   cr0,r0,r8		/* check if updated */ | 
|  | 250 | bne-	1b | 
|  | 251 | blr | 
|  | 252 | .cfi_endproc | 
|  | 253 | V_FUNCTION_END(__do_get_xsec) |