更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_vm_syscall.c 文件参考

浏览源代码.

函数

STATUS_T OsCheckMMapParams (VADDR_T *vaddr, unsigned long flags, size_t len, unsigned long pgoff)
 
STATUS_T OsNamedMmapingPermCheck (struct file *filep, unsigned long flags, unsigned prot)
 
STATUS_T OsAnonMMap (LosVmMapRegion *region)
 匿名映射 更多...
 
VADDR_T LOS_MMap (VADDR_T vaddr, size_t len, unsigned prot, unsigned long flags, int fd, unsigned long pgoff)
 
STATUS_T LOS_UnMMap (VADDR_T addr, size_t size)
 解除映射关系 更多...
 
STATIC INLINE BOOL OsProtMprotectPermCheck (unsigned long prot, LosVmMapRegion *region)
 
VOID * OsShrinkHeap (VOID *addr, LosVmSpace *space)
 收缩堆区 更多...
 
VOID * LOS_DoBrk (VOID *addr)
 
STATIC UINT32 OsInheritOldRegionName (UINT32 oldRegionFlags)
 继承老线性区的标签 更多...
 
INT32 LOS_DoMprotect (VADDR_T vaddr, size_t len, unsigned long prot)
 修改内存段的访问权限 更多...
 
STATUS_T OsMremapCheck (VADDR_T addr, size_t oldLen, VADDR_T newAddr, size_t newLen, unsigned int flags)
 
VADDR_T LOS_DoMremap (VADDR_T oldAddress, size_t oldSize, size_t newSize, int flags, VADDR_T newAddr)
 重新映射虚拟内存地址。 更多...
 
VOID LOS_DumpMemRegion (VADDR_T vaddr)
 输出内存线性区 更多...
 

函数说明

◆ LOS_DoBrk()

VOID * LOS_DoBrk ( VOID *  addr)
 用户进程向内核申请空间,进一步说用于扩展用户堆栈空间,或者回收用户堆栈空间
 扩展当前进程的堆空间
 一个进程所有的线性区都在进程指定的线性地址范围内,
 线性区之间是不会有地址的重叠的,开始都是连续的,随着进程的运行出现了释放再分配的情况
 由此出现了断断续续的线性区,内核回收线性区时会检测是否和周边的线性区可合并成一个更大
 的线性区用于分配。
参数
addr
返回
VOID*

在文件 los_vm_syscall.c259 行定义.

