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