gettimeofday.S 5.9 KB

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