• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * \file       trc_pkt_decode_etmv4i.cpp
3  * \brief      OpenCSD : ETMv4 decoder
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 /*
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "opencsd/etmv4/trc_pkt_decode_etmv4i.h"
36 
37 #include "common/trc_gen_elem.h"
38 
39 
40 #define DCD_NAME "DCD_ETMV4"
41 
42 static const uint32_t ETMV4_SUPPORTED_DECODE_OP_FLAGS = OCSD_OPFLG_PKTDEC_COMMON;
43 
TrcPktDecodeEtmV4I()44 TrcPktDecodeEtmV4I::TrcPktDecodeEtmV4I()
45     : TrcPktDecodeBase(DCD_NAME)
46 {
47     initDecoder();
48 }
49 
TrcPktDecodeEtmV4I(int instIDNum)50 TrcPktDecodeEtmV4I::TrcPktDecodeEtmV4I(int instIDNum)
51     : TrcPktDecodeBase(DCD_NAME,instIDNum)
52 {
53     initDecoder();
54 }
55 
~TrcPktDecodeEtmV4I()56 TrcPktDecodeEtmV4I::~TrcPktDecodeEtmV4I()
57 {
58 }
59 
60 /*********************** implementation packet decoding interface */
61 
processPacket()62 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::processPacket()
63 {
64     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
65     ocsd_err_t err = OCSD_OK;
66     bool bPktDone = false;
67 
68     while(!bPktDone)
69     {
70         switch (m_curr_state)
71         {
72         case NO_SYNC:
73             // output the initial not synced packet to the sink
74             err = m_out_elem.resetElemStack();
75             if (!err)
76                 err = m_out_elem.addElemType(m_index_curr_pkt, OCSD_GEN_TRC_ELEM_NO_SYNC);
77             if (!err)
78             {
79                 outElem().setUnSyncEOTReason(m_unsync_eot_info);
80                 resp = m_out_elem.sendElements();
81                 m_curr_state = WAIT_SYNC;
82             }
83             else
84                 resp = OCSD_RESP_FATAL_SYS_ERR;
85 
86             // fall through to check if the current packet is the async we are waiting for.
87             break;
88 
89         case WAIT_SYNC:
90             if(m_curr_packet_in->getType() == ETM4_PKT_I_ASYNC)
91                 m_curr_state = WAIT_TINFO;
92             bPktDone = true;
93             break;
94 
95         case WAIT_TINFO:
96             m_need_ctxt = true;
97             m_need_addr = true;
98             if(m_curr_packet_in->getType() == ETM4_PKT_I_TRACE_INFO)
99             {
100                 doTraceInfoPacket();
101                 m_curr_state = DECODE_PKTS;
102                 m_return_stack.flush();
103             }
104             bPktDone = true;
105             break;
106 
107         case DECODE_PKTS:
108             // this may change the state to RESOLVE_ELEM if required;
109             err = decodePacket();
110             if (err)
111             {
112 #ifdef OCSD_WARN_UNSUPPORTED
113                 if (err == OCSD_ERR_UNSUPP_DECODE_PKT)
114                     resp = OCSD_RESP_WARN_CONT;
115                 else
116 #else
117                 resp = OCSD_RESP_FATAL_INVALID_DATA;
118 #endif
119 
120                 bPktDone = true;
121             }
122             else if (m_curr_state != RESOLVE_ELEM)
123                 bPktDone = true;
124             break;
125 
126         case RESOLVE_ELEM:
127             // this will change the state to DECODE_PKTS once required elem resolved &
128             // needed generic packets output
129             resp = resolveElements();
130             if ((m_curr_state == DECODE_PKTS) || (!OCSD_DATA_RESP_IS_CONT(resp)))
131                 bPktDone = true;
132             break;
133         }
134     }
135     return resp;
136 }
137 
onEOT()138 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::onEOT()
139 {
140     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
141     ocsd_err_t err;
142     if ((err = commitElemOnEOT()) != OCSD_OK)
143     {
144         resp = OCSD_RESP_FATAL_INVALID_DATA;
145         LogError(ocsdError(OCSD_ERR_SEV_ERROR, err, "Error flushing element stack at end of trace data."));
146     }
147     else
148         resp = m_out_elem.sendElements();
149     return resp;
150 }
151 
onReset()152 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::onReset()
153 {
154     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
155     m_unsync_eot_info = UNSYNC_RESET_DECODER;
156     resetDecoder();
157     return resp;
158 }
159 
onFlush()160 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::onFlush()
161 {
162     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
163 
164     if (m_curr_state == RESOLVE_ELEM)
165         resp = resolveElements();
166     else
167         resp = m_out_elem.sendElements();
168     return resp;
169 }
170 
onProtocolConfig()171 ocsd_err_t TrcPktDecodeEtmV4I::onProtocolConfig()
172 {
173     ocsd_err_t err = OCSD_OK;
174 
175     // set some static config elements
176     m_CSID = m_config->getTraceID();
177     m_max_spec_depth = m_config->MaxSpecDepth();
178 
179     // elements associated with data trace
180 #ifdef DATA_TRACE_SUPPORTED
181     m_p0_key_max = m_config->P0_Key_Max();
182     m_cond_key_max_incr = m_config->CondKeyMaxIncr();
183 #endif
184 
185     m_out_elem.initCSID(m_CSID);
186 
187     // set up static trace instruction decode elements
188     m_instr_info.dsb_dmb_waypoints = 0;
189     m_instr_info.wfi_wfe_branch = m_config->wfiwfeBranch() ? 1 : 0;
190     m_instr_info.pe_type.arch = m_config->archVersion();
191     m_instr_info.pe_type.profile = m_config->coreProfile();
192 
193     m_IASize64 = (m_config->iaSizeMax() == 64);
194 
195     if (m_config->enabledRetStack())
196     {
197         m_return_stack.set_active(true);
198 #ifdef TRC_RET_STACK_DEBUG
199         m_return_stack.set_dbg_logger(this);
200 #endif
201     }
202 
203     // check config compatible with current decoder support level.
204     // at present no data trace, no spec depth, no return stack, no QE
205     // Remove these checks as support is added.
206     if(m_config->enabledDataTrace())
207     {
208         err = OCSD_ERR_HW_CFG_UNSUPP;
209         LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : Data trace elements not supported"));
210     }
211     else if(m_config->enabledLSP0Trace())
212     {
213         err = OCSD_ERR_HW_CFG_UNSUPP;
214         LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : LSP0 elements not supported."));
215     }
216     else if(m_config->enabledCondITrace() != EtmV4Config::COND_TR_DIS)
217     {
218         err = OCSD_ERR_HW_CFG_UNSUPP;
219         LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : Trace on conditional non-branch elements not supported."));
220     }
221     else if(m_config->enabledQE())
222     {
223         err = OCSD_ERR_HW_CFG_UNSUPP;
224         LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv4 instruction decode : Trace using Q elements not supported."));
225     }
226     return err;
227 }
228 
229 /************* local decode methods */
initDecoder()230 void TrcPktDecodeEtmV4I::initDecoder()
231 {
232     // set the operational modes supported.
233     m_supported_op_flags = ETMV4_SUPPORTED_DECODE_OP_FLAGS;
234 
235     /* init elements that get set by config */
236     m_max_spec_depth = 0;
237     m_CSID = 0;
238     m_IASize64 = false;
239 
240     // elements associated with data trace
241 #ifdef DATA_TRACE_SUPPORTED
242     m_p0_key_max = 0;
243     m_cond_key_max_incr = 0;
244 #endif
245 
246     // reset decoder state to unsynced
247     m_unsync_eot_info = UNSYNC_INIT_DECODER;
248     resetDecoder();
249 }
250 
resetDecoder()251 void TrcPktDecodeEtmV4I::resetDecoder()
252 {
253     m_curr_state = NO_SYNC;
254     m_timestamp = 0;
255     m_context_id = 0;
256     m_vmid_id = 0;
257     m_is_secure = true;
258     m_is_64bit = false;
259     m_cc_threshold = 0;
260     m_curr_spec_depth = 0;
261     m_need_ctxt = true;
262     m_need_addr = true;
263     m_elem_pending_addr = false;
264     m_prev_overflow = false;
265     m_P0_stack.delete_all();
266     m_out_elem.resetElemStack();
267     m_last_IS = 0;
268     clearElemRes();
269 
270     // elements associated with data trace
271 #ifdef DATA_TRACE_SUPPORTED
272     m_p0_key = 0;
273     m_cond_c_key = 0;
274     m_cond_r_key = 0;
275 #endif
276 }
277 
onFirstInitOK()278 void TrcPktDecodeEtmV4I::onFirstInitOK()
279 {
280     // once init, set the output element interface to the out elem list.
281     m_out_elem.initSendIf(this->getTraceElemOutAttachPt());
282 }
283 
284 // Changes a packet into stack of trace elements - these will be resolved and output later
decodePacket()285 ocsd_err_t TrcPktDecodeEtmV4I::decodePacket()
286 {
287     ocsd_err_t err = OCSD_OK;
288     bool bAllocErr = false;
289     bool is_addr = false;
290 
291     switch(m_curr_packet_in->getType())
292     {
293     case ETM4_PKT_I_ASYNC: // nothing to do with this packet.
294     case ETM4_PKT_I_IGNORE: // or this one.
295         break;
296 
297     case ETM4_PKT_I_TRACE_INFO:
298         // skip subsequent TInfo packets.
299         m_return_stack.flush();
300         break;
301 
302     case ETM4_PKT_I_TRACE_ON:
303         {
304             if (m_P0_stack.createParamElemNoParam(P0_TRC_ON, false, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
305                 bAllocErr = true;
306         }
307         break;
308 
309     case ETM4_PKT_I_ATOM_F1:
310     case ETM4_PKT_I_ATOM_F2:
311     case ETM4_PKT_I_ATOM_F3:
312     case ETM4_PKT_I_ATOM_F4:
313     case ETM4_PKT_I_ATOM_F5:
314     case ETM4_PKT_I_ATOM_F6:
315         {
316             if (m_P0_stack.createAtomElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getAtom()) == 0)
317                 bAllocErr = true;
318             else
319                 m_curr_spec_depth += m_curr_packet_in->getAtom().num;
320         }
321         break;
322 
323     case ETM4_PKT_I_CTXT:
324         {
325             if (m_P0_stack.createContextElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getContext(), m_last_IS) == 0)
326                 bAllocErr = true;
327         }
328         break;
329 
330     case ETM4_PKT_I_ADDR_MATCH:
331         {
332             etmv4_addr_val_t addr;
333 
334             addr.val = m_curr_packet_in->getAddrVal();
335             addr.isa = m_last_IS = m_curr_packet_in->getAddrIS();
336 
337             if (m_P0_stack.createAddrElem(m_curr_packet_in->getType(), m_index_curr_pkt, addr) == 0)
338                 bAllocErr = true;
339             is_addr = true;
340         }
341         break;
342 
343     case ETM4_PKT_I_ADDR_CTXT_L_64IS0:
344     case ETM4_PKT_I_ADDR_CTXT_L_64IS1:
345     case ETM4_PKT_I_ADDR_CTXT_L_32IS0:
346     case ETM4_PKT_I_ADDR_CTXT_L_32IS1:
347         {
348             m_last_IS = m_curr_packet_in->getAddrIS();
349             if (m_P0_stack.createContextElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getContext(), m_last_IS) == 0)
350                 bAllocErr = true;
351         }
352     case ETM4_PKT_I_ADDR_L_32IS0:
353     case ETM4_PKT_I_ADDR_L_32IS1:
354     case ETM4_PKT_I_ADDR_L_64IS0:
355     case ETM4_PKT_I_ADDR_L_64IS1:
356     case ETM4_PKT_I_ADDR_S_IS0:
357     case ETM4_PKT_I_ADDR_S_IS1:
358         {
359             etmv4_addr_val_t addr;
360 
361             addr.val = m_curr_packet_in->getAddrVal();
362             addr.isa = m_last_IS = m_curr_packet_in->getAddrIS();
363 
364             if (m_P0_stack.createAddrElem(m_curr_packet_in->getType(), m_index_curr_pkt, addr) == 0)
365                 bAllocErr = true;
366             is_addr = true;
367         }
368         break;
369 
370     // Exceptions
371     case ETM4_PKT_I_EXCEPT:
372          {
373             if (m_P0_stack.createExceptElem(m_curr_packet_in->getType(), m_index_curr_pkt,
374                                             (m_curr_packet_in->exception_info.addr_interp == 0x2),
375                                             m_curr_packet_in->exception_info.exceptionType) == 0)
376                 bAllocErr = true;
377             else
378                 m_elem_pending_addr = true;  // wait for following packets before marking for commit.
379         }
380         break;
381 
382     case ETM4_PKT_I_EXCEPT_RTN:
383         {
384             // P0 element if V7M profile.
385             bool bV7MProfile = (m_config->archVersion() == ARCH_V7) && (m_config->coreProfile() == profile_CortexM);
386             if (m_P0_stack.createParamElemNoParam(P0_EXCEP_RET, bV7MProfile, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
387                 bAllocErr = true;
388             else if (bV7MProfile)
389                 m_curr_spec_depth++;
390         }
391         break;
392 
393     case ETM4_PKT_I_FUNC_RET:
394         {
395             // P0 element iff V8M profile, otherwise ignore
396             if (OCSD_IS_V8_ARCH(m_config->archVersion()) && (m_config->coreProfile() == profile_CortexM))
397             {
398                 if (m_P0_stack.createParamElemNoParam(P0_FUNC_RET, true, m_curr_packet_in->getType(), m_index_curr_pkt) == 0)
399                     bAllocErr = true;
400                 else
401                     m_curr_spec_depth++;
402             }
403         }
404         break;
405 
406     // event trace
407     case ETM4_PKT_I_EVENT:
408         {
409             std::vector<uint32_t> params = { 0 };
410             params[0] = (uint32_t)m_curr_packet_in->event_val;
411             if (m_P0_stack.createParamElem(P0_EVENT, false, m_curr_packet_in->getType(), m_index_curr_pkt, params) == 0)
412                 bAllocErr = true;
413 
414         }
415         break;
416 
417     /* cycle count packets */
418     case ETM4_PKT_I_CCNT_F1:
419     case ETM4_PKT_I_CCNT_F2:
420     case ETM4_PKT_I_CCNT_F3:
421         {
422             std::vector<uint32_t> params = { 0 };
423             params[0] = m_curr_packet_in->getCC();
424             if (m_P0_stack.createParamElem(P0_CC, false, m_curr_packet_in->getType(), m_index_curr_pkt, params) == 0)
425                 bAllocErr = true;
426 
427         }
428         break;
429 
430     // timestamp
431     case ETM4_PKT_I_TIMESTAMP:
432         {
433             bool bTSwithCC = m_config->enabledCCI();
434             uint64_t ts = m_curr_packet_in->getTS();
435             std::vector<uint32_t> params = { 0, 0, 0 };
436             params[0] = (uint32_t)(ts & 0xFFFFFFFF);
437             params[1] = (uint32_t)((ts >> 32) & 0xFFFFFFFF);
438             if (bTSwithCC)
439                 params[2] = m_curr_packet_in->getCC();
440             if (m_P0_stack.createParamElem(bTSwithCC ? P0_TS_CC : P0_TS, false, m_curr_packet_in->getType(), m_index_curr_pkt, params) == 0)
441                 bAllocErr = true;
442 
443         }
444         break;
445 
446     case ETM4_PKT_I_BAD_SEQUENCE:
447         err = handleBadPacket("Bad byte sequence in packet.");
448         break;
449 
450     case ETM4_PKT_I_BAD_TRACEMODE:
451         err = handleBadPacket("Invalid packet type for trace mode.");
452         break;
453 
454     case ETM4_PKT_I_RESERVED:
455         err = handleBadPacket("Reserved packet header");
456         break;
457 
458     // speculation
459     case ETM4_PKT_I_MISPREDICT:
460     case ETM4_PKT_I_CANCEL_F1_MISPRED:
461     case ETM4_PKT_I_CANCEL_F2:
462     case ETM4_PKT_I_CANCEL_F3:
463         m_elem_res.mispredict = true;
464         if (m_curr_packet_in->getNumAtoms())
465         {
466             if (m_P0_stack.createAtomElem(m_curr_packet_in->getType(), m_index_curr_pkt, m_curr_packet_in->getAtom()) == 0)
467                 bAllocErr = true;
468             else
469                 m_curr_spec_depth += m_curr_packet_in->getNumAtoms();
470         }
471 
472     case ETM4_PKT_I_CANCEL_F1:
473         m_elem_res.P0_cancel = m_curr_packet_in->getCancelElem();
474         break;
475 
476     case ETM4_PKT_I_COMMIT:
477         m_elem_res.P0_commit = m_curr_packet_in->getCommitElem();
478         break;
479 
480     case ETM4_PKT_I_OVERFLOW:
481         m_prev_overflow = true;
482     case ETM4_PKT_I_DISCARD:
483         m_curr_spec_depth = 0;
484         m_elem_res.discard = true;
485         break;
486 
487     /*** presently unsupported packets ***/
488     /* Q elemnts */
489     case ETM4_PKT_I_Q:
490     /* conditional instruction tracing */
491     case ETM4_PKT_I_COND_FLUSH:
492     case ETM4_PKT_I_COND_I_F1:
493     case ETM4_PKT_I_COND_I_F2:
494     case ETM4_PKT_I_COND_I_F3:
495     case ETM4_PKT_I_COND_RES_F1:
496     case ETM4_PKT_I_COND_RES_F2:
497     case ETM4_PKT_I_COND_RES_F3:
498     case ETM4_PKT_I_COND_RES_F4:
499     // data synchronisation markers
500     case ETM4_PKT_I_NUM_DS_MKR:
501     case ETM4_PKT_I_UNNUM_DS_MKR:
502         // all currently unsupported
503         {
504         ocsd_err_severity_t sev = OCSD_ERR_SEV_ERROR;
505 #ifdef OCSD_WARN_UNSUPPORTED
506         sev = OCSD_ERR_SEV_WARN;
507         //resp = OCSD_RESP_WARN_CONT;
508 #else
509         //resp = OCSD_RESP_FATAL_INVALID_DATA;
510 #endif
511         err = OCSD_ERR_UNSUPP_DECODE_PKT;
512         LogError(ocsdError(sev, err, "Data trace releated, unsupported packet type."));
513         }
514         break;
515 
516     default:
517         // any other packet - bad packet error
518         err = OCSD_ERR_BAD_DECODE_PKT;
519         LogError(ocsdError(OCSD_ERR_SEV_ERROR,err,"Unknown packet type."));
520         break;
521     }
522 
523     // we need to wait for following address after certain packets
524     // - work out if we have seen enough here...
525     if (is_addr && m_elem_pending_addr)
526     {
527         m_curr_spec_depth++;  // increase spec depth for element waiting on address.
528         m_elem_pending_addr = false;  // can't be waiting on both
529     }
530 
531     if(bAllocErr)
532     {
533         err = OCSD_ERR_MEM;
534         LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_MEM,"Memory allocation error."));
535     }
536     else if(m_curr_spec_depth > m_max_spec_depth)
537     {
538         // auto commit anything above max spec depth
539         // (this will auto commit anything if spec depth not supported!)
540         m_elem_res.P0_commit = m_curr_spec_depth - m_max_spec_depth;
541     }
542 
543     if (!err && isElemForRes())
544         m_curr_state = RESOLVE_ELEM;
545     return err;
546 }
547 
doTraceInfoPacket()548 void TrcPktDecodeEtmV4I::doTraceInfoPacket()
549 {
550     m_trace_info = m_curr_packet_in->getTraceInfo();
551     m_cc_threshold = m_curr_packet_in->getCCThreshold();
552     m_curr_spec_depth = m_curr_packet_in->getCurrSpecDepth();
553 
554     // elements associated with data trace
555 #ifdef DATA_TRACE_SUPPORTED
556     m_p0_key = m_curr_packet_in->getP0Key();
557 #endif
558 }
559 
560 /* Element resolution
561  * Commit or cancel elements as required
562  * Send any buffered output packets.
563  */
resolveElements()564 ocsd_datapath_resp_t TrcPktDecodeEtmV4I::resolveElements()
565 {
566     ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
567     bool Complete = false;
568 
569     while (!Complete)
570     {
571         if (m_out_elem.numElemToSend())
572             resp = m_out_elem.sendElements();
573         else if (isElemForRes())
574         {
575             ocsd_err_t err = OCSD_OK;
576             if (m_elem_res.P0_commit)
577                 err = commitElements();
578 
579             if (!err && m_elem_res.P0_cancel)
580                 err = cancelElements();
581 
582             if (!err && m_elem_res.mispredict)
583                 err = mispredictAtom();
584 
585             if (!err && m_elem_res.discard)
586                 err = discardElements();
587 
588             if (err != OCSD_OK)
589                 resp = OCSD_RESP_FATAL_INVALID_DATA;
590         }
591 
592         // break out on error or wait request.
593         if (!OCSD_DATA_RESP_IS_CONT(resp))
594             break;
595 
596         // completion is nothing to send and nothing to commit
597         Complete = !m_out_elem.numElemToSend() && !isElemForRes();
598 
599         // done all elements - need more packets.
600         if (Complete) {
601             // if we are still in resolve, the goto decode.
602             if (m_curr_state == RESOLVE_ELEM)
603                 m_curr_state = DECODE_PKTS;
604         }
605     }
606     return resp;
607 }
608 
609 /*
610  * Walks through the element stack, processing from oldest element to the newest,
611    according to the number of P0 elements that need committing.
612    Build a stack of output elements in the process.
613  */
commitElements()614 ocsd_err_t TrcPktDecodeEtmV4I::commitElements()
615 {
616     ocsd_err_t err = OCSD_OK;
617     bool bPopElem = true;       // do we remove the element from the stack (multi atom elements may need to stay!)
618     int num_commit_req = m_elem_res.P0_commit;
619     ocsd_trc_index_t err_idx = 0;
620     TrcStackElem *pElem = 0;    // stacked element pointer
621 
622     err = m_out_elem.resetElemStack();
623 
624     while(m_elem_res.P0_commit && !err)
625     {
626         if (m_P0_stack.size() > 0)
627         {
628             pElem = m_P0_stack.back();  // get oldest element
629             err_idx = pElem->getRootIndex(); // save index in case of error.
630 
631             switch (pElem->getP0Type())
632             {
633             // indicates a trace restart - beginning of trace or discontinuiuty
634             case P0_TRC_ON:
635                 err = m_out_elem.addElemType(pElem->getRootIndex(), OCSD_GEN_TRC_ELEM_TRACE_ON);
636                 if (!err)
637                 {
638                     m_out_elem.getCurrElem().trace_on_reason = m_prev_overflow ? TRACE_ON_OVERFLOW : TRACE_ON_NORMAL;
639                     m_prev_overflow = false;
640                     m_return_stack.flush();
641                 }
642                 break;
643 
644             case P0_ADDR:
645                 {
646                 TrcStackElemAddr *pAddrElem = dynamic_cast<TrcStackElemAddr *>(pElem);
647                 m_return_stack.clear_pop_pending(); // address removes the need to pop the indirect address target from the stack
648                 if(pAddrElem)
649                 {
650                     SetInstrInfoInAddrISA(pAddrElem->getAddr().val, pAddrElem->getAddr().isa);
651                     m_need_addr = false;
652                 }
653                 }
654                 break;
655 
656             case P0_CTXT:
657                 {
658                 TrcStackElemCtxt *pCtxtElem = dynamic_cast<TrcStackElemCtxt *>(pElem);
659                 if(pCtxtElem)
660                 {
661                     etmv4_context_t ctxt = pCtxtElem->getContext();
662                     // check this is an updated context
663                     if(ctxt.updated)
664                     {
665                         err = m_out_elem.addElem(pElem->getRootIndex());
666                         if (!err)
667                             updateContext(pCtxtElem, outElem());
668                     }
669                 }
670                 }
671                 break;
672 
673             case P0_EVENT:
674             case P0_TS:
675             case P0_CC:
676             case P0_TS_CC:
677                 err = processTS_CC_EventElem(pElem);
678                 break;
679 
680             case P0_ATOM:
681                 {
682                 TrcStackElemAtom *pAtomElem = dynamic_cast<TrcStackElemAtom *>(pElem);
683 
684                 if(pAtomElem)
685                 {
686                     while(!pAtomElem->isEmpty() && m_elem_res.P0_commit && !err)
687                     {
688                         ocsd_atm_val atom = pAtomElem->commitOldest();
689 
690                         // check if prev atom left us an indirect address target on the return stack
691                         if ((err = returnStackPop()) != OCSD_OK)
692                             break;
693 
694                         // if address and context do instruction trace follower.
695                         // otherwise skip atom and reduce committed elements
696                         if(!m_need_ctxt && !m_need_addr)
697                         {
698                             err = processAtom(atom);
699                         }
700                         m_elem_res.P0_commit--; // mark committed
701                     }
702                     if(!pAtomElem->isEmpty())
703                         bPopElem = false;   // don't remove if still atoms to process.
704                 }
705                 }
706                 break;
707 
708             case P0_EXCEP:
709                 // check if prev atom left us an indirect address target on the return stack
710                 if ((err = returnStackPop()) != OCSD_OK)
711                     break;
712 
713                 err = processException();  // output trace + exception elements.
714                 m_elem_res.P0_commit--;
715                 break;
716 
717             case P0_EXCEP_RET:
718                 err = m_out_elem.addElemType(pElem->getRootIndex(), OCSD_GEN_TRC_ELEM_EXCEPTION_RET);
719                 if (!err)
720                 {
721                     if (pElem->isP0()) // are we on a core that counts ERET as P0?
722                         m_elem_res.P0_commit--;
723                 }
724                 break;
725 
726             case P0_FUNC_RET:
727                 // func ret is V8M - data trace only - hint that data has been popped off the stack.
728                 // at this point nothing to do till the decoder starts handling data trace.
729                 if (pElem->isP0())
730                     m_elem_res.P0_commit--;
731                 break;
732             }
733 
734             if(bPopElem)
735                 m_P0_stack.delete_back();  // remove element from stack;
736         }
737         else
738         {
739             // too few elements for commit operation - decode error.
740             err = OCSD_ERR_COMMIT_PKT_OVERRUN;
741             LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_COMMIT_PKT_OVERRUN,err_idx,m_CSID,"Not enough elements to commit"));
742         }
743     }
744 
745     // reduce the spec depth by number of comitted elements
746     m_curr_spec_depth -= (num_commit_req-m_elem_res.P0_commit);
747     return err;
748 }
749 
returnStackPop()750 ocsd_err_t TrcPktDecodeEtmV4I::returnStackPop()
751 {
752     ocsd_err_t err = OCSD_OK;
753     ocsd_isa nextISA;
754 
755     if (m_return_stack.pop_pending())
756     {
757         ocsd_vaddr_t popAddr = m_return_stack.pop(nextISA);
758         if (m_return_stack.overflow())
759         {
760             err = OCSD_ERR_RET_STACK_OVERFLOW;
761             LogError(ocsdError(OCSD_ERR_SEV_ERROR, err, "Trace Return Stack Overflow."));
762         }
763         else
764         {
765             m_instr_info.instr_addr = popAddr;
766             m_instr_info.isa = nextISA;
767             m_need_addr = false;
768         }
769     }
770     return err;
771 }
772 
commitElemOnEOT()773 ocsd_err_t TrcPktDecodeEtmV4I::commitElemOnEOT()
774 {
775     ocsd_err_t err = OCSD_OK;
776     TrcStackElem *pElem = 0;
777 
778     // nothing outstanding - reset the stack before we add more
779     if (!m_out_elem.numElemToSend())
780         m_out_elem.resetElemStack();
781 
782     while((m_P0_stack.size() > 0) && !err)
783     {
784         // scan for outstanding events, TS and CC, that appear before any outstanding
785         // uncommited P0 element.
786         pElem = m_P0_stack.back();
787 
788             switch(pElem->getP0Type())
789             {
790                 // clear stack and stop
791             case P0_UNKNOWN:
792             case P0_ATOM:
793             case P0_TRC_ON:
794             case P0_EXCEP:
795             case P0_EXCEP_RET:
796             case P0_OVERFLOW:
797                 m_P0_stack.delete_all();
798                 break;
799 
800             //skip
801         case P0_ADDR:
802         case P0_CTXT:
803             break;
804 
805             // output
806         case P0_EVENT:
807         case P0_TS:
808         case P0_CC:
809         case P0_TS_CC:
810             err = processTS_CC_EventElem(pElem);
811             break;
812         }
813         m_P0_stack.delete_back();
814     }
815 
816     if(!err)
817     {
818         err = m_out_elem.addElemType(m_index_curr_pkt, OCSD_GEN_TRC_ELEM_EO_TRACE);
819         outElem().setUnSyncEOTReason(m_prev_overflow ? UNSYNC_OVERFLOW : UNSYNC_EOT);
820     }
821     return err;
822 }
823 
824 // cancel elements. These not output
cancelElements()825 ocsd_err_t TrcPktDecodeEtmV4I::cancelElements()
826 {
827     ocsd_err_t err = OCSD_OK;
828     bool P0StackDone = false;  // checked all P0 elements on the stack
829     TrcStackElem *pElem = 0;   // stacked element pointer
830     EtmV4P0Stack temp;
831     int num_cancel_req = m_elem_res.P0_cancel;
832 
833     while (m_elem_res.P0_cancel)
834     {
835         //search the stack for the newest elements
836         if (!P0StackDone)
837         {
838             if (m_P0_stack.size() == 0)
839                 P0StackDone = true;
840             else
841             {
842                 // get the newest element
843                 pElem = m_P0_stack.front();
844                 if (pElem->isP0()) {
845                     if (pElem->getP0Type() == P0_ATOM)
846                     {
847                         TrcStackElemAtom *pAtomElem = (TrcStackElemAtom *)pElem;
848                         // atom - cancel N atoms
849                         m_elem_res.P0_cancel -= pAtomElem->cancelNewest(m_elem_res.P0_cancel);
850                         if (pAtomElem->isEmpty())
851                             m_P0_stack.delete_front();  // remove the element
852                     }
853                     else
854                     {
855                         m_elem_res.P0_cancel--;
856                         m_P0_stack.delete_front();  // remove the element
857                     }
858                 } else {
859                 // not P0, make a keep / remove decision
860                     switch (pElem->getP0Type())
861                     {
862                     // keep these
863                     case P0_EVENT:
864                     case P0_TS:
865                     case P0_CC:
866                     case P0_TS_CC:
867                         m_P0_stack.pop_front(false);
868                         temp.push_back(pElem);
869                         break;
870 
871                     default:
872                         m_P0_stack.delete_front();
873                         break;
874                     }
875                 }
876             }
877         }
878         // may have some unseen elements
879         else if (m_unseen_spec_elem)
880         {
881             m_unseen_spec_elem--;
882             m_elem_res.P0_cancel--;
883         }
884         // otherwise we have some sort of overrun
885         else
886         {
887             // too few elements for commit operation - decode error.
888             err = OCSD_ERR_COMMIT_PKT_OVERRUN;
889             LogError(ocsdError(OCSD_ERR_SEV_ERROR, err, m_index_curr_pkt, m_CSID, "Not enough elements to cancel"));
890             m_elem_res.P0_cancel = 0;
891             break;
892         }
893 
894         if (temp.size())
895         {
896             while (temp.size())
897             {
898                 pElem = temp.back();
899                 m_P0_stack.push_front(pElem);
900                 temp.pop_back(false);
901             }
902         }
903     }
904     m_curr_spec_depth -= num_cancel_req - m_elem_res.P0_cancel;
905     return err;
906 }
907 
908 // mispredict an atom
mispredictAtom()909 ocsd_err_t TrcPktDecodeEtmV4I::mispredictAtom()
910 {
911     ocsd_err_t err = OCSD_OK;
912     bool bFoundAtom = false, bDone = false;
913     TrcStackElem *pElem = 0;
914 
915     m_P0_stack.from_front_init();   // init iterator at front.
916     while (!bDone)
917     {
918         pElem = m_P0_stack.from_front_next();
919         if (pElem)
920         {
921             if (pElem->getP0Type() == P0_ATOM)
922             {
923                 TrcStackElemAtom *pAtomElem = dynamic_cast<TrcStackElemAtom *>(pElem);
924                 if (pAtomElem)
925                 {
926                     pAtomElem->mispredictNewest();
927                     bFoundAtom = true;
928                 }
929                 bDone = true;
930             }
931             else if (pElem->getP0Type() == P0_ADDR)
932             {
933                 // need to disregard any addresses that appear between mispredict and the atom in question
934                 m_P0_stack.erase_curr_from_front();
935             }
936         }
937         else
938             bDone = true;
939     }
940 
941     // if missed atom then either overrun error or mispredict on unseen element
942     if (!bFoundAtom && !m_unseen_spec_elem)
943     {
944         err = OCSD_ERR_COMMIT_PKT_OVERRUN;
945         LogError(ocsdError(OCSD_ERR_SEV_ERROR, err, m_index_curr_pkt, m_CSID, "Not found mispredict atom"));
946     }
947     m_elem_res.mispredict = false;
948     return err;
949 }
950 
951 // discard elements and flush
discardElements()952 ocsd_err_t TrcPktDecodeEtmV4I::discardElements()
953 {
954     ocsd_err_t err = OCSD_OK;
955     TrcStackElem *pElem = 0;   // stacked element pointer
956 
957     // dump P0, elemnts - output remaining CC / TS
958     while ((m_P0_stack.size() > 0) && !err)
959     {
960         pElem = m_P0_stack.back();
961         err = processTS_CC_EventElem(pElem);
962         m_P0_stack.delete_back();
963     }
964 
965     // clear all speculation info
966     clearElemRes();
967     m_curr_spec_depth = 0;
968 
969     // set decode state
970     m_curr_state = NO_SYNC;
971     m_unsync_eot_info = m_prev_overflow ? UNSYNC_OVERFLOW : UNSYNC_DISCARD;
972 
973     // unsync so need context & address.
974     m_need_ctxt = true;
975     m_need_addr = true;
976     m_elem_pending_addr = false;
977     return err;
978 }
979 
processTS_CC_EventElem(TrcStackElem * pElem)980 ocsd_err_t TrcPktDecodeEtmV4I::processTS_CC_EventElem(TrcStackElem *pElem)
981 {
982     ocsd_err_t err = OCSD_OK;
983 
984     switch (pElem->getP0Type())
985     {
986         case P0_EVENT:
987         {
988             TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
989             if (pParamElem)
990                 err = addElemEvent(pParamElem);
991         }
992         break;
993 
994         case P0_TS:
995         {
996             TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
997             if (pParamElem)
998                 err = addElemTS(pParamElem, false);
999         }
1000         break;
1001 
1002         case P0_CC:
1003         {
1004             TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
1005             if (pParamElem)
1006                 err = addElemCC(pParamElem);
1007         }
1008         break;
1009 
1010         case P0_TS_CC:
1011         {
1012             TrcStackElemParam *pParamElem = dynamic_cast<TrcStackElemParam *>(pElem);
1013             if (pParamElem)
1014                 err = addElemTS(pParamElem, true);
1015         }
1016         break;
1017     }
1018     return err;
1019 
1020 }
1021 
addElemCC(TrcStackElemParam * pParamElem)1022 ocsd_err_t TrcPktDecodeEtmV4I::addElemCC(TrcStackElemParam *pParamElem)
1023 {
1024     ocsd_err_t err = OCSD_OK;
1025 
1026     err = m_out_elem.addElemType(pParamElem->getRootIndex(), OCSD_GEN_TRC_ELEM_CYCLE_COUNT);
1027     if (!err)
1028         outElem().setCycleCount(pParamElem->getParam(0));
1029     return err;
1030 }
1031 
addElemTS(TrcStackElemParam * pParamElem,bool withCC)1032 ocsd_err_t TrcPktDecodeEtmV4I::addElemTS(TrcStackElemParam *pParamElem, bool withCC)
1033 {
1034     ocsd_err_t err = OCSD_OK;
1035 
1036     err = m_out_elem.addElemType(pParamElem->getRootIndex(), OCSD_GEN_TRC_ELEM_TIMESTAMP);
1037     if (!err)
1038     {
1039         outElem().timestamp = (uint64_t)(pParamElem->getParam(0)) | (((uint64_t)pParamElem->getParam(1)) << 32);
1040         if (withCC)
1041             outElem().setCycleCount(pParamElem->getParam(2));
1042     }
1043     return err;
1044 }
1045 
addElemEvent(TrcStackElemParam * pParamElem)1046 ocsd_err_t TrcPktDecodeEtmV4I::addElemEvent(TrcStackElemParam *pParamElem)
1047 {
1048     ocsd_err_t err = OCSD_OK;
1049 
1050     err = m_out_elem.addElemType(pParamElem->getRootIndex(), OCSD_GEN_TRC_ELEM_EVENT);
1051     if (!err)
1052     {
1053         outElem().trace_event.ev_type = EVENT_NUMBERED;
1054         outElem().trace_event.ev_number = pParamElem->getParam(0);
1055     }
1056     return err;
1057 }
1058 
setElemTraceRange(OcsdTraceElement & elemIn,const instr_range_t & addr_range,const bool executed,ocsd_trc_index_t index)1059 void TrcPktDecodeEtmV4I::setElemTraceRange(OcsdTraceElement &elemIn, const instr_range_t &addr_range,
1060                                            const bool executed, ocsd_trc_index_t index)
1061 {
1062     elemIn.setType(OCSD_GEN_TRC_ELEM_INSTR_RANGE);
1063     elemIn.setLastInstrInfo(executed, m_instr_info.type, m_instr_info.sub_type, m_instr_info.instr_size);
1064     elemIn.setISA(m_instr_info.isa);
1065     elemIn.setLastInstrCond(m_instr_info.is_conditional);
1066     elemIn.setAddrRange(addr_range.st_addr, addr_range.en_addr, addr_range.num_instr);
1067     if (executed)
1068         m_instr_info.isa = m_instr_info.next_isa;
1069 }
1070 
processAtom(const ocsd_atm_val atom)1071 ocsd_err_t TrcPktDecodeEtmV4I::processAtom(const ocsd_atm_val atom)
1072 {
1073     ocsd_err_t err;
1074     TrcStackElem *pElem = m_P0_stack.back();  // get the atom element
1075     WP_res_t WPRes;
1076     instr_range_t addr_range;
1077 
1078     // new element for this processed atom
1079     if ((err = m_out_elem.addElem(pElem->getRootIndex())) != OCSD_OK)
1080         return err;
1081 
1082     err = traceInstrToWP(addr_range, WPRes);
1083     if(err != OCSD_OK)
1084     {
1085         if(err == OCSD_ERR_UNSUPPORTED_ISA)
1086         {
1087              m_need_addr = true;
1088              m_need_ctxt = true;
1089              LogError(ocsdError(OCSD_ERR_SEV_WARN,err,pElem->getRootIndex(),m_CSID,"Warning: unsupported instruction set processing atom packet."));
1090              // wait for next context
1091              return OCSD_OK;
1092         }
1093         else
1094         {
1095             LogError(ocsdError(OCSD_ERR_SEV_ERROR,err,pElem->getRootIndex(),m_CSID,"Error processing atom packet."));
1096             return err;
1097         }
1098     }
1099 
1100     if(WPFound(WPRes))
1101     {
1102         //  save recorded next instuction address
1103         ocsd_vaddr_t nextAddr = m_instr_info.instr_addr;
1104 
1105         // action according to waypoint type and atom value
1106         switch(m_instr_info.type)
1107         {
1108         case OCSD_INSTR_BR:
1109             if (atom == ATOM_E)
1110             {
1111                 m_instr_info.instr_addr = m_instr_info.branch_addr;
1112                 if (m_instr_info.is_link)
1113                     m_return_stack.push(nextAddr, m_instr_info.isa);
1114 
1115             }
1116             break;
1117 
1118         case OCSD_INSTR_BR_INDIRECT:
1119             if (atom == ATOM_E)
1120             {
1121                 m_need_addr = true; // indirect branch taken - need new address.
1122                 if (m_instr_info.is_link)
1123                     m_return_stack.push(nextAddr,m_instr_info.isa);
1124                 m_return_stack.set_pop_pending();  // need to know next packet before we know what is to happen
1125             }
1126             break;
1127         }
1128         setElemTraceRange(outElem(), addr_range, (atom == ATOM_E), pElem->getRootIndex());
1129     }
1130     else
1131     {
1132         // no waypoint - likely inaccessible memory range.
1133         m_need_addr = true; // need an address update
1134 
1135         if(addr_range.st_addr != addr_range.en_addr)
1136         {
1137             // some trace before we were out of memory access range
1138             setElemTraceRange(outElem(), addr_range, true, pElem->getRootIndex());
1139 
1140             // another element for the nacc...
1141             if (WPNacc(WPRes))
1142                 err = m_out_elem.addElem(pElem->getRootIndex());
1143         }
1144 
1145         if(WPNacc(WPRes) && !err)
1146         {
1147             outElem().setType(OCSD_GEN_TRC_ELEM_ADDR_NACC);
1148             outElem().st_addr = m_instr_info.instr_addr;
1149         }
1150     }
1151     return err;
1152 }
1153 
1154 // Exception processor
processException()1155 ocsd_err_t TrcPktDecodeEtmV4I::processException()
1156 {
1157     ocsd_err_t err;
1158     TrcStackElem *pElem = 0;
1159     TrcStackElemExcept *pExceptElem = 0;
1160     TrcStackElemAddr *pAddressElem = 0;
1161     TrcStackElemCtxt *pCtxtElem = 0;
1162     bool branch_target = false;    // exception address implies prior branch target address
1163     ocsd_vaddr_t excep_ret_addr;
1164     ocsd_trc_index_t excep_pkt_index;
1165     WP_res_t WPRes = WP_NOT_FOUND;
1166 
1167     // grab the exception element off the stack
1168     pExceptElem = dynamic_cast<TrcStackElemExcept *>(m_P0_stack.back());  // get the exception element
1169     excep_pkt_index = pExceptElem->getRootIndex();
1170     branch_target = pExceptElem->getPrevSame();
1171     m_P0_stack.pop_back(); // remove the exception element
1172 
1173     pElem = m_P0_stack.back();  // look at next element.
1174     if(pElem->getP0Type() == P0_CTXT)
1175     {
1176         pCtxtElem = dynamic_cast<TrcStackElemCtxt *>(pElem);
1177         m_P0_stack.pop_back(); // remove the context element
1178         pElem = m_P0_stack.back();  // next one should be an address element
1179     }
1180 
1181    if(pElem->getP0Type() != P0_ADDR)
1182    {
1183        // no following address element - indicate processing error.
1184        LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_PACKET_SEQ, excep_pkt_index,m_CSID,"Address missing in exception packet."));
1185        return OCSD_ERR_BAD_PACKET_SEQ;
1186    }
1187    else
1188    {
1189         // extract address
1190         pAddressElem = static_cast<TrcStackElemAddr *>(pElem);
1191         excep_ret_addr = pAddressElem->getAddr().val;
1192 
1193         // see if there is an address + optional context element implied
1194         // prior to the exception.
1195         if (branch_target)
1196         {
1197             // this was a branch target address - update current setting
1198             bool b64bit = m_instr_info.isa == ocsd_isa_aarch64;
1199             if (pCtxtElem) {
1200                 b64bit = pCtxtElem->getContext().SF;
1201             }
1202 
1203             // as the exception address was also a branch target address then update the
1204             // current maintained address value. This also means that there is no range to
1205             // output before the exception packet.
1206             m_instr_info.instr_addr = excep_ret_addr;
1207             m_instr_info.isa = (pAddressElem->getAddr().isa == 0) ?
1208                     (b64bit ? ocsd_isa_aarch64 : ocsd_isa_arm) : ocsd_isa_thumb2;
1209             m_need_addr = false;
1210         }
1211     }
1212 
1213     // need to output something - set up an element
1214     if ((err = m_out_elem.addElem(excep_pkt_index)))
1215         return err;
1216 
1217     // output a context element if present
1218     if (pCtxtElem)
1219     {
1220         updateContext(pCtxtElem, outElem());
1221 
1222         // used the element - need another for later stages
1223         if ((err = m_out_elem.addElem(excep_pkt_index)))
1224             return err;
1225     }
1226 
1227     // if the preferred return address is not the end of the last output range...
1228     if (m_instr_info.instr_addr != excep_ret_addr)
1229     {
1230         bool range_out = false;
1231         instr_range_t addr_range;
1232 
1233         // look for match to return address.
1234         err = traceInstrToWP(addr_range, WPRes, true, excep_ret_addr);
1235 
1236         if(err != OCSD_OK)
1237         {
1238             if(err == OCSD_ERR_UNSUPPORTED_ISA)
1239             {
1240                 m_need_addr = true;
1241                 m_need_ctxt = true;
1242                 LogError(ocsdError(OCSD_ERR_SEV_WARN,err, excep_pkt_index,m_CSID,"Warning: unsupported instruction set processing exception packet."));
1243             }
1244             else
1245             {
1246                 LogError(ocsdError(OCSD_ERR_SEV_ERROR,err, excep_pkt_index,m_CSID,"Error processing exception packet."));
1247             }
1248             return err;
1249         }
1250 
1251         if(WPFound(WPRes))
1252         {
1253             // waypoint address found - output range
1254             setElemTraceRange(outElem(), addr_range, true, excep_pkt_index);
1255             range_out = true;
1256         }
1257         else
1258         {
1259             // no waypoint - likely inaccessible memory range.
1260             m_need_addr = true; // need an address update
1261 
1262             if(addr_range.st_addr != addr_range.en_addr)
1263             {
1264                 // some trace before we were out of memory access range
1265                 setElemTraceRange(outElem(), addr_range, true, excep_pkt_index);
1266                 range_out = true;
1267             }
1268         }
1269 
1270         // used the element need another for NACC or EXCEP.
1271         if (range_out)
1272         {
1273             if ((err = m_out_elem.addElem(excep_pkt_index)))
1274                 return err;
1275         }
1276     }
1277 
1278     // watchpoint walk resulted in inaccessible memory call...
1279     if (WPNacc(WPRes))
1280     {
1281 
1282         outElem().setType(OCSD_GEN_TRC_ELEM_ADDR_NACC);
1283         outElem().st_addr = m_instr_info.instr_addr;
1284 
1285         // used the element - need another for the final exception packet.
1286         if ((err = m_out_elem.addElem(excep_pkt_index)))
1287             return err;
1288     }
1289 
1290     // output exception element.
1291     outElem().setType(OCSD_GEN_TRC_ELEM_EXCEPTION);
1292 
1293     // add end address as preferred return address to end addr in element
1294     outElem().en_addr = excep_ret_addr;
1295     outElem().excep_ret_addr = 1;
1296     outElem().excep_ret_addr_br_tgt = branch_target;
1297     outElem().exception_number = pExceptElem->getExcepNum();
1298 
1299     m_P0_stack.delete_popped();     // clear the used elements from the stack
1300     return err;
1301 }
1302 
SetInstrInfoInAddrISA(const ocsd_vaddr_t addr_val,const uint8_t isa)1303 void TrcPktDecodeEtmV4I::SetInstrInfoInAddrISA(const ocsd_vaddr_t addr_val, const uint8_t isa)
1304 {
1305     m_instr_info.instr_addr = addr_val;
1306     m_instr_info.isa = calcISA(m_is_64bit, isa);
1307 }
1308 
1309 // trace an instruction range to a waypoint - and set next address to restart from.
traceInstrToWP(instr_range_t & range,WP_res_t & WPRes,const bool traceToAddrNext,const ocsd_vaddr_t nextAddrMatch)1310 ocsd_err_t TrcPktDecodeEtmV4I::traceInstrToWP(instr_range_t &range, WP_res_t &WPRes, const bool traceToAddrNext /*= false*/, const ocsd_vaddr_t nextAddrMatch /*= 0*/)
1311 {
1312     uint32_t opcode;
1313     uint32_t bytesReq;
1314     ocsd_err_t err = OCSD_OK;
1315 
1316     range.st_addr = range.en_addr = m_instr_info.instr_addr;
1317     range.num_instr = 0;
1318 
1319     WPRes = WP_NOT_FOUND;
1320 
1321     while(WPRes == WP_NOT_FOUND)
1322     {
1323         // start off by reading next opcode;
1324         bytesReq = 4;
1325         err = accessMemory(m_instr_info.instr_addr, getCurrMemSpace(),&bytesReq,(uint8_t *)&opcode);
1326         if(err != OCSD_OK) break;
1327 
1328         if(bytesReq == 4) // got data back
1329         {
1330             m_instr_info.opcode = opcode;
1331             err = instrDecode(&m_instr_info);
1332             if(err != OCSD_OK) break;
1333 
1334             // increment address - may be adjusted by direct branch value later
1335             m_instr_info.instr_addr += m_instr_info.instr_size;
1336             range.num_instr++;
1337 
1338             // either walking to match the next instruction address or a real watchpoint
1339             if (traceToAddrNext)
1340             {
1341                 if (m_instr_info.instr_addr == nextAddrMatch)
1342                     WPRes = WP_FOUND;
1343             }
1344             else if (m_instr_info.type != OCSD_INSTR_OTHER)
1345                 WPRes = WP_FOUND;
1346         }
1347         else
1348         {
1349             // not enough memory accessible.
1350             WPRes = WP_NACC;
1351         }
1352     }
1353     // update the range decoded address in the output packet.
1354     range.en_addr = m_instr_info.instr_addr;
1355     return err;
1356 }
1357 
updateContext(TrcStackElemCtxt * pCtxtElem,OcsdTraceElement & elem)1358 void TrcPktDecodeEtmV4I::updateContext(TrcStackElemCtxt *pCtxtElem, OcsdTraceElement &elem)
1359 {
1360     etmv4_context_t ctxt = pCtxtElem->getContext();
1361 
1362     elem.setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
1363 
1364     // map to output element and local saved state.
1365     m_is_64bit = (ctxt.SF != 0);
1366     elem.context.bits64 = ctxt.SF;
1367     m_is_secure = (ctxt.NS == 0);
1368     elem.context.security_level = ctxt.NS ? ocsd_sec_nonsecure : ocsd_sec_secure;
1369     elem.context.exception_level = (ocsd_ex_level)ctxt.EL;
1370     elem.context.el_valid = 1;
1371     if(ctxt.updated_c)
1372     {
1373         elem.context.ctxt_id_valid = 1;
1374         m_context_id = elem.context.context_id = ctxt.ctxtID;
1375     }
1376     if(ctxt.updated_v)
1377     {
1378         elem.context.vmid_valid = 1;
1379         m_vmid_id = elem.context.vmid = ctxt.VMID;
1380     }
1381 
1382     // need to update ISA in case context follows address.
1383     elem.isa = m_instr_info.isa = calcISA(m_is_64bit, pCtxtElem->getIS());
1384     m_need_ctxt = false;
1385 }
1386 
handleBadPacket(const char * reason)1387 ocsd_err_t TrcPktDecodeEtmV4I::handleBadPacket(const char *reason)
1388 {
1389     ocsd_err_t err = OCSD_OK;
1390 
1391     if(getComponentOpMode() && OCSD_OPFLG_PKTDEC_ERROR_BAD_PKTS)
1392     {
1393         // error out - stop decoding
1394         err = OCSD_ERR_BAD_DECODE_PKT;
1395         LogError(ocsdError(OCSD_ERR_SEV_ERROR,err,reason));
1396     }
1397     else
1398     {
1399         LogError(ocsdError(OCSD_ERR_SEV_WARN, OCSD_ERR_BAD_DECODE_PKT, reason));
1400         // switch to unsync - clear decode state
1401         resetDecoder();
1402         m_curr_state = NO_SYNC;
1403         m_unsync_eot_info = UNSYNC_BAD_PACKET;
1404     }
1405     return err;
1406 }
1407 
getCurrMemSpace()1408 inline ocsd_mem_space_acc_t TrcPktDecodeEtmV4I::getCurrMemSpace()
1409 {
1410     static ocsd_mem_space_acc_t SMemSpace[] = {
1411         OCSD_MEM_SPACE_EL1S,
1412         OCSD_MEM_SPACE_EL1S,
1413         OCSD_MEM_SPACE_EL2S,
1414         OCSD_MEM_SPACE_EL3
1415     };
1416 
1417     static ocsd_mem_space_acc_t NSMemSpace[] = {
1418         OCSD_MEM_SPACE_EL1N,
1419         OCSD_MEM_SPACE_EL1N,
1420         OCSD_MEM_SPACE_EL2,
1421         OCSD_MEM_SPACE_EL3
1422     };
1423 
1424     /* if no valid EL value - just use S/NS */
1425     if (!outElem().context.el_valid)
1426         return  m_is_secure ? OCSD_MEM_SPACE_S : OCSD_MEM_SPACE_N;
1427 
1428     /* mem space according to EL + S/NS */
1429     int el = (int)(outElem().context.exception_level) & 0x3;
1430     return m_is_secure ? SMemSpace[el] : NSMemSpace[el];
1431 }
1432 /* End of File trc_pkt_decode_etmv4i.cpp */
1433