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 #include "tx_user.h"
47
48 #ifdef TX_ENABLE_STACK_CHECKING
49
50 /* Include necessary system files. */
51
52 #include "tx_api.h"
53
54 #include "xtensa_rtos.h"
55 #ifdef XT_BOARD
56 #include <xtensa/xtbsp.h>
57 #endif
58 #ifdef XT_SIMULATOR
59 #include <xtensa/simcall.h>
60 #endif
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65
66
67 /**************************************************************************/
68 /* */
69 /* DESCRIPTION */
70 /* */
71 /* Callback to notify of a stack overflow when registered with */
72 /* tx_stack_error_notify and stack checking is enabled (ThreadX */
73 /* is compiled with TX_ENABLE_STACK_CHECKING defined). */
74 /* */
75 /* The handler notifies the user in any/all of the following ways: */
76 /* - A message via the simulator (extremely reliable, simulator only). */
77 /* - A message on the board's display (emulation board only). */
78 /* - A message to stdout (uses low-level write to avoid printf which */
79 /* is large and would trash state the user might want to examine). */
80 /* The most reliable methods are done first. Several might work. */
81 /* */
82 /* After notifying the user as best it can, the handler stops the */
83 /* application in the most reliable of the following ways: */
84 /* - Passes control to the debugger (if attached). */
85 /* - Terminates the simulation (simulator only). */
86 /* - Panics. */
87 /* */
88 /* RELEASE HISTORY */
89 /* */
90 /* DATE NAME DESCRIPTION */
91 /* */
92 /* 12-31-2020 Cadence Design Systems Initial Version 6.1.3 */
93 /* */
94 /**************************************************************************/
95
_tx_xtensa_stack_error_handler(TX_THREAD * thread)96 VOID _tx_xtensa_stack_error_handler(TX_THREAD * thread)
97 {
98 #ifdef XT_SIMULATOR
99 register int32_t sc __asm__ ("a2") = SYS_log_msg;
100 register char * msg __asm__ ("a3")
101 = "**** Stack overflow in thread 0x%08x.\n";
102 register TX_THREAD * thd __asm__ ("a4") = thread;
103 __asm__ volatile ("simcall" :: "a" (sc), "a" (msg), "a" (thd) );
104 #endif
105
106 #ifdef XT_BOARD
107 xtbsp_display_string("StkOflow");
108 #endif
109
110 write(1, "**** Stack overflow in thread \"", 31);
111 write(1, thread->tx_thread_name, strlen(thread->tx_thread_name));
112 write(1, "\"\n", 2);
113
114 #ifdef XT_SIMULATOR
115 sc = SYS_gdb_abort;
116 __asm__ volatile ("simcall"); /* control to debugger or exit */
117 #else
118 __asm__ volatile ("break 1, 15"); /* control to debugger or panic */
119 #endif
120 }
121
122 #endif /* TX_ENABLE_STACK_CHECKING */
123
124