2008-09-12 17:54:20 +02:00
|
|
|
/****************************************************************************
|
2011-05-07 22:08:46 +02:00
|
|
|
* fs/mmap/fs_mmap.c
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
2019-03-14 18:20:14 +01:00
|
|
|
* Copyright (C) 2008-2009, 2011, 2019 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/ioctl.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
#include <stdint.h>
|
2008-09-12 17:54:20 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2019-03-14 18:20:14 +01: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"
|
2008-09-12 17:54:20 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2008-09-12 17:54:20 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX operates in a flat open address space. Therefore, it generally
|
2011-05-07 22:08:46 +02:00
|
|
|
* does not require mmap() functionality. There are two exceptions:
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
2011-05-07 22:08:46 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2008-09-12 17:54:20 +02:00
|
|
|
* start A hint at where to map the memory -- ignored. The address
|
2011-05-07 22:08:46 +02:00
|
|
|
* of the underlying media is fixed and cannot be re-mapped without
|
2008-09-12 17:54:20 +02:00
|
|
|
* MMU support.
|
2011-05-07 22:08:46 +02:00
|
|
|
* length The length of the mapping. For exception #1 above, this length
|
|
|
|
* ignored: The entire underlying media is always accessible.
|
2008-09-12 17:54:20 +02:00
|
|
|
* prot See the PROT_* definitions in sys/mman.h.
|
|
|
|
* PROT_NONE - Will cause an error
|
|
|
|
* PROT_READ - PROT_WRITE and PROT_EXEC also assumed
|
|
|
|
* PROT_WRITE - PROT_READ and PROT_EXEC also assumed
|
|
|
|
* PROT_EXEC - PROT_READ and PROT_WRITE also assumed
|
|
|
|
* flags See the MAP_* definitions in sys/mman.h.
|
2019-11-23 15:07:04 +01:00
|
|
|
* MAP_SHARED - MAP_PRIVATE or MAP_SHARED required
|
|
|
|
* MAP_PRIVATE - MAP_PRIVATE or MAP_SHARED required
|
2008-09-12 17:54:20 +02:00
|
|
|
* MAP_FIXED - Will cause an error
|
|
|
|
* MAP_FILE - Ignored
|
2019-03-14 18:20:14 +01:00
|
|
|
* MAP_ANONYMOUS - Optional
|
2008-09-12 17:54:20 +02:00
|
|
|
* MAP_ANON - Will cause an error
|
|
|
|
* MAP_GROWSDOWN - Ignored
|
|
|
|
* MAP_DENYWRITE - Will cause an error
|
|
|
|
* MAP_EXECUTABLE - Ignored
|
|
|
|
* MAP_LOCKED - Ignored
|
|
|
|
* MAP_NORESERVE - Ignored
|
|
|
|
* MAP_POPULATE - Ignored
|
|
|
|
* MAP_NONBLOCK - Ignored
|
2008-09-12 21:17:15 +02:00
|
|
|
* fd file descriptor of the backing file -- required.
|
2008-09-12 17:54:20 +02:00
|
|
|
* offset The offset into the file to map
|
|
|
|
*
|
2011-05-07 22:08:46 +02:00
|
|
|
* Returned Value:
|
|
|
|
* On success, mmap() returns a pointer to the mapped area. On error, the
|
|
|
|
* value MAP_FAILED is returned, and errno is set appropriately.
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
|
|
|
* ENOSYS
|
|
|
|
* Returned if any of the unsupported mmap() features are attempted
|
|
|
|
* EBADF
|
|
|
|
* 'fd' is not a valid file descriptor.
|
|
|
|
* EINVAL
|
|
|
|
* Length is 0. flags contained neither MAP_PRIVATE or MAP_SHARED, or
|
|
|
|
* contained both of these values.
|
|
|
|
* ENODEV
|
|
|
|
* The underlying filesystem of the specified file does not support
|
|
|
|
* memory mapping.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR void *mmap(FAR void *start, size_t length, int prot, int flags,
|
|
|
|
int fd, off_t offset)
|
|
|
|
{
|
|
|
|
FAR void *addr;
|
2019-03-14 18:20:14 +01:00
|
|
|
int errcode;
|
2019-11-23 15:07:04 +01:00
|
|
|
int ret = -1;
|
2008-09-12 17:54:20 +02:00
|
|
|
|
|
|
|
/* Since only a tiny subset of mmap() functionality, we have to verify many
|
|
|
|
* things.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2019-11-23 15:07:04 +01:00
|
|
|
/* Fixed mappings and protections are not currently supported. These
|
2019-03-14 18:20:14 +01:00
|
|
|
* options could be supported in the KERNEL build with an MMU, but that
|
|
|
|
* logic is not in place.
|
|
|
|
*/
|
|
|
|
|
2008-09-12 17:54:20 +02:00
|
|
|
if (prot == PROT_NONE ||
|
2019-11-23 15:07:04 +01:00
|
|
|
(flags & (MAP_FIXED | MAP_DENYWRITE)) != 0)
|
2008-09-12 17:54:20 +02:00
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Unsupported options, prot=%x flags=%04x\n", prot, flags);
|
2019-03-14 18:20:14 +01:00
|
|
|
errcode = ENOSYS;
|
|
|
|
goto errout;
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2019-11-23 15:07:04 +01:00
|
|
|
/* A length of 0 is invalid. */
|
2019-03-14 18:20:14 +01:00
|
|
|
|
2019-11-23 15:07:04 +01:00
|
|
|
if (length == 0)
|
2008-09-12 17:54:20 +02:00
|
|
|
{
|
2020-11-20 03:59:51 +01:00
|
|
|
ferr("ERROR: Invalid length, length=%zu\n", length);
|
2019-03-14 18:20:14 +01:00
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout;
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-14 18:20:14 +01:00
|
|
|
/* Check if we are just be asked to allocate memory, i.e., MAP_ANONYMOUS
|
|
|
|
* set meaning that the memory is not backed up from a file. The file
|
|
|
|
* descriptor should be -1 (or refer to opened /dev/zero) in this case.
|
|
|
|
* The file descriptor is ignored in either case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & MAP_ANONYMOUS) != 0)
|
|
|
|
{
|
|
|
|
FAR void *alloc;
|
|
|
|
|
|
|
|
/* REVISIT: Should reside outside of the heap. That is really the
|
|
|
|
* only purpose of MAP_ANONYMOUS: To get non-heap memory. In KERNEL
|
|
|
|
* build, this could be accomplished using pgalloc(), provided that
|
|
|
|
* you had logic in place to assign a virtual address to the mapping.
|
|
|
|
*/
|
|
|
|
|
|
|
|
alloc = kumm_zalloc(length);
|
|
|
|
if (alloc == NULL)
|
|
|
|
{
|
|
|
|
ferr("ERROR: kumm_alloc() failed: %d\n", ret);
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
2019-11-23 15:07:04 +01:00
|
|
|
/* Perform the ioctl to get the base address of the file in 'mapped'
|
2009-12-15 01:18:32 +01:00
|
|
|
* in memory. (casting to uintptr_t first eliminates complaints on some
|
2009-12-13 16:13:37 +01:00
|
|
|
* architectures where the sizeof long is different from the size of
|
|
|
|
* a pointer).
|
2008-09-12 17:54:20 +02:00
|
|
|
*/
|
|
|
|
|
2019-11-23 15:07:04 +01:00
|
|
|
if ((flags & MAP_PRIVATE) == 0)
|
|
|
|
{
|
|
|
|
ret = ioctl(fd, FIOC_MMAP, (unsigned long)((uintptr_t)&addr));
|
|
|
|
}
|
|
|
|
|
2008-09-12 17:54:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-03-14 18:20:14 +01:00
|
|
|
/* Not directly mappable, probably because the underlying media does
|
|
|
|
* not support random access.
|
|
|
|
*/
|
|
|
|
|
2011-05-07 22:08:46 +02:00
|
|
|
#ifdef CONFIG_FS_RAMMAP
|
2019-03-14 18:20:14 +01:00
|
|
|
/* Allocate memory and copy the file into memory. We would, of course,
|
|
|
|
* do much better in the KERNEL build using the MMU.
|
|
|
|
*/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
return rammap(fd, length, offset);
|
|
|
|
#else
|
2019-03-14 18:20:14 +01:00
|
|
|
/* Error out. The errno value was already set by ioctl() */
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: ioctl(FIOC_MMAP) failed: %d\n", get_errno());
|
2019-11-23 15:07:04 +01:00
|
|
|
errcode = ENOSYS;
|
|
|
|
goto errout;
|
2011-05-07 22:08:46 +02:00
|
|
|
#endif
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the offset address */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
return (FAR void *)(((FAR uint8_t *)addr) + offset);
|
2019-03-14 18:20:14 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
set_errno(errcode);
|
|
|
|
return MAP_FAILED;
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|