citra-emu
/
citra-canary
Archived
1
0
Fork 0

Merge pull request #2952 from MerryMage/page-tables

Switchable Page Tables
This commit is contained in:
B3n30 2017-09-25 08:29:32 +02:00 committed by GitHub
commit d881dee818
12 changed files with 56 additions and 27 deletions

View File

@ -41,6 +41,9 @@ public:
/// Clear all instruction cache /// Clear all instruction cache
virtual void ClearInstructionCache() = 0; virtual void ClearInstructionCache() = 0;
/// Notify CPU emulation that page tables have changed
virtual void PageTableChanged() = 0;
/** /**
* Set the Program Counter to an address * Set the Program Counter to an address
* @param addr Address to set PC to * @param addr Address to set PC to

View File

@ -41,7 +41,7 @@ static bool IsReadOnlyMemory(u32 vaddr) {
} }
static Dynarmic::UserCallbacks GetUserCallbacks( static Dynarmic::UserCallbacks GetUserCallbacks(
const std::shared_ptr<ARMul_State>& interpeter_state) { const std::shared_ptr<ARMul_State>& interpeter_state, Memory::PageTable* current_page_table) {
Dynarmic::UserCallbacks user_callbacks{}; Dynarmic::UserCallbacks user_callbacks{};
user_callbacks.InterpreterFallback = &InterpreterFallback; user_callbacks.InterpreterFallback = &InterpreterFallback;
user_callbacks.user_arg = static_cast<void*>(interpeter_state.get()); user_callbacks.user_arg = static_cast<void*>(interpeter_state.get());
@ -56,16 +56,14 @@ static Dynarmic::UserCallbacks GetUserCallbacks(
user_callbacks.memory.Write16 = &Memory::Write16; user_callbacks.memory.Write16 = &Memory::Write16;
user_callbacks.memory.Write32 = &Memory::Write32; user_callbacks.memory.Write32 = &Memory::Write32;
user_callbacks.memory.Write64 = &Memory::Write64; user_callbacks.memory.Write64 = &Memory::Write64;
// TODO(Subv): Re-add the page table pointers once dynarmic supports switching page tables at user_callbacks.page_table = &current_page_table->pointers;
// runtime.
user_callbacks.page_table = nullptr;
user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state); user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state);
return user_callbacks; return user_callbacks;
} }
ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) { ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) {
interpreter_state = std::make_shared<ARMul_State>(initial_mode); interpreter_state = std::make_shared<ARMul_State>(initial_mode);
jit = std::make_unique<Dynarmic::Jit>(GetUserCallbacks(interpreter_state)); PageTableChanged();
} }
void ARM_Dynarmic::SetPC(u32 pc) { void ARM_Dynarmic::SetPC(u32 pc) {
@ -136,6 +134,7 @@ void ARM_Dynarmic::AddTicks(u64 ticks) {
MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64)); MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64));
void ARM_Dynarmic::ExecuteInstructions(int num_instructions) { void ARM_Dynarmic::ExecuteInstructions(int num_instructions) {
ASSERT(Memory::GetCurrentPageTable() == current_page_table);
MICROPROFILE_SCOPE(ARM_Jit); MICROPROFILE_SCOPE(ARM_Jit);
std::size_t ticks_executed = jit->Run(static_cast<unsigned>(num_instructions)); std::size_t ticks_executed = jit->Run(static_cast<unsigned>(num_instructions));
@ -178,3 +177,16 @@ void ARM_Dynarmic::PrepareReschedule() {
void ARM_Dynarmic::ClearInstructionCache() { void ARM_Dynarmic::ClearInstructionCache() {
jit->ClearCache(); jit->ClearCache();
} }
void ARM_Dynarmic::PageTableChanged() {
current_page_table = Memory::GetCurrentPageTable();
auto iter = jits.find(current_page_table);
if (iter != jits.end()) {
jit = iter->second.get();
return;
}
jit = new Dynarmic::Jit(GetUserCallbacks(interpreter_state, current_page_table));
jits.emplace(current_page_table, std::unique_ptr<Dynarmic::Jit>(jit));
}

View File

@ -4,12 +4,17 @@
#pragma once #pragma once
#include <map>
#include <memory> #include <memory>
#include <dynarmic/dynarmic.h> #include <dynarmic/dynarmic.h>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/arm/arm_interface.h" #include "core/arm/arm_interface.h"
#include "core/arm/skyeye_common/armstate.h" #include "core/arm/skyeye_common/armstate.h"
namespace Memory {
struct PageTable;
} // namespace Memory
class ARM_Dynarmic final : public ARM_Interface { class ARM_Dynarmic final : public ARM_Interface {
public: public:
ARM_Dynarmic(PrivilegeMode initial_mode); ARM_Dynarmic(PrivilegeMode initial_mode);
@ -36,8 +41,11 @@ public:
void ExecuteInstructions(int num_instructions) override; void ExecuteInstructions(int num_instructions) override;
void ClearInstructionCache() override; void ClearInstructionCache() override;
void PageTableChanged() override;
private: private:
std::unique_ptr<Dynarmic::Jit> jit; Dynarmic::Jit* jit = nullptr;
Memory::PageTable* current_page_table = nullptr;
std::map<Memory::PageTable*, std::unique_ptr<Dynarmic::Jit>> jits;
std::shared_ptr<ARMul_State> interpreter_state; std::shared_ptr<ARMul_State> interpreter_state;
}; };

View File

@ -25,6 +25,10 @@ void ARM_DynCom::ClearInstructionCache() {
trans_cache_buf_top = 0; trans_cache_buf_top = 0;
} }
void ARM_DynCom::PageTableChanged() {
ClearInstructionCache();
}
void ARM_DynCom::SetPC(u32 pc) { void ARM_DynCom::SetPC(u32 pc) {
state->Reg[15] = pc; state->Reg[15] = pc;
} }

View File

@ -16,6 +16,7 @@ public:
~ARM_DynCom(); ~ARM_DynCom();
void ClearInstructionCache() override; void ClearInstructionCache() override;
void PageTableChanged() override;
void SetPC(u32 pc) override; void SetPC(u32 pc) override;
u32 GetPC() const override; u32 GetPC() const override;

View File

@ -178,16 +178,13 @@ static void SwitchContext(Thread* new_thread) {
ready_queue.remove(new_thread->current_priority, new_thread); ready_queue.remove(new_thread->current_priority, new_thread);
new_thread->status = THREADSTATUS_RUNNING; new_thread->status = THREADSTATUS_RUNNING;
Core::CPU().LoadContext(new_thread->context);
Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
if (previous_process != current_thread->owner_process) { if (previous_process != current_thread->owner_process) {
Kernel::g_current_process = current_thread->owner_process; Kernel::g_current_process = current_thread->owner_process;
Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
// We have switched processes and thus, page tables, clear the instruction cache so we
// don't keep stale data from the previous process.
Core::CPU().ClearInstructionCache();
} }
Core::CPU().LoadContext(new_thread->context);
Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
} else { } else {
current_thread = nullptr; current_thread = nullptr;
// Note: We do not reset the current process and current page table when idling because // Note: We do not reset the current process and current page table when idling because

View File

@ -270,7 +270,7 @@ ResultStatus AppLoader_THREEDSX::Load() {
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings; Kernel::g_current_process->address_mappings = default_address_mappings;
Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
// Attach the default resource limit (APPLICATION) to the process // Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit = Kernel::g_current_process->resource_limit =

View File

@ -397,7 +397,7 @@ ResultStatus AppLoader_ELF::Load() {
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings; Kernel::g_current_process->address_mappings = default_address_mappings;
Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
// Attach the default resource limit (APPLICATION) to the process // Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit = Kernel::g_current_process->resource_limit =

View File

@ -108,7 +108,7 @@ ResultStatus AppLoader_NCCH::LoadExec() {
codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
// Attach a resource limit to the process based on the resource limit category // Attach a resource limit to the process based on the resource limit category
Kernel::g_current_process->resource_limit = Kernel::g_current_process->resource_limit =

View File

@ -9,6 +9,8 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/arm/arm_interface.h"
#include "core/core.h"
#include "core/hle/kernel/memory.h" #include "core/hle/kernel/memory.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "core/hle/lock.h" #include "core/hle/lock.h"
@ -22,10 +24,17 @@ namespace Memory {
static std::array<u8, Memory::VRAM_SIZE> vram; static std::array<u8, Memory::VRAM_SIZE> vram;
static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram; static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram;
PageTable* current_page_table = nullptr; static PageTable* current_page_table = nullptr;
std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers() { void SetCurrentPageTable(PageTable* page_table) {
return &current_page_table->pointers; current_page_table = page_table;
if (Core::System::GetInstance().IsPoweredOn()) {
Core::CPU().PageTableChanged();
}
}
PageTable* GetCurrentPageTable() {
return current_page_table;
} }
static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) { static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) {

View File

@ -182,7 +182,8 @@ enum : VAddr {
}; };
/// Currently active page table /// Currently active page table
extern PageTable* current_page_table; void SetCurrentPageTable(PageTable* page_table);
PageTable* GetCurrentPageTable();
bool IsValidVirtualAddress(const VAddr addr); bool IsValidVirtualAddress(const VAddr addr);
bool IsValidPhysicalAddress(const PAddr addr); bool IsValidPhysicalAddress(const PAddr addr);
@ -259,10 +260,4 @@ enum class FlushMode {
*/ */
void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode); void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode);
/**
* Dynarmic has an optimization to memory accesses when the pointer to the page exists that
* can be used by setting up the current page table as a callback. This function is used to
* retrieve the current page table for that purpose.
*/
std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers();
} // namespace Memory } // namespace Memory

View File

@ -21,7 +21,7 @@ TestEnvironment::TestEnvironment(bool mutable_memory_)
Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory); Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory);
Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory); Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory);
Memory::current_page_table = &page_table; Memory::SetCurrentPageTable(&page_table);
} }
TestEnvironment::~TestEnvironment() { TestEnvironment::~TestEnvironment() {