mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-04-19 19:14:48 +00:00
libkernel: Fix sceKernelAllocateDirectMemory and sceKernelAvailableDirectMemorySize (#2803)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* AllocateDirectMemory fixes search_start and search_end were ignored in certain cases, this fixes that issue. I've also basically rewritten the function in the process, since the lack of documentation made it difficult to make the proper adjustments. * DirectQueryAvailable fixes remaining_size was calculated incorrectly in cases where a free dmem_area had a base earlier than search_start, or an end after search_end. * Reduce sceKernelGetDirectMemorySize log severity By this point, we've confirmed that sceKernelGetDirectMemorySize is hardware-accurate. There's no reason to clog logs with this function, which games usually call before every sceKernelAllocateDirectMemory call. * Clang * Fix phys_addr_out phys_addr_out should be equal to search_start in cases where search_start is greater than the dmem_area base. * Dividing by zero is fun Need to check for alignment when aligning things. * Update memory.cpp * Clang
This commit is contained in:
parent
df89241eb8
commit
20b11f2d63
2 changed files with 42 additions and 25 deletions
|
@ -19,7 +19,7 @@
|
|||
namespace Libraries::Kernel {
|
||||
|
||||
u64 PS4_SYSV_ABI sceKernelGetDirectMemorySize() {
|
||||
LOG_WARNING(Kernel_Vmm, "called");
|
||||
LOG_TRACE(Kernel_Vmm, "called");
|
||||
const auto* memory = Core::Memory::Instance();
|
||||
return memory->GetTotalDirectSize();
|
||||
}
|
||||
|
|
|
@ -139,35 +139,35 @@ PAddr MemoryManager::Allocate(PAddr search_start, PAddr search_end, size_t size,
|
|||
alignment = alignment > 0 ? alignment : 16_KB;
|
||||
|
||||
auto dmem_area = FindDmemArea(search_start);
|
||||
auto mapping_start = search_start > dmem_area->second.base
|
||||
? Common::AlignUp(search_start, alignment)
|
||||
: Common::AlignUp(dmem_area->second.base, alignment);
|
||||
auto mapping_end = Common::AlignUp(mapping_start + size, alignment);
|
||||
|
||||
const auto is_suitable = [&] {
|
||||
if (dmem_area == dmem_map.end()) {
|
||||
return false;
|
||||
}
|
||||
const auto aligned_base = Common::AlignUp(dmem_area->second.base, alignment);
|
||||
const auto alignment_size = aligned_base - dmem_area->second.base;
|
||||
const auto remaining_size =
|
||||
dmem_area->second.size >= alignment_size ? dmem_area->second.size - alignment_size : 0;
|
||||
return dmem_area->second.is_free && remaining_size >= size;
|
||||
};
|
||||
while (dmem_area != dmem_map.end() && !is_suitable() &&
|
||||
dmem_area->second.GetEnd() <= search_end) {
|
||||
++dmem_area;
|
||||
// Find the first free, large enough dmem area in the range.
|
||||
while ((!dmem_area->second.is_free || dmem_area->second.GetEnd() < mapping_end) &&
|
||||
dmem_area != dmem_map.end()) {
|
||||
// The current dmem_area isn't suitable, move to the next one.
|
||||
dmem_area++;
|
||||
|
||||
// Update local variables based on the new dmem_area
|
||||
mapping_start = search_start > dmem_area->second.base
|
||||
? Common::AlignUp(search_start, alignment)
|
||||
: Common::AlignUp(dmem_area->second.base, alignment);
|
||||
mapping_end = Common::AlignUp(mapping_start + size, alignment);
|
||||
}
|
||||
if (!is_suitable()) {
|
||||
|
||||
if (dmem_area == dmem_map.end()) {
|
||||
// There are no suitable mappings in this range
|
||||
LOG_ERROR(Kernel_Vmm, "Unable to find free direct memory area: size = {:#x}", size);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Align free position
|
||||
PAddr free_addr = dmem_area->second.base;
|
||||
free_addr = Common::AlignUp(free_addr, alignment);
|
||||
|
||||
// Add the allocated region to the list and commit its pages.
|
||||
auto& area = CarveDmemArea(free_addr, size)->second;
|
||||
auto& area = CarveDmemArea(mapping_start, size)->second;
|
||||
area.memory_type = memory_type;
|
||||
area.is_free = false;
|
||||
return free_addr;
|
||||
return mapping_start;
|
||||
}
|
||||
|
||||
void MemoryManager::Free(PAddr phys_addr, size_t size) {
|
||||
|
@ -632,17 +632,34 @@ int MemoryManager::DirectQueryAvailable(PAddr search_start, PAddr search_end, si
|
|||
auto dmem_area = FindDmemArea(search_start);
|
||||
PAddr paddr{};
|
||||
size_t max_size{};
|
||||
while (dmem_area != dmem_map.end() && dmem_area->second.GetEnd() <= search_end) {
|
||||
|
||||
while (dmem_area != dmem_map.end()) {
|
||||
if (!dmem_area->second.is_free) {
|
||||
dmem_area++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto aligned_base = alignment > 0 ? Common::AlignUp(dmem_area->second.base, alignment)
|
||||
: dmem_area->second.base;
|
||||
auto aligned_base = alignment > 0 ? Common::AlignUp(dmem_area->second.base, alignment)
|
||||
: dmem_area->second.base;
|
||||
const auto alignment_size = aligned_base - dmem_area->second.base;
|
||||
const auto remaining_size =
|
||||
auto remaining_size =
|
||||
dmem_area->second.size >= alignment_size ? dmem_area->second.size - alignment_size : 0;
|
||||
|
||||
if (dmem_area->second.base < search_start) {
|
||||
// We need to trim remaining_size to ignore addresses before search_start
|
||||
remaining_size = remaining_size > (search_start - dmem_area->second.base)
|
||||
? remaining_size - (search_start - dmem_area->second.base)
|
||||
: 0;
|
||||
aligned_base = alignment > 0 ? Common::AlignUp(search_start, alignment) : search_start;
|
||||
}
|
||||
|
||||
if (dmem_area->second.GetEnd() > search_end) {
|
||||
// We need to trim remaining_size to ignore addresses beyond search_end
|
||||
remaining_size = remaining_size > (search_start - dmem_area->second.base)
|
||||
? remaining_size - (dmem_area->second.GetEnd() - search_end)
|
||||
: 0;
|
||||
}
|
||||
|
||||
if (remaining_size > max_size) {
|
||||
paddr = aligned_base;
|
||||
max_size = remaining_size;
|
||||
|
|
Loading…
Add table
Reference in a new issue