]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/MicroBlazeV8/port_exceptions.c
e78ada6f27b7ec7aaea72e05b388ecaa58d99dcf
[freertos] / Source / portable / GCC / MicroBlazeV8 / port_exceptions.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 \r
58 /* Hardware includes. */\r
59 #include <microblaze_exceptions_i.h>\r
60 #include <microblaze_exceptions_g.h>\r
61 \r
62 /* The Xilinx library defined exception entry point stacks a number of\r
63 registers.  These definitions are offsets from the stack pointer to the various\r
64 stacked register values. */\r
65 #define portexR3_STACK_OFFSET   4\r
66 #define portexR4_STACK_OFFSET   5\r
67 #define portexR5_STACK_OFFSET   6\r
68 #define portexR6_STACK_OFFSET   7\r
69 #define portexR7_STACK_OFFSET   8\r
70 #define portexR8_STACK_OFFSET   9\r
71 #define portexR9_STACK_OFFSET   10\r
72 #define portexR10_STACK_OFFSET  11\r
73 #define portexR11_STACK_OFFSET  12\r
74 #define portexR12_STACK_OFFSET  13\r
75 #define portexR15_STACK_OFFSET  16\r
76 #define portexR18_STACK_OFFSET  19\r
77 #define portexMSR_STACK_OFFSET  20\r
78 #define portexR19_STACK_OFFSET  -1\r
79 \r
80 /* This is defined to equal the size, in bytes, of the stack frame generated by\r
81 the Xilinx standard library exception entry point.  It is required to determine\r
82 the stack pointer value prior to the exception being entered. */\r
83 #define portexASM_HANDLER_STACK_FRAME_SIZE 84UL\r
84 \r
85 /* The number of bytes a MicroBlaze instruction consumes. */\r
86 #define portexINSTRUCTION_SIZE  4\r
87 \r
88 /* Exclude this entire file if the MicroBlaze is not configured to handle\r
89 exceptions, or the application defined configuration constant\r
90 configINSTALL_EXCEPTION_HANDLERS is not set to 1. */\r
91 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )\r
92 \r
93 /* This variable is set in the exception entry code, before\r
94 vPortExceptionHandler is called. */\r
95 unsigned long *pulStackPointerOnFunctionEntry = NULL;\r
96 \r
97 /* This is the structure that is filled with the MicroBlaze context as it\r
98 existed immediately prior to the exception occurrence.  A pointer to this\r
99 structure is passed into the vApplicationExceptionRegisterDump() callback\r
100 function, if one is defined. */\r
101 static xPortRegisterDump xRegisterDump;\r
102 \r
103 /* This is the FreeRTOS exception handler that is installed for all exception\r
104 types.  It is called from vPortExceptionHanlderEntry() - which is itself defined\r
105 in portasm.S. */\r
106 void vPortExceptionHandler( void *pvExceptionID );\r
107 extern void vPortExceptionHandlerEntry( void *pvExceptionID );\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* vApplicationExceptionRegisterDump() is a callback function that the \r
112 application can optionally define to receive a populated xPortRegisterDump\r
113 structure.  If the application chooses not to define a version of \r
114 vApplicationExceptionRegisterDump() then this weekly defined default \r
115 implementation will be called instead. */\r
116 extern void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump ) __attribute__((weak));\r
117 void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump )\r
118 {\r
119         ( void ) xRegisterDump;\r
120 \r
121         for( ;; )\r
122         {\r
123                 portNOP();\r
124         }\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void vPortExceptionHandler( void *pvExceptionID )\r
129 {\r
130 extern void *pxCurrentTCB;\r
131 \r
132         /* Fill an xPortRegisterDump structure with the MicroBlaze context as it\r
133         was immediately before the exception occurrence. */\r
134         \r
135         /* First fill in the name and handle of the task that was in the Running \r
136         state when the exception occurred. */\r
137         xRegisterDump.xCurrentTaskHandle = pxCurrentTCB;\r
138         xRegisterDump.pcCurrentTaskName = pcTaskNameGet( NULL );\r
139 \r
140         configASSERT( pulStackPointerOnFunctionEntry );\r
141 \r
142         /* Obtain the values of registers that were stacked prior to this function\r
143         being called, and may have changed since they were stacked. */
144         xRegisterDump.ulR3 = pulStackPointerOnFunctionEntry[ portexR3_STACK_OFFSET ];\r
145         xRegisterDump.ulR4 = pulStackPointerOnFunctionEntry[ portexR4_STACK_OFFSET ];\r
146         xRegisterDump.ulR5 = pulStackPointerOnFunctionEntry[ portexR5_STACK_OFFSET ];\r
147         xRegisterDump.ulR6 = pulStackPointerOnFunctionEntry[ portexR6_STACK_OFFSET ];\r
148         xRegisterDump.ulR7 = pulStackPointerOnFunctionEntry[ portexR7_STACK_OFFSET ];\r
149         xRegisterDump.ulR8 = pulStackPointerOnFunctionEntry[ portexR8_STACK_OFFSET ];\r
150         xRegisterDump.ulR9 = pulStackPointerOnFunctionEntry[ portexR9_STACK_OFFSET ];\r
151         xRegisterDump.ulR10 = pulStackPointerOnFunctionEntry[ portexR10_STACK_OFFSET ];\r
152         xRegisterDump.ulR11 = pulStackPointerOnFunctionEntry[ portexR11_STACK_OFFSET ];\r
153         xRegisterDump.ulR12 = pulStackPointerOnFunctionEntry[ portexR12_STACK_OFFSET ];\r
154         xRegisterDump.ulR15_return_address_from_subroutine = pulStackPointerOnFunctionEntry[ portexR15_STACK_OFFSET ];\r
155         xRegisterDump.ulR18 = pulStackPointerOnFunctionEntry[ portexR18_STACK_OFFSET ];\r
156         xRegisterDump.ulR19 = pulStackPointerOnFunctionEntry[ portexR19_STACK_OFFSET ];\r
157         xRegisterDump.ulMSR = pulStackPointerOnFunctionEntry[ portexMSR_STACK_OFFSET ];\r
158         \r
159         /* Obtain the value of all other registers. */\r
160         xRegisterDump.ulR2_small_data_area = mfgpr( R2 );\r
161         xRegisterDump.ulR13_read_write_small_data_area = mfgpr( R13 );\r
162         xRegisterDump.ulR14_return_address_from_interrupt = mfgpr( R14 );\r
163         xRegisterDump.ulR16_return_address_from_trap = mfgpr( R16 );\r
164         xRegisterDump.ulR17_return_address_from_exceptions = mfgpr( R17 );\r
165         xRegisterDump.ulR20 = mfgpr( R20 );\r
166         xRegisterDump.ulR21 = mfgpr( R21 );\r
167         xRegisterDump.ulR22 = mfgpr( R22 );\r
168         xRegisterDump.ulR23 = mfgpr( R23 );\r
169         xRegisterDump.ulR24 = mfgpr( R24 );\r
170         xRegisterDump.ulR25 = mfgpr( R25 );\r
171         xRegisterDump.ulR26 = mfgpr( R26 );\r
172         xRegisterDump.ulR27 = mfgpr( R27 );\r
173         xRegisterDump.ulR28 = mfgpr( R28 );\r
174         xRegisterDump.ulR29 = mfgpr( R29 );\r
175         xRegisterDump.ulR30 = mfgpr( R30 );\r
176         xRegisterDump.ulR31 = mfgpr( R31 );\r
177         xRegisterDump.ulR1_SP = ( ( unsigned long ) pulStackPointerOnFunctionEntry ) + portexASM_HANDLER_STACK_FRAME_SIZE;\r
178         xRegisterDump.ulEAR = mfear();\r
179         xRegisterDump.ulESR = mfesr();\r
180         xRegisterDump.ulEDR = mfedr();\r
181         \r
182         /* Move the saved program counter back to the instruction that was executed\r
183         when the exception occurred.  This is only valid for certain types of\r
184         exception. */\r
185         xRegisterDump.ulPC = xRegisterDump.ulR17_return_address_from_exceptions - portexINSTRUCTION_SIZE;\r
186 \r
187         #if XPAR_MICROBLAZE_0_USE_FPU == 1\r
188         {\r
189                 xRegisterDump.ulFSR = mffsr();\r
190         }\r
191         #else\r
192         {\r
193                 xRegisterDump.ulFSR = 0UL;\r
194         }\r
195         #endif\r
196 \r
197         /* Also fill in a string that describes what type of exception this is.\r
198         The string uses the same ID names as defined in the MicroBlaze standard\r
199         library exception header files. */\r
200         switch( ( unsigned long ) pvExceptionID )\r
201         {\r
202                 case XEXC_ID_FSL :\r
203                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_FSL";\r
204                                 break;\r
205 \r
206                 case XEXC_ID_UNALIGNED_ACCESS :\r
207                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_UNALIGNED_ACCESS";\r
208                                 break;\r
209 \r
210                 case XEXC_ID_ILLEGAL_OPCODE :\r
211                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_ILLEGAL_OPCODE";\r
212                                 break;\r
213 \r
214                 case XEXC_ID_M_AXI_I_EXCEPTION :\r
215                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_M_AXI_I_EXCEPTION or XEXC_ID_IPLB_EXCEPTION";\r
216                                 break;\r
217 \r
218                 case XEXC_ID_M_AXI_D_EXCEPTION :\r
219                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_M_AXI_D_EXCEPTION or XEXC_ID_DPLB_EXCEPTION";\r
220                                 break;\r
221 \r
222                 case XEXC_ID_DIV_BY_ZERO :\r
223                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_DIV_BY_ZERO";\r
224                                 break;\r
225 \r
226                 case XEXC_ID_STACK_VIOLATION :\r
227                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_STACK_VIOLATION or XEXC_ID_MMU";\r
228                                 break;\r
229 \r
230                 #if XPAR_MICROBLAZE_0_USE_FPU == 1\r
231 \r
232                         case XEXC_ID_FPU :\r
233                                                 /*_RB_ More decoding required here and in other exceptions. */\r
234                                                 xRegisterDump.pcExceptionCause = ( signed char * const ) "XEXC_ID_FPU see ulFSR value";\r
235                                                 break;\r
236 \r
237                 #endif /* XPAR_MICROBLAZE_0_USE_FPU */\r
238         }\r
239 \r
240         /* vApplicationExceptionRegisterDump() is a callback function that the \r
241         application can optionally define to receive the populated xPortRegisterDump\r
242         structure.  If the application chooses not to define a version of \r
243         vApplicationExceptionRegisterDump() then the weekly defined default \r
244         implementation within this file will be called instead. */\r
245         vApplicationExceptionRegisterDump( &xRegisterDump );\r
246 \r
247         /* Must not attempt to leave this function! */\r
248         for( ;; )\r
249         {\r
250                 portNOP();\r
251         }\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 void vPortExceptionsInstallHandlers( void )\r
256 {\r
257 static unsigned long ulHandlersAlreadyInstalled = pdFALSE;\r
258 \r
259         if( ulHandlersAlreadyInstalled == pdFALSE )\r
260         {\r
261                 ulHandlersAlreadyInstalled = pdTRUE;\r
262 \r
263                 #if XPAR_MICROBLAZE_0_UNALIGNED_EXCEPTIONS == 1\r
264                         microblaze_register_exception_handler( XEXC_ID_UNALIGNED_ACCESS, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_UNALIGNED_ACCESS );\r
265                 #endif /* XPAR_MICROBLAZE_0_UNALIGNED_EXCEPTIONS*/\r
266 \r
267                 #if XPAR_MICROBLAZE_0_ILL_OPCODE_EXCEPTION == 1\r
268                         microblaze_register_exception_handler( XEXC_ID_ILLEGAL_OPCODE, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_ILLEGAL_OPCODE );\r
269                 #endif /* XPAR_MICROBLAZE_0_ILL_OPCODE_EXCEPTION*/\r
270 \r
271                 #if XPAR_MICROBLAZE_0_M_AXI_I_BUS_EXCEPTION == 1\r
272                         microblaze_register_exception_handler( XEXC_ID_M_AXI_I_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_M_AXI_I_EXCEPTION );\r
273                 #endif /* XPAR_MICROBLAZE_0_M_AXI_I_BUS_EXCEPTION*/\r
274 \r
275                 #if XPAR_MICROBLAZE_0_M_AXI_D_BUS_EXCEPTION == 1\r
276                         microblaze_register_exception_handler( XEXC_ID_M_AXI_D_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_M_AXI_D_EXCEPTION );\r
277                 #endif /* XPAR_MICROBLAZE_0_M_AXI_D_BUS_EXCEPTION*/\r
278 \r
279                 #if XPAR_MICROBLAZE_0_IPLB_BUS_EXCEPTION == 1\r
280                         microblaze_register_exception_handler( XEXC_ID_IPLB_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_IPLB_EXCEPTION );\r
281                 #endif /* XPAR_MICROBLAZE_0_IPLB_BUS_EXCEPTION*/\r
282 \r
283                 #if XPAR_MICROBLAZE_0_DPLB_BUS_EXCEPTION == 1\r
284                         microblaze_register_exception_handler( XEXC_ID_DPLB_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_DPLB_EXCEPTION );\r
285                 #endif /* XPAR_MICROBLAZE_0_DPLB_BUS_EXCEPTION*/\r
286 \r
287                 #if XPAR_MICROBLAZE_0_DIV_ZERO_EXCEPTION == 1\r
288                         microblaze_register_exception_handler( XEXC_ID_DIV_BY_ZERO, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_DIV_BY_ZERO );\r
289                 #endif /* XPAR_MICROBLAZE_0_DIV_ZERO_EXCEPTION*/\r
290 \r
291                 #if XPAR_MICROBLAZE_0_FPU_EXCEPTION == 1\r
292                         microblaze_register_exception_handler( XEXC_ID_FPU, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_FPU );\r
293                 #endif /* XPAR_MICROBLAZE_0_FPU_EXCEPTION*/\r
294 \r
295                 #if XPAR_MICROBLAZE_0_FSL_EXCEPTION == 1\r
296                         microblaze_register_exception_handler( XEXC_ID_FSL, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_FSL );\r
297                 #endif /* XPAR_MICROBLAZE_0_FSL_EXCEPTION*/\r
298         }\r
299 }\r
300 \r
301 /* Exclude the entire file if the MicroBlaze is not configured to handle\r
302 exceptions, or the application defined configuration item \r
303 configINSTALL_EXCEPTION_HANDLERS is not set to 1. */\r
304 #endif /* ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 ) */\r
305 \r
306 \r
307 \r