99b42e0568acbc2b34cd8c7bd983988605ff0064
[ginge.git] / loader / patches.c
1 /*
2  * GINGE - GINGE Is Not Gp2x Emulator
3  * (C) notaz, 2010-2011,2016
4  *
5  * This work is licensed under the MAME license, see COPYING file for details.
6  */
7 #include <stdio.h>
8
9 #include "header.h"
10 #include "sys_cacheflush.h"
11
12 #include "override.c"
13
14 // note: first mask int must be always full for the search algo
15 static const unsigned int sig_mask_all[] = {
16   0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
17   0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
18 };
19
20 static const unsigned int sig_open[] = {
21   0xe59cc000, // ldr ip, [ip]
22   0xe33c0000, // teq ip, #0
23   0x1a000003, // bne 0x1c
24   0xef900005, // svc 0x900005
25 };
26 #define sig_mask_open sig_mask_all
27
28 static const unsigned int sig_open_a1[] = {
29   0xef900005, // svc 0x900005
30   0xe1a0f00e, // mov pc, lr
31 };
32 #define sig_mask_open_a1 sig_mask_all
33
34 static const unsigned int sig_mmap[] = {
35   0xe92d000f, // push {r0, r1, r2, r3}
36   0xe1a0000d, // mov  r0, sp
37   0xef90005a, // svc  0x90005a
38   0xe28dd010, // add  sp, sp, #16
39 };
40 #define sig_mask_mmap sig_mask_all
41
42 static const unsigned int sig_munmap[] = {
43   0xef90005b, // svc  0x90005b
44   0xe3700a01, // cmn  r0, #0x1000
45   0x312fff1e, // bxcc lr
46 };
47 #define sig_mask_munmap sig_mask_all
48
49 static const unsigned int sig_mmap2[] = {
50   0xe52d5004, // push {r5}
51   0xe59d5008, // ldr  r5, [sp, #8]
52   0xe52d4004, // push {r4}
53   0xe59d4008, // ldr  r4, [sp, #8]
54   0xe1b0ca05, // lsls ip, r5, #20
55   0x1a000006, // bne  0x34
56   0xe1a05625, // lsr  r5, r5, #12
57   0xef9000c0, // svc  0x9000c0
58 };
59 #define sig_mask_mmap2 sig_mask_all
60
61 static const unsigned int sig_read[] = {
62   0xe59fc080, // ldr ip, [pc, #128]
63   0xe59cc000, // ldr ip, [ip]
64   0xe33c0000, // teq ip, #0
65   0x1a000003, // bne 0x20
66   0xef900003, // svc 0x900003
67 };
68 #define sig_mask_read sig_mask_all
69
70 static const unsigned int sig_read_a1[] = {
71   0xef900003, // svc  0x900003
72   0xe3700a01, // cmn  r0, #0x1000
73   0x312fff1e, // bxcc lr
74 };
75 #define sig_mask_read_a1 sig_mask_all
76
77 static const unsigned int sig_hw_read[] = {
78   0xef900003, // svc  0x900003
79   0xe3700a01, // cmn  r0, #0x1000
80   0xe1a04000, // mov  r4, r0
81 };
82 #define sig_mask_hw_read sig_mask_all
83
84 static const unsigned int sig_ioctl[] = {
85   0xef900036, // svc  0x900036
86   0xe3700a01, // cmn  r0, #0x1000
87   0x312fff1e, // bxcc lr
88 };
89 #define sig_mask_ioctl sig_mask_all
90
91 static const unsigned int sig_hw_ioctl[] = {
92   0xef900036, // svc  0x900036
93   0xe3700a01, // cmn  r0, #0x1000
94   0xe1a04000, // mov  r4, r0
95 };
96 #define sig_mask_hw_ioctl sig_mask_all
97
98 static const unsigned int sig_sigaction[] = {
99   0xe59f300c, //    ldr r3, [pc, #12]
100   0xe3530000, //    cmp r3, #0
101   0x0a000000, //    beq 0f
102   0xea000000, //    b   *
103   0xea000000, // 0: b   *
104 };
105 static const unsigned int sig_mask_sigaction[] = {
106   0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000
107 };
108
109 static const unsigned int sig_execve[] = {
110   0xef90000b, // svc 0x90000b
111   0xe1a04000, // mov r4, r0
112   0xe3700a01, // cmn r0, #4096
113 };
114 #define sig_mask_execve sig_mask_all
115
116 static const unsigned int sig_execve2[] = {
117   0xef90000b, // svc 0x90000b
118   0xe3700a01, // cmn r0, #4096
119   0xe1a04000, // mov r4, r0
120 };
121 #define sig_mask_execve2 sig_mask_all
122
123 static const unsigned int sig_chdir[] = {
124   0xef90000c, // svc  0x90000c
125   0xe3700a01, // cmn  r0, #4096
126   0x312fff1e, // bxcc lr
127   0xea0004bb, // b    *
128 };
129 static const unsigned int sig_mask_chdir[] = {
130   0xffffffff, 0xffffffff, 0xffffffff, 0xff000000
131 };
132
133 /* additional wrappers for harder case of syscalls within the code stream */
134 #ifdef PND /* fix PC, not needed on ARM9 */
135 # define SVC_CMN_R0_MOV_R4_PC_ADJ() \
136 "  ldr  r12, [sp, #5*4]\n" \
137 "  add  r12, r12, #4\n" \
138 "  str  r12, [sp, #5*4]\n"
139 #else
140 # define SVC_CMN_R0_MOV_R4_PC_ADJ()
141 #endif
142
143 #define SVC_CMN_R0_MOV_R4_WRAPPER(name, target) \
144 extern int name(); \
145 asm( \
146 #name ":\n" \
147 "  stmfd sp!, {r1-r3,r12,lr}\n" \
148    SVC_CMN_R0_MOV_R4_PC_ADJ() \
149 "  bl   " #target "\n" \
150 "  cmn  r0, #0x1000\n" \
151 "  mov  r4, r0\n" \
152 "  ldmfd sp!, {r1-r3,r12,lr,pc}\n" \
153 );
154
155 SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read)
156 SVC_CMN_R0_MOV_R4_WRAPPER(hw_ioctl, w_ioctl)
157
158 #define PATCH_(p, f, t) { sig_##p, sig_mask_##p, ARRAY_SIZE(sig_##p), t, f, #p }
159 #define PATCH(f) PATCH_(f, w_##f, 0)
160
161 static const struct {
162   const unsigned int *sig;
163   const unsigned int *sig_mask;
164   size_t sig_cnt;
165   unsigned int type;
166   void *func;
167   const char *name;
168 } patches[] = {
169   PATCH (open),
170   PATCH_(open_a1, w_open, 0),
171   PATCH (mmap),
172   PATCH (mmap2), // mmap2 syscall
173   PATCH (munmap),
174   PATCH (read),
175   PATCH_(read_a1, w_read, 0),
176   PATCH_(hw_read, hw_read, 1),
177   PATCH (ioctl),
178   PATCH_(hw_ioctl, hw_ioctl, 1),
179   PATCH (sigaction),
180 //  PATCH_(execve, execve2, 0), // hangs
181   PATCH (chdir),
182 };
183
184 void do_patches(void *ptr, unsigned int size)
185 {
186   int i, s;
187
188   for (i = 0; i < ARRAY_SIZE(patches); i++) {
189     const unsigned int *sig = patches[i].sig;
190     const unsigned int *sig_mask = patches[i].sig_mask;
191     unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
192     unsigned int *seg_end = seg + size / 4;
193     unsigned int sig0 = sig[0];
194
195     for (; seg < seg_end; seg++) {
196       if (*seg != sig0)
197         continue;
198
199       for (s = 1; s < patches[i].sig_cnt; s++)
200         if ((seg[s] ^ sig[s]) & sig_mask[s])
201           break;
202
203       if (s == patches[i].sig_cnt) {
204         dbg("  patch #%i @ %08x type %d %s\n",
205           i, (int)seg, patches[i].type, patches[i].name);
206         switch (patches[i].type) {
207         case 0:
208           seg[0] = 0xe51ff004; // ldr   pc, [pc, #-4]
209           seg[1] = (unsigned int)patches[i].func;
210           break;
211         case 1:
212           seg[0] = 0xe92d8000; // stmfd sp!, {pc}
213           seg[1] = 0xe51ff004; // ldr   pc, [pc, #-4]
214           seg[2] = (unsigned int)patches[i].func;
215           break;
216         default:
217           err("bad patch type: %u\n", patches[i].type);
218           abort();
219         }
220         seg += patches[i].sig_cnt - 1;
221       }
222     }
223   }
224
225   sys_cacheflush(ptr, (char *)ptr + size);
226 }
227
228 // vim:shiftwidth=2:expandtab