xref: /freebsd/sys/amd64/linux32/linux32_vdso_gtod.c (revision c8a79231a5a74fc4263ec3d18c6519a974efb529)
19931033bSDmitry Chagin /*-
29931033bSDmitry Chagin  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
299931033bSDmitry Chagin #include <sys/cdefs.h>
309931033bSDmitry Chagin __FBSDID("$FreeBSD$");
319931033bSDmitry Chagin 
329931033bSDmitry Chagin #include <sys/elf.h>
339931033bSDmitry Chagin #include <sys/errno.h>
349931033bSDmitry Chagin #include <sys/proc.h>
359931033bSDmitry Chagin #include <sys/stddef.h>
369931033bSDmitry Chagin #define	_KERNEL
379931033bSDmitry Chagin #include <sys/vdso.h>
389931033bSDmitry Chagin #undef	_KERNEL
399931033bSDmitry Chagin #include <stdbool.h>
409931033bSDmitry Chagin 
41332eca05SDmitry Chagin #include <i386/include/atomic.h>
42332eca05SDmitry Chagin #include <i386/include/cpufunc.h>
43332eca05SDmitry Chagin #include <i386/include/stdarg.h>
449931033bSDmitry Chagin 
459931033bSDmitry Chagin #include <amd64/linux32/linux.h>
469931033bSDmitry Chagin #include <amd64/linux32/linux32_syscall.h>
479931033bSDmitry Chagin #include <compat/linux/linux_errno.h>
48*c8a79231SDmitry Chagin #include <compat/linux/linux_time.h>
499931033bSDmitry Chagin 
509931033bSDmitry Chagin /* The kernel fixup this at vDSO install */
519931033bSDmitry Chagin uintptr_t *kern_timekeep_base = NULL;
529931033bSDmitry Chagin uint32_t kern_tsc_selector = 0;
535a6a4fb2SDmitry Chagin uint32_t kern_cpu_selector = 0;
549931033bSDmitry Chagin 
559931033bSDmitry Chagin #include <x86/linux/linux_vdso_gettc_x86.inc>
565a6a4fb2SDmitry Chagin #include <x86/linux/linux_vdso_getcpu_x86.inc>
579931033bSDmitry Chagin 
589931033bSDmitry Chagin static int
599931033bSDmitry Chagin write(int fd, const void *buf, size_t size)
609931033bSDmitry Chagin {
619931033bSDmitry Chagin 	int res;
629931033bSDmitry Chagin 
639931033bSDmitry Chagin 	__asm__ __volatile__
649931033bSDmitry Chagin 	(
659931033bSDmitry Chagin 	    "int $0x80"
669931033bSDmitry Chagin 	    : "=a"(res)
679931033bSDmitry Chagin 	    : "a"(LINUX32_SYS_write), "b"(fd), "c"(buf), "d"(size)
689931033bSDmitry Chagin 	    : "cc", "memory"
699931033bSDmitry Chagin 	);
709931033bSDmitry Chagin 	return (res);
719931033bSDmitry Chagin }
729931033bSDmitry Chagin 
739931033bSDmitry Chagin static int
749931033bSDmitry Chagin __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)
759931033bSDmitry Chagin {
769931033bSDmitry Chagin 	int res;
779931033bSDmitry Chagin 
789931033bSDmitry Chagin 	__asm__ __volatile__
799931033bSDmitry Chagin 	(
809931033bSDmitry Chagin 	    "int $0x80"
819931033bSDmitry Chagin 	    : "=a"(res)
829931033bSDmitry Chagin 	    : "a"(LINUX32_SYS_linux_clock_gettime), "b"(clock_id), "c"(ts)
839931033bSDmitry Chagin 	    : "cc", "memory"
849931033bSDmitry Chagin 	);
859931033bSDmitry Chagin 	return (res);
869931033bSDmitry Chagin }
879931033bSDmitry Chagin 
889931033bSDmitry Chagin static int
899931033bSDmitry Chagin __vdso_clock_gettime64_fallback(clockid_t clock_id, struct l_timespec64 *ts)
909931033bSDmitry Chagin {
919931033bSDmitry Chagin 	int res;
929931033bSDmitry Chagin 
939931033bSDmitry Chagin 	__asm__ __volatile__
949931033bSDmitry Chagin 	(
959931033bSDmitry Chagin 	    "int $0x80"
969931033bSDmitry Chagin 	    : "=a"(res)
979931033bSDmitry Chagin 	    : "a"(LINUX32_SYS_linux_clock_gettime64), "b"(clock_id), "c"(ts)
989931033bSDmitry Chagin 	    : "cc", "memory"
999931033bSDmitry Chagin 	);
1009931033bSDmitry Chagin 	return (res);
1019931033bSDmitry Chagin }
1029931033bSDmitry Chagin 
1039931033bSDmitry Chagin static int
1049931033bSDmitry Chagin __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
1059931033bSDmitry Chagin {
1069931033bSDmitry Chagin 	int res;
1079931033bSDmitry Chagin 
1089931033bSDmitry Chagin 	__asm__ __volatile__
1099931033bSDmitry Chagin 	(
1109931033bSDmitry Chagin 	    "int $0x80"
1119931033bSDmitry Chagin 	    : "=a"(res)
1129931033bSDmitry Chagin 	    : "a"(LINUX32_SYS_linux_gettimeofday), "b"(tv), "c"(tz)
1139931033bSDmitry Chagin 	    : "cc", "memory"
1149931033bSDmitry Chagin 	);
1159931033bSDmitry Chagin 	return (res);
1169931033bSDmitry Chagin }
1179931033bSDmitry Chagin 
1189931033bSDmitry Chagin static int
1199931033bSDmitry Chagin __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
1209931033bSDmitry Chagin {
1219931033bSDmitry Chagin 	int res;
1229931033bSDmitry Chagin 
1239931033bSDmitry Chagin 	__asm__ __volatile__
1249931033bSDmitry Chagin 	(
1259931033bSDmitry Chagin 	    "int $0x80"
1269931033bSDmitry Chagin 	    : "=a"(res)
1279931033bSDmitry Chagin 	    : "a"(LINUX32_SYS_linux_clock_getres), "b"(clock_id), "c"(ts)
1289931033bSDmitry Chagin 	    : "cc", "memory"
1299931033bSDmitry Chagin 	);
1309931033bSDmitry Chagin 	return (res);
1319931033bSDmitry Chagin }
1329931033bSDmitry Chagin 
1339931033bSDmitry Chagin static int
1345a6a4fb2SDmitry Chagin __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
1355a6a4fb2SDmitry Chagin {
1365a6a4fb2SDmitry Chagin 	int res;
1375a6a4fb2SDmitry Chagin 
1385a6a4fb2SDmitry Chagin 	__asm__ __volatile__
1395a6a4fb2SDmitry Chagin 	(
1405a6a4fb2SDmitry Chagin 	    "int $0x80"
1415a6a4fb2SDmitry Chagin 	    : "=a"(res)
1425a6a4fb2SDmitry Chagin 	    : "a"(LINUX32_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
1435a6a4fb2SDmitry Chagin 	    : "cc", "memory"
1445a6a4fb2SDmitry Chagin 	);
1455a6a4fb2SDmitry Chagin 	return (res);
1465a6a4fb2SDmitry Chagin }
1475a6a4fb2SDmitry Chagin 
1485a6a4fb2SDmitry Chagin static int
1499931033bSDmitry Chagin __vdso_time_fallback(long *tm)
1509931033bSDmitry Chagin {
1519931033bSDmitry Chagin 	int res;
1529931033bSDmitry Chagin 
1539931033bSDmitry Chagin 	__asm__ __volatile__
1549931033bSDmitry Chagin 	(
1559931033bSDmitry Chagin 	    "int $0x80"
1569931033bSDmitry Chagin 	    : "=a"(res)
1579931033bSDmitry Chagin 	    : "a"(LINUX32_SYS_linux_time), "b"(tm)
1589931033bSDmitry Chagin 	    : "cc", "memory"
1599931033bSDmitry Chagin 	);
1609931033bSDmitry Chagin 	return (res);
1619931033bSDmitry Chagin }
1629931033bSDmitry Chagin 
1639931033bSDmitry Chagin #include <compat/linux/linux_vdso_gtod.inc>
164