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 },
45 { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
49 static long w_open_raw(const char *pathname, int flags, mode_t mode)
54 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
55 const char *p, *oname;
58 oname = takeover_devs[i].name;
59 p = strchr(oname, '*');
63 len = strlen(oname) + 1;
65 if (strncmp(pathname, oname, len) == 0) {
66 ret = takeover_devs[i].fd;
71 if (i == ARRAY_SIZE(takeover_devs))
72 ret = g_open_raw(pathname, flags, mode);
75 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
76 struct dev_fd_t *eifd = &emu_interesting_fds[i];
77 if (strcmp(pathname, eifd->name) == 0) {
79 if (eifd->open_cb != NULL)
86 strace("open(%s) = %ld\n", pathname, ret);
90 static int w_open(const char *pathname, int flags, mode_t mode)
92 long ret = w_open_raw(pathname, flags, mode);
93 return g_syscall_error(ret);
96 static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
101 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
102 ret = emu_do_mmap(length, prot, flags, fd, offset);
104 ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
106 strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
107 addr, length, prot, flags, fd, (long)offset, ret);
111 static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
113 long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
114 return g_syscall_error(ret);
116 #define w_mmap2 w_mmap
118 static long w_munmap_raw(void *addr, size_t length)
122 ret = emu_do_munmap(addr, length);
124 ret = g_munmap_raw(addr, length);
126 strace("munmap(%p, %x) = %ld\n", addr, length, ret);
130 static int w_munmap(void *addr, size_t length)
132 long ret = w_munmap_raw(addr, length);
133 return g_syscall_error(ret);
136 long w_read_raw(int fd, void *buf, size_t count)
140 if (fd == FAKEDEV_GPIO)
141 ret = emu_read_gpiodev(buf, count);
143 ret = g_read_raw(fd, buf, count);
145 //strace("read(%d, %p, %ld) = %ld\n", fd, buf, count, ret);
149 static ssize_t w_read(int fd, void *buf, size_t count)
151 long ret = w_read_raw(fd, buf, count);
152 return g_syscall_error(ret);
155 long w_ioctl_raw(int fd, int request, void *argp)
159 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
160 fd == emu_interesting_fds[IFD_SOUND].fd)
161 ret = emu_do_ioctl(fd, request, argp);
163 ret = g_ioctl_raw(fd, request, argp);
165 strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
169 static int w_ioctl(int fd, int request, void *argp)
171 long ret = w_ioctl_raw(fd, request, argp);
172 return g_syscall_error(ret);
175 static int w_sigaction(int signum, const void *act, void *oldact)
177 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
182 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
185 if (fd != FAKEDEV_TTY0)
186 ret = tcgetattr(fd, termios_p);
190 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
194 static UNUSED int w_tcsetattr(int fd, int optional_actions,
195 const struct termios *termios_p)
198 if (fd != FAKEDEV_TTY0)
199 ret = tcsetattr(fd, optional_actions, termios_p);
203 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
207 static UNUSED FILE *w_fopen(const char *path, const char *mode)
209 FILE *ret = emu_do_fopen(path, mode);
210 strace("fopen(%s, %s) = %p\n", path, mode, ret);
214 static UNUSED int w_system(const char *command)
216 int ret = emu_do_system(command);
217 strace("system(%s) = %d\n", command, ret);
221 extern char **environ;
223 static UNUSED int w_execl(const char *path, const char *arg, ...)
225 // don't allow exec (for now)
226 strace("execl(%s, %s, ...) = ?\n", path, arg);
230 static UNUSED int w_execlp(const char *file, const char *arg, ...)
232 strace("execlp(%s, %s, ...) = ?\n", file, arg);
236 static UNUSED int w_execle(const char *path, const char *arg, ...)
238 strace("execle(%s, %s, ...) = ?\n", path, arg);
242 static UNUSED int w_execv(const char *path, char *const argv[])
244 strace("execv(%s, %p) = ?\n", path, argv);
245 return emu_do_execve(path, argv, environ);
248 static UNUSED int w_execvp(const char *file, char *const argv[])
250 strace("execvp(%s, %p) = ?\n", file, argv);
251 return emu_do_execve(file, argv, environ);
254 // static note: this can't safely return because of the way it's patched in
255 // static note2: can't be used, execve hangs?
256 static UNUSED int w_execve(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 int w_chdir(const char *path)
267 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
270 ret = g_chdir_raw(path);
272 strace("chdir(%s) = %ld\n", path, ret);
273 return g_syscall_error(ret);
276 static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz)
280 if (path != NULL && strncmp(path, "/proc/", 6) == 0
281 && strcmp(strrchr(path, '/'), "/exe") == 0)
283 ret = snprintf(buf, bufsiz, "%s", bin_path);
288 ret = g_readlink_raw(path, buf, bufsiz);
290 strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret);
291 return g_syscall_error(ret);
316 #define MAKE_WRAP_SYM_N(sym) \
317 /* alias wrap symbols to real names */ \
318 typeof(sym) sym __attribute__((alias("w_" #sym)))
320 #define MAKE_WRAP_SYM(sym) \
321 MAKE_WRAP_SYM_N(sym); \
322 /* wrapper to real functions, to be set up on load */ \
323 static typeof(sym) *p_real_##sym
325 // note: update symver too
326 MAKE_WRAP_SYM_N(open);
327 MAKE_WRAP_SYM(fopen);
328 MAKE_WRAP_SYM_N(mmap);
329 MAKE_WRAP_SYM_N(munmap);
330 MAKE_WRAP_SYM_N(read);
331 MAKE_WRAP_SYM_N(ioctl);
332 MAKE_WRAP_SYM(sigaction);
333 MAKE_WRAP_SYM(tcgetattr);
334 MAKE_WRAP_SYM(tcsetattr);
335 MAKE_WRAP_SYM(system);
336 MAKE_WRAP_SYM_N(execl);
337 MAKE_WRAP_SYM_N(execlp);
338 MAKE_WRAP_SYM_N(execle);
339 MAKE_WRAP_SYM_N(execv);
340 MAKE_WRAP_SYM_N(execvp);
341 MAKE_WRAP_SYM(execve);
342 MAKE_WRAP_SYM_N(chdir);
343 MAKE_WRAP_SYM_N(readlink);
344 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
346 #define REAL_FUNC_NP(name) \
347 { #name, (void **)&p_real_##name }
349 static const struct {
352 } real_funcs_np[] = {
353 //REAL_FUNC_NP(open),
355 //REAL_FUNC_NP(mmap),
356 //REAL_FUNC_NP(munmap),
357 //REAL_FUNC_NP(read),
358 //REAL_FUNC_NP(ioctl),
359 REAL_FUNC_NP(sigaction),
360 REAL_FUNC_NP(tcgetattr),
361 REAL_FUNC_NP(tcsetattr),
362 REAL_FUNC_NP(system),
364 REAL_FUNC_NP(execve),
365 //REAL_FUNC_NP(chdir),
368 //#define open p_real_open
369 #define fopen p_real_fopen
370 //#define mmap p_real_mmap
371 //#define munmap p_real_munmap
372 //#define read p_real_read
373 //#define ioctl p_real_ioctl
374 #define sigaction p_real_sigaction
375 #define tcgetattr p_real_tcgetattr
376 #define tcsetattr p_real_tcsetattr
377 #define system p_real_system
378 #define execve p_real_execve
379 //#define chdir p_real_chdir
386 // just call real funcs for static, pointer for dynamic
387 int real_open(const char *pathname, int flags, ...)
394 mode = va_arg(ap, mode_t);
396 ret = g_open_raw(pathname, flags, mode);
397 return g_syscall_error(ret);
400 FILE *real_fopen(const char *path, const char *mode)
402 return fopen(path, mode);
405 void *real_mmap(void *addr, size_t length, int prot, int flags,
406 int fd, off_t offset)
408 long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
409 return (void *)g_syscall_error(ret);
412 int real_munmap(void *addr, size_t length)
414 return g_syscall_error(g_munmap_raw(addr, length));
417 int real_read(int fd, void *buf, size_t count)
419 long ret = g_read_raw(fd, buf, count);
420 return g_syscall_error(ret);
423 int real_ioctl(int fd, int request, void *argp)
425 long ret = g_ioctl_raw(fd, request, argp);
426 return g_syscall_error(ret);
429 int real_close(int fd)
431 long ret = g_close_raw(fd);
432 return g_syscall_error(ret);
435 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
437 return sigaction(signum, act, oldact);
440 int real_tcgetattr(int fd, struct termios *termios_p)
442 return tcgetattr(fd, termios_p);
445 int real_tcsetattr(int fd, int optional_actions,
446 const struct termios *termios_p)
448 return tcsetattr(fd, optional_actions, termios_p);
451 int real_system(const char *command)
453 return system(command);
456 // real_exec* is missing intentionally - we don't need them
458 int real_execve(const char *filename, char *const argv[],
461 return execve(filename, argv, envp);
464 int real_chdir(const char *path)
466 long ret = g_chdir_raw(path);
467 return g_syscall_error(ret);
470 ssize_t real_readlink(const char *path, char *buf, size_t bufsiz)
472 long ret = g_readlink_raw(path, buf, bufsiz);
473 return g_syscall_error(ret);
476 void real_sleep(unsigned int seconds)
478 struct timespec ts = { seconds, 0 };
479 g_nanosleep_raw(&ts, NULL);
482 void real_usleep(unsigned int usec)
484 struct timespec ts = { usec / 1000000, usec % 1000000 };
485 g_nanosleep_raw(&ts, NULL);
488 void real_exit(int status)
490 g_exit_group_raw(status);
493 // vim:shiftwidth=2:expandtab