2011-05-07 22:08:46 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* fs/mmap/fs_rammap.c
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
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>
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <string.h>
|
2011-05-08 02:20:44 +02:00
|
|
|
#include <unistd.h>
|
2011-05-07 22:08:46 +02:00
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2023-04-20 14:37:45 +02:00
|
|
|
#include <nuttx/sched.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
|
|
|
|
|
|
|
#ifdef CONFIG_FS_RAMMAP
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2011-05-08 16:42:42 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-04 13:52:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2011-05-08 16:42:42 +02:00
|
|
|
|
2023-01-04 13:52:29 +01:00
|
|
|
static int unmap_rammap(FAR struct task_group_s *group,
|
|
|
|
FAR struct mm_map_entry_s *entry,
|
|
|
|
FAR void *start,
|
|
|
|
size_t length)
|
2021-06-23 19:42:11 +02:00
|
|
|
{
|
2024-08-19 06:38:55 +02:00
|
|
|
FAR void *newaddr = NULL;
|
2023-01-10 17:48:45 +01:00
|
|
|
off_t offset;
|
|
|
|
bool kernel = entry->priv.i;
|
|
|
|
int ret = OK;
|
2023-01-04 13:52:29 +01: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 freeing a block of memory but leaving a block of
|
|
|
|
* memory at the end. This is a consequence of using kumm_realloc() to
|
|
|
|
* simulate the unmapping.
|
|
|
|
*/
|
|
|
|
|
2023-01-13 12:24:56 +01:00
|
|
|
offset = (uintptr_t)start - (uintptr_t)entry->vaddr;
|
2023-01-04 13:52:29 +01:00
|
|
|
if (offset + length < entry->length)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Cannot umap without unmapping to the end\n");
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Okay.. the region is being unmapped to the end. Make sure the length
|
|
|
|
* indicates that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
length = entry->length - offset;
|
|
|
|
|
|
|
|
/* Are we unmapping the entire region (offset == 0)? */
|
|
|
|
|
|
|
|
if (length >= entry->length)
|
|
|
|
{
|
|
|
|
/* Free the region */
|
|
|
|
|
|
|
|
if (kernel)
|
|
|
|
{
|
|
|
|
kmm_free(entry->vaddr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
kumm_free(entry->vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then remove the mapping from the list */
|
|
|
|
|
|
|
|
ret = mm_map_remove(get_group_mm(group), entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. We have been asked to "unmap' only a portion of the memory
|
|
|
|
* (offset > 0).
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (kernel)
|
|
|
|
{
|
|
|
|
newaddr = kmm_realloc(entry->vaddr, length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newaddr = kumm_realloc(entry->vaddr, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(newaddr == entry->vaddr);
|
2023-01-10 17:48:45 +01:00
|
|
|
entry->vaddr = newaddr;
|
2023-01-04 13:52:29 +01:00
|
|
|
entry->length = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2011-05-08 16:42:42 +02:00
|
|
|
|
2011-05-07 22:08:46 +02:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2011-05-07 22:08:46 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rammmap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Support simulation of memory mapped files by copying files into RAM.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2021-03-19 22:06:08 +01:00
|
|
|
* filep file descriptor of the backing file -- required.
|
2023-01-16 11:03:58 +01:00
|
|
|
* entry mmap entry information.
|
|
|
|
* field offset and length must be initialized correctly.
|
2021-03-19 22:06:08 +01:00
|
|
|
* kernel kmm_zalloc or kumm_zalloc
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2023-01-16 11:03:58 +01:00
|
|
|
* On success, rammap returns 0 and entry->vaddr points to memory mapped.
|
|
|
|
* Otherwise errno is returned appropriately.
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
|
|
|
* EBADF
|
|
|
|
* 'fd' is not a valid file descriptor.
|
|
|
|
* EINVAL
|
|
|
|
* 'length' or 'offset' are invalid
|
|
|
|
* ENOMEM
|
|
|
|
* Insufficient memory is available to map the file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-04 13:52:29 +01:00
|
|
|
int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
|
|
|
|
bool kernel)
|
2011-05-07 22:08:46 +02:00
|
|
|
{
|
2011-05-08 02:20:44 +02:00
|
|
|
FAR uint8_t *rdbuffer;
|
|
|
|
ssize_t nread;
|
|
|
|
off_t fpos;
|
|
|
|
int ret;
|
2023-01-04 13:52:29 +01:00
|
|
|
size_t length = entry->length;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
|
|
|
/* There is a major design flaw that I have not yet thought of fix for:
|
|
|
|
* The goal is to have a single region of memory that represents a single
|
|
|
|
* file and can be shared by many threads. That is, given a filename a
|
|
|
|
* thread should be able to open the file, get a file descriptor, and
|
|
|
|
* call mmap() to get a memory region. Different file descriptors opened
|
|
|
|
* with the same file path should get the same memory region when mapped.
|
|
|
|
*
|
|
|
|
* The design flaw is that I don't have sufficient knowledge to know that
|
|
|
|
* these different file descriptors map to the same file. So, for the time
|
|
|
|
* being, a new memory region is created each time that rammap() is called.
|
|
|
|
* Not very useful!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocate a region of memory of the specified size */
|
|
|
|
|
2023-01-04 13:52:29 +01:00
|
|
|
rdbuffer = kernel ? kmm_malloc(length) : kumm_malloc(length);
|
|
|
|
if (!rdbuffer)
|
2011-05-08 02:20:44 +02:00
|
|
|
{
|
2023-01-10 17:48:45 +01:00
|
|
|
ferr("ERROR: Region allocation failed, length: %zu\n", length);
|
2021-03-19 22:06:08 +01:00
|
|
|
return -ENOMEM;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-16 10:55:02 +01:00
|
|
|
entry->vaddr = rdbuffer; /* save the buffer firstly */
|
|
|
|
|
2011-05-08 02:20:44 +02:00
|
|
|
/* Seek to the specified file offset */
|
|
|
|
|
2023-01-04 13:52:29 +01:00
|
|
|
fpos = file_seek(filep, entry->offset, SEEK_SET);
|
2020-05-02 16:30:58 +02:00
|
|
|
if (fpos < 0)
|
2011-05-08 02:20:44 +02:00
|
|
|
{
|
|
|
|
/* Seek failed... errno has already been set, but EINVAL is probably
|
|
|
|
* the correct response.
|
|
|
|
*/
|
|
|
|
|
2023-01-10 17:48:45 +01:00
|
|
|
ferr("ERROR: Seek to position %zu failed\n", (size_t)entry->offset);
|
2020-05-02 16:30:58 +02:00
|
|
|
ret = fpos;
|
2011-05-08 02:20:44 +02:00
|
|
|
goto errout_with_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file data into the memory region */
|
|
|
|
|
|
|
|
while (length > 0)
|
|
|
|
{
|
2021-03-19 22:06:08 +01:00
|
|
|
nread = file_read(filep, rdbuffer, length);
|
2011-05-08 02:20:44 +02:00
|
|
|
if (nread < 0)
|
|
|
|
{
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Handle the special case where the read was interrupted by a
|
|
|
|
* signal.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
if (nread != -EINTR)
|
2015-10-11 19:39:29 +02:00
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
/* All other read errors are bad. */
|
2015-10-11 19:39:29 +02:00
|
|
|
|
2023-01-10 17:48:45 +01:00
|
|
|
ferr("ERROR: Read failed: offset=%zu ret=%zd\n",
|
|
|
|
(size_t)entry->offset, nread);
|
2017-10-11 20:13:41 +02:00
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
ret = nread;
|
2015-10-11 19:39:29 +02:00
|
|
|
goto errout_with_region;
|
2017-10-11 20:13:41 +02:00
|
|
|
}
|
2012-09-10 22:00:04 +02:00
|
|
|
}
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
/* Check for end of file. */
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
if (nread == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
/* Increment number of bytes read */
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
rdbuffer += nread;
|
|
|
|
length -= nread;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero any memory beyond the amount read from the file */
|
|
|
|
|
|
|
|
memset(rdbuffer, 0, length);
|
|
|
|
|
|
|
|
/* Add the buffer to the list of regions */
|
|
|
|
|
2023-01-10 17:48:45 +01:00
|
|
|
entry->priv.i = kernel;
|
2023-01-04 13:52:29 +01:00
|
|
|
entry->munmap = unmap_rammap;
|
|
|
|
|
2023-04-20 14:37:45 +02:00
|
|
|
ret = mm_map_add(get_current_mm(), entry);
|
2011-05-08 02:20:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
goto errout_with_region;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
return OK;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
|
|
|
errout_with_region:
|
2021-03-19 22:06:08 +01:00
|
|
|
if (kernel)
|
|
|
|
{
|
2023-01-16 11:03:58 +01:00
|
|
|
kmm_free(entry->vaddr);
|
2021-03-19 22:06:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-16 11:03:58 +01:00
|
|
|
kumm_free(entry->vaddr);
|
2021-03-19 22:06:08 +01:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
return ret;
|
2011-05-07 22:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_FS_RAMMAP */
|