1 /*!
2 * \file trc_mem_acc_base.h
3 * \brief OpenCSD : Memory accessor base class.
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_MEM_ACC_BASE_H_INCLUDED
36 #define ARM_TRC_MEM_ACC_BASE_H_INCLUDED
37
38 #include "opencsd/ocsd_if_types.h"
39 #include <string>
40
41 /*!
42 * @class TrcMemAccessorBase
43 * @brief Memory range to access by trace decoder.
44 *
45 * Represents a memory access range for the trace decoder.
46 * Range inclusive from m_startAddress to m_endAddress.
47 * e.g. a 1k range from 0x1000 has start of 0x1000 and end of 0x13FF
48 *
49 * Derived classes provide specific access types such as binary files and memory buffers.
50 *
51 */
52 class TrcMemAccessorBase
53 {
54 public:
55
56 /** Describes the storage type of the underlying memory accessor */
57 enum MemAccTypes {
58 MEMACC_UNKNOWN,
59 MEMACC_FILE, //<! Binary data file accessor
60 MEMACC_BUFPTR, //<! memory buffer accessor
61 MEMACC_CB_IF, //<! callback interface accessor - use for live memory access
62 };
63
64 /** default constructor */
65 TrcMemAccessorBase(MemAccTypes type);
66
67 /** costruct with address range values */
68 TrcMemAccessorBase(MemAccTypes type, ocsd_vaddr_t startAddr, ocsd_vaddr_t endAddr);
69
70 /** default desctructor */
~TrcMemAccessorBase()71 virtual ~TrcMemAccessorBase() {};
72
73 /*!
74 * Set the inclusive address range of this accessor.
75 *
76 * @param startAddr : start address of the range.
77 * @param endAddr : end address of the range.
78 */
79 void setRange(ocsd_vaddr_t startAddr, ocsd_vaddr_t endAddr);
80
81 /*!
82 * test if an address is in the inclusive range for this accessor
83 *
84 * @param s_address : Address to test.
85 *
86 * @return const bool : true if the address is in range.
87 */
88 virtual const bool addrInRange(const ocsd_vaddr_t s_address) const;
89
90
91 /*!
92 * test if an address is the start of range for this accessor
93 *
94 * @param s_address : Address to test.
95 *
96 * @return const bool : true if the address is start of range.
97 */
98 virtual const bool addrStartOfRange(const ocsd_vaddr_t s_address) const;
99
100 /*!
101 * Test number of bytes available from the start address, up to the number of requested bytes.
102 * Tests if all the requested bytes are available from the supplied start address.
103 * Returns the number available up to full requested amount.
104 *
105 * @param s_address : Start address within the range.
106 * @param reqBytes : Number of bytes needed from the start address.
107 *
108 * @return const uint32_t : Bytes available, up to reqBytes. 0 is s_address not in range.
109 */
110 virtual const uint32_t bytesInRange(const ocsd_vaddr_t s_address, const uint32_t reqBytes) const;
111
112 /*!
113 * test if supplied range accessor overlaps this range.
114 *
115 * @param *p_test_acc : Accessor to test for overlap.
116 *
117 * @return bool : true if overlap, false if not.
118 */
119 virtual const bool overLapRange(const TrcMemAccessorBase *p_test_acc) const;
120
121 /*!
122 * Read bytes from via the accessor from the memory range.
123 *
124 * @param s_address : Start address of the read.
125 * @param memSpace : memory space for this access.
126 * @param trcID : Trace ID of trace source.
127 * @param reqBytes : Number of bytes required.
128 * @param *byteBuffer : Buffer to copy the bytes into.
129 *
130 * @return uint32_t : Number of bytes read, 0 if s_address out of range, or mem space not accessible.
131 */
132 virtual const uint32_t readBytes(const ocsd_vaddr_t s_address, const ocsd_mem_space_acc_t memSpace, const uint8_t trcID, const uint32_t reqBytes, uint8_t *byteBuffer) = 0;
133
134 /*!
135 * Validate the address range - ensure addresses aligned, different, st < en etc.
136 *
137 * @return bool : true if valid range.
138 */
139 virtual const bool validateRange();
140
141
getType()142 const enum MemAccTypes getType() const { return m_type; };
143
144 /* handle memory spaces */
setMemSpace(ocsd_mem_space_acc_t memSpace)145 void setMemSpace(ocsd_mem_space_acc_t memSpace) { m_mem_space = memSpace; };
getMemSpace()146 const ocsd_mem_space_acc_t getMemSpace() const { return m_mem_space; };
inMemSpace(const ocsd_mem_space_acc_t mem_space)147 const bool inMemSpace(const ocsd_mem_space_acc_t mem_space) const { return (bool)(((uint8_t)m_mem_space & (uint8_t)mem_space) != 0); };
148
149 /* memory access info logging */
150 virtual void getMemAccString(std::string &accStr) const;
151
152 static void getMemAccSpaceString(std::string& spaceStr, const ocsd_mem_space_acc_t mem_space);
153
154 protected:
155 ocsd_vaddr_t m_startAddress; /**< accessible range start address */
156 ocsd_vaddr_t m_endAddress; /**< accessible range end address */
157 const MemAccTypes m_type; /**< memory accessor type */
158 ocsd_mem_space_acc_t m_mem_space; /**< Matching memory space of this acessor */
159 };
160
TrcMemAccessorBase(MemAccTypes accType,ocsd_vaddr_t startAddr,ocsd_vaddr_t endAddr)161 inline TrcMemAccessorBase::TrcMemAccessorBase(MemAccTypes accType, ocsd_vaddr_t startAddr, ocsd_vaddr_t endAddr) :
162 m_startAddress(startAddr),
163 m_endAddress(endAddr),
164 m_type(accType),
165 m_mem_space(OCSD_MEM_SPACE_ANY)
166 {
167 }
168
TrcMemAccessorBase(MemAccTypes accType)169 inline TrcMemAccessorBase::TrcMemAccessorBase(MemAccTypes accType) :
170 m_startAddress(0),
171 m_endAddress(0),
172 m_type(accType),
173 m_mem_space(OCSD_MEM_SPACE_ANY)
174 {
175 }
176
setRange(ocsd_vaddr_t startAddr,ocsd_vaddr_t endAddr)177 inline void TrcMemAccessorBase::setRange(ocsd_vaddr_t startAddr, ocsd_vaddr_t endAddr)
178 {
179 m_startAddress = startAddr;
180 m_endAddress = endAddr;
181 }
182
addrInRange(const ocsd_vaddr_t s_address)183 inline const bool TrcMemAccessorBase::addrInRange(const ocsd_vaddr_t s_address) const
184 {
185 return (s_address >= m_startAddress) && (s_address <= m_endAddress);
186 }
187
addrStartOfRange(const ocsd_vaddr_t s_address)188 inline const bool TrcMemAccessorBase::addrStartOfRange(const ocsd_vaddr_t s_address) const
189 {
190 return (s_address == m_startAddress);
191 }
192
193
bytesInRange(const ocsd_vaddr_t s_address,const uint32_t reqBytes)194 inline const uint32_t TrcMemAccessorBase::bytesInRange(const ocsd_vaddr_t s_address, const uint32_t reqBytes) const
195 {
196 ocsd_vaddr_t bytesInRange = 0;
197 if(addrInRange(s_address)) // start not in range, return 0.
198 {
199 // bytes available till end address.
200 bytesInRange = m_endAddress - s_address + 1;
201 if(bytesInRange > reqBytes)
202 bytesInRange = reqBytes;
203 }
204 return (uint32_t)bytesInRange;
205 }
206
overLapRange(const TrcMemAccessorBase * p_test_acc)207 inline const bool TrcMemAccessorBase::overLapRange(const TrcMemAccessorBase *p_test_acc) const
208 {
209 if( addrInRange(p_test_acc->m_startAddress) ||
210 addrInRange(p_test_acc->m_endAddress)
211 )
212 return true;
213 return false;
214 }
215
validateRange()216 inline const bool TrcMemAccessorBase::validateRange()
217 {
218 if(m_startAddress & 0x1) // at least hword aligned for thumb
219 return false;
220 if((m_endAddress + 1) & 0x1)
221 return false;
222 if(m_startAddress == m_endAddress) // zero length range.
223 return false;
224 if(m_startAddress > m_endAddress) // values bakcwards / invalid
225 return false;
226 return true;
227 }
228
229
230 class TrcMemAccFactory
231 {
232 public:
233 /** Accessor Creation */
234 static ocsd_err_t CreateBufferAccessor(TrcMemAccessorBase **pAccessor, const ocsd_vaddr_t s_address, const uint8_t *p_buffer, const uint32_t size);
235 static ocsd_err_t CreateFileAccessor(TrcMemAccessorBase **pAccessor, const std::string &pathToFile, ocsd_vaddr_t startAddr, size_t offset = 0, size_t size = 0);
236 static ocsd_err_t CreateCBAccessor(TrcMemAccessorBase **pAccessor, const ocsd_vaddr_t s_address, const ocsd_vaddr_t e_address, const ocsd_mem_space_acc_t mem_space);
237
238 /** Accessor Destruction */
239 static void DestroyAccessor(TrcMemAccessorBase *pAccessor);
240 private:
TrcMemAccFactory()241 TrcMemAccFactory() {};
~TrcMemAccFactory()242 ~TrcMemAccFactory() {};
243 };
244
245 #endif // ARM_TRC_MEM_ACC_BASE_H_INCLUDED
246
247 /* End of File trc_mem_acc_base.h */
248