260{
262 size_t size;
263 VOID *ret = NULL;
264 LosVmMapRegion *region = NULL;
265 VOID *alignAddr = NULL;
266 VOID *shrinkAddr = NULL;
267
268 if (addr == NULL) {//参数地址未传情况
269 return (void *)(UINTPTR)space->heapNow;//以现有指向地址为基础进行扩展
270 }
271
272 if ((UINTPTR)addr < (UINTPTR)space->heapBase) {//heapBase是堆区的开始地址,所以参数地址不能低于它
273 return (VOID *)-ENOMEM;
274 }
275
276 size = (UINTPTR)addr - (UINTPTR)space->heapBase;//算出大小
277 size = ROUNDUP(size, PAGE_SIZE); //圆整size
278 alignAddr = (CHAR *)(UINTPTR)(space->heapBase) + size;//得到新的线性区的结束地址
279 PRINT_INFO("brk addr %p , size 0x%x, alignAddr %p, align %d\n", addr, size, alignAddr, PAGE_SIZE);
280
281 (VOID)LOS_MuxAcquire(&space->regionMux);
282 if (addr < (VOID *)(UINTPTR)space->heapNow) {//如果地址小于堆区现地址
283 shrinkAddr = OsShrinkHeap(addr, space);//收缩堆区
284 (VOID)LOS_MuxRelease(&space->regionMux);
285 return shrinkAddr;
286 }
287
288 if ((UINTPTR)alignAddr >= space->mapBase) {//参数地址 大于映射区地址
289 VM_ERR("Process heap memory space is insufficient");//进程堆空间不足
290 ret = (VOID *)-ENOMEM;
291 goto REGION_ALLOC_FAILED;
292 }
293
294 if (space->heapBase == space->heapNow) {//往往是第一次调用本函数才会出现,因为初始化时 heapBase = heapNow
295 region = LOS_RegionAlloc(space, space->heapBase, size,//分配一个可读/可写/可使用的线性区,只需分配一次
296 VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE |//线性区的大小由range.size决定
297 VM_MAP_REGION_FLAG_FIXED | VM_MAP_REGION_FLAG_PERM_USER, 0);
298 if (region == NULL) {
299 ret = (VOID *)-ENOMEM;
300 VM_ERR("LOS_RegionAlloc failed");
301 goto REGION_ALLOC_FAILED;
302 }
303 region->regionFlags |= VM_MAP_REGION_FLAG_HEAP;//贴上线性区类型为堆区的标签,注意一个线性区可以有多种标签
304 space->heap = region;//指定线性区为堆区
305 }
306
307 space->heapNow = (VADDR_T)(UINTPTR)alignAddr;//更新堆区顶部位置
308 space->heap->range.size = size; //更新堆区大小,经此操作线性区变大或缩小了
309 ret = (VOID *)(UINTPTR)space->heapNow;//返回堆顶
310
311REGION_ALLOC_FAILED:
312 (VOID)LOS_MuxRelease(&space->regionMux);
313 return ret;
314}
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
unsigned long VADDR_T
Definition: los_typedef.h:208
unsigned long UINTPTR
Definition: los_typedef.h:68
char CHAR
Definition: los_typedef.h:63
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
LosVmMapRegion * LOS_RegionAlloc(LosVmSpace *vmSpace, VADDR_T vaddr, size_t len, UINT32 regionFlags, VM_OFFSET_T pgoff)
Definition: los_vm_map.c:581
VOID * OsShrinkHeap(VOID *addr, LosVmSpace *space)
收缩堆区
LosVmSpace * vmSpace
UINT32 size
Definition: los_vm_map.h:85
UINT32 regionFlags
Definition: los_vm_map.h:125
LosVmMapRange range
Definition: los_vm_map.h:123
虚拟空间,每个进程都有一个属于自己的虚拟内存地址空间
Definition: los_vm_map.h:146
LosVmMapRegion * heap
Definition: los_vm_map.h:154
VADDR_T mapBase
Definition: los_vm_map.h:155
VADDR_T heapBase
Definition: los_vm_map.h:152
LosMux regionMux
Definition: los_vm_map.h:149
VADDR_T heapNow
Definition: los_vm_map.h:153
函数调用图:
这是这个函数的调用关系图:

◆ LOS_DoMprotect()

INT32 LOS_DoMprotect ( VADDR_T  vaddr,
size_t  len,
unsigned long  prot 
)

修改内存段的访问权限

在文件 los_vm_syscall.c337 行定义.

