mm/map: Add a common virtual memory region allocator

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
This commit is contained in:
Jukka Laitinen 2023-01-11 12:23:08 +04:00 committed by Xiang Xiao
parent 70de321de3
commit 2236facca9
5 changed files with 180 additions and 4 deletions

View File

@ -28,6 +28,7 @@
#include <nuttx/config.h>
#include <nuttx/queue.h>
#include <nuttx/mutex.h>
#include <nuttx/mm/gran.h>
/****************************************************************************
* Forward declarations
@ -73,6 +74,11 @@ struct mm_map_entry_s
struct mm_map_s
{
sq_queue_t mm_map_sq;
#ifdef CONFIG_ARCH_VMA_MAPPING
GRAN_HANDLE mm_map_vpages;
#endif
rmutex_t mm_map_mutex;
};
@ -120,14 +126,15 @@ void mm_map_unlock(void);
* Initialization function, called only by group_initialize
*
* Input Parameters:
* mm - Pointer to the mm_map structure to be initialized
* mm - Pointer to the mm_map structure to be initialized
* kernel - Indicates whether we are initializing a kernel task
*
* Returned Value:
* None
*
****************************************************************************/
void mm_map_initialize(FAR struct mm_map_s *mm);
void mm_map_initialize(FAR struct mm_map_s *mm, bool kernel);
/****************************************************************************
* Name: mm_map_destroy
@ -162,6 +169,46 @@ void mm_map_destroy(FAR struct mm_map_s *mm);
*
****************************************************************************/
#ifdef CONFIG_ARCH_VMA_MAPPING
/****************************************************************************
* Name: vm_alloc_region
*
* Description:
* Allocate virtual memory region from the process virtual memory area.
*
* Input Parameters:
* mm - A reference to the process mm_map struct
* vaddr - Virtual start address where the allocation starts, if NULL, will
* seek and return an address that satisfies the 'size' parameter
* size - Size of the area to allocate
*
* Returned Value:
* Pointer to reserved vaddr, or NULL if out-of-memory
*
****************************************************************************/
FAR void *vm_alloc_region(FAR struct mm_map_s *mm, FAR void *vaddr,
size_t size);
/****************************************************************************
* Name: vm_release_region
*
* Description:
* Free a previously allocated virtual memory region
*
* Input Parameters:
* mm - A reference to the process' mm_map struct
* vaddr - Virtual start address where the allocation starts.
* size - Size of the allocated area.
*
****************************************************************************/
void vm_release_region(FAR struct mm_map_s *mm, FAR void *vaddr,
size_t size);
#endif
int mm_map_add(FAR struct mm_map_entry_s *entry);
/****************************************************************************

View File

@ -22,6 +22,10 @@
CSRCS += mm_map.c
ifeq ($(CONFIG_ARCH_VMA_MAPPING),y)
CSRCS += vm_region.c
endif
# Add the shared memory directory to the build
DEPPATH += --dep-path map

View File

@ -23,6 +23,8 @@
****************************************************************************/
#include <nuttx/mm/map.h>
#include <nuttx/pgalloc.h>
#include <nuttx/addrenv.h>
#include <stdbool.h>
#include <stddef.h>
#include <nuttx/sched.h>
@ -87,10 +89,29 @@ void mm_map_unlock(void)
*
****************************************************************************/
void mm_map_initialize(FAR struct mm_map_s *mm)
void mm_map_initialize(FAR struct mm_map_s *mm, bool kernel)
{
sq_init(&mm->mm_map_sq);
nxrmutex_init(&mm->mm_map_mutex);
/* Create the virtual pages allocator for user process */
#ifdef CONFIG_ARCH_VMA_MAPPING
if (!kernel)
{
mm->mm_map_vpages = gran_initialize((FAR void *)CONFIG_ARCH_SHM_VBASE,
ARCH_SHM_MAXPAGES << MM_PGSHIFT,
MM_PGSHIFT, MM_PGSHIFT);
if (!mm->mm_map_vpages)
{
merr("gran_initialize() failed\n");
}
}
else
{
mm->mm_map_vpages = NULL;
}
#endif
}
/****************************************************************************
@ -131,6 +152,15 @@ void mm_map_destroy(FAR struct mm_map_s *mm)
}
nxrmutex_destroy(&mm->mm_map_mutex);
/* Release the virtual pages allocator */
#ifdef CONFIG_ARCH_VMA_MAPPING
if (mm->mm_map_vpages)
{
gran_release(mm->mm_map_vpages);
}
#endif
}
/****************************************************************************

94
mm/map/vm_region.c Normal file
View File

@ -0,0 +1,94 @@
/****************************************************************************
* mm/map/vm_region.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 <nuttx/mm/map.h>
#include <nuttx/mm/gran.h>
#include <debug.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: vm_alloc_region
*
* Description:
* Allocate virtual memory region from the process virtual memory area.
*
* Input Parameters:
* mm - A reference to the process mm_map struct
* vaddr - Virtual start address where the allocation starts, if NULL, will
* seek and return an address that satisfies the 'size' parameter
* size - Size of the area to allocate
*
* Returned Value:
* Pointer to reserved vaddr, or NULL if out-of-memory
*
****************************************************************************/
FAR void *vm_alloc_region(FAR struct mm_map_s *mm, FAR void *vaddr,
size_t size)
{
FAR void *ret = NULL;
DEBUGASSERT(mm != NULL);
if (mm->mm_map_vpages != NULL)
{
if (vaddr == NULL)
{
ret = gran_alloc(mm->mm_map_vpages, size);
}
else
{
ret = gran_reserve(mm->mm_map_vpages, (uintptr_t)vaddr,
size);
}
}
return ret;
}
/****************************************************************************
* Name: vm_release_region
*
* Description:
* Free a previously allocated virtual memory region
*
* Input Parameters:
* mm - A reference to the process' mm_map struct
* vaddr - Virtual start address where the allocation starts.
* size - Size of the allocated area.
*
****************************************************************************/
void vm_release_region(FAR struct mm_map_s *mm, FAR void *vaddr, size_t size)
{
DEBUGASSERT(mm != NULL);
if (mm->mm_map_vpages != NULL)
{
gran_free(mm->mm_map_vpages, vaddr, size);
}
}

View File

@ -240,7 +240,8 @@ void group_initialize(FAR struct task_tcb_s *tcb)
/* Allocate mm_map list if required */
mm_map_initialize(&group->tg_mm_map);
mm_map_initialize(&group->tg_mm_map,
(tcb->cmn.flags & TCB_FLAG_TTYPE_KERNEL) != 0);
#ifdef HAVE_GROUP_MEMBERS
/* Assign the PID of this new task as a member of the group. */