ee5cc2da99dad4eb6a99aeca56e4a125676bc409
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_xen_memory.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <sys/mman.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/queue.h>
46 #include <sys/file.h>
47 #include <unistd.h>
48 #include <limits.h>
49 #include <errno.h>
50 #include <sys/ioctl.h>
51 #include <sys/time.h>
52
53 #include <rte_log.h>
54 #include <rte_memory.h>
55 #include <rte_memzone.h>
56 #include <rte_launch.h>
57 #include <rte_tailq.h>
58 #include <rte_eal.h>
59 #include <rte_eal_memconfig.h>
60 #include <rte_per_lcore.h>
61 #include <rte_lcore.h>
62 #include <rte_common.h>
63 #include <rte_string_fns.h>
64
65 #include "eal_private.h"
66 #include "eal_internal_cfg.h"
67 #include "eal_filesystem.h"
68 #include <exec-env/rte_dom0_common.h>
69
70 #define PAGE_SIZE RTE_PGSIZE_4K
71 #define DEFAUL_DOM0_NAME "dom0-mem"
72
73 static int xen_fd = -1;
74 static const char sys_dir_path[] = "/sys/kernel/mm/dom0-mm/memsize-mB";
75
76 /*
77  * Try to mmap *size bytes in /dev/zero. If it is successful, return the
78  * pointer to the mmap'd area and keep *size unmodified. Else, retry
79  * with a smaller zone: decrease *size by mem_size until it reaches
80  * 0. In this case, return NULL. Note: this function returns an address
81  * which is a multiple of mem_size size.
82  */
83 static void *
84 xen_get_virtual_area(size_t *size, size_t mem_size)
85 {
86         void *addr;
87         int fd;
88         long aligned_addr;
89
90         RTE_LOG(INFO, EAL, "Ask a virtual area of 0x%zu bytes\n", *size);
91
92         fd = open("/dev/zero", O_RDONLY);
93         if (fd < 0){
94                 RTE_LOG(ERR, EAL, "Cannot open /dev/zero\n");
95                 return NULL;
96         }
97         do {
98                 addr = mmap(NULL, (*size) + mem_size, PROT_READ,
99                         MAP_PRIVATE, fd, 0);
100                 if (addr == MAP_FAILED)
101                         *size -= mem_size;
102         } while (addr == MAP_FAILED && *size > 0);
103
104         if (addr == MAP_FAILED) {
105                 close(fd);
106                 RTE_LOG(INFO, EAL, "Cannot get a virtual area\n");
107                 return NULL;
108         }
109
110         munmap(addr, (*size) + mem_size);
111         close(fd);
112
113         /* align addr to a mem_size boundary */
114         aligned_addr = (uintptr_t)addr;
115         aligned_addr = RTE_ALIGN_CEIL(aligned_addr, mem_size);
116         addr = (void *)(aligned_addr);
117
118         RTE_LOG(INFO, EAL, "Virtual area found at %p (size = 0x%zx)\n",
119                 addr, *size);
120
121         return addr;
122 }
123
124 /**
125  * Get memory size configuration from /sys/devices/virtual/misc/dom0_mm
126  * /memsize-mB/memsize file, and the size unit is mB.
127  */
128 static int
129 get_xen_memory_size(void)
130 {
131         char path[PATH_MAX];
132         unsigned long mem_size = 0;
133         static const char *file_name;
134
135         file_name = "memsize";
136         snprintf(path, sizeof(path), "%s/%s",
137                         sys_dir_path, file_name);
138
139         if (eal_parse_sysfs_value(path, &mem_size) < 0)
140                 return -1;
141
142         if (mem_size == 0)
143                 rte_exit(EXIT_FAILURE,"XEN-DOM0:the %s/%s was not"
144                         " configured.\n",sys_dir_path, file_name);
145         if (mem_size % 2)
146                 rte_exit(EXIT_FAILURE,"XEN-DOM0:the %s/%s must be"
147                         " even number.\n",sys_dir_path, file_name);
148
149         if (mem_size > DOM0_CONFIG_MEMSIZE)
150                 rte_exit(EXIT_FAILURE,"XEN-DOM0:the %s/%s should not be larger"
151                         " than %d mB\n",sys_dir_path, file_name, DOM0_CONFIG_MEMSIZE);
152
153         return mem_size;
154 }
155
156 /**
157  * Based on physical address to caculate MFN in Xen Dom0.
158  */
159 phys_addr_t
160 rte_mem_phy2mch(uint32_t memseg_id, const phys_addr_t phy_addr)
161 {
162         int mfn_id;
163         uint64_t mfn, mfn_offset;
164         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
165         struct rte_memseg *memseg = mcfg->memseg;
166
167         mfn_id = (phy_addr - memseg[memseg_id].phys_addr) / RTE_PGSIZE_2M;
168
169         /*the MFN is contiguous in 2M */
170         mfn_offset = (phy_addr - memseg[memseg_id].phys_addr) %
171                                         RTE_PGSIZE_2M / PAGE_SIZE;
172         mfn = mfn_offset + memseg[memseg_id].mfn[mfn_id];
173
174         /** return mechine address */
175         return (mfn * PAGE_SIZE + phy_addr % PAGE_SIZE);
176 }
177
178 int
179 rte_xen_dom0_memory_init(void)
180 {
181         void *vir_addr, *vma_addr = NULL;
182         int err, ret = 0;
183         uint32_t i, requested, mem_size, memseg_idx, num_memseg = 0;
184         size_t vma_len = 0;
185         struct memory_info meminfo;
186         struct memseg_info seginfo[RTE_MAX_MEMSEG];
187         int flags, page_size = getpagesize();
188         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
189         struct rte_memseg *memseg = mcfg->memseg;
190         uint64_t total_mem = internal_config.memory;
191
192         memset(seginfo, 0, sizeof(seginfo));
193         memset(&meminfo, 0, sizeof(struct memory_info));
194
195         mem_size = get_xen_memory_size();
196         requested = (unsigned) (total_mem / 0x100000);
197         if (requested > mem_size)
198                 /* if we didn't satisfy total memory requirements */
199                 rte_exit(EXIT_FAILURE,"Not enough memory available! Requested: %uMB,"
200                                 " available: %uMB\n", requested, mem_size);
201         else if (total_mem != 0)
202                 mem_size = requested;
203
204         /* Check FD and open once */
205         if (xen_fd < 0) {
206                 xen_fd = open(DOM0_MM_DEV, O_RDWR);
207                 if (xen_fd < 0) {
208                         RTE_LOG(ERR, EAL, "Can not open %s\n",DOM0_MM_DEV);
209                         return -1;
210                 }
211         }
212
213         meminfo.size = mem_size;
214
215         /* construct memory mangement name for Dom0 */
216         snprintf(meminfo.name, DOM0_NAME_MAX, "%s-%s",
217                 internal_config.hugefile_prefix, DEFAUL_DOM0_NAME);
218
219         /* Notify kernel driver to allocate memory */
220         ret = ioctl(xen_fd, RTE_DOM0_IOCTL_PREPARE_MEMSEG, &meminfo);
221         if (ret < 0) {
222                 RTE_LOG(ERR, EAL, "XEN DOM0:failed to get memory\n");
223                 err = -EIO;
224                 goto fail;
225         }
226
227         /* Get number of memory segment from driver */
228         ret = ioctl(xen_fd, RTE_DOM0_IOCTL_GET_NUM_MEMSEG, &num_memseg);
229         if (ret < 0) {
230                 RTE_LOG(ERR, EAL, "XEN DOM0:failed to get memseg count.\n");
231                 err = -EIO;
232                 goto fail;
233         }
234
235         if(num_memseg > RTE_MAX_MEMSEG){
236                 RTE_LOG(ERR, EAL, "XEN DOM0: the memseg count %d is greater"
237                         " than max memseg %d.\n",num_memseg, RTE_MAX_MEMSEG);
238                 err = -EIO;
239                 goto fail;
240         }
241
242         /* get all memory segements information */
243         ret = ioctl(xen_fd, RTE_DOM0_IOCTL_GET_MEMSEG_INFO, seginfo);
244         if (ret < 0) {
245                 RTE_LOG(ERR, EAL, "XEN DOM0:failed to get memseg info.\n");
246                 err = -EIO;
247                 goto fail;
248         }
249
250         /* map all memory segments to contiguous user space */
251         for (memseg_idx = 0; memseg_idx < num_memseg; memseg_idx++)
252         {
253                 vma_len = seginfo[memseg_idx].size;
254
255                 /**
256                  * get the biggest virtual memory area up to vma_len. If it fails,
257                  * vma_addr is NULL, so let the kernel provide the address.
258                  */
259                 vma_addr = xen_get_virtual_area(&vma_len, RTE_PGSIZE_2M);
260                 if (vma_addr == NULL) {
261                         flags = MAP_SHARED;
262                         vma_len = RTE_PGSIZE_2M;
263                 } else
264                         flags = MAP_SHARED | MAP_FIXED;
265
266                 seginfo[memseg_idx].size = vma_len;
267                 vir_addr = mmap(vma_addr, seginfo[memseg_idx].size,
268                         PROT_READ|PROT_WRITE, flags, xen_fd,
269                         memseg_idx * page_size);
270                 if (vir_addr == MAP_FAILED) {
271                         RTE_LOG(ERR, EAL, "XEN DOM0:Could not mmap %s\n",
272                                 DOM0_MM_DEV);
273                         err = -EIO;
274                         goto fail;
275                 }
276
277                 memseg[memseg_idx].addr = vir_addr;
278                 memseg[memseg_idx].phys_addr = page_size *
279                         seginfo[memseg_idx].pfn ;
280                 memseg[memseg_idx].len = seginfo[memseg_idx].size;
281                 for ( i = 0; i < seginfo[memseg_idx].size / RTE_PGSIZE_2M; i++)
282                         memseg[memseg_idx].mfn[i] = seginfo[memseg_idx].mfn[i];
283
284                 /* MFNs are continuous in 2M, so assume that page size is 2M */
285                 memseg[memseg_idx].hugepage_sz = RTE_PGSIZE_2M;
286
287                 memseg[memseg_idx].nchannel = mcfg->nchannel;
288                 memseg[memseg_idx].nrank = mcfg->nrank;
289
290                 /* NUMA is not suppoted in Xen Dom0, so only set socket 0*/
291                 memseg[memseg_idx].socket_id = 0;
292         }
293
294         return 0;
295 fail:
296         if (xen_fd > 0) {
297                 close(xen_fd);
298                 xen_fd = -1;
299         }
300         return err;
301 }
302
303 /*
304  * This creates the memory mappings in the secondary process to match that of
305  * the server process. It goes through each memory segment in the DPDK runtime
306  * configuration, mapping them in order to form a contiguous block in the
307  * virtual memory space
308  */
309 int
310 rte_xen_dom0_memory_attach(void)
311 {
312         const struct rte_mem_config *mcfg;
313         unsigned s = 0; /* s used to track the segment number */
314         int xen_fd = -1;
315         int ret = -1;
316         void *vir_addr;
317         char name[DOM0_NAME_MAX] = {0};
318         int page_size = getpagesize();
319
320         mcfg = rte_eal_get_configuration()->mem_config;
321
322         /* Check FD and open once */
323         if (xen_fd < 0) {
324                 xen_fd = open(DOM0_MM_DEV, O_RDWR);
325                 if (xen_fd < 0) {
326                         RTE_LOG(ERR, EAL, "Can not open %s\n",DOM0_MM_DEV);
327                         goto error;
328                 }
329         }
330
331         /* construct memory mangement name for Dom0 */
332         snprintf(name, DOM0_NAME_MAX, "%s-%s",
333                 internal_config.hugefile_prefix, DEFAUL_DOM0_NAME);
334         /* attach to memory segments of primary process */
335         ret = ioctl(xen_fd, RTE_DOM0_IOCTL_ATTACH_TO_MEMSEG, name);
336         if (ret) {
337                 RTE_LOG(ERR, EAL,"attach memory segments fail.\n");
338                 goto error;
339         }
340
341         /* map all segments into memory to make sure we get the addrs */
342         for (s = 0; s < RTE_MAX_MEMSEG; ++s) {
343
344                 /*
345                  * the first memory segment with len==0 is the one that
346                  * follows the last valid segment.
347                  */
348                 if (mcfg->memseg[s].len == 0)
349                         break;
350
351                 vir_addr = mmap(mcfg->memseg[s].addr, mcfg->memseg[s].len,
352                                 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, xen_fd,
353                                 s * page_size);
354                 if (vir_addr == MAP_FAILED) {
355                         RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
356                                 "in %s to requested address [%p]\n",
357                                 (unsigned long long)mcfg->memseg[s].len, DOM0_MM_DEV,
358                                 mcfg->memseg[s].addr);
359                         goto error;
360                 }
361         }
362         return 0;
363
364 error:
365         if (xen_fd >= 0) {
366                 close(xen_fd);
367                 xen_fd = -1;
368         }
369         return -1;
370 }