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 /*   Copyright (c) Cadence Design Systems, Inc.                           */
14 /*                                                                        */
15 /* Permission is hereby granted, free of charge, to any person obtaining  */
16 /* a copy of this software and associated documentation files (the        */
17 /* "Software"), to deal in the Software without restriction, including    */
18 /* without limitation the rights to use, copy, modify, merge, publish,    */
19 /* distribute, sublicense, and/or sell copies of the Software, and to     */
20 /* permit persons to whom the Software is furnished to do so, subject to  */
21 /* the following conditions:                                              */
22 /*                                                                        */
23 /* The above copyright notice and this permission notice shall be         */
24 /* included in all copies or substantial portions of the Software.        */
25 /*                                                                        */
26 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
27 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
28 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
29 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
30 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
31 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
32 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
33 /**************************************************************************/
34 
35 /**************************************************************************/
36 /**************************************************************************/
37 /**                                                                       */
38 /** ThreadX Component                                                     */
39 /**                                                                       */
40 /**   Support for Xtensa applications                                     */
41 /**                                                                       */
42 /**************************************************************************/
43 /**************************************************************************/
44 
45 
46 #ifdef TX_INCLUDE_USER_DEFINE_FILE
47 #include "tx_user.h"
48 #endif
49 
50 #ifdef TX_ENABLE_STACK_CHECKING
51 
52 /* Include necessary system files.  */
53 
54 #include "tx_api.h"
55 
56 #include "xtensa_rtos.h"
57 #ifdef XT_BOARD
58 #include <xtensa/xtbsp.h>
59 #endif
60 #ifdef XT_SIMULATOR
61 #include <xtensa/simcall.h>
62 #endif
63 
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 
68 
69 /**************************************************************************/
70 /*                                                                        */
71 /*  DESCRIPTION                                                           */
72 /*                                                                        */
73 /*    Callback to notify of a stack overflow when registered with         */
74 /*    tx_stack_error_notify and stack checking is enabled (ThreadX        */
75 /*    is compiled with TX_ENABLE_STACK_CHECKING defined).                 */
76 /*                                                                        */
77 /*    The handler notifies the user in any/all of the following ways:     */
78 /*    - A message via the simulator (extremely reliable, simulator only). */
79 /*    - A message on the board's display (emulation board only).          */
80 /*    - A message to stdout (uses low-level write to avoid printf which   */
81 /*      is large and would trash state the user might want to examine).   */
82 /*    The most reliable methods are done first. Several might work.       */
83 /*                                                                        */
84 /*    After notifying the user as best it can, the handler stops the      */
85 /*    application in the most reliable of the following ways:             */
86 /*    - Passes control to the debugger (if attached).                     */
87 /*    - Terminates the simulation (simulator only).                       */
88 /*    - Panics.                                                           */
89 /*                                                                        */
90 /*  RELEASE HISTORY                                                       */
91 /*                                                                        */
92 /*    DATE              NAME                      DESCRIPTION             */
93 /*                                                                        */
94 /*  12-31-2020     Cadence Design Systems   Initial Version 6.1.3         */
95 /*                                                                        */
96 /**************************************************************************/
97 
_tx_xtensa_stack_error_handler(TX_THREAD * thread)98 VOID  _tx_xtensa_stack_error_handler(TX_THREAD * thread)
99 {
100     #ifdef XT_SIMULATOR
101     register int32_t sc  __asm__ ("a2") = SYS_log_msg;
102     register char *  msg __asm__ ("a3")
103         = "**** Stack overflow in thread 0x%08x.\n";
104     register TX_THREAD * thd __asm__ ("a4") = thread;
105     __asm__ volatile ("simcall" :: "a" (sc), "a" (msg), "a" (thd) );
106     #endif
107 
108     #ifdef XT_BOARD
109     xtbsp_display_string("StkOflow");
110     #endif
111 
112     write(1, "**** Stack overflow in thread \"", 31);
113     write(1, thread->tx_thread_name, strlen(thread->tx_thread_name));
114     write(1, "\"\n", 2);
115 
116     #ifdef XT_SIMULATOR
117     sc = SYS_gdb_abort;
118     __asm__ volatile ("simcall");           /* control to debugger or exit */
119     #else
120     __asm__ volatile ("break 1, 15");       /* control to debugger or panic */
121     #endif
122 }
123 
124 #endif /* TX_ENABLE_STACK_CHECKING */
125 
126