2011-05-07 22:08:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/mmap/fs_munmap.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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.
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2022-04-08 09:31:24 +02:00
|
|
|
#include <nuttx/mm/map.h>
|
2011-05-07 22:08:46 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <assert.h>
|
2011-05-07 22:08:46 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2011-05-08 16:42:42 +02:00
|
|
|
#include "fs_rammap.h"
|
2024-07-15 11:52:00 +02:00
|
|
|
#include "sched/sched.h"
|
2011-05-07 22:08:46 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2021-03-19 22:06:08 +01:00
|
|
|
* Private Functions
|
2011-05-07 22:08:46 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2024-05-16 16:40:34 +02:00
|
|
|
static int file_munmap_(FAR void *start, size_t length,
|
|
|
|
enum mm_map_type_e type)
|
2011-05-07 22:08:46 +02:00
|
|
|
{
|
2024-07-15 11:52:00 +02:00
|
|
|
FAR struct tcb_s *tcb = this_task();
|
2022-04-08 09:31:24 +02:00
|
|
|
FAR struct task_group_s *group = tcb->group;
|
|
|
|
FAR struct mm_map_entry_s *entry = NULL;
|
2023-04-20 14:37:45 +02:00
|
|
|
FAR struct mm_map_s *mm = get_current_mm();
|
2022-04-08 09:31:24 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Iterate through all the mappings and call the underlying
|
|
|
|
* unmap for every mapping where "start" lies
|
|
|
|
* break loop on any errors.
|
|
|
|
*
|
|
|
|
* Get exclusive access to mm_map for this
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = mm_map_lock();
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2023-06-08 11:41:46 +02:00
|
|
|
entry = mm_map_find(mm, start, length);
|
|
|
|
|
|
|
|
/* If entry don't find, the start and length is invalid. */
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
2022-04-08 09:31:24 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(entry->munmap);
|
|
|
|
ret = entry->munmap(group, entry, start, length);
|
|
|
|
}
|
2023-06-08 11:41:46 +02:00
|
|
|
while (ret == OK && (entry = mm_map_find(mm, start, length)));
|
2022-04-08 09:31:24 +02:00
|
|
|
|
2023-06-08 11:41:46 +02:00
|
|
|
unlock:
|
2022-04-08 09:31:24 +02:00
|
|
|
mm_map_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2020-09-17 09:26:35 +02:00
|
|
|
}
|
2021-03-19 22:06:08 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_mummap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Equivalent to the standard file_mummap() function except it does not set
|
|
|
|
* the errno variable.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_munmap(FAR void *start, size_t length)
|
|
|
|
{
|
2024-05-16 16:40:34 +02:00
|
|
|
return file_munmap_(start, length, MAP_KERNEL);
|
2021-03-19 22:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: munmap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* munmap() system call deletes mappings for the specified address range.
|
|
|
|
* All memory starting with 'start' and continuing for a length of 'length'
|
|
|
|
* bytes are removed.
|
|
|
|
*
|
|
|
|
* NuttX operates in a flat open address space. Therefore, it generally
|
|
|
|
* does not require mmap() and, hence, munmap functionality. There are
|
|
|
|
* two exceptions where mmap() is available:
|
|
|
|
*
|
|
|
|
* 1. mmap() is the API that is used to support direct access to random
|
|
|
|
* access media under the following very restrictive conditions:
|
|
|
|
*
|
2024-06-19 10:51:56 +02:00
|
|
|
* a. The filesystem implements the mmap file operation. Any file
|
2021-03-19 22:06:08 +01:00
|
|
|
* system that maps files contiguously on the media should support
|
|
|
|
* this ioctl. (vs. file system that scatter files over the media
|
|
|
|
* in non-contiguous sectors). As of this writing, ROMFS is the
|
|
|
|
* only file system that meets this requirement.
|
|
|
|
* b. The underlying block driver supports the BIOC_XIPBASE ioctl
|
|
|
|
* command that maps the underlying media to a randomly accessible
|
2024-06-19 10:51:56 +02:00
|
|
|
* address. At present, only the RAM/ROM disk driver does this.
|
2021-03-19 22:06:08 +01:00
|
|
|
*
|
2024-06-19 10:51:56 +02:00
|
|
|
* munmap() is still not required in this first case. The mapped address
|
|
|
|
* is a static address in the MCUs address space does not need to be
|
|
|
|
* munmapped. Support for munmap() in this case provided by the simple
|
|
|
|
* definition in sys/mman.h:
|
2021-03-19 22:06:08 +01:00
|
|
|
*
|
|
|
|
* #define munmap(start, length)
|
|
|
|
*
|
|
|
|
* 2. If CONFIG_FS_RAMMAP is defined in the configuration, then mmap() will
|
|
|
|
* support simulation of memory mapped files by copying files whole
|
|
|
|
* into RAM. munmap() is required in this case to free the allocated
|
|
|
|
* memory holding the shared copy of the file.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* start The start address of the mapping to delete. For this
|
2024-06-19 10:51:56 +02:00
|
|
|
* simplified munmap() implementation, the must be the start
|
2021-03-19 22:06:08 +01:00
|
|
|
* address of the memory region (the same address returned by
|
|
|
|
* mmap()).
|
2024-06-19 10:51:56 +02:00
|
|
|
* length The length region to be unmapped.
|
2021-03-19 22:06:08 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, munmap() returns 0, on failure -1, and errno is set
|
|
|
|
* (probably to EINVAL).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int munmap(FAR void *start, size_t length)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2024-05-16 16:40:34 +02:00
|
|
|
ret = file_munmap_(start, length, MAP_USER);
|
2021-03-19 22:06:08 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|