4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
36 #include "mempool_osdep.h"
37 #include <rte_errno.h>
39 #ifdef RTE_EXEC_ENV_LINUXAPP
46 #define PAGEMAP_FNAME "/proc/self/pagemap"
49 * the pfn (page frame number) are bits 0-54 (see pagemap.txt in linux
52 #define PAGEMAP_PFN_BITS 54
53 #define PAGEMAP_PFN_MASK RTE_LEN2MASK(PAGEMAP_PFN_BITS, phys_addr_t)
57 get_phys_map(void *va, phys_addr_t pa[], uint32_t pg_num, uint32_t pg_sz)
63 ofs = (uintptr_t)va / pg_sz * sizeof(*pa);
64 nb = pg_num * sizeof(*pa);
66 if ((fd = open(PAGEMAP_FNAME, O_RDONLY)) < 0)
69 if ((rc = pread(fd, pa, nb, ofs)) < 0 || (rc -= nb) != 0) {
71 RTE_LOG(ERR, USER1, "failed read of %u bytes from \'%s\' "
72 "at offset %zu, error code: %d\n",
73 nb, PAGEMAP_FNAME, (size_t)ofs, errno);
79 for (i = 0; i != pg_num; i++)
80 pa[i] = (pa[i] & PAGEMAP_PFN_MASK) * pg_sz;
86 mempool_anon_create(const char *name, unsigned elt_num, unsigned elt_size,
87 unsigned cache_size, unsigned private_data_size,
88 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
89 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
90 int socket_id, unsigned flags)
92 struct rte_mempool *mp;
95 uint32_t n, pg_num, pg_shift, pg_sz, total_size;
103 pg_sz = getpagesize();
104 if (rte_is_power_of_2(pg_sz) == 0) {
109 pg_shift = rte_bsf32(pg_sz);
111 total_size = rte_mempool_calc_obj_size(elt_size, flags, NULL);
113 /* calc max memory size and max number of pages needed. */
114 sz = rte_mempool_xmem_size(elt_num, total_size, pg_shift);
115 pg_num = sz >> pg_shift;
117 /* get chunk of virtually continuos memory.*/
118 if ((va = mmap(NULL, sz, PROT_READ | PROT_WRITE,
119 MAP_SHARED | MAP_ANONYMOUS | MAP_LOCKED,
120 -1, 0)) == MAP_FAILED) {
121 RTE_LOG(ERR, USER1, "%s(%s) failed mmap of %zu bytes, "
123 __func__, name, sz, errno);
128 /* extract physical mappings of the allocated memory. */
129 if ((pa = calloc(pg_num, sizeof (*pa))) != NULL &&
130 (rc = get_phys_map(va, pa, pg_num, pg_sz)) == 0) {
133 * Check that allocated size is big enough to hold elt_num
134 * objects and a calcualte how many bytes are actually required.
137 if ((usz = rte_mempool_xmem_usage(va, elt_num, total_size, pa,
138 pg_num, pg_shift)) < 0) {
142 RTE_LOG(ERR, USER1, "%s(%s) only %u objects from %u "
143 "requested can be created over "
144 "mmaped region %p of %zu bytes\n",
145 __func__, name, n, elt_num, va, sz);
148 /* unmap unused pages if any */
149 if ((size_t)usz < sz) {
155 "%s(%s): unmap unused %zu of %zu "
156 "mmaped bytes @%p\n",
157 __func__, name, (size_t)usz, sz, uv);
160 pg_num = sz >> pg_shift;
163 if ((mp = rte_mempool_xmem_create(name, elt_num,
164 elt_size, cache_size, private_data_size,
165 mp_init, mp_init_arg,
166 obj_init, obj_init_arg,
167 socket_id, flags, va, pa, pg_num,
170 RTE_VERIFY(elt_num == mp->size);
183 #else /* RTE_EXEC_ENV_LINUXAPP */
187 mempool_anon_create(__rte_unused const char *name,
188 __rte_unused unsigned elt_num, __rte_unused unsigned elt_size,
189 __rte_unused unsigned cache_size,
190 __rte_unused unsigned private_data_size,
191 __rte_unused rte_mempool_ctor_t *mp_init,
192 __rte_unused void *mp_init_arg,
193 __rte_unused rte_mempool_obj_ctor_t *obj_init,
194 __rte_unused void *obj_init_arg,
195 __rte_unused int socket_id, __rte_unused unsigned flags)
201 #endif /* RTE_EXEC_ENV_LINUXAPP */