virtual_memory.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include "virtual_memory.hpp"
  16. #include <stdexcept>
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #else
  20. #ifdef __APPLE__
  21. #include <mach/vm_statistics.h>
  22. #endif
  23. #include <sys/types.h>
  24. #include <sys/mman.h>
  25. #ifndef MAP_ANONYMOUS
  26. #define MAP_ANONYMOUS MAP_ANON
  27. #endif
  28. #endif
  29. #ifdef _WIN32
  30. std::string getErrorMessage(const char* function) {
  31. LPSTR messageBuffer = nullptr;
  32. size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  33. NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
  34. std::string message(messageBuffer, size);
  35. LocalFree(messageBuffer);
  36. return std::string(function) + std::string(": ") + message;
  37. }
  38. void setPrivilege(const char* pszPrivilege, BOOL bEnable) {
  39. HANDLE hToken;
  40. TOKEN_PRIVILEGES tp;
  41. BOOL status;
  42. DWORD error;
  43. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  44. throw std::runtime_error(getErrorMessage("OpenProcessToken"));
  45. if (!LookupPrivilegeValue(NULL, pszPrivilege, &tp.Privileges[0].Luid))
  46. throw std::runtime_error(getErrorMessage("LookupPrivilegeValue"));
  47. tp.PrivilegeCount = 1;
  48. if (bEnable)
  49. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  50. else
  51. tp.Privileges[0].Attributes = 0;
  52. status = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  53. error = GetLastError();
  54. if (!status || (error != ERROR_SUCCESS))
  55. throw std::runtime_error(getErrorMessage("AdjustTokenPrivileges"));
  56. if (!CloseHandle(hToken))
  57. throw std::runtime_error(getErrorMessage("CloseHandle"));
  58. }
  59. #endif
  60. void* allocExecutableMemory(std::size_t bytes) {
  61. void* mem;
  62. #ifdef _WIN32
  63. mem = VirtualAlloc(nullptr, bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  64. if (mem == nullptr)
  65. throw std::runtime_error(getErrorMessage("allocExecutableMemory - VirtualAlloc"));
  66. #else
  67. mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  68. if (mem == MAP_FAILED)
  69. throw std::runtime_error("allocExecutableMemory - mmap failed");
  70. #endif
  71. return mem;
  72. }
  73. constexpr std::size_t align(std::size_t pos, std::size_t align) {
  74. return ((pos - 1) / align + 1) * align;
  75. }
  76. void* allocLargePagesMemory(std::size_t bytes) {
  77. void* mem;
  78. #ifdef _WIN32
  79. setPrivilege("SeLockMemoryPrivilege", 1);
  80. auto pageMinimum = GetLargePageMinimum();
  81. if (pageMinimum > 0)
  82. mem = VirtualAlloc(NULL, align(bytes, pageMinimum), MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
  83. else
  84. throw std::runtime_error("allocLargePagesMemory - Large pages are not supported");
  85. if (mem == nullptr)
  86. throw std::runtime_error(getErrorMessage("allocLargePagesMemory - VirtualAlloc"));
  87. #else
  88. #ifdef __APPLE__
  89. mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
  90. #else
  91. mem = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0);
  92. #endif
  93. if (mem == MAP_FAILED)
  94. throw std::runtime_error("allocLargePagesMemory - mmap failed");
  95. #endif
  96. return mem;
  97. }
  98. void freePagedMemory(void* ptr, std::size_t bytes) {
  99. #ifdef _WIN32
  100. VirtualFree(ptr, 0, MEM_RELEASE);
  101. #else
  102. munmap(ptr, bytes);
  103. #endif
  104. }