plat: avoid MAP_FIXED
[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>
049a6b3e 16#include <dirent.h>
4ab30ad4 17#include <sys/time.h>
18#include <time.h>
19#include <unistd.h>
997b2e4f 20#include <sys/mman.h>
ca69c3e5 21#include <errno.h>
c52e6628 22#include <sys/stat.h>
4ab30ad4 23
a86e9a3e 24#include "../plat.h"
049a6b3e 25
ca69c3e5 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
049a6b3e 33
34int 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
c52e6628 44static int plat_get_data_dir(char *dst, int len)
d2f29611 45{
c52e6628
PC
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);
2b9f9c51 51 if (ret < 0) {
52 perror("readlink");
53 ret = 0;
54 }
55 dst[ret] = 0;
d2f29611 56
c52e6628 57 for (j = ret - 1; j > 0; j--)
2b9f9c51 58 if (dst[j] == '/') {
59 dst[++j] = 0;
60 break;
61 }
f35e50ef 62 return j;
c52e6628
PC
63#endif
64}
65
66int 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
79int plat_get_root_dir(char *dst, int len)
80{
af7e5006 81#if !defined(__GP2X__) && !defined(PANDORA)
82 const char *home = getenv("HOME");
83 int ret;
c52e6628 84
af7e5006 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 }
c52e6628 92#endif
af7e5006 93 return plat_get_data_dir(dst, len);
d2f29611 94}
95
b5bfb864 96#ifdef __GP2X__
97/* Wiz has a borked gettimeofday().. */
1eb704b6 98#define plat_get_ticks_ms plat_get_ticks_ms_good
99#define plat_get_ticks_us plat_get_ticks_us_good
b5bfb864 100#endif
101
4ab30ad4 102unsigned 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;
b5bfb864 110 /* approximate /= 1000 */
4ab30ad4 111 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
112
113 return ret;
114}
115
b5bfb864 116unsigned 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
4ab30ad4 129void plat_sleep_ms(int ms)
130{
131 usleep(ms * 1000);
132}
133
134int 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
ca69c3e5 171void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
997b2e4f 172{
f4750ef3 173 static int hugetlb_warned;
ca69c3e5 174 int prot = PROT_READ | PROT_WRITE;
175 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
997b2e4f 176 void *req, *ret;
1ed48049 177
997b2e4f 178 req = (void *)addr;
ca69c3e5 179 if (need_exec)
180 prot |= PROT_EXEC;
26ea1817 181 /* avoid MAP_FIXED, it overrides existing mappings..
ca69c3e5 182 if (is_fixed)
183 flags |= MAP_FIXED;
26ea1817 184 */
f4750ef3 185 if (size >= HUGETLB_THRESHOLD)
ca69c3e5 186 flags |= MAP_HUGETLB;
187
188 ret = mmap(req, size, prot, flags, -1, 0);
189 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
f4750ef3 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 }
ca69c3e5 196 flags &= ~MAP_HUGETLB;
197 ret = mmap(req, size, prot, flags, -1, 0);
198 }
997b2e4f 199 if (ret == MAP_FAILED)
200 return NULL;
ca69c3e5 201
26ea1817 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 }
997b2e4f 210
211 return ret;
212}
213
1ed48049 214void *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
997b2e4f 227void plat_munmap(void *ptr, size_t size)
228{
ca69c3e5 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 }
997b2e4f 241}
242
6282e17e
GI
243int 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
2b9f9c51 253/* lprintf */
254void 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