| 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 | void* linearMemAlign(size_t size, size_t alignment); |
| 12 | void linearFree(void* mem); |
| 13 | |
| 14 | int32_t svcDuplicateHandle(uint32_t* out, uint32_t original); |
| 15 | int32_t svcCloseHandle(uint32_t handle); |
| 16 | int32_t svcControlMemory(void* addr_out, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); |
| 17 | int32_t svcControlProcessMemory(uint32_t process, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); |
| 18 | |
| 19 | int32_t threadCreate(void *(*entrypoint)(void*), void* arg, size_t stack_size, int32_t prio, int32_t affinity, bool detached); |
| 20 | int32_t threadJoin(int32_t thread, int64_t timeout_ns); |
| 21 | void threadFree(int32_t thread); |
| 22 | void threadExit(int32_t rc) __attribute__((noreturn)); |
| 23 | |
| 24 | int32_t svcBackdoor(int32_t (*callback)(void)); |
| 25 | |
| 26 | #define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0) |
| 27 | |
| 28 | void wait_for_input(void); |
| 29 | |
| 30 | extern __attribute__((weak)) int __ctr_svchax; |
| 31 | |
| 32 | typedef int32_t (*ctr_callback_type)(void); |
| 33 | |
| 34 | static inline void ctr_invalidate_ICache_kernel(void) |
| 35 | { |
| 36 | __asm__ volatile( |
| 37 | "cpsid aif\n\t" |
| 38 | "mov r0, #0\n\t" |
| 39 | "mcr p15, 0, r0, c7, c5, 0\n\t"); |
| 40 | } |
| 41 | |
| 42 | static inline void ctr_flush_DCache_kernel(void) |
| 43 | { |
| 44 | __asm__ volatile( |
| 45 | "cpsid aif\n\t" |
| 46 | "mov r0, #0\n\t" |
| 47 | "mcr p15, 0, r0, c7, c10, 0\n\t"); |
| 48 | } |
| 49 | |
| 50 | static inline void ctr_invalidate_ICache(void) |
| 51 | { |
| 52 | svcBackdoor((ctr_callback_type)ctr_invalidate_ICache_kernel); |
| 53 | } |
| 54 | |
| 55 | static inline void ctr_flush_DCache(void) |
| 56 | { |
| 57 | svcBackdoor((ctr_callback_type)ctr_flush_DCache_kernel); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static inline void ctr_flush_invalidate_cache(void) |
| 62 | { |
| 63 | ctr_flush_DCache(); |
| 64 | ctr_invalidate_ICache(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | #endif // _3DS_UTILS_H |