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.
11 #include <sys/types.h>
16 #include <sys/ioctl.h>
21 #include "realfuncs.h"
27 #define strace g_printf
32 #define UNUSED __attribute__((unused))
34 static const struct dev_fd_t takeover_devs[] = {
35 { "/dev/mem", FAKEDEV_MEM },
36 { "/dev/GPIO", FAKEDEV_GPIO },
37 { "/dev/fb0", FAKEDEV_FB0 },
38 { "/dev/fb/0", FAKEDEV_FB0 },
39 { "/dev/fb1", FAKEDEV_FB1 },
40 { "/dev/fb/1", FAKEDEV_FB1 },
41 { "/dev/mmuhack", FAKEDEV_MMUHACK },
42 { "/dev/tty", FAKEDEV_TTY0 },
43 { "/dev/tty0", FAKEDEV_TTY0 },
44 { "/dev/touchscreen/wm97xx", FAKEDEV_WM97XX },
45 { "/etc/pointercal", FAKEDEV_WM97XX_P },
47 { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
51 long w_open_raw(const char *pathname, int flags, mode_t mode)
56 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
57 const char *p, *oname;
60 oname = takeover_devs[i].name;
61 p = strchr(oname, '*');
65 len = strlen(oname) + 1;
67 if (strncmp(pathname, oname, len) == 0) {
68 ret = takeover_devs[i].fd;
73 if (i == ARRAY_SIZE(takeover_devs)) {
74 const char *w_path = emu_wrap_path(pathname);
75 ret = g_open_raw(w_path, flags, mode);
76 emu_wrap_path_free(w_path, pathname);
80 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
81 struct dev_fd_t *eifd = &emu_interesting_fds[i];
82 if (strcmp(pathname, eifd->name) == 0) {
84 if (eifd->open_cb != NULL)
91 strace("open(%s) = %ld\n", pathname, ret);
95 static int w_open(const char *pathname, int flags, mode_t mode)
97 long ret = w_open_raw(pathname, flags, mode);
98 return g_syscall_error(ret);
101 static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
102 int fd, off_t offset)
106 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
107 ret = emu_do_mmap(length, prot, flags, fd, offset);
109 ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
111 strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
112 addr, length, prot, flags, fd, (long)offset, ret);
116 static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
118 long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
119 return g_syscall_error(ret);
121 #define w_mmap2 w_mmap
123 static long w_munmap_raw(void *addr, size_t length)
127 ret = emu_do_munmap(addr, length);
129 ret = g_munmap_raw(addr, length);
131 strace("munmap(%p, %x) = %ld\n", addr, length, ret);
135 static int w_munmap(void *addr, size_t length)
137 long ret = w_munmap_raw(addr, length);
138 return g_syscall_error(ret);
141 long w_read_raw(int fd, void *buf, size_t count)
145 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
146 ret = emu_do_read(fd, buf, count);
148 ret = g_read_raw(fd, buf, count);
150 //strace("read(%d, %p, %zd) = %ld\n", fd, buf, count, ret);
154 static ssize_t w_read(int fd, void *buf, size_t count)
156 long ret = w_read_raw(fd, buf, count);
157 return g_syscall_error(ret);
160 long w_ioctl_raw(int fd, int request, void *argp)
164 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
165 fd == emu_interesting_fds[IFD_SOUND].fd)
166 ret = emu_do_ioctl(fd, request, argp);
168 ret = g_ioctl_raw(fd, request, argp);
170 strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
174 static int w_ioctl(int fd, int request, void *argp)
176 long ret = w_ioctl_raw(fd, request, argp);
177 return g_syscall_error(ret);
180 static int w_sigaction(int signum, const void *act, void *oldact)
182 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
187 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
190 if (fd != FAKEDEV_TTY0)
191 ret = tcgetattr(fd, termios_p);
195 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
199 static UNUSED int w_tcsetattr(int fd, int optional_actions,
200 const struct termios *termios_p)
203 if (fd != FAKEDEV_TTY0)
204 ret = tcsetattr(fd, optional_actions, termios_p);
208 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
212 static UNUSED FILE *w_fopen(const char *path, const char *mode)
214 FILE *ret = emu_do_fopen(path, mode);
215 strace("fopen(%s, %s) = %p\n", path, mode, ret);
219 static UNUSED int w_system(const char *command)
221 int ret = emu_do_system(command);
222 strace("system(%s) = %d\n", command, ret);
226 extern char **environ;
228 static UNUSED int w_execl(const char *path, const char *arg, ...)
230 // don't allow exec (for now)
231 strace("execl(%s, %s, ...) = ?\n", path, arg);
235 static UNUSED int w_execlp(const char *file, const char *arg, ...)
237 strace("execlp(%s, %s, ...) = ?\n", file, arg);
241 static UNUSED int w_execle(const char *path, const char *arg, ...)
243 strace("execle(%s, %s, ...) = ?\n", path, arg);
247 static UNUSED int w_execv(const char *path, char *const argv[])
249 strace("execv(%s, %p) = ?\n", path, argv);
250 return emu_do_execve(path, argv, environ);
253 static UNUSED int w_execvp(const char *file, char *const argv[])
255 strace("execvp(%s, %p) = ?\n", file, argv);
256 return emu_do_execve(file, argv, environ);
259 long w_execve_raw(const char *filename, char * const argv[],
262 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
263 return emu_do_execve(filename, argv, envp);
266 static UNUSED int w_execve(const char *filename, char * const argv[],
269 long ret = w_execve_raw(filename, argv, envp);
270 return g_syscall_error(ret);
273 static int w_chdir(const char *path)
277 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
280 ret = g_chdir_raw(path);
282 strace("chdir(%s) = %ld\n", path, ret);
283 return g_syscall_error(ret);
286 static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz)
291 if (path != NULL && strncmp(path, "/proc/", 6) == 0
292 && strcmp(strrchr(path, '/'), "/exe") == 0)
294 ret = snprintf(buf, bufsiz, "%s", bin_path);
300 ret = g_readlink_raw(path, buf, bufsiz);
302 strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret);
303 return g_syscall_error(ret);
328 #define MAKE_WRAP_SYM_N(sym) \
329 /* alias wrap symbols to real names */ \
330 typeof(sym) sym __attribute__((alias("w_" #sym)))
332 #define MAKE_WRAP_SYM(sym) \
333 MAKE_WRAP_SYM_N(sym); \
334 /* wrapper to real functions, to be set up on load */ \
335 static typeof(sym) *p_real_##sym
337 // note: update symver too
338 MAKE_WRAP_SYM_N(open);
339 MAKE_WRAP_SYM(fopen);
340 MAKE_WRAP_SYM_N(mmap);
341 MAKE_WRAP_SYM_N(munmap);
342 MAKE_WRAP_SYM_N(read);
343 MAKE_WRAP_SYM_N(ioctl);
344 MAKE_WRAP_SYM(sigaction);
345 MAKE_WRAP_SYM(tcgetattr);
346 MAKE_WRAP_SYM(tcsetattr);
347 MAKE_WRAP_SYM(system);
348 MAKE_WRAP_SYM_N(execl);
349 MAKE_WRAP_SYM_N(execlp);
350 MAKE_WRAP_SYM_N(execle);
351 MAKE_WRAP_SYM_N(execv);
352 MAKE_WRAP_SYM_N(execvp);
353 MAKE_WRAP_SYM_N(execve);
354 MAKE_WRAP_SYM_N(chdir);
355 MAKE_WRAP_SYM_N(readlink);
356 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
358 #define REAL_FUNC_NP(name) \
359 { #name, (void **)&p_real_##name }
361 static const struct {
364 } real_funcs_np[] = {
365 //REAL_FUNC_NP(open),
367 //REAL_FUNC_NP(mmap),
368 //REAL_FUNC_NP(munmap),
369 //REAL_FUNC_NP(read),
370 //REAL_FUNC_NP(ioctl),
371 REAL_FUNC_NP(sigaction),
372 REAL_FUNC_NP(tcgetattr),
373 REAL_FUNC_NP(tcsetattr),
374 REAL_FUNC_NP(system),
376 //REAL_FUNC_NP(execve),
377 //REAL_FUNC_NP(chdir),
380 //#define open p_real_open
381 #define fopen p_real_fopen
382 //#define mmap p_real_mmap
383 //#define munmap p_real_munmap
384 //#define read p_real_read
385 //#define ioctl p_real_ioctl
386 #define sigaction p_real_sigaction
387 #define tcgetattr p_real_tcgetattr
388 #define tcsetattr p_real_tcsetattr
389 #define system p_real_system
390 //#define execve p_real_execve
391 //#define chdir p_real_chdir
398 // just call real funcs for static, pointer for dynamic
399 int real_open(const char *pathname, int flags, ...)
406 mode = va_arg(ap, mode_t);
408 ret = g_open_raw(pathname, flags, mode);
409 return g_syscall_error(ret);
412 FILE *real_fopen(const char *path, const char *mode)
414 return fopen(path, mode);
417 void *real_mmap(void *addr, size_t length, int prot, int flags,
418 int fd, off_t offset)
420 long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
421 return (void *)g_syscall_error(ret);
424 int real_munmap(void *addr, size_t length)
426 return g_syscall_error(g_munmap_raw(addr, length));
429 int real_read(int fd, void *buf, size_t count)
431 long ret = g_read_raw(fd, buf, count);
432 return g_syscall_error(ret);
435 int real_ioctl(int fd, int request, void *argp)
437 long ret = g_ioctl_raw(fd, request, argp);
438 return g_syscall_error(ret);
441 int real_close(int fd)
443 long ret = g_close_raw(fd);
444 return g_syscall_error(ret);
447 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
449 return sigaction(signum, act, oldact);
452 int real_tcgetattr(int fd, struct termios *termios_p)
454 return tcgetattr(fd, termios_p);
457 int real_tcsetattr(int fd, int optional_actions,
458 const struct termios *termios_p)
460 return tcsetattr(fd, optional_actions, termios_p);
463 int real_system(const char *command)
465 return system(command);
468 // real_exec* is missing intentionally - we don't need them
470 int real_execve(const char *filename, char *const argv[],
473 long ret = g_execve_raw(filename, argv, envp);
474 return g_syscall_error(ret);
477 int real_chdir(const char *path)
479 long ret = g_chdir_raw(path);
480 return g_syscall_error(ret);
483 ssize_t real_readlink(const char *path, char *buf, size_t bufsiz)
485 long ret = g_readlink_raw(path, buf, bufsiz);
486 return g_syscall_error(ret);
489 void real_sleep(unsigned int seconds)
491 struct timespec ts = { seconds, 0 };
492 g_nanosleep_raw(&ts, NULL);
495 void real_usleep(unsigned int usec)
497 struct timespec ts = { usec / 1000000, usec % 1000000 };
498 g_nanosleep_raw(&ts, NULL);
501 void real_exit(int status)
503 g_exit_group_raw(status);
506 // vim:shiftwidth=2:expandtab