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.
10 #include "sys_cacheflush.h"
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_mmap[] = {
29 0xe92d000f, // push {r0, r1, r2, r3}
30 0xe1a0000d, // mov r0, sp
31 0xef90005a, // svc 0x90005a
32 0xe28dd010, // add sp, sp, #16
34 #define sig_mask_mmap sig_mask_all
36 static const unsigned int sig_munmap[] = {
37 0xef90005b, // svc 0x90005b
38 0xe3700a01, // cmn r0, #0x1000
39 0x312fff1e, // bxcc lr
41 #define sig_mask_munmap sig_mask_all
43 static const unsigned int sig_mmap2[] = {
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
53 #define sig_mask_mmap2 sig_mask_all
55 static const unsigned int sig_read[] = {
56 0xe59fc080, // ldr ip, [pc, #128]
57 0xe59cc000, // ldr ip, [ip]
58 0xe33c0000, // teq ip, #0
59 0x1a000003, // bne 0x20
60 0xef900003, // svc 0x900003
62 #define sig_mask_read sig_mask_all
64 static const unsigned int sig_ioctl[] = {
65 0xef900036, // svc 0x900036
66 0xe3700a01, // cmn r0, #0x1000
67 0x312fff1e, // bxcc lr
69 #define sig_mask_ioctl sig_mask_all
71 static const unsigned int sig_hw_ioctl[] = {
72 0xef900036, // svc 0x900036
73 0xe3700a01, // cmn r0, #0x1000
74 0xe1a04000, // mov r4, r0
76 #define sig_mask_hw_ioctl sig_mask_all
78 static const unsigned int sig_sigaction[] = {
79 0xe59f300c, // ldr r3, [pc, #12]
80 0xe3530000, // cmp r3, #0
85 static const unsigned int sig_mask_sigaction[] = {
86 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000
89 static const unsigned int sig_execve[] = {
90 0xef90000b, // svc 0x90000b
91 0xe1a04000, // mov r4, r0
92 0xe3700a01, // cmn r0, #4096
94 #define sig_mask_execve sig_mask_all
96 static const unsigned int sig_execve2[] = {
97 0xef90000b, // svc 0x90000b
98 0xe3700a01, // cmn r0, #4096
99 0xe1a04000, // mov r4, r0
101 #define sig_mask_execve2 sig_mask_all
103 static const unsigned int sig_chdir[] = {
104 0xef90000c, // svc 0x90000c
105 0xe3700a01, // cmn r0, #4096
106 0x312fff1e, // bxcc lr
109 static const unsigned int sig_mask_chdir[] = {
110 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000
113 /* additional wrapper for harder case of syscalls within the code stream */
114 extern int hw_ioctl(int fd, int request, void *argp);
117 " stmfd sp!, {r1-r3,r12,lr}\n"
118 #ifdef PND // fix PC, not needed on ARM9
119 " ldr r1, [sp, #5*4]\n"
121 " str r1, [sp, #5*4]\n"
126 " ldmfd sp!, {r1-r3,r12,lr,pc}\n"
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)
132 static const struct {
133 const unsigned int *sig;
134 const unsigned int *sig_mask;
141 PATCH(mmap2), // mmap2 syscall
145 PATCH_(hw_ioctl, hw_ioctl, 1),
147 // PATCH_(execve, execve2), // hangs
151 void do_patches(void *ptr, unsigned int size)
155 for (i = 0; i < ARRAY_SIZE(patches); i++) {
156 const unsigned int *sig = patches[i].sig;
157 const unsigned int *sig_mask = patches[i].sig_mask;
158 unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
159 unsigned int *seg_end = seg + size / 4;
160 unsigned int sig0 = sig[0];
162 for (; seg < seg_end; seg++) {
166 for (s = 1; s < patches[i].sig_cnt; s++)
167 if ((seg[s] ^ sig[s]) & sig_mask[s])
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) {
174 seg[0] = 0xe59ff000; // ldr pc, [pc]
176 seg[2] = (unsigned int)patches[i].func;
179 seg[0] = 0xe92d8000; // stmfd sp!, {pc}
180 seg[1] = 0xe51ff004; // pc, [pc, #-4]
181 seg[2] = (unsigned int)patches[i].func;
184 err("bad patch type: %u\n", patches[i].type);
187 seg += patches[i].sig_cnt - 1;
192 sys_cacheflush(ptr, (char *)ptr + size);
195 // vim:shiftwidth=2:expandtab