]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MicroBlazeV9/port_exceptions.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / GCC / MicroBlazeV9 / port_exceptions.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Scheduler includes. */\r
29 #include "FreeRTOS.h"\r
30 #include "task.h"\r
31 \r
32 /* Hardware includes. */\r
33 #include <microblaze_exceptions_i.h>\r
34 #include <microblaze_exceptions_g.h>\r
35 \r
36 /* The Xilinx library defined exception entry point stacks a number of\r
37 registers.  These definitions are offsets from the stack pointer to the various\r
38 stacked register values. */\r
39 #define portexR3_STACK_OFFSET   4\r
40 #define portexR4_STACK_OFFSET   5\r
41 #define portexR5_STACK_OFFSET   6\r
42 #define portexR6_STACK_OFFSET   7\r
43 #define portexR7_STACK_OFFSET   8\r
44 #define portexR8_STACK_OFFSET   9\r
45 #define portexR9_STACK_OFFSET   10\r
46 #define portexR10_STACK_OFFSET  11\r
47 #define portexR11_STACK_OFFSET  12\r
48 #define portexR12_STACK_OFFSET  13\r
49 #define portexR15_STACK_OFFSET  16\r
50 #define portexR18_STACK_OFFSET  19\r
51 #define portexMSR_STACK_OFFSET  20\r
52 #define portexR19_STACK_OFFSET  -1\r
53 \r
54 /* This is defined to equal the size, in bytes, of the stack frame generated by\r
55 the Xilinx standard library exception entry point.  It is required to determine\r
56 the stack pointer value prior to the exception being entered. */\r
57 #define portexASM_HANDLER_STACK_FRAME_SIZE 84UL\r
58 \r
59 /* The number of bytes a MicroBlaze instruction consumes. */\r
60 #define portexINSTRUCTION_SIZE  4\r
61 \r
62 /* Exclude this entire file if the MicroBlaze is not configured to handle\r
63 exceptions, or the application defined configuration constant\r
64 configINSTALL_EXCEPTION_HANDLERS is not set to 1. */\r
65 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )\r
66 \r
67 /* This variable is set in the exception entry code, before\r
68 vPortExceptionHandler is called. */\r
69 uint32_t *pulStackPointerOnFunctionEntry = NULL;\r
70 \r
71 /* This is the structure that is filled with the MicroBlaze context as it\r
72 existed immediately prior to the exception occurrence.  A pointer to this\r
73 structure is passed into the vApplicationExceptionRegisterDump() callback\r
74 function, if one is defined. */\r
75 static xPortRegisterDump xRegisterDump;\r
76 \r
77 /* This is the FreeRTOS exception handler that is installed for all exception\r
78 types.  It is called from vPortExceptionHanlderEntry() - which is itself defined\r
79 in portasm.S. */\r
80 void vPortExceptionHandler( void *pvExceptionID );\r
81 extern void vPortExceptionHandlerEntry( void *pvExceptionID );\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* vApplicationExceptionRegisterDump() is a callback function that the\r
86 application can optionally define to receive a populated xPortRegisterDump\r
87 structure.  If the application chooses not to define a version of\r
88 vApplicationExceptionRegisterDump() then this weekly defined default\r
89 implementation will be called instead. */\r
90 extern void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump ) __attribute__((weak));\r
91 void vApplicationExceptionRegisterDump( xPortRegisterDump *xRegisterDump )\r
92 {\r
93         ( void ) xRegisterDump;\r
94 \r
95         for( ;; )\r
96         {\r
97                 portNOP();\r
98         }\r
99 }\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vPortExceptionHandler( void *pvExceptionID )\r
103 {\r
104 extern void *pxCurrentTCB;\r
105 \r
106         /* Fill an xPortRegisterDump structure with the MicroBlaze context as it\r
107         was immediately before the exception occurrence. */\r
108 \r
109         /* First fill in the name and handle of the task that was in the Running\r
110         state when the exception occurred. */\r
111         xRegisterDump.xCurrentTaskHandle = pxCurrentTCB;\r
112         xRegisterDump.pcCurrentTaskName = pcTaskGetName( NULL );\r
113 \r
114         configASSERT( pulStackPointerOnFunctionEntry );\r
115 \r
116         /* Obtain the values of registers that were stacked prior to this function\r
117         being called, and may have changed since they were stacked. */\r
118         xRegisterDump.ulR3 = pulStackPointerOnFunctionEntry[ portexR3_STACK_OFFSET ];\r
119         xRegisterDump.ulR4 = pulStackPointerOnFunctionEntry[ portexR4_STACK_OFFSET ];\r
120         xRegisterDump.ulR5 = pulStackPointerOnFunctionEntry[ portexR5_STACK_OFFSET ];\r
121         xRegisterDump.ulR6 = pulStackPointerOnFunctionEntry[ portexR6_STACK_OFFSET ];\r
122         xRegisterDump.ulR7 = pulStackPointerOnFunctionEntry[ portexR7_STACK_OFFSET ];\r
123         xRegisterDump.ulR8 = pulStackPointerOnFunctionEntry[ portexR8_STACK_OFFSET ];\r
124         xRegisterDump.ulR9 = pulStackPointerOnFunctionEntry[ portexR9_STACK_OFFSET ];\r
125         xRegisterDump.ulR10 = pulStackPointerOnFunctionEntry[ portexR10_STACK_OFFSET ];\r
126         xRegisterDump.ulR11 = pulStackPointerOnFunctionEntry[ portexR11_STACK_OFFSET ];\r
127         xRegisterDump.ulR12 = pulStackPointerOnFunctionEntry[ portexR12_STACK_OFFSET ];\r
128         xRegisterDump.ulR15_return_address_from_subroutine = pulStackPointerOnFunctionEntry[ portexR15_STACK_OFFSET ];\r
129         xRegisterDump.ulR18 = pulStackPointerOnFunctionEntry[ portexR18_STACK_OFFSET ];\r
130         xRegisterDump.ulR19 = pulStackPointerOnFunctionEntry[ portexR19_STACK_OFFSET ];\r
131         xRegisterDump.ulMSR = pulStackPointerOnFunctionEntry[ portexMSR_STACK_OFFSET ];\r
132 \r
133         /* Obtain the value of all other registers. */\r
134         xRegisterDump.ulR2_small_data_area = mfgpr( R2 );\r
135         xRegisterDump.ulR13_read_write_small_data_area = mfgpr( R13 );\r
136         xRegisterDump.ulR14_return_address_from_interrupt = mfgpr( R14 );\r
137         xRegisterDump.ulR16_return_address_from_trap = mfgpr( R16 );\r
138         xRegisterDump.ulR17_return_address_from_exceptions = mfgpr( R17 );\r
139         xRegisterDump.ulR20 = mfgpr( R20 );\r
140         xRegisterDump.ulR21 = mfgpr( R21 );\r
141         xRegisterDump.ulR22 = mfgpr( R22 );\r
142         xRegisterDump.ulR23 = mfgpr( R23 );\r
143         xRegisterDump.ulR24 = mfgpr( R24 );\r
144         xRegisterDump.ulR25 = mfgpr( R25 );\r
145         xRegisterDump.ulR26 = mfgpr( R26 );\r
146         xRegisterDump.ulR27 = mfgpr( R27 );\r
147         xRegisterDump.ulR28 = mfgpr( R28 );\r
148         xRegisterDump.ulR29 = mfgpr( R29 );\r
149         xRegisterDump.ulR30 = mfgpr( R30 );\r
150         xRegisterDump.ulR31 = mfgpr( R31 );\r
151         xRegisterDump.ulR1_SP = ( ( uint32_t ) pulStackPointerOnFunctionEntry ) + portexASM_HANDLER_STACK_FRAME_SIZE;\r
152         xRegisterDump.ulEAR = mfear();\r
153         xRegisterDump.ulESR = mfesr();\r
154         xRegisterDump.ulEDR = mfedr();\r
155 \r
156         /* Move the saved program counter back to the instruction that was executed\r
157         when the exception occurred.  This is only valid for certain types of\r
158         exception. */\r
159         xRegisterDump.ulPC = xRegisterDump.ulR17_return_address_from_exceptions - portexINSTRUCTION_SIZE;\r
160 \r
161         #if( XPAR_MICROBLAZE_USE_FPU != 0 )\r
162         {\r
163                 xRegisterDump.ulFSR = mffsr();\r
164         }\r
165         #else\r
166         {\r
167                 xRegisterDump.ulFSR = 0UL;\r
168         }\r
169         #endif\r
170 \r
171         /* Also fill in a string that describes what type of exception this is.\r
172         The string uses the same ID names as defined in the MicroBlaze standard\r
173         library exception header files. */\r
174         switch( ( uint32_t ) pvExceptionID )\r
175         {\r
176                 case XEXC_ID_FSL :\r
177                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_FSL";\r
178                                 break;\r
179 \r
180                 case XEXC_ID_UNALIGNED_ACCESS :\r
181                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_UNALIGNED_ACCESS";\r
182                                 break;\r
183 \r
184                 case XEXC_ID_ILLEGAL_OPCODE :\r
185                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_ILLEGAL_OPCODE";\r
186                                 break;\r
187 \r
188                 case XEXC_ID_M_AXI_I_EXCEPTION :\r
189                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_M_AXI_I_EXCEPTION or XEXC_ID_IPLB_EXCEPTION";\r
190                                 break;\r
191 \r
192                 case XEXC_ID_M_AXI_D_EXCEPTION :\r
193                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_M_AXI_D_EXCEPTION or XEXC_ID_DPLB_EXCEPTION";\r
194                                 break;\r
195 \r
196                 case XEXC_ID_DIV_BY_ZERO :\r
197                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_DIV_BY_ZERO";\r
198                                 break;\r
199 \r
200                 case XEXC_ID_STACK_VIOLATION :\r
201                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_STACK_VIOLATION or XEXC_ID_MMU";\r
202                                 break;\r
203 \r
204                 #if( XPAR_MICROBLAZE_USE_FPU != 0 )\r
205 \r
206                         case XEXC_ID_FPU :\r
207                                                 xRegisterDump.pcExceptionCause = ( int8_t * const ) "XEXC_ID_FPU see ulFSR value";\r
208                                                 break;\r
209 \r
210                 #endif /* XPAR_MICROBLAZE_USE_FPU */\r
211         }\r
212 \r
213         /* vApplicationExceptionRegisterDump() is a callback function that the\r
214         application can optionally define to receive the populated xPortRegisterDump\r
215         structure.  If the application chooses not to define a version of\r
216         vApplicationExceptionRegisterDump() then the weekly defined default\r
217         implementation within this file will be called instead. */\r
218         vApplicationExceptionRegisterDump( &xRegisterDump );\r
219 \r
220         /* Must not attempt to leave this function! */\r
221         for( ;; )\r
222         {\r
223                 portNOP();\r
224         }\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vPortExceptionsInstallHandlers( void )\r
229 {\r
230 static uint32_t ulHandlersAlreadyInstalled = pdFALSE;\r
231 \r
232         if( ulHandlersAlreadyInstalled == pdFALSE )\r
233         {\r
234                 ulHandlersAlreadyInstalled = pdTRUE;\r
235 \r
236                 #if XPAR_MICROBLAZE_UNALIGNED_EXCEPTIONS == 1\r
237                         microblaze_register_exception_handler( XEXC_ID_UNALIGNED_ACCESS, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_UNALIGNED_ACCESS );\r
238                 #endif /* XPAR_MICROBLAZE_UNALIGNED_EXCEPTIONS*/\r
239 \r
240                 #if XPAR_MICROBLAZE_ILL_OPCODE_EXCEPTION == 1\r
241                         microblaze_register_exception_handler( XEXC_ID_ILLEGAL_OPCODE, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_ILLEGAL_OPCODE );\r
242                 #endif /* XPAR_MICROBLAZE_ILL_OPCODE_EXCEPTION */\r
243 \r
244                 #if XPAR_MICROBLAZE_M_AXI_I_BUS_EXCEPTION == 1\r
245                         microblaze_register_exception_handler( XEXC_ID_M_AXI_I_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_M_AXI_I_EXCEPTION );\r
246                 #endif /* XPAR_MICROBLAZE_M_AXI_I_BUS_EXCEPTION */\r
247 \r
248                 #if XPAR_MICROBLAZE_M_AXI_D_BUS_EXCEPTION == 1\r
249                         microblaze_register_exception_handler( XEXC_ID_M_AXI_D_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_M_AXI_D_EXCEPTION );\r
250                 #endif /* XPAR_MICROBLAZE_M_AXI_D_BUS_EXCEPTION */\r
251 \r
252                 #if XPAR_MICROBLAZE_IPLB_BUS_EXCEPTION == 1\r
253                         microblaze_register_exception_handler( XEXC_ID_IPLB_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_IPLB_EXCEPTION );\r
254                 #endif /* XPAR_MICROBLAZE_IPLB_BUS_EXCEPTION */\r
255 \r
256                 #if XPAR_MICROBLAZE_DPLB_BUS_EXCEPTION == 1\r
257                         microblaze_register_exception_handler( XEXC_ID_DPLB_EXCEPTION, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_DPLB_EXCEPTION );\r
258                 #endif /* XPAR_MICROBLAZE_DPLB_BUS_EXCEPTION */\r
259 \r
260                 #if XPAR_MICROBLAZE_DIV_ZERO_EXCEPTION == 1\r
261                         microblaze_register_exception_handler( XEXC_ID_DIV_BY_ZERO, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_DIV_BY_ZERO );\r
262                 #endif /* XPAR_MICROBLAZE_DIV_ZERO_EXCEPTION */\r
263 \r
264                 #if XPAR_MICROBLAZE_FPU_EXCEPTION == 1\r
265                         microblaze_register_exception_handler( XEXC_ID_FPU, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_FPU );\r
266                 #endif /* XPAR_MICROBLAZE_FPU_EXCEPTION */\r
267 \r
268                 #if XPAR_MICROBLAZE_FSL_EXCEPTION == 1\r
269                         microblaze_register_exception_handler( XEXC_ID_FSL, vPortExceptionHandlerEntry, ( void * ) XEXC_ID_FSL );\r
270                 #endif /* XPAR_MICROBLAZE_FSL_EXCEPTION */\r
271 \r
272                 microblaze_enable_exceptions();\r
273         }\r
274 }\r
275 \r
276 /* Exclude the entire file if the MicroBlaze is not configured to handle\r
277 exceptions, or the application defined configuration item\r
278 configINSTALL_EXCEPTION_HANDLERS is not set to 1. */\r
279 #endif /* ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 ) */\r
280 \r
281 \r
282 \r