Skip to contentSkip to conversation

setcontext/getcontext support

Linux-ia64 David Mosberger 2 participants 2 messages Mar 16, 2001

lore
Share & tools
lore
#0 lore raw

The patch below adds support for the following routines:

setcontext()
getcontext()
makecontext()
swapcontext()

These routines are intended to be used for *synchronous* context switching only. In particular, doing a setcontext() on the sigcontext structure passed to a signal handler will NOT work: you can only setcontext()/swapcontext() to a context that was created by getcontext()/makecontext().

Another limitation is that at most eight integer arguments can be passed with makecontext(). Actually, you can pass anything as long as those things are passed in register in0-in7.

The routines haven't been tested exhaustively, but the simple test program included below does work and I think they should be in reasonably good shape for use. Thanks to Uli for providing this test program.

Uli, if the patch looks OK, I'd appreciate it if you could check it in.

Enjoy,

	--david

2001-03-16 David Mosberger <[email protected]>

* sysdeps/unix/sysv/linux/ia64/bits/sigcontext.h (struct sigcontext): Drop hack that was needed for 2.1.1 kernel headers.

* sysdeps/unix/sysv/linux/ia64/ucontext_i.h: New file.

* sysdeps/unix/sysv/linux/ia64/Versions: Mention __start_context.

* sysdeps/unix/sysv/linux/ia64/sys/ucontext.h: Rewrite to make it overlay with kernel's "struct sigcontext".

* sysdeps/unix/sysv/linux/ia64/Makefile (sysdep_headers): Mention ia64/rse.h for "misc" subdir.
(sysdep_routines): Mention __start_context for "stdlib" subdir.

* sysdeps/unix/sysv/linux/ia64/ia64/rse.h: New file (based on kernel file of the same name).

* stdlib/Makefile (tests): Mention tst-setcontext.

* stdlib/tst-setcontext.c: New file (based on a sample program by Uli Drepper).

* sysdeps/unix/sysv/linux/ia64/setcontext.S: New file.
* sysdeps/unix/sysv/linux/ia64/getcontext.S: Ditto.
* sysdeps/unix/sysv/linux/ia64/__start_context.S: Ditto.
* sysdeps/unix/sysv/linux/ia64/makecontext.c: Ditto.

* sysdeps/unix/sysv/linux/ia64/__longjmp.S (__longjmp): Use extr.u instead of shift & and.

stdlib/Makefile +1 -1
diff --exclude CVS -urN libc/stdlib/Makefile libc-lia/stdlib/Makefile
@@ -59,7 +59,7 @@
 tests		:= tst-strtol tst-strtod testmb testrand testsort testdiv \
 		   test-canon test-canon2 tst-strtoll tst-environ	  \
 		   tst-xpg-basename tst-random tst-bsearch tst-limits	  \
-		   tst-rand48 bug-strtod
+		   tst-rand48 bug-strtod tst-setcontext
 
 
 # Several mpn functions from GNU MP are used by the strtod function.
stdlib/tst-setcontext.c +71 -0

71 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/Makefile +8 -0
diff --exclude CVS -urN libc/sysdeps/unix/sysv/linux/ia64/Makefile libc-lia/sysdeps/unix/sysv/linux/ia64/Makefile
@@ -1,3 +1,11 @@
+ifeq ($(subdir),misc)
+sysdep_headers += ia64/rse.h
+endif
+
+ifeq ($(subdir),stdlib)
+sysdep_routines += __start_context
+endif
+
 ifeq ($(subdir),csu)
 CFLAGS-initfini.s += -DWEAK_GMON_START
 endif
sysdeps/unix/sysv/linux/ia64/Versions +2 -0
diff --exclude CVS -urN libc/sysdeps/unix/sysv/linux/ia64/Versions libc-lia/sysdeps/unix/sysv/linux/ia64/Versions
@@ -18,6 +18,8 @@
 
     # linuxthreads
     __clone2;
