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 /**
33 * @defgroup los_vm_filemap vm filemap definition
34 * @ingroup kernel
35 */
36
37 #ifndef __LOS_VM_FILEMAP_H__
38 #define __LOS_VM_FILEMAP_H__
39
40 #ifdef LOSCFG_FS_VFS
41 #include "fs/file.h"
42 #endif
43 #include "los_vm_map.h"
44 #include "los_vm_page.h"
45 #include "los_vm_common.h"
46 #include "los_vm_phys.h"
47
48 #ifdef __cplusplus
49 #if __cplusplus
50 extern "C" {
51 #endif /* __cplusplus */
52 #endif /* __cplusplus */
53
54 typedef struct FilePage {
55 LOS_DL_LIST node;
56 LOS_DL_LIST lru;
57 LOS_DL_LIST i_mmap; /* list of mappings */
58 UINT32 n_maps; /* num of mapping */
59 struct VmPhysSeg *physSeg; /* physical memory that file page belongs to */
60 struct VmPage *vmPage;
61 struct page_mapping *mapping;
62 VM_OFFSET_T pgoff;
63 UINT32 flags;
64 UINT16 dirtyOff;
65 UINT16 dirtyEnd;
66 } LosFilePage;
67
68 typedef struct MapInfo {
69 LOS_DL_LIST node;
70 VADDR_T vaddr;
71 LosFilePage *page;
72 LosArchMmu *archMmu;
73 } LosMapInfo;
74
75 enum OsPageFlags {
76 FILE_PAGE_FREE,
77 FILE_PAGE_LOCKED,
78 FILE_PAGE_REFERENCED,
79 FILE_PAGE_DIRTY,
80 FILE_PAGE_LRU,
81 FILE_PAGE_ACTIVE,
82 FILE_PAGE_SHARED,
83 };
84
85 #define PGOFF_MAX 2000
86 #define MAX_SHRINK_PAGECACHE_TRY 2
87 #define VM_FILEMAP_MAX_SCAN (SYS_MEM_SIZE_DEFAULT >> PAGE_SHIFT)
88 #define VM_FILEMAP_MIN_SCAN 32
89
OsSetPageLocked(LosVmPage * page)90 STATIC INLINE VOID OsSetPageLocked(LosVmPage *page)
91 {
92 LOS_BitmapSet(&page->flags, FILE_PAGE_LOCKED);
93 }
94
OsCleanPageLocked(LosVmPage * page)95 STATIC INLINE VOID OsCleanPageLocked(LosVmPage *page)
96 {
97 LOS_BitmapClr(&page->flags, FILE_PAGE_LOCKED);
98 }
99
OsSetPageDirty(LosVmPage * page)100 STATIC INLINE VOID OsSetPageDirty(LosVmPage *page)
101 {
102 LOS_BitmapSet(&page->flags, FILE_PAGE_DIRTY);
103 }
104
OsCleanPageDirty(LosVmPage * page)105 STATIC INLINE VOID OsCleanPageDirty(LosVmPage *page)
106 {
107 LOS_BitmapClr(&page->flags, FILE_PAGE_DIRTY);
108 }
109
OsSetPageActive(LosVmPage * page)110 STATIC INLINE VOID OsSetPageActive(LosVmPage *page)
111 {
112 LOS_BitmapSet(&page->flags, FILE_PAGE_ACTIVE);
113 }
114
OsCleanPageActive(LosVmPage * page)115 STATIC INLINE VOID OsCleanPageActive(LosVmPage *page)
116 {
117 LOS_BitmapClr(&page->flags, FILE_PAGE_ACTIVE);
118 }
119
OsSetPageLRU(LosVmPage * page)120 STATIC INLINE VOID OsSetPageLRU(LosVmPage *page)
121 {
122 LOS_BitmapSet(&page->flags, FILE_PAGE_LRU);
123 }
124
OsSetPageFree(LosVmPage * page)125 STATIC INLINE VOID OsSetPageFree(LosVmPage *page)
126 {
127 LOS_BitmapSet(&page->flags, FILE_PAGE_FREE);
128 }
129
OsCleanPageFree(LosVmPage * page)130 STATIC INLINE VOID OsCleanPageFree(LosVmPage *page)
131 {
132 LOS_BitmapClr(&page->flags, FILE_PAGE_FREE);
133 }
134
OsSetPageReferenced(LosVmPage * page)135 STATIC INLINE VOID OsSetPageReferenced(LosVmPage *page)
136 {
137 LOS_BitmapSet(&page->flags, FILE_PAGE_REFERENCED);
138 }
139
OsCleanPageReferenced(LosVmPage * page)140 STATIC INLINE VOID OsCleanPageReferenced(LosVmPage *page)
141 {
142 LOS_BitmapClr(&page->flags, FILE_PAGE_REFERENCED);
143 }
144
OsIsPageActive(LosVmPage * page)145 STATIC INLINE BOOL OsIsPageActive(LosVmPage *page)
146 {
147 return BIT_GET(page->flags, FILE_PAGE_ACTIVE);
148 }
149
OsIsPageLocked(LosVmPage * page)150 STATIC INLINE BOOL OsIsPageLocked(LosVmPage *page)
151 {
152 return BIT_GET(page->flags, FILE_PAGE_LOCKED);
153 }
154
OsIsPageReferenced(LosVmPage * page)155 STATIC INLINE BOOL OsIsPageReferenced(LosVmPage *page)
156 {
157 return BIT_GET(page->flags, FILE_PAGE_REFERENCED);
158 }
159
OsIsPageDirty(LosVmPage * page)160 STATIC INLINE BOOL OsIsPageDirty(LosVmPage *page)
161 {
162 return BIT_GET(page->flags, FILE_PAGE_DIRTY);
163 }
164
OsIsPageMapped(LosFilePage * page)165 STATIC INLINE BOOL OsIsPageMapped(LosFilePage *page)
166 {
167 return (page->n_maps != 0);
168 }
169
170 /* The follow three functions is used to SHM module */
OsSetPageShared(LosVmPage * page)171 STATIC INLINE VOID OsSetPageShared(LosVmPage *page)
172 {
173 LOS_BitmapSet(&page->flags, FILE_PAGE_SHARED);
174 }
175
OsCleanPageShared(LosVmPage * page)176 STATIC INLINE VOID OsCleanPageShared(LosVmPage *page)
177 {
178 LOS_BitmapClr(&page->flags, FILE_PAGE_SHARED);
179 }
180
OsIsPageShared(LosVmPage * page)181 STATIC INLINE BOOL OsIsPageShared(LosVmPage *page)
182 {
183 return BIT_GET(page->flags, FILE_PAGE_SHARED);
184 }
185
186 typedef struct ProcessCB LosProcessCB;
187
188 #ifdef LOSCFG_FS_VFS
189 INT32 OsVfsFileMmap(struct file *filep, LosVmMapRegion *region);
190 STATUS_T OsNamedMMap(struct file *filep, LosVmMapRegion *region);
191 VOID OsVmmFileRegionFree(struct file *filep, LosProcessCB *processCB);
192 #endif
193
194 LosFilePage *OsPageCacheAlloc(struct page_mapping *mapping, VM_OFFSET_T pgoff);
195 LosFilePage *OsFindGetEntry(struct page_mapping *mapping, VM_OFFSET_T pgoff);
196 LosMapInfo *OsGetMapInfo(const LosFilePage *page, const LosArchMmu *archMmu, VADDR_T vaddr);
197 VOID OsAddMapInfo(LosFilePage *page, LosArchMmu *archMmu, VADDR_T vaddr);
198 VOID OsDelMapInfo(LosVmMapRegion *region, LosVmPgFault *pgFault, BOOL cleanDirty);
199 VOID OsFileCacheFlush(struct page_mapping *mapping);
200 VOID OsFileCacheRemove(struct page_mapping *mapping);
201 VOID OsUnmapPageLocked(LosFilePage *page, LosMapInfo *info);
202 VOID OsUnmapAllLocked(LosFilePage *page);
203 VOID OsLruCacheAdd(LosFilePage *fpage, enum OsLruList lruType);
204 VOID OsLruCacheDel(LosFilePage *fpage);
205 LosFilePage *OsDumpDirtyPage(LosFilePage *oldPage);
206 VOID OsDoFlushDirtyPage(LosFilePage *fpage);
207 VOID OsDeletePageCacheLru(LosFilePage *page);
208 VOID OsPageRefDecNoLock(LosFilePage *page);
209 VOID OsPageRefIncLocked(LosFilePage *page);
210 int OsTryShrinkMemory(size_t nPage);
211 VOID OsMarkPageDirty(LosFilePage *fpage, const LosVmMapRegion *region, int off, int len);
212
213 #ifdef LOSCFG_DEBUG_VERSION
214 VOID ResetPageCacheHitInfo(int *try, int *hit);
215 struct file_map* GetFileMappingList(void);
216 #endif
217 #ifdef __cplusplus
218 #if __cplusplus
219 }
220 #endif /* __cplusplus */
221 #endif /* __cplusplus */
222
223 #endif /* __LOS_VM_FILEMAP_H__ */
224
225