physical.hh revision 11614:29606f000389
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Hansson
38 */
39
40#ifndef __MEM_PHYSICAL_HH__
41#define __MEM_PHYSICAL_HH__
42
43#include "base/addr_range_map.hh"
44#include "mem/packet.hh"
45
46/**
47 * Forward declaration to avoid header dependencies.
48 */
49class AbstractMemory;
50
51/**
52 * A single entry for the backing store.
53 */
54class BackingStoreEntry
55{
56  public:
57
58    /**
59     * Create a backing store entry. Don't worry about managing the memory
60     * pointers, because PhysicalMemory is responsible for that.
61     */
62    BackingStoreEntry(AddrRange range, uint8_t* pmem,
63                      bool conf_table_reported, bool in_addr_map, bool kvm_map)
64        : range(range), pmem(pmem), confTableReported(conf_table_reported),
65          inAddrMap(in_addr_map), kvmMap(kvm_map)
66        {}
67
68    /**
69     * The address range covered in the guest.
70     */
71     AddrRange range;
72
73    /**
74     * Pointer to the host memory this range maps to. This memory is the same
75     * size as the range field.
76     */
77     uint8_t* pmem;
78
79     /**
80      * Whether this memory should be reported to the configuration table
81      */
82     bool confTableReported;
83
84     /**
85      * Whether this memory should appear in the global address map
86      */
87     bool inAddrMap;
88
89     /**
90      * Whether KVM should map this memory into the guest address space during
91      * acceleration.
92      */
93     bool kvmMap;
94};
95
96/**
97 * The physical memory encapsulates all memories in the system and
98 * provides basic functionality for accessing those memories without
99 * going through the memory system and interconnect.
100 *
101 * The physical memory is also responsible for providing the host
102 * system backingstore used by the memories in the simulated guest
103 * system. When the system is created, the physical memory allocates
104 * the backing store based on the address ranges that are populated in
105 * the system, and does so independent of how those map to actual
106 * memory controllers. Thus, the physical memory completely abstracts
107 * the mapping of the backing store of the host system and the address
108 * mapping in the guest system. This enables us to arbitrarily change
109 * the number of memory controllers, and their address mapping, as
110 * long as the ranges stay the same.
111 */
112class PhysicalMemory : public Serializable
113{
114
115  private:
116
117    // Name for debugging
118    std::string _name;
119
120    // Global address map
121    AddrRangeMap<AbstractMemory*> addrMap;
122
123    // a mutable cache for the last address map iterator that matched
124    // an address
125    mutable AddrRangeMap<AbstractMemory*>::const_iterator rangeCache;
126
127    // All address-mapped memories
128    std::vector<AbstractMemory*> memories;
129
130    // The total memory size
131    uint64_t size;
132
133    // Let the user choose if we reserve swap space when calling mmap
134    const bool mmapUsingNoReserve;
135
136    // The physical memory used to provide the memory in the simulated
137    // system
138    std::vector<BackingStoreEntry> backingStore;
139
140    // Prevent copying
141    PhysicalMemory(const PhysicalMemory&);
142
143    // Prevent assignment
144    PhysicalMemory& operator=(const PhysicalMemory&);
145
146    /**
147     * Create the memory region providing the backing store for a
148     * given address range that corresponds to a set of memories in
149     * the simulated system.
150     *
151     * @param range The address range covered
152     * @param memories The memories this range maps to
153     * @param kvm_map Should KVM map this memory for the guest
154     */
155    void createBackingStore(AddrRange range,
156                            const std::vector<AbstractMemory*>& _memories,
157                            bool conf_table_reported,
158                            bool in_addr_map, bool kvm_map);
159
160  public:
161
162    /**
163     * Create a physical memory object, wrapping a number of memories.
164     */
165    PhysicalMemory(const std::string& _name,
166                   const std::vector<AbstractMemory*>& _memories,
167                   bool mmap_using_noreserve);
168
169    /**
170     * Unmap all the backing store we have used.
171     */
172    ~PhysicalMemory();
173
174    /**
175     * Return the name for debugging and for creation of sections for
176     * checkpointing.
177     */
178    const std::string name() const { return _name; }
179
180    /**
181     * Check if a physical address is within a range of a memory that
182     * is part of the global address map.
183     *
184     * @param addr A physical address
185     * @return Whether the address corresponds to a memory
186     */
187    bool isMemAddr(Addr addr) const;
188
189    /**
190     * Get the memory ranges for all memories that are to be reported
191     * to the configuration table. The ranges are merged before they
192     * are returned such that any interleaved ranges appear as a
193     * single range.
194     *
195     * @return All configuration table memory ranges
196     */
197    AddrRangeList getConfAddrRanges() const;
198
199    /**
200     * Get the total physical memory size.
201     *
202     * @return The sum of all memory sizes
203     */
204    uint64_t totalSize() const { return size; }
205
206     /**
207     * Get the pointers to the backing store for external host
208     * access. Note that memory in the guest should be accessed using
209     * access() or functionalAccess(). This interface is primarily
210     * intended for CPU models using hardware virtualization. Note
211     * that memories that are null are not present, and that the
212     * backing store may also contain memories that are not part of
213     * the OS-visible global address map and thus are allowed to
214     * overlap.
215     *
216     * @return Pointers to the memory backing store
217     */
218    std::vector<BackingStoreEntry> getBackingStore() const
219    { return backingStore; }
220
221    /**
222     * Perform an untimed memory access and update all the state
223     * (e.g. locked addresses) and statistics accordingly. The packet
224     * is turned into a response if required.
225     *
226     * @param pkt Packet performing the access
227     */
228    void access(PacketPtr pkt);
229
230    /**
231     * Perform an untimed memory read or write without changing
232     * anything but the memory itself. No stats are affected by this
233     * access. In addition to normal accesses this also facilitates
234     * print requests.
235     *
236     * @param pkt Packet performing the access
237     */
238    void functionalAccess(PacketPtr pkt);
239
240    /**
241     * Serialize all the memories in the system. This is independent
242     * of the logical memory layout, and the serialization only sees
243     * the contigous backing store, independent of how this maps to
244     * logical memories in the guest system.
245     *
246     * @param os stream to serialize to
247     */
248    void serialize(CheckpointOut &cp) const override;
249
250    /**
251     * Serialize a specific store.
252     *
253     * @param store_id Unique identifier of this backing store
254     * @param range The address range of this backing store
255     * @param pmem The host pointer to this backing store
256     */
257    void serializeStore(CheckpointOut &cp, unsigned int store_id,
258                        AddrRange range, uint8_t* pmem) const;
259
260    /**
261     * Unserialize the memories in the system. As with the
262     * serialization, this action is independent of how the address
263     * ranges are mapped to logical memories in the guest system.
264     */
265    void unserialize(CheckpointIn &cp) override;
266
267    /**
268     * Unserialize a specific backing store, identified by a section.
269     */
270    void unserializeStore(CheckpointIn &cp);
271
272};
273
274#endif //__MEM_PHYSICAL_HH__
275