1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/elf.h> 29 #include <sys/errno.h> 30 #include <sys/stdarg.h> 31 #include <sys/stddef.h> 32 #include <sys/time.h> 33 #define _KERNEL 34 #include <sys/vdso.h> 35 #undef _KERNEL 36 37 #include <limits.h> 38 #include <stdbool.h> 39 40 #include <machine/atomic.h> 41 #include <machine/cpufunc.h> 42 43 #include <amd64/linux/linux.h> 44 #include <amd64/linux/linux_syscall.h> 45 #include <compat/linux/linux_errno.h> 46 #include <compat/linux/linux_time.h> 47 48 /* The kernel fixup this at vDSO install */ 49 uintptr_t *kern_timekeep_base = NULL; 50 uint32_t kern_tsc_selector = 0; 51 uint32_t kern_cpu_selector = 0; 52 53 #include <x86/linux/linux_vdso_gettc_x86.inc> 54 #include <x86/linux/linux_vdso_getcpu_x86.inc> 55 56 /* for debug purpose */ 57 static int 58 write(int fd, const void *buf, size_t size) 59 { 60 int res; 61 62 __asm__ __volatile__ 63 ( 64 "syscall" 65 : "=a"(res) 66 : "a"(LINUX_SYS_linux_write), "D"(fd), "S"(buf), "d"(size) 67 : "cc", "rcx", "r11", "memory" 68 ); 69 return (res); 70 } 71 72 static int 73 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts) 74 { 75 int res; 76 77 __asm__ __volatile__ 78 ( 79 "syscall" 80 : "=a"(res) 81 : "a"(LINUX_SYS_linux_clock_gettime), "D"(clock_id), "S"(ts) 82 : "cc", "rcx", "r11", "memory" 83 ); 84 return (res); 85 } 86 87 static int 88 __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz) 89 { 90 int res; 91 92 __asm__ __volatile__ 93 ( 94 "syscall" 95 : "=a"(res) 96 : "a"(LINUX_SYS_gettimeofday), "D"(tv), "S"(tz) 97 : "cc", "rcx", "r11", "memory" 98 ); 99 return (res); 100 } 101 102 static int 103 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts) 104 { 105 int res; 106 107 __asm__ __volatile__ 108 ( 109 "syscall" 110 : "=a"(res) 111 : "a"(LINUX_SYS_linux_clock_getres), "D"(clock_id), "S"(ts) 112 : "cc", "rcx", "r11", "memory" 113 ); 114 return (res); 115 } 116 117 static int 118 __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache) 119 { 120 int res; 121 122 __asm__ __volatile__ 123 ( 124 "syscall" 125 : "=a"(res) 126 : "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache) 127 : "cc", "rcx", "r11", "memory" 128 ); 129 return (res); 130 } 131 132 static int 133 __vdso_time_fallback(long *tm) 134 { 135 int res; 136 137 __asm__ __volatile__ 138 ( 139 "syscall" 140 : "=a"(res) 141 : "a"(LINUX_SYS_linux_time), "D"(tm) 142 : "cc", "rcx", "r11", "memory" 143 ); 144 return (res); 145 } 146 147 #include <compat/linux/linux_vdso_gtod.inc> 148