physical.cc revision 6712
12391SN/A/* 22391SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 32391SN/A * All rights reserved. 42391SN/A * 52391SN/A * Redistribution and use in source and binary forms, with or without 62391SN/A * modification, are permitted provided that the following conditions are 72391SN/A * met: redistributions of source code must retain the above copyright 82391SN/A * notice, this list of conditions and the following disclaimer; 92391SN/A * redistributions in binary form must reproduce the above copyright 102391SN/A * notice, this list of conditions and the following disclaimer in the 112391SN/A * documentation and/or other materials provided with the distribution; 122391SN/A * neither the name of the copyright holders nor the names of its 132391SN/A * contributors may be used to endorse or promote products derived from 142391SN/A * this software without specific prior written permission. 152391SN/A * 162391SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172391SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182391SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192391SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202391SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212391SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222391SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232391SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242391SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252391SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262391SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Ron Dreslinski 292914Ssaidi@eecs.umich.edu * Ali Saidi 302391SN/A */ 312391SN/A 322391SN/A#include <sys/types.h> 332391SN/A#include <sys/mman.h> 342391SN/A#include <errno.h> 352391SN/A#include <fcntl.h> 362391SN/A#include <unistd.h> 372391SN/A#include <zlib.h> 382391SN/A 396712Snate@binkert.org#include <cstdio> 402391SN/A#include <iostream> 412391SN/A#include <string> 422391SN/A 436329Sgblack@eecs.umich.edu#include "arch/registers.hh" 442391SN/A#include "base/misc.hh" 455399Ssaidi@eecs.umich.edu#include "base/random.hh" 466216Snate@binkert.org#include "base/types.hh" 472391SN/A#include "config/full_system.hh" 486658Snate@binkert.org#include "config/the_isa.hh" 493879Ssaidi@eecs.umich.edu#include "mem/packet_access.hh" 502394SN/A#include "mem/physical.hh" 512415SN/A#include "sim/eventq.hh" 522394SN/A 532391SN/Ausing namespace std; 542423SN/Ausing namespace TheISA; 552391SN/A 564762Snate@binkert.orgPhysicalMemory::PhysicalMemory(const Params *p) 575477Snate@binkert.org : MemObject(p), pmemAddr(NULL), pagePtr(0), 585477Snate@binkert.org lat(p->latency), lat_var(p->latency_var), 595477Snate@binkert.org cachedSize(params()->range.size()), cachedStart(params()->range.start) 602391SN/A{ 614762Snate@binkert.org if (params()->range.size() % TheISA::PageBytes != 0) 622391SN/A panic("Memory Size not divisible by page size\n"); 632391SN/A 645477Snate@binkert.org if (params()->null) 655477Snate@binkert.org return; 665477Snate@binkert.org 672391SN/A int map_flags = MAP_ANON | MAP_PRIVATE; 684918Snate@binkert.org pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 694918Snate@binkert.org PROT_READ | PROT_WRITE, map_flags, -1, 0); 702391SN/A 713012Ssaidi@eecs.umich.edu if (pmemAddr == (void *)MAP_FAILED) { 722391SN/A perror("mmap"); 732391SN/A fatal("Could not mmap!\n"); 742391SN/A } 752391SN/A 763751Sgblack@eecs.umich.edu //If requested, initialize all the memory to 0 774762Snate@binkert.org if (p->zero) 784762Snate@binkert.org memset(pmemAddr, 0, p->range.size()); 792391SN/A} 802391SN/A 812541SN/Avoid 822541SN/APhysicalMemory::init() 832541SN/A{ 844470Sstever@eecs.umich.edu if (ports.size() == 0) { 854470Sstever@eecs.umich.edu fatal("PhysicalMemory object %s is unconnected!", name()); 864470Sstever@eecs.umich.edu } 874470Sstever@eecs.umich.edu 884467Sstever@eecs.umich.edu for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 894467Sstever@eecs.umich.edu if (*pi) 904467Sstever@eecs.umich.edu (*pi)->sendStatusChange(Port::RangeChange); 914467Sstever@eecs.umich.edu } 922541SN/A} 932541SN/A 942391SN/APhysicalMemory::~PhysicalMemory() 952391SN/A{ 963012Ssaidi@eecs.umich.edu if (pmemAddr) 974762Snate@binkert.org munmap((char*)pmemAddr, params()->range.size()); 982416SN/A //Remove memPorts? 992391SN/A} 1002391SN/A 1012391SN/AAddr 1022391SN/APhysicalMemory::new_page() 1032391SN/A{ 1043012Ssaidi@eecs.umich.edu Addr return_addr = pagePtr << LogVMPageSize; 1054040Ssaidi@eecs.umich.edu return_addr += start(); 1062391SN/A 1073012Ssaidi@eecs.umich.edu ++pagePtr; 1082391SN/A return return_addr; 1092391SN/A} 1102391SN/A 1116227Snate@binkert.orgunsigned 1126227Snate@binkert.orgPhysicalMemory::deviceBlockSize() const 1132408SN/A{ 1142409SN/A //Can accept anysize request 1152409SN/A return 0; 1162408SN/A} 1172408SN/A 1183012Ssaidi@eecs.umich.eduTick 1193349Sbinkertn@umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt) 1203012Ssaidi@eecs.umich.edu{ 1215399Ssaidi@eecs.umich.edu Tick latency = lat; 1225399Ssaidi@eecs.umich.edu if (lat_var != 0) 1235399Ssaidi@eecs.umich.edu latency += random_mt.random<Tick>(0, lat_var); 1245399Ssaidi@eecs.umich.edu return latency; 1253012Ssaidi@eecs.umich.edu} 1262413SN/A 1273170Sstever@eecs.umich.edu 1283170Sstever@eecs.umich.edu 1293170Sstever@eecs.umich.edu// Add load-locked to tracking list. Should only be called if the 1306076Sgblack@eecs.umich.edu// operation is a load and the LLSC flag is set. 1313170Sstever@eecs.umich.eduvoid 1324626Sstever@eecs.umich.eduPhysicalMemory::trackLoadLocked(PacketPtr pkt) 1333170Sstever@eecs.umich.edu{ 1344626Sstever@eecs.umich.edu Request *req = pkt->req; 1353170Sstever@eecs.umich.edu Addr paddr = LockedAddr::mask(req->getPaddr()); 1363170Sstever@eecs.umich.edu 1373170Sstever@eecs.umich.edu // first we check if we already have a locked addr for this 1383170Sstever@eecs.umich.edu // xc. Since each xc only gets one, we just update the 1393170Sstever@eecs.umich.edu // existing record with the new address. 1403170Sstever@eecs.umich.edu list<LockedAddr>::iterator i; 1413170Sstever@eecs.umich.edu 1423170Sstever@eecs.umich.edu for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) { 1433170Sstever@eecs.umich.edu if (i->matchesContext(req)) { 1445714Shsul@eecs.umich.edu DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n", 1455714Shsul@eecs.umich.edu req->contextId(), paddr); 1463170Sstever@eecs.umich.edu i->addr = paddr; 1473170Sstever@eecs.umich.edu return; 1483170Sstever@eecs.umich.edu } 1493170Sstever@eecs.umich.edu } 1503170Sstever@eecs.umich.edu 1513170Sstever@eecs.umich.edu // no record for this xc: need to allocate a new one 1525714Shsul@eecs.umich.edu DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n", 1535714Shsul@eecs.umich.edu req->contextId(), paddr); 1543170Sstever@eecs.umich.edu lockedAddrList.push_front(LockedAddr(req)); 1553170Sstever@eecs.umich.edu} 1563170Sstever@eecs.umich.edu 1573170Sstever@eecs.umich.edu 1583170Sstever@eecs.umich.edu// Called on *writes* only... both regular stores and 1593170Sstever@eecs.umich.edu// store-conditional operations. Check for conventional stores which 1603170Sstever@eecs.umich.edu// conflict with locked addresses, and for success/failure of store 1613170Sstever@eecs.umich.edu// conditionals. 1623170Sstever@eecs.umich.edubool 1634626Sstever@eecs.umich.eduPhysicalMemory::checkLockedAddrList(PacketPtr pkt) 1643170Sstever@eecs.umich.edu{ 1654626Sstever@eecs.umich.edu Request *req = pkt->req; 1663170Sstever@eecs.umich.edu Addr paddr = LockedAddr::mask(req->getPaddr()); 1676102Sgblack@eecs.umich.edu bool isLLSC = pkt->isLLSC(); 1683170Sstever@eecs.umich.edu 1693170Sstever@eecs.umich.edu // Initialize return value. Non-conditional stores always 1703170Sstever@eecs.umich.edu // succeed. Assume conditional stores will fail until proven 1713170Sstever@eecs.umich.edu // otherwise. 1726102Sgblack@eecs.umich.edu bool success = !isLLSC; 1733170Sstever@eecs.umich.edu 1743170Sstever@eecs.umich.edu // Iterate over list. Note that there could be multiple matching 1753170Sstever@eecs.umich.edu // records, as more than one context could have done a load locked 1763170Sstever@eecs.umich.edu // to this location. 1773170Sstever@eecs.umich.edu list<LockedAddr>::iterator i = lockedAddrList.begin(); 1783170Sstever@eecs.umich.edu 1793170Sstever@eecs.umich.edu while (i != lockedAddrList.end()) { 1803170Sstever@eecs.umich.edu 1813170Sstever@eecs.umich.edu if (i->addr == paddr) { 1823170Sstever@eecs.umich.edu // we have a matching address 1833170Sstever@eecs.umich.edu 1846102Sgblack@eecs.umich.edu if (isLLSC && i->matchesContext(req)) { 1853170Sstever@eecs.umich.edu // it's a store conditional, and as far as the memory 1863170Sstever@eecs.umich.edu // system can tell, the requesting context's lock is 1873170Sstever@eecs.umich.edu // still valid. 1885714Shsul@eecs.umich.edu DPRINTF(LLSC, "StCond success: context %d addr %#x\n", 1895714Shsul@eecs.umich.edu req->contextId(), paddr); 1903170Sstever@eecs.umich.edu success = true; 1913170Sstever@eecs.umich.edu } 1923170Sstever@eecs.umich.edu 1933170Sstever@eecs.umich.edu // Get rid of our record of this lock and advance to next 1945714Shsul@eecs.umich.edu DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n", 1955714Shsul@eecs.umich.edu i->contextId, paddr); 1963170Sstever@eecs.umich.edu i = lockedAddrList.erase(i); 1973170Sstever@eecs.umich.edu } 1983170Sstever@eecs.umich.edu else { 1993170Sstever@eecs.umich.edu // no match: advance to next record 2003170Sstever@eecs.umich.edu ++i; 2013170Sstever@eecs.umich.edu } 2023170Sstever@eecs.umich.edu } 2033170Sstever@eecs.umich.edu 2046102Sgblack@eecs.umich.edu if (isLLSC) { 2054040Ssaidi@eecs.umich.edu req->setExtraData(success ? 1 : 0); 2063170Sstever@eecs.umich.edu } 2073170Sstever@eecs.umich.edu 2083170Sstever@eecs.umich.edu return success; 2093170Sstever@eecs.umich.edu} 2103170Sstever@eecs.umich.edu 2114626Sstever@eecs.umich.edu 2124626Sstever@eecs.umich.edu#if TRACING_ON 2134626Sstever@eecs.umich.edu 2144626Sstever@eecs.umich.edu#define CASE(A, T) \ 2154626Sstever@eecs.umich.edu case sizeof(T): \ 2166429Ssteve.reinhardt@amd.com DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \ 2176429Ssteve.reinhardt@amd.com A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \ 2184626Sstever@eecs.umich.edu break 2194626Sstever@eecs.umich.edu 2204626Sstever@eecs.umich.edu 2214626Sstever@eecs.umich.edu#define TRACE_PACKET(A) \ 2224626Sstever@eecs.umich.edu do { \ 2234626Sstever@eecs.umich.edu switch (pkt->getSize()) { \ 2244626Sstever@eecs.umich.edu CASE(A, uint64_t); \ 2254626Sstever@eecs.umich.edu CASE(A, uint32_t); \ 2264626Sstever@eecs.umich.edu CASE(A, uint16_t); \ 2274626Sstever@eecs.umich.edu CASE(A, uint8_t); \ 2284626Sstever@eecs.umich.edu default: \ 2296429Ssteve.reinhardt@amd.com DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \ 2306429Ssteve.reinhardt@amd.com A, pkt->getSize(), pkt->getAddr()); \ 2314626Sstever@eecs.umich.edu } \ 2324626Sstever@eecs.umich.edu } while (0) 2334626Sstever@eecs.umich.edu 2344626Sstever@eecs.umich.edu#else 2354626Sstever@eecs.umich.edu 2364626Sstever@eecs.umich.edu#define TRACE_PACKET(A) 2374626Sstever@eecs.umich.edu 2384626Sstever@eecs.umich.edu#endif 2394626Sstever@eecs.umich.edu 2404626Sstever@eecs.umich.eduTick 2414626Sstever@eecs.umich.eduPhysicalMemory::doAtomicAccess(PacketPtr pkt) 2422413SN/A{ 2434040Ssaidi@eecs.umich.edu assert(pkt->getAddr() >= start() && 2444040Ssaidi@eecs.umich.edu pkt->getAddr() + pkt->getSize() <= start() + size()); 2452414SN/A 2464626Sstever@eecs.umich.edu if (pkt->memInhibitAsserted()) { 2474626Sstever@eecs.umich.edu DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n", 2484626Sstever@eecs.umich.edu pkt->getAddr()); 2494626Sstever@eecs.umich.edu return 0; 2503175Srdreslin@umich.edu } 2514626Sstever@eecs.umich.edu 2524626Sstever@eecs.umich.edu uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 2534626Sstever@eecs.umich.edu 2544626Sstever@eecs.umich.edu if (pkt->cmd == MemCmd::SwapReq) { 2554040Ssaidi@eecs.umich.edu IntReg overwrite_val; 2564040Ssaidi@eecs.umich.edu bool overwrite_mem; 2574040Ssaidi@eecs.umich.edu uint64_t condition_val64; 2584040Ssaidi@eecs.umich.edu uint32_t condition_val32; 2594040Ssaidi@eecs.umich.edu 2605477Snate@binkert.org if (!pmemAddr) 2615477Snate@binkert.org panic("Swap only works if there is real memory (i.e. null=False)"); 2624040Ssaidi@eecs.umich.edu assert(sizeof(IntReg) >= pkt->getSize()); 2634040Ssaidi@eecs.umich.edu 2644040Ssaidi@eecs.umich.edu overwrite_mem = true; 2654040Ssaidi@eecs.umich.edu // keep a copy of our possible write value, and copy what is at the 2664040Ssaidi@eecs.umich.edu // memory address into the packet 2674052Ssaidi@eecs.umich.edu std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize()); 2684626Sstever@eecs.umich.edu std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 2694040Ssaidi@eecs.umich.edu 2704040Ssaidi@eecs.umich.edu if (pkt->req->isCondSwap()) { 2714040Ssaidi@eecs.umich.edu if (pkt->getSize() == sizeof(uint64_t)) { 2724052Ssaidi@eecs.umich.edu condition_val64 = pkt->req->getExtraData(); 2734626Sstever@eecs.umich.edu overwrite_mem = !std::memcmp(&condition_val64, hostAddr, 2744626Sstever@eecs.umich.edu sizeof(uint64_t)); 2754040Ssaidi@eecs.umich.edu } else if (pkt->getSize() == sizeof(uint32_t)) { 2764052Ssaidi@eecs.umich.edu condition_val32 = (uint32_t)pkt->req->getExtraData(); 2774626Sstever@eecs.umich.edu overwrite_mem = !std::memcmp(&condition_val32, hostAddr, 2784626Sstever@eecs.umich.edu sizeof(uint32_t)); 2794040Ssaidi@eecs.umich.edu } else 2804040Ssaidi@eecs.umich.edu panic("Invalid size for conditional read/write\n"); 2814040Ssaidi@eecs.umich.edu } 2824040Ssaidi@eecs.umich.edu 2834040Ssaidi@eecs.umich.edu if (overwrite_mem) 2844626Sstever@eecs.umich.edu std::memcpy(hostAddr, &overwrite_val, pkt->getSize()); 2854040Ssaidi@eecs.umich.edu 2866429Ssteve.reinhardt@amd.com assert(!pkt->req->isInstFetch()); 2874626Sstever@eecs.umich.edu TRACE_PACKET("Read/Write"); 2884626Sstever@eecs.umich.edu } else if (pkt->isRead()) { 2894626Sstever@eecs.umich.edu assert(!pkt->isWrite()); 2906102Sgblack@eecs.umich.edu if (pkt->isLLSC()) { 2914626Sstever@eecs.umich.edu trackLoadLocked(pkt); 2924040Ssaidi@eecs.umich.edu } 2935477Snate@binkert.org if (pmemAddr) 2945477Snate@binkert.org memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 2956429Ssteve.reinhardt@amd.com TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read"); 2964626Sstever@eecs.umich.edu } else if (pkt->isWrite()) { 2974626Sstever@eecs.umich.edu if (writeOK(pkt)) { 2985477Snate@binkert.org if (pmemAddr) 2995477Snate@binkert.org memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 3006429Ssteve.reinhardt@amd.com assert(!pkt->req->isInstFetch()); 3014626Sstever@eecs.umich.edu TRACE_PACKET("Write"); 3024626Sstever@eecs.umich.edu } 3034626Sstever@eecs.umich.edu } else if (pkt->isInvalidate()) { 3044626Sstever@eecs.umich.edu //upgrade or invalidate 3054626Sstever@eecs.umich.edu if (pkt->needsResponse()) { 3064626Sstever@eecs.umich.edu pkt->makeAtomicResponse(); 3074626Sstever@eecs.umich.edu } 3084040Ssaidi@eecs.umich.edu } else { 3092413SN/A panic("unimplemented"); 3102413SN/A } 3112420SN/A 3124626Sstever@eecs.umich.edu if (pkt->needsResponse()) { 3134626Sstever@eecs.umich.edu pkt->makeAtomicResponse(); 3144626Sstever@eecs.umich.edu } 3154626Sstever@eecs.umich.edu return calculateLatency(pkt); 3162413SN/A} 3172413SN/A 3184626Sstever@eecs.umich.edu 3194626Sstever@eecs.umich.eduvoid 3204626Sstever@eecs.umich.eduPhysicalMemory::doFunctionalAccess(PacketPtr pkt) 3214626Sstever@eecs.umich.edu{ 3224626Sstever@eecs.umich.edu assert(pkt->getAddr() >= start() && 3234626Sstever@eecs.umich.edu pkt->getAddr() + pkt->getSize() <= start() + size()); 3244626Sstever@eecs.umich.edu 3255222Sksewell@umich.edu 3264626Sstever@eecs.umich.edu uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 3274626Sstever@eecs.umich.edu 3285314Sstever@gmail.com if (pkt->isRead()) { 3295477Snate@binkert.org if (pmemAddr) 3305477Snate@binkert.org memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 3314626Sstever@eecs.umich.edu TRACE_PACKET("Read"); 3325314Sstever@gmail.com pkt->makeAtomicResponse(); 3335314Sstever@gmail.com } else if (pkt->isWrite()) { 3345477Snate@binkert.org if (pmemAddr) 3355477Snate@binkert.org memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 3364626Sstever@eecs.umich.edu TRACE_PACKET("Write"); 3375314Sstever@gmail.com pkt->makeAtomicResponse(); 3385314Sstever@gmail.com } else if (pkt->isPrint()) { 3395315Sstever@gmail.com Packet::PrintReqState *prs = 3405315Sstever@gmail.com dynamic_cast<Packet::PrintReqState*>(pkt->senderState); 3415315Sstever@gmail.com // Need to call printLabels() explicitly since we're not going 3425315Sstever@gmail.com // through printObj(). 3435314Sstever@gmail.com prs->printLabels(); 3445315Sstever@gmail.com // Right now we just print the single byte at the specified address. 3455314Sstever@gmail.com ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr); 3464626Sstever@eecs.umich.edu } else { 3474626Sstever@eecs.umich.edu panic("PhysicalMemory: unimplemented functional command %s", 3484626Sstever@eecs.umich.edu pkt->cmdString()); 3494626Sstever@eecs.umich.edu } 3504490Sstever@eecs.umich.edu} 3514490Sstever@eecs.umich.edu 3524626Sstever@eecs.umich.edu 3532413SN/APort * 3542738Sstever@eecs.umich.eduPhysicalMemory::getPort(const std::string &if_name, int idx) 3552413SN/A{ 3564468Sstever@eecs.umich.edu // Accept request for "functional" port for backwards compatibility 3574468Sstever@eecs.umich.edu // with places where this function is called from C++. I'd prefer 3584468Sstever@eecs.umich.edu // to move all these into Python someday. 3594468Sstever@eecs.umich.edu if (if_name == "functional") { 3604468Sstever@eecs.umich.edu return new MemoryPort(csprintf("%s-functional", name()), this); 3614468Sstever@eecs.umich.edu } 3624468Sstever@eecs.umich.edu 3634467Sstever@eecs.umich.edu if (if_name != "port") { 3642462SN/A panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 3652462SN/A } 3664467Sstever@eecs.umich.edu 3676227Snate@binkert.org if (idx >= (int)ports.size()) { 3686227Snate@binkert.org ports.resize(idx + 1); 3694467Sstever@eecs.umich.edu } 3704467Sstever@eecs.umich.edu 3714467Sstever@eecs.umich.edu if (ports[idx] != NULL) { 3724467Sstever@eecs.umich.edu panic("PhysicalMemory::getPort: port %d already assigned", idx); 3734467Sstever@eecs.umich.edu } 3744467Sstever@eecs.umich.edu 3754467Sstever@eecs.umich.edu MemoryPort *port = 3764467Sstever@eecs.umich.edu new MemoryPort(csprintf("%s-port%d", name(), idx), this); 3774467Sstever@eecs.umich.edu 3784467Sstever@eecs.umich.edu ports[idx] = port; 3794467Sstever@eecs.umich.edu return port; 3802413SN/A} 3812413SN/A 3824467Sstever@eecs.umich.edu 3832413SN/Avoid 3842413SN/APhysicalMemory::recvStatusChange(Port::Status status) 3852413SN/A{ 3862413SN/A} 3872413SN/A 3882640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 3892640Sstever@eecs.umich.edu PhysicalMemory *_memory) 3905606Snate@binkert.org : SimpleTimingPort(_name, _memory), memory(_memory) 3912413SN/A{ } 3922413SN/A 3932413SN/Avoid 3942413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 3952413SN/A{ 3962413SN/A memory->recvStatusChange(status); 3972413SN/A} 3982413SN/A 3992413SN/Avoid 4002522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 4014475Sstever@eecs.umich.edu bool &snoop) 4022413SN/A{ 4032522SN/A memory->getAddressRanges(resp, snoop); 4042497SN/A} 4052497SN/A 4062497SN/Avoid 4074475Sstever@eecs.umich.eduPhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop) 4082497SN/A{ 4094475Sstever@eecs.umich.edu snoop = false; 4102522SN/A resp.clear(); 4114762Snate@binkert.org resp.push_back(RangeSize(start(), params()->range.size())); 4122413SN/A} 4132413SN/A 4146227Snate@binkert.orgunsigned 4156227Snate@binkert.orgPhysicalMemory::MemoryPort::deviceBlockSize() const 4162415SN/A{ 4172415SN/A return memory->deviceBlockSize(); 4182415SN/A} 4192413SN/A 4202413SN/ATick 4213349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt) 4222413SN/A{ 4234626Sstever@eecs.umich.edu return memory->doAtomicAccess(pkt); 4242413SN/A} 4252413SN/A 4262413SN/Avoid 4273349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt) 4282413SN/A{ 4295314Sstever@gmail.com pkt->pushLabel(memory->name()); 4305314Sstever@gmail.com 4314929Sstever@gmail.com if (!checkFunctional(pkt)) { 4324929Sstever@gmail.com // Default implementation of SimpleTimingPort::recvFunctional() 4334929Sstever@gmail.com // calls recvAtomic() and throws away the latency; we can save a 4344929Sstever@gmail.com // little here by just not calculating the latency. 4354929Sstever@gmail.com memory->doFunctionalAccess(pkt); 4364929Sstever@gmail.com } 4375314Sstever@gmail.com 4385314Sstever@gmail.com pkt->popLabel(); 4392413SN/A} 4402413SN/A 4412914Ssaidi@eecs.umich.eduunsigned int 4422914Ssaidi@eecs.umich.eduPhysicalMemory::drain(Event *de) 4432914Ssaidi@eecs.umich.edu{ 4444467Sstever@eecs.umich.edu int count = 0; 4454467Sstever@eecs.umich.edu for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 4464467Sstever@eecs.umich.edu count += (*pi)->drain(de); 4474467Sstever@eecs.umich.edu } 4484467Sstever@eecs.umich.edu 4492914Ssaidi@eecs.umich.edu if (count) 4502914Ssaidi@eecs.umich.edu changeState(Draining); 4512914Ssaidi@eecs.umich.edu else 4522914Ssaidi@eecs.umich.edu changeState(Drained); 4532914Ssaidi@eecs.umich.edu return count; 4542914Ssaidi@eecs.umich.edu} 4552413SN/A 4562391SN/Avoid 4572391SN/APhysicalMemory::serialize(ostream &os) 4582391SN/A{ 4595477Snate@binkert.org if (!pmemAddr) 4605477Snate@binkert.org return; 4615477Snate@binkert.org 4622391SN/A gzFile compressedMem; 4632391SN/A string filename = name() + ".physmem"; 4642391SN/A 4652391SN/A SERIALIZE_SCALAR(filename); 4662391SN/A 4672391SN/A // write memory file 4682391SN/A string thefile = Checkpoint::dir() + "/" + filename.c_str(); 4692391SN/A int fd = creat(thefile.c_str(), 0664); 4702391SN/A if (fd < 0) { 4712391SN/A perror("creat"); 4722391SN/A fatal("Can't open physical memory checkpoint file '%s'\n", filename); 4732391SN/A } 4742391SN/A 4752391SN/A compressedMem = gzdopen(fd, "wb"); 4762391SN/A if (compressedMem == NULL) 4772391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 4782391SN/A filename); 4792391SN/A 4804762Snate@binkert.org if (gzwrite(compressedMem, pmemAddr, params()->range.size()) != 4816227Snate@binkert.org (int)params()->range.size()) { 4822391SN/A fatal("Write failed on physical memory checkpoint file '%s'\n", 4832391SN/A filename); 4842391SN/A } 4852391SN/A 4862391SN/A if (gzclose(compressedMem)) 4872391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 4882391SN/A filename); 4892391SN/A} 4902391SN/A 4912391SN/Avoid 4922391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 4932391SN/A{ 4945477Snate@binkert.org if (!pmemAddr) 4955477Snate@binkert.org return; 4965477Snate@binkert.org 4972391SN/A gzFile compressedMem; 4982391SN/A long *tempPage; 4992391SN/A long *pmem_current; 5002391SN/A uint64_t curSize; 5012391SN/A uint32_t bytesRead; 5026227Snate@binkert.org const uint32_t chunkSize = 16384; 5032391SN/A 5042391SN/A string filename; 5052391SN/A 5062391SN/A UNSERIALIZE_SCALAR(filename); 5072391SN/A 5082391SN/A filename = cp->cptDir + "/" + filename; 5092391SN/A 5102391SN/A // mmap memoryfile 5112391SN/A int fd = open(filename.c_str(), O_RDONLY); 5122391SN/A if (fd < 0) { 5132391SN/A perror("open"); 5142391SN/A fatal("Can't open physical memory checkpoint file '%s'", filename); 5152391SN/A } 5162391SN/A 5172391SN/A compressedMem = gzdopen(fd, "rb"); 5182391SN/A if (compressedMem == NULL) 5192391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 5202391SN/A filename); 5212391SN/A 5223012Ssaidi@eecs.umich.edu // unmap file that was mmaped in the constructor 5233012Ssaidi@eecs.umich.edu // This is done here to make sure that gzip and open don't muck with our 5243012Ssaidi@eecs.umich.edu // nice large space of memory before we reallocate it 5254762Snate@binkert.org munmap((char*)pmemAddr, params()->range.size()); 5262391SN/A 5274762Snate@binkert.org pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 5284762Snate@binkert.org PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 5292391SN/A 5303012Ssaidi@eecs.umich.edu if (pmemAddr == (void *)MAP_FAILED) { 5312391SN/A perror("mmap"); 5322391SN/A fatal("Could not mmap physical memory!\n"); 5332391SN/A } 5342391SN/A 5352391SN/A curSize = 0; 5362391SN/A tempPage = (long*)malloc(chunkSize); 5372391SN/A if (tempPage == NULL) 5382391SN/A fatal("Unable to malloc memory to read file %s\n", filename); 5392391SN/A 5402391SN/A /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 5414762Snate@binkert.org while (curSize < params()->range.size()) { 5422391SN/A bytesRead = gzread(compressedMem, tempPage, chunkSize); 5434762Snate@binkert.org if (bytesRead != chunkSize && 5444762Snate@binkert.org bytesRead != params()->range.size() - curSize) 5452391SN/A fatal("Read failed on physical memory checkpoint file '%s'" 5462391SN/A " got %d bytes, expected %d or %d bytes\n", 5474762Snate@binkert.org filename, bytesRead, chunkSize, 5484762Snate@binkert.org params()->range.size() - curSize); 5492391SN/A 5502391SN/A assert(bytesRead % sizeof(long) == 0); 5512391SN/A 5526227Snate@binkert.org for (uint32_t x = 0; x < bytesRead / sizeof(long); x++) 5532391SN/A { 5542391SN/A if (*(tempPage+x) != 0) { 5553012Ssaidi@eecs.umich.edu pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 5562391SN/A *pmem_current = *(tempPage+x); 5572391SN/A } 5582391SN/A } 5592391SN/A curSize += bytesRead; 5602391SN/A } 5612391SN/A 5622391SN/A free(tempPage); 5632391SN/A 5642391SN/A if (gzclose(compressedMem)) 5652391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 5662391SN/A filename); 5672391SN/A 5682391SN/A} 5692391SN/A 5704762Snate@binkert.orgPhysicalMemory * 5714762Snate@binkert.orgPhysicalMemoryParams::create() 5722391SN/A{ 5734762Snate@binkert.org return new PhysicalMemory(this); 5742391SN/A} 575