Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/MemArenaAndroid.cpp
3185 views
1
// Copyright (C) 2003 Dolphin Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#include "ppsspp_config.h"
19
20
#ifdef __ANDROID__
21
22
#include <sys/stat.h>
23
#include <fcntl.h>
24
#include <unistd.h>
25
#include <cerrno>
26
#include <sys/ioctl.h>
27
#include <linux/ashmem.h>
28
#include <dlfcn.h>
29
30
#include "Common/Log.h"
31
#include "Common/MemoryUtil.h"
32
#include "Common/MemArena.h"
33
#include "Common/StringUtils.h"
34
#include "Common/System/System.h"
35
36
// Hopefully this ABI will never change...
37
38
#define ASHMEM_DEVICE "/dev/ashmem"
39
40
bool MemArena::NeedsProbing() {
41
return false;
42
}
43
44
// ashmem_create_region - creates a new ashmem region and returns the file
45
// descriptor, or <0 on error
46
// This function is defined in much later version of the ndk, so we can only access it via dlopen().
47
// `name' is an optional label to give the region (visible in /proc/pid/maps)
48
// `size' is the size of the region, in page-aligned bytes
49
static int ashmem_create_region(const char *name, size_t size) {
50
static void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
51
using type_ASharedMemory_create = int(*)(const char *name, size_t size);
52
static type_ASharedMemory_create function_create = nullptr;
53
54
if (handle != nullptr) {
55
function_create =
56
reinterpret_cast<type_ASharedMemory_create>(dlsym(handle, "ASharedMemory_create"));
57
}
58
59
if (function_create != nullptr) {
60
return function_create(name, size);
61
} else {
62
return -1;
63
}
64
}
65
66
// legacy_ashmem_create_region - creates a new ashmem region and returns the file
67
// descriptor, or <0 on error
68
// `name' is an optional label to give the region (visible in /proc/pid/maps)
69
// `size' is the size of the region, in page-aligned bytes
70
static int legacy_ashmem_create_region(const char *name, size_t size) {
71
int fd = open(ASHMEM_DEVICE, O_RDWR);
72
if (fd < 0)
73
return fd;
74
75
int ret;
76
if (name) {
77
char buf[ASHMEM_NAME_LEN];
78
truncate_cpy(buf, name);
79
ret = ioctl(fd, ASHMEM_SET_NAME, buf);
80
if (ret < 0)
81
goto error;
82
}
83
84
ret = ioctl(fd, ASHMEM_SET_SIZE, size);
85
if (ret < 0)
86
goto error;
87
88
return fd;
89
90
error:
91
ERROR_LOG(Log::MemMap, "NASTY ASHMEM ERROR: ret = %08x", ret);
92
close(fd);
93
return ret;
94
}
95
96
// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
97
size_t MemArena::roundup(size_t x) {
98
return x;
99
}
100
101
bool MemArena::GrabMemSpace(size_t size) {
102
// Use ashmem so we don't have to allocate a file on disk!
103
const char* name = "PPSSPP_RAM";
104
105
// Since version 26 Android provides a new api for accessing SharedMemory.
106
if (System_GetPropertyInt(SYSPROP_SYSTEMVERSION) >= 26) {
107
fd = ashmem_create_region(name, size);
108
} else {
109
fd = legacy_ashmem_create_region(name, size);
110
}
111
// Note that it appears that ashmem is pinned by default, so no need to pin.
112
if (fd < 0) {
113
ERROR_LOG(Log::MemMap, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno));
114
return false;
115
}
116
return true;
117
}
118
119
void MemArena::ReleaseSpace() {
120
if (fd >= 0) {
121
close(fd);
122
}
123
fd = -1;
124
}
125
126
void *MemArena::CreateView(s64 offset, size_t size, void *base) {
127
void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | ((base == 0) ? 0 : MAP_FIXED), fd, offset);
128
if (retval == MAP_FAILED) {
129
NOTICE_LOG(Log::MemMap, "mmap on ashmem (fd: %d) failed", (int)fd);
130
return nullptr;
131
}
132
return retval;
133
}
134
135
void MemArena::ReleaseView(s64 offset, void* view, size_t size) {
136
munmap(view, size);
137
}
138
139
u8* MemArena::Find4GBBase() {
140
#if PPSSPP_ARCH(64BIT)
141
// We should probably just go look in /proc/self/maps for some free space.
142
// But let's try the anonymous mmap trick, just like on 32-bit, but bigger and
143
// aligned to 4GB for the movk trick. We can ensure that we get an aligned 4GB
144
// address by grabbing 8GB and aligning the pointer.
145
const uint64_t EIGHT_GIGS = 0x200000000ULL;
146
void *base = mmap(0, EIGHT_GIGS, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
147
if (base && base != MAP_FAILED) {
148
INFO_LOG(Log::System, "base: %p", base);
149
uint64_t aligned_base = ((uint64_t)base + 0xFFFFFFFF) & ~0xFFFFFFFFULL;
150
INFO_LOG(Log::System, "aligned_base: %p", (void *)aligned_base);
151
munmap(base, EIGHT_GIGS);
152
return reinterpret_cast<u8 *>(aligned_base);
153
} else {
154
u8 *hardcoded_ptr = reinterpret_cast<u8*>(0x2300000000ULL);
155
INFO_LOG(Log::System, "Failed to anonymously map 8GB. Fall back to the hardcoded pointer %p.", hardcoded_ptr);
156
// Just grab some random 4GB...
157
// This has been known to fail lately though, see issue #12249.
158
return hardcoded_ptr;
159
}
160
#else
161
// Address masking is used in 32-bit mode, so we can get away with less memory.
162
void *base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
163
164
if (base == MAP_FAILED) {
165
ERROR_LOG(Log::System, "Failed to map 256 MB of memory space: %s", strerror(errno));
166
return nullptr;
167
}
168
169
munmap(base, 0x10000000);
170
return static_cast<u8*>(base);
171
#endif
172
}
173
174
#endif // __ANDROID__
175
176