comparison packages/language/c/libc/signals/current/tests/signal2.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 0ec04793409a
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //========================================================================
2 //
3 // signal2.c
4 //
5 // ISO C signal handling test
6 //
7 //========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): jlarmour
35 // Contributors:
36 // Date: 2000-04-18
37 // Purpose: Test hardware signal functionality
38 // Description: This file contains a number of tests for ISO C signal
39 // handling when used with hardware exceptions
40 // Usage:
41 //
42 //####DESCRIPTIONEND####
43 //
44 //========================================================================
45
46 // CONFIGURATION
47
48 #include <pkgconf/system.h>
49 #include <pkgconf/libc_signals.h> // C library signals configuration
50
51 // INCLUDES
52
53 #include <cyg/infra/cyg_type.h> // Common type definitions and support
54 #include <cyg/infra/testcase.h> // Test infrastructure
55
56 #ifdef CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
57
58 #include <cyg/hal/hal_intr.h> // exception ranges &
59 // HAL_VSR_SET_TO_ECOS_HANDLER
60
61 #include <signal.h> // Signal functions
62 #include <setjmp.h> // setjmp(), longjmp()
63
64 // STATICS
65
66 static int state;
67 static jmp_buf jbuf;
68 #endif
69
70 // FUNCTIONS
71
72 #ifdef CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
73
74 static void
75 myhandler(int sig)
76 {
77 __sighandler_t handler1;
78
79 CYG_TEST_INFO("myhandler() called");
80 ++state;
81
82 handler1 = signal(sig, &myhandler);
83
84 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
85 "handler reset itself to default");
86
87 longjmp(jbuf, 1);
88 } // myhandler()
89
90 static void
91 cause_memerror(void)
92 {
93 volatile int x;
94 volatile CYG_ADDRESS p=(CYG_ADDRESS) &state;
95
96 do {
97 // do a read which prevents us accidentally writing over something
98 // important. Make it misaligned to increase the chances of an
99 // exception happening
100 x = *(volatile int *)(p+1);
101 p += (CYG_ADDRESS)0x100000;
102 } while(p != 0);
103 } // cause_memerror()
104
105 // num must always be 0 - do it this way in case the optimizer tries to
106 // get smart
107 static int
108 cause_fpe(int num)
109 {
110 double a;
111
112 a = 1.0/num; // Depending on FPU emulation and/or
113 // the FPU architecture, this may
114 // cause an exception.
115 // (float division by zero)
116
117 return ((int)a)/num; // This may cause an exception if
118 // the architecture supports it.
119 // (integer division by zero).
120 } // cause_fpe()
121
122 #endif
123
124 int
125 main( int argc, char *argv[] )
126 {
127 #ifdef CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
128 __sighandler_t handler1;
129
130 // special callout to request GDB to alter its handling of signals
131 CYG_TEST_GDBCMD("handle SIGBUS nostop");
132 CYG_TEST_GDBCMD("handle SIGSEGV nostop");
133 CYG_TEST_GDBCMD("handle SIGILL nostop");
134 CYG_TEST_GDBCMD("handle SIGFPE nostop");
135 CYG_TEST_GDBCMD("handle SIGSYS nostop");
136
137 // Not sure about this - Jifl. Do any of our targets generate SIGTRAP anyway?
138 #if 0
139 #ifndef CYGPKG_HAL_I386_LINUX
140 CYG_TEST_GDBCMD("handle SIGTRAP nostop pass");
141 #endif
142 #endif
143 #endif // ifdef CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
144
145 CYG_TEST_INIT();
146
147 #ifdef CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
148 // Avoid compiler warnings if tests are not applicable.
149 if (0) cause_memerror();
150 if (0) cause_fpe(0);
151 #endif
152
153 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C "
154 "library signal functions");
155
156 // Now reset the exception handlers various to eCos handlers so that we
157 // have control; this is the target side equivalent of the CYG_TEST_GDBCMD
158 // lines above:
159 #ifdef HAL_VSR_SET_TO_ECOS_HANDLER
160 // Reclaim the VSR off CygMon possibly
161 #ifdef CYGNUM_HAL_EXCEPTION_DATA_ACCESS
162 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_ACCESS, NULL );
163 #endif
164 #ifdef CYGNUM_HAL_EXCEPTION_DATA_TLBMISS_ACCESS
165 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_TLBMISS_ACCESS, NULL );
166 #endif
167 #ifdef CYGNUM_HAL_EXCEPTION_DATA_UNALIGNED_ACCESS
168 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_UNALIGNED_ACCESS, NULL );
169 #endif
170 #ifdef CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION
171 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION, NULL );
172 #endif
173 #ifdef CYGNUM_HAL_EXCEPTION_DIV_BY_ZERO
174 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DIV_BY_ZERO, NULL );
175 #endif
176 #endif
177
178 #ifdef CYGSEM_LIBC_SIGNALS_HWEXCEPTIONS
179
180
181 // Test 1
182
183 CYG_TEST_INFO("Test 1");
184
185 handler1 = signal(SIGSEGV, &myhandler);
186
187 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
188 "SIGSEGV handler initialized to default");
189
190
191 handler1 = signal(SIGBUS, &myhandler);
192
193 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
194 "SIGBUS handler initialized to default");
195
196
197 handler1 = signal(SIGILL, &myhandler);
198
199 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
200 "SIGILL handler initialized to default");
201
202 handler1 = signal(SIGSYS, &myhandler);
203
204 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
205 "SIGSYS handler initialized to default");
206
207 handler1 = signal(SIGTRAP, &myhandler);
208
209 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
210 "SIGTRAP handler initialized to default");
211
212 handler1 = signal(SIGFPE, &myhandler);
213
214 CYG_TEST_PASS_FAIL(handler1 == SIG_DFL,
215 "SIGFPE handler initialized to default");
216
217 // Test 2
218
219 CYG_TEST_INFO("Test 2");
220
221 state = 2;
222
223 #if defined(CYGPKG_HAL_POWERPC_SIM)
224 // The exception generated by the SIM is not recognized by GDB.
225 // PR 19945 workaround.
226 CYG_TEST_PASS("Test 2 not applicable to PowerPC SIM");
227 #else
228 if (0==setjmp(jbuf)) {
229 cause_memerror();
230 CYG_TEST_FAIL("Didn't cause exception");
231 }
232
233 CYG_TEST_PASS_FAIL(3==state, "handler returned correctly");
234 #endif
235
236 // Test 3
237
238 CYG_TEST_INFO("Test 3");
239
240 state = 3;
241
242 #if defined(CYGPKG_HAL_SH_EDK7708)
243 CYG_TEST_PASS("Test 3 not applicable to sh 7708");
244 #elif defined(CYGPKG_HAL_MN10300)
245 CYG_TEST_PASS("Test 3 not applicable to mn10300");
246 #elif defined(CYGPKG_HAL_MIPS_TX39)
247 CYG_TEST_PASS("Test 3 not applicable to mips-tx39");
248 #elif defined(CYGPKG_HAL_SPARCLITE)
249 CYG_TEST_PASS("Test 3 not applicable to sparclite");
250 #elif defined(CYGPKG_HAL_I386_LINUX)
251 CYG_TEST_PASS("Test 3 not applicable to i386/Linux");
252 // See linux.S for details.
253 #elif defined(CYGPKG_HAL_POWERPC)
254 CYG_TEST_PASS("Test 3 not applicable to PowerPC");
255 #elif defined(CYGPKG_HAL_ARM)
256 CYG_TEST_PASS("Test 3 not applicable to ARM");
257 #else
258
259 if (0==setjmp(jbuf)) {
260 (void)cause_fpe(0);
261 CYG_TEST_FAIL("Didn't cause exception");
262 }
263
264 CYG_TEST_PASS_FAIL(4==state, "handler returned correctly");
265 #endif
266
267 #else
268 CYG_TEST_NA("Testing not applicable to this configuration");
269 #endif
270
271 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C "
272 "library signal functions");
273
274 } // main()
275
276 // EOF signal2.c