2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
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.
8 * See the COPYING file in the top-level directory.
22 #include "../common/plat.h"
24 /* XXX: maybe unhardcode pagesize? */
25 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
26 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
28 #define MAP_HUGETLB 0x40000 /* arch specific */
32 int plat_is_dir(const char *path)
35 if ((dir = opendir(path))) {
42 int plat_get_root_dir(char *dst, int len)
46 ret = readlink("/proc/self/exe", dst, len - 1);
53 for (j = strlen(dst); j > 0; j--)
63 /* Wiz has a borked gettimeofday().. */
64 #define plat_get_ticks_ms plat_get_ticks_ms_good
65 #define plat_get_ticks_us plat_get_ticks_us_good
68 unsigned int plat_get_ticks_ms(void)
73 gettimeofday(&tv, NULL);
75 ret = (unsigned)tv.tv_sec * 1000;
76 /* approximate /= 1000 */
77 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
82 unsigned int plat_get_ticks_us(void)
87 gettimeofday(&tv, NULL);
89 ret = (unsigned)tv.tv_sec * 1000000;
90 ret += (unsigned)tv.tv_usec;
95 void plat_sleep_ms(int ms)
100 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
102 struct timeval tv, *timeout = NULL;
103 int i, ret, fdmax = -1;
106 if (timeout_ms >= 0) {
107 tv.tv_sec = timeout_ms / 1000;
108 tv.tv_usec = (timeout_ms % 1000) * 1000;
113 for (i = 0; i < count; i++) {
114 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
115 FD_SET(fds_hnds[i], &fdset);
118 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
121 perror("plat_wait_event: select failed");
127 return -1; /* timeout */
130 for (i = 0; i < count; i++)
131 if (FD_ISSET(fds_hnds[i], &fdset))
137 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
139 static int hugetlb_disabled;
140 int prot = PROT_READ | PROT_WRITE;
141 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
149 if (size >= HUGETLB_THRESHOLD && !hugetlb_disabled)
150 flags |= MAP_HUGETLB;
152 ret = mmap(req, size, prot, flags, -1, 0);
153 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
155 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
157 hugetlb_disabled = 1;
158 flags &= ~MAP_HUGETLB;
159 ret = mmap(req, size, prot, flags, -1, 0);
161 if (ret == MAP_FAILED)
164 if (req != NULL && ret != req)
166 "warning: mmaped to %p, requested %p\n", ret, req);
171 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
175 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
176 if (ret == MAP_FAILED)
179 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
184 void plat_munmap(void *ptr, size_t size)
188 ret = munmap(ptr, size);
189 if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
190 // prehaps an autorounded hugetlb mapping?
191 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
192 ret = munmap(ptr, size);
196 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
201 void lprintf(const char *fmt, ...)