* @brief Maps a file or device into memory. * * This system call implements the `mmap2` system call, which maps a file or device into memory. * It provides a way for processes to access file contents directly in memory, bypassing * the need for explicit read or write operations. This function supports advanced memory * mapping options such as shared or private mappings, specific protection fl
| 2004 | * @see mmap(), munmap(), msync() |
| 2005 | */ |
| 2006 | void *sys_mmap2(void *addr, size_t length, int prot, |
| 2007 | int flags, int fd, size_t pgoffset) |
| 2008 | { |
| 2009 | sysret_t rc = 0; |
| 2010 | long offset = 0; |
| 2011 | |
| 2012 | /* aligned for user addr */ |
| 2013 | if ((rt_base_t)addr & ARCH_PAGE_MASK) |
| 2014 | { |
| 2015 | if (flags & MAP_FIXED) |
| 2016 | rc = -EINVAL; |
| 2017 | else |
| 2018 | { |
| 2019 | offset = (char *)addr - (char *)RT_ALIGN_DOWN((rt_base_t)addr, ARCH_PAGE_SIZE); |
| 2020 | length += offset; |
| 2021 | addr = (void *)RT_ALIGN_DOWN((rt_base_t)addr, ARCH_PAGE_SIZE); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | if (rc == 0) |
| 2026 | { |
| 2027 | /* fix parameter passing (both along have same effect) */ |
| 2028 | if (fd == -1 || flags & MAP_ANONYMOUS) |
| 2029 | { |
| 2030 | fd = -1; |
| 2031 | /* MAP_SHARED has no effect and treated as nothing */ |
| 2032 | flags &= ~MAP_SHARED; |
| 2033 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
| 2034 | } |
| 2035 | rc = (sysret_t)lwp_mmap2(lwp_self(), addr, length, prot, flags, fd, pgoffset); |
| 2036 | } |
| 2037 | |
| 2038 | return rc < 0 ? (char *)rc : (char *)rc + offset; |
| 2039 | } |
| 2040 | |
| 2041 | /** |
| 2042 | * @brief Unmaps a memory region previously mapped with mmap or mmap2. |