1*c120c564SAndrew Turner /* 2*c120c564SAndrew Turner * \file trc_mem_acc_file.h 3*c120c564SAndrew Turner * \brief OpenCSD : Access binary target memory file 4*c120c564SAndrew Turner * 5*c120c564SAndrew Turner * \copyright Copyright (c) 2015, ARM Limited. All Rights Reserved. 6*c120c564SAndrew Turner */ 7*c120c564SAndrew Turner 8*c120c564SAndrew Turner /* 9*c120c564SAndrew Turner * Redistribution and use in source and binary forms, with or without modification, 10*c120c564SAndrew Turner * are permitted provided that the following conditions are met: 11*c120c564SAndrew Turner * 12*c120c564SAndrew Turner * 1. Redistributions of source code must retain the above copyright notice, 13*c120c564SAndrew Turner * this list of conditions and the following disclaimer. 14*c120c564SAndrew Turner * 15*c120c564SAndrew Turner * 2. Redistributions in binary form must reproduce the above copyright notice, 16*c120c564SAndrew Turner * this list of conditions and the following disclaimer in the documentation 17*c120c564SAndrew Turner * and/or other materials provided with the distribution. 18*c120c564SAndrew Turner * 19*c120c564SAndrew Turner * 3. Neither the name of the copyright holder nor the names of its contributors 20*c120c564SAndrew Turner * may be used to endorse or promote products derived from this software without 21*c120c564SAndrew Turner * specific prior written permission. 22*c120c564SAndrew Turner * 23*c120c564SAndrew Turner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 24*c120c564SAndrew Turner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25*c120c564SAndrew Turner * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26*c120c564SAndrew Turner * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27*c120c564SAndrew Turner * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28*c120c564SAndrew Turner * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29*c120c564SAndrew Turner * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30*c120c564SAndrew Turner * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31*c120c564SAndrew Turner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32*c120c564SAndrew Turner * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33*c120c564SAndrew Turner */ 34*c120c564SAndrew Turner 35*c120c564SAndrew Turner #ifndef ARM_TRC_MEM_ACC_FILE_H_INCLUDED 36*c120c564SAndrew Turner #define ARM_TRC_MEM_ACC_FILE_H_INCLUDED 37*c120c564SAndrew Turner 38*c120c564SAndrew Turner #include <map> 39*c120c564SAndrew Turner #include <string> 40*c120c564SAndrew Turner #include <fstream> 41*c120c564SAndrew Turner #include <list> 42*c120c564SAndrew Turner 43*c120c564SAndrew Turner #include "opencsd/ocsd_if_types.h" 44*c120c564SAndrew Turner #include "mem_acc/trc_mem_acc_base.h" 45*c120c564SAndrew Turner 46*c120c564SAndrew Turner // an add-on region to a file - allows setting of a region at a none-zero offset for a file. 47*c120c564SAndrew Turner class FileRegionMemAccessor : public TrcMemAccessorBase 48*c120c564SAndrew Turner { 49*c120c564SAndrew Turner public: FileRegionMemAccessor()50*c120c564SAndrew Turner FileRegionMemAccessor() : TrcMemAccessorBase(MEMACC_FILE) {}; ~FileRegionMemAccessor()51*c120c564SAndrew Turner virtual ~FileRegionMemAccessor() {}; 52*c120c564SAndrew Turner setOffset(const size_t offset)53*c120c564SAndrew Turner void setOffset(const size_t offset) { m_file_offset = offset; }; getOffset()54*c120c564SAndrew Turner const size_t getOffset() const { return m_file_offset; }; 55*c120c564SAndrew Turner 56*c120c564SAndrew Turner bool operator<(const FileRegionMemAccessor& rhs) { return this->m_startAddress < rhs.m_startAddress; }; 57*c120c564SAndrew Turner 58*c120c564SAndrew Turner // not going to use these objects to read bytes - defer to the file class for that. 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)59*c120c564SAndrew Turner 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) { return 0; }; 60*c120c564SAndrew Turner regionStartAddress()61*c120c564SAndrew Turner const ocsd_vaddr_t regionStartAddress() const { return m_startAddress; }; 62*c120c564SAndrew Turner 63*c120c564SAndrew Turner private: 64*c120c564SAndrew Turner size_t m_file_offset; 65*c120c564SAndrew Turner }; 66*c120c564SAndrew Turner 67*c120c564SAndrew Turner /*! 68*c120c564SAndrew Turner * @class TrcMemAccessorFile 69*c120c564SAndrew Turner * @brief Memory accessor for a binary file. 70*c120c564SAndrew Turner * 71*c120c564SAndrew Turner * Memory accessor based on a binary file snapshot of some memory. 72*c120c564SAndrew Turner * 73*c120c564SAndrew Turner * Static creation code to allow reference counted accessor usable for 74*c120c564SAndrew Turner * multiple access maps attached to multiple source trees for the same system. 75*c120c564SAndrew Turner */ 76*c120c564SAndrew Turner class TrcMemAccessorFile : public TrcMemAccessorBase 77*c120c564SAndrew Turner { 78*c120c564SAndrew Turner public: 79*c120c564SAndrew Turner /** read bytes override - reads from file */ 80*c120c564SAndrew Turner virtual const uint32_t readBytes(const ocsd_vaddr_t address, const ocsd_mem_space_acc_t memSpace, const uint8_t trcID, const uint32_t reqBytes, uint8_t *byteBuffer); 81*c120c564SAndrew Turner 82*c120c564SAndrew Turner protected: 83*c120c564SAndrew Turner TrcMemAccessorFile(); /**< protected default constructor */ 84*c120c564SAndrew Turner virtual ~ TrcMemAccessorFile(); /**< protected default destructor */ 85*c120c564SAndrew Turner 86*c120c564SAndrew Turner /** increment reference counter */ IncRefCount()87*c120c564SAndrew Turner void IncRefCount() { m_ref_count++; }; 88*c120c564SAndrew Turner 89*c120c564SAndrew Turner /** decrement reference counter */ DecRefCount()90*c120c564SAndrew Turner void DecRefCount() { m_ref_count--; }; 91*c120c564SAndrew Turner 92*c120c564SAndrew Turner /** get current reference count */ getRefCount()93*c120c564SAndrew Turner const int getRefCount() const { return m_ref_count; }; 94*c120c564SAndrew Turner 95*c120c564SAndrew Turner /*! 96*c120c564SAndrew Turner * Initialise accessor with file name and path, and start address. 97*c120c564SAndrew Turner * File opened and length calculated to determine end address for the range. 98*c120c564SAndrew Turner * 99*c120c564SAndrew Turner * @param &pathToFile : Binary file path and name 100*c120c564SAndrew Turner * @param startAddr : system memory address associated with start of binary datain file. 101*c120c564SAndrew Turner * 102*c120c564SAndrew Turner * @return bool : true if set up successfully, false if file could not be opened. 103*c120c564SAndrew Turner */ 104*c120c564SAndrew Turner ocsd_err_t initAccessor(const std::string &pathToFile, ocsd_vaddr_t startAddr, size_t offset, size_t size); 105*c120c564SAndrew Turner 106*c120c564SAndrew Turner /** get the file path */ getFilePath()107*c120c564SAndrew Turner const std::string &getFilePath() const { return m_file_path; }; 108*c120c564SAndrew Turner 109*c120c564SAndrew Turner /** get an offset region if extant for the address */ 110*c120c564SAndrew Turner FileRegionMemAccessor *getRegionForAddress(const ocsd_vaddr_t startAddr) const; 111*c120c564SAndrew Turner 112*c120c564SAndrew Turner /* validate ranges */ 113*c120c564SAndrew Turner virtual const bool validateRange(); 114*c120c564SAndrew Turner 115*c120c564SAndrew Turner public: 116*c120c564SAndrew Turner 117*c120c564SAndrew Turner /*! 118*c120c564SAndrew Turner * File may contain multiple none-overlapping ranges in a single file. 119*c120c564SAndrew Turner * 120*c120c564SAndrew Turner * @param startAddr : Address for beginning of byte data. 121*c120c564SAndrew Turner * @param size : size of range in bytes. 122*c120c564SAndrew Turner * @param offset : offset into file for that data. 123*c120c564SAndrew Turner * 124*c120c564SAndrew Turner * @return bool : true if set successfully. 125*c120c564SAndrew Turner */ 126*c120c564SAndrew Turner bool AddOffsetRange(const ocsd_vaddr_t startAddr, const size_t size, const size_t offset); 127*c120c564SAndrew Turner 128*c120c564SAndrew Turner /*! 129*c120c564SAndrew Turner * Override in case we have multiple regions in the file. 130*c120c564SAndrew Turner * 131*c120c564SAndrew Turner * @param s_address : Address to test. 132*c120c564SAndrew Turner * 133*c120c564SAndrew Turner * @return const bool : true if the address is in range. 134*c120c564SAndrew Turner */ 135*c120c564SAndrew Turner virtual const bool addrInRange(const ocsd_vaddr_t s_address) const; 136*c120c564SAndrew Turner 137*c120c564SAndrew Turner /*! 138*c120c564SAndrew Turner * test if an address is the start of range for this accessor 139*c120c564SAndrew Turner * 140*c120c564SAndrew Turner * @param s_address : Address to test. 141*c120c564SAndrew Turner * 142*c120c564SAndrew Turner * @return const bool : true if the address is start of range. 143*c120c564SAndrew Turner */ 144*c120c564SAndrew Turner virtual const bool addrStartOfRange(const ocsd_vaddr_t s_address) const; 145*c120c564SAndrew Turner 146*c120c564SAndrew Turner /*! 147*c120c564SAndrew Turner * Test number of bytes available from the start address, up to the number of requested bytes. 148*c120c564SAndrew Turner * Tests if all the requested bytes are available from the supplied start address. 149*c120c564SAndrew Turner * Returns the number available up to full requested amount. 150*c120c564SAndrew Turner * 151*c120c564SAndrew Turner * @param s_address : Start address within the range. 152*c120c564SAndrew Turner * @param reqBytes : Number of bytes needed from the start address. 153*c120c564SAndrew Turner * 154*c120c564SAndrew Turner * @return const uint32_t : Bytes available, up to reqBytes. 0 is s_address not in range. 155*c120c564SAndrew Turner */ 156*c120c564SAndrew Turner virtual const uint32_t bytesInRange(const ocsd_vaddr_t s_address, const uint32_t reqBytes) const; 157*c120c564SAndrew Turner 158*c120c564SAndrew Turner /*! 159*c120c564SAndrew Turner * test is supplied range accessor overlaps this range. 160*c120c564SAndrew Turner * 161*c120c564SAndrew Turner * @param *p_test_acc : Accessor to test for overlap. 162*c120c564SAndrew Turner * 163*c120c564SAndrew Turner * @return bool : true if overlap, false if not. 164*c120c564SAndrew Turner */ 165*c120c564SAndrew Turner virtual const bool overLapRange(const TrcMemAccessorBase *p_test_acc) const; 166*c120c564SAndrew Turner 167*c120c564SAndrew Turner /*! Override to handle ranges and offset accessors plus add in file name. */ 168*c120c564SAndrew Turner virtual void getMemAccString(std::string &accStr) const; 169*c120c564SAndrew Turner 170*c120c564SAndrew Turner 171*c120c564SAndrew Turner /*! 172*c120c564SAndrew Turner * Create a file accessor based on the supplied path and address. 173*c120c564SAndrew Turner * Keeps a list of file accessors created. 174*c120c564SAndrew Turner * 175*c120c564SAndrew Turner * File will be checked to ensure valid accessor can be created. 176*c120c564SAndrew Turner * 177*c120c564SAndrew Turner * If an accessor using the supplied file is currently in use then a reference to that 178*c120c564SAndrew Turner * accessor will be returned and the accessor reference counter updated. 179*c120c564SAndrew Turner * 180*c120c564SAndrew Turner * @param &pathToFile : Path to binary file 181*c120c564SAndrew Turner * @param startAddr : Start address of data represented by file. 182*c120c564SAndrew Turner * 183*c120c564SAndrew Turner * @return TrcMemAccessorFile * : pointer to accessor if successful, 0 if it could not be created. 184*c120c564SAndrew Turner */ 185*c120c564SAndrew Turner static ocsd_err_t createFileAccessor(TrcMemAccessorFile **p_acc, const std::string &pathToFile, ocsd_vaddr_t startAddr, size_t offset = 0, size_t size = 0); 186*c120c564SAndrew Turner 187*c120c564SAndrew Turner /*! 188*c120c564SAndrew Turner * Destroy supplied accessor. 189*c120c564SAndrew Turner * 190*c120c564SAndrew Turner * Reference counter decremented and checked and accessor destroyed if no longer in use. 191*c120c564SAndrew Turner * 192*c120c564SAndrew Turner * @param *p_accessor : File Accessor to destroy. 193*c120c564SAndrew Turner */ 194*c120c564SAndrew Turner static void destroyFileAccessor(TrcMemAccessorFile *p_accessor); 195*c120c564SAndrew Turner 196*c120c564SAndrew Turner /*! 197*c120c564SAndrew Turner * Test if any accessor is currently using the supplied file path 198*c120c564SAndrew Turner * 199*c120c564SAndrew Turner * @param &pathToFile : Path to test. 200*c120c564SAndrew Turner * 201*c120c564SAndrew Turner * @return bool : true if an accessor exists with this file path. 202*c120c564SAndrew Turner */ 203*c120c564SAndrew Turner static const bool isExistingFileAccessor(const std::string &pathToFile); 204*c120c564SAndrew Turner 205*c120c564SAndrew Turner /*! 206*c120c564SAndrew Turner * Get the accessor using the supplied file path 207*c120c564SAndrew Turner * Use after createFileAccessor if additional memory ranges need 208*c120c564SAndrew Turner * adding to an exiting file accessor. 209*c120c564SAndrew Turner * 210*c120c564SAndrew Turner * @param &pathToFile : Path to test. 211*c120c564SAndrew Turner * 212*c120c564SAndrew Turner * @return TrcMemAccessorFile * : none 0 if an accessor exists with this file path. 213*c120c564SAndrew Turner */ 214*c120c564SAndrew Turner static TrcMemAccessorFile * getExistingFileAccessor(const std::string &pathToFile); 215*c120c564SAndrew Turner 216*c120c564SAndrew Turner 217*c120c564SAndrew Turner 218*c120c564SAndrew Turner 219*c120c564SAndrew Turner private: 220*c120c564SAndrew Turner static std::map<std::string, TrcMemAccessorFile *> s_FileAccessorMap; /**< map of file accessors in use. */ 221*c120c564SAndrew Turner 222*c120c564SAndrew Turner private: 223*c120c564SAndrew Turner std::ifstream m_mem_file; /**< input binary file stream */ 224*c120c564SAndrew Turner ocsd_vaddr_t m_file_size; /**< size of the file */ 225*c120c564SAndrew Turner int m_ref_count; /**< accessor reference count */ 226*c120c564SAndrew Turner std::string m_file_path; /**< path to input file */ 227*c120c564SAndrew Turner std::list<FileRegionMemAccessor *> m_access_regions; /**< additional regions in the file at non-zero offsets */ 228*c120c564SAndrew Turner bool m_base_range_set; /**< true when offset 0 set */ 229*c120c564SAndrew Turner bool m_has_access_regions; /**< true if single file contains multiple regions */ 230*c120c564SAndrew Turner }; 231*c120c564SAndrew Turner 232*c120c564SAndrew Turner #endif // ARM_TRC_MEM_ACC_FILE_H_INCLUDED 233*c120c564SAndrew Turner 234*c120c564SAndrew Turner /* End of File trc_mem_acc_file.h */ 235