2 * GINGE - GINGE Is Not Gp2x Emulator
5 * This work is licensed under the MAME license, see COPYING file for details.
11 #include <sys/types.h>
16 #include <sys/ioctl.h>
21 #include "realfuncs.h"
30 #define UNUSED __attribute__((unused))
32 static const struct dev_fd_t takeover_devs[] = {
33 { "/dev/mem", FAKEDEV_MEM },
34 { "/dev/GPIO", FAKEDEV_GPIO },
35 { "/dev/fb0", FAKEDEV_FB0 },
36 { "/dev/fb/0", FAKEDEV_FB0 },
37 { "/dev/fb1", FAKEDEV_FB1 },
38 { "/dev/fb/1", FAKEDEV_FB1 },
39 { "/dev/mmuhack", FAKEDEV_MMUHACK },
40 { "/dev/tty", FAKEDEV_TTY0 },
41 { "/dev/tty0", FAKEDEV_TTY0 },
43 { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
47 static int w_open(const char *pathname, int flags, mode_t mode)
51 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
52 const char *p, *oname;
55 oname = takeover_devs[i].name;
56 p = strchr(oname, '*');
60 len = strlen(oname) + 1;
62 if (strncmp(pathname, oname, len) == 0) {
63 ret = takeover_devs[i].fd;
68 if (i == ARRAY_SIZE(takeover_devs))
69 ret = open(pathname, flags, mode);
72 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
73 struct dev_fd_t *eifd = &emu_interesting_fds[i];
74 if (strcmp(pathname, eifd->name) == 0) {
76 if (eifd->open_cb != NULL)
83 strace("open(%s) = %d\n", pathname, ret);
87 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
90 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
91 ret = emu_do_mmap(length, prot, flags, fd, offset);
93 ret = mmap(addr, length, prot, flags, fd, offset);
95 // threads are using heap before they mmap their stack
96 // printf needs valid stack for pthread/errno
97 if (((long)&ret & 0xf0000000) == 0xb0000000)
98 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
101 #define w_mmap2 w_mmap
103 static int w_munmap(void *addr, size_t length)
106 ret = emu_do_munmap(addr, length);
108 ret = munmap(addr, length);
110 if (((long)&ret & 0xf0000000) == 0xb0000000)
111 strace("munmap(%p, %x) = %d\n", addr, length, ret);
115 static ssize_t w_read(int fd, void *buf, size_t count)
118 if (fd == FAKEDEV_GPIO)
119 ret = emu_read_gpiodev(buf, count);
121 ret = read(fd, buf, count);
123 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
127 static int w_ioctl(int fd, int request, void *argp)
131 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
132 fd == emu_interesting_fds[IFD_SOUND].fd)
133 ret = emu_do_ioctl(fd, request, argp);
135 ret = ioctl(fd, request, argp);
137 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
141 static int w_sigaction(int signum, const void *act, void *oldact)
143 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
148 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
151 if (fd != FAKEDEV_TTY0)
152 ret = tcgetattr(fd, termios_p);
156 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
160 static UNUSED int w_tcsetattr(int fd, int optional_actions,
161 const struct termios *termios_p)
164 if (fd != FAKEDEV_TTY0)
165 ret = tcsetattr(fd, optional_actions, termios_p);
169 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
173 static UNUSED FILE *w_fopen(const char *path, const char *mode)
175 FILE *ret = emu_do_fopen(path, mode);
176 strace("fopen(%s, %s) = %p\n", path, mode, ret);
180 static UNUSED int w_system(const char *command)
182 int ret = emu_do_system(command);
183 strace("system(%s) = %d\n", command, ret);
187 extern char **environ;
189 static UNUSED int w_execl(const char *path, const char *arg, ...)
191 // don't allow exec (for now)
192 strace("execl(%s, %s, ...) = ?\n", path, arg);
196 static UNUSED int w_execlp(const char *file, const char *arg, ...)
198 strace("execlp(%s, %s, ...) = ?\n", file, arg);
202 static UNUSED int w_execle(const char *path, const char *arg, ...)
204 strace("execle(%s, %s, ...) = ?\n", path, arg);
208 static UNUSED int w_execv(const char *path, char *const argv[])
210 strace("execv(%s, %p) = ?\n", path, argv);
211 return emu_do_execve(path, argv, environ);
214 static UNUSED int w_execvp(const char *file, char *const argv[])
216 strace("execvp(%s, %p) = ?\n", file, argv);
217 return emu_do_execve(file, argv, environ);
220 // static note: this can't safely return because of the way it's patched in
221 // static note2: can't be used, execve hangs?
222 static UNUSED int w_execve(const char *filename, char *const argv[],
225 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
226 return emu_do_execve(filename, argv, envp);
229 static int w_chdir(const char *path)
232 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
236 strace("chdir(%s) = %d\n", path, ret);
260 #define MAKE_WRAP_SYM_N(sym) \
261 /* alias wrap symbols to real names */ \
262 typeof(sym) sym __attribute__((alias("w_" #sym)))
264 #define MAKE_WRAP_SYM(sym) \
265 MAKE_WRAP_SYM_N(sym); \
266 /* wrapper to real functions, to be set up on load */ \
267 static typeof(sym) *p_real_##sym
269 // note: update symver too
271 MAKE_WRAP_SYM(fopen);
273 MAKE_WRAP_SYM(munmap);
275 MAKE_WRAP_SYM(ioctl);
276 MAKE_WRAP_SYM(sigaction);
277 MAKE_WRAP_SYM(tcgetattr);
278 MAKE_WRAP_SYM(tcsetattr);
279 MAKE_WRAP_SYM(system);
280 MAKE_WRAP_SYM_N(execl);
281 MAKE_WRAP_SYM_N(execlp);
282 MAKE_WRAP_SYM_N(execle);
283 MAKE_WRAP_SYM_N(execv);
284 MAKE_WRAP_SYM_N(execvp);
285 MAKE_WRAP_SYM(execve);
286 MAKE_WRAP_SYM(chdir);
287 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
289 #define REAL_FUNC_NP(name) \
290 { #name, (void **)&p_real_##name }
292 static const struct {
295 } real_funcs_np[] = {
299 REAL_FUNC_NP(munmap),
302 REAL_FUNC_NP(sigaction),
303 REAL_FUNC_NP(tcgetattr),
304 REAL_FUNC_NP(tcsetattr),
305 REAL_FUNC_NP(system),
307 REAL_FUNC_NP(execve),
311 #define open p_real_open
312 #define fopen p_real_fopen
313 #define mmap p_real_mmap
314 #define munmap p_real_munmap
315 #define read p_real_read
316 #define ioctl p_real_ioctl
317 #define sigaction p_real_sigaction
318 #define tcgetattr p_real_tcgetattr
319 #define tcsetattr p_real_tcsetattr
320 #define system p_real_system
321 #define execve p_real_execve
322 #define chdir p_real_chdir
329 // just call real funcs for static, pointer for dynamic
330 int real_open(const char *pathname, int flags, ...)
336 mode = va_arg(ap, mode_t);
338 return open(pathname, flags, mode);
341 FILE *real_fopen(const char *path, const char *mode)
343 return fopen(path, mode);
346 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
348 return mmap(addr, length, prot, flags, fd, offset);
351 int real_munmap(void *addr, size_t length)
353 return munmap(addr, length);
356 int real_read(int fd, void *buf, size_t count)
358 return read(fd, buf, count);
361 int real_ioctl(int d, int request, void *argp)
363 return ioctl(d, request, argp);
366 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
368 return sigaction(signum, act, oldact);
371 int real_tcgetattr(int fd, struct termios *termios_p)
373 return tcgetattr(fd, termios_p);
376 int real_tcsetattr(int fd, int optional_actions,
377 const struct termios *termios_p)
379 return tcsetattr(fd, optional_actions, termios_p);
382 int real_system(const char *command)
384 return system(command);
387 // real_exec* is missing intentionally - we don't need them
389 int real_execve(const char *filename, char *const argv[],
392 return execve(filename, argv, envp);
395 int real_chdir(const char *path)
400 // vim:shiftwidth=2:expandtab