338{
340 LosVmMapRegion *region = NULL;
341 UINT32 vmFlags;
342 UINT32 count;
343 int ret;
344
345 (VOID)LOS_MuxAcquire(&space->regionMux);
346 region = LOS_RegionFind(space, vaddr);//通过虚拟地址找到线性区
347 if (!IS_ALIGNED(vaddr, PAGE_SIZE) || (region == NULL) || (vaddr > vaddr + len)) {
348 ret = -EINVAL;
349 goto OUT_MPROTECT;
350 }
351
352 if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))) {
353 ret = -EINVAL;
354 goto OUT_MPROTECT;
355 }
356 //如果是堆区或VDSO区,说明区内容是不能修改的
357 if ((region->regionFlags & VM_MAP_REGION_FLAG_VDSO) || (region->regionFlags & VM_MAP_REGION_FLAG_HEAP)) {
358 ret = -EPERM;
359 goto OUT_MPROTECT;
360 }
361 //如果是共享文件,说明内容也不能修改
362 if (LOS_IsRegionTypeFile(region) && (region->regionFlags & VM_MAP_REGION_FLAG_SHARED)) {
363 if (!OsProtMprotectPermCheck(prot, region)) {
364 ret = -EACCES;
365 goto OUT_MPROTECT;
366 }
367 }
368 len = LOS_Align(len, PAGE_SIZE);
369 /* can't operation cross region */
370 if ((region->range.base + region->range.size) < (vaddr + len)) {
371 ret = -EINVAL;
372 goto OUT_MPROTECT;
373 }
374
375 /* if only move some part of region, we need to split first */
376 if (region->range.size > len) {//如果只修改部分区域,我们需要先拆分区
377 OsVmRegionAdjust(space, vaddr, len);//调整下线性区范围
378 }
379
380 vmFlags = OsCvtProtFlagsToRegionFlags(prot, 0);//转换FLAGS
381 vmFlags |= (region->regionFlags & VM_MAP_REGION_FLAG_SHARED) ? VM_MAP_REGION_FLAG_SHARED : 0;
382 vmFlags |= OsInheritOldRegionName(region->regionFlags);
383 region = LOS_RegionFind(space, vaddr);
384 if (region == NULL) {
385 ret = -ENOMEM;
386 goto OUT_MPROTECT;
387 }
388 region->regionFlags = vmFlags;
389 count = len >> PAGE_SHIFT;
390 ret = LOS_ArchMmuChangeProt(&space->archMmu, vaddr, count, region->regionFlags);//修改访问权限实体函数
391 if (ret) {
392 ret = -ENOMEM;
393 goto OUT_MPROTECT;
394 }
395 ret = LOS_OK;
396
397OUT_MPROTECT:
398#ifdef LOSCFG_VM_OVERLAP_CHECK
399 if (VmmAspaceRegionsOverlapCheck(aspace) < 0) {
400 (VOID)OsShellCmdDumpVm(0, NULL);
401 ret = -ENOMEM;
402 }
403#endif
404
405 (VOID)LOS_MuxRelease(&space->regionMux);
406 return ret;
407}
LITE_OS_SEC_TEXT UINTPTR LOS_Align(UINTPTR addr, UINT32 boundary)
Align the value (addr) by some bytes (boundary) you specify.
Definition: los_misc.c:35
STATUS_T LOS_ArchMmuChangeProt(LosArchMmu *archMmu, VADDR_T vaddr, size_t count, UINT32 flags)
LOS_ArchMmuChangeProt 修改进程空间虚拟地址区间的映射属性 改变内存段的访问权限,例如: 读/写/可执行/不可用 ==
Definition: los_arch_mmu.c:949
unsigned int UINT32
Definition: los_typedef.h:57
STATIC INLINE BOOL LOS_IsRegionTypeFile(LosVmMapRegion *region)
是否为文件映射区
Definition: los_vm_map.h:234
STATIC INLINE UINT32 OsCvtProtFlagsToRegionFlags(unsigned long prot, unsigned long flags)
从外部权限标签转化为线性区权限标签
Definition: los_vm_map.h:197
LosVmMapRegion * LOS_RegionFind(LosVmSpace *vmSpace, VADDR_T addr)
查找线性区 根据起始地址在进程空间内查找是否存在
Definition: los_vm_map.c:414
STATUS_T OsVmRegionAdjust(LosVmSpace *space, VADDR_T vaddr, size_t size)
STATIC UINT32 OsInheritOldRegionName(UINT32 oldRegionFlags)
继承老线性区的标签
STATIC INLINE BOOL OsProtMprotectPermCheck(unsigned long prot, LosVmMapRegion *region)
VADDR_T base
Definition: los_vm_map.h:84
LosArchMmu archMmu
Definition: los_vm_map.h:157
LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdDumpVm(INT32 argc, const CHAR *argv[])
查看进程的虚拟内存使用情况。vmm [-a / -h / –help], vmm [pid]
Definition: vm_shellcmd.c:108
函数调用图:
这是这个函数的调用关系图:

◆ LOS_DoMremap()

VADDR_T LOS_DoMremap ( VADDR_T  oldAddress,
size_t  oldSize,
size_t  newSize,
int  flags,
VADDR_T  newAddr 
)

重新映射虚拟内存地址。

在文件 los_vm_syscall.c460 行定义.

