diff options
| author | 2018-01-04 00:13:23 -0500 | |
|---|---|---|
| committer | 2018-01-04 00:13:23 -0500 | |
| commit | 3f8b9181b553e8af9bd93899832ddc2c8cf73e8b (patch) | |
| tree | 00df941867db6c14f172f6eb4ed99964ac3bc5b3 /src/core/arm | |
| 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.
Diffstat (limited to 'src/core/arm')
| -rw-r--r-- | src/core/arm/unicorn/arm_unicorn.cpp | 201 | ||||
| -rw-r--r-- | src/core/arm/unicorn/arm_unicorn.h | 39 |
2 files changed, 240 insertions, 0 deletions
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 | }; | ||