2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* process.c - manage lwp context switches be means of setjmp/longjmp. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
21 #if defined(AFS_S390_LINUX20_ENV)
22 extern int PRE_Block; /* used in lwp.c and process.s */
24 extern char PRE_Block; /* used in lwp.c and process.s */
27 #if defined(USE_UCONTEXT) && defined(HAVE_UCONTEXT_H)
29 # if defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
30 # define AFS_UCONTEXT_NOSTACK
34 savecontext(void (*ep) (void), struct lwp_context *savearea, char *newsp)
36 # ifdef AFS_UCONTEXT_NOSTACK
37 /* getcontext does not export stack info */
44 getcontext(&savearea->ucontext);
45 # ifdef AFS_UCONTEXT_NOSTACK
46 savearea->topstack = (char *)&stackvar;
48 savearea->topstack = savearea->ucontext.uc_stack.ss_sp;
50 switch (savearea->state) {
56 thread.uc_stack.ss_sp =
57 newsp - AFS_LWP_MINSTACKSIZE + sizeof(void *) +
59 thread.uc_stack.ss_size = AFS_LWP_MINSTACKSIZE - sizeof(void *);
60 makecontext(&thread, ep, 0);
72 returnto(struct lwp_context *savearea)
77 setcontext(&savearea->ucontext);
85 #if defined(AFS_SGI64_ENV)
91 #elif defined(AFS_HPUX_ENV) || defined(AFS_PARISC_LINUX24_ENV)
93 #elif defined(AFS_LINUX20_ENV)
94 #if defined(AFS_PPC_LINUX20_ENV) || defined(AFS_PPC64_LINUX20_ENV)
96 #elif defined(AFS_I386_LINUX20_ENV)
98 #elif defined(AFS_S390_LINUX20_ENV)
101 #elif defined(AFS_SPARC_LINUX20_ENV)
104 #elif defined(AFS_SPARC64_LINUX20_ENV) && defined(AFS_32BIT_USR_ENV)
107 #elif defined(AFS_ALPHA_LINUX20_ENV)
110 #elif defined(AFS_PARISC_LINUX24_ENV)
113 #error Unsupported linux LWP system type.
115 #elif defined(AFS_X86_FBSD_ENV)
117 #elif defined(AFS_DARWIN_ENV)
120 Need offset to SP in jmp_buf for this platform.
123 * On SGIs the type of the elements of the array passed to setjmp
124 * differs based on the ISA chosen. It is int for mips1 and mips2 and
125 * __uint64_t for mips3 and mips4
128 #if (_MIPS_ISA == _MIPS_ISA_MIPS3 || _MIPS_ISA == _MIPS_ISA_MIPS4)
129 typedef __uint64_t jmp_buf_type;
132 #if defined(AFS_ALPHA_LINUX20_ENV) || defined(AFS_PPC64_LINUX20_ENV)
133 typedef long jmp_buf_type;
135 typedef int jmp_buf_type;
136 #endif /*AFS_ALPHA_LINUX20_ENV */
139 static jmp_buf jmp_tmp;
140 static void (*EP) (void);
142 static jmp_buf_type *jmpBuffer;
144 /** Starting with Glibc 2.4 pointers in jmp_buf are mangled (XOR) for "protection".
145 * On Sparc ucontext functions are not implemented.
147 #define ptr_mangle(x) (x)
148 #ifdef AFS_LINUX20_ENV
151 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 3)
153 #if defined(AFS_SPARC64_LINUX24_ENV) || defined(AFS_SPARC_LINUX24_ENV)
154 /* technically we should use POINTER_GUARD
155 * ( == offsetof (tcbhead_t, pointer_guard) )
159 static int ptr_mangle(int p)
161 register char *tls_ptr __asm__("%g7");
162 return p ^ *(int*)(tls_ptr + 0x18);
165 #error need ptr_mangle support or use UCONTEXT
174 savecontext(void (*ep)(void), struct lwp_context *savearea, char *sp)
181 code = setjmp(savearea->setjmp_buffer);
182 jmpBuffer = (jmp_buf_type *) savearea->setjmp_buffer;
183 savearea->topstack = (char *) ptr_mangle(jmpBuffer[LWP_SP]);
187 int i, *ptr = (int *)savearea->setjmp_buffer;
188 printf("savecontext\n");
189 for (i = 0; i < 5; i++)
190 printf("(%d) 0x%x ", i, ptr[i]);
192 for (i = 5; i < 10; i++)
193 printf("(%d) 0x%x ", i, ptr[i]);
202 rc = setjmp(jmp_tmp);
205 jmpBuffer = (jmp_buf_type *) jmp_tmp;
206 jmpBuffer[LWP_SP] = ptr_mangle((jmp_buf_type) sp);
207 #if defined(AFS_S390_LINUX20_ENV) || defined(AFS_SPARC_LINUX20_ENV) || (defined(AFS_SPARC64_LINUX20_ENV) && defined(AFS_32BIT_USR_ENV))
208 jmpBuffer[LWP_FP] = ptr_mangle((jmp_buf_type) sp);
214 assert(0); /* never returns */
217 perror("Error in setjmp1\n");
222 case 2: /* restoring frame */
226 perror("Error in setjmp2 : restoring\n");
233 returnto(struct lwp_context * savearea)
236 int i, *ptr = savearea->setjmp_buffer;
238 printf("Returning to \n");
239 for (i = 0; i < 5; i++)
240 printf("(%d) 0x%x ", i, ptr[i]);
242 for (i = 5; i < 10; i++)
243 printf("(%d) 0x%x ", i, ptr[i]);
247 longjmp(savearea->setjmp_buffer, 2);