use plat_mmap for RAM too
[pcsx_rearmed.git] / frontend / linux / plat.c
CommitLineData
698517be 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
7badc935 11#define _GNU_SOURCE 1
698517be 12#include <stdio.h>
13#include <string.h>
14#include <stdarg.h>
15#include <dirent.h>
16#include <sys/time.h>
17#include <time.h>
18#include <unistd.h>
19#include <sys/mman.h>
9ee0fd5b 20#include <errno.h>
698517be 21
22#include "../common/plat.h"
23
9ee0fd5b 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
698517be 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
42int plat_get_root_dir(char *dst, int len)
43{
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;
52
53 for (j = strlen(dst); j > 0; j--)
54 if (dst[j] == '/') {
55 dst[++j] = 0;
56 break;
57 }
58
69d41dfb 59 return j;
698517be 60}
61
62#ifdef __GP2X__
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
66#endif
67
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;
76 /* approximate /= 1000 */
77 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
78
79 return ret;
80}
81
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
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
a2ad8cc5 137void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
698517be 138{
9ee0fd5b 139 static int hugetlb_disabled;
140 int prot = PROT_READ | PROT_WRITE;
141 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
698517be 142 void *req, *ret;
143
144 req = (void *)addr;
9ee0fd5b 145 if (need_exec)
146 prot |= PROT_EXEC;
a2ad8cc5 147 if (is_fixed)
148 flags |= MAP_FIXED;
9ee0fd5b 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 }
698517be 161 if (ret == MAP_FAILED)
162 return NULL;
9ee0fd5b 163
164 if (req != NULL && ret != req)
165 fprintf(stderr,
166 "warning: mmaped to %p, requested %p\n", ret, req);
698517be 167
168 return ret;
169}
170
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
184void plat_munmap(void *ptr, size_t size)
185{
9ee0fd5b 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 }
698517be 198}
199
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