mm/map: revise device mapping functions

This revises vm_map_region() by accepting unaligned paddr, which is
aligned-down before mapping and in-page offset is then added to vaddr
before returning. It also moves vm_map_region() and vm_unmap_region()
to vm_region.c.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
This commit is contained in:
Yanfeng Liu 2024-05-03 07:04:21 +08:00 committed by Xiang Xiao
parent 0cd5689bcb
commit 0f5ba00a50
3 changed files with 63 additions and 92 deletions

View File

@ -200,7 +200,10 @@ void vm_release_region(FAR struct mm_map_s *mm, FAR void *vaddr,
* *
* Description: * Description:
* Allocate virtual memory and maps given physical memory into user space * Allocate virtual memory and maps given physical memory into user space
* of the current process. * of the current process. The mapped region can be larger than requested
* if given paddr isn't page-aligned, use with care so that not to create
* unwanted security holes. The returned virtual address has same in-page
* alignment as given paddr.
* *
* Input Parameters: * Input Parameters:
* paddr - Starting physical address * paddr - Starting physical address

View File

@ -1,91 +0,0 @@
/****************************************************************************
* mm/map/vm_map.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/mm/map.h>
#include <nuttx/pgalloc.h>
#include <nuttx/sched.h>
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_ARCH_VMA_MAPPING
/* map physical region to userspace */
FAR void *vm_map_region(uintptr_t paddr, size_t size)
{
FAR void *vaddr;
uintptr_t tvaddr;
uint i = 0;
int ret = OK;
size_t npages = MM_NPAGES(size);
DEBUGASSERT(npages > 0);
DEBUGASSERT(MM_ISALIGNED(paddr));
vaddr = vm_alloc_region(get_current_mm(), 0, size);
if (vaddr)
{
tvaddr = (uintptr_t)vaddr;
for (; i < npages; i++, tvaddr += MM_PGSIZE, paddr += MM_PGSIZE)
{
ret = up_shmat(&paddr, 1, tvaddr);
if (ret)
{
goto errorout;
}
}
}
return vaddr;
errorout:
if (i) /* undo mapped pages */
{
up_shmdt((uintptr_t)vaddr, i);
}
vm_release_region(get_current_mm(), vaddr, size);
return 0;
}
/* unmap userspace device pointer */
int vm_unmap_region(FAR void *vaddr, size_t size)
{
size_t npages = MM_NPAGES(size);
int ret;
DEBUGASSERT(MM_ISALIGNED(vaddr));
DEBUGASSERT(npages);
ret = up_shmdt((uintptr_t)vaddr, npages);
vm_release_region(get_current_mm(), vaddr, size);
return ret;
}
#endif /* CONFIG_ARCH_VMA_MAPPING */

View File

@ -22,8 +22,10 @@
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
#include <nuttx/arch.h>
#include <nuttx/mm/map.h> #include <nuttx/mm/map.h>
#include <nuttx/mm/gran.h> #include <nuttx/mm/gran.h>
#include <nuttx/pgalloc.h>
#include <debug.h> #include <debug.h>
/**************************************************************************** /****************************************************************************
@ -91,3 +93,60 @@ void vm_release_region(FAR struct mm_map_s *mm, FAR void *vaddr, size_t size)
gran_free(mm->mm_map_vpages, vaddr, size); gran_free(mm->mm_map_vpages, vaddr, size);
} }
} }
/* map physical region to userspace */
FAR void *vm_map_region(uintptr_t paddr, size_t size)
{
FAR void *vaddr;
uintptr_t tvaddr;
size_t npages;
uintptr_t tpaddr = MM_PGALIGNDOWN(paddr);
uint i = 0;
int ret = OK;
DEBUGASSERT(paddr);
size += (paddr & MM_PGMASK);
npages = MM_NPAGES(size);
DEBUGASSERT(npages);
vaddr = vm_alloc_region(get_current_mm(), 0, size);
if (vaddr)
{
tvaddr = (uintptr_t)vaddr;
for (; i < npages; i++, tvaddr += MM_PGSIZE, tpaddr += MM_PGSIZE)
{
ret = up_shmat(&tpaddr, 1, tvaddr);
if (ret)
{
goto error;
}
}
}
return vaddr + (MM_PGMASK & paddr);
error:
if (i) /* undo alway mapped pages */
{
up_shmdt((uintptr_t)vaddr, i);
}
vm_release_region(get_current_mm(), vaddr, size);
return 0;
}
/* unmap userspace device pointer */
int vm_unmap_region(FAR void *vaddr, size_t size)
{
int ret;
DEBUGASSERT(size && vaddr);
size += ((uintptr_t)vaddr & MM_PGMASK);
vaddr = (void *)MM_PGALIGNDOWN(vaddr);
ret = up_shmdt((uintptr_t)vaddr, MM_NPAGES(size));
vm_release_region(get_current_mm(), vaddr, size);
return ret;
}