port_proxy.hh revision 14008:e36048ba1c2c
11689SN/A/*
210333Smitch.hayenga@arm.com * Copyright (c) 2011-2013, 2018 ARM Limited
39920Syasuko.eckert@amd.com * All rights reserved
47944SGiacomo.Gabrielli@arm.com *
57944SGiacomo.Gabrielli@arm.com * The license below extends only to copyright in the software and shall
67944SGiacomo.Gabrielli@arm.com * not be construed as granting a license to any other intellectual
77944SGiacomo.Gabrielli@arm.com * property including but not limited to intellectual property relating
87944SGiacomo.Gabrielli@arm.com * to a hardware implementation of the functionality of the software
97944SGiacomo.Gabrielli@arm.com * licensed hereunder.  You may use the software subject to the license
107944SGiacomo.Gabrielli@arm.com * terms below provided that you ensure that this notice is replicated
117944SGiacomo.Gabrielli@arm.com * unmodified and in its entirety in all distributions of the software,
127944SGiacomo.Gabrielli@arm.com * modified or unmodified, in source code or in binary form.
137944SGiacomo.Gabrielli@arm.com *
147944SGiacomo.Gabrielli@arm.com * Redistribution and use in source and binary forms, with or without
152326SN/A * modification, are permitted provided that the following conditions are
161689SN/A * met: redistributions of source code must retain the above copyright
171689SN/A * notice, this list of conditions and the following disclaimer;
181689SN/A * redistributions in binary form must reproduce the above copyright
191689SN/A * notice, this list of conditions and the following disclaimer in the
201689SN/A * documentation and/or other materials provided with the distribution;
211689SN/A * neither the name of the copyright holders nor the names of its
221689SN/A * contributors may be used to endorse or promote products derived from
231689SN/A * this software without specific prior written permission.
241689SN/A *
251689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
261689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
271689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
281689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
291689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
301689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
311689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
321689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
331689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
341689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
351689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
361689SN/A *
371689SN/A * Authors: Andreas Hansson
381689SN/A */
391689SN/A
402665Ssaidi@eecs.umich.edu/**
412665Ssaidi@eecs.umich.edu * @file
422831Sksewell@umich.edu * PortProxy Object Declaration.
431689SN/A *
441689SN/A * Port proxies are used when non-structural entities need access to
459944Smatt.horsnell@ARM.com * the memory system (or structural entities that want to peak into
469944Smatt.horsnell@ARM.com * the memory system without making a real memory access).
479944Smatt.horsnell@ARM.com *
482064SN/A * Proxy objects replace the previous FunctionalPort, TranslatingPort
491060SN/A * and VirtualPort objects, which provided the same functionality as
501060SN/A * the proxies, but were instances of ports not corresponding to real
512292SN/A * structural ports of the simulated system. Via the port proxies all
521717SN/A * the accesses go through an actual port (either the system port,
538232Snate@binkert.org * e.g. for processes or initialisation, or a the data port of the
544762Snate@binkert.org * CPU, e.g. for threads) and thus are transparent to a potentially
556221Snate@binkert.org * distributed memory and automatically adhere to the memory map of
564762Snate@binkert.org * the system.
571060SN/A */
588737Skoansin.tan@gmail.com
598737Skoansin.tan@gmail.com#ifndef __MEM_PORT_PROXY_HH__
608737Skoansin.tan@gmail.com#define __MEM_PORT_PROXY_HH__
615529Snate@binkert.org
621061SN/A#include "mem/port.hh"
632292SN/A#include "sim/byteswap.hh"
645606Snate@binkert.org
658581Ssteve.reinhardt@amd.com/**
668581Ssteve.reinhardt@amd.com * This object is a proxy for a structural port, to be used for debug
671060SN/A * accesses.
682292SN/A *
692292SN/A * This proxy object is used when non structural entities
702292SN/A * (e.g. thread contexts, object file loaders) need access to the
712292SN/A * memory system. It calls the corresponding functions on the underlying
722292SN/A * structural port, and provides templatized convenience access functions.
732292SN/A *
742326SN/A * The addresses are interpreted as physical addresses.
752292SN/A *
762292SN/A * @sa SETranslatingProxy
772292SN/A * @sa FSTranslatingProxy
782292SN/A */
792292SN/Aclass PortProxy
802292SN/A{
815336Shines@cs.fsu.edu  private:
822292SN/A
834873Sstever@eecs.umich.edu    /** The actual physical port used by this proxy. */
842292SN/A    MasterPort &_port;
852292SN/A
862292SN/A    /** Granularity of any transactions issued through this proxy. */
874329Sktlim@umich.edu    const unsigned int _cacheLineSize;
885529Snate@binkert.org
894329Sktlim@umich.edu  public:
904329Sktlim@umich.edu    PortProxy(MasterPort &port, unsigned int cacheLineSize) :
914329Sktlim@umich.edu        _port(port), _cacheLineSize(cacheLineSize)
922292SN/A    {}
932292SN/A    virtual ~PortProxy() { }
942292SN/A
952292SN/A
962292SN/A
972292SN/A    /** Fixed functionality for use in base classes. */
985529Snate@binkert.org
991060SN/A    /**
1009920Syasuko.eckert@amd.com     * Read size bytes memory at physical address and store in p.
1019920Syasuko.eckert@amd.com     */
10210935Snilay@cs.wisc.edu    void readBlobPhys(Addr addr, Request::Flags flags,
1031060SN/A                      uint8_t* p, int size) const;
1041060SN/A
1051060SN/A    /**
1062326SN/A     * Write size bytes from p to physical address.
1071060SN/A     */
1081060SN/A    void writeBlobPhys(Addr addr, Request::Flags flags,
1091060SN/A                       const uint8_t* p, int size) const;
1101060SN/A
1112292SN/A    /**
1126221Snate@binkert.org     * Fill size bytes starting at physical addr with byte value val.
1136221Snate@binkert.org     */
1146221Snate@binkert.org    void memsetBlobPhys(Addr addr, Request::Flags flags,
1151060SN/A                        uint8_t v, int size) const;
1161060SN/A
1172307SN/A
1182292SN/A
1192980Sgblack@eecs.umich.edu    /** Methods to override in base classes */
1202292SN/A
1212292SN/A    /**
1222292SN/A     * Read size bytes memory at address and store in p.
1232292SN/A     * Returns true on success and false on failure.
1242292SN/A     */
1252292SN/A    virtual bool
1262292SN/A    tryReadBlob(Addr addr, uint8_t *p, int size) const
1272292SN/A    {
1282292SN/A        readBlobPhys(addr, 0, p, size);
1292292SN/A        return true;
1306221Snate@binkert.org    }
1316221Snate@binkert.org
1322292SN/A    /**
1332292SN/A     * Write size bytes from p to address.
1342292SN/A     * Returns true on success and false on failure.
1352292SN/A     */
1362292SN/A    virtual bool
1372292SN/A    tryWriteBlob(Addr addr, const uint8_t *p, int size) const
1382292SN/A    {
1392292SN/A        writeBlobPhys(addr, 0, p, size);
1402292SN/A        return true;
1416221Snate@binkert.org    }
1426221Snate@binkert.org
1432292SN/A    /**
1442292SN/A     * Fill size bytes starting at addr with byte value val.
1452831Sksewell@umich.edu     * Returns true on success and false on failure.
1462292SN/A     */
1472292SN/A    virtual bool
1482292SN/A    tryMemsetBlob(Addr addr, uint8_t val, int size) const
1492292SN/A    {
1502292SN/A        memsetBlobPhys(addr, 0, val, size);
1512292SN/A        return true;
1522292SN/A    }
1532292SN/A
1542292SN/A
1556221Snate@binkert.org
1566221Snate@binkert.org    /** Higher level interfaces based on the above. */
1572292SN/A
1582292SN/A    /**
1592831Sksewell@umich.edu     * Same as tryReadBlob, but insists on success.
1602292SN/A     */
1612292SN/A    void
1622292SN/A    readBlob(Addr addr, uint8_t* p, int size) const
1632292SN/A    {
1642292SN/A        if (!tryReadBlob(addr, p, size))
1652292SN/A            fatal("readBlob(%#x, ...) failed", addr);
1662292SN/A    }
1672292SN/A
1682292SN/A    /**
1692292SN/A     * Same as tryWriteBlob, but insists on success.
1702326SN/A     */
1712348SN/A    void
1722326SN/A    writeBlob(Addr addr, const uint8_t* p, int size) const
1732326SN/A    {
1742348SN/A        if (!tryWriteBlob(addr, p, size))
1752292SN/A            fatal("writeBlob(%#x, ...) failed", addr);
1762292SN/A    }
1772292SN/A
1782292SN/A    /**
1792292SN/A     * Same as tryMemsetBlob, but insists on success.
1802292SN/A     */
1812292SN/A    void
1821060SN/A    memsetBlob(Addr addr, uint8_t v, int size) const
1831060SN/A    {
1841061SN/A        if (!tryMemsetBlob(addr, v, size))
1851060SN/A            fatal("memsetBlob(%#x, ...) failed", addr);
1861062SN/A    }
1871062SN/A
1882301SN/A    /**
1891062SN/A     * Read sizeof(T) bytes from address and return as object T.
1901062SN/A     */
1911062SN/A    template <typename T>
1921062SN/A    T read(Addr address) const;
1931062SN/A
1941062SN/A    /**
1951062SN/A     * Write object T to address. Writes sizeof(T) bytes.
1961062SN/A     */
1971062SN/A    template <typename T>
1981062SN/A    void write(Addr address, T data) const;
1992301SN/A
2002301SN/A    /**
2012301SN/A     * Read sizeof(T) bytes from address and return as object T.
2022301SN/A     * Performs endianness conversion from the selected guest to host order.
2031062SN/A     */
2041062SN/A    template <typename T>
2051062SN/A    T read(Addr address, ByteOrder guest_byte_order) const;
2061062SN/A
2071062SN/A    /**
2081062SN/A     * Write object T to address. Writes sizeof(T) bytes.
2091062SN/A     * Performs endianness conversion from host to the selected guest order.
2101062SN/A     */
2111062SN/A    template <typename T>
2121062SN/A    void write(Addr address, T data, ByteOrder guest_byte_order) const;
2131062SN/A
2141062SN/A    /**
2151062SN/A     * Write the string str into guest memory at address addr.
2161062SN/A     * Returns true on success and false on failure.
2171062SN/A     */
2181062SN/A    bool tryWriteString(Addr addr, const char *str) const;
2191062SN/A
2201062SN/A    /**
2211062SN/A     * Same as tryWriteString, but insists on success.
2221062SN/A     */
2231062SN/A    void
2241062SN/A    writeString(Addr addr, const char *str) const
2251062SN/A    {
2261062SN/A        if (!tryWriteString(addr, str))
2271062SN/A            fatal("writeString(%#x, ...) failed", addr);
2281062SN/A    }
2291062SN/A
2301062SN/A    /**
2311062SN/A     * Reads the string at guest address addr into the std::string str.
2321062SN/A     * Returns true on success and false on failure.
2331062SN/A     */
2341062SN/A    bool tryReadString(std::string &str, Addr addr) const;
2351062SN/A
2361062SN/A    /**
2371062SN/A     * Same as tryReadString, but insists on success.
2381062SN/A     */
2391062SN/A    void
2401062SN/A    readString(std::string &str, Addr addr) const
2411062SN/A    {
2421062SN/A        if (!tryReadString(str, addr))
2431062SN/A            fatal("readString(%#x, ...) failed", addr);
2441062SN/A    }
2451062SN/A};
2461062SN/A
2471062SN/A
2481062SN/Atemplate <typename T>
2491062SN/AT
2502361SN/APortProxy::read(Addr address) const
2512326SN/A{
2522301SN/A    T data;
2532301SN/A    readBlob(address, (uint8_t*)&data, sizeof(T));
2542301SN/A    return data;
2552301SN/A}
2562301SN/A
2572301SN/Atemplate <typename T>
2582326SN/Avoid
2592301SN/APortProxy::write(Addr address, T data) const
2602361SN/A{
2612326SN/A    writeBlob(address, (uint8_t*)&data, sizeof(T));
2622307SN/A}
2638240Snate@binkert.org
2642301SN/Atemplate <typename T>
2652307SN/AT
2662301SN/APortProxy::read(Addr address, ByteOrder byte_order) const
2672301SN/A{
2682301SN/A    T data;
2692301SN/A    readBlob(address, (uint8_t*)&data, sizeof(T));
2708240Snate@binkert.org    return gtoh(data, byte_order);
2712301SN/A}
2722301SN/A
2732301SN/Atemplate <typename T>
2742301SN/Avoid
2752301SN/APortProxy::write(Addr address, T data, ByteOrder byte_order) const
2762301SN/A{
2772301SN/A    data = htog(data, byte_order);
2782326SN/A    writeBlob(address, (uint8_t*)&data, sizeof(T));
2794762Snate@binkert.org}
2808240Snate@binkert.org
2812301SN/A#endif // __MEM_PORT_PROXY_HH__
2822301SN/A