8ae002
commit 8d2c0a593bdefd220be0822fb70de6b8d3bfd39d
8ae002
Author: Adhemerval Zanella <azanella@linux.vnet.ibm.com>
8ae002
Date:   Fri Nov 7 12:25:32 2014 -0500
8ae002
8ae002
    powerpc: Add the lock elision using HTM
8ae002
    
8ae002
    This patch adds support for lock elision using ISA 2.07 hardware
8ae002
    transactional memory instructions for pthread_mutex primitives.
8ae002
    Similar to s390 version, the for elision logic defined in
8ae002
    'force-elision.h' is only enabled if ENABLE_LOCK_ELISION is defined.
8ae002
    
8ae002
    Also, the lock elision code should be able to be built even with
8ae002
    a compiler that does not provide HTM support with builtins.
8ae002
    However I have noted the performance is sub-optimal due scheduling
8ae002
    pressures.
8ae002
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-conf.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-conf.c
8ae002
@@ -0,0 +1,80 @@
8ae002
+/* elision-conf.c: Lock elision tunable parameters.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include "config.h"
8ae002
+#include <pthreadP.h>
8ae002
+#include <elision-conf.h>
8ae002
+#include <unistd.h>
8ae002
+#include <dl-procinfo.h>
8ae002
+
8ae002
+/* Reasonable initial tuning values, may be revised in the future.
8ae002
+   This is a conservative initial value.  */
8ae002
+
8ae002
+struct elision_config __elision_aconf =
8ae002
+  {
8ae002
+    /* How many times to use a non-transactional lock after a transactional
8ae002
+       failure has occurred because the lock is already acquired.  Expressed
8ae002
+       in number of lock acquisition attempts.  */
8ae002
+    .skip_lock_busy = 3,
8ae002
+    /* How often to not attempt to use elision if a transaction aborted due
8ae002
+       to reasons other than other threads' memory accesses.  Expressed in
8ae002
+       number of lock acquisition attempts.  */
8ae002
+    .skip_lock_internal_abort = 3,
8ae002
+    /* How often to not attempt to use elision if a lock used up all retries
8ae002
+       without success.  Expressed in number of lock acquisition attempts.  */
8ae002
+    .skip_lock_out_of_tbegin_retries = 3,
8ae002
+    /* How often we retry using elision if there is chance for the transaction
8ae002
+       to finish execution (e.g., it wasn't aborted due to the lock being
8ae002
+       already acquired.  */
8ae002
+    .try_tbegin = 3,
8ae002
+    /* Same as SKIP_LOCK_INTERNAL_ABORT but for trylock.  */
8ae002
+    .skip_trylock_internal_abort = 3,
8ae002
+  };
8ae002
+
8ae002
+/* Force elision for all new locks.  This is used to decide whether existing
8ae002
+   DEFAULT locks should be automatically use elision in pthread_mutex_lock().
8ae002
+   Disabled for suid programs.  Only used when elision is available.  */
8ae002
+
8ae002
+int __pthread_force_elision attribute_hidden;
8ae002
+
8ae002
+/* Initialize elision.  */
8ae002
+
8ae002
+static void
8ae002
+elision_init (int argc __attribute__ ((unused)),
8ae002
+	      char **argv  __attribute__ ((unused)),
8ae002
+	      char **environ)
8ae002
+{
8ae002
+#ifdef ENABLE_LOCK_ELISION
8ae002
+  int elision_available = (GLRO (dl_hwcap2) & PPC_FEATURE2_HAS_HTM) ? 1 : 0;
8ae002
+  __pthread_force_elision = __libc_enable_secure ? 0 : elision_available;
8ae002
+#endif
8ae002
+}
8ae002
+
8ae002
+#ifdef SHARED
8ae002
+# define INIT_SECTION ".init_array"
8ae002
+# define MAYBE_CONST
8ae002
+#else
8ae002
+# define INIT_SECTION ".preinit_array"
8ae002
+# define MAYBE_CONST const
8ae002
+#endif
8ae002
+
8ae002
+void (*MAYBE_CONST __pthread_init_array []) (int, char **, char **)
8ae002
+  __attribute__ ((section (INIT_SECTION), aligned (sizeof (void *)))) =
8ae002
+{
8ae002
+  &elision_init
8ae002
+};
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-conf.h
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-conf.h
8ae002
@@ -0,0 +1,42 @@
8ae002
+/* elision-conf.h: Lock elision tunable parameters.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#ifndef _ELISION_CONF_H
8ae002
+#define _ELISION_CONF_H 1
8ae002
+
8ae002
+#include <pthread.h>
8ae002
+#include <time.h>
8ae002
+
8ae002
+/* Should make sure there is no false sharing on this.  */
8ae002
+struct elision_config
8ae002
+{
8ae002
+  int skip_lock_busy;
8ae002
+  int skip_lock_internal_abort;
8ae002
+  int skip_lock_out_of_tbegin_retries;
8ae002
+  int try_tbegin;
8ae002
+  int skip_trylock_internal_abort;
8ae002
+} __attribute__ ((__aligned__ (128)));
8ae002
+
8ae002
+extern struct elision_config __elision_aconf attribute_hidden;
8ae002
+
8ae002
+extern int __pthread_force_elision attribute_hidden;
8ae002
+
8ae002
+/* Tell the test suite to test elision for this architecture.  */
8ae002
+#define HAVE_ELISION 1
8ae002
+
8ae002
+#endif
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-lock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-lock.c
8ae002
@@ -0,0 +1,107 @@
8ae002
+/* elision-lock.c: Elided pthread mutex lock.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <stdio.h>
8ae002
+#include <pthread.h>
8ae002
+#include <pthreadP.h>
8ae002
+#include <lowlevellock.h>
8ae002
+#include <elision-conf.h>
8ae002
+#include "htm.h"
8ae002
+
8ae002
+/* PowerISA 2.0.7 Section B.5.5 defines isync to be insufficient as a
8ae002
+   barrier in acquire mechanism for HTM operations, a strong 'sync' is
8ae002
+   required.  */
8ae002
+#undef __arch_compare_and_exchange_val_32_acq
8ae002
+#define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval)           \
8ae002
+  ({                                                                          \
8ae002
+      __typeof (*(mem)) __tmp;                                                \
8ae002
+      __typeof (mem)  __memp = (mem);                                         \
8ae002
+      __asm __volatile (                                                      \
8ae002
+                        "1:     lwarx   %0,0,%1" MUTEX_HINT_ACQ "\n"          \
8ae002
+                        "       cmpw    %0,%2\n"                              \
8ae002
+                        "       bne     2f\n"                                 \
8ae002
+                        "       stwcx.  %3,0,%1\n"                            \
8ae002
+                        "       bne-    1b\n"                                 \
8ae002
+                        "2:     sync"                                         \
8ae002
+                        : "=&r" (__tmp)                                       \
8ae002
+                        : "b" (__memp), "r" (oldval), "r" (newval)            \
8ae002
+                        : "cr0", "memory");                                   \
8ae002
+      __tmp;                                                                  \
8ae002
+  })
8ae002
+
8ae002
+#if !defined(LLL_LOCK) && !defined(EXTRAARG)
8ae002
+/* Make sure the configuration code is always linked in for static
8ae002
+   libraries.  */
8ae002
+#include "elision-conf.c"
8ae002
+#endif
8ae002
+
8ae002
+#ifndef EXTRAARG
8ae002
+# define EXTRAARG
8ae002
+#endif
8ae002
+#ifndef LLL_LOCK
8ae002
+# define LLL_LOCK(a,b) lll_lock(a,b), 0
8ae002
+#endif
8ae002
+
8ae002
+#define aconf __elision_aconf
8ae002
+
8ae002
+/* Adaptive lock using transactions.
8ae002
+   By default the lock region is run as a transaction, and when it
8ae002
+   aborts or the lock is busy the lock adapts itself.  */
8ae002
+
8ae002
+int
8ae002
+__lll_lock_elision (int *lock, short *adapt_count, EXTRAARG int pshared)
8ae002
+{
8ae002
+  if (*adapt_count > 0)
8ae002
+    {
8ae002
+      (*adapt_count)--;
8ae002
+      goto use_lock;
8ae002
+    }
8ae002
+
8ae002
+  int try_begin = aconf.try_tbegin;
8ae002
+  while (1)
8ae002
+    {
8ae002
+      if (__builtin_tbegin (0))
8ae002
+	{
8ae002
+	  if (*lock == 0)
8ae002
+	    return 0;
8ae002
+	  /* Lock was busy.  Fall back to normal locking.  */
8ae002
+	  __builtin_tabort (_ABORT_LOCK_BUSY);
8ae002
+	}
8ae002
+      else
8ae002
+	{
8ae002
+	  /* A persistent failure indicates that a retry will probably
8ae002
+	     result in another failure.  Use normal locking now and
8ae002
+	     for the next couple of calls.  */
8ae002
+	  if (try_begin-- <= 0
8ae002
+	      || _TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
8ae002
+	    {
8ae002
+	      if (aconf.skip_lock_internal_abort > 0)
8ae002
+		*adapt_count = aconf.skip_lock_internal_abort;
8ae002
+	      goto use_lock;
8ae002
+	    }
8ae002
+	  /* Same logic as above, but for for a number of temporary failures
8ae002
+	     in a row.  */
8ae002
+	  else if (aconf.skip_lock_out_of_tbegin_retries > 0
8ae002
+                   && aconf.try_tbegin > 0)
8ae002
+	    *adapt_count = aconf.skip_lock_out_of_tbegin_retries;
8ae002
+	}
8ae002
+     }
8ae002
+
8ae002
+use_lock:
8ae002
+  return LLL_LOCK ((*lock), pshared);
8ae002
+}
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-timed.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-timed.c
8ae002
@@ -0,0 +1,28 @@
8ae002
+/* elision-timed.c: Lock elision timed lock.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <time.h>
8ae002
+#include <elision-conf.h>
8ae002
+#include <lowlevellock.h>
8ae002
+
8ae002
+#define __lll_lock_elision __lll_timedlock_elision
8ae002
+#define EXTRAARG const struct timespec *t,
8ae002
+#undef LLL_LOCK
8ae002
+#define LLL_LOCK(a, b) lll_timedlock(a, t, b)
8ae002
+
8ae002
+#include "elision-lock.c"
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-trylock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-trylock.c
8ae002
@@ -0,0 +1,68 @@
8ae002
+/* elision-trylock.c: Lock eliding trylock for pthreads.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <pthread.h>
8ae002
+#include <pthreadP.h>
8ae002
+#include <lowlevellock.h>
8ae002
+#include <elision-conf.h>
8ae002
+#include "htm.h"
8ae002
+
8ae002
+#define aconf __elision_aconf
8ae002
+
8ae002
+/* Try to elide a futex trylock.  FUTEX is the futex variable.  ADAPT_COUNT is
8ae002
+   the adaptation counter in the mutex.  */
8ae002
+
8ae002
+int
8ae002
+__lll_trylock_elision (int *futex, short *adapt_count)
8ae002
+{
8ae002
+  /* Implement POSIX semantics by forbiding nesting elided trylocks.  */
8ae002
+  __builtin_tabort (_ABORT_NESTED_TRYLOCK);
8ae002
+
8ae002
+  /* Only try a transaction if it's worth it.  */
8ae002
+  if (*adapt_count > 0)
8ae002
+    {
8ae002
+      (*adapt_count)--;
8ae002
+      goto use_lock;
8ae002
+    }
8ae002
+
8ae002
+  if (__builtin_tbegin (0))
8ae002
+    {
8ae002
+      if (*futex == 0)
8ae002
+	return 0;
8ae002
+
8ae002
+      /* Lock was busy.  Fall back to normal locking.  */
8ae002
+      __builtin_tabort (_ABORT_LOCK_BUSY);
8ae002
+    }
8ae002
+  else
8ae002
+    {
8ae002
+      if (_TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
8ae002
+	{
8ae002
+	  /* A persistent failure indicates that a retry will probably
8ae002
+	     result in another failure.  Use normal locking now and
8ae002
+	     for the next couple of calls.  */
8ae002
+	  if (aconf.skip_trylock_internal_abort > 0)
8ae002
+	    *adapt_count = aconf.skip_trylock_internal_abort;
8ae002
+	}
8ae002
+
8ae002
+	if (aconf.skip_lock_busy > 0)
8ae002
+	  *adapt_count = aconf.skip_lock_busy;
8ae002
+    }
8ae002
+
8ae002
+use_lock:
8ae002
+  return lll_trylock (*futex);
8ae002
+}
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-unlock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/elision-unlock.c
8ae002
@@ -0,0 +1,32 @@
8ae002
+/* elision-unlock.c: Commit an elided pthread lock.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include "pthreadP.h"
8ae002
+#include <lowlevellock.h>
8ae002
+#include "htm.h"
8ae002
+
8ae002
+int
8ae002
+__lll_unlock_elision(int *lock, int pshared)
8ae002
+{
8ae002
+  /* When the lock was free we're in a transaction.  */
8ae002
+  if (*lock == 0)
8ae002
+    __builtin_tend (0);
8ae002
+  else
8ae002
+    lll_unlock ((*lock), pshared);
8ae002
+  return 0;
8ae002
+}
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/force-elision.h
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/force-elision.h
8ae002
@@ -0,0 +1,28 @@
8ae002
+/* force-elision.h: Automatic enabling of elision for mutexes
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#ifdef ENABLE_LOCK_ELISION
8ae002
+/* Automatically enable elision for existing user lock kinds.  */
8ae002
+#define FORCE_ELISION(m, s)						\
8ae002
+  if (__pthread_force_elision						\
8ae002
+      && (m->__data.__kind & PTHREAD_MUTEX_ELISION_FLAGS_NP) == 0)	\
8ae002
+    {									\
8ae002
+      mutex->__data.__kind |= PTHREAD_MUTEX_ELISION_NP;			\
8ae002
+      s;								\
8ae002
+    }
8ae002
+#endif
8ae002
Index: glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/htm.h
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/sysdeps/unix/sysv/linux/powerpc/htm.h
8ae002
@@ -0,0 +1,138 @@
8ae002
+/* Shared HTM header.  Emulate transactional execution facility intrinsics for
8ae002
+   compilers and assemblers that do not support the intrinsics and instructions
8ae002
+   yet.
8ae002
+
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#ifndef _HTM_H
8ae002
+#define _HTM_H 1
8ae002
+
8ae002
+#ifdef __ASSEMBLER__
8ae002
+
8ae002
+/* tbegin.  */
8ae002
+.macro TBEGIN
8ae002
+	.long 0x7c00051d
8ae002
+.endm
8ae002
+
8ae002
+/* tend. 0  */
8ae002
+.macro TEND
8ae002
+	.long 0x7c00055d
8ae002
+.endm
8ae002
+
8ae002
+/* tabort. code  */
8ae002
+.macro TABORT code
8ae002
+	.byte 0x7c
8ae002
+	.byte \code
8ae002
+	.byte 0x07
8ae002
+	.byte 0x1d
8ae002
+.endm
8ae002
+
8ae002
+/*"TEXASR - Transaction EXception And Summary Register"
8ae002
+   mfspr %dst,130  */
8ae002
+.macro TEXASR dst
8ae002
+	mfspr \dst,130
8ae002
+.endm
8ae002
+
8ae002
+#else
8ae002
+
8ae002
+#include <endian.h>
8ae002
+
8ae002
+/* Official HTM intrinsics interface matching GCC, but works
8ae002
+   on older GCC compatible compilers and binutils.
8ae002
+   We should somehow detect if the compiler supports it, because
8ae002
+   it may be able to generate slightly better code.  */
8ae002
+
8ae002
+#define TBEGIN ".long 0x7c00051d"
8ae002
+#define TEND   ".long 0x7c00055d"
8ae002
+#if __BYTE_ORDER == __LITTLE_ENDIAN
8ae002
+# define TABORT ".byte 0x1d,0x07,%1,0x1d"
8ae002
+#else
8ae002
+# define TABORT ".byte 0x7c,%1,0x07,0x1d"
8ae002
+#endif
8ae002
+
8ae002
+#define __force_inline        inline __attribute__((__always_inline__))
8ae002
+
8ae002
+#ifndef __HTM__
8ae002
+
8ae002
+#define _TEXASRU_EXTRACT_BITS(TEXASR,BITNUM,SIZE) \
8ae002
+  (((TEXASR) >> (31-(BITNUM))) & ((1<<(SIZE))-1))
8ae002
+#define _TEXASRU_FAILURE_PERSISTENT(TEXASRU) \
8ae002
+  _TEXASRU_EXTRACT_BITS(TEXASRU, 7, 1)
8ae002
+
8ae002
+#define _tbegin()			\
8ae002
+  ({ unsigned int __ret;		\
8ae002
+     asm volatile (			\
8ae002
+       TBEGIN "\t\n"			\
8ae002
+       "mfcr   %0\t\n"			\
8ae002
+       "rlwinm %0,%0,3,1\t\n"		\
8ae002
+       "xori %0,%0,1\t\n"		\
8ae002
+       : "=r" (__ret) :			\
8ae002
+       : "cr0", "memory");		\
8ae002
+     __ret;				\
8ae002
+  })
8ae002
+
8ae002
+#define _tend()				\
8ae002
+  ({ unsigned int __ret;		\
8ae002
+     asm volatile (			\
8ae002
+       TEND "\t\n"			\
8ae002
+       "mfcr   %0\t\n"			\
8ae002
+       "rlwinm %0,%0,3,1\t\n"		\
8ae002
+       "xori %0,%0,1\t\n"		\
8ae002
+       : "=r" (__ret) :			\
8ae002
+       : "cr0", "memory");		\
8ae002
+     __ret;				\
8ae002
+  })
8ae002
+
8ae002
+#define _tabort(__code)			\
8ae002
+  ({ unsigned int __ret;		\
8ae002
+     asm volatile (			\
8ae002
+       TABORT "\t\n"			\
8ae002
+       "mfcr   %0\t\n"			\
8ae002
+       "rlwinm %0,%0,3,1\t\n"		\
8ae002
+       "xori %0,%0,1\t\n"		\
8ae002
+       : "=r" (__ret) : "r" (__code)	\
8ae002
+       : "cr0", "memory");		\
8ae002
+     __ret;				\
8ae002
+  })
8ae002
+
8ae002
+#define _texasru()			\
8ae002
+  ({ unsigned long __ret;		\
8ae002
+     asm volatile (			\
8ae002
+       "mfspr %0,131\t\n"		\
8ae002
+       : "=r" (__ret));			\
8ae002
+     __ret;				\
8ae002
+  })
8ae002
+
8ae002
+#define __builtin_tbegin(tdb)       _tbegin ()
8ae002
+#define __builtin_tend(nested)      _tend ()
8ae002
+#define __builtin_tabort(abortcode) _tabort (abortcode)
8ae002
+#define __builtin_get_texasru()     _texasru ()
8ae002
+
8ae002
+#else
8ae002
+# include <htmintrin.h>
8ae002
+#endif /* __HTM__  */
8ae002
+
8ae002
+#endif /* __ASSEMBLER__ */
8ae002
+
8ae002
+/* Definitions used for TEXASR Failure code (bits 0:6), they need to be even
8ae002
+   because tabort. always sets the first bit.  */
8ae002
+#define _ABORT_LOCK_BUSY       0x3f   /* Lock already used.  */
8ae002
+#define _ABORT_NESTED_TRYLOCK  0x3e   /* Write operation in trylock.  */
8ae002
+#define _ABORT_SYSCALL         0x3d   /* Syscall issued.  */
8ae002
+
8ae002
+#endif
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/lowlevellock.h
8ae002
===================================================================
8ae002
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/powerpc/lowlevellock.h
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/lowlevellock.h
8ae002
@@ -326,4 +326,28 @@ extern int __lll_timedwait_tid (int *, c
8ae002
     __res;								      \
8ae002
   })
