gettimeofday.S 5.9 KB

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