bca718
commit b9eb92ab05204df772eb4929eccd018637c9f3e9
bca718
Author: H.J. Lu <hjl.tools@gmail.com>
bca718
Date:   Wed Oct 21 14:44:23 2015 -0700
bca718
bca718
    Add Prefer_MAP_32BIT_EXEC to map executable pages with MAP_32BIT
bca718
    
bca718
    According to Silvermont software optimization guide, for 64-bit
bca718
    applications, branch prediction performance can be negatively impacted
bca718
    when the target of a branch is more than 4GB away from the branch.  Add
bca718
    the Prefer_MAP_32BIT_EXEC bit so that mmap will try to map executable
bca718
    pages with MAP_32BIT first.  NB: MAP_32BIT will map to lower 2GB, not
bca718
    lower 4GB, address.  Prefer_MAP_32BIT_EXEC reduces bits available for
bca718
    address space layout randomization (ASLR), which is always disabled for
bca718
    SUID programs and can only be enabled by setting environment variable,
bca718
    LD_PREFER_MAP_32BIT_EXEC.
bca718
    
bca718
    On Fedora 23, this patch speeds up GCC 5 testsuite by 3% on Silvermont.
bca718
    
bca718
        [BZ #19367]
bca718
        * sysdeps/unix/sysv/linux/wordsize-64/mmap.c: New file.
bca718
        * sysdeps/unix/sysv/linux/x86_64/64/dl-librecon.h: Likewise.
bca718
        * sysdeps/unix/sysv/linux/x86_64/64/mmap.c: Likewise.
bca718
        * sysdeps/x86/cpu-features.h (bit_Prefer_MAP_32BIT_EXEC): New.
bca718
        (index_Prefer_MAP_32BIT_EXEC): Likewise.
bca718
bca718
Index: glibc-2.17-c758a686/sysdeps/unix/sysv/linux/wordsize-64/mmap.c
bca718
===================================================================
bca718
--- /dev/null
bca718
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/wordsize-64/mmap.c
bca718
@@ -0,0 +1,40 @@
bca718
+/* Linux mmap system call.  64-bit version.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public License as
bca718
+   published by the Free Software Foundation; either version 2.1 of the
bca718
+   License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#include <sys/types.h>
bca718
+#include <sys/mman.h>
bca718
+#include <errno.h>
bca718
+#include <sysdep.h>
bca718
+
bca718
+/* An architecture may override this.  */
bca718
+#ifndef MMAP_PREPARE
bca718
+# define MMAP_PREPARE(addr, len, prot, flags, fd, offset)
bca718
+#endif
bca718
+
bca718
+__ptr_t
bca718
+__mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
bca718
+{
bca718
+  MMAP_PREPARE (addr, len, prot, flags, fd, offset);
bca718
+  return (__ptr_t) INLINE_SYSCALL (mmap, 6, addr, len, prot, flags,
bca718
+				   fd, offset);
bca718
+}
bca718
+
bca718
+weak_alias (__mmap, mmap)
bca718
+weak_alias (__mmap, mmap64)
bca718
+weak_alias (__mmap, __mmap64)
bca718
Index: glibc-2.17-c758a686/sysdeps/unix/sysv/linux/x86_64/64/dl-librecon.h
bca718
===================================================================
bca718
--- /dev/null
bca718
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/x86_64/64/dl-librecon.h
bca718
@@ -0,0 +1,44 @@
bca718
+/* Optional code to distinguish library flavours.  x86-64 version.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#ifndef _DL_LIBRECON_H
bca718
+
bca718
+#include <sysdeps/unix/sysv/linux/dl-librecon.h>
bca718
+
bca718
+/* Recognizing extra environment variables.  For 64-bit applications,
bca718
+   branch prediction performance may be negatively impacted when the
bca718
+   target of a branch is more than 4GB away from the branch.  Add the
bca718
+   Prefer_MAP_32BIT_EXEC bit so that mmap will try to map executable
bca718
+   pages with MAP_32BIT first.  NB: MAP_32BIT will map to lower 2GB,
bca718
+   not lower 4GB, address.  Prefer_MAP_32BIT_EXEC reduces bits available
bca718
+   for address space layout randomization (ASLR).  Prefer_MAP_32BIT_EXEC
bca718
+   is always disabled for SUID programs and can be enabled by setting
bca718
+   environment variable, LD_PREFER_MAP_32BIT_EXEC.  */
bca718
+#define EXTRA_LD_ENVVARS \
bca718
+  case 21:							      \
bca718
+    if (memcmp (envline, "PREFER_MAP_32BIT_EXEC", 21) == 0)	      \
bca718
+      GLRO(dl_x86_cpu_features).feature[index_Prefer_MAP_32BIT_EXEC]  \
bca718
+	= bit_Prefer_MAP_32BIT_EXEC;				      \
bca718
+    break;
bca718
+
bca718
+/* Extra unsecure variables.  The names are all stuffed in a single
bca718
+   string which means they have to be terminated with a '\0' explicitly.  */
bca718
+#define EXTRA_UNSECURE_ENVVARS \
bca718
+  "LD_PREFER_MAP_32BIT_EXEC\0"
bca718
+
bca718
+#endif /* dl-librecon.h */
bca718
Index: glibc-2.17-c758a686/sysdeps/unix/sysv/linux/x86_64/64/mmap.c
bca718
===================================================================
bca718
--- /dev/null
bca718
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/x86_64/64/mmap.c
bca718
@@ -0,0 +1,37 @@
bca718
+/* Linux mmap system call.  x86-64 version.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public License as
bca718
+   published by the Free Software Foundation; either version 2.1 of the
bca718
+   License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#include <ldsodefs.h>
bca718
+
bca718
+/* If the Prefer_MAP_32BIT_EXEC bit is set, try to map executable pages
bca718
+   with MAP_32BIT first.  */
bca718
+#define MMAP_PREPARE(addr, len, prot, flags, fd, offset)		\
bca718
+  if ((addr) == NULL							\
bca718
+      && ((prot) & PROT_EXEC) != 0					\
bca718
+      && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC))			\
bca718
+    {									\
bca718
+      __ptr_t ret = (__ptr_t) INLINE_SYSCALL (mmap, 6, (addr), (len),	\
bca718
+					      (prot),			\
bca718
+					      (flags) | MAP_32BIT,	\
bca718
+					      (fd), (offset));		\
bca718
+      if (ret != MAP_FAILED)						\
bca718
+	return ret;							\
bca718
+    }
bca718
+
bca718
+#include <sysdeps/unix/sysv/linux/wordsize-64/mmap.c>
bca718
Index: glibc-2.17-c758a686/sysdeps/x86/cpu-features.h
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86/cpu-features.h
bca718
+++ glibc-2.17-c758a686/sysdeps/x86/cpu-features.h
bca718
@@ -32,6 +32,7 @@
bca718
 #define bit_AVX_Fast_Unaligned_Load	(1 << 11)
