| 1 | /* |
| 2 | * GINGE - GINGE Is Not Gp2x Emulator |
| 3 | * (C) notaz, 2010-2011,2016 |
| 4 | * |
| 5 | * This work is licensed under the MAME license, see COPYING file for details. |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdarg.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <sys/mman.h> |
| 15 | #include <unistd.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <signal.h> |
| 18 | #include <termios.h> |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #include "realfuncs.h" |
| 22 | #include "syscalls.h" |
| 23 | #include "llibc.h" |
| 24 | #include "header.h" |
| 25 | |
| 26 | #if (DBG & 1) |
| 27 | #define strace g_printf |
| 28 | #else |
| 29 | #define strace(...) |
| 30 | #endif |
| 31 | |
| 32 | #define UNUSED __attribute__((unused)) |
| 33 | |
| 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 }, |
| 47 | #ifdef PND |
| 48 | { "/dev/input/event*", -ENODEV }, // hide for now, may cause dupe events |
| 49 | #endif |
| 50 | }; |
| 51 | |
| 52 | long w_open_raw(const char *pathname, int flags, mode_t mode) |
| 53 | { |
| 54 | long ret; |
| 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) { |
| 58 | const char *p, *oname; |
| 59 | int len; |
| 60 | |
| 61 | oname = takeover_devs[i].name; |
| 62 | p = strchr(oname, '*'); |
| 63 | if (p != NULL) |
| 64 | len = p - oname; |
| 65 | else |
| 66 | len = strlen(oname) + 1; |
| 67 | |
| 68 | if (strncmp(pathname, oname, len) == 0) { |
| 69 | ret = takeover_devs[i].fd; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 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); |
| 78 | } |
| 79 | |
| 80 | if (ret >= 0) { |
| 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) { |
| 84 | eifd->fd = ret; |
| 85 | if (eifd->open_cb != NULL) |
| 86 | eifd->open_cb(ret); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | strace("open(%s) = %ld\n", pathname, ret); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static int w_open(const char *pathname, int flags, mode_t mode) |
| 97 | { |
| 98 | long ret = w_open_raw(pathname, flags, mode); |
| 99 | return g_syscall_error(ret); |
| 100 | } |
| 101 | |
| 102 | static long w_mmap_raw(void *addr, size_t length, int prot, int flags, |
| 103 | int fd, off_t offset) |
| 104 | { |
| 105 | long ret; |
| 106 | |
| 107 | if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) |
| 108 | ret = emu_do_mmap(length, prot, flags, fd, offset); |
| 109 | else |
| 110 | ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12); |
| 111 | |
| 112 | strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n", |
| 113 | addr, length, prot, flags, fd, (long)offset, ret); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) |
| 118 | { |
| 119 | long ret = w_mmap_raw(addr, length, prot, flags, fd, offset); |
| 120 | return g_syscall_error(ret); |
| 121 | } |
| 122 | #define w_mmap2 w_mmap |
| 123 | |
| 124 | static long w_munmap_raw(void *addr, size_t length) |
| 125 | { |
| 126 | long ret; |
| 127 | |
| 128 | ret = emu_do_munmap(addr, length); |
| 129 | if (ret == -EAGAIN) |
| 130 | ret = g_munmap_raw(addr, length); |
| 131 | |
| 132 | strace("munmap(%p, %x) = %ld\n", addr, length, ret); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static int w_munmap(void *addr, size_t length) |
| 137 | { |
| 138 | long ret = w_munmap_raw(addr, length); |
| 139 | return g_syscall_error(ret); |
| 140 | } |
| 141 | |
| 142 | long w_read_raw(int fd, void *buf, size_t count) |
| 143 | { |
| 144 | long ret; |
| 145 | |
| 146 | if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) |
| 147 | ret = emu_do_read(fd, buf, count); |
| 148 | else |
| 149 | ret = g_read_raw(fd, buf, count); |
| 150 | |
| 151 | //strace("read(%d, %p, %zd) = %ld\n", fd, buf, count, ret); |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static ssize_t w_read(int fd, void *buf, size_t count) |
| 156 | { |
| 157 | long ret = w_read_raw(fd, buf, count); |
| 158 | return g_syscall_error(ret); |
| 159 | } |
| 160 | |
| 161 | long w_ioctl_raw(int fd, int request, void *argp) |
| 162 | { |
| 163 | long ret; |
| 164 | |
| 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); |
| 168 | else |
| 169 | ret = g_ioctl_raw(fd, request, argp); |
| 170 | |
| 171 | strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static int w_ioctl(int fd, int request, void *argp) |
| 176 | { |
| 177 | long ret = w_ioctl_raw(fd, request, argp); |
| 178 | return g_syscall_error(ret); |
| 179 | } |
| 180 | |
| 181 | static int w_sigaction(int signum, const void *act, void *oldact) |
| 182 | { |
| 183 | strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* dynamic only */ |
| 188 | static UNUSED int w_tcgetattr(int fd, struct termios *termios_p) |
| 189 | { |
| 190 | int ret; |
| 191 | if (fd != FAKEDEV_TTY0) |
| 192 | ret = tcgetattr(fd, termios_p); |
| 193 | else |
| 194 | ret = 0; |
| 195 | |
| 196 | strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static UNUSED int w_tcsetattr(int fd, int optional_actions, |
| 201 | const struct termios *termios_p) |
| 202 | { |
| 203 | int ret; |
| 204 | if (fd != FAKEDEV_TTY0) |
| 205 | ret = tcsetattr(fd, optional_actions, termios_p); |
| 206 | else |
| 207 | ret = 0; |
| 208 | |
| 209 | strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static UNUSED FILE *w_fopen(const char *path, const char *mode) |
| 214 | { |
| 215 | FILE *ret = emu_do_fopen(path, mode); |
| 216 | strace("fopen(%s, %s) = %p\n", path, mode, ret); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static UNUSED int w_system(const char *command) |
| 221 | { |
| 222 | int ret = emu_do_system(command); |
| 223 | strace("system(%s) = %d\n", command, ret); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | extern char **environ; |
| 228 | |
| 229 | static UNUSED int w_execl(const char *path, const char *arg, ...) |
| 230 | { |
| 231 | // don't allow exec (for now) |
| 232 | strace("execl(%s, %s, ...) = ?\n", path, arg); |
| 233 | exit(1); |
| 234 | } |
| 235 | |
| 236 | static UNUSED int w_execlp(const char *file, const char *arg, ...) |
| 237 | { |
| 238 | strace("execlp(%s, %s, ...) = ?\n", file, arg); |
| 239 | exit(1); |
| 240 | } |
| 241 | |
| 242 | static UNUSED int w_execle(const char *path, const char *arg, ...) |
| 243 | { |
| 244 | strace("execle(%s, %s, ...) = ?\n", path, arg); |
| 245 | exit(1); |
| 246 | } |
| 247 | |
| 248 | static UNUSED int w_execv(const char *path, char *const argv[]) |
| 249 | { |
| 250 | strace("execv(%s, %p) = ?\n", path, argv); |
| 251 | return emu_do_execve(path, argv, environ); |
| 252 | } |
| 253 | |
| 254 | static UNUSED int w_execvp(const char *file, char *const argv[]) |
| 255 | { |
| 256 | strace("execvp(%s, %p) = ?\n", file, argv); |
| 257 | return emu_do_execve(file, argv, environ); |
| 258 | } |
| 259 | |
| 260 | long w_execve_raw(const char *filename, char * const argv[], |
| 261 | char * const envp[]) |
| 262 | { |
| 263 | strace("execve(%s, %p, %p) = ?\n", filename, argv, envp); |
| 264 | return emu_do_execve(filename, argv, envp); |
| 265 | } |
| 266 | |
| 267 | static UNUSED int w_execve(const char *filename, char * const argv[], |
| 268 | char * const envp[]) |
| 269 | { |
| 270 | long ret = w_execve_raw(filename, argv, envp); |
| 271 | return g_syscall_error(ret); |
| 272 | } |
| 273 | |
| 274 | static int w_chdir(const char *path) |
| 275 | { |
| 276 | long ret; |
| 277 | |
| 278 | if (path != NULL && strstr(path, "/usr/gp2x") != NULL) |
| 279 | ret = 0; |
| 280 | else |
| 281 | ret = g_chdir_raw(path); |
| 282 | |
| 283 | strace("chdir(%s) = %ld\n", path, ret); |
| 284 | return g_syscall_error(ret); |
| 285 | } |
| 286 | |
| 287 | static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz) |
| 288 | { |
| 289 | long ret; |
| 290 | |
| 291 | #ifndef DL |
| 292 | if (path != NULL && strncmp(path, "/proc/", 6) == 0 |
| 293 | && strcmp(strrchr(path, '/'), "/exe") == 0) |
| 294 | { |
| 295 | ret = snprintf(buf, bufsiz, "%s", bin_path); |
| 296 | if (ret > bufsiz) |
| 297 | ret = bufsiz; |
| 298 | } |
| 299 | else |
| 300 | #endif |
| 301 | ret = g_readlink_raw(path, buf, bufsiz); |
| 302 | |
| 303 | strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret); |
| 304 | return g_syscall_error(ret); |
| 305 | } |
| 306 | |
| 307 | #undef open |
| 308 | #undef fopen |
| 309 | #undef mmap |
| 310 | #undef munmap |
| 311 | #undef read |
| 312 | #undef ioctl |
| 313 | #undef close |
| 314 | #undef sigaction |
| 315 | #undef tcgetattr |
| 316 | #undef tcsetattr |
| 317 | #undef system |
| 318 | #undef execl |
| 319 | #undef execlp |
| 320 | #undef execle |
| 321 | #undef execv |
| 322 | #undef execvp |
| 323 | #undef execve |
| 324 | #undef chdir |
| 325 | #undef readlink |
| 326 | |
| 327 | #ifdef DL |
| 328 | |
| 329 | #define MAKE_WRAP_SYM_N(sym) \ |
| 330 | /* alias wrap symbols to real names */ \ |
| 331 | typeof(sym) sym __attribute__((alias("w_" #sym))) |
| 332 | |
| 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 |
| 337 | |
| 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"))); |
| 358 | |
| 359 | #define REAL_FUNC_NP(name) \ |
| 360 | { #name, (void **)&p_real_##name } |
| 361 | |
| 362 | static const struct { |
| 363 | const char *name; |
| 364 | void **func_ptr; |
| 365 | } real_funcs_np[] = { |
| 366 | //REAL_FUNC_NP(open), |
| 367 | REAL_FUNC_NP(fopen), |
| 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), |
| 376 | // exec* - skipped |
| 377 | //REAL_FUNC_NP(execve), |
| 378 | //REAL_FUNC_NP(chdir), |
| 379 | }; |
| 380 | |
| 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 |
| 393 | |
| 394 | #undef MAKE_WRAP_SYM |
| 395 | #undef REAL_FUNC_NP |
| 396 | |
| 397 | #endif |
| 398 | |
| 399 | // just call real funcs for static, pointer for dynamic |
| 400 | int real_open(const char *pathname, int flags, ...) |
| 401 | { |
| 402 | va_list ap; |
| 403 | mode_t mode; |
| 404 | long ret; |
| 405 | |
| 406 | va_start(ap, flags); |
| 407 | mode = va_arg(ap, mode_t); |
| 408 | va_end(ap); |
| 409 | ret = g_open_raw(pathname, flags, mode); |
| 410 | return g_syscall_error(ret); |
| 411 | } |
| 412 | |
| 413 | FILE *real_fopen(const char *path, const char *mode) |
| 414 | { |
| 415 | return fopen(path, mode); |
| 416 | } |
| 417 | |
| 418 | void *real_mmap(void *addr, size_t length, int prot, int flags, |
| 419 | int fd, off_t offset) |
| 420 | { |
| 421 | long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12); |
| 422 | return (void *)g_syscall_error(ret); |
| 423 | } |
| 424 | |
| 425 | int real_munmap(void *addr, size_t length) |
| 426 | { |
| 427 | return g_syscall_error(g_munmap_raw(addr, length)); |
| 428 | } |
| 429 | |
| 430 | int real_read(int fd, void *buf, size_t count) |
| 431 | { |
| 432 | long ret = g_read_raw(fd, buf, count); |
| 433 | return g_syscall_error(ret); |
| 434 | } |
| 435 | |
| 436 | int real_ioctl(int fd, int request, void *argp) |
| 437 | { |
| 438 | long ret = g_ioctl_raw(fd, request, argp); |
| 439 | return g_syscall_error(ret); |
| 440 | } |
| 441 | |
| 442 | int real_close(int fd) |
| 443 | { |
| 444 | long ret = g_close_raw(fd); |
| 445 | return g_syscall_error(ret); |
| 446 | } |
| 447 | |
| 448 | int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact) |
| 449 | { |
| 450 | return sigaction(signum, act, oldact); |
| 451 | } |
| 452 | |
| 453 | int real_tcgetattr(int fd, struct termios *termios_p) |
| 454 | { |
| 455 | return tcgetattr(fd, termios_p); |
| 456 | } |
| 457 | |
| 458 | int real_tcsetattr(int fd, int optional_actions, |
| 459 | const struct termios *termios_p) |
| 460 | { |
| 461 | return tcsetattr(fd, optional_actions, termios_p); |
| 462 | } |
| 463 | |
| 464 | int real_system(const char *command) |
| 465 | { |
| 466 | return system(command); |
| 467 | } |
| 468 | |
| 469 | // real_exec* is missing intentionally - we don't need them |
| 470 | |
| 471 | int real_execve(const char *filename, char *const argv[], |
| 472 | char *const envp[]) |
| 473 | { |
| 474 | long ret = g_execve_raw(filename, argv, envp); |
| 475 | return g_syscall_error(ret); |
| 476 | } |
| 477 | |
| 478 | int real_chdir(const char *path) |
| 479 | { |
| 480 | long ret = g_chdir_raw(path); |
| 481 | return g_syscall_error(ret); |
| 482 | } |
| 483 | |
| 484 | ssize_t real_readlink(const char *path, char *buf, size_t bufsiz) |
| 485 | { |
| 486 | long ret = g_readlink_raw(path, buf, bufsiz); |
| 487 | return g_syscall_error(ret); |
| 488 | } |
| 489 | |
| 490 | void real_sleep(unsigned int seconds) |
| 491 | { |
| 492 | struct timespec ts = { seconds, 0 }; |
| 493 | g_nanosleep_raw(&ts, NULL); |
| 494 | } |
| 495 | |
| 496 | void real_usleep(unsigned int usec) |
| 497 | { |
| 498 | struct timespec ts = { usec / 1000000, usec % 1000000 }; |
| 499 | g_nanosleep_raw(&ts, NULL); |
| 500 | } |
| 501 | |
| 502 | void real_exit(int status) |
| 503 | { |
| 504 | g_exit_group_raw(status); |
| 505 | } |
| 506 | |
| 507 | // vim:shiftwidth=2:expandtab |