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 }, |
8cbb0278 |
42 | { "/dev/tty", FAKEDEV_TTY }, |
7fd42181 |
43 | { "/dev/tty0", FAKEDEV_TTY0 }, |
88d814e3 |
44 | { "/dev/touchscreen/wm97xx", FAKEDEV_WM97XX }, |
45 | { "/etc/pointercal", FAKEDEV_WM97XX_P }, |
8cbb0278 |
46 | { "/dev/input/mice", -ENODEV }, |
7000b522 |
47 | #ifdef PND |
8cbb0278 |
48 | { "/dev/input/event*", -ENODEV }, // hide for now, may cause dupe events |
7000b522 |
49 | #endif |
7fd42181 |
50 | }; |
51 | |
2692251d |
52 | long w_open_raw(const char *pathname, int flags, mode_t mode) |
7fd42181 |
53 | { |
b4f4cb40 |
54 | long ret; |
55 | int i; |
7fd42181 |
56 | |
57 | for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) { |
7000b522 |
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) { |
7fd42181 |
69 | ret = takeover_devs[i].fd; |
70 | break; |
71 | } |
72 | } |
73 | |
2692251d |
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 | } |
7fd42181 |
79 | |
80 | if (ret >= 0) { |
81 | for (i = 0; emu_interesting_fds[i].name != NULL; i++) { |
7000b522 |
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); |
7fd42181 |
87 | break; |
88 | } |
89 | } |
90 | } |
91 | |
b4f4cb40 |
92 | strace("open(%s) = %ld\n", pathname, ret); |
7fd42181 |
93 | return ret; |
94 | } |
95 | |
b4f4cb40 |
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) |
7fd42181 |
104 | { |
b4f4cb40 |
105 | long ret; |
106 | |
7fd42181 |
107 | if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) |
108 | ret = emu_do_mmap(length, prot, flags, fd, offset); |
109 | else |
b4f4cb40 |
110 | ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12); |
7fd42181 |
111 | |
b4f4cb40 |
112 | strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n", |
113 | addr, length, prot, flags, fd, (long)offset, ret); |
7fd42181 |
114 | return ret; |
115 | } |
b4f4cb40 |
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 | } |
7fd42181 |
122 | #define w_mmap2 w_mmap |
123 | |
b4f4cb40 |
124 | static long w_munmap_raw(void *addr, size_t length) |
3ef07128 |
125 | { |
b4f4cb40 |
126 | long ret; |
127 | |
3ef07128 |
128 | ret = emu_do_munmap(addr, length); |
129 | if (ret == -EAGAIN) |
b4f4cb40 |
130 | ret = g_munmap_raw(addr, length); |
3ef07128 |
131 | |
b4f4cb40 |
132 | strace("munmap(%p, %x) = %ld\n", addr, length, ret); |
3ef07128 |
133 | return ret; |
134 | } |
135 | |
b4f4cb40 |
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) |
7fd42181 |
143 | { |
b4f4cb40 |
144 | long ret; |
145 | |
88d814e3 |
146 | if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) |
147 | ret = emu_do_read(fd, buf, count); |
7fd42181 |
148 | else |
b4f4cb40 |
149 | ret = g_read_raw(fd, buf, count); |
7fd42181 |
150 | |
88d814e3 |
151 | //strace("read(%d, %p, %zd) = %ld\n", fd, buf, count, ret); |
7fd42181 |
152 | return ret; |
153 | } |
154 | |
b4f4cb40 |
155 | static ssize_t w_read(int fd, void *buf, size_t count) |
7fd42181 |
156 | { |
b4f4cb40 |
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; |
7fd42181 |
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 |
b4f4cb40 |
169 | ret = g_ioctl_raw(fd, request, argp); |
7fd42181 |
170 | |
b4f4cb40 |
171 | strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret); |
7fd42181 |
172 | return ret; |
173 | } |
174 | |
b4f4cb40 |
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 | |
7fd42181 |
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 | |
7000b522 |
227 | extern char **environ; |
228 | |
db9191ed |
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); |
7000b522 |
233 | exit(1); |
db9191ed |
234 | } |
235 | |
236 | static UNUSED int w_execlp(const char *file, const char *arg, ...) |
237 | { |
238 | strace("execlp(%s, %s, ...) = ?\n", file, arg); |
7000b522 |
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); |
db9191ed |
258 | } |
259 | |
78684356 |
260 | long w_execve_raw(const char *filename, char * const argv[], |
261 | char * const envp[]) |
db9191ed |
262 | { |
263 | strace("execve(%s, %p, %p) = ?\n", filename, argv, envp); |
7000b522 |
264 | return emu_do_execve(filename, argv, envp); |
db9191ed |
265 | } |
266 | |
78684356 |
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 | |
db9191ed |
274 | static int w_chdir(const char *path) |
275 | { |
b4f4cb40 |
276 | long ret; |
277 | |
db9191ed |
278 | if (path != NULL && strstr(path, "/usr/gp2x") != NULL) |
279 | ret = 0; |
280 | else |
b4f4cb40 |
281 | ret = g_chdir_raw(path); |
c3831532 |
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 | |
d5a3e1bc |
291 | #ifndef DL |
c3831532 |
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 |
d5a3e1bc |
300 | #endif |
c3831532 |
301 | ret = g_readlink_raw(path, buf, bufsiz); |
302 | |
303 | strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret); |
b4f4cb40 |
304 | return g_syscall_error(ret); |
db9191ed |
305 | } |
306 | |
7fd42181 |
307 | #undef open |
308 | #undef fopen |
309 | #undef mmap |
3ef07128 |
310 | #undef munmap |
7fd42181 |
311 | #undef read |
312 | #undef ioctl |
b4f4cb40 |
313 | #undef close |
7fd42181 |
314 | #undef sigaction |
315 | #undef tcgetattr |
316 | #undef tcsetattr |
317 | #undef system |
db9191ed |
318 | #undef execl |
319 | #undef execlp |
7000b522 |
320 | #undef execle |
321 | #undef execv |
322 | #undef execvp |
db9191ed |
323 | #undef execve |
324 | #undef chdir |
c3831532 |
325 | #undef readlink |
7fd42181 |
326 | |
327 | #ifdef DL |
328 | |
db9191ed |
329 | #define MAKE_WRAP_SYM_N(sym) \ |
7fd42181 |
330 | /* alias wrap symbols to real names */ \ |
db9191ed |
331 | typeof(sym) sym __attribute__((alias("w_" #sym))) |
332 | |
333 | #define MAKE_WRAP_SYM(sym) \ |
334 | MAKE_WRAP_SYM_N(sym); \ |
7fd42181 |
335 | /* wrapper to real functions, to be set up on load */ \ |
336 | static typeof(sym) *p_real_##sym |
337 | |
4d045184 |
338 | // note: update symver too |
b4f4cb40 |
339 | MAKE_WRAP_SYM_N(open); |
7fd42181 |
340 | MAKE_WRAP_SYM(fopen); |
b4f4cb40 |
341 | MAKE_WRAP_SYM_N(mmap); |
342 | MAKE_WRAP_SYM_N(munmap); |
343 | MAKE_WRAP_SYM_N(read); |
344 | MAKE_WRAP_SYM_N(ioctl); |
7fd42181 |
345 | MAKE_WRAP_SYM(sigaction); |
346 | MAKE_WRAP_SYM(tcgetattr); |
347 | MAKE_WRAP_SYM(tcsetattr); |
348 | MAKE_WRAP_SYM(system); |
db9191ed |
349 | MAKE_WRAP_SYM_N(execl); |
350 | MAKE_WRAP_SYM_N(execlp); |
7000b522 |
351 | MAKE_WRAP_SYM_N(execle); |
352 | MAKE_WRAP_SYM_N(execv); |
353 | MAKE_WRAP_SYM_N(execvp); |
78684356 |
354 | MAKE_WRAP_SYM_N(execve); |
b4f4cb40 |
355 | MAKE_WRAP_SYM_N(chdir); |
c3831532 |
356 | MAKE_WRAP_SYM_N(readlink); |
7fd42181 |
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[] = { |
b4f4cb40 |
366 | //REAL_FUNC_NP(open), |
7fd42181 |
367 | REAL_FUNC_NP(fopen), |
b4f4cb40 |
368 | //REAL_FUNC_NP(mmap), |
369 | //REAL_FUNC_NP(munmap), |
370 | //REAL_FUNC_NP(read), |
371 | //REAL_FUNC_NP(ioctl), |
7fd42181 |
372 | REAL_FUNC_NP(sigaction), |
373 | REAL_FUNC_NP(tcgetattr), |
374 | REAL_FUNC_NP(tcsetattr), |
375 | REAL_FUNC_NP(system), |
db9191ed |
376 | // exec* - skipped |
78684356 |
377 | //REAL_FUNC_NP(execve), |
b4f4cb40 |
378 | //REAL_FUNC_NP(chdir), |
7fd42181 |
379 | }; |
380 | |
b4f4cb40 |
381 | //#define open p_real_open |
7fd42181 |
382 | #define fopen p_real_fopen |
b4f4cb40 |
383 | //#define mmap p_real_mmap |
384 | //#define munmap p_real_munmap |
385 | //#define read p_real_read |
386 | //#define ioctl p_real_ioctl |
7fd42181 |
387 | #define sigaction p_real_sigaction |
388 | #define tcgetattr p_real_tcgetattr |
389 | #define tcsetattr p_real_tcsetattr |
390 | #define system p_real_system |
78684356 |
391 | //#define execve p_real_execve |
b4f4cb40 |
392 | //#define chdir p_real_chdir |
7fd42181 |
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 |
4d045184 |
400 | int real_open(const char *pathname, int flags, ...) |
7fd42181 |
401 | { |
4d045184 |
402 | va_list ap; |
403 | mode_t mode; |
b4f4cb40 |
404 | long ret; |
4d045184 |
405 | |
406 | va_start(ap, flags); |
407 | mode = va_arg(ap, mode_t); |
408 | va_end(ap); |
b4f4cb40 |
409 | ret = g_open_raw(pathname, flags, mode); |
410 | return g_syscall_error(ret); |
7fd42181 |
411 | } |
412 | |
413 | FILE *real_fopen(const char *path, const char *mode) |
414 | { |
415 | return fopen(path, mode); |
416 | } |
417 | |
b4f4cb40 |
418 | void *real_mmap(void *addr, size_t length, int prot, int flags, |
419 | int fd, off_t offset) |
7fd42181 |
420 | { |
b4f4cb40 |
421 | long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12); |
422 | return (void *)g_syscall_error(ret); |
7fd42181 |
423 | } |
424 | |
3ef07128 |
425 | int real_munmap(void *addr, size_t length) |
426 | { |
b4f4cb40 |
427 | return g_syscall_error(g_munmap_raw(addr, length)); |
3ef07128 |
428 | } |
429 | |
7fd42181 |
430 | int real_read(int fd, void *buf, size_t count) |
431 | { |
b4f4cb40 |
432 | long ret = g_read_raw(fd, buf, count); |
433 | return g_syscall_error(ret); |
7fd42181 |
434 | } |
435 | |
b4f4cb40 |
436 | int real_ioctl(int fd, int request, void *argp) |
7fd42181 |
437 | { |
b4f4cb40 |
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); |
7fd42181 |
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 | } |
db9191ed |
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 | { |
78684356 |
474 | long ret = g_execve_raw(filename, argv, envp); |
475 | return g_syscall_error(ret); |
db9191ed |
476 | } |
477 | |
478 | int real_chdir(const char *path) |
479 | { |
b4f4cb40 |
480 | long ret = g_chdir_raw(path); |
481 | return g_syscall_error(ret); |
482 | } |
483 | |
c3831532 |
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 | |
b4f4cb40 |
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); |
db9191ed |
505 | } |
499bf01c |
506 | |
507 | // vim:shiftwidth=2:expandtab |