gettimeofday.S 7.2 KB

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