win_memmap.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_vmcore.h"
  6. #define TRACE_MEMMAP 0
  7. static DWORD
  8. access_to_win32_flags(int prot)
  9. {
  10. DWORD protect = PAGE_NOACCESS;
  11. if (prot & MMAP_PROT_EXEC) {
  12. if (prot & MMAP_PROT_WRITE)
  13. protect = PAGE_EXECUTE_READWRITE;
  14. else
  15. protect = PAGE_EXECUTE_READ;
  16. }
  17. else if (prot & MMAP_PROT_WRITE) {
  18. protect = PAGE_READWRITE;
  19. }
  20. else if (prot & MMAP_PROT_READ) {
  21. protect = PAGE_READONLY;
  22. }
  23. return protect;
  24. }
  25. void *
  26. os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
  27. {
  28. DWORD alloc_type = MEM_RESERVE;
  29. DWORD protect;
  30. size_t request_size, page_size;
  31. void *addr;
  32. page_size = os_getpagesize();
  33. request_size = (size + page_size - 1) & ~(page_size - 1);
  34. if (request_size < size)
  35. /* integer overflow */
  36. return NULL;
  37. #if WASM_ENABLE_JIT != 0
  38. /**
  39. * Allocate memory at the highest possible address if the
  40. * request size is large, or LLVM JIT might report error:
  41. * IMAGE_REL_AMD64_ADDR32NB relocation requires an ordered
  42. * section layout.
  43. */
  44. if (request_size > 10 * BH_MB)
  45. alloc_type |= MEM_TOP_DOWN;
  46. #endif
  47. protect = access_to_win32_flags(prot);
  48. if (protect != PAGE_NOACCESS) {
  49. alloc_type |= MEM_COMMIT;
  50. }
  51. addr = VirtualAlloc((LPVOID)hint, request_size, alloc_type, protect);
  52. #if TRACE_MEMMAP != 0
  53. printf("Map memory, request_size: %zu, alloc_type: 0x%x, "
  54. "protect: 0x%x, ret: %p\n",
  55. request_size, alloc_type, protect, addr);
  56. #endif
  57. return addr;
  58. }
  59. void
  60. os_munmap(void *addr, size_t size)
  61. {
  62. size_t page_size = os_getpagesize();
  63. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  64. if (addr) {
  65. if (!VirtualFree(addr, request_size, MEM_DECOMMIT)) {
  66. printf("warning: os_munmap decommit pages failed, "
  67. "addr: %p, request_size: %zu, errno: %d\n",
  68. addr, request_size, errno);
  69. return;
  70. }
  71. if (!VirtualFree(addr, 0, MEM_RELEASE)) {
  72. printf("warning: os_munmap release pages failed, "
  73. "addr: %p, size: %zu, errno:%d\n",
  74. addr, request_size, errno);
  75. }
  76. }
  77. #if TRACE_MEMMAP != 0
  78. printf("Unmap memory, addr: %p, request_size: %zu\n", addr, request_size);
  79. #endif
  80. }
  81. void *
  82. os_mem_commit(void *addr, size_t size, int flags)
  83. {
  84. DWORD protect = access_to_win32_flags(flags);
  85. size_t page_size = os_getpagesize();
  86. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  87. if (!addr)
  88. return NULL;
  89. #if TRACE_MEMMAP != 0
  90. printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", addr,
  91. request_size, protect);
  92. #endif
  93. return VirtualAlloc((LPVOID)addr, request_size, MEM_COMMIT, protect);
  94. }
  95. void
  96. os_mem_decommit(void *addr, size_t size)
  97. {
  98. size_t page_size = os_getpagesize();
  99. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  100. if (!addr)
  101. return;
  102. #if TRACE_MEMMAP != 0
  103. printf("Decommit memory, addr: %p, request_size: %zu\n", addr,
  104. request_size);
  105. #endif
  106. VirtualFree((LPVOID)addr, request_size, MEM_DECOMMIT);
  107. }
  108. int
  109. os_mprotect(void *addr, size_t size, int prot)
  110. {
  111. DWORD protect;
  112. size_t page_size = os_getpagesize();
  113. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  114. if (!addr)
  115. return 0;
  116. protect = access_to_win32_flags(prot);
  117. #if TRACE_MEMMAP != 0
  118. printf("Mprotect memory, addr: %p, request_size: %zu, protect: 0x%x\n",
  119. addr, request_size, protect);
  120. #endif
  121. return VirtualProtect((LPVOID)addr, request_size, protect, NULL);
  122. }