1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*LINTLIBRARY*/
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * 4.3BSD signal compatibility functions
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * the implementation interprets signal masks equal to -1 as "all of the
43*0Sstevel@tonic-gate  * signals in the signal set", thereby allowing signals with numbers
44*0Sstevel@tonic-gate  * above 32 to be blocked when referenced in code such as:
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  *	for (i = 0; i < NSIG; i++)
47*0Sstevel@tonic-gate  *		mask |= sigmask(i)
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include <sys/types.h>
51*0Sstevel@tonic-gate #include <ucontext.h>
52*0Sstevel@tonic-gate #include <signal.h>
53*0Sstevel@tonic-gate #include <errno.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #undef	BUS_OBJERR	/* namespace conflict */
56*0Sstevel@tonic-gate #include <sys/siginfo.h>
57*0Sstevel@tonic-gate #include "libc.h"
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #pragma weak sigvechandler = _sigvechandler
60*0Sstevel@tonic-gate #pragma weak sigsetmask = _sigsetmask
61*0Sstevel@tonic-gate #pragma weak sigblock = _sigblock
62*0Sstevel@tonic-gate #pragma weak sigpause = usigpause
63*0Sstevel@tonic-gate #pragma weak sigvec = _sigvec
64*0Sstevel@tonic-gate #pragma weak sigstack = _sigstack
65*0Sstevel@tonic-gate #pragma weak signal = usignal
66*0Sstevel@tonic-gate #pragma weak siginterrupt = _siginterrupt
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * DO NOT remove the _ from these 3 functions or the subsequent
70*0Sstevel@tonic-gate  * calls to them below.  The non _ versions of these functions
71*0Sstevel@tonic-gate  * are the wrong functions to call.  This is BCP.  Extra
72*0Sstevel@tonic-gate  * care should be taken when modifying this code.
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate extern int _sigfillset(sigset_t *);
75*0Sstevel@tonic-gate extern int _sigemptyset(sigset_t *);
76*0Sstevel@tonic-gate extern int _sigprocmask(int, const sigset_t *, sigset_t *);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #define	set2mask(setp) ((setp)->__sigbits[0])
79*0Sstevel@tonic-gate #define	mask2set(mask, setp) \
80*0Sstevel@tonic-gate 	((mask) == -1 ? _sigfillset(setp) : \
81*0Sstevel@tonic-gate 	    ((void) _sigemptyset(setp), (((setp)->__sigbits[0]) = (int)(mask))))
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate void (*_siguhandler[NSIG])() = { 0 };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate  * forward declarations
87*0Sstevel@tonic-gate  */
88*0Sstevel@tonic-gate int ucbsiginterrupt(int, int);
89*0Sstevel@tonic-gate int ucbsigvec(int, struct sigvec *, struct sigvec *);
90*0Sstevel@tonic-gate int ucbsigpause(int);
91*0Sstevel@tonic-gate int ucbsigblock(int);
92*0Sstevel@tonic-gate int ucbsigsetmask(int);
93*0Sstevel@tonic-gate static void ucbsigvechandler(int, siginfo_t *, ucontext_t *);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * sigvechandler is the real signal handler installed for all
97*0Sstevel@tonic-gate  * signals handled in the 4.3BSD compatibility interface - it translates
98*0Sstevel@tonic-gate  * SVR4 signal hander arguments into 4.3BSD signal handler arguments
99*0Sstevel@tonic-gate  * and then calls the real handler
100*0Sstevel@tonic-gate  */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate int
103*0Sstevel@tonic-gate _sigvechandler(int sig, siginfo_t *sip, ucontext_t *ucp)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	ucbsigvechandler(sig, sip, ucp);
106*0Sstevel@tonic-gate 	return (0);	/* keep the same as the original prototype */
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate static void
110*0Sstevel@tonic-gate ucbsigvechandler(int sig, siginfo_t *sip, ucontext_t *ucp)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	struct sigcontext sc;
113*0Sstevel@tonic-gate 	int code;
114*0Sstevel@tonic-gate 	char *addr;
115*0Sstevel@tonic-gate #ifdef NEVER
116*0Sstevel@tonic-gate 	int gwinswitch = 0;
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	sc.sc_onstack = ((ucp->uc_stack.ss_flags & SS_ONSTACK) != 0);
120*0Sstevel@tonic-gate 	sc.sc_mask = set2mask(&ucp->uc_sigmask);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate #if defined(__sparc)
123*0Sstevel@tonic-gate 	if (sig == SIGFPE && sip != NULL && SI_FROMKERNEL(sip) &&
124*0Sstevel@tonic-gate 	    (sip->si_code == FPE_INTDIV || sip->si_code == FPE_INTOVF)) {
125*0Sstevel@tonic-gate 		/*
126*0Sstevel@tonic-gate 		 * Hack to emulate the 4.x kernel behavior of incrementing
127*0Sstevel@tonic-gate 		 * the PC on integer divide by zero and integer overflow
128*0Sstevel@tonic-gate 		 * on sparc machines.  (5.x does not increment the PC.)
129*0Sstevel@tonic-gate 		 */
130*0Sstevel@tonic-gate 		ucp->uc_mcontext.gregs[REG_PC] =
131*0Sstevel@tonic-gate 			ucp->uc_mcontext.gregs[REG_nPC];
132*0Sstevel@tonic-gate 		ucp->uc_mcontext.gregs[REG_nPC] += 4;
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	sc.sc_sp = ucp->uc_mcontext.gregs[REG_SP];
135*0Sstevel@tonic-gate 	sc.sc_pc = ucp->uc_mcontext.gregs[REG_PC];
136*0Sstevel@tonic-gate 	sc.sc_npc = ucp->uc_mcontext.gregs[REG_nPC];
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/* XX64 There is no REG_PSR for sparcv9, we map in REG_CCR for now */
139*0Sstevel@tonic-gate #if defined(__sparcv9)
140*0Sstevel@tonic-gate 	sc.sc_psr = ucp->uc_mcontext.gregs[REG_CCR];
141*0Sstevel@tonic-gate #else
142*0Sstevel@tonic-gate 	sc.sc_psr = ucp->uc_mcontext.gregs[REG_PSR];
143*0Sstevel@tonic-gate #endif
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	sc.sc_g1 = ucp->uc_mcontext.gregs[REG_G1];
146*0Sstevel@tonic-gate 	sc.sc_o0 = ucp->uc_mcontext.gregs[REG_O0];
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	/*
149*0Sstevel@tonic-gate 	 * XXX - What a kludge!
150*0Sstevel@tonic-gate 	 * Store a pointer to the original ucontext_t in the sigcontext
151*0Sstevel@tonic-gate 	 * so that it's available to the sigcleanup call that needs to
152*0Sstevel@tonic-gate 	 * return from the signal handler.  Otherwise, vital information
153*0Sstevel@tonic-gate 	 * (e.g., the "out" registers) that's only saved in the
154*0Sstevel@tonic-gate 	 * ucontext_t isn't available to sigcleanup.
155*0Sstevel@tonic-gate 	 */
156*0Sstevel@tonic-gate 	sc.sc_wbcnt = (int)(sizeof (*ucp));
157*0Sstevel@tonic-gate 	sc.sc_spbuf[0] = (char *)sig;
158*0Sstevel@tonic-gate 	sc.sc_spbuf[1] = (char *)ucp;
159*0Sstevel@tonic-gate #ifdef NEVER
160*0Sstevel@tonic-gate 	/*
161*0Sstevel@tonic-gate 	 * XXX - Sorry, we can never pass the saved register windows
162*0Sstevel@tonic-gate 	 * on in the sigcontext because we use that space to save the
163*0Sstevel@tonic-gate 	 * ucontext_t.
164*0Sstevel@tonic-gate 	 */
165*0Sstevel@tonic-gate 	if (ucp->uc_mcontext.gwins != (gwindows_t *)0) {
166*0Sstevel@tonic-gate 		int i, j;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 		gwinswitch = 1;
169*0Sstevel@tonic-gate 		sc.sc_wbcnt = ucp->uc_mcontext.gwins->wbcnt;
170*0Sstevel@tonic-gate 		/* XXX - should use bcopy to move this in bulk */
171*0Sstevel@tonic-gate 		for (i = 0; i < ucp->uc_mcontext.gwins; i++) {
172*0Sstevel@tonic-gate 			sc.sc_spbuf[i] = ucp->uc_mcontext.gwins->spbuf[i];
173*0Sstevel@tonic-gate 			for (j = 0; j < 8; j++)
174*0Sstevel@tonic-gate 				sc.sc_wbuf[i][j] =
175*0Sstevel@tonic-gate 				    ucp->uc_mcontext.gwins->wbuf[i].rw_local[j];
176*0Sstevel@tonic-gate 			for (j = 0; j < 8; j++)
177*0Sstevel@tonic-gate 				sc.sc_wbuf[i][j+8] =
178*0Sstevel@tonic-gate 				    ucp->uc_mcontext.gwins->wbuf[i].rw_in[j];
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate #endif
182*0Sstevel@tonic-gate #endif
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/*
185*0Sstevel@tonic-gate 	 * Translate signal codes from new to old.
186*0Sstevel@tonic-gate 	 * /usr/include/sys/siginfo.h contains new codes.
187*0Sstevel@tonic-gate 	 * /usr/ucbinclude/sys/signal.h contains old codes.
188*0Sstevel@tonic-gate 	 */
189*0Sstevel@tonic-gate 	code = 0;
190*0Sstevel@tonic-gate 	addr = SIG_NOADDR;
191*0Sstevel@tonic-gate 	if (sip != NULL && SI_FROMKERNEL(sip)) {
192*0Sstevel@tonic-gate 		addr = sip->si_addr;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 		switch (sig) {
195*0Sstevel@tonic-gate 		case SIGILL:
196*0Sstevel@tonic-gate 			switch (sip->si_code) {
197*0Sstevel@tonic-gate 			case ILL_PRVOPC:
198*0Sstevel@tonic-gate 				code = ILL_PRIVINSTR_FAULT;
199*0Sstevel@tonic-gate 				break;
200*0Sstevel@tonic-gate 			case ILL_BADSTK:
201*0Sstevel@tonic-gate 				code = ILL_STACK;
202*0Sstevel@tonic-gate 				break;
203*0Sstevel@tonic-gate 			case ILL_ILLTRP:
204*0Sstevel@tonic-gate 				code = ILL_TRAP_FAULT(sip->si_trapno);
205*0Sstevel@tonic-gate 				break;
206*0Sstevel@tonic-gate 			default:
207*0Sstevel@tonic-gate 				code = ILL_ILLINSTR_FAULT;
208*0Sstevel@tonic-gate 				break;
209*0Sstevel@tonic-gate 			}
210*0Sstevel@tonic-gate 			break;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 		case SIGEMT:
213*0Sstevel@tonic-gate 			code = EMT_TAG;
214*0Sstevel@tonic-gate 			break;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		case SIGFPE:
217*0Sstevel@tonic-gate 			switch (sip->si_code) {
218*0Sstevel@tonic-gate 			case FPE_INTDIV:
219*0Sstevel@tonic-gate 				code = FPE_INTDIV_TRAP;
220*0Sstevel@tonic-gate 				break;
221*0Sstevel@tonic-gate 			case FPE_INTOVF:
222*0Sstevel@tonic-gate 				code = FPE_INTOVF_TRAP;
223*0Sstevel@tonic-gate 				break;
224*0Sstevel@tonic-gate 			case FPE_FLTDIV:
225*0Sstevel@tonic-gate 				code = FPE_FLTDIV_TRAP;
226*0Sstevel@tonic-gate 				break;
227*0Sstevel@tonic-gate 			case FPE_FLTOVF:
228*0Sstevel@tonic-gate 				code = FPE_FLTOVF_TRAP;
229*0Sstevel@tonic-gate 				break;
230*0Sstevel@tonic-gate 			case FPE_FLTUND:
231*0Sstevel@tonic-gate 				code = FPE_FLTUND_TRAP;
232*0Sstevel@tonic-gate 				break;
233*0Sstevel@tonic-gate 			case FPE_FLTRES:
234*0Sstevel@tonic-gate 				code = FPE_FLTINEX_TRAP;
235*0Sstevel@tonic-gate 				break;
236*0Sstevel@tonic-gate 			default:
237*0Sstevel@tonic-gate 				code = FPE_FLTOPERR_TRAP;
238*0Sstevel@tonic-gate 				break;
239*0Sstevel@tonic-gate 			}
240*0Sstevel@tonic-gate 			break;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		case SIGBUS:
243*0Sstevel@tonic-gate 			switch (sip->si_code) {
244*0Sstevel@tonic-gate 			case BUS_ADRALN:
245*0Sstevel@tonic-gate 				code = BUS_ALIGN;
246*0Sstevel@tonic-gate 				break;
247*0Sstevel@tonic-gate 			case BUS_ADRERR:
248*0Sstevel@tonic-gate 				code = BUS_HWERR;
249*0Sstevel@tonic-gate 				break;
250*0Sstevel@tonic-gate 			default:	/* BUS_OBJERR */
251*0Sstevel@tonic-gate 				code = FC_MAKE_ERR(sip->si_errno);
252*0Sstevel@tonic-gate 				break;
253*0Sstevel@tonic-gate 			}
254*0Sstevel@tonic-gate 			break;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		case SIGSEGV:
257*0Sstevel@tonic-gate 			switch (sip->si_code) {
258*0Sstevel@tonic-gate 			case SEGV_MAPERR:
259*0Sstevel@tonic-gate 				code = SEGV_NOMAP;
260*0Sstevel@tonic-gate 				break;
261*0Sstevel@tonic-gate 			case SEGV_ACCERR:
262*0Sstevel@tonic-gate 				code = SEGV_PROT;
263*0Sstevel@tonic-gate 				break;
264*0Sstevel@tonic-gate 			default:
265*0Sstevel@tonic-gate 				code = FC_MAKE_ERR(sip->si_errno);
266*0Sstevel@tonic-gate 				break;
267*0Sstevel@tonic-gate 			}
268*0Sstevel@tonic-gate 			break;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 		default:
271*0Sstevel@tonic-gate 			addr = SIG_NOADDR;
272*0Sstevel@tonic-gate 			break;
273*0Sstevel@tonic-gate 		}
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	(*_siguhandler[sig])(sig, code, &sc, addr);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (sc.sc_onstack)
279*0Sstevel@tonic-gate 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
280*0Sstevel@tonic-gate 	else
281*0Sstevel@tonic-gate 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
282*0Sstevel@tonic-gate 	mask2set(sc.sc_mask, &ucp->uc_sigmask);
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate #if defined(__sparc)
285*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_SP] = sc.sc_sp;
286*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PC] = sc.sc_pc;
287*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_nPC] = sc.sc_npc;
288*0Sstevel@tonic-gate #if defined(__sparcv9)
289*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_CCR] = sc.sc_psr;
290*0Sstevel@tonic-gate #else
291*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PSR] = sc.sc_psr;
292*0Sstevel@tonic-gate #endif
293*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_G1] = sc.sc_g1;
294*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O0] = sc.sc_o0;
295*0Sstevel@tonic-gate #ifdef NEVER
296*0Sstevel@tonic-gate 	if (gwinswitch == 1) {
297*0Sstevel@tonic-gate 		int i, j;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 		ucp->uc_mcontext.gwins->wbcnt = sc.sc_wbcnt;
300*0Sstevel@tonic-gate 		/* XXX - should use bcopy to move this in bulk */
301*0Sstevel@tonic-gate 		for (i = 0; i < sc.sc_wbcnt; i++) {
302*0Sstevel@tonic-gate 			ucp->uc_mcontext.gwins->spbuf[i] = sc.sc_spbuf[i];
303*0Sstevel@tonic-gate 			for (j = 0; j < 8; j++)
304*0Sstevel@tonic-gate 				ucp->uc_mcontext.gwins->wbuf[i].rw_local[j] =
305*0Sstevel@tonic-gate 				    sc.sc_wbuf[i][j];
306*0Sstevel@tonic-gate 			for (j = 0; j < 8; j++)
307*0Sstevel@tonic-gate 				ucp->uc_mcontext.gwins->wbuf[i].rw_in[j] =
308*0Sstevel@tonic-gate 				    sc.sc_wbuf[i][j+8];
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate #endif
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	if (sig == SIGFPE) {
314*0Sstevel@tonic-gate 		if (ucp->uc_mcontext.fpregs.fpu_qcnt > 0) {
315*0Sstevel@tonic-gate 			ucp->uc_mcontext.fpregs.fpu_qcnt--;
316*0Sstevel@tonic-gate 			ucp->uc_mcontext.fpregs.fpu_q++;
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate #endif
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	(void) setcontext(ucp);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate #if defined(__sparc)
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * Emulate the special sigcleanup trap.
327*0Sstevel@tonic-gate  * This is only used by statically linked 4.x applications
328*0Sstevel@tonic-gate  * and thus is only called by the static BCP support.
329*0Sstevel@tonic-gate  * It lives here because of its close relationship with
330*0Sstevel@tonic-gate  * the ucbsigvechandler code above.
331*0Sstevel@tonic-gate  *
332*0Sstevel@tonic-gate  * It's used by 4.x applications to:
333*0Sstevel@tonic-gate  *	1. return from a signal handler (in __sigtramp)
334*0Sstevel@tonic-gate  *	2. [sig]longjmp
335*0Sstevel@tonic-gate  *	3. context switch, in the old 4.x liblwp
336*0Sstevel@tonic-gate  */
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate void
339*0Sstevel@tonic-gate __sigcleanup(struct sigcontext *scp)
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	ucontext_t uc, *ucp;
342*0Sstevel@tonic-gate 	int sig;
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	/*
345*0Sstevel@tonic-gate 	 * If there's a pointer to a ucontext_t hiding in the sigcontext,
346*0Sstevel@tonic-gate 	 * we *must* use that to return, since it contains important data
347*0Sstevel@tonic-gate 	 * such as the original "out" registers when the signal occurred.
348*0Sstevel@tonic-gate 	 */
349*0Sstevel@tonic-gate 	if (scp->sc_wbcnt == sizeof (*ucp)) {
350*0Sstevel@tonic-gate 		sig = (int)scp->sc_spbuf[0];
351*0Sstevel@tonic-gate 		ucp = (ucontext_t *)scp->sc_spbuf[1];
352*0Sstevel@tonic-gate 	} else {
353*0Sstevel@tonic-gate 		/*
354*0Sstevel@tonic-gate 		 * Otherwise, use a local ucontext_t and
355*0Sstevel@tonic-gate 		 * initialize it with getcontext.
356*0Sstevel@tonic-gate 		 */
357*0Sstevel@tonic-gate 		sig = 0;
358*0Sstevel@tonic-gate 		ucp = &uc;
359*0Sstevel@tonic-gate 		(void) getcontext(ucp);
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	if (scp->sc_onstack) {
363*0Sstevel@tonic-gate 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
364*0Sstevel@tonic-gate 	} else
365*0Sstevel@tonic-gate 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
366*0Sstevel@tonic-gate 	mask2set(scp->sc_mask, &ucp->uc_sigmask);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_SP] = scp->sc_sp;
369*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PC] = scp->sc_pc;
370*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_nPC] = scp->sc_npc;
371*0Sstevel@tonic-gate #if defined(__sparcv9)
372*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_CCR] = scp->sc_psr;
373*0Sstevel@tonic-gate #else
374*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_PSR] = scp->sc_psr;
375*0Sstevel@tonic-gate #endif
376*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_G1] = scp->sc_g1;
377*0Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_O0] = scp->sc_o0;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	if (sig == SIGFPE) {
380*0Sstevel@tonic-gate 		if (ucp->uc_mcontext.fpregs.fpu_qcnt > 0) {
381*0Sstevel@tonic-gate 			ucp->uc_mcontext.fpregs.fpu_qcnt--;
382*0Sstevel@tonic-gate 			ucp->uc_mcontext.fpregs.fpu_q++;
383*0Sstevel@tonic-gate 		}
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	(void) setcontext(ucp);
386*0Sstevel@tonic-gate 	/* NOTREACHED */
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate #endif
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate int
391*0Sstevel@tonic-gate _sigsetmask(int mask)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	return (ucbsigsetmask(mask));
394*0Sstevel@tonic-gate }
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate int
397*0Sstevel@tonic-gate ucbsigsetmask(int mask)
398*0Sstevel@tonic-gate {
399*0Sstevel@tonic-gate 	sigset_t oset;
400*0Sstevel@tonic-gate 	sigset_t nset;
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	(void) _sigprocmask(0, (sigset_t *)0, &nset);
403*0Sstevel@tonic-gate 	mask2set(mask, &nset);
404*0Sstevel@tonic-gate 	(void) _sigprocmask(SIG_SETMASK, &nset, &oset);
405*0Sstevel@tonic-gate 	return (set2mask(&oset));
406*0Sstevel@tonic-gate }
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate int
409*0Sstevel@tonic-gate _sigblock(int mask)
410*0Sstevel@tonic-gate {
411*0Sstevel@tonic-gate 	return (ucbsigblock(mask));
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate int
415*0Sstevel@tonic-gate ucbsigblock(int mask)
416*0Sstevel@tonic-gate {
417*0Sstevel@tonic-gate 	sigset_t oset;
418*0Sstevel@tonic-gate 	sigset_t nset;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	(void) _sigprocmask(0, (sigset_t *)0, &nset);
421*0Sstevel@tonic-gate 	mask2set(mask, &nset);
422*0Sstevel@tonic-gate 	(void) _sigprocmask(SIG_BLOCK, &nset, &oset);
423*0Sstevel@tonic-gate 	return (set2mask(&oset));
424*0Sstevel@tonic-gate }
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate int
427*0Sstevel@tonic-gate usigpause(int mask)
428*0Sstevel@tonic-gate {
429*0Sstevel@tonic-gate 	return (ucbsigpause(mask));
430*0Sstevel@tonic-gate }
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate int
433*0Sstevel@tonic-gate ucbsigpause(int mask)
434*0Sstevel@tonic-gate {
435*0Sstevel@tonic-gate 	sigset_t set, oset;
436*0Sstevel@tonic-gate 	int ret;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	(void) _sigprocmask(0, (sigset_t *)0, &set);
439*0Sstevel@tonic-gate 	oset = set;
440*0Sstevel@tonic-gate 	mask2set(mask, &set);
441*0Sstevel@tonic-gate 	ret = sigsuspend(&set);
442*0Sstevel@tonic-gate 	(void) _sigprocmask(SIG_SETMASK, &oset, (sigset_t *)0);
443*0Sstevel@tonic-gate 	return (ret);
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate int
447*0Sstevel@tonic-gate _sigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
448*0Sstevel@tonic-gate {
449*0Sstevel@tonic-gate 	return (ucbsigvec(sig, nvec, ovec));
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate int
453*0Sstevel@tonic-gate ucbsigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
454*0Sstevel@tonic-gate {
455*0Sstevel@tonic-gate 	struct sigaction nact;
456*0Sstevel@tonic-gate 	struct sigaction oact;
457*0Sstevel@tonic-gate 	struct sigaction *nactp;
458*0Sstevel@tonic-gate 	void (*ohandler)(int, int, struct sigcontext *, char *);
459*0Sstevel@tonic-gate 	void (*nhandler)(int, int, struct sigcontext *, char *);
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
462*0Sstevel@tonic-gate 		errno = EINVAL;
463*0Sstevel@tonic-gate 		return (-1);
464*0Sstevel@tonic-gate 	}
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	if ((long)ovec == -1L || (long)nvec == -1L) {
467*0Sstevel@tonic-gate 		errno = EFAULT;
468*0Sstevel@tonic-gate 		return (-1);
469*0Sstevel@tonic-gate 	}
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	ohandler = _siguhandler[sig];
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	if (nvec) {
474*0Sstevel@tonic-gate 		(void) _sigaction(sig, (struct sigaction *)0, &nact);
475*0Sstevel@tonic-gate 		nhandler = nvec->sv_handler;
476*0Sstevel@tonic-gate 		/*
477*0Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
478*0Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL,
479*0Sstevel@tonic-gate 		 * do not change the signal's entry in the handler array.
480*0Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
481*0Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
482*0Sstevel@tonic-gate 		 */
483*0Sstevel@tonic-gate 		if ((void (*)(int))nhandler != SIG_DFL &&
484*0Sstevel@tonic-gate 		    (void (*)(int))nhandler != SIG_IGN) {
485*0Sstevel@tonic-gate 			_siguhandler[sig] = nhandler;
486*0Sstevel@tonic-gate 			nact.sa_handler = (void (*)(int))ucbsigvechandler;
487*0Sstevel@tonic-gate 		} else {
488*0Sstevel@tonic-gate 			nact.sa_handler = (void (*)(int))nhandler;
489*0Sstevel@tonic-gate 		}
490*0Sstevel@tonic-gate 		mask2set(nvec->sv_mask, &nact.sa_mask);
491*0Sstevel@tonic-gate 		if (sig == SIGKILL || sig == SIGSTOP)
492*0Sstevel@tonic-gate 			nact.sa_handler = SIG_DFL;
493*0Sstevel@tonic-gate 		nact.sa_flags = SA_SIGINFO;
494*0Sstevel@tonic-gate 		if (!(nvec->sv_flags & SV_INTERRUPT))
495*0Sstevel@tonic-gate 			nact.sa_flags |= SA_RESTART;
496*0Sstevel@tonic-gate 		if (nvec->sv_flags & SV_RESETHAND)
497*0Sstevel@tonic-gate 			nact.sa_flags |= SA_RESETHAND;
498*0Sstevel@tonic-gate 		if (nvec->sv_flags & SV_ONSTACK)
499*0Sstevel@tonic-gate 			nact.sa_flags |= SA_ONSTACK;
500*0Sstevel@tonic-gate 		nactp = &nact;
501*0Sstevel@tonic-gate 	} else
502*0Sstevel@tonic-gate 		nactp = (struct sigaction *)0;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	if (_sigaction(sig, nactp, &oact) < 0) {
505*0Sstevel@tonic-gate 		_siguhandler[sig] = ohandler;
506*0Sstevel@tonic-gate 		return (-1);
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	if (ovec) {
510*0Sstevel@tonic-gate 		if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN)
511*0Sstevel@tonic-gate 			ovec->sv_handler =
512*0Sstevel@tonic-gate 			    (void (*) (int, int, struct sigcontext *, char *))
513*0Sstevel@tonic-gate 				oact.sa_handler;
514*0Sstevel@tonic-gate 		else
515*0Sstevel@tonic-gate 			ovec->sv_handler = ohandler;
516*0Sstevel@tonic-gate 		ovec->sv_mask = set2mask(&oact.sa_mask);
517*0Sstevel@tonic-gate 		ovec->sv_flags = 0;
518*0Sstevel@tonic-gate 		if (oact.sa_flags & SA_ONSTACK)
519*0Sstevel@tonic-gate 			ovec->sv_flags |= SV_ONSTACK;
520*0Sstevel@tonic-gate 		if (oact.sa_flags & SA_RESETHAND)
521*0Sstevel@tonic-gate 			ovec->sv_flags |= SV_RESETHAND;
522*0Sstevel@tonic-gate 		if (!(oact.sa_flags & SA_RESTART))
523*0Sstevel@tonic-gate 			ovec->sv_flags |= SV_INTERRUPT;
524*0Sstevel@tonic-gate 	}
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	return (0);
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate int
530*0Sstevel@tonic-gate _sigstack(struct sigstack *nss, struct sigstack *oss)
531*0Sstevel@tonic-gate {
532*0Sstevel@tonic-gate 	struct sigaltstack nalt;
533*0Sstevel@tonic-gate 	struct sigaltstack oalt;
534*0Sstevel@tonic-gate 	struct sigaltstack *naltp;
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	if (nss) {
537*0Sstevel@tonic-gate 		/*
538*0Sstevel@tonic-gate 		 * XXX: assumes stack growth is down (like sparc)
539*0Sstevel@tonic-gate 		 */
540*0Sstevel@tonic-gate 		nalt.ss_sp = nss->ss_sp - SIGSTKSZ;
541*0Sstevel@tonic-gate 		nalt.ss_size = SIGSTKSZ;
542*0Sstevel@tonic-gate 		nalt.ss_flags = 0;
543*0Sstevel@tonic-gate 		naltp = &nalt;
544*0Sstevel@tonic-gate 	} else
545*0Sstevel@tonic-gate 		naltp = (struct sigaltstack *)0;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	if (sigaltstack(naltp, &oalt) < 0)
548*0Sstevel@tonic-gate 		return (-1);
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	if (oss) {
551*0Sstevel@tonic-gate 		/*
552*0Sstevel@tonic-gate 		 * XXX: assumes stack growth is down (like sparc)
553*0Sstevel@tonic-gate 		 */
554*0Sstevel@tonic-gate 		oss->ss_sp = oalt.ss_sp + oalt.ss_size;
555*0Sstevel@tonic-gate 		oss->ss_onstack = ((oalt.ss_flags & SS_ONSTACK) != 0);
556*0Sstevel@tonic-gate 	}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	return (0);
559*0Sstevel@tonic-gate }
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate void (*
562*0Sstevel@tonic-gate ucbsignal(int s, void (*a)(int)))(int)
563*0Sstevel@tonic-gate {
564*0Sstevel@tonic-gate 	struct sigvec osv;
565*0Sstevel@tonic-gate 	struct sigvec nsv;
566*0Sstevel@tonic-gate 	static int mask[NSIG];
567*0Sstevel@tonic-gate 	static int flags[NSIG];
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	nsv.sv_handler = (void (*) (int, int, struct sigcontext *, char *)) a;
570*0Sstevel@tonic-gate 	nsv.sv_mask = mask[s];
571*0Sstevel@tonic-gate 	nsv.sv_flags = flags[s];
572*0Sstevel@tonic-gate 	if (ucbsigvec(s, &nsv, &osv) < 0)
573*0Sstevel@tonic-gate 		return (SIG_ERR);
574*0Sstevel@tonic-gate 	if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) {
575*0Sstevel@tonic-gate 		mask[s] = nsv.sv_mask = osv.sv_mask;
576*0Sstevel@tonic-gate 		flags[s] = nsv.sv_flags =
577*0Sstevel@tonic-gate 			osv.sv_flags & ~(SV_RESETHAND|SV_INTERRUPT);
578*0Sstevel@tonic-gate 		if (ucbsigvec(s, &nsv, (struct sigvec *)0) < 0)
579*0Sstevel@tonic-gate 			return (SIG_ERR);
580*0Sstevel@tonic-gate 	}
581*0Sstevel@tonic-gate 	return ((void (*) (int)) osv.sv_handler);
582*0Sstevel@tonic-gate }
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate void (*
585*0Sstevel@tonic-gate usignal(int s, void (*a) (int)))(int)
586*0Sstevel@tonic-gate {
587*0Sstevel@tonic-gate 	return (ucbsignal(s, a));
588*0Sstevel@tonic-gate }
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate /*
591*0Sstevel@tonic-gate  * Set signal state to prevent restart of system calls
592*0Sstevel@tonic-gate  * after an instance of the indicated signal.
593*0Sstevel@tonic-gate  */
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate int
596*0Sstevel@tonic-gate _siginterrupt(int sig, int flag)
597*0Sstevel@tonic-gate {
598*0Sstevel@tonic-gate 	return (ucbsiginterrupt(sig, flag));
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate int
602*0Sstevel@tonic-gate ucbsiginterrupt(int sig, int flag)
603*0Sstevel@tonic-gate {
604*0Sstevel@tonic-gate 	struct sigvec sv;
605*0Sstevel@tonic-gate 	int ret;
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	if ((ret = ucbsigvec(sig, 0, &sv)) < 0)
608*0Sstevel@tonic-gate 		return (ret);
609*0Sstevel@tonic-gate 	if (flag)
610*0Sstevel@tonic-gate 		sv.sv_flags |= SV_INTERRUPT;
611*0Sstevel@tonic-gate 	else
612*0Sstevel@tonic-gate 		sv.sv_flags &= ~SV_INTERRUPT;
613*0Sstevel@tonic-gate 	return (ucbsigvec(sig, &sv, 0));
614*0Sstevel@tonic-gate }
615