xref: /titanic_52/usr/src/cmd/csh/sparc/signal.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 /*
18  * 4.3BSD signal compatibility functions
19  *
20  * the implementation interprets signal masks equal to -1 as "all of the
21  * signals in the signal set", thereby allowing signals with numbers
22  * above 32 to be blocked when referenced in code such as:
23  *
24  *	for (i = 0; i < NSIG; i++)
25  *		mask |= sigmask(i)
26  */
27 
28 #include <sys/types.h>
29 #include <sys/siginfo.h>
30 #include <sys/ucontext.h>
31 #include <signal.h>
32 #include "signal.h"
33 #include <errno.h>
34 #include <stdio.h>
35 
36 #define set2mask(setp) ((setp)->__sigbits[0])
37 #define mask2set(mask, setp) \
38 	((mask) == -1 ? sigfillset(setp) : sigemptyset(setp), (((setp)->__sigbits[0]) = (mask)))
39 
40 void (*_siguhandler[NSIG])() = { 0 };
41 
42 /*
43  * sigstack is emulated with sigaltstack by guessing an appropriate
44  * value for the stack size - on machines that have stacks that grow
45  * upwards, the ss_sp arguments for both functions mean the same thing,
46  * (the initial stack pointer sigstack() is also the stack base
47  * sigaltstack()), so a "very large" value should be chosen for the
48  * stack size - on machines that have stacks that grow downwards, the
49  * ss_sp arguments mean opposite things, so 0 should be used (hopefully
50  * these machines don't have hardware stack bounds registers that pay
51  * attention to sigaltstack()'s size argument.
52  */
53 
54 #ifdef sun
55 #define SIGSTACKSIZE	0
56 #endif
57 
58 
59 /*
60  * sigvechandler is the real signal handler installed for all
61  * signals handled in the 4.3BSD compatibility interface - it translates
62  * SVR4 signal hander arguments into 4.3BSD signal handler arguments
63  * and then calls the real handler
64  */
65 
66 static void
67 sigvechandler(sig, sip, ucp)
68 	int sig;
69 	siginfo_t *sip;
70 	ucontext_t *ucp;
71 {
72 	struct sigcontext sc;
73 	int code;
74 	char *addr;
75 	register int i, j;
76 	int gwinswitch = 0;
77 
78 	sc.sc_onstack = ((ucp->uc_stack.ss_flags & SS_ONSTACK) != 0);
79 	sc.sc_mask = set2mask(&ucp->uc_sigmask);
80 
81 	/*
82 	 * Machine dependent code begins
83 	 */
84 	sc.sc_sp = ucp->uc_mcontext.gregs[REG_O6];
85 	sc.sc_pc = ucp->uc_mcontext.gregs[REG_PC];
86 	sc.sc_npc = ucp->uc_mcontext.gregs[REG_nPC];
87 	sc.sc_psr = ucp->uc_mcontext.gregs[REG_PSR];
88 	sc.sc_g1 = ucp->uc_mcontext.gregs[REG_G1];
89 	sc.sc_o0 = ucp->uc_mcontext.gregs[REG_O0];
90 	if (ucp->uc_mcontext.gwins != (gwindows_t *)0) {
91 		gwinswitch = 1;
92 		sc.sc_wbcnt = ucp->uc_mcontext.gwins->wbcnt;
93 		for (i = 0; i < MAXWINDOW; i++) {
94 			for (j = 0; j < 16; j++)
95 				sc.sc_spbuf[i][j] = (int)ucp->uc_mcontext.gwins->spbuf[j];
96 			for (j = 0; j < 8; j++)
97 				sc.sc_wbuf[i][j] = ucp->uc_mcontext.gwins->wbuf[i].rw_local[j];
98 			for (j = 0; j < 8; j++)
99 				sc.sc_wbuf[i][j+8] = ucp->uc_mcontext.gwins->wbuf[i].rw_in[j];
100 		}
101 	}
102 	/*
103 	 * Machine dependent code ends
104 	 */
105 
106 	if (sip != NULL)
107 		if ((code = sip->si_code) == BUS_OBJERR)
108 			code = SEGV_MAKE_ERR(sip->si_errno);
109 
110 	if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS)
111 		if (sip != NULL)
112 			addr = (char *)sip->si_addr;
113 	else
114 		addr = SIG_NOADDR;
115 
116 	(*_siguhandler[sig])(sig, code, &sc, addr);
117 
118 	if (sc.sc_onstack)
119 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
120 	else
121 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
122 	mask2set(sc.sc_mask, &ucp->uc_sigmask);
123 
124 	/*
125 	 * Machine dependent code begins
126 	 */
127 	ucp->uc_mcontext.gregs[REG_O6] = sc.sc_sp;
128 	ucp->uc_mcontext.gregs[REG_PC] = sc.sc_pc;
129 	ucp->uc_mcontext.gregs[REG_nPC] = sc.sc_npc;
130 	ucp->uc_mcontext.gregs[REG_PSR] = sc.sc_psr;
131 	ucp->uc_mcontext.gregs[REG_G1] = sc.sc_g1;
132 	ucp->uc_mcontext.gregs[REG_O0] = sc.sc_o0;
133 	if (gwinswitch == 1) {
134 		ucp->uc_mcontext.gwins->wbcnt = sc.sc_wbcnt;
135 		for (i = 0; i < MAXWINDOW; i++) {
136 			for (j = 0; j < 16; j++)
137 				ucp->uc_mcontext.gwins->spbuf[j] = (greg_t *)sc.sc_spbuf[i][j];
138 			for (j = 0; j < 8; j++)
139 				ucp->uc_mcontext.gwins->wbuf[i].rw_local[j] = sc.sc_wbuf[i][j];
140 			for (j = 0; j < 8; j++)
141 				ucp->uc_mcontext.gwins->wbuf[i].rw_in[j] = sc.sc_wbuf[i][j+8];
142 		}
143 	}
144 	/*
145 	 * Machine dependent code ends
146 	 */
147 
148 	setcontext (ucp);
149 }
150 
151 sigsetmask(mask)
152 	int mask;
153 {
154 	sigset_t oset;
155 	sigset_t nset;
156 
157 	(void) sigprocmask(0, (sigset_t *)0, &nset);
158 	mask2set(mask, &nset);
159 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
160 	return set2mask(&oset);
161 }
162 
163 sigblock(mask)
164 	int mask;
165 {
166 	sigset_t oset;
167 	sigset_t nset;
168 
169 	(void) sigprocmask(0, (sigset_t *)0, &nset);
170 	mask2set(mask, &nset);
171 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
172 	return set2mask(&oset);
173 }
174 
175 sigpause(mask)
176 	int mask;
177 {
178 	sigset_t set;
179 
180 	(void) sigprocmask(0, (sigset_t *)0, &set);
181 	mask2set(mask, &set);
182 	return (sigsuspend(&set));
183 }
184 
185 sigvec(sig, nvec, ovec)
186         int sig;
187         struct sigvec *nvec;
188 	struct sigvec *ovec;
189 {
190         struct sigaction nact;
191         struct sigaction oact;
192         struct sigaction *nactp;
193         void (*ohandler)(), (*nhandler)();
194 
195         if (sig <= 0 || sig >= NSIG) {
196                 errno = EINVAL;
197                 return -1;
198         }
199 
200         ohandler = _siguhandler[sig];
201 
202         if (nvec) {
203 		_sigaction(sig, (struct sigaction *)0, &nact);
204                 nhandler = nvec->sv_handler;
205                 _siguhandler[sig] = nhandler;
206                 if (nhandler != SIG_DFL && nhandler != SIG_IGN)
207                         nact.sa_handler = (void (*)())sigvechandler;
208 		else
209 			nact.sa_handler = nhandler;
210 		mask2set(nvec->sv_mask, &nact.sa_mask);
211 		/*
212 		if ( sig == SIGTSTP || sig == SIGSTOP )
213 			nact.sa_handler = SIG_DFL; 	*/
214 		nact.sa_flags = SA_SIGINFO;
215 		if (!(nvec->sv_flags & SV_INTERRUPT))
216 			nact.sa_flags |= SA_RESTART;
217 		if (nvec->sv_flags & SV_RESETHAND)
218 			nact.sa_flags |= SA_RESETHAND;
219 		if (nvec->sv_flags & SV_ONSTACK)
220 			nact.sa_flags |= SA_ONSTACK;
221 		nactp = &nact;
222         } else
223 		nactp = (struct sigaction *)0;
224 
225         if (_sigaction(sig, nactp, &oact) < 0) {
226                 _siguhandler[sig] = ohandler;
227                 return -1;
228         }
229 
230         if (ovec) {
231 		if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN)
232 			ovec->sv_handler = oact.sa_handler;
233 		else
234 			ovec->sv_handler = ohandler;
235 		ovec->sv_mask = set2mask(&oact.sa_mask);
236 		ovec->sv_flags = 0;
237 		if (oact.sa_flags & SA_ONSTACK)
238 			ovec->sv_flags |= SV_ONSTACK;
239 		if (oact.sa_flags & SA_RESETHAND)
240 			ovec->sv_flags |= SV_RESETHAND;
241 		if (!(oact.sa_flags & SA_RESTART))
242 			ovec->sv_flags |= SV_INTERRUPT;
243 	}
244 
245         return 0;
246 }
247 
248 
249 void (*
250 signal(s, a))()
251         int s;
252         void (*a)();
253 {
254         struct sigvec osv;
255 	struct sigvec nsv;
256         static int mask[NSIG];
257         static int flags[NSIG];
258 
259 	nsv.sv_handler = a;
260 	nsv.sv_mask = mask[s];
261 	nsv.sv_flags = flags[s];
262         if (sigvec(s, &nsv, &osv) < 0)
263                 return (SIG_ERR);
264         if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) {
265                 mask[s] = nsv.sv_mask = osv.sv_mask;
266                 flags[s] = nsv.sv_flags = osv.sv_flags & ~SV_RESETHAND;
267                 if (sigvec(s, &nsv, (struct sigvec *)0) < 0)
268                         return (SIG_ERR);
269         }
270         return (osv.sv_handler);
271 }
272