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 29 #include <sys/cdefs.h> 30 #include <sys/elf.h> 31 #include <sys/errno.h> 32 #include <sys/proc.h> 33 #include <sys/stddef.h> 34 #define _KERNEL 35 #include <sys/vdso.h> 36 #undef _KERNEL 37 #include <stdbool.h> 38 39 #include <i386/include/atomic.h> 40 #include <i386/include/cpufunc.h> 41 #include <i386/include/stdarg.h> 42 43 #include <amd64/linux32/linux.h> 44 #include <amd64/linux32/linux32_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 static int 57 write(int fd, const void *buf, size_t size) 58 { 59 int res; 60 61 __asm__ __volatile__ 62 ( 63 "int $0x80" 64 : "=a"(res) 65 : "a"(LINUX32_SYS_write), "b"(fd), "c"(buf), "d"(size) 66 : "cc", "memory" 67 ); 68 return (res); 69 } 70 71 static int 72 __vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts) 73 { 74 int res; 75 76 __asm__ __volatile__ 77 ( 78 "int $0x80" 79 : "=a"(res) 80 : "a"(LINUX32_SYS_linux_clock_gettime), "b"(clock_id), "c"(ts) 81 : "cc", "memory" 82 ); 83 return (res); 84 } 85 86 static int 87 __vdso_clock_gettime64_fallback(clockid_t clock_id, struct l_timespec64 *ts) 88 { 89 int res; 90 91 __asm__ __volatile__ 92 ( 93 "int $0x80" 94 : "=a"(res) 95 : "a"(LINUX32_SYS_linux_clock_gettime64), "b"(clock_id), "c"(ts) 96 : "cc", "memory" 97 ); 98 return (res); 99 } 100 101 static int 102 __vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz) 103 { 104 int res; 105 106 __asm__ __volatile__ 107 ( 108 "int $0x80" 109 : "=a"(res) 110 : "a"(LINUX32_SYS_linux_gettimeofday), "b"(tv), "c"(tz) 111 : "cc", "memory" 112 ); 113 return (res); 114 } 115 116 static int 117 __vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts) 118 { 119 int res; 120 121 __asm__ __volatile__ 122 ( 123 "int $0x80" 124 : "=a"(res) 125 : "a"(LINUX32_SYS_linux_clock_getres), "b"(clock_id), "c"(ts) 126 : "cc", "memory" 127 ); 128 return (res); 129 } 130 131 static int 132 __vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache) 133 { 134 int res; 135 136 __asm__ __volatile__ 137 ( 138 "int $0x80" 139 : "=a"(res) 140 : "a"(LINUX32_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache) 141 : "cc", "memory" 142 ); 143 return (res); 144 } 145 146 static int 147 __vdso_time_fallback(long *tm) 148 { 149 int res; 150 151 __asm__ __volatile__ 152 ( 153 "int $0x80" 154 : "=a"(res) 155 : "a"(LINUX32_SYS_linux_time), "b"(tm) 156 : "cc", "memory" 157 ); 158 return (res); 159 } 160 161 #include <compat/linux/linux_vdso_gtod.inc> 162