1 // vim:shiftwidth=2:expandtab
10 #include "sys_cacheflush.h"
12 static const unsigned int sig_open[] = {
13 0xe59cc000, 0xe33c0000, 0x1a000003, 0xef900005
16 static const unsigned int sig_mmap[] = {
17 0xe92d000f, 0xe1a0000d, 0xef90005a, 0xe28dd010
20 static const unsigned int sig_mmap_[] = {
21 0xe52d5004, 0xe59d5008, 0xe52d4004, 0xe59d4008,
22 0xe1b0ca05, 0x1a000006, 0xe1a05625, 0xef9000c0
25 #define FAKE_DEVMEM_DEVICE 10001
27 static int w_open(const char *pathname, int flags, mode_t mode)
30 if (strcmp(pathname, "/dev/mem") != 0)
31 ret = open(pathname, flags, mode);
33 ret = FAKE_DEVMEM_DEVICE;
35 printf("open(%s) = %d\n", pathname, ret);
39 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
42 if (fd != FAKE_DEVMEM_DEVICE)
43 ret = mmap(addr, length, prot, flags, fd, offset);
45 ret = emu_mmap_dev(length, prot, flags, offset);
47 printf("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
50 #define w_mmap_ w_mmap
52 #define PATCH(f) { sig_##f, ARRAY_SIZE(sig_##f), w_##f }
55 const unsigned int *sig;
61 PATCH(mmap_), // mmap using mmap2 syscall
64 void do_patches(void *ptr, unsigned int size)
68 for (i = 0; i < ARRAY_SIZE(patches); i++) {
69 const unsigned int *sig = patches[i].sig;
70 unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
71 unsigned int *seg_end = seg + size / 4;
72 unsigned int sig0 = sig[0];
74 for (; seg < seg_end; seg++) {
78 for (s = 1; s < patches[i].sig_cnt; s++)
82 if (s == patches[i].sig_cnt)
88 printf(" patch #%i @ %08x\n", i, (int)seg);
89 seg[0] = 0xe59ff000; // ldr pc, [pc]
91 seg[2] = (unsigned int)patches[i].func;
94 sys_cacheflush(ptr, (char *)ptr + size);