gettimeofday.S 7.2 KB

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