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