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