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 7f664d9 commit 663d816
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void hook(Backend backend, int intno, Object user) {
return;
}

log.warn("handleInterrupt intno=" + intno + ", NR=" + NR + ", svcNumber=0x" + Integer.toHexString(svcNumber) + ", PC=" + pc + ", LR=" + UnidbgPointer.register(emulator, ArmConst.UC_ARM_REG_LR) + ", syscall=" + syscall, exception);
log.warn("handleInterrupt intno=" + intno + ", NR=" + NR + ", svcNumber=0x" + Integer.toHexString(svcNumber) + ", PC=" + pc + ", LR=" + UnidbgPointer.register(emulator, Arm64Const.UC_ARM64_REG_LR) + ", syscall=" + syscall, exception);

if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.apache.commons.logging.LogFactory;
import unicorn.Unicorn;

public abstract class DynarmicBackend implements Backend {
public abstract class DynarmicBackend implements Backend, DynarmicCallback {

private static final Log log = LogFactory.getLog(DynarmicBackend.class);

Expand All @@ -26,6 +26,15 @@ static DynarmicBackend tryInitialize(boolean is64Bit) {

protected DynarmicBackend(Dynarmic dynarmic) {
this.dynarmic = dynarmic;
this.dynarmic.setDynarmicCallback(this);
}

@Override
public void callSVC(int swi) {
if (log.isDebugEnabled()) {
log.debug("callSVC swi=" + swi);
}
interruptHookNotifier.notifyCallSVC(this);
}

@Override
Expand All @@ -44,7 +53,7 @@ public void emu_start(long begin, long until, long timeout, long count) {

@Override
public void emu_stop() {
throw new AbstractMethodError();
dynarmic.emu_stop();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Dynarmic implements Closeable {

private static final Log log = LogFactory.getLog(Dynarmic.class);

private static native int setDynarmicCallback(long handle, DynarmicCallback callback);

private static native long nativeInitialize(boolean is64Bit);
private static native void nativeDestroy(long handle);

Expand All @@ -19,6 +21,7 @@ public class Dynarmic implements Closeable {
private static native int mem_write(long handle, long address, byte[] bytes);
private static native byte[] mem_read(long handle, long address, int size);

private static native long reg_read_pc64(long handle);
private static native int reg_set_sp64(long handle, long value);
private static native long reg_read_sp64(long handle);
private static native int reg_set_tpidr_el0(long handle, long value);
Expand All @@ -27,13 +30,25 @@ public class Dynarmic implements Closeable {
private static native long reg_read(long handle, int index);

private static native int run(long handle, long pc);
private static native int emu_stop(long handle);

private final long nativeHandle;

public Dynarmic(boolean is64Bit) {
this.nativeHandle = nativeInitialize(is64Bit);
}

public void setDynarmicCallback(DynarmicCallback callback) {
if (log.isDebugEnabled()) {
log.debug("setDynarmicCallback callback" + callback);
}

int ret = setDynarmicCallback(nativeHandle, callback);
if (ret != 0) {
throw new DynarmicException("ret=" + ret);
}
}

public void emu_start(long begin) {
if (log.isDebugEnabled()) {
log.debug("emu_start begin=0x" + Long.toHexString(begin));
Expand All @@ -45,6 +60,17 @@ public void emu_start(long begin) {
}
}

public void emu_stop() {
if (log.isDebugEnabled()) {
log.debug("emu_stop");
}

int ret = emu_stop(nativeHandle);
if (ret != 0) {
throw new DynarmicException("ret=" + ret);
}
}

public void mem_unmap(long address, long size) {
long start = log.isDebugEnabled() ? System.currentTimeMillis() : 0;
int ret = mem_unmap(nativeHandle, address, size);
Expand Down Expand Up @@ -88,11 +114,20 @@ public void reg_set_sp64(long value) {
}
}

public long reg_read_pc64() {
long pc = reg_read_pc64(nativeHandle);
if (log.isDebugEnabled()) {
log.debug("reg_read_pc64=0x" + Long.toHexString(pc));
}
return pc;
}

public long reg_read_sp64() {
long sp = reg_read_sp64(nativeHandle);
if (log.isDebugEnabled()) {
log.debug("reg_read_sp64");
log.debug("reg_read_sp64=0x" + Long.toHexString(sp));
}
return reg_read_sp64(nativeHandle);
return sp;
}

public void reg_set_tpidr_el0(long value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ public DynarmicBackend64(Dynarmic dynarmic) {
public Number reg_read(int regId) {
switch (regId) {
case Arm64Const.UC_ARM64_REG_X0:
return dynarmic.reg_read64(0);
case Arm64Const.UC_ARM64_REG_X1:
case Arm64Const.UC_ARM64_REG_X2:
case Arm64Const.UC_ARM64_REG_X3:
case Arm64Const.UC_ARM64_REG_X4:
case Arm64Const.UC_ARM64_REG_X5:
case Arm64Const.UC_ARM64_REG_X6:
case Arm64Const.UC_ARM64_REG_X7:
case Arm64Const.UC_ARM64_REG_X8:
return dynarmic.reg_read64(regId - Arm64Const.UC_ARM64_REG_X0);
case Arm64Const.UC_ARM64_REG_SP:
return dynarmic.reg_read_sp64();
case Arm64Const.UC_ARM64_REG_LR:
return dynarmic.reg_read64(30);
case Arm64Const.UC_ARM64_REG_PC:
return dynarmic.reg_read_pc64();
default:
throw new DynarmicException("regId=" + regId);
}
Expand All @@ -24,16 +36,16 @@ public Number reg_read(int regId) {
@Override
public void reg_write(int regId, Number value) {
switch (regId) {
case Arm64Const.UC_ARM64_REG_X30:
dynarmic.reg_write64(30, value.longValue());
break;
case Arm64Const.UC_ARM64_REG_X0:
case Arm64Const.UC_ARM64_REG_X1:
dynarmic.reg_write64(regId - Arm64Const.UC_ARM64_REG_X0, value.longValue());
break;
case Arm64Const.UC_ARM64_REG_SP:
dynarmic.reg_set_sp64(value.longValue());
break;
case Arm64Const.UC_ARM64_REG_LR:
dynarmic.reg_write64(30, value.longValue());
break;
case Arm64Const.UC_ARM64_REG_TPIDR_EL0:
dynarmic.reg_set_tpidr_el0(value.longValue());
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.unidbg.arm.backend.dynarmic;

public interface DynarmicCallback {

void callSVC(int swi);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.unidbg.arm.backend.dynarmic;

import com.github.unidbg.arm.ARMEmulator;
import com.github.unidbg.arm.backend.Backend;
import com.github.unidbg.arm.backend.InterruptHook;

public class InterruptHookNotifier {
Expand All @@ -12,4 +14,8 @@ public InterruptHookNotifier(InterruptHook callback, Object user_data) {
this.user_data = user_data;
}

public void notifyCallSVC(Backend backend) {
callback.hook(backend, ARMEmulator.EXCP_SWI, user_data);
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 663d816

Please sign in to comment.