/* * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "los_vm_page.h" #include "los_vm_common.h" #include "los_vm_phys.h" #include "los_vm_boot.h" #include "los_vm_filemap.h" #ifdef __cplusplus #if __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ LosVmPage *g_vmPageArray = NULL; size_t g_vmPageArraySize; //虚拟页初始化 STATIC VOID OsVmPageInit(LosVmPage *page, paddr_t pa, UINT8 segID) { LOS_ListInit(&page->node); //页节点初始化 page->flags = FILE_PAGE_FREE; //映射文件初始标识 LOS_AtomicSet(&page->refCounts, 0); //引用次数0 page->physAddr = pa; //物理地址 page->segID = segID; //物理地址使用段管理,段ID page->order = VM_LIST_ORDER_MAX; //所属伙伴算法块组 } STATIC INLINE VOID OsVmPageOrderListInit(LosVmPage *page, size_t nPages) { OsVmPhysPagesFreeContiguous(page, nPages);//释放页面使可用于伙伴算法分配 } //* page初始化 VOID OsVmPageStartup(VOID) { struct VmPhysSeg *seg = NULL; LosVmPage *page = NULL; paddr_t pa; UINT32 nPage; INT32 segID; OsVmPhysAreaSizeAdjust(ROUNDUP((g_vmBootMemBase - KERNEL_ASPACE_BASE), PAGE_SIZE));//校正 g_physArea size nPage = OsVmPhysPageNumGet();//得到 g_physArea 总页数 g_vmPageArraySize = nPage * sizeof(LosVmPage);//页表总大小 g_vmPageArray = (LosVmPage *)OsVmBootMemAlloc(g_vmPageArraySize);//申请页表存放区域 OsVmPhysAreaSizeAdjust(ROUNDUP(g_vmPageArraySize, PAGE_SIZE));// g_physArea 变小 OsVmPhysSegAdd();// 段页绑定 OsVmPhysInit();// 加入空闲链表和设置置换算法,LRU(最近最久未使用)算法 for (segID = 0; segID < g_vmPhysSegNum; segID++) { seg = &g_vmPhysSeg[segID]; nPage = seg->size >> PAGE_SHIFT; for (page = seg->pageBase, pa = seg->start; page <= seg->pageBase + nPage; page++, pa += PAGE_SIZE) { OsVmPageInit(page, pa, segID);//page初始化 } OsVmPageOrderListInit(seg->pageBase, nPage);// 页面分配的排序 } } //通过物理地址获取页框 LosVmPage *LOS_VmPageGet(PADDR_T paddr) { INT32 segID; LosVmPage *page = NULL; for (segID = 0; segID < g_vmPhysSegNum; segID++) {//物理内存采用段页管理 page = OsVmPhysToPage(paddr, segID);//通过物理地址和段ID找出物理页框 if (page != NULL) { break; } } return page; } #ifdef __cplusplus #if __cplusplus } #endif /* __cplusplus */ #endif /* __cplusplus */