1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2020 Dmitry Kozlyuk
8 #include <rte_eal_paging.h>
11 #include "eal_internal_cfg.h"
12 #include "eal_memalloc.h"
13 #include "eal_memcfg.h"
14 #include "eal_options.h"
15 #include "eal_private.h"
16 #include "eal_windows.h"
18 #include <rte_virt2phys.h>
20 /* MinGW-w64 headers lack VirtualAlloc2() in some distributions.
21 * Note: definitions are copied verbatim from Microsoft documentation
22 * and don't follow DPDK code style.
24 #ifndef MEM_EXTENDED_PARAMETER_TYPE_BITS
26 #define MEM_EXTENDED_PARAMETER_TYPE_BITS 4
28 /* https://docs.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-mem_extended_parameter_type */
29 typedef enum MEM_EXTENDED_PARAMETER_TYPE {
30 MemExtendedParameterInvalidType,
31 MemExtendedParameterAddressRequirements,
32 MemExtendedParameterNumaNode,
33 MemExtendedParameterPartitionHandle,
34 MemExtendedParameterUserPhysicalHandle,
35 MemExtendedParameterAttributeFlags,
36 MemExtendedParameterMax
37 } *PMEM_EXTENDED_PARAMETER_TYPE;
39 /* https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-mem_extended_parameter */
40 typedef struct MEM_EXTENDED_PARAMETER {
42 DWORD64 Type : MEM_EXTENDED_PARAMETER_TYPE_BITS;
43 DWORD64 Reserved : 64 - MEM_EXTENDED_PARAMETER_TYPE_BITS;
52 } MEM_EXTENDED_PARAMETER, *PMEM_EXTENDED_PARAMETER;
54 #endif /* defined(MEM_EXTENDED_PARAMETER_TYPE_BITS) */
56 /* https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc2 */
57 typedef PVOID (*VirtualAlloc2_type)(
63 MEM_EXTENDED_PARAMETER *ExtendedParameters,
67 /* MinGW-w64 distributions, even those that declare VirtualAlloc2(),
68 * lack it in import libraries, which results in a failure at link time.
69 * Link it dynamically in such case.
71 static VirtualAlloc2_type VirtualAlloc2_ptr;
73 #ifdef RTE_TOOLCHAIN_GCC
75 #define MEM_COALESCE_PLACEHOLDERS 0x00000001
76 #define MEM_PRESERVE_PLACEHOLDER 0x00000002
77 #define MEM_REPLACE_PLACEHOLDER 0x00004000
78 #define MEM_RESERVE_PLACEHOLDER 0x00040000
81 eal_mem_win32api_init(void)
83 /* Contrary to the docs, VirtualAlloc2() is not in kernel32.dll,
84 * see https://github.com/MicrosoftDocs/feedback/issues/1129.
86 static const char library_name[] = "kernelbase.dll";
87 static const char function[] = "VirtualAlloc2";
89 HMODULE library = NULL;
93 if (VirtualAlloc2_ptr != NULL)
96 library = LoadLibraryA(library_name);
97 if (library == NULL) {
98 RTE_LOG_WIN32_ERR("LoadLibraryA(\"%s\")", library_name);
102 VirtualAlloc2_ptr = (VirtualAlloc2_type)(
103 (void *)GetProcAddress(library, function));
104 if (VirtualAlloc2_ptr == NULL) {
105 RTE_LOG_WIN32_ERR("GetProcAddress(\"%s\", \"%s\")\n",
106 library_name, function);
108 /* Contrary to the docs, Server 2016 is not supported. */
109 RTE_LOG(ERR, EAL, "Windows 10 or Windows Server 2019 "
110 " is required for memory management\n");
114 FreeLibrary(library);
121 /* Stub in case VirtualAlloc2() is provided by the toolchain. */
123 eal_mem_win32api_init(void)
125 VirtualAlloc2_ptr = VirtualAlloc2;
129 #endif /* defined(RTE_TOOLCHAIN_GCC) */
131 static HANDLE virt2phys_device = INVALID_HANDLE_VALUE;
134 eal_mem_virt2iova_init(void)
136 HDEVINFO list = INVALID_HANDLE_VALUE;
137 SP_DEVICE_INTERFACE_DATA ifdata;
138 SP_DEVICE_INTERFACE_DETAIL_DATA *detail = NULL;
142 list = SetupDiGetClassDevs(
143 &GUID_DEVINTERFACE_VIRT2PHYS, NULL, NULL,
144 DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
145 if (list == INVALID_HANDLE_VALUE) {
146 RTE_LOG_WIN32_ERR("SetupDiGetClassDevs()");
150 ifdata.cbSize = sizeof(ifdata);
151 if (!SetupDiEnumDeviceInterfaces(
152 list, NULL, &GUID_DEVINTERFACE_VIRT2PHYS, 0, &ifdata)) {
153 RTE_LOG_WIN32_ERR("SetupDiEnumDeviceInterfaces()");
157 if (!SetupDiGetDeviceInterfaceDetail(
158 list, &ifdata, NULL, 0, &detail_size, NULL)) {
159 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
161 "SetupDiGetDeviceInterfaceDetail(probe)");
166 detail = malloc(detail_size);
167 if (detail == NULL) {
168 RTE_LOG(ERR, EAL, "Cannot allocate virt2phys "
169 "device interface detail data\n");
173 detail->cbSize = sizeof(*detail);
174 if (!SetupDiGetDeviceInterfaceDetail(
175 list, &ifdata, detail, detail_size, NULL, NULL)) {
176 RTE_LOG_WIN32_ERR("SetupDiGetDeviceInterfaceDetail(read)");
180 RTE_LOG(DEBUG, EAL, "Found virt2phys device: %s\n", detail->DevicePath);
182 virt2phys_device = CreateFile(
183 detail->DevicePath, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
184 if (virt2phys_device == INVALID_HANDLE_VALUE) {
185 RTE_LOG_WIN32_ERR("CreateFile()");
189 /* Indicate success. */
194 if (list != INVALID_HANDLE_VALUE)
195 SetupDiDestroyDeviceInfoList(list);
201 eal_mem_virt2iova_cleanup(void)
203 if (virt2phys_device != INVALID_HANDLE_VALUE)
204 CloseHandle(virt2phys_device);
208 rte_mem_virt2phy(const void *virt)
211 DWORD bytes_returned;
213 if (virt2phys_device == INVALID_HANDLE_VALUE)
214 return RTE_BAD_PHYS_ADDR;
216 if (!DeviceIoControl(
217 virt2phys_device, IOCTL_VIRT2PHYS_TRANSLATE,
218 &virt, sizeof(virt), &phys, sizeof(phys),
219 &bytes_returned, NULL)) {
220 RTE_LOG_WIN32_ERR("DeviceIoControl(IOCTL_VIRT2PHYS_TRANSLATE)");
221 return RTE_BAD_PHYS_ADDR;
224 return phys.QuadPart;
228 rte_mem_virt2iova(const void *virt)
232 if (rte_eal_iova_mode() == RTE_IOVA_VA)
233 return (rte_iova_t)virt;
235 phys = rte_mem_virt2phy(virt);
236 if (phys == RTE_BAD_PHYS_ADDR)
238 return (rte_iova_t)phys;
241 /* Always using physical addresses under Windows if they can be obtained. */
243 rte_eal_using_phys_addrs(void)
245 return virt2phys_device != INVALID_HANDLE_VALUE;
248 /* Approximate error mapping from VirtualAlloc2() to POSIX mmap(3). */
250 set_errno_from_win32_alloc_error(DWORD code)
257 case ERROR_INVALID_ADDRESS:
258 /* A valid requested address is not available. */
259 case ERROR_COMMITMENT_LIMIT:
260 /* May occur when committing regular memory. */
261 case ERROR_NO_SYSTEM_RESOURCES:
262 /* Occurs when the system runs out of hugepages. */
266 case ERROR_INVALID_PARAMETER:
274 eal_mem_reserve(void *requested_addr, size_t size, int flags)
279 /* Windows requires hugepages to be committed. */
280 if (flags & EAL_RESERVE_HUGEPAGES) {
285 process = GetCurrentProcess();
287 virt = VirtualAlloc2_ptr(process, requested_addr, size,
288 MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS,
291 DWORD err = GetLastError();
292 RTE_LOG_WIN32_ERR("VirtualAlloc2()");
293 set_errno_from_win32_alloc_error(err);
297 if ((flags & EAL_RESERVE_FORCE_ADDRESS) && (virt != requested_addr)) {
298 if (!VirtualFreeEx(process, virt, 0, MEM_RELEASE))
299 RTE_LOG_WIN32_ERR("VirtualFreeEx()");
308 eal_mem_alloc_socket(size_t size, int socket_id)
310 DWORD flags = MEM_RESERVE | MEM_COMMIT;
313 flags = MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES;
314 addr = VirtualAllocExNuma(GetCurrentProcess(), NULL, size, flags,
315 PAGE_READWRITE, eal_socket_numa_node(socket_id));
322 eal_mem_commit(void *requested_addr, size_t size, int socket_id)
325 MEM_EXTENDED_PARAMETER param;
326 DWORD param_count = 0;
330 process = GetCurrentProcess();
332 if (requested_addr != NULL) {
333 MEMORY_BASIC_INFORMATION info;
335 if (VirtualQueryEx(process, requested_addr, &info,
336 sizeof(info)) != sizeof(info)) {
337 RTE_LOG_WIN32_ERR("VirtualQuery(%p)", requested_addr);
341 /* Split reserved region if only a part is committed. */
342 flags = MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER;
343 if ((info.RegionSize > size) && !VirtualFreeEx(
344 process, requested_addr, size, flags)) {
346 "VirtualFreeEx(%p, %zu, preserve placeholder)",
347 requested_addr, size);
351 /* Temporarily release the region to be committed.
353 * There is an inherent race for this memory range
354 * if another thread allocates memory via OS API.
355 * However, VirtualAlloc2(MEM_REPLACE_PLACEHOLDER)
356 * doesn't work with MEM_LARGE_PAGES on Windows Server.
358 if (!VirtualFreeEx(process, requested_addr, 0, MEM_RELEASE)) {
359 RTE_LOG_WIN32_ERR("VirtualFreeEx(%p, 0, release)",
365 if (socket_id != SOCKET_ID_ANY) {
367 memset(¶m, 0, sizeof(param));
368 param.Type = MemExtendedParameterNumaNode;
369 param.ULong = eal_socket_numa_node(socket_id);
372 flags = MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES;
373 addr = VirtualAlloc2_ptr(process, requested_addr, size,
374 flags, PAGE_READWRITE, ¶m, param_count);
376 /* Logging may overwrite GetLastError() result. */
377 DWORD err = GetLastError();
378 RTE_LOG_WIN32_ERR("VirtualAlloc2(%p, %zu, commit large pages)",
379 requested_addr, size);
380 set_errno_from_win32_alloc_error(err);
384 if ((requested_addr != NULL) && (addr != requested_addr)) {
385 /* We lost the race for the requested_addr. */
386 if (!VirtualFreeEx(process, addr, 0, MEM_RELEASE))
387 RTE_LOG_WIN32_ERR("VirtualFreeEx(%p, release)", addr);
389 rte_errno = EADDRNOTAVAIL;
397 eal_mem_decommit(void *addr, size_t size)
403 process = GetCurrentProcess();
405 /* Hugepages cannot be decommited on Windows,
406 * so free them and replace the block with a placeholder.
407 * There is a race for VA in this block until VirtualAlloc2 call.
409 if (!VirtualFreeEx(process, addr, 0, MEM_RELEASE)) {
410 RTE_LOG_WIN32_ERR("VirtualFreeEx(%p, 0, release)", addr);
414 flags = MEM_RESERVE | MEM_RESERVE_PLACEHOLDER;
415 stub = VirtualAlloc2_ptr(
416 process, addr, size, flags, PAGE_NOACCESS, NULL, 0);
418 /* We lost the race for the VA. */
419 if (!VirtualFreeEx(process, stub, 0, MEM_RELEASE))
420 RTE_LOG_WIN32_ERR("VirtualFreeEx(%p, release)", stub);
421 rte_errno = EADDRNOTAVAIL;
425 /* No need to join reserved regions adjacent to the freed one:
426 * eal_mem_commit() will just pick up the page-size placeholder
433 * Free a reserved memory region in full or in part.
436 * Starting address of the area to free.
438 * Number of bytes to free. Must be a multiple of page size.
440 * Fail if the region is not in reserved state.
442 * * 0 on successful deallocation;
443 * * 1 if region must be in reserved state but it is not;
444 * * (-1) on system API failures.
447 mem_free(void *addr, size_t size, bool reserved)
449 MEMORY_BASIC_INFORMATION info;
452 process = GetCurrentProcess();
455 process, addr, &info, sizeof(info)) != sizeof(info)) {
456 RTE_LOG_WIN32_ERR("VirtualQueryEx(%p)", addr);
460 if (reserved && (info.State != MEM_RESERVE))
463 /* Free complete region. */
464 if ((addr == info.AllocationBase) && (size == info.RegionSize)) {
465 if (!VirtualFreeEx(process, addr, 0, MEM_RELEASE)) {
466 RTE_LOG_WIN32_ERR("VirtualFreeEx(%p, 0, release)",
472 /* Split the part to be freed and the remaining reservation. */
473 if (!VirtualFreeEx(process, addr, size,
474 MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)) {
476 "VirtualFreeEx(%p, %zu, preserve placeholder)",
481 /* Actually free reservation part. */
482 if (!VirtualFreeEx(process, addr, 0, MEM_RELEASE)) {
483 RTE_LOG_WIN32_ERR("VirtualFreeEx(%p, 0, release)", addr);
491 eal_mem_free(void *virt, size_t size)
493 mem_free(virt, size, false);
497 eal_mem_set_dump(void *virt, size_t size, bool dump)
503 /* Windows does not dump reserved memory by default.
505 * There is <werapi.h> to include or exclude regions from the dump,
506 * but this is not currently required by EAL.
514 rte_mem_map(void *requested_addr, size_t size, int prot, int flags,
515 int fd, uint64_t offset)
517 HANDLE file_handle = INVALID_HANDLE_VALUE;
518 HANDLE mapping_handle = INVALID_HANDLE_VALUE;
520 DWORD sys_access = 0;
521 DWORD size_high = (DWORD)(size >> 32);
522 DWORD size_low = (DWORD)size;
523 DWORD offset_high = (DWORD)(offset >> 32);
524 DWORD offset_low = (DWORD)offset;
527 if (prot & RTE_PROT_EXECUTE) {
528 if (prot & RTE_PROT_READ) {
529 sys_prot = PAGE_EXECUTE_READ;
530 sys_access = FILE_MAP_READ | FILE_MAP_EXECUTE;
532 if (prot & RTE_PROT_WRITE) {
533 sys_prot = PAGE_EXECUTE_READWRITE;
534 sys_access = FILE_MAP_WRITE | FILE_MAP_EXECUTE;
537 if (prot & RTE_PROT_READ) {
538 sys_prot = PAGE_READONLY;
539 sys_access = FILE_MAP_READ;
541 if (prot & RTE_PROT_WRITE) {
542 sys_prot = PAGE_READWRITE;
543 sys_access = FILE_MAP_WRITE;
547 if (flags & RTE_MAP_PRIVATE)
548 sys_access |= FILE_MAP_COPY;
550 if ((flags & RTE_MAP_ANONYMOUS) == 0)
551 file_handle = (HANDLE)_get_osfhandle(fd);
553 mapping_handle = CreateFileMapping(
554 file_handle, NULL, sys_prot, size_high, size_low, NULL);
555 if (mapping_handle == INVALID_HANDLE_VALUE) {
556 RTE_LOG_WIN32_ERR("CreateFileMapping()");
560 /* There is a race for the requested_addr between mem_free()
561 * and MapViewOfFileEx(). MapViewOfFile3() that can replace a reserved
562 * region with a mapping in a single operation, but it does not support
565 if (requested_addr != NULL) {
566 int ret = mem_free(requested_addr, size, true);
569 RTE_LOG(ERR, EAL, "Cannot map memory "
570 "to a region not reserved\n");
571 rte_errno = EADDRNOTAVAIL;
577 virt = MapViewOfFileEx(mapping_handle, sys_access,
578 offset_high, offset_low, size, requested_addr);
580 RTE_LOG_WIN32_ERR("MapViewOfFileEx()");
584 if ((flags & RTE_MAP_FORCE_ADDRESS) && (virt != requested_addr)) {
585 if (!UnmapViewOfFile(virt))
586 RTE_LOG_WIN32_ERR("UnmapViewOfFile()");
590 if (!CloseHandle(mapping_handle))
591 RTE_LOG_WIN32_ERR("CloseHandle()");
597 rte_mem_unmap(void *virt, size_t size)
601 if (!UnmapViewOfFile(virt)) {
602 RTE_LOG_WIN32_ERR("UnmapViewOfFile()");
610 eal_get_baseaddr(void)
612 /* Windows strategy for memory allocation is undocumented.
613 * Returning 0 here effectively disables address guessing
614 * unless user provides an address hint.
620 rte_mem_page_size(void)
622 static SYSTEM_INFO info;
624 if (info.dwPageSize == 0)
625 GetSystemInfo(&info);
627 return info.dwPageSize;
631 rte_mem_lock(const void *virt, size_t size)
633 /* VirtualLock() takes `void*`, work around compiler warning. */
634 void *addr = (void *)((uintptr_t)virt);
636 if (!VirtualLock(addr, size)) {
637 RTE_LOG_WIN32_ERR("VirtualLock(%p %#zx)", virt, size);
645 rte_eal_memseg_init(void)
647 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
648 EAL_LOG_NOT_IMPLEMENTED();
652 return eal_dynmem_memseg_lists_init();
656 eal_nohuge_init(void)
658 struct rte_mem_config *mcfg;
659 struct rte_memseg_list *msl;
661 uint64_t mem_sz, page_sz;
664 mcfg = rte_eal_get_configuration()->mem_config;
665 struct internal_config *internal_conf =
666 eal_get_internal_configuration();
668 /* nohuge mode is legacy mode */
669 internal_conf->legacy_mem = 1;
671 msl = &mcfg->memsegs[0];
673 mem_sz = internal_conf->memory;
674 page_sz = RTE_PGSIZE_4K;
675 n_segs = mem_sz / page_sz;
677 if (eal_memseg_list_init_named(
678 msl, "nohugemem", page_sz, n_segs, 0, true)) {
683 NULL, mem_sz, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
685 RTE_LOG_WIN32_ERR("VirtualAlloc(size=%#zx)", mem_sz);
686 RTE_LOG(ERR, EAL, "Cannot allocate memory\n");
693 eal_memseg_list_populate(msl, addr, n_segs);
695 if (mcfg->dma_maskbits &&
696 rte_mem_check_dma_mask_thread_unsafe(mcfg->dma_maskbits)) {
698 "%s(): couldn't allocate memory due to IOVA "
699 "exceeding limits of current DMA mask.\n", __func__);
707 rte_eal_hugepage_init(void)
709 const struct internal_config *internal_conf =
710 eal_get_internal_configuration();
712 return internal_conf->no_hugetlbfs ?
713 eal_nohuge_init() : eal_dynmem_hugepage_init();
717 rte_eal_hugepage_attach(void)
719 EAL_LOG_NOT_IMPLEMENTED();