2 * \file trc_pkt_decode_etmv4i.cpp
3 * \brief OpenCSD : ETMv4 decoder
5 * \copyright Copyright (c) 2015, 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 "opencsd/etmv4/trc_pkt_decode_etmv4i.h"
38 #include "common/trc_gen_elem.h"
41 #define DCD_NAME "DCD_ETMV4"
43 static const uint32_t ETMV4_SUPPORTED_DECODE_OP_FLAGS = OCSD_OPFLG_PKTDEC_COMMON;
45 TrcPktDecodeEtmV4I::TrcPktDecodeEtmV4I()
46 : TrcPktDecodeBase(DCD_NAME)
51 TrcPktDecodeEtmV4I::TrcPktDecodeEtmV4I(int instIDNum)
52 : TrcPktDecodeBase(DCD_NAME,instIDNum)
57 TrcPktDecodeEtmV4I::~TrcPktDecodeEtmV4I()
61 /*********************** implementation packet decoding interface */
63 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::processPacket()
65 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
66 bool bPktDone = false;
73 // output the initial not synced packet to the sink
74 m_output_elem.setType(OCSD_GEN_TRC_ELEM_NO_SYNC);
75 resp = outputTraceElement(m_output_elem);
76 m_curr_state = WAIT_SYNC;
77 // fall through to check if the current packet is the async we are waiting for.
81 if(m_curr_packet_in->getType() == ETM4_PKT_I_ASYNC)
82 m_curr_state = WAIT_TINFO;
89 if(m_curr_packet_in->getType() == ETM4_PKT_I_TRACE_INFO)
92 m_curr_state = DECODE_PKTS;
93 m_return_stack.flush();
99 resp = decodePacket(bPktDone); // this may change the state to commit elem;
103 resp = commitElements(bPktDone); // this will change the state to DECODE_PKTS once all elem committed.
111 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::onEOT()
113 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
119 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::onReset()
121 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
126 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::onFlush()
128 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
130 // continue exception processing (can't go through processPacket as elements no longer on stack)
131 if(m_excep_info.proc != EXCEP_POP)
132 resp = processException();
133 // continue ongoing output operations on comitted elements.
134 else if(m_curr_state == COMMIT_ELEM)
135 resp = processPacket();
136 // continue flushing at end of trace
142 ocsd_err_t TrcPktDecodeEtmV4I::onProtocolConfig()
144 ocsd_err_t err = OCSD_OK;
146 // set some static config elements
147 m_CSID = m_config->getTraceID();
148 m_max_spec_depth = m_config->MaxSpecDepth();
149 m_p0_key_max = m_config->P0_Key_Max();
150 m_cond_key_max_incr = m_config->CondKeyMaxIncr();
152 // set up static trace instruction decode elements
153 m_instr_info.dsb_dmb_waypoints = 0;
154 m_instr_info.wfi_wfe_branch = m_config->wfiwfeBranch() ? 1 : 0;
155 m_instr_info.pe_type.arch = m_config->archVersion();
156 m_instr_info.pe_type.profile = m_config->coreProfile();
158 m_IASize64 = (m_config->iaSizeMax() == 64);
160 if (m_config->enabledRetStack())
162 m_return_stack.set_active(true);
163 #ifdef TRC_RET_STACK_DEBUG
164 m_return_stack.set_dbg_logger(this);
168 // check config compatible with current decoder support level.
169 // at present no data trace, no spec depth, no return stack, no QE
170 // Remove these checks as support is added.
171 if(m_max_spec_depth != 0)
173 err = OCSD_ERR_HW_CFG_UNSUPP;
174 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : None-zero speculation depth not supported"));
176 else if(m_config->enabledDataTrace())
178 err = OCSD_ERR_HW_CFG_UNSUPP;
179 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : Data trace elements not supported"));
181 else if(m_config->enabledLSP0Trace())
183 err = OCSD_ERR_HW_CFG_UNSUPP;
184 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : LSP0 elements not supported."));
186 else if(m_config->enabledCondITrace() != EtmV4Config::COND_TR_DIS)
188 err = OCSD_ERR_HW_CFG_UNSUPP;
189 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : Trace on conditional non-branch elements not supported."));
191 else if(m_config->enabledQE())
193 err = OCSD_ERR_HW_CFG_UNSUPP;
194 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : Trace using Q elements not supported."));
200 /************* local decode methods */
201 void TrcPktDecodeEtmV4I::initDecoder()
203 // set the operational modes supported.
204 m_supported_op_flags = ETMV4_SUPPORTED_DECODE_OP_FLAGS;
206 /* init elements that get set by config */
207 m_max_spec_depth = 0;
210 m_cond_key_max_incr = 0;
213 // reset decoder state to unsynced
217 void TrcPktDecodeEtmV4I::resetDecoder()
219 m_curr_state = NO_SYNC;
226 m_curr_spec_depth = 0;
232 m_except_pending_addr = false;
233 m_mem_nacc_pending = false;
234 m_prev_overflow = false;
235 m_P0_stack.delete_all();
236 m_output_elem.init();
237 m_excep_info.proc = EXCEP_POP;
241 // this function can output an immediate generic element if this covers the complete packet decode,
242 // or stack P0 and other elements for later processing on commit or cancel.
243 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::decodePacket(bool &Complete)
245 bool bAllocErr = false;
246 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
248 bool is_addr = false;
249 bool is_except = false;
251 switch(m_curr_packet_in->getType())
253 case ETM4_PKT_I_ASYNC: // nothing to do with this packet.
254 case ETM4_PKT_I_IGNORE: // or this one.
257 case ETM4_PKT_I_TRACE_INFO:
258 // skip subsequent TInfo packets.
259 m_return_stack.flush();
262 case ETM4_PKT_I_TRACE_ON:
264 if (m_P0_stack.createParamElemNoParam(P0_TRC_ON, false, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
269 case ETM4_PKT_I_OVERFLOW:
271 if (m_P0_stack.createParamElemNoParam(P0_OVERFLOW, false, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
276 case ETM4_PKT_I_ATOM_F1:
277 case ETM4_PKT_I_ATOM_F2:
278 case ETM4_PKT_I_ATOM_F3:
279 case ETM4_PKT_I_ATOM_F4:
280 case ETM4_PKT_I_ATOM_F5:
281 case ETM4_PKT_I_ATOM_F6:
283 if (m_P0_stack.createAtomElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getAtom()) == 0)
286 m_curr_spec_depth += m_curr_packet_in->getAtom().num;
290 case ETM4_PKT_I_CTXT:
292 if (m_P0_stack.createContextElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getContext()) == 0)
297 case ETM4_PKT_I_ADDR_MATCH:
299 etmv4_addr_val_t addr;
301 addr.val = m_curr_packet_in->getAddrVal();
302 addr.isa = m_curr_packet_in->getAddrIS();
303 if (m_P0_stack.createAddrElem(m_curr_packet_in->getType(), m_index_curr_pkt, addr) == 0)
309 case ETM4_PKT_I_ADDR_CTXT_L_64IS0:
310 case ETM4_PKT_I_ADDR_CTXT_L_64IS1:
311 case ETM4_PKT_I_ADDR_CTXT_L_32IS0:
312 case ETM4_PKT_I_ADDR_CTXT_L_32IS1:
314 if (m_P0_stack.createContextElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getContext()) == 0)
317 case ETM4_PKT_I_ADDR_L_32IS0:
318 case ETM4_PKT_I_ADDR_L_32IS1:
319 case ETM4_PKT_I_ADDR_L_64IS0:
320 case ETM4_PKT_I_ADDR_L_64IS1:
321 case ETM4_PKT_I_ADDR_S_IS0:
322 case ETM4_PKT_I_ADDR_S_IS1:
324 etmv4_addr_val_t addr;
326 addr.val = m_curr_packet_in->getAddrVal();
327 addr.isa = m_curr_packet_in->getAddrIS();
328 if (m_P0_stack.createAddrElem(m_curr_packet_in->getType(), m_index_curr_pkt, addr) == 0)
335 case ETM4_PKT_I_EXCEPT:
337 if (m_P0_stack.createExceptElem(m_curr_packet_in->getType(), m_index_curr_pkt,
338 (m_curr_packet_in->exception_info.addr_interp == 0x2),
339 m_curr_packet_in->exception_info.exceptionType) == 0)
343 m_except_pending_addr = true; // wait for following packets before marking for commit.
349 case ETM4_PKT_I_EXCEPT_RTN:
351 // P0 element if V7M profile.
352 bool bV7MProfile = (m_config->archVersion() == ARCH_V7) && (m_config->coreProfile() == profile_CortexM);
353 if (m_P0_stack.createParamElemNoParam(P0_EXCEP_RET, bV7MProfile, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
355 else if (bV7MProfile)
360 case ETM4_PKT_I_FUNC_RET:
362 // P0 element iff V8M profile, otherwise ignore
363 if (OCSD_IS_V8_ARCH(m_config->archVersion()) && (m_config->coreProfile() == profile_CortexM))
365 if (m_P0_stack.createParamElemNoParam(P0_FUNC_RET, true, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
374 case ETM4_PKT_I_EVENT:
376 std::vector<uint32_t> params = { 0 };
377 params[0] = (uint32_t)m_curr_packet_in->event_val;
378 if (m_P0_stack.createParamElem(P0_EVENT, false, m_curr_packet_in->getType(), m_index_curr_pkt, params) == 0)
384 /* cycle count packets */
385 case ETM4_PKT_I_CCNT_F1:
386 case ETM4_PKT_I_CCNT_F2:
387 case ETM4_PKT_I_CCNT_F3:
389 std::vector<uint32_t> params = { 0 };
390 params[0] = m_curr_packet_in->getCC();
391 if (m_P0_stack.createParamElem(P0_CC, false, m_curr_packet_in->getType(), m_index_curr_pkt, params) == 0)
398 case ETM4_PKT_I_TIMESTAMP:
400 bool bTSwithCC = m_config->enabledCCI();
401 uint64_t ts = m_curr_packet_in->getTS();
402 std::vector<uint32_t> params = { 0, 0, 0 };
403 params[0] = (uint32_t)(ts & 0xFFFFFFFF);
404 params[1] = (uint32_t)((ts >> 32) & 0xFFFFFFFF);
406 params[2] = m_curr_packet_in->getCC();
407 if (m_P0_stack.createParamElem(bTSwithCC ? P0_TS_CC : P0_TS, false, m_curr_packet_in->getType(), m_index_curr_pkt, params) == 0)
413 case ETM4_PKT_I_BAD_SEQUENCE:
414 resp = handleBadPacket("Bad byte sequence in packet.");
417 case ETM4_PKT_I_BAD_TRACEMODE:
418 resp = handleBadPacket("Invalid packet type for trace mode.");
421 case ETM4_PKT_I_RESERVED:
422 resp = handleBadPacket("Reserved packet header");
425 /*** presently unsupported packets ***/
426 /* conditional instruction tracing */
427 case ETM4_PKT_I_COND_FLUSH:
428 case ETM4_PKT_I_COND_I_F1:
429 case ETM4_PKT_I_COND_I_F2:
430 case ETM4_PKT_I_COND_I_F3:
431 case ETM4_PKT_I_COND_RES_F1:
432 case ETM4_PKT_I_COND_RES_F2:
433 case ETM4_PKT_I_COND_RES_F3:
434 case ETM4_PKT_I_COND_RES_F4:
436 case ETM4_PKT_I_CANCEL_F1:
437 case ETM4_PKT_I_CANCEL_F2:
438 case ETM4_PKT_I_CANCEL_F3:
439 case ETM4_PKT_I_COMMIT:
440 case ETM4_PKT_I_MISPREDICT:
441 case ETM4_PKT_I_DISCARD:
442 // data synchronisation markers
443 case ETM4_PKT_I_NUM_DS_MKR:
444 case ETM4_PKT_I_UNNUM_DS_MKR:
447 resp = OCSD_RESP_FATAL_INVALID_DATA;
448 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_DECODE_PKT,"Unsupported packet type."));
452 // any other packet - bad packet error
453 resp = OCSD_RESP_FATAL_INVALID_DATA;
454 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_DECODE_PKT,"Unknown packet type."));
459 // we need to wait for following address after exception
460 // - work out if we have seen enough here...
461 if(m_except_pending_addr && !is_except)
463 m_except_pending_addr = false; //next packet has to be an address
464 // exception packet sequence complete
467 m_curr_spec_depth++; // exceptions are P0 elements so up the spec depth to commit if needed.
471 resp = OCSD_RESP_FATAL_INVALID_DATA;
472 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_DECODE_PKT,"Expected Address packet to follow exception packet."));
478 resp = OCSD_RESP_FATAL_SYS_ERR;
479 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_MEM,"Memory allocation error."));
481 else if(m_curr_spec_depth > m_max_spec_depth)
483 // auto commit anything above max spec depth
484 // (this will auto commit anything if spec depth not supported!)
485 m_P0_commit = m_curr_spec_depth - m_max_spec_depth;
486 m_curr_state = COMMIT_ELEM;
487 Complete = false; // force the processing of the commit elements.
492 void TrcPktDecodeEtmV4I::doTraceInfoPacket()
494 m_trace_info = m_curr_packet_in->getTraceInfo();
495 m_cc_threshold = m_curr_packet_in->getCCThreshold();
496 m_p0_key = m_curr_packet_in->getP0Key();
497 m_curr_spec_depth = m_curr_packet_in->getCurrSpecDepth();
501 * Walks through the element stack, processing from oldest element to the newest,
502 according to the number of P0 elements that need committing.
504 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::commitElements(bool &Complete)
506 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
507 bool bPause = false; // pause commit operation
508 bool bPopElem = true; // do we remove the element from the stack (multi atom elements may need to stay!)
509 int num_commit_req = m_P0_commit;
510 ocsd_trc_index_t err_idx = 0;
512 Complete = true; // assume we exit due to completion of commit operation
514 TrcStackElem *pElem = 0; // stacked element pointer
516 while(m_P0_commit && !bPause)
518 if(m_P0_stack.size() > 0)
520 pElem = m_P0_stack.back(); // get oldest element
521 err_idx = pElem->getRootIndex(); // save index in case of error.
523 switch (pElem->getP0Type())
525 // indicates a trace restart - beginning of trace or discontinuiuty
527 m_output_elem.setType(OCSD_GEN_TRC_ELEM_TRACE_ON);
528 m_output_elem.trace_on_reason = m_prev_overflow ? TRACE_ON_OVERFLOW : TRACE_ON_NORMAL;
529 m_prev_overflow = false;
530 resp = outputTraceElementIdx(pElem->getRootIndex(),m_output_elem);
531 m_return_stack.flush();
536 TrcStackElemAddr *pAddrElem = dynamic_cast<TrcStackElemAddr *>(pElem);
537 m_return_stack.clear_pop_pending(); // address removes the need to pop the indirect address target from the stack
540 SetInstrInfoInAddrISA(pAddrElem->getAddr().val, pAddrElem->getAddr().isa);
548 TrcStackElemCtxt *pCtxtElem = dynamic_cast<TrcStackElemCtxt *>(pElem);
551 etmv4_context_t ctxt = pCtxtElem->getContext();
552 // check this is an updated context
555 updateContext(pCtxtElem);
557 m_output_elem.setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
558 resp = outputTraceElementIdx(pElem->getRootIndex(),m_output_elem);
566 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
568 resp = this->outputEvent(pParamElem);
574 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
576 resp = outputTS(pParamElem,false);
582 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
584 resp = outputCC(pParamElem);
590 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
592 resp = outputTS(pParamElem,true);
597 m_prev_overflow = true;
602 TrcStackElemAtom *pAtomElem = dynamic_cast<TrcStackElemAtom *>(pElem);
606 bool bContProcess = true;
607 while(!pAtomElem->isEmpty() && m_P0_commit && bContProcess)
609 ocsd_atm_val atom = pAtomElem->commitOldest();
611 // check if prev atom left us an indirect address target on the return stack
612 if ((resp = returnStackPop()) != OCSD_RESP_CONT)
615 // if address and context do instruction trace follower.
616 // otherwise skip atom and reduce committed elements
617 if(!m_need_ctxt && !m_need_addr)
619 resp = processAtom(atom,bContProcess);
621 m_P0_commit--; // mark committed
623 if(!pAtomElem->isEmpty())
624 bPopElem = false; // don't remove if still atoms to process.
630 // check if prev atom left us an indirect address target on the return stack
631 if ((resp = returnStackPop()) != OCSD_RESP_CONT)
634 m_excep_info.proc = EXCEP_POP; // set state in case we need to stop part way through
635 resp = processException(); // output trace + exception elements.
640 m_output_elem.setType(OCSD_GEN_TRC_ELEM_EXCEPTION_RET);
641 resp = outputTraceElementIdx(pElem->getRootIndex(),m_output_elem);
642 if(pElem->isP0()) // are we on a core that counts ERET as P0?
647 // func ret is V8M - data trace only - hint that data has been popped off the stack.
648 // at this point nothing to do till the decoder starts handling data trace.
655 m_P0_stack.delete_back(); // remove element from stack;
657 // if response not continue, then break out of the loop.
658 if(!OCSD_DATA_RESP_IS_CONT(resp))
665 // too few elements for commit operation - decode error.
666 resp = OCSD_RESP_FATAL_INVALID_DATA;
667 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_COMMIT_PKT_OVERRUN,err_idx,m_CSID,"Not enough elements to commit"));
672 // done all elements - need more packets.
674 m_curr_state = DECODE_PKTS;
676 // reduce the spec depth by number of comitted elements
677 m_curr_spec_depth -= (num_commit_req-m_P0_commit);
681 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::returnStackPop()
683 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
686 if (m_return_stack.pop_pending())
688 ocsd_vaddr_t popAddr = m_return_stack.pop(nextISA);
689 if (m_return_stack.overflow())
691 resp = OCSD_RESP_FATAL_INVALID_DATA;
692 LogError(ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_RET_STACK_OVERFLOW, "Trace Return Stack Overflow."));
696 m_instr_info.instr_addr = popAddr;
697 m_instr_info.isa = nextISA;
704 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::flushEOT()
706 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
709 TrcStackElem *pElem = 0;
710 while(OCSD_DATA_RESP_IS_CONT(resp) && (m_P0_stack.size() > 0))
712 // scan for outstanding events, TS and CC, before any outstanding
713 // P0 commit elements.
714 pElem = m_P0_stack.back();
716 switch(pElem->getP0Type())
718 // clear stack and stop
725 m_P0_stack.delete_all();
736 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
738 resp = this->outputEvent(pParamElem);
744 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
746 resp = outputTS(pParamElem,false);
752 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
754 resp = outputCC(pParamElem);
760 TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
762 resp = outputTS(pParamElem,true);
766 m_P0_stack.delete_back();
769 if(OCSD_DATA_RESP_IS_CONT(resp) && (m_P0_stack.size() == 0))
771 m_output_elem.setType(OCSD_GEN_TRC_ELEM_EO_TRACE);
772 resp = outputTraceElement(m_output_elem);
779 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::outputCC(TrcStackElemParam *pParamElem)
781 m_output_elem.setType(OCSD_GEN_TRC_ELEM_CYCLE_COUNT);
782 m_output_elem.setCycleCount(pParamElem->getParam(0));
783 return outputTraceElementIdx(pParamElem->getRootIndex(),m_output_elem);
786 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::outputTS(TrcStackElemParam *pParamElem, bool withCC)
788 m_output_elem.setType(OCSD_GEN_TRC_ELEM_TIMESTAMP);
789 m_output_elem.timestamp = (uint64_t)(pParamElem->getParam(0)) | (((uint64_t)pParamElem->getParam(1)) << 32);
791 m_output_elem.setCycleCount(pParamElem->getParam(2));
792 return outputTraceElementIdx(pParamElem->getRootIndex(),m_output_elem);
795 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::outputEvent(TrcStackElemParam *pParamElem)
797 m_output_elem.setType(OCSD_GEN_TRC_ELEM_EVENT);
798 m_output_elem.trace_event.ev_type = EVENT_NUMBERED;
799 m_output_elem.trace_event.ev_number = pParamElem->getParam(0);
800 return outputTraceElementIdx(pParamElem->getRootIndex(),m_output_elem);
803 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::outputTraceRange(const bool executed, ocsd_trc_index_t index)
805 m_output_elem.setType(OCSD_GEN_TRC_ELEM_INSTR_RANGE);
806 m_output_elem.setLastInstrInfo(executed, m_instr_info.type, m_instr_info.sub_type, m_instr_info.instr_size);
807 m_output_elem.setISA(m_instr_info.isa);
808 m_output_elem.setLastInstrCond(m_instr_info.is_conditional);
810 m_instr_info.isa = m_instr_info.next_isa;
811 return outputTraceElementIdx(index, m_output_elem);
814 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::processAtom(const ocsd_atm_val atom, bool &bCont)
816 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
817 TrcStackElem *pElem = m_P0_stack.back(); // get the atom element
818 bool bWPFound = false;
822 err = traceInstrToWP(bWPFound);
825 if(err == OCSD_ERR_UNSUPPORTED_ISA)
829 LogError(ocsdError(OCSD_ERR_SEV_WARN,err,pElem->getRootIndex(),m_CSID,"Warning: unsupported instruction set processing atom packet."));
830 // wait for next context
836 resp = OCSD_RESP_FATAL_INVALID_DATA;
837 LogError(ocsdError(OCSD_ERR_SEV_ERROR,err,pElem->getRootIndex(),m_CSID,"Error processing atom packet."));
844 // save recorded next instuction address
845 ocsd_vaddr_t nextAddr = m_instr_info.instr_addr;
847 // action according to waypoint type and atom value
848 switch(m_instr_info.type)
853 m_instr_info.instr_addr = m_instr_info.branch_addr;
854 if (m_instr_info.is_link)
855 m_return_stack.push(nextAddr, m_instr_info.isa);
860 case OCSD_INSTR_BR_INDIRECT:
863 m_need_addr = true; // indirect branch taken - need new address.
864 if (m_instr_info.is_link)
865 m_return_stack.push(nextAddr,m_instr_info.isa);
866 m_return_stack.set_pop_pending(); // need to know next packet before we know what is to happen
870 resp = outputTraceRange((atom == ATOM_E), pElem->getRootIndex());
875 // no waypoint - likely inaccessible memory range.
876 m_need_addr = true; // need an address update
878 if(m_output_elem.st_addr != m_output_elem.en_addr)
880 // some trace before we were out of memory access range
881 resp = outputTraceRange(true, pElem->getRootIndex());
884 if(m_mem_nacc_pending && OCSD_DATA_RESP_IS_CONT(resp))
886 m_output_elem.setType(OCSD_GEN_TRC_ELEM_ADDR_NACC);
887 m_output_elem.st_addr = m_nacc_addr;
888 resp = outputTraceElementIdx(pElem->getRootIndex(),m_output_elem);
889 m_mem_nacc_pending = false;
893 if(!OCSD_DATA_RESP_IS_CONT(resp))
899 // Exception processor
900 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::processException()
902 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
903 TrcStackElemExcept *pExceptElem;
905 m_excep_info.addr_b_tgt = false;
907 if(m_excep_info.proc == EXCEP_POP)
909 pExceptElem = dynamic_cast<TrcStackElemExcept *>(m_P0_stack.back()); // get the exception element
910 TrcStackElemAddr *pAddressElem = 0;
911 TrcStackElemCtxt *pCtxtElem = 0;
912 TrcStackElem *pElem = 0;
914 m_P0_stack.pop_back(); // remove the exception element
915 pElem = m_P0_stack.back(); // look at next element.
916 if(pElem->getP0Type() == P0_CTXT)
918 pCtxtElem = dynamic_cast<TrcStackElemCtxt *>(pElem);
919 m_P0_stack.pop_back(); // remove the context element
920 pElem = m_P0_stack.back(); // next one should be an address element
923 if(pElem->getP0Type() != P0_ADDR)
925 // no following address element - indicate processing error.
926 resp = OCSD_RESP_FATAL_INVALID_DATA;
927 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_PACKET_SEQ,pExceptElem->getRootIndex(),m_CSID,"Address missing in exception packet."));
932 pAddressElem = static_cast<TrcStackElemAddr *>(pElem);
934 // fill in exception info for use later
935 m_excep_info.addr = pAddressElem->getAddr();
936 m_excep_info.number = pExceptElem->getExcepNum();
937 m_excep_info.index = pExceptElem->getRootIndex();
938 m_excep_info.addr_b_tgt = pExceptElem->getPrevSame();
940 // see if there is an address + optional context element implied
941 // prior to the exception.
942 if (m_excep_info.addr_b_tgt)
944 // this was a branch target address - update current setting
945 bool b64bit = m_instr_info.isa == ocsd_isa_aarch64;
947 b64bit = pCtxtElem->getContext().SF;
949 m_instr_info.instr_addr = m_excep_info.addr.val;
950 m_instr_info.isa = (m_excep_info.addr.isa == 0) ?
951 (b64bit ? ocsd_isa_aarch64 : ocsd_isa_arm) : ocsd_isa_thumb2;
955 // figure out next move
957 m_excep_info.proc = EXCEP_CTXT;
958 updateContext(pCtxtElem);
960 else if(m_excep_info.addr.val == m_instr_info.instr_addr)
961 m_excep_info.proc = EXCEP_EXCEP;
963 m_excep_info.proc = EXCEP_RANGE;
965 m_P0_stack.delete_popped();
968 // output a context element
969 if (m_excep_info.proc == EXCEP_CTXT)
971 m_output_elem.setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
972 resp = outputTraceElementIdx(m_excep_info.index, m_output_elem);
973 m_excep_info.proc = EXCEP_EXCEP;
974 if (!OCSD_DATA_RESP_IS_CONT(resp))
978 // output a range element
979 if(m_excep_info.proc == EXCEP_RANGE)
981 bool bWPFound = false;
984 // last instr_info address is the start address
985 m_output_elem.st_addr = m_instr_info.instr_addr;
987 // look for match to return address.
988 err = traceInstrToWP(bWPFound,true,m_excep_info.addr.val);
992 if(err == OCSD_ERR_UNSUPPORTED_ISA)
996 LogError(ocsdError(OCSD_ERR_SEV_WARN,err,m_excep_info.index,m_CSID,"Warning: unsupported instruction set processing exception packet."));
1000 resp = OCSD_RESP_FATAL_INVALID_DATA;
1001 LogError(ocsdError(OCSD_ERR_SEV_ERROR,err,m_excep_info.index,m_CSID,"Error processing exception packet."));
1002 m_excep_info.proc = EXCEP_POP; // nothing more to do, reset to start of exception handling
1008 // waypoint address found - output range
1009 resp = outputTraceRange(true, m_excep_info.index);
1010 m_excep_info.proc = EXCEP_EXCEP;
1014 // no waypoint - likely inaccessible memory range.
1015 m_need_addr = true; // need an address update
1017 if(m_output_elem.st_addr != m_output_elem.en_addr)
1019 // some trace before we were out of memory access range
1020 resp = outputTraceRange(true, m_excep_info.index);
1023 m_excep_info.proc = m_mem_nacc_pending ? EXCEP_NACC : EXCEP_EXCEP;
1027 if((m_excep_info.proc == EXCEP_NACC) && OCSD_DATA_RESP_IS_CONT(resp))
1029 m_output_elem.setType(OCSD_GEN_TRC_ELEM_ADDR_NACC);
1030 m_output_elem.st_addr = m_nacc_addr;
1031 resp = outputTraceElementIdx(m_excep_info.index,m_output_elem);
1032 m_excep_info.proc = EXCEP_EXCEP;
1033 m_mem_nacc_pending = false;
1036 if((m_excep_info.proc == EXCEP_EXCEP) && OCSD_DATA_RESP_IS_CONT(resp))
1039 m_output_elem.setType(OCSD_GEN_TRC_ELEM_EXCEPTION);
1040 // add end address as preferred return address to end addr in element
1041 m_output_elem.en_addr = m_excep_info.addr.val;
1042 m_output_elem.excep_ret_addr = 1;
1043 m_output_elem.excep_ret_addr_br_tgt = m_excep_info.addr_b_tgt;
1044 m_output_elem.exception_number = m_excep_info.number;
1045 resp = outputTraceElementIdx(m_excep_info.index,m_output_elem);
1046 m_excep_info.proc = EXCEP_POP;
1051 void TrcPktDecodeEtmV4I::SetInstrInfoInAddrISA(const ocsd_vaddr_t addr_val, const uint8_t isa)
1053 m_instr_info.instr_addr = addr_val;
1055 m_instr_info.isa = ocsd_isa_aarch64;
1057 m_instr_info.isa = (isa == 0) ? ocsd_isa_arm : ocsd_isa_thumb2;
1060 // trace an instruction range to a waypoint - and set next address to restart from.
1061 ocsd_err_t TrcPktDecodeEtmV4I::traceInstrToWP(bool &bWPFound, const bool traceToAddrNext /*= false*/, const ocsd_vaddr_t nextAddrMatch /*= 0*/)
1065 ocsd_err_t err = OCSD_OK;
1067 // TBD?: update mem space to allow for EL as well.
1068 ocsd_mem_space_acc_t mem_space = m_is_secure ? OCSD_MEM_SPACE_S : OCSD_MEM_SPACE_N;
1070 m_output_elem.st_addr = m_output_elem.en_addr = m_instr_info.instr_addr;
1071 m_output_elem.num_instr_range = 0;
1075 while(!bWPFound && !m_mem_nacc_pending)
1077 // start off by reading next opcode;
1079 err = accessMemory(m_instr_info.instr_addr,mem_space,&bytesReq,(uint8_t *)&opcode);
1080 if(err != OCSD_OK) break;
1082 if(bytesReq == 4) // got data back
1084 m_instr_info.opcode = opcode;
1085 err = instrDecode(&m_instr_info);
1086 if(err != OCSD_OK) break;
1088 // increment address - may be adjusted by direct branch value later
1089 m_instr_info.instr_addr += m_instr_info.instr_size;
1091 // update the range decoded address in the output packet.
1092 m_output_elem.en_addr = m_instr_info.instr_addr;
1093 m_output_elem.num_instr_range++;
1095 // either walking to match the next instruction address or a real watchpoint
1097 bWPFound = (m_output_elem.en_addr == nextAddrMatch);
1099 bWPFound = (m_instr_info.type != OCSD_INSTR_OTHER);
1103 // not enough memory accessible.
1104 m_mem_nacc_pending = true;
1105 m_nacc_addr = m_instr_info.instr_addr;
1111 void TrcPktDecodeEtmV4I::updateContext(TrcStackElemCtxt *pCtxtElem)
1113 etmv4_context_t ctxt = pCtxtElem->getContext();
1114 // map to output element and local saved state.
1115 m_is_64bit = (ctxt.SF != 0);
1116 m_output_elem.context.bits64 = ctxt.SF;
1117 m_is_secure = (ctxt.NS == 0);
1118 m_output_elem.context.security_level = ctxt.NS ? ocsd_sec_nonsecure : ocsd_sec_secure;
1119 m_output_elem.context.exception_level = (ocsd_ex_level)ctxt.EL;
1120 m_output_elem.context.el_valid = 1;
1123 m_output_elem.context.ctxt_id_valid = 1;
1124 m_context_id = m_output_elem.context.context_id = ctxt.ctxtID;
1128 m_output_elem.context.vmid_valid = 1;
1129 m_vmid_id = m_output_elem.context.vmid = ctxt.VMID;
1131 m_need_ctxt = false;
1134 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::handleBadPacket(const char *reason)
1136 ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
1138 if(getComponentOpMode() && OCSD_OPFLG_PKTDEC_ERROR_BAD_PKTS)
1140 // error out - stop decoding
1141 resp = OCSD_RESP_FATAL_INVALID_DATA;
1142 LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_DECODE_PKT,reason));
1146 // switch to unsync - clear decode state
1147 m_output_elem.setType(OCSD_GEN_TRC_ELEM_NO_SYNC);
1148 resp = outputTraceElement(m_output_elem);
1150 m_curr_state = WAIT_SYNC;
1155 /* End of File trc_pkt_decode_etmv4i.cpp */