update license in source code itself
[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{
ca69c3e5 140 static int hugetlb_disabled;
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;
150 if (size >= HUGETLB_THRESHOLD && !hugetlb_disabled)
151 flags |= MAP_HUGETLB;
152
153 ret = mmap(req, size, prot, flags, -1, 0);
154 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
155 fprintf(stderr,
156 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
157 req, size, errno);
158 hugetlb_disabled = 1;
159 flags &= ~MAP_HUGETLB;
160 ret = mmap(req, size, prot, flags, -1, 0);
161 }
997b2e4f 162 if (ret == MAP_FAILED)
163 return NULL;
ca69c3e5 164
165 if (req != NULL && ret != req)
166 fprintf(stderr,
167 "warning: mmaped to %p, requested %p\n", ret, req);
997b2e4f 168
169 return ret;
170}
171
1ed48049 172void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
173{
174 void *ret;
175
176 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
177 if (ret == MAP_FAILED)
178 return NULL;
179 if (ret != ptr)
180 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
181
182 return ret;
183}
184
997b2e4f 185void plat_munmap(void *ptr, size_t size)
186{
ca69c3e5 187 int ret;
188
189 ret = munmap(ptr, size);
190 if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
191 // prehaps an autorounded hugetlb mapping?
192 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
193 ret = munmap(ptr, size);
194 }
195 if (ret != 0) {
196 fprintf(stderr,
197 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
198 }
997b2e4f 199}
200
2b9f9c51 201/* lprintf */
202void lprintf(const char *fmt, ...)
203{
204 va_list vl;
205
206 va_start(vl, fmt);
207 vprintf(fmt, vl);
208 va_end(vl);
209}
210