461{
462 LosVmMapRegion *regionOld = NULL;
463 LosVmMapRegion *regionNew = NULL;
464 STATUS_T status;
465 VADDR_T ret;
467
468 oldSize = LOS_Align(oldSize, PAGE_SIZE);
469 newSize = LOS_Align(newSize, PAGE_SIZE);
470
471 (VOID)LOS_MuxAcquire(&space->regionMux);
472
473 status = OsMremapCheck(oldAddress, oldSize, newAddr, newSize, (unsigned int)flags);
474 if (status) {
475 ret = status;
476 goto OUT_MREMAP;
477 }
478
479 /* if only move some part of region, we need to split first */
480 status = OsVmRegionAdjust(space, oldAddress, oldSize);
481 if (status) {
482 ret = -ENOMEM;
483 goto OUT_MREMAP;
484 }
485
486 regionOld = LOS_RegionFind(space, oldAddress);
487 if (regionOld == NULL) {
488 ret = -ENOMEM;
489 goto OUT_MREMAP;
490 }
491
492 if ((unsigned int)flags & MREMAP_FIXED) {
493 regionNew = OsVmRegionDup(space, regionOld, newAddr, newSize);
494 if (!regionNew) {
495 ret = -ENOMEM;
496 goto OUT_MREMAP;
497 }
498 status = LOS_ArchMmuMove(&space->archMmu, oldAddress, newAddr,
499 ((newSize < regionOld->range.size) ? newSize : regionOld->range.size) >> PAGE_SHIFT,
500 regionOld->regionFlags);
501 if (status) {
502 LOS_RegionFree(space, regionNew);
503 ret = -ENOMEM;
504 goto OUT_MREMAP;
505 }
506 LOS_RegionFree(space, regionOld);
507 ret = newAddr;
508 goto OUT_MREMAP;
509 }
510 // take it as shrink operation
511 if (oldSize > newSize) {
512 LOS_UnMMap(oldAddress + newSize, oldSize - newSize);
513 ret = oldAddress;
514 goto OUT_MREMAP;
515 }
516 status = OsIsRegionCanExpand(space, regionOld, newSize);
517 // we can expand directly.
518 if (!status) {
519 regionOld->range.size = newSize;
520 ret = oldAddress;
521 goto OUT_MREMAP;
522 }
523
524 if ((unsigned int)flags & MREMAP_MAYMOVE) {
525 regionNew = OsVmRegionDup(space, regionOld, 0, newSize);
526 if (regionNew == NULL) {
527 ret = -ENOMEM;
528 goto OUT_MREMAP;
529 }
530 status = LOS_ArchMmuMove(&space->archMmu, oldAddress, regionNew->range.base,
531 regionOld->range.size >> PAGE_SHIFT, regionOld->regionFlags);
532 if (status) {
533 LOS_RegionFree(space, regionNew);
534 ret = -ENOMEM;
535 goto OUT_MREMAP;
536 }
537 LOS_RegionFree(space, regionOld);
538 ret = regionNew->range.base;
539 goto OUT_MREMAP;
540 }
541
542 ret = -EINVAL;
543OUT_MREMAP:
544#ifdef LOSCFG_VM_OVERLAP_CHECK
545 if (VmmAspaceRegionsOverlapCheck(aspace) < 0) {
546 (VOID)OsShellCmdDumpVm(0, NULL);
547 ret = -ENOMEM;
548 }
549#endif
550
551 (VOID)LOS_MuxRelease(&space->regionMux);
552 return ret;
553}
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
int STATUS_T
Definition: los_typedef.h:215
LosVmMapRegion * OsVmRegionDup(LosVmSpace *space, LosVmMapRegion *oldRegion, VADDR_T vaddr, size_t size)
STATUS_T OsIsRegionCanExpand(LosVmSpace *space, LosVmMapRegion *region, size_t size)
STATUS_T LOS_RegionFree(LosVmSpace *space, LosVmMapRegion *region)
释放进程空间指定线性区
Definition: los_vm_map.c:694
STATUS_T OsMremapCheck(VADDR_T addr, size_t oldLen, VADDR_T newAddr, size_t newLen, unsigned int flags)
STATUS_T LOS_UnMMap(VADDR_T addr, size_t size)
解除映射关系
函数调用图:
这是这个函数的调用关系图:

◆ LOS_DumpMemRegion()

