2 * GINGE - GINGE Is Not Gp2x Emulator
3 * (C) notaz, 2010-2011,2016
5 * This work is licensed under the MAME license, see COPYING file for details.
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
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
26 #define sig_mask_open sig_mask_all
28 static const unsigned int sig_open_a1[] = {
29 0xef900005, // svc 0x900005
30 0xe1a0f00e, // mov pc, lr
32 #define sig_mask_open_a1 sig_mask_all
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
40 #define sig_mask_mmap sig_mask_all
42 static const unsigned int sig_munmap[] = {
43 0xef90005b, // svc 0x90005b
44 0xe3700a01, // cmn r0, #0x1000
45 0x312fff1e, // bxcc lr
47 #define sig_mask_munmap sig_mask_all
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
59 #define sig_mask_mmap2 sig_mask_all
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
68 #define sig_mask_read sig_mask_all
70 static const unsigned int sig_read_a1[] = {
71 0xef900003, // svc 0x900003
72 0xe3700a01, // cmn r0, #0x1000
73 0x312fff1e, // bxcc lr
75 #define sig_mask_read_a1 sig_mask_all
77 static const unsigned int sig_hw_read[] = {
78 0xef900003, // svc 0x900003
79 0xe3700a01, // cmn r0, #0x1000
80 0xe1a04000, // mov r4, r0
82 #define sig_mask_hw_read sig_mask_all
84 static const unsigned int sig_ioctl[] = {
85 0xef900036, // svc 0x900036
86 0xe3700a01, // cmn r0, #0x1000
87 0x312fff1e, // bxcc lr
89 #define sig_mask_ioctl sig_mask_all
91 static const unsigned int sig_hw_ioctl[] = {
92 0xef900036, // svc 0x900036
93 0xe3700a01, // cmn r0, #0x1000
94 0xe1a04000, // mov r4, r0
96 #define sig_mask_hw_ioctl sig_mask_all
98 static const unsigned int sig_sigaction[] = {
99 0xe59f300c, // ldr r3, [pc, #12]
100 0xe3530000, // cmp r3, #0
101 0x0a000000, // beq 0f
103 0xea000000, // 0: b *
105 static const unsigned int sig_mask_sigaction[] = {
106 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000
109 static const unsigned int sig_execve[] = {
110 0xef90000b, // svc 0x90000b
111 0xe1a04000, // mov r4, r0
112 0xe3700a01, // cmn r0, #4096
114 #define sig_mask_execve sig_mask_all
116 static const unsigned int sig_execve2[] = {
117 0xef90000b, // svc 0x90000b
118 0xe3700a01, // cmn r0, #4096
119 0xe1a04000, // mov r4, r0
121 #define sig_mask_execve2 sig_mask_all
123 static const unsigned int sig_chdir[] = {
124 0xef90000c, // svc 0x90000c
125 0xe3700a01, // cmn r0, #4096
126 0x312fff1e, // bxcc lr
129 static const unsigned int sig_mask_chdir[] = {
130 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000
133 static const unsigned int sig_readlink[] = {
134 0xef900055, // svc 0x900055
135 0xe3700a01, // cmn r0, #0x1000
136 0x312fff1e, // bxcc lr
138 #define sig_mask_readlink sig_mask_all
141 static const unsigned int sig_cache1[] = {
142 0xee073f5e, // mcr 15, 0, r3, cr7, cr14, 2
144 #define sig_mask_cache1 sig_mask_all
146 static const unsigned int sig_cache2[] = {
147 0xee070f17, // mcr 15, 0, r0, cr7, cr7, 0
149 #define sig_mask_cache2 sig_mask_all
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"
158 # define SVC_CMN_R0_MOV_R4_PC_ADJ()
161 #define SVC_CMN_R0_MOV_R4_WRAPPER(name, target) \
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" \
170 " ldmfd sp!, {r1-r3,r12,lr,pc}\n" \
173 SVC_CMN_R0_MOV_R4_WRAPPER(hw_read, w_read_raw)
174 SVC_CMN_R0_MOV_R4_WRAPPER(hw_ioctl, w_ioctl_raw)
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)
179 static const struct {
180 const unsigned int *sig;
181 const unsigned int *sig_mask;
188 PATCH_(open_a1, w_open, 0),
190 PATCH (mmap2), // mmap2 syscall
193 PATCH_(read_a1, w_read, 0),
194 PATCH_(hw_read, hw_read, 1),
196 PATCH_(hw_ioctl, hw_ioctl, 1),
198 // PATCH_(execve, execve2, 0), // hangs
201 PATCH_(cache1, NULL, 2),
202 PATCH_(cache2, NULL, 2),
205 void do_patches(void *ptr, unsigned int size)
207 unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
208 unsigned int *seg_end = seg + size / 4;
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;
219 sig_mask = patches[i].sig_mask;
220 for (s = 1; s < patches[i].sig_cnt; s++)
221 if ((seg[s] ^ sig[s]) & sig_mask[s])
224 if (s == patches[i].sig_cnt) {
225 switch (patches[i].type) {
227 seg[0] = 0xe51ff004; // ldr pc, [pc, #-4]
228 seg[1] = (unsigned int)patches[i].func;
231 seg[0] = 0xe92d8000; // stmfd sp!, {pc}
232 seg[1] = 0xe51ff004; // ldr pc, [pc, #-4]
233 seg[2] = (unsigned int)patches[i].func;
236 if (seg < (unsigned int *)ptr + 1 || (seg[-1] >> 28) != 0x0e)
239 seg[0] = 0xe1a00000; // nop
242 err("bad patch type: %u\n", patches[i].type);
245 dbg(" patch #%2i @ %08x type %d %s\n",
246 i, (int)seg, patches[i].type, patches[i].name);
247 seg += patches[i].sig_cnt - 1;
253 sys_cacheflush(ptr, (char *)ptr + size);
256 // vim:shiftwidth=2:expandtab