2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
30 /* XXX: maybe unhardcode pagesize? */
31 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
32 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
34 #define MAP_HUGETLB 0x40000 /* arch specific */
38 int plat_is_dir(const char *path)
41 if ((dir = opendir(path))) {
48 int plat_get_data_dir(char *dst, int len)
56 static int plat_get_exe_dir(char *dst, int len)
59 memcpy(dst, PICO_DATA_DIR, sizeof PICO_DATA_DIR);
60 return sizeof(PICO_DATA_DIR) - 1;
62 int j, ret = readlink(
75 for (j = ret - 1; j > 0; j--)
84 int plat_get_skin_dir(char *dst, int len)
86 int ret = plat_get_exe_dir(dst, len);
90 memcpy(dst + ret, "skin/", sizeof "skin/");
91 return ret + sizeof("skin/") - 1;
95 #define PICO_HOME_DIR "/.picodrive/"
97 int plat_get_root_dir(char *dst, int len)
99 #if !defined(NO_HOME_DIR) && !defined(__GP2X__) && !defined(PANDORA)
100 const char *home = getenv("HOME");
104 ret = snprintf(dst, len, "%s%s", home, PICO_HOME_DIR);
111 return plat_get_exe_dir(dst, len);
115 /* Wiz has a borked gettimeofday().. */
116 #define plat_get_ticks_ms plat_get_ticks_ms_good
117 #define plat_get_ticks_us plat_get_ticks_us_good
120 unsigned int plat_get_ticks_ms(void)
125 gettimeofday(&tv, NULL);
127 ret = (unsigned)tv.tv_sec * 1000;
128 /* approximate /= 1000 */
129 ret += ((unsigned)tv.tv_usec * 4194) >> 22;
134 unsigned int plat_get_ticks_us(void)
139 gettimeofday(&tv, NULL);
141 ret = (unsigned)tv.tv_sec * 1000000;
142 ret += (unsigned)tv.tv_usec;
147 void plat_sleep_ms(int ms)
152 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
155 struct timeval tv, *timeout = NULL;
156 int i, ret, fdmax = -1;
159 if (timeout_ms >= 0) {
160 tv.tv_sec = timeout_ms / 1000;
161 tv.tv_usec = (timeout_ms % 1000) * 1000;
166 for (i = 0; i < count; i++) {
167 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
168 FD_SET(fds_hnds[i], &fdset);
171 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
174 perror("plat_wait_event: select failed");
180 return -1; /* timeout */
183 for (i = 0; i < count; i++)
184 if (FD_ISSET(fds_hnds[i], &fdset))
192 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
194 static int hugetlb_warned;
195 int prot = PROT_READ | PROT_WRITE;
196 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
202 /* avoid MAP_FIXED, it overrides existing mappings..
206 if (size >= HUGETLB_THRESHOLD)
207 flags |= MAP_HUGETLB;
213 ret = mmap(req, size, prot, flags, -1, 0);
214 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
215 if (!hugetlb_warned) {
217 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
221 flags &= ~MAP_HUGETLB;
222 ret = mmap(req, size, prot, flags, -1, 0);
224 if (ret != MAP_FAILED && ((uintptr_t)ret & (2*1024*1024 - 1))) {
225 // try to manually realign assuming bottom-to-top alloc
227 ret = (void *)((uintptr_t)ret & ~(2*1024*1024 - 1));
228 ret = mmap(ret, size, prot, flags, -1, 0);
230 if (ret != MAP_FAILED)
231 madvise(ret, size, MADV_HUGEPAGE);
234 if (ret == MAP_FAILED)
237 if (req != NULL && ret != req) {
238 fprintf(stderr, "%s: mmaped to %p, requested %p\n",
239 is_fixed ? "error" : "warning", ret, req);
249 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
253 #ifdef MREMAP_MAYMOVE
254 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
255 if (ret == MAP_FAILED)
258 fprintf(stderr, "mremap %p %zd %zd: ",
259 ptr, oldsize, newsize);
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)
266 memcpy(ret, ptr, oldsize);
267 munmap(ptr, oldsize);
270 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
275 void plat_munmap(void *ptr, size_t size)
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);
287 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
291 int plat_mem_set_exec(void *ptr, size_t size)
293 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
295 fprintf(stderr, "mprotect(%p, %zd) failed: %d\n",
302 void lprintf(const char *fmt, ...)