+
+    __start_context;
   }
   GLIBC_2.2.2 {
     # w*
sysdeps/unix/sysv/linux/ia64/__longjmp.S +1 -3
diff --exclude CVS -urN libc/sysdeps/unix/sysv/linux/ia64/__longjmp.S libc-lia/sysdeps/unix/sysv/linux/ia64/__longjmp.S
@@ -62,9 +62,7 @@
 	sub r8=r8,in0		// r8 <- &orig_jmpbuf - &jmpbuf
 	;;
 	ld8 r25=[r2]		// r25 <- jmpbuf.ar_unat
-	shr r8=r8,3		// r8 <- (&orig_jmpbuf - &jmpbuf)/8
-	;;
-	and r8=0x3f,r8
+	extr.u r8=r8,3,6	// r8 <- (&orig_jmpbuf - &jmpbuf)/8 & 0x3f
 	;;
 	cmp.lt pNeg,pPos=r8,r0
 	mov r2=in0
sysdeps/unix/sysv/linux/ia64/__start_context.S +51 -0

51 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/bits/sigcontext.h +0 -6
diff --exclude CVS -urN libc/sysdeps/unix/sysv/linux/ia64/bits/sigcontext.h libc-lia/sysdeps/unix/sysv/linux/ia64/bits/sigcontext.h
@@ -21,12 +21,6 @@
 # error "Never use <bits/sigcontext.h> directly; include <signal.h> instead."
 #endif
 
-#ifndef sigcontext_struct
-/* Kernel headers before 2.1.1 define a struct sigcontext_struct, but
-   we need sigcontext.  */
-# define sigcontext_struct sigcontext
-#endif
-
 #include <asm/fpu.h>
 #include <bits/sigstack.h>
 
sysdeps/unix/sysv/linux/ia64/getcontext.S +153 -0

153 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/ia64/rse.h +78 -0

78 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/makecontext.c +92 -0

92 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/setcontext.S +149 -0

149 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/setjmp.S +1 -1
diff --exclude CVS -urN libc/sysdeps/unix/sysv/linux/ia64/setjmp.S libc-lia/sysdeps/unix/sysv/linux/ia64/setjmp.S
@@ -19,7 +19,7 @@
    The layout of the jmp_buf is as follows.  This is subject to change
    and user-code should never depend on the particular layout of
    jmp_buf!
-  
+
 
   	offset:	description:
 	-------	------------
sysdeps/unix/sysv/linux/ia64/swapcontext.c +40 -0
diff --exclude CVS -urN libc/sysdeps/unix/sysv/linux/ia64/swapcontext.c libc-lia/sysdeps/unix/sysv/linux/ia64/swapcontext.c
@@ -0,0 +1,40 @@
+/* Copyright (C) 2001 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+     Contributed by David Mosberger-Tang <[email protected]>.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <ucontext.h>
+
+struct rv
+  {
+    long retval;
+    long first_return;
+  };
+
+extern struct rv __getcontext (ucontext_t *__ucp) __THROW;
+extern int __setcontext (__const ucontext_t *__ucp) __THROW;
+
+int
+__swapcontext (ucontext_t *oucp, const ucontext_t *ucp)
+{
+  struct rv rv = __getcontext (oucp);
+  if (rv.first_return)
+    __setcontext (ucp);
+  return 0;
+}
+
+weak_alias (__swapcontext, swapcontext)
sysdeps/unix/sysv/linux/ia64/sys/ucontext.h +34 -42

86 lines not on this page · raw message

sysdeps/unix/sysv/linux/ia64/ucontext_i.h +58 -0

58 lines not on this page · raw message

#1 lore raw

I've added the patch now, with some changes:

- we are not going to add a new subdir ia64/. The sys/ subdir is there for just that purpose

- there seems to be no reason to export __start_context. It was exported with the wrong version anyway

- I've made the test program more robust. Especially not having it fail if the functions are not implemented was necessary.

Thanks,

Signature · 3 lines
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------