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 #ifdef TX_ENABLE_STACK_CHECKING
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          */
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 /*  09-30-2020     William E. Lamie         Initial Version 6.1           */
75 /*                                                                        */
76 /**************************************************************************/
_tx_thread_stack_error_notify(VOID (* stack_error_handler)(TX_THREAD * thread_ptr))77 UINT  _tx_thread_stack_error_notify(VOID (*stack_error_handler)(TX_THREAD *thread_ptr))
78 {
79 
80 #ifndef TX_ENABLE_STACK_CHECKING
81 
82 UINT        status;
83 
84 
85     /* Access input argument just for the sake of lint, MISRA, etc.  */
86     if (stack_error_handler != TX_NULL)
87     {
88 
89         /* Stack checking is not enabled, just return an error.  */
90         status =  TX_FEATURE_NOT_ENABLED;
91     }
92     else
93     {
94 
95         /* Stack checking is not enabled, just return an error.  */
96         status =  TX_FEATURE_NOT_ENABLED;
97     }
98 
99     /* Return completion status.  */
100     return(status);
101 #else
102 
103 TX_INTERRUPT_SAVE_AREA
104 
105 
106     /* Disable interrupts.  */
107     TX_DISABLE
108 
109     /* Make entry in event log.  */
110     TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_STACK_ERROR_NOTIFY, 0, 0, 0, 0, TX_TRACE_THREAD_EVENTS)
111 
112     /* Make entry in event log.  */
113     TX_EL_THREAD_STACK_ERROR_NOTIFY_INSERT
114 
115     /* Setup global thread stack error handler.  */
116     _tx_thread_application_stack_error_handler =  stack_error_handler;
117 
118     /* Restore interrupts.  */
119     TX_RESTORE
120 
121     /* Return success to caller.  */
122     return(TX_SUCCESS);
123 #endif
124 }
125 
126