10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "los_stackinfo_pri.h"
330d163575Sopenharmony_ci#include "los_printf_pri.h"
340d163575Sopenharmony_ci#include "los_config.h"
350d163575Sopenharmony_ci#include "securec.h"
360d163575Sopenharmony_ci#ifdef LOSCFG_SHELL
370d163575Sopenharmony_ci#include "shcmd.h"
380d163575Sopenharmony_ci#include "shell.h"
390d163575Sopenharmony_ci#endif
400d163575Sopenharmony_ci
410d163575Sopenharmony_ciconst StackInfo *g_stackInfo = NULL;
420d163575Sopenharmony_ciUINT32 g_stackNum;
430d163575Sopenharmony_ci
440d163575Sopenharmony_ciUINT32 OsStackWaterLineGet(const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)
450d163575Sopenharmony_ci{
460d163575Sopenharmony_ci    UINT32 size;
470d163575Sopenharmony_ci    const UINTPTR *tmp = NULL;
480d163575Sopenharmony_ci    if (*stackTop == OS_STACK_MAGIC_WORD) {
490d163575Sopenharmony_ci        tmp = stackTop + 1;
500d163575Sopenharmony_ci        while ((tmp < stackBottom) && (*tmp == OS_STACK_INIT)) {
510d163575Sopenharmony_ci            tmp++;
520d163575Sopenharmony_ci        }
530d163575Sopenharmony_ci        size = (UINT32)((UINTPTR)stackBottom - (UINTPTR)tmp);
540d163575Sopenharmony_ci        *peakUsed = (size == 0) ? size : (size + sizeof(CHAR *));
550d163575Sopenharmony_ci        return LOS_OK;
560d163575Sopenharmony_ci    } else {
570d163575Sopenharmony_ci        *peakUsed = OS_INVALID_WATERLINE;
580d163575Sopenharmony_ci        return LOS_NOK;
590d163575Sopenharmony_ci    }
600d163575Sopenharmony_ci}
610d163575Sopenharmony_ci
620d163575Sopenharmony_ciVOID OsExcStackCheck(VOID)
630d163575Sopenharmony_ci{
640d163575Sopenharmony_ci    UINT32 index;
650d163575Sopenharmony_ci    UINT32 cpuid;
660d163575Sopenharmony_ci    UINTPTR *stackTop = NULL;
670d163575Sopenharmony_ci
680d163575Sopenharmony_ci    if (g_stackInfo == NULL) {
690d163575Sopenharmony_ci        return;
700d163575Sopenharmony_ci    }
710d163575Sopenharmony_ci    for (index = 0; index < g_stackNum; index++) {
720d163575Sopenharmony_ci        for (cpuid = 0; cpuid < LOSCFG_KERNEL_CORE_NUM; cpuid++) {
730d163575Sopenharmony_ci            stackTop = (UINTPTR *)((UINTPTR)g_stackInfo[index].stackTop + cpuid * g_stackInfo[index].stackSize);
740d163575Sopenharmony_ci            if (*stackTop != OS_STACK_MAGIC_WORD) {
750d163575Sopenharmony_ci                PRINT_ERR("cpu:%u %s overflow , magic word changed to 0x%x\n",
760d163575Sopenharmony_ci                          LOSCFG_KERNEL_CORE_NUM - 1 - cpuid, g_stackInfo[index].stackName, *stackTop);
770d163575Sopenharmony_ci            }
780d163575Sopenharmony_ci        }
790d163575Sopenharmony_ci    }
800d163575Sopenharmony_ci}
810d163575Sopenharmony_ci
820d163575Sopenharmony_ciVOID OsExcStackInfo(VOID)
830d163575Sopenharmony_ci{
840d163575Sopenharmony_ci    UINT32 index;
850d163575Sopenharmony_ci    UINT32 cpuid;
860d163575Sopenharmony_ci    UINT32 size;
870d163575Sopenharmony_ci    UINTPTR *stackTop = NULL;
880d163575Sopenharmony_ci    UINTPTR *stack = NULL;
890d163575Sopenharmony_ci
900d163575Sopenharmony_ci    if (g_stackInfo == NULL) {
910d163575Sopenharmony_ci        return;
920d163575Sopenharmony_ci    }
930d163575Sopenharmony_ci
940d163575Sopenharmony_ci    PrintExcInfo("\n stack name    cpu id     stack addr     total size   used size\n"
950d163575Sopenharmony_ci                 " ----------    ------     ---------      --------     --------\n");
960d163575Sopenharmony_ci
970d163575Sopenharmony_ci    for (index = 0; index < g_stackNum; index++) {
980d163575Sopenharmony_ci        for (cpuid = 0; cpuid < LOSCFG_KERNEL_CORE_NUM; cpuid++) {
990d163575Sopenharmony_ci            stackTop = (UINTPTR *)((UINTPTR)g_stackInfo[index].stackTop + cpuid * g_stackInfo[index].stackSize);
1000d163575Sopenharmony_ci            stack = (UINTPTR *)((UINTPTR)stackTop + g_stackInfo[index].stackSize);
1010d163575Sopenharmony_ci            (VOID)OsStackWaterLineGet(stack, stackTop, &size);
1020d163575Sopenharmony_ci
1030d163575Sopenharmony_ci            PrintExcInfo("%11s      %-5d    %-10p     0x%-8x   0x%-4x\n", g_stackInfo[index].stackName,
1040d163575Sopenharmony_ci                         LOSCFG_KERNEL_CORE_NUM - 1 - cpuid, stackTop, g_stackInfo[index].stackSize, size);
1050d163575Sopenharmony_ci        }
1060d163575Sopenharmony_ci    }
1070d163575Sopenharmony_ci
1080d163575Sopenharmony_ci    OsExcStackCheck();
1090d163575Sopenharmony_ci}
1100d163575Sopenharmony_ci
1110d163575Sopenharmony_ciVOID OsExcStackInfoReg(const StackInfo *stackInfo, UINT32 stackNum)
1120d163575Sopenharmony_ci{
1130d163575Sopenharmony_ci    g_stackInfo = stackInfo;
1140d163575Sopenharmony_ci    g_stackNum = stackNum;
1150d163575Sopenharmony_ci}
1160d163575Sopenharmony_ci
1170d163575Sopenharmony_ciVOID OsStackInit(VOID *stacktop, UINT32 stacksize)
1180d163575Sopenharmony_ci{
1190d163575Sopenharmony_ci    /* initialize the task stack, write magic num to stack top */
1200d163575Sopenharmony_ci    errno_t ret = memset_s(stacktop, stacksize, (INT32)OS_STACK_INIT, stacksize);
1210d163575Sopenharmony_ci    if (ret == EOK) {
1220d163575Sopenharmony_ci        *((UINTPTR *)stacktop) = OS_STACK_MAGIC_WORD;
1230d163575Sopenharmony_ci    }
1240d163575Sopenharmony_ci}
1250d163575Sopenharmony_ci
1260d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_CMD_DEBUG
1270d163575Sopenharmony_ciSHELLCMD_ENTRY(stack_shellcmd, CMD_TYPE_EX, "stack", 1, (CmdCallBackFunc)OsExcStackInfo);
1280d163575Sopenharmony_ci#endif
129