bca718
 #define bit_AVX512F_Usable		(1 << 12)
bca718
 #define bit_AVX512DQ_Usable		(1 << 13)
bca718
+#define bit_Prefer_MAP_32BIT_EXEC	(1 << 16)
bca718
 
bca718
 /* CPUID Feature flags.  */
bca718
 
bca718
@@ -91,6 +92,7 @@
bca718
 # define index_AVX_Fast_Unaligned_Load	FEATURE_INDEX_1*FEATURE_SIZE
bca718
 # define index_AVX512F_Usable		FEATURE_INDEX_1*FEATURE_SIZE
bca718
 # define index_AVX512DQ_Usable		FEATURE_INDEX_1*FEATURE_SIZE
bca718
+# define index_Prefer_MAP_32BIT_EXEC	FEATURE_INDEX_1*FEATURE_SIZE
bca718
 
bca718
 # if defined (_LIBC) && !IS_IN (nonlib)
bca718
 #  ifdef __x86_64__
bca718
@@ -267,6 +269,7 @@ extern const struct cpu_features *__get_
bca718
 # define index_AVX_Fast_Unaligned_Load	FEATURE_INDEX_1
bca718
 # define index_AVX512F_Usable		FEATURE_INDEX_1
bca718
 # define index_AVX512DQ_Usable		FEATURE_INDEX_1
bca718
+# define index_Prefer_MAP_32BIT_EXEC	FEATURE_INDEX_1
bca718
 
bca718
 #endif	/* !__ASSEMBLER__ */
bca718