port_proxy.hh revision 14009
18706Sandreas.hansson@arm.com/*
212522Sandreas.sandberg@arm.com * Copyright (c) 2011-2013, 2018 ARM Limited
38706Sandreas.hansson@arm.com * All rights reserved
48706Sandreas.hansson@arm.com *
58706Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68706Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78706Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88706Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98706Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108706Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118706Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128706Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138706Sandreas.hansson@arm.com *
148706Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
158706Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
168706Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
178706Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
188706Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
198706Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
208706Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
218706Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
228706Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
238706Sandreas.hansson@arm.com * this software without specific prior written permission.
248706Sandreas.hansson@arm.com *
258706Sandreas.hansson@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
268706Sandreas.hansson@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
278706Sandreas.hansson@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
288706Sandreas.hansson@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
298706Sandreas.hansson@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
308706Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
318706Sandreas.hansson@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
328706Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
338706Sandreas.hansson@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
348706Sandreas.hansson@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
358706Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
368706Sandreas.hansson@arm.com *
378706Sandreas.hansson@arm.com * Authors: Andreas Hansson
388706Sandreas.hansson@arm.com */
398706Sandreas.hansson@arm.com
408706Sandreas.hansson@arm.com/**
418706Sandreas.hansson@arm.com * @file
428706Sandreas.hansson@arm.com * PortProxy Object Declaration.
438706Sandreas.hansson@arm.com *
448853Sandreas.hansson@arm.com * Port proxies are used when non-structural entities need access to
458853Sandreas.hansson@arm.com * the memory system (or structural entities that want to peak into
468853Sandreas.hansson@arm.com * the memory system without making a real memory access).
478853Sandreas.hansson@arm.com *
488853Sandreas.hansson@arm.com * Proxy objects replace the previous FunctionalPort, TranslatingPort
498853Sandreas.hansson@arm.com * and VirtualPort objects, which provided the same functionality as
508853Sandreas.hansson@arm.com * the proxies, but were instances of ports not corresponding to real
518853Sandreas.hansson@arm.com * structural ports of the simulated system. Via the port proxies all
528853Sandreas.hansson@arm.com * the accesses go through an actual port (either the system port,
538853Sandreas.hansson@arm.com * e.g. for processes or initialisation, or a the data port of the
548853Sandreas.hansson@arm.com * CPU, e.g. for threads) and thus are transparent to a potentially
558706Sandreas.hansson@arm.com * distributed memory and automatically adhere to the memory map of
568706Sandreas.hansson@arm.com * the system.
578706Sandreas.hansson@arm.com */
588706Sandreas.hansson@arm.com
598706Sandreas.hansson@arm.com#ifndef __MEM_PORT_PROXY_HH__
608706Sandreas.hansson@arm.com#define __MEM_PORT_PROXY_HH__
618706Sandreas.hansson@arm.com
628706Sandreas.hansson@arm.com#include "mem/port.hh"
638706Sandreas.hansson@arm.com#include "sim/byteswap.hh"
648706Sandreas.hansson@arm.com
658706Sandreas.hansson@arm.com/**
668853Sandreas.hansson@arm.com * This object is a proxy for a structural port, to be used for debug
678853Sandreas.hansson@arm.com * accesses.
688706Sandreas.hansson@arm.com *
698706Sandreas.hansson@arm.com * This proxy object is used when non structural entities
708706Sandreas.hansson@arm.com * (e.g. thread contexts, object file loaders) need access to the
718706Sandreas.hansson@arm.com * memory system. It calls the corresponding functions on the underlying
728706Sandreas.hansson@arm.com * structural port, and provides templatized convenience access functions.
738706Sandreas.hansson@arm.com *
748706Sandreas.hansson@arm.com * The addresses are interpreted as physical addresses.
758706Sandreas.hansson@arm.com *
768706Sandreas.hansson@arm.com * @sa SETranslatingProxy
778706Sandreas.hansson@arm.com * @sa FSTranslatingProxy
788706Sandreas.hansson@arm.com */
798706Sandreas.hansson@arm.comclass PortProxy
808706Sandreas.hansson@arm.com{
818853Sandreas.hansson@arm.com  private:
828853Sandreas.hansson@arm.com
838853Sandreas.hansson@arm.com    /** The actual physical port used by this proxy. */
848922Swilliam.wang@arm.com    MasterPort &_port;
858706Sandreas.hansson@arm.com
869814Sandreas.hansson@arm.com    /** Granularity of any transactions issued through this proxy. */
879814Sandreas.hansson@arm.com    const unsigned int _cacheLineSize;
889814Sandreas.hansson@arm.com
898706Sandreas.hansson@arm.com  public:
909814Sandreas.hansson@arm.com    PortProxy(MasterPort &port, unsigned int cacheLineSize) :
9113893Sgabeblack@google.com        _port(port), _cacheLineSize(cacheLineSize)
9213893Sgabeblack@google.com    {}
938706Sandreas.hansson@arm.com    virtual ~PortProxy() { }
948706Sandreas.hansson@arm.com
958706Sandreas.hansson@arm.com
968706Sandreas.hansson@arm.com
9714008Sgabeblack@google.com    /** Fixed functionality for use in base classes. */
9812532Sandreas.sandberg@arm.com
9912532Sandreas.sandberg@arm.com    /**
10012532Sandreas.sandberg@arm.com     * Read size bytes memory at physical address and store in p.
10112532Sandreas.sandberg@arm.com     */
10212532Sandreas.sandberg@arm.com    void readBlobPhys(Addr addr, Request::Flags flags,
10314009Sgabeblack@google.com                      void *p, int size) const;
10412532Sandreas.sandberg@arm.com
10512532Sandreas.sandberg@arm.com    /**
10612532Sandreas.sandberg@arm.com     * Write size bytes from p to physical address.
10712532Sandreas.sandberg@arm.com     */
10812532Sandreas.sandberg@arm.com    void writeBlobPhys(Addr addr, Request::Flags flags,
10914009Sgabeblack@google.com                       const void *p, int size) const;
11012532Sandreas.sandberg@arm.com
11112532Sandreas.sandberg@arm.com    /**
11212532Sandreas.sandberg@arm.com     * Fill size bytes starting at physical addr with byte value val.
11312532Sandreas.sandberg@arm.com     */
11412532Sandreas.sandberg@arm.com    void memsetBlobPhys(Addr addr, Request::Flags flags,
11512532Sandreas.sandberg@arm.com                        uint8_t v, int size) const;
1168706Sandreas.hansson@arm.com
11714008Sgabeblack@google.com
11814008Sgabeblack@google.com
11914008Sgabeblack@google.com    /** Methods to override in base classes */
12014008Sgabeblack@google.com
12114008Sgabeblack@google.com    /**
12214008Sgabeblack@google.com     * Read size bytes memory at address and store in p.
12314008Sgabeblack@google.com     * Returns true on success and false on failure.
12414008Sgabeblack@google.com     */
12514008Sgabeblack@google.com    virtual bool
12614009Sgabeblack@google.com    tryReadBlob(Addr addr, void *p, int size) const
12714008Sgabeblack@google.com    {
12814008Sgabeblack@google.com        readBlobPhys(addr, 0, p, size);
12914008Sgabeblack@google.com        return true;
13014008Sgabeblack@google.com    }
13114008Sgabeblack@google.com
13214008Sgabeblack@google.com    /**
13314008Sgabeblack@google.com     * Write size bytes from p to address.
13414008Sgabeblack@google.com     * Returns true on success and false on failure.
13514008Sgabeblack@google.com     */
13614008Sgabeblack@google.com    virtual bool
13714009Sgabeblack@google.com    tryWriteBlob(Addr addr, const void *p, int size) const
13814008Sgabeblack@google.com    {
13914008Sgabeblack@google.com        writeBlobPhys(addr, 0, p, size);
14014008Sgabeblack@google.com        return true;
14114008Sgabeblack@google.com    }
14214008Sgabeblack@google.com
14314008Sgabeblack@google.com    /**
14414008Sgabeblack@google.com     * Fill size bytes starting at addr with byte value val.
14514008Sgabeblack@google.com     * Returns true on success and false on failure.
14614008Sgabeblack@google.com     */
14714008Sgabeblack@google.com    virtual bool
14814008Sgabeblack@google.com    tryMemsetBlob(Addr addr, uint8_t val, int size) const
14914008Sgabeblack@google.com    {
15014008Sgabeblack@google.com        memsetBlobPhys(addr, 0, val, size);
15114008Sgabeblack@google.com        return true;
15214008Sgabeblack@google.com    }
15314008Sgabeblack@google.com
15414008Sgabeblack@google.com
15514008Sgabeblack@google.com
15614008Sgabeblack@google.com    /** Higher level interfaces based on the above. */
15714008Sgabeblack@google.com
15814008Sgabeblack@google.com    /**
15914008Sgabeblack@google.com     * Same as tryReadBlob, but insists on success.
16014008Sgabeblack@google.com     */
16114008Sgabeblack@google.com    void
16214009Sgabeblack@google.com    readBlob(Addr addr, void *p, int size) const
16314008Sgabeblack@google.com    {
16414008Sgabeblack@google.com        if (!tryReadBlob(addr, p, size))
16514008Sgabeblack@google.com            fatal("readBlob(%#x, ...) failed", addr);
16614008Sgabeblack@google.com    }
16714008Sgabeblack@google.com
16814008Sgabeblack@google.com    /**
16914008Sgabeblack@google.com     * Same as tryWriteBlob, but insists on success.
17014008Sgabeblack@google.com     */
17114008Sgabeblack@google.com    void
17214009Sgabeblack@google.com    writeBlob(Addr addr, const void *p, int size) const
17314008Sgabeblack@google.com    {
17414008Sgabeblack@google.com        if (!tryWriteBlob(addr, p, size))
17514008Sgabeblack@google.com            fatal("writeBlob(%#x, ...) failed", addr);
17614008Sgabeblack@google.com    }
17714008Sgabeblack@google.com
17814008Sgabeblack@google.com    /**
17914008Sgabeblack@google.com     * Same as tryMemsetBlob, but insists on success.
18014008Sgabeblack@google.com     */
18114008Sgabeblack@google.com    void
18214008Sgabeblack@google.com    memsetBlob(Addr addr, uint8_t v, int size) const
18314008Sgabeblack@google.com    {
18414008Sgabeblack@google.com        if (!tryMemsetBlob(addr, v, size))
18514008Sgabeblack@google.com            fatal("memsetBlob(%#x, ...) failed", addr);
18614008Sgabeblack@google.com    }
18714008Sgabeblack@google.com
1888706Sandreas.hansson@arm.com    /**
1898706Sandreas.hansson@arm.com     * Read sizeof(T) bytes from address and return as object T.
1908706Sandreas.hansson@arm.com     */
1918706Sandreas.hansson@arm.com    template <typename T>
1928861Sandreas.hansson@arm.com    T read(Addr address) const;
1938706Sandreas.hansson@arm.com
1948706Sandreas.hansson@arm.com    /**
1958706Sandreas.hansson@arm.com     * Write object T to address. Writes sizeof(T) bytes.
1968706Sandreas.hansson@arm.com     */
1978706Sandreas.hansson@arm.com    template <typename T>
1988861Sandreas.hansson@arm.com    void write(Addr address, T data) const;
1998706Sandreas.hansson@arm.com
20012522Sandreas.sandberg@arm.com    /**
20112522Sandreas.sandberg@arm.com     * Read sizeof(T) bytes from address and return as object T.
20213893Sgabeblack@google.com     * Performs endianness conversion from the selected guest to host order.
20312522Sandreas.sandberg@arm.com     */
20412522Sandreas.sandberg@arm.com    template <typename T>
20513893Sgabeblack@google.com    T read(Addr address, ByteOrder guest_byte_order) const;
20612522Sandreas.sandberg@arm.com
20712522Sandreas.sandberg@arm.com    /**
20812522Sandreas.sandberg@arm.com     * Write object T to address. Writes sizeof(T) bytes.
20913893Sgabeblack@google.com     * Performs endianness conversion from host to the selected guest order.
21012522Sandreas.sandberg@arm.com     */
21112522Sandreas.sandberg@arm.com    template <typename T>
21213893Sgabeblack@google.com    void write(Addr address, T data, ByteOrder guest_byte_order) const;
21314008Sgabeblack@google.com
21414008Sgabeblack@google.com    /**
21514008Sgabeblack@google.com     * Write the string str into guest memory at address addr.
21614008Sgabeblack@google.com     * Returns true on success and false on failure.
21714008Sgabeblack@google.com     */
21814008Sgabeblack@google.com    bool tryWriteString(Addr addr, const char *str) const;
21914008Sgabeblack@google.com
22014008Sgabeblack@google.com    /**
22114008Sgabeblack@google.com     * Same as tryWriteString, but insists on success.
22214008Sgabeblack@google.com     */
22314008Sgabeblack@google.com    void
22414008Sgabeblack@google.com    writeString(Addr addr, const char *str) const
22514008Sgabeblack@google.com    {
22614008Sgabeblack@google.com        if (!tryWriteString(addr, str))
22714008Sgabeblack@google.com            fatal("writeString(%#x, ...) failed", addr);
22814008Sgabeblack@google.com    }
22914008Sgabeblack@google.com
23014008Sgabeblack@google.com    /**
23114008Sgabeblack@google.com     * Reads the string at guest address addr into the std::string str.
23214008Sgabeblack@google.com     * Returns true on success and false on failure.
23314008Sgabeblack@google.com     */
23414008Sgabeblack@google.com    bool tryReadString(std::string &str, Addr addr) const;
23514008Sgabeblack@google.com
23614008Sgabeblack@google.com    /**
23714008Sgabeblack@google.com     * Same as tryReadString, but insists on success.
23814008Sgabeblack@google.com     */
23914008Sgabeblack@google.com    void
24014008Sgabeblack@google.com    readString(std::string &str, Addr addr) const
24114008Sgabeblack@google.com    {
24214008Sgabeblack@google.com        if (!tryReadString(str, addr))
24314008Sgabeblack@google.com            fatal("readString(%#x, ...) failed", addr);
24414008Sgabeblack@google.com    }
2458706Sandreas.hansson@arm.com};
2468706Sandreas.hansson@arm.com
2478706Sandreas.hansson@arm.com
2488706Sandreas.hansson@arm.comtemplate <typename T>
2498706Sandreas.hansson@arm.comT
2508861Sandreas.hansson@arm.comPortProxy::read(Addr address) const
2518706Sandreas.hansson@arm.com{
2528706Sandreas.hansson@arm.com    T data;
25314009Sgabeblack@google.com    readBlob(address, &data, sizeof(T));
2548706Sandreas.hansson@arm.com    return data;
2558706Sandreas.hansson@arm.com}
2568706Sandreas.hansson@arm.com
2578706Sandreas.hansson@arm.comtemplate <typename T>
2588706Sandreas.hansson@arm.comvoid
2598861Sandreas.hansson@arm.comPortProxy::write(Addr address, T data) const
2608706Sandreas.hansson@arm.com{
26114009Sgabeblack@google.com    writeBlob(address, &data, sizeof(T));
2628706Sandreas.hansson@arm.com}
2638706Sandreas.hansson@arm.com
26412522Sandreas.sandberg@arm.comtemplate <typename T>
26512522Sandreas.sandberg@arm.comT
26613893Sgabeblack@google.comPortProxy::read(Addr address, ByteOrder byte_order) const
26712522Sandreas.sandberg@arm.com{
26812522Sandreas.sandberg@arm.com    T data;
26914009Sgabeblack@google.com    readBlob(address, &data, sizeof(T));
27012522Sandreas.sandberg@arm.com    return gtoh(data, byte_order);
27112522Sandreas.sandberg@arm.com}
27212522Sandreas.sandberg@arm.com
27312522Sandreas.sandberg@arm.comtemplate <typename T>
27412522Sandreas.sandberg@arm.comvoid
27513893Sgabeblack@google.comPortProxy::write(Addr address, T data, ByteOrder byte_order) const
27612522Sandreas.sandberg@arm.com{
27712522Sandreas.sandberg@arm.com    data = htog(data, byte_order);
27814009Sgabeblack@google.com    writeBlob(address, &data, sizeof(T));
27912522Sandreas.sandberg@arm.com}
28012522Sandreas.sandberg@arm.com
2818706Sandreas.hansson@arm.com#endif // __MEM_PORT_PROXY_HH__
282