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