更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
vmm_proc.c
浏览该文件的文档.
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 <sys/statfs.h>
33#include <sys/mount.h>
34#include "proc_fs.h"
35
36#ifdef LOSCFG_SHELL_CMD_DEBUG
37#include "los_vm_map.h"
38#include "los_vm_dump.h"
39#include "los_vm_lock.h"
40#include "los_process_pri.h"
41
42#ifdef LOSCFG_KERNEL_VM
43//获取虚拟内存内核相关的信息,用这个方法去窥视内核是个很好的办法,精读本函数的过程是理解虚拟内存实现的过程
44STATIC VOID OsVmDumpSeqSpaces(struct SeqBuf *seqBuf)
45{
46 LosVmSpace *space = NULL;
47 LosVmMapRegion *region = NULL;
48 LosRbNode *pstRbNode = NULL;
49 LosRbNode *pstRbNodeNext = NULL;
50 UINT32 pssPages = 0;
51 UINT32 spacePages;
52 UINT32 regionPages;
53 LosProcessCB *pcb = NULL;
54 LOS_DL_LIST *aspaceList = LOS_GetVmSpaceList();
55 LosMux *aspaceListMux = OsGVmSpaceMuxGet();
56
57 (VOID)LOS_MuxAcquire(aspaceListMux);
58 LOS_DL_LIST_FOR_EACH_ENTRY(space, aspaceList, LosVmSpace, node) {
59 (VOID)LOS_MuxAcquire(&space->regionMux);
60 spacePages = OsCountAspacePages(space);
61 pcb = OsGetPIDByAspace(space);
62 if (pcb == NULL) {
63 (VOID)LOS_MuxRelease(&space->regionMux);
64 continue;
65 }
66 (VOID)LosBufPrintf(seqBuf, "\r\n PID aspace name base size pages \n");
67 (VOID)LosBufPrintf(seqBuf, " ---- ------ ---- ---- ----- ----\n");
68 (VOID)LosBufPrintf(seqBuf, " %-4d %#010x %-10.10s %#010x %#010x %d\n",
69 pcb->processID, space, pcb->processName, space->base, space->size, spacePages);
70 (VOID)LosBufPrintf(seqBuf,
71 "\r\n\t region name base size mmu_flags pages pg/ref\n");
72 (VOID)LosBufPrintf(seqBuf,
73 "\t ------ ---- ---- ---- --------- ----- -----\n");
74
75 RB_SCAN_SAFE(&space->regionRbTree, pstRbNode, pstRbNodeNext)
76 region = (LosVmMapRegion *)pstRbNode;
77 regionPages = OsCountRegionPages(space, region, &pssPages);
78 CHAR *flagsStr = OsArchFlagsToStr(region->regionFlags);
79 if (flagsStr == NULL) {
80 break;
81 }
82 (VOID)LosBufPrintf(seqBuf, "\t %#010x %-19.19s %#010x %#010x %-15.15s %4d %4d\n",
83 region, OsGetRegionNameOrFilePath(region), region->range.base,
84 region->range.size, flagsStr, regionPages, pssPages);
85 (VOID)LOS_MemFree(m_aucSysMem0, flagsStr);
86 (VOID)OsRegionOverlapCheck(space, region);
87 RB_SCAN_SAFE_END(&space->regionRbTree, pstRbNode, pstRbNodeNext)
88 (VOID)LOS_MuxRelease(&space->regionMux);
89 }
90 (VOID)LOS_MuxRelease(aspaceListMux);
91}
92/// .read 接口的现实
93static int VmmProcFill(struct SeqBuf *m, void *v)
94{
95 (void)v;
97
98 return 0;
99}
100///实现 操作proc file 接口,也可理解为驱动程序不同
101static const struct ProcFileOperations VMM_PROC_FOPS = {
102 .write = NULL,
103 .read = VmmProcFill,
104};
105//初始化 虚拟内存 内容信息
106void ProcVmmInit(void)
107{
108 struct ProcDirEntry *pde = CreateProcEntry("vmm", 0, NULL);//创建目录
109 if (pde == NULL) {
110 PRINT_ERR("create /proc/vmm error!\n");
111 return;
112 }
113
114 pde->procFileOps = &VMM_PROC_FOPS;//每个目录的驱动程序都不一样
115}
116#endif
117#endif
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
int LosBufPrintf(struct SeqBuf *seqBuf, const char *fmt,...)
支持可变参数 写 buf
Definition: los_seq_buf.c:133
unsigned int UINT32
Definition: los_typedef.h:57
char CHAR
Definition: los_typedef.h:63
LosProcessCB * OsGetPIDByAspace(LosVmSpace *space)
通过虚拟空间获取进程实体
Definition: los_vm_dump.c:256
UINT32 OsCountRegionPages(LosVmSpace *space, LosVmMapRegion *region, UINT32 *pssPages)
统计虚拟空间中某个线性区的页数
Definition: los_vm_dump.c:278
CHAR * OsArchFlagsToStr(const UINT32 archFlags)
Definition: los_vm_dump.c:326
const CHAR * OsGetRegionNameOrFilePath(LosVmMapRegion *region)
Definition: los_vm_dump.c:57
INT32 OsRegionOverlapCheck(LosVmSpace *space, LosVmMapRegion *region)
Definition: los_vm_dump.c:442
UINT32 OsCountAspacePages(LosVmSpace *space)
统计虚拟空间的总页数
Definition: los_vm_dump.c:312
STATIC INLINE STATUS_T LOS_MuxAcquire(LosMux *m)
Definition: los_vm_lock.h:48
STATIC INLINE STATUS_T LOS_MuxRelease(LosMux *m)
Definition: los_vm_lock.h:53
LosMux * OsGVmSpaceMuxGet(VOID)
LOS_DL_LIST * LOS_GetVmSpaceList(VOID)
获取进程空间链表指针 g_vmSpaceList中挂的是进程空间 g_kVmSpace, g_vMallocSpace,所有用户进程的空间(独有一个进程空间)
Definition: los_vm_map.c:135
struct ProcDirEntry * CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent)
create a proc node
Definition: proc_file.c:364
Definition: los_mux.h:73
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
const struct ProcFileOperations * procFileOps
驱动程序,每个 /proc 下目录的驱动程序都不一样
Definition: proc_fs.h:106
真正最后能操作pro file的接口,proc本质是个内存文件系统, vfs - > ProcFileOperations
Definition: proc_fs.h:89
ssize_t(* write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: proc_fs.h:91
UINT32 processID
CHAR processName[OS_PCB_NAME_LEN]
UINT32 size
Definition: los_vm_map.h:85
VADDR_T base
Definition: los_vm_map.h:84
UINT32 regionFlags
Definition: los_vm_map.h:125
LosVmMapRange range
Definition: los_vm_map.h:123
虚拟空间,每个进程都有一个属于自己的虚拟内存地址空间
Definition: los_vm_map.h:146
VADDR_T base
Definition: los_vm_map.h:150
LosRbTree regionRbTree
Definition: los_vm_map.h:148
LosMux regionMux
Definition: los_vm_map.h:149
UINT32 size
Definition: los_vm_map.h:151
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
STATIC VOID OsVmDumpSeqSpaces(struct SeqBuf *seqBuf)
Definition: vmm_proc.c:44
static int VmmProcFill(struct SeqBuf *m, void *v)
.read 接口的现实
Definition: vmm_proc.c:93
static const struct ProcFileOperations VMM_PROC_FOPS
实现 操作proc file 接口,也可理解为驱动程序不同
Definition: vmm_proc.c:101
void ProcVmmInit(void)
Definition: vmm_proc.c:106