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>
|
|
|
|
|
|
|
|
#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"
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
static int file_munmap_(FAR void *start, size_t length, bool kernel)
|
2011-05-07 22:08:46 +02:00
|
|
|
{
|
2020-09-17 09:26:35 +02:00
|
|
|
#ifdef CONFIG_FS_RAMMAP
|
2011-05-08 02:20:44 +02:00
|
|
|
FAR struct fs_rammap_s *prev;
|
|
|
|
FAR struct fs_rammap_s *curr;
|
|
|
|
FAR void *newaddr;
|
2011-05-08 16:42:42 +02:00
|
|
|
unsigned int offset;
|
2011-05-08 02:20:44 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find a region containing this start and length in the list of regions */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&g_rammaps.exclsem);
|
2011-05-08 02:20:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-19 22:06:08 +01:00
|
|
|
return ret;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Search the list of regions */
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2020-09-17 09:26:35 +02:00
|
|
|
for (prev = NULL, curr = g_rammaps.head; curr;
|
|
|
|
prev = curr, curr = curr->flink)
|
2011-05-08 02:20:44 +02:00
|
|
|
{
|
|
|
|
/* Does this region include any part of the specified range? */
|
|
|
|
|
|
|
|
if ((uintptr_t)start < (uintptr_t)curr->addr + curr->length &&
|
|
|
|
(uintptr_t)start + length >= (uintptr_t)curr->addr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we find the region */
|
|
|
|
|
|
|
|
if (!curr)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Region not found\n");
|
2021-03-19 22:06:08 +01:00
|
|
|
ret = -EINVAL;
|
2011-05-08 02:20:44 +02:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
/* Get the offset from the beginning of the region and the actual number
|
|
|
|
* of bytes to "unmap". All mappings must extend to the end of the region.
|
|
|
|
* There is no support for free a block of memory but leaving a block of
|
2014-09-01 00:15:11 +02:00
|
|
|
* memory at the end. This is a consequence of using kumm_realloc() to
|
2011-05-08 16:42:42 +02:00
|
|
|
* simulate the unmapping.
|
2011-05-08 02:20:44 +02:00
|
|
|
*/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
offset = start - curr->addr;
|
|
|
|
if (offset + length < curr->length)
|
2011-05-08 02:20:44 +02:00
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Cannot umap without unmapping to the end\n");
|
2021-03-19 22:06:08 +01:00
|
|
|
ret = -ENOSYS;
|
2011-05-08 02:20:44 +02:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
/* Okay.. the region is beging umapped to the end. Make sure the length
|
|
|
|
* indicates that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
length = curr->length - offset;
|
|
|
|
|
|
|
|
/* Are we unmapping the entire region (offset == 0)? */
|
|
|
|
|
|
|
|
if (length >= curr->length)
|
|
|
|
{
|
|
|
|
/* Yes.. remove the mapping from the list */
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
prev->flink = curr->flink;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_rammaps.head = curr->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then free the region */
|
|
|
|
|
2021-07-12 21:38:28 +02:00
|
|
|
if (kernel)
|
2021-03-19 22:06:08 +01:00
|
|
|
{
|
2021-07-12 21:38:28 +02:00
|
|
|
kmm_free(curr);
|
2021-03-19 22:06:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
kumm_free(curr);
|
|
|
|
}
|
2011-05-08 16:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. We have been asked to "unmap' only a portion of the memory
|
|
|
|
* (offset > 0).
|
2011-05-08 02:20:44 +02:00
|
|
|
*/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
else
|
2011-05-08 02:20:44 +02:00
|
|
|
{
|
2021-03-19 22:06:08 +01:00
|
|
|
if (kernel)
|
|
|
|
{
|
|
|
|
newaddr = kmm_realloc(curr->addr,
|
|
|
|
sizeof(struct fs_rammap_s) + length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newaddr = kumm_realloc(curr->addr,
|
|
|
|
sizeof(struct fs_rammap_s) + length);
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
DEBUGASSERT(newaddr == (FAR void *)(curr->addr));
|
2019-11-27 14:50:19 +01:00
|
|
|
UNUSED(newaddr); /* May not be used */
|
2011-05-08 16:42:42 +02:00
|
|
|
curr->length = length;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_rammaps.exclsem);
|
2011-05-08 02:20:44 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_rammaps.exclsem);
|
2021-03-19 22:06:08 +01:00
|
|
|
return ret;
|
2020-09-17 09:26:35 +02:00
|
|
|
#else
|
|
|
|
return OK;
|
2011-05-07 22:08:46 +02:00
|
|
|
#endif /* CONFIG_FS_RAMMAP */
|
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)
|
|
|
|
{
|
|
|
|
return file_munmap_(start, length, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* a. The filesystem supports the FIOC_MMAP ioctl command. Any file
|
|
|
|
* 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
|
|
|
|
* address. At present, only the RAM/ROM disk driver does this.
|
|
|
|
*
|
|
|
|
* munmap() is still not required in this first case. In this first
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* #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
|
|
|
|
* simplified munmap() implementation, the *must* be the start
|
|
|
|
* address of the memory region (the same address returned by
|
|
|
|
* mmap()).
|
|
|
|
* length The length region to be umapped.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
ret = file_munmap_(start, length, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|