VOID LOS_DumpMemRegion ( VADDR_T  vaddr)

输出内存线性区

在文件 los_vm_syscall.c555 行定义.

556{
557 LosVmSpace *space = NULL;
558
559 space = OsCurrProcessGet()->vmSpace;
560 if (space == NULL) {
561 return;
562 }
563
564 if (LOS_IsRangeInSpace(space, ROUNDDOWN(vaddr, MB), MB) == FALSE) {//是否在空间范围内
565 return;
566 }
567
568 OsDumpPte(vaddr);//dump L1 L2
569 OsDumpAspace(space);//dump 空间
570}
VOID OsDumpAspace(LosVmSpace *space)
dump 指定虚拟空间的信息
Definition: los_vm_dump.c:396
VOID OsDumpPte(VADDR_T vaddr)
dump 页表项
Definition: los_vm_dump.c:456
BOOL LOS_IsRangeInSpace(const LosVmSpace *space, VADDR_T vaddr, size_t size)
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MMap()

VADDR_T LOS_MMap ( VADDR_T  vaddr,
size_t  len,
unsigned  prot,
unsigned long  flags,
int  fd,
unsigned long  pgoff 
)
 mmap基础概念:
 一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系.
 实现这样的映射关系后,进程就可以采用指针的方式读写操作这一段内存,而系统会自动回写脏页面到对应的文件磁盘上,
 即完成了对文件的操作而不必再调用read,write等系统调用函数。相反,内核空间对这段区域的修改也直接反映用户空间,
 从而可以实现不同进程间的文件共享。

 https://www.cnblogs.com/huxiao-tee/p/4660352.html
 http://abcdxyzk.github.io/blog/2015/09/11/kernel-mm-mmap/

 参数     描述      
 addr   指向欲映射的内存起始地址,通常设为 NULL,代表让系统自动选定地址,映射成功后返回该地址。
 length 代表将文件中多大的部分映射到内存。
 prot   用于设置内存段的访问权限,有如下权限:
         PROT_EXEC 映射区域可被执行
         PROT_READ 映射区域可被读取
         PROT_WRITE 映射区域可被写入
         PROT_NONE 映射区域不能存取
         
 flags  控制程序对内存段的改变所造成的影响,有如下属性:
         MAP_FIXED 如果参数start所指的地址无法成功建立映射时,则放弃映射,不对地址做修正。通常不鼓励用此旗标。
         MAP_SHARED 对映射区域的写入数据会复制回文件内,而且允许其他映射该文件的进程共享。
         MAP_PRIVATE 对映射区域的写入操作会产生一个映射文件的复制,即私人的“写入时复制”(copy on write)对此区域作的任何修改都不会写回原来的文件内容。
         MAP_ANONYMOUS建立匿名映射。此时会忽略参数fd,不涉及文件,而且映射区域无法和其他进程共享。
         MAP_DENYWRITE只允许对映射区域的写入操作,其他对文件直接写入的操作将会被拒绝。
         MAP_LOCKED 将映射区域锁定住,这表示该区域不会被置换(swap)。

 fd:        要映射到内存中的文件描述符。如果使用匿名内存映射时,即flags中设置了MAP_ANONYMOUS,fd设为-1。
         有些系统不支持匿名内存映射,则可以使用fopen打开/dev/zero文件,然后对该文件进行映射,可以同样达到匿名内存映射的效果。

 offset 文件映射的偏移量,通常设置为0,代表从文件最前方开始对应,offset必须是PAGE_SIZE的整数倍。
 成功返回:虚拟内存地址,这地址是页对齐。
 失败返回:(void *)-1。
参数
vaddr
len
prot
flags
fd
pgoff
返回
VADDR_T

在文件 los_vm_syscall.c149 行定义.

