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
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 /* xtime_coarse_nsec is already right-shifted */
126 /* Get coarse timespec. */
127 adr vdso_data, _vdso_data
129 ldp x10, x11, [vdso_data, #VDSO_XTIME_CRS_SEC]
131 /* Get wtm timespec. */
132 ldp x13, x14, [vdso_data, #VDSO_WTM_CLK_SEC]
134 /* Check the sequence counter. */
138 cmp w0, #CLOCK_MONOTONIC_COARSE
141 /* Add on wtm timespec. */
146 /* Normalise the new timespec. */
147 mov x15, #NSEC_PER_SEC_LO16
148 movk x15, #NSEC_PER_SEC_HI16, lsl #16
160 6: /* Store to the user timespec. */
162 stp x10, x11, [x1, #TSPEC_TV_SEC]
167 8: /* Syscall fallback. */
168 mov x8, #__NR_clock_gettime
172 ENDPROC(__kernel_clock_gettime)
174 /* int __kernel_clock_getres(clockid_t clock_id, struct timespec *res); */
175 ENTRY(__kernel_clock_getres)
177 cmp w0, #CLOCK_REALTIME
178 ccmp w0, #CLOCK_MONOTONIC, #0x4, ne
184 cmp w0, #CLOCK_REALTIME_COARSE
185 ccmp w0, #CLOCK_MONOTONIC_COARSE, #0x4, ne
192 3: /* res == NULL. */
196 4: /* Syscall fallback. */
197 mov x8, #__NR_clock_getres
201 .quad CLOCK_REALTIME_RES
203 .quad CLOCK_COARSE_RES
205 ENDPROC(__kernel_clock_getres)
208 * Read the current time from the architected counter.
209 * Expects vdso_data to be initialised.
210 * Clobbers the temporary registers (x9 - x15).
212 * - w9 = vDSO sequence counter
213 * - (x10, x11) = (ts->tv_sec, shifted ts->tv_nsec)
216 ENTRY(__do_get_tspec)
219 /* Read from the vDSO data page. */
220 ldr x10, [vdso_data, #VDSO_CS_CYCLE_LAST]
221 ldp x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
222 ldp w11, w12, [vdso_data, #VDSO_CS_MULT]
225 /* Read the virtual counter. */
229 /* Calculate cycle delta and convert to ns. */
231 /* We can only guarantee 56 bits of precision. */
232 movn x15, #0xff00, lsl #48
236 /* Use the kernel time to calculate the new timespec. */
237 mov x11, #NSEC_PER_SEC_LO16
238 movk x11, #NSEC_PER_SEC_HI16, lsl #16
248 ENDPROC(__do_get_tspec)