summaryrefslogtreecommitdiffstats
path: root/src/common/mem_arena.cpp
blob: b76ac92d34748f06afe3aa0180f389e2e491f38a (plain) (blame)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include <string>

#include "common/memory_util.h"
#include "common/mem_arena.h"
#include "common/string_util.h"

#ifndef _WIN32
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#ifdef ANDROID
#include <sys/ioctl.h>
#include <linux/ashmem.h>
#endif
#endif

#ifdef IOS
void* globalbase = NULL;
#endif

#ifdef ANDROID

// Hopefully this ABI will never change...


#define ASHMEM_DEVICE	"/dev/ashmem"

/*
* ashmem_create_region - creates a new ashmem region and returns the file
* descriptor, or <0 on error
*
* `name' is an optional label to give the region (visible in /proc/pid/maps)
* `size' is the size of the region, in page-aligned bytes
*/
int ashmem_create_region(const char *name, size_t size)
{
    int fd, ret;

    fd = open(ASHMEM_DEVICE, O_RDWR);
    if (fd < 0)
        return fd;

    if (name) {
        char buf[ASHMEM_NAME_LEN];

        strncpy(buf, name, sizeof(buf));
        ret = ioctl(fd, ASHMEM_SET_NAME, buf);
        if (ret < 0)
            goto error;
    }

    ret = ioctl(fd, ASHMEM_SET_SIZE, size);
    if (ret < 0)
        goto error;

    return fd;

error:
    ERROR_LOG(MEMMAP, "NASTY ASHMEM ERROR: ret = %08x", ret);
    close(fd);
    return ret;
}

int ashmem_set_prot_region(int fd, int prot)
{
    return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
}

int ashmem_pin_region(int fd, size_t offset, size_t len)
{
    struct ashmem_pin pin = { offset, len };
    return ioctl(fd, ASHMEM_PIN, &pin);
}

int ashmem_unpin_region(int fd, size_t offset, size_t len)
{
    struct ashmem_pin pin = { offset, len };
    return ioctl(fd, ASHMEM_UNPIN, &pin);
}
#endif  // Android


#if defined(_WIN32) && !defined(_XBOX)
SYSTEM_INFO sysInfo;
#endif


// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
#ifdef _WIN32
size_t roundup(size_t x) {
#ifndef _XBOX
    int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000;
#else
    int gran = 0x10000; // 64k in 360
#endif
    return (x + gran - 1) & ~(gran - 1);
}
#else
size_t roundup(size_t x) {
    return x;
}
#endif


void MemArena::GrabLowMemSpace(size_t size)
{
#ifdef _WIN32
#ifndef _XBOX
    hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL);
    GetSystemInfo(&sysInfo);
#endif
#elif defined(ANDROID)
    // Use ashmem so we don't have to allocate a file on disk!
    fd = ashmem_create_region("PPSSPP_RAM", size);
    // Note that it appears that ashmem is pinned by default, so no need to pin.
    if (fd < 0)
    {
        ERROR_LOG(MEMMAP, "Failed to grab ashmem space of size: %08x  errno: %d", (int)size, (int)(errno));
        return;
    }
#else
    // Try to find a non-existing filename for our shared memory.
    // In most cases the first one will be available, but it's nicer to search
    // a bit more.
    for (int i = 0; i < 10000; i++)
    {
        std::string file_name = StringFromFormat("/citramem.%d", i);
        fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
        if (fd != -1)
        {
            shm_unlink(file_name.c_str());
            break;
        }
        else if (errno != EEXIST)
        {
            ERROR_LOG(MEMMAP, "shm_open failed: %s", strerror(errno));
            return;
        }
    }
    if (ftruncate(fd, size) < 0)
        ERROR_LOG(MEMMAP, "Failed to allocate low memory space");
#endif
}


void MemArena::ReleaseSpace()
{
#ifdef _WIN32
    CloseHandle(hMemoryMapping);
    hMemoryMapping = 0;
#elif defined(__SYMBIAN32__)
    memmap->Close();
    delete memmap;
#else
    close(fd);
#endif
}


void *MemArena::CreateView(s64 offset, size_t size, void *base)
{
#ifdef _WIN32
#ifdef _XBOX
    size = roundup(size);
    // use 64kb pages
    void * ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
    return ptr;
#else
    size = roundup(size);
    void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
    return ptr;
#endif
#else
    void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED |
        // Do not sync memory to underlying file. Linux has this by default.
#ifdef BLACKBERRY
        MAP_NOSYNCFILE |
#elif defined(__FreeBSD__)
        MAP_NOSYNC |
#endif
        ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);

    if (retval == MAP_FAILED)
    {
        NOTICE_LOG(MEMMAP, "mmap failed");
        return nullptr;
    }
    return retval;