150{
151 STATUS_T status;
152 VADDR_T resultVaddr;
153 UINT32 regionFlags;
154 LosVmMapRegion *newRegion = NULL;//应用的内存分配对应到内核就是分配一个线性区
155 struct file *filep = NULL;// inode : file = 1:N ,一对多关系,一个inode可以被多个进程打开,返回不同的file但都指向同一个inode
156 LosVmSpace *vmSpace = OsCurrProcessGet()->vmSpace;
157
158 len = ROUNDUP(len, PAGE_SIZE);
159 STATUS_T checkRst = OsCheckMMapParams(&vaddr, flags, len, pgoff);
160 if (checkRst != LOS_OK) {
161 return checkRst;
162 }
163
164 if (LOS_IsNamedMapping(flags)) {//是否文件映射
165 status = fs_getfilep(fd, &filep);//获取文件描述符和状态
166 if (status < 0) {
167 return -EBADF;
168 }
169
170 status = OsNamedMmapingPermCheck(filep, flags, prot);
171 if (status < 0) {
172 return status;
173 }
174 }
175
176 (VOID)LOS_MuxAcquire(&vmSpace->regionMux);
177 /* user mode calls mmap to release heap physical memory without releasing heap virtual space */
178 status = OsUserHeapFree(vmSpace, vaddr, len);//用户模式释放堆物理内存而不释放堆虚拟空间
179 if (status == LOS_OK) {//OsUserHeapFree 干两件事 1.解除映射关系 2.释放物理页
180 resultVaddr = vaddr;
181 goto MMAP_DONE;
182 }
183 //地址不在堆区
184 regionFlags = OsCvtProtFlagsToRegionFlags(prot, flags);//将参数flag转换Region的flag
185 newRegion = LOS_RegionAlloc(vmSpace, vaddr, len, regionFlags, pgoff);//分配一个线性区
186 if (newRegion == NULL) {
187 resultVaddr = (VADDR_T)-ENOMEM;//ENOMEM:内存溢出
188 goto MMAP_DONE;
189 }
190 newRegion->regionFlags |= VM_MAP_REGION_FLAG_MMAP;
191 resultVaddr = newRegion->range.base;//线性区基地址为分配的地址
192
193 if (LOS_IsNamedMapping(flags)) {
194 status = OsNamedMMap(filep, newRegion);//文件映射
195 } else {
196 status = OsAnonMMap(newRegion);//匿名映射
197 }
198
199 if (status != LOS_OK) {
200 LOS_RbDelNode(&vmSpace->regionRbTree, &newRegion->rbNode);//从红黑树和双循环链表中删除
201 LOS_RegionFree(vmSpace, newRegion);//释放
202 resultVaddr = (VADDR_T)-ENOMEM;
203 goto MMAP_DONE;
204 }
205
206MMAP_DONE:
207 (VOID)LOS_MuxRelease(&vmSpace->regionMux);
208 return resultVaddr;
209}
VOID LOS_RbDelNode(LosRbTree *pstTree, LosRbNode *pstNode)
Definition: los_rbtree.c:700
STATUS_T OsNamedMMap(struct file *filep, LosVmMapRegion *region)
INT32 OsUserHeapFree(LosVmSpace *vmSpace, VADDR_T addr, size_t len)
STATUS_T OsAnonMMap(LosVmMapRegion *region)
匿名映射
STATUS_T OsNamedMmapingPermCheck(struct file *filep, unsigned long flags, unsigned prot)
STATUS_T OsCheckMMapParams(VADDR_T *vaddr, unsigned long flags, size_t len, unsigned long pgoff)
STATIC INLINE BOOL LOS_IsNamedMapping(unsigned long flags)
LosRbNode rbNode
Definition: los_vm_map.h:120
LosRbTree regionRbTree
Definition: los_vm_map.h:148
函数调用图:
这是这个函数的调用关系图:

◆ LOS_UnMMap()

STATUS_T LOS_UnMMap ( VADDR_T  addr,
size_t  size 
)

解除映射关系

在文件 los_vm_syscall.c211 行定义.

212{
213 if ((addr <= 0) || (size <= 0)) {
214 return -EINVAL;
215 }
216
217 return OsUnMMap(OsCurrProcessGet()->vmSpace, addr, size);
218}
STATUS_T OsUnMMap(LosVmSpace *space, VADDR_T addr, size_t size)
函数调用图:
这是这个函数的调用关系图:

◆ OsAnonMMap()

STATUS_T OsAnonMMap ( LosVmMapRegion region)

匿名映射

在文件 los_vm_syscall.c99 行定义.

