Skip to content

Commit

Permalink
LV2/Loader: Fix kernel regions addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Aug 21, 2023
1 parent 7a0185d commit 1843a27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
23 changes: 19 additions & 4 deletions rpcs3/Emu/Cell/PPUModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1860,15 +1860,30 @@ bool ppu_load_exec(const ppu_exec_object& elf, bool virtual_load, const std::str
else if (already_loaded)
{
}
else if (!vm::falloc(addr, size, vm::main))
else if (![&]() -> bool
{
ppu_loader.error("vm::falloc(vm::main) failed (addr=0x%x, memsz=0x%x)", addr, size); // TODO
// 1M pages if it is RSX shared
const u32 area_flags = (_seg.flags >> 28) ? vm::page_size_1m : vm::page_size_64k;
const u32 alloc_at = std::max<u32>(addr & -0x10000000, 0x10000);

if (!vm::falloc(addr, size))
const auto area = vm::reserve_map(vm::any, std::max<u32>(addr & -0x10000000, 0x10000), 0x10000000, area_flags);

if (!area)
{
return false;
}

if (area->addr != alloc_at || (area->flags & 0xf00) != area_flags)
{
ppu_loader.error("ppu_load_exec(): vm::falloc() failed (addr=0x%x, memsz=0x%x)", addr, size);
ppu_loader.error("Failed to allocate memory at 0x%x - conflicting memory area exists: area->addr=0x%x, area->flags=0x%x", addr, area->addr, area->flags);
return false;
}

return area->falloc(addr, size);
}())
{
ppu_loader.error("ppu_load_exec(): vm::falloc() failed (addr=0x%x, memsz=0x%x)", addr, size);
return false;
}

// Store only LOAD segments (TODO)
Expand Down
10 changes: 8 additions & 2 deletions rpcs3/Emu/Cell/lv2/sys_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ struct sys_memory_address_table
}
};

std::shared_ptr<vm::block_t> reserve_map(u32 alloc_size, u32 align)
{
return vm::reserve_map(align == 0x10000 ? vm::user64k : vm::user1m, 0, align == 0x10000 ? 0x20000000 : utils::align(alloc_size, 0x10000000)
, align == 0x10000 ? (vm::page_size_64k | vm::bf0_0x1) : (vm::page_size_1m | vm::bf0_0x1));
}

// Todo: fix order of error checks

error_code sys_memory_allocate(cpu_thread& cpu, u32 size, u64 flags, vm::ptr<u32> alloc_addr)
Expand Down Expand Up @@ -123,7 +129,7 @@ error_code sys_memory_allocate(cpu_thread& cpu, u32 size, u64 flags, vm::ptr<u32
return CELL_ENOMEM;
}

if (const auto area = vm::reserve_map(align == 0x10000 ? vm::user64k : vm::user1m, 0, utils::align(size, 0x10000000), 0x401))
if (const auto area = reserve_map(size, align))
{
if (const u32 addr = area->alloc(size, nullptr, align))
{
Expand Down Expand Up @@ -197,7 +203,7 @@ error_code sys_memory_allocate_from_container(cpu_thread& cpu, u32 size, u32 cid
return ct.ret;
}

if (const auto area = vm::reserve_map(align == 0x10000 ? vm::user64k : vm::user1m, 0, utils::align(size, 0x10000000), 0x401))
if (const auto area = reserve_map(size, align))
{
if (const u32 addr = area->alloc(size))
{
Expand Down
8 changes: 4 additions & 4 deletions rpcs3/Emu/Memory/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1803,12 +1803,12 @@ namespace vm
{
const u32 max = (0xC0000000 - size) & (0 - align);

if (size > 0xC0000000 - 0x20000000 || max < 0x20000000)
if (size > 0xC0000000 - 0x10000000 || max < 0x10000000)
{
return nullptr;
}

for (u32 addr = utils::align<u32>(0x20000000, align);; addr += align)
for (u32 addr = utils::align<u32>(0x10000000, align);; addr += align)
{
if (_test_map(addr, size))
{
Expand Down Expand Up @@ -2131,8 +2131,8 @@ namespace vm

g_locations =
{
std::make_shared<block_t>(0x00010000, 0x1FFF0000, page_size_64k | preallocated), // main
std::make_shared<block_t>(0x20000000, 0x10000000, page_size_64k | bf0_0x1), // user 64k pages
std::make_shared<block_t>(0x00010000, 0x0FFF0000, page_size_64k | preallocated), // main
nullptr, // user 64k pages
nullptr, // user 1m pages
nullptr, // rsx context
std::make_shared<block_t>(0xC0000000, 0x10000000, page_size_64k | preallocated), // video
Expand Down

0 comments on commit 1843a27

Please sign in to comment.