diff mbox series

[v2,8/8] linux: Consolidate gettimeofday implementation

Message ID 20190905205620.4646-8-adhemerval.zanella@linaro.org
State New
Headers show
Series [v2,1/8] Remove PREPARE_VERSION and PREPARE_VERSION_KNOW | expand

Commit Message

Adhemerval Zanella Sept. 5, 2019, 8:56 p.m. UTC
Changes from previous version:

  - Update due powerpc64 fixes.

--

The gettimeofday syscall has currently 3 possible implementations:

  1. Wire-up __NR_gettimeofday with a VDSO implementation accessed
     directly through IFUNC (aarch64, x86, and powerpc*).

  2. vDSO gettimeofday (arm, mips*, riscv*, s390, and sparc).

  3. Wire-up __NR_gettimeofday (alpha, hppa, ia64, m68k, microblaze,
     nios, sh4).

This patch consolidates all implementation on Linux generic
sysdeps/unix/sysv/linux/gettimeofday.c.  To simplify the code, some changes
are made:

   * The wire-up with vDSO implementation may route external calls directly
     to vDSO through IFUNC.  To enable it the architecture need to
     explicit define USE_GETTIMEOFDAY_VSYSCALL_IFUNC.

   * Also, powerpc and x86 tries to route internal time usages to
     IFUNC mechanism, which is problematic since powerpc32 and i686 does
     not really support it.  Instead, all internal calls are routed to
     a default internal symbol which in turn calls INTERNAL_VSYSCALL.

   * Static linking also uses the fallback mechanism which calls
     INTERNAL_VSYSCALL, so vDSO is used for this case as well.

The generic implementation issues a syscall as default, calls the
vDSO if the architecture defines HAVE_GETTIMEOFDAY_VSYSCALL, and
route the external calls to iFUNC if the architecture also defines
USE_GETTIMEOFDAY_VSYSCALL_IFUNC.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
powerpc64-linux-gnu, powerpc64le-linux-gnu, and aarch64-linux-gnu.

	* sysdeps/unix/sysv/linux/aarch64/gettimeofday.c: Remove file.
	* sysdeps/unix/sysv/linux/i386/gettimeofday.c: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/gettimeofday.c: Likewise.
	* sysdeps/unix/sysv/linux/x86/gettimeofday.c: Likewise.
	* sysdeps/unix/sysv/linux/aarch64/sysdep.h
	(USE_GETTIMEOFDAY_VSYSCALL_IFUNC): Define.
	* sysdeps/unix/sysv/linux/i386/sysdep.h
	(USE_GETTIMEOFDAY_VSYSCALL_IFUNC): Likewise.
	* sysdeps/unix/sysv/linux/x86_64/sysdep.h
	(USE_GETTIMEOFDAY_VSYSCALL_IFUNC): Likewise.
	* sysdeps/unix/sysv/linux/powerpc/sysdep.h
	(USE_GETTIMEOFDAY_VSYSCALL_IFUNC): Likewise.
	* sysdeps/unix/sysv/linux/gettimeofday.c (gettimeofday): Handle all
	possible Linux implementations (wire-up syscall, vDSO implementation,
	and iFUNC).
        (gettimeofday_syscall): New function.
---
 .../unix/sysv/linux/aarch64/gettimeofday.c    | 58 -------------
 sysdeps/unix/sysv/linux/aarch64/sysdep.h      |  1 +
 sysdeps/unix/sysv/linux/gettimeofday.c        | 51 +++++++++--
 sysdeps/unix/sysv/linux/i386/gettimeofday.c   | 35 --------
 sysdeps/unix/sysv/linux/i386/sysdep.h         |  1 +
 .../unix/sysv/linux/powerpc/gettimeofday.c    | 84 -------------------
 sysdeps/unix/sysv/linux/powerpc/sysdep.h      |  1 +
 sysdeps/unix/sysv/linux/x86/gettimeofday.c    | 63 --------------
 sysdeps/unix/sysv/linux/x86_64/sysdep.h       |  1 +
 9 files changed, 48 insertions(+), 247 deletions(-)
 delete mode 100644 sysdeps/unix/sysv/linux/aarch64/gettimeofday.c
 delete mode 100644 sysdeps/unix/sysv/linux/i386/gettimeofday.c
 delete mode 100644 sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
 delete mode 100644 sysdeps/unix/sysv/linux/x86/gettimeofday.c