#endif
}


void MemArena::ReleaseView(void* view, size_t size)
{
#ifdef _WIN32
#ifndef _XBOX
    UnmapViewOfFile(view);
#endif
#elif defined(__SYMBIAN32__)
    memmap->Decommit(((int)view - (int)memmap->Base()) & 0x3FFFFFFF, size);
#else
    munmap(view, size);
#endif
}

#ifndef __SYMBIAN32__
u8* MemArena::Find4GBBase()
{
#ifdef _M_X64
#ifdef _WIN32
    // 64 bit
    u8* base = (u8*)VirtualAlloc(0, 0xE1000000, MEM_RESERVE, PAGE_READWRITE);
    VirtualFree(base, 0, MEM_RELEASE);
    return base;
#else
    // Very precarious - mmap cannot return an error when trying to map already used pages.
    // This makes the Windows approach above unusable on Linux, so we will simply pray...
    return reinterpret_cast<u8*>(0x2300000000ULL);
#endif

#else // 32 bit

#ifdef _WIN32
    u8* base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE, PAGE_READWRITE);
    if (base) {
        VirtualFree(base, 0, MEM_RELEASE);
    }
    return base;
#else
#ifdef IOS
    void* base = NULL;
    if (globalbase == NULL){
        base = mmap(0, 0x08000000, PROT_READ | PROT_WRITE,
            MAP_ANON | MAP_SHARED, -1, 0);
        if (base == MAP_FAILED) {
            PanicAlert("Failed to map 128 MB of memory space: %s", strerror(errno));
            return 0;
        }
        munmap(base, 0x08000000);
        globalbase = base;
    }
    else{ base = globalbase; }
#else
    void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
        MAP_ANON | MAP_SHARED, -1, 0);
    if (base == MAP_FAILED) {
        PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
        return 0;
    }
    munmap(base, 0x10000000);
#endif
    return static_cast<u8*>(base);
#endif
#endif
}
#endif


// yeah, this could also be done in like two bitwise ops...
#define SKIP(a_flags, b_flags) 
//	if (!(a_flags & MV_WII_ONLY) && (b_flags & MV_WII_ONLY)) 
//		continue; 
//	if (!(a_flags & MV_FAKE_VMEM) && (b_flags & MV_FAKE_VMEM)) 
//		continue; 

static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32 flags, MemArena *arena) {
    // OK, we know where to find free space. Now grab it!
    // We just mimic the popular BAT setup.
    size_t position = 0;
    size_t last_position = 0;

#if defined(_XBOX)
    void *ptr;
#endif

    // Zero all the pointers to be sure.
    for (int i = 0; i < num_views; i++)
    {
        if (views[i].out_ptr_low)
            *views[i].out_ptr_low = 0;
        if (views[i].out_ptr)
            *views[i].out_ptr = 0;
    }

    int i;
    for (i = 0; i < num_views; i++)
    {
        const MemoryView &view = views[i];
        if (view.size == 0)
            continue;
        SKIP(flags, view.flags);
        if (view.flags & MV_MIRROR_PREVIOUS) {
            position = last_position;
        }
        else {
#ifdef __SYMBIAN32__
            *(view.out_ptr_low) = (u8*)((int)arena->memmap->Base() + view.virtual_address);
            arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
        }
        *(view.out_ptr) = (u8*)((int)arena->memmap->Base() + view.virtual_address & 0x3FFFFFFF);
#elif defined(_XBOX)
            *(view.out_ptr_low) = (u8*)(base + view.virtual_address);
            //arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
            ptr = VirtualAlloc(base + (view.virtual_address & 0x3FFFFFFF), view.size, MEM_COMMIT, PAGE_READWRITE);
        }
        *(view.out_ptr) = (u8*)base + (view.virtual_address & 0x3FFFFFFF);
#else
            *(view.out_ptr_low) = (u8*)arena->CreateView(position, view.size);
            if (!*view.out_ptr_low)
                goto bail;
        }
#ifdef _M_X64
        *view.out_ptr = (u8*)arena->CreateView(
            position, view.size, base + view.virtual_address);
