xref: /titanic_50/usr/src/lib/libc/sparc/gen/siglongjmp.c (revision ba7866cd2cbdf574f47d4e38a1301b90744dd677)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #pragma weak _siglongjmp = siglongjmp
33 
34 #include "lint.h"
35 #include <sys/types.h>
36 #include <sys/stack.h>
37 #include <sys/frame.h>
38 #include <memory.h>
39 #include <ucontext.h>
40 #include <setjmp.h>
41 #include "sigjmp_struct.h"
42 #include "libc.h"
43 
44 void
45 siglongjmp(sigjmp_buf env, int val)
46 {
47 	extern void _fetch_globals(greg_t *);
48 	ucontext_t uc;
49 	greg_t *reg = uc.uc_mcontext.gregs;
50 	volatile sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
51 	greg_t fp = bp->sjs_fp;
52 	greg_t i7 = bp->sjs_i7;
53 
54 	/*
55 	 * Create a ucontext_t structure from scratch.
56 	 * We only need to fetch the globals.
57 	 * The outs are assumed to be trashed on return from sigsetjmp().
58 	 * The ins and locals are restored from the resumed register window.
59 	 * The floating point state is unmodified.
60 	 * Everything else is in the sigjmp_struct_t buffer.
61 	 */
62 	(void) memset(&uc, 0, sizeof (uc));
63 	uc.uc_flags = UC_STACK | UC_CPU;
64 	_fetch_globals(&reg[REG_G1]);
65 	uc.uc_stack = bp->sjs_stack;
66 	uc.uc_link = bp->sjs_uclink;
67 	reg[REG_PC] = bp->sjs_pc;
68 	reg[REG_nPC] = reg[REG_PC] + 0x4;
69 	reg[REG_SP] = bp->sjs_sp;
70 
71 	if (bp->sjs_flags & JB_SAVEMASK) {
72 		uc.uc_flags |= UC_SIGMASK;
73 		uc.uc_sigmask = bp->sjs_sigmask;
74 	}
75 
76 	if (val)
77 		reg[REG_O0] = (greg_t)val;
78 	else
79 		reg[REG_O0] = (greg_t)1;
80 
81 	/*
82 	 * Copy the fp and i7 values into the register window save area.
83 	 * These may have been clobbered between calls to sigsetjmp
84 	 * and siglongjmp.  For example, the save area could have been
85 	 * relocated to lower addresses and the original save area
86 	 * given to an alloca() call.  Notice that all reads from
87 	 * the sigjmp_struct_t buffer should take place before the
88 	 * following two writes.  It is possible that user code may
89 	 * move/copy the sigjmpbuf around, and overlap the original
90 	 * register window save area.
91 	 */
92 	if (bp->sjs_sp != 0 && (bp->sjs_flags & JB_FRAMEPTR)) {
93 		struct frame *sp = (struct frame *)(bp->sjs_sp + STACK_BIAS);
94 		sp->fr_savfp = (struct frame *)fp;
95 		sp->fr_savpc = i7;
96 	}
97 
98 	(void) setcontext(&uc);
99 }
100