|
0
|
1 /*================================================================= |
|
|
2 // |
|
|
3 // kexcept1.cxx |
|
|
4 // |
|
|
5 // Kernel C API Exception test 1 |
|
|
6 // |
|
|
7 //================================================================= |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (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://sourceware.cygnus.com/ecos |
|
|
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 Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): dsm |
|
2
|
33 // Contributors: dsm, jlarmour |
|
|
34 // Date: 1999-02-16 |
|
0
|
35 // Description: Test basic exception functionality |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 */ |
|
|
38 |
|
2
|
39 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
|
40 |
|
0
|
41 #include <cyg/kernel/kapi.h> |
|
|
42 |
|
|
43 #include <cyg/infra/testcase.h> |
|
|
44 |
|
|
45 #ifdef CYGPKG_KERNEL_EXCEPTIONS |
|
|
46 |
|
|
47 #ifdef CYGFUN_KERNEL_API_C |
|
|
48 |
|
|
49 #include <cyg/hal/hal_intr.h> // exception ranges |
|
|
50 |
|
|
51 #include "testaux.h" |
|
|
52 |
|
|
53 #define NTHREADS 1 |
|
2
|
54 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
0
|
55 |
|
|
56 static cyg_handle_t thread[NTHREADS]; |
|
|
57 |
|
|
58 static cyg_thread thread_obj[NTHREADS]; |
|
|
59 static char stack[NTHREADS][STACKSIZE]; |
|
|
60 |
|
|
61 |
|
|
62 static cyg_exception_handler_t handler0; |
|
|
63 static int d0; |
|
|
64 |
|
|
65 static void handler0(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info) |
|
|
66 { |
|
|
67 CYG_TEST_INFO("handler 0 called"); |
|
|
68 |
|
|
69 CYG_TEST_CHECK((cyg_addrword_t)123 == data, "handler given wrong data"); |
|
|
70 |
|
|
71 // ignore machine specific stuff |
|
|
72 CYG_UNUSED_PARAM(cyg_code_t, number); |
|
|
73 CYG_UNUSED_PARAM(cyg_addrword_t, info); |
|
|
74 |
|
|
75 CYG_TEST_PASS_FINISH("Except 1 OK"); |
|
|
76 } |
|
|
77 |
|
|
78 static void handler1(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info) |
|
|
79 { |
|
|
80 CYG_TEST_INFO("handler 1 called"); |
|
|
81 |
|
|
82 CYG_TEST_CHECK((cyg_addrword_t)&d0 == data, "handler given wrong data"); |
|
|
83 |
|
|
84 #ifdef CYGSEM_KERNEL_EXCEPTIONS_DECODE |
|
2
|
85 CYG_TEST_CHECK(number == CYGNUM_HAL_EXCEPTION_MAX, "handler given wrong number"); |
|
0
|
86 #else |
|
|
87 CYG_UNUSED_PARAM(cyg_code_t, number); |
|
|
88 #endif |
|
|
89 |
|
|
90 CYG_TEST_CHECK((cyg_addrword_t)99 == info, "handler given wrong info"); |
|
|
91 } |
|
|
92 |
|
|
93 |
|
|
94 // The following function attempts to cause an exception in various |
|
|
95 // hacky ways. It is machine dependent what exception is generated. |
|
|
96 // It does reads rather than writes hoping not to corrupt anything |
|
|
97 // important. |
|
|
98 void cause_exception(void) |
|
|
99 { |
|
|
100 int x; |
|
|
101 unsigned int p=0; |
|
|
102 |
|
|
103 do { |
|
|
104 x=*(volatile int *)(p-1)/p; |
|
|
105 p+=0x100000; |
|
|
106 } while(p != 0); |
|
|
107 } |
|
|
108 |
|
|
109 static void entry0( CYG_ADDRWORD data ) |
|
|
110 { |
|
|
111 cyg_code_t n; |
|
|
112 cyg_exception_handler_t *old_handler, *old_handler1; |
|
|
113 cyg_addrword_t old_data, old_data1; |
|
|
114 |
|
|
115 CYG_UNUSED_PARAM(CYG_ADDRESS, data); |
|
|
116 |
|
|
117 cyg_exception_set_handler( |
|
2
|
118 CYGNUM_HAL_EXCEPTION_MAX, |
|
0
|
119 &handler1, |
|
|
120 (cyg_addrword_t)&d0, |
|
|
121 &old_handler, |
|
|
122 &old_data); |
|
|
123 |
|
|
124 cyg_exception_set_handler( |
|
2
|
125 CYGNUM_HAL_EXCEPTION_MAX, |
|
0
|
126 &handler1, |
|
|
127 (cyg_addrword_t)&d0, |
|
|
128 &old_handler1, |
|
|
129 &old_data1); |
|
|
130 |
|
|
131 CYG_TEST_CHECK(old_handler1 == &handler1, |
|
|
132 "register exception: old_handler not the one previously registered"); |
|
|
133 CYG_TEST_CHECK(old_data1 == (cyg_addrword_t)&d0, |
|
|
134 "register exception: old_data not those previously registered"); |
|
|
135 |
|
|
136 cyg_exception_call_handler( |
|
|
137 cyg_thread_self(), |
|
2
|
138 CYGNUM_HAL_EXCEPTION_MAX, |
|
0
|
139 (cyg_addrword_t)99); |
|
|
140 |
|
|
141 CYG_TEST_INFO("handler 1 returned"); |
|
|
142 |
|
2
|
143 cyg_exception_clear_handler(CYGNUM_HAL_EXCEPTION_MAX); |
|
|
144 cyg_exception_clear_handler(CYGNUM_HAL_EXCEPTION_MAX); |
|
0
|
145 |
|
2
|
146 #ifdef CYGPKG_HAL_I386_LINUX |
|
|
147 // We can't catch segmentation violation exceptions on Linux. |
|
|
148 CYG_TEST_PASS_FINISH("Except 1 OK"); |
|
|
149 #endif |
|
|
150 |
|
|
151 for(n = CYGNUM_HAL_EXCEPTION_MIN; n <= CYGNUM_HAL_EXCEPTION_MAX; n++) { |
|
0
|
152 cyg_exception_set_handler( |
|
|
153 n, |
|
|
154 handler0, |
|
|
155 (cyg_addrword_t)123, |
|
|
156 &old_handler1, |
|
|
157 &old_data1); |
|
|
158 } |
|
|
159 |
|
2
|
160 CYG_TEST_PASS("Attempting to provoke exception"); |
|
0
|
161 |
|
|
162 cause_exception(); |
|
|
163 |
|
|
164 CYG_TEST_FAIL_FINISH("Couldn't cause exception"); |
|
|
165 } |
|
|
166 |
|
|
167 #ifdef CYG_HAL_TX39_JMR3904 |
|
|
168 |
|
|
169 extern void __default_exception_vsr(void); |
|
|
170 |
|
|
171 #endif |
|
|
172 |
|
|
173 void except0_main( void ) |
|
|
174 { |
|
2
|
175 // Use CYG_TEST_GDBCMD _before_ CYG_TEST_INIT() |
|
|
176 CYG_TEST_GDBCMD("handle SIGBUS nostop"); |
|
|
177 CYG_TEST_GDBCMD("handle SIGSEGV nostop"); |
|
|
178 |
|
0
|
179 CYG_TEST_INIT(); |
|
|
180 |
|
2
|
181 #ifdef HAL_VSR_SET_TO_ECOS_HANDLER |
|
|
182 // Reclaim the VSR off CygMon possibly |
|
|
183 #ifdef CYGNUM_HAL_EXCEPTION_DATA_ACCESS |
|
|
184 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_ACCESS, NULL ); |
|
|
185 #endif |
|
|
186 #ifdef CYGNUM_HAL_EXCEPTION_DATA_UNALIGNED_ACCESS |
|
|
187 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_DATA_UNALIGNED_ACCESS, NULL ); |
|
|
188 #endif |
|
|
189 #ifdef CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION |
|
|
190 HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_ILLEGAL_INSTRUCTION, NULL ); |
|
|
191 #endif |
|
|
192 #endif |
|
0
|
193 |
|
|
194 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kexcept1", |
|
2
|
195 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
0
|
196 cyg_thread_resume(thread[0]); |
|
|
197 |
|
|
198 cyg_scheduler_start(); |
|
|
199 |
|
|
200 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
201 } |
|
|
202 |
|
|
203 externC void |
|
|
204 cyg_start( void ) |
|
|
205 { |
|
|
206 except0_main(); |
|
|
207 } |
|
|
208 |
|
|
209 #else // def CYGFUN_KERNEL_API_C |
|
|
210 #define N_A_MSG "Kernel C API layer disabled" |
|
|
211 #endif // def CYGFUN_KERNEL_API_C |
|
|
212 #else // def CYGPKG_KERNEL_EXCEPTIONS |
|
|
213 #define N_A_MSG "Exceptions disabled" |
|
|
214 #endif // def CYGPKG_KERNEL_EXCEPTIONS |
|
|
215 |
|
|
216 #ifdef N_A_MSG |
|
|
217 externC void |
|
|
218 cyg_start( void ) |
|
|
219 { |
|
|
220 CYG_TEST_INIT(); |
|
2
|
221 CYG_TEST_NA( N_A_MSG); |
|
0
|
222 } |
|
|
223 #endif // N_A_MSG |
|
|
224 |
|
|
225 /* EOF kexcept1.c */ |