CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/MemArenaAndroid.cpp
Views: 1401
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
close(fd);
121
}
122
123
void *MemArena::CreateView(s64 offset, size_t size, void *base) {
124
void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | ((base == 0) ? 0 : MAP_FIXED), fd, offset);
125
if (retval == MAP_FAILED) {
126
NOTICE_LOG(Log::MemMap, "mmap on ashmem (fd: %d) failed", (int)fd);
127
return nullptr;
128
}
129
return retval;
130
}
131
132
void MemArena::ReleaseView(s64 offset, void* view, size_t size) {
133
munmap(view, size);
134
}
135
136
u8* MemArena::Find4GBBase() {
137
#if PPSSPP_ARCH(64BIT)
138
// We should probably just go look in /proc/self/maps for some free space.
139
// But let's try the anonymous mmap trick, just like on 32-bit, but bigger and
140
// aligned to 4GB for the movk trick. We can ensure that we get an aligned 4GB
141
// address by grabbing 8GB and aligning the pointer.
142
const uint64_t EIGHT_GIGS = 0x200000000ULL;
143
void *base = mmap(0, EIGHT_GIGS, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
144
if (base && base != MAP_FAILED) {
145
INFO_LOG(Log::System, "base: %p", base);
146
uint64_t aligned_base = ((uint64_t)base + 0xFFFFFFFF) & ~0xFFFFFFFFULL;
147
INFO_LOG(Log::System, "aligned_base: %p", (void *)aligned_base);
148
munmap(base, EIGHT_GIGS);
149
return reinterpret_cast<u8 *>(aligned_base);
150
} else {
151
u8 *hardcoded_ptr = reinterpret_cast<u8*>(0x2300000000ULL);
152
INFO_LOG(Log::System, "Failed to anonymously map 8GB. Fall back to the hardcoded pointer %p.", hardcoded_ptr);
153
// Just grab some random 4GB...
154
// This has been known to fail lately though, see issue #12249.
155
return hardcoded_ptr;
156
}
157
#else
158
// Address masking is used in 32-bit mode, so we can get away with less memory.
159
void *base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
160
161
if (base == MAP_FAILED) {
162
ERROR_LOG(Log::System, "Failed to map 256 MB of memory space: %s", strerror(errno));
163
return nullptr;
164
}
165
166
munmap(base, 0x10000000);
167
return static_cast<u8*>(base);
168
#endif
169
}
170
171
#endif // __ANDROID__
172
173