13 #include "3ds_utils.h"
15 #define PROT_READ 0b001
16 #define PROT_WRITE 0b010
17 #define PROT_EXEC 0b100
19 #define MAP_FIXED 0x10
20 #define MAP_ANONYMOUS 0x20
22 #define MAP_FAILED ((void *)-1)
24 static void* dynarec_cache = NULL;
25 static void* dynarec_cache_mapping = NULL;
27 static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
34 if((prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) &&
35 (flags == (MAP_PRIVATE | MAP_ANONYMOUS)))
39 /* this hack works only for pcsx_rearmed */
40 uint32_t currentHandle;
43 dynarec_cache = memalign(0x1000, len);
45 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
46 svcControlProcessMemory(currentHandle, addr, dynarec_cache,
47 len, MEMOP_MAP, prot);
48 svcCloseHandle(currentHandle);
49 dynarec_cache_mapping = addr;
54 printf("tried to mmap RWX pages without svcControlProcessMemory access !\n");
60 addr_out = malloc(len);
67 static inline int mprotect(void *addr, size_t len, int prot)
71 uint32_t currentHandle;
72 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
73 svcControlProcessMemory(currentHandle, addr, NULL,
74 len, MEMOP_PROT, prot);
75 svcCloseHandle(currentHandle);
79 printf("mprotect called without svcControlProcessMemory access !\n");
83 static inline int munmap(void *addr, size_t len)
85 if((addr == dynarec_cache_mapping) && __ctr_svchax)
87 uint32_t currentHandle;
88 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
89 svcControlProcessMemory(currentHandle,
90 dynarec_cache, dynarec_cache_mapping,
91 len, MEMOP_UNMAP, 0b111);
92 svcCloseHandle(currentHandle);
93 dynarec_cache_mapping = NULL;