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.
27 /* XXX: maybe unhardcode pagesize? */
28 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
29 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
31 #define MAP_HUGETLB 0x40000 /* arch specific */
35 int plat_is_dir(const char *path)
38 if ((dir = opendir(path))) {
45 int plat_get_data_dir(char *dst, int len)
53 static int plat_get_exe_dir(char *dst, int len)
56 memcpy(dst, PICO_DATA_DIR, sizeof PICO_DATA_DIR);
57 return sizeof(PICO_DATA_DIR) - 1;
59 int j, ret = readlink(
72 for (j = ret - 1; j > 0; j--)
81 int plat_get_skin_dir(char *dst, int len)
83 int ret = plat_get_exe_dir(dst, len);
87 memcpy(dst + ret, "skin/", sizeof "skin/");
88 return ret + sizeof("skin/") - 1;
92 #define PICO_HOME_DIR "/.picodrive/"
94 int plat_get_root_dir(char *dst, int len)
96 #if !defined(NO_HOME_DIR) && !defined(__GP2X__) && !defined(PANDORA)
97 const char *home = getenv("HOME");
101 ret = snprintf(dst, len, "%s%s", home, PICO_HOME_DIR);
108 return plat_get_exe_dir(dst, len);
112 /* Wiz has a borked gettimeofday().. */
113 #define plat_get_ticks_ms plat_get_ticks_ms_good
114 #define plat_get_ticks_us plat_get_ticks_us_good
117 unsigned int plat_get_ticks_ms(void)
122 gettimeofday(&tv, NULL);
124 ret = (unsigned)tv.tv_sec * 1000;
125 /* approximate /= 1000 */
126 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
131 unsigned int plat_get_ticks_us(void)
136 gettimeofday(&tv, NULL);
138 ret = (unsigned)tv.tv_sec * 1000000;
139 ret += (unsigned)tv.tv_usec;
144 void plat_sleep_ms(int ms)
149 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
151 struct timeval tv, *timeout = NULL;
152 int i, ret, fdmax = -1;
155 if (timeout_ms >= 0) {
156 tv.tv_sec = timeout_ms / 1000;
157 tv.tv_usec = (timeout_ms % 1000) * 1000;
162 for (i = 0; i < count; i++) {
163 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
164 FD_SET(fds_hnds[i], &fdset);
167 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
170 perror("plat_wait_event: select failed");
176 return -1; /* timeout */
179 for (i = 0; i < count; i++)
180 if (FD_ISSET(fds_hnds[i], &fdset))
186 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
188 static int hugetlb_warned;
189 int prot = PROT_READ | PROT_WRITE;
190 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
196 /* avoid MAP_FIXED, it overrides existing mappings..
200 if (size >= HUGETLB_THRESHOLD)
201 flags |= MAP_HUGETLB;
207 ret = mmap(req, size, prot, flags, -1, 0);
208 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
209 if (!hugetlb_warned) {
211 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
215 flags &= ~MAP_HUGETLB;
216 ret = mmap(req, size, prot, flags, -1, 0);
218 if (ret != MAP_FAILED && ((uintptr_t)ret & (2*1024*1024 - 1))) {
219 // try to manually realign assuming bottom-to-top alloc
221 ret = (void *)((uintptr_t)ret & ~(2*1024*1024 - 1));
222 ret = mmap(ret, size, prot, flags, -1, 0);
224 if (ret != MAP_FAILED)
225 madvise(ret, size, MADV_HUGEPAGE);
228 if (ret == MAP_FAILED)
231 if (req != NULL && ret != req) {
232 fprintf(stderr, "%s: mmaped to %p, requested %p\n",
233 is_fixed ? "error" : "warning", ret, req);
243 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
247 #ifdef MREMAP_MAYMOVE
248 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
249 if (ret == MAP_FAILED)
252 fprintf(stderr, "mremap %p %zd %zd: ",
253 ptr, oldsize, newsize);
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)
260 memcpy(ret, ptr, oldsize);
261 munmap(ptr, oldsize);
264 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
269 void plat_munmap(void *ptr, size_t size)
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);
281 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
285 int plat_mem_set_exec(void *ptr, size_t size)
287 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
289 fprintf(stderr, "mprotect(%p, %zd) failed: %d\n",
296 void lprintf(const char *fmt, ...)