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