gettimeofday.S 5.7 KB

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