plat: avoid MAP_FIXED
[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 <dirent.h>
17 #include <sys/time.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <sys/mman.h>
21 #include <errno.h>
22 #include <sys/stat.h>
23
24 #include "../plat.h"
25
26 /* XXX: maybe unhardcode pagesize? */
27 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
28 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
29 #ifndef MAP_HUGETLB
30 #define MAP_HUGETLB 0x40000 /* arch specific */
31 #endif
32
33
34 int plat_is_dir(const char *path)
35 {
36         DIR *dir;
37         if ((dir = opendir(path))) {
38                 closedir(dir);
39                 return 1;
40         }
41         return 0;
42 }
43
44 static int plat_get_data_dir(char *dst, int len)
45 {
46 #ifdef PICO_DATA_DIR
47         memcpy(dst, PICO_DATA_DIR, sizeof PICO_DATA_DIR);
48         return sizeof(PICO_DATA_DIR) - 1;
49 #else
50         int j, ret = readlink("/proc/self/exe", dst, len - 1);
51         if (ret < 0) {
52                 perror("readlink");
53                 ret = 0;
54         }
55         dst[ret] = 0;
56
57         for (j = ret - 1; j > 0; j--)
58                 if (dst[j] == '/') {
59                         dst[++j] = 0;
60                         break;
61                 }
62         return j;
63 #endif
64 }
65
66 int plat_get_skin_dir(char *dst, int len)
67 {
68         int ret = plat_get_data_dir(dst, len);
69         if (ret < 0)
70                 return ret;
71
72         memcpy(dst + ret, "skin/", sizeof "skin/");
73         return ret + sizeof("skin/") - 1;
74 }
75
76 #ifndef PICO_HOME_DIR
77 #define PICO_HOME_DIR "/.picodrive/"
78 #endif
79 int plat_get_root_dir(char *dst, int len)
80 {
81 #if !defined(__GP2X__) && !defined(PANDORA)
82         const char *home = getenv("HOME");
83         int ret;
84
85         if (home != NULL) {
86                 ret = snprintf(dst, len, "%s%s", home, PICO_HOME_DIR);
87                 if (ret >= len)
88                         ret = len - 1;
89                 mkdir(dst, 0755);
90                 return ret;
91         }
92 #endif
93         return plat_get_data_dir(dst, len);
94 }
95
96 #ifdef __GP2X__
97 /* Wiz has a borked gettimeofday().. */
98 #define plat_get_ticks_ms plat_get_ticks_ms_good
99 #define plat_get_ticks_us plat_get_ticks_us_good
100 #endif
101
102 unsigned int plat_get_ticks_ms(void)
103 {
104         struct timeval tv;
105         unsigned int ret;
106
107         gettimeofday(&tv, NULL);
108
109         ret = (unsigned)tv.tv_sec * 1000;
110         /* approximate /= 1000 */
111         ret += ((unsigned)tv.tv_usec * 4195) >> 22;
112
113         return ret;
114 }
115
116 unsigned int plat_get_ticks_us(void)
117 {
118         struct timeval tv;
119         unsigned int ret;
120
121         gettimeofday(&tv, NULL);
122
123         ret = (unsigned)tv.tv_sec * 1000000;
124         ret += (unsigned)tv.tv_usec;
125
126         return ret;
127 }
128
129 void plat_sleep_ms(int ms)
130 {
131         usleep(ms * 1000);
132 }
133
134 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
135 {
136         struct timeval tv, *timeout = NULL;
137         int i, ret, fdmax = -1;
138         fd_set fdset;
139
140         if (timeout_ms >= 0) {
141                 tv.tv_sec = timeout_ms / 1000;
142                 tv.tv_usec = (timeout_ms % 1000) * 1000;
143                 timeout = &tv;
144         }
145
146         FD_ZERO(&fdset);
147         for (i = 0; i < count; i++) {
148                 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
149                 FD_SET(fds_hnds[i], &fdset);
150         }
151
152         ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
153         if (ret == -1)
154         {
155                 perror("plat_wait_event: select failed");
156                 sleep(1);
157                 return -1;
158         }
159
160         if (ret == 0)
161                 return -1; /* timeout */
162
163         ret = -1;
164         for (i = 0; i < count; i++)
165                 if (FD_ISSET(fds_hnds[i], &fdset))
166                         ret = fds_hnds[i];
167
168         return ret;
169 }
170
171 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
172 {
173         static int hugetlb_warned;
174         int prot = PROT_READ | PROT_WRITE;
175         int flags = MAP_PRIVATE | MAP_ANONYMOUS;
176         void *req, *ret;
177
178         req = (void *)addr;
179         if (need_exec)
180                 prot |= PROT_EXEC;
181         /* avoid MAP_FIXED, it overrides existing mappings..
182         if (is_fixed)
183                 flags |= MAP_FIXED;
184         */
185         if (size >= HUGETLB_THRESHOLD)
186                 flags |= MAP_HUGETLB;
187
188         ret = mmap(req, size, prot, flags, -1, 0);
189         if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
190                 if (!hugetlb_warned) {
191                         fprintf(stderr,
192                                 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
193                                 req, size, errno);
194                         hugetlb_warned = 1;
195                 }
196                 flags &= ~MAP_HUGETLB;
197                 ret = mmap(req, size, prot, flags, -1, 0);
198         }
199         if (ret == MAP_FAILED)
200                 return NULL;
201
202         if (req != NULL && ret != req) {
203                 fprintf(stderr, "%s: mmaped to %p, requested %p\n",
204                         is_fixed ? "error" : "warning", ret, req);
205                 if (is_fixed) {
206                         munmap(ret, size);
207                         return NULL;
208                 }
209         }
210
211         return ret;
212 }
213
214 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
215 {
216         void *ret;
217
218         ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
219         if (ret == MAP_FAILED)
220                 return NULL;
221         if (ret != ptr)
222                 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
223
224         return ret;
225 }
226
227 void plat_munmap(void *ptr, size_t size)
228 {
229         int ret;
230
231         ret = munmap(ptr, size);
232         if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
233                 // prehaps an autorounded hugetlb mapping?
234                 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
235                 ret = munmap(ptr, size);
236         }
237         if (ret != 0) {
238                 fprintf(stderr,
239                         "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
240         }
241 }
242
243 int plat_mem_set_exec(void *ptr, size_t size)
244 {
245         int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
246         if (ret != 0)
247                 fprintf(stderr, "mprotect(%p, %zd) failed: %d\n",
248                         ptr, size, errno);
249
250         return ret;
251 }
252
253 /* lprintf */
254 void lprintf(const char *fmt, ...)
255 {
256         va_list vl;
257
258         va_start(vl, fmt);
259         vprintf(fmt, vl);
260         va_end(vl);
261 }
262