mirror of
https://github.com/apache/nuttx.git
synced 2026-06-04 14:53:47 +08:00
fs: Implement file_mmap and file_munmap API
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
committed by
Masayuki Ishikawa
parent
2b0b298ab6
commit
8612af4ae5
+150
-116
@@ -37,10 +37,156 @@
|
||||
#include "inode/inode.h"
|
||||
#include "fs_rammap.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: file_mmap_
|
||||
****************************************************************************/
|
||||
|
||||
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)
|
||||
{
|
||||
FAR void *addr;
|
||||
int ret;
|
||||
|
||||
/* Since only a tiny subset of mmap() functionality, we have to verify many
|
||||
* things.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_DEBUG_FEATURES
|
||||
/* Fixed mappings and protections are not currently supported. These
|
||||
* options could be supported in the KERNEL build with an MMU, but that
|
||||
* logic is not in place.
|
||||
*/
|
||||
|
||||
if ((flags & (MAP_FIXED | MAP_DENYWRITE)) != 0)
|
||||
{
|
||||
ferr("ERROR: Unsupported options, prot=%x flags=%04x\n", prot, flags);
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
#ifndef CONFIG_FS_RAMMAP
|
||||
if ((flags & MAP_PRIVATE) != 0)
|
||||
{
|
||||
ferr("ERROR: MAP_PRIVATE is not supported without file mapping"
|
||||
"emulation\n");
|
||||
return -ENOSYS;
|
||||
}
|
||||
#endif /* CONFIG_FS_RAMMAP */
|
||||
|
||||
/* A length of 0 is invalid. */
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
ferr("ERROR: Invalid length, length=%zu\n", length);
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif /* CONFIG_DEBUG_FEATURES */
|
||||
|
||||
if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE &&
|
||||
(flags & MAP_SHARED) != 0)
|
||||
{
|
||||
ferr("ERROR: Unsupported options for read-only file descriptor,"
|
||||
"prot=%x flags=%04x\n", prot, flags);
|
||||
return -EACCES;
|
||||
}
|
||||
|
||||
if ((filep->f_oflags & O_RDOK) == 0)
|
||||
{
|
||||
ferr("ERROR: File descriptor does not have read permission\n");
|
||||
return -EACCES;
|
||||
}
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
*mapped = kernel ? kmm_zalloc(length) : kumm_zalloc(length);
|
||||
if (*mapped == NULL)
|
||||
{
|
||||
ferr("ERROR: kumm_alloc() failed: %d\n", ret);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
return rammap(filep, length, offset, kernel, mapped);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Perform the ioctl to get the base address of the file in 'mapped'
|
||||
* in memory. (casting to uintptr_t first eliminates complaints on some
|
||||
* architectures where the sizeof long is different from the size of
|
||||
* a pointer).
|
||||
*/
|
||||
|
||||
ret = file_ioctl(filep, FIOC_MMAP, (unsigned long)((uintptr_t)&addr));
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Not directly mappable, probably because the underlying media does
|
||||
* not support random access.
|
||||
*/
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
||||
return rammap(filep, length, offset, kernel, mapped);
|
||||
#else
|
||||
ferr("ERROR: file_ioctl(FIOC_MMAP) failed: %d\n", ret);
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Return the offset address */
|
||||
|
||||
*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
|
||||
*
|
||||
@@ -118,47 +264,10 @@
|
||||
FAR void *mmap(FAR void *start, size_t length, int prot, int flags,
|
||||
int fd, off_t offset)
|
||||
{
|
||||
FAR void *addr;
|
||||
FAR struct file *filep;
|
||||
FAR void *mapped;
|
||||
int ret;
|
||||
|
||||
/* Since only a tiny subset of mmap() functionality, we have to verify many
|
||||
* things.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_DEBUG_FEATURES
|
||||
/* Fixed mappings and protections are not currently supported. These
|
||||
* options could be supported in the KERNEL build with an MMU, but that
|
||||
* logic is not in place.
|
||||
*/
|
||||
|
||||
if ((flags & (MAP_FIXED | MAP_DENYWRITE)) != 0)
|
||||
{
|
||||
ferr("ERROR: Unsupported options, prot=%x flags=%04x\n", prot, flags);
|
||||
ret = -ENOSYS;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
#ifndef CONFIG_FS_RAMMAP
|
||||
if ((flags & MAP_PRIVATE) != 0)
|
||||
{
|
||||
ferr("ERROR: MAP_PRIVATE is not supported without file mapping"
|
||||
"emulation\n");
|
||||
ret = -ENOSYS;
|
||||
goto errout;
|
||||
}
|
||||
#endif /* CONFIG_FS_RAMMAP */
|
||||
|
||||
/* A length of 0 is invalid. */
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
ferr("ERROR: Invalid length, length=%zu\n", length);
|
||||
ret = -EINVAL;
|
||||
goto errout;
|
||||
}
|
||||
#endif /* CONFIG_DEBUG_FEATURES */
|
||||
|
||||
if (fs_getfilep(fd, &filep) < 0)
|
||||
{
|
||||
ferr("ERROR: Invalid file descriptor, fd=%d\n", fd);
|
||||
@@ -166,89 +275,14 @@ FAR void *mmap(FAR void *start, size_t length, int prot, int flags,
|
||||
goto errout;
|
||||
}
|
||||
|
||||
if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE &&
|
||||
(flags & MAP_SHARED) != 0)
|
||||
{
|
||||
ferr("ERROR: Unsupported options for read-only file descriptor,"
|
||||
"fd=%d prot=%x flags=%04x\n", fd, prot, flags);
|
||||
ret = -EACCES;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
if ((filep->f_oflags & O_RDOK) == 0)
|
||||
{
|
||||
ferr("ERROR: File descriptor does not have read permission,"
|
||||
"fd=%d\n", fd);
|
||||
ret = -EACCES;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
ret = -ENOMEM;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
return alloc;
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
return rammap(fd, length, offset);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Perform the ioctl to get the base address of the file in 'mapped'
|
||||
* in memory. (casting to uintptr_t first eliminates complaints on some
|
||||
* architectures where the sizeof long is different from the size of
|
||||
* a pointer).
|
||||
*/
|
||||
|
||||
ret = nx_ioctl(fd, FIOC_MMAP, (unsigned long)((uintptr_t)&addr));
|
||||
ret = file_mmap_(filep, start, length,
|
||||
prot, flags, offset, false, &mapped);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Not directly mappable, probably because the underlying media does
|
||||
* not support random access.
|
||||
*/
|
||||
|
||||
#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.
|
||||
*/
|
||||
|
||||
return rammap(fd, length, offset);
|
||||
#else
|
||||
ferr("ERROR: nx_ioctl(FIOC_MMAP) failed: %d\n", ret);
|
||||
goto errout;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Return the offset address */
|
||||
|
||||
return (FAR void *)(((FAR uint8_t *)addr) + offset);
|
||||
return mapped;
|
||||
|
||||
errout:
|
||||
set_errno(-ret);
|
||||
|
||||
Reference in New Issue
Block a user