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_TTY },
43 { "/dev/tty0", FAKEDEV_TTY0 },
44 { "/dev/touchscreen/wm97xx", FAKEDEV_WM97XX },
45 { "/etc/pointercal", FAKEDEV_WM97XX_P },
46 { "/dev/input/mice", -ENODEV },
48 { "/dev/input/event*", -ENODEV }, // hide for now, may cause dupe events
52 long w_open_raw(const char *pathname, int flags, mode_t mode)
57 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
58 const char *p, *oname;
61 oname = takeover_devs[i].name;
62 p = strchr(oname, '*');
66 len = strlen(oname) + 1;
68 if (strncmp(pathname, oname, len) == 0) {
69 ret = takeover_devs[i].fd;
74 if (i == ARRAY_SIZE(takeover_devs)) {
75 const char *w_path = emu_wrap_path(pathname);
76 ret = g_open_raw(w_path, flags, mode);
77 emu_wrap_path_free(w_path, pathname);
81 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
82 struct dev_fd_t *eifd = &emu_interesting_fds[i];
83 if (strcmp(pathname, eifd->name) == 0) {
85 if (eifd->open_cb != NULL)
92 strace("open(%s) = %ld\n", pathname, ret);
96 static int w_open(const char *pathname, int flags, mode_t mode)
98 long ret = w_open_raw(pathname, flags, mode);
99 return g_syscall_error(ret);
102 static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
103 int fd, off_t offset)
107 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
108 ret = emu_do_mmap(length, prot, flags, fd, offset);
110 ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
112 strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
113 addr, length, prot, flags, fd, (long)offset, ret);
117 static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
119 long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
120 return g_syscall_error(ret);
122 #define w_mmap2 w_mmap
124 static long w_munmap_raw(void *addr, size_t length)
128 ret = emu_do_munmap(addr, length);
130 ret = g_munmap_raw(addr, length);
132 strace("munmap(%p, %x) = %ld\n", addr, length, ret);
136 static int w_munmap(void *addr, size_t length)
138 long ret = w_munmap_raw(addr, length);
139 return g_syscall_error(ret);
142 long w_read_raw(int fd, void *buf, size_t count)
146 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
147 ret = emu_do_read(fd, buf, count);
149 ret = g_read_raw(fd, buf, count);
151 //strace("read(%d, %p, %zd) = %ld\n", fd, buf, count, ret);
155 static ssize_t w_read(int fd, void *buf, size_t count)
157 long ret = w_read_raw(fd, buf, count);
158 return g_syscall_error(ret);
161 long w_ioctl_raw(int fd, int request, void *argp)
165 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
166 fd == emu_interesting_fds[IFD_SOUND].fd)
167 ret = emu_do_ioctl(fd, request, argp);
169 ret = g_ioctl_raw(fd, request, argp);
171 strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
175 static int w_ioctl(int fd, int request, void *argp)
177 long ret = w_ioctl_raw(fd, request, argp);
178 return g_syscall_error(ret);
181 static int w_sigaction(int signum, const void *act, void *oldact)
183 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
188 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
191 if (fd != FAKEDEV_TTY0)
192 ret = tcgetattr(fd, termios_p);
196 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
200 static UNUSED int w_tcsetattr(int fd, int optional_actions,
201 const struct termios *termios_p)
204 if (fd != FAKEDEV_TTY0)
205 ret = tcsetattr(fd, optional_actions, termios_p);
209 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
213 static UNUSED FILE *w_fopen(const char *path, const char *mode)
215 FILE *ret = emu_do_fopen(path, mode);
216 strace("fopen(%s, %s) = %p\n", path, mode, ret);
220 static UNUSED int w_system(const char *command)
222 int ret = emu_do_system(command);
223 strace("system(%s) = %d\n", command, ret);
227 extern char **environ;
229 static UNUSED int w_execl(const char *path, const char *arg, ...)
231 // don't allow exec (for now)
232 strace("execl(%s, %s, ...) = ?\n", path, arg);
236 static UNUSED int w_execlp(const char *file, const char *arg, ...)
238 strace("execlp(%s, %s, ...) = ?\n", file, arg);
242 static UNUSED int w_execle(const char *path, const char *arg, ...)
244 strace("execle(%s, %s, ...) = ?\n", path, arg);
248 static UNUSED int w_execv(const char *path, char *const argv[])
250 strace("execv(%s, %p) = ?\n", path, argv);
251 return emu_do_execve(path, argv, environ);
254 static UNUSED int w_execvp(const char *file, char *const argv[])
256 strace("execvp(%s, %p) = ?\n", file, argv);
257 return emu_do_execve(file, argv, environ);
260 long w_execve_raw(const char *filename, char * const argv[],
263 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
264 return emu_do_execve(filename, argv, envp);
267 static UNUSED int w_execve(const char *filename, char * const argv[],
270 long ret = w_execve_raw(filename, argv, envp);
271 return g_syscall_error(ret);
274 static int w_chdir(const char *path)
278 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
281 ret = g_chdir_raw(path);
283 strace("chdir(%s) = %ld\n", path, ret);
284 return g_syscall_error(ret);
287 static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz)
292 if (path != NULL && strncmp(path, "/proc/", 6) == 0
293 && strcmp(strrchr(path, '/'), "/exe") == 0)
295 ret = snprintf(buf, bufsiz, "%s", bin_path);
301 ret = g_readlink_raw(path, buf, bufsiz);
303 strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret);
304 return g_syscall_error(ret);
329 #define MAKE_WRAP_SYM_N(sym) \
330 /* alias wrap symbols to real names */ \
331 typeof(sym) sym __attribute__((alias("w_" #sym)))
333 #define MAKE_WRAP_SYM(sym) \
334 MAKE_WRAP_SYM_N(sym); \
335 /* wrapper to real functions, to be set up on load */ \
336 static typeof(sym) *p_real_##sym
338 // note: update symver too
339 MAKE_WRAP_SYM_N(open);
340 MAKE_WRAP_SYM(fopen);
341 MAKE_WRAP_SYM_N(mmap);
342 MAKE_WRAP_SYM_N(munmap);
343 MAKE_WRAP_SYM_N(read);
344 MAKE_WRAP_SYM_N(ioctl);
345 MAKE_WRAP_SYM(sigaction);
346 MAKE_WRAP_SYM(tcgetattr);
347 MAKE_WRAP_SYM(tcsetattr);
348 MAKE_WRAP_SYM(system);
349 MAKE_WRAP_SYM_N(execl);
350 MAKE_WRAP_SYM_N(execlp);
351 MAKE_WRAP_SYM_N(execle);
352 MAKE_WRAP_SYM_N(execv);
353 MAKE_WRAP_SYM_N(execvp);
354 MAKE_WRAP_SYM_N(execve);
355 MAKE_WRAP_SYM_N(chdir);
356 MAKE_WRAP_SYM_N(readlink);
357 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
359 #define REAL_FUNC_NP(name) \
360 { #name, (void **)&p_real_##name }
362 static const struct {
365 } real_funcs_np[] = {
366 //REAL_FUNC_NP(open),
368 //REAL_FUNC_NP(mmap),
369 //REAL_FUNC_NP(munmap),
370 //REAL_FUNC_NP(read),
371 //REAL_FUNC_NP(ioctl),
372 REAL_FUNC_NP(sigaction),
373 REAL_FUNC_NP(tcgetattr),
374 REAL_FUNC_NP(tcsetattr),
375 REAL_FUNC_NP(system),
377 //REAL_FUNC_NP(execve),
378 //REAL_FUNC_NP(chdir),
381 //#define open p_real_open
382 #define fopen p_real_fopen
383 //#define mmap p_real_mmap
384 //#define munmap p_real_munmap
385 //#define read p_real_read
386 //#define ioctl p_real_ioctl
387 #define sigaction p_real_sigaction
388 #define tcgetattr p_real_tcgetattr
389 #define tcsetattr p_real_tcsetattr
390 #define system p_real_system
391 //#define execve p_real_execve
392 //#define chdir p_real_chdir
399 // just call real funcs for static, pointer for dynamic
400 int real_open(const char *pathname, int flags, ...)
407 mode = va_arg(ap, mode_t);
409 ret = g_open_raw(pathname, flags, mode);
410 return g_syscall_error(ret);
413 FILE *real_fopen(const char *path, const char *mode)
415 return fopen(path, mode);
418 void *real_mmap(void *addr, size_t length, int prot, int flags,
419 int fd, off_t offset)
421 long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
422 return (void *)g_syscall_error(ret);
425 int real_munmap(void *addr, size_t length)
427 return g_syscall_error(g_munmap_raw(addr, length));
430 int real_read(int fd, void *buf, size_t count)
432 long ret = g_read_raw(fd, buf, count);
433 return g_syscall_error(ret);
436 int real_ioctl(int fd, int request, void *argp)
438 long ret = g_ioctl_raw(fd, request, argp);
439 return g_syscall_error(ret);
442 int real_close(int fd)
444 long ret = g_close_raw(fd);
445 return g_syscall_error(ret);
448 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
450 return sigaction(signum, act, oldact);
453 int real_tcgetattr(int fd, struct termios *termios_p)
455 return tcgetattr(fd, termios_p);
458 int real_tcsetattr(int fd, int optional_actions,
459 const struct termios *termios_p)
461 return tcsetattr(fd, optional_actions, termios_p);
464 int real_system(const char *command)
466 return system(command);
469 // real_exec* is missing intentionally - we don't need them
471 int real_execve(const char *filename, char *const argv[],
474 long ret = g_execve_raw(filename, argv, envp);
475 return g_syscall_error(ret);
478 int real_chdir(const char *path)
480 long ret = g_chdir_raw(path);
481 return g_syscall_error(ret);
484 ssize_t real_readlink(const char *path, char *buf, size_t bufsiz)
486 long ret = g_readlink_raw(path, buf, bufsiz);
487 return g_syscall_error(ret);
490 void real_sleep(unsigned int seconds)
492 struct timespec ts = { seconds, 0 };
493 g_nanosleep_raw(&ts, NULL);
496 void real_usleep(unsigned int usec)
498 struct timespec ts = { usec / 1000000, usec % 1000000 };
499 g_nanosleep_raw(&ts, NULL);
502 void real_exit(int status)
504 g_exit_group_raw(status);
507 // vim:shiftwidth=2:expandtab