gettimeofday.S 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /* Offset for the low 32-bit part of a field of long type */
  19. #ifdef CONFIG_PPC64
  20. #define LOPART 4
  21. #define TSPEC_TV_SEC TSPC64_TV_SEC+LOPART
  22. #else
  23. #define LOPART 0
  24. #define TSPEC_TV_SEC TSPC32_TV_SEC
  25. #endif
  26. .text
  27. /*
  28. * Exact prototype of gettimeofday
  29. *
  30. * int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz);
  31. *
  32. */
  33. V_FUNCTION_BEGIN(__kernel_gettimeofday)
  34. .cfi_startproc
  35. mflr r12
  36. .cfi_register lr,r12
  37. mr r10,r3 /* r10 saves tv */
  38. mr r11,r4 /* r11 saves tz */
  39. bl __get_datapage@local /* get data page */
  40. mr r9, r3 /* datapage ptr in r9 */
  41. cmplwi r10,0 /* check if tv is NULL */
  42. beq 3f
  43. lis r7,1000000@ha /* load up USEC_PER_SEC */
  44. addi r7,r7,1000000@l /* so we get microseconds in r4 */
  45. bl __do_get_tspec@local /* get sec/usec from tb & kernel */
  46. stw r3,TVAL32_TV_SEC(r10)
  47. stw r4,TVAL32_TV_USEC(r10)
  48. 3: cmplwi 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. .cfi_endproc
  59. V_FUNCTION_END(__kernel_gettimeofday)
  60. /*
  61. * Exact prototype of clock_gettime()
  62. *
  63. * int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp);
  64. *
  65. */
  66. V_FUNCTION_BEGIN(__kernel_clock_gettime)
  67. .cfi_startproc
  68. /* Check for supported clock IDs */
  69. cmpli cr0,r3,CLOCK_REALTIME
  70. cmpli cr1,r3,CLOCK_MONOTONIC
  71. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  72. bne cr0,99f
  73. mflr r12 /* r12 saves lr */
  74. .cfi_register lr,r12
  75. mr r11,r4 /* r11 saves tp */
  76. bl __get_datapage@local /* get data page */
  77. mr r9,r3 /* datapage ptr in r9 */
  78. lis r7,NSEC_PER_SEC@h /* want nanoseconds */
  79. ori r7,r7,NSEC_PER_SEC@l
  80. 50: bl __do_get_tspec@local /* get sec/nsec from tb & kernel */
  81. bne cr1,80f /* not monotonic -> all done */
  82. /*
  83. * CLOCK_MONOTONIC
  84. */
  85. /* now we must fixup using wall to monotonic. We need to snapshot
  86. * that value and do the counter trick again. Fortunately, we still
  87. * have the counter value in r8 that was returned by __do_get_xsec.
  88. * At this point, r3,r4 contain our sec/nsec values, r5 and r6
  89. * can be used, r7 contains NSEC_PER_SEC.
  90. */
  91. lwz r5,WTOM_CLOCK_SEC(r9)
  92. lwz r6,WTOM_CLOCK_NSEC(r9)
  93. /* We now have our offset in r5,r6. We create a fake dependency
  94. * on that value and re-check the counter
  95. */
  96. or r0,r6,r5
  97. xor r0,r0,r0
  98. add r9,r9,r0
  99. lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
  100. cmpl cr0,r8,r0 /* check if updated */
  101. bne- 50b
  102. /* Calculate and store result. Note that this mimics the C code,
  103. * which may cause funny results if nsec goes negative... is that
  104. * possible at all ?
  105. */
  106. add r3,r3,r5
  107. add r4,r4,r6
  108. cmpw cr0,r4,r7
  109. cmpwi cr1,r4,0
  110. blt 1f
  111. subf r4,r7,r4
  112. addi r3,r3,1
  113. 1: bge cr1,80f
  114. addi r3,r3,-1
  115. add r4,r4,r7
  116. 80: stw r3,TSPC32_TV_SEC(r11)
  117. stw r4,TSPC32_TV_NSEC(r11)
  118. mtlr r12
  119. crclr cr0*4+so
  120. li r3,0
  121. blr
  122. /*
  123. * syscall fallback
  124. */
  125. 99:
  126. li r0,__NR_clock_gettime
  127. sc
  128. blr
  129. .cfi_endproc
  130. V_FUNCTION_END(__kernel_clock_gettime)
  131. /*
  132. * Exact prototype of clock_getres()
  133. *
  134. * int __kernel_clock_getres(clockid_t clock_id, struct timespec *res);
  135. *
  136. */
  137. V_FUNCTION_BEGIN(__kernel_clock_getres)
  138. .cfi_startproc
  139. /* Check for supported clock IDs */
  140. cmpwi cr0,r3,CLOCK_REALTIME
  141. cmpwi cr1,r3,CLOCK_MONOTONIC
  142. cror cr0*4+eq,cr0*4+eq,cr1*4+eq
  143. bne cr0,99f
  144. li r3,0
  145. cmpli cr0,r4,0
  146. crclr cr0*4+so
  147. beqlr
  148. lis r5,CLOCK_REALTIME_RES@h
  149. ori r5,r5,CLOCK_REALTIME_RES@l
  150. stw r3,TSPC32_TV_SEC(r4)
  151. stw r5,TSPC32_TV_NSEC(r4)
  152. blr
  153. /*
  154. * syscall fallback
  155. */
  156. 99:
  157. li r0,__NR_clock_getres
  158. sc
  159. blr
  160. .cfi_endproc
  161. V_FUNCTION_END(__kernel_clock_getres)
  162. /*
  163. * Exact prototype of time()
  164. *
  165. * time_t time(time *t);
  166. *
  167. */
  168. V_FUNCTION_BEGIN(__kernel_time)
  169. .cfi_startproc
  170. mflr r12
  171. .cfi_register lr,r12
  172. mr r11,r3 /* r11 holds t */
  173. bl __get_datapage@local
  174. mr r9, r3 /* datapage ptr in r9 */
  175. lwz r3,STAMP_XTIME+TSPEC_TV_SEC(r9)
  176. cmplwi r11,0 /* check if t is NULL */
  177. beq 2f
  178. stw r3,0(r11) /* store result at *t */
  179. 2: mtlr r12
  180. crclr cr0*4+so
  181. blr
  182. .cfi_endproc
  183. V_FUNCTION_END(__kernel_time)
  184. /*
  185. * This is the core of clock_gettime() and gettimeofday(),
  186. * it returns the current time in r3 (seconds) and r4.
  187. * On entry, r7 gives the resolution of r4, either USEC_PER_SEC
  188. * or NSEC_PER_SEC, giving r4 in microseconds or nanoseconds.
  189. * It expects the datapage ptr in r9 and doesn't clobber it.
  190. * It clobbers r0, r5 and r6.
  191. * On return, r8 contains the counter value that can be reused.
  192. * This clobbers cr0 but not any other cr field.
  193. */
  194. __do_get_tspec:
  195. .cfi_startproc
  196. /* Check for update count & load values. We use the low
  197. * order 32 bits of the update count
  198. */
  199. 1: lwz r8,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
  200. andi. r0,r8,1 /* pending update ? loop */
  201. bne- 1b
  202. xor r0,r8,r8 /* create dependency */
  203. add r9,r9,r0
  204. /* Load orig stamp (offset to TB) */
  205. lwz r5,CFG_TB_ORIG_STAMP(r9)
  206. lwz r6,(CFG_TB_ORIG_STAMP+4)(r9)
  207. /* Get a stable TB value */
  208. #ifdef CONFIG_8xx
  209. 2: mftbu r3
  210. mftbl r4
  211. mftbu r0
  212. #else
  213. 2: mfspr r3, SPRN_TBRU
  214. mfspr r4, SPRN_TBRL
  215. mfspr r0, SPRN_TBRU
  216. #endif
  217. cmplw cr0,r3,r0
  218. bne- 2b
  219. /* Subtract tb orig stamp and shift left 12 bits.
  220. */
  221. subfc r4,r6,r4
  222. subfe r0,r5,r3
  223. slwi r0,r0,12
  224. rlwimi. r0,r4,12,20,31
  225. slwi r4,r4,12
  226. /*
  227. * Load scale factor & do multiplication.
  228. * We only use the high 32 bits of the tb_to_xs value.
  229. * Even with a 1GHz timebase clock, the high 32 bits of
  230. * tb_to_xs will be at least 4 million, so the error from
  231. * ignoring the low 32 bits will be no more than 0.25ppm.
  232. * The error will just make the clock run very very slightly
  233. * slow until the next time the kernel updates the VDSO data,
  234. * at which point the clock will catch up to the kernel's value,
  235. * so there is no long-term error accumulation.
  236. */
  237. lwz r5,CFG_TB_TO_XS(r9) /* load values */
  238. mulhwu r4,r4,r5
  239. li r3,0
  240. beq+ 4f /* skip high part computation if 0 */
  241. mulhwu r3,r0,r5
  242. mullw r5,r0,r5
  243. addc r4,r4,r5
  244. addze r3,r3
  245. 4:
  246. /* At this point, we have seconds since the xtime stamp
  247. * as a 32.32 fixed-point number in r3 and r4.
  248. * Load & add the xtime stamp.
  249. */
  250. lwz r5,STAMP_XTIME+TSPEC_TV_SEC(r9)
  251. lwz r6,STAMP_SEC_FRAC(r9)
  252. addc r4,r4,r6
  253. adde r3,r3,r5
  254. /* We create a fake dependency on the result in r3/r4
  255. * and re-check the counter
  256. */
  257. or r6,r4,r3
  258. xor r0,r6,r6
  259. add r9,r9,r0
  260. lwz r0,(CFG_TB_UPDATE_COUNT+LOPART)(r9)
  261. cmplw cr0,r8,r0 /* check if updated */
  262. bne- 1b
  263. mulhwu r4,r4,r7 /* convert to micro or nanoseconds */
  264. blr
  265. .cfi_endproc