Skip to content

Commit

Permalink
libretro, add libmappedmemory option for drc
Browse files Browse the repository at this point in the history
this platform requires *freeing* of the drc memory, so add a hook for that, inside an ifdef so we don't have to touch every platform
  • Loading branch information
ashquarky authored and irixxxx committed Jun 30, 2024
1 parent c4c8468 commit 89d07d4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Makefile.libretro
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ else ifeq ($(platform), wiiu)
CFLAGS += -DGEKKO -DWIIU -DHW_RVL -DHW_WUP -D__wiiu__ -ffunction-sections -fdata-sections -mcpu=750 -meabi -mhard-float
STATIC_LINKING = 1
STATIC_LINKING_LINK = 1
OBJS += platform/libretro/wiiu/libmemorymapping.o
CFLAGS += -isystem $(DEVKITPRO)/wut/include

# Nintendo Switch (libtransistor)
else ifeq ($(platform), switch)
Expand Down
3 changes: 3 additions & 0 deletions cpu/drc/cmn.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ void drc_cmn_init(void)

void drc_cmn_cleanup(void)
{
#ifdef HW_WUP
plat_mem_free_for_drc(tcache);
#endif
}

// vim:shiftwidth=2:expandtab
1 change: 1 addition & 0 deletions pico/pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern void plat_munmap(void *ptr, size_t size);

// memory for the dynarec; plat_mem_get_for_drc() can just return NULL
extern void *plat_mem_get_for_drc(size_t size);
extern void plat_mem_free_for_drc(void *mem);
extern int plat_mem_set_exec(void *ptr, size_t size);

// this one should handle display mode changes
Expand Down
21 changes: 18 additions & 3 deletions platform/libretro/libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ void linearFree(void* mem);

static int ctr_svchack_successful = 0;

#elif defined(HW_WUP)
#include "wiiu/libmemorymapping.h"

#elif defined(VITA)
#define TARGET_SIZE_2 24 // 2^24 = 16 megabytes

Expand Down Expand Up @@ -486,16 +489,28 @@ void *plat_mem_get_for_drc(size_t size)
#if defined VITA
sceKernelGetMemBlockBase(sceBlock, &mem);
#elif defined HW_WUP
// For WiiU, a slice of RWX memory left from the exploit is used, see:
// https://github.com/embercold/pcsx_rearmed/commit/af0453223
mem = (void *)(0x01000000 - size);
// First try Aroma libmappedmemory...
mem = wiiu_alloc_mappedmemory(size, 4096);
if (!mem)
// Then fall back to RWX memory left from the exploit.
// https://github.com/embercold/pcsx_rearmed/commit/af0453223
// May want to remove this once Aroma libretro is common.
mem = (void *)(0x01000000 - size);
#elif defined __PS3__
ps3mapi_process_page_allocate(sysProcessGetPid(), size, PAGE_SIZE_AUTO, 0x2F, 1, page_table);
mem = (void *)page_table[0];
#endif
return mem;
}

#ifdef HW_WUP
void plat_mem_free_for_drc(void *mem)
{
wiiu_free_mappedmemory(mem);
wiiu_deinit_mappedmemory();
}
#endif

int plat_mem_set_exec(void *ptr, size_t size)
{
int ret = -1;
Expand Down
50 changes: 50 additions & 0 deletions platform/libretro/wiiu/libmemorymapping.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "libmemorymapping.h"

#include <coreinit/dynload.h>
#include <stdbool.h>
#include <stdint.h>

// Aroma libmappedmemory - Maschell promises he won't break ABI here
static void* (**MEMAllocFromMappedMemoryEx)(uint32_t size, int32_t alignment);
static void* (**MEMFreeToMappedMemory)(void *ptr);
static OSDynLoad_Module libmappedmemory;

static bool wiiu_init_libmappedmemory() {
OSDynLoad_Error err;

// already ran?
if (MEMAllocFromMappedMemoryEx)
return true;

err = OSDynLoad_Acquire("homebrew_memorymapping", &libmappedmemory);
if (err != OS_DYNLOAD_OK)
return false;

err = OSDynLoad_FindExport(libmappedmemory, OS_DYNLOAD_EXPORT_DATA, "MEMAllocFromMappedMemoryEx", (void**)&MEMAllocFromMappedMemoryEx);
if (err != OS_DYNLOAD_OK)
return false;

err = OSDynLoad_FindExport(libmappedmemory, OS_DYNLOAD_EXPORT_DATA, "MEMFreeToMappedMemory", (void**)&MEMFreeToMappedMemory);
if (err != OS_DYNLOAD_OK)
return false;

return true;
}

void *wiiu_alloc_mappedmemory(uint32_t size, int32_t alignment) {
if (!wiiu_init_libmappedmemory()) return NULL;

return (*MEMAllocFromMappedMemoryEx)(size, alignment);
}

void wiiu_free_mappedmemory(void *mem) {
if (!wiiu_init_libmappedmemory()) return;

(*MEMFreeToMappedMemory)(mem);
}

void wiiu_deinit_mappedmemory(void) {
OSDynLoad_Release(libmappedmemory);
MEMAllocFromMappedMemoryEx = NULL;
MEMFreeToMappedMemory = NULL;
}
10 changes: 10 additions & 0 deletions platform/libretro/wiiu/libmemorymapping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef LIBMEMORYMAPPING_H
#define LIBMEMORYMAPPING_H

#include <stdint.h>

void *wiiu_alloc_mappedmemory(uint32_t size, int32_t alignment);
void wiiu_free_mappedmemory(void *mem);
void wiiu_deinit_mappedmemory(void);

#endif // LIBMEMORYMAPPING_H

0 comments on commit 89d07d4

Please sign in to comment.