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 16:42:42 +02:00
|
|
|
#include <sys/mman.h>
|
2011-05-08 02:20:44 +02:00
|
|
|
|
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
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
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>
|
|
|
|
|
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
|
|
|
|
|
|
|
#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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is the list of all mapped files */
|
|
|
|
|
|
|
|
struct fs_allmaps_s g_rammaps;
|
|
|
|
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rammap_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verified that this capability has been initialized.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void rammap_initialize(void)
|
|
|
|
{
|
|
|
|
if (!g_rammaps.initialized)
|
|
|
|
{
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_rammaps.exclsem, 0, 1);
|
2011-05-08 16:42:42 +02:00
|
|
|
g_rammaps.initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2011-05-07 22:08:46 +02:00
|
|
|
* fd file descriptor of the backing file -- required.
|
|
|
|
* length The length of the mapping. For exception #1 above, this length
|
|
|
|
* ignored: The entire underlying media is always accessible.
|
|
|
|
* offset The offset into the file to map
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-01-12 05:33:46 +01:00
|
|
|
* On success, rammmap() returns a pointer to the mapped area. On error,
|
|
|
|
* the value MAP_FAILED is returned, and errno is set 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
FAR void *rammap(int fd, size_t length, off_t offset)
|
2011-05-07 22:08:46 +02:00
|
|
|
{
|
2011-05-08 16:42:42 +02:00
|
|
|
FAR struct fs_rammap_s *map;
|
2011-05-08 02:20:44 +02:00
|
|
|
FAR uint8_t *alloc;
|
|
|
|
FAR uint8_t *rdbuffer;
|
|
|
|
ssize_t nread;
|
|
|
|
off_t fpos;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2014-09-01 00:24:24 +02:00
|
|
|
alloc = (FAR uint8_t *)kumm_malloc(sizeof(struct fs_rammap_s) + length);
|
2011-05-08 02:20:44 +02:00
|
|
|
if (!alloc)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Region allocation failed, length: %d\n", (int)length);
|
2020-05-02 16:30:58 +02:00
|
|
|
ret = -ENOMEM;
|
2011-05-08 02:20:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the region */
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
map = (FAR struct fs_rammap_s *)alloc;
|
|
|
|
memset(map, 0, sizeof(struct fs_rammap_s));
|
|
|
|
map->addr = alloc + sizeof(struct fs_rammap_s);
|
|
|
|
map->length = length;
|
|
|
|
map->offset = offset;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
|
|
|
/* Seek to the specified file offset */
|
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
fpos = nx_seek(fd, offset, SEEK_SET);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Seek to position %d failed\n", (int)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 */
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
rdbuffer = map->addr;
|
2011-05-08 02:20:44 +02:00
|
|
|
while (length > 0)
|
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
nread = nx_read(fd, 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
|
|
|
|
2021-02-15 09:08:51 +01:00
|
|
|
ferr("ERROR: Read failed: offset=%d ret=%d\n",
|
2017-10-11 20:13:41 +02:00
|
|
|
(int)offset, (int)nread);
|
|
|
|
|
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 */
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
rammap_initialize();
|
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)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
goto errout_with_region;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
map->flink = g_rammaps.head;
|
2011-05-08 16:42:42 +02:00
|
|
|
g_rammaps.head = map;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_rammaps.exclsem);
|
2011-05-08 16:42:42 +02:00
|
|
|
return map->addr;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
|
|
|
errout_with_region:
|
2014-09-01 00:15:11 +02:00
|
|
|
kumm_free(alloc);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-05-08 02:20:44 +02:00
|
|
|
errout:
|
2020-05-02 16:30:58 +02:00
|
|
|
set_errno(-ret);
|
2011-05-08 02:20:44 +02:00
|
|
|
return MAP_FAILED;
|
2011-05-07 22:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_FS_RAMMAP */
|