1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org> 5 * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/elf.h> 34 #include <sys/errno.h> 35 #include <sys/proc.h> 36 #include <sys/stddef.h> 37 #define _KERNEL 38 #include <sys/vdso.h> 39 #undef _KERNEL 40 #include <stdbool.h> 41 42 #include <machine/atomic.h> 43 #include <machine/stdarg.h> 44 45 #include <arm64/linux/linux.h> 46 #include <arm64/linux/linux_syscall.h> 47 #include <compat/linux/linux_errno.h> 48 #include <compat/linux/linux_time.h> 49 50 /* The kernel fixup this at vDSO install */ 51 uintptr_t *kern_timekeep_base = NULL; 52 uint32_t kern_tsc_selector = 0; 53 54 static int 55 write(int lfd, const void *lbuf, size_t lsize) 56 { 57 register long svc asm("x8") = LINUX_SYS_write; 58 register int fd asm("x0") = lfd; 59 register const char *buf asm("x1") = lbuf; 60 register long size asm("x2") = lsize; 61 register long res asm ("x0"); 62 63 asm volatile( 64 " svc #0\n" 65 : "=r" (res) 66 : "r" (fd), "r" (buf), "r" (size), "r" (svc) 67 : "memory"); 68 return (res); 69 } 70 71 static int 72 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *lts) 73 { 74 register long svc asm("x8") = LINUX_SYS_linux_clock_gettime; 75 register clockid_t clockid asm("x0") = clock_id; 76 register struct l_timespec *ts asm("x1") = lts; 77 register long res asm ("x0"); 78 79 asm volatile( 80 " svc #0\n" 81 : "=r" (res) 82 : "r" (clockid), "r" (ts), "r" (svc) 83 : "memory"); 84 return (res); 85 } 86 87 static int 88 __vdso_gettimeofday_fallback(l_timeval *ltv, struct timezone *ltz) 89 { 90 register long svc asm("x8") = LINUX_SYS_gettimeofday; 91 register l_timeval *tv asm("x0") = ltv; 92 register struct timezone *tz asm("x1") = ltz; 93 register long res asm ("x0"); 94 95 asm volatile( 96 " svc #0\n" 97 : "=r" (res) 98 : "r" (tv), "r" (tz), "r" (svc) 99 : "memory"); 100 return (res); 101 } 102 103 static int 104 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *lts) 105 { 106 register long svc asm("x8") = LINUX_SYS_linux_clock_getres; 107 register clockid_t clockid asm("x0") = clock_id; 108 register struct l_timespec *ts asm("x1") = lts; 109 register long res asm ("x0"); 110 111 asm volatile( 112 " svc #0\n" 113 : "=r" (res) 114 : "r" (clockid), "r" (ts), "r" (svc) 115 : "memory"); 116 return (res); 117 } 118 119 /* 120 * copied from lib/libc/aarch64/sys/__vdso_gettc.c 121 */ 122 123 static inline uint64_t 124 cp15_cntvct_get(void) 125 { 126 uint64_t reg; 127 128 __asm __volatile("mrs %0, cntvct_el0" : "=r" (reg)); 129 return (reg); 130 } 131 132 static inline uint64_t 133 cp15_cntpct_get(void) 134 { 135 uint64_t reg; 136 137 __asm __volatile("mrs %0, cntpct_el0" : "=r" (reg)); 138 return (reg); 139 } 140 141 int 142 __vdso_gettc(const struct vdso_timehands *th, u_int *tc) 143 { 144 145 if (th->th_algo != VDSO_TH_ALGO_ARM_GENTIM) 146 return (ENOSYS); 147 __asm __volatile("isb" : : : "memory"); 148 *tc = th->th_physical == 0 ? cp15_cntvct_get() : cp15_cntpct_get(); 149 return (0); 150 } 151 152 #include <compat/linux/linux_vdso_gtod.inc> 153