499bf01c |
1 | /* |
2 | * GINGE - GINGE Is Not Gp2x Emulator |
0e62bbe6 |
3 | * (C) notaz, 2010-2011,2016 |
499bf01c |
4 | * |
5 | * This work is licensed under the MAME license, see COPYING file for details. |
6 | */ |
11913091 |
7 | #include <stdio.h> |
11913091 |
8 | |
9 | #include "header.h" |
10 | #include "sys_cacheflush.h" |
11 | |
7fd42181 |
12 | #include "override.c" |
dc90c02d |
13 | |
0e62bbe6 |
14 | // note: first mask int must be always full for the search algo |
dc90c02d |
15 | static const unsigned int sig_mask_all[] = { |
16 | 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, |
17 | 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff |
18 | }; |
0881206b |
19 | |
11913091 |
20 | static const unsigned int sig_open[] = { |
0e62bbe6 |
21 | 0xe59cc000, // ldr ip, [ip] |
22 | 0xe33c0000, // teq ip, #0 |
23 | 0x1a000003, // bne 0x1c |
24 | 0xef900005, // svc 0x900005 |
11913091 |
25 | }; |
dc90c02d |
26 | #define sig_mask_open sig_mask_all |
11913091 |
27 | |
7b39b9a9 |
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 | |
11913091 |
34 | static const unsigned int sig_mmap[] = { |
0e62bbe6 |
35 | 0xe92d000f, // push {r0, r1, r2, r3} |
36 | 0xe1a0000d, // mov r0, sp |
37 | 0xef90005a, // svc 0x90005a |
38 | 0xe28dd010, // add sp, sp, #16 |
11913091 |
39 | }; |
dc90c02d |
40 | #define sig_mask_mmap sig_mask_all |
11913091 |
41 | |
3ef07128 |
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 | |
7fd42181 |
49 | static const unsigned int sig_mmap2[] = { |
0e62bbe6 |
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 |
7b39b9a9 |
57 | 0xef9000c0, // svc 0x9000c0 |
11913091 |
58 | }; |
7fd42181 |
59 | #define sig_mask_mmap2 sig_mask_all |
11913091 |
60 | |
3d295a9f |
61 | static const unsigned int sig_read[] = { |
0e62bbe6 |
62 | 0xe59fc080, // ldr ip, [pc, #128] |
63 | 0xe59cc000, // ldr ip, [ip] |
64 | 0xe33c0000, // teq ip, #0 |
65 | 0x1a000003, // bne 0x20 |
66 | 0xef900003, // svc 0x900003 |
3d295a9f |
67 | }; |
dc90c02d |
68 | #define sig_mask_read sig_mask_all |
3d295a9f |
69 | |
7b39b9a9 |
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 | |
dc90c02d |
84 | static const unsigned int sig_ioctl[] = { |
0e62bbe6 |
85 | 0xef900036, // svc 0x900036 |
86 | 0xe3700a01, // cmn r0, #0x1000 |
87 | 0x312fff1e, // bxcc lr |
c1ffd5ba |
88 | }; |
dc90c02d |
89 | #define sig_mask_ioctl sig_mask_all |
c1ffd5ba |
90 | |
0e62bbe6 |
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 | |
dc90c02d |
98 | static const unsigned int sig_sigaction[] = { |
0e62bbe6 |
99 | 0xe59f300c, // ldr r3, [pc, #12] |
100 | 0xe3530000, // cmp r3, #0 |
101 | 0x0a000000, // beq 0f |
102 | 0xea000000, // b * |
103 | 0xea000000, // 0: b * |
dc90c02d |
104 | }; |
105 | static const unsigned int sig_mask_sigaction[] = { |
106 | 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000 |
107 | }; |
108 | |
db9191ed |
109 | static const unsigned int sig_execve[] = { |
0e62bbe6 |
110 | 0xef90000b, // svc 0x90000b |
111 | 0xe1a04000, // mov r4, r0 |
112 | 0xe3700a01, // cmn r0, #4096 |
db9191ed |
113 | }; |
114 | #define sig_mask_execve sig_mask_all |
115 | |
116 | static const unsigned int sig_execve2[] = { |
0e62bbe6 |
117 | 0xef90000b, // svc 0x90000b |
118 | 0xe3700a01, // cmn r0, #4096 |
119 | 0xe1a04000, // mov r4, r0 |
db9191ed |
120 | }; |
121 | #define sig_mask_execve2 sig_mask_all |
122 | |
123 | static const unsigned int sig_chdir[] = { |
0e62bbe6 |
124 | 0xef90000c, // svc 0x90000c |
125 | 0xe3700a01, // cmn r0, #4096 |
126 | 0x312fff1e, // bxcc lr |
127 | 0xea0004bb, // b * |
db9191ed |
128 | }; |
129 | static const unsigned int sig_mask_chdir[] = { |
130 | 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000 |
131 | }; |
dc90c02d |
132 | |
1fc90bfe |
133 | /* special */ |
134 | static const unsigned int sig_cache1[] = { |
135 | 0xee073f5e, // mcr 15, 0, r3, cr7, cr14, 2 |
136 | }; |
137 | #define sig_mask_cache1 sig_mask_all |
138 | |
139 | static const unsigned int sig_cache2[] = { |
140 | 0xee070f17, // mcr 15, 0, r0, cr7, cr7, 0 |
141 | }; |
142 | #define sig_mask_cache2 sig_mask_all |
143 | |
7b39b9a9 |
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() |
0e62bbe6 |
152 | #endif |
7b39b9a9 |
153 | |
154 | #define SVC_CMN_R0_MOV_R4_WRAPPER(name, target) \ |
155 | extern int name(); \ |
156 | asm( \ |
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" \ |
0e62bbe6 |
164 | ); |
165 | |
7b39b9a9 |
166 | SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read) |
167 | SVC_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) |
11913091 |
171 | |
172 | static const struct { |
173 | const unsigned int *sig; |
dc90c02d |
174 | const unsigned int *sig_mask; |
11913091 |
175 | size_t sig_cnt; |
0e62bbe6 |
176 | unsigned int type; |
11913091 |
177 | void *func; |
7b39b9a9 |
178 | const char *name; |
11913091 |
179 | } patches[] = { |
7b39b9a9 |
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), |
0e62bbe6 |
189 | PATCH_(hw_ioctl, hw_ioctl, 1), |
7b39b9a9 |
190 | PATCH (sigaction), |
191 | // PATCH_(execve, execve2, 0), // hangs |
192 | PATCH (chdir), |
1fc90bfe |
193 | PATCH_(cache1, NULL, 2), |
194 | PATCH_(cache2, NULL, 2), |
11913091 |
195 | }; |
196 | |
197 | void do_patches(void *ptr, unsigned int size) |
198 | { |
1fc90bfe |
199 | unsigned int *seg = (void *)(((long)ptr + 3) & ~3); |
200 | unsigned int *seg_end = seg + size / 4; |
11913091 |
201 | int i, s; |
202 | |
1fc90bfe |
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; |
11913091 |
207 | |
1fc90bfe |
208 | if (*seg != sig[0]) |
11913091 |
209 | continue; |
210 | |
1fc90bfe |
211 | sig_mask = patches[i].sig_mask; |
11913091 |
212 | for (s = 1; s < patches[i].sig_cnt; s++) |
dc90c02d |
213 | if ((seg[s] ^ sig[s]) & sig_mask[s]) |
11913091 |
214 | break; |
215 | |
0e62bbe6 |
216 | if (s == patches[i].sig_cnt) { |
0e62bbe6 |
217 | switch (patches[i].type) { |
218 | case 0: |
7b39b9a9 |
219 | seg[0] = 0xe51ff004; // ldr pc, [pc, #-4] |
220 | seg[1] = (unsigned int)patches[i].func; |
0e62bbe6 |
221 | break; |
222 | case 1: |
223 | seg[0] = 0xe92d8000; // stmfd sp!, {pc} |
7b39b9a9 |
224 | seg[1] = 0xe51ff004; // ldr pc, [pc, #-4] |
0e62bbe6 |
225 | seg[2] = (unsigned int)patches[i].func; |
226 | break; |
1fc90bfe |
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; |
0e62bbe6 |
233 | default: |
234 | err("bad patch type: %u\n", patches[i].type); |
235 | abort(); |
236 | } |
1fc90bfe |
237 | dbg(" patch #%2i @ %08x type %d %s\n", |
238 | i, (int)seg, patches[i].type, patches[i].name); |
0e62bbe6 |
239 | seg += patches[i].sig_cnt - 1; |
1fc90bfe |
240 | break; |
0e62bbe6 |
241 | } |
11913091 |
242 | } |
11913091 |
243 | } |
244 | |
245 | sys_cacheflush(ptr, (char *)ptr + size); |
246 | } |
247 | |
499bf01c |
248 | // vim:shiftwidth=2:expandtab |