extend mmap wrapper functionality
[libpicofe.git] / linux / plat.c
CommitLineData
957a9eac 1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
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.
8 * See the COPYING file in the top-level directory.
9 */
10
705bdf63 11#define _GNU_SOURCE 1
4ab30ad4 12#include <stdio.h>
d2f29611 13#include <string.h>
2b9f9c51 14#include <stdarg.h>
049a6b3e 15#include <dirent.h>
4ab30ad4 16#include <sys/time.h>
17#include <time.h>
18#include <unistd.h>
997b2e4f 19#include <sys/mman.h>
ca69c3e5 20#include <errno.h>
4ab30ad4 21
049a6b3e 22#include "../common/plat.h"
23
ca69c3e5 24/* XXX: maybe unhardcode pagesize? */
25#define HUGETLB_PAGESIZE (2 * 1024 * 1024)
26#define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
27#ifndef MAP_HUGETLB
28#define MAP_HUGETLB 0x40000 /* arch specific */
29#endif
30
049a6b3e 31
32int plat_is_dir(const char *path)
33{
34 DIR *dir;
35 if ((dir = opendir(path))) {
36 closedir(dir);
37 return 1;
38 }
39 return 0;
40}
41
d2f29611 42int plat_get_root_dir(char *dst, int len)
43{
2b9f9c51 44 int j, ret;
45
46 ret = readlink("/proc/self/exe", dst, len - 1);
47 if (ret < 0) {
48 perror("readlink");
49 ret = 0;
50 }
51 dst[ret] = 0;
d2f29611 52
d2f29611 53 for (j = strlen(dst); j > 0; j--)
2b9f9c51 54 if (dst[j] == '/') {
55 dst[++j] = 0;
56 break;
57 }
d2f29611 58
f35e50ef 59 return j;
d2f29611 60}
61
b5bfb864 62#ifdef __GP2X__
63/* Wiz has a borked gettimeofday().. */
1eb704b6 64#define plat_get_ticks_ms plat_get_ticks_ms_good
65#define plat_get_ticks_us plat_get_ticks_us_good
b5bfb864 66#endif
67
4ab30ad4 68unsigned int plat_get_ticks_ms(void)
69{
70 struct timeval tv;
71 unsigned int ret;
72
73 gettimeofday(&tv, NULL);
74
75 ret = (unsigned)tv.tv_sec * 1000;
b5bfb864 76 /* approximate /= 1000 */
4ab30ad4 77 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
78
79 return ret;
80}
81
b5bfb864 82unsigned int plat_get_ticks_us(void)
83{
84 struct timeval tv;
85 unsigned int ret;
86
87 gettimeofday(&tv, NULL);
88
89 ret = (unsigned)tv.tv_sec * 1000000;
90 ret += (unsigned)tv.tv_usec;
91
92 return ret;
93}
94
4ab30ad4 95void plat_sleep_ms(int ms)
96{
97 usleep(ms * 1000);
98}
99
100int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
101{
102 struct timeval tv, *timeout = NULL;
103 int i, ret, fdmax = -1;
104 fd_set fdset;
105
106 if (timeout_ms >= 0) {
107 tv.tv_sec = timeout_ms / 1000;
108 tv.tv_usec = (timeout_ms % 1000) * 1000;
109 timeout = &tv;
110 }
111
112 FD_ZERO(&fdset);
113 for (i = 0; i < count; i++) {
114 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
115 FD_SET(fds_hnds[i], &fdset);
116 }
117
118 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
119 if (ret == -1)
120 {
121 perror("plat_wait_event: select failed");
122 sleep(1);
123 return -1;
124 }
125
126 if (ret == 0)
127 return -1; /* timeout */
128
129 ret = -1;
130 for (i = 0; i < count; i++)
131 if (FD_ISSET(fds_hnds[i], &fdset))
132 ret = fds_hnds[i];
133
134 return ret;
135}
136
ca69c3e5 137void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
997b2e4f 138{
ca69c3e5 139 static int hugetlb_disabled;
140 int prot = PROT_READ | PROT_WRITE;
141 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
997b2e4f 142 void *req, *ret;
1ed48049 143
997b2e4f 144 req = (void *)addr;
ca69c3e5 145 if (need_exec)
146 prot |= PROT_EXEC;
147 if (is_fixed)
148 flags |= MAP_FIXED;
149 if (size >= HUGETLB_THRESHOLD && !hugetlb_disabled)
150 flags |= MAP_HUGETLB;
151
152 ret = mmap(req, size, prot, flags, -1, 0);
153 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
154 fprintf(stderr,
155 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
156 req, size, errno);
157 hugetlb_disabled = 1;
158 flags &= ~MAP_HUGETLB;
159 ret = mmap(req, size, prot, flags, -1, 0);
160 }
997b2e4f 161 if (ret == MAP_FAILED)
162 return NULL;
ca69c3e5 163
164 if (req != NULL && ret != req)
165 fprintf(stderr,
166 "warning: mmaped to %p, requested %p\n", ret, req);
997b2e4f 167
168 return ret;
169}
170
1ed48049 171void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
172{
173 void *ret;
174
175 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
176 if (ret == MAP_FAILED)
177 return NULL;
178 if (ret != ptr)
179 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
180
181 return ret;
182}
183
997b2e4f 184void plat_munmap(void *ptr, size_t size)
185{
ca69c3e5 186 int ret;
187
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);
193 }
194 if (ret != 0) {
195 fprintf(stderr,
196 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
197 }
997b2e4f 198}
199
2b9f9c51 200/* lprintf */
201void lprintf(const char *fmt, ...)
202{
203 va_list vl;
204
205 va_start(vl, fmt);
206 vprintf(fmt, vl);
207 va_end(vl);
208}
209