Commit 1ae52e14 authored by swift_gan's avatar swift_gan Committed by swift_gan

[NativeHook]add inst msr mrs

parent eacc71d2
......@@ -231,6 +231,14 @@ void AssemblerA64::Subs(RegisterA64 &rd, const Operand &operand) {
Emit(reinterpret_cast<Unit<Base> *>(new INST_A64(ADD_SUB_IMM)(INST_A64(ADD_SUB_IMM)::SUB, INST_A64(ADD_SUB_IMM)::Sign, rd, operand)));
}
void AssemblerA64::Mrs(SystemRegister &sysReg, RegisterA64 &rt) {
Emit(reinterpret_cast<Unit<Base> *>(new INST_A64(MSR_MRS)(INST_A64(MSR_MRS)::MRS, sysReg, rt)));
}
void AssemblerA64::Msr(SystemRegister &sysReg, RegisterA64 &rt) {
Emit(reinterpret_cast<Unit<Base> *>(new INST_A64(MSR_MRS)(INST_A64(MSR_MRS)::MSR, sysReg, rt)));
}
......@@ -83,6 +83,9 @@ namespace SandHook {
void Sub(RegisterA64& rd, const Operand& operand);
void Subs(RegisterA64& rd, const Operand& operand);
void Msr(SystemRegister &sysReg, RegisterA64& rt);
void Mrs(SystemRegister &sysReg, RegisterA64& rt);
public:
CodeContainer codeContainer = CodeContainer(nullptr);
......
......@@ -49,4 +49,8 @@ void *InlineHookArm64Android::inlineHook(void *origin, void *replace) {
//commit inline trampoline
assemblerInline.finish();
return backup;
}
\ No newline at end of file
}
bool InlineHookArm64Android::breakPoint(void *point, void (*callback)(REG regs[], void* sp)) {
return false;
}
......@@ -18,6 +18,9 @@ namespace SandHook {
delete hookLock;
}
void *inlineHook(void *origin, void *replace) override;
bool breakPoint(void *point, void (*callback)(REG[], void*)) override;
protected:
std::mutex* hookLock;
};
......
......@@ -844,6 +844,8 @@ void A64_STP_LDP::assembler() {
case AddrMode::PreIndex:
get()->addrmode = PreIndex;
break;
default:
valid = false;
}
get()->imm7 = TruncateToUint7(operand.offset >> (rt1->isX() ? 3 : 2));
}
......@@ -895,3 +897,23 @@ void A64_ADD_SUB_IMM::assembler() {
valid = false;
}
}
A64_MSR_MRS::A64_MSR_MRS(A64_STRUCT_MSR_MRS &inst) : InstructionA64(&inst) {
decode(&inst);
}
A64_MSR_MRS::A64_MSR_MRS(OP op, SystemRegister &systemRegister, RegisterA64 &rt) : op(op), systemRegister(
&systemRegister), rt(&rt) {}
void A64_MSR_MRS::decode(A64_STRUCT_MSR_MRS *inst) {
DECODE_OP;
DECODE_RT(XReg);
//TODO
}
void A64_MSR_MRS::assembler() {
SET_OPCODE(MSR_MRS);
ENCODE_OP;
ENCODE_RT;
get()->sysreg = systemRegister->value();
}
......@@ -36,6 +36,8 @@ inline U32 instCode() override { \
return ENUM_VALUE(InstCodeA64, InstCodeA64::X); \
}
#define DEFINE_INST(X) class INST_A64(X) : public InstructionA64<STRUCT_A64(X)>
using namespace SandHook::RegistersA64;
using namespace SandHook::Asm;
......@@ -471,7 +473,7 @@ namespace SandHook {
};
class INST_A64(BR_BLR_RET) : public InstructionA64<STRUCT_A64(BR_BLR_RET)> {
DEFINE_INST(BR_BLR_RET) {
public:
enum OP {
......@@ -573,7 +575,7 @@ namespace SandHook {
};
class INST_A64(MOV_REG) : public InstructionA64<STRUCT_A64(MOV_REG)> {
DEFINE_INST(MOV_REG) {
public:
A64_MOV_REG();
......@@ -596,7 +598,7 @@ namespace SandHook {
};
class INST_A64(SUB_EXT_REG) : public InstructionA64<STRUCT_A64(SUB_EXT_REG)> {
DEFINE_INST(SUB_EXT_REG) {
public:
A64_SUB_EXT_REG();
......@@ -623,7 +625,7 @@ namespace SandHook {
};
class INST_A64(EXCEPTION_GEN) : public InstructionA64<STRUCT_A64(EXCEPTION_GEN)> {
DEFINE_INST(EXCEPTION_GEN) {
public:
enum OP {
......@@ -740,7 +742,7 @@ namespace SandHook {
};
class INST_A64(STP_LDP) : public InstructionA64<STRUCT_A64(STP_LDP)> {
DEFINE_INST(STP_LDP) {
public:
enum OP {
STP = 0b0,
......@@ -778,7 +780,7 @@ namespace SandHook {
};
class INST_A64(ADD_SUB_IMM) : public InstructionA64<STRUCT_A64(ADD_SUB_IMM)> {
DEFINE_INST(ADD_SUB_IMM) {
public:
enum OP {
......@@ -817,6 +819,33 @@ namespace SandHook {
Operand operand;
};
DEFINE_INST(MSR_MRS) {
public:
enum OP {
MSR = 0,
MRS = 1
};
A64_MSR_MRS(A64_STRUCT_MSR_MRS &inst);
A64_MSR_MRS(OP op, SystemRegister &systemRegister, RegisterA64 &rt);
DEFINE_INST_CODE(MSR_MRS)
DEFINE_IS(MSR_MRS)
void decode(A64_STRUCT_MSR_MRS *inst) override;
void assembler() override;
public:
OP op;
SystemRegister* systemRegister;
RegisterA64* rt;
};
}
}
......
......@@ -28,7 +28,8 @@ enum class InstCodeA64 {
EXCEPTION_GEN,
SVC,
STP_LDP,
ADD_SUB_IMM
ADD_SUB_IMM,
MSR_MRS
};
#endif //SANDHOOK_NH_INST_CODE_ARM64_H
......@@ -227,6 +227,12 @@ DEFINE_STRUCT_A64(ADD_SUB_IMM) {
InstA64 sf:1;
};
DEFINE_OPCODE_A64(MSR_MRS, 0b1101010100)
DEFINE_STRUCT_A64(MSR_MRS) {
InstA64 rt:WideReg;
InstA64 sysreg:16;
InstA64 op:1;
InstA64 opcode:10;
};
#endif //SANDHOOK_NH_INST_AARCH64_H
......@@ -87,6 +87,43 @@ namespace SandHook {
static WRegister* registers[];
};
class SystemRegister {
public:
struct SystemReg {
U16 op2:3;
U16 CRm:4;
U16 CRn:4;
U16 op1:3;
U16 op0:2;
};
SystemRegister(U16 op0, U16 op1, U16 crn, U16 crm, U16 op2) {
reg.op0 = op0;
reg.op1 = op1;
reg.CRn = crn;
reg.CRm = crm;
reg.op2 = op2;
}
U16 value() {
union {
U16 raw;
SystemReg reg;
} ret;
ret.reg = reg;
return ret.raw;
}
public:
SystemReg reg;
};
}
}
......
......@@ -22,5 +22,8 @@ namespace SandHook {
XRegister XZR = X31;
WRegister WZR = W31;
RegisterA64 UnknowRegiser = RegisterA64(38);
SystemRegister NZCV = SystemRegister(3, 3, 4, 2, 0);
SystemRegister FPCR = SystemRegister(3, 3, 4, 4, 0);
}
}
\ No newline at end of file
......@@ -48,6 +48,12 @@ namespace SandHook {
return &WZR;
}
}
// System/special register names.
// This information is not encoded as one field but as the concatenation of
// multiple fields (Op0<0>, Op1, Crn, Crm, Op2).
extern SystemRegister NZCV;
extern SystemRegister FPCR;
}
}
......
......@@ -12,6 +12,8 @@
#include "assembler.h"
#include "code_relocate.h"
typedef Addr REG;
namespace SandHook {
namespace Hook {
......@@ -19,7 +21,9 @@ namespace SandHook {
public:
//return == backup method
virtual void* inlineHook(void* origin, void* replace) = 0;
virtual bool breakPoint(void* point, void (*callback)(REG[], void *)) {
return false;
};
protected:
static AndroidCodeBuffer* backupBuffer;
public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment