forked from tonioni/WinUAE
/
vm.cpp
410 lines (384 loc) · 10.4 KB
/
vm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Multi-platform virtual memory functions for UAE.
* Copyright (C) 2015 Frode Solheim
*
* Licensed under the terms of the GNU General Public License version 2.
* See the file 'COPYING' for full license text.
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "uae/vm.h"
#include "uae/log.h"
#include "memory.h"
#ifdef _WIN32
#else
#include <sys/mman.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if defined(__APPLE__)
#include <sys/sysctl.h>
#endif
#ifdef LINUX
#define HAVE_MAP_32BIT 1
#endif
/* Commiting memory on Windows zeroes the region. We do the same on other
* platforms so the functions exhibits the same behavior. I.e. a decommit
* followed by a commit results in zeroed memory. */
#define CLEAR_MEMORY_ON_COMMIT
// #define TRACK_ALLOCATIONS
#ifdef TRACK_ALLOCATIONS
struct alloc_size {
void *address;
uae_u32 size;
};
#define MAX_ALLOCATIONS 2048
/* A bit inefficient, but good enough for few and rare allocs. Storing
* the size at the start of the allocated memory would be better, but this
* could be awkward if/when you want to allocate page-aligned memory. */
static struct alloc_size alloc_sizes[MAX_ALLOCATIONS];
static void add_allocation(void *address, uae_u32 size)
{
uae_log("VM: add_allocation %p (%d)\n", address, size);
for (int i = 0; i < MAX_ALLOCATIONS; i++) {
if (alloc_sizes[i].address == NULL) {
alloc_sizes[i].address = address;
alloc_sizes[i].size = size;
return;
}
}
abort();
}
static uae_u32 find_allocation(void *address)
{
for (int i = 0; i < MAX_ALLOCATIONS; i++) {
if (alloc_sizes[i].address == address) {
return alloc_sizes[i].size;
}
}
abort();
}
static uae_u32 remove_allocation(void *address)
{
for (int i = 0; i < MAX_ALLOCATIONS; i++) {
if (alloc_sizes[i].address == address) {
alloc_sizes[i].address = NULL;
uae_u32 size = alloc_sizes[i].size;
alloc_sizes[i].size = 0;
return size;
}
}
abort();
}
#endif /* TRACK_ALLOCATIONS */
static int protect_to_native(int protect)
{
#ifdef _WIN32
if (protect == UAE_VM_NO_ACCESS) return PAGE_NOACCESS;
if (protect == UAE_VM_READ) return PAGE_READONLY;
if (protect == UAE_VM_READ_WRITE) return PAGE_READWRITE;
if (protect == UAE_VM_READ_EXECUTE) return PAGE_EXECUTE_READ;
if (protect == UAE_VM_READ_WRITE_EXECUTE) return PAGE_EXECUTE_READWRITE;
uae_log("VM: Invalid protect value %d\n", protect);
return PAGE_NOACCESS;
#else
if (protect == UAE_VM_NO_ACCESS) return PROT_NONE;
if (protect == UAE_VM_READ) return PROT_READ;
if (protect == UAE_VM_READ_WRITE) return PROT_READ | PROT_WRITE;
if (protect == UAE_VM_READ_EXECUTE) return PROT_READ | PROT_EXEC;
if (protect == UAE_VM_READ_WRITE_EXECUTE) {
return PROT_READ | PROT_WRITE | PROT_EXEC;
}
uae_log("VM: Invalid protect value %d\n", protect);
return PROT_NONE;
#endif
}
static const char *protect_description(int protect)
{
if (protect == UAE_VM_NO_ACCESS) return "NO_ACCESS";
if (protect == UAE_VM_READ) return "READ";
if (protect == UAE_VM_READ_WRITE) return "READ_WRITE";
if (protect == UAE_VM_READ_EXECUTE) return "READ_EXECUTE";
if (protect == UAE_VM_READ_WRITE_EXECUTE) return "READ_WRITE_EXECUTE";
return "UNKNOWN";
}
int uae_vm_page_size(void)
{
static int page_size = 0;
if (page_size == 0) {
#ifdef _WIN32
SYSTEM_INFO si;
GetSystemInfo(&si);
page_size = si.dwPageSize;
#else
page_size = sysconf(_SC_PAGESIZE);
#endif
}
return page_size;
}
static void *uae_vm_alloc_with_flags(uae_u32 size, int flags, int protect)
{
void *address = NULL;
uae_log("VM: Allocate 0x%-8x bytes [%d] (%s)\n",
size, flags, protect_description(protect));
#ifdef _WIN32
int va_type = MEM_COMMIT | MEM_RESERVE;
if (flags & UAE_VM_WRITE_WATCH) {
va_type |= MEM_WRITE_WATCH;
}
int va_protect = protect_to_native(protect);
#ifdef CPU_64_BIT
if (flags & UAE_VM_32BIT) {
/* Stupid algorithm to find available space, but should
* work well enough when there is not a lot of allocations. */
uae_u8 *p = (uae_u8 *) 0x50000000;
while (address == NULL) {
if (p >= (void*) 0x60000000) {
break;
}
address = VirtualAlloc(p, size, va_type, va_protect);
p += uae_vm_page_size();
}
}
#endif
if (!address) {
address = VirtualAlloc(NULL, size, va_type, va_protect);
}
#else
//size = size < uae_vm_page_size() ? uae_vm_page_size() : size;
int mmap_flags = MAP_PRIVATE | MAP_ANON;
int mmap_prot = protect_to_native(protect);
#ifdef CPU_64_BIT
if (flags & UAE_VM_32BIT) {
#ifdef HAVE_MAP_32BIT
mmap_flags |= MAP_32BIT;
#else
/* Stupid algorithm to find available space, but should
* work well enough when there is not a lot of allocations. */
uae_u8 *p = natmem_offset - 0x10000000;
uae_u8 *p_end = p + 0x10000000;
while (address == NULL) {
if (p >= p_end) {
break;
}
printf("%p\n", p);
address = mmap(p, size, mmap_prot, mmap_flags, -1, 0);
/* FIXME: check 32-bit result */
if (address == MAP_FAILED) {
address = NULL;
}
p += uae_vm_page_size();
}
#endif
}
#endif
if (address == NULL) {
address = mmap(0, size, mmap_prot, mmap_flags, -1, 0);
if (address == MAP_FAILED) {
address = NULL;
}
}
#endif
if (address == NULL) {
uae_log("VM: uae_vm_alloc(%u, %d, %d) mmap failed (%d)\n",
size, flags, protect, errno);
return NULL;
}
#ifdef TRACK_ALLOCATIONS
add_allocation(address, size);
#endif
uae_log("VM: %p\n", address);
return address;
}
void *uae_vm_alloc(uae_u32 size, int flags, int protect)
{
return uae_vm_alloc_with_flags(size, flags, protect);
}
static bool do_protect(void *address, int size, int protect)
{
#ifdef TRACK_ALLOCATIONS
uae_u32 allocated_size = find_allocation(address);
assert(allocated_size == size);
#endif
#ifdef _WIN32
DWORD old;
if (VirtualProtect(address, size, protect_to_native(protect), &old) == 0) {
uae_log("VM: uae_vm_protect(%p, %d, %d) VirtualProtect failed (%d)\n",
address, size, protect, GetLastError());
return false;
}
#else
if (mprotect(address, size, protect_to_native(protect)) != 0) {
uae_log("VM: uae_vm_protect(%p, %d, %d) mprotect failed (%d)\n",
address, size, protect, errno);
return false;
}
#endif
return true;
}
bool uae_vm_protect(void *address, int size, int protect)
{
return do_protect(address, size, protect);
}
static bool do_free(void *address, int size)
{
#ifdef TRACK_ALLOCATIONS
uae_u32 allocated_size = remove_allocation(address);
assert(allocated_size == size);
#endif
#ifdef _WIN32
return VirtualFree(address, 0, MEM_RELEASE) != 0;
#else
if (munmap(address, size) != 0) {
uae_log("VM: uae_vm_free(%p, %d) munmap failed (%d)\n",
address, size, errno);
return false;
}
#endif
return true;
}
bool uae_vm_free(void *address, int size)
{
uae_log("VM: Free 0x%-8x bytes at %p\n", size, address);
return do_free(address, size);
}
static void *try_reserve(uintptr_t try_addr, uae_u32 size, int flags)
{
void *address = NULL;
if (try_addr) {
uae_log("VM: Reserve 0x%-8x bytes, try address 0x%llx\n",
size, (uae_u64) try_addr);
} else {
uae_log("VM: Reserve 0x%-8x bytes\n", size);
}
#ifdef _WIN32
int va_type = MEM_RESERVE;
if (flags & UAE_VM_WRITE_WATCH) {
va_type |= MEM_WRITE_WATCH;
}
int va_protect = protect_to_native(UAE_VM_NO_ACCESS);
address = VirtualAlloc((void *) try_addr, size, va_type, va_protect);
if (address == NULL) {
return NULL;
}
#else
int mmap_flags = MAP_PRIVATE | MAP_ANON;
address = mmap((void *) try_addr, size, PROT_NONE, mmap_flags, -1, 0);
if (address == MAP_FAILED) {
return NULL;
}
#endif
#ifdef CPU_64_BIT
if (flags & UAE_VM_32BIT) {
uintptr_t end = (uintptr_t) address + size;
if (address && end > (uintptr_t) 0x100000000ULL) {
uae_log("VM: Reserve 0x%-8x bytes, got address 0x%llx (> 32-bit)\n",
size, (uae_u64) (uintptr_t) address);
#ifdef _WIN32
VirtualFree(address, 0, MEM_RELEASE);
#else
munmap(address, size);
#endif
return NULL;
}
}
#endif
return address;
}
void *uae_vm_reserve(uae_u32 size, int flags)
{
void *address = NULL;
#ifdef _WIN32
address = try_reserve(0x80000000, size, flags);
if (address == NULL && (flags & UAE_VM_32BIT)) {
if (size <= 768 * 1024 * 1024) {
address = try_reserve(0x78000000 - size, size, flags);
}
}
if (address == NULL && (flags & UAE_VM_32BIT) == 0) {
address = try_reserve(0, size, flags);
}
#else
#ifdef CPU_64_BIT
if (flags & UAE_VM_32BIT) {
#else
if (true) {
#endif
uintptr_t try_addr = 0x50000000;
while (address == NULL) {
address = try_reserve(try_addr, size, flags);
if (address == NULL) {
try_addr -= 0x4000000;
if (try_addr < 0x20000000) {
break;
}
continue;
}
}
}
if (address == NULL && (flags & UAE_VM_32BIT) == 0) {
address = try_reserve(0, size, flags);
}
#endif
if (address) {
uae_log("VM: Reserve 0x%-8x bytes, got address 0x%llx\n",
size, (uae_u64) (uintptr_t) address);
} else {
uae_log("VM: Reserve 0x%-8x bytes failed!\n", size);
}
return address;
}
void *uae_vm_reserve_fixed(void *want_addr, uae_u32 size, int flags)
{
void *address = NULL;
uae_log("VM: Reserve 0x%-8x bytes at %p (fixed)\n", size, want_addr);
address = try_reserve((uintptr_t) want_addr, size, flags);
if (address == NULL) {
uae_log("VM: Reserve 0x%-8x bytes at %p failed!\n", size, want_addr);
return NULL;
}
if (address != want_addr) {
do_free(address, size);
return NULL;
}
uae_log("VM: Reserve 0x%-8x bytes, got address 0x%llx\n",
size, (uae_u64) (uintptr_t) address);
return address;
}
void *uae_vm_commit(void *address, uae_u32 size, int protect)
{
uae_log("VM: Commit 0x%-8x bytes at %p (%s)\n",
size, address, protect_description(protect));
#ifdef _WIN32
int va_type = MEM_COMMIT ;
int va_protect = protect_to_native(protect);
address = VirtualAlloc(address, size, va_type, va_protect);
#else
#ifdef CLEAR_MEMORY_ON_COMMIT
do_protect(address, size, UAE_VM_READ_WRITE);
memset(address, 0, size);
#endif
do_protect(address, size, protect);
#endif
return address;
}
bool uae_vm_decommit(void *address, uae_u32 size)
{
uae_log("VM: Decommit 0x%-8x bytes at %p\n", size, address);
#ifdef _WIN32
return VirtualFree (address, size, MEM_DECOMMIT) != 0;
#else
#if 0
/* FIXME: perhaps we can unmmap and mmap the memory region again to
* allow the operating system to throw away the (unused) pages. Of
* course, we have problem if re-mmaping it fails. If we do this,
* we may be able to remove the memory clear operation in
* uae_vm_commit. We might also be able to use mmap with MAP_FIXED
* to more "safely" overwrite the old mapping. */
munmap(address, size);
mmap(...);
#endif
return do_protect(address, size, UAE_VM_NO_ACCESS);
#endif
}