Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Userland implementation of gettimeofday() for 32 bits processes in a |
| 3 | * ppc64 kernel for use in the vDSO |
| 4 | * |
| 5 | * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org, |
| 6 | * IBM Corp. |
| 7 | * |
| 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. |
| 12 | */ |
| 13 | #include <linux/config.h> |
| 14 | #include <asm/processor.h> |
| 15 | #include <asm/ppc_asm.h> |
| 16 | #include <asm/vdso.h> |
| 17 | #include <asm/asm-offsets.h> |
| 18 | #include <asm/unistd.h> |
| 19 | |
| 20 | .text |
| 21 | /* |
| 22 | * Exact prototype of gettimeofday |
| 23 | * |
| 24 | * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz); |
| 25 | * |
| 26 | */ |
| 27 | V_FUNCTION_BEGIN(__kernel_gettimeofday) |
| 28 | .cfi_startproc |
| 29 | mflr r12 |
| 30 | .cfi_register lr,r12 |
| 31 | |
| 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 */ |
| 38 | |
| 39 | /* seconds are xsec >> 20 */ |
| 40 | rlwinm r5,r4,12,20,31 |
| 41 | rlwimi r5,r3,12,0,19 |
| 42 | stw r5,TVAL32_TV_SEC(r10) |
| 43 | |
| 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 |
| 47 | */ |
| 48 | rlwinm r5,r4,12,0,19 |
| 49 | lis r6,1000000@h |
| 50 | ori r6,r6,1000000@l |
| 51 | mulhwu r5,r5,r6 |
| 52 | stw r5,TVAL32_TV_USEC(r10) |
| 53 | |
| 54 | cmpli cr0,r11,0 /* check if tz is NULL */ |
| 55 | beq 1f |
| 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) |
| 60 | |
| 61 | 1: mtlr r12 |
Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 62 | crclr cr0*4+so |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 63 | li r3,0 |
| 64 | blr |
| 65 | |
| 66 | 2: |
| 67 | mtlr r12 |
| 68 | mr r3,r10 |
| 69 | mr r4,r11 |
| 70 | li r0,__NR_gettimeofday |
| 71 | sc |
| 72 | blr |
| 73 | .cfi_endproc |
| 74 | V_FUNCTION_END(__kernel_gettimeofday) |
| 75 | |
| 76 | /* |
| 77 | * Exact prototype of clock_gettime() |
| 78 | * |
| 79 | * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); |
| 80 | * |
| 81 | */ |
| 82 | V_FUNCTION_BEGIN(__kernel_clock_gettime) |
| 83 | .cfi_startproc |
| 84 | /* Check for supported clock IDs */ |
| 85 | cmpli cr0,r3,CLOCK_REALTIME |
| 86 | cmpli cr1,r3,CLOCK_MONOTONIC |
Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 87 | cror cr0*4+eq,cr0*4+eq,cr1*4+eq |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 88 | bne cr0,99f |
| 89 | |
| 90 | mflr r12 /* r12 saves lr */ |
| 91 | .cfi_register lr,r12 |
| 92 | mr r10,r3 /* r10 saves id */ |
| 93 | mr r11,r4 /* r11 saves tp */ |
| 94 | bl __get_datapage@local /* get data page */ |
Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 95 | mr r9,r3 /* datapage ptr in r9 */ |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 96 | beq cr1,50f /* if monotonic -> jump there */ |
| 97 | |
| 98 | /* |
| 99 | * CLOCK_REALTIME |
| 100 | */ |
| 101 | |
| 102 | bl __do_get_xsec@local /* get xsec from tb & kernel */ |
| 103 | bne- 98f /* out of line -> do syscall */ |
| 104 | |
| 105 | /* seconds are xsec >> 20 */ |
| 106 | rlwinm r5,r4,12,20,31 |
| 107 | rlwimi r5,r3,12,0,19 |
| 108 | stw r5,TSPC32_TV_SEC(r11) |
| 109 | |
| 110 | /* get remaining xsec and convert to nsec. we scale |
| 111 | * up remaining xsec by 12 bits and get the top 32 bits |
| 112 | * of the multiplication, then we multiply by 1000 |
| 113 | */ |
| 114 | rlwinm r5,r4,12,0,19 |
| 115 | lis r6,1000000@h |
| 116 | ori r6,r6,1000000@l |
| 117 | mulhwu r5,r5,r6 |
| 118 | mulli r5,r5,1000 |
| 119 | stw r5,TSPC32_TV_NSEC(r11) |
| 120 | mtlr r12 |
Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 121 | crclr cr0*4+so |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 122 | li r3,0 |
| 123 | blr |
| 124 | |
| 125 | /* |
| 126 | * CLOCK_MONOTONIC |
| 127 | */ |
| 128 | |
| 129 | 50: bl __do_get_xsec@local /* get xsec from tb & kernel */ |
| 130 | bne- 98f /* out of line -> do syscall */ |
| 131 | |
| 132 | /* seconds are xsec >> 20 */ |
| 133 | rlwinm r6,r4,12,20,31 |
| 134 | rlwimi r6,r3,12,0,19 |
| 135 | |
| 136 | /* get remaining xsec and convert to nsec. we scale |
| 137 | * up remaining xsec by 12 bits and get the top 32 bits |
| 138 | * of the multiplication, then we multiply by 1000 |
| 139 | */ |
| 140 | rlwinm r7,r4,12,0,19 |
| 141 | lis r5,1000000@h |
| 142 | ori r5,r5,1000000@l |
| 143 | mulhwu r7,r7,r5 |
| 144 | mulli r7,r7,1000 |
| 145 | |
| 146 | /* now we must fixup using wall to monotonic. We need to snapshot |
| 147 | * that value and do the counter trick again. Fortunately, we still |
| 148 | * have the counter value in r8 that was returned by __do_get_xsec. |
| 149 | * At this point, r6,r7 contain our sec/nsec values, r3,r4 and r5 |
| 150 | * can be used |
| 151 | */ |
| 152 | |
| 153 | lwz r3,WTOM_CLOCK_SEC(r9) |
| 154 | lwz r4,WTOM_CLOCK_NSEC(r9) |
| 155 | |
| 156 | /* We now have our result in r3,r4. We create a fake dependency |
| 157 | * on that result and re-check the counter |
| 158 | */ |
| 159 | or r5,r4,r3 |
| 160 | xor r0,r5,r5 |
| 161 | add r9,r9,r0 |
| 162 | #ifdef CONFIG_PPC64 |
| 163 | lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9) |
| 164 | #else |
| 165 | lwz r0,(CFG_TB_UPDATE_COUNT)(r9) |
| 166 | #endif |
| 167 | cmpl cr0,r8,r0 /* check if updated */ |
| 168 | bne- 50b |
| 169 | |
| 170 | /* Calculate and store result. Note that this mimmics the C code, |
| 171 | * which may cause funny results if nsec goes negative... is that |
| 172 | * possible at all ? |
| 173 | */ |
| 174 | add r3,r3,r6 |
| 175 | add r4,r4,r7 |
| 176 | lis r5,NSEC_PER_SEC@h |
| 177 | ori r5,r5,NSEC_PER_SEC@l |
Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 178 | cmpl cr0,r4,r5 |
| 179 | cmpli cr1,r4,0 |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 180 | blt 1f |
| 181 | subf r4,r5,r4 |
| 182 | addi r3,r3,1 |
Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 183 | 1: bge cr1,1f |
| 184 | addi r3,r3,-1 |
| 185 | add r4,r4,r5 |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 186 | 1: stw r3,TSPC32_TV_SEC(r11) |
| 187 | stw r4,TSPC32_TV_NSEC(r11) |
| 188 | |
| 189 | mtlr r12 |
Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 190 | crclr cr0*4+so |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 191 | li r3,0 |
| 192 | blr |
| 193 | |
| 194 | /* |
| 195 | * syscall fallback |
| 196 | */ |
| 197 | 98: |
| 198 | mtlr r12 |
| 199 | mr r3,r10 |
| 200 | mr r4,r11 |
| 201 | 99: |
| 202 | li r0,__NR_clock_gettime |
| 203 | sc |
| 204 | blr |
| 205 | .cfi_endproc |
| 206 | V_FUNCTION_END(__kernel_clock_gettime) |
| 207 | |
| 208 | |
| 209 | /* |
| 210 | * Exact prototype of clock_getres() |
| 211 | * |
| 212 | * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); |
| 213 | * |
| 214 | */ |
| 215 | V_FUNCTION_BEGIN(__kernel_clock_getres) |
| 216 | .cfi_startproc |
| 217 | /* Check for supported clock IDs */ |
| 218 | cmpwi cr0,r3,CLOCK_REALTIME |
| 219 | cmpwi cr1,r3,CLOCK_MONOTONIC |
Benjamin Herrenschmidt | 0c37ec2 | 2005-11-14 14:55:58 +1100 | [diff] [blame] | 220 | cror cr0*4+eq,cr0*4+eq,cr1*4+eq |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 221 | bne cr0,99f |
| 222 | |
| 223 | li r3,0 |
| 224 | cmpli cr0,r4,0 |
Benjamin Herrenschmidt | 5d66da3 | 2005-11-16 13:54:32 +1100 | [diff] [blame] | 225 | crclr cr0*4+so |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 226 | beqlr |
| 227 | lis r5,CLOCK_REALTIME_RES@h |
| 228 | ori r5,r5,CLOCK_REALTIME_RES@l |
| 229 | stw r3,TSPC32_TV_SEC(r4) |
| 230 | stw r5,TSPC32_TV_NSEC(r4) |
| 231 | blr |
| 232 | |
| 233 | /* |
| 234 | * syscall fallback |
| 235 | */ |
| 236 | 99: |
| 237 | li r0,__NR_clock_getres |
| 238 | sc |
| 239 | blr |
| 240 | .cfi_endproc |
| 241 | V_FUNCTION_END(__kernel_clock_getres) |
| 242 | |
| 243 | |
| 244 | /* |
| 245 | * This is the core of gettimeofday() & friends, it returns the xsec |
| 246 | * value in r3 & r4 and expects the datapage ptr (non clobbered) |
| 247 | * in r9. clobbers r0,r4,r5,r6,r7,r8. |
| 248 | * When returning, r8 contains the counter value that can be reused |
| 249 | * by the monotonic clock implementation |
| 250 | */ |
| 251 | __do_get_xsec: |
| 252 | .cfi_startproc |
| 253 | /* Check for update count & load values. We use the low |
| 254 | * order 32 bits of the update count |
| 255 | */ |
| 256 | #ifdef CONFIG_PPC64 |
| 257 | 1: lwz r8,(CFG_TB_UPDATE_COUNT+4)(r9) |
| 258 | #else |
| 259 | 1: lwz r8,(CFG_TB_UPDATE_COUNT)(r9) |
| 260 | #endif |
| 261 | andi. r0,r8,1 /* pending update ? loop */ |
| 262 | bne- 1b |
| 263 | xor r0,r8,r8 /* create dependency */ |
| 264 | add r9,r9,r0 |
| 265 | |
| 266 | /* Load orig stamp (offset to TB) */ |
| 267 | lwz r5,CFG_TB_ORIG_STAMP(r9) |
| 268 | lwz r6,(CFG_TB_ORIG_STAMP+4)(r9) |
| 269 | |
| 270 | /* Get a stable TB value */ |
| 271 | 2: mftbu r3 |
| 272 | mftbl r4 |
| 273 | mftbu r0 |
| 274 | cmpl cr0,r3,r0 |
| 275 | bne- 2b |
| 276 | |
| 277 | /* Substract tb orig stamp. If the high part is non-zero, we jump to |
| 278 | * the slow path which call the syscall. |
| 279 | * If it's ok, then we have our 32 bits tb_ticks value in r7 |
| 280 | */ |
| 281 | subfc r7,r6,r4 |
| 282 | subfe. r0,r5,r3 |
| 283 | bne- 3f |
| 284 | |
| 285 | /* Load scale factor & do multiplication */ |
| 286 | lwz r5,CFG_TB_TO_XS(r9) /* load values */ |
| 287 | lwz r6,(CFG_TB_TO_XS+4)(r9) |
| 288 | mulhwu r4,r7,r5 |
| 289 | mulhwu r6,r7,r6 |
| 290 | mullw r0,r7,r5 |
| 291 | addc r6,r6,r0 |
| 292 | |
| 293 | /* At this point, we have the scaled xsec value in r4 + XER:CA |
| 294 | * we load & add the stamp since epoch |
| 295 | */ |
| 296 | lwz r5,CFG_STAMP_XSEC(r9) |
| 297 | lwz r6,(CFG_STAMP_XSEC+4)(r9) |
| 298 | adde r4,r4,r6 |
| 299 | addze r3,r5 |
| 300 | |
| 301 | /* We now have our result in r3,r4. We create a fake dependency |
| 302 | * on that result and re-check the counter |
| 303 | */ |
| 304 | or r6,r4,r3 |
| 305 | xor r0,r6,r6 |
| 306 | add r9,r9,r0 |
| 307 | #ifdef CONFIG_PPC64 |
| 308 | lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9) |
| 309 | #else |
| 310 | lwz r0,(CFG_TB_UPDATE_COUNT)(r9) |
| 311 | #endif |
| 312 | cmpl cr0,r8,r0 /* check if updated */ |
| 313 | bne- 1b |
| 314 | |
| 315 | /* Warning ! The caller expects CR:EQ to be set to indicate a |
| 316 | * successful calculation (so it won't fallback to the syscall |
| 317 | * method). We have overriden that CR bit in the counter check, |
| 318 | * but fortunately, the loop exit condition _is_ CR:EQ set, so |
| 319 | * we can exit safely here. If you change this code, be careful |
| 320 | * of that side effect. |
| 321 | */ |
| 322 | 3: blr |
| 323 | .cfi_endproc |