gettimeofday.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 r11,r4 /* r11 saves tp */
  74. bl V_LOCAL_FUNC(__get_datapage) /* get data page */
  75. 50: bl V_LOCAL_FUNC(__do_get_tspec) /* get time from tb & kernel */
  76. bne cr1,80f /* if not monotonic, all done */
  77. /*
  78. * CLOCK_MONOTONIC
  79. */
  80. /* now we must fixup using wall to monotonic. We need to snapshot
  81. * that value and do the counter trick again. Fortunately, we still
  82. * have the counter value in r8 that was returned by __do_get_tspec.
  83. * At this point, r4,r5 contain our sec/nsec values.
  84. */
  85. lwa r6,WTOM_CLOCK_SEC(r3)
  86. lwa r9,WTOM_CLOCK_NSEC(r3)
  87. /* We now have our result in r6,r9. We create a fake dependency
  88. * on that result and re-check the counter
  89. */
  90. or r0,r6,r9
  91. xor r0,r0,r0
  92. add r3,r3,r0
  93. ld r0,CFG_TB_UPDATE_COUNT(r3)
  94. cmpld cr0,r0,r8 /* check if updated */
  95. bne- 50b
  96. /* Add wall->monotonic offset and check for overflow or underflow.
  97. */
  98. add r4,r4,r6
  99. add r5,r5,r9
  100. cmpd cr0,r5,r7
  101. cmpdi cr1,r5,0
  102. blt 1f
  103. subf r5,r7,r5
  104. addi r4,r4,1
  105. 1: bge cr1,80f
  106. addi r4,r4,-1
  107. add r5,r5,r7
  108. 80: std r4,TSPC64_TV_SEC(r11)
  109. std r5,TSPC64_TV_NSEC(r11)
  110. mtlr r12
  111. crclr cr0*4+so
  112. li r3,0
  113. blr
  114. /*
  115. * syscall fallback
  116. */
  117. 99:
  118. li r0,__NR_clock_gettime
  119. sc
  120. blr
  121. .cfi_endproc
  122. V_FUNCTION_END(__kernel_clock_gettime)
  123. /*
  124. * Exact prototype of clock_getres()
  125. *
  126. * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
  127. *
  128. */
  129. V_FUNCTION_BEGIN(__kernel_clock_getres)
  130. .cfi_startproc
  131. /* Check for supported clock IDs */
  132. cmpwi cr0,r3,CLOCK_REALTIME
  133. cmpwi cr1,r3,CLOCK_MONOTONIC
  134. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  135. bne cr0,99f
  136. li r3,0
  137. cmpli cr0,r4,0
  138. crclr cr0*4+so
  139. beqlr
  140. lis r5,CLOCK_REALTIME_RES@h
  141. ori r5,r5,CLOCK_REALTIME_RES@l
  142. std r3,TSPC64_TV_SEC(r4)
  143. std r5,TSPC64_TV_NSEC(r4)
  144. blr
  145. /*
  146. * syscall fallback
  147. */
  148. 99:
  149. li r0,__NR_clock_getres
  150. sc
  151. blr
  152. .cfi_endproc
  153. V_FUNCTION_END(__kernel_clock_getres)
  154. /*
  155. * This is the core of gettimeofday(), it returns the xsec
  156. * value in r4 and expects the datapage ptr (non clobbered)
  157. * in r3. clobbers r0,r4,r5,r6,r7,r8
  158. * When returning, r8 contains the counter value that can be reused
  159. */
  160. V_FUNCTION_BEGIN(__do_get_xsec)
  161. .cfi_startproc
  162. /* check for update count & load values */
  163. 1: ld r8,CFG_TB_UPDATE_COUNT(r3)
  164. andi. r0,r8,1 /* pending update ? loop */
  165. bne- 1b
  166. xor r0,r8,r8 /* create dependency */
  167. add r3,r3,r0
  168. /* Get TB & offset it. We use the MFTB macro which will generate
  169. * workaround code for Cell.
  170. */
  171. MFTB(r7)
  172. ld r9,CFG_TB_ORIG_STAMP(r3)
  173. subf r7,r9,r7
  174. /* Scale result */
  175. ld r5,CFG_TB_TO_XS(r3)
  176. mulhdu r7,r7,r5
  177. /* Add stamp since epoch */
  178. ld r6,CFG_STAMP_XSEC(r3)
  179. add r4,r6,r7
  180. xor r0,r4,r4
  181. add r3,r3,r0
  182. ld r0,CFG_TB_UPDATE_COUNT(r3)
  183. cmpld cr0,r0,r8 /* check if updated */
  184. bne- 1b
  185. blr
  186. .cfi_endproc
  187. V_FUNCTION_END(__do_get_xsec)
  188. /*
  189. * This is the core of clock_gettime(), it returns the current
  190. * time in seconds and nanoseconds in r4 and r5.
  191. * It expects the datapage ptr in r3 and doesn't clobber it.
  192. * It clobbers r0 and r6 and returns NSEC_PER_SEC in r7.
  193. * On return, r8 contains the counter value that can be reused.
  194. * This clobbers cr0 but not any other cr field.
  195. */
  196. V_FUNCTION_BEGIN(__do_get_tspec)
  197. .cfi_startproc
  198. /* check for update count & load values */
  199. 1: ld r8,CFG_TB_UPDATE_COUNT(r3)
  200. andi. r0,r8,1 /* pending update ? loop */
  201. bne- 1b
  202. xor r0,r8,r8 /* create dependency */
  203. add r3,r3,r0
  204. /* Get TB & offset it. We use the MFTB macro which will generate
  205. * workaround code for Cell.
  206. */
  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. sldi r7,r7,12 /* compute time since stamp_xtime */
  213. mulhdu r6,r7,r5 /* in units of 2^-32 seconds */
  214. /* Add stamp since epoch */
  215. ld r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
  216. ld r5,STAMP_XTIME+TSPC64_TV_NSEC(r3)
  217. or r0,r4,r5
  218. or r0,r0,r6
  219. xor r0,r0,r0
  220. add r3,r3,r0
  221. ld r0,CFG_TB_UPDATE_COUNT(r3)
  222. cmpld r0,r8 /* check if updated */
  223. bne- 1b /* reload if so */
  224. /* convert to seconds & nanoseconds and add to stamp */
  225. lis r7,NSEC_PER_SEC@h
  226. ori r7,r7,NSEC_PER_SEC@l
  227. mulhwu r0,r6,r7 /* compute nanoseconds and */
  228. srdi r6,r6,32 /* seconds since stamp_xtime */
  229. clrldi r0,r0,32
  230. add r5,r5,r0 /* add nanoseconds together */
  231. cmpd r5,r7 /* overflow? */
  232. add r4,r4,r6
  233. bltlr /* all done if no overflow */
  234. subf r5,r7,r5 /* if overflow, adjust */
  235. addi r4,r4,1
  236. blr
  237. .cfi_endproc
  238. V_FUNCTION_END(__do_get_tspec)