mm/arch: userspace device mapping support

This patch adds definitions to support user space device mappings
that allows devices like frame buffer to be accessible from user
space in kernel mode.

The are mainly two changes:

- in `mm/`:
  added vm_map_region(), vm_unmap_region() for drivers to do
  device mapping easily.
- in `arch/`:
  extended ARCH_SHM_NPAGES as user-space mapping region size.
  decoupled ARCH_SHM_MAXREGIONS from region size calculations and
  limit its usage only for SysV shm purposes.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
This commit is contained in:
Yanfeng Liu
2024-04-17 11:21:22 +08:00
committed by Xiang Xiao
parent 1f7147129a
commit 3822d88669
6 changed files with 140 additions and 22 deletions
+3 -2
View File
@@ -191,8 +191,9 @@ config MM_SHM
depends on MM_PGALLOC && BUILD_KERNEL
select ARCH_VMA_MAPPING
---help---
Build in support for the shared memory interfaces shmget(), shmat(),
shmctl(), and shmdt().
Build support for mapping physical memory to user-space via
shared memory interfaces like shmget(), shmat(), shmctl(),
etc, or device mapping interfaces like vm_map_region() etc.
config MM_KMAP
bool "Support for dynamic kernel virtual mappings"
+1 -2
View File
@@ -117,8 +117,7 @@ void mm_map_initialize(FAR struct mm_map_s *mm, bool kernel)
if (!kernel)
{
mm->mm_map_vpages = gran_initialize((FAR void *)CONFIG_ARCH_SHM_VBASE,
ARCH_SHM_MAXPAGES << MM_PGSHIFT,
MM_PGSHIFT, MM_PGSHIFT);
ARCH_SHM_SIZE, MM_PGSHIFT, MM_PGSHIFT);
if (!mm->mm_map_vpages)
{
merr("gran_initialize() failed\n");
+91
View File
@@ -0,0 +1,91 @@
/****************************************************************************
* 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 */