100{
101 LOS_SetRegionTypeAnon(region);
102 return LOS_OK;
103}
STATIC INLINE VOID LOS_SetRegionTypeAnon(LosVmMapRegion *region)
设为匿名swap映射线性区
Definition: los_vm_map.h:270
函数调用图:
这是这个函数的调用关系图:

◆ OsCheckMMapParams()

STATUS_T OsCheckMMapParams ( VADDR_T vaddr,
unsigned long  flags,
size_t  len,
unsigned long  pgoff 
)

在文件 los_vm_syscall.c50 行定义.

51{
52 if ((len == 0) || (len > USER_ASPACE_SIZE)) {
53 return -EINVAL;
54 }
55 if (len > OsCurrProcessGet()->vmSpace->mapSize) {
56 return -ENOMEM;
57 }
58
59 if (((flags & MAP_FIXED) == 0) && ((flags & MAP_FIXED_NOREPLACE) == 0)) {
60 *vaddr = ROUNDUP(*vaddr, PAGE_SIZE);
61 if ((*vaddr != 0) && (!LOS_IsUserAddressRange(*vaddr, len))) {
62 *vaddr = 0;
63 }
64 } else if ((!LOS_IsUserAddressRange(*vaddr, len)) || (!IS_ALIGNED(*vaddr, PAGE_SIZE))) {
65 return -EINVAL;
66 }
67
68 if ((flags & MAP_SUPPORT_MASK) == 0) {//映射权限限制
69 return -EINVAL;
70 }
71 if (((flags & MAP_SHARED_PRIVATE) == 0) || ((flags & MAP_SHARED_PRIVATE) == MAP_SHARED_PRIVATE)) {
72 return -EINVAL;
73 }
74
75 if (((len >> PAGE_SHIFT) + pgoff) < pgoff) {
76 return -EINVAL;
77 }
78
79 return LOS_OK;
80}
STATIC INLINE BOOL LOS_IsUserAddressRange(VADDR_T vaddr, size_t len)
虚拟地址[vaddr,vaddr + len]是否在用户空间
Definition: los_vm_map.h:281
函数调用图:
这是这个函数的调用关系图:

◆ OsInheritOldRegionName()

STATIC UINT32 OsInheritOldRegionName ( UINT32  oldRegionFlags)

继承老线性区的标签

在文件 los_vm_syscall.c316 行定义.

317{
318 UINT32 vmFlags = 0;
319
320 if (oldRegionFlags & VM_MAP_REGION_FLAG_HEAP) { //如果是从大堆区中申请的
321 vmFlags |= VM_MAP_REGION_FLAG_HEAP; //线性区则贴上堆区标签
322 } else if (oldRegionFlags & VM_MAP_REGION_FLAG_STACK) {
323 vmFlags |= VM_MAP_REGION_FLAG_STACK;
324 } else if (oldRegionFlags & VM_MAP_REGION_FLAG_TEXT) {
325 vmFlags |= VM_MAP_REGION_FLAG_TEXT;
326 } else if (oldRegionFlags & VM_MAP_REGION_FLAG_VDSO) {
327 vmFlags |= VM_MAP_REGION_FLAG_VDSO;
328 } else if (oldRegionFlags & VM_MAP_REGION_FLAG_MMAP) {
329 vmFlags |= VM_MAP_REGION_FLAG_MMAP;
330 } else if (oldRegionFlags & VM_MAP_REGION_FLAG_SHM) {
331 vmFlags |= VM_MAP_REGION_FLAG_SHM;
332 }
333
334 return vmFlags;
335}
这是这个函数的调用关系图:

◆ OsMremapCheck()

STATUS_T OsMremapCheck ( VADDR_T  addr,
size_t  oldLen,
VADDR_T  newAddr,
size_t  newLen,
unsigned int  flags 
)

在文件 los_vm_syscall.c409 行定义.

