| 1 | #ifndef MMAN_H |
| 2 | #define MMAN_H |
| 3 | |
| 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <3ds/svc.h> |
| 12 | #include "3ds_utils.h" |
| 13 | |
| 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 | |
| 21 | #define MAP_FAILED ((void *)-1) |
| 22 | |
| 23 | void SysPrintf(const char *fmt, ...); |
| 24 | |
| 25 | #if 0 // not used |
| 26 | static void* dynarec_cache = NULL; |
| 27 | static void* dynarec_cache_mapping = NULL; |
| 28 | |
| 29 | static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) |
| 30 | { |
| 31 | (void)fd; |
| 32 | (void)offset; |
| 33 | |
| 34 | void* addr_out; |
| 35 | |
| 36 | if((prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) && |
| 37 | (flags == (MAP_PRIVATE | MAP_ANONYMOUS))) |
| 38 | { |
| 39 | if(__ctr_svchax) |
| 40 | { |
| 41 | /* this hack works only for pcsx_rearmed */ |
| 42 | uint32_t currentHandle; |
| 43 | |
| 44 | if (!dynarec_cache) { |
| 45 | dynarec_cache = memalign(0x1000, len); |
| 46 | if (!dynarec_cache) |
| 47 | return MAP_FAILED; |
| 48 | } |
| 49 | |
| 50 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 51 | svcControlProcessMemory(currentHandle, (uintptr_t)addr, (uintptr_t)dynarec_cache, |
| 52 | len, MEMOP_MAP, prot); |
| 53 | svcCloseHandle(currentHandle); |
| 54 | dynarec_cache_mapping = addr; |
| 55 | memset(addr, 0, len); |
| 56 | return addr; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | printf("tried to mmap RWX pages without svcControlProcessMemory access !\n"); |
| 61 | return MAP_FAILED; |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | addr_out = memalign(0x1000, len); |
| 67 | if (!addr_out) |
| 68 | return MAP_FAILED; |
| 69 | |
| 70 | memset(addr_out, 0, len); |
| 71 | return addr_out; |
| 72 | } |
| 73 | |
| 74 | static inline int munmap(void *addr, size_t len) |
| 75 | { |
| 76 | if((addr == dynarec_cache_mapping) && __ctr_svchax) |
| 77 | { |
| 78 | uint32_t currentHandle; |
| 79 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 80 | svcControlProcessMemory(currentHandle, |
| 81 | (uintptr_t)dynarec_cache, (uintptr_t)dynarec_cache_mapping, |
| 82 | len, MEMOP_UNMAP, 0b111); |
| 83 | svcCloseHandle(currentHandle); |
| 84 | dynarec_cache_mapping = NULL; |
| 85 | |
| 86 | } |
| 87 | else |
| 88 | free(addr); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | static inline int mprotect(void *addr, size_t len, int prot) |
| 95 | { |
| 96 | if (__ctr_svchax) |
| 97 | { |
| 98 | uint32_t currentHandle = 0; |
| 99 | int r; |
| 100 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 101 | r = svcControlProcessMemory(currentHandle, (uintptr_t)addr, 0, |
| 102 | len, MEMOP_PROT, prot); |
| 103 | svcCloseHandle(currentHandle); |
| 104 | if (r < 0) { |
| 105 | SysPrintf("svcControlProcessMemory failed for %p %u %x: %d\n", |
| 106 | addr, len, prot, r); |
| 107 | return -1; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | SysPrintf("mprotect called without svcControlProcessMemory access!\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | #ifdef __cplusplus |
| 117 | }; |
| 118 | #endif |
| 119 | |
| 120 | #endif // MMAN_H |
| 121 | |