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" |
d0b9b0df |
10 | #include "syscalls.h" |
11913091 |
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 | |
c3831532 |
133 | static const unsigned int sig_readlink[] = { |
134 | 0xef900055, // svc 0x900055 |
135 | 0xe3700a01, // cmn r0, #0x1000 |
136 | 0x312fff1e, // bxcc lr |
137 | }; |
138 | #define sig_mask_readlink sig_mask_all |
139 | |
1fc90bfe |
140 | /* special */ |
141 | static const unsigned int sig_cache1[] = { |
142 | 0xee073f5e, // mcr 15, 0, r3, cr7, cr14, 2 |
143 | }; |
144 | #define sig_mask_cache1 sig_mask_all |
145 | |
146 | static const unsigned int sig_cache2[] = { |
147 | 0xee070f17, // mcr 15, 0, r0, cr7, cr7, 0 |
148 | }; |
149 | #define sig_mask_cache2 sig_mask_all |
150 | |
7b39b9a9 |
151 | /* additional wrappers for harder case of syscalls within the code stream */ |
152 | #ifdef PND /* fix PC, not needed on ARM9 */ |
153 | # define SVC_CMN_R0_MOV_R4_PC_ADJ() \ |
154 | " ldr r12, [sp, #5*4]\n" \ |
155 | " add r12, r12, #4\n" \ |
156 | " str r12, [sp, #5*4]\n" |
157 | #else |
158 | # define SVC_CMN_R0_MOV_R4_PC_ADJ() |
0e62bbe6 |
159 | #endif |
7b39b9a9 |
160 | |
161 | #define SVC_CMN_R0_MOV_R4_WRAPPER(name, target) \ |
162 | extern int name(); \ |
163 | asm( \ |
164 | #name ":\n" \ |
165 | " stmfd sp!, {r1-r3,r12,lr}\n" \ |
166 | SVC_CMN_R0_MOV_R4_PC_ADJ() \ |
167 | " bl " #target "\n" \ |
168 | " cmn r0, #0x1000\n" \ |
169 | " mov r4, r0\n" \ |
170 | " ldmfd sp!, {r1-r3,r12,lr,pc}\n" \ |
0e62bbe6 |
171 | ); |
172 | |
b4f4cb40 |
173 | SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read_raw) |
174 | SVC_CMN_R0_MOV_R4_WRAPPER(hw_ioctl, w_ioctl_raw) |
7b39b9a9 |
175 | |
176 | #define PATCH_(p, f, t) { sig_##p, sig_mask_##p, ARRAY_SIZE(sig_##p), t, f, #p } |
177 | #define PATCH(f) PATCH_(f, w_##f, 0) |
11913091 |
178 | |
179 | static const struct { |
180 | const unsigned int *sig; |
dc90c02d |
181 | const unsigned int *sig_mask; |
11913091 |
182 | size_t sig_cnt; |
0e62bbe6 |
183 | unsigned int type; |
11913091 |
184 | void *func; |
7b39b9a9 |
185 | const char *name; |
11913091 |
186 | } patches[] = { |
7b39b9a9 |
187 | PATCH (open), |
188 | PATCH_(open_a1, w_open, 0), |
189 | PATCH (mmap), |
190 | PATCH (mmap2), // mmap2 syscall |
191 | PATCH (munmap), |
192 | PATCH (read), |
193 | PATCH_(read_a1, w_read, 0), |
194 | PATCH_(hw_read, hw_read, 1), |
195 | PATCH (ioctl), |
0e62bbe6 |
196 | PATCH_(hw_ioctl, hw_ioctl, 1), |
7b39b9a9 |
197 | PATCH (sigaction), |
198 | // PATCH_(execve, execve2, 0), // hangs |
199 | PATCH (chdir), |
c3831532 |
200 | PATCH (readlink), |
1fc90bfe |
201 | PATCH_(cache1, NULL, 2), |
202 | PATCH_(cache2, NULL, 2), |
11913091 |
203 | }; |
204 | |
205 | void do_patches(void *ptr, unsigned int size) |
206 | { |
1fc90bfe |
207 | unsigned int *seg = (void *)(((long)ptr + 3) & ~3); |
208 | unsigned int *seg_end = seg + size / 4; |
11913091 |
209 | int i, s; |
210 | |
1fc90bfe |
211 | for (; seg < seg_end; seg++) { |
212 | for (i = 0; i < ARRAY_SIZE(patches); i++) { |
213 | const unsigned int *sig = patches[i].sig; |
214 | const unsigned int *sig_mask; |
11913091 |
215 | |
1fc90bfe |
216 | if (*seg != sig[0]) |
11913091 |
217 | continue; |
218 | |
1fc90bfe |
219 | sig_mask = patches[i].sig_mask; |
11913091 |
220 | for (s = 1; s < patches[i].sig_cnt; s++) |
dc90c02d |
221 | if ((seg[s] ^ sig[s]) & sig_mask[s]) |
11913091 |
222 | break; |
223 | |
0e62bbe6 |
224 | if (s == patches[i].sig_cnt) { |
0e62bbe6 |
225 | switch (patches[i].type) { |
226 | case 0: |
7b39b9a9 |
227 | seg[0] = 0xe51ff004; // ldr pc, [pc, #-4] |
228 | seg[1] = (unsigned int)patches[i].func; |
0e62bbe6 |
229 | break; |
230 | case 1: |
231 | seg[0] = 0xe92d8000; // stmfd sp!, {pc} |
7b39b9a9 |
232 | seg[1] = 0xe51ff004; // ldr pc, [pc, #-4] |
0e62bbe6 |
233 | seg[2] = (unsigned int)patches[i].func; |
234 | break; |
1fc90bfe |
235 | case 2: |
236 | if (seg < (unsigned int *)ptr + 1 || (seg[-1] >> 28) != 0x0e) |
237 | // might be data |
238 | continue; |
239 | seg[0] = 0xe1a00000; // nop |
240 | break; |
0e62bbe6 |
241 | default: |
242 | err("bad patch type: %u\n", patches[i].type); |
243 | abort(); |
244 | } |
1fc90bfe |
245 | dbg(" patch #%2i @ %08x type %d %s\n", |
246 | i, (int)seg, patches[i].type, patches[i].name); |
0e62bbe6 |
247 | seg += patches[i].sig_cnt - 1; |
1fc90bfe |
248 | break; |
0e62bbe6 |
249 | } |
11913091 |
250 | } |
11913091 |
251 | } |
252 | |
253 | sys_cacheflush(ptr, (char *)ptr + size); |
254 | } |
255 | |
499bf01c |
256 | // vim:shiftwidth=2:expandtab |