199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2020 Dmitry Kozlyuk 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #include <stdint.h> 699a2dd95SBruce Richardson 799a2dd95SBruce Richardson #include <rte_compat.h> 899a2dd95SBruce Richardson 999a2dd95SBruce Richardson /** 1099a2dd95SBruce Richardson * @file 1199a2dd95SBruce Richardson * @internal 1299a2dd95SBruce Richardson * 1399a2dd95SBruce Richardson * Wrappers for OS facilities related to memory paging, used across DPDK. 1499a2dd95SBruce Richardson */ 1599a2dd95SBruce Richardson 1699a2dd95SBruce Richardson /** Memory protection flags. */ 1799a2dd95SBruce Richardson enum rte_mem_prot { 1899a2dd95SBruce Richardson RTE_PROT_READ = 1 << 0, /**< Read access. */ 1999a2dd95SBruce Richardson RTE_PROT_WRITE = 1 << 1, /**< Write access. */ 2099a2dd95SBruce Richardson RTE_PROT_EXECUTE = 1 << 2 /**< Code execution. */ 2199a2dd95SBruce Richardson }; 2299a2dd95SBruce Richardson 2399a2dd95SBruce Richardson /** Additional flags for memory mapping. */ 2499a2dd95SBruce Richardson enum rte_map_flags { 2599a2dd95SBruce Richardson /** Changes to the mapped memory are visible to other processes. */ 2699a2dd95SBruce Richardson RTE_MAP_SHARED = 1 << 0, 2799a2dd95SBruce Richardson /** Mapping is not backed by a regular file. */ 2899a2dd95SBruce Richardson RTE_MAP_ANONYMOUS = 1 << 1, 2999a2dd95SBruce Richardson /** Copy-on-write mapping, changes are invisible to other processes. */ 3099a2dd95SBruce Richardson RTE_MAP_PRIVATE = 1 << 2, 3199a2dd95SBruce Richardson /** 3299a2dd95SBruce Richardson * Force mapping to the requested address. This flag should be used 3399a2dd95SBruce Richardson * with caution, because to fulfill the request implementation 3499a2dd95SBruce Richardson * may remove all other mappings in the requested region. However, 3599a2dd95SBruce Richardson * it is not required to do so, thus mapping with this flag may fail. 3699a2dd95SBruce Richardson */ 3799a2dd95SBruce Richardson RTE_MAP_FORCE_ADDRESS = 1 << 3 3899a2dd95SBruce Richardson }; 3999a2dd95SBruce Richardson 4099a2dd95SBruce Richardson /** 4199a2dd95SBruce Richardson * Map a portion of an opened file or the page file into memory. 4299a2dd95SBruce Richardson * 4399a2dd95SBruce Richardson * This function is similar to POSIX mmap(3) with common MAP_ANONYMOUS 4499a2dd95SBruce Richardson * extension, except for the return value. 4599a2dd95SBruce Richardson * 4699a2dd95SBruce Richardson * @param requested_addr 4799a2dd95SBruce Richardson * Desired virtual address for mapping. Can be NULL to let OS choose. 4899a2dd95SBruce Richardson * @param size 4999a2dd95SBruce Richardson * Size of the mapping in bytes. 5099a2dd95SBruce Richardson * @param prot 5199a2dd95SBruce Richardson * Protection flags, a combination of rte_mem_prot values. 5299a2dd95SBruce Richardson * @param flags 5399a2dd95SBruce Richardson * Additional mapping flags, a combination of rte_map_flags. 5499a2dd95SBruce Richardson * @param fd 5599a2dd95SBruce Richardson * Mapped file descriptor. Can be negative for anonymous mapping. 5699a2dd95SBruce Richardson * @param offset 5799a2dd95SBruce Richardson * Offset of the mapped region in fd. Must be 0 for anonymous mappings. 5899a2dd95SBruce Richardson * @return 5999a2dd95SBruce Richardson * Mapped address or NULL on failure and rte_errno is set to OS error. 6099a2dd95SBruce Richardson */ 6199a2dd95SBruce Richardson __rte_internal 6299a2dd95SBruce Richardson void * 6399a2dd95SBruce Richardson rte_mem_map(void *requested_addr, size_t size, int prot, int flags, 64*6beb2d29SLance Richardson int fd, uint64_t offset); 6599a2dd95SBruce Richardson 6699a2dd95SBruce Richardson /** 6799a2dd95SBruce Richardson * OS-independent implementation of POSIX munmap(3). 6899a2dd95SBruce Richardson */ 6999a2dd95SBruce Richardson __rte_internal 7099a2dd95SBruce Richardson int 7199a2dd95SBruce Richardson rte_mem_unmap(void *virt, size_t size); 7299a2dd95SBruce Richardson 7399a2dd95SBruce Richardson /** 7499a2dd95SBruce Richardson * Get system page size. This function never fails. 7599a2dd95SBruce Richardson * 7699a2dd95SBruce Richardson * @return 7799a2dd95SBruce Richardson * Page size in bytes. 7899a2dd95SBruce Richardson */ 7999a2dd95SBruce Richardson __rte_internal 8099a2dd95SBruce Richardson size_t 8199a2dd95SBruce Richardson rte_mem_page_size(void); 8299a2dd95SBruce Richardson 8399a2dd95SBruce Richardson /** 8499a2dd95SBruce Richardson * Lock in physical memory all pages crossed by the address region. 8599a2dd95SBruce Richardson * 8699a2dd95SBruce Richardson * @param virt 8799a2dd95SBruce Richardson * Base virtual address of the region. 8899a2dd95SBruce Richardson * @param size 8999a2dd95SBruce Richardson * Size of the region. 9099a2dd95SBruce Richardson * @return 9199a2dd95SBruce Richardson * 0 on success, negative on error. 9299a2dd95SBruce Richardson * 9399a2dd95SBruce Richardson * @see rte_mem_page_size() to retrieve the page size. 9499a2dd95SBruce Richardson * @see rte_mem_lock_page() to lock an entire single page. 9599a2dd95SBruce Richardson */ 9699a2dd95SBruce Richardson __rte_internal 9799a2dd95SBruce Richardson int 9899a2dd95SBruce Richardson rte_mem_lock(const void *virt, size_t size); 99