ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
Revision: 1.1
Committed: 2001-05-20T20:31:50Z (23 years, 3 months ago) by gbeauche
Branch: MAIN
Log Message:
- new and updated SIGSEGV support functions:
  + configure script cleanups
  + possible support for Direct Addressing / VOSF on other platforms

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * sigsegv.cpp - SIGSEGV signals support
3     *
4     * Derived from Bruno Haible's work on his SIGSEGV library for clisp
5     * <http://clisp.sourceforge.net/>
6     *
7     * Basilisk II (C) 1997-2001 Christian Bauer
8     *
9     * This program is free software; you can redistribute it and/or modify
10     * it under the terms of the GNU General Public License as published by
11     * the Free Software Foundation; either version 2 of the License, or
12     * (at your option) any later version.
13     *
14     * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18     *
19     * You should have received a copy of the GNU General Public License
20     * along with this program; if not, write to the Free Software
21     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22     */
23    
24     #ifdef HAVE_UNISTD_H
25     #include <unistd.h>
26     #endif
27    
28     #ifdef HAVE_CONFIG_H
29     #include "config.h"
30     #endif
31    
32     #include <signal.h>
33     #include "sigsegv.h"
34    
35     // Return value type of a signal handler (standard type if not defined)
36     #ifndef RETSIGTYPE
37     #define RETSIGTYPE void
38     #endif
39    
40     // Type of the system signal handler
41     typedef RETSIGTYPE (*signal_handler)(int);
42    
43     // User's SIGSEGV handler
44     static sigsegv_handler_t sigsegv_user_handler = 0;
45    
46     // Actual SIGSEGV handler installer
47     static bool sigsegv_do_install_handler(int sig);
48    
49    
50     /*
51     * OS-dependant SIGSEGV signals support section
52     */
53    
54     #if HAVE_SIGINFO_T
55     // Generic extended signal handler
56     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
57     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *
58     #define SIGSEGV_FAULT_ADDRESS sip->si_addr
59     #endif
60    
61     #if HAVE_SIGCONTEXT_SUBTERFUGE
62     // Linux kernels prior to 2.4 ?
63     #if defined(__linux__)
64     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
65     #if (defined(i386) || defined(__i386__))
66     #include <asm/sigcontext.h>
67     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext scs
68     #define SIGSEGV_FAULT_ADDRESS scs.cr2
69     #define SIGSEGV_FAULT_INSTRUCTION scs.eip
70     #endif
71     #if (defined(sparc) || defined(__sparc__))
72     #include <asm/sigcontext.h>
73     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext* scp, char* addr
74     #define SIGSEGV_FAULT_ADDRESS addr
75     #endif
76     #if (defined(powerpc) || defined(__powerpc__))
77     #include <asm/sigcontext.h>
78     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, struct sigcontext* scp
79     #define SIGSEGV_FAULT_ADDRESS scp->regs->dar
80     #define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip
81     #endif
82     #endif
83    
84     // Irix 5 or 6 on MIPS
85     #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
86     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
87     #define SIGSEGV_FAULT_ADDRESS scp->sc_badvaddr
88     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
89     #endif
90    
91     // OSF/1 on Alpha
92     #if defined(__osf__)
93     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
94     #define SIGSEGV_FAULT_ADDRESS scp->sc_traparg_a0
95     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
96     #endif
97    
98     // AIX
99     #if defined(_AIX)
100     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
101     #define SIGSEGV_FAULT_ADDRESS scp->sc_jmpbuf.jmp_context.o_vaddr
102     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
103     #endif
104    
105     // NetBSD or FreeBSD
106     #if defined(__NetBSD__) || defined(__FreeBSD__)
107     #if (defined(m68k) || defined(__m68k__))
108     #include <m68k/frame.h>
109     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
110     #define SIGSEGV_FAULT_ADDRESS ({ \
111     struct sigstate { \
112     int ss_flags; \
113     struct frame ss_frame; \
114     }; \
115     struct sigstate *state = (struct sigstate *)scp->sc_ap; \
116     char *fault_addr; \
117     switch (state->ss_frame.f_format) { \
118     case 7: /* 68040 access error */ \
119     /* "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown */ \
120     fault_addr = state->ss_frame.f_fmt7.f_fa; \
121     break; \
122     default: \
123     fault_addr = (char *)code; \
124     break; \
125     } \
126     fault_addr; \
127     })
128     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV)
129     #else
130     #define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, void *scp, char *addr
131     #define SIGSEGV_FAULT_ADDRESS addr
132     #define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS)
133     #endif
134     #endif
135     #endif
136    
137     // Fallbacks
138     #ifndef SIGSEGV_FAULT_INSTRUCTION
139     #define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC
140     #endif
141    
142    
143     /*
144     * SIGSEGV global handler
145     */
146    
147     static void sigsegv_handler(SIGSEGV_FAULT_HANDLER_ARGLIST)
148     {
149     // Call user's handler and reinstall the global handler, if required
150     if (sigsegv_user_handler((sigsegv_address_t)SIGSEGV_FAULT_ADDRESS, (sigsegv_address_t)SIGSEGV_FAULT_INSTRUCTION)) {
151     #if (defined(HAVE_SIGACTION) ? defined(SIGACTION_NEED_REINSTALL) : defined(SIGNAL_NEED_REINSTALL))
152     sigsegv_do_install_handler(sig);
153     #endif
154     }
155     else {
156     // FAIL: reinstall default handler for "safe" crash
157     #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
158     SIGSEGV_ALL_SIGNALS
159     #undef FAULT_HANDLER
160     }
161     }
162    
163    
164     /*
165     * SIGSEGV handler initialization
166     */
167    
168     #if defined(HAVE_SIGINFO_T)
169     static bool sigsegv_do_install_handler(int sig)
170     {
171     // Setup SIGSEGV handler to process writes to frame buffer
172     #ifdef HAVE_SIGACTION
173     struct sigaction vosf_sa;
174     sigemptyset(&vosf_sa.sa_mask);
175     vosf_sa.sa_sigaction = sigsegv_handler;
176     vosf_sa.sa_flags = SA_SIGINFO;
177     return (sigaction(sig, &vosf_sa, 0) == 0);
178     #else
179     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
180     #endif
181     }
182     #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
183     static bool sigsegv_do_install_handler(int sig)
184     {
185     // Setup SIGSEGV handler to process writes to frame buffer
186     #ifdef HAVE_SIGACTION
187     struct sigaction vosf_sa;
188     sigemptyset(&vosf_sa.sa_mask);
189     vosf_sa.sa_handler = (signal_handler)sigsegv_handler;
190     #if !EMULATED_68K && defined(__NetBSD__)
191     sigaddset(&vosf_sa.sa_mask, SIGALRM);
192     vosf_sa.sa_flags = SA_ONSTACK;
193     #else
194     vosf_sa.sa_flags = 0;
195     #endif
196     return (sigaction(sig, &vosf_sa, 0) == 0);
197     #else
198     return (signal(sig, (signal_handler)sigsegv_handler) != SIG_ERR);
199     #endif
200     }
201     #endif
202    
203     bool sigsegv_install_handler(sigsegv_handler_t handler)
204     {
205     #if defined(HAVE_SIGINFO_T) || defined(HAVE_SIGCONTEXT_SUBTERFUGE)
206     sigsegv_user_handler = handler;
207     bool success = true;
208     #define FAULT_HANDLER(sig) success = success && sigsegv_do_install_handler(sig);
209     SIGSEGV_ALL_SIGNALS
210     #undef FAULT_HANDLER
211     return success;
212     #else
213     // FAIL: no siginfo_t nor sigcontext subterfuge is available
214     return false;
215     #endif
216     }
217    
218    
219     /*
220     * SIGSEGV handler deinitialization
221     */
222    
223     void sigsegv_deinstall_handler(void)
224     {
225     sigsegv_user_handler = 0;
226     #define FAULT_HANDLER(sig) signal(sig, SIG_DFL);
227     SIGSEGV_ALL_SIGNALS
228     #undef FAULT_HANDLER
229     }
230    
231     /*
232     * Test program used for configure/test
233     */
234    
235     #ifdef CONFIGURE_TEST
236     #include <stdio.h>
237     #include <stdlib.h>
238     #include <unistd.h>
239     #include <fcntl.h>
240     #include <sys/mman.h>
241    
242     static caddr_t page = 0;
243     static int page_size;
244     static int handler_called = 0;
245    
246     static bool sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
247     {
248     handler_called++;
249     if ((fault_address - 123) != page)
250     exit(1);
251     if (mprotect((caddr_t)((unsigned long)fault_address & -page_size), page_size, PROT_READ | PROT_WRITE) != 0)
252     exit(1);
253     return true;
254     }
255    
256     int main(void)
257     {
258     int zero_fd = open("/dev/zero", O_RDWR);
259     if (zero_fd < 0)
260     return 1;
261    
262     page_size = getpagesize();
263     page = (caddr_t)mmap(0, page_size, PROT_READ, MAP_PRIVATE, zero_fd, 0);
264     if (page == MAP_FAILED)
265     return 1;
266    
267     if (!sigsegv_install_handler(sigsegv_test_handler))
268     return 1;
269    
270     page[123] = 45;
271     page[123] = 45;
272    
273     if (handler_called != 1)
274     return 1;
275    
276     return 0;
277     }
278     #endif