gettimeofday.S 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Userland implementation of gettimeofday() for 32 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 r10,r3 /* r10 saves tv */
  31. mr r11,r4 /* r11 saves tz */
  32. bl __get_datapage@local /* get data page */
  33. mr r9, r3 /* datapage ptr in r9 */
  34. bl __do_get_xsec@local /* get xsec from tb & kernel */
  35. bne- 2f /* out of line -> do syscall */
  36. /* seconds are xsec >> 20 */
  37. rlwinm r5,r4,12,20,31
  38. rlwimi r5,r3,12,0,19
  39. stw r5,TVAL32_TV_SEC(r10)
  40. /* get remaining xsec and convert to usec. we scale
  41. * up remaining xsec by 12 bits and get the top 32 bits
  42. * of the multiplication
  43. */
  44. rlwinm r5,r4,12,0,19
  45. lis r6,1000000@h
  46. ori r6,r6,1000000@l
  47. mulhwu r5,r5,r6
  48. stw r5,TVAL32_TV_USEC(r10)
  49. cmpli cr0,r11,0 /* check if tz is NULL */
  50. beq 1f
  51. lwz r4,CFG_TZ_MINUTEWEST(r9)/* fill tz */
  52. lwz r5,CFG_TZ_DSTTIME(r9)
  53. stw r4,TZONE_TZ_MINWEST(r11)
  54. stw r5,TZONE_TZ_DSTTIME(r11)
  55. 1: mtlr r12
  56. li r3,0
  57. blr
  58. 2:
  59. mtlr r12
  60. mr r3,r10
  61. mr r4,r11
  62. li r0,__NR_gettimeofday
  63. sc
  64. blr
  65. .cfi_endproc
  66. V_FUNCTION_END(__kernel_gettimeofday)
  67. /*
  68. * Exact prototype of clock_gettime()
  69. *
  70. * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
  71. *
  72. */
  73. V_FUNCTION_BEGIN(__kernel_clock_gettime)
  74. .cfi_startproc
  75. /* Check for supported clock IDs */
  76. cmpli cr0,r3,CLOCK_REALTIME
  77. cmpli cr1,r3,CLOCK_MONOTONIC
  78. cror cr0,cr0,cr1
  79. bne cr0,99f
  80. mflr r12 /* r12 saves lr */
  81. .cfi_register lr,r12
  82. mr r10,r3 /* r10 saves id */
  83. mr r11,r4 /* r11 saves tp */
  84. bl __get_datapage@local /* get data page */
  85. mr r9, r3 /* datapage ptr in r9 */
  86. beq cr1,50f /* if monotonic -> jump there */
  87. /*
  88. * CLOCK_REALTIME
  89. */
  90. bl __do_get_xsec@local /* get xsec from tb & kernel */
  91. bne- 98f /* out of line -> do syscall */
  92. /* seconds are xsec >> 20 */
  93. rlwinm r5,r4,12,20,31
  94. rlwimi r5,r3,12,0,19
  95. stw r5,TSPC32_TV_SEC(r11)
  96. /* get remaining xsec and convert to nsec. we scale
  97. * up remaining xsec by 12 bits and get the top 32 bits
  98. * of the multiplication, then we multiply by 1000
  99. */
  100. rlwinm r5,r4,12,0,19
  101. lis r6,1000000@h
  102. ori r6,r6,1000000@l
  103. mulhwu r5,r5,r6
  104. mulli r5,r5,1000
  105. stw r5,TSPC32_TV_NSEC(r11)
  106. mtlr r12
  107. li r3,0
  108. blr
  109. /*
  110. * CLOCK_MONOTONIC
  111. */
  112. 50: bl __do_get_xsec@local /* get xsec from tb & kernel */
  113. bne- 98f /* out of line -> do syscall */
  114. /* seconds are xsec >> 20 */
  115. rlwinm r6,r4,12,20,31
  116. rlwimi r6,r3,12,0,19
  117. /* get remaining xsec and convert to nsec. we scale
  118. * up remaining xsec by 12 bits and get the top 32 bits
  119. * of the multiplication, then we multiply by 1000
  120. */
  121. rlwinm r7,r4,12,0,19
  122. lis r5,1000000@h
  123. ori r5,r5,1000000@l
  124. mulhwu r7,r7,r5
  125. mulli r7,r7,1000
  126. /* now we must fixup using wall to monotonic. We need to snapshot
  127. * that value and do the counter trick again. Fortunately, we still
  128. * have the counter value in r8 that was returned by __do_get_xsec.
  129. * At this point, r6,r7 contain our sec/nsec values, r3,r4 and r5
  130. * can be used
  131. */
  132. lwz r3,WTOM_CLOCK_SEC(r9)
  133. lwz r4,WTOM_CLOCK_NSEC(r9)
  134. /* We now have our result in r3,r4. We create a fake dependency
  135. * on that result and re-check the counter
  136. */
  137. or r5,r4,r3
  138. xor r0,r5,r5
  139. add r9,r9,r0
  140. #ifdef CONFIG_PPC64
  141. lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
  142. #else
  143. lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
  144. #endif
  145. cmpl cr0,r8,r0 /* check if updated */
  146. bne- 50b
  147. /* Calculate and store result. Note that this mimmics the C code,
  148. * which may cause funny results if nsec goes negative... is that
  149. * possible at all ?
  150. */
  151. add r3,r3,r6
  152. add r4,r4,r7
  153. lis r5,NSEC_PER_SEC@h
  154. ori r5,r5,NSEC_PER_SEC@l
  155. cmpli cr0,r4,r5
  156. blt 1f
  157. subf r4,r5,r4
  158. addi r3,r3,1
  159. 1: stw r3,TSPC32_TV_SEC(r11)
  160. stw r4,TSPC32_TV_NSEC(r11)
  161. mtlr r12
  162. li r3,0
  163. blr
  164. /*
  165. * syscall fallback
  166. */
  167. 98:
  168. mtlr r12
  169. mr r3,r10
  170. mr r4,r11
  171. 99:
  172. li r0,__NR_clock_gettime
  173. sc
  174. blr
  175. .cfi_endproc
  176. V_FUNCTION_END(__kernel_clock_gettime)
  177. /*
  178. * Exact prototype of clock_getres()
  179. *
  180. * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
  181. *
  182. */
  183. V_FUNCTION_BEGIN(__kernel_clock_getres)
  184. .cfi_startproc
  185. /* Check for supported clock IDs */
  186. cmpwi cr0,r3,CLOCK_REALTIME
  187. cmpwi cr1,r3,CLOCK_MONOTONIC
  188. cror cr0,cr0,cr1
  189. bne cr0,99f
  190. li r3,0
  191. cmpli cr0,r4,0
  192. beqlr
  193. lis r5,CLOCK_REALTIME_RES@h
  194. ori r5,r5,CLOCK_REALTIME_RES@l
  195. stw r3,TSPC32_TV_SEC(r4)
  196. stw r5,TSPC32_TV_NSEC(r4)
  197. blr
  198. /*
  199. * syscall fallback
  200. */
  201. 99:
  202. li r0,__NR_clock_getres
  203. sc
  204. blr
  205. .cfi_endproc
  206. V_FUNCTION_END(__kernel_clock_getres)
  207. /*
  208. * This is the core of gettimeofday() & friends, it returns the xsec
  209. * value in r3 & r4 and expects the datapage ptr (non clobbered)
  210. * in r9. clobbers r0,r4,r5,r6,r7,r8.
  211. * When returning, r8 contains the counter value that can be reused
  212. * by the monotonic clock implementation
  213. */
  214. __do_get_xsec:
  215. .cfi_startproc
  216. /* Check for update count & load values. We use the low
  217. * order 32 bits of the update count
  218. */
  219. #ifdef CONFIG_PPC64
  220. 1: lwz r8,(CFG_TB_UPDATE_COUNT+4)(r9)
  221. #else
  222. 1: lwz r8,(CFG_TB_UPDATE_COUNT)(r9)
  223. #endif
  224. andi. r0,r8,1 /* pending update ? loop */
  225. bne- 1b
  226. xor r0,r8,r8 /* create dependency */
  227. add r9,r9,r0
  228. /* Load orig stamp (offset to TB) */
  229. lwz r5,CFG_TB_ORIG_STAMP(r9)
  230. lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
  231. /* Get a stable TB value */
  232. 2: mftbu r3
  233. mftbl r4
  234. mftbu r0
  235. cmpl cr0,r3,r0
  236. bne- 2b
  237. /* Substract tb orig stamp. If the high part is non-zero, we jump to
  238. * the slow path which call the syscall.
  239. * If it's ok, then we have our 32 bits tb_ticks value in r7
  240. */
  241. subfc r7,r6,r4
  242. subfe. r0,r5,r3
  243. bne- 3f
  244. /* Load scale factor & do multiplication */
  245. lwz r5,CFG_TB_TO_XS(r9) /* load values */
  246. lwz r6,(CFG_TB_TO_XS+4)(r9)
  247. mulhwu r4,r7,r5
  248. mulhwu r6,r7,r6
  249. mullw r0,r7,r5
  250. addc r6,r6,r0
  251. /* At this point, we have the scaled xsec value in r4 + XER:CA
  252. * we load & add the stamp since epoch
  253. */
  254. lwz r5,CFG_STAMP_XSEC(r9)
  255. lwz r6,(CFG_STAMP_XSEC+4)(r9)
  256. adde r4,r4,r6
  257. addze r3,r5
  258. /* We now have our result in r3,r4. We create a fake dependency
  259. * on that result and re-check the counter
  260. */
  261. or r6,r4,r3
  262. xor r0,r6,r6
  263. add r9,r9,r0
  264. #ifdef CONFIG_PPC64
  265. lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
  266. #else
  267. lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
  268. #endif
  269. cmpl cr0,r8,r0 /* check if updated */
  270. bne- 1b
  271. /* Warning ! The caller expects CR:EQ to be set to indicate a
  272. * successful calculation (so it won't fallback to the syscall
  273. * method). We have overriden that CR bit in the counter check,
  274. * but fortunately, the loop exit condition _is_ CR:EQ set, so
  275. * we can exit safely here. If you change this code, be careful
  276. * of that side effect.
  277. */
  278. 3: blr
  279. .cfi_endproc