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