1 /*
2  * \file       trc_pkt_decode_etmv4i.h
3  * \brief      OpenCSD : ETMv4 instruction 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 #ifndef ARM_TRC_PKT_DECODE_ETMV4I_H_INCLUDED
36 #define ARM_TRC_PKT_DECODE_ETMV4I_H_INCLUDED
37 
38 #include "common/trc_pkt_decode_base.h"
39 #include "opencsd/etmv4/trc_pkt_elem_etmv4i.h"
40 #include "opencsd/etmv4/trc_cmp_cfg_etmv4.h"
41 #include "common/trc_gen_elem.h"
42 #include "common/trc_ret_stack.h"
43 #include "common/ocsd_gen_elem_stack.h"
44 #include "opencsd/etmv4/trc_etmv4_stack_elem.h"
45 
46 class TrcStackElem;
47 class TrcStackElemParam;
48 class TrcStackElemCtxt;
49 
50 class TrcPktDecodeEtmV4I : public TrcPktDecodeBase<EtmV4ITrcPacket, EtmV4Config>
51 {
52 public:
53     TrcPktDecodeEtmV4I();
54     TrcPktDecodeEtmV4I(int instIDNum);
55     virtual ~TrcPktDecodeEtmV4I();
56 
57 protected:
58     /* implementation packet decoding interface */
59     virtual ocsd_datapath_resp_t processPacket();
60     virtual ocsd_datapath_resp_t onEOT();
61     virtual ocsd_datapath_resp_t onReset();
62     virtual ocsd_datapath_resp_t onFlush();
63     virtual ocsd_err_t onProtocolConfig();
64     virtual const uint8_t getCoreSightTraceID() { return m_CSID; };
65 
66     /* local decode methods */
67     void initDecoder();      // initial state on creation (zeros all config)
68     void resetDecoder();     // reset state to start of decode. (moves state, retains config)
69     virtual void onFirstInitOK(); // override to set init related info.
70 
71     ocsd_err_t decodePacket();    // decode packet into trace elements. return true to indicate decode complete - can change FSM to commit state - return is false.
72     ocsd_datapath_resp_t resolveElements();   // commit/cancel trace elements generated from latest / prior packets & send to output - may get wait response, or flag completion.
73     ocsd_err_t commitElements(); // commit elements - process element stack to generate output packets.
74     ocsd_err_t commitElemOnEOT();
75     ocsd_err_t cancelElements();    // cancel elements. These not output
76     ocsd_err_t mispredictAtom();    // mispredict an atom
77     ocsd_err_t discardElements();   // discard elements and flush
78 
79     void doTraceInfoPacket();
80     void updateContext(TrcStackElemCtxt *pCtxtElem, OcsdTraceElement &elem);
81 
82     // process atom will create instruction trace, or no memory access trace output elements.
83     ocsd_err_t processAtom(const ocsd_atm_val atom);
84 
85     // process an exception element - output instruction trace + exception generic type.
86     ocsd_err_t processException();
87 
88     // process Q element
89     ocsd_err_t processQElement();
90 
91     // process an element that cannot be cancelled / discarded
92     ocsd_err_t processTS_CC_EventElem(TrcStackElem *pElem);
93 
94     // process a bad packet
95     ocsd_err_t handleBadPacket(const char *reason);
96 
97     ocsd_err_t addElemCC(TrcStackElemParam *pParamElem);
98     ocsd_err_t addElemTS(TrcStackElemParam *pParamElem, bool withCC);
99     ocsd_err_t addElemEvent(TrcStackElemParam *pParamElem);
100 
101 private:
102     void SetInstrInfoInAddrISA(const ocsd_vaddr_t addr_val, const uint8_t isa);
103     const ocsd_isa calcISA(const bool SF, const uint8_t IS) const
104     {
105         if (SF)
106             return ocsd_isa_aarch64;
107         return (IS == 0) ? ocsd_isa_arm : ocsd_isa_thumb2;
108     }
109     typedef enum {
110         WP_NOT_FOUND,
111         WP_FOUND,
112         WP_NACC
113     } WP_res_t;
114 
115     typedef struct {
116         ocsd_vaddr_t st_addr;
117         ocsd_vaddr_t en_addr;
118         uint32_t num_instr;
119     } instr_range_t;
120 
121     //!< follow instructions from the current address to a WP. true if good, false if memory cannot be accessed.
122     ocsd_err_t traceInstrToWP(instr_range_t &instr_range, WP_res_t &WPRes, const bool traceToAddrNext = false, const ocsd_vaddr_t nextAddrMatch = 0);
123 
124     inline const bool WPFound(WP_res_t res) const { return (res == WP_FOUND); };
125     inline const bool WPNacc(WP_res_t res) const { return (res == WP_NACC); };
126 
127     ocsd_err_t returnStackPop();  // pop return stack and update instruction address.
128 
129     void setElemTraceRange(OcsdTraceElement &elemIn, const instr_range_t &addr_range, const bool executed, ocsd_trc_index_t index);
130 
131     ocsd_mem_space_acc_t getCurrMemSpace();
132 
133 //** intra packet state (see ETMv4 spec 6.2.1);
134 
135     // timestamping
136     uint64_t m_timestamp;   // last broadcast global Timestamp.
137 
138     // state and context
139     uint32_t m_context_id;              // most recent context ID
140     uint32_t m_vmid_id;                 // most recent VMID
141     bool m_is_secure;                   // true if Secure
142     bool m_is_64bit;                    // true if 64 bit
143     uint8_t m_last_IS;                  // last instruction set value from address packet.
144 
145     // cycle counts
146     int m_cc_threshold;
147 
148     // speculative trace
149     int m_curr_spec_depth;
150     int m_max_spec_depth;   // nax depth - from ID reg, beyond which auto-commit occurs
151     int m_unseen_spec_elem; // speculative elements at decode start
152 
153 /** Remove elements that are associated with data trace */
154 #ifdef DATA_TRACE_SUPPORTED
155     // data trace associative elements (unsupported at present in the decoder).
156     int m_p0_key;
157     int m_p0_key_max;
158 
159     // conditional non-branch trace - when data trace active (unsupported at present in the decoder)
160     int m_cond_c_key;
161     int m_cond_r_key;
162     int m_cond_key_max_incr;
163 #endif
164 
165     uint8_t m_CSID; //!< Coresight trace ID for this decoder.
166 
167     bool m_IASize64;    //!< True if 64 bit instruction addresses supported.
168 
169 //** Other processor state;
170 
171     // trace decode FSM
172     typedef enum {
173         NO_SYNC,        //!< pre start trace - init state or after reset or overflow, loss of sync.
174         WAIT_SYNC,      //!< waiting for sync packet.
175         WAIT_TINFO,     //!< waiting for trace info packet.
176         DECODE_PKTS,    //!< processing packets - creating decode elements on stack
177         RESOLVE_ELEM,   //!< analyze / resolve decode elements - create generic trace elements and pass on.
178     } processor_state_t;
179 
180     processor_state_t m_curr_state;
181     unsync_info_t m_unsync_eot_info;   //!< addition info when / why unsync / eot
182 
183 //** P0 element stack
184     EtmV4P0Stack m_P0_stack;    //!< P0 decode element stack
185 
186     // element resolution
187     struct {
188         int P0_commit;    //!< number of elements to commit
189         int P0_cancel;    //!< elements to cancel
190         bool mispredict;  //!< mispredict latest atom
191         bool discard;     //!< discard elements
192     } m_elem_res;
193 
194     //! true if any of the element resolution fields are non-zero
195     const bool isElemForRes() const {
196         return (m_elem_res.P0_commit || m_elem_res.P0_cancel ||
197             m_elem_res.mispredict || m_elem_res.discard);
198     }
199 
200     void clearElemRes() {
201         m_elem_res.P0_commit = 0;
202         m_elem_res.P0_cancel = 0;
203         m_elem_res.mispredict = false;
204         m_elem_res.discard = false;
205     }
206 
207     // packet decode state
208     bool m_need_ctxt;   //!< need context to continue
209     bool m_need_addr;   //!< need an address to continue
210     bool m_elem_pending_addr;    //!< next address packet is needed for prev element.
211 
212     ocsd_instr_info m_instr_info;  //!< instruction info for code follower - in address is the next to be decoded.
213 
214     etmv4_trace_info_t m_trace_info; //!< trace info for this trace run.
215 
216     bool m_prev_overflow;
217 
218     TrcAddrReturnStack m_return_stack;  //!< the address return stack.
219 
220 //** output element handling
221     OcsdGenElemStack m_out_elem;  //!< output element stack.
222     OcsdTraceElement &outElem() { return m_out_elem.getCurrElem(); };   //!< current  out element
223 };
224 
225 #endif // ARM_TRC_PKT_DECODE_ETMV4I_H_INCLUDED
226 
227 /* End of File trc_pkt_decode_etmv4i.h */
228