2 * Userspace implementations of gettimeofday() and friends.
4 * Copyright (C) 2012 ARM Limited
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Author: Will Deacon <will.deacon@arm.com>
21 #include <linux/linkage.h>
22 #include <asm/asm-offsets.h>
23 #include <asm/unistd.h>
25 #define NSEC_PER_SEC_LO16 0xca00
26 #define NSEC_PER_SEC_HI16 0x3b9a
33 9999: ldr seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
34 tbnz seqcnt, #0, 9999b
36 ldr use_syscall, [vdso_data, #VDSO_USE_SYSCALL]
39 .macro seqcnt_read, cnt
41 ldr \cnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
44 .macro seqcnt_check, cnt, fail
51 /* int __kernel_gettimeofday(struct timeval *tv, struct timezone *tz); */
52 ENTRY(__kernel_gettimeofday)
57 /* Acquire the sequence counter and get the timespec. */
58 adr vdso_data, _vdso_data
62 /* If tv is NULL, skip to the timezone code. */
67 /* Convert ns to us. */
71 stp x10, x11, [x0, #TVAL_TV_SEC]
73 /* If tz is NULL, return 0. */
75 ldp w4, w5, [vdso_data, #VDSO_TZ_MINWEST]
76 stp w4, w5, [x1, #TZ_MINWEST]
81 /* Syscall fallback. */
82 mov x8, #__NR_gettimeofday
86 ENDPROC(__kernel_gettimeofday)
88 /* int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); */
89 ENTRY(__kernel_clock_gettime)
91 cmp w0, #CLOCK_REALTIME
92 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
98 /* Get kernel timespec. */
99 adr vdso_data, _vdso_data
106 cmp w0, #CLOCK_MONOTONIC
109 /* Get wtm timespec. */
110 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
112 /* Check the sequence counter. */
117 cmp w0, #CLOCK_REALTIME_COARSE
118 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
121 /* Get coarse timespec. */
122 adr vdso_data, _vdso_data
124 ldp x10, x11, [vdso_data, #VDSO_XTIME_CRS_SEC]
126 /* Get wtm timespec. */
127 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
129 /* Check the sequence counter. */
133 cmp w0, #CLOCK_MONOTONIC_COARSE
136 /* Add on wtm timespec. */
141 /* Normalise the new timespec. */
142 mov x15, #NSEC_PER_SEC_LO16
143 movk x15, #NSEC_PER_SEC_HI16, lsl #16
155 6: /* Store to the user timespec. */
157 stp x10, x11, [x1, #TSPEC_TV_SEC]
162 8: /* Syscall fallback. */
163 mov x8, #__NR_clock_gettime
167 ENDPROC(__kernel_clock_gettime)
169 /* int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); */
170 ENTRY(__kernel_clock_getres)
174 cmp w0, #CLOCK_REALTIME
175 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
181 cmp w0, #CLOCK_REALTIME_COARSE
182 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
188 3: /* res == NULL. */
192 4: /* Syscall fallback. */
193 mov x8, #__NR_clock_getres
197 .quad CLOCK_REALTIME_RES
199 .quad CLOCK_COARSE_RES
201 ENDPROC(__kernel_clock_getres)
204 * Read the current time from the architected counter.
205 * Expects vdso_data to be initialised.
206 * Clobbers the temporary registers (x9 - x15).
208 * - w9 = vDSO sequence counter
209 * - (x10, x11) = (ts->tv_sec, shifted ts->tv_nsec)
212 ENTRY(__do_get_tspec)
215 /* Read from the vDSO data page. */
216 ldr x10, [vdso_data, #VDSO_CS_CYCLE_LAST]
217 ldp x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
218 ldp w11, w12, [vdso_data, #VDSO_CS_MULT]
221 /* Read the virtual counter. */
225 /* Calculate cycle delta and convert to ns. */
227 /* We can only guarantee 56 bits of precision. */
228 movn x15, #0xff00, lsl #48
232 /* Use the kernel time to calculate the new timespec. */
233 mov x11, #NSEC_PER_SEC_LO16
234 movk x11, #NSEC_PER_SEC_HI16, lsl #16
244 ENDPROC(__do_get_tspec)