| 1 | /* |
| 2 | * GINGE - GINGE Is Not Gp2x Emulator |
| 3 | * (C) notaz, 2010-2011,2016 |
| 4 | * |
| 5 | * This work is licensed under the MAME license, see COPYING file for details. |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | #include "header.h" |
| 10 | #include "sys_cacheflush.h" |
| 11 | |
| 12 | #include "override.c" |
| 13 | |
| 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 |
| 18 | }; |
| 19 | |
| 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 |
| 25 | }; |
| 26 | #define sig_mask_open sig_mask_all |
| 27 | |
| 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 |
| 33 | }; |
| 34 | #define sig_mask_mmap sig_mask_all |
| 35 | |
| 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 | |
| 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 |
| 52 | }; |
| 53 | #define sig_mask_mmap2 sig_mask_all |
| 54 | |
| 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 |
| 61 | }; |
| 62 | #define sig_mask_read sig_mask_all |
| 63 | |
| 64 | static const unsigned int sig_ioctl[] = { |
| 65 | 0xef900036, // svc 0x900036 |
| 66 | 0xe3700a01, // cmn r0, #0x1000 |
| 67 | 0x312fff1e, // bxcc lr |
| 68 | }; |
| 69 | #define sig_mask_ioctl sig_mask_all |
| 70 | |
| 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 | |
| 78 | static const unsigned int sig_sigaction[] = { |
| 79 | 0xe59f300c, // ldr r3, [pc, #12] |
| 80 | 0xe3530000, // cmp r3, #0 |
| 81 | 0x0a000000, // beq 0f |
| 82 | 0xea000000, // b * |
| 83 | 0xea000000, // 0: b * |
| 84 | }; |
| 85 | static const unsigned int sig_mask_sigaction[] = { |
| 86 | 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000 |
| 87 | }; |
| 88 | |
| 89 | static const unsigned int sig_execve[] = { |
| 90 | 0xef90000b, // svc 0x90000b |
| 91 | 0xe1a04000, // mov r4, r0 |
| 92 | 0xe3700a01, // cmn r0, #4096 |
| 93 | }; |
| 94 | #define sig_mask_execve sig_mask_all |
| 95 | |
| 96 | static const unsigned int sig_execve2[] = { |
| 97 | 0xef90000b, // svc 0x90000b |
| 98 | 0xe3700a01, // cmn r0, #4096 |
| 99 | 0xe1a04000, // mov r4, r0 |
| 100 | }; |
| 101 | #define sig_mask_execve2 sig_mask_all |
| 102 | |
| 103 | static const unsigned int sig_chdir[] = { |
| 104 | 0xef90000c, // svc 0x90000c |
| 105 | 0xe3700a01, // cmn r0, #4096 |
| 106 | 0x312fff1e, // bxcc lr |
| 107 | 0xea0004bb, // b * |
| 108 | }; |
| 109 | static const unsigned int sig_mask_chdir[] = { |
| 110 | 0xffffffff, 0xffffffff, 0xffffffff, 0xff000000 |
| 111 | }; |
| 112 | |
| 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) |
| 131 | |
| 132 | static const struct { |
| 133 | const unsigned int *sig; |
| 134 | const unsigned int *sig_mask; |
| 135 | size_t sig_cnt; |
| 136 | unsigned int type; |
| 137 | void *func; |
| 138 | } patches[] = { |
| 139 | PATCH(open), |
| 140 | PATCH(mmap), |
| 141 | PATCH(mmap2), // mmap2 syscall |
| 142 | PATCH(munmap), |
| 143 | PATCH(read), |
| 144 | PATCH(ioctl), |
| 145 | PATCH_(hw_ioctl, hw_ioctl, 1), |
| 146 | PATCH(sigaction), |
| 147 | // PATCH_(execve, execve2), // hangs |
| 148 | PATCH(chdir), |
| 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; |
| 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]; |
| 161 | |
| 162 | for (; seg < seg_end; seg++) { |
| 163 | if (*seg != sig0) |
| 164 | continue; |
| 165 | |
| 166 | for (s = 1; s < patches[i].sig_cnt; s++) |
| 167 | if ((seg[s] ^ sig[s]) & sig_mask[s]) |
| 168 | break; |
| 169 | |
| 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 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | sys_cacheflush(ptr, (char *)ptr + size); |
| 193 | } |
| 194 | |
| 195 | // vim:shiftwidth=2:expandtab |