mman.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef MMAN_H
  2. #define MMAN_H
  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <malloc.h>
  11. #include <switch.h>
  12. #include <stdlib.h>
  13. //#include "3ds_utils.h"
  14. #define PROT_READ 0b001
  15. #define PROT_WRITE 0b010
  16. #define PROT_EXEC 0b100
  17. #define MAP_PRIVATE 2
  18. #define MAP_FIXED 0x10
  19. #define MAP_ANONYMOUS 0x20
  20. #define MAP_FAILED ((void *)-1)
  21. static void *dynarec_cache = NULL;
  22. static void *dynarec_cache_mapping = NULL;
  23. static inline void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
  24. {
  25. (void)fd;
  26. (void)offset;
  27. //void* addr_out;
  28. Result rc = svcMapPhysicalMemory(addr, len);
  29. if (R_FAILED(rc))
  30. {
  31. printf("mmap failed\n");
  32. return malloc(len);
  33. }
  34. return addr;
  35. // if((prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) &&
  36. // (flags == (MAP_PRIVATE | MAP_ANONYMOUS)))
  37. // {
  38. // if(true)// __ctr_svchax)
  39. // {
  40. // /* this hack works only for pcsx_rearmed */
  41. // uint32_t currentHandle;
  42. //
  43. // if(!dynarec_cache)
  44. // dynarec_cache = memalign(0x1000, len);
  45. //
  46. // //svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  47. // //svcControlProcessMemory(currentHandle, addr, dynarec_cache,
  48. // // len, MEMOP_MAP, prot);
  49. // svcCloseHandle(currentHandle);
  50. // dynarec_cache_mapping = addr;
  51. // return addr;
  52. // }
  53. // else
  54. // {
  55. // printf("tried to mmap RWX pages without svcControlProcessMemory access !\n");
  56. // return MAP_FAILED;
  57. // }
  58. //
  59. // }
  60. // addr_out = memalign(0x1000, len);
  61. // if(!addr_out)
  62. // return MAP_FAILED;
  63. //
  64. // return addr_out;
  65. }
  66. static inline int mprotect(void *addr, size_t len, int prot)
  67. {
  68. return 0;
  69. //if(true) // __ctr_svchax)
  70. //{
  71. // uint32_t currentHandle;
  72. // //svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  73. // //svcControlProcessMemory(currentHandle, addr, NULL,
  74. // // len, MEMOP_PROT, prot);
  75. // svcCloseHandle(currentHandle);
  76. // return 0;
  77. //}
  78. //printf("mprotect called without svcControlProcessMemory access !\n");
  79. //return -1;
  80. }
  81. static inline int munmap(void *addr, size_t len)
  82. {
  83. Result rc = svcUnmapPhysicalMemory(addr, len);
  84. if (R_FAILED(rc))
  85. {
  86. printf("munmap failed\n");
  87. free(addr);
  88. }
  89. return 0;
  90. // if((addr == dynarec_cache_mapping) && true)//__ctr_svchax)
  91. // {
  92. // uint32_t currentHandle;
  93. // //svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  94. // //svcControlProcessMemory(currentHandle,
  95. // // dynarec_cache, dynarec_cache_mapping,
  96. // // len, MEMOP_UNMAP, 0b111);
  97. // svcCloseHandle(currentHandle);
  98. // dynarec_cache_mapping = NULL;
  99. //
  100. // }
  101. // else
  102. free(addr);
  103. return 0;
  104. }
  105. #ifdef __cplusplus
  106. };
  107. #endif
  108. #endif // MMAN_H