9931033bbf
The vDSO (virtual dynamic shared object) is a small shared library that the kernel maps R/O into the address space of all Linux processes on image activation. The vDSO is a fully formed ELF image, shared by all processes with the same ABI, has no process private data. The primary purpose of the vDSO: - non-executable stack, signal trampolines not copied to the stack; - signal trampolines unwind, mandatory for the NPTL; - to avoid contex-switch overhead frequently used system calls can be implemented in the vDSO: for now gettimeofday, clock_gettime. The first two have been implemented, so add the implementation of system calls. System calls implemenation based on a native timekeeping code with some limitations: - ifunc can't be used, as vDSO r/o mapped to the process VA and rtld can't relocate symbols; - reading HPET memory is not implemented for now (TODO). In case on any error vDSO system calls fallback to the kernel system calls. For unimplemented vDSO system calls added prototypes which call corresponding kernel system call. Tested by: trasz (arm64) Differential revision: https://reviews.freebsd.org/D30900 MFC after: 2 weeks
147 lines
3.5 KiB
C
147 lines
3.5 KiB
C
/*-
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
*
|
|
* Copyright (c) 2021 Dmitry Chagin <dchagin@FreeBSD.org>
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
#include <sys/elf.h>
|
|
#include <sys/errno.h>
|
|
#include <sys/proc.h>
|
|
#include <sys/stddef.h>
|
|
#define _KERNEL
|
|
#include <sys/vdso.h>
|
|
#undef _KERNEL
|
|
#include <stdbool.h>
|
|
#include <strings.h>
|
|
|
|
#include <machine/atomic.h>
|
|
#include <machine/stdarg.h>
|
|
|
|
#include <amd64/linux/linux.h>
|
|
#include <amd64/linux/linux_syscall.h>
|
|
#include <compat/linux/linux_errno.h>
|
|
#include <compat/linux/linux_timer.h>
|
|
|
|
/* The kernel fixup this at vDSO install */
|
|
uintptr_t *kern_timekeep_base = NULL;
|
|
uint32_t kern_tsc_selector = 0;
|
|
|
|
#include <x86/linux/linux_vdso_gettc_x86.inc>
|
|
|
|
/* for debug purpose */
|
|
static int
|
|
write(int fd, const void *buf, size_t size)
|
|
{
|
|
int res;
|
|
|
|
__asm__ __volatile__
|
|
(
|
|
"syscall"
|
|
: "=a"(res)
|
|
: "a"(LINUX_SYS_write), "D"(fd), "S"(buf), "d"(size)
|
|
: "cc", "rcx", "r11", "memory"
|
|
);
|
|
return (res);
|
|
}
|
|
|
|
static int
|
|
__vdso_clock_gettime_fallback(clockid_t clock_id, struct l_timespec *ts)
|
|
{
|
|
int res;
|
|
|
|
__asm__ __volatile__
|
|
(
|
|
"syscall"
|
|
: "=a"(res)
|
|
: "a"(LINUX_SYS_linux_clock_gettime), "D"(clock_id), "S"(ts)
|
|
: "cc", "rcx", "r11", "memory"
|
|
);
|
|
return (res);
|
|
}
|
|
|
|
static int
|
|
__vdso_gettimeofday_fallback(l_timeval *tv, struct timezone *tz)
|
|
{
|
|
int res;
|
|
|
|
__asm__ __volatile__
|
|
(
|
|
"syscall"
|
|
: "=a"(res)
|
|
: "a"(LINUX_SYS_gettimeofday), "D"(tv), "S"(tz)
|
|
: "cc", "rcx", "r11", "memory"
|
|
);
|
|
return (res);
|
|
}
|
|
|
|
static int
|
|
__vdso_clock_getres_fallback(clockid_t clock_id, struct l_timespec *ts)
|
|
{
|
|
int res;
|
|
|
|
__asm__ __volatile__
|
|
(
|
|
"syscall"
|
|
: "=a"(res)
|
|
: "a"(LINUX_SYS_linux_clock_getres), "D"(clock_id), "S"(ts)
|
|
: "cc", "rcx", "r11", "memory"
|
|
);
|
|
return (res);
|
|
}
|
|
|
|
static int
|
|
__vdso_getcpu_fallback(uint32_t *cpu, uint32_t *node, void *cache)
|
|
{
|
|
int res;
|
|
|
|
__asm__ __volatile__
|
|
(
|
|
"syscall"
|
|
: "=a"(res)
|
|
: "a"(LINUX_SYS_linux_getcpu), "D"(cpu), "S"(node), "d"(cache)
|
|
: "cc", "rcx", "r11", "memory"
|
|
);
|
|
return (res);
|
|
}
|
|
|
|
static int
|
|
__vdso_time_fallback(long *tm)
|
|
{
|
|
int res;
|
|
|
|
__asm__ __volatile__
|
|
(
|
|
"syscall"
|
|
: "=a"(res)
|
|
: "a"(LINUX_SYS_linux_time), "D"(tm)
|
|
: "cc", "rcx", "r11", "memory"
|
|
);
|
|
return (res);
|
|
}
|
|
|
|
#include <compat/linux/linux_vdso_gtod.inc>
|