• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "stdlib.h"
33 #include "los_memory_pri.h"
34 #ifdef LOSCFG_SAVE_EXCINFO
35 #include "los_excinfo_pri.h"
36 #endif
37 #ifdef LOSCFG_SHELL
38 #include "shcmd.h"
39 #include "shell.h"
40 #endif
41 #include "los_vm_common.h"
42 #include "los_vm_boot.h"
43 #include "los_vm_map.h"
44 #include "los_vm_dump.h"
45 
46 
47 #define MEM_SIZE_1K 0x400
48 #define MEM_SIZE_1M 0x100000
49 
50 #define MEM_SIZE_TO_KB(size)    (((size) + (MEM_SIZE_1K >> 1)) / MEM_SIZE_1K)
51 #define MEM_SIZE_TO_MB(size)    (((size) + (MEM_SIZE_1M >> 1)) / MEM_SIZE_1M)
52 
OsDumpMemByte(size_t length,UINTPTR addr)53 VOID OsDumpMemByte(size_t length, UINTPTR addr)
54 {
55     size_t dataLen;
56     UINTPTR *alignAddr = NULL;
57     UINT32 count = 0;
58 
59     dataLen = ALIGN(length, sizeof(UINTPTR));
60     alignAddr = (UINTPTR *)TRUNCATE(addr, sizeof(UINTPTR));
61     if ((dataLen == 0) || (alignAddr == NULL)) {
62         return;
63     }
64     while (dataLen) {
65         if (IS_ALIGNED(count, sizeof(CHAR *))) {
66             PRINTK("\n 0x%lx :", alignAddr);
67 #ifdef LOSCFG_SAVE_EXCINFO
68             WriteExcInfoToBuf("\n 0x%lx :", alignAddr);
69 #endif
70         }
71 #ifdef __LP64__
72         PRINTK("%0+16lx ", *alignAddr);
73 #else
74         PRINTK("%0+8lx ", *alignAddr);
75 #endif
76 #ifdef LOSCFG_SAVE_EXCINFO
77 #ifdef __LP64__
78         WriteExcInfoToBuf("0x%0+16x ", *alignAddr);
79 #else
80         WriteExcInfoToBuf("0x%0+8x ", *alignAddr);
81 #endif
82 #endif
83         alignAddr++;
84         dataLen -= sizeof(CHAR *);
85         count++;
86     }
87     PRINTK("\n");
88 #ifdef LOSCFG_SAVE_EXCINFO
89     WriteExcInfoToBuf("\n");
90 #endif
91 
92     return;
93 }
94 
OsShellCmdMemCheck(INT32 argc,const CHAR * argv[])95 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdMemCheck(INT32 argc, const CHAR *argv[])
96 {
97     if (argc > 0) {
98         PRINTK("\nUsage: memcheck\n");
99         return OS_ERROR;
100     }
101 
102     if (LOS_MemIntegrityCheck(m_aucSysMem1) == LOS_OK) {
103         PRINTK("system memcheck over, all passed!\n");
104 #ifdef LOSCFG_SAVE_EXCINFO
105         WriteExcInfoToBuf("system memcheck over, all passed!\n");
106 #endif
107     }
108 
109     return 0;
110 }
111 
112 #ifdef LOSCFG_SHELL
OsShellCmdSectionInfo(INT32 argc,const CHAR * argv[])113 LITE_OS_SEC_TEXT_MINOR STATIC VOID OsShellCmdSectionInfo(INT32 argc, const CHAR *argv[])
114 {
115     size_t textLen = &__text_end - &__text_start;
116     size_t dataLen = &__ram_data_end - &__ram_data_start;
117     size_t rodataLen = &__rodata_end - &__rodata_start;
118     size_t bssLen = &__bss_end - &__bss_start;
119 
120     PRINTK("\r\n        text         data          rodata        bss\n");
121     if ((argc == 1) && (strcmp(argv[0], "-k") == 0)) {
122         PRINTK("Mem:    %-9lu    %-10lu    %-10lu    %-10lu\n", MEM_SIZE_TO_KB(textLen), MEM_SIZE_TO_KB(dataLen),
123                MEM_SIZE_TO_KB(rodataLen), MEM_SIZE_TO_KB(bssLen));
124     } else if ((argc == 1) && (strcmp(argv[0], "-m") == 0)) {
125         PRINTK("Mem:    %-9lu    %-10lu    %-10lu    %-10lu\n", MEM_SIZE_TO_MB(textLen), MEM_SIZE_TO_MB(dataLen),
126                MEM_SIZE_TO_MB(rodataLen), MEM_SIZE_TO_MB(bssLen));
127     } else {
128         PRINTK("Mem:    %-9lu    %-10lu    %-10lu    %-10lu\n", textLen, dataLen, rodataLen, bssLen);
129     }
130 }
131 
OsShellCmdFreeInfo(INT32 argc,const CHAR * argv[])132 LITE_OS_SEC_TEXT_MINOR STATIC UINT32 OsShellCmdFreeInfo(INT32 argc, const CHAR *argv[])
133 {
134     UINT32 memUsed = LOS_MemTotalUsedGet(m_aucSysMem1);
135     UINT32 totalMem = LOS_MemPoolSizeGet(m_aucSysMem1);
136     UINT32 freeMem = totalMem - memUsed;
137     UINT32 memUsedHeap = memUsed;
138 
139 #ifdef LOSCFG_KERNEL_VM
140     UINT32 usedCount, totalCount;
141     OsVmPhysUsedInfoGet(&usedCount, &totalCount);
142     totalMem = SYS_MEM_SIZE_DEFAULT;
143     memUsed = SYS_MEM_SIZE_DEFAULT - (totalCount << PAGE_SHIFT);
144     memUsed += (usedCount << PAGE_SHIFT) - freeMem;
145     freeMem = totalMem - memUsed;
146 #else
147     totalMem = SYS_MEM_SIZE_DEFAULT;
148     memUsed = g_vmBootMemBase - KERNEL_ASPACE_BASE;
149     memUsed -= freeMem;
150     freeMem -= totalMem - memUsed;
151 #endif
152 
153     if ((argc == 0) ||
154         ((argc == 1) && (strcmp(argv[0], "-k") == 0)) ||
155         ((argc == 1) && (strcmp(argv[0], "-m") == 0))) {
156         PRINTK("\r\n        total        used          free          heap\n");
157     }
158 
159     if ((argc == 1) && (strcmp(argv[0], "-k") == 0)) {
160         PRINTK("Mem:    %-9u    %-10u    %-10u    %-10u\n", MEM_SIZE_TO_KB(totalMem), MEM_SIZE_TO_KB(memUsed),
161                MEM_SIZE_TO_KB(freeMem), MEM_SIZE_TO_KB(memUsedHeap));
162     } else if ((argc == 1) && (strcmp(argv[0], "-m") == 0)) {
163         PRINTK("Mem:    %-9u    %-10u    %-10u    %-10u\n", MEM_SIZE_TO_MB(totalMem), MEM_SIZE_TO_MB(memUsed),
164                MEM_SIZE_TO_MB(freeMem), MEM_SIZE_TO_MB(memUsedHeap));
165     } else if (argc == 0) {
166         PRINTK("Mem:    %-9u    %-10u    %-10u    %-10u\n", totalMem, memUsed, freeMem, memUsedHeap);
167     } else {
168         PRINTK("\nUsage: free or free [-k/-m]\n");
169         return OS_ERROR;
170     }
171     return 0;
172 }
173 
OsShellCmdFree(INT32 argc,const CHAR * argv[])174 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdFree(INT32 argc, const CHAR *argv[])
175 {
176     if (argc > 1) {
177         PRINTK("\nUsage: free or free [-k/-m]\n");
178         return OS_ERROR;
179     }
180     if (OsShellCmdFreeInfo(argc, argv) != 0) {
181         return OS_ERROR;
182     }
183     OsShellCmdSectionInfo(argc, argv);
184     return 0;
185 }
186 
OsShellCmdUname(INT32 argc,const CHAR * argv[])187 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdUname(INT32 argc, const CHAR *argv[])
188 {
189     if (argc == 0) {
190         PRINTK("%s\n", KERNEL_NAME);
191         return 0;
192     }
193 
194     if (argc == 1) {
195         if (strcmp(argv[0], "-a") == 0) {
196             PRINTK("%s %d.%d.%d.%d %s %s\n", KERNEL_NAME, KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE, \
197                 __DATE__, __TIME__);
198             return 0;
199         } else if (strcmp(argv[0], "-s") == 0) {
200             PRINTK("%s\n", KERNEL_NAME);
201             return 0;
202         } else if (strcmp(argv[0], "-t") == 0) {
203             PRINTK("build date : %s %s\n", __DATE__, __TIME__);
204             return 0;
205         } else if (strcmp(argv[0], "-v") == 0) {
206             PRINTK("%d.%d.%d.%d\n", KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE);
207             return 0;
208         } else if (strcmp(argv[0], "--help") == 0) {
209             PRINTK("-a,            print all information\n"
210                    "-s,            print the kernel name\n"
211                    "-t,            print the build date\n"
212                    "-v,            print the kernel version\n");
213             return 0;
214         }
215     }
216 
217     PRINTK("uname: invalid option %s\n"
218            "Try 'uname --help' for more information.\n",
219            argv[0]);
220     return OS_ERROR;
221 }
222 #ifdef LOSCFG_MEM_LEAKCHECK
OsShellCmdMemUsed(INT32 argc,const CHAR * argv[])223 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdMemUsed(INT32 argc, const CHAR *argv[])
224 {
225     if (argc > 0) {
226         PRINTK("\nUsage: memused\n");
227         return OS_ERROR;
228     }
229 
230     OsMemUsedNodeShow(m_aucSysMem1);
231 
232     return 0;
233 }
234 #endif
235 
236 #ifdef LOSCFG_MEM_LEAKCHECK
237 SHELLCMD_ENTRY(memused_shellcmd, CMD_TYPE_EX, "memused", 0, (CmdCallBackFunc)OsShellCmdMemUsed);
238 #endif
239 
240 #ifdef LOSCFG_SHELL_CMD_DEBUG
241 SHELLCMD_ENTRY(memcheck_shellcmd, CMD_TYPE_EX, "memcheck", 0, (CmdCallBackFunc)OsShellCmdMemCheck);
242 #endif
243 SHELLCMD_ENTRY(free_shellcmd, CMD_TYPE_EX, "free", XARGS, (CmdCallBackFunc)OsShellCmdFree);
244 SHELLCMD_ENTRY(uname_shellcmd, CMD_TYPE_EX, "uname", XARGS, (CmdCallBackFunc)OsShellCmdUname);
245 #endif
246 
247