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
|
|
|
*
|
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
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-09-12 17:54:20 +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.
|
2008-09-12 17:54:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2021-03-12 23:03:52 +01:00
|
|
|
#include <fcntl.h>
|
2008-09-12 17:54:20 +02:00
|
|
|
#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
|
|
|
|
|
|
|
/****************************************************************************
|
2021-03-19 22:06:08 +01:00
|
|
|
* Private Functions
|
2008-09-12 17:54:20 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-03-19 22:06:08 +01:00
|
|
|
* Name: file_mmap_
|
2008-09-12 17:54:20 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
static int file_mmap_(FAR struct file *filep, FAR void *start,
|
|
|
|
size_t length, int prot, int flags,
|
|
|
|
off_t offset, bool kernel, FAR void **mapped)
|
2008-09-12 17:54:20 +02:00
|
|
|
{
|
|
|
|
FAR void *addr;
|
2021-03-15 20:35:30 +01:00
|
|
|
int ret;
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-06-23 20:29:19 +02:00
|
|
|
if ((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);
|
2021-03-19 22:06:08 +01:00
|
|
|
return -ENOSYS;
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:35:30 +01:00
|
|
|
#ifndef CONFIG_FS_RAMMAP
|
|
|
|
if ((flags & MAP_PRIVATE) != 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: MAP_PRIVATE is not supported without file mapping"
|
|
|
|
"emulation\n");
|
2021-03-19 22:06:08 +01:00
|
|
|
return -ENOSYS;
|
2021-03-15 20:35:30 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_FS_RAMMAP */
|
|
|
|
|
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);
|
2021-03-19 22:06:08 +01:00
|
|
|
return -EINVAL;
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|
2021-03-15 20:35:30 +01:00
|
|
|
#endif /* CONFIG_DEBUG_FEATURES */
|
2008-09-12 17:54:20 +02:00
|
|
|
|
2021-03-12 23:03:52 +01:00
|
|
|
if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE &&
|
|
|
|
(flags & MAP_SHARED) != 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Unsupported options for read-only file descriptor,"
|
2021-03-19 22:06:08 +01:00
|
|
|
"prot=%x flags=%04x\n", prot, flags);
|
|
|
|
return -EACCES;
|
2021-03-12 23:03:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((filep->f_oflags & O_RDOK) == 0)
|
|
|
|
{
|
2021-03-19 22:06:08 +01:00
|
|
|
ferr("ERROR: File descriptor does not have read permission\n");
|
|
|
|
return -EACCES;
|
2021-03-12 23:03:52 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
*mapped = kernel ? kmm_zalloc(length) : kumm_zalloc(length);
|
|
|
|
if (*mapped == NULL)
|
2019-03-14 18:20:14 +01:00
|
|
|
{
|
|
|
|
ferr("ERROR: kumm_alloc() failed: %d\n", ret);
|
2021-03-19 22:06:08 +01:00
|
|
|
return -ENOMEM;
|
2019-03-14 18:20:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
return OK;
|
2019-03-14 18:20:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:35:30 +01:00
|
|
|
if ((flags & MAP_PRIVATE) != 0)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_FS_RAMMAP
|
|
|
|
/* Allocate memory and copy the file into memory. We would, of course,
|
|
|
|
* do much better in the KERNEL build using the MMU.
|
|
|
|
*/
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
return rammap(filep, length, offset, kernel, mapped);
|
2021-03-15 20:35:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
ret = file_ioctl(filep, 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.
|
|
|
|
*/
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
return rammap(filep, length, offset, kernel, mapped);
|
2011-05-08 16:42:42 +02:00
|
|
|
#else
|
2021-03-19 22:06:08 +01:00
|
|
|
ferr("ERROR: file_ioctl(FIOC_MMAP) failed: %d\n", ret);
|
|
|
|
return ret;
|
2011-05-07 22:08:46 +02:00
|
|
|
#endif
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the offset address */
|
|
|
|
|
2021-03-19 22:06:08 +01:00
|
|
|
*mapped = (FAR void *)(((FAR uint8_t *)addr) + offset);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_mmap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Equivalent to the standard mmap() function except that is accepts
|
|
|
|
* a struct file instance instead of a file descriptor and it does not set
|
|
|
|
* the errno variable.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_mmap(FAR struct file *filep, FAR void *start, size_t length,
|
|
|
|
int prot, int flags, off_t offset, FAR void **mapped)
|
|
|
|
{
|
|
|
|
return file_mmap_(filep, start, length,
|
|
|
|
prot, flags, offset, true, mapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX operates in a flat open address space. Therefore, it generally
|
|
|
|
* does not require mmap() functionality. There are two exceptions:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* start A hint at where to map the memory -- ignored. The address
|
|
|
|
* of the underlying media is fixed and cannot be re-mapped without
|
|
|
|
* MMU support.
|
|
|
|
* length The length of the mapping. For exception #1 above, this length
|
|
|
|
* ignored: The entire underlying media is always accessible.
|
|
|
|
* 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.
|
|
|
|
* MAP_SHARED - MAP_PRIVATE or MAP_SHARED required
|
|
|
|
* MAP_PRIVATE - MAP_PRIVATE or MAP_SHARED required
|
|
|
|
* MAP_FIXED - Will cause an error
|
|
|
|
* MAP_FILE - Ignored
|
|
|
|
* MAP_ANONYMOUS - Optional
|
|
|
|
* 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
|
|
|
|
* fd file descriptor of the backing file -- required.
|
|
|
|
* offset The offset into the file to map
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* EACCES
|
|
|
|
* The fd argument is not open for read, regardless of the
|
|
|
|
* protection specified, or fd is not open for write and PROT_WRITE
|
|
|
|
* was specified for a MAP_SHARED type mapping.
|
|
|
|
* 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.
|
|
|
|
* ENOMEM
|
|
|
|
* Insufficient memory is available to map the file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR void *mmap(FAR void *start, size_t length, int prot, int flags,
|
|
|
|
int fd, off_t offset)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
FAR void *mapped;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fs_getfilep(fd, &filep) < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Invalid file descriptor, fd=%d\n", fd);
|
|
|
|
ret = -EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = file_mmap_(filep, start, length,
|
|
|
|
prot, flags, offset, false, &mapped);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapped;
|
2019-03-14 18:20:14 +01:00
|
|
|
|
|
|
|
errout:
|
2020-05-02 16:30:58 +02:00
|
|
|
set_errno(-ret);
|
2019-03-14 18:20:14 +01:00
|
|
|
return MAP_FAILED;
|
2008-09-12 17:54:20 +02:00
|
|
|
}
|