|
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 |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): dsm |
|
|
33 // Contributors: dsm |
|
|
34 // Date: 1998-06-15 |
|
|
35 // Description: Test basic exception functionality |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 */ |
|
|
38 |
|
|
39 #include <cyg/kernel/kapi.h> |
|
|
40 |
|
|
41 #include <cyg/infra/testcase.h> |
|
|
42 |
|
|
43 #ifdef CYGPKG_KERNEL_EXCEPTIONS |
|
|
44 |
|
|
45 #ifdef CYGFUN_KERNEL_API_C |
|
|
46 |
|
|
47 #include <cyg/hal/hal_intr.h> // exception ranges |
|
|
48 |
|
|
49 #include "testaux.h" |
|
|
50 |
|
|
51 #define NTHREADS 1 |
|
|
52 #define STACKSIZE 4096 |
|
|
53 |
|
|
54 static cyg_handle_t thread[NTHREADS]; |
|
|
55 |
|
|
56 static cyg_thread thread_obj[NTHREADS]; |
|
|
57 static char stack[NTHREADS][STACKSIZE]; |
|
|
58 |
|
|
59 |
|
|
60 static cyg_exception_handler_t handler0; |
|
|
61 static int d0; |
|
|
62 |
|
|
63 static void handler0(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info) |
|
|
64 { |
|
|
65 CYG_TEST_INFO("handler 0 called"); |
|
|
66 |
|
|
67 CYG_TEST_CHECK((cyg_addrword_t)123 == data, "handler given wrong data"); |
|
|
68 |
|
|
69 // ignore machine specific stuff |
|
|
70 CYG_UNUSED_PARAM(cyg_code_t, number); |
|
|
71 CYG_UNUSED_PARAM(cyg_addrword_t, info); |
|
|
72 |
|
|
73 CYG_TEST_PASS_FINISH("Except 1 OK"); |
|
|
74 } |
|
|
75 |
|
|
76 static void handler1(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info) |
|
|
77 { |
|
|
78 CYG_TEST_INFO("handler 1 called"); |
|
|
79 |
|
|
80 CYG_TEST_CHECK((cyg_addrword_t)&d0 == data, "handler given wrong data"); |
|
|
81 |
|
|
82 #ifdef CYGSEM_KERNEL_EXCEPTIONS_DECODE |
|
|
83 CYG_TEST_CHECK(number == CYG_EXCEPTION_MAX, "handler given wrong number"); |
|
|
84 #else |
|
|
85 CYG_UNUSED_PARAM(cyg_code_t, number); |
|
|
86 #endif |
|
|
87 |
|
|
88 CYG_TEST_CHECK((cyg_addrword_t)99 == info, "handler given wrong info"); |
|
|
89 } |
|
|
90 |
|
|
91 |
|
|
92 // The following function attempts to cause an exception in various |
|
|
93 // hacky ways. It is machine dependent what exception is generated. |
|
|
94 // It does reads rather than writes hoping not to corrupt anything |
|
|
95 // important. |
|
|
96 void cause_exception(void) |
|
|
97 { |
|
|
98 int x; |
|
|
99 unsigned int p=0; |
|
|
100 |
|
|
101 do { |
|
|
102 x=*(volatile int *)(p-1)/p; |
|
|
103 p+=0x100000; |
|
|
104 } while(p != 0); |
|
|
105 } |
|
|
106 |
|
|
107 static void entry0( CYG_ADDRWORD data ) |
|
|
108 { |
|
|
109 cyg_code_t n; |
|
|
110 cyg_exception_handler_t *old_handler, *old_handler1; |
|
|
111 cyg_addrword_t old_data, old_data1; |
|
|
112 |
|
|
113 CYG_UNUSED_PARAM(CYG_ADDRESS, data); |
|
|
114 |
|
|
115 cyg_exception_set_handler( |
|
|
116 CYG_EXCEPTION_MAX, |
|
|
117 &handler1, |
|
|
118 (cyg_addrword_t)&d0, |
|
|
119 &old_handler, |
|
|
120 &old_data); |
|
|
121 |
|
|
122 cyg_exception_set_handler( |
|
|
123 CYG_EXCEPTION_MAX, |
|
|
124 &handler1, |
|
|
125 (cyg_addrword_t)&d0, |
|
|
126 &old_handler1, |
|
|
127 &old_data1); |
|
|
128 |
|
|
129 CYG_TEST_CHECK(old_handler1 == &handler1, |
|
|
130 "register exception: old_handler not the one previously registered"); |
|
|
131 CYG_TEST_CHECK(old_data1 == (cyg_addrword_t)&d0, |
|
|
132 "register exception: old_data not those previously registered"); |
|
|
133 |
|
|
134 cyg_exception_call_handler( |
|
|
135 cyg_thread_self(), |
|
|
136 CYG_EXCEPTION_MAX, |
|
|
137 (cyg_addrword_t)99); |
|
|
138 |
|
|
139 CYG_TEST_INFO("handler 1 returned"); |
|
|
140 |
|
|
141 cyg_exception_clear_handler(CYG_EXCEPTION_MAX); |
|
|
142 cyg_exception_clear_handler(CYG_EXCEPTION_MAX); |
|
|
143 |
|
|
144 for(n = CYG_EXCEPTION_MIN; n <= CYG_EXCEPTION_MAX; n++) { |
|
|
145 cyg_exception_set_handler( |
|
|
146 n, |
|
|
147 handler0, |
|
|
148 (cyg_addrword_t)123, |
|
|
149 &old_handler1, |
|
|
150 &old_data1); |
|
|
151 } |
|
|
152 |
|
|
153 CYG_TEST_INFO("Attempting to provoke exception"); |
|
|
154 |
|
|
155 cause_exception(); |
|
|
156 |
|
|
157 CYG_TEST_FAIL_FINISH("Couldn't cause exception"); |
|
|
158 } |
|
|
159 |
|
|
160 #ifdef CYG_HAL_TX39_JMR3904 |
|
|
161 |
|
|
162 extern void __default_exception_vsr(void); |
|
|
163 |
|
|
164 #endif |
|
|
165 |
|
|
166 void except0_main( void ) |
|
|
167 { |
|
|
168 CYG_TEST_INIT(); |
|
|
169 |
|
|
170 #ifdef CYG_HAL_TX39_JMR3904 |
|
|
171 |
|
|
172 cyg_interrupt_set_vsr( CYG_VECTOR_LOAD_ADDRESS, __default_exception_vsr ); |
|
|
173 |
|
|
174 #endif |
|
|
175 |
|
|
176 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kexcept1", |
|
|
177 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
|
178 cyg_thread_resume(thread[0]); |
|
|
179 |
|
|
180 cyg_scheduler_start(); |
|
|
181 |
|
|
182 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
183 } |
|
|
184 |
|
|
185 externC void |
|
|
186 cyg_start( void ) |
|
|
187 { |
|
|
188 except0_main(); |
|
|
189 } |
|
|
190 |
|
|
191 #else // def CYGFUN_KERNEL_API_C |
|
|
192 #define N_A_MSG "Kernel C API layer disabled" |
|
|
193 #endif // def CYGFUN_KERNEL_API_C |
|
|
194 #else // def CYGPKG_KERNEL_EXCEPTIONS |
|
|
195 #define N_A_MSG "Exceptions disabled" |
|
|
196 #endif // def CYGPKG_KERNEL_EXCEPTIONS |
|
|
197 |
|
|
198 #ifdef N_A_MSG |
|
|
199 externC void |
|
|
200 cyg_start( void ) |
|
|
201 { |
|
|
202 CYG_TEST_INIT(); |
|
|
203 CYG_TEST_PASS_FINISH( "N/A: " N_A_MSG); |
|
|
204 } |
|
|
205 #endif // N_A_MSG |
|
|
206 |
|
|
207 /* EOF kexcept1.c */ |