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 | |
28 | static const unsigned int sig_mmap[] = { |
0e62bbe6 |
29 | 0xe92d000f, // push {r0, r1, r2, r3} |
30 | 0xe1a0000d, // mov r0, sp |
31 | 0xef90005a, // svc 0x90005a |
32 | 0xe28dd010, // add sp, sp, #16 |
11913091 |
33 | }; |
dc90c02d |
34 | #define sig_mask_mmap sig_mask_all |
11913091 |
35 | |
3ef07128 |
36 | static const unsigned int sig_munmap[] = { |
37 | 0xef90005b, // svc 0x90005b |
38 | 0xe3700a01, // cmn r0, #0x1000 |
39 | 0x312fff1e, // bxcc lr |
40 | }; |
41 | #define sig_mask_munmap sig_mask_all |
42 | |
7fd42181 |
43 | static const unsigned int sig_mmap2[] = { |
0e62bbe6 |
44 | 0xe52d5004, // push {r5} |
45 | 0xe59d5008, // ldr r5, [sp, #8] |
46 | 0xe52d4004, // push {r4} |
47 | 0xe59d4008, // ldr r4, [sp, #8] |
48 | 0xe1b0ca05, // lsls ip, r5, #20 |
49 | 0x1a000006, // bne 0x34 |
50 | 0xe1a05625, // lsr r5, r5, #12 |
51 | 0xef9000c0, // svc 0x009000c0 |
11913091 |
52 | }; |
7fd42181 |
53 | #define sig_mask_mmap2 sig_mask_all |
11913091 |
54 | |
3d295a9f |
55 | static const unsigned int sig_read[] = { |
0e62bbe6 |
56 | 0xe59fc080, // ldr ip, [pc, #128] |
57 | 0xe59cc000, // ldr ip, [ip] |
58 | 0xe33c0000, // teq ip, #0 |
59 | 0x1a000003, // bne 0x20 |
60 | 0xef900003, // svc 0x900003 |
3d295a9f |
61 | }; |
dc90c02d |
62 | #define sig_mask_read sig_mask_all |
3d295a9f |
63 | |
dc90c02d |
64 | static const unsigned int sig_ioctl[] = { |
0e62bbe6 |
65 | 0xef900036, // svc 0x900036 |
66 | 0xe3700a01, // cmn r0, #0x1000 |
67 | 0x312fff1e, // bxcc lr |
c1ffd5ba |
68 | }; |
dc90c02d |
69 | #define sig_mask_ioctl sig_mask_all |
c1ffd5ba |
70 | |
0e62bbe6 |
71 | static const unsigned int sig_hw_ioctl[] = { |
72 | 0xef900036, // svc 0x900036 |
73 | 0xe3700a01, // cmn r0, #0x1000 |
74 | 0xe1a04000, // mov r4, r0 |
75 | }; |
76 | #define sig_mask_hw_ioctl sig_mask_all |
77 | |
dc90c02d |
78 | static const unsigned int sig_sigaction[] = { |
0e62bbe6 |
79 | 0xe59f300c, // ldr r3, [pc, #12] |
80 | 0xe3530000, // cmp r3, #0 |
81 | 0x0a000000, // beq 0f |
82 | 0xea000000, // b * |
83 | 0xea000000, // 0: b * |
dc90c02d |
84 | }; |
85 | static const unsigned int sig_mask_sigaction[] = { |
86 | 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000 |
87 | }; |
88 | |
db9191ed |
89 | static const unsigned int sig_execve[] = { |
0e62bbe6 |
90 | 0xef90000b, // svc 0x90000b |
91 | 0xe1a04000, // mov r4, r0 |
92 | 0xe3700a01, // cmn r0, #4096 |
db9191ed |
93 | }; |
94 | #define sig_mask_execve sig_mask_all |
95 | |
96 | static const unsigned int sig_execve2[] = { |
0e62bbe6 |
97 | 0xef90000b, // svc 0x90000b |
98 | 0xe3700a01, // cmn r0, #4096 |
99 | 0xe1a04000, // mov r4, r0 |
db9191ed |
100 | }; |
101 | #define sig_mask_execve2 sig_mask_all |
102 | |
103 | static const unsigned int sig_chdir[] = { |
0e62bbe6 |
104 | 0xef90000c, // svc 0x90000c |
105 | 0xe3700a01, // cmn r0, #4096 |
106 | 0x312fff1e, // bxcc lr |
107 | 0xea0004bb, // b * |
db9191ed |
108 | }; |
109 | static const unsigned int sig_mask_chdir[] = { |
110 | 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000 |
111 | }; |
dc90c02d |
112 | |
0e62bbe6 |
113 | /* additional wrapper for harder case of syscalls within the code stream */ |
114 | extern int hw_ioctl(int fd, int request, void *argp); |
115 | asm( |
116 | "hw_ioctl:\n" |
117 | " stmfd sp!, {r1-r3,r12,lr}\n" |
118 | #ifdef PND // fix PC, not needed on ARM9 |
119 | " ldr r1, [sp, #5*4]\n" |
120 | " add r1, r1, #4\n" |
121 | " str r1, [sp, #5*4]\n" |
122 | #endif |
123 | " bl w_ioctl\n" |
124 | " cmn r0, #0x1000\n" |
125 | " mov r4, r0\n" |
126 | " ldmfd sp!, {r1-r3,r12,lr,pc}\n" |
127 | ); |
128 | |
129 | #define PATCH_(f, p, t) { sig_##p, sig_mask_##p, ARRAY_SIZE(sig_##p), t, f } |
130 | #define PATCH(f) PATCH_(w_##f, f, 0) |
11913091 |
131 | |
132 | static const struct { |
133 | const unsigned int *sig; |
dc90c02d |
134 | const unsigned int *sig_mask; |
11913091 |
135 | size_t sig_cnt; |
0e62bbe6 |
136 | unsigned int type; |
11913091 |
137 | void *func; |
138 | } patches[] = { |
139 | PATCH(open), |
140 | PATCH(mmap), |
7fd42181 |
141 | PATCH(mmap2), // mmap2 syscall |
3ef07128 |
142 | PATCH(munmap), |
3d295a9f |
143 | PATCH(read), |
dc90c02d |
144 | PATCH(ioctl), |
0e62bbe6 |
145 | PATCH_(hw_ioctl, hw_ioctl, 1), |
dc90c02d |
146 | PATCH(sigaction), |
db9191ed |
147 | // PATCH_(execve, execve2), // hangs |
148 | PATCH(chdir), |
11913091 |
149 | }; |
150 | |
151 | void do_patches(void *ptr, unsigned int size) |
152 | { |
153 | int i, s; |
154 | |
155 | for (i = 0; i < ARRAY_SIZE(patches); i++) { |
156 | const unsigned int *sig = patches[i].sig; |
dc90c02d |
157 | const unsigned int *sig_mask = patches[i].sig_mask; |
11913091 |
158 | unsigned int *seg = (void *)(((long)ptr + 3) & ~3); |
159 | unsigned int *seg_end = seg + size / 4; |
160 | unsigned int sig0 = sig[0]; |
161 | |
162 | for (; seg < seg_end; seg++) { |
163 | if (*seg != sig0) |
164 | continue; |
165 | |
166 | for (s = 1; s < patches[i].sig_cnt; s++) |
dc90c02d |
167 | if ((seg[s] ^ sig[s]) & sig_mask[s]) |
11913091 |
168 | break; |
169 | |
0e62bbe6 |
170 | if (s == patches[i].sig_cnt) { |
171 | dbg(" patch #%i @ %08x type %d\n", i, (int)seg, patches[i].type); |
172 | switch (patches[i].type) { |
173 | case 0: |
174 | seg[0] = 0xe59ff000; // ldr pc, [pc] |
175 | seg[1] = 0; |
176 | seg[2] = (unsigned int)patches[i].func; |
177 | break; |
178 | case 1: |
179 | seg[0] = 0xe92d8000; // stmfd sp!, {pc} |
180 | seg[1] = 0xe51ff004; // pc, [pc, #-4] |
181 | seg[2] = (unsigned int)patches[i].func; |
182 | break; |
183 | default: |
184 | err("bad patch type: %u\n", patches[i].type); |
185 | abort(); |
186 | } |
187 | seg += patches[i].sig_cnt - 1; |
188 | } |
11913091 |
189 | } |
11913091 |
190 | } |
191 | |
192 | sys_cacheflush(ptr, (char *)ptr + size); |
193 | } |
194 | |
499bf01c |
195 | // vim:shiftwidth=2:expandtab |