gettimeofday.S 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. lis r7,1000000@ha /* load up USEC_PER_SEC */
  35. addi r7,r7,1000000@l
  36. bl V_LOCAL_FUNC(__do_get_tspec) /* get sec/us from tb & kernel */
  37. std r4,TVAL64_TV_SEC(r11) /* store sec in tv */
  38. std r5,TVAL64_TV_USEC(r11) /* store usec in tv */
  39. 2: cmpldi r10,0 /* check if tz is NULL */
  40. beq 1f
  41. lwz r4,CFG_TZ_MINUTEWEST(r3)/* fill tz */
  42. lwz r5,CFG_TZ_DSTTIME(r3)
  43. stw r4,TZONE_TZ_MINWEST(r10)
  44. stw r5,TZONE_TZ_DSTTIME(r10)
  45. 1: mtlr r12
  46. crclr cr0*4+so
  47. li r3,0 /* always success */
  48. blr
  49. .cfi_endproc
  50. V_FUNCTION_END(__kernel_gettimeofday)
  51. /*
  52. * Exact prototype of clock_gettime()
  53. *
  54. * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
  55. *
  56. */
  57. V_FUNCTION_BEGIN(__kernel_clock_gettime)
  58. .cfi_startproc
  59. /* Check for supported clock IDs */
  60. cmpwi cr0,r3,CLOCK_REALTIME
  61. cmpwi cr1,r3,CLOCK_MONOTONIC
  62. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  63. cmpwi cr5,r3,CLOCK_REALTIME_COARSE
  64. cmpwi cr6,r3,CLOCK_MONOTONIC_COARSE
  65. cror cr5*4+eq,cr5*4+eq,cr6*4+eq
  66. cror cr0*4+eq,cr0*4+eq,cr5*4+eq
  67. bne cr0,99f
  68. mflr r12 /* r12 saves lr */
  69. .cfi_register lr,r12
  70. mr r11,r4 /* r11 saves tp */
  71. bl V_LOCAL_FUNC(__get_datapage) /* get data page */
  72. lis r7,NSEC_PER_SEC@h /* want nanoseconds */
  73. ori r7,r7,NSEC_PER_SEC@l
  74. beq cr5,70f
  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. ld 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. b 78f
  97. /*
  98. * For coarse clocks we get data directly from the vdso data page, so
  99. * we don't need to call __do_get_tspec, but we still need to do the
  100. * counter trick.
  101. */
  102. 70: ld r8,CFG_TB_UPDATE_COUNT(r3)
  103. andi. r0,r8,1 /* pending update ? loop */
  104. bne- 70b
  105. add r3,r3,r0 /* r0 is already 0 */
  106. /*
  107. * CLOCK_REALTIME_COARSE, below values are needed for MONOTONIC_COARSE
  108. * too
  109. */
  110. ld r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
  111. ld r5,STAMP_XTIME+TSPC64_TV_NSEC(r3)
  112. bne cr6,75f
  113. /* CLOCK_MONOTONIC_COARSE */
  114. ld r6,WTOM_CLOCK_SEC(r3)
  115. lwa r9,WTOM_CLOCK_NSEC(r3)
  116. /* check if counter has updated */
  117. or r0,r6,r9
  118. 75: or r0,r0,r4
  119. or r0,r0,r5
  120. xor r0,r0,r0
  121. add r3,r3,r0
  122. ld r0,CFG_TB_UPDATE_COUNT(r3)
  123. cmpld cr0,r0,r8 /* check if updated */
  124. bne- 70b
  125. /* Counter has not updated, so continue calculating proper values for
  126. * sec and nsec if monotonic coarse, or just return with the proper
  127. * values for realtime.
  128. */
  129. bne cr6,80f
  130. /* Add wall->monotonic offset and check for overflow or underflow */
  131. 78: add r4,r4,r6
  132. add r5,r5,r9
  133. cmpd cr0,r5,r7
  134. cmpdi cr1,r5,0
  135. blt 79f
  136. subf r5,r7,r5
  137. addi r4,r4,1
  138. 79: bge cr1,80f
  139. addi r4,r4,-1
  140. add r5,r5,r7
  141. 80: std r4,TSPC64_TV_SEC(r11)
  142. std r5,TSPC64_TV_NSEC(r11)
  143. mtlr r12
  144. crclr cr0*4+so
  145. li r3,0
  146. blr
  147. /*
  148. * syscall fallback
  149. */
  150. 99:
  151. li r0,__NR_clock_gettime
  152. .cfi_restore lr
  153. sc
  154. blr
  155. .cfi_endproc
  156. V_FUNCTION_END(__kernel_clock_gettime)
  157. /*
  158. * Exact prototype of clock_getres()
  159. *
  160. * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
  161. *
  162. */
  163. V_FUNCTION_BEGIN(__kernel_clock_getres)
  164. .cfi_startproc
  165. /* Check for supported clock IDs */
  166. cmpwi cr0,r3,CLOCK_REALTIME
  167. cmpwi cr1,r3,CLOCK_MONOTONIC
  168. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  169. bne cr0,99f
  170. mflr r12
  171. .cfi_register lr,r12
  172. bl V_LOCAL_FUNC(__get_datapage)
  173. lwz r5, CLOCK_HRTIMER_RES(r3)
  174. mtlr r12
  175. li r3,0
  176. cmpldi cr0,r4,0
  177. crclr cr0*4+so
  178. beqlr
  179. std r3,TSPC64_TV_SEC(r4)
  180. std r5,TSPC64_TV_NSEC(r4)
  181. blr
  182. /*
  183. * syscall fallback
  184. */
  185. 99:
  186. li r0,__NR_clock_getres
  187. sc
  188. blr
  189. .cfi_endproc
  190. V_FUNCTION_END(__kernel_clock_getres)
  191. /*
  192. * Exact prototype of time()
  193. *
  194. * time_t time(time *t);
  195. *
  196. */
  197. V_FUNCTION_BEGIN(__kernel_time)
  198. .cfi_startproc
  199. mflr r12
  200. .cfi_register lr,r12
  201. mr r11,r3 /* r11 holds t */
  202. bl V_LOCAL_FUNC(__get_datapage)
  203. ld r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
  204. cmpldi r11,0 /* check if t is NULL */
  205. beq 2f
  206. std r4,0(r11) /* store result at *t */
  207. 2: mtlr r12
  208. crclr cr0*4+so
  209. mr r3,r4
  210. blr
  211. .cfi_endproc
  212. V_FUNCTION_END(__kernel_time)
  213. /*
  214. * This is the core of clock_gettime() and gettimeofday(),
  215. * it returns the current time in r4 (seconds) and r5.
  216. * On entry, r7 gives the resolution of r5, either USEC_PER_SEC
  217. * or NSEC_PER_SEC, giving r5 in microseconds or nanoseconds.
  218. * It expects the datapage ptr in r3 and doesn't clobber it.
  219. * It clobbers r0, r6 and r9.
  220. * On return, r8 contains the counter value that can be reused.
  221. * This clobbers cr0 but not any other cr field.
  222. */
  223. V_FUNCTION_BEGIN(__do_get_tspec)
  224. .cfi_startproc
  225. /* check for update count & load values */
  226. 1: ld r8,CFG_TB_UPDATE_COUNT(r3)
  227. andi. r0,r8,1 /* pending update ? loop */
  228. bne- 1b
  229. xor r0,r8,r8 /* create dependency */
  230. add r3,r3,r0
  231. /* Get TB & offset it. We use the MFTB macro which will generate
  232. * workaround code for Cell.
  233. */
  234. MFTB(r6)
  235. ld r9,CFG_TB_ORIG_STAMP(r3)
  236. subf r6,r9,r6
  237. /* Scale result */
  238. ld r5,CFG_TB_TO_XS(r3)
  239. sldi r6,r6,12 /* compute time since stamp_xtime */
  240. mulhdu r6,r6,r5 /* in units of 2^-32 seconds */
  241. /* Add stamp since epoch */
  242. ld r4,STAMP_XTIME+TSPC64_TV_SEC(r3)
  243. lwz r5,STAMP_SEC_FRAC(r3)
  244. or r0,r4,r5
  245. or r0,r0,r6
  246. xor r0,r0,r0
  247. add r3,r3,r0
  248. ld r0,CFG_TB_UPDATE_COUNT(r3)
  249. cmpld r0,r8 /* check if updated */
  250. bne- 1b /* reload if so */
  251. /* convert to seconds & nanoseconds and add to stamp */
  252. add r6,r6,r5 /* add on fractional seconds of xtime */
  253. mulhwu r5,r6,r7 /* compute micro or nanoseconds and */
  254. srdi r6,r6,32 /* seconds since stamp_xtime */
  255. clrldi r5,r5,32
  256. add r4,r4,r6
  257. blr
  258. .cfi_endproc
  259. V_FUNCTION_END(__do_get_tspec)