1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "los_stackinfo_pri.h"
33 #include "los_printf_pri.h"
34 #include "los_config.h"
35 #include "securec.h"
36 #ifdef LOSCFG_SHELL
37 #include "shcmd.h"
38 #include "shell.h"
39 #endif
40 
41 const StackInfo *g_stackInfo = NULL;
42 UINT32 g_stackNum;
43 
OsStackWaterLineGet(const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)44 UINT32 OsStackWaterLineGet(const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)
45 {
46     UINT32 size;
47     const UINTPTR *tmp = NULL;
48     if (*stackTop == OS_STACK_MAGIC_WORD) {
49         tmp = stackTop + 1;
50         while ((tmp < stackBottom) && (*tmp == OS_STACK_INIT)) {
51             tmp++;
52         }
53         size = (UINT32)((UINTPTR)stackBottom - (UINTPTR)tmp);
54         *peakUsed = (size == 0) ? size : (size + sizeof(CHAR *));
55         return LOS_OK;
56     } else {
57         *peakUsed = OS_INVALID_WATERLINE;
58         return LOS_NOK;
59     }
60 }
61 
OsExcStackChecknull62 VOID OsExcStackCheck(VOID)
63 {
64     UINT32 index;
65     UINT32 cpuid;
66     UINTPTR *stackTop = NULL;
67 
68     if (g_stackInfo == NULL) {
69         return;
70     }
71     for (index = 0; index < g_stackNum; index++) {
72         for (cpuid = 0; cpuid < LOSCFG_KERNEL_CORE_NUM; cpuid++) {
73             stackTop = (UINTPTR *)((UINTPTR)g_stackInfo[index].stackTop + cpuid * g_stackInfo[index].stackSize);
74             if (*stackTop != OS_STACK_MAGIC_WORD) {
75                 PRINT_ERR("cpu:%u %s overflow , magic word changed to 0x%x\n",
76                           LOSCFG_KERNEL_CORE_NUM - 1 - cpuid, g_stackInfo[index].stackName, *stackTop);
77             }
78         }
79     }
80 }
81 
OsExcStackInfonull82 VOID OsExcStackInfo(VOID)
83 {
84     UINT32 index;
85     UINT32 cpuid;
86     UINT32 size;
87     UINTPTR *stackTop = NULL;
88     UINTPTR *stack = NULL;
89 
90     if (g_stackInfo == NULL) {
91         return;
92     }
93 
94     PrintExcInfo("\n stack name    cpu id     stack addr     total size   used size\n"
95                  " ----------    ------     ---------      --------     --------\n");
96 
97     for (index = 0; index < g_stackNum; index++) {
98         for (cpuid = 0; cpuid < LOSCFG_KERNEL_CORE_NUM; cpuid++) {
99             stackTop = (UINTPTR *)((UINTPTR)g_stackInfo[index].stackTop + cpuid * g_stackInfo[index].stackSize);
100             stack = (UINTPTR *)((UINTPTR)stackTop + g_stackInfo[index].stackSize);
101             (VOID)OsStackWaterLineGet(stack, stackTop, &size);
102 
103             PrintExcInfo("%11s      %-5d    %-10p     0x%-8x   0x%-4x\n", g_stackInfo[index].stackName,
104                          LOSCFG_KERNEL_CORE_NUM - 1 - cpuid, stackTop, g_stackInfo[index].stackSize, size);
105         }
106     }
107 
108     OsExcStackCheck();
109 }
110 
OsExcStackInfoReg(const StackInfo *stackInfo, UINT32 stackNum)111 VOID OsExcStackInfoReg(const StackInfo *stackInfo, UINT32 stackNum)
112 {
113     g_stackInfo = stackInfo;
114     g_stackNum = stackNum;
115 }
116 
OsStackInit(VOID *stacktop, UINT32 stacksize)117 VOID OsStackInit(VOID *stacktop, UINT32 stacksize)
118 {
119     /* initialize the task stack, write magic num to stack top */
120     errno_t ret = memset_s(stacktop, stacksize, (INT32)OS_STACK_INIT, stacksize);
121     if (ret == EOK) {
122         *((UINTPTR *)stacktop) = OS_STACK_MAGIC_WORD;
123     }
124 }
125 
126 #ifdef LOSCFG_SHELL_CMD_DEBUG
127 SHELLCMD_ENTRY(stack_shellcmd, CMD_TYPE_EX, "stack", 1, (CmdCallBackFunc)OsExcStackInfo);
128 #endif
129