abstract_mem.hh revision 12749
112863Sgabeblack@google.com/* 212863Sgabeblack@google.com * Copyright (c) 2012 ARM Limited 312863Sgabeblack@google.com * All rights reserved 412863Sgabeblack@google.com * 512863Sgabeblack@google.com * The license below extends only to copyright in the software and shall 612863Sgabeblack@google.com * not be construed as granting a license to any other intellectual 712863Sgabeblack@google.com * property including but not limited to intellectual property relating 812863Sgabeblack@google.com * to a hardware implementation of the functionality of the software 912863Sgabeblack@google.com * licensed hereunder. You may use the software subject to the license 1012863Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated 1112863Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software, 1212863Sgabeblack@google.com * modified or unmodified, in source code or in binary form. 1312863Sgabeblack@google.com * 1412863Sgabeblack@google.com * Copyright (c) 2001-2005 The Regents of The University of Michigan 1512863Sgabeblack@google.com * All rights reserved. 1612863Sgabeblack@google.com * 1712863Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 1812863Sgabeblack@google.com * modification, are permitted provided that the following conditions are 1912863Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 2012863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 2112863Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 2212863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 2312863Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 2412863Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 2512863Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 2612863Sgabeblack@google.com * this software without specific prior written permission. 2712863Sgabeblack@google.com * 2812863Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2912863Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3012863Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3112863Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3212863Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3312950Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3413046Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3513035Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3613035Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3713035Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3813053Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3912950Sgabeblack@google.com * 4012950Sgabeblack@google.com * Authors: Ron Dreslinski 4112950Sgabeblack@google.com * Andreas Hansson 4212950Sgabeblack@google.com */ 4313053Sgabeblack@google.com 4413053Sgabeblack@google.com/** 4513053Sgabeblack@google.com * @file 4613053Sgabeblack@google.com * AbstractMemory declaration 4713059Sgabeblack@google.com */ 4813053Sgabeblack@google.com 4913053Sgabeblack@google.com#ifndef __MEM_ABSTRACT_MEMORY_HH__ 5013053Sgabeblack@google.com#define __MEM_ABSTRACT_MEMORY_HH__ 5112950Sgabeblack@google.com 5212863Sgabeblack@google.com#include "mem/mem_object.hh" 5312863Sgabeblack@google.com#include "params/AbstractMemory.hh" 5413035Sgabeblack@google.com#include "sim/stats.hh" 5513035Sgabeblack@google.com 5613035Sgabeblack@google.com 5713035Sgabeblack@google.comclass System; 5813035Sgabeblack@google.com 5913035Sgabeblack@google.com/** 6013035Sgabeblack@google.com * Locked address class that represents a physical address and a 6113035Sgabeblack@google.com * context id. 6213035Sgabeblack@google.com */ 6313035Sgabeblack@google.comclass LockedAddr { 6413035Sgabeblack@google.com 6513035Sgabeblack@google.com private: 6613035Sgabeblack@google.com 6713035Sgabeblack@google.com // on alpha, minimum LL/SC granularity is 16 bytes, so lower 6813035Sgabeblack@google.com // bits need to masked off. 6913035Sgabeblack@google.com static const Addr Addr_Mask = 0xf; 7013035Sgabeblack@google.com 7112863Sgabeblack@google.com public: 7212863Sgabeblack@google.com 7312863Sgabeblack@google.com // locked address 7412863Sgabeblack@google.com Addr addr; 7512950Sgabeblack@google.com 7612950Sgabeblack@google.com // locking hw context 7713191Sgabeblack@google.com const ContextID contextId; 7813191Sgabeblack@google.com 7912863Sgabeblack@google.com static Addr mask(Addr paddr) { return (paddr & ~Addr_Mask); } 8013035Sgabeblack@google.com 8113035Sgabeblack@google.com // check for matching execution context 8212863Sgabeblack@google.com bool matchesContext(const RequestPtr &req) const 8312863Sgabeblack@google.com { 8412950Sgabeblack@google.com return (contextId == req->contextId()); 8512982Sgabeblack@google.com } 8612982Sgabeblack@google.com 8712950Sgabeblack@google.com LockedAddr(const RequestPtr &req) : addr(mask(req->getPaddr())), 8812863Sgabeblack@google.com contextId(req->contextId()) 8912950Sgabeblack@google.com {} 9013191Sgabeblack@google.com 9113191Sgabeblack@google.com // constructor for unserialization use 9212950Sgabeblack@google.com LockedAddr(Addr _addr, int _cid) : addr(_addr), contextId(_cid) 9312950Sgabeblack@google.com {} 9412950Sgabeblack@google.com}; 9512950Sgabeblack@google.com 9612950Sgabeblack@google.com/** 9712950Sgabeblack@google.com * An abstract memory represents a contiguous block of physical 9812950Sgabeblack@google.com * memory, with an associated address range, and also provides basic 9912950Sgabeblack@google.com * functionality for reading and writing this memory without any 10012950Sgabeblack@google.com * timing information. It is a MemObject since any subclass must have 10112950Sgabeblack@google.com * at least one slave port. 10212950Sgabeblack@google.com */ 10312950Sgabeblack@google.comclass AbstractMemory : public MemObject 10412950Sgabeblack@google.com{ 10512950Sgabeblack@google.com protected: 10612950Sgabeblack@google.com 10712950Sgabeblack@google.com // Address range of this memory 10812950Sgabeblack@google.com AddrRange range; 10912950Sgabeblack@google.com 11012950Sgabeblack@google.com // Pointer to host memory used to implement this memory 11112950Sgabeblack@google.com uint8_t* pmemAddr; 11212950Sgabeblack@google.com 11312950Sgabeblack@google.com // Enable specific memories to be reported to the configuration table 11412863Sgabeblack@google.com const bool confTableReported; 11513035Sgabeblack@google.com 11613035Sgabeblack@google.com // Should the memory appear in the global address map 11713053Sgabeblack@google.com const bool inAddrMap; 11813091Sgabeblack@google.com 11913091Sgabeblack@google.com // Should KVM map this memory for the guest 12013053Sgabeblack@google.com const bool kvmMap; 12113059Sgabeblack@google.com 12213191Sgabeblack@google.com std::list<LockedAddr> lockedAddrList; 12313191Sgabeblack@google.com 12413191Sgabeblack@google.com // helper function for checkLockedAddrs(): we really want to 12513191Sgabeblack@google.com // inline a quick check for an empty locked addr list (hopefully 12613191Sgabeblack@google.com // the common case), and do the full list search (if necessary) in 12712863Sgabeblack@google.com // this out-of-line function 12812863Sgabeblack@google.com bool checkLockedAddrList(PacketPtr pkt); 12912950Sgabeblack@google.com 13013079Sgabeblack@google.com // Record the address of a load-locked operation so that we can 13112950Sgabeblack@google.com // clear the execution context's lock flag if a matching store is 13212863Sgabeblack@google.com // performed 13313045Sgabeblack@google.com void trackLoadLocked(PacketPtr pkt); 13413045Sgabeblack@google.com 13513045Sgabeblack@google.com // Compare a store address with any locked addresses so we can 13613046Sgabeblack@google.com // clear the lock flag appropriately. Return value set to 'false' 13712982Sgabeblack@google.com // if store operation should be suppressed (because it was a 13812863Sgabeblack@google.com // conditional store and the address was no longer locked by the 13912863Sgabeblack@google.com // requesting execution context), 'true' otherwise. Note that 14012863Sgabeblack@google.com // this method must be called on *all* stores since even 141 // non-conditional stores must clear any matching lock addresses. 142 bool writeOK(PacketPtr pkt) { 143 const RequestPtr &req = pkt->req; 144 if (lockedAddrList.empty()) { 145 // no locked addrs: nothing to check, store_conditional fails 146 bool isLLSC = pkt->isLLSC(); 147 if (isLLSC) { 148 req->setExtraData(0); 149 } 150 return !isLLSC; // only do write if not an sc 151 } else { 152 // iterate over list... 153 return checkLockedAddrList(pkt); 154 } 155 } 156 157 /** Number of total bytes read from this memory */ 158 Stats::Vector bytesRead; 159 /** Number of instruction bytes read from this memory */ 160 Stats::Vector bytesInstRead; 161 /** Number of bytes written to this memory */ 162 Stats::Vector bytesWritten; 163 /** Number of read requests */ 164 Stats::Vector numReads; 165 /** Number of write requests */ 166 Stats::Vector numWrites; 167 /** Number of other requests */ 168 Stats::Vector numOther; 169 /** Read bandwidth from this memory */ 170 Stats::Formula bwRead; 171 /** Read bandwidth from this memory */ 172 Stats::Formula bwInstRead; 173 /** Write bandwidth from this memory */ 174 Stats::Formula bwWrite; 175 /** Total bandwidth from this memory */ 176 Stats::Formula bwTotal; 177 178 /** Pointor to the System object. 179 * This is used for getting the number of masters in the system which is 180 * needed when registering stats 181 */ 182 System *_system; 183 184 185 private: 186 187 // Prevent copying 188 AbstractMemory(const AbstractMemory&); 189 190 // Prevent assignment 191 AbstractMemory& operator=(const AbstractMemory&); 192 193 public: 194 195 typedef AbstractMemoryParams Params; 196 197 AbstractMemory(const Params* p); 198 virtual ~AbstractMemory() {} 199 200 /** 201 * Initialise this memory. 202 */ 203 void init() override; 204 205 /** 206 * See if this is a null memory that should never store data and 207 * always return zero. 208 * 209 * @return true if null 210 */ 211 bool isNull() const { return params()->null; } 212 213 /** 214 * Set the host memory backing store to be used by this memory 215 * controller. 216 * 217 * @param pmem_addr Pointer to a segment of host memory 218 */ 219 void setBackingStore(uint8_t* pmem_addr); 220 221 /** 222 * Get the list of locked addresses to allow checkpointing. 223 */ 224 const std::list<LockedAddr>& getLockedAddrList() const 225 { return lockedAddrList; } 226 227 /** 228 * Add a locked address to allow for checkpointing. 229 */ 230 void addLockedAddr(LockedAddr addr) { lockedAddrList.push_back(addr); } 231 232 /** read the system pointer 233 * Implemented for completeness with the setter 234 * @return pointer to the system object */ 235 System* system() const { return _system; } 236 237 /** Set the system pointer on this memory 238 * This can't be done via a python parameter because the system needs 239 * pointers to all the memories and the reverse would create a cycle in the 240 * object graph. An init() this is set. 241 * @param sys system pointer to set 242 */ 243 void system(System *sys) { _system = sys; } 244 245 const Params * 246 params() const 247 { 248 return dynamic_cast<const Params *>(_params); 249 } 250 251 /** 252 * Get the address range 253 * 254 * @return a single contigous address range 255 */ 256 AddrRange getAddrRange() const; 257 258 /** 259 * Get the memory size. 260 * 261 * @return the size of the memory 262 */ 263 uint64_t size() const { return range.size(); } 264 265 /** 266 * Get the start address. 267 * 268 * @return the start address of the memory 269 */ 270 Addr start() const { return range.start(); } 271 272 /** 273 * Should this memory be passed to the kernel and part of the OS 274 * physical memory layout. 275 * 276 * @return if this memory is reported 277 */ 278 bool isConfReported() const { return confTableReported; } 279 280 /** 281 * Some memories are used as shadow memories or should for other 282 * reasons not be part of the global address map. 283 * 284 * @return if this memory is part of the address map 285 */ 286 bool isInAddrMap() const { return inAddrMap; } 287 288 /** 289 * When shadow memories are in use, KVM may want to make one or the other, 290 * but cannot map both into the guest address space. 291 * 292 * @return if this memory should be mapped into the KVM guest address space 293 */ 294 bool isKvmMap() const { return kvmMap; } 295 296 /** 297 * Perform an untimed memory access and update all the state 298 * (e.g. locked addresses) and statistics accordingly. The packet 299 * is turned into a response if required. 300 * 301 * @param pkt Packet performing the access 302 */ 303 void access(PacketPtr pkt); 304 305 /** 306 * Perform an untimed memory read or write without changing 307 * anything but the memory itself. No stats are affected by this 308 * access. In addition to normal accesses this also facilitates 309 * print requests. 310 * 311 * @param pkt Packet performing the access 312 */ 313 void functionalAccess(PacketPtr pkt); 314 315 /** 316 * Register Statistics 317 */ 318 void regStats() override; 319 320}; 321 322#endif //__MEM_ABSTRACT_MEMORY_HH__ 323