ed98e70e9ee3d500795619ba72e29b35e53ea9ab
[dpdk.git] / lib / eal / include / rte_eal_paging.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Dmitry Kozlyuk
3  */
4
5 #include <stdint.h>
6
7 #include <rte_compat.h>
8
9 /**
10  * @file
11  * @internal
12  *
13  * Wrappers for OS facilities related to memory paging, used across DPDK.
14  */
15
16 /** Memory protection flags. */
17 enum rte_mem_prot {
18         RTE_PROT_READ = 1 << 0,   /**< Read access. */
19         RTE_PROT_WRITE = 1 << 1,  /**< Write access. */
20         RTE_PROT_EXECUTE = 1 << 2 /**< Code execution. */
21 };
22
23 /** Additional flags for memory mapping. */
24 enum rte_map_flags {
25         /** Changes to the mapped memory are visible to other processes. */
26         RTE_MAP_SHARED = 1 << 0,
27         /** Mapping is not backed by a regular file. */
28         RTE_MAP_ANONYMOUS = 1 << 1,
29         /** Copy-on-write mapping, changes are invisible to other processes. */
30         RTE_MAP_PRIVATE = 1 << 2,
31         /**
32          * Force mapping to the requested address. This flag should be used
33          * with caution, because to fulfill the request implementation
34          * may remove all other mappings in the requested region. However,
35          * it is not required to do so, thus mapping with this flag may fail.
36          */
37         RTE_MAP_FORCE_ADDRESS = 1 << 3
38 };
39
40 /**
41  * Map a portion of an opened file or the page file into memory.
42  *
43  * This function is similar to POSIX mmap(3) with common MAP_ANONYMOUS
44  * extension, except for the return value.
45  *
46  * @param requested_addr
47  *  Desired virtual address for mapping. Can be NULL to let OS choose.
48  * @param size
49  *  Size of the mapping in bytes.
50  * @param prot
51  *  Protection flags, a combination of rte_mem_prot values.
52  * @param flags
53  *  Additional mapping flags, a combination of rte_map_flags.
54  * @param fd
55  *  Mapped file descriptor. Can be negative for anonymous mapping.
56  * @param offset
57  *  Offset of the mapped region in fd. Must be 0 for anonymous mappings.
58  * @return
59  *  Mapped address or NULL on failure and rte_errno is set to OS error.
60  */
61 __rte_internal
62 void *
63 rte_mem_map(void *requested_addr, size_t size, int prot, int flags,
64         int fd, size_t offset);
65
66 /**
67  * OS-independent implementation of POSIX munmap(3).
68  */
69 __rte_internal
70 int
71 rte_mem_unmap(void *virt, size_t size);
72
73 /**
74  * Get system page size. This function never fails.
75  *
76  * @return
77  *   Page size in bytes.
78  */
79 __rte_internal
80 size_t
81 rte_mem_page_size(void);
82
83 /**
84  * Lock in physical memory all pages crossed by the address region.
85  *
86  * @param virt
87  *   Base virtual address of the region.
88  * @param size
89  *   Size of the region.
90  * @return
91  *   0 on success, negative on error.
92  *
93  * @see rte_mem_page_size() to retrieve the page size.
94  * @see rte_mem_lock_page() to lock an entire single page.
95  */
96 __rte_internal
97 int
98 rte_mem_lock(const void *virt, size_t size);