From 4db13cabae241c4c93614c9e2e966d6e19adf87d Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 1 Oct 2024 19:43:13 +0300 Subject: [PATCH] add dedicated mmap functions for libnx the previous code would call svcUnmapPhysicalMemory() on mem possibly allocated by aligned_alloc() which didn't look right --- frontend/libretro.c | 33 ++++++++++++++++++++++++++++++--- frontend/switch/sys/mman.h | 2 ++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/frontend/libretro.c b/frontend/libretro.c index bcd3c615..52c2c1c9 100644 --- a/frontend/libretro.c +++ b/frontend/libretro.c @@ -498,6 +498,31 @@ static void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag) } #endif +#ifdef HAVE_LIBNX +static void *pl_switch_mmap(unsigned long addr, size_t size, + enum psxMapTag tag, int *can_retry_addr) +{ + void *ret = MAP_FAILED; + *can_retry_addr = 0; + (void)addr; + + // there's svcMapPhysicalMemory() but user logs show it doesn't hand out + // any desired addresses, so don't even bother + ret = aligned_alloc(0x1000, size); + if (!ret) + return MAP_FAILED; + memset(ret, 0, size); + return ret; +} + +static void pl_switch_munmap(void *ptr, size_t size, enum psxMapTag tag) +{ + (void)size; + (void)tag; + free(ptr); +} +#endif + #ifdef VITA typedef struct { @@ -3792,11 +3817,13 @@ void retro_init(void) syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0); #endif -#ifdef _3DS +#if defined(_3DS) psxMapHook = pl_3ds_mmap; psxUnmapHook = pl_3ds_munmap; -#endif -#ifdef VITA +#elif defined(HAVE_LIBNX) + psxMapHook = pl_switch_mmap; + psxUnmapHook = pl_switch_munmap; +#elif defined(VITA) if (init_vita_mmap() < 0) abort(); psxMapHook = pl_vita_mmap; diff --git a/frontend/switch/sys/mman.h b/frontend/switch/sys/mman.h index 2e084a64..1d31e252 100644 --- a/frontend/switch/sys/mman.h +++ b/frontend/switch/sys/mman.h @@ -20,6 +20,7 @@ extern "C" { #define ALIGNMENT 0x1000 +#if 0 // not used static inline void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) { (void)fd; @@ -51,6 +52,7 @@ static inline int munmap(void *addr, size_t len) } return 0; } +#endif #ifdef __cplusplus }; -- 2.39.5