improved logging in sceKernelAllocateDirectMemory

This commit is contained in:
georgemoralis 2023-08-03 13:27:07 +03:00
parent ba8b614afc
commit 813385caa3
1 changed files with 17 additions and 14 deletions

View File

@ -1,4 +1,7 @@
#include "memory_management.h"
#include <debug.h> #include <debug.h>
#include <bit> #include <bit>
#include <magic_enum.hpp> #include <magic_enum.hpp>
@ -7,10 +10,11 @@
#include "../ErrorCodes.h" #include "../ErrorCodes.h"
#include "../Libs.h" #include "../Libs.h"
#include "Objects/physical_memory.h" #include "Objects/physical_memory.h"
#include "memory_management.h"
namespace HLE::Libs::LibKernel::MemoryManagement { namespace HLE::Libs::LibKernel::MemoryManagement {
constexpr bool log_file_memory = true; // disable it to disable logging
bool isPowerOfTwo(u64 n) { return std::popcount(n) == 1; } bool isPowerOfTwo(u64 n) { return std::popcount(n) == 1; }
bool is16KBAligned(u64 n) { return ((n % (16ull * 1024) == 0)); } bool is16KBAligned(u64 n) { return ((n % (16ull * 1024) == 0)); }
@ -24,43 +28,42 @@ int PS4_SYSV_ABI sceKernelAllocateDirectMemory(s64 searchStart, s64 searchEnd, u
PRINT_FUNCTION_NAME(); PRINT_FUNCTION_NAME();
if (searchStart < 0 || searchEnd <= searchStart) { if (searchStart < 0 || searchEnd <= searchStart) {
// TODO debug logging LOG_TRACE_IF(log_file_memory, "sceKernelAllocateDirectMemory returned SCE_KERNEL_ERROR_EINVAL searchStart,searchEnd invalid\n");
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
bool isInRange = (searchStart < len && searchEnd > len); bool isInRange = (searchStart < len && searchEnd > len);
if (len <= 0 || !is16KBAligned(len) || !isInRange) { if (len <= 0 || !is16KBAligned(len) || !isInRange) {
// TODO debug logging LOG_TRACE_IF(log_file_memory, "sceKernelAllocateDirectMemory returned SCE_KERNEL_ERROR_EINVAL memory range invalid\n");
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
if ((alignment != 0 || is16KBAligned(alignment)) && !isPowerOfTwo(alignment)) { if ((alignment != 0 || is16KBAligned(alignment)) && !isPowerOfTwo(alignment)) {
// TODO debug logging LOG_TRACE_IF(log_file_memory, "sceKernelAllocateDirectMemory returned SCE_KERNEL_ERROR_EINVAL alignment invalid\n");
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
if (physAddrOut == nullptr) { if (physAddrOut == nullptr) {
// TODO debug logging LOG_TRACE_IF(log_file_memory, "sceKernelAllocateDirectMemory returned SCE_KERNEL_ERROR_EINVAL physAddrOut is null\n");
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
auto memtype = magic_enum::enum_cast<MemoryTypes>(memoryType); auto memtype = magic_enum::enum_cast<MemoryTypes>(memoryType);
LOG_INFO_IF(true, "search_start = {}\n", log_hex_full(searchStart)); LOG_INFO_IF(log_file_memory, "search_start = {}\n", log_hex_full(searchStart));
LOG_INFO_IF(true, "search_end = {}\n", log_hex_full(searchEnd)); LOG_INFO_IF(log_file_memory, "search_end = {}\n", log_hex_full(searchEnd));
LOG_INFO_IF(true, "len = {}\n", log_hex_full(len)); LOG_INFO_IF(log_file_memory, "len = {}\n", log_hex_full(len));
LOG_INFO_IF(true, "alignment = {}\n", log_hex_full(alignment)); LOG_INFO_IF(log_file_memory, "alignment = {}\n", log_hex_full(alignment));
LOG_INFO_IF(true, "memory_type = {}\n", magic_enum::enum_name(memtype.value())); LOG_INFO_IF(log_file_memory, "memory_type = {}\n", magic_enum::enum_name(memtype.value()));
u64 physical_addr = 0; u64 physical_addr = 0;
auto* physical_memory = Singleton<HLE::Kernel::Objects::PhysicalMemory>::Instance(); auto* physical_memory = Singleton<HLE::Kernel::Objects::PhysicalMemory>::Instance();
if (!physical_memory->Alloc(searchStart, searchEnd, len, alignment, &physical_addr, memoryType)) { if (!physical_memory->Alloc(searchStart, searchEnd, len, alignment, &physical_addr, memoryType)) {
// TODO debug logging LOG_TRACE_IF(log_file_memory, "sceKernelAllocateDirectMemory returned SCE_KERNEL_ERROR_EAGAIN can't allocate physical memory\n");
return SCE_KERNEL_ERROR_EAGAIN; return SCE_KERNEL_ERROR_EAGAIN;
} }
*physAddrOut = static_cast<s64>(physical_addr); *physAddrOut = static_cast<s64>(physical_addr);
LOG_INFO_IF(true, "physAddrOut = {}\n", log_hex_full(physical_addr)); LOG_INFO_IF(true, "physAddrOut = {}\n", log_hex_full(physical_addr));
return SCE_OK; return SCE_OK;
} }
int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int flags, s64 directMemoryStart, u64 alignment) int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int flags, s64 directMemoryStart, u64 alignment) {
{
auto* physical_memory = Singleton<HLE::Kernel::Objects::PhysicalMemory>::Instance(); auto* physical_memory = Singleton<HLE::Kernel::Objects::PhysicalMemory>::Instance();
BREAKPOINT(); BREAKPOINT();
return SCE_OK; return SCE_OK;