1 /*
2  * \file       trc_pkt_proc_ptm.h
3  * \brief      OpenCSD :
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 
9 /*
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
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.
19  *
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.
23  *
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.
34  */
35 
36 #ifndef ARM_TRC_PKT_PROC_PTM_H_INCLUDED
37 #define ARM_TRC_PKT_PROC_PTM_H_INCLUDED
38 
39 #include "trc_pkt_types_ptm.h"
40 #include "common/trc_pkt_proc_base.h"
41 #include "trc_pkt_elem_ptm.h"
42 #include "trc_cmp_cfg_ptm.h"
43 
44 class PtmTrcPacket;
45 class PtmConfig;
46 
47 /** @addtogroup ocsd_pkt_proc
48 @{*/
49 
50 
51 
52 class TrcPktProcPtm : public TrcPktProcBase< PtmTrcPacket,  ocsd_ptm_pkt_type, PtmConfig>
53 {
54 public:
55     TrcPktProcPtm();
56     TrcPktProcPtm(int instIDNum);
57     virtual ~TrcPktProcPtm();
58 
59 protected:
60     /* implementation packet processing interface */
61     virtual ocsd_datapath_resp_t processData(  const ocsd_trc_index_t index,
62                                                 const uint32_t dataBlockSize,
63                                                 const uint8_t *pDataBlock,
64                                                 uint32_t *numBytesProcessed);
65     virtual ocsd_datapath_resp_t onEOT();
66     virtual ocsd_datapath_resp_t onReset();
67     virtual ocsd_datapath_resp_t onFlush();
68     virtual ocsd_err_t onProtocolConfig();
69     virtual const bool isBadPacket() const;
70 
71     void InitPacketState();      // clear current packet state.
72     void InitProcessorState();   // clear all previous process state
73 
74     ocsd_datapath_resp_t outputPacket();
75 
76     typedef enum _process_state {
77         WAIT_SYNC,
78         PROC_HDR,
79         PROC_DATA,
80         SEND_PKT,
81     } process_state;
82 
83     process_state m_process_state;  // process algorithm state.
84 
85     std::vector<uint8_t> m_currPacketData;  // raw data
86     uint32_t m_currPktIdx;   // index into packet when expanding
87     PtmTrcPacket m_curr_packet;  // expanded packet
88     ocsd_trc_index_t m_curr_pkt_index; // trace index at start of packet.
89 
90     const bool readByte(uint8_t &currByte);
91     const bool readByte(); // just read into buffer, don't need the value
92     void unReadByte();  // remove last byte from the buffer.
93 
94     uint8_t m_chanIDCopy;
95 
96     // current data block being processed.
97     const uint8_t *m_pDataIn;
98     uint32_t m_dataInLen;
99     uint32_t m_dataInProcessed;
100     ocsd_trc_index_t m_block_idx; // index start for current block
101 
102     // processor synchronisation
103     const bool isSync() const;
104     ocsd_datapath_resp_t waitASync();       //!< look for first synchronisation point in the packet stream
105     bool m_waitASyncSOPkt;
106     bool m_bAsyncRawOp;
107     bool m_bOPNotSyncPkt;                   //!< true if output not sync packet when waiting for ASYNC
108 
109     // ** packet processing functions.
110     void pktASync();
111     void pktISync();
112     void pktTrigger();
113     void pktWPointUpdate();
114     void pktIgnore();
115     void pktCtxtID();
116     void pktVMID();
117     void pktAtom();
118     void pktTimeStamp();
119     void pktExceptionRet();
120     void pktBranchAddr();
121     void pktReserved();
122 
123     // async finder
124     typedef enum _async_result {
125         ASYNC,      //!< pattern confirmed async 0x00 x 5, 0x80
126         NOT_ASYNC,  //!< pattern confirmed not async
127         ASYNC_EXTRA_0,  //!< pattern confirmed 0x00 x N + ASYNC
128         THROW_0,    //!< long pattern of 0x00 - throw some away.
129         ASYNC_INCOMPLETE, //!< not enough input data.
130     } async_result_t;
131 
132     async_result_t findAsync();
133 
134     int m_async_0;  // number of current consecutive async 0s
135 
136     bool m_part_async;
137 
138     // number of extra 0s before we throw 0 on async detect.
139     #define ASYNC_PAD_0_LIMIT 11
140     // number of 0s minimum to form an async
141     #define ASYNC_REQ_0 5
142 
143     // extraction sub-routines
144     void extractCtxtID(int idx, uint32_t &ctxtID);
145     void extractCycleCount(int idx, uint32_t &cycleCount);
146     int extractTS(uint64_t &tsVal, uint8_t &tsUpdateBits);
147     uint32_t extractAddress(const int offset,uint8_t &total_bits);
148 
149     // number of bytes required for a complete packet - used in some multi byte packets
150     int m_numPktBytesReq;
151 
152     // packet processing state
153     bool m_needCycleCount;
154     bool m_gotCycleCount;
155     int m_gotCCBytes;           // number of CC bytes read so far
156 
157     int m_numCtxtIDBytes;
158     int m_gotCtxtIDBytes;
159 
160     bool m_gotTSBytes;      //!< got all TS bytes
161     int m_tsByteMax;        //!< max size for TS portion of TS packet.
162 
163     // branch address state
164     bool m_gotAddrBytes;    //!< got all Addr bytes in branch packet
165     int m_numAddrBytes;     //!< number of address bytes
166     bool m_gotExcepBytes;   //!< got all needed exception bytes
167     int m_numExcepBytes;          //!< got 1st exception byte
168     ocsd_isa m_addrPktIsa; //!< ISA of the branch address packet
169     int m_excepAltISA;      //!< Alt ISA bit iff exception bytes
170 
171     // bad packets
172     void throwMalformedPacketErr(const char *pszErrMsg);
173     void throwPacketHeaderErr(const char *pszErrMsg);
174 
175 
176     // packet processing function table
177     typedef void (TrcPktProcPtm::*PPKTFN)(void);
178     PPKTFN m_pIPktFn;
179 
180     struct _pkt_i_table_t {
181         ocsd_ptm_pkt_type pkt_type;
182         PPKTFN pptkFn;
183     } m_i_table[256];
184 
185     void BuildIPacketTable();
186 
187 };
188 
189 inline const bool TrcPktProcPtm::isSync() const
190 {
191     return (bool)(m_curr_packet.getType() == PTM_PKT_NOTSYNC);
192 }
193 
194 inline void TrcPktProcPtm::throwMalformedPacketErr(const char *pszErrMsg)
195 {
196     m_curr_packet.SetErrType(PTM_PKT_BAD_SEQUENCE);
197     throw ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_PACKET_SEQ,m_curr_pkt_index,m_chanIDCopy,pszErrMsg);
198 }
199 
200 inline void TrcPktProcPtm::throwPacketHeaderErr(const char *pszErrMsg)
201 {
202     throw ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_INVALID_PCKT_HDR,m_curr_pkt_index,m_chanIDCopy,pszErrMsg);
203 }
204 
205 inline const bool TrcPktProcPtm::readByte()
206 {
207     uint8_t currByte;
208     return readByte(currByte);
209 }
210 
211 /** @}*/
212 
213 #endif // ARM_TRC_PKT_PROC_PTM_H_INCLUDED
214 
215 /* End of File trc_pkt_proc_ptm.h */
216