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 static 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 ret = g_open_raw(pathname, flags, mode);
77 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
78 struct dev_fd_t *eifd = &emu_interesting_fds[i];
79 if (strcmp(pathname, eifd->name) == 0) {
81 if (eifd->open_cb != NULL)
88 strace("open(%s) = %ld\n", pathname, ret);
92 static int w_open(const char *pathname, int flags, mode_t mode)
94 long ret = w_open_raw(pathname, flags, mode);
95 return g_syscall_error(ret);
98 static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
103 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
104 ret = emu_do_mmap(length, prot, flags, fd, offset);
106 ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
108 strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
109 addr, length, prot, flags, fd, (long)offset, ret);
113 static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
115 long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
116 return g_syscall_error(ret);
118 #define w_mmap2 w_mmap
120 static long w_munmap_raw(void *addr, size_t length)
124 ret = emu_do_munmap(addr, length);
126 ret = g_munmap_raw(addr, length);
128 strace("munmap(%p, %x) = %ld\n", addr, length, ret);
132 static int w_munmap(void *addr, size_t length)
134 long ret = w_munmap_raw(addr, length);
135 return g_syscall_error(ret);
138 long w_read_raw(int fd, void *buf, size_t count)
142 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
143 ret = emu_do_read(fd, buf, count);
145 ret = g_read_raw(fd, buf, count);
147 //strace("read(%d, %p, %zd) = %ld\n", fd, buf, count, ret);
151 static ssize_t w_read(int fd, void *buf, size_t count)
153 long ret = w_read_raw(fd, buf, count);
154 return g_syscall_error(ret);
157 long w_ioctl_raw(int fd, int request, void *argp)
161 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
162 fd == emu_interesting_fds[IFD_SOUND].fd)
163 ret = emu_do_ioctl(fd, request, argp);
165 ret = g_ioctl_raw(fd, request, argp);
167 strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
171 static int w_ioctl(int fd, int request, void *argp)
173 long ret = w_ioctl_raw(fd, request, argp);
174 return g_syscall_error(ret);
177 static int w_sigaction(int signum, const void *act, void *oldact)
179 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
184 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
187 if (fd != FAKEDEV_TTY0)
188 ret = tcgetattr(fd, termios_p);
192 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
196 static UNUSED int w_tcsetattr(int fd, int optional_actions,
197 const struct termios *termios_p)
200 if (fd != FAKEDEV_TTY0)
201 ret = tcsetattr(fd, optional_actions, termios_p);
205 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
209 static UNUSED FILE *w_fopen(const char *path, const char *mode)
211 FILE *ret = emu_do_fopen(path, mode);
212 strace("fopen(%s, %s) = %p\n", path, mode, ret);
216 static UNUSED int w_system(const char *command)
218 int ret = emu_do_system(command);
219 strace("system(%s) = %d\n", command, ret);
223 extern char **environ;
225 static UNUSED int w_execl(const char *path, const char *arg, ...)
227 // don't allow exec (for now)
228 strace("execl(%s, %s, ...) = ?\n", path, arg);
232 static UNUSED int w_execlp(const char *file, const char *arg, ...)
234 strace("execlp(%s, %s, ...) = ?\n", file, arg);
238 static UNUSED int w_execle(const char *path, const char *arg, ...)
240 strace("execle(%s, %s, ...) = ?\n", path, arg);
244 static UNUSED int w_execv(const char *path, char *const argv[])
246 strace("execv(%s, %p) = ?\n", path, argv);
247 return emu_do_execve(path, argv, environ);
250 static UNUSED int w_execvp(const char *file, char *const argv[])
252 strace("execvp(%s, %p) = ?\n", file, argv);
253 return emu_do_execve(file, argv, environ);
256 long w_execve_raw(const char *filename, char * const argv[],
259 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
260 return emu_do_execve(filename, argv, envp);
263 static UNUSED int w_execve(const char *filename, char * const argv[],
266 long ret = w_execve_raw(filename, argv, envp);
267 return g_syscall_error(ret);
270 static int w_chdir(const char *path)
274 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
277 ret = g_chdir_raw(path);
279 strace("chdir(%s) = %ld\n", path, ret);
280 return g_syscall_error(ret);
283 static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz)
288 if (path != NULL && strncmp(path, "/proc/", 6) == 0
289 && strcmp(strrchr(path, '/'), "/exe") == 0)
291 ret = snprintf(buf, bufsiz, "%s", bin_path);
297 ret = g_readlink_raw(path, buf, bufsiz);
299 strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret);
300 return g_syscall_error(ret);
325 #define MAKE_WRAP_SYM_N(sym) \
326 /* alias wrap symbols to real names */ \
327 typeof(sym) sym __attribute__((alias("w_" #sym)))
329 #define MAKE_WRAP_SYM(sym) \
330 MAKE_WRAP_SYM_N(sym); \
331 /* wrapper to real functions, to be set up on load */ \
332 static typeof(sym) *p_real_##sym
334 // note: update symver too
335 MAKE_WRAP_SYM_N(open);
336 MAKE_WRAP_SYM(fopen);
337 MAKE_WRAP_SYM_N(mmap);
338 MAKE_WRAP_SYM_N(munmap);
339 MAKE_WRAP_SYM_N(read);
340 MAKE_WRAP_SYM_N(ioctl);
341 MAKE_WRAP_SYM(sigaction);
342 MAKE_WRAP_SYM(tcgetattr);
343 MAKE_WRAP_SYM(tcsetattr);
344 MAKE_WRAP_SYM(system);
345 MAKE_WRAP_SYM_N(execl);
346 MAKE_WRAP_SYM_N(execlp);
347 MAKE_WRAP_SYM_N(execle);
348 MAKE_WRAP_SYM_N(execv);
349 MAKE_WRAP_SYM_N(execvp);
350 MAKE_WRAP_SYM_N(execve);
351 MAKE_WRAP_SYM_N(chdir);
352 MAKE_WRAP_SYM_N(readlink);
353 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
355 #define REAL_FUNC_NP(name) \
356 { #name, (void **)&p_real_##name }
358 static const struct {
361 } real_funcs_np[] = {
362 //REAL_FUNC_NP(open),
364 //REAL_FUNC_NP(mmap),
365 //REAL_FUNC_NP(munmap),
366 //REAL_FUNC_NP(read),
367 //REAL_FUNC_NP(ioctl),
368 REAL_FUNC_NP(sigaction),
369 REAL_FUNC_NP(tcgetattr),
370 REAL_FUNC_NP(tcsetattr),
371 REAL_FUNC_NP(system),
373 //REAL_FUNC_NP(execve),
374 //REAL_FUNC_NP(chdir),
377 //#define open p_real_open
378 #define fopen p_real_fopen
379 //#define mmap p_real_mmap
380 //#define munmap p_real_munmap
381 //#define read p_real_read
382 //#define ioctl p_real_ioctl
383 #define sigaction p_real_sigaction
384 #define tcgetattr p_real_tcgetattr
385 #define tcsetattr p_real_tcsetattr
386 #define system p_real_system
387 //#define execve p_real_execve
388 //#define chdir p_real_chdir
395 // just call real funcs for static, pointer for dynamic
396 int real_open(const char *pathname, int flags, ...)
403 mode = va_arg(ap, mode_t);
405 ret = g_open_raw(pathname, flags, mode);
406 return g_syscall_error(ret);
409 FILE *real_fopen(const char *path, const char *mode)
411 return fopen(path, mode);
414 void *real_mmap(void *addr, size_t length, int prot, int flags,
415 int fd, off_t offset)
417 long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
418 return (void *)g_syscall_error(ret);
421 int real_munmap(void *addr, size_t length)
423 return g_syscall_error(g_munmap_raw(addr, length));
426 int real_read(int fd, void *buf, size_t count)
428 long ret = g_read_raw(fd, buf, count);
429 return g_syscall_error(ret);
432 int real_ioctl(int fd, int request, void *argp)
434 long ret = g_ioctl_raw(fd, request, argp);
435 return g_syscall_error(ret);
438 int real_close(int fd)
440 long ret = g_close_raw(fd);
441 return g_syscall_error(ret);
444 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
446 return sigaction(signum, act, oldact);
449 int real_tcgetattr(int fd, struct termios *termios_p)
451 return tcgetattr(fd, termios_p);
454 int real_tcsetattr(int fd, int optional_actions,
455 const struct termios *termios_p)
457 return tcsetattr(fd, optional_actions, termios_p);
460 int real_system(const char *command)
462 return system(command);
465 // real_exec* is missing intentionally - we don't need them
467 int real_execve(const char *filename, char *const argv[],
470 long ret = g_execve_raw(filename, argv, envp);
471 return g_syscall_error(ret);
474 int real_chdir(const char *path)
476 long ret = g_chdir_raw(path);
477 return g_syscall_error(ret);
480 ssize_t real_readlink(const char *path, char *buf, size_t bufsiz)
482 long ret = g_readlink_raw(path, buf, bufsiz);
483 return g_syscall_error(ret);
486 void real_sleep(unsigned int seconds)
488 struct timespec ts = { seconds, 0 };
489 g_nanosleep_raw(&ts, NULL);
492 void real_usleep(unsigned int usec)
494 struct timespec ts = { usec / 1000000, usec % 1000000 };
495 g_nanosleep_raw(&ts, NULL);
498 void real_exit(int status)
500 g_exit_group_raw(status);
503 // vim:shiftwidth=2:expandtab