19931033bSDmitry Chagin /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
39931033bSDmitry Chagin *
49931033bSDmitry Chagin * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
59931033bSDmitry Chagin *
69931033bSDmitry Chagin * Redistribution and use in source and binary forms, with or without
79931033bSDmitry Chagin * modification, are permitted provided that the following conditions
89931033bSDmitry Chagin * are met:
99931033bSDmitry Chagin * 1. Redistributions of source code must retain the above copyright
109931033bSDmitry Chagin * notice, this list of conditions and the following disclaimer.
119931033bSDmitry Chagin * 2. Redistributions in binary form must reproduce the above copyright
129931033bSDmitry Chagin * notice, this list of conditions and the following disclaimer in the
139931033bSDmitry Chagin * documentation and/or other materials provided with the distribution.
149931033bSDmitry Chagin *
159931033bSDmitry Chagin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
169931033bSDmitry Chagin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179931033bSDmitry Chagin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189931033bSDmitry Chagin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199931033bSDmitry Chagin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209931033bSDmitry Chagin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
219931033bSDmitry Chagin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
229931033bSDmitry Chagin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
239931033bSDmitry Chagin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249931033bSDmitry Chagin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
259931033bSDmitry Chagin * SUCH DAMAGE.
269931033bSDmitry Chagin */
279931033bSDmitry Chagin
289931033bSDmitry Chagin #include <sys/elf.h>
299931033bSDmitry Chagin #include <sys/errno.h>
309931033bSDmitry Chagin #include <sys/proc.h>
319931033bSDmitry Chagin #include <sys/stddef.h>
329931033bSDmitry Chagin #define _KERNEL
339931033bSDmitry Chagin #include <sys/vdso.h>
349931033bSDmitry Chagin #undef _KERNEL
359931033bSDmitry Chagin #include <stdbool.h>
369931033bSDmitry Chagin
379931033bSDmitry Chagin #include <machine/atomic.h>
38332eca05SDmitry Chagin #include <machine/cpufunc.h>
399931033bSDmitry Chagin #include <machine/stdarg.h>
409931033bSDmitry Chagin
419931033bSDmitry Chagin #include <amd64/linux/linux.h>
429931033bSDmitry Chagin #include <amd64/linux/linux_syscall.h>
439931033bSDmitry Chagin #include <compat/linux/linux_errno.h>
44c8a79231SDmitry Chagin #include <compat/linux/linux_time.h>
459931033bSDmitry Chagin
469931033bSDmitry Chagin /* The kernel fixup this at vDSO install */
479931033bSDmitry Chagin uintptr_t *kern_timekeep_base = NULL;
489931033bSDmitry Chagin uint32_t kern_tsc_selector = 0;
495a6a4fb2SDmitry Chagin uint32_t kern_cpu_selector = 0;
509931033bSDmitry Chagin
519931033bSDmitry Chagin #include <x86/linux/linux_vdso_gettc_x86.inc>
525a6a4fb2SDmitry Chagin #include <x86/linux/linux_vdso_getcpu_x86.inc>
539931033bSDmitry Chagin
549931033bSDmitry Chagin /* for debug purpose */
559931033bSDmitry Chagin static int
write(int fd,const void * buf,size_t size)569931033bSDmitry Chagin write(int fd, const void *buf, size_t size)
579931033bSDmitry Chagin {
589931033bSDmitry Chagin int res;
599931033bSDmitry Chagin
609931033bSDmitry Chagin __asm__ __volatile__
619931033bSDmitry Chagin (
629931033bSDmitry Chagin "syscall"
639931033bSDmitry Chagin : "=a"(res)
64*e58ff664SDmitry Chagin : "a"(LINUX_SYS_linux_write), "D"(fd), "S"(buf), "d"(size)
659931033bSDmitry Chagin : "cc", "rcx", "r11", "memory"
669931033bSDmitry Chagin );
679931033bSDmitry Chagin return (res);
689931033bSDmitry Chagin }
699931033bSDmitry Chagin
709931033bSDmitry Chagin static int
__vdso_clock_gettime_fallback(clockid_t clock_id,struct l_timespec * ts)719931033bSDmitry Chagin __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)
729931033bSDmitry Chagin {
739931033bSDmitry Chagin int res;
749931033bSDmitry Chagin
759931033bSDmitry Chagin __asm__ __volatile__
769931033bSDmitry Chagin (
779931033bSDmitry Chagin "syscall"
789931033bSDmitry Chagin : "=a"(res)
799931033bSDmitry Chagin : "a"(LINUX_SYS_linux_clock_gettime), "D"(clock_id), "S"(ts)
809931033bSDmitry Chagin : "cc", "rcx", "r11", "memory"
819931033bSDmitry Chagin );
829931033bSDmitry Chagin return (res);
839931033bSDmitry Chagin }
849931033bSDmitry Chagin
859931033bSDmitry Chagin static int
__vdso_gettimeofday_fallback(l_timeval * tv,struct timezone * tz)869931033bSDmitry Chagin __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
879931033bSDmitry Chagin {
889931033bSDmitry Chagin int res;
899931033bSDmitry Chagin
909931033bSDmitry Chagin __asm__ __volatile__
919931033bSDmitry Chagin (
929931033bSDmitry Chagin "syscall"
939931033bSDmitry Chagin : "=a"(res)
949931033bSDmitry Chagin : "a"(LINUX_SYS_gettimeofday), "D"(tv), "S"(tz)
959931033bSDmitry Chagin : "cc", "rcx", "r11", "memory"
969931033bSDmitry Chagin );
979931033bSDmitry Chagin return (res);
989931033bSDmitry Chagin }
999931033bSDmitry Chagin
1009931033bSDmitry Chagin static int
__vdso_clock_getres_fallback(clockid_t clock_id,struct l_timespec * ts)1019931033bSDmitry Chagin __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
1029931033bSDmitry Chagin {
1039931033bSDmitry Chagin int res;
1049931033bSDmitry Chagin
1059931033bSDmitry Chagin __asm__ __volatile__
1069931033bSDmitry Chagin (
1079931033bSDmitry Chagin "syscall"
1089931033bSDmitry Chagin : "=a"(res)
1099931033bSDmitry Chagin : "a"(LINUX_SYS_linux_clock_getres), "D"(clock_id), "S"(ts)
1109931033bSDmitry Chagin : "cc", "rcx", "r11", "memory"
1119931033bSDmitry Chagin );
1129931033bSDmitry Chagin return (res);
1139931033bSDmitry Chagin }
1149931033bSDmitry Chagin
1159931033bSDmitry Chagin static int
__vdso_getcpu_fallback(uint32_t * cpu,uint32_t * node,void * cache)1169931033bSDmitry Chagin __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
1179931033bSDmitry Chagin {
1189931033bSDmitry Chagin int res;
1199931033bSDmitry Chagin
1209931033bSDmitry Chagin __asm__ __volatile__
1219931033bSDmitry Chagin (
1229931033bSDmitry Chagin "syscall"
1239931033bSDmitry Chagin : "=a"(res)
1249931033bSDmitry Chagin : "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
1259931033bSDmitry Chagin : "cc", "rcx", "r11", "memory"
1269931033bSDmitry Chagin );
1279931033bSDmitry Chagin return (res);
1289931033bSDmitry Chagin }
1299931033bSDmitry Chagin
1309931033bSDmitry Chagin static int
__vdso_time_fallback(long * tm)1319931033bSDmitry Chagin __vdso_time_fallback(long *tm)
1329931033bSDmitry Chagin {
1339931033bSDmitry Chagin int res;
1349931033bSDmitry Chagin
1359931033bSDmitry Chagin __asm__ __volatile__
1369931033bSDmitry Chagin (
1379931033bSDmitry Chagin "syscall"
1389931033bSDmitry Chagin : "=a"(res)
1399931033bSDmitry Chagin : "a"(LINUX_SYS_linux_time), "D"(tm)
1409931033bSDmitry Chagin : "cc", "rcx", "r11", "memory"
1419931033bSDmitry Chagin );
1429931033bSDmitry Chagin return (res);
1439931033bSDmitry Chagin }
1449931033bSDmitry Chagin
1459931033bSDmitry Chagin #include <compat/linux/linux_vdso_gtod.inc>
146