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