xref: /titanic_52/usr/src/lib/libc/sparcv9/gen/siglongjmp.c (revision 61679b0b6826b0ae7e3f751acd91412fcfa45d1e)
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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1988 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 #pragma weak _siglongjmp = siglongjmp
30 
31 #include "lint.h"
32 #include <sys/types.h>
33 #include <sys/stack.h>
34 #include <sys/frame.h>
35 #include <memory.h>
36 #include <ucontext.h>
37 #include <setjmp.h>
38 #include "sigjmp_struct.h"
39 #include "libc.h"
40 
41 void
42 siglongjmp(sigjmp_buf env, int val)
43 {
44 	extern void _fetch_globals(greg_t *);
45 	ucontext_t uc;
46 	greg_t *reg = uc.uc_mcontext.gregs;
47 	volatile sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
48 	greg_t fp = bp->sjs_fp;
49 	greg_t i7 = bp->sjs_i7;
50 
51 	/*
52 	 * Create a ucontext_t structure from scratch.
53 	 * We only need to fetch the globals.
54 	 * The outs are assumed to be trashed on return from sigsetjmp().
55 	 * The ins and locals are restored from the resumed register window.
56 	 * The floating point state is unmodified.
57 	 * Everything else is in the sigjmp_struct_t buffer.
58 	 */
59 	(void) memset(&uc, 0, sizeof (uc));
60 	uc.uc_flags = UC_STACK | UC_CPU;
61 	_fetch_globals(&reg[REG_G1]);
62 	uc.uc_stack = bp->sjs_stack;
63 	uc.uc_link = bp->sjs_uclink;
64 	reg[REG_PC] = bp->sjs_pc;
65 	reg[REG_nPC] = reg[REG_PC] + 0x4;
66 	reg[REG_SP] = bp->sjs_sp;
67 	reg[REG_ASI] = bp->sjs_asi;
68 	reg[REG_FPRS] = bp->sjs_fprs;
69 
70 	if (bp->sjs_flags & JB_SAVEMASK) {
71 		uc.uc_flags |= UC_SIGMASK;
72 		uc.uc_sigmask = bp->sjs_sigmask;
73 	}
74 
75 	if (val)
76 		reg[REG_O0] = (greg_t)val;
77 	else
78 		reg[REG_O0] = (greg_t)1;
79 
80 	/*
81 	 * Copy the fp and i7 values into the register window save area.
82 	 * These may have been clobbered between calls to sigsetjmp
83 	 * and siglongjmp.  For example, the save area could have been
84 	 * relocated to lower addresses and the original save area
85 	 * given to an alloca() call.  Notice that all reads from
86 	 * the sigjmp_struct_t buffer should take place before the
87 	 * following two writes.  It is possible that user code may
88 	 * move/copy the sigjmpbuf around, and overlap the original
89 	 * register window save area.
90 	 */
91 	if (bp->sjs_sp != 0 && (bp->sjs_flags & JB_FRAMEPTR)) {
92 		struct frame *sp = (struct frame *)(bp->sjs_sp + STACK_BIAS);
93 		sp->fr_savfp = (struct frame *)fp;
94 		sp->fr_savpc = i7;
95 	}
96 
97 	(void) setcontext(&uc);
98 }
99