summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar B3n302017-09-25 08:29:32 +0200
committerGravatar GitHub2017-09-25 08:29:32 +0200
commitd881dee818e7e59b72cb11cea634eb70bdcd3d35 (patch)
tree4f9b241fb63b90647e2992e83d83de7d25bb5d42 /src
parentMerge pull request #2951 from huwpascoe/perf-4 (diff)
parentARM_Interface: Implement PageTableChanged (diff)
downloadyuzu-d881dee818e7e59b72cb11cea634eb70bdcd3d35.tar.gz
yuzu-d881dee818e7e59b72cb11cea634eb70bdcd3d35.tar.xz
yuzu-d881dee818e7e59b72cb11cea634eb70bdcd3d35.zip
Merge pull request #2952 from MerryMage/page-tables
Switchable Page Tables
Diffstat (limited to 'src')
-rw-r--r--src/core/arm/arm_interface.h3
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.cpp22
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.h10
-rw-r--r--src/core/arm/dyncom/arm_dyncom.cpp4
-rw-r--r--src/core/arm/dyncom/arm_dyncom.h1
-rw-r--r--src/core/hle/kernel/thread.cpp11
-rw-r--r--src/core/loader/3dsx.cpp2
-rw-r--r--src/core/loader/elf.cpp2
-rw-r--r--src/core/loader/ncch.cpp2
-rw-r--r--src/core/memory.cpp15
-rw-r--r--src/core/memory.h9
-rw-r--r--src/tests/core/arm/arm_test_common.cpp2
12 files changed, 56 insertions, 27 deletions
diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h
index ccd43f431..2aa017a54 100644
--- a/src/core/arm/arm_interface.h
+++ b/src/core/arm/arm_interface.h
@@ -41,6 +41,9 @@ public:
41 /// Clear all instruction cache 41 /// Clear all instruction cache
42 virtual void ClearInstructionCache() = 0; 42 virtual void ClearInstructionCache() = 0;
43 43
44 /// Notify CPU emulation that page tables have changed
45 virtual void PageTableChanged() = 0;
46
44 /** 47 /**
45 * Set the Program Counter to an address 48 * Set the Program Counter to an address
46 * @param addr Address to set PC to 49 * @param addr Address to set PC to
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp
index 34c5aa381..42ae93ae8 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.cpp
+++ b/src/core/arm/dynarmic/arm_dynarmic.cpp
@@ -41,7 +41,7 @@ static bool IsReadOnlyMemory(u32 vaddr) {
41} 41}
42 42
43static Dynarmic::UserCallbacks GetUserCallbacks( 43static Dynarmic::UserCallbacks GetUserCallbacks(
44 const std::shared_ptr<ARMul_State>& interpeter_state) { 44 const std::shared_ptr<ARMul_State>& interpeter_state, Memory::PageTable* current_page_table) {
45 Dynarmic::UserCallbacks user_callbacks{}; 45 Dynarmic::UserCallbacks user_callbacks{};
46 user_callbacks.InterpreterFallback = &InterpreterFallback; 46 user_callbacks.InterpreterFallback = &InterpreterFallback;
47 user_callbacks.user_arg = static_cast<void*>(interpeter_state.get()); 47 user_callbacks.user_arg = static_cast<void*>(interpeter_state.get());
@@ -56,16 +56,14 @@ static Dynarmic::UserCallbacks GetUserCallbacks(
56 user_callbacks.memory.Write16 = &Memory::Write16; 56 user_callbacks.memory.Write16 = &Memory::Write16;
57 user_callbacks.memory.Write32 = &Memory::Write32; 57 user_callbacks.memory.Write32 = &Memory::Write32;
58 user_callbacks.memory.Write64 = &Memory::Write64; 58 user_callbacks.memory.Write64 = &Memory::Write64;
59 // TODO(Subv): Re-add the page table pointers once dynarmic supports switching page tables at 59 user_callbacks.page_table = &current_page_table->pointers;
60 // runtime.
61 user_callbacks.page_table = nullptr;
62 user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state); 60 user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state);
63 return user_callbacks; 61 return user_callbacks;
64} 62}
65 63
66ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) { 64ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) {
67 interpreter_state = std::make_shared<ARMul_State>(initial_mode); 65 interpreter_state = std::make_shared<ARMul_State>(initial_mode);
68 jit = std::make_unique<Dynarmic::Jit>(GetUserCallbacks(interpreter_state)); 66 PageTableChanged();
69} 67}
70 68
71void ARM_Dynarmic::SetPC(u32 pc) { 69void ARM_Dynarmic::SetPC(u32 pc) {
@@ -136,6 +134,7 @@ void ARM_Dynarmic::AddTicks(u64 ticks) {
136MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64)); 134MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64));
137 135
138void ARM_Dynarmic::ExecuteInstructions(int num_instructions) { 136void ARM_Dynarmic::ExecuteInstructions(int num_instructions) {
137 ASSERT(Memory::GetCurrentPageTable() == current_page_table);
139 MICROPROFILE_SCOPE(ARM_Jit); 138 MICROPROFILE_SCOPE(ARM_Jit);
140 139
141 std::size_t ticks_executed = jit->Run(static_cast<unsigned>(num_instructions)); 140 std::size_t ticks_executed = jit->Run(static_cast<unsigned>(num_instructions));
@@ -178,3 +177,16 @@ void ARM_Dynarmic::PrepareReschedule() {
178void ARM_Dynarmic::ClearInstructionCache() { 177void ARM_Dynarmic::ClearInstructionCache() {
179 jit->ClearCache(); 178 jit->ClearCache();
180} 179}
180
181void ARM_Dynarmic::PageTableChanged() {
182 current_page_table = Memory::GetCurrentPageTable();
183
184 auto iter = jits.find(current_page_table);
185 if (iter != jits.end()) {
186 jit = iter->second.get();
187 return;
188 }
189
190 jit = new Dynarmic::Jit(GetUserCallbacks(interpreter_state, current_page_table));
191 jits.emplace(current_page_table, std::unique_ptr<Dynarmic::Jit>(jit));
192}
diff --git a/src/core/arm/dynarmic/arm_dynarmic.h b/src/core/arm/dynarmic/arm_dynarmic.h
index 834dc989e..96148a1a5 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.h
+++ b/src/core/arm/dynarmic/arm_dynarmic.h
@@ -4,12 +4,17 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <map>
7#include <memory> 8#include <memory>
8#include <dynarmic/dynarmic.h> 9#include <dynarmic/dynarmic.h>
9#include "common/common_types.h" 10#include "common/common_types.h"
10#include "core/arm/arm_interface.h" 11#include "core/arm/arm_interface.h"
11#include "core/arm/skyeye_common/armstate.h" 12#include "core/arm/skyeye_common/armstate.h"
12 13
14namespace Memory {
15struct PageTable;
16} // namespace Memory
17
13class ARM_Dynarmic final : public ARM_Interface { 18class ARM_Dynarmic final : public ARM_Interface {
14public: 19public:
15 ARM_Dynarmic(PrivilegeMode initial_mode); 20 ARM_Dynarmic(PrivilegeMode initial_mode);
@@ -36,8 +41,11 @@ public:
36 void ExecuteInstructions(int num_instructions) override; 41 void ExecuteInstructions(int num_instructions) override;
37 42
38 void ClearInstructionCache() override; 43 void ClearInstructionCache() override;
44 void PageTableChanged() override;
39 45
40private: 46private:
41 std::unique_ptr<Dynarmic::Jit> jit; 47 Dynarmic::Jit* jit = nullptr;
48 Memory::PageTable* current_page_table = nullptr;
49 std::map<Memory::PageTable*, std::unique_ptr<Dynarmic::Jit>> jits;
42 std::shared_ptr<ARMul_State> interpreter_state; 50 std::shared_ptr<ARMul_State> interpreter_state;
43}; 51};
diff --git a/src/core/arm/dyncom/arm_dyncom.cpp b/src/core/arm/dyncom/arm_dyncom.cpp
index 81f9bf99e..da955c9b9 100644
--- a/src/core/arm/dyncom/arm_dyncom.cpp
+++ b/src/core/arm/dyncom/arm_dyncom.cpp
@@ -25,6 +25,10 @@ void ARM_DynCom::ClearInstructionCache() {
25 trans_cache_buf_top = 0; 25 trans_cache_buf_top = 0;
26} 26}
27 27
28void ARM_DynCom::PageTableChanged() {
29 ClearInstructionCache();
30}
31
28void ARM_DynCom::SetPC(u32 pc) { 32void ARM_DynCom::SetPC(u32 pc) {
29 state->Reg[15] = pc; 33 state->Reg[15] = pc;
30} 34}
diff --git a/src/core/arm/dyncom/arm_dyncom.h b/src/core/arm/dyncom/arm_dyncom.h
index 62c174f3c..0ae535671 100644
--- a/src/core/arm/dyncom/arm_dyncom.h
+++ b/src/core/arm/dyncom/arm_dyncom.h
@@ -16,6 +16,7 @@ public:
16 ~ARM_DynCom(); 16 ~ARM_DynCom();
17 17
18 void ClearInstructionCache() override; 18 void ClearInstructionCache() override;
19 void PageTableChanged() override;
19 20
20 void SetPC(u32 pc) override; 21 void SetPC(u32 pc) override;
21 u32 GetPC() const override; 22 u32 GetPC() const override;
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 324415a36..61378211f 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -178,16 +178,13 @@ static void SwitchContext(Thread* new_thread) {
178 ready_queue.remove(new_thread->current_priority, new_thread); 178 ready_queue.remove(new_thread->current_priority, new_thread);
179 new_thread->status = THREADSTATUS_RUNNING; 179 new_thread->status = THREADSTATUS_RUNNING;
180 180
181 Core::CPU().LoadContext(new_thread->context);
182 Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
183
184 if (previous_process != current_thread->owner_process) { 181 if (previous_process != current_thread->owner_process) {
185 Kernel::g_current_process = current_thread->owner_process; 182 Kernel::g_current_process = current_thread->owner_process;
186 Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; 183 SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
187 // We have switched processes and thus, page tables, clear the instruction cache so we
188 // don't keep stale data from the previous process.
189 Core::CPU().ClearInstructionCache();
190 } 184 }
185
186 Core::CPU().LoadContext(new_thread->context);
187 Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
191 } else { 188 } else {
192 current_thread = nullptr; 189 current_thread = nullptr;
193 // Note: We do not reset the current process and current page table when idling because 190 // Note: We do not reset the current process and current page table when idling because
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 69cdc0867..a03515e6e 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -270,7 +270,7 @@ ResultStatus AppLoader_THREEDSX::Load() {
270 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); 270 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
271 Kernel::g_current_process->svc_access_mask.set(); 271 Kernel::g_current_process->svc_access_mask.set();
272 Kernel::g_current_process->address_mappings = default_address_mappings; 272 Kernel::g_current_process->address_mappings = default_address_mappings;
273 Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; 273 Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
274 274
275 // Attach the default resource limit (APPLICATION) to the process 275 // Attach the default resource limit (APPLICATION) to the process
276 Kernel::g_current_process->resource_limit = 276 Kernel::g_current_process->resource_limit =
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index 2f27606a1..2de1f4e81 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -397,7 +397,7 @@ ResultStatus AppLoader_ELF::Load() {
397 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); 397 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
398 Kernel::g_current_process->svc_access_mask.set(); 398 Kernel::g_current_process->svc_access_mask.set();
399 Kernel::g_current_process->address_mappings = default_address_mappings; 399 Kernel::g_current_process->address_mappings = default_address_mappings;
400 Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; 400 Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
401 401
402 // Attach the default resource limit (APPLICATION) to the process 402 // Attach the default resource limit (APPLICATION) to the process
403 Kernel::g_current_process->resource_limit = 403 Kernel::g_current_process->resource_limit =
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index bef7fa567..c46d7cfc6 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -108,7 +108,7 @@ ResultStatus AppLoader_NCCH::LoadExec() {
108 codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); 108 codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
109 109
110 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); 110 Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
111 Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table; 111 Memory::SetCurrentPageTable(&Kernel::g_current_process->vm_manager.page_table);
112 112
113 // Attach a resource limit to the process based on the resource limit category 113 // Attach a resource limit to the process based on the resource limit category
114 Kernel::g_current_process->resource_limit = 114 Kernel::g_current_process->resource_limit =
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 68a6b1ac2..a6b5f6c99 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -9,6 +9,8 @@
9#include "common/common_types.h" 9#include "common/common_types.h"
10#include "common/logging/log.h" 10#include "common/logging/log.h"
11#include "common/swap.h" 11#include "common/swap.h"
12#include "core/arm/arm_interface.h"
13#include "core/core.h"
12#include "core/hle/kernel/memory.h" 14#include "core/hle/kernel/memory.h"
13#include "core/hle/kernel/process.h" 15#include "core/hle/kernel/process.h"
14#include "core/hle/lock.h" 16#include "core/hle/lock.h"
@@ -22,10 +24,17 @@ namespace Memory {
22static std::array<u8, Memory::VRAM_SIZE> vram; 24static std::array<u8, Memory::VRAM_SIZE> vram;
23static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram; 25static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram;
24 26
25PageTable* current_page_table = nullptr; 27static PageTable* current_page_table = nullptr;
26 28
27std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers() { 29void SetCurrentPageTable(PageTable* page_table) {
28 return &current_page_table->pointers; 30 current_page_table = page_table;
31 if (Core::System::GetInstance().IsPoweredOn()) {
32 Core::CPU().PageTableChanged();
33 }
34}
35
36PageTable* GetCurrentPageTable() {
37 return current_page_table;
29} 38}
30 39
31static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) { 40static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) {
diff --git a/src/core/memory.h b/src/core/memory.h
index b228a48c2..1865bfea0 100644
--- a/src/core/memory.h
+++ b/src/core/memory.h
@@ -182,7 +182,8 @@ enum : VAddr {
182}; 182};
183 183
184/// Currently active page table 184/// Currently active page table
185extern PageTable* current_page_table; 185void SetCurrentPageTable(PageTable* page_table);
186PageTable* GetCurrentPageTable();
186 187
187bool IsValidVirtualAddress(const VAddr addr); 188bool IsValidVirtualAddress(const VAddr addr);
188bool IsValidPhysicalAddress(const PAddr addr); 189bool IsValidPhysicalAddress(const PAddr addr);
@@ -259,10 +260,4 @@ enum class FlushMode {
259 */ 260 */
260void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode); 261void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode);
261 262
262/**
263 * Dynarmic has an optimization to memory accesses when the pointer to the page exists that
264 * can be used by setting up the current page table as a callback. This function is used to
265 * retrieve the current page table for that purpose.
266 */
267std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers();
268} // namespace Memory 263} // namespace Memory
diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp
index 8384ce744..cfe0d503a 100644
--- a/src/tests/core/arm/arm_test_common.cpp
+++ b/src/tests/core/arm/arm_test_common.cpp
@@ -21,7 +21,7 @@ TestEnvironment::TestEnvironment(bool mutable_memory_)
21 Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory); 21 Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory);
22 Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory); 22 Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory);
23 23
24 Memory::current_page_table = &page_table; 24 Memory::SetCurrentPageTable(&page_table);
25} 25}
26 26
27TestEnvironment::~TestEnvironment() { 27TestEnvironment::~TestEnvironment() {