physical.hh revision 10482
12391SN/A/*
28931Sandreas.hansson@arm.com * Copyright (c) 2012 ARM Limited
38931Sandreas.hansson@arm.com * All rights reserved
48931Sandreas.hansson@arm.com *
58931Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68931Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78931Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88931Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98931Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108931Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118931Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128931Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
132391SN/A *
142391SN/A * Redistribution and use in source and binary forms, with or without
152391SN/A * modification, are permitted provided that the following conditions are
162391SN/A * met: redistributions of source code must retain the above copyright
172391SN/A * notice, this list of conditions and the following disclaimer;
182391SN/A * redistributions in binary form must reproduce the above copyright
192391SN/A * notice, this list of conditions and the following disclaimer in the
202391SN/A * documentation and/or other materials provided with the distribution;
212391SN/A * neither the name of the copyright holders nor the names of its
222391SN/A * contributors may be used to endorse or promote products derived from
232391SN/A * this software without specific prior written permission.
242391SN/A *
252391SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
262391SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272391SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282391SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
292391SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
302391SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
312391SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
322391SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
332391SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
342391SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
352391SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362665Ssaidi@eecs.umich.edu *
378931Sandreas.hansson@arm.com * Authors: Andreas Hansson
382391SN/A */
392391SN/A
4010482Sandreas.hansson@arm.com#ifndef __MEM_PHYSICAL_HH__
4110482Sandreas.hansson@arm.com#define __MEM_PHYSICAL_HH__
422391SN/A
439235Sandreas.hansson@arm.com#include "base/addr_range_map.hh"
4410482Sandreas.hansson@arm.com#include "mem/packet.hh"
459293Sandreas.hansson@arm.com
469293Sandreas.hansson@arm.com/**
479293Sandreas.hansson@arm.com * Forward declaration to avoid header dependencies.
489293Sandreas.hansson@arm.com */
499293Sandreas.hansson@arm.comclass AbstractMemory;
504762Snate@binkert.org
518931Sandreas.hansson@arm.com/**
528931Sandreas.hansson@arm.com * The physical memory encapsulates all memories in the system and
538931Sandreas.hansson@arm.com * provides basic functionality for accessing those memories without
548931Sandreas.hansson@arm.com * going through the memory system and interconnect.
559293Sandreas.hansson@arm.com *
569293Sandreas.hansson@arm.com * The physical memory is also responsible for providing the host
579293Sandreas.hansson@arm.com * system backingstore used by the memories in the simulated guest
589293Sandreas.hansson@arm.com * system. When the system is created, the physical memory allocates
599293Sandreas.hansson@arm.com * the backing store based on the address ranges that are populated in
6010482Sandreas.hansson@arm.com * the system, and does so independent of how those map to actual
619293Sandreas.hansson@arm.com * memory controllers. Thus, the physical memory completely abstracts
629293Sandreas.hansson@arm.com * the mapping of the backing store of the host system and the address
639293Sandreas.hansson@arm.com * mapping in the guest system. This enables us to arbitrarily change
649293Sandreas.hansson@arm.com * the number of memory controllers, and their address mapping, as
659293Sandreas.hansson@arm.com * long as the ranges stay the same.
668931Sandreas.hansson@arm.com */
679293Sandreas.hansson@arm.comclass PhysicalMemory : public Serializable
682391SN/A{
692413SN/A
702391SN/A  private:
712391SN/A
729293Sandreas.hansson@arm.com    // Name for debugging
739293Sandreas.hansson@arm.com    std::string _name;
749293Sandreas.hansson@arm.com
758931Sandreas.hansson@arm.com    // Global address map
769235Sandreas.hansson@arm.com    AddrRangeMap<AbstractMemory*> addrMap;
773170Sstever@eecs.umich.edu
788931Sandreas.hansson@arm.com    // a mutable cache for the last range that matched an address
799235Sandreas.hansson@arm.com    mutable AddrRange rangeCache;
803170Sstever@eecs.umich.edu
818931Sandreas.hansson@arm.com    // All address-mapped memories
828931Sandreas.hansson@arm.com    std::vector<AbstractMemory*> memories;
833170Sstever@eecs.umich.edu
848931Sandreas.hansson@arm.com    // The total memory size
858931Sandreas.hansson@arm.com    uint64_t size;
863170Sstever@eecs.umich.edu
879293Sandreas.hansson@arm.com    // The physical memory used to provide the memory in the simulated
889293Sandreas.hansson@arm.com    // system
8910482Sandreas.hansson@arm.com    std::vector<std::pair<AddrRange, uint8_t*>> backingStore;
909293Sandreas.hansson@arm.com
918931Sandreas.hansson@arm.com    // Prevent copying
928931Sandreas.hansson@arm.com    PhysicalMemory(const PhysicalMemory&);
933170Sstever@eecs.umich.edu
948931Sandreas.hansson@arm.com    // Prevent assignment
958931Sandreas.hansson@arm.com    PhysicalMemory& operator=(const PhysicalMemory&);
968719SAli.Saidi@ARM.com
979293Sandreas.hansson@arm.com    /**
989293Sandreas.hansson@arm.com     * Create the memory region providing the backing store for a
999293Sandreas.hansson@arm.com     * given address range that corresponds to a set of memories in
1009293Sandreas.hansson@arm.com     * the simulated system.
1019293Sandreas.hansson@arm.com     *
1029293Sandreas.hansson@arm.com     * @param range The address range covered
1039293Sandreas.hansson@arm.com     * @param memories The memories this range maps to
1049293Sandreas.hansson@arm.com     */
1059293Sandreas.hansson@arm.com    void createBackingStore(AddrRange range,
1069293Sandreas.hansson@arm.com                            const std::vector<AbstractMemory*>& _memories);
1079293Sandreas.hansson@arm.com
1082391SN/A  public:
1092391SN/A
1108931Sandreas.hansson@arm.com    /**
1118931Sandreas.hansson@arm.com     * Create a physical memory object, wrapping a number of memories.
1128931Sandreas.hansson@arm.com     */
1139293Sandreas.hansson@arm.com    PhysicalMemory(const std::string& _name,
1149293Sandreas.hansson@arm.com                   const std::vector<AbstractMemory*>& _memories);
1152391SN/A
1168931Sandreas.hansson@arm.com    /**
1179293Sandreas.hansson@arm.com     * Unmap all the backing store we have used.
1188931Sandreas.hansson@arm.com     */
1199293Sandreas.hansson@arm.com    ~PhysicalMemory();
1209293Sandreas.hansson@arm.com
1219293Sandreas.hansson@arm.com    /**
1229293Sandreas.hansson@arm.com     * Return the name for debugging and for creation of sections for
1239293Sandreas.hansson@arm.com     * checkpointing.
1249293Sandreas.hansson@arm.com     */
1259293Sandreas.hansson@arm.com    const std::string name() const { return _name; }
1264762Snate@binkert.org
1278931Sandreas.hansson@arm.com    /**
1288931Sandreas.hansson@arm.com     * Check if a physical address is within a range of a memory that
1298931Sandreas.hansson@arm.com     * is part of the global address map.
1308931Sandreas.hansson@arm.com     *
1318931Sandreas.hansson@arm.com     * @param addr A physical address
1328931Sandreas.hansson@arm.com     * @return Whether the address corresponds to a memory
1338931Sandreas.hansson@arm.com     */
1348931Sandreas.hansson@arm.com    bool isMemAddr(Addr addr) const;
1352391SN/A
1368931Sandreas.hansson@arm.com    /**
1378931Sandreas.hansson@arm.com     * Get the memory ranges for all memories that are to be reported
1389413Sandreas.hansson@arm.com     * to the configuration table. The ranges are merged before they
1399413Sandreas.hansson@arm.com     * are returned such that any interleaved ranges appear as a
1409413Sandreas.hansson@arm.com     * single range.
1418931Sandreas.hansson@arm.com     *
1428931Sandreas.hansson@arm.com     * @return All configuration table memory ranges
1438931Sandreas.hansson@arm.com     */
1448931Sandreas.hansson@arm.com    AddrRangeList getConfAddrRanges() const;
1458923Sandreas.hansson@arm.com
1468931Sandreas.hansson@arm.com    /**
1478931Sandreas.hansson@arm.com     * Get the total physical memory size.
1488931Sandreas.hansson@arm.com     *
1498931Sandreas.hansson@arm.com     * @return The sum of all memory sizes
1508931Sandreas.hansson@arm.com     */
1518931Sandreas.hansson@arm.com    uint64_t totalSize() const { return size; }
1528923Sandreas.hansson@arm.com
1539293Sandreas.hansson@arm.com     /**
1549293Sandreas.hansson@arm.com     * Get the pointers to the backing store for external host
1559293Sandreas.hansson@arm.com     * access. Note that memory in the guest should be accessed using
1569293Sandreas.hansson@arm.com     * access() or functionalAccess(). This interface is primarily
1579293Sandreas.hansson@arm.com     * intended for CPU models using hardware virtualization. Note
1589293Sandreas.hansson@arm.com     * that memories that are null are not present, and that the
1599293Sandreas.hansson@arm.com     * backing store may also contain memories that are not part of
1609293Sandreas.hansson@arm.com     * the OS-visible global address map and thus are allowed to
1619293Sandreas.hansson@arm.com     * overlap.
1629293Sandreas.hansson@arm.com     *
1639293Sandreas.hansson@arm.com     * @return Pointers to the memory backing store
1649293Sandreas.hansson@arm.com     */
16510482Sandreas.hansson@arm.com    std::vector<std::pair<AddrRange, uint8_t*>> getBackingStore() const
1669293Sandreas.hansson@arm.com    { return backingStore; }
1679293Sandreas.hansson@arm.com
1688931Sandreas.hansson@arm.com    /**
1699293Sandreas.hansson@arm.com     * Perform an untimed memory access and update all the state
1709293Sandreas.hansson@arm.com     * (e.g. locked addresses) and statistics accordingly. The packet
1719293Sandreas.hansson@arm.com     * is turned into a response if required.
1728931Sandreas.hansson@arm.com     *
1739293Sandreas.hansson@arm.com     * @param pkt Packet performing the access
1748719SAli.Saidi@ARM.com     */
1758931Sandreas.hansson@arm.com    void access(PacketPtr pkt);
1769293Sandreas.hansson@arm.com
1779293Sandreas.hansson@arm.com    /**
1789293Sandreas.hansson@arm.com     * Perform an untimed memory read or write without changing
1799293Sandreas.hansson@arm.com     * anything but the memory itself. No stats are affected by this
1809293Sandreas.hansson@arm.com     * access. In addition to normal accesses this also facilitates
1819293Sandreas.hansson@arm.com     * print requests.
1829293Sandreas.hansson@arm.com     *
1839293Sandreas.hansson@arm.com     * @param pkt Packet performing the access
1849293Sandreas.hansson@arm.com     */
1858931Sandreas.hansson@arm.com    void functionalAccess(PacketPtr pkt);
1869293Sandreas.hansson@arm.com
1879293Sandreas.hansson@arm.com    /**
1889293Sandreas.hansson@arm.com     * Serialize all the memories in the system. This is independent
1899293Sandreas.hansson@arm.com     * of the logical memory layout, and the serialization only sees
1909293Sandreas.hansson@arm.com     * the contigous backing store, independent of how this maps to
1919293Sandreas.hansson@arm.com     * logical memories in the guest system.
1929293Sandreas.hansson@arm.com     *
1939293Sandreas.hansson@arm.com     * @param os stream to serialize to
1949293Sandreas.hansson@arm.com     */
1959293Sandreas.hansson@arm.com    void serialize(std::ostream& os);
1969293Sandreas.hansson@arm.com
1979293Sandreas.hansson@arm.com    /**
1989293Sandreas.hansson@arm.com     * Serialize a specific store.
1999293Sandreas.hansson@arm.com     *
2009293Sandreas.hansson@arm.com     * @param store_id Unique identifier of this backing store
2019293Sandreas.hansson@arm.com     * @param range The address range of this backing store
2029293Sandreas.hansson@arm.com     * @param pmem The host pointer to this backing store
2039293Sandreas.hansson@arm.com     */
2049293Sandreas.hansson@arm.com    void serializeStore(std::ostream& os, unsigned int store_id,
2059293Sandreas.hansson@arm.com                        AddrRange range, uint8_t* pmem);
2069293Sandreas.hansson@arm.com
2079293Sandreas.hansson@arm.com    /**
2089293Sandreas.hansson@arm.com     * Unserialize the memories in the system. As with the
2099293Sandreas.hansson@arm.com     * serialization, this action is independent of how the address
2109293Sandreas.hansson@arm.com     * ranges are mapped to logical memories in the guest system.
2119293Sandreas.hansson@arm.com     */
2129293Sandreas.hansson@arm.com    void unserialize(Checkpoint* cp, const std::string& section);
2139293Sandreas.hansson@arm.com
2149293Sandreas.hansson@arm.com    /**
2159293Sandreas.hansson@arm.com     * Unserialize a specific backing store, identified by a section.
2169293Sandreas.hansson@arm.com     */
2179293Sandreas.hansson@arm.com    void unserializeStore(Checkpoint* cp, const std::string& section);
2189293Sandreas.hansson@arm.com
2192391SN/A};
2202391SN/A
22110482Sandreas.hansson@arm.com#endif //__MEM_PHYSICAL_HH__
222