gettimeofday.S 6.0 KB

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