-- 
2.17.1
diff mbox series

Patch

diff --git a/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c b/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c
deleted file mode 100644
index 9499a11fda..0000000000
--- a/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c
+++ /dev/null
@@ -1,58 +0,0 @@ 
-/* Copyright (C) 2018-2019 Free Software Foundation, Inc.
-
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License as
-   published by the Free Software Foundation; either version 2.1 of the
-   License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
-
-/* Get the current time of day and timezone information,
-   putting it into *tv and *tz.  If tz is null, *tz is not filled.
-   Returns 0 on success, -1 on errors.  */
-
-#include <sys/time.h>
-
-#ifdef SHARED
-
-# include <dl-vdso.h>
-# include <sysdep-vdso.h>
-
-/* Used as a fallback in the ifunc resolver if VDSO is not available
-   and for libc.so internal __gettimeofday calls.  */
-
-static int
-__gettimeofday_vsyscall (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
-}
-
-# define INIT_ARCH()
-libc_ifunc_hidden (__gettimeofday, __gettimeofday,
-		   (get_vdso_symbol ("__vdso_gettimeofday")
-		    ?: __gettimeofday_vsyscall))
-libc_hidden_def (__gettimeofday)
-
-#else
-
-# include <sysdep.h>
-int
-__gettimeofday (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
-}
-libc_hidden_def (__gettimeofday)
-
-#endif
-
-weak_alias (__gettimeofday, gettimeofday)
-libc_hidden_weak (gettimeofday)
diff --git a/sysdeps/unix/sysv/linux/aarch64/sysdep.h b/sysdeps/unix/sysv/linux/aarch64/sysdep.h
index d57f7232e2..526741ed82 100644
--- a/sysdeps/unix/sysv/linux/aarch64/sysdep.h
+++ b/sysdeps/unix/sysv/linux/aarch64/sysdep.h
@@ -163,6 +163,7 @@ 
 # define HAVE_CLOCK_GETRES_VSYSCALL	"__kernel_clock_getres"
 # define HAVE_CLOCK_GETTIME_VSYSCALL	"__kernel_clock_gettime"
 # define HAVE_GETTIMEOFDAY_VSYSCALL	"__kernel_gettimeofday"
+# define USE_GETTIMEOFDAY_VSYSCALL_IFUNC	1
 
 /* Previously AArch64 used the generic version without the libc_hidden_def
    which lead in a non existent __send symbol in libc.so.  */
diff --git a/sysdeps/unix/sysv/linux/gettimeofday.c b/sysdeps/unix/sysv/linux/gettimeofday.c
index a74f03825a..833e97ca7d 100644
--- a/sysdeps/unix/sysv/linux/gettimeofday.c
+++ b/sysdeps/unix/sysv/linux/gettimeofday.c
@@ -16,24 +16,61 @@ 
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <sys/time.h>
+/* Currently we have 3 possible gettimeofday implementations, which is also
+   selected in the order:
+
+   1. Wire-up __NR_gettimeofday with a vDSO implementation (currently aarch64,
+      x86, and powerpc).
+
+   2. vDSO gettimeofday (arm, mips*, riscv*, s390, and sparc).
+
+   3. Wire-up __NR_gettimeofday (alpha, hppa, ia64, m68k, microblaze,
+      nios, sh4).  */
 
+#define __gettimeofday __redirect___gettimeofday
+#include <sys/time.h>
 #undef __gettimeofday
 
+#include <sysdep.h>
 #ifdef HAVE_GETTIMEOFDAY_VSYSCALL
 # define HAVE_VSYSCALL
 #endif
 #include <sysdep-vdso.h>
