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