| 1 | #ifndef _3DS_UTILS_H |
| 2 | #define _3DS_UTILS_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #define MEMOP_PROT 6 |
| 7 | #define MEMOP_MAP 4 |
| 8 | #define MEMOP_UNMAP 5 |
| 9 | |
| 10 | void* linearMemAlign(size_t size, size_t alignment); |
| 11 | void linearFree(void* mem); |
| 12 | |
| 13 | int32_t svcDuplicateHandle(uint32_t* out, uint32_t original); |
| 14 | int32_t svcCloseHandle(uint32_t handle); |
| 15 | int32_t svcControlMemory(void* addr_out, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); |
| 16 | int32_t svcControlProcessMemory(uint32_t process, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm); |
| 17 | |
| 18 | int32_t svcCreateThread(int32_t* thread, void *(*entrypoint)(void*), void* arg, void* stack_top, int32_t thread_priority, int32_t processor_id); |
| 19 | int32_t svcWaitSynchronization(int32_t handle, int64_t nanoseconds); |
| 20 | void svcExitThread(void) __attribute__((noreturn)); |
| 21 | |
| 22 | int32_t svcBackdoor(int32_t (*callback)(void)); |
| 23 | |
| 24 | #define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0) |
| 25 | |
| 26 | void wait_for_input(void); |
| 27 | |
| 28 | extern __attribute__((weak)) int __ctr_svchax; |
| 29 | |
| 30 | typedef int32_t (*ctr_callback_type)(void); |
| 31 | |
| 32 | static inline void ctr_invalidate_ICache_kernel(void) |
| 33 | { |
| 34 | __asm__ volatile( |
| 35 | "cpsid aif\n\t" |
| 36 | "mov r0, #0\n\t" |
| 37 | "mcr p15, 0, r0, c7, c5, 0\n\t"); |
| 38 | } |
| 39 | |
| 40 | static inline void ctr_flush_DCache_kernel(void) |
| 41 | { |
| 42 | __asm__ volatile( |
| 43 | "cpsid aif\n\t" |
| 44 | "mov r0, #0\n\t" |
| 45 | "mcr p15, 0, r0, c7, c10, 0\n\t"); |
| 46 | } |
| 47 | |
| 48 | static inline void ctr_invalidate_ICache(void) |
| 49 | { |
| 50 | svcBackdoor((ctr_callback_type)ctr_invalidate_ICache_kernel); |
| 51 | } |
| 52 | |
| 53 | static inline void ctr_flush_DCache(void) |
| 54 | { |
| 55 | svcBackdoor((ctr_callback_type)ctr_flush_DCache_kernel); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static inline void ctr_flush_invalidate_cache(void) |
| 60 | { |
| 61 | ctr_flush_DCache(); |
| 62 | ctr_invalidate_ICache(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | #endif // _3DS_UTILS_H |