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