add plat_mem_set_exec()
[libpicofe.git] / linux / plat.c
CommitLineData
957a9eac 1/*
f89d8471 2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2012
957a9eac 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.
f89d8471 8 * - MAME license.
957a9eac 9 * See the COPYING file in the top-level directory.
10 */
11
705bdf63 12#define _GNU_SOURCE 1
4ab30ad4 13#include <stdio.h>
d2f29611 14#include <string.h>
2b9f9c51 15#include <stdarg.h>
049a6b3e 16#include <dirent.h>
4ab30ad4 17#include <sys/time.h>
18#include <time.h>
19#include <unistd.h>
997b2e4f 20#include <sys/mman.h>
ca69c3e5 21#include <errno.h>
4ab30ad4 22
a86e9a3e 23#include "../plat.h"
049a6b3e 24
ca69c3e5 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
049a6b3e 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
d2f29611 43int plat_get_root_dir(char *dst, int len)
44{
2b9f9c51 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;
d2f29611 53
d2f29611 54 for (j = strlen(dst); j > 0; j--)
2b9f9c51 55 if (dst[j] == '/') {
56 dst[++j] = 0;
57 break;
58 }
d2f29611 59
f35e50ef 60 return j;
d2f29611 61}
62
b5bfb864 63#ifdef __GP2X__
64/* Wiz has a borked gettimeofday().. */
1eb704b6 65#define plat_get_ticks_ms plat_get_ticks_ms_good
66#define plat_get_ticks_us plat_get_ticks_us_good
b5bfb864 67#endif
68
4ab30ad4 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;
b5bfb864 77 /* approximate /= 1000 */
4ab30ad4 78 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
79
80 return ret;
81}
82
b5bfb864 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
4ab30ad4 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
ca69c3e5 138void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
997b2e4f 139{
f4750ef3 140 static int hugetlb_warned;
ca69c3e5 141 int prot = PROT_READ | PROT_WRITE;
142 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
997b2e4f 143 void *req, *ret;
1ed48049 144
997b2e4f 145 req = (void *)addr;
ca69c3e5 146 if (need_exec)
147 prot |= PROT_EXEC;
148 if (is_fixed)
149 flags |= MAP_FIXED;
f4750ef3 150 if (size >= HUGETLB_THRESHOLD)
ca69c3e5 151 flags |= MAP_HUGETLB;
152
153 ret = mmap(req, size, prot, flags, -1, 0);
154 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
f4750ef3 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 }
ca69c3e5 161 flags &= ~MAP_HUGETLB;
162 ret = mmap(req, size, prot, flags, -1, 0);
163 }
997b2e4f 164 if (ret == MAP_FAILED)
165 return NULL;
ca69c3e5 166
167 if (req != NULL && ret != req)
168 fprintf(stderr,
169 "warning: mmaped to %p, requested %p\n", ret, req);
997b2e4f 170
171 return ret;
172}
173
1ed48049 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
997b2e4f 187void plat_munmap(void *ptr, size_t size)
188{
ca69c3e5 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 }
997b2e4f 201}
202
6282e17e
GI
203int plat_mem_set_exec(void *ptr, size_t size)
204{
205 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
206 if (ret != 0)
207 fprintf(stderr, "mprotect(%p, %zd) failed: %d\n",
208 ptr, size, errno);
209
210 return ret;
211}
212
2b9f9c51 213/* lprintf */
214void lprintf(const char *fmt, ...)
215{
216 va_list vl;
217
218 va_start(vl, fmt);
219 vprintf(fmt, vl);
220 va_end(vl);
221}
222