runtime: fix context clobbering on AIX

On AIX 64-bits, r13 is a pointer to thread data.
setcontext() overwrites r13 with the value saved by getcontext().
So, when a goroutine is scheduled on a new thread, r13 will point
to the old thread data after calling setcontext().

Code courtesy of Damien Bergamini.

Issue golang/go#19200

Change-Id: I579f002834b467cfbac888feea4951452881fb08
Reviewed-on: https://go-review.googlesource.com/41854
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/libgo/runtime/proc.c b/libgo/runtime/proc.c
index fb40797..55a8d23 100644
--- a/libgo/runtime/proc.c
+++ b/libgo/runtime/proc.c
@@ -152,18 +152,17 @@
 
 # elif defined(_AIX)
 
-// TODO: configure detects TLS clobbering, so initcontext and fixcontext are
-// required to complete the build, but more investigation is necessary to
-// understand the clobbering issue and fix it.
-
 static inline void
 initcontext(void)
 {
 }
 
 static inline void
-fixcontext(ucontext_t* c __attribute__ ((unused)))
+fixcontext(ucontext_t* c)
 {
+	// Thread pointer is in r13, per 64-bit ABI.
+	if (sizeof (c->uc_mcontext.jmp_context.gpr[13]) == 8)
+		asm ("std 13, %0" : "=m"(c->uc_mcontext.jmp_context.gpr[13]));
 }
 
 # else