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