Path: blob/main/sys/amd64/linux/linux_vdso_gtod.c
103755 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021 Dmitry Chagin <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/elf.h>28#include <sys/errno.h>29#include <sys/proc.h>30#include <sys/stdarg.h>31#include <sys/stddef.h>32#define _KERNEL33#include <sys/vdso.h>34#undef _KERNEL35#include <stdbool.h>3637#include <machine/atomic.h>38#include <machine/cpufunc.h>3940#include <amd64/linux/linux.h>41#include <amd64/linux/linux_syscall.h>42#include <compat/linux/linux_errno.h>43#include <compat/linux/linux_time.h>4445/* The kernel fixup this at vDSO install */46uintptr_t *kern_timekeep_base = NULL;47uint32_t kern_tsc_selector = 0;48uint32_t kern_cpu_selector = 0;4950#include <x86/linux/linux_vdso_gettc_x86.inc>51#include <x86/linux/linux_vdso_getcpu_x86.inc>5253/* for debug purpose */54static int55write(int fd, const void *buf, size_t size)56{57int res;5859__asm__ __volatile__60(61"syscall"62: "=a"(res)63: "a"(LINUX_SYS_linux_write), "D"(fd), "S"(buf), "d"(size)64: "cc", "rcx", "r11", "memory"65);66return (res);67}6869static int70__vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)71{72int res;7374__asm__ __volatile__75(76"syscall"77: "=a"(res)78: "a"(LINUX_SYS_linux_clock_gettime), "D"(clock_id), "S"(ts)79: "cc", "rcx", "r11", "memory"80);81return (res);82}8384static int85__vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)86{87int res;8889__asm__ __volatile__90(91"syscall"92: "=a"(res)93: "a"(LINUX_SYS_gettimeofday), "D"(tv), "S"(tz)94: "cc", "rcx", "r11", "memory"95);96return (res);97}9899static int100__vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)101{102int res;103104__asm__ __volatile__105(106"syscall"107: "=a"(res)108: "a"(LINUX_SYS_linux_clock_getres), "D"(clock_id), "S"(ts)109: "cc", "rcx", "r11", "memory"110);111return (res);112}113114static int115__vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)116{117int res;118119__asm__ __volatile__120(121"syscall"122: "=a"(res)123: "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)124: "cc", "rcx", "r11", "memory"125);126return (res);127}128129static int130__vdso_time_fallback(long *tm)131{132int res;133134__asm__ __volatile__135(136"syscall"137: "=a"(res)138: "a"(LINUX_SYS_linux_time), "D"(tm)139: "cc", "rcx", "r11", "memory"140);141return (res);142}143144#include <compat/linux/linux_vdso_gtod.inc>145146147