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