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.
25 /* XXX: maybe unhardcode pagesize? */
26 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
27 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
29 #define MAP_HUGETLB 0x40000 /* arch specific */
33 int plat_is_dir(const char *path)
36 if ((dir = opendir(path))) {
43 int plat_get_root_dir(char *dst, int len)
47 ret = readlink("/proc/self/exe", dst, len - 1);
54 for (j = strlen(dst); j > 0; j--)
64 /* Wiz has a borked gettimeofday().. */
65 #define plat_get_ticks_ms plat_get_ticks_ms_good
66 #define plat_get_ticks_us plat_get_ticks_us_good
69 unsigned int plat_get_ticks_ms(void)
74 gettimeofday(&tv, NULL);
76 ret = (unsigned)tv.tv_sec * 1000;
77 /* approximate /= 1000 */
78 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
83 unsigned int plat_get_ticks_us(void)
88 gettimeofday(&tv, NULL);
90 ret = (unsigned)tv.tv_sec * 1000000;
91 ret += (unsigned)tv.tv_usec;
96 void plat_sleep_ms(int ms)
101 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
103 struct timeval tv, *timeout = NULL;
104 int i, ret, fdmax = -1;
107 if (timeout_ms >= 0) {
108 tv.tv_sec = timeout_ms / 1000;
109 tv.tv_usec = (timeout_ms % 1000) * 1000;
114 for (i = 0; i < count; i++) {
115 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
116 FD_SET(fds_hnds[i], &fdset);
119 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
122 perror("plat_wait_event: select failed");
128 return -1; /* timeout */
131 for (i = 0; i < count; i++)
132 if (FD_ISSET(fds_hnds[i], &fdset))
138 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
140 static int hugetlb_warned;
141 int prot = PROT_READ | PROT_WRITE;
142 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
150 if (size >= HUGETLB_THRESHOLD)
151 flags |= MAP_HUGETLB;
153 ret = mmap(req, size, prot, flags, -1, 0);
154 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
155 if (!hugetlb_warned) {
157 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
161 flags &= ~MAP_HUGETLB;
162 ret = mmap(req, size, prot, flags, -1, 0);
164 if (ret == MAP_FAILED)
167 if (req != NULL && ret != req)
169 "warning: mmaped to %p, requested %p\n", ret, req);
174 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
178 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
179 if (ret == MAP_FAILED)
182 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
187 void plat_munmap(void *ptr, size_t size)
191 ret = munmap(ptr, size);
192 if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
193 // prehaps an autorounded hugetlb mapping?
194 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
195 ret = munmap(ptr, size);
199 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
204 void lprintf(const char *fmt, ...)