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