14 #include "3ds_utils.h"
16 #define PROT_READ 0b001
17 #define PROT_WRITE 0b010
18 #define PROT_EXEC 0b100
20 #define MAP_FIXED 0x10
21 #define MAP_ANONYMOUS 0x20
23 #define MAP_FAILED ((void *)-1)
25 static void* dynarec_cache = NULL;
26 static void* dynarec_cache_mapping = NULL;
28 static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
35 if((prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) &&
36 (flags == (MAP_PRIVATE | MAP_ANONYMOUS)))
40 /* this hack works only for pcsx_rearmed */
41 uint32_t currentHandle;
44 dynarec_cache = memalign(0x1000, len);
49 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
50 svcControlProcessMemory(currentHandle, addr, dynarec_cache,
51 len, MEMOP_MAP, prot);
52 svcCloseHandle(currentHandle);
53 dynarec_cache_mapping = addr;
59 printf("tried to mmap RWX pages without svcControlProcessMemory access !\n");
65 addr_out = memalign(0x1000, len);
69 memset(addr_out, 0, len);
73 static inline int mprotect(void *addr, size_t len, int prot)
77 uint32_t currentHandle;
78 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
79 svcControlProcessMemory(currentHandle, addr, NULL,
80 len, MEMOP_PROT, prot);
81 svcCloseHandle(currentHandle);
85 printf("mprotect called without svcControlProcessMemory access !\n");
89 static inline int munmap(void *addr, size_t len)
91 if((addr == dynarec_cache_mapping) && __ctr_svchax)
93 uint32_t currentHandle;
94 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
95 svcControlProcessMemory(currentHandle,
96 dynarec_cache, dynarec_cache_mapping,
97 len, MEMOP_UNMAP, 0b111);
98 svcCloseHandle(currentHandle);
99 dynarec_cache_mapping = NULL;