/*
* GINGE - GINGE Is Not Gp2x Emulator
- * (C) notaz, 2010-2011
+ * (C) notaz, 2010-2011,2016
*
* This work is licensed under the MAME license, see COPYING file for details.
*/
#include <errno.h>
#include "realfuncs.h"
+#include "syscalls.h"
+#include "llibc.h"
#include "header.h"
#if (DBG & 1)
-#define strace printf
+#define strace g_printf
#else
#define strace(...)
#endif
{ "/dev/fb1", FAKEDEV_FB1 },
{ "/dev/fb/1", FAKEDEV_FB1 },
{ "/dev/mmuhack", FAKEDEV_MMUHACK },
- { "/dev/tty", FAKEDEV_TTY0 },
+ { "/dev/tty", FAKEDEV_TTY },
{ "/dev/tty0", FAKEDEV_TTY0 },
+ { "/dev/touchscreen/wm97xx", FAKEDEV_WM97XX },
+ { "/etc/pointercal", FAKEDEV_WM97XX_P },
+ { "/dev/input/mice", -ENODEV },
#ifdef PND
- { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
+ { "/dev/input/event*", -ENODEV }, // hide for now, may cause dupe events
#endif
};
-static int w_open(const char *pathname, int flags, mode_t mode)
+long w_open_raw(const char *pathname, int flags, mode_t mode)
{
- int i, ret;
+ long ret;
+ int i;
for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
const char *p, *oname;
}
}
- if (i == ARRAY_SIZE(takeover_devs))
- ret = open(pathname, flags, mode);
+ if (i == ARRAY_SIZE(takeover_devs)) {
+ const char *w_path = emu_wrap_path(pathname);
+ ret = g_open_raw(w_path, flags, mode);
+ emu_wrap_path_free(w_path, pathname);
+ }
if (ret >= 0) {
for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
}
}
- strace("open(%s) = %d\n", pathname, ret);
+ strace("open(%s) = %ld\n", pathname, ret);
return ret;
}
-static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+static int w_open(const char *pathname, int flags, mode_t mode)
+{
+ long ret = w_open_raw(pathname, flags, mode);
+ return g_syscall_error(ret);
+}
+
+static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
+ int fd, off_t offset)
{
- void *ret;
+ long ret;
+
if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
ret = emu_do_mmap(length, prot, flags, fd, offset);
else
- ret = mmap(addr, length, prot, flags, fd, offset);
+ ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
- // threads are using heap before they mmap their stack
- // printf needs valid stack for pthread/errno
- if (((long)&ret & 0xf0000000) == 0xb0000000)
- strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
+ strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
+ addr, length, prot, flags, fd, (long)offset, ret);
return ret;
}
+
+static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+{
+ long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
+ return g_syscall_error(ret);
+}
#define w_mmap2 w_mmap
-static int w_munmap(void *addr, size_t length)
+static long w_munmap_raw(void *addr, size_t length)
{
- int ret;
+ long ret;
+
ret = emu_do_munmap(addr, length);
if (ret == -EAGAIN)
- ret = munmap(addr, length);
+ ret = g_munmap_raw(addr, length);
- if (((long)&ret & 0xf0000000) == 0xb0000000)
- strace("munmap(%p, %x) = %d\n", addr, length, ret);
+ strace("munmap(%p, %x) = %ld\n", addr, length, ret);
return ret;
}
-static ssize_t w_read(int fd, void *buf, size_t count)
+static int w_munmap(void *addr, size_t length)
{
- ssize_t ret;
- if (fd == FAKEDEV_GPIO)
- ret = emu_read_gpiodev(buf, count);
+ long ret = w_munmap_raw(addr, length);
+ return g_syscall_error(ret);
+}
+
+long w_read_raw(int fd, void *buf, size_t count)
+{
+ long ret;
+
+ if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
+ ret = emu_do_read(fd, buf, count);
else
- ret = read(fd, buf, count);
+ ret = g_read_raw(fd, buf, count);
- //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
+ //strace("read(%d, %p, %zd) = %ld\n", fd, buf, count, ret);
return ret;
}
-static int w_ioctl(int fd, int request, void *argp)
+static ssize_t w_read(int fd, void *buf, size_t count)
{
- int ret;
+ long ret = w_read_raw(fd, buf, count);
+ return g_syscall_error(ret);
+}
+
+long w_ioctl_raw(int fd, int request, void *argp)
+{
+ long ret;
if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
fd == emu_interesting_fds[IFD_SOUND].fd)
ret = emu_do_ioctl(fd, request, argp);
else
- ret = ioctl(fd, request, argp);
+ ret = g_ioctl_raw(fd, request, argp);
- strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
+ strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
return ret;
}
+static int w_ioctl(int fd, int request, void *argp)
+{
+ long ret = w_ioctl_raw(fd, request, argp);
+ return g_syscall_error(ret);
+}
+
static int w_sigaction(int signum, const void *act, void *oldact)
{
strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
return emu_do_execve(file, argv, environ);
}
-// static note: this can't safely return because of the way it's patched in
-// static note2: can't be used, execve hangs?
-static UNUSED int w_execve(const char *filename, char *const argv[],
- char *const envp[])
+long w_execve_raw(const char *filename, char * const argv[],
+ char * const envp[])
{
strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
return emu_do_execve(filename, argv, envp);
}
+static UNUSED int w_execve(const char *filename, char * const argv[],
+ char * const envp[])
+{
+ long ret = w_execve_raw(filename, argv, envp);
+ return g_syscall_error(ret);
+}
+
static int w_chdir(const char *path)
{
- int ret;
+ long ret;
+
if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
ret = 0;
else
- ret = chdir(path);
- strace("chdir(%s) = %d\n", path, ret);
- return ret;
+ ret = g_chdir_raw(path);
+
+ strace("chdir(%s) = %ld\n", path, ret);
+ return g_syscall_error(ret);
+}
+
+static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz)
+{
+ long ret;
+
+#ifndef DL
+ if (path != NULL && strncmp(path, "/proc/", 6) == 0
+ && strcmp(strrchr(path, '/'), "/exe") == 0)
+ {
+ ret = snprintf(buf, bufsiz, "%s", bin_path);
+ if (ret > bufsiz)
+ ret = bufsiz;
+ }
+ else
+#endif
+ ret = g_readlink_raw(path, buf, bufsiz);
+
+ strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret);
+ return g_syscall_error(ret);
}
#undef open
#undef munmap
#undef read
#undef ioctl
+#undef close
#undef sigaction
#undef tcgetattr
#undef tcsetattr
#undef execvp
#undef execve
#undef chdir
+#undef readlink
#ifdef DL
static typeof(sym) *p_real_##sym
// note: update symver too
-MAKE_WRAP_SYM(open);
+MAKE_WRAP_SYM_N(open);
MAKE_WRAP_SYM(fopen);
-MAKE_WRAP_SYM(mmap);
-MAKE_WRAP_SYM(munmap);
-MAKE_WRAP_SYM(read);
-MAKE_WRAP_SYM(ioctl);
+MAKE_WRAP_SYM_N(mmap);
+MAKE_WRAP_SYM_N(munmap);
+MAKE_WRAP_SYM_N(read);
+MAKE_WRAP_SYM_N(ioctl);
MAKE_WRAP_SYM(sigaction);
MAKE_WRAP_SYM(tcgetattr);
MAKE_WRAP_SYM(tcsetattr);
MAKE_WRAP_SYM_N(execle);
MAKE_WRAP_SYM_N(execv);
MAKE_WRAP_SYM_N(execvp);
-MAKE_WRAP_SYM(execve);
-MAKE_WRAP_SYM(chdir);
+MAKE_WRAP_SYM_N(execve);
+MAKE_WRAP_SYM_N(chdir);
+MAKE_WRAP_SYM_N(readlink);
typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
#define REAL_FUNC_NP(name) \
const char *name;
void **func_ptr;
} real_funcs_np[] = {
- REAL_FUNC_NP(open),
+ //REAL_FUNC_NP(open),
REAL_FUNC_NP(fopen),
- REAL_FUNC_NP(mmap),
- REAL_FUNC_NP(munmap),
- REAL_FUNC_NP(read),
- REAL_FUNC_NP(ioctl),
+ //REAL_FUNC_NP(mmap),
+ //REAL_FUNC_NP(munmap),
+ //REAL_FUNC_NP(read),
+ //REAL_FUNC_NP(ioctl),
REAL_FUNC_NP(sigaction),
REAL_FUNC_NP(tcgetattr),
REAL_FUNC_NP(tcsetattr),
REAL_FUNC_NP(system),
// exec* - skipped
- REAL_FUNC_NP(execve),
- REAL_FUNC_NP(chdir),
+ //REAL_FUNC_NP(execve),
+ //REAL_FUNC_NP(chdir),
};
-#define open p_real_open
+//#define open p_real_open
#define fopen p_real_fopen
-#define mmap p_real_mmap
-#define munmap p_real_munmap
-#define read p_real_read
-#define ioctl p_real_ioctl
+//#define mmap p_real_mmap
+//#define munmap p_real_munmap
+//#define read p_real_read
+//#define ioctl p_real_ioctl
#define sigaction p_real_sigaction
#define tcgetattr p_real_tcgetattr
#define tcsetattr p_real_tcsetattr
#define system p_real_system
-#define execve p_real_execve
-#define chdir p_real_chdir
+//#define execve p_real_execve
+//#define chdir p_real_chdir
#undef MAKE_WRAP_SYM
#undef REAL_FUNC_NP
{
va_list ap;
mode_t mode;
+ long ret;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
- return open(pathname, flags, mode);
+ ret = g_open_raw(pathname, flags, mode);
+ return g_syscall_error(ret);
}
FILE *real_fopen(const char *path, const char *mode)
return fopen(path, mode);
}
-void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+void *real_mmap(void *addr, size_t length, int prot, int flags,
+ int fd, off_t offset)
{
- return mmap(addr, length, prot, flags, fd, offset);
+ long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
+ return (void *)g_syscall_error(ret);
}
int real_munmap(void *addr, size_t length)
{
- return munmap(addr, length);
+ return g_syscall_error(g_munmap_raw(addr, length));
}
int real_read(int fd, void *buf, size_t count)
{
- return read(fd, buf, count);
+ long ret = g_read_raw(fd, buf, count);
+ return g_syscall_error(ret);
}
-int real_ioctl(int d, int request, void *argp)
+int real_ioctl(int fd, int request, void *argp)
{
- return ioctl(d, request, argp);
+ long ret = g_ioctl_raw(fd, request, argp);
+ return g_syscall_error(ret);
+}
+
+int real_close(int fd)
+{
+ long ret = g_close_raw(fd);
+ return g_syscall_error(ret);
}
int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
int real_execve(const char *filename, char *const argv[],
char *const envp[])
{
- return execve(filename, argv, envp);
+ long ret = g_execve_raw(filename, argv, envp);
+ return g_syscall_error(ret);
}
int real_chdir(const char *path)
{
- return chdir(path);
+ long ret = g_chdir_raw(path);
+ return g_syscall_error(ret);
+}
+
+ssize_t real_readlink(const char *path, char *buf, size_t bufsiz)
+{
+ long ret = g_readlink_raw(path, buf, bufsiz);
+ return g_syscall_error(ret);
+}
+
+void real_sleep(unsigned int seconds)
+{
+ struct timespec ts = { seconds, 0 };
+ g_nanosleep_raw(&ts, NULL);
+}
+
+void real_usleep(unsigned int usec)
+{
+ struct timespec ts = { usec / 1000000, usec % 1000000 };
+ g_nanosleep_raw(&ts, NULL);
+}
+
+void real_exit(int status)
+{
+ g_exit_group_raw(status);
}
// vim:shiftwidth=2:expandtab