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>
20 #include "realfuncs.h"
29 #define UNUSED __attribute__((unused))
31 static const struct dev_fd_t takeover_devs[] = {
32 { "/dev/mem", FAKEDEV_MEM },
33 { "/dev/GPIO", FAKEDEV_GPIO },
34 { "/dev/fb0", FAKEDEV_FB0 },
35 { "/dev/fb/0", FAKEDEV_FB0 },
36 { "/dev/fb1", FAKEDEV_FB1 },
37 { "/dev/fb/1", FAKEDEV_FB1 },
38 { "/dev/mmuhack", FAKEDEV_MMUHACK },
39 { "/dev/tty", FAKEDEV_TTY0 },
40 { "/dev/tty0", FAKEDEV_TTY0 },
42 { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
46 static int w_open(const char *pathname, int flags, mode_t mode)
50 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
51 const char *p, *oname;
54 oname = takeover_devs[i].name;
55 p = strchr(oname, '*');
59 len = strlen(oname) + 1;
61 if (strncmp(pathname, oname, len) == 0) {
62 ret = takeover_devs[i].fd;
67 if (i == ARRAY_SIZE(takeover_devs))
68 ret = open(pathname, flags, mode);
71 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
72 struct dev_fd_t *eifd = &emu_interesting_fds[i];
73 if (strcmp(pathname, eifd->name) == 0) {
75 if (eifd->open_cb != NULL)
82 strace("open(%s) = %d\n", pathname, ret);
86 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
89 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
90 ret = emu_do_mmap(length, prot, flags, fd, offset);
92 ret = mmap(addr, length, prot, flags, fd, offset);
94 // threads are using heap before they mmap their stack
95 // printf needs valid stack for pthread/errno
96 if (((long)&ret & 0xf0000000) == 0xb0000000)
97 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
100 #define w_mmap2 w_mmap
102 static ssize_t w_read(int fd, void *buf, size_t count)
105 if (fd == FAKEDEV_GPIO)
106 ret = emu_read_gpiodev(buf, count);
108 ret = read(fd, buf, count);
110 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
114 static int w_ioctl(int fd, int request, void *argp)
118 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
119 fd == emu_interesting_fds[IFD_SOUND].fd)
120 ret = emu_do_ioctl(fd, request, argp);
122 ret = ioctl(fd, request, argp);
124 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
128 static int w_sigaction(int signum, const void *act, void *oldact)
130 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
135 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
138 if (fd != FAKEDEV_TTY0)
139 ret = tcgetattr(fd, termios_p);
143 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
147 static UNUSED int w_tcsetattr(int fd, int optional_actions,
148 const struct termios *termios_p)
151 if (fd != FAKEDEV_TTY0)
152 ret = tcsetattr(fd, optional_actions, termios_p);
156 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
160 static UNUSED FILE *w_fopen(const char *path, const char *mode)
162 FILE *ret = emu_do_fopen(path, mode);
163 strace("fopen(%s, %s) = %p\n", path, mode, ret);
167 static UNUSED int w_system(const char *command)
169 int ret = emu_do_system(command);
170 strace("system(%s) = %d\n", command, ret);
174 extern char **environ;
176 static UNUSED int w_execl(const char *path, const char *arg, ...)
178 // don't allow exec (for now)
179 strace("execl(%s, %s, ...) = ?\n", path, arg);
183 static UNUSED int w_execlp(const char *file, const char *arg, ...)
185 strace("execlp(%s, %s, ...) = ?\n", file, arg);
189 static UNUSED int w_execle(const char *path, const char *arg, ...)
191 strace("execle(%s, %s, ...) = ?\n", path, arg);
195 static UNUSED int w_execv(const char *path, char *const argv[])
197 strace("execv(%s, %p) = ?\n", path, argv);
198 return emu_do_execve(path, argv, environ);
201 static UNUSED int w_execvp(const char *file, char *const argv[])
203 strace("execvp(%s, %p) = ?\n", file, argv);
204 return emu_do_execve(file, argv, environ);
207 // static note: this can't safely return because of the way it's patched in
208 // static note2: can't be used, execve hangs?
209 static UNUSED int w_execve(const char *filename, char *const argv[],
212 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
213 return emu_do_execve(filename, argv, envp);
216 static int w_chdir(const char *path)
219 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
223 strace("chdir(%s) = %d\n", path, ret);
246 #define MAKE_WRAP_SYM_N(sym) \
247 /* alias wrap symbols to real names */ \
248 typeof(sym) sym __attribute__((alias("w_" #sym)))
250 #define MAKE_WRAP_SYM(sym) \
251 MAKE_WRAP_SYM_N(sym); \
252 /* wrapper to real functions, to be set up on load */ \
253 static typeof(sym) *p_real_##sym
255 // note: update symver too
257 MAKE_WRAP_SYM(fopen);
260 MAKE_WRAP_SYM(ioctl);
261 MAKE_WRAP_SYM(sigaction);
262 MAKE_WRAP_SYM(tcgetattr);
263 MAKE_WRAP_SYM(tcsetattr);
264 MAKE_WRAP_SYM(system);
265 MAKE_WRAP_SYM_N(execl);
266 MAKE_WRAP_SYM_N(execlp);
267 MAKE_WRAP_SYM_N(execle);
268 MAKE_WRAP_SYM_N(execv);
269 MAKE_WRAP_SYM_N(execvp);
270 MAKE_WRAP_SYM(execve);
271 MAKE_WRAP_SYM(chdir);
272 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
274 #define REAL_FUNC_NP(name) \
275 { #name, (void **)&p_real_##name }
277 static const struct {
280 } real_funcs_np[] = {
286 REAL_FUNC_NP(sigaction),
287 REAL_FUNC_NP(tcgetattr),
288 REAL_FUNC_NP(tcsetattr),
289 REAL_FUNC_NP(system),
291 REAL_FUNC_NP(execve),
295 #define open p_real_open
296 #define fopen p_real_fopen
297 #define mmap p_real_mmap
298 #define read p_real_read
299 #define ioctl p_real_ioctl
300 #define sigaction p_real_sigaction
301 #define tcgetattr p_real_tcgetattr
302 #define tcsetattr p_real_tcsetattr
303 #define system p_real_system
304 #define execve p_real_execve
305 #define chdir p_real_chdir
312 // just call real funcs for static, pointer for dynamic
313 int real_open(const char *pathname, int flags, ...)
319 mode = va_arg(ap, mode_t);
321 return open(pathname, flags, mode);
324 FILE *real_fopen(const char *path, const char *mode)
326 return fopen(path, mode);
329 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
331 return mmap(addr, length, prot, flags, fd, offset);
334 int real_read(int fd, void *buf, size_t count)
336 return read(fd, buf, count);
339 int real_ioctl(int d, int request, void *argp)
341 return ioctl(d, request, argp);
344 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
346 return sigaction(signum, act, oldact);
349 int real_tcgetattr(int fd, struct termios *termios_p)
351 return tcgetattr(fd, termios_p);
354 int real_tcsetattr(int fd, int optional_actions,
355 const struct termios *termios_p)
357 return tcsetattr(fd, optional_actions, termios_p);
360 int real_system(const char *command)
362 return system(command);
365 // real_exec* is missing intentionally - we don't need them
367 int real_execve(const char *filename, char *const argv[],
370 return execve(filename, argv, envp);
373 int real_chdir(const char *path)
378 // vim:shiftwidth=2:expandtab