更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_vm_phys.h
浏览该文件的文档.
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#ifndef __LOS_VM_PHYS_H__
33#define __LOS_VM_PHYS_H__
34
35#include "los_typedef.h"
36#include "los_list.h"
37#include "los_spinlock.h"
38#include "los_vm_page.h"
39
40#ifdef __cplusplus
41#if __cplusplus
42extern "C" {
43#endif /* __cplusplus */
44#endif /* __cplusplus */
45
46/*!
47 * @brief
48 * @verbatim
49 LRU是Least Recently Used的缩写,即最近最少使用页面置换算法,是为虚拟页式存储管理服务的,
50 是根据页面调入内存后的使用情况进行决策了。由于无法预测各页面将来的使用情况,只能利用
51 “最近的过去”作为“最近的将来”的近似,因此,LRU算法就是将最近最久未使用的页面予以淘汰。
52 * @endverbatim
53 */
54#define VM_LIST_ORDER_MAX 9 ///< 伙伴算法分组数量,从 2^0,2^1,...,2^8 (256*4K)=1M
55#define VM_PHYS_SEG_MAX 32 ///< 最大支持32个段
56
57#ifndef min
58#define min(x, y) ((x) < (y) ? (x) : (y))
59#endif
60
61#define VM_PAGE_TO_PHYS(page) ((page)->physAddr) ///< 获取物理页框的物理基地址
62#define VM_ORDER_TO_PAGES(order) (1 << (order)) ///< 伙伴算法由order 定位到该块组的页面单位,例如:order=2时,page[4]
63#define VM_ORDER_TO_PHYS(order) (1 << (PAGE_SHIFT + (order))) ///< 通过order块组跳到物理地址
64#define VM_PHYS_TO_ORDER(phys) (min(LOS_LowBitGet((phys) >> PAGE_SHIFT), VM_LIST_ORDER_MAX - 1)) ///< 通过物理地址定位到order
65
66struct VmFreeList {
67 LOS_DL_LIST node; ///< 双循环链表用于挂空闲物理内框节点,通过 VmPage->node 挂上来
68 UINT32 listCnt; ///< 空闲物理页总数
69};
70
71/*!
72 * @brief Lru全称是Least Recently Used,即最近最久未使用的意思 针对匿名页和文件页各拆成对应链表。
73 */
74enum OsLruList {// 页属性
75 VM_LRU_INACTIVE_ANON = 0, ///< 非活动匿名页(swap)
76 VM_LRU_ACTIVE_ANON, ///< 活动匿名页(swap)
77 VM_LRU_INACTIVE_FILE, ///< 非活动文件页(磁盘)
78 VM_LRU_ACTIVE_FILE, ///< 活动文件页(磁盘)
79 VM_LRU_UNEVICTABLE, ///< 禁止换出的页
81};
82/*!
83 * @brief 物理段描述符
84 */
85typedef struct VmPhysSeg {
86 PADDR_T start; /* The start of physical memory area | 物理内存段的开始地址*/
87 size_t size; /* The size of physical memory area | 物理内存段的大小*/
88 LosVmPage *pageBase; /* The first page address of this area | 本段首个物理页框地址*/
89 SPIN_LOCK_S freeListLock; /* The buddy list spinlock | 伙伴算法自旋锁,用于操作freeList链表*/
90 struct VmFreeList freeList[VM_LIST_ORDER_MAX]; /* The free pages in the buddy list | 伙伴算法的分组,默认分成10组 2^0,2^1,...,2^VM_LIST_ORDER_MAX*/
91 SPIN_LOCK_S lruLock; ///< 用于置换的自旋锁,用于操作lruList
92 size_t lruSize[VM_NR_LRU_LISTS]; ///< 5个双循环链表大小,如此方便得到size
93 LOS_DL_LIST lruList[VM_NR_LRU_LISTS]; ///< 页面置换算法,5个双循环链表头,它们分别描述五中不同类型的链表
95/*!
96 * @brief 物理区描述,仅用于方案商配置范围使用
97 */
98struct VmPhysArea {
99 PADDR_T start; ///< 物理内存区基地址
100 size_t size; ///< 物理内存总大小
101};
102
103extern struct VmPhysSeg g_vmPhysSeg[VM_PHYS_SEG_MAX]; ///< 物理内存采用段页式管理,先切段后伙伴算法页
104extern INT32 g_vmPhysSegNum; ///< 段总数
105
106UINT32 OsVmPagesToOrder(size_t nPages);
107struct VmPhysSeg *OsVmPhysSegGet(LosVmPage *page);
109VOID *OsVmPageToVaddr(LosVmPage *page);
110VOID OsVmPhysSegAdd(VOID);
111VOID OsVmPhysInit(VOID);
112VOID OsVmPhysAreaSizeAdjust(size_t size);
114LosVmPage *OsVmVaddrToPage(VOID *ptr);
115VOID OsPhysSharePageCopy(PADDR_T oldPaddr, PADDR_T *newPaddr, LosVmPage *newPage);
116VOID OsVmPhysPagesFreeContiguous(LosVmPage *page, size_t nPages);
119
121VOID LOS_PhysPageFree(LosVmPage *page);
122size_t LOS_PhysPagesAlloc(size_t nPages, LOS_DL_LIST *list);
123size_t LOS_PhysPagesFree(LOS_DL_LIST *list);
124VOID *LOS_PhysPagesAllocContiguous(size_t nPages);
125VOID LOS_PhysPagesFreeContiguous(VOID *ptr, size_t nPages);
128
129#ifdef __cplusplus
130#if __cplusplus
131}
132#endif /* __cplusplus */
133#endif /* __cplusplus */
134
135#endif /* __LOS_VM_PHYS_H__ */
双向链表由内联函数实现 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-apx-dll....
unsigned long PADDR_T
Definition: los_typedef.h:207
signed int INT32
Definition: los_typedef.h:60
unsigned long paddr_t
Definition: los_typedef.h:209
unsigned long VADDR_T
Definition: los_typedef.h:208
unsigned char UINT8
Definition: los_typedef.h:55
unsigned int UINT32
Definition: los_typedef.h:57
VOID * LOS_PhysPagesAllocContiguous(size_t nPages)
分配连续的物理页
Definition: los_vm_phys.c:478
VOID OsPhysSharePageCopy(PADDR_T oldPaddr, PADDR_T *newPaddr, LosVmPage *newPage)
拷贝共享页面
Definition: los_vm_phys.c:602
struct VmPhysSeg LosVmPhysSeg
物理段描述符
INT32 g_vmPhysSegNum
段总数
Definition: los_vm_phys.c:88
LosVmPage * OsVmPaddrToPage(paddr_t paddr)
Definition: los_vm_phys.c:267
OsLruList
Lru全称是Least Recently Used,即最近最久未使用的意思 针对匿名页和文件页各拆成对应链表。
Definition: los_vm_phys.h:74
@ VM_LRU_UNEVICTABLE
禁止换出的页
Definition: los_vm_phys.h:79
@ VM_LRU_ACTIVE_ANON
活动匿名页(swap)
Definition: los_vm_phys.h:76
@ VM_NR_LRU_LISTS
Definition: los_vm_phys.h:80
@ VM_LRU_ACTIVE_FILE
活动文件页(磁盘)
Definition: los_vm_phys.h:78
@ VM_LRU_INACTIVE_FILE
非活动文件页(磁盘)
Definition: los_vm_phys.h:77
@ VM_LRU_INACTIVE_ANON
非活动匿名页(swap)
Definition: los_vm_phys.h:75
VOID LOS_PhysPageFree(LosVmPage *page)
释放一个物理页框
Definition: los_vm_phys.c:546
VOID OsVmPhysPagesFreeContiguous(LosVmPage *page, size_t nPages)
连续的释放物理页框, 如果8页连在一块是一起释放的
Definition: los_vm_phys.c:420
VOID LOS_PhysPagesFreeContiguous(VOID *ptr, size_t nPages)
释放指定页数地址连续的物理内存
Definition: los_vm_phys.c:494
size_t LOS_PhysPagesAlloc(size_t nPages, LOS_DL_LIST *list)
LOS_PhysPagesAlloc 分配nPages页个物理页框,并将页框挂入list 返回已分配的页面大小,不负责一定能分配到nPages的页框
Definition: los_vm_phys.c:581
struct VmPhysSeg g_vmPhysSeg[VM_PHYS_SEG_MAX]
物理内存采用段页式管理,先切段后伙伴算法页
Definition: los_vm_phys.c:87
PADDR_T OsKVaddrToPaddr(VADDR_T kvaddr)
Definition: los_vm_phys.c:519
struct VmPhysSeg * OsVmPhysSegGet(LosVmPage *page)
获取物理页框所在段
Definition: los_vm_phys.c:643
LosVmPage * OsVmPhysToPage(paddr_t pa, UINT8 segID)
通过物理地址获取所属参数段的物理页框
Definition: los_vm_phys.c:250
LosVmPage * LOS_PhysPageAlloc(VOID)
申请一个物理页
Definition: los_vm_phys.c:566
UINT32 OsVmPhysPageNumGet(VOID)
获得物理内存的总页数
Definition: los_vm_phys.c:152
LosVmPhysSeg * OsGVmPhysSegGet(VOID)
UINT32 OsVmPagesToOrder(size_t nPages)
获取参数nPages对应的块组,例如 7 -> 2^3 返回 3
Definition: los_vm_phys.c:652
VOID OsVmPhysSegAdd(VOID)
添加物理段
Definition: los_vm_phys.c:127
VOID OsVmPhysAreaSizeAdjust(size_t size)
段区域大小调整
Definition: los_vm_phys.c:141
size_t LOS_PhysPagesFree(LOS_DL_LIST *list)
释放双链表中的所有节点内存,本质是回归到伙伴orderlist中
Definition: los_vm_phys.c:661
LosVmPage * OsVmVaddrToPage(VOID *ptr)
通过虚拟地址找映射的物理页框
Definition: los_vm_phys.c:295
VOID OsVmPhysInit(VOID)
物理段初始化
Definition: los_vm_phys.c:181
VOID * OsVmPageToVaddr(LosVmPage *page)
通过page获取内核空间的虚拟地址 参考OsArchMmuInit #define SYS_MEM_BASE DDR_MEM_ADDR /* physical memory base 物理地址的起始...
Definition: los_vm_phys.c:288
VADDR_T * LOS_PaddrToKVaddr(PADDR_T paddr)
通过物理地址获取内核虚拟地址
Definition: los_vm_phys.c:688
LOS_DL_LIST node
双循环链表用于挂空闲物理内框节点,通过 VmPage->node 挂上来
Definition: los_vm_phys.h:67
UINT32 listCnt
空闲物理页总数
Definition: los_vm_phys.h:68
物理页框描述符 虚拟内存体现的是程序对内存资源的需求,而物理内存是对该请求的供应。 伙伴算法的思想是:把内存中连续的空闲页框空间看成是空闲页框块,并按照它们的大小(连续页框的数目)分组
Definition: los_vm_page.h:53
物理区描述,仅用于方案商配置范围使用
Definition: los_vm_phys.h:98
size_t size
物理内存总大小
Definition: los_vm_phys.h:100
PADDR_T start
物理内存区基地址
Definition: los_vm_phys.h:99
物理段描述符
Definition: los_vm_phys.h:85
LOS_DL_LIST lruList[VM_NR_LRU_LISTS]
页面置换算法,5个双循环链表头,它们分别描述五中不同类型的链表
Definition: los_vm_phys.h:93
SPIN_LOCK_S lruLock
用于置换的自旋锁,用于操作lruList
Definition: los_vm_phys.h:91
size_t lruSize[VM_NR_LRU_LISTS]
5个双循环链表大小,如此方便得到size
Definition: los_vm_phys.h:92
SPIN_LOCK_S freeListLock
Definition: los_vm_phys.h:89
LosVmPage * pageBase
Definition: los_vm_phys.h:88
struct VmFreeList freeList[VM_LIST_ORDER_MAX]
Definition: los_vm_phys.h:90
size_t size
Definition: los_vm_phys.h:87
PADDR_T start
Definition: los_vm_phys.h:86