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