]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MicroBlazeV8/port_exceptions.c
Update version number.
[freertos] / FreeRTOS / Source / portable / GCC / MicroBlazeV8 / port_exceptions.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Hardware includes. */\r
70 #include <microblaze_exceptions_i.h>\r
71 #include <microblaze_exceptions_g.h>\r
72 \r
73 /* The Xilinx library defined exception entry point stacks a number of\r
74 registers.  These definitions are offsets from the stack pointer to the various\r
75 stacked register values. */\r
76 #define portexR3_STACK_OFFSET   4\r
77 #define portexR4_STACK_OFFSET   5\r
78 #define portexR5_STACK_OFFSET   6\r
79 #define portexR6_STACK_OFFSET   7\r
80 #define portexR7_STACK_OFFSET   8\r
81 #define portexR8_STACK_OFFSET   9\r
82 #define portexR9_STACK_OFFSET   10\r
83 #define portexR10_STACK_OFFSET  11\r
84 #define portexR11_STACK_OFFSET  12\r
85 #define portexR12_STACK_OFFSET  13\r
86 #define portexR15_STACK_OFFSET  16\r
87 #define portexR18_STACK_OFFSET  19\r
88 #define portexMSR_STACK_OFFSET  20\r
89 #define portexR19_STACK_OFFSET  -1\r
90 \r
91 /* This is defined to equal the size, in bytes, of the stack frame generated by\r
92 the Xilinx standard library exception entry point.  It is required to determine\r
93 the stack pointer value prior to the exception being entered. */\r
94 #define portexASM_HANDLER_STACK_FRAME_SIZE 84UL\r
95 \r
96 /* The number of bytes a MicroBlaze instruction consumes. */\r
97 #define portexINSTRUCTION_SIZE  4\r
98 \r
99 /* Exclude this entire file if the MicroBlaze is not configured to handle\r
100 exceptions, or the application defined configuration constant\r
101 configINSTALL_EXCEPTION_HANDLERS is not set to 1. */\r
102 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )\r
103 \r
104 /* This variable is set in the exception entry code, before\r
105 vPortExceptionHandler is called. */\r
106 unsigned long *pulStackPointerOnFunctionEntry = NULL;\r
107 \r
108 /* This is the structure that is filled with the MicroBlaze context as it\r
109 existed immediately prior to the exception occurrence.  A pointer to this\r
110 structure is passed into the vApplicationExceptionRegisterDump() callback\r
111 function, if one is defined. */\r
112 static xPortRegisterDump xRegisterDump;\r
113 \r
114 /* This is the FreeRTOS exception handler that is installed for all exception\r
115 types.  It is called from vPortExceptionHanlderEntry() - which is itself defined\r
116 in portasm.S. */\r
117 void vPortExceptionHandler( void *pvExceptionID );\r
118 extern void vPortExceptionHandlerEntry( void *pvExceptionID );\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /* vApplicationExceptionRegisterDump() is a callback function that the \r
123 application can optionally define to receive a populated xPortRegisterDump\r
124 structure.  If the application chooses not to define a version of \r
125 vApplicationExceptionRegisterDump() then this weekly defined default \r
126 implementation will be called instead. */\r
127 extern void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump ) __attribute__((weak));\r
128 void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump )\r
129 {\r
130         ( void ) xRegisterDump;\r
131 \r
132         for( ;; )\r
133         {\r
134                 portNOP();\r
135         }\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void vPortExceptionHandler( void *pvExceptionID )\r
140 {\r
141 extern void *pxCurrentTCB;\r
142 \r
143         /* Fill an xPortRegisterDump structure with the MicroBlaze context as it\r
144         was immediately before the exception occurrence. */\r
145         \r
146         /* First fill in the name and handle of the task that was in the Running \r
147         state when the exception occurred. */\r
148         xRegisterDump.xCurrentTaskHandle = pxCurrentTCB;\r
149         xRegisterDump.pcCurrentTaskName = pcTaskGetTaskName( NULL );\r
150 \r
151         configASSERT( pulStackPointerOnFunctionEntry );\r
152 \r
153         /* Obtain the values of registers that were stacked prior to this function\r
154         being called, and may have changed since they were stacked. */\r
155         xRegisterDump.ulR3 = pulStackPointerOnFunctionEntry[ portexR3_STACK_OFFSET ];\r
156         xRegisterDump.ulR4 = pulStackPointerOnFunctionEntry[ portexR4_STACK_OFFSET ];\r
157         xRegisterDump.ulR5 = pulStackPointerOnFunctionEntry[ portexR5_STACK_OFFSET ];\r
158         xRegisterDump.ulR6 = pulStackPointerOnFunctionEntry[ portexR6_STACK_OFFSET ];\r
159         xRegisterDump.ulR7 = pulStackPointerOnFunctionEntry[ portexR7_STACK_OFFSET ];\r
160         xRegisterDump.ulR8 = pulStackPointerOnFunctionEntry[ portexR8_STACK_OFFSET ];\r
161         xRegisterDump.ulR9 = pulStackPointerOnFunctionEntry[ portexR9_STACK_OFFSET ];\r
162         xRegisterDump.ulR10 = pulStackPointerOnFunctionEntry[ portexR10_STACK_OFFSET ];\r
163         xRegisterDump.ulR11 = pulStackPointerOnFunctionEntry[ portexR11_STACK_OFFSET ];\r
164         xRegisterDump.ulR12 = pulStackPointerOnFunctionEntry[ portexR12_STACK_OFFSET ];\r
165         xRegisterDump.ulR15_return_address_from_subroutine = pulStackPointerOnFunctionEntry[ portexR15_STACK_OFFSET ];\r
166         xRegisterDump.ulR18 = pulStackPointerOnFunctionEntry[ portexR18_STACK_OFFSET ];\r
167         xRegisterDump.ulR19 = pulStackPointerOnFunctionEntry[ portexR19_STACK_OFFSET ];\r
168         xRegisterDump.ulMSR = pulStackPointerOnFunctionEntry[ portexMSR_STACK_OFFSET ];\r
169         \r
170         /* Obtain the value of all other registers. */\r
171         xRegisterDump.ulR2_small_data_area = mfgpr( R2 );\r
172         xRegisterDump.ulR13_read_write_small_data_area = mfgpr( R13 );\r
173         xRegisterDump.ulR14_return_address_from_interrupt = mfgpr( R14 );\r
174         xRegisterDump.ulR16_return_address_from_trap = mfgpr( R16 );\r
175         xRegisterDump.ulR17_return_address_from_exceptions = mfgpr( R17 );\r
176         xRegisterDump.ulR20 = mfgpr( R20 );\r
177         xRegisterDump.ulR21 = mfgpr( R21 );\r
178         xRegisterDump.ulR22 = mfgpr( R22 );\r
179         xRegisterDump.ulR23 = mfgpr( R23 );\r
180         xRegisterDump.ulR24 = mfgpr( R24 );\r
181         xRegisterDump.ulR25 = mfgpr( R25 );\r
182         xRegisterDump.ulR26 = mfgpr( R26 );\r
183         xRegisterDump.ulR27 = mfgpr( R27 );\r
184         xRegisterDump.ulR28 = mfgpr( R28 );\r
185         xRegisterDump.ulR29 = mfgpr( R29 );\r
186         xRegisterDump.ulR30 = mfgpr( R30 );\r
187         xRegisterDump.ulR31 = mfgpr( R31 );\r
188         xRegisterDump.ulR1_SP = ( ( unsigned long ) pulStackPointerOnFunctionEntry ) + portexASM_HANDLER_STACK_FRAME_SIZE;\r
189         xRegisterDump.ulEAR = mfear();\r
190         xRegisterDump.ulESR = mfesr();\r
191         xRegisterDump.ulEDR = mfedr();\r
192         \r
193         /* Move the saved program counter back to the instruction that was executed\r
194         when the exception occurred.  This is only valid for certain types of\r
195         exception. */\r
196         xRegisterDump.ulPC = xRegisterDump.ulR17_return_address_from_exceptions - portexINSTRUCTION_SIZE;\r
197 \r
198         #if XPAR_MICROBLAZE_0_USE_FPU == 1\r
199         {\r
200                 xRegisterDump.ulFSR = mffsr();\r
201         }\r
202         #else\r
203         {\r
204                 xRegisterDump.ulFSR = 0UL;\r
205         }\r
206         #endif\r
207 \r
208         /* Also fill in a string that describes what type of exception this is.\r
209         The string uses the same ID names as defined in the MicroBlaze standard\r
210         library exception header files. */\r
211         switch( ( unsigned long ) pvExceptionID )\r
212         {\r
213                 case XEXC_ID_FSL :\r
214                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_FSL";\r
215                                 break;\r
216 \r
217                 case XEXC_ID_UNALIGNED_ACCESS :\r
218                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_UNALIGNED_ACCESS";\r
219                                 break;\r
220 \r
221                 case XEXC_ID_ILLEGAL_OPCODE :\r
222                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_ILLEGAL_OPCODE";\r
223                                 break;\r
224 \r
225                 case XEXC_ID_M_AXI_I_EXCEPTION :\r
226                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_M_AXI_I_EXCEPTION or XEXC_ID_IPLB_EXCEPTION";\r
227                                 break;\r
228 \r
229                 case XEXC_ID_M_AXI_D_EXCEPTION :\r
230                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_M_AXI_D_EXCEPTION or XEXC_ID_DPLB_EXCEPTION";\r
231                                 break;\r
232 \r
233                 case XEXC_ID_DIV_BY_ZERO :\r
234                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_DIV_BY_ZERO";\r
235                                 break;\r
236 \r
237                 case XEXC_ID_STACK_VIOLATION :\r
238                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_STACK_VIOLATION or XEXC_ID_MMU";\r
239                                 break;\r
240 \r
241                 #if XPAR_MICROBLAZE_0_USE_FPU == 1\r
242 \r
243                         case XEXC_ID_FPU :\r
244                                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_FPU see ulFSR value";\r
245                                                 break;\r
246 \r
247                 #endif /* XPAR_MICROBLAZE_0_USE_FPU */\r
248         }\r
249 \r
250         /* vApplicationExceptionRegisterDump() is a callback function that the \r
251         application can optionally define to receive the populated xPortRegisterDump\r
252         structure.  If the application chooses not to define a version of \r
253         vApplicationExceptionRegisterDump() then the weekly defined default \r
254         implementation within this file will be called instead. */\r
255         vApplicationExceptionRegisterDump( &xRegisterDump );\r
256 \r
257         /* Must not attempt to leave this function! */\r
258         for( ;; )\r
259         {\r
260                 portNOP();\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vPortExceptionsInstallHandlers( void )\r
266 {\r
267 static unsigned long ulHandlersAlreadyInstalled = pdFALSE;\r
268 \r
269         if( ulHandlersAlreadyInstalled == pdFALSE )\r
270         {\r
271                 ulHandlersAlreadyInstalled = pdTRUE;\r
272 \r
273                 #if XPAR_MICROBLAZE_0_UNALIGNED_EXCEPTIONS == 1\r
274                         microblaze_register_exception_handler( XEXC_ID_UNALIGNED_ACCESS, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_UNALIGNED_ACCESS );\r
275                 #endif /* XPAR_MICROBLAZE_0_UNALIGNED_EXCEPTIONS*/\r
276 \r
277                 #if XPAR_MICROBLAZE_0_ILL_OPCODE_EXCEPTION == 1\r
278                         microblaze_register_exception_handler( XEXC_ID_ILLEGAL_OPCODE, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_ILLEGAL_OPCODE );\r
279                 #endif /* XPAR_MICROBLAZE_0_ILL_OPCODE_EXCEPTION*/\r
280 \r
281                 #if XPAR_MICROBLAZE_0_M_AXI_I_BUS_EXCEPTION == 1\r
282                         microblaze_register_exception_handler( XEXC_ID_M_AXI_I_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_M_AXI_I_EXCEPTION );\r
283                 #endif /* XPAR_MICROBLAZE_0_M_AXI_I_BUS_EXCEPTION*/\r
284 \r
285                 #if XPAR_MICROBLAZE_0_M_AXI_D_BUS_EXCEPTION == 1\r
286                         microblaze_register_exception_handler( XEXC_ID_M_AXI_D_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_M_AXI_D_EXCEPTION );\r
287                 #endif /* XPAR_MICROBLAZE_0_M_AXI_D_BUS_EXCEPTION*/\r
288 \r
289                 #if XPAR_MICROBLAZE_0_IPLB_BUS_EXCEPTION == 1\r
290                         microblaze_register_exception_handler( XEXC_ID_IPLB_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_IPLB_EXCEPTION );\r
291                 #endif /* XPAR_MICROBLAZE_0_IPLB_BUS_EXCEPTION*/\r
292 \r
293                 #if XPAR_MICROBLAZE_0_DPLB_BUS_EXCEPTION == 1\r
294                         microblaze_register_exception_handler( XEXC_ID_DPLB_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_DPLB_EXCEPTION );\r
295                 #endif /* XPAR_MICROBLAZE_0_DPLB_BUS_EXCEPTION*/\r
296 \r
297                 #if XPAR_MICROBLAZE_0_DIV_ZERO_EXCEPTION == 1\r
298                         microblaze_register_exception_handler( XEXC_ID_DIV_BY_ZERO, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_DIV_BY_ZERO );\r
299                 #endif /* XPAR_MICROBLAZE_0_DIV_ZERO_EXCEPTION*/\r
300 \r
301                 #if XPAR_MICROBLAZE_0_FPU_EXCEPTION == 1\r
302                         microblaze_register_exception_handler( XEXC_ID_FPU, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_FPU );\r
303                 #endif /* XPAR_MICROBLAZE_0_FPU_EXCEPTION*/\r
304 \r
305                 #if XPAR_MICROBLAZE_0_FSL_EXCEPTION == 1\r
306                         microblaze_register_exception_handler( XEXC_ID_FSL, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_FSL );\r
307                 #endif /* XPAR_MICROBLAZE_0_FSL_EXCEPTION*/\r
308         }\r
309 }\r
310 \r
311 /* Exclude the entire file if the MicroBlaze is not configured to handle\r
312 exceptions, or the application defined configuration item \r
313 configINSTALL_EXCEPTION_HANDLERS is not set to 1. */\r
314 #endif /* ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 ) */\r
315 \r
316 \r
317 \r