2 * \file ocsd_code_follower.cpp
3 * \brief OpenCSD : Instruction Code path follower.
5 * \copyright Copyright (c) 2016, ARM Limited. All Rights Reserved.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
20 * 3. Neither the name of the copyright holder nor the names of its contributors
21 * may be used to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "common/ocsd_code_follower.h"
38 OcsdCodeFollower::OcsdCodeFollower()
40 m_instr_info.pe_type.arch = ARCH_UNKNOWN;
41 m_instr_info.pe_type.profile = profile_Unknown;
42 m_instr_info.isa = ocsd_isa_unknown;
43 m_instr_info.dsb_dmb_waypoints = 0;
44 m_instr_info.instr_addr = 0;
45 m_instr_info.opcode = 0;
49 m_st_range_addr = m_en_range_addr = m_next_addr = 0;
50 m_b_next_valid = false;
54 OcsdCodeFollower::~OcsdCodeFollower()
58 void OcsdCodeFollower::initInterfaces(componentAttachPt<ITargetMemAccess> *pMemAccess, componentAttachPt<IInstrDecode> *pIDecode)
60 m_pMemAccess = pMemAccess;
61 m_pIDecode = pIDecode;
64 bool OcsdCodeFollower::initFollowerState()
66 bool initDone = false;
68 // reset per follow flags
69 m_b_next_valid = false;
72 // set range addresses
73 m_en_range_addr = m_next_addr = m_st_range_addr;
75 // check initialisation is valid.
77 // must have attached memory access and i-decode objects
78 if(m_pMemAccess && m_pIDecode)
80 initDone = (m_pMemAccess->hasAttachedAndEnabled() && m_pIDecode->hasAttachedAndEnabled());
86 * Decodes an instruction at a single location, calculates the next address
87 * if possible according to the instruction type and atom.
89 * @param addrStart : Address of the instruction
90 * @param A : Atom value - E or N
92 * @return ocsd_err_t : OCSD_OK - decode correct, check flags for next address
93 * : OCSD_ERR_MEM_NACC - unable to access memory area @ address - need new address in trace packet stream.
94 * : OCSD_ERR_NOT_INIT - not initialised - fatal.
95 * : OCSD_<other> - other error occured - fatal.
97 ocsd_err_t OcsdCodeFollower::followSingleAtom(const ocsd_vaddr_t addrStart, const ocsd_atm_val A)
99 ocsd_err_t err = OCSD_ERR_NOT_INIT;
101 if(!initFollowerState())
104 m_en_range_addr = m_st_range_addr = m_instr_info.instr_addr = addrStart;
105 err = decodeSingleOpCode();
110 // set end range - always after the instruction executed.
111 m_en_range_addr = m_instr_info.instr_addr + m_instr_info.instr_size;
113 // assume next addr is the instruction after
114 m_next_addr = m_en_range_addr;
115 m_b_next_valid = true;
117 // case when next address is different
118 switch(m_instr_info.type)
121 if(A == ATOM_E) // executed the direct branch
122 m_next_addr = m_instr_info.branch_addr;
125 case OCSD_INSTR_BR_INDIRECT:
126 if(A == ATOM_E) // executed indirect branch
127 m_b_next_valid = false;
133 ocsd_err_t OcsdCodeFollower::decodeSingleOpCode()
135 ocsd_err_t err = OCSD_OK;
136 // request 4 bytes for the opcode - even for Thumb which may be T32
137 uint32_t bytesReq = 4;
138 uint32_t opcode; // buffer for opcode
140 // read memory location for opcode
141 err = m_pMemAccess->first()->ReadTargetMemory(m_instr_info.instr_addr,m_mem_space_csid,m_mem_acc_rule,&bytesReq,(uint8_t *)&opcode);
143 // operational error (not access problem - that is indicated by 0 bytes returned)
147 if(bytesReq == 4) // check that we got all memory requested.
149 m_instr_info.opcode = opcode;
150 err = m_pIDecode->first()->DecodeInstruction(&m_instr_info);
152 else // otherwise memory unavailable.
155 m_nacc_address = m_instr_info.instr_addr;
156 err = OCSD_ERR_MEM_NACC;
161 /* End of File ocsd_code_follower.cpp */