11f5520497d7cd72e03edf2638010fe4dffe108d
[picodrive.git] / platform / libretro / 3ds / 3ds_utils.c
1
2 #include "3ds_utils.h"
3
4 #define GET_VERSION_MAJOR(version)    ((version) >>24)
5
6 typedef int (*ctr_callback_type)(void);
7
8 int srvGetServiceHandle(unsigned int* out, const char* name);
9 int svcCloseHandle(unsigned int handle);
10 int svcBackdoor(ctr_callback_type);
11 int32_t svcGetSystemInfo(int64_t* out, uint32_t type, int32_t param);
12 void ctr_clear_cache(void);
13
14 static int has_rosalina;
15
16 void check_rosalina(void) {
17   int64_t version;
18   uint32_t major;
19
20   has_rosalina = 0;
21
22   if (!svcGetSystemInfo(&version, 0x10000, 0)) {
23      major = GET_VERSION_MAJOR(version);
24
25      if (major >= 8)
26        has_rosalina = 1;
27   }
28 }
29
30 static void ctr_enable_all_svc_kernel(void)
31 {
32    __asm__ volatile("cpsid aif");
33
34    unsigned int*  svc_access_control = *(*(unsigned int***)0xFFFF9000 + 0x22) - 0x6;
35
36    svc_access_control[0]=0xFFFFFFFE;
37    svc_access_control[1]=0xFFFFFFFF;
38    svc_access_control[2]=0xFFFFFFFF;
39    svc_access_control[3]=0x3FFFFFFF;
40 }
41
42 static void ctr_enable_all_svc(void)
43 {
44    svcBackdoor((ctr_callback_type)ctr_enable_all_svc_kernel);
45 }
46
47 static void ctr_clean_invalidate_kernel(void)
48 {
49    __asm__ volatile(
50       "mrs r1, cpsr\n"
51       "cpsid aif\n"                  // disable interrupts
52       "mov r0, #0\n"
53       "mcr p15, 0, r0, c7, c10, 0\n" // clean dcache
54       "mcr p15, 0, r0, c7, c10, 4\n" // DSB
55       "mcr p15, 0, r0, c7, c5, 0\n"  // invalidate icache+BTAC
56       "msr cpsr_cx, r1\n"            // restore interrupts
57       ::: "r0", "r1");
58 }
59
60 void ctr_flush_invalidate_cache(void)
61 {
62   if (has_rosalina) {
63     ctr_clear_cache();
64    } else {
65     //   __asm__ volatile("svc 0x2E\n\t");
66     //   __asm__ volatile("svc 0x4B\n\t");
67     svcBackdoor((ctr_callback_type)ctr_clean_invalidate_kernel);
68    }
69 }
70
71 int ctr_svchack_init(void)
72 {
73    extern unsigned int __ctr_svchax;
74    extern unsigned int __service_ptr;
75
76    if(__ctr_svchax)
77       return 1; /* All services have already been enabled */
78
79    if(__service_ptr)
80       return 0;
81
82    /* CFW */
83    ctr_enable_all_svc();
84    return 1;
85 }
86