Merge pull request #1557 from bunnei/ldr_ro
Implement the LoadNro functions from the ldr:ro service.
This commit is contained in:
commit
a94831f2a9
|
@ -232,6 +232,12 @@ void Process::LoadModule(CodeSet module_, VAddr base_addr) {
|
||||||
MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::CodeStatic);
|
MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::CodeStatic);
|
||||||
MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeMutable);
|
MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeMutable);
|
||||||
MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeMutable);
|
MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeMutable);
|
||||||
|
|
||||||
|
// Clear instruction cache in CPU JIT
|
||||||
|
Core::System::GetInstance().ArmInterface(0).ClearInstructionCache();
|
||||||
|
Core::System::GetInstance().ArmInterface(1).ClearInstructionCache();
|
||||||
|
Core::System::GetInstance().ArmInterface(2).ClearInstructionCache();
|
||||||
|
Core::System::GetInstance().ArmInterface(3).ClearInstructionCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<VAddr> Process::HeapAllocate(VAddr target, u64 size, VMAPermission perms) {
|
ResultVal<VAddr> Process::HeapAllocate(VAddr target, u64 size, VMAPermission perms) {
|
||||||
|
|
|
@ -143,6 +143,26 @@ ResultVal<VMManager::VMAHandle> VMManager::MapBackingMemory(VAddr target, u8* me
|
||||||
return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
|
return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ResultVal<VAddr> VMManager::FindFreeRegion(u64 size) const {
|
||||||
|
// Find the first Free VMA.
|
||||||
|
const VAddr base = GetASLRRegionBaseAddress();
|
||||||
|
const VMAHandle vma_handle = std::find_if(vma_map.begin(), vma_map.end(), [&](const auto& vma) {
|
||||||
|
if (vma.second.type != VMAType::Free)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const VAddr vma_end = vma.second.base + vma.second.size;
|
||||||
|
return vma_end > base && vma_end >= base + size;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (vma_handle == vma_map.end()) {
|
||||||
|
// TODO(Subv): Find the correct error code here.
|
||||||
|
return ResultCode(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const VAddr target = std::max(base, vma_handle->second.base);
|
||||||
|
return MakeResult<VAddr>(target);
|
||||||
|
}
|
||||||
|
|
||||||
ResultVal<VMManager::VMAHandle> VMManager::MapMMIO(VAddr target, PAddr paddr, u64 size,
|
ResultVal<VMManager::VMAHandle> VMManager::MapMMIO(VAddr target, PAddr paddr, u64 size,
|
||||||
MemoryState state,
|
MemoryState state,
|
||||||
Memory::MemoryHookPointer mmio_handler) {
|
Memory::MemoryHookPointer mmio_handler) {
|
||||||
|
|
|
@ -157,6 +157,14 @@ public:
|
||||||
*/
|
*/
|
||||||
ResultVal<VMAHandle> MapBackingMemory(VAddr target, u8* memory, u64 size, MemoryState state);
|
ResultVal<VMAHandle> MapBackingMemory(VAddr target, u8* memory, u64 size, MemoryState state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the first free address that can hold a region of the desired size.
|
||||||
|
*
|
||||||
|
* @param size Size of the desired region.
|
||||||
|
* @return The found free address.
|
||||||
|
*/
|
||||||
|
ResultVal<VAddr> FindFreeRegion(u64 size) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps a memory-mapped IO region at a given address.
|
* Maps a memory-mapped IO region at a given address.
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,9 +3,13 @@
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
|
#include "core/hle/ipc_helpers.h"
|
||||||
|
#include "core/hle/kernel/process.h"
|
||||||
#include "core/hle/service/ldr/ldr.h"
|
#include "core/hle/service/ldr/ldr.h"
|
||||||
#include "core/hle/service/service.h"
|
#include "core/hle/service/service.h"
|
||||||
|
#include "core/loader/nro.h"
|
||||||
|
|
||||||
namespace Service::LDR {
|
namespace Service::LDR {
|
||||||
|
|
||||||
|
@ -59,16 +63,58 @@ public:
|
||||||
explicit RelocatableObject() : ServiceFramework{"ldr:ro"} {
|
explicit RelocatableObject() : ServiceFramework{"ldr:ro"} {
|
||||||
// clang-format off
|
// clang-format off
|
||||||
static const FunctionInfo functions[] = {
|
static const FunctionInfo functions[] = {
|
||||||
{0, nullptr, "LoadNro"},
|
{0, &RelocatableObject::LoadNro, "LoadNro"},
|
||||||
{1, nullptr, "UnloadNro"},
|
{1, nullptr, "UnloadNro"},
|
||||||
{2, nullptr, "LoadNrr"},
|
{2, &RelocatableObject::LoadNrr, "LoadNrr"},
|
||||||
{3, nullptr, "UnloadNrr"},
|
{3, nullptr, "UnloadNrr"},
|
||||||
{4, nullptr, "Initialize"},
|
{4, &RelocatableObject::Initialize, "Initialize"},
|
||||||
};
|
};
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
RegisterHandlers(functions);
|
RegisterHandlers(functions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoadNrr(Kernel::HLERequestContext& ctx) {
|
||||||
|
IPC::ResponseBuilder rb{ctx, 2};
|
||||||
|
rb.Push(RESULT_SUCCESS);
|
||||||
|
LOG_WARNING(Service_LDR, "(STUBBED) called");
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadNro(Kernel::HLERequestContext& ctx) {
|
||||||
|
IPC::RequestParser rp{ctx};
|
||||||
|
rp.Skip(2, false);
|
||||||
|
const VAddr nro_addr{rp.Pop<VAddr>()};
|
||||||
|
const u64 nro_size{rp.Pop<u64>()};
|
||||||
|
const VAddr bss_addr{rp.Pop<VAddr>()};
|
||||||
|
const u64 bss_size{rp.Pop<u64>()};
|
||||||
|
|
||||||
|
// Read NRO data from memory
|
||||||
|
std::vector<u8> nro_data(nro_size);
|
||||||
|
Memory::ReadBlock(nro_addr, nro_data.data(), nro_size);
|
||||||
|
|
||||||
|
// Load NRO as new executable module
|
||||||
|
const VAddr addr{*Core::CurrentProcess()->VMManager().FindFreeRegion(nro_size + bss_size)};
|
||||||
|
Loader::AppLoader_NRO::LoadNro(nro_data, fmt::format("nro-{:08x}", addr), addr);
|
||||||
|
|
||||||
|
// TODO(bunnei): This is an incomplete implementation. It was tested with Super Mario Party.
|
||||||
|
// It is currently missing:
|
||||||
|
// - Signature checks with LoadNRR
|
||||||
|
// - Checking if a module has already been loaded
|
||||||
|
// - Using/validating BSS, etc. params (these are used from NRO header instead)
|
||||||
|
// - Error checking
|
||||||
|
// - ...Probably other things
|
||||||
|
|
||||||
|
IPC::ResponseBuilder rb{ctx, 4};
|
||||||
|
rb.Push(RESULT_SUCCESS);
|
||||||
|
rb.Push(addr);
|
||||||
|
LOG_WARNING(Service_LDR, "(STUBBED) called");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Initialize(Kernel::HLERequestContext& ctx) {
|
||||||
|
IPC::ResponseBuilder rb{ctx, 2};
|
||||||
|
rb.Push(RESULT_SUCCESS);
|
||||||
|
LOG_WARNING(Service_LDR, "(STUBBED) called");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void InstallInterfaces(SM::ServiceManager& sm) {
|
void InstallInterfaces(SM::ServiceManager& sm) {
|
||||||
|
|
|
@ -127,18 +127,23 @@ static constexpr u32 PageAlignSize(u32 size) {
|
||||||
return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
|
return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AppLoader_NRO::LoadNro(const FileSys::VfsFile& file, VAddr load_base) {
|
/*static*/ bool AppLoader_NRO::LoadNro(const std::vector<u8>& data, const std::string& name,
|
||||||
// Read NSO header
|
VAddr load_base) {
|
||||||
NroHeader nro_header{};
|
|
||||||
if (sizeof(NroHeader) != file.ReadObject(&nro_header)) {
|
if (data.size() < sizeof(NroHeader)) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read NSO header
|
||||||
|
NroHeader nro_header{};
|
||||||
|
std::memcpy(&nro_header, data.data(), sizeof(NroHeader));
|
||||||
if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
|
if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build program image
|
// Build program image
|
||||||
std::vector<u8> program_image = file.ReadBytes(PageAlignSize(nro_header.file_size));
|
std::vector<u8> program_image(PageAlignSize(nro_header.file_size));
|
||||||
|
std::memcpy(program_image.data(), data.data(), program_image.size());
|
||||||
if (program_image.size() != PageAlignSize(nro_header.file_size)) {
|
if (program_image.size() != PageAlignSize(nro_header.file_size)) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -182,11 +187,15 @@ bool AppLoader_NRO::LoadNro(const FileSys::VfsFile& file, VAddr load_base) {
|
||||||
Core::CurrentProcess()->LoadModule(std::move(codeset), load_base);
|
Core::CurrentProcess()->LoadModule(std::move(codeset), load_base);
|
||||||
|
|
||||||
// Register module with GDBStub
|
// Register module with GDBStub
|
||||||
GDBStub::RegisterModule(file.GetName(), load_base, load_base);
|
GDBStub::RegisterModule(name, load_base, load_base);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool AppLoader_NRO::LoadNro(const FileSys::VfsFile& file, VAddr load_base) {
|
||||||
|
return AppLoader_NRO::LoadNro(file.ReadAllBytes(), file.GetName(), load_base);
|
||||||
|
}
|
||||||
|
|
||||||
ResultStatus AppLoader_NRO::Load(Kernel::Process& process) {
|
ResultStatus AppLoader_NRO::Load(Kernel::Process& process) {
|
||||||
if (is_loaded) {
|
if (is_loaded) {
|
||||||
return ResultStatus::ErrorAlreadyLoaded;
|
return ResultStatus::ErrorAlreadyLoaded;
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
#include "core/loader/linker.h"
|
#include "core/loader/linker.h"
|
||||||
#include "core/loader/loader.h"
|
#include "core/loader/loader.h"
|
||||||
|
@ -40,6 +41,8 @@ public:
|
||||||
ResultStatus ReadTitle(std::string& title) override;
|
ResultStatus ReadTitle(std::string& title) override;
|
||||||
bool IsRomFSUpdatable() const override;
|
bool IsRomFSUpdatable() const override;
|
||||||
|
|
||||||
|
static bool LoadNro(const std::vector<u8>& data, const std::string& name, VAddr load_base);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool LoadNro(const FileSys::VfsFile& file, VAddr load_base);
|
bool LoadNro(const FileSys::VfsFile& file, VAddr load_base);
|
||||||
|
|
||||||
|
|
Reference in New Issue