xref: /freebsd/sys/i386/linux/linux_vdso_gtod.c (revision e58ff66464ac313296b683992c9131d7a85047de)
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 <i386/linux/linux.h>
429931033bSDmitry Chagin #include <i386/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 static int
write(int fd,const void * buf,size_t size)559931033bSDmitry Chagin write(int fd, const void *buf, size_t size)
569931033bSDmitry Chagin {
579931033bSDmitry Chagin 	int res;
589931033bSDmitry Chagin 
599931033bSDmitry Chagin 	__asm__ __volatile__
609931033bSDmitry Chagin 	(
619931033bSDmitry Chagin 	    "int $0x80"
629931033bSDmitry Chagin 	    : "=a"(res)
63*e58ff664SDmitry Chagin 	    : "a"(LINUX_SYS_linux_write), "b"(fd), "c"(buf), "d"(size)
649931033bSDmitry Chagin 	    : "cc", "memory"
659931033bSDmitry Chagin 	);
669931033bSDmitry Chagin 	return (res);
679931033bSDmitry Chagin }
689931033bSDmitry Chagin 
699931033bSDmitry Chagin static int
__vdso_clock_gettime_fallback(clockid_t clock_id,struct l_timespec * ts)709931033bSDmitry Chagin __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)
719931033bSDmitry Chagin {
729931033bSDmitry Chagin 	int res;
739931033bSDmitry Chagin 
749931033bSDmitry Chagin 	__asm__ __volatile__
759931033bSDmitry Chagin 	(
769931033bSDmitry Chagin 	    "int $0x80"
779931033bSDmitry Chagin 	    : "=a"(res)
789931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_clock_gettime), "b"(clock_id), "c"(ts)
799931033bSDmitry Chagin 	    : "cc", "memory"
809931033bSDmitry Chagin 	);
819931033bSDmitry Chagin 	return (res);
829931033bSDmitry Chagin }
839931033bSDmitry Chagin 
849931033bSDmitry Chagin static int
__vdso_clock_gettime64_fallback(clockid_t clock_id,struct l_timespec64 * ts)859931033bSDmitry Chagin __vdso_clock_gettime64_fallback(clockid_t clock_id, struct l_timespec64 *ts)
869931033bSDmitry Chagin {
879931033bSDmitry Chagin 	int res;
889931033bSDmitry Chagin 
899931033bSDmitry Chagin 	__asm__ __volatile__
909931033bSDmitry Chagin 	(
919931033bSDmitry Chagin 	    "int $0x80"
929931033bSDmitry Chagin 	    : "=a"(res)
939931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_clock_gettime64), "b"(clock_id), "c"(ts)
949931033bSDmitry Chagin 	    : "cc", "memory"
959931033bSDmitry Chagin 	);
969931033bSDmitry Chagin 	return (res);
979931033bSDmitry Chagin }
989931033bSDmitry Chagin 
999931033bSDmitry Chagin static int
__vdso_gettimeofday_fallback(l_timeval * tv,struct timezone * tz)1009931033bSDmitry Chagin __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
1019931033bSDmitry Chagin {
1029931033bSDmitry Chagin 	int res;
1039931033bSDmitry Chagin 
1049931033bSDmitry Chagin 	__asm__ __volatile__
1059931033bSDmitry Chagin 	(
1069931033bSDmitry Chagin 	    "int $0x80"
1079931033bSDmitry Chagin 	    : "=a"(res)
1089931033bSDmitry Chagin 	    : "a"(LINUX_SYS_gettimeofday), "b"(tv), "c"(tz)
1099931033bSDmitry Chagin 	    : "cc", "memory"
1109931033bSDmitry Chagin 	);
1119931033bSDmitry Chagin 	return (res);
1129931033bSDmitry Chagin }
1139931033bSDmitry Chagin 
1149931033bSDmitry Chagin static int
__vdso_clock_getres_fallback(clockid_t clock_id,struct l_timespec * ts)1159931033bSDmitry Chagin __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
1169931033bSDmitry Chagin {
1179931033bSDmitry Chagin 	int res;
1189931033bSDmitry Chagin 
1199931033bSDmitry Chagin 	__asm__ __volatile__
1209931033bSDmitry Chagin 	(
1219931033bSDmitry Chagin 	    "int $0x80"
1229931033bSDmitry Chagin 	    : "=a"(res)
1239931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_clock_getres), "b"(clock_id), "c"(ts)
1249931033bSDmitry Chagin 	    : "cc", "memory"
1259931033bSDmitry Chagin 	);
1269931033bSDmitry Chagin 	return (res);
1279931033bSDmitry Chagin }
1289931033bSDmitry Chagin 
1299931033bSDmitry Chagin static int
__vdso_getcpu_fallback(uint32_t * cpu,uint32_t * node,void * cache)1305a6a4fb2SDmitry Chagin __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
1315a6a4fb2SDmitry Chagin {
1325a6a4fb2SDmitry Chagin 	int res;
1335a6a4fb2SDmitry Chagin 
1345a6a4fb2SDmitry Chagin 	__asm__ __volatile__
1355a6a4fb2SDmitry Chagin 	(
1365a6a4fb2SDmitry Chagin 	    "int $0x80"
1375a6a4fb2SDmitry Chagin 	    : "=a"(res)
1385a6a4fb2SDmitry Chagin 	    : "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
1395a6a4fb2SDmitry Chagin 	    : "cc", "memory"
1405a6a4fb2SDmitry Chagin 	);
1415a6a4fb2SDmitry Chagin 	return (res);
1425a6a4fb2SDmitry Chagin }
1435a6a4fb2SDmitry Chagin 
1445a6a4fb2SDmitry Chagin static int
__vdso_time_fallback(long * tm)1459931033bSDmitry Chagin __vdso_time_fallback(long *tm)
1469931033bSDmitry Chagin {
1479931033bSDmitry Chagin 	int res;
1489931033bSDmitry Chagin 
1499931033bSDmitry Chagin 	__asm__ __volatile__
1509931033bSDmitry Chagin 	(
1519931033bSDmitry Chagin 	    "int $0x80"
1529931033bSDmitry Chagin 	    : "=a"(res)
1539931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_time), "b"(tm)
1549931033bSDmitry Chagin 	    : "cc", "memory"
1559931033bSDmitry Chagin 	);
1569931033bSDmitry Chagin 	return (res);
1579931033bSDmitry Chagin }
1589931033bSDmitry Chagin 
1599931033bSDmitry Chagin #include <compat/linux/linux_vdso_gtod.inc>
160