1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Thread                                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "tx_api.h"
28 #if defined(TX_MISRA_ENABLE) || defined(TX_ENABLE_STACK_CHECKING) || defined(TX_PORT_THREAD_STACK_ERROR_HANDLING)
29 #include "tx_thread.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _tx_thread_stack_error_handler                      PORTABLE C      */
37 /*                                                           6.1.9        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes stack errors detected during run-time. The  */
45 /*    processing currently consists of a spin loop.                       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    thread_ptr                            Thread control block pointer  */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    ThreadX internal code                                               */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
68 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
69 /*                                            update misra support,       */
70 /*                                            resulting in version 6.1    */
71 /*  10-16-2020     William E. Lamie         Modified comment(s),          */
72 /*                                            fixed link issue,           */
73 /*                                            resulting in version 6.1.1  */
74 /*  06-02-2021     William E. Lamie         Modified comment(s),          */
75 /*                                            fixed link issue, added     */
76 /*                                            conditional compilation     */
77 /*                                            for ARMv8-M (Cortex M23/33) */
78 /*                                            resulting in version 6.1.7  */
79 /*  10-15-2021     Yuxin Zhou               Modified comment(s), improved */
80 /*                                            stack check error handling, */
81 /*                                            resulting in version 6.1.9  */
82 /*                                                                        */
83 /**************************************************************************/
_tx_thread_stack_error_handler(TX_THREAD * thread_ptr)84 VOID  _tx_thread_stack_error_handler(TX_THREAD *thread_ptr)
85 {
86 
87 TX_INTERRUPT_SAVE_AREA
88 
89 #if defined(TX_ENABLE_STACK_CHECKING) || defined(TX_PORT_THREAD_STACK_ERROR_HANDLING)
90 
91     /* Disable interrupts.  */
92     TX_DISABLE
93 
94     /* Determine if the application has registered an error handler.  */
95     if (_tx_thread_application_stack_error_handler != TX_NULL)
96     {
97 
98         /* Yes, an error handler is present, simply call the application error handler.  */
99         (_tx_thread_application_stack_error_handler)(thread_ptr);
100     }
101 
102     /* Restore interrupts.  */
103     TX_RESTORE
104 
105 #else
106 
107     /* Access input argument just for the sake of lint, MISRA, etc.  */
108     if (thread_ptr != TX_NULL)
109     {
110 
111         /* Disable interrupts.  */
112         TX_DISABLE
113 
114         /* Restore interrupts.  */
115         TX_RESTORE
116     }
117 #endif
118 }
119 #endif
120