public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [PATCH v2 8/8] linux: Consolidate gettimeofday implementation
Date: Thu, 05 Sep 2019 20:56:00 -0000	[thread overview]
Message-ID: <20190905205620.4646-8-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20190905205620.4646-1-adhemerval.zanella@linaro.org>

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

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"
-- 
2.17.1

  parent reply	other threads:[~2019-09-05 20:56 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05 20:56 [PATCH v2 1/8] Remove PREPARE_VERSION and PREPARE_VERSION_KNOW Adhemerval Zanella
2019-09-05 20:56 ` [PATCH 3/8] Enable vDSO on interposed malloc linked statically Adhemerval Zanella
2019-10-08 18:35   ` Adhemerval Zanella
2019-10-08 19:04     ` DJ Delorie
2019-09-05 20:56 ` [PATCH v2 5/8] mips: Consolidate INTERNAL_VSYSCALL_CALL Adhemerval Zanella
2019-09-17 14:24   ` Adhemerval Zanella
2019-09-05 20:56 ` [PATCH v2 2/8] Refactor vDSO initialization code Adhemerval Zanella
2019-09-17 14:23   ` Adhemerval Zanella
2019-09-24 11:41   ` [PATCH] Fix vDSO initialization on arm and mips Andreas Schwab
2019-09-24 11:52     ` Florian Weimer
2019-09-24 14:08     ` Adhemerval Zanella
2019-09-05 20:56 ` [PATCH v2 6/8] sparc64: Use linux generic time implementation Adhemerval Zanella
2019-09-17 14:26   ` Adhemerval Zanella
2019-09-05 20:56 ` [PATCH v2 4/8] powerpc: Simplify vsyscall internal macros Adhemerval Zanella
2019-09-05 22:04   ` cseo
2019-09-17 14:24     ` Adhemerval Zanella
2019-09-05 20:56 ` [PATCH v2 7/8] linux: Consolidate time implementation Adhemerval Zanella
2019-09-05 20:56 ` Adhemerval Zanella [this message]
2019-09-11 14:10 ` [PATCH v2 1/8] Remove PREPARE_VERSION and PREPARE_VERSION_KNOW Andreas Schwab
2019-09-17 14:23   ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190905205620.4646-8-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).