This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: fix ia64 longjmp() to work from alternate signal-stack


Here is an updated (and working) patch.  If it looks OK, please apply.

	--david

----
./ChangeLog

2004-09-13  David Mosberger  <davidm@hpl.hp.com>

	* signal/Versions: Export __sigaltstack with GLIBC_PRIVATE.

./nptl/ChangeLog

2004-09-01  David Mosberger  <davidm@hpl.hp.com>

	* sysdeps/unix/sysv/linux/ia64/jmpbuf-unwind.h
	(__libc_unwind_longjmp): Delete macro and declare as function.
	* sysdeps/unix/sysv/linux/ia64/Makefile (sysdep_routines): Mention
	__ia64_longjmp, sigstack_longjmp, and __sigstack_longjmp for
	nptl directory.
	* sysdeps/unix/sysv/linux/ia64/__ia64_longjmp.S: New file.
	* sysdeps/unix/sysv/linux/ia64/__sigstack_longjmp.c: Likewise.
	* sysdeps/unix/sysv/linux/ia64/unwind_longjmp.c: Likewise.

Index: nptl/sysdeps/unix/sysv/linux/ia64/Makefile
--- /dev/null
+++ nptl/sysdeps/unix/sysv/linux/ia64/Makefile
@@ -0,0 +1,3 @@
+ifeq ($(subdir),nptl)
+libpthread-sysdep_routines += __ia64_longjmp unwind_longjmp __sigstack_longjmp
+endif
Index: nptl/sysdeps/unix/sysv/linux/ia64/__ia64_longjmp.S
--- /dev/null
+++ nptl/sysdeps/unix/sysv/linux/ia64/__ia64_longjmp.S
@@ -0,0 +1,156 @@
+/* Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
+   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <sysdep.h>
+#include <features.h>
+
+LEAF(__ia64_flush_rbs)
+	flushrs
+	mov r9 = ar.rsc		// 12 cyc latency
+	;;
+	mov r8 = ar.bsp		// 12 cyc latency
+	;;
+	and r16 = ~0x3, r9	// clear ar.rsc.mode
+	;;
+	mov ar.rsc = r16	// put RSE into enforced-lazy mode
+	;;
+	mov r10 = ar.rnat	// 5 cyc latency
+	ret
+END(__ia64_flush_rbs)
+
+
+#	define	pPos	p6	/* is rotate count positive? */
+#	define	pNeg	p7	/* is rotate count negative? */
+
+/* __ia64_longjmp(__jmp_buf buf, int val, long rnat, long rsc)  */
+
+
+LEAF(__ia64_longjmp)
+	alloc r8=ar.pfs,4,0,0,0
+	add r2=0x98,in0		// r2 <- &jmpbuf.orig_jmp_buf_addr
+	add r3=0x88,in0		// r3 <- &jmpbuf.ar_bsp
+	;;
+	ld8 r8=[r2]		// r8 <- orig_jmp_buf_addr
+	ld8 r23=[r3],8		// r23 <- jmpbuf.ar_bsp
+	mov r2=in0
+	;;
+	//
+	// Note: we need to redo the "flushrs" here even though it's
+	// already been done by __ia64_flush_rbs.  It is needed to
+	// ensure that ar.bspstore == ar.bsp.
+	//
+	flushrs			// flush dirty regs to backing store
+	ld8 r25=[r3]		// r25 <- jmpbuf.ar_unat
+	sub r8=r8,in0		// r8 <- &orig_jmpbuf - &jmpbuf
+	;;
+	add r3=8,in0		// r3 <- &jmpbuf.r1
+	extr.u r8=r8,3,6	// r8 <- (&orig_jmpbuf - &jmpbuf)/8 & 0x3f
+	;;
+	cmp.lt pNeg,pPos=r8,r0
+	;;
+(pPos)	mov r16=r8
+(pNeg)	add r16=64,r8
+(pPos)	sub r17=64,r8
+(pNeg)	sub r17=r0,r8
+	;;
+	shr.u r8=r25,r16
+	shl r9=r25,r17
+	;;
+	or r25=r8,r9
+	;;
+	mov ar.unat=r25 // setup ar.unat (NaT bits for r1, r4-r7, and r12)
+	;;
+	ld8.fill.nta sp=[r2],16	// r12 (sp)
+	ld8.fill.nta gp=[r3],16 // r1 (gp)
+	dep r11=-1,r23,3,6	// r11 <- ia64_rse_rnat_addr(jmpbuf.ar_bsp)
+	;;
+	ld8.nta r16=[r2],16		// caller's unat
+	ld8.nta r17=[r3],16		// fpsr
+	;;
+	ld8.fill.nta r4=[r2],16		// r4
+	ld8.fill.nta r5=[r3],16		// r5 (gp)
+	;;
+	ld8.fill.nta r6=[r2],16		// r6
+	ld8.fill.nta r7=[r3],16		// r7
+	;;
+	mov ar.unat=r16			// restore caller's unat
+	mov ar.fpsr=r17			// restore fpsr
+	;;
+	ld8.nta r16=[r2],16		// b0
+	ld8.nta r17=[r3],16		// b1
+	;;
+	mov ar.bspstore=r23	// restore ar.bspstore
+	ld8.nta r18=[r2],16		// b2
+	;;
+	mov ar.rnat=in2		// restore ar.rnat
+	ld8.nta r19=[r3],16		// b3
+	;;
+	ld8.nta r20=[r2],16		// b4
+	ld8.nta r21=[r3],16		// b5
+	;;
+	ld8.nta r11=[r2],16		// ar.pfs
+	ld8.nta r22=[r3],56		// ar.lc
+	;;
+	ld8.nta r24=[r2],32		// pr
+	mov ar.rsc=in3		// restore ar.rsc
+	mov b0=r16
+	;;
+	ldf.fill.nta f2=[r2],32
+	ldf.fill.nta f3=[r3],32
+	mov b1=r17
+	;;
+	ldf.fill.nta f4=[r2],32
+	ldf.fill.nta f5=[r3],32
+	mov b2=r18
+	;;
+	ldf.fill.nta f16=[r2],32
+	ldf.fill.nta f17=[r3],32
+	mov b3=r19
+	;;
+	ldf.fill.nta f18=[r2],32
+	ldf.fill.nta f19=[r3],32
+	mov b4=r20
+	;;
+	ldf.fill.nta f20=[r2],32
+	ldf.fill.nta f21=[r3],32
+	mov b5=r21
+	;;
+	ldf.fill.nta f22=[r2],32
+	ldf.fill.nta f23=[r3],32
+	mov ar.lc=r22
+	;;
+	ldf.fill.nta f24=[r2],32
+	ldf.fill.nta f25=[r3],32
+	cmp.eq p8,p9=0,in1
+	;;
+	ldf.fill.nta f26=[r2],32
+	ldf.fill.nta f27=[r3],32
+	mov ar.pfs=r11
+	;;
+	ldf.fill.nta f28=[r2],32
+	ldf.fill.nta f29=[r3],32
+(p8)	mov r8=1
+	;;
+	ldf.fill.nta f30=[r2]
+	ldf.fill.nta f31=[r3]
+(p9)	mov r8=in1
+
+	invala			// virt. -> phys. regnum mapping may change
+	mov pr=r24,-1
+	ret
+END(__ia64_longjmp)
Index: nptl/sysdeps/unix/sysv/linux/ia64/__sigstack_longjmp.c
--- /dev/null
+++ nptl/sysdeps/unix/sysv/linux/ia64/__sigstack_longjmp.c
@@ -0,0 +1,166 @@
+/* Copyright (C) 2004 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+/* The public __longjmp() implementation is limited to jumping within
+   the same stack.  That is, in general it is not possible to use this
+   __longjmp() implementation to cross from one stack to another.
+   In constrast, the __sigstack_longjmp() implemented here allows
+   crossing from the alternate signal stack to the normal stack
+   as a special case.  */
+
+#include <assert.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <sys/rse.h>
+
+#define JB_SP	0
+#define JB_BSP	17
+
+struct rbs_flush_values
+  {
+    unsigned long bsp;
+    unsigned long rsc;
+    unsigned long rnat;
+  };
+
+extern struct rbs_flush_values __ia64_flush_rbs (void);
+extern void __ia64_longjmp (__jmp_buf buf, int val, long rnat, long rsc)
+     __attribute__ ((__noreturn__));
+
+static void
+copy_rbs (unsigned long *dst, unsigned long *dst_end, unsigned long dst_rnat,
+	  unsigned long *src, unsigned long *src_end,
+	  unsigned long current_rnat)
+{
+  unsigned long dst_slot, src_rnat = 0, src_slot, *src_rnat_addr, nat_bit;
+  int first_time = 1;
+
+  while (dst < dst_end)
+    {
+      dst_slot = ia64_rse_slot_num (dst);
+      if (dst_slot == 63)
+	{
+	  *dst++ = dst_rnat;
+	  dst_rnat = 0;
+	}
+      else
+	{
+	  /* read source value, including NaT bit: */
+	  src_slot = ia64_rse_slot_num (src);
+	  if (src_slot == 63)
+	    {
+	      /* skip src RNaT slot */
+	      ++src;
+	      src_slot = 0;
+	    }
+	  if (first_time || src_slot == 0)
+	    {
+	      first_time = 0;
+	      src_rnat_addr = ia64_rse_rnat_addr (src);
+	      if (src_rnat_addr < src_end)
+		src_rnat = *src_rnat_addr;
+	      else
+		src_rnat = current_rnat;
+	    }
+	  nat_bit = (src_rnat >> src_slot) & 1;
+
+	  assert (src < src_end);
+
+	  *dst++ = *src++;
+	  if (nat_bit)
+	    dst_rnat |=  (1UL << dst_slot);
+	  else
+	    dst_rnat &= ~(1UL << dst_slot);
+	}
+    }
+  dst_slot = ia64_rse_slot_num (dst);
+  if (dst_slot > 0)
+    *ia64_rse_rnat_addr (dst) = dst_rnat;
+}
+
+void
+__sigstack_longjmp (__jmp_buf buf, int val)
+{
+  unsigned long *rbs_base, *bsp, *bspstore, *jb_bsp, jb_sp, ss_sp;
+  unsigned long ndirty, rnat, load_rnat, *jb_rnat_addr;
+  struct sigcontext *sc;
+  stack_t stk;
+  struct rbs_flush_values c;
+
+  /* put RSE into enforced-lazy mode and return current bsp/rsc/rnat: */
+  c = __ia64_flush_rbs ();
+
+  jb_sp  = ((unsigned long *)  buf)[JB_SP];
+  jb_bsp = ((unsigned long **) buf)[JB_BSP];
+
+  __sigaltstack (NULL, &stk);
+
+  ss_sp = (unsigned long) stk.ss_sp;
+  jb_rnat_addr = ia64_rse_rnat_addr (jb_bsp);
+
+  if ((stk.ss_flags & SS_ONSTACK) == 0 || jb_sp - ss_sp < stk.ss_size)
+    /* Normal non-stack-crossing longjmp; if the RNaT slot for the bsp
+       saved in the jump-buffer is the same as the one for the current
+       BSP, use the current AR.RNAT value, otherwise, load it from the
+       jump-buffer's RNaT-slot.  */
+    load_rnat = (ia64_rse_rnat_addr ((unsigned long *) c.bsp) != jb_rnat_addr);
+  else
+    {
+      /* If we are on the alternate signal-stack and the jump-buffer
+	 lies outside the signal-stack, we may need to copy back the
+	 dirty partition which was torn off and saved on the
+	 signal-stack when the signal was delivered.
+
+	 Caveat: we assume that the top of the alternate signal-stack
+		 stores the sigcontext structure of the signal that
+		 caused the switch to the signal-stack.	 This should
+		 be a fairly safe assumption but the kernel _could_
+		 do things differently.. */
+      sc = ((struct sigcontext *) ((ss_sp + stk.ss_size) & -16) - 1);
+
+      /* As a sanity-check, verify that the register-backing-store base
+	 of the alternate signal-stack is where we expect it.  */
+      rbs_base = (unsigned long *)
+	((ss_sp + sizeof (long) - 1) & -sizeof (long));
+
+      assert ((unsigned long) rbs_base == sc->sc_rbs_base);
+
+      ndirty = ia64_rse_num_regs (rbs_base, rbs_base + (sc->sc_loadrs >> 19));
+      bsp = (unsigned long *) sc->sc_ar_bsp;
+      bspstore = ia64_rse_skip_regs (bsp, -ndirty);
+
+      if (bspstore < jb_bsp)
+	/* AR.BSPSTORE at the time of the signal was below the value
+	   of AR.BSP saved in the jump-buffer => copy the missing
+	   portion from the torn off dirty partition which got saved
+	   on the alternate signal-stack.  */
+	copy_rbs (bspstore, jb_bsp, sc->sc_ar_rnat,
+		  rbs_base, (unsigned long *) c.bsp, c.rnat);
+
+      load_rnat = 1;
+    }
+  if (load_rnat)
+    rnat = *jb_rnat_addr;
+  else
+    rnat = c.rnat;
+  __ia64_longjmp (buf, val, rnat, c.rsc);
+}
Index: nptl/sysdeps/unix/sysv/linux/ia64/jmpbuf-unwind.h
--- nptl/sysdeps/unix/sysv/linux/ia64/jmpbuf-unwind.h
+++ nptl/sysdeps/unix/sysv/linux/ia64/jmpbuf-unwind.h
@@ -32,5 +32,8 @@
 #define _JMPBUF_UNWINDS_ADJ(_jmpbuf, _address, _adj) \
   ((uintptr_t)(_address) - (_adj) < (uintptr_t)(((long *)_jmpbuf)[0]) - (_adj))
 
