| 1 | #ifndef _3DS_UTILS_H |
| 2 | #define _3DS_UTILS_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdbool.h> |
| 6 | |
| 7 | #define MEMOP_PROT 6 |
| 8 | #define MEMOP_MAP 4 |
| 9 | #define MEMOP_UNMAP 5 |
| 10 | |
| 11 | #define GET_VERSION_MAJOR(version) ((version) >>24) |
| 12 | |
| 13 | void* linearMemAlign(size_t size, size_t alignment); |
| 14 | void linearFree(void* mem); |
| 15 | |
| 16 | int32_t svcDuplicateHandle(uint32_t* out, uint32_t original); |
| 17 | int32_t svcCloseHandle(uint32_t handle); |
| 18 | int32_t svcControlMemory(void* addr_out, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); |
| 19 | int32_t svcControlProcessMemory(uint32_t process, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); |
| 20 | |
| 21 | int32_t threadCreate(void *(*entrypoint)(void*), void* arg, size_t stack_size, int32_t prio, int32_t affinity, bool detached); |
| 22 | int32_t threadJoin(int32_t thread, int64_t timeout_ns); |
| 23 | void threadFree(int32_t thread); |
| 24 | void threadExit(int32_t rc) __attribute__((noreturn)); |
| 25 | |
| 26 | int32_t svcGetSystemInfo(int64_t* out, uint32_t type, int32_t param); |
| 27 | |
| 28 | int32_t svcCreateSemaphore(uint32_t *sem, int32_t initial_count, uint32_t max_count); |
| 29 | int32_t svcReleaseSemaphore(int32_t *count, uint32_t sem, int32_t release_count); |
| 30 | int32_t svcWaitSynchronization(uint32_t handle, int64_t nanoseconds); |
| 31 | |
| 32 | typedef int32_t LightLock; |
| 33 | |
| 34 | void LightLock_Init(LightLock* lock); |
| 35 | void LightLock_Lock(LightLock* lock); |
| 36 | int LightLock_TryLock(LightLock* lock); |
| 37 | void LightLock_Unlock(LightLock* lock); |
| 38 | |
| 39 | int32_t APT_CheckNew3DS(bool *out); |
| 40 | |
| 41 | int32_t svcBackdoor(int32_t (*callback)(void)); |
| 42 | |
| 43 | #define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0) |
| 44 | |
| 45 | void wait_for_input(void); |
| 46 | |
| 47 | extern __attribute__((weak)) int __ctr_svchax; |
| 48 | |
| 49 | bool has_rosalina; |
| 50 | |
| 51 | static void check_rosalina() { |
| 52 | int64_t version; |
| 53 | uint32_t major; |
| 54 | |
| 55 | has_rosalina = false; |
| 56 | |
| 57 | if (!svcGetSystemInfo(&version, 0x10000, 0)) { |
| 58 | major = GET_VERSION_MAJOR(version); |
| 59 | |
| 60 | if (major >= 8) |
| 61 | has_rosalina = true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void ctr_clear_cache(void); |
| 66 | |
| 67 | typedef int32_t (*ctr_callback_type)(void); |
| 68 | |
| 69 | static inline void ctr_invalidate_ICache_kernel(void) |
| 70 | { |
| 71 | __asm__ volatile( |
| 72 | "cpsid aif\n\t" |
| 73 | "mov r0, #0\n\t" |
| 74 | "mcr p15, 0, r0, c7, c5, 0\n\t"); |
| 75 | } |
| 76 | |
| 77 | static inline void ctr_flush_DCache_kernel(void) |
| 78 | { |
| 79 | __asm__ volatile( |
| 80 | "cpsid aif\n\t" |
| 81 | "mov r0, #0\n\t" |
| 82 | "mcr p15, 0, r0, c7, c10, 0\n\t"); |
| 83 | } |
| 84 | |
| 85 | static inline void ctr_invalidate_ICache(void) |
| 86 | { |
| 87 | svcBackdoor((ctr_callback_type)ctr_invalidate_ICache_kernel); |
| 88 | } |
| 89 | |
| 90 | static inline void ctr_flush_DCache(void) |
| 91 | { |
| 92 | svcBackdoor((ctr_callback_type)ctr_flush_DCache_kernel); |
| 93 | } |
| 94 | |
| 95 | static inline void ctr_flush_invalidate_cache(void) |
| 96 | { |
| 97 | if (has_rosalina) { |
| 98 | ctr_clear_cache(); |
| 99 | } else { |
| 100 | ctr_flush_DCache(); |
| 101 | ctr_invalidate_ICache(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #endif // _3DS_UTILS_H |