• 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 "stdio.h"
34 #include "ctype.h"
35 #include "los_printf.h"
36 #include "string.h"
37 #include "securec.h"
38 #ifdef LOSCFG_SHELL
39 #include "shcmd.h"
40 #include "shell.h"
41 #endif
42 #include "los_oom.h"
43 #include "los_vm_dump.h"
44 #include "los_process_pri.h"
45 #ifdef LOSCFG_FS_VFS
46 #include "path_cache.h"
47 #endif
48 
49 #ifdef LOSCFG_KERNEL_VM
50 
51 #define ARGC_2             2
52 #define ARGC_1             1
53 #define ARGC_0             0
54 #define VMM_CMD            "vmm"
55 #define OOM_CMD            "oom"
56 #define VMM_PMM_CMD        "v2p"
57 
OsDumpKernelAspace(VOID)58 LITE_OS_SEC_TEXT_MINOR VOID OsDumpKernelAspace(VOID)
59 {
60     LosVmSpace *kAspace = LOS_GetKVmSpace();
61     if (kAspace != NULL)  {
62         OsDumpAspace(kAspace);
63     } else {
64         VM_ERR("kernel aspace is NULL");
65     }
66     return;
67 }
68 
OsPid(const CHAR * str)69 LITE_OS_SEC_TEXT_MINOR INT32 OsPid(const CHAR *str)
70 {
71     UINT32 len = strlen(str);
72     if (len <= 2) { // pid range is 0~63, max pid string length is 2
73         for (UINT32 i = 0; i < len; i++) {
74             if (isdigit(str[i]) == 0) {
75                 return -1;
76             }
77         }
78         return atoi(str);
79     }
80     return -1;
81 }
82 
OsPrintUsage(VOID)83 LITE_OS_SEC_TEXT_MINOR VOID OsPrintUsage(VOID)
84 {
85     PRINTK("-a,            print all vm address space information\n"
86            "-k,            print the kernel vm address space information\n"
87            "pid(0~63),     print process[pid] vm address space information\n"
88            "-h | --help,   print vmm command usage\n");
89 }
90 
OsDoDumpVm(pid_t pid)91 LITE_OS_SEC_TEXT_MINOR VOID OsDoDumpVm(pid_t pid)
92 {
93     LosProcessCB *processCB = NULL;
94 
95     if (OsProcessIDUserCheckInvalid(pid)) {
96         PRINTK("\tThe process [%d] not valid\n", pid);
97         return;
98     }
99 
100     processCB = OS_PCB_FROM_PID(pid);
101     if (!OsProcessIsUnused(processCB) && (processCB->vmSpace != NULL)) {
102         OsDumpAspace(processCB->vmSpace);
103     } else {
104         PRINTK("\tThe process [%d] not active\n", pid);
105     }
106 }
107 
OsShellCmdDumpVm(INT32 argc,const CHAR * argv[])108 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdDumpVm(INT32 argc, const CHAR *argv[])
109 {
110     if (argc == 0) {
111         OsDumpAllAspace();
112     } else if (argc == 1) {
113         pid_t pid = OsPid(argv[0]);
114         if (strcmp(argv[0], "-a") == 0) {
115             OsDumpAllAspace();
116         } else if (strcmp(argv[0], "-k") == 0) {
117             OsDumpKernelAspace();
118         } else if (pid >= 0) {
119             OsDoDumpVm(pid);
120         } else if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) {
121             OsPrintUsage();
122         } else {
123             PRINTK("%s: invalid option: %s\n", VMM_CMD, argv[0]);
124             OsPrintUsage();
125         }
126     } else {
127         OsPrintUsage();
128     }
129 
130     return OS_ERROR;
131 }
132 
V2PPrintUsage(VOID)133 LITE_OS_SEC_TEXT_MINOR VOID V2PPrintUsage(VOID)
134 {
135     PRINTK("pid vaddr(0x1000000~0x3e000000),     print physical address of virtual address\n"
136            "-h | --help,                     print v2p command usage\n");
137 }
138 
OsShellCmdV2P(INT32 argc,const CHAR * argv[])139 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdV2P(INT32 argc, const CHAR *argv[])
140 {
141     UINT32 vaddr;
142     PADDR_T paddr;
143     CHAR *endPtr = NULL;
144 
145     if (argc == 0) {
146         V2PPrintUsage();
147     } else if (argc == 1) {
148         if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) {
149             V2PPrintUsage();
150         }
151     } else if (argc == 2) {
152         pid_t pid = OsPid(argv[0]);
153         vaddr = strtoul((CHAR *)argv[1], &endPtr, 0);
154         if ((endPtr == NULL) || (*endPtr != 0) || !LOS_IsUserAddress(vaddr)) {
155             PRINTK("vaddr %s invalid. should be in range(0x1000000~0x3e000000) \n", argv[1]);
156             return OS_ERROR;
157         } else {
158             if (pid >= 0) {
159                 if (pid < g_processMaxNum) {
160                     LosProcessCB *processCB = OS_PCB_FROM_PID(pid);
161                     if (!OsProcessIsUnused(processCB)) {
162                         paddr = 0;
163                         LOS_ArchMmuQuery(&processCB->vmSpace->archMmu, (VADDR_T)vaddr, &paddr, 0);
164                         if (paddr == 0) {
165                             PRINTK("vaddr %#x is not in range or mapped\n", vaddr);
166                         } else {
167                             PRINTK("vaddr %#x is paddr %#x\n", vaddr, paddr);
168                         }
169                     } else {
170                         PRINTK("\tThe process [%d] not active\n", pid);
171                     }
172                 } else {
173                     PRINTK("\tThe process [%d] not valid\n", pid);
174                 }
175             } else {
176                 PRINTK("%s: invalid option: %s %s\n", VMM_PMM_CMD, argv[0], argv[1]);
177             }
178         }
179     }
180 
181     return LOS_OK;
182 }
183 
OsShellCmdDumpPmm(VOID)184 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdDumpPmm(VOID)
185 {
186     OsVmPhysDump();
187 
188     PathCacheMemoryDump();
189     VnodeMemoryDump();
190     return OS_ERROR;
191 }
192 
OomPrintUsage(VOID)193 LITE_OS_SEC_TEXT_MINOR VOID OomPrintUsage(VOID)
194 {
195     PRINTK("\t-i [interval],     set oom check interval (ms)\n"
196            "\t-m [mem byte],     set oom low memory threshold (Byte)\n"
197            "\t-r [mem byte],     set page cache reclaim memory threshold (Byte)\n"
198            "\t-h | --help,       print vmm command usage\n");
199 }
200 
OsShellCmdOom(INT32 argc,const CHAR * argv[])201 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdOom(INT32 argc, const CHAR *argv[])
202 {
203     UINT32 lowMemThreshold;
204     UINT32 reclaimMemThreshold;
205     UINT32 checkInterval;
206     CHAR *endPtr = NULL;
207 
208     if (argc == ARGC_0) {
209         OomInfodump();
210     } else if (argc == ARGC_1) {
211         if (strcmp(argv[0], "-h") != 0 && strcmp(argv[0], "--help") != 0) {
212             PRINTK("%s: invalid option: %s\n", OOM_CMD, argv[0]);
213         }
214         OomPrintUsage();
215     } else if (argc == ARGC_2) {
216         if (strcmp(argv[0], "-m") == 0) {
217             lowMemThreshold = strtoul((CHAR *)argv[1], &endPtr, 0);
218             if ((endPtr == NULL) || (*endPtr != 0)) {
219                 PRINTK("[oom] low mem threshold %s(byte) invalid.\n", argv[1]);
220                 return OS_ERROR;
221             } else {
222                 OomSetLowMemThreashold(lowMemThreshold);
223             }
224         } else if (strcmp(argv[0], "-i") == 0) {
225             checkInterval = strtoul((CHAR *)argv[1], &endPtr, 0);
226             if ((endPtr == NULL) || (*endPtr != 0)) {
227                 PRINTK("[oom] check interval %s(us) invalid.\n", argv[1]);
228                 return OS_ERROR;
229             } else {
230                 OomSetCheckInterval(checkInterval);
231             }
232         } else if (strcmp(argv[0], "-r") == 0) {
233             reclaimMemThreshold = strtoul((CHAR *)argv[1], &endPtr, 0);
234             if ((endPtr == NULL) || (*endPtr != 0)) {
235                 PRINTK("[oom] reclaim mem threshold %s(byte) invalid.\n", argv[1]);
236                 return OS_ERROR;
237             } else {
238                 OomSetReclaimMemThreashold(reclaimMemThreshold);
239             }
240         } else {
241             PRINTK("%s: invalid option: %s %s\n", OOM_CMD, argv[0], argv[1]);
242             OomPrintUsage();
243         }
244     } else {
245         PRINTK("%s: invalid option\n", OOM_CMD);
246         OomPrintUsage();
247     }
248 
249     return OS_ERROR;
250 }
251 
252 #ifdef LOSCFG_SHELL_CMD_DEBUG
253 SHELLCMD_ENTRY(oom_shellcmd, CMD_TYPE_SHOW, OOM_CMD, 2, (CmdCallBackFunc)OsShellCmdOom);
254 SHELLCMD_ENTRY(vm_shellcmd, CMD_TYPE_SHOW, VMM_CMD, 1, (CmdCallBackFunc)OsShellCmdDumpVm);
255 SHELLCMD_ENTRY(v2p_shellcmd, CMD_TYPE_SHOW, VMM_PMM_CMD, 1, (CmdCallBackFunc)OsShellCmdV2P);
256 #endif
257 
258 #ifdef LOSCFG_SHELL
259 SHELLCMD_ENTRY(pmm_shellcmd, CMD_TYPE_SHOW, "pmm", 0, (CmdCallBackFunc)OsShellCmdDumpPmm);
260 #endif
261 #endif
262 
263