Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Match changes to svcMapProcessMemoryEx from latest Luma3DS (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloMK7 authored Sep 27, 2024
1 parent f3fb038 commit 608383e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/core/file_sys/plugin_3gx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Map(
4 * 1024 * 1024 // 4 MiB
};

const bool is_mem_private = header.infos.flags.use_private_memory != 0;

// Map memory block. This behaviour mimics how plugins are loaded on 3DS as much as possible.
// Calculate the sizes of the different memory regions
const u32 block_size = mem_region_sizes[header.infos.flags.memory_region_size.Value()];
Expand All @@ -199,7 +201,8 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Map(
std::fill(backing_memory_fb.GetPtr(), backing_memory_fb.GetPtr() + _3GX_fb_size, 0);

auto vma_heap_fb = process.vm_manager.MapBackingMemory(
_3GX_heap_load_addr, backing_memory_fb, _3GX_fb_size, Kernel::MemoryState::Continuous);
_3GX_heap_load_addr, backing_memory_fb, _3GX_fb_size,
is_mem_private ? Kernel::MemoryState::Private : Kernel::MemoryState::Shared);
ASSERT(vma_heap_fb.Succeeded());
process.vm_manager.Reprotect(vma_heap_fb.Unwrap(), Kernel::VMAPermission::ReadWrite);

Expand All @@ -217,7 +220,8 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Map(

// Then we map part of the memory, which contains the executable
auto vma = process.vm_manager.MapBackingMemory(_3GX_exe_load_addr, backing_memory, exe_size,
Kernel::MemoryState::Continuous);
is_mem_private ? Kernel::MemoryState::Private
: Kernel::MemoryState::Shared);
ASSERT(vma.Succeeded());
process.vm_manager.Reprotect(vma.Unwrap(), Kernel::VMAPermission::ReadWriteExecute);

Expand Down Expand Up @@ -256,7 +260,8 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Map(
// Map the rest of the memory at the heap location
auto vma_heap = process.vm_manager.MapBackingMemory(
_3GX_heap_load_addr + _3GX_fb_size, backing_memory_heap,
block_size - exe_size - _3GX_fb_size, Kernel::MemoryState::Continuous);
block_size - exe_size - _3GX_fb_size,
is_mem_private ? Kernel::MemoryState::Private : Kernel::MemoryState::Shared);
ASSERT(vma_heap.Succeeded());
process.vm_manager.Reprotect(vma_heap.Unwrap(), Kernel::VMAPermission::ReadWriteExecute);

Expand Down
3 changes: 3 additions & 0 deletions src/core/file_sys/plugin_3gx.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class Plugin3GXLoader {
BitField<1, 1, u32_le> embedded_swap_func;
BitField<2, 2, u32_le> memory_region_size;
BitField<4, 2, u32_le> compatibility;
BitField<6, 1, u32_le> events_self_managed;
BitField<7, 1, u32_le> swap_not_needed;
BitField<8, 1, u32_le> use_private_memory;
} flags;
u32_le exe_load_checksum;
u32_le builtin_load_exe_args[4];
Expand Down
40 changes: 33 additions & 7 deletions src/core/hle/kernel/svc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ enum class ControlProcessOP {
PROCESSOP_DISABLE_CREATE_THREAD_RESTRICTIONS,
};

/**
* Accepted by the custom svcMapProcessMemoryEx.
*/
enum class MapMemoryExFlag {
/**
* Maps the memory region as PRIVATE instead of SHARED
*/
MAPEXFLAGS_PRIVATE = (1 << 0),
};

class SVC : public SVCWrapper<SVC> {
public:
SVC(Core::System& system);
Expand Down Expand Up @@ -460,7 +470,8 @@ class SVC : public SVCWrapper<SVC> {
Result InvalidateEntireInstructionCache();
u32 ConvertVaToPa(u32 addr);
Result MapProcessMemoryEx(Handle dst_process_handle, u32 dst_address, Handle src_process_handle,
u32 src_address, u32 size);
u32 src_address, u32 size, MapMemoryExFlag flags,
Handle dst_process_handle_backup);
Result UnmapProcessMemoryEx(Handle process, u32 dst_address, u32 size);
Result ControlProcess(Handle process_handle, u32 process_OP, u32 varg2, u32 varg3);

Expand Down Expand Up @@ -2012,7 +2023,22 @@ u32 SVC::ConvertVaToPa(u32 addr) {
}

Result SVC::MapProcessMemoryEx(Handle dst_process_handle, u32 dst_address,
Handle src_process_handle, u32 src_address, u32 size) {
Handle src_process_handle, u32 src_address, u32 size,
MapMemoryExFlag flags, Handle dst_process_handle_backup) {

// Determine if this is the second version of the svc by checking the value at R0.
constexpr u32 SVC_VERSION2_MAGIC = 0xFFFFFFF2;
if (static_cast<u32>(dst_process_handle) == SVC_VERSION2_MAGIC) {
// Version 2, actual handle is provided in 6th argument
dst_process_handle = dst_process_handle_backup;
} else {
// Version 1, the flags argument is not used
flags = static_cast<MapMemoryExFlag>(0);
}

const bool map_as_private =
(static_cast<u32>(flags) & static_cast<u32>(MapMemoryExFlag::MAPEXFLAGS_PRIVATE)) != 0;

std::shared_ptr<Process> dst_process =
kernel.GetCurrentProcess()->handle_table.Get<Process>(dst_process_handle);
std::shared_ptr<Process> src_process =
Expand All @@ -2024,11 +2050,12 @@ Result SVC::MapProcessMemoryEx(Handle dst_process_handle, u32 dst_address,
size = (size & ~0xFFF) + Memory::CITRA_PAGE_SIZE;
}

// TODO(PabloMK7) Fix-up this svc.

// Only linear memory supported
auto vma = src_process->vm_manager.FindVMA(src_address);
R_UNLESS(vma != src_process->vm_manager.vma_map.end() &&
vma->second.type == VMAType::BackingMemory &&
vma->second.meminfo_state == MemoryState::Continuous,
vma->second.type == VMAType::BackingMemory,
ResultInvalidAddress);

const u32 offset = src_address - vma->second.base;
Expand All @@ -2038,7 +2065,7 @@ Result SVC::MapProcessMemoryEx(Handle dst_process_handle, u32 dst_address,
dst_address,
memory.GetFCRAMRef(vma->second.backing_memory.GetPtr() + offset -
kernel.memory.GetFCRAMPointer(0)),
size, Kernel::MemoryState::Continuous);
size, map_as_private ? MemoryState::Private : MemoryState::Shared);

if (!vma_res.Succeeded()) {
return ResultInvalidAddressState;
Expand All @@ -2060,8 +2087,7 @@ Result SVC::UnmapProcessMemoryEx(Handle process, u32 dst_address, u32 size) {
// Only linear memory supported
auto vma = dst_process->vm_manager.FindVMA(dst_address);
R_UNLESS(vma != dst_process->vm_manager.vma_map.end() &&
vma->second.type == VMAType::BackingMemory &&
vma->second.meminfo_state == MemoryState::Continuous,
vma->second.type == VMAType::BackingMemory,
ResultInvalidAddress);

dst_process->vm_manager.UnmapRange(dst_address, size);
Expand Down
2 changes: 1 addition & 1 deletion src/core/hle/service/plgldr/plgldr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SERVICE_CONSTRUCT_IMPL(Service::PLGLDR::PLG_LDR)

namespace Service::PLGLDR {

static const Kernel::CoreVersion plgldr_version = Kernel::CoreVersion(1, 0, 0);
static const Kernel::CoreVersion plgldr_version = Kernel::CoreVersion(1, 0, 2);

PLG_LDR::PLG_LDR(Core::System& system_) : ServiceFramework{"plg:ldr", 1}, system(system_) {
static const FunctionInfo functions[] = {
Expand Down

0 comments on commit 608383e

Please sign in to comment.