get rid of pthreads
[ginge.git] / loader / patches.c
... / ...
CommitLineData
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 "syscalls.h"
11
12#include "override.c"
13
14// note: first mask int must be always full for the search algo
15static const unsigned int sig_mask_all[] = {
16 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
17 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
18};
19
20static 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
28static 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
34static 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
42static 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
49static 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
61static 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
70static 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
77static 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
84static 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
91static 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
98static 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};
105static const unsigned int sig_mask_sigaction[] = {
106 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000
107};
108
109static 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
116static 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
123static const unsigned int sig_chdir[] = {
124 0xef90000c, // svc 0x90000c
125 0xe3700a01, // cmn r0, #4096
126 0x312fff1e, // bxcc lr
127 0xea0004bb, // b *
128};
129static const unsigned int sig_mask_chdir[] = {
130 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000
131};
132
133/* special */
134static const unsigned int sig_cache1[] = {
135 0xee073f5e, // mcr 15, 0, r3, cr7, cr14, 2
136};
137#define sig_mask_cache1 sig_mask_all
138
139static const unsigned int sig_cache2[] = {
140 0xee070f17, // mcr 15, 0, r0, cr7, cr7, 0
141};
142#define sig_mask_cache2 sig_mask_all
143
144/* additional wrappers for harder case of syscalls within the code stream */
145#ifdef PND /* fix PC, not needed on ARM9 */
146# define SVC_CMN_R0_MOV_R4_PC_ADJ() \
147" ldr r12, [sp, #5*4]\n" \
148" add r12, r12, #4\n" \
149" str r12, [sp, #5*4]\n"
150#else
151# define SVC_CMN_R0_MOV_R4_PC_ADJ()
152#endif
153
154#define SVC_CMN_R0_MOV_R4_WRAPPER(name, target) \
155extern int name(); \
156asm( \
157#name ":\n" \
158" stmfd sp!, {r1-r3,r12,lr}\n" \
159 SVC_CMN_R0_MOV_R4_PC_ADJ() \
160" bl " #target "\n" \
161" cmn r0, #0x1000\n" \
162" mov r4, r0\n" \
163" ldmfd sp!, {r1-r3,r12,lr,pc}\n" \
164);
165
166SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read)
167SVC_CMN_R0_MOV_R4_WRAPPER(hw_ioctl, w_ioctl)
168
169#define PATCH_(p, f, t) { sig_##p, sig_mask_##p, ARRAY_SIZE(sig_##p), t, f, #p }
170#define PATCH(f) PATCH_(f, w_##f, 0)
171
172static const struct {
173 const unsigned int *sig;
174 const unsigned int *sig_mask;
175 size_t sig_cnt;
176 unsigned int type;
177 void *func;
178 const char *name;
179} patches[] = {
180 PATCH (open),
181 PATCH_(open_a1, w_open, 0),
182 PATCH (mmap),
183 PATCH (mmap2), // mmap2 syscall
184 PATCH (munmap),
185 PATCH (read),
186 PATCH_(read_a1, w_read, 0),
187 PATCH_(hw_read, hw_read, 1),
188 PATCH (ioctl),
189 PATCH_(hw_ioctl, hw_ioctl, 1),
190 PATCH (sigaction),
191// PATCH_(execve, execve2, 0), // hangs
192 PATCH (chdir),
193 PATCH_(cache1, NULL, 2),
194 PATCH_(cache2, NULL, 2),
195};
196
197void do_patches(void *ptr, unsigned int size)
198{
199 unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
200 unsigned int *seg_end = seg + size / 4;
201 int i, s;
202
203 for (; seg < seg_end; seg++) {
204 for (i = 0; i < ARRAY_SIZE(patches); i++) {
205 const unsigned int *sig = patches[i].sig;
206 const unsigned int *sig_mask;
207
208 if (*seg != sig[0])
209 continue;
210
211 sig_mask = patches[i].sig_mask;
212 for (s = 1; s < patches[i].sig_cnt; s++)
213 if ((seg[s] ^ sig[s]) & sig_mask[s])
214 break;
215
216 if (s == patches[i].sig_cnt) {
217 switch (patches[i].type) {
218 case 0:
219 seg[0] = 0xe51ff004; // ldr pc, [pc, #-4]
220 seg[1] = (unsigned int)patches[i].func;
221 break;
222 case 1:
223 seg[0] = 0xe92d8000; // stmfd sp!, {pc}
224 seg[1] = 0xe51ff004; // ldr pc, [pc, #-4]
225 seg[2] = (unsigned int)patches[i].func;
226 break;
227 case 2:
228 if (seg < (unsigned int *)ptr + 1 || (seg[-1] >> 28) != 0x0e)
229 // might be data
230 continue;
231 seg[0] = 0xe1a00000; // nop
232 break;
233 default:
234 err("bad patch type: %u\n", patches[i].type);
235 abort();
236 }
237 dbg(" patch #%2i @ %08x type %d %s\n",
238 i, (int)seg, patches[i].type, patches[i].name);
239 seg += patches[i].sig_cnt - 1;
240 break;
241 }
242 }
243 }
244
245 sys_cacheflush(ptr, (char *)ptr + size);
246}
247
248// vim:shiftwidth=2:expandtab