xref: /freebsd/sys/amd64/linux/linux_vdso_gtod.c (revision 5a6a4fb284b1a9c4b4105fd6ab8143356f3ef3f7)
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 #include <sys/cdefs.h>
299931033bSDmitry Chagin __FBSDID("$FreeBSD$");
309931033bSDmitry Chagin 
319931033bSDmitry Chagin #include <sys/elf.h>
329931033bSDmitry Chagin #include <sys/errno.h>
339931033bSDmitry Chagin #include <sys/proc.h>
349931033bSDmitry Chagin #include <sys/stddef.h>
359931033bSDmitry Chagin #define	_KERNEL
369931033bSDmitry Chagin #include <sys/vdso.h>
379931033bSDmitry Chagin #undef	_KERNEL
389931033bSDmitry Chagin #include <stdbool.h>
399931033bSDmitry Chagin 
409931033bSDmitry Chagin #include <machine/atomic.h>
41332eca05SDmitry Chagin #include <machine/cpufunc.h>
429931033bSDmitry Chagin #include <machine/stdarg.h>
439931033bSDmitry Chagin 
449931033bSDmitry Chagin #include <amd64/linux/linux.h>
459931033bSDmitry Chagin #include <amd64/linux/linux_syscall.h>
469931033bSDmitry Chagin #include <compat/linux/linux_errno.h>
479931033bSDmitry Chagin #include <compat/linux/linux_timer.h>
489931033bSDmitry Chagin 
499931033bSDmitry Chagin /* The kernel fixup this at vDSO install */
509931033bSDmitry Chagin uintptr_t *kern_timekeep_base = NULL;
519931033bSDmitry Chagin uint32_t kern_tsc_selector = 0;
52*5a6a4fb2SDmitry Chagin uint32_t kern_cpu_selector = 0;
539931033bSDmitry Chagin 
549931033bSDmitry Chagin #include <x86/linux/linux_vdso_gettc_x86.inc>
55*5a6a4fb2SDmitry Chagin #include <x86/linux/linux_vdso_getcpu_x86.inc>
569931033bSDmitry Chagin 
579931033bSDmitry Chagin /* for debug purpose */
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 	    "syscall"
669931033bSDmitry Chagin 	    : "=a"(res)
679931033bSDmitry Chagin 	    : "a"(LINUX_SYS_write), "D"(fd), "S"(buf), "d"(size)
689931033bSDmitry Chagin 	    : "cc", "rcx", "r11", "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 	    "syscall"
819931033bSDmitry Chagin 	    : "=a"(res)
829931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_clock_gettime), "D"(clock_id), "S"(ts)
839931033bSDmitry Chagin 	    : "cc", "rcx", "r11", "memory"
849931033bSDmitry Chagin 	);
859931033bSDmitry Chagin 	return (res);
869931033bSDmitry Chagin }
879931033bSDmitry Chagin 
889931033bSDmitry Chagin static int
899931033bSDmitry Chagin __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
909931033bSDmitry Chagin {
919931033bSDmitry Chagin 	int res;
929931033bSDmitry Chagin 
939931033bSDmitry Chagin 	__asm__ __volatile__
949931033bSDmitry Chagin 	(
959931033bSDmitry Chagin 	    "syscall"
969931033bSDmitry Chagin 	    : "=a"(res)
979931033bSDmitry Chagin 	    : "a"(LINUX_SYS_gettimeofday), "D"(tv), "S"(tz)
989931033bSDmitry Chagin 	    : "cc", "rcx", "r11", "memory"
999931033bSDmitry Chagin 	);
1009931033bSDmitry Chagin 	return (res);
1019931033bSDmitry Chagin }
1029931033bSDmitry Chagin 
1039931033bSDmitry Chagin static int
1049931033bSDmitry Chagin __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
1059931033bSDmitry Chagin {
1069931033bSDmitry Chagin 	int res;
1079931033bSDmitry Chagin 
1089931033bSDmitry Chagin 	__asm__ __volatile__
1099931033bSDmitry Chagin 	(
1109931033bSDmitry Chagin 	    "syscall"
1119931033bSDmitry Chagin 	    : "=a"(res)
1129931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_clock_getres), "D"(clock_id), "S"(ts)
1139931033bSDmitry Chagin 	    : "cc", "rcx", "r11", "memory"
1149931033bSDmitry Chagin 	);
1159931033bSDmitry Chagin 	return (res);
1169931033bSDmitry Chagin }
1179931033bSDmitry Chagin 
1189931033bSDmitry Chagin static int
1199931033bSDmitry Chagin __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
1209931033bSDmitry Chagin {
1219931033bSDmitry Chagin 	int res;
1229931033bSDmitry Chagin 
1239931033bSDmitry Chagin 	__asm__ __volatile__
1249931033bSDmitry Chagin 	(
1259931033bSDmitry Chagin 	    "syscall"
1269931033bSDmitry Chagin 	    : "=a"(res)
1279931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
1289931033bSDmitry Chagin 	    : "cc", "rcx", "r11", "memory"
1299931033bSDmitry Chagin 	);
1309931033bSDmitry Chagin 	return (res);
1319931033bSDmitry Chagin }
1329931033bSDmitry Chagin 
1339931033bSDmitry Chagin static int
1349931033bSDmitry Chagin __vdso_time_fallback(long *tm)
1359931033bSDmitry Chagin {
1369931033bSDmitry Chagin 	int res;
1379931033bSDmitry Chagin 
1389931033bSDmitry Chagin 	__asm__ __volatile__
1399931033bSDmitry Chagin 	(
1409931033bSDmitry Chagin 	    "syscall"
1419931033bSDmitry Chagin 	    : "=a"(res)
1429931033bSDmitry Chagin 	    : "a"(LINUX_SYS_linux_time), "D"(tm)
1439931033bSDmitry Chagin 	    : "cc", "rcx", "r11", "memory"
1449931033bSDmitry Chagin 	);
1459931033bSDmitry Chagin 	return (res);
1469931033bSDmitry Chagin }
1479931033bSDmitry Chagin 
1489931033bSDmitry Chagin #include <compat/linux/linux_vdso_gtod.inc>
149