gettimeofday.S 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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*4+eq,cr0*4+eq,cr1*4+eq
  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. cmpl cr0,r4,r5
  156. cmpli cr1,r4,0
  157. blt 1f
  158. subf r4,r5,r4
  159. addi r3,r3,1
  160. 1: bge cr1,1f
  161. addi r3,r3,-1
  162. add r4,r4,r5
  163. 1: stw r3,TSPC32_TV_SEC(r11)
  164. stw r4,TSPC32_TV_NSEC(r11)
  165. mtlr r12
  166. li r3,0
  167. blr
  168. /*
  169. * syscall fallback
  170. */
  171. 98:
  172. mtlr r12
  173. mr r3,r10
  174. mr r4,r11
  175. 99:
  176. li r0,__NR_clock_gettime
  177. sc
  178. blr
  179. .cfi_endproc
  180. V_FUNCTION_END(__kernel_clock_gettime)
  181. /*
  182. * Exact prototype of clock_getres()
  183. *
  184. * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
  185. *
  186. */
  187. V_FUNCTION_BEGIN(__kernel_clock_getres)
  188. .cfi_startproc
  189. /* Check for supported clock IDs */
  190. cmpwi cr0,r3,CLOCK_REALTIME
  191. cmpwi cr1,r3,CLOCK_MONOTONIC
  192. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  193. bne cr0,99f
  194. li r3,0
  195. cmpli cr0,r4,0
  196. beqlr
  197. lis r5,CLOCK_REALTIME_RES@h
  198. ori r5,r5,CLOCK_REALTIME_RES@l
  199. stw r3,TSPC32_TV_SEC(r4)
  200. stw r5,TSPC32_TV_NSEC(r4)
  201. blr
  202. /*
  203. * syscall fallback
  204. */
  205. 99:
  206. li r0,__NR_clock_getres
  207. sc
  208. blr
  209. .cfi_endproc
  210. V_FUNCTION_END(__kernel_clock_getres)
  211. /*
  212. * This is the core of gettimeofday() & friends, it returns the xsec
  213. * value in r3 & r4 and expects the datapage ptr (non clobbered)
  214. * in r9. clobbers r0,r4,r5,r6,r7,r8.
  215. * When returning, r8 contains the counter value that can be reused
  216. * by the monotonic clock implementation
  217. */
  218. __do_get_xsec:
  219. .cfi_startproc
  220. /* Check for update count & load values. We use the low
  221. * order 32 bits of the update count
  222. */
  223. #ifdef CONFIG_PPC64
  224. 1: lwz r8,(CFG_TB_UPDATE_COUNT+4)(r9)
  225. #else
  226. 1: lwz r8,(CFG_TB_UPDATE_COUNT)(r9)
  227. #endif
  228. andi. r0,r8,1 /* pending update ? loop */
  229. bne- 1b
  230. xor r0,r8,r8 /* create dependency */
  231. add r9,r9,r0
  232. /* Load orig stamp (offset to TB) */
  233. lwz r5,CFG_TB_ORIG_STAMP(r9)
  234. lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
  235. /* Get a stable TB value */
  236. 2: mftbu r3
  237. mftbl r4
  238. mftbu r0
  239. cmpl cr0,r3,r0
  240. bne- 2b
  241. /* Substract tb orig stamp. If the high part is non-zero, we jump to
  242. * the slow path which call the syscall.
  243. * If it's ok, then we have our 32 bits tb_ticks value in r7
  244. */
  245. subfc r7,r6,r4
  246. subfe. r0,r5,r3
  247. bne- 3f
  248. /* Load scale factor & do multiplication */
  249. lwz r5,CFG_TB_TO_XS(r9) /* load values */
  250. lwz r6,(CFG_TB_TO_XS+4)(r9)
  251. mulhwu r4,r7,r5
  252. mulhwu r6,r7,r6
  253. mullw r0,r7,r5
  254. addc r6,r6,r0
  255. /* At this point, we have the scaled xsec value in r4 + XER:CA
  256. * we load & add the stamp since epoch
  257. */
  258. lwz r5,CFG_STAMP_XSEC(r9)
  259. lwz r6,(CFG_STAMP_XSEC+4)(r9)
  260. adde r4,r4,r6
  261. addze r3,r5
  262. /* We now have our result in r3,r4. We create a fake dependency
  263. * on that result and re-check the counter
  264. */
  265. or r6,r4,r3
  266. xor r0,r6,r6
  267. add r9,r9,r0
  268. #ifdef CONFIG_PPC64
  269. lwz r0,(CFG_TB_UPDATE_COUNT+4)(r9)
  270. #else
  271. lwz r0,(CFG_TB_UPDATE_COUNT)(r9)
  272. #endif
  273. cmpl cr0,r8,r0 /* check if updated */
  274. bne- 1b
  275. /* Warning ! The caller expects CR:EQ to be set to indicate a
  276. * successful calculation (so it won't fallback to the syscall
  277. * method). We have overriden that CR bit in the counter check,
  278. * but fortunately, the loop exit condition _is_ CR:EQ set, so
  279. * we can exit safely here. If you change this code, be careful
  280. * of that side effect.
  281. */
  282. 3: blr
  283. .cfi_endproc