win_memmap.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. void * os_mmap(void *hint, size_t size, int prot, int flags)
  7. {
  8. DWORD AllocType = MEM_RESERVE | MEM_COMMIT;
  9. DWORD flProtect = PAGE_NOACCESS;
  10. size_t request_size, page_size;
  11. void *addr;
  12. page_size = getpagesize();
  13. request_size = (size + page_size - 1) & ~(page_size - 1);
  14. if (request_size < size)
  15. /* integer overflow */
  16. return NULL;
  17. if (request_size == 0)
  18. request_size = page_size;
  19. if (prot & MMAP_PROT_EXEC) {
  20. if (prot & MMAP_PROT_WRITE)
  21. flProtect = PAGE_EXECUTE_READWRITE;
  22. else
  23. flProtect = PAGE_EXECUTE_READ;
  24. }
  25. else if (prot & MMAP_PROT_WRITE)
  26. flProtect = PAGE_READWRITE;
  27. else if (prot & MMAP_PROT_READ)
  28. flProtect = PAGE_READONLY;
  29. addr = VirtualAlloc((LPVOID)hint, request_size, AllocType,
  30. flProtect);
  31. return addr;
  32. }
  33. void
  34. os_munmap(void *addr, size_t size)
  35. {
  36. size_t page_size = getpagesize();
  37. size_t request_size = (size + page_size - 1) & ~(page_size - 1);
  38. if (addr) {
  39. if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
  40. if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
  41. os_printf("os_munmap error addr:%p, size:0x%lx, errno:%d\n",
  42. addr, request_size, errno);
  43. }
  44. }
  45. }
  46. }
  47. int
  48. os_mprotect(void *addr, size_t size, int prot)
  49. {
  50. DWORD AllocType = MEM_RESERVE | MEM_COMMIT;
  51. DWORD flProtect = PAGE_NOACCESS;
  52. if (!addr)
  53. return 0;
  54. if (prot & MMAP_PROT_EXEC) {
  55. if (prot & MMAP_PROT_WRITE)
  56. flProtect = PAGE_EXECUTE_READWRITE;
  57. else
  58. flProtect = PAGE_EXECUTE_READ;
  59. }
  60. else if (prot & MMAP_PROT_WRITE)
  61. flProtect = PAGE_READWRITE;
  62. else if (prot & MMAP_PROT_READ)
  63. flProtect = PAGE_READONLY;
  64. return VirtualProtect((LPVOID)addr, size, flProtect, NULL);
  65. }
  66. void
  67. os_dcache_flush(void)
  68. {
  69. }