Instruction decode updated to recognise new instructions in v8.x architecture that are used as P0 elements in ETMv4.x trace.
Signed-off-by: Mike Leach mike.leach@linaro.org --- decoder/include/i_dec/trc_i_decode.h | 1 + decoder/include/i_dec/trc_idec_arminst.h | 9 +++ decoder/source/i_dec/trc_i_decode.cpp | 50 ++++++++++-- decoder/source/i_dec/trc_idec_arminst.cpp | 99 ++++++++++++++++++++++- 4 files changed, 152 insertions(+), 7 deletions(-)
diff --git a/decoder/include/i_dec/trc_i_decode.h b/decoder/include/i_dec/trc_i_decode.h index ac31a79..0285f41 100644 --- a/decoder/include/i_dec/trc_i_decode.h +++ b/decoder/include/i_dec/trc_i_decode.h @@ -49,6 +49,7 @@ private: ocsd_err_t DecodeA32(ocsd_instr_info *instr_info); ocsd_err_t DecodeA64(ocsd_instr_info *instr_info); ocsd_err_t DecodeT32(ocsd_instr_info *instr_info); + void SetArchVersion(ocsd_instr_info *instr_info); };
#endif // ARM_TRC_I_DECODE_H_INCLUDED diff --git a/decoder/include/i_dec/trc_idec_arminst.h b/decoder/include/i_dec/trc_idec_arminst.h index 6654375..8697f68 100644 --- a/decoder/include/i_dec/trc_idec_arminst.h +++ b/decoder/include/i_dec/trc_idec_arminst.h @@ -75,6 +75,7 @@ int inst_ARM_is_direct_branch(uint32_t inst); int inst_Thumb_is_direct_branch(uint32_t inst); int inst_Thumb_is_direct_branch_link(uint32_t inst, uint8_t *is_link, uint8_t *is_cond); int inst_A64_is_direct_branch(uint32_t inst); +int inst_A64_is_direct_branch_link(uint32_t inst, uint8_t *is_link);
/* Get branch destination for a direct branch. @@ -86,6 +87,7 @@ int inst_A64_branch_destination(uint64_t addr, uint32_t inst, uint64_t *pnpc); int inst_ARM_is_indirect_branch(uint32_t inst); int inst_Thumb_is_indirect_branch_link(uint32_t inst, uint8_t *is_link); int inst_Thumb_is_indirect_branch(uint32_t inst); +int inst_A64_is_indirect_branch_link(uint32_t inst, uint8_t *is_link); int inst_A64_is_indirect_branch(uint32_t inst);
int inst_ARM_is_branch_and_link(uint32_t inst); @@ -111,6 +113,10 @@ arm_barrier_t inst_ARM_barrier(uint32_t inst); arm_barrier_t inst_Thumb_barrier(uint32_t inst); arm_barrier_t inst_A64_barrier(uint32_t inst);
+int inst_ARM_wfiwfe(uint32_t inst); +int inst_Thumb_wfiwfe(uint32_t inst); +int inst_A64_wfiwfe(uint32_t inst); + /* Test whether an instruction is definitely undefined, e.g. because allocated to a "permanently UNDEFINED" space (UDF mnemonic). @@ -127,6 +133,9 @@ int inst_A64_is_UDF(uint32_t inst); ocsd_instr_subtype get_instr_subtype(); void clear_instr_subtype();
+/* set arch version info. */ +void set_arch_version(uint16_t version); + #endif // ARM_TRC_IDEC_ARMINST_H_INCLUDED
/* End of File trc_idec_arminst.h */ diff --git a/decoder/source/i_dec/trc_i_decode.cpp b/decoder/source/i_dec/trc_i_decode.cpp index 1fc2120..5ffc926 100644 --- a/decoder/source/i_dec/trc_i_decode.cpp +++ b/decoder/source/i_dec/trc_i_decode.cpp @@ -40,6 +40,8 @@ ocsd_err_t TrcIDecode::DecodeInstruction(ocsd_instr_info *instr_info) { ocsd_err_t err = OCSD_OK; clear_instr_subtype(); + SetArchVersion(instr_info); + switch(instr_info->isa) { case ocsd_isa_arm: @@ -65,6 +67,23 @@ ocsd_err_t TrcIDecode::DecodeInstruction(ocsd_instr_info *instr_info) return err; }
+void TrcIDecode::SetArchVersion(ocsd_instr_info *instr_info) +{ + uint16_t arch = 0x0700; + bool m_profile = (instr_info->pe_type.profile == profile_CortexM); + + switch (instr_info->pe_type.arch) + { + case ARCH_V8: arch = 0x0800; break; + case ARCH_V8r3: arch = 0x0803; break; + case ARCH_V7: + default: + break; + } + set_arch_version(arch); +} + + ocsd_err_t TrcIDecode::DecodeA32(ocsd_instr_info *instr_info) { uint32_t branchAddr = 0; @@ -107,7 +126,13 @@ ocsd_err_t TrcIDecode::DecodeA32(ocsd_instr_info *instr_info) break; } } - + else if (instr_info->wfi_wfe_branch) + { + if (inst_ARM_wfiwfe(instr_info->opcode)) + { + instr_info->type = OCSD_INSTR_WFI_WFE; + } + } instr_info->is_conditional = inst_ARM_is_conditional(instr_info->opcode);
return OCSD_OK; @@ -123,17 +148,17 @@ ocsd_err_t TrcIDecode::DecodeA64(ocsd_instr_info *instr_info) instr_info->next_isa = instr_info->isa; // assume same ISA instr_info->is_link = 0;
- if(inst_A64_is_indirect_branch(instr_info->opcode)) + if(inst_A64_is_indirect_branch_link(instr_info->opcode, &instr_info->is_link)) { instr_info->type = OCSD_INSTR_BR_INDIRECT; - instr_info->is_link = inst_A64_is_branch_and_link(instr_info->opcode); +// instr_info->is_link = inst_A64_is_branch_and_link(instr_info->opcode); } - else if(inst_A64_is_direct_branch(instr_info->opcode)) + else if(inst_A64_is_direct_branch_link(instr_info->opcode, &instr_info->is_link)) { inst_A64_branch_destination(instr_info->instr_addr,instr_info->opcode,&branchAddr); instr_info->type = OCSD_INSTR_BR; instr_info->branch_addr = (ocsd_vaddr_t)branchAddr; - instr_info->is_link = inst_A64_is_branch_and_link(instr_info->opcode); +// instr_info->is_link = inst_A64_is_branch_and_link(instr_info->opcode); } else if((barrier = inst_A64_barrier(instr_info->opcode)) != ARM_BARRIER_NONE) { @@ -150,6 +175,13 @@ ocsd_err_t TrcIDecode::DecodeA64(ocsd_instr_info *instr_info) break; } } + else if (instr_info->wfi_wfe_branch) + { + if (inst_A64_wfiwfe(instr_info->opcode)) + { + instr_info->type = OCSD_INSTR_WFI_WFE; + } + }
instr_info->is_conditional = inst_A64_is_conditional(instr_info->opcode);
@@ -202,7 +234,13 @@ ocsd_err_t TrcIDecode::DecodeT32(ocsd_instr_info *instr_info) break; } } - + else if (instr_info->wfi_wfe_branch) + { + if (inst_Thumb_wfiwfe(instr_info->opcode)) + { + instr_info->type = OCSD_INSTR_WFI_WFE; + } + } instr_info->is_conditional = inst_Thumb_is_conditional(instr_info->opcode); instr_info->thumb_it_conditions = inst_Thumb_is_IT(instr_info->opcode);
diff --git a/decoder/source/i_dec/trc_idec_arminst.cpp b/decoder/source/i_dec/trc_idec_arminst.cpp index f3f2732..09964a1 100644 --- a/decoder/source/i_dec/trc_idec_arminst.cpp +++ b/decoder/source/i_dec/trc_idec_arminst.cpp @@ -45,6 +45,9 @@ block identification and trace decode.
static ocsd_instr_subtype instr_sub_type = OCSD_S_INSTR_NONE;
+/* need to spot the architecture version for certain instructions */ +static uint16_t arch_version = 0x70; + ocsd_instr_subtype get_instr_subtype() { return instr_sub_type; @@ -55,6 +58,11 @@ void clear_instr_subtype() instr_sub_type = OCSD_S_INSTR_NONE; }
+void set_arch_version(uint16_t version) +{ + arch_version = version; +} + int inst_ARM_is_direct_branch(uint32_t inst) { int is_direct_branch = 1; @@ -73,6 +81,16 @@ int inst_ARM_is_direct_branch(uint32_t inst) return is_direct_branch; }
+int inst_ARM_wfiwfe(uint32_t inst) +{ + if ( ((inst & 0xf0000000) != 0xf0000000) && + ((inst & 0x0ffffffe) == 0x0320f002) + ) + /* WFI & WFE may be traced as branches in etm4.3 ++ */ + return 1; + return 0; +} + int inst_ARM_is_indirect_branch(uint32_t inst) { int is_indirect_branch = 1; @@ -163,6 +181,22 @@ int inst_Thumb_is_direct_branch_link(uint32_t inst, uint8_t *is_link, uint8_t *i return is_direct_branch; }
+int inst_Thumb_wfiwfe(uint32_t inst) +{ + int is_wfiwfe = 1; + /* WFI, WFE may be branches in etm4.3++ */ + if ((inst & 0xfffffffe) == 0xf3af8002) { + /* WFI & WFE (encoding T2) */ + } + else if ((inst & 0xffef0000) == 0xbf200000) { + /* WFI & WFE (encoding T1) */ + } + else { + is_wfiwfe = 0; + } + return is_wfiwfe; +} + int inst_Thumb_is_indirect_branch(uint32_t inst) { uint8_t link; @@ -175,7 +209,7 @@ int inst_Thumb_is_indirect_branch_link(uint32_t inst, uint8_t *is_link) int is_branch = 1;
if ((inst & 0xff000000) == 0x47000000) { - /* BX, BLX (reg) */ + /* BX, BLX (reg) [v8M includes BXNS, BLXNS] */ if (inst & 0x00800000) { *is_link = 1; instr_sub_type = OCSD_S_INSTR_BR_LINK; @@ -221,6 +255,12 @@ int inst_Thumb_is_indirect_branch_link(uint32_t inst, uint8_t *is_link) }
int inst_A64_is_direct_branch(uint32_t inst) +{ + uint8_t link = 0; + return inst_A64_is_direct_branch_link(inst, &link); +} + +int inst_A64_is_direct_branch_link(uint32_t inst, uint8_t *is_link) { int is_direct_branch = 1; if ((inst & 0x7c000000) == 0x34000000) { @@ -229,23 +269,69 @@ int inst_A64_is_direct_branch(uint32_t inst) /* B<cond> */ } else if ((inst & 0x7c000000) == 0x14000000) { /* B, BL imm */ + if (inst & 0x80000000) { + *is_link = 1; + instr_sub_type = OCSD_S_INSTR_BR_LINK; + } } else { is_direct_branch = 0; } return is_direct_branch; }
+int inst_A64_wfiwfe(uint32_t inst) +{ + /* WFI, WFE may be traced as branches in etm 4.3++ */ + if ((inst & 0xffffffdf) == 0xd503205f) + return 1; + return 0; +} + int inst_A64_is_indirect_branch(uint32_t inst) +{ + uint8_t link = 0; + return inst_A64_is_indirect_branch_link(inst, &link); +} + +int inst_A64_is_indirect_branch_link(uint32_t inst, uint8_t *is_link) { int is_indirect_branch = 1; + if ((inst & 0xffdffc1f) == 0xd61f0000) { /* BR, BLR */ + if (inst & 0x00200000) { + *is_link = 1; + instr_sub_type = OCSD_S_INSTR_BR_LINK; + } } else if ((inst & 0xfffffc1f) == 0xd65f0000) { instr_sub_type = OCSD_S_INSTR_V8_RET; /* RET */ } else if ((inst & 0xffffffff) == 0xd69f03e0) { /* ERET */ instr_sub_type = OCSD_S_INSTR_V8_ERET; + } else if (arch_version >= 0x0803) { + /* new pointer auth instr for v8.3 arch */ + if ((inst & 0xffdff800) == 0xd61f0800) { + /* BRAA, BRAB, BLRAA, BLRBB */ + if (inst & 0x00200000) { + *is_link = 1; + instr_sub_type = OCSD_S_INSTR_BR_LINK; + } + } else if ((inst & 0xffdff81F) == 0xd71f081F) { + /* BRAAZ, BRABZ, BLRAAZ, BLRBBZ */ + if (inst & 0x00200000) { + *is_link = 1; + instr_sub_type = OCSD_S_INSTR_BR_LINK; + } + } else if ((inst & 0xfffffbff) == 0xd69f0bff) { + /* ERETAA, ERETAB */ + instr_sub_type = OCSD_S_INSTR_V8_ERET; + } else if ((inst & 0xfffffbff) == 0xd65f0bff) { + /* RETAA, RETAB */ + instr_sub_type = OCSD_S_INSTR_V8_RET; + } else { + is_indirect_branch = 0; + } } else { is_indirect_branch = 0; } @@ -419,6 +505,17 @@ int inst_A64_is_branch_and_link(uint32_t inst) } else if ((inst & 0xfc000000) == 0x94000000) { /* BL */ instr_sub_type = OCSD_S_INSTR_BR_LINK; + } else if (arch_version >= 0x0803) { + /* new pointer auth instr for v8.3 arch */ + if ((inst & 0xfffff800) == 0xd73f0800) { + /* BLRAA, BLRBB */ + instr_sub_type = OCSD_S_INSTR_BR_LINK; + } else if ((inst & 0xfffff81F) == 0xd63f081F) { + /* BLRAAZ, BLRBBZ */ + instr_sub_type = OCSD_S_INSTR_BR_LINK; + } else { + is_branch = 0; + } } else { is_branch = 0; }