-/* We use the normal lobngjmp for unwinding.  */
-#define __libc_unwind_longjmp(buf, val) __libc_longjmp (buf, val)
+/* We use a longjmp() which can cross from the alternate signal-stack
+   to the normal stack.  */
+extern void __libc_unwind_longjmp (sigjmp_buf env, int val)
+          __attribute__ ((noreturn));
+hidden_proto (__libc_unwind_longjmp)
Index: nptl/sysdeps/unix/sysv/linux/ia64/unwind_longjmp.c
--- /dev/null
+++ nptl/sysdeps/unix/sysv/linux/ia64/unwind_longjmp.c
@@ -0,0 +1,44 @@
+/* Copyright (C) 1991,92,94,95,97,98,2000,02,04 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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <stddef.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <pthreadP.h>
+#include <jmpbuf-unwind.h>
+
+extern void __sigstack_longjmp (__jmp_buf, int)
+     __attribute__ ((noreturn));
+
+/* Like __libc_siglongjmp(), but safe for crossing from alternate
+   signal stack to normal stack.  Needed by NPTL.  */
+void
+__libc_unwind_longjmp (sigjmp_buf env, int val)
+{
+  /* Perform any cleanups needed by the frames being unwound.  */
+  __pthread_cleanup_upto (env->__jmpbuf, CURRENT_STACK_FRAME);
+
+  if (env[0].__mask_was_saved)
+    /* Restore the saved signal mask.  */
+    (void) __sigprocmask (SIG_SETMASK, &env[0].__saved_mask,
+			  (sigset_t *) NULL);
+
+  /* Call the machine-dependent function to restore machine state.  */
+  __sigstack_longjmp (env[0].__jmpbuf, val ?: 1);
+}
+hidden_def (__libc_unwind_longjmp)
Index: signal/Versions
--- signal/Versions
+++ signal/Versions
@@ -49,4 +49,7 @@
     # Needed to provide a pointer to the XPG sigpause function.
     __xpg_sigpause;
   }
+  GLIBC_PRIVATE {
+    __sigaltstack;
+  }
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]