gettimeofday.S 7.3 KB

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