8ae002
 
8ae002
+/* Transactional lock elision definitions.  */
8ae002
+extern int __lll_timedlock_elision
8ae002
+  (int *futex, short *adapt_count, const struct timespec *timeout, int private)
8ae002
+  attribute_hidden;
8ae002
+
8ae002
+#define lll_timedlock_elision(futex, adapt_count, timeout, private)	      \
8ae002
+  __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
8ae002
+
8ae002
+extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
8ae002
+  attribute_hidden;
8ae002
+
8ae002
+extern int __lll_unlock_elision(int *lock, int private)
8ae002
+  attribute_hidden;
8ae002
+
8ae002
+extern int __lll_trylock_elision(int *lock, short *adapt_count)
8ae002
+  attribute_hidden;
8ae002
+
8ae002
+#define lll_lock_elision(futex, adapt_count, private) \
8ae002
+  __lll_lock_elision (&(futex), &(adapt_count), private)
8ae002
+#define lll_unlock_elision(futex, private) \
8ae002
+  __lll_unlock_elision (&(futex), private)
8ae002
+#define lll_trylock_elision(futex, adapt_count) \
8ae002
+  __lll_trylock_elision (&(futex), &(adapt_count))
8ae002
+
8ae002
 #endif	/* lowlevellock.h */
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_cond_lock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_cond_lock.c
8ae002
@@ -0,0 +1,22 @@
8ae002
+/* Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+/* The cond lock is not actually elided yet, but we still need to handle
8ae002
+   already elided locks.  */
8ae002
+#include <elision-conf.h>
8ae002
+
8ae002
+#include "sysdeps/unix/sysv/linux/pthread_mutex_cond_lock.c"
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_lock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_lock.c
8ae002
@@ -0,0 +1,22 @@
8ae002
+/* Elided version of pthread_mutex_lock.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <elision-conf.h>
8ae002
+#include <force-elision.h>
8ae002
+
8ae002
+#include <nptl/pthread_mutex_lock.c>
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_timedlock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_timedlock.c
8ae002
@@ -0,0 +1,22 @@
8ae002
+/* Elided version of pthread_mutex_timedlock.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <elision-conf.h>
8ae002
+#include <force-elision.h>
8ae002
+
8ae002
+#include <nptl/pthread_mutex_timedlock.c>
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_trylock.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/pthread_mutex_trylock.c
8ae002
@@ -0,0 +1,22 @@
8ae002
+/* Elided version of pthread_mutex_trylock.
8ae002
+   Copyright (C) 2014 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <elision-conf.h>
8ae002
+#include <force-elision.h>
8ae002
+
8ae002
+#include <nptl/pthread_mutex_trylock.c>
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h
8ae002
===================================================================
8ae002
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h
8ae002
@@ -90,14 +90,25 @@ typedef union
8ae002
        binary compatibility.  */
