Commit | Line | Data |
---|---|---|
957a9eac | 1 | /* |
f89d8471 | 2 | * (C) GraÅžvydas "notaz" Ignotas, 2008-2012 |
957a9eac | 3 | * |
4 | * This work is licensed under the terms of any of these licenses | |
5 | * (at your option): | |
6 | * - GNU GPL, version 2 or later. | |
7 | * - GNU LGPL, version 2.1 or later. | |
f89d8471 | 8 | * - MAME license. |
957a9eac | 9 | * See the COPYING file in the top-level directory. |
10 | */ | |
11 | ||
705bdf63 | 12 | #define _GNU_SOURCE 1 |
4ab30ad4 | 13 | #include <stdio.h> |
d2f29611 | 14 | #include <string.h> |
2b9f9c51 | 15 | #include <stdarg.h> |
978d305f | 16 | #include <stdint.h> |
049a6b3e | 17 | #include <dirent.h> |
4ab30ad4 | 18 | #include <sys/time.h> |
19 | #include <time.h> | |
20 | #include <unistd.h> | |
ca69c3e5 | 21 | #include <errno.h> |
c52e6628 | 22 | #include <sys/stat.h> |
9fba90ac | 23 | #ifndef __MINGW32__ |
24 | #include <sys/mman.h> | |
25 | #endif | |
4ab30ad4 | 26 | |
a86e9a3e | 27 | #include "../plat.h" |
9fba90ac | 28 | #include "../posix.h" |
049a6b3e | 29 | |
ca69c3e5 | 30 | /* XXX: maybe unhardcode pagesize? */ |
31 | #define HUGETLB_PAGESIZE (2 * 1024 * 1024) | |
32 | #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2) | |
33 | #ifndef MAP_HUGETLB | |
34 | #define MAP_HUGETLB 0x40000 /* arch specific */ | |
35 | #endif | |
36 | ||
049a6b3e | 37 | |
38 | int plat_is_dir(const char *path) | |
39 | { | |
40 | DIR *dir; | |
41 | if ((dir = opendir(path))) { | |
42 | closedir(dir); | |
43 | return 1; | |
44 | } | |
45 | return 0; | |
46 | } | |
47 | ||
a23a278d | 48 | int plat_get_data_dir(char *dst, int len) |
49 | { | |
50 | if (len > 1) | |
51 | strcpy(dst, "/"); | |
52 | else *dst = 0; | |
53 | return strlen(dst); | |
54 | } | |
55 | ||
56 | static int plat_get_exe_dir(char *dst, int len) | |
d2f29611 | 57 | { |
c52e6628 PC |
58 | #ifdef PICO_DATA_DIR |
59 | memcpy(dst, PICO_DATA_DIR, sizeof PICO_DATA_DIR); | |
60 | return sizeof(PICO_DATA_DIR) - 1; | |
61 | #else | |
babe4580 MG |
62 | int j, ret = readlink( |
63 | #ifdef __FreeBSD__ | |
64 | "/proc/curproc/file", | |
65 | #else | |
66 | "/proc/self/exe", | |
67 | #endif | |
68 | dst, len - 1); | |
2b9f9c51 | 69 | if (ret < 0) { |
70 | perror("readlink"); | |
71 | ret = 0; | |
72 | } | |
73 | dst[ret] = 0; | |
d2f29611 | 74 | |
c52e6628 | 75 | for (j = ret - 1; j > 0; j--) |
2b9f9c51 | 76 | if (dst[j] == '/') { |
77 | dst[++j] = 0; | |
78 | break; | |
79 | } | |
f35e50ef | 80 | return j; |
c52e6628 PC |
81 | #endif |
82 | } | |
83 | ||
84 | int plat_get_skin_dir(char *dst, int len) | |
85 | { | |
a23a278d | 86 | int ret = plat_get_exe_dir(dst, len); |
c52e6628 | 87 | if (ret < 0) |
7a431548 | 88 | ret = 0; |
c52e6628 PC |
89 | |
90 | memcpy(dst + ret, "skin/", sizeof "skin/"); | |
91 | return ret + sizeof("skin/") - 1; | |
92 | } | |
93 | ||
94 | #ifndef PICO_HOME_DIR | |
95 | #define PICO_HOME_DIR "/.picodrive/" | |
96 | #endif | |
97 | int plat_get_root_dir(char *dst, int len) | |
98 | { | |
e50d6e06 | 99 | #if !defined(NO_HOME_DIR) && !defined(__GP2X__) && !defined(PANDORA) |
af7e5006 | 100 | const char *home = getenv("HOME"); |
101 | int ret; | |
c52e6628 | 102 | |
af7e5006 | 103 | if (home != NULL) { |
104 | ret = snprintf(dst, len, "%s%s", home, PICO_HOME_DIR); | |
105 | if (ret >= len) | |
106 | ret = len - 1; | |
107 | mkdir(dst, 0755); | |
108 | return ret; | |
109 | } | |
c52e6628 | 110 | #endif |
a23a278d | 111 | return plat_get_exe_dir(dst, len); |
d2f29611 | 112 | } |
113 | ||
b5bfb864 | 114 | #ifdef __GP2X__ |
115 | /* Wiz has a borked gettimeofday().. */ | |
1eb704b6 | 116 | #define plat_get_ticks_ms plat_get_ticks_ms_good |
117 | #define plat_get_ticks_us plat_get_ticks_us_good | |
b5bfb864 | 118 | #endif |
119 | ||
4ab30ad4 | 120 | unsigned int plat_get_ticks_ms(void) |
121 | { | |
122 | struct timeval tv; | |
123 | unsigned int ret; | |
124 | ||
125 | gettimeofday(&tv, NULL); | |
126 | ||
127 | ret = (unsigned)tv.tv_sec * 1000; | |
b5bfb864 | 128 | /* approximate /= 1000 */ |
82b48547 | 129 | ret += ((unsigned)tv.tv_usec * 4194) >> 22; |
4ab30ad4 | 130 | |
131 | return ret; | |
132 | } | |
133 | ||
b5bfb864 | 134 | unsigned int plat_get_ticks_us(void) |
135 | { | |
136 | struct timeval tv; | |
137 | unsigned int ret; | |
138 | ||
139 | gettimeofday(&tv, NULL); | |
140 | ||
141 | ret = (unsigned)tv.tv_sec * 1000000; | |
142 | ret += (unsigned)tv.tv_usec; | |
143 | ||
144 | return ret; | |
145 | } | |
146 | ||
4ab30ad4 | 147 | void plat_sleep_ms(int ms) |
148 | { | |
149 | usleep(ms * 1000); | |
150 | } | |
151 | ||
152 | int plat_wait_event(int *fds_hnds, int count, int timeout_ms) | |
153 | { | |
7e1e5116 | 154 | #ifndef __MINGW32__ |
4ab30ad4 | 155 | struct timeval tv, *timeout = NULL; |
156 | int i, ret, fdmax = -1; | |
157 | fd_set fdset; | |
158 | ||
159 | if (timeout_ms >= 0) { | |
160 | tv.tv_sec = timeout_ms / 1000; | |
161 | tv.tv_usec = (timeout_ms % 1000) * 1000; | |
162 | timeout = &tv; | |
163 | } | |
164 | ||
165 | FD_ZERO(&fdset); | |
166 | for (i = 0; i < count; i++) { | |
167 | if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i]; | |
168 | FD_SET(fds_hnds[i], &fdset); | |
169 | } | |
170 | ||
171 | ret = select(fdmax + 1, &fdset, NULL, NULL, timeout); | |
172 | if (ret == -1) | |
173 | { | |
174 | perror("plat_wait_event: select failed"); | |
175 | sleep(1); | |
176 | return -1; | |
177 | } | |
178 | ||
179 | if (ret == 0) | |
180 | return -1; /* timeout */ | |
181 | ||
182 | ret = -1; | |
183 | for (i = 0; i < count; i++) | |
184 | if (FD_ISSET(fds_hnds[i], &fdset)) | |
185 | ret = fds_hnds[i]; | |
7e1e5116 | 186 | #else |
187 | int ret = -1; | |
188 | #endif | |
4ab30ad4 | 189 | return ret; |
190 | } | |
191 | ||
ca69c3e5 | 192 | void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed) |
997b2e4f | 193 | { |
f4750ef3 | 194 | static int hugetlb_warned; |
ca69c3e5 | 195 | int prot = PROT_READ | PROT_WRITE; |
196 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; | |
997b2e4f | 197 | void *req, *ret; |
1ed48049 | 198 | |
997b2e4f | 199 | req = (void *)addr; |
ca69c3e5 | 200 | if (need_exec) |
201 | prot |= PROT_EXEC; | |
26ea1817 | 202 | /* avoid MAP_FIXED, it overrides existing mappings.. |
ca69c3e5 | 203 | if (is_fixed) |
204 | flags |= MAP_FIXED; | |
26ea1817 | 205 | */ |
f4750ef3 | 206 | if (size >= HUGETLB_THRESHOLD) |
ca69c3e5 | 207 | flags |= MAP_HUGETLB; |
5d636caa | 208 | #ifdef MAP_JIT |
209 | if (need_exec) | |
210 | flags |= MAP_JIT; | |
211 | #endif | |
ca69c3e5 | 212 | |
213 | ret = mmap(req, size, prot, flags, -1, 0); | |
214 | if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) { | |
f4750ef3 | 215 | if (!hugetlb_warned) { |
216 | fprintf(stderr, | |
217 | "warning: failed to do hugetlb mmap (%p, %zu): %d\n", | |
218 | req, size, errno); | |
219 | hugetlb_warned = 1; | |
220 | } | |
ca69c3e5 | 221 | flags &= ~MAP_HUGETLB; |
222 | ret = mmap(req, size, prot, flags, -1, 0); | |
978d305f | 223 | #ifdef MADV_HUGEPAGE |
224 | if (ret != MAP_FAILED && ((uintptr_t)ret & (2*1024*1024 - 1))) { | |
225 | // try to manually realign assuming bottom-to-top alloc | |
226 | munmap(ret, size); | |
227 | ret = (void *)((uintptr_t)ret & ~(2*1024*1024 - 1)); | |
228 | ret = mmap(ret, size, prot, flags, -1, 0); | |
229 | } | |
230 | if (ret != MAP_FAILED) | |
231 | madvise(ret, size, MADV_HUGEPAGE); | |
232 | #endif | |
ca69c3e5 | 233 | } |
997b2e4f | 234 | if (ret == MAP_FAILED) |
235 | return NULL; | |
ca69c3e5 | 236 | |
26ea1817 | 237 | if (req != NULL && ret != req) { |
238 | fprintf(stderr, "%s: mmaped to %p, requested %p\n", | |
239 | is_fixed ? "error" : "warning", ret, req); | |
240 | if (is_fixed) { | |
241 | munmap(ret, size); | |
242 | return NULL; | |
243 | } | |
244 | } | |
997b2e4f | 245 | |
246 | return ret; | |
247 | } | |
248 | ||
1ed48049 | 249 | void *plat_mremap(void *ptr, size_t oldsize, size_t newsize) |
250 | { | |
251 | void *ret; | |
252 | ||
5d636caa | 253 | #ifdef MREMAP_MAYMOVE |
1ed48049 | 254 | ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE); |
5d636caa | 255 | if (ret == MAP_FAILED) |
256 | #endif | |
257 | { | |
448ec62f | 258 | fprintf(stderr, "mremap %p %zd %zd: ", |
259 | ptr, oldsize, newsize); | |
260 | perror(NULL); | |
261 | // might be because huge pages can't be remapped, | |
262 | // just make a new mapping | |
263 | ret = plat_mmap(0, newsize, 0, 0); | |
264 | if (ret == MAP_FAILED) | |
265 | return NULL; | |
266 | memcpy(ret, ptr, oldsize); | |
267 | munmap(ptr, oldsize); | |
268 | } | |
1ed48049 | 269 | if (ret != ptr) |
270 | printf("warning: mremap moved: %p -> %p\n", ptr, ret); | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
997b2e4f | 275 | void plat_munmap(void *ptr, size_t size) |
276 | { | |
ca69c3e5 | 277 | int ret; |
278 | ||
279 | ret = munmap(ptr, size); | |
280 | if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) { | |
281 | // prehaps an autorounded hugetlb mapping? | |
282 | size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1); | |
283 | ret = munmap(ptr, size); | |
284 | } | |
285 | if (ret != 0) { | |
286 | fprintf(stderr, | |
287 | "munmap(%p, %zu) failed: %d\n", ptr, size, errno); | |
288 | } | |
997b2e4f | 289 | } |
290 | ||
6282e17e GI |
291 | int plat_mem_set_exec(void *ptr, size_t size) |
292 | { | |
293 | int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC); | |
294 | if (ret != 0) | |
295 | fprintf(stderr, "mprotect(%p, %zd) failed: %d\n", | |
296 | ptr, size, errno); | |
297 | ||
298 | return ret; | |
299 | } | |
300 | ||
2b9f9c51 | 301 | /* lprintf */ |
302 | void lprintf(const char *fmt, ...) | |
303 | { | |
304 | va_list vl; | |
305 | ||
306 | va_start(vl, fmt); | |
307 | vprintf(fmt, vl); | |
308 | va_end(vl); | |
309 | } | |
310 |