+#include <libc-vdso.h>
+
+/* Get the current time of day and timezone information, putting it into *tv
+   and *tz.  If tz is null, *tz is not filled. Returns 0 on success, -1 on
+   errors.  */
+static int
+gettimeofday_syscall (struct timeval *tv, struct timezone *tz)
+{
+  return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
+}
+
+#if HAVE_IFUNC && defined USE_GETTIMEOFDAY_VSYSCALL_IFUNC
+/* Route externals calls direct to vDSO and static and internal calls to
+   fallback implementation (which also might call the vDSO).  */
+# ifdef SHARED
+#  undef INIT_ARCH
+#  define INIT_ARCH() \
+  void *vdso_gettimeofday = get_vdso_symbol (HAVE_GETTIMEOFDAY_VSYSCALL)
+
+libc_ifunc_redirected (__redirect___gettimeofday, __gettimeofday,
+		       vdso_gettimeofday
+		       ? VDSO_IFUNC_RET (vdso_gettimeofday)
+		       : (void *) gettimeofday_syscall);
+libc_hidden_def_redir (gettimeofday_syscall, __gettimeofday)
+#  else
+strong_alias (gettimeofday_syscall, __gettimeofday)
+#  endif /* SHARED  */
+#else
 
-/* Get the current time of day and timezone information,
-   putting it into *tv and *tz.  If tz is null, *tz is not filled.
-   Returns 0 on success, -1 on errors.  */
 int
 __gettimeofday (struct timeval *tv, struct timezone *tz)
 {
-  return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
+  return gettimeofday_syscall (tv, tz);
 }
-libc_hidden_def (__gettimeofday)
+libc_hidden_def_redir (__gettimeofday, __gettimeofday)
+#endif
 weak_alias (__gettimeofday, gettimeofday)
 libc_hidden_weak (gettimeofday)
diff --git a/sysdeps/unix/sysv/linux/i386/gettimeofday.c b/sysdeps/unix/sysv/linux/i386/gettimeofday.c
deleted file mode 100644
index 185450ece6..0000000000
--- a/sysdeps/unix/sysv/linux/i386/gettimeofday.c
+++ /dev/null
@@ -1,35 +0,0 @@ 
-/* gettimeofday - get the time.  Linux/i386 version.
-   Copyright (C) 2015-2019 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
-
-#ifdef SHARED
-# define __gettimeofday __redirect___gettimeofday
-#endif
-
-#include <sys/time.h>
-
-#ifdef SHARED
-# undef __gettimeofday
-# define __gettimeofday_type __redirect___gettimeofday
-
-# undef libc_hidden_def
-# define libc_hidden_def(name) \
-  __hidden_ver1 (__gettimeofday_syscall, __GI___gettimeofday, \
-	       __gettimeofday_syscall);
-#endif
-
-#include <sysdeps/unix/sysv/linux/x86/gettimeofday.c>
diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
index 4f79f25989..076e4393d3 100644
--- a/sysdeps/unix/sysv/linux/i386/sysdep.h
+++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
@@ -315,6 +315,7 @@  struct libc_do_syscall_args
 /* List of system calls which are supported as vsyscalls.  */
 # define HAVE_CLOCK_GETTIME_VSYSCALL    "__vdso_clock_gettime"
 # define HAVE_GETTIMEOFDAY_VSYSCALL     "__vdso_gettimeofday"
+# define USE_GETTIMEOFDAY_VSYSCALL_IFUNC	1
 # define HAVE_TIME_VSYSCALL             "__vdso_time"
 # define USE_TIME_VSYSCALL_IFUNC	1
 
