shadPS4/src/core/libraries/kernel
Stephen Miller 6cdc52cdde
Core: More Memory Cleanup & Fixes (#2997)
* Only perform GPU memory mapping when GPU can access it

This better aligns with hardware observations, and should also speed up unmaps and decommits, since they don't need to be compared with the GPU max address anymore.

* Reserve fixes

ReserveVirtualRange seems to follow the 0x200000000 base address like MemoryPoolReserve does.
Both also need checks in their flags Fixed path to ensure we're mapping in-bounds. If we're not in mapping to our address space, we'll end up reserving and returning the wrong address, which could lead to weird memory issues in games.

I'll need to test on real hardware to verify if such changes are appropriate.

* Better sceKernelMmap

Handles errors where we would previously throw exceptions. Also moves the file logic to MapFile, since that way all the possible errors are in one place.
Also fixes some function parameters to align with our current standards.

* Major refactor

MapDirectMemory, MapFlexibleMemory, ReserveVirtualRange, and MemoryPoolReserve all internally use mmap to perform their mappings. Naturally, this means that all functions have similar behaviors, and a lot of duplicate code.
This add necessary conditional behavior to MapMemory so MemoryPoolReserve and ReserveVirtualRange can use it, without disrupting the behavior of MapDirectMemory or MapFlexibleMemory calls.

* Accurate phys_addr for non-direct mappings

* Properly handle GPU access rights

Since my first commit restricts GPU mappings to memory areas with GPU access permissions, we also need to be updating the GPU mappings appropriately during Protect calls too.

* Update memory.cpp

* Update memory.h

* Update memory.cpp

* Update memory.cpp

* Update memory.cpp

* Revert "Update memory.cpp"

This reverts commit 2c55d014c0.

* Coalesce dmem map

Aligns with hardware observations, hopefully shouldn't break anything since nothing should change hardware-wise when release dmem calls and unmap calls are performed?
Either that or Windows breaks because Windows, will need to test.

* Implement posix_mprotect

Unity calls this
Also fixes the names of sceKernelMprotect and sceKernelMtypeprotect, though that's more of a style change and can be reverted if requested.

* Fix sceKernelSetVirtualRangeName

Partially addresses a "regression" introduced when I fixed up some asserts.
As noted in the code, this implementation is still slightly inaccurate, as handling this properly could cause regressions on Windows.

* Unconditional assert in MapFile

* Remove protect warning

This is expected behavior, shouldn't need any logging.

* Respect alignment

Forgot to properly do this when updating ReserveVirtualRange and MemoryPoolReserve

* Fix Mprotect on free memory

On real hardware, this just does nothing. If something did get protected, there's no way to query that information.
Therefore, it seems pretty safe to just behave like munmap and return size here.

* Minor tidy-up

No functional difference, but looks better.
2025-05-29 18:56:03 +03:00
..
sync semaphore: Attempt to acquire before checking timeout 2024-12-24 15:39:17 +02:00
threads event_flag: Lower error logs to debug. 2025-05-13 14:46:59 -07:00
aio.cpp kernel: Clean up and fix some mistakes. (#2907) 2025-05-11 14:22:17 -07:00
aio.h sceKernelAio* implementation (#2160) 2025-01-16 18:27:52 +02:00
debug.cpp Minor libkernel changes (#2721) 2025-03-30 01:27:33 +02:00
debug.h Minor libkernel changes (#2721) 2025-03-30 01:27:33 +02:00
equeue.cpp equeue: Move small timer check to WaitForEvents. (#3000) 2025-05-28 19:54:47 +03:00
equeue.h equeue: Clean up timers implementation. (#2925) 2025-05-13 14:05:29 -07:00
file_system.cpp Only perform early read-write open when truncating is needed (#2874) 2025-05-02 11:22:05 -07:00
file_system.h libkernel: Filesystem code cleanup (#2554) 2025-03-26 18:03:35 +02:00
kernel.cpp externals: Remove winpthreads. (#2932) 2025-05-15 13:59:34 -07:00
kernel.h kernel: Clean up and fix some mistakes. (#2907) 2025-05-11 14:22:17 -07:00
memory.cpp Core: More Memory Cleanup & Fixes (#2997) 2025-05-29 18:56:03 +03:00
memory.h Core: More Memory Cleanup & Fixes (#2997) 2025-05-29 18:56:03 +03:00
orbis_error.h core: Library cleanup (#1631) 2024-11-30 22:37:36 +02:00
posix_error.h core: Library cleanup (#1631) 2024-11-30 22:37:36 +02:00
process.cpp Implement sceKernelGetModuleInfo, sceKernelGetModuleInfoInternal, and sceKernelGetModuleList (#2850) 2025-04-27 09:32:01 +03:00
process.h Handle error behavior in sceSysmoduleGetModuleInfoForUnwind stub (#2629) 2025-03-09 23:17:33 +02:00
threads.cpp kernel: Rewrite pthread emulation (#1440) 2024-11-21 22:59:38 +02:00
threads.h kernel: Implement scePthreadGetaffinity (#2916) 2025-05-12 10:46:53 -07:00
time.cpp externals: Remove winpthreads. (#2932) 2025-05-15 13:59:34 -07:00
time.h externals: Remove winpthreads. (#2932) 2025-05-15 13:59:34 -07:00