gettimeofday.S 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. 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. li r3,0
  93. blr
  94. /*
  95. * CLOCK_MONOTONIC
  96. */
  97. 50: bl V_LOCAL_FUNC(__do_get_xsec) /* get xsec from tb & kernel */
  98. lis r7,15 /* r7 = 1000000 = USEC_PER_SEC */
  99. ori r7,r7,16960
  100. rldicl r5,r4,44,20 /* r5 = sec = xsec / XSEC_PER_SEC */
  101. rldicr r6,r5,20,43 /* r6 = sec * XSEC_PER_SEC */
  102. subf r0,r6,r4 /* r0 = xsec = (xsec - r6) */
  103. mulld r0,r0,r7 /* usec = (xsec * USEC_PER_SEC) /
  104. * XSEC_PER_SEC
  105. */
  106. rldicl r6,r0,44,20
  107. mulli r6,r6,1000 /* nsec = usec * 1000 */
  108. /* now we must fixup using wall to monotonic. We need to snapshot
  109. * that value and do the counter trick again. Fortunately, we still
  110. * have the counter value in r8 that was returned by __do_get_xsec.
  111. * At this point, r5,r6 contain our sec/nsec values.
  112. * can be used
  113. */
  114. lwa r4,WTOM_CLOCK_SEC(r3)
  115. lwa r7,WTOM_CLOCK_NSEC(r3)
  116. /* We now have our result in r4,r7. We create a fake dependency
  117. * on that result and re-check the counter
  118. */
  119. or r9,r4,r7
  120. xor r0,r9,r9
  121. add r3,r3,r0
  122. ld r0,CFG_TB_UPDATE_COUNT(r3)
  123. cmpld cr0,r0,r8 /* check if updated */
  124. bne- 50b
  125. /* Calculate and store result. Note that this mimmics the C code,
  126. * which may cause funny results if nsec goes negative... is that
  127. * possible at all ?
  128. */
  129. add r4,r4,r5
  130. add r7,r7,r6
  131. lis r9,NSEC_PER_SEC@h
  132. ori r9,r9,NSEC_PER_SEC@l
  133. cmpl cr0,r7,r9
  134. cmpli cr1,r7,0
  135. blt 1f
  136. subf r7,r9,r7
  137. addi r4,r4,1
  138. 1: bge cr1,1f
  139. addi r4,r4,-1
  140. add r7,r7,r9
  141. 1: std r4,TSPC64_TV_SEC(r11)
  142. std r7,TSPC64_TV_NSEC(r11)
  143. mtlr r12
  144. li r3,0
  145. blr
  146. /*
  147. * syscall fallback
  148. */
  149. 98:
  150. mtlr r12
  151. mr r3,r10
  152. mr r4,r11
  153. 99:
  154. li r0,__NR_clock_gettime
  155. sc
  156. blr
  157. .cfi_endproc
  158. V_FUNCTION_END(__kernel_clock_gettime)
  159. /*
  160. * Exact prototype of clock_getres()
  161. *
  162. * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
  163. *
  164. */
  165. V_FUNCTION_BEGIN(__kernel_clock_getres)
  166. .cfi_startproc
  167. /* Check for supported clock IDs */
  168. cmpwi cr0,r3,CLOCK_REALTIME
  169. cmpwi cr1,r3,CLOCK_MONOTONIC
  170. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  171. bne cr0,99f
  172. li r3,0
  173. cmpli cr0,r4,0
  174. beqlr
  175. lis r5,CLOCK_REALTIME_RES@h
  176. ori r5,r5,CLOCK_REALTIME_RES@l
  177. std r3,TSPC64_TV_SEC(r4)
  178. std r5,TSPC64_TV_NSEC(r4)
  179. blr
  180. /*
  181. * syscall fallback
  182. */
  183. 99:
  184. li r0,__NR_clock_getres
  185. sc
  186. blr
  187. .cfi_endproc
  188. V_FUNCTION_END(__kernel_clock_getres)
  189. /*
  190. * This is the core of gettimeofday(), it returns the xsec
  191. * value in r4 and expects the datapage ptr (non clobbered)
  192. * in r3. clobbers r0,r4,r5,r6,r7,r8
  193. * When returning, r8 contains the counter value that can be reused
  194. */
  195. V_FUNCTION_BEGIN(__do_get_xsec)
  196. .cfi_startproc
  197. /* check for update count & load values */
  198. 1: ld r8,CFG_TB_UPDATE_COUNT(r3)
  199. andi. r0,r4,1 /* pending update ? loop */
  200. bne- 1b
  201. xor r0,r4,r4 /* create dependency */
  202. add r3,r3,r0
  203. /* Get TB & offset it */
  204. mftb r7
  205. ld r9,CFG_TB_ORIG_STAMP(r3)
  206. subf r7,r9,r7
  207. /* Scale result */
  208. ld r5,CFG_TB_TO_XS(r3)
  209. mulhdu r7,r7,r5
  210. /* Add stamp since epoch */
  211. ld r6,CFG_STAMP_XSEC(r3)
  212. add r4,r6,r7
  213. xor r0,r4,r4
  214. add r3,r3,r0
  215. ld r0,CFG_TB_UPDATE_COUNT(r3)
  216. cmpld cr0,r0,r8 /* check if updated */
  217. bne- 1b
  218. blr
  219. .cfi_endproc
  220. V_FUNCTION_END(__do_get_xsec)