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]
78 stp w4, w5, [x1, #TZ_MINWEST]
83 /* Syscall fallback. */
84 mov x8, #__NR_gettimeofday
88 ENDPROC(__kernel_gettimeofday)
90 /* int __kernel_clock_gettime(clockid_t clock_id, struct timespec *tp); */
91 ENTRY(__kernel_clock_gettime)
93 cmp w0, #CLOCK_REALTIME
94 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
100 /* Get kernel timespec. */
101 adr vdso_data, _vdso_data
108 cmp w0, #CLOCK_MONOTONIC
111 /* Get wtm timespec. */
112 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
114 /* Check the sequence counter. */
119 cmp w0, #CLOCK_REALTIME_COARSE
120 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
123 /* Get coarse timespec. */
124 adr vdso_data, _vdso_data
126 ldp x10, x11, [vdso_data, #VDSO_XTIME_CRS_SEC]
128 /* Get wtm timespec. */
129 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
131 /* Check the sequence counter. */
135 cmp w0, #CLOCK_MONOTONIC_COARSE
138 /* Add on wtm timespec. */
143 /* Normalise the new timespec. */
144 mov x15, #NSEC_PER_SEC_LO16
145 movk x15, #NSEC_PER_SEC_HI16, lsl #16
157 6: /* Store to the user timespec. */
159 stp x10, x11, [x1, #TSPEC_TV_SEC]
164 8: /* Syscall fallback. */
165 mov x8, #__NR_clock_gettime
169 ENDPROC(__kernel_clock_gettime)
171 /* int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); */
172 ENTRY(__kernel_clock_getres)
176 cmp w0, #CLOCK_REALTIME
177 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
183 cmp w0, #CLOCK_REALTIME_COARSE
184 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
190 3: /* res == NULL. */
194 4: /* Syscall fallback. */
195 mov x8, #__NR_clock_getres
199 .quad CLOCK_REALTIME_RES
201 .quad CLOCK_COARSE_RES
203 ENDPROC(__kernel_clock_getres)
206 * Read the current time from the architected counter.
207 * Expects vdso_data to be initialised.
208 * Clobbers the temporary registers (x9 - x15).
210 * - w9 = vDSO sequence counter
211 * - (x10, x11) = (ts->tv_sec, shifted ts->tv_nsec)
214 ENTRY(__do_get_tspec)
217 /* Read from the vDSO data page. */
218 ldr x10, [vdso_data, #VDSO_CS_CYCLE_LAST]
219 ldp x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
220 ldp w11, w12, [vdso_data, #VDSO_CS_MULT]
223 /* Read the virtual counter. */
227 /* Calculate cycle delta and convert to ns. */
229 /* We can only guarantee 56 bits of precision. */
230 movn x15, #0xff00, lsl #48
234 /* Use the kernel time to calculate the new timespec. */
235 mov x11, #NSEC_PER_SEC_LO16
236 movk x11, #NSEC_PER_SEC_HI16, lsl #16
246 ENDPROC(__do_get_tspec)