#else
        if (view.flags & MV_MIRROR_PREVIOUS) {  // TODO: should check if the two & 0x3FFFFFFF are identical.
            // No need to create multiple identical views.
            *view.out_ptr = *views[i - 1].out_ptr;
        }
        else {
            *view.out_ptr = (u8*)arena->CreateView(
                position, view.size, base + (view.virtual_address & 0x3FFFFFFF));
            if (!*view.out_ptr)
                goto bail;
        }
#endif

#endif
        last_position = position;
        position += roundup(view.size);
    }

    return true;

bail:
    // Argh! ERROR! Free what we grabbed so far so we can try again.
    for (int j = 0; j <= i; j++)
    {
        if (views[i].size == 0)
            continue;
        SKIP(flags, views[i].flags);
        if (views[j].out_ptr_low && *views[j].out_ptr_low)
        {
            arena->ReleaseView(*views[j].out_ptr_low, views[j].size);
            *views[j].out_ptr_low = NULL;
        }
        if (*views[j].out_ptr)
        {
#ifdef _M_X64
            arena->ReleaseView(*views[j].out_ptr, views[j].size);
#else
            if (!(views[j].flags & MV_MIRROR_PREVIOUS))
            {
                arena->ReleaseView(*views[j].out_ptr, views[j].size);
            }
#endif
            *views[j].out_ptr = NULL;
        }
    }
    return false;
}

u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena *arena)
{
    size_t total_mem = 0;
    int base_attempts = 0;

    for (int i = 0; i < num_views; i++)
    {
        if (views[i].size == 0)
            continue;
        SKIP(flags, views[i].flags);
        if ((views[i].flags & MV_MIRROR_PREVIOUS) == 0)
            total_mem += roundup(views[i].size);
    }
    // Grab some pagefile backed memory out of the void ...
#ifndef __SYMBIAN32__
    arena->GrabLowMemSpace(total_mem);
#endif

    // Now, create views in high memory where there's plenty of space.
#ifdef _M_X64
    u8 *base = MemArena::Find4GBBase();
    // This really shouldn't fail - in 64-bit, there will always be enough
    // address space.
    if (!Memory_TryBase(base, views, num_views, flags, arena))
    {
        PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
        return 0;
    }
#elif defined(_XBOX)
    // Reserve 256MB
    u8 *base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
    if (!Memory_TryBase(base, views, num_views, flags, arena))
    {
        PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
        exit(0);
        return 0;
    }
#elif defined(_WIN32)
    // Try a whole range of possible bases. Return once we got a valid one.
    u32 max_base_addr = 0x7FFF0000 - 0x10000000;
    u8 *base = NULL;

    for (u32 base_addr = 0x01000000; base_addr < max_base_addr; base_addr += 0x400000)
    {
        base_attempts++;
        base = (u8 *)base_addr;
        if (Memory_TryBase(base, views, num_views, flags, arena))
        {
            INFO_LOG(MEMMAP, "Found valid memory base at %p after %i tries.", base, base_attempts);
            base_attempts = 0;
            break;
        }
    }
#elif defined(__SYMBIAN32__)
    arena->memmap = new RChunk();
    arena->memmap->CreateDisconnectedLocal(0, 0, 0x10000000);
    if (!Memory_TryBase(arena->memmap->Base(), views, num_views, flags, arena))
    {
        PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
        return 0;
    }
    u8* base = arena->memmap->Base();
#else
    // Linux32 is fine with the x64 method, although limited to 32-bit with no automirrors.
    u8 *base = MemArena::Find4GBBase();
    if (!Memory_TryBase(base, views, num_views, flags, arena))
    {
        ERROR_LOG(MEMMAP, "MemoryMap_Setup: Failed finding a memory base.");
        PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
        return 0;
    }
#endif
    if (base_attempts)
        PanicAlert("No possible memory base pointer found!");
    return base;
}

void MemoryMap_Shutdown(const MemoryView *views, int num_views, u32 flags, MemArena *arena)
{
    for (int i = 0; i < num_views; i++)
    {
        if (views[i].size == 0)
            continue;
        SKIP(flags, views[i].flags);
        if (views[i].out_ptr_low && *views[i].out_ptr_low)
            arena->ReleaseView(*views[i].out_ptr_low, views[i].size);
        if (*views[i].out_ptr && (views[i].out_ptr_low && *views[i].out_ptr != *views[i].out_ptr_low))
            arena->ReleaseView(*views[i].out_ptr, views[i].size);
        *views[i].out_ptr = NULL;
        if (views[i].out_ptr_low)
            *views[i].out_ptr_low = NULL;
    }
}