win_memmap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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)
  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. protect = access_to_win32_flags(prot);
  38. if (protect != PAGE_NOACCESS) {
  39. alloc_type |= MEM_COMMIT;
  40. }
  41. addr = VirtualAlloc((LPVOID)hint, request_size, alloc_type, protect);
  42. #if TRACE_MEMMAP != 0
  43. printf("Map memory, request_size: %zu, alloc_type: 0x%x, "
  44. "protect: 0x%x, ret: %p\n",
  45. request_size, alloc_type, protect, addr);
  46. #endif
  47. return addr;
  48. }
  49. void
  50. os_munmap(void *addr, size_t size)
  51. {
  52. size_t page_size = os_getpagesize();
  53. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  54. if (addr) {
  55. if (!VirtualFree(addr, request_size, MEM_DECOMMIT)) {
  56. printf("warning: os_munmap decommit pages failed, "
  57. "addr: %p, request_size: %zu, errno: %d\n",
  58. addr, request_size, errno);
  59. return;
  60. }
  61. if (!VirtualFree(addr, 0, MEM_RELEASE)) {
  62. printf("warning: os_munmap release pages failed, "
  63. "addr: %p, size: %zu, errno:%d\n",
  64. addr, request_size, errno);
  65. }
  66. }
  67. #if TRACE_MEMMAP != 0
  68. printf("Unmap memory, addr: %p, request_size: %zu\n", addr, request_size);
  69. #endif
  70. }
  71. void *
  72. os_mem_commit(void *addr, size_t size, int flags)
  73. {
  74. DWORD protect = access_to_win32_flags(flags);
  75. size_t page_size = os_getpagesize();
  76. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  77. if (!addr)
  78. return NULL;
  79. #if TRACE_MEMMAP != 0
  80. printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", addr,
  81. request_size, protect);
  82. #endif
  83. return VirtualAlloc((LPVOID)addr, request_size, MEM_COMMIT, protect);
  84. }
  85. void
  86. os_mem_decommit(void *addr, size_t size)
  87. {
  88. size_t page_size = os_getpagesize();
  89. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  90. if (!addr)
  91. return;
  92. #if TRACE_MEMMAP != 0
  93. printf("Decommit memory, addr: %p, request_size: %zu\n", addr,
  94. request_size);
  95. #endif
  96. VirtualFree((LPVOID)addr, request_size, MEM_DECOMMIT);
  97. }
  98. int
  99. os_mprotect(void *addr, size_t size, int prot)
  100. {
  101. DWORD protect;
  102. size_t page_size = os_getpagesize();
  103. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  104. if (!addr)
  105. return 0;
  106. protect = access_to_win32_flags(prot);
  107. #if TRACE_MEMMAP != 0
  108. printf("Mprotect memory, addr: %p, request_size: %zu, protect: 0x%x\n",
  109. addr, request_size, protect);
  110. #endif
  111. return VirtualProtect((LPVOID)addr, request_size, protect, NULL);
  112. }