1 |
27 |
unneback |
/*=================================================================
|
2 |
|
|
//
|
3 |
|
|
// kexcept1.cxx
|
4 |
|
|
//
|
5 |
|
|
// Kernel C API Exception test 1
|
6 |
|
|
//
|
7 |
|
|
//=================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//=================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): dsm
|
44 |
|
|
// Contributors: dsm, jlarmour
|
45 |
|
|
// Date: 1999-02-16
|
46 |
|
|
// Description: Test basic exception functionality
|
47 |
|
|
//####DESCRIPTIONEND####
|
48 |
|
|
*/
|
49 |
|
|
|
50 |
|
|
#include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
|
51 |
|
|
|
52 |
|
|
#include <cyg/kernel/kapi.h>
|
53 |
|
|
|
54 |
|
|
#include <cyg/infra/testcase.h>
|
55 |
|
|
|
56 |
|
|
#ifdef CYGPKG_KERNEL_EXCEPTIONS
|
57 |
|
|
|
58 |
|
|
#ifdef CYGFUN_KERNEL_API_C
|
59 |
|
|
|
60 |
|
|
#include <cyg/hal/hal_intr.h> // exception ranges
|
61 |
|
|
|
62 |
|
|
#include "testaux.h"
|
63 |
|
|
|
64 |
|
|
#ifndef CYGPKG_HAL_ARM_PID
|
65 |
|
|
#define EXCEPTION_DATA_ACCESS
|
66 |
|
|
#endif
|
67 |
|
|
|
68 |
|
|
#define NTHREADS 1
|
69 |
|
|
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
|
70 |
|
|
|
71 |
|
|
static cyg_handle_t thread[NTHREADS];
|
72 |
|
|
|
73 |
|
|
static cyg_thread thread_obj[NTHREADS];
|
74 |
|
|
static char stack[NTHREADS][STACKSIZE];
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
#ifdef EXCEPTION_DATA_ACCESS
|
79 |
|
|
static cyg_exception_handler_t handler0;
|
80 |
|
|
|
81 |
|
|
static void handler0(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info)
|
82 |
|
|
{
|
83 |
|
|
CYG_TEST_INFO("handler 0 called");
|
84 |
|
|
|
85 |
|
|
CYG_TEST_CHECK((cyg_addrword_t)123 == data, "handler given wrong data");
|
86 |
|
|
|
87 |
|
|
// ignore machine specific stuff
|
88 |
|
|
CYG_UNUSED_PARAM(cyg_code_t, number);
|
89 |
|
|
CYG_UNUSED_PARAM(cyg_addrword_t, info);
|
90 |
|
|
|
91 |
|
|
CYG_TEST_PASS_FINISH("Except 1 OK");
|
92 |
|
|
}
|
93 |
|
|
#endif
|
94 |
|
|
|
95 |
|
|
static int d0;
|
96 |
|
|
|
97 |
|
|
static void handler1(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info)
|
98 |
|
|
{
|
99 |
|
|
CYG_TEST_INFO("handler 1 called");
|
100 |
|
|
|
101 |
|
|
CYG_TEST_CHECK((cyg_addrword_t)&d0 == data, "handler given wrong data");
|
102 |
|
|
|
103 |
|
|
#ifdef CYGSEM_KERNEL_EXCEPTIONS_DECODE
|
104 |
|
|
CYG_TEST_CHECK(number == CYGNUM_HAL_EXCEPTION_MAX, "handler given wrong number");
|
105 |
|
|
#else
|
106 |
|
|
CYG_UNUSED_PARAM(cyg_code_t, number);
|
107 |
|
|
#endif
|
108 |
|
|
|
109 |
|
|
CYG_TEST_CHECK((cyg_addrword_t)99 == info, "handler given wrong info");
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
#ifdef EXCEPTION_DATA_ACCESS
|
114 |
|
|
// The following function attempts to cause an exception in various
|
115 |
|
|
// hacky ways. It is machine dependent what exception is generated.
|
116 |
|
|
// It does reads rather than writes hoping not to corrupt anything
|
117 |
|
|
// important.
|
118 |
|
|
static int
|
119 |
|
|
cause_fpe(int num)
|
120 |
|
|
{
|
121 |
|
|
double a;
|
122 |
|
|
|
123 |
|
|
a = 1.0/num; // Depending on FPU emulation and/or
|
124 |
|
|
// the FPU architecture, this may
|
125 |
|
|
// cause an exception.
|
126 |
|
|
// (float division by zero)
|
127 |
|
|
|
128 |
|
|
return ((int)a)/num; // This may cause an exception if
|
129 |
|
|
// the architecture supports it.
|
130 |
|
|
// (integer division by zero).
|
131 |
|
|
} // cause_fpe()
|
132 |
|
|
|
133 |
|
|
void cause_exception(void)
|
134 |
|
|
{
|
135 |
|
|
int x;
|
136 |
|
|
unsigned int p=0;
|
137 |
|
|
|
138 |
|
|
// First try for an address exception (unaligned access exception
|
139 |
|
|
// or SEGV/BUS exceptions)
|
140 |
|
|
do {
|
141 |
|
|
x=*(volatile int *)(p-1);
|
142 |
|
|
p+=0x100000;
|
143 |
|
|
} while(p != 0);
|
144 |
|
|
|
145 |
|
|
// Next try an integer or floating point divide-by-zero exception.
|
146 |
|
|
cause_fpe(0);
|
147 |
|
|
}
|
148 |
|
|
#endif
|
149 |
|
|
|
150 |
|
|
static void entry0( CYG_ADDRWORD data )
|
151 |
|
|
{
|
152 |
|
|
#ifdef EXCEPTION_DATA_ACCESS
|
153 |
|
|
cyg_code_t n;
|
154 |
|
|
#endif
|
155 |
|
|
cyg_exception_handler_t *old_handler, *old_handler1;
|
156 |
|
|
cyg_addrword_t old_data, old_data1;
|
157 |
|
|
|
158 |
|
|
CYG_UNUSED_PARAM(CYG_ADDRESS, data);
|
159 |
|
|
|
160 |
|
|
cyg_exception_set_handler(
|
161 |
|
|
CYGNUM_HAL_EXCEPTION_MAX,
|
162 |
|
|
&handler1,
|
163 |
|
|
(cyg_addrword_t)&d0,
|
164 |
|
|
&old_handler,
|
165 |
|
|
&old_data);
|
166 |
|
|
|
167 |
|
|
cyg_exception_set_handler(
|
168 |
|
|
CYGNUM_HAL_EXCEPTION_MAX,
|
169 |
|
|
&handler1,
|
170 |
|
|
(cyg_addrword_t)&d0,
|
171 |
|
|
&old_handler1,
|
172 |
|
|
&old_data1);
|
173 |
|
|
|
174 |
|
|
CYG_TEST_CHECK(old_handler1 == &handler1,
|
175 |
|
|
"register exception: old_handler not the one previously registered");
|
176 |
|
|
CYG_TEST_CHECK(old_data1 == (cyg_addrword_t)&d0,
|
177 |
|
|
"register exception: old_data not those previously registered");
|
178 |
|
|
|
179 |
|
|
cyg_exception_call_handler(
|
180 |
|
|
cyg_thread_self(),
|
181 |
|
|
CYGNUM_HAL_EXCEPTION_MAX,
|
182 |
|
|
(cyg_addrword_t)99);
|
183 |
|
|
|
184 |
|
|
CYG_TEST_INFO("handler 1 returned");
|
185 |
|
|
|
186 |
|
|
cyg_exception_clear_handler(CYGNUM_HAL_EXCEPTION_MAX);
|
187 |
|
|
cyg_exception_clear_handler(CYGNUM_HAL_EXCEPTION_MAX);
|
188 |
|
|
|
189 |
|
|
#ifdef EXCEPTION_DATA_ACCESS
|
190 |
|
|
|
191 |
|
|
#if 0
|
192 |
|
|
#elif defined(CYGPKG_HAL_POWERPC_SIM)
|
193 |
|
|
// The exception generated by the SIM is not recognized by GDB.
|
194 |
|
|
// PR 19945 workaround.
|
195 |
|
|
CYG_TEST_NA("Not applicable to PowerPC SIM");
|
196 |
|
|
#endif
|
197 |
|
|
|
198 |
|
|
for(n = CYGNUM_HAL_EXCEPTION_MIN; n <= CYGNUM_HAL_EXCEPTION_MAX; n++) {
|
199 |
|
|
cyg_exception_set_handler(
|
200 |
|
|
n,
|
201 |
|
|
handler0,
|
202 |
|
|
(cyg_addrword_t)123,
|
203 |
|
|
&old_handler1,
|
204 |
|
|
&old_data1);
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
CYG_TEST_PASS("Attempting to provoke exception");
|
208 |
|
|
|
209 |
|
|
cause_exception();
|
210 |
|
|
|
211 |
|
|
CYG_TEST_FAIL_FINISH("Couldn't cause exception");
|
212 |
|
|
#else // EXCEPTION_DATA_ACCESS
|
213 |
|
|
CYG_TEST_NA("Platform does not support data exceptions");
|
214 |
|
|
#endif
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
#ifdef CYG_HAL_MIPS_TX39_JMR3904
|
218 |
|
|
|
219 |
|
|
extern void __default_exception_vsr(void);
|
220 |
|
|
|
221 |
|
|
#endif
|
222 |
|
|
|
223 |
|
|
void except0_main( void )
|
224 |
|
|
{
|
225 |
|
|
// Use CYG_TEST_GDBCMD _before_ CYG_TEST_INIT()
|
226 |
|
|
CYG_TEST_GDBCMD("handle SIGBUS nostop");
|
227 |
|
|
CYG_TEST_GDBCMD("handle SIGSEGV nostop");
|
228 |
|
|
CYG_TEST_GDBCMD("handle SIGFPE nostop");
|
229 |
|
|
|
230 |
|
|
CYG_TEST_INIT();
|
231 |
|
|
|
232 |
|
|
#ifdef HAL_VSR_SET_TO_ECOS_HANDLER
|
233 |
|
|
// Reclaim the VSR off CygMon possibly
|
234 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_DATA_ACCESS
|
235 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_ACCESS, NULL );
|
236 |
|
|
#endif
|
237 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_DATA_TLBMISS_ACCESS
|
238 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_TLBMISS_ACCESS, NULL );
|
239 |
|
|
#endif
|
240 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_DATA_UNALIGNED_ACCESS
|
241 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_UNALIGNED_ACCESS, NULL );
|
242 |
|
|
#endif
|
243 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION
|
244 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION, NULL );
|
245 |
|
|
#endif
|
246 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_DIV_BY_ZERO
|
247 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DIV_BY_ZERO, NULL );
|
248 |
|
|
#endif
|
249 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_FPU
|
250 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_FPU, NULL );
|
251 |
|
|
#endif
|
252 |
|
|
#ifdef CYGNUM_HAL_EXCEPTION_FPU_DIV_BY_ZERO
|
253 |
|
|
HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_FPU_DIV_BY_ZERO, NULL );
|
254 |
|
|
#endif
|
255 |
|
|
#endif
|
256 |
|
|
|
257 |
|
|
cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kexcept1",
|
258 |
|
|
(void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
|
259 |
|
|
cyg_thread_resume(thread[0]);
|
260 |
|
|
|
261 |
|
|
cyg_scheduler_start();
|
262 |
|
|
|
263 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
externC void
|
267 |
|
|
cyg_start( void )
|
268 |
|
|
{
|
269 |
|
|
except0_main();
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
#else // def CYGFUN_KERNEL_API_C
|
273 |
|
|
#define N_A_MSG "Kernel C API layer disabled"
|
274 |
|
|
#endif // def CYGFUN_KERNEL_API_C
|
275 |
|
|
#else // def CYGPKG_KERNEL_EXCEPTIONS
|
276 |
|
|
#define N_A_MSG "Exceptions disabled"
|
277 |
|
|
#endif // def CYGPKG_KERNEL_EXCEPTIONS
|
278 |
|
|
|
279 |
|
|
#ifdef N_A_MSG
|
280 |
|
|
externC void
|
281 |
|
|
cyg_start( void )
|
282 |
|
|
{
|
283 |
|
|
CYG_TEST_INIT();
|
284 |
|
|
CYG_TEST_NA( N_A_MSG);
|
285 |
|
|
}
|
286 |
|
|
#endif // N_A_MSG
|
287 |
|
|
|
288 |
|
|
/* EOF kexcept1.c */
|