410{
412 LosVmMapRegion *region = LOS_RegionFind(space, addr);
413 VADDR_T regionEnd;
414
415 if ((region == NULL) || (region->range.base > addr) || (newLen == 0)) {
416 return -EINVAL;
417 }
418
419 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE)) {
420 return -EINVAL;
421 }
422
423 if (((flags & MREMAP_FIXED) == MREMAP_FIXED) && ((flags & MREMAP_MAYMOVE) == 0)) {
424 return -EINVAL;
425 }
426
427 if (!IS_ALIGNED(addr, PAGE_SIZE)) {
428 return -EINVAL;
429 }
430
431 regionEnd = region->range.base + region->range.size;
432
433 /* we can't operate across region */
434 if (oldLen > regionEnd - addr) {
435 return -EFAULT;
436 }
437
438 /* avoiding overflow */
439 if (newLen > oldLen) {
440 if ((addr + newLen) < addr) {
441 return -EINVAL;
442 }
443 }
444
445 /* avoid new region overlaping with the old one */
446 if (flags & MREMAP_FIXED) {
447 if (((region->range.base + region->range.size) > newAddr) &&
448 (region->range.base < (newAddr + newLen))) {
449 return -EINVAL;
450 }
451
452 if (!IS_ALIGNED(newAddr, PAGE_SIZE)) {
453 return -EINVAL;
454 }
455 }
456
457 return LOS_OK;
458}
函数调用图:
这是这个函数的调用关系图:

◆ OsNamedMmapingPermCheck()

STATUS_T OsNamedMmapingPermCheck ( struct file filep,
unsigned long  flags,
unsigned  prot 
)

在文件 los_vm_syscall.c82 行定义.

83{
84 if (!((unsigned int)filep->f_oflags & O_RDWR) && (((unsigned int)filep->f_oflags & O_ACCMODE) ^ O_RDONLY)) {
85 return -EACCES;
86 }
87 if (flags & MAP_SHARED) {
88 if (((unsigned int)filep->f_oflags & O_APPEND) && (prot & PROT_WRITE)) {
89 return -EACCES;
90 }
91 if ((prot & PROT_WRITE) && !((unsigned int)filep->f_oflags & O_RDWR)) {
92 return -EACCES;
93 }
94 }
95
96 return LOS_OK;
97}
int f_oflags
这是这个函数的调用关系图:

◆ OsProtMprotectPermCheck()

STATIC INLINE BOOL OsProtMprotectPermCheck ( unsigned long  prot,
LosVmMapRegion region 
)

在文件 los_vm_syscall.c219 行定义.

220{
221 UINT32 protFlags = 0;
222 UINT32 permFlags = 0;
223 UINT32 fileFlags = region->unTypeData.rf.f_oflags;
224 permFlags |= ((fileFlags & O_ACCMODE) ^ O_RDONLY) ? 0 : VM_MAP_REGION_FLAG_PERM_READ;
225 permFlags |= (fileFlags & O_WRONLY) ? VM_MAP_REGION_FLAG_PERM_WRITE : 0;
226 permFlags |= (fileFlags & O_RDWR) ? (VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE) : 0;
227 protFlags |= (prot & PROT_READ) ? VM_MAP_REGION_FLAG_PERM_READ : 0;
228 protFlags |= (prot & PROT_WRITE) ? VM_MAP_REGION_FLAG_PERM_WRITE : 0;
229
230 return ((protFlags & permFlags) == protFlags);
231}
struct VmMapRegion::@4::VmRegionFile rf
union VmMapRegion::@4 unTypeData
这是这个函数的调用关系图:

◆ OsShrinkHeap()

VOID * OsShrinkHeap ( VOID *  addr,
LosVmSpace space 
)

收缩堆区

在文件 los_vm_syscall.c233 行定义.

234{
235 VADDR_T newBrk, oldBrk;
236
237 newBrk = LOS_Align((VADDR_T)(UINTPTR)addr, PAGE_SIZE);//新堆顶
238 oldBrk = LOS_Align(space->heapNow, PAGE_SIZE);//旧堆顶
239 if (LOS_UnMMap(newBrk, (oldBrk - newBrk)) < 0) {//解除相差区的映射
240 return (void *)(UINTPTR)space->heapNow;//解除失败就持续现有的
241 }
242 space->heapNow = (VADDR_T)(UINTPTR)addr;//返回新堆顶
243 return addr;
244}
函数调用图:
这是这个函数的调用关系图: