Skip to content

Commit

Permalink
Dynarmic continue...
Browse files Browse the repository at this point in the history
  • Loading branch information
WebDucerBlog committed Oct 11, 2020
1 parent a2eae50 commit 7f664d9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.unidbg.arm.backend.BackendFactory;
import com.github.unidbg.arm.backend.ReadHook;
import com.github.unidbg.arm.backend.WriteHook;
import com.github.unidbg.arm.backend.dynarmic.DynarmicException;
import com.github.unidbg.arm.context.RegisterContext;
import com.github.unidbg.debugger.DebugServer;
import com.github.unidbg.debugger.Debugger;
Expand Down Expand Up @@ -393,7 +394,7 @@ public void run() {
return (r0.intValue() & 0xffffffffL) | ((r1.intValue() & 0xffffffffL) << 32);
}
} catch (RuntimeException e) {
if (!entry && e instanceof UnicornException && !log.isDebugEnabled()) {
if (!entry && (e instanceof UnicornException || e instanceof DynarmicException) && !log.isDebugEnabled()) {
log.warn("emulate " + pointer + " failed: sp=" + getStackPointer() + ", offset=" + (System.currentTimeMillis() - start) + "ms", e);
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.unidbg.arm.backend.DynarmicBackend;
import unicorn.Arm64Const;
import unicorn.UnicornException;

public class DynarmicBackend64 extends DynarmicBackend {

Expand All @@ -18,7 +17,7 @@ public Number reg_read(int regId) {
case Arm64Const.UC_ARM64_REG_SP:
return dynarmic.reg_read_sp64();
default:
throw new UnicornException("regId=" + regId);
throw new DynarmicException("regId=" + regId);
}
}

Expand All @@ -39,7 +38,7 @@ public void reg_write(int regId, Number value) {
dynarmic.reg_set_tpidr_el0(value.longValue());
break;
default:
throw new UnicornException("regId=" + regId);
throw new DynarmicException("regId=" + regId);
}
}

Expand Down
2 changes: 1 addition & 1 deletion unidbg-dynarmic/src/main/native/dynarmic/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ JAVA_INC="$(realpath "$JAVA_HOME"/include)"
JAVA_PLATFORM_INC="$(dirname "$(find "$JAVA_INC" -name jni_md.h)")"

"$(/usr/libexec/java_home -v 1.8)"/bin/javah -cp ../../../../../unidbg-api/target/classes com.github.unidbg.arm.backend.dynarmic.Dynarmic && \
xcrun -sdk macosx clang++ -m64 -o libdynarmic.dylib -shared -std=c++11 \
xcrun -sdk macosx clang++ -m64 -o libdynarmic.dylib -shared -std=c++17 \
-I ~/git/dynarmic/include dynarmic.cpp \
-I "$JAVA_INC" -I "$JAVA_PLATFORM_INC" \
~/git/dynarmic/build/src/libdynarmic.a \
Expand Down
81 changes: 58 additions & 23 deletions unidbg-dynarmic/src/main/native/dynarmic/dynarmic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
#include <exception>

#include <sys/mman.h>
#include "dynarmic.h"

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

KHASH_MAP_INIT_INT64(memory, t_memory_page)
#include "dynarmic.h"

static void *get_memory(khash_t(memory) *memory, long vaddr) {
long base = vaddr & ~PAGE_MASK;
Expand All @@ -26,16 +20,38 @@ static void *get_memory(khash_t(memory) *memory, long vaddr) {
}

class DynarmicCallbacks64 final : public Dynarmic::A64::UserCallbacks {

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

public:
DynarmicCallbacks64(khash_t(memory) *memory)
: memory{memory} {}

~DynarmicCallbacks64() = default;

u8 MemoryRead8(u64 vaddr) override {
fprintf(stderr, "MemoryRead8[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return 0;
}
u16 MemoryRead16(u64 vaddr) override {
fprintf(stderr, "MemoryRead16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return 0;
if(vaddr & 1) {
fprintf(stderr, "MemoryRead16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return 0;
}
u16 *dest = (u16 *) get_memory(memory, vaddr);
if(dest) {
// printf("MemoryRead16[%s->%s:%d]: vaddr=%p, data=0x%x\n", __FILE__, __func__, __LINE__, (void*)vaddr, dest[0]);
return dest[0];
} else {
fprintf(stderr, "MemoryRead16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return 0;
}
}
u32 MemoryRead32(u64 vaddr) override {
if(vaddr & 3) {
Expand All @@ -45,6 +61,7 @@ class DynarmicCallbacks64 final : public Dynarmic::A64::UserCallbacks {
}
u32 *dest = (u32 *) get_memory(memory, vaddr);
if(dest) {
printf("MemoryRead32[%s->%s:%d]: vaddr=%p, data=0x%x\n", __FILE__, __func__, __LINE__, (void*)vaddr, dest[0]);
return dest[0];
} else {
fprintf(stderr, "MemoryRead32[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
Expand Down Expand Up @@ -81,8 +98,19 @@ class DynarmicCallbacks64 final : public Dynarmic::A64::UserCallbacks {
}
}
void MemoryWrite16(u64 vaddr, u16 value) override {
fprintf(stderr, "MemoryWrite16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
if(vaddr & 1) {
fprintf(stderr, "MemoryWrite16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return;
}
u16 *dest = (u16 *) get_memory(memory, vaddr);
if(dest) {
dest[0] = value;
} else {
fprintf(stderr, "MemoryWrite16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
}

}
void MemoryWrite32(u64 vaddr, u32 value) override {
if(vaddr & 3) {
Expand Down Expand Up @@ -120,27 +148,26 @@ class DynarmicCallbacks64 final : public Dynarmic::A64::UserCallbacks {
bool MemoryWriteExclusive8(u64 vaddr, std::uint8_t value, std::uint8_t expected) override {
fprintf(stderr, "MemoryWriteExclusive8[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return false;
return true;
}
bool MemoryWriteExclusive16(u64 vaddr, std::uint16_t value, std::uint16_t expected) override {
fprintf(stderr, "MemoryWriteExclusive16[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return false;
MemoryWrite16(vaddr, value);
return true;
}
bool MemoryWriteExclusive32(u64 vaddr, std::uint32_t value, std::uint32_t expected) override {
fprintf(stderr, "MemoryWriteExclusive32[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return false;
return true;
}
bool MemoryWriteExclusive64(u64 vaddr, std::uint64_t value, std::uint64_t expected) override {
fprintf(stderr, "MemoryWriteExclusive64[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return false;
return true;
}
bool MemoryWriteExclusive128(u64 vaddr, Dynarmic::A64::Vector value, Dynarmic::A64::Vector expected) override {
fprintf(stderr, "MemoryWriteExclusive128[%s->%s:%d]: vaddr=%p\n", __FILE__, __func__, __LINE__, (void*)vaddr);
abort();
return false;
return true;
}

void InterpreterFallback(u64 pc, std::size_t num_instructions) override {
Expand All @@ -159,16 +186,18 @@ class DynarmicCallbacks64 final : public Dynarmic::A64::UserCallbacks {
}

void AddTicks(u64 ticks) override {
this->ticks += ticks;
}

u64 GetTicksRemaining() override {
return (u64) -1;
return 0x10000000000;
}

u64 GetCNTPCT() override {
return 0;
return 0x10000000000;
}

u64 ticks = 0;
u64 tpidrro_el0 = 0;
u64 tpidr_el0 = 0;
khash_t(memory) *memory = NULL;
Expand Down Expand Up @@ -196,16 +225,16 @@ JNIEXPORT jlong JNICALL Java_com_github_unidbg_arm_backend_dynarmic_Dynarmic_nat
dynarmic->is64Bit = is64Bit == JNI_TRUE;
dynarmic->memory = kh_init(memory);
if(dynarmic->is64Bit) {
std::shared_ptr<DynarmicCallbacks64> cb = std::make_shared<DynarmicCallbacks64>();
std::shared_ptr<DynarmicCallbacks64> cb = std::make_shared<DynarmicCallbacks64>(dynarmic->memory);
DynarmicCallbacks64 *callbacks = cb.get();

Dynarmic::A64::UserConfig config;
config.callbacks = callbacks;
config.tpidrro_el0 = &callbacks->tpidrro_el0;
config.tpidr_el0 = &callbacks->tpidr_el0;

dynarmic->cb64 = cb;
dynarmic->jit64 = std::make_shared<Dynarmic::A64::Jit>(config);
callbacks->memory = dynarmic->memory;
}
return (jlong) dynarmic;
}
Expand Down Expand Up @@ -522,6 +551,12 @@ JNIEXPORT jint JNICALL Java_com_github_unidbg_arm_backend_dynarmic_Dynarmic_run
return 0;
}

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
return JNI_VERSION_1_6;
}

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions unidbg-dynarmic/src/main/native/dynarmic/dynarmic.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ typedef struct memory_page {
void *addr;
int perms;
} *t_memory_page;

KHASH_MAP_INIT_INT64(memory, t_memory_page)
Binary file not shown.

0 comments on commit 7f664d9

Please sign in to comment.