1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Thread                                                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "tx_api.h"
29 #include "tx_thread.h"
30 #if defined(TX_ENABLE_STACK_CHECKING) || defined(TX_PORT_THREAD_STACK_ERROR_HANDLING)
31 #include "tx_trace.h"
32 #endif
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _tx_thread_stack_error_notify                       PORTABLE C      */
40 /*                                                           6.1.9        */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    William E. Lamie, Microsoft Corporation                             */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function registers an application stack error handler. If      */
48 /*    ThreadX detects a stack error, this application handler is called.  */
49 /*                                                                        */
50 /*    Note: stack checking must be enabled for this routine to serve any  */
51 /*    purpose via the TX_ENABLE_STACK_CHECKING define.                    */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    stack_error_handler                   Pointer to stack error        */
56 /*                                            handler, TX_NULL to disable */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    status                                Service return status         */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    None                                                                */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  06-02-2021     Yuxin Zhou               Modified comment(s), added    */
78 /*                                            conditional compilation     */
79 /*                                            for ARMv8-M (Cortex M23/33) */
80 /*                                            resulting in version 6.1.7  */
81 /*  10-15-2021     Yuxin Zhou               Modified comment(s), improved */
82 /*                                            stack check error handling, */
83 /*                                            resulting in version 6.1.9  */
84 /*                                                                        */
85 /**************************************************************************/
_tx_thread_stack_error_notify(VOID (* stack_error_handler)(TX_THREAD * thread_ptr))86 UINT  _tx_thread_stack_error_notify(VOID (*stack_error_handler)(TX_THREAD *thread_ptr))
87 {
88 
89 #if !defined(TX_ENABLE_STACK_CHECKING) && !defined(TX_PORT_THREAD_STACK_ERROR_HANDLING)
90 
91 UINT        status;
92 
93 
94     /* Access input argument just for the sake of lint, MISRA, etc.  */
95     if (stack_error_handler != TX_NULL)
96     {
97 
98         /* Stack checking is not enabled, just return an error.  */
99         status =  TX_FEATURE_NOT_ENABLED;
100     }
101     else
102     {
103 
104         /* Stack checking is not enabled, just return an error.  */
105         status =  TX_FEATURE_NOT_ENABLED;
106     }
107 
108     /* Return completion status.  */
109     return(status);
110 
111 #else
112 
113 TX_INTERRUPT_SAVE_AREA
114 
115 
116     /* Disable interrupts.  */
117     TX_DISABLE
118 
119     /* Make entry in event log.  */
120     TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_STACK_ERROR_NOTIFY, 0, 0, 0, 0, TX_TRACE_THREAD_EVENTS)
121 
122     /* Make entry in event log.  */
123     TX_EL_THREAD_STACK_ERROR_NOTIFY_INSERT
124 
125     /* Setup global thread stack error handler.  */
126     _tx_thread_application_stack_error_handler =  stack_error_handler;
127 
128     /* Restore interrupts.  */
129     TX_RESTORE
130 
131     /* Return success to caller.  */
132     return(TX_SUCCESS);
133 #endif
134 }
135