diff options
| author | 2018-01-04 00:13:23 -0500 | |
|---|---|---|
| committer | 2018-01-04 00:13:23 -0500 | |
| commit | 3f8b9181b553e8af9bd93899832ddc2c8cf73e8b (patch) | |
| tree | 00df941867db6c14f172f6eb4ed99964ac3bc5b3 | |
| parent | DownloadExternals: Use yuzu repo. (diff) | |
| download | yuzu-3f8b9181b553e8af9bd93899832ddc2c8cf73e8b.tar.gz yuzu-3f8b9181b553e8af9bd93899832ddc2c8cf73e8b.tar.xz yuzu-3f8b9181b553e8af9bd93899832ddc2c8cf73e8b.zip | |
unicorn: Use for arm interface on Windows.
| -rw-r--r-- | CMakeLists.txt | 31 | ||||
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/arm/unicorn/arm_unicorn.cpp | 201 | ||||
| -rw-r--r-- | src/core/arm/unicorn/arm_unicorn.h | 39 | ||||
| -rw-r--r-- | src/core/core.cpp | 9 |
5 files changed, 273 insertions, 9 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d9c2f78a2..c45c830c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -12,6 +12,8 @@ option(CITRA_USE_BUNDLED_SDL2 "Download bundled SDL2 binaries" OFF) | |||
| 12 | option(ENABLE_QT "Enable the Qt frontend" ON) | 12 | option(ENABLE_QT "Enable the Qt frontend" ON) |
| 13 | option(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" OFF) | 13 | option(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" OFF) |
| 14 | 14 | ||
| 15 | option(YUZU_USE_BUNDLED_UNICORN "Download bundled Unicorn binaries" OFF) | ||
| 16 | |||
| 15 | option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON) | 17 | option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON) |
| 16 | option(CITRA_USE_BUNDLED_CURL "FOR MINGW ONLY: Download curl configured against winssl instead of openssl" OFF) | 18 | option(CITRA_USE_BUNDLED_CURL "FOR MINGW ONLY: Download curl configured against winssl instead of openssl" OFF) |
| 17 | if (ENABLE_WEB_SERVICE AND CITRA_USE_BUNDLED_CURL AND WINDOWS AND MSVC) | 19 | if (ENABLE_WEB_SERVICE AND CITRA_USE_BUNDLED_CURL AND WINDOWS AND MSVC) |
| @@ -210,6 +212,35 @@ else() | |||
| 210 | set(SDL2_FOUND NO) | 212 | set(SDL2_FOUND NO) |
| 211 | endif() | 213 | endif() |
| 212 | 214 | ||
| 215 | if (YUZU_USE_BUNDLED_UNICORN) | ||
| 216 | # Detect toolchain and platform | ||
| 217 | if (MSVC14 AND ARCHITECTURE_x86_64) | ||
| 218 | set(UNICORN_VER "unicorn-1.0.1-yuzu") | ||
| 219 | else() | ||
| 220 | message(FATAL_ERROR "No bundled Unicorn binaries for your toolchain. Disable YUZU_USE_BUNDLED_UNICORN and provide your own.") | ||
| 221 | endif() | ||
| 222 | |||
| 223 | if (DEFINED UNICORN_VER) | ||
| 224 | download_bundled_external("unicorn/" ${UNICORN_VER} UNICORN_PREFIX) | ||
| 225 | endif() | ||
| 226 | |||
| 227 | if (DEFINED UNICORN_VER) | ||
| 228 | download_bundled_external("unicorn/" ${UNICORN_VER} UNICORN_PREFIX) | ||
| 229 | endif() | ||
| 230 | |||
| 231 | set(UNICORN_FOUND YES) | ||
| 232 | set(UNICORN_INCLUDE_DIR "${UNICORN_PREFIX}/include" CACHE PATH "Path to Unicorn headers") | ||
| 233 | set(UNICORN_LIBRARY "${UNICORN_PREFIX}/lib/x64/unicorn_static.lib" CACHE PATH "Path to Unicorn library") | ||
| 234 | else() | ||
| 235 | find_package(unicorn REQUIRED) | ||
| 236 | endif() | ||
| 237 | |||
| 238 | if (UNICORN_FOUND) | ||
| 239 | add_library(unicorn INTERFACE) | ||
| 240 | target_link_libraries(unicorn INTERFACE "${UNICORN_LIBRARY}") | ||
| 241 | target_include_directories(unicorn INTERFACE "${UNICORN_INCLUDE_DIR}") | ||
| 242 | endif() | ||
| 243 | |||
| 213 | if (ENABLE_QT) | 244 | if (ENABLE_QT) |
| 214 | if (CITRA_USE_BUNDLED_QT) | 245 | if (CITRA_USE_BUNDLED_QT) |
| 215 | if (MSVC14 AND ARCHITECTURE_x86_64) | 246 | if (MSVC14 AND ARCHITECTURE_x86_64) |
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 3f47cd286..f3ed33cfb 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -158,7 +158,7 @@ set(HEADERS | |||
| 158 | create_directory_groups(${SRCS} ${HEADERS}) | 158 | create_directory_groups(${SRCS} ${HEADERS}) |
| 159 | add_library(core STATIC ${SRCS} ${HEADERS}) | 159 | add_library(core STATIC ${SRCS} ${HEADERS}) |
| 160 | target_link_libraries(core PUBLIC common PRIVATE audio_core network video_core) | 160 | target_link_libraries(core PUBLIC common PRIVATE audio_core network video_core) |
| 161 | target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp dynarmic fmt lz4_static) | 161 | target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp dynarmic fmt lz4_static unicorn) |
| 162 | if (ENABLE_WEB_SERVICE) | 162 | if (ENABLE_WEB_SERVICE) |
| 163 | target_link_libraries(core PUBLIC json-headers web_service) | 163 | target_link_libraries(core PUBLIC json-headers web_service) |
| 164 | endif() | 164 | endif() |
diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp new file mode 100644 index 000000000..92aef0654 --- /dev/null +++ b/src/core/arm/unicorn/arm_unicorn.cpp | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | // Copyright 2018 Yuzu Emulator Team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <unicorn/arm64.h> | ||
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/microprofile.h" | ||
| 8 | #include "core/arm/unicorn/arm_unicorn.h" | ||
| 9 | #include "core/core.h" | ||
| 10 | #include "core/core_timing.h" | ||
| 11 | #include "core/hle/kernel/svc.h" | ||
| 12 | |||
| 13 | #define CHECKED(expr) \ | ||
| 14 | do { \ | ||
| 15 | if (auto _cerr = (expr)) { \ | ||
| 16 | ASSERT_MSG(false, "Call " #expr " failed with error: %u (%s)\n", _cerr, \ | ||
| 17 | uc_strerror(_cerr)); \ | ||
| 18 | } \ | ||
| 19 | } while (0) | ||
| 20 | |||
| 21 | static void InterruptHook(uc_engine* uc, u32 intNo, void* user_data) { | ||
| 22 | u32 esr{}; | ||
| 23 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_ESR, &esr)); | ||
| 24 | |||
| 25 | auto ec = esr >> 26; | ||
| 26 | auto iss = esr & 0xFFFFFF; | ||
| 27 | |||
| 28 | switch (ec) { | ||
| 29 | case 0x15: // SVC | ||
| 30 | Kernel::CallSVC(iss); | ||
| 31 | break; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | static bool UnmappedMemoryHook(uc_engine* uc, uc_mem_type type, u64 addr, int size, u64 value, | ||
| 36 | void* user_data) { | ||
| 37 | ARM_Interface::ThreadContext ctx{}; | ||
| 38 | Core::CPU().SaveContext(ctx); | ||
| 39 | ASSERT_MSG(false, "Attempted to read from unmapped memory"); | ||
| 40 | return {}; | ||
| 41 | } | ||
| 42 | |||
| 43 | ARM_Unicorn::ARM_Unicorn() { | ||
| 44 | CHECKED(uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc)); | ||
| 45 | |||
| 46 | auto fpv = 3 << 20; | ||
| 47 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_CPACR_EL1, &fpv)); | ||
| 48 | |||
| 49 | uc_hook hook{}; | ||
| 50 | CHECKED(uc_hook_add(uc, &hook, UC_HOOK_INTR, (void*)InterruptHook, this, 0, -1)); | ||
| 51 | CHECKED(uc_hook_add(uc, &hook, UC_HOOK_MEM_INVALID, (void*)UnmappedMemoryHook, this, 0, -1)); | ||
| 52 | } | ||
| 53 | |||
| 54 | ARM_Unicorn::~ARM_Unicorn() { | ||
| 55 | CHECKED(uc_close(uc)); | ||
| 56 | } | ||
| 57 | |||
| 58 | void ARM_Unicorn::MapBackingMemory(VAddr address, size_t size, u8* memory, | ||
| 59 | Kernel::VMAPermission perms) { | ||
| 60 | CHECKED(uc_mem_map_ptr(uc, address, size, static_cast<u32>(perms), memory)); | ||
| 61 | } | ||
| 62 | |||
| 63 | void ARM_Unicorn::SetPC(u64 pc) { | ||
| 64 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_PC, &pc)); | ||
| 65 | } | ||
| 66 | |||
| 67 | u64 ARM_Unicorn::GetPC() const { | ||
| 68 | u64 val{}; | ||
| 69 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_PC, &val)); | ||
| 70 | return val; | ||
| 71 | } | ||
| 72 | |||
| 73 | u64 ARM_Unicorn::GetReg(int regn) const { | ||
| 74 | u64 val{}; | ||
| 75 | auto treg = UC_ARM64_REG_SP; | ||
| 76 | if (regn <= 28) { | ||
| 77 | treg = (uc_arm64_reg)(UC_ARM64_REG_X0 + regn); | ||
| 78 | } else if (regn < 31) { | ||
| 79 | treg = (uc_arm64_reg)(UC_ARM64_REG_X29 + regn - 29); | ||
| 80 | } | ||
| 81 | CHECKED(uc_reg_read(uc, treg, &val)); | ||
| 82 | return val; | ||
| 83 | } | ||
| 84 | |||
| 85 | void ARM_Unicorn::SetReg(int regn, u64 val) { | ||
| 86 | auto treg = UC_ARM64_REG_SP; | ||
| 87 | if (regn <= 28) { | ||
| 88 | treg = (uc_arm64_reg)(UC_ARM64_REG_X0 + regn); | ||
| 89 | } else if (regn < 31) { | ||
| 90 | treg = (uc_arm64_reg)(UC_ARM64_REG_X29 + regn - 29); | ||
| 91 | } | ||
| 92 | CHECKED(uc_reg_write(uc, treg, &val)); | ||
| 93 | } | ||
| 94 | |||
| 95 | const u128& ARM_Unicorn::GetExtReg(int /*index*/) const { | ||
| 96 | UNIMPLEMENTED(); | ||
| 97 | static constexpr u128 res{}; | ||
| 98 | return res; | ||
| 99 | } | ||
| 100 | |||
| 101 | void ARM_Unicorn::SetExtReg(int /*index*/, u128& /*value*/) { | ||
| 102 | UNIMPLEMENTED(); | ||
| 103 | } | ||
| 104 | |||
| 105 | u32 ARM_Unicorn::GetVFPReg(int /*index*/) const { | ||
| 106 | UNIMPLEMENTED(); | ||
| 107 | return {}; | ||
| 108 | } | ||
| 109 | |||
| 110 | void ARM_Unicorn::SetVFPReg(int /*index*/, u32 /*value*/) { | ||
| 111 | UNIMPLEMENTED(); | ||
| 112 | } | ||
| 113 | |||
| 114 | u32 ARM_Unicorn::GetCPSR() const { | ||
| 115 | u64 nzcv{}; | ||
| 116 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_NZCV, &nzcv)); | ||
| 117 | return static_cast<u32>(nzcv); | ||
| 118 | } | ||
| 119 | |||
| 120 | void ARM_Unicorn::SetCPSR(u32 cpsr) { | ||
| 121 | u64 nzcv = cpsr; | ||
| 122 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_NZCV, &nzcv)); | ||
| 123 | } | ||
| 124 | |||
| 125 | VAddr ARM_Unicorn::GetTlsAddress() const { | ||
| 126 | u64 base{}; | ||
| 127 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_TPIDRRO_EL0, &base)); | ||
| 128 | return base; | ||
| 129 | } | ||
| 130 | |||
| 131 | void ARM_Unicorn::SetTlsAddress(VAddr base) { | ||
| 132 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_TPIDRRO_EL0, &base)); | ||
| 133 | } | ||
| 134 | |||
| 135 | MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64)); | ||
| 136 | |||
| 137 | void ARM_Unicorn::ExecuteInstructions(int num_instructions) { | ||
| 138 | MICROPROFILE_SCOPE(ARM_Jit); | ||
| 139 | CHECKED(uc_emu_start(uc, GetPC(), 1ULL << 63, 0, num_instructions)); | ||
| 140 | CoreTiming::AddTicks(num_instructions); | ||
| 141 | } | ||
| 142 | |||
| 143 | void ARM_Unicorn::SaveContext(ARM_Interface::ThreadContext& ctx) { | ||
| 144 | int uregs[32]; | ||
| 145 | void* tregs[32]; | ||
| 146 | |||
| 147 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_SP, &ctx.sp)); | ||
| 148 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_PC, &ctx.pc)); | ||
| 149 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_NZCV, &ctx.cpsr)); | ||
| 150 | |||
| 151 | for (auto i = 0; i < 29; ++i) { | ||
| 152 | uregs[i] = UC_ARM64_REG_X0 + i; | ||
| 153 | tregs[i] = &ctx.cpu_registers[i]; | ||
| 154 | } | ||
| 155 | |||
| 156 | CHECKED(uc_reg_read_batch(uc, uregs, tregs, 29)); | ||
| 157 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_X29, &ctx.cpu_registers[29])); | ||
| 158 | CHECKED(uc_reg_read(uc, UC_ARM64_REG_X30, &ctx.lr)); | ||
| 159 | |||
| 160 | ctx.tls_address = GetTlsAddress(); | ||
| 161 | |||
| 162 | for (int i = 0; i < 32; ++i) { | ||
| 163 | uregs[i] = UC_ARM64_REG_Q0 + i; | ||
| 164 | tregs[i] = &ctx.fpu_registers[i]; | ||
| 165 | } | ||
| 166 | |||
| 167 | CHECKED(uc_reg_read_batch(uc, uregs, tregs, 32)); | ||
| 168 | } | ||
| 169 | |||
| 170 | void ARM_Unicorn::LoadContext(const ARM_Interface::ThreadContext& ctx) { | ||
| 171 | int uregs[32]; | ||
| 172 | void* tregs[32]; | ||
| 173 | |||
| 174 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_SP, &ctx.sp)); | ||
| 175 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_PC, &ctx.pc)); | ||
| 176 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_NZCV, &ctx.cpsr)); | ||
| 177 | |||
| 178 | for (int i = 0; i < 29; ++i) { | ||
| 179 | uregs[i] = UC_ARM64_REG_X0 + i; | ||
| 180 | tregs[i] = (void*)&ctx.cpu_registers[i]; | ||
| 181 | } | ||
| 182 | |||
| 183 | CHECKED(uc_reg_write_batch(uc, uregs, tregs, 29)); | ||
| 184 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_X29, &ctx.cpu_registers[29])); | ||
| 185 | CHECKED(uc_reg_write(uc, UC_ARM64_REG_X30, &ctx.lr)); | ||
| 186 | |||
| 187 | SetTlsAddress(ctx.tls_address); | ||
| 188 | |||
| 189 | for (auto i = 0; i < 32; ++i) { | ||
| 190 | uregs[i] = UC_ARM64_REG_Q0 + i; | ||
| 191 | tregs[i] = (void*)&ctx.fpu_registers[i]; | ||
| 192 | } | ||
| 193 | |||
| 194 | CHECKED(uc_reg_write_batch(uc, uregs, tregs, 32)); | ||
| 195 | } | ||
| 196 | |||
| 197 | void ARM_Unicorn::PrepareReschedule() { | ||
| 198 | CHECKED(uc_emu_stop(uc)); | ||
| 199 | } | ||
| 200 | |||
| 201 | void ARM_Unicorn::ClearInstructionCache() {} | ||
diff --git a/src/core/arm/unicorn/arm_unicorn.h b/src/core/arm/unicorn/arm_unicorn.h new file mode 100644 index 000000000..90db3eb7f --- /dev/null +++ b/src/core/arm/unicorn/arm_unicorn.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | // Copyright 2018 Yuzu Emulator Team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <unicorn/unicorn.h> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "core/arm/arm_interface.h" | ||
| 10 | |||
| 11 | class ARM_Unicorn final : public ARM_Interface { | ||
| 12 | |||
| 13 | public: | ||
| 14 | ARM_Unicorn(); | ||
| 15 | ~ARM_Unicorn(); | ||
| 16 | void MapBackingMemory(VAddr address, size_t size, u8* memory, | ||
| 17 | Kernel::VMAPermission perms) override; | ||
| 18 | void SetPC(u64 pc) override; | ||
| 19 | u64 GetPC() const override; | ||
| 20 | u64 GetReg(int index) const override; | ||
| 21 | void SetReg(int index, u64 value) override; | ||
| 22 | const u128& GetExtReg(int index) const override; | ||
| 23 | void SetExtReg(int index, u128& value) override; | ||
| 24 | u32 GetVFPReg(int index) const override; | ||
| 25 | void SetVFPReg(int index, u32 value) override; | ||
| 26 | u32 GetCPSR() const override; | ||
| 27 | void SetCPSR(u32 cpsr) override; | ||
| 28 | VAddr GetTlsAddress() const override; | ||
| 29 | void SetTlsAddress(VAddr address) override; | ||
| 30 | void SaveContext(ThreadContext& ctx) override; | ||
| 31 | void LoadContext(const ThreadContext& ctx) override; | ||
| 32 | void PrepareReschedule() override; | ||
| 33 | void ExecuteInstructions(int num_instructions) override; | ||
| 34 | void ClearInstructionCache() override; | ||
| 35 | void PageTableChanged() override{}; | ||
| 36 | |||
| 37 | private: | ||
| 38 | uc_engine* uc{}; | ||
| 39 | }; | ||
diff --git a/src/core/core.cpp b/src/core/core.cpp index 886cb0972..d7e2450ff 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp | |||
| @@ -6,8 +6,6 @@ | |||
| 6 | #include <utility> | 6 | #include <utility> |
| 7 | #include "audio_core/audio_core.h" | 7 | #include "audio_core/audio_core.h" |
| 8 | #include "common/logging/log.h" | 8 | #include "common/logging/log.h" |
| 9 | #include "core/arm/arm_interface.h" | ||
| 10 | #include "core/arm/dynarmic/arm_dynarmic.h" | ||
| 11 | #include "core/arm/unicorn/arm_unicorn.h" | 9 | #include "core/arm/unicorn/arm_unicorn.h" |
| 12 | #include "core/core.h" | 10 | #include "core/core.h" |
| 13 | #include "core/core_timing.h" | 11 | #include "core/core_timing.h" |
| @@ -140,12 +138,7 @@ void System::Reschedule() { | |||
| 140 | System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) { | 138 | System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) { |
| 141 | LOG_DEBUG(HW_Memory, "initialized OK"); | 139 | LOG_DEBUG(HW_Memory, "initialized OK"); |
| 142 | 140 | ||
| 143 | if (Settings::values.use_cpu_jit) { | 141 | cpu_core = std::make_unique<ARM_Unicorn>(); |
| 144 | cpu_core = std::make_unique<ARM_Dynarmic>(); | ||
| 145 | } else { | ||
| 146 | cpu_core = std::make_unique<ARM_Unicorn>(); | ||
| 147 | } | ||
| 148 | |||
| 149 | telemetry_session = std::make_unique<Core::TelemetrySession>(); | 142 | telemetry_session = std::make_unique<Core::TelemetrySession>(); |
| 150 | 143 | ||
| 151 | CoreTiming::Init(); | 144 | CoreTiming::Init(); |