diff --git a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
deleted file mode 100644
index e046594d40..0000000000
--- a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
+++ /dev/null
@@ -1,84 +0,0 @@ 
-/* Copyright (C) 2005-2019 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
-
-#if defined SHARED && !defined __powerpc64__
-# define __gettimeofday __redirect___gettimeofday
-#else
-# define __redirect___gettimeofday __gettimeofday
-#endif
-
-#include <sys/time.h>
-
-#ifdef SHARED
-
-# include <dl-vdso.h>
-# include <libc-vdso.h>
-# include <dl-machine.h>
-
-# ifndef __powerpc64__
-#  undef __gettimeofday
-
-int
-__gettimeofday_vsyscall (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
-}
-
-/* __GI___gettimeofday is defined as hidden and for ppc32 it enables the
-   compiler make a local call (symbol@local) for internal GLIBC usage. It
-   means the PLT won't be used and the ifunc resolver will be called directly.
-   For ppc64 a call to a function in another translation unit might use a
-   different toc pointer thus disallowing direct branchess and making internal
-   ifuncs calls safe.  */
-#  undef libc_hidden_def
-#  define libc_hidden_def(name)					\
-  __hidden_ver1 (__gettimeofday_vsyscall, __GI___gettimeofday,	\
-	       __gettimeofday_vsyscall);
-
-# endif /* !__powerpc64__  */
-
-static int
-__gettimeofday_syscall (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
-}
-
-# define INIT_ARCH() \
-  void *vdso_gettimeofday = get_vdso_symbol (HAVE_GETTIMEOFDAY_VSYSCALL)
-  
-/* If the vDSO is not available we fall back syscall.  */
-libc_ifunc_hidden (__redirect___gettimeofday, __gettimeofday,
-		   vdso_gettimeofday
-		   ? VDSO_IFUNC_RET (vdso_gettimeofday)
-		   : (void *) __gettimeofday_syscall);
-libc_hidden_def (__gettimeofday)
-
-#else
-
-# include <sysdep.h>
-# include <errno.h>
-
-int
-__gettimeofday (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
-}
-libc_hidden_def (__gettimeofday)
-
-#endif
-weak_alias (__gettimeofday, gettimeofday)
-libc_hidden_weak (gettimeofday)
diff --git a/sysdeps/unix/sysv/linux/powerpc/sysdep.h b/sysdeps/unix/sysv/linux/powerpc/sysdep.h
index 084b6525ca..743eb67557 100644
--- a/sysdeps/unix/sysv/linux/powerpc/sysdep.h
+++ b/sysdeps/unix/sysv/linux/powerpc/sysdep.h
@@ -26,6 +26,7 @@ 
 #define HAVE_TIME_VSYSCALL		"__kernel_time"
 #define USE_TIME_VSYSCALL_IFUNC		1
 #define HAVE_GETTIMEOFDAY_VSYSCALL	"__kernel_gettimeofday"
+#define USE_GETTIMEOFDAY_VSYSCALL_IFUNC	1
 #define HAVE_GET_TBFREQ                 "__kernel_get_tbfreq"
 
 #if defined(__PPC64__) || defined(__powerpc64__)
diff --git a/sysdeps/unix/sysv/linux/x86/gettimeofday.c b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
deleted file mode 100644
index 3ceab633e5..0000000000
--- a/sysdeps/unix/sysv/linux/x86/gettimeofday.c
+++ /dev/null
@@ -1,63 +0,0 @@ 
-/* gettimeofday - get the time.  Linux/x86 version.
-   Copyright (C) 2015-2019 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
-
-#include <sys/time.h>
-
-#ifdef SHARED
-
-# include <dl-vdso.h>
-# include <errno.h>
-# include <sysdep-vdso.h>
-# include <sysdep-vdso.h>
-
-static int
-__gettimeofday_syscall (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
-}
-
-# ifndef __gettimeofday_type
-/* The i386 gettimeofday.c includes this file with a defined
-   __gettimeofday_type macro.  For x86_64 we have to define it to __gettimeofday
-   as the internal symbol is the ifunc'ed one.  */
-#  define __gettimeofday_type __gettimeofday
-# endif
-
-# define INIT_ARCH()
-
-/* If the vDSO is not available we fall back to syscall.  */
-libc_ifunc_hidden (__gettimeofday_type, __gettimeofday,
-		   (get_vdso_symbol ("__vdso_gettimeofday")
-		    ?: __gettimeofday_syscall));
-libc_hidden_def (__gettimeofday)
-
-#else
-
-# include <sysdep.h>
-# include <errno.h>
-
-int
-__gettimeofday (struct timeval *tv, struct timezone *tz)
-{
-  return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
-}
-libc_hidden_def (__gettimeofday)
-
-#endif
-weak_alias (__gettimeofday, gettimeofday)
-libc_hidden_weak (gettimeofday)
diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
index c64fbd1e26..4ea9c4fa31 100644
--- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
+++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
@@ -376,6 +376,7 @@ 
 /* List of system calls which are supported as vsyscalls.  */
 # define HAVE_CLOCK_GETTIME_VSYSCALL    "__vdso_clock_gettime"
 # define HAVE_GETTIMEOFDAY_VSYSCALL     "__vdso_gettimeofday"
+# define USE_GETTIMEOFDAY_VSYSCALL_IFUNC	1
 # define HAVE_TIME_VSYSCALL		"__vdso_time"
 # define USE_TIME_VSYSCALL_IFUNC	1
 # define HAVE_GETCPU_VSYSCALL		"__vdso_getcpu"