mirror of
https://github.com/yuzu-emu/yuzu-android.git
synced 2025-06-12 11:58:28 -05:00
Merge pull request #12074 from GPUCode/yuwu-on-the-metal
Implement Native Code Execution (NCE)
This commit is contained in:
@ -75,12 +75,26 @@ struct CodeSet final {
|
||||
return segments[2];
|
||||
}
|
||||
|
||||
#ifdef HAS_NCE
|
||||
Segment& PatchSegment() {
|
||||
return patch_segment;
|
||||
}
|
||||
|
||||
const Segment& PatchSegment() const {
|
||||
return patch_segment;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// The overall data that backs this code set.
|
||||
Kernel::PhysicalMemory memory;
|
||||
|
||||
/// The segments that comprise this code set.
|
||||
std::array<Segment, 3> segments;
|
||||
|
||||
#ifdef HAS_NCE
|
||||
Segment patch_segment;
|
||||
#endif
|
||||
|
||||
/// The entry point address for this code set.
|
||||
KProcessAddress entrypoint = 0;
|
||||
};
|
||||
|
@ -25,8 +25,8 @@ constexpr std::array<KAddressSpaceInfo, 13> AddressSpaceInfos{{
|
||||
{ .bit_width = 36, .address = 2_GiB , .size = 64_GiB - 2_GiB , .type = KAddressSpaceInfo::Type::MapLarge, },
|
||||
{ .bit_width = 36, .address = Size_Invalid, .size = 8_GiB , .type = KAddressSpaceInfo::Type::Heap, },
|
||||
{ .bit_width = 36, .address = Size_Invalid, .size = 6_GiB , .type = KAddressSpaceInfo::Type::Alias, },
|
||||
#ifdef ANDROID
|
||||
// With Android, we use a 38-bit address space due to memory limitations. This should (safely) truncate ASLR region.
|
||||
#ifdef HAS_NCE
|
||||
// With NCE, we use a 38-bit address space due to memory limitations. This should (safely) truncate ASLR region.
|
||||
{ .bit_width = 39, .address = 128_MiB , .size = 256_GiB - 128_MiB, .type = KAddressSpaceInfo::Type::Map39Bit, },
|
||||
#else
|
||||
{ .bit_width = 39, .address = 128_MiB , .size = 512_GiB - 128_MiB, .type = KAddressSpaceInfo::Type::Map39Bit, },
|
||||
|
@ -88,6 +88,22 @@ Result FlushDataCache(AddressType addr, u64 size) {
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
constexpr Common::MemoryPermission ConvertToMemoryPermission(KMemoryPermission perm) {
|
||||
Common::MemoryPermission perms{};
|
||||
if (True(perm & KMemoryPermission::UserRead)) {
|
||||
perms |= Common::MemoryPermission::Read;
|
||||
}
|
||||
if (True(perm & KMemoryPermission::UserWrite)) {
|
||||
perms |= Common::MemoryPermission::Write;
|
||||
}
|
||||
#ifdef HAS_NCE
|
||||
if (True(perm & KMemoryPermission::UserExecute)) {
|
||||
perms |= Common::MemoryPermission::Execute;
|
||||
}
|
||||
#endif
|
||||
return perms;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void KPageTableBase::MemoryRange::Open() {
|
||||
@ -170,7 +186,8 @@ Result KPageTableBase::InitializeForProcess(Svc::CreateProcessFlag as_type, bool
|
||||
KMemoryManager::Pool pool, KProcessAddress code_address,
|
||||
size_t code_size, KSystemResource* system_resource,
|
||||
KResourceLimit* resource_limit,
|
||||
Core::Memory::Memory& memory) {
|
||||
Core::Memory::Memory& memory,
|
||||
KProcessAddress aslr_space_start) {
|
||||
// Calculate region extents.
|
||||
const size_t as_width = GetAddressSpaceWidth(as_type);
|
||||
const KProcessAddress start = 0;
|
||||
@ -211,7 +228,8 @@ Result KPageTableBase::InitializeForProcess(Svc::CreateProcessFlag as_type, bool
|
||||
heap_region_size = GetSpaceSize(KAddressSpaceInfo::Type::Heap);
|
||||
stack_region_size = GetSpaceSize(KAddressSpaceInfo::Type::Stack);
|
||||
kernel_map_region_size = GetSpaceSize(KAddressSpaceInfo::Type::MapSmall);
|
||||
m_code_region_start = GetSpaceStart(KAddressSpaceInfo::Type::Map39Bit);
|
||||
m_code_region_start = m_address_space_start + aslr_space_start +
|
||||
GetSpaceStart(KAddressSpaceInfo::Type::Map39Bit);
|
||||
m_code_region_end = m_code_region_start + GetSpaceSize(KAddressSpaceInfo::Type::Map39Bit);
|
||||
m_alias_code_region_start = m_code_region_start;
|
||||
m_alias_code_region_end = m_code_region_end;
|
||||
@ -5643,7 +5661,8 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a
|
||||
case OperationType::Map: {
|
||||
ASSERT(virt_addr != 0);
|
||||
ASSERT(Common::IsAligned(GetInteger(virt_addr), PageSize));
|
||||
m_memory->MapMemoryRegion(*m_impl, virt_addr, num_pages * PageSize, phys_addr);
|
||||
m_memory->MapMemoryRegion(*m_impl, virt_addr, num_pages * PageSize, phys_addr,
|
||||
ConvertToMemoryPermission(properties.perm));
|
||||
|
||||
// Open references to pages, if we should.
|
||||
if (this->IsHeapPhysicalAddress(phys_addr)) {
|
||||
@ -5658,8 +5677,11 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a
|
||||
}
|
||||
case OperationType::ChangePermissions:
|
||||
case OperationType::ChangePermissionsAndRefresh:
|
||||
case OperationType::ChangePermissionsAndRefreshAndFlush:
|
||||
case OperationType::ChangePermissionsAndRefreshAndFlush: {
|
||||
m_memory->ProtectRegion(*m_impl, virt_addr, num_pages * PageSize,
|
||||
ConvertToMemoryPermission(properties.perm));
|
||||
R_SUCCEED();
|
||||
}
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
@ -5687,7 +5709,8 @@ Result KPageTableBase::Operate(PageLinkedList* page_list, KProcessAddress virt_a
|
||||
const size_t size{node.GetNumPages() * PageSize};
|
||||
|
||||
// Map the pages.
|
||||
m_memory->MapMemoryRegion(*m_impl, virt_addr, size, node.GetAddress());
|
||||
m_memory->MapMemoryRegion(*m_impl, virt_addr, size, node.GetAddress(),
|
||||
ConvertToMemoryPermission(properties.perm));
|
||||
|
||||
virt_addr += size;
|
||||
}
|
||||
|
@ -235,7 +235,8 @@ public:
|
||||
bool enable_device_address_space_merge, bool from_back,
|
||||
KMemoryManager::Pool pool, KProcessAddress code_address,
|
||||
size_t code_size, KSystemResource* system_resource,
|
||||
KResourceLimit* resource_limit, Core::Memory::Memory& memory);
|
||||
KResourceLimit* resource_limit, Core::Memory::Memory& memory,
|
||||
KProcessAddress aslr_space_start);
|
||||
|
||||
void Finalize();
|
||||
|
||||
|
@ -300,7 +300,7 @@ Result KProcess::Initialize(const Svc::CreateProcessParameter& params, const KPa
|
||||
False(params.flags & Svc::CreateProcessFlag::DisableDeviceAddressSpaceMerge);
|
||||
R_TRY(m_page_table.Initialize(as_type, enable_aslr, enable_das_merge, !enable_aslr, pool,
|
||||
params.code_address, params.code_num_pages * PageSize,
|
||||
m_system_resource, res_limit, this->GetMemory()));
|
||||
m_system_resource, res_limit, this->GetMemory(), 0));
|
||||
}
|
||||
ON_RESULT_FAILURE_2 {
|
||||
m_page_table.Finalize();
|
||||
@ -332,7 +332,7 @@ Result KProcess::Initialize(const Svc::CreateProcessParameter& params, const KPa
|
||||
|
||||
Result KProcess::Initialize(const Svc::CreateProcessParameter& params,
|
||||
std::span<const u32> user_caps, KResourceLimit* res_limit,
|
||||
KMemoryManager::Pool pool) {
|
||||
KMemoryManager::Pool pool, KProcessAddress aslr_space_start) {
|
||||
ASSERT(res_limit != nullptr);
|
||||
|
||||
// Set members.
|
||||
@ -393,7 +393,7 @@ Result KProcess::Initialize(const Svc::CreateProcessParameter& params,
|
||||
False(params.flags & Svc::CreateProcessFlag::DisableDeviceAddressSpaceMerge);
|
||||
R_TRY(m_page_table.Initialize(as_type, enable_aslr, enable_das_merge, !enable_aslr, pool,
|
||||
params.code_address, code_size, m_system_resource, res_limit,
|
||||
this->GetMemory()));
|
||||
this->GetMemory(), aslr_space_start));
|
||||
}
|
||||
ON_RESULT_FAILURE_2 {
|
||||
m_page_table.Finalize();
|
||||
@ -1128,7 +1128,7 @@ KProcess::KProcess(KernelCore& kernel)
|
||||
KProcess::~KProcess() = default;
|
||||
|
||||
Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size,
|
||||
bool is_hbl) {
|
||||
KProcessAddress aslr_space_start, bool is_hbl) {
|
||||
// Create a resource limit for the process.
|
||||
const auto physical_memory_size =
|
||||
m_kernel.MemoryManager().GetSize(Kernel::KMemoryManager::Pool::Application);
|
||||
@ -1179,7 +1179,7 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std:
|
||||
.name = {},
|
||||
.version = {},
|
||||
.program_id = metadata.GetTitleID(),
|
||||
.code_address = code_address,
|
||||
.code_address = code_address + GetInteger(aslr_space_start),
|
||||
.code_num_pages = static_cast<s32>(code_size / PageSize),
|
||||
.flags = flag,
|
||||
.reslimit = Svc::InvalidHandle,
|
||||
@ -1193,7 +1193,7 @@ Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std:
|
||||
|
||||
// Initialize for application process.
|
||||
R_TRY(this->Initialize(params, metadata.GetKernelCapabilities(), res_limit,
|
||||
KMemoryManager::Pool::Application));
|
||||
KMemoryManager::Pool::Application, aslr_space_start));
|
||||
|
||||
// Assign remaining properties.
|
||||
m_is_hbl = is_hbl;
|
||||
@ -1214,6 +1214,17 @@ void KProcess::LoadModule(CodeSet code_set, KProcessAddress base_addr) {
|
||||
ReprotectSegment(code_set.CodeSegment(), Svc::MemoryPermission::ReadExecute);
|
||||
ReprotectSegment(code_set.RODataSegment(), Svc::MemoryPermission::Read);
|
||||
ReprotectSegment(code_set.DataSegment(), Svc::MemoryPermission::ReadWrite);
|
||||
|
||||
#ifdef HAS_NCE
|
||||
if (Settings::IsNceEnabled()) {
|
||||
auto& buffer = m_kernel.System().DeviceMemory().buffer;
|
||||
const auto& code = code_set.CodeSegment();
|
||||
const auto& patch = code_set.PatchSegment();
|
||||
buffer.Protect(GetInteger(base_addr + code.addr), code.size, true, true, true);
|
||||
buffer.Protect(GetInteger(base_addr + patch.addr), patch.size, true, true, true);
|
||||
ReprotectSegment(code_set.PatchSegment(), Svc::MemoryPermission::None);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool KProcess::InsertWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type) {
|
||||
|
@ -120,6 +120,9 @@ private:
|
||||
std::atomic<s64> m_num_ipc_messages{};
|
||||
std::atomic<s64> m_num_ipc_replies{};
|
||||
std::atomic<s64> m_num_ipc_receives{};
|
||||
#ifdef HAS_NCE
|
||||
std::unordered_map<u64, u64> m_post_handlers{};
|
||||
#endif
|
||||
|
||||
private:
|
||||
Result StartTermination();
|
||||
@ -150,7 +153,8 @@ public:
|
||||
std::span<const u32> caps, KResourceLimit* res_limit,
|
||||
KMemoryManager::Pool pool, bool immortal);
|
||||
Result Initialize(const Svc::CreateProcessParameter& params, std::span<const u32> user_caps,
|
||||
KResourceLimit* res_limit, KMemoryManager::Pool pool);
|
||||
KResourceLimit* res_limit, KMemoryManager::Pool pool,
|
||||
KProcessAddress aslr_space_start);
|
||||
void Exit();
|
||||
|
||||
const char* GetName() const {
|
||||
@ -466,6 +470,12 @@ public:
|
||||
|
||||
static void Switch(KProcess* cur_process, KProcess* next_process);
|
||||
|
||||
#ifdef HAS_NCE
|
||||
std::unordered_map<u64, u64>& GetPostHandlers() noexcept {
|
||||
return m_post_handlers;
|
||||
}
|
||||
#endif
|
||||
|
||||
public:
|
||||
// Attempts to insert a watchpoint into a free slot. Returns false if none are available.
|
||||
bool InsertWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type);
|
||||
@ -479,7 +489,7 @@ public:
|
||||
|
||||
public:
|
||||
Result LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size,
|
||||
bool is_hbl);
|
||||
KProcessAddress aslr_space_start, bool is_hbl);
|
||||
|
||||
void LoadModule(CodeSet code_set, KProcessAddress base_addr);
|
||||
|
||||
|
@ -23,10 +23,11 @@ public:
|
||||
Result Initialize(Svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge,
|
||||
bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address,
|
||||
size_t code_size, KSystemResource* system_resource,
|
||||
KResourceLimit* resource_limit, Core::Memory::Memory& memory) {
|
||||
R_RETURN(m_page_table.InitializeForProcess(as_type, enable_aslr, enable_das_merge,
|
||||
from_back, pool, code_address, code_size,
|
||||
system_resource, resource_limit, memory));
|
||||
KResourceLimit* resource_limit, Core::Memory::Memory& memory,
|
||||
KProcessAddress aslr_space_start) {
|
||||
R_RETURN(m_page_table.InitializeForProcess(
|
||||
as_type, enable_aslr, enable_das_merge, from_back, pool, code_address, code_size,
|
||||
system_resource, resource_limit, memory, aslr_space_start));
|
||||
}
|
||||
|
||||
void Finalize() {
|
||||
|
@ -655,6 +655,21 @@ public:
|
||||
return m_stack_top;
|
||||
}
|
||||
|
||||
public:
|
||||
// TODO: This shouldn't be defined in kernel namespace
|
||||
struct NativeExecutionParameters {
|
||||
u64 tpidr_el0{};
|
||||
u64 tpidrro_el0{};
|
||||
void* native_context{};
|
||||
std::atomic<u32> lock{1};
|
||||
bool is_running{};
|
||||
u32 magic{Common::MakeMagic('Y', 'U', 'Z', 'U')};
|
||||
};
|
||||
|
||||
NativeExecutionParameters& GetNativeExecutionParameters() {
|
||||
return m_native_execution_parameters;
|
||||
}
|
||||
|
||||
private:
|
||||
KThread* RemoveWaiterByKey(bool* out_has_waiters, KProcessAddress key,
|
||||
bool is_kernel_address_key);
|
||||
@ -914,6 +929,7 @@ private:
|
||||
ThreadWaitReasonForDebugging m_wait_reason_for_debugging{};
|
||||
uintptr_t m_argument{};
|
||||
KProcessAddress m_stack_top{};
|
||||
NativeExecutionParameters m_native_execution_parameters{};
|
||||
|
||||
public:
|
||||
using ConditionVariableThreadTreeType = ConditionVariableThreadTree;
|
||||
|
@ -1,8 +1,12 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "common/settings.h"
|
||||
#include "core/arm/dynarmic/arm_dynarmic_32.h"
|
||||
#include "core/arm/dynarmic/arm_dynarmic_64.h"
|
||||
#ifdef HAS_NCE
|
||||
#include "core/arm/nce/arm_nce.h"
|
||||
#endif
|
||||
#include "core/core.h"
|
||||
#include "core/hle/kernel/k_scheduler.h"
|
||||
#include "core/hle/kernel/kernel.h"
|
||||
@ -14,7 +18,8 @@ PhysicalCore::PhysicalCore(std::size_t core_index, Core::System& system, KSchedu
|
||||
: m_core_index{core_index}, m_system{system}, m_scheduler{scheduler} {
|
||||
#if defined(ARCHITECTURE_x86_64) || defined(ARCHITECTURE_arm64)
|
||||
// TODO(bunnei): Initialization relies on a core being available. We may later replace this with
|
||||
// a 32-bit instance of Dynarmic. This should be abstracted out to a CPU manager.
|
||||
// an NCE interface or a 32-bit instance of Dynarmic. This should be abstracted out to a CPU
|
||||
// manager.
|
||||
auto& kernel = system.Kernel();
|
||||
m_arm_interface = std::make_unique<Core::ARM_Dynarmic_64>(
|
||||
system, kernel.IsMulticore(),
|
||||
@ -28,6 +33,13 @@ PhysicalCore::PhysicalCore(std::size_t core_index, Core::System& system, KSchedu
|
||||
PhysicalCore::~PhysicalCore() = default;
|
||||
|
||||
void PhysicalCore::Initialize(bool is_64_bit) {
|
||||
#if defined(HAS_NCE)
|
||||
if (Settings::IsNceEnabled()) {
|
||||
m_arm_interface = std::make_unique<Core::ARM_NCE>(m_system, m_system.Kernel().IsMulticore(),
|
||||
m_core_index);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if defined(ARCHITECTURE_x86_64) || defined(ARCHITECTURE_arm64)
|
||||
auto& kernel = m_system.Kernel();
|
||||
if (!is_64_bit) {
|
||||
|
Reference in New Issue
Block a user