更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_arch_mmu.h
浏览该文件的文档.
1/*!
2 * @file los_arch_mmu.h
3 * @brief
4 *
5 * \n
6 * @link https://blog.csdn.net/kuangyufei/article/details/109032636
7 * \n https://blog.csdn.net/qq_38410730/article/details/81036768
8 * @verbatim
9 https://blog.csdn.net/kuangyufei/article/details/109032636
10 https://blog.csdn.net/qq_38410730/article/details/81036768
11 虚拟内存空间中的地址叫做“虚拟地址”;而实际物理内存空间中的地址叫做“实际物理地址”或“物理地址”。处理器
12 运算器和应用程序设计人员看到的只是虚拟内存空间和虚拟地址,而处理器片外的地址总线看到的只是物理地址空间和物理地址。
13 MMU是 MemoryManagementUnit 的缩写即,内存管理单元.
14 MMU 的作用:
15 1. 将虚拟地址翻译成为物理地址,然后访问实际的物理地址
16 2. 访问权限控制
17 当处理器试图访问一个虚存页面时,首先到页表中去查询该页是否已映射到物理页框中,并记录在页表中。如果在,
18 则MMU会把页码转换成页框码,并加上虚拟地址提供的页内偏移量形成物理地址后去访问物理内存;如果不在,
19 则意味着该虚存页面还没有被载入内存,这时MMU就会通知操作系统:发生了一个页面访问错误(页面错误),
20 接下来系统会启动所谓的“请页”机制,即调用相应的系统操作函数,判断该虚拟地址是否为有效地址。
21
22 如果是有效的地址,就从虚拟内存中将该地址指向的页面读入到内存中的一个空闲页框中,并在页表中添加上
23 相对应的表项,最后处理器将从发生页面错误的地方重新开始运行;如果是无效的地址,则表明进程在试图访问
24 一个不存在的虚拟地址,此时操作系统将终止此次访问。
25 * @endverbatim
26 *
27 * \n
28 * @version
29 * @author weharmonyos.com | 鸿蒙研究站 | 每天死磕一点点
30 * @date 2021-11-16
31 *
32 * @history
33 *
34 */
35/*
36 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
37 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without modification,
40 * are permitted provided that the following conditions are met:
41 *
42 * 1. Redistributions of source code must retain the above copyright notice, this list of
43 * conditions and the following disclaimer.
44 *
45 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
46 * of conditions and the following disclaimer in the documentation and/or other materials
47 * provided with the distribution.
48 *
49 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
50 * to endorse or promote products derived from this software without specific prior written
51 * permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
55 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
60 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
61 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
62 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
63 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 */
65
66/**
67 * @defgroup los_arch_mmu architecture mmu
68 * @ingroup kernel
69 */
70
71#ifndef __LOS_ARCH_MMU_H__
72#define __LOS_ARCH_MMU_H__
73
74#include "los_typedef.h"
75#include "los_vm_phys.h"
76#ifndef LOSCFG_PAGE_TABLE_FINE_LOCK
77#include "los_spinlock.h"
78#endif
79
80#ifdef __cplusplus
81#if __cplusplus
82extern "C" {
83#endif /* __cplusplus */
84#endif /* __cplusplus */
85/// 内存管理单元(英语:memory management unit,缩写为MMU),有时称作分页内存管理单元(英语:paged memory management unit,缩写为PMMU)。
86typedef struct ArchMmu {//内存管理单元
87#ifndef LOSCFG_PAGE_TABLE_FINE_LOCK
88 SPIN_LOCK_S lock; /**< arch mmu page table entry modification spin lock */
89#endif
90 VADDR_T *virtTtb; /**< translation table base virtual addr | 注意:这里是个指针,内核操作都用这个地址*/
91 PADDR_T physTtb; /**< translation table base phys addr | 注意:这里是个值,这个值是记录给MMU使用的,MMU只认它,内核是无法使用的*/
92 UINT32 asid; /**< TLB asid | 标识进程用的,由mmu初始化阶段申请分配,有了它在mmu层面才知道是哪个进程的虚拟地址*/
93 LOS_DL_LIST ptList; /**< page table vm page list | L1 为表头,后面挂的是n多L2*/
95
96BOOL OsArchMmuInit(LosArchMmu *archMmu, VADDR_T *virtTtb);
97STATUS_T LOS_ArchMmuQuery(const LosArchMmu *archMmu, VADDR_T vaddr, PADDR_T *paddr, UINT32 *flags);
98STATUS_T LOS_ArchMmuUnmap(LosArchMmu *archMmu, VADDR_T vaddr, size_t count);
99STATUS_T LOS_ArchMmuMap(LosArchMmu *archMmu, VADDR_T vaddr, PADDR_T paddr, size_t count, UINT32 flags);
100STATUS_T LOS_ArchMmuChangeProt(LosArchMmu *archMmu, VADDR_T vaddr, size_t count, UINT32 flags);
101STATUS_T LOS_ArchMmuMove(LosArchMmu *archMmu, VADDR_T oldVaddr, VADDR_T newVaddr, size_t count, UINT32 flags);
104VOID OsArchMmuInitPerCPU(VOID);
106
107#ifdef __cplusplus
108#if __cplusplus
109}
110#endif /* __cplusplus */
111#endif /* __cplusplus */
112
113#endif /* __LOS_ARCH_MMU_H__ */
114
VOID LOS_ArchMmuContextSwitch(LosArchMmu *archMmu)
LOS_ArchMmuContextSwitch 切换MMU上下文
STATUS_T LOS_ArchMmuDestroy(LosArchMmu *archMmu)
LOS_ArchMmuDestroy 销毁MMU 和 initMmu 相呼应,释放页表页
STATUS_T LOS_ArchMmuMap(LosArchMmu *archMmu, VADDR_T vaddr, PADDR_T paddr, size_t count, UINT32 flags)
LOS_ArchMmuMap 映射进程空间虚拟地址区间与物理地址区间 所谓的map就是生成L1,L2页表项的过程
Definition: los_arch_mmu.c:891
VADDR_T * OsGFirstTableGet(VOID)
Definition: los_arch_mmu.c:186
struct ArchMmu LosArchMmu
内存管理单元(英语:memory management unit,缩写为MMU),有时称作分页内存管理单元(英语:paged memory management unit,缩写为PMMU)。
STATUS_T LOS_ArchMmuChangeProt(LosArchMmu *archMmu, VADDR_T vaddr, size_t count, UINT32 flags)
LOS_ArchMmuChangeProt 修改进程空间虚拟地址区间的映射属性 改变内存段的访问权限,例如: 读/写/可执行/不可用 ==
Definition: los_arch_mmu.c:949
STATUS_T LOS_ArchMmuMove(LosArchMmu *archMmu, VADDR_T oldVaddr, VADDR_T newVaddr, size_t count, UINT32 flags)
LOS_ArchMmuMove 将进程空间一个虚拟地址区间的映射关系转移至另一块未使用的虚拟地址区间重新做映射。
Definition: los_arch_mmu.c:996
BOOL OsArchMmuInit(LosArchMmu *archMmu, VADDR_T *virtTtb)
Definition: los_arch_mmu.c:537
STATUS_T LOS_ArchMmuUnmap(LosArchMmu *archMmu, VADDR_T vaddr, size_t count)
LOS_ArchMmuUnmap 解除进程空间虚拟地址区间与物理地址区间的映射关系
Definition: los_arch_mmu.c:619
VOID OsArchMmuInitPerCPU(VOID)
STATUS_T LOS_ArchMmuQuery(const LosArchMmu *archMmu, VADDR_T vaddr, PADDR_T *paddr, UINT32 *flags)
LOS_ArchMmuQuery 获取进程空间虚拟地址对应的物理地址以及映射属性。 本函数是内核高频函数,通过MMU查询虚拟地址是否映射过,带走映射的物理地址和权限
Definition: los_arch_mmu.c:569
unsigned long PADDR_T
Definition: los_typedef.h:207
unsigned long VADDR_T
Definition: los_typedef.h:208
int STATUS_T
Definition: los_typedef.h:215
unsigned int UINT32
Definition: los_typedef.h:57
size_t BOOL
Definition: los_typedef.h:88
内存管理单元(英语:memory management unit,缩写为MMU),有时称作分页内存管理单元(英语:paged memory management unit,缩写为PMMU)。
Definition: los_arch_mmu.h:86
SPIN_LOCK_S lock
Definition: los_arch_mmu.h:88
VADDR_T * virtTtb
Definition: los_arch_mmu.h:90
UINT32 asid
Definition: los_arch_mmu.h:92
PADDR_T physTtb
Definition: los_arch_mmu.h:91
LOS_DL_LIST ptList
Definition: los_arch_mmu.h:93