8ae002
     int __kind;
8ae002
 #if __WORDSIZE == 64
8ae002
-    int __spins;
8ae002
+    short __spins;
8ae002
+    short __elision;
8ae002
     __pthread_list_t __list;
8ae002
 # define __PTHREAD_MUTEX_HAVE_PREV	1
8ae002
+# define __PTHREAD_SPINS             0, 0
8ae002
+# define __PTHREAD_MUTEX_HAVE_ELISION	1
8ae002
 #else
8ae002
     unsigned int __nusers;
8ae002
     __extension__ union
8ae002
     {
8ae002
-      int __spins;
8ae002
+      struct
8ae002
+      {
8ae002
+	short __espins;
8ae002
+	short __elision;
8ae002
+# define __spins __elision_data.__espins
8ae002
+# define __elision __elision_data.__elision
8ae002
+# define __PTHREAD_SPINS         { 0, 0 }
8ae002
+# define __PTHREAD_MUTEX_HAVE_ELISION	2
8ae002
+      } __elision_data;
8ae002
       __pthread_slist_t __list;
8ae002
     };
8ae002
 #endif
8ae002
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/Makefile
8ae002
===================================================================
8ae002
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/powerpc/Makefile
8ae002
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/powerpc/Makefile
8ae002
@@ -1,2 +1,4 @@
8ae002
 # pull in __syscall_error routine
8ae002
 libpthread-routines += sysdep
8ae002
+libpthread-sysdep_routines += elision-lock elision-unlock elision-timed \
8ae002
+			      elision-trylock