| 1 | /* |
| 2 | * GINGE - GINGE Is Not Gp2x Emulator |
| 3 | * (C) notaz, 2010-2011 |
| 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 "header.h" |
| 23 | |
| 24 | #if (DBG & 1) |
| 25 | #define strace printf |
| 26 | #else |
| 27 | #define strace(...) |
| 28 | #endif |
| 29 | |
| 30 | #define UNUSED __attribute__((unused)) |
| 31 | |
| 32 | static const struct dev_fd_t takeover_devs[] = { |
| 33 | { "/dev/mem", FAKEDEV_MEM }, |
| 34 | { "/dev/GPIO", FAKEDEV_GPIO }, |
| 35 | { "/dev/fb0", FAKEDEV_FB0 }, |
| 36 | { "/dev/fb/0", FAKEDEV_FB0 }, |
| 37 | { "/dev/fb1", FAKEDEV_FB1 }, |
| 38 | { "/dev/fb/1", FAKEDEV_FB1 }, |
| 39 | { "/dev/mmuhack", FAKEDEV_MMUHACK }, |
| 40 | { "/dev/tty", FAKEDEV_TTY0 }, |
| 41 | { "/dev/tty0", FAKEDEV_TTY0 }, |
| 42 | #ifdef PND |
| 43 | { "/dev/input/event*", -1 }, // hide for now, may cause dupe events |
| 44 | #endif |
| 45 | }; |
| 46 | |
| 47 | static int w_open(const char *pathname, int flags, mode_t mode) |
| 48 | { |
| 49 | int i, ret; |
| 50 | |
| 51 | for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) { |
| 52 | const char *p, *oname; |
| 53 | int len; |
| 54 | |
| 55 | oname = takeover_devs[i].name; |
| 56 | p = strchr(oname, '*'); |
| 57 | if (p != NULL) |
| 58 | len = p - oname; |
| 59 | else |
| 60 | len = strlen(oname) + 1; |
| 61 | |
| 62 | if (strncmp(pathname, oname, len) == 0) { |
| 63 | ret = takeover_devs[i].fd; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (i == ARRAY_SIZE(takeover_devs)) |
| 69 | ret = open(pathname, flags, mode); |
| 70 | |
| 71 | if (ret >= 0) { |
| 72 | for (i = 0; emu_interesting_fds[i].name != NULL; i++) { |
| 73 | struct dev_fd_t *eifd = &emu_interesting_fds[i]; |
| 74 | if (strcmp(pathname, eifd->name) == 0) { |
| 75 | eifd->fd = ret; |
| 76 | if (eifd->open_cb != NULL) |
| 77 | eifd->open_cb(ret); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | strace("open(%s) = %d\n", pathname, ret); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) |
| 88 | { |
| 89 | void *ret; |
| 90 | if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) |
| 91 | ret = emu_do_mmap(length, prot, flags, fd, offset); |
| 92 | else |
| 93 | ret = mmap(addr, length, prot, flags, fd, offset); |
| 94 | |
| 95 | // threads are using heap before they mmap their stack |
| 96 | // printf needs valid stack for pthread/errno |
| 97 | if (((long)&ret & 0xf0000000) == 0xb0000000) |
| 98 | strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret); |
| 99 | return ret; |
| 100 | } |
| 101 | #define w_mmap2 w_mmap |
| 102 | |
| 103 | static int w_munmap(void *addr, size_t length) |
| 104 | { |
| 105 | int ret; |
| 106 | ret = emu_do_munmap(addr, length); |
| 107 | if (ret == -EAGAIN) |
| 108 | ret = munmap(addr, length); |
| 109 | |
| 110 | if (((long)&ret & 0xf0000000) == 0xb0000000) |
| 111 | strace("munmap(%p, %x) = %d\n", addr, length, ret); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static ssize_t w_read(int fd, void *buf, size_t count) |
| 116 | { |
| 117 | ssize_t ret; |
| 118 | if (fd == FAKEDEV_GPIO) |
| 119 | ret = emu_read_gpiodev(buf, count); |
| 120 | else |
| 121 | ret = read(fd, buf, count); |
| 122 | |
| 123 | //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static int w_ioctl(int fd, int request, void *argp) |
| 128 | { |
| 129 | int ret; |
| 130 | |
| 131 | if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) || |
| 132 | fd == emu_interesting_fds[IFD_SOUND].fd) |
| 133 | ret = emu_do_ioctl(fd, request, argp); |
| 134 | else |
| 135 | ret = ioctl(fd, request, argp); |
| 136 | |
| 137 | strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int w_sigaction(int signum, const void *act, void *oldact) |
| 142 | { |
| 143 | strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* dynamic only */ |
| 148 | static UNUSED int w_tcgetattr(int fd, struct termios *termios_p) |
| 149 | { |
| 150 | int ret; |
| 151 | if (fd != FAKEDEV_TTY0) |
| 152 | ret = tcgetattr(fd, termios_p); |
| 153 | else |
| 154 | ret = 0; |
| 155 | |
| 156 | strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static UNUSED int w_tcsetattr(int fd, int optional_actions, |
| 161 | const struct termios *termios_p) |
| 162 | { |
| 163 | int ret; |
| 164 | if (fd != FAKEDEV_TTY0) |
| 165 | ret = tcsetattr(fd, optional_actions, termios_p); |
| 166 | else |
| 167 | ret = 0; |
| 168 | |
| 169 | strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static UNUSED FILE *w_fopen(const char *path, const char *mode) |
| 174 | { |
| 175 | FILE *ret = emu_do_fopen(path, mode); |
| 176 | strace("fopen(%s, %s) = %p\n", path, mode, ret); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static UNUSED int w_system(const char *command) |
| 181 | { |
| 182 | int ret = emu_do_system(command); |
| 183 | strace("system(%s) = %d\n", command, ret); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | extern char **environ; |
| 188 | |
| 189 | static UNUSED int w_execl(const char *path, const char *arg, ...) |
| 190 | { |
| 191 | // don't allow exec (for now) |
| 192 | strace("execl(%s, %s, ...) = ?\n", path, arg); |
| 193 | exit(1); |
| 194 | } |
| 195 | |
| 196 | static UNUSED int w_execlp(const char *file, const char *arg, ...) |
| 197 | { |
| 198 | strace("execlp(%s, %s, ...) = ?\n", file, arg); |
| 199 | exit(1); |
| 200 | } |
| 201 | |
| 202 | static UNUSED int w_execle(const char *path, const char *arg, ...) |
| 203 | { |
| 204 | strace("execle(%s, %s, ...) = ?\n", path, arg); |
| 205 | exit(1); |
| 206 | } |
| 207 | |
| 208 | static UNUSED int w_execv(const char *path, char *const argv[]) |
| 209 | { |
| 210 | strace("execv(%s, %p) = ?\n", path, argv); |
| 211 | return emu_do_execve(path, argv, environ); |
| 212 | } |
| 213 | |
| 214 | static UNUSED int w_execvp(const char *file, char *const argv[]) |
| 215 | { |
| 216 | strace("execvp(%s, %p) = ?\n", file, argv); |
| 217 | return emu_do_execve(file, argv, environ); |
| 218 | } |
| 219 | |
| 220 | // static note: this can't safely return because of the way it's patched in |
| 221 | // static note2: can't be used, execve hangs? |
| 222 | static UNUSED int w_execve(const char *filename, char *const argv[], |
| 223 | char *const envp[]) |
| 224 | { |
| 225 | strace("execve(%s, %p, %p) = ?\n", filename, argv, envp); |
| 226 | return emu_do_execve(filename, argv, envp); |
| 227 | } |
| 228 | |
| 229 | static int w_chdir(const char *path) |
| 230 | { |
| 231 | int ret; |
| 232 | if (path != NULL && strstr(path, "/usr/gp2x") != NULL) |
| 233 | ret = 0; |
| 234 | else |
| 235 | ret = chdir(path); |
| 236 | strace("chdir(%s) = %d\n", path, ret); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | #undef open |
| 241 | #undef fopen |
| 242 | #undef mmap |
| 243 | #undef munmap |
| 244 | #undef read |
| 245 | #undef ioctl |
| 246 | #undef sigaction |
| 247 | #undef tcgetattr |
| 248 | #undef tcsetattr |
| 249 | #undef system |
| 250 | #undef execl |
| 251 | #undef execlp |
| 252 | #undef execle |
| 253 | #undef execv |
| 254 | #undef execvp |
| 255 | #undef execve |
| 256 | #undef chdir |
| 257 | |
| 258 | #ifdef DL |
| 259 | |
| 260 | #define MAKE_WRAP_SYM_N(sym) \ |
| 261 | /* alias wrap symbols to real names */ \ |
| 262 | typeof(sym) sym __attribute__((alias("w_" #sym))) |
| 263 | |
| 264 | #define MAKE_WRAP_SYM(sym) \ |
| 265 | MAKE_WRAP_SYM_N(sym); \ |
| 266 | /* wrapper to real functions, to be set up on load */ \ |
| 267 | static typeof(sym) *p_real_##sym |
| 268 | |
| 269 | // note: update symver too |
| 270 | MAKE_WRAP_SYM(open); |
| 271 | MAKE_WRAP_SYM(fopen); |
| 272 | MAKE_WRAP_SYM(mmap); |
| 273 | MAKE_WRAP_SYM(munmap); |
| 274 | MAKE_WRAP_SYM(read); |
| 275 | MAKE_WRAP_SYM(ioctl); |
| 276 | MAKE_WRAP_SYM(sigaction); |
| 277 | MAKE_WRAP_SYM(tcgetattr); |
| 278 | MAKE_WRAP_SYM(tcsetattr); |
| 279 | MAKE_WRAP_SYM(system); |
| 280 | MAKE_WRAP_SYM_N(execl); |
| 281 | MAKE_WRAP_SYM_N(execlp); |
| 282 | MAKE_WRAP_SYM_N(execle); |
| 283 | MAKE_WRAP_SYM_N(execv); |
| 284 | MAKE_WRAP_SYM_N(execvp); |
| 285 | MAKE_WRAP_SYM(execve); |
| 286 | MAKE_WRAP_SYM(chdir); |
| 287 | typeof(mmap) mmap2 __attribute__((alias("w_mmap"))); |
| 288 | |
| 289 | #define REAL_FUNC_NP(name) \ |
| 290 | { #name, (void **)&p_real_##name } |
| 291 | |
| 292 | static const struct { |
| 293 | const char *name; |
| 294 | void **func_ptr; |
| 295 | } real_funcs_np[] = { |
| 296 | REAL_FUNC_NP(open), |
| 297 | REAL_FUNC_NP(fopen), |
| 298 | REAL_FUNC_NP(mmap), |
| 299 | REAL_FUNC_NP(munmap), |
| 300 | REAL_FUNC_NP(read), |
| 301 | REAL_FUNC_NP(ioctl), |
| 302 | REAL_FUNC_NP(sigaction), |
| 303 | REAL_FUNC_NP(tcgetattr), |
| 304 | REAL_FUNC_NP(tcsetattr), |
| 305 | REAL_FUNC_NP(system), |
| 306 | // exec* - skipped |
| 307 | REAL_FUNC_NP(execve), |
| 308 | REAL_FUNC_NP(chdir), |
| 309 | }; |
| 310 | |
| 311 | #define open p_real_open |
| 312 | #define fopen p_real_fopen |
| 313 | #define mmap p_real_mmap |
| 314 | #define munmap p_real_munmap |
| 315 | #define read p_real_read |
| 316 | #define ioctl p_real_ioctl |
| 317 | #define sigaction p_real_sigaction |
| 318 | #define tcgetattr p_real_tcgetattr |
| 319 | #define tcsetattr p_real_tcsetattr |
| 320 | #define system p_real_system |
| 321 | #define execve p_real_execve |
| 322 | #define chdir p_real_chdir |
| 323 | |
| 324 | #undef MAKE_WRAP_SYM |
| 325 | #undef REAL_FUNC_NP |
| 326 | |
| 327 | #endif |
| 328 | |
| 329 | // just call real funcs for static, pointer for dynamic |
| 330 | int real_open(const char *pathname, int flags, ...) |
| 331 | { |
| 332 | va_list ap; |
| 333 | mode_t mode; |
| 334 | |
| 335 | va_start(ap, flags); |
| 336 | mode = va_arg(ap, mode_t); |
| 337 | va_end(ap); |
| 338 | return open(pathname, flags, mode); |
| 339 | } |
| 340 | |
| 341 | FILE *real_fopen(const char *path, const char *mode) |
| 342 | { |
| 343 | return fopen(path, mode); |
| 344 | } |
| 345 | |
| 346 | void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) |
| 347 | { |
| 348 | return mmap(addr, length, prot, flags, fd, offset); |
| 349 | } |
| 350 | |
| 351 | int real_munmap(void *addr, size_t length) |
| 352 | { |
| 353 | return munmap(addr, length); |
| 354 | } |
| 355 | |
| 356 | int real_read(int fd, void *buf, size_t count) |
| 357 | { |
| 358 | return read(fd, buf, count); |
| 359 | } |
| 360 | |
| 361 | int real_ioctl(int d, int request, void *argp) |
| 362 | { |
| 363 | return ioctl(d, request, argp); |
| 364 | } |
| 365 | |
| 366 | int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact) |
| 367 | { |
| 368 | return sigaction(signum, act, oldact); |
| 369 | } |
| 370 | |
| 371 | int real_tcgetattr(int fd, struct termios *termios_p) |
| 372 | { |
| 373 | return tcgetattr(fd, termios_p); |
| 374 | } |
| 375 | |
| 376 | int real_tcsetattr(int fd, int optional_actions, |
| 377 | const struct termios *termios_p) |
| 378 | { |
| 379 | return tcsetattr(fd, optional_actions, termios_p); |
| 380 | } |
| 381 | |
| 382 | int real_system(const char *command) |
| 383 | { |
| 384 | return system(command); |
| 385 | } |
| 386 | |
| 387 | // real_exec* is missing intentionally - we don't need them |
| 388 | |
| 389 | int real_execve(const char *filename, char *const argv[], |
| 390 | char *const envp[]) |
| 391 | { |
| 392 | return execve(filename, argv, envp); |
| 393 | } |
| 394 | |
| 395 | int real_chdir(const char *path) |
| 396 | { |
| 397 | return chdir(path); |
| 398 | } |
| 399 | |
| 400 | // vim